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