• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • libtdegames
 

libtdegames

  • libtdegames
  • kgame
kplayer.cpp
1 /*
2  This file is part of the TDE games library
3  Copyright (C) 2001 Martin Heni (martin@heni-online.de)
4  Copyright (C) 2001 Andreas Beckermann (b_mann@gmx.de)
5 
6  This library is free software; you can redistribute it and/or
7  modify it under the terms of the GNU Library General Public
8  License version 2 as published by the Free Software Foundation.
9 
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Library General Public License for more details.
14 
15  You should have received a copy of the GNU Library General Public License
16  along with this library; see the file COPYING.LIB. If not, write to
17  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  Boston, MA 02110-1301, USA.
19 */
20 /*
21  $Id$
22 */
23 
24 
25 #include "kgame.h"
26 #include "kgameio.h"
27 #include "kplayer.h"
28 #include "kgamemessage.h"
29 #include "kgamepropertyhandler.h"
30 
31 #include <kdebug.h>
32 #include <tdelocale.h>
33 
34 #include <tqbuffer.h>
35 
36 #include <stdio.h>
37 #include <assert.h>
38 
39 #define KPLAYER_LOAD_COOKIE 7285
40 
41 class KPlayerPrivate
42 {
43 public:
44  KPlayerPrivate()
45  {
46  mNetworkPlayer = 0;
47  }
48 
49  TQ_UINT32 mId;
50  bool mVirtual; // virtual player
51  int mPriority; // tag for replacement
52 
53  KPlayer* mNetworkPlayer; // replacement player
54 
55  KGamePropertyHandler mProperties;
56 
57 // Playerdata
58  KGamePropertyTQString mName;
59  KGamePropertyTQString mGroup;
60 };
61 
62 KPlayer::KPlayer() : TQObject(0,0)
63 {
64  init();
65 }
66 
67 KPlayer::KPlayer(KGame* game) : TQObject(0, 0)
68 {
69  init();
70  game->addPlayer(this);
71 }
72 
73 void KPlayer::init()
74 {
75 // note that NO KGame object exists here! so we cannot use KGameProperty::send!
76  kdDebug(11001) << k_funcinfo << ": this=" << this << ", sizeof(this)="<<sizeof(KPlayer) << endl;
77  kdDebug(11001) << "sizeof(m_Group)="<<sizeof(d->mGroup)<<endl;
78  d = new KPlayerPrivate;
79 
80  d->mProperties.registerHandler(KGameMessage::IdPlayerProperty,
81  this,TQ_SLOT(sendProperty(int, TQDataStream&, bool*)),
82  TQ_SLOT(emitSignal(KGamePropertyBase *)));
83  d->mVirtual=false;
84  mActive=true;
85  mGame=0;
86  d->mId=0; // "0" is always an invalid ID!
87  d->mPriority=0;
88  // I guess we cannot translate the group otherwise no
89  // international conenctions are possible
90 
91  mUserId.registerData(KGamePropertyBase::IdUserId, this, i18n("UserId"));
92  mUserId.setLocal(0);
93  d->mGroup.registerData(KGamePropertyBase::IdGroup, this, i18n("Group"));
94  d->mGroup.setLocal(i18n("default"));
95  d->mName.registerData(KGamePropertyBase::IdName, this, i18n("Name"));
96  d->mName.setLocal(i18n("default"));
97 
98  mAsyncInput.registerData(KGamePropertyBase::IdAsyncInput, this, i18n("AsyncInput"));
99  mAsyncInput.setLocal(false);
100  mMyTurn.registerData(KGamePropertyBase::IdTurn, this, i18n("myTurn"));
101  mMyTurn.setLocal(false);
102  mMyTurn.setEmittingSignal(true);
103  mMyTurn.setOptimized(false);
104 }
105 
106 KPlayer::~KPlayer()
107 {
108  kdDebug(11001) << k_funcinfo << ": this=" << this <<", id=" << this->id() << endl;
109 
110  // Delete IODevices
111  KGameIO *input;
112  while((input=mInputList.first()))
113  {
114  delete input;
115  }
116  if (game())
117  {
118  game()->playerDeleted(this);
119  }
120 
121 // note: mProperties does not use autoDelete or so - user must delete objects
122 // himself
123  d->mProperties.clear();
124  delete d;
125 // kdDebug(11001) << k_funcinfo << " done" << endl;
126 }
127 
128 bool KPlayer::forwardMessage(TQDataStream &msg,int msgid,TQ_UINT32 receiver,TQ_UINT32 sender)
129 {
130  if (!isActive())
131  {
132  return false;
133  }
134  if (!game())
135  {
136  return false;
137  }
138  kdDebug(11001) << k_funcinfo << ": to game sender="<<sender<<"" << "recv="<<receiver <<"msgid="<<msgid << endl;
139  return game()->sendSystemMessage(msg,msgid,receiver,sender);
140 }
141 
142 bool KPlayer::forwardInput(TQDataStream &msg,bool transmit,TQ_UINT32 sender)
143 {
144  if (!isActive())
145  {
146  return false;
147  }
148  if (!game())
149  {
150  return false;
151  }
152 
153  kdDebug(11001) << k_funcinfo << ": to game playerInput(sender="<<sender<<")" << endl;
154  if (!asyncInput() && !myTurn())
155  {
156  kdDebug(11001) << k_funcinfo << ": rejected cause it is not our turn" << endl;
157  return false;
158  }
159 
160  // AB: I hope I remember the usage correctly:
161  // this function is called twice (on sender side) - once with transmit = true
162  // where it sends the input to the comserver and once with transmit = false
163  // where it really looks at the input
164  if (transmit)
165  {
166  kdDebug(11001) << "indirect playerInput" << endl;
167  return game()->sendPlayerInput(msg,this,sender);
168  }
169  else
170  {
171  kdDebug(11001) << "direct playerInput" << endl;
172  return game()->systemPlayerInput(msg,this,sender);
173  }
174 }
175 
176 void KPlayer::setId(TQ_UINT32 newid)
177 {
178  // Needs to be after the sendProcess
179  d->mId=newid;
180 }
181 
182 
183 void KPlayer::setGroup(const TQString& group)
184 { d->mGroup = group; }
185 
186 const TQString& KPlayer::group() const
187 { return d->mGroup.value(); }
188 
189 void KPlayer::setName(const TQString& name)
190 { d->mName = name; }
191 
192 const TQString& KPlayer::name() const
193 { return d->mName.value(); }
194 
195 TQ_UINT32 KPlayer::id() const
196 { return d->mId; }
197 
198 KGamePropertyHandler * KPlayer::dataHandler()
199 { return &d->mProperties; }
200 
201 void KPlayer::setVirtual(bool v)
202 { d->mVirtual = v; }
203 
204 bool KPlayer::isVirtual() const
205 { return d->mVirtual;}
206 
207 void KPlayer::setNetworkPlayer(KPlayer* p)
208 { d->mNetworkPlayer = p; }
209 
210 KPlayer* KPlayer::networkPlayer() const
211 { return d->mNetworkPlayer; }
212 
213 int KPlayer::networkPriority() const
214 { return d->mPriority; }
215 
216 void KPlayer::setNetworkPriority(int p)
217 { d->mPriority = p; }
218 
219 bool KPlayer::addGameIO(KGameIO *input)
220 {
221  if (!input)
222  {
223  return false;
224  }
225  mInputList.append(input);
226  input->initIO(this); // set player and init device
227  return true;
228 }
229 
230 // input=0, remove all
231 bool KPlayer::removeGameIO(KGameIO *targetinput,bool deleteit)
232 {
233  kdDebug(11001) << k_funcinfo << ": " << targetinput << " delete=" << deleteit<< endl;
234  bool result=true;
235  if (!targetinput) // delete all
236  {
237  KGameIO *input;
238  while((input=mInputList.first()))
239  {
240  if (input) removeGameIO(input,deleteit);
241  }
242  }
243  else
244  {
245 // kdDebug(11001) << "remove IO " << targetinput << endl;
246  if (deleteit)
247  {
248  delete targetinput;
249  }
250  else
251  {
252  targetinput->setPlayer(0);
253  result=mInputList.remove(targetinput);
254  }
255  }
256  return result;
257 }
258 
259 KGameIO * KPlayer::findRttiIO(int rtti) const
260 {
261  TQPtrListIterator<KGameIO> it(mInputList);
262  while (it.current())
263  {
264  if (it.current()->rtti() == rtti)
265  {
266  return it.current();
267  }
268  ++it;
269  }
270  return 0;
271 }
272 
273 int KPlayer::calcIOValue()
274 {
275  int value=0;
276  TQPtrListIterator<KGameIO> it(mInputList);
277  while (it.current())
278  {
279  value|=it.current()->rtti();
280  ++it;
281  }
282  return value;
283 }
284 
285 bool KPlayer::setTurn(bool b, bool exclusive)
286 {
287  kdDebug(11001) << k_funcinfo << ": " << id() << " (" << this << ") to " << b << endl;
288  if (!isActive())
289  {
290  return false;
291  }
292 
293  // if we get to do an exclusive turn all other players are disallowed
294  if (exclusive && b && game())
295  {
296  KPlayer *player;
297  KGame::KGamePlayerList *list=game()->playerList();
298  for ( player=list->first(); player != 0; player=list->next() )
299  {
300  if (player==this)
301  {
302  continue;
303  }
304  player->setTurn(false,false);
305  }
306  }
307 
308  // Return if nothing changed
309  mMyTurn = b;
310 
311  return true;
312 }
313 
314 bool KPlayer::load(TQDataStream &stream)
315 {
316  TQ_INT32 id,priority;
317  stream >> id >> priority;
318  setId(id);
319  setNetworkPriority(priority);
320 
321  // Load Player Data
322  //FIXME: maybe set all properties setEmitSignal(false) before?
323  d->mProperties.load(stream);
324 
325  TQ_INT16 cookie;
326  stream >> cookie;
327  if (cookie==KPLAYER_LOAD_COOKIE)
328  {
329  kdDebug(11001) << " Player loaded propertly"<<endl;
330  }
331  else
332  {
333  kdError(11001) << " Player loading error. probably format error"<<endl;
334  }
335 
336  // emit signalLoad(stream);
337  return true;
338 }
339 
340 bool KPlayer::save(TQDataStream &stream)
341 {
342  stream << (TQ_INT32)id() << (TQ_INT32)networkPriority();
343 
344  d->mProperties.save(stream);
345 
346  stream << (TQ_INT16)KPLAYER_LOAD_COOKIE;
347 
348  //emit signalSave(stream);
349  return true;
350 }
351 
352 
353 void KPlayer::networkTransmission(TQDataStream &stream,int msgid,TQ_UINT32 sender)
354 {
355  //kdDebug(11001) << k_funcinfo ": msgid=" << msgid << " sender=" << sender << " we are=" << id() << endl;
356  // PlayerProperties processed
357  bool issender;
358  if (game())
359  {
360  issender=sender==game()->gameId();
361  }
362  else
363  {
364  issender=true;
365  }
366  if (d->mProperties.processMessage(stream,msgid,issender))
367  {
368  return ;
369  }
370  switch(msgid)
371  {
372  case KGameMessage::IdPlayerInput:
373  {
374  kdDebug(11001) << k_funcinfo << ": Got player move "
375  << "KPlayer (virtual) forwards it to the game object" << endl;
376  forwardInput(stream,false);
377  }
378  break;
379  default:
380  emit signalNetworkData(msgid - KGameMessage::IdUser,
381  ((TQBuffer*)stream.device())->readAll(),sender,this);
382  kdDebug(11001) << k_funcinfo << ": "
383  << "User data msgid " << msgid << endl;
384  break;
385  }
386 
387 }
388 
389 KGamePropertyBase* KPlayer::findProperty(int id) const
390 {
391  return d->mProperties.find(id);
392 }
393 
394 bool KPlayer::addProperty(KGamePropertyBase* data)
395 {
396  return d->mProperties.addProperty(data);
397 }
398 
399 void KPlayer::sendProperty(int msgid, TQDataStream& stream, bool* sent)
400 {
401  if (game())
402  {
403  bool s = game()->sendPlayerProperty(msgid, stream, id());
404  if (s)
405  {
406  *sent = true;
407  }
408  }
409 }
410 
411 void KPlayer::emitSignal(KGamePropertyBase *me)
412 {
413  // Notify KGameIO (Process) for a new turn
414  if (me->id()==KGamePropertyBase::IdTurn)
415  {
416  //kdDebug(11001) << k_funcinfo << ": for KGamePropertyBase::IdTurn " << endl;
417  TQPtrListIterator<KGameIO> it(mInputList);
418  while (it.current())
419  {
420  it.current()->notifyTurn(mMyTurn.value());
421  ++it;
422  }
423  }
424  emit signalPropertyChanged(me,this);
425 }
426 
427 // --------------------- DEBUG --------------------
428 void KPlayer::Debug()
429 {
430  kdDebug(11001) << "------------------- KPLAYER -----------------------" << endl;
431  kdDebug(11001) << "this: " << this << endl;
432  kdDebug(11001) << "rtti: " << rtti() << endl;
433  kdDebug(11001) << "id : " << id() << endl;
434  kdDebug(11001) << "Name : " << name() << endl;
435  kdDebug(11001) << "Group: " << group() << endl;
436  kdDebug(11001) << "Async: " << asyncInput() << endl;
437  kdDebug(11001) << "myTurn: " << myTurn() << endl;
438  kdDebug(11001) << "Virtual: " << isVirtual() << endl;
439  kdDebug(11001) << "Active: " << isActive() << endl;
440  kdDebug(11001) << "Priority:" << networkPriority() << endl;
441  kdDebug(11001) << "Game : " << game() << endl;
442  kdDebug(11001) << "#IOs: " << mInputList.count() << endl;
443  kdDebug(11001) << "---------------------------------------------------" << endl;
444 }
445 
446 #include "kplayer.moc"
KPlayer::setNetworkPriority
void setNetworkPriority(int b)
Set whether this player can be replaced by a network player.
Definition: kplayer.cpp:216
KGamePropertyBase::setEmittingSignal
void setEmittingSignal(bool p)
Sets this property to emit a signal on value changed.
Definition: kgameproperty.h:157
KPlayer::game
KGame * game() const
Query to which game the player belongs to.
Definition: kplayer.h:128
KPlayer::rtti
virtual int rtti() const
The idendification of the player.
Definition: kplayer.h:99
KPlayer::addProperty
bool addProperty(KGamePropertyBase *data)
Adds a property to a player.
Definition: kplayer.cpp:394
KPlayer::networkPlayer
KPlayer * networkPlayer() const
Returns the player which got inactivated to allow this player to be set up via network.
Definition: kplayer.cpp:210
KGameProperty::setLocal
bool setLocal(type v)
This function sets the value of the property directly, i.e.
Definition: kgameproperty.h:715
KPlayer::findRttiIO
KGameIO * findRttiIO(int rtti) const
Finds the KGameIO devies with the given rtti code.
Definition: kplayer.cpp:259
KPlayer::setTurn
bool setTurn(bool b, bool exclusive=true)
Sets whether this player is the next to turn.
Definition: kplayer.cpp:285
KPlayer::calcIOValue
int calcIOValue()
Calculates a checksum over the IO devices.
Definition: kplayer.cpp:273
KGamePropertyHandler
A collection class for KGameProperty objects.
Definition: kgamepropertyhandler.h:72
KGamePropertyBase::setOptimized
void setOptimized(bool p)
Sets this property to try to optimize signal and network handling by not sending it out when the prop...
Definition: kgameproperty.h:169
KGameNetwork::gameId
TQ_UINT32 gameId() const
The unique ID of this game.
Definition: kgamenetwork.cpp:86
KGame::sendPlayerProperty
bool sendPlayerProperty(int msgid, TQDataStream &s, TQ_UINT32 playerId)
This is called by KPlayer::sendProperty only! Internal function!
Definition: kgame.cpp:1428
KPlayer::setGroup
void setGroup(const TQString &group)
A group the player belongs to.
Definition: kplayer.cpp:183
KGamePropertyBase::id
int id() const
Definition: kgameproperty.h:238
KPlayer::Debug
void Debug()
Gives debug output of the game status.
Definition: kplayer.cpp:428
KPlayer::emitSignal
void emitSignal(KGamePropertyBase *me)
Called by KGameProperty only! Internal function!
Definition: kplayer.cpp:411
KPlayer::signalNetworkData
void signalNetworkData(int msgid, const TQByteArray &buffer, TQ_UINT32 sender, KPlayer *me)
The player object got a message which was targeted at it but has no default method to process it...
KPlayer::forwardInput
virtual bool forwardInput(TQDataStream &msg, bool transmit=true, TQ_UINT32 sender=0)
Forwards input to the game object..internal use only.
Definition: kplayer.cpp:142
KGameNetwork::sendSystemMessage
bool sendSystemMessage(const TQByteArray &buffer, int msgid, TQ_UINT32 receiver=0, TQ_UINT32 sender=0)
Sends a network message msg with a given msg id msgid to all clients.
Definition: kgamenetwork.cpp:396
KPlayer::signalPropertyChanged
void signalPropertyChanged(KGamePropertyBase *property, KPlayer *me)
This signal is emmited if a player property changes its value and the property is set to notify this ...
KGame::addPlayer
void addPlayer(KPlayer *newplayer)
Note that KPlayer::save must be implemented properly, as well as KPlayer::rtti This will only send a ...
Definition: kgame.cpp:412
KGameIO::setPlayer
void setPlayer(KPlayer *p)
Sets the player to which this IO belongs to.
Definition: kgameio.h:105
KGameProperty::value
const type & value() const
Definition: kgameproperty.h:764
KPlayer::group
virtual const TQString & group() const
Query the group the player belongs to.
Definition: kplayer.cpp:186
KPlayer::addGameIO
bool addGameIO(KGameIO *input)
Adds an IO device for the player.
Definition: kplayer.cpp:219
KPlayer::name
virtual const TQString & name() const
Definition: kplayer.cpp:192
KPlayer::id
TQ_UINT32 id() const
Returns the id of the player.
Definition: kplayer.cpp:195
KPlayer::sendProperty
void sendProperty(int msgid, TQDataStream &stream, bool *sent)
Called by KGameProperty only! Internal function!
Definition: kplayer.cpp:399
KPlayer::isVirtual
bool isVirtual() const
Is this player a virtual player, ie is it created by mirroring a real player from another network gam...
Definition: kplayer.cpp:204
KGameIO::initIO
virtual void initIO(KPlayer *p)
Init this device by setting the player and e.g.
Definition: kgameio.cpp:66
KPlayer::asyncInput
bool asyncInput() const
Query whether this player does asynchronous input.
Definition: kplayer.h:145
KPlayer
Base class for a game player.
Definition: kplayer.h:69
KPlayer::setName
void setName(const TQString &name)
Sets the name of the player.
Definition: kplayer.cpp:189
KGamePropertyBase
Base class of KGameProperty.
Definition: kgameproperty.h:42
KGame::sendPlayerInput
virtual bool sendPlayerInput(TQDataStream &msg, KPlayer *player, TQ_UINT32 sender=0)
Called by KPlayer to send a player input to the KMessageServer.
Definition: kgame.cpp:709
KPlayer::findProperty
KGamePropertyBase * findProperty(int id) const
Searches for a property of the player given its id.
Definition: kplayer.cpp:389
KPlayer::forwardMessage
virtual bool forwardMessage(TQDataStream &msg, int msgid, TQ_UINT32 receiver=0, TQ_UINT32 sender=0)
Forwards Message to the game object..internal use only.
Definition: kplayer.cpp:128
KGame::systemPlayerInput
virtual bool systemPlayerInput(TQDataStream &msg, KPlayer *player, TQ_UINT32 sender=0)
Called when a player input arrives from KMessageServer.
Definition: kgame.cpp:727
KPlayer::isActive
bool isActive() const
Is this player an active player.
Definition: kplayer.h:174
KPlayer::load
virtual bool load(TQDataStream &stream)
Load a saved player, from file OR network.
Definition: kplayer.cpp:314
KPlayer::setNetworkPlayer
void setNetworkPlayer(KPlayer *p)
Sets this network player replacement.
Definition: kplayer.cpp:207
KGamePropertyBase::registerData
int registerData(int id, KGamePropertyHandler *owner, PropertyPolicy p, TQString name=0)
You have to register a KGamePropertyBase before you can use it.
Definition: kgameproperty.cpp:91
KGame
The main KDE game object.
Definition: kgame.h:62
KPlayer::dataHandler
KGamePropertyHandler * dataHandler()
Definition: kplayer.cpp:198
KPlayer::networkTransmission
void networkTransmission(TQDataStream &stream, int msgid, TQ_UINT32 sender)
Receives a message.
Definition: kplayer.cpp:353
KGame::playerList
KGamePlayerList * playerList()
Returns a list of all active players.
Definition: kgame.cpp:699
KPlayer::removeGameIO
bool removeGameIO(KGameIO *input=0, bool deleteit=true)
remove (and delete) a game IO device
Definition: kplayer.cpp:231
KPlayer::KPlayer
KPlayer()
Create a new player object.
Definition: kplayer.cpp:62
KGameIO
Base class for IO devices for games.
Definition: kgameio.h:55
KPlayer::networkPriority
int networkPriority() const
Returns whether this player can be replaced by a network connection player.
Definition: kplayer.cpp:213
KGameProperty< TQString >
KGame::playerDeleted
void playerDeleted(KPlayer *player)
Called by the destructor of KPlayer to remove itself from the game.
Definition: kgame.cpp:492
KPlayer::save
virtual bool save(TQDataStream &stream)
Save a player to a file OR to network.
Definition: kplayer.cpp:340
KPlayer::myTurn
bool myTurn() const
is it my turn to go
Definition: kplayer.h:350

libtdegames

Skip menu "libtdegames"
  • Main Page
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Class Members
  • Related Pages

libtdegames

Skip menu "libtdegames"
  • libtdegames
Generated for libtdegames by doxygen 1.8.13
This website is maintained by Timothy Pearson.