kgame.h
00001 /* 00002 This file is part of the KDE games library 00003 Copyright (C) 2001 Martin Heni (martin@heni-online.de) 00004 Copyright (C) 2001 Andreas Beckermann (b_mann@gmx.de) 00005 00006 This library is free software; you can redistribute it and/or 00007 modify it under the terms of the GNU Library General Public 00008 License version 2 as published by the Free Software Foundation. 00009 00010 This library is distributed in the hope that it will be useful, 00011 but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00013 Library General Public License for more details. 00014 00015 You should have received a copy of the GNU Library General Public License 00016 along with this library; see the file COPYING.LIB. If not, write to 00017 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00018 Boston, MA 02110-1301, USA. 00019 */ 00020 /* 00021 $Id$ 00022 */ 00023 #ifndef __KGAME_H_ 00024 #define __KGAME_H_ 00025 00026 #include <tqstring.h> 00027 #include <tqptrlist.h> 00028 #include <tqvaluelist.h> 00029 00030 #include "kgamenetwork.h" 00031 #include <kdemacros.h> 00032 class KRandomSequence; 00033 00034 class KPlayer; 00035 class KGamePropertyBase; 00036 class KGamePropertyHandler; 00037 class KGameSequence; 00038 00039 class KGamePrivate; 00040 00062 class KDE_EXPORT KGame : public KGameNetwork 00063 { 00064 Q_OBJECT 00065 TQ_OBJECT 00066 00067 public: 00068 typedef TQPtrList<KPlayer> KGamePlayerList; 00069 00091 enum GamePolicy 00092 { 00093 PolicyUndefined = 0, 00094 PolicyClean = 1, 00095 PolicyDirty = 2, 00096 PolicyLocal = 3 00097 }; 00098 00104 KGame(int cookie=42,TQObject* parent=0); 00105 00109 virtual ~KGame(); 00110 00114 virtual void Debug(); 00115 00121 enum GameStatus 00122 { 00123 Init = 0, 00124 Run = 1, 00125 Pause = 2, 00126 End = 3, 00127 Abort = 4, 00128 SystemPause = 5, 00129 Intro = 6, 00130 UserStatus = 7 00131 }; 00132 00133 // Properties 00139 KGamePlayerList *playerList(); 00140 00144 const KGamePlayerList *playerList() const; 00145 00150 KGamePlayerList *inactivePlayerList(); 00151 00155 const KGamePlayerList *inactivePlayerList() const; 00156 00162 KRandomSequence *random() const; 00163 00168 KGameSequence *gameSequence() const; 00169 00174 bool isRunning() const; 00175 00176 // Player handling 00182 KPlayer *findPlayer(TQ_UINT32 id) const; 00183 00193 void setGameSequence(KGameSequence* sequence); 00194 00212 void addPlayer(KPlayer* newplayer); 00213 00220 //AB: TODO: make sendMessage to return if the message will be able to be 00221 //sent, eg if a socket is connected, etc. If sendMessage returns false 00222 //remove the player directly using systemRemovePlayer 00223 bool removePlayer(KPlayer * player) { return removePlayer(player, 0); } 00224 00229 void playerDeleted(KPlayer * player); 00230 00234 bool activatePlayer(KPlayer *player); 00235 00239 bool inactivatePlayer(KPlayer *player); 00240 00247 void setMaxPlayers(uint maxnumber); 00248 00253 int maxPlayers() const; 00254 00261 void setMinPlayers(uint minnumber); 00262 00267 uint minPlayers() const; 00268 00273 uint playerCount() const; 00274 00279 virtual KPlayer * nextPlayer(KPlayer *last,bool exclusive=true); 00280 00281 // Input events 00286 virtual bool sendPlayerInput(TQDataStream &msg,KPlayer *player,TQ_UINT32 sender=0); 00287 00298 virtual bool systemPlayerInput(TQDataStream &msg,KPlayer *player,TQ_UINT32 sender=0); 00299 00326 virtual KPlayer *createPlayer(int rtti,int io,bool isvirtual); 00327 00328 // load/save 00341 virtual bool load(TQDataStream &stream,bool reset=true); 00342 00351 virtual bool load(TQString filename,bool reset=true); 00352 00362 virtual bool save(TQDataStream &stream,bool saveplayers=true); 00363 00372 virtual bool save(TQString filename,bool saveplayers=true); 00373 00381 virtual bool reset(); 00382 00383 00384 // Game sequence 00390 int gameStatus() const; 00391 00397 void setGameStatus(int status); 00398 00402 bool addProperty(KGamePropertyBase* data); 00403 00407 bool sendPlayerProperty(int msgid, TQDataStream& s, TQ_UINT32 playerId); 00408 00413 KGamePropertyBase* findProperty(int id) const; 00414 00421 void setPolicy(GamePolicy p,bool recursive=true); 00422 00426 GamePolicy policy() const; 00427 00439 bool sendGroupMessage(const TQByteArray& msg, int msgid, TQ_UINT32 sender, const TQString& group); 00440 bool sendGroupMessage(const TQDataStream &msg, int msgid, TQ_UINT32 sender, const TQString& group); 00441 bool sendGroupMessage(int msg, int msgid, TQ_UINT32 sender, const TQString& group); 00442 bool sendGroupMessage(const TQString& msg, int msgid, TQ_UINT32 sender, const TQString& group); 00443 00462 virtual void networkTransmission(TQDataStream &stream, int msgid, TQ_UINT32 receiver, TQ_UINT32 sender, TQ_UINT32 clientID); 00463 00467 KGamePropertyHandler* dataHandler() const; 00468 00469 protected slots: 00473 void sendProperty(int msgid, TQDataStream& stream, bool* sent); 00474 00478 void emitSignal(KGamePropertyBase *me); 00479 00484 virtual void prepareNext(); 00485 00486 00491 void slotClientConnected(TQ_UINT32 clientId); 00492 00500 void slotClientDisconnected(TQ_UINT32 clientId,bool broken); 00501 00507 void slotServerDisconnected(); 00508 00509 signals: 00527 void signalReplacePlayerIO(KPlayer* player, bool* remove); 00528 00543 void signalLoadPrePlayers(TQDataStream &stream); 00544 00552 void signalLoad(TQDataStream &stream); 00553 00567 void signalSavePrePlayers(TQDataStream &stream); 00568 00576 void signalSave(TQDataStream &stream); 00577 00589 void signalLoadError(TQDataStream &stream,bool network,int cookie, bool &result); 00590 00597 void signalNetworkData(int msgid,const TQByteArray& buffer, TQ_UINT32 receiver, TQ_UINT32 sender); 00598 00604 void signalMessageUpdate(int msgid,TQ_UINT32 receiver,TQ_UINT32 sender); 00605 00616 void signalPlayerLeftGame(KPlayer *player); 00617 00623 void signalPlayerJoinedGame(KPlayer *player); 00624 00625 00630 void signalPropertyChanged(KGamePropertyBase *property, KGame *me); 00631 00640 void signalGameOver(int status, KPlayer *current, KGame *me); 00641 00653 void signalClientJoinedGame(TQ_UINT32 clientid,KGame *me); 00654 00672 void signalClientLeftGame(int clientID,int oldgamestatus,KGame *me); 00673 00674 00675 protected: 00704 virtual bool playerInput(TQDataStream &msg,KPlayer *player)=0; 00705 00706 00718 KPlayer *playerInputFinished(KPlayer *player); 00719 00720 00754 virtual void newPlayersJoin(KGamePlayerList *oldplayer, 00755 KGamePlayerList *newplayer, 00756 TQValueList<int> &inactivate) { 00757 Q_UNUSED( oldplayer ); 00758 Q_UNUSED( newplayer ); 00759 Q_UNUSED( inactivate ); 00760 } 00761 00770 void savePlayers(TQDataStream &stream,KGamePlayerList *list=0); 00771 00782 void savePlayer(TQDataStream& stream,KPlayer* player); 00783 00792 KPlayer *loadPlayer(TQDataStream& stream,bool isvirtual=false); 00793 00794 00798 bool systemInactivatePlayer(KPlayer *player); 00799 00803 bool systemActivatePlayer(KPlayer *player); 00804 00822 void systemAddPlayer(KPlayer* newplayer); 00823 00831 void systemRemovePlayer(KPlayer* player,bool deleteit); 00832 00843 virtual void negotiateNetworkGame(TQ_UINT32 clientID); 00844 00849 void syncRandom(); 00850 00851 void deletePlayers(); 00852 void deleteInactivePlayers(); 00853 00861 virtual int checkGameOver(KPlayer *player); 00862 00876 virtual bool loadgame(TQDataStream &stream, bool network, bool reset); 00877 00887 virtual bool savegame(TQDataStream &stream, bool network,bool saveplayers); 00888 00889 private: 00890 //AB: this is to hide the "receiver" parameter from the user. It shouldn't be 00891 //used if possible (except for init). 00900 //void addPlayer(KPlayer* newplayer, TQ_UINT32 receiver); 00901 00907 bool removePlayer(KPlayer * player, TQ_UINT32 receiver); 00908 00912 void setupGame(TQ_UINT32 sender); 00913 00917 void setupGameContinue(TQDataStream& msg, TQ_UINT32 sender); 00918 00926 bool systemRemove(KPlayer* player,bool deleteit); 00927 00928 00929 private: 00930 KGamePrivate* d; 00931 }; 00932 00933 #endif