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: kgame.h 465369 2005-09-29 14:33:08Z mueller $
00022 */
00023 #ifndef __KGAME_H_
00024 #define __KGAME_H_
00025 
00026 #include <qstring.h>
00027 #include <qptrlist.h>
00028 #include <qvaluelist.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 
00066 public:
00067     typedef QPtrList<KPlayer> KGamePlayerList;
00068 
00090     enum GamePolicy
00091     {
00092         PolicyUndefined = 0,
00093         PolicyClean = 1,
00094         PolicyDirty = 2,
00095         PolicyLocal = 3
00096     };
00097 
00103     KGame(int cookie=42,QObject* parent=0);
00104 
00108     virtual ~KGame();
00109 
00113     virtual void Debug();
00114 
00120     enum GameStatus
00121     {
00122         Init = 0,
00123         Run = 1,
00124         Pause = 2,
00125         End = 3,
00126         Abort = 4,
00127         SystemPause = 5,
00128         Intro = 6,
00129         UserStatus = 7
00130     };
00131 
00132     // Properties
00138     KGamePlayerList *playerList();
00139 
00143     const KGamePlayerList *playerList() const;
00144 
00149     KGamePlayerList *inactivePlayerList();
00150 
00154     const KGamePlayerList *inactivePlayerList() const;
00155 
00161     KRandomSequence *random() const;
00162 
00167     KGameSequence *gameSequence() const;
00168 
00173     bool isRunning() const;
00174 
00175     // Player handling
00181     KPlayer *findPlayer(Q_UINT32 id) const;
00182 
00192     void setGameSequence(KGameSequence* sequence);
00193 
00211     void addPlayer(KPlayer* newplayer);
00212 
00219     //AB: TODO: make sendMessage to return if the message will be able to be
00220     //sent, eg if a socket is connected, etc. If sendMessage returns false
00221     //remove the player directly using systemRemovePlayer
00222     bool removePlayer(KPlayer * player) { return removePlayer(player, 0); }
00223 
00228     void playerDeleted(KPlayer * player);
00229 
00233     bool activatePlayer(KPlayer *player);
00234 
00238     bool inactivatePlayer(KPlayer *player);
00239 
00246     void setMaxPlayers(uint maxnumber);
00247 
00252     int maxPlayers() const;
00253 
00260     void setMinPlayers(uint minnumber);
00261 
00266     uint minPlayers() const;
00267 
00272     uint playerCount() const;
00273 
00278     virtual KPlayer * nextPlayer(KPlayer *last,bool exclusive=true);
00279 
00280     // Input events
00285     virtual bool sendPlayerInput(QDataStream &msg,KPlayer *player,Q_UINT32 sender=0);
00286 
00297     virtual bool systemPlayerInput(QDataStream &msg,KPlayer *player,Q_UINT32 sender=0);
00298 
00325     virtual KPlayer *createPlayer(int rtti,int io,bool isvirtual);
00326 
00327     // load/save
00340     virtual bool load(QDataStream &stream,bool reset=true);
00341 
00350     virtual bool load(QString filename,bool reset=true);
00351 
00361     virtual bool save(QDataStream &stream,bool saveplayers=true);
00362 
00371     virtual bool save(QString filename,bool saveplayers=true);
00372 
00380     virtual bool reset();
00381 
00382 
00383     // Game sequence
00389     int gameStatus() const;
00390 
00396     void setGameStatus(int status);
00397 
00401     bool addProperty(KGamePropertyBase* data);
00402 
00406     bool sendPlayerProperty(int msgid, QDataStream& s, Q_UINT32 playerId);
00407 
00412     KGamePropertyBase* findProperty(int id) const;
00413 
00420     void setPolicy(GamePolicy p,bool recursive=true);
00421 
00425     GamePolicy policy() const;
00426 
00438     bool sendGroupMessage(const QByteArray& msg, int msgid, Q_UINT32 sender, const QString& group);
00439     bool sendGroupMessage(const QDataStream &msg, int msgid, Q_UINT32 sender, const QString& group);
00440     bool sendGroupMessage(int msg, int msgid, Q_UINT32 sender, const QString& group);
00441     bool sendGroupMessage(const QString& msg, int msgid, Q_UINT32 sender, const QString& group);
00442 
00461     virtual void networkTransmission(QDataStream &stream, int msgid, Q_UINT32 receiver, Q_UINT32 sender, Q_UINT32 clientID);
00462 
00466     KGamePropertyHandler* dataHandler() const;
00467 
00468 protected slots:
00472     void sendProperty(int msgid, QDataStream& stream, bool* sent);
00473 
00477     void emitSignal(KGamePropertyBase *me);
00478 
00483     virtual void prepareNext();
00484 
00485 
00490     void slotClientConnected(Q_UINT32 clientId);
00491 
00499     void slotClientDisconnected(Q_UINT32 clientId,bool broken);
00500 
00506     void slotServerDisconnected();
00507 
00508 signals:
00526     void signalReplacePlayerIO(KPlayer* player, bool* remove);
00527 
00542     void signalLoadPrePlayers(QDataStream &stream);
00543 
00551     void signalLoad(QDataStream &stream);
00552 
00566     void signalSavePrePlayers(QDataStream &stream);
00567     
00575     void signalSave(QDataStream &stream);
00576 
00588     void signalLoadError(QDataStream &stream,bool network,int cookie, bool &result);
00589 
00596     void signalNetworkData(int msgid,const QByteArray& buffer, Q_UINT32 receiver, Q_UINT32 sender);
00597 
00603     void signalMessageUpdate(int msgid,Q_UINT32 receiver,Q_UINT32 sender);
00604 
00615     void signalPlayerLeftGame(KPlayer *player);
00616 
00622     void signalPlayerJoinedGame(KPlayer *player);
00623 
00624 
00629     void signalPropertyChanged(KGamePropertyBase *property, KGame *me);
00630 
00639     void signalGameOver(int status, KPlayer *current, KGame *me);
00640 
00652     void signalClientJoinedGame(Q_UINT32 clientid,KGame *me);
00653 
00671     void signalClientLeftGame(int clientID,int oldgamestatus,KGame *me);
00672 
00673 
00674 protected:
00703     virtual bool playerInput(QDataStream &msg,KPlayer *player)=0;
00704 
00705 
00717     KPlayer *playerInputFinished(KPlayer *player);
00718 
00719 
00753     virtual void newPlayersJoin(KGamePlayerList *oldplayer,
00754                 KGamePlayerList *newplayer,
00755                 QValueList<int> &inactivate) {
00756         Q_UNUSED( oldplayer );
00757         Q_UNUSED( newplayer );
00758         Q_UNUSED( inactivate );
00759     };
00760 
00769     void savePlayers(QDataStream &stream,KGamePlayerList *list=0);
00770 
00781     void savePlayer(QDataStream& stream,KPlayer* player);
00782 
00791     KPlayer *loadPlayer(QDataStream& stream,bool isvirtual=false);
00792 
00793 
00797     bool systemInactivatePlayer(KPlayer *player);
00798 
00802     bool systemActivatePlayer(KPlayer *player);
00803 
00821     void systemAddPlayer(KPlayer* newplayer);
00822 
00830     void systemRemovePlayer(KPlayer* player,bool deleteit);
00831 
00842     virtual void negotiateNetworkGame(Q_UINT32 clientID);
00843 
00848     void syncRandom();
00849 
00850     void deletePlayers();
00851     void deleteInactivePlayers();
00852 
00860     virtual int checkGameOver(KPlayer *player);
00861 
00875     virtual bool loadgame(QDataStream &stream, bool network, bool reset);
00876 
00886     virtual bool savegame(QDataStream &stream, bool network,bool saveplayers);
00887 
00888 private:
00889     //AB: this is to hide the "receiver" parameter from the user. It shouldn't be
00890     //used if possible (except for init).
00899     //void addPlayer(KPlayer* newplayer, Q_UINT32 receiver);
00900 
00906     bool removePlayer(KPlayer * player, Q_UINT32 receiver);
00907 
00911     void setupGame(Q_UINT32 sender);
00912 
00916     void setupGameContinue(QDataStream& msg, Q_UINT32 sender);
00917 
00925     bool systemRemove(KPlayer* player,bool deleteit);
00926 
00927 
00928 private:
00929     KGamePrivate* d;
00930 };
00931 
00932 #endif
KDE Home | KDE Accessibility Home | Description of Access Keys