home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Linux / SLAX 6.0.8 / slax-6.0.8.iso / slax / base / 006-devel.lzm / usr / include / kgame / kgame.h next >
Encoding:
C/C++ Source or Header  |  2007-05-14  |  31.3 KB  |  933 lines

  1. /*
  2.     This file is part of the KDE 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: kgame.h 650394 2007-04-04 12:46:34Z mueller $
  22. */
  23. #ifndef __KGAME_H_
  24. #define __KGAME_H_
  25.  
  26. #include <qstring.h>
  27. #include <qptrlist.h>
  28. #include <qvaluelist.h>
  29.  
  30. #include "kgamenetwork.h"
  31. #include <kdemacros.h>
  32. class KRandomSequence;
  33.  
  34. class KPlayer;
  35. class KGamePropertyBase;
  36. class KGamePropertyHandler;
  37. class KGameSequence;
  38.  
  39. class KGamePrivate;
  40.  
  41. /**
  42.  * @short The main KDE game object
  43.  *
  44.  * The KGame class is the central game object. A game basically
  45.  * consists of following features:
  46.  * - Player handling (add, remove,...)
  47.  * - Game status (end,start,pause,...)
  48.  * - load/save
  49.  * - Move (and message) handling
  50.  * - nextPlayer and gameOver()
  51.  * - Network connection (for KGameNetwork)
  52.  *
  53.  * Example:
  54.  * \code
  55.  * KGame *game=new KGame;
  56.  * \endcode
  57.  *
  58.  *
  59.  * @author Martin Heni <martin@heni-online.de>
  60.  *
  61.  */
  62. class KDE_EXPORT KGame : public KGameNetwork
  63. {
  64.   Q_OBJECT
  65.  
  66. public:
  67.     typedef QPtrList<KPlayer> KGamePlayerList;
  68.  
  69.     /**
  70.      * The policy of the property. This can be PolicyClean (setVale uses
  71.      * send), PolicyDirty (setValue uses changeValue) or
  72.      * PolicyLocal (setValue uses setLocal).
  73.      *
  74.      * A "clean" policy means that the property is always the same on every
  75.      * client. This is achieved by calling send which actually changes
  76.      * the value only when the message from the MessageServer is received.
  77.      *
  78.      * A "dirty" policy means that as soon as setValue is called the
  79.      * property is changed immediately. And additionally sent over network.
  80.      * This can sometimes lead to bugs as the other clients do not
  81.      * immediately have the same value. For more information see
  82.      * changeValue.
  83.      *
  84.      * PolicyLocal means that a KGameProperty behaves like ever
  85.      * "normal" variable. Whenever setValue is called (e.g. using "=")
  86.      * the value of the property is changes immediately without sending it
  87.      * over network. You might want to use this if you are sure that all
  88.      * clients set the property at the same time.
  89.      **/
  90.     enum GamePolicy
  91.     {
  92.         PolicyUndefined = 0,
  93.         PolicyClean = 1,
  94.         PolicyDirty = 2,
  95.         PolicyLocal = 3
  96.     };
  97.  
  98.     /**
  99.      * Create a KGame object. The cookie is used to identify your
  100.      * game in load/save and network operations. Change this between
  101.      * games.
  102.      */
  103.     KGame(int cookie=42,QObject* parent=0);
  104.  
  105.     /**
  106.     * Destructs the game
  107.     */
  108.     virtual ~KGame();
  109.  
  110.     /**
  111.      * Gives debug output of the game status
  112.      */
  113.     virtual void Debug();
  114.  
  115.     /**
  116.      * Game status - Use this to Control the game flow.
  117.      * The KGame e.g. sets the status to Pause when you have
  118.      * less player than the minimum amount
  119.      */
  120.     enum GameStatus
  121.     {
  122.         Init = 0,
  123.         Run = 1,
  124.         Pause = 2,
  125.         End = 3,
  126.         Abort = 4,
  127.         SystemPause = 5,
  128.         Intro = 6,
  129.         UserStatus = 7
  130.     };
  131.  
  132.     // Properties
  133.     /**
  134.      * Returns a list of all active players
  135.      *
  136.      * @return the list of players
  137.      */
  138.     KGamePlayerList *playerList();
  139.  
  140.     /**
  141.      * The same as @ref playerList but returns a const pointer.
  142.      **/
  143.     const KGamePlayerList *playerList() const;
  144.  
  145.     /**
  146.      * Returns a list of all inactive players
  147.      * @return the list of players
  148.      */
  149.     KGamePlayerList *inactivePlayerList();
  150.  
  151.     /**
  152.      * The same as @ref inactivePlayerList but returns a const pointer.
  153.      **/
  154.     const KGamePlayerList *inactivePlayerList() const;
  155.  
  156.     /**
  157.      * Returns a pointer to the game's KRandomSequence. This sequence is
  158.      * identical for all network players!
  159.      * @return KRandomSequence pointer
  160.      */
  161.     KRandomSequence *random() const;
  162.  
  163.     /**
  164.      * @return The KGameSequence object that is currently in use.
  165.      * @see setGameSequence
  166.      **/
  167.     KGameSequence *gameSequence() const;
  168.  
  169.     /**
  170.      * Is the game running
  171.      * @return true/false
  172.      */
  173.     bool isRunning() const;
  174.  
  175.     // Player handling
  176.     /**
  177.      * Returns the player object for a given player id
  178.      * @param id Player id
  179.      * @return player object
  180.      */
  181.     KPlayer *findPlayer(Q_UINT32 id) const;
  182.  
  183.     /**
  184.      * Set a new @ref KGameSequence to control player management. By default
  185.      * KGame uses a normal @ref KGameSequence object. You might want to subclass
  186.      * that and provide your own object.
  187.      *
  188.      * The previous sequence will get deleted.
  189.      * @param sequence The new game sequence object. KGame takes ownership and
  190.      * will delete it on destruction!
  191.      **/
  192.     void setGameSequence(KGameSequence* sequence);
  193.  
  194.     /**
  195.      * Note that KPlayer::save must be implemented properly, as well as
  196.      * KPlayer::rtti
  197.      * This will only send a message to all clients. The player is _not_ added
  198.      * directly!
  199.      * See also playerInput which will be called as soon as the
  200.      * player really has been added.
  201.      *
  202.      * Note that an added player will first get into a "queue" and won't be in
  203.      * the game. It will be added to the game as soon as systemAddPlayer is
  204.      * called what will happen as soon as IdAddPlayer is received.
  205.      *
  206.      * Note: you probably want to connect to signalPlayerJoinedGame for
  207.      * further initialization!
  208.      * @param newplayer The player you want to add. KGame will send a message to
  209.      * all clients and add the player using systemAddPlayer
  210.      **/
  211.     void addPlayer(KPlayer* newplayer);
  212.  
  213.     /**
  214.      * Sends a message over the network, msgid=IdRemovePlayer.
  215.      *
  216.      * As soon as this message is received by networkTransmission
  217.      * systemRemovePlayer is called and the player is removed.
  218.      **/
  219.     //AB: TODO: make sendMessage to return if the message will be able to be
  220.     //sent, eg if a socket is connected, etc. If sendMessage returns false
  221.     //remove the player directly using systemRemovePlayer
  222.     bool removePlayer(KPlayer * player) { return removePlayer(player, 0); }
  223.  
  224.     /**
  225.      * Called by the destructor of KPlayer to remove itself from the game
  226.      *
  227.      **/
  228.     void playerDeleted(KPlayer * player);
  229.  
  230.     /**
  231.      * sends activate player: internal use only?
  232.      */
  233.     bool activatePlayer(KPlayer *player);
  234.  
  235.     /**
  236.      * sends inactivate player: internal use only?
  237.      */
  238.     bool inactivatePlayer(KPlayer *player);
  239.  
  240.     /**
  241.      * Set the maximal number of players. After this is
  242.      * reached no more players can be added. You must be ADMIN to call this (@see
  243.      * isAdmin).
  244.      * @param maxnumber maximal number of players
  245.      */
  246.     void setMaxPlayers(uint maxnumber);
  247.  
  248.     /**
  249.      * What is the maximal number of players?
  250.      * @return maximal number of players
  251.      */
  252.     int maxPlayers() const;
  253.  
  254.     /**
  255.      * Set the minimal number of players. A game can not be started
  256.      * with less player resp. is paused when already running. You must be ADMIN
  257.      * to call this (see @ref isAdmin)!
  258.      * @param minnumber minimal number of players
  259.      */
  260.     void setMinPlayers(uint minnumber);
  261.  
  262.     /**
  263.      * What is the minimal number of players?
  264.      * @return minimal number of players
  265.      */
  266.     uint minPlayers() const;
  267.  
  268.     /**
  269.      * Returns how many players are plugged into the game
  270.      * @return number of players
  271.      */
  272.     uint playerCount() const;
  273.  
  274.     /**
  275.      * @deprecated
  276.      * Use @ref KGameSequence::nextPlayer instead
  277.      **/
  278.     virtual KPlayer * nextPlayer(KPlayer *last,bool exclusive=true);
  279.  
  280.     // Input events
  281.     /**
  282.      * Called by KPlayer to send a player input to the
  283.      * KMessageServer.
  284.      **/
  285.     virtual bool sendPlayerInput(QDataStream &msg,KPlayer *player,Q_UINT32 sender=0);
  286.  
  287.     /**
  288.      * Called when a player input arrives from KMessageServer.
  289.      *
  290.      * Calls prepareNext (using QTimer::singleShot) if gameOver()
  291.      * returns 0. This function should normally not be used outside KGame.
  292.      * It could be made non-virtual,protected in a later version. At the
  293.      * moment it is a virtual function to give you more control over KGame.
  294.      *
  295.      * For documentation see playerInput.
  296.      **/
  297.     virtual bool systemPlayerInput(QDataStream &msg,KPlayer *player,Q_UINT32 sender=0);
  298.  
  299.     /**
  300.     * This virtual function is called if the KGame needs to create a new player.
  301.     * This happens only over a network and with load/save. Doing nothing
  302.     * will create a default KPlayer. If you want to have your own player
  303.     * you have to create one with the given rtti here.
  304.     * Note: If your game uses a player class derived from KPlayer you MUST
  305.     * override this function and create your player here. Otherwise the
  306.     * game will crash.
  307.     * Example:
  308.     * \code
  309.     *  KPlayer *MyGame::createPlayer(int rtti,int io,bool isvirtual)
  310.     *  {
  311.     *    KPlayer *player=new MyPlayer;
  312.     *    if (!isvirtual) // network player ?
  313.     *    {
  314.     *      // Define something like this to add the IO modules
  315.     *      createIO(player,(KGameIO::IOMode)io);
  316.     *    }
  317.     *    return player;
  318.     *    }
  319.     * \endcode
  320.     *
  321.     * @param rtti is the type of the player (0 means default KPlayer)
  322.     * @param io is the 'or'ed rtti of the KGameIO's
  323.     * @param isvirtual true if player is virtual
  324.     */
  325.     virtual KPlayer *createPlayer(int rtti,int io,bool isvirtual);
  326.  
  327.     // load/save
  328.     /**
  329.      * Load a saved game, from file OR network. This function has
  330.      * to be overwritten or you need to connect to the load signal
  331.      * if you have game data other than KGameProperty.
  332.      * For file load you should reset() the game before any load attempt
  333.      * to make sure you load into an clear state.
  334.      *
  335.      * @param stream a data stream where you can stream the game from
  336.      * @param reset - shall the game be reset before loading
  337.      *
  338.      * @return true?
  339.      */
  340.     virtual bool load(QDataStream &stream,bool reset=true);
  341.  
  342.     /**
  343.      * Same as above function but with different parameters
  344.      *
  345.      * @param filename - the filename of the file to be opened
  346.      * @param reset - shall the game be reset before loading
  347.      *
  348.      * @return true?
  349.      **/
  350.     virtual bool load(QString filename,bool reset=true);
  351.  
  352.     /**
  353.      * Save a game to a file OR to network. Otherwise the same as
  354.      * the load function
  355.      *
  356.      * @param stream a data stream to load the game from
  357.      * @param saveplayers If true then all players wil be saved too
  358.      *
  359.      * @return true?
  360.      */
  361.     virtual bool save(QDataStream &stream,bool saveplayers=true);
  362.  
  363.     /**
  364.      * Same as above function but with different parameters
  365.      *
  366.      * @param filename the filename of the file to be saved
  367.      * @param saveplayers If true then all players wil be saved too
  368.      *
  369.      * @return true?
  370.      **/
  371.     virtual bool save(QString filename,bool saveplayers=true);
  372.  
  373.     /**
  374.      * Resets the game, i.e. puts it into a state where everything
  375.      * can be started from, e.g. a load game
  376.      * Right now it does only need to delete all players
  377.      *
  378.      * @return true on success
  379.      */
  380.     virtual bool reset();
  381.  
  382.  
  383.     // Game sequence
  384.     /**
  385.      * returns the game status, ie running,pause,ended,...
  386.      *
  387.      * @return game status
  388.      */
  389.     int gameStatus() const;
  390.  
  391.     /**
  392.      * sets the game status
  393.      *
  394.      * @param status the new status
  395.      */
  396.     void setGameStatus(int status);
  397.  
  398.     /**
  399.     *  docu: see KPlayer
  400.     **/
  401.     bool addProperty(KGamePropertyBase* data);
  402.  
  403.     /**
  404.      * This is called by KPlayer::sendProperty only! Internal function!
  405.      **/
  406.     bool sendPlayerProperty(int msgid, QDataStream& s, Q_UINT32 playerId);
  407.  
  408.     /**
  409.     * This function allows to find the pointer to a player
  410.     * property when you know it's id
  411.     */
  412.     KGamePropertyBase* findProperty(int id) const;
  413.  
  414.     /**
  415.      * Changes the consistency policy of a property. The
  416.      * GamePolicy is one of PolicyClean (default), PolicyDirty or PolicyLocal.
  417.      *
  418.      * It is up to you to decide how you want to work.
  419.      **/
  420.     void setPolicy(GamePolicy p,bool recursive=true);
  421.  
  422.     /**
  423.      * @return The default policy of the property
  424.      **/
  425.     GamePolicy policy() const;
  426.  
  427.     /**
  428.      * See KGameNetwork::sendMessage
  429.      *
  430.      * Send a network message msg with a given message ID msgid to all players of
  431.      * a given group (see KPlayer::group)
  432.      * @param msg the message which will be send. See messages.txt for contents
  433.      * @param msgid an id for this message
  434.      * @param sender the id of the sender
  435.      * @param group the group of the receivers
  436.      * @return true if worked
  437.      */
  438.     bool sendGroupMessage(const QByteArray& msg, int msgid, Q_UINT32 sender, const QString& group);
  439.     bool sendGroupMessage(const QDataStream &msg, int msgid, Q_UINT32 sender, const QString& group);
  440.     bool sendGroupMessage(int msg, int msgid, Q_UINT32 sender, const QString& group);
  441.     bool sendGroupMessage(const QString& msg, int msgid, Q_UINT32 sender, const QString& group);
  442.  
  443.     /**
  444.      * This will either forward an incoming message to a specified player
  445.      * (see KPlayer::networkTransmission) or
  446.      * handle the message directly (e.g. if msgif==IdRemovePlayer it will remove
  447.      * the (in the stream) specified player). If both is not possible (i.e. the
  448.      * message is user specified data) the signal signalNetworkData is
  449.      * emitted.
  450.      *
  451.      * This emits signalMessageUpdate <em>before</em> doing anything with
  452.      * the message. You can use this signal when you want to be notified about
  453.      * an update/change.
  454.      * @param msgid Specifies the kind of the message. See messages.txt for
  455.      * further information
  456.      * @param stream The message that is being sent
  457.      * @param receiver The is of the player this message is for. 0 For broadcast.
  458.      * @param sender
  459.      * @param clientID the client from which we received the transmission - hardly used
  460.      **/
  461.     virtual void networkTransmission(QDataStream &stream, int msgid, Q_UINT32 receiver, Q_UINT32 sender, Q_UINT32 clientID);
  462.  
  463.     /**
  464.      * Returns a pointer to the KGame property handler
  465.      **/
  466.     KGamePropertyHandler* dataHandler() const;
  467.  
  468. protected slots:
  469.     /**
  470.      * Called by KGamePropertyHandler only! Internal function!
  471.      **/
  472.     void sendProperty(int msgid, QDataStream& stream, bool* sent);
  473.  
  474.     /**
  475.       * Called by KGamePropertyHandler only! Internal function!
  476.      **/
  477.     void emitSignal(KGamePropertyBase *me);
  478.  
  479.     /**
  480.      * @deprecated
  481.      * Use KGameSequence::prepareNext() instead
  482.      **/
  483.     virtual void prepareNext();
  484.  
  485.  
  486.     /**
  487.      * Calls negotiateNetworkGame()
  488.      * See KGameNetwork::signalClientConnected
  489.      **/
  490.     void slotClientConnected(Q_UINT32 clientId);
  491.  
  492.     /**
  493.      * This slot is called whenever the connection to a client is lost (ie the
  494.      * signal KGameNetwork::signalClientDisconnected is emitted) and will remove
  495.      * the players from that client.
  496.      * @param clientId The client the connection has been lost to
  497.      * @param broken (ignore this - not used)
  498.      **/
  499.     void slotClientDisconnected(Q_UINT32 clientId,bool broken);
  500.  
  501.     /**
  502.      * This slot is called whenever the connection to the server is lost (ie the
  503.      * signal KGameNetwork::signalConnectionBroken is emitted) and will
  504.      * switch to local game mode
  505.      **/
  506.     void slotServerDisconnected();
  507.  
  508. signals:
  509.     /**
  510.      * When a client disconnects from the game usually all players from that
  511.      * client are removed. But if you use completely the KGame structure you
  512.      * probably don't want this. You just want to replace the KGameIO of the
  513.      * (human) player by a computer KGameIO. So this player continues game but
  514.      * is from this point on controlled by the computer.
  515.      *
  516.      * You achieve this by connecting to this signal. It is emitted as soon as a
  517.      * client disconnects on <em>all</em> other clients. Make sure to add a new
  518.      * KGameIO only once! you might want to use @ref isAdmin for this. If you
  519.      * added a new KGameIO set *remove=false otherwise the player is completely
  520.      * removed.
  521.      * @param player The player that is about to be removed. Add your new
  522.      * KGameIO here - but only on <em>one</em> client!
  523.      * @param remove Set this to FALSE if you don't want this player to be
  524.      * removed completely.
  525.      **/
  526.     void signalReplacePlayerIO(KPlayer* player, bool* remove);
  527.  
  528.     /**
  529.      * The game will be loaded from the given stream. Load from here
  530.      * the data which is NOT a game or player property.
  531.      * It is not necessary to use this signal for a full property game.
  532.      *
  533.      * This signal is emitted <em>before</em> the players are loaded by 
  534.      * KGame. See also signalLoad
  535.      *
  536.      * You must load <em>exactly</em> the same data from the stream that you have saved
  537.      * in signalSavePrePlayers. Otherwise player loading will not work
  538.      * anymore.
  539.      *
  540.      * @param stream the load stream
  541.      */
  542.     void signalLoadPrePlayers(QDataStream &stream);
  543.  
  544.     /**
  545.      * The game will be loaded from the given stream. Load from here
  546.      * the data which is NOT a game or player property.
  547.      * It is not necessary to use this signal for a full property game.
  548.      *
  549.      * @param stream the load stream
  550.      */
  551.     void signalLoad(QDataStream &stream);
  552.  
  553.     /**
  554.      * The game will be saved to the given stream. Fill this with data
  555.      * which is NOT a game or player property.
  556.      * It is not necessary to use this signal for a full property game.
  557.      *
  558.      * This signal is emitted <em>before</em> the players are saved by 
  559.      * KGame. See also signalSave
  560.      *
  561.      * If you can choose between signalSavePrePlayers and signalSave then
  562.      * better use signalSave
  563.      *
  564.      * @param stream the save stream
  565.      **/
  566.     void signalSavePrePlayers(QDataStream &stream);
  567.     
  568.     /**
  569.      * The game will be saved to the given stream. Fill this with data
  570.      * which is NOT a game or player property.
  571.      * It is not necessary to use this signal for a full property game.
  572.      *
  573.      * @param stream the save stream
  574.      */
  575.     void signalSave(QDataStream &stream);
  576.  
  577.     /**
  578.      * Is emmited if a game with a different version cookie is loaded.
  579.      * Normally this should result in an error. But maybe you do support
  580.      * loading of older game versions. Here would be a good place to do a
  581.      * conversion.
  582.      *
  583.      * @param stream - the load stream
  584.      * @param network - true if this is a network connect. False for load game
  585.      * @param cookie - the saved cookie. It differs from KGame::cookie()
  586.      * @param result - set this to true if you managed to load the game
  587.      */
  588.     void signalLoadError(QDataStream &stream,bool network,int cookie, bool &result);
  589.  
  590.     /**
  591.      * We got an user defined update message. This is usually done
  592.      * by a sendData in a inherited KGame Object which defines its
  593.      * own methods and has to syncronise them over the network.
  594.      * Reaction to this is usually a call to a KGame function.
  595.      */
  596.     void signalNetworkData(int msgid,const QByteArray& buffer, Q_UINT32 receiver, Q_UINT32 sender);
  597.  
  598.     /**
  599.      * We got an network message. this can be used to notify us that something
  600.      * changed. What changed can be seen in the message id. Whether this is
  601.      * the best possible method to do this is unclear...
  602.      */
  603.     void signalMessageUpdate(int msgid,Q_UINT32 receiver,Q_UINT32 sender);
  604.  
  605.     /**
  606.      * a player left the game because of a broken connection or so!
  607.      *
  608.      * Note that when this signal is emitted the player is not part of @ref
  609.      * playerList anymore but the pointer is still valid. You should do some
  610.      * final cleanups here since the player is usually deleted after the signal
  611.      * is emitted.
  612.      *
  613.      * @param player the player who left the game
  614.      */
  615.     void signalPlayerLeftGame(KPlayer *player);
  616.  
  617.     /**
  618.      * a player joined the game
  619.      *
  620.      * @param player the player who joined the game
  621.      */
  622.     void signalPlayerJoinedGame(KPlayer *player);
  623.  
  624.  
  625.     /**
  626.     * This signal is emmited if a player property changes its value and
  627.     * the property is set to notify this change
  628.     */
  629.     void signalPropertyChanged(KGamePropertyBase *property, KGame *me);
  630.  
  631.     /**
  632.      * Is emitted after a call to gameOver() returns a non zero
  633.      * return code. This code is forwarded to this signal as 'status'.
  634.      *
  635.      * @param status the return code of gameOver()
  636.      * @param current the player who did the last move
  637.      * @param me a pointer to the KGame object
  638.      */
  639.     void signalGameOver(int status, KPlayer *current, KGame *me);
  640.  
  641.     /**
  642.     * Is emmited after a client is successfully connected to the game.
  643.     * The client id is the id of the new game client. An easy way to
  644.     * check whether that's us is
  645.     * \code
  646.     *   if (clientid==gameid()) .. // we joined
  647.     *   else ... // someone joined the game
  648.     * \endcode
  649.     * @param clientid - The id of the new client
  650.     * @param me - our game pointer
  651.     */
  652.     void signalClientJoinedGame(Q_UINT32 clientid,KGame *me);
  653.  
  654.     /**
  655.     * This signal is emitted after a network partner left the
  656.     * game (either by a broken connection or voluntarily).
  657.     * All changes to the network players have already be done.
  658.     * If there are not enough players left, the game might have
  659.     * been paused. To check this you get the old gamestatus
  660.     * before the disconnection as argument here. The id of the
  661.     * client who left the game allows to distinguish who left the
  662.     * game. If it is 0, the server disconnected and you were a client
  663.     * which has been switched back to local play.
  664.     * You can use this signal to, e.g. set some menues back to local
  665.     * player when they were network before.
  666.     *
  667.     * @param clientID - 0:server left, otherwise the client who left
  668.     * @param oldgamestatus - the gamestatus before the loss
  669.     * @param me - our game pointer
  670.     **/
  671.     void signalClientLeftGame(int clientID,int oldgamestatus,KGame *me);
  672.  
  673.  
  674. protected:
  675.     /**
  676.      * A player input occurred. This is the most important function
  677.      * as the given message will contain the current move made by
  678.      * the given player.
  679.      * Note that you HAVE to overwrite this function. Otherwise your
  680.      * game makes no sense at all.
  681.      * Generally you have to return TRUE in this function. Only then
  682.      * the game sequence is proceeded by calling @ref playerInputFinished
  683.      * which in turn will check for game over or the next player
  684.      * However, if you have a delayed move, because you e.g. move a
  685.      * card or a piece you want to return FALSE to pause the game sequence
  686.      * and then manually call @ref playerInputFinished to resume it.
  687.      * Example:
  688.      * \code
  689.      * bool MyClass::playerInput(QDataStream &msg,KPlayer *player)
  690.      * {
  691.      *   Q_INT32 move;
  692.      *   msg >>  move;
  693.      *   kdDebug() << "  Player " << player->id() << " moved to " << move <<
  694.      *   endl;
  695.      *   return true;
  696.      * }
  697.      * \endcode
  698.      *
  699.      * @param msg the move message
  700.      * @param player the player who did the move
  701.      * @return true - input ready, false: input manual
  702.      */
  703.     virtual bool playerInput(QDataStream &msg,KPlayer *player)=0;
  704.  
  705.  
  706.     /**
  707.     * Called after the player input is processed by the game. Here the
  708.     * checks for game over and nextPlayer (in the case of turn base games)
  709.     * are processed.
  710.     * Call this manually if you have a delayed move, i.e. your playerInput
  711.     * function returns FALSE. If it returns true you need not do anything
  712.     * here.
  713.     *
  714.     * @return the current player
  715.     *
  716.     **/
  717.     KPlayer *playerInputFinished(KPlayer *player);
  718.  
  719.  
  720.     /**
  721.     * This virtual function can be overwritten for your own player management.
  722.     * It is called when a new game connects to an existing network game or
  723.     * to the network master. In case you do not want all players of both games
  724.     * to be present in the new network game, you can deactivate players here.
  725.     * This is of particular importance if you have a game with fixed number of
  726.     * player like e.g. chess. A network connect needs to disable one player of
  727.     * each game to make sense.
  728.     *
  729.     * Not overwriting this function will activate a default behaviour which
  730.     * will deactivate players until the @ref maxPlayers() numebr is reached
  731.     * according to the KPlayer::networkPriority() value. Players with a low
  732.     * value will be kicked out first. With equal priority players of the new
  733.     * client will leave first. This means, not setting this value and not
  734.     * overwriting this function will never allow a chess game to add client
  735.     * players!!!
  736.     * On the other hand setting one player of each game to a networkPriorty of
  737.     * say 10, already does most of the work for you.
  738.     *
  739.     * The parameters of this function are the playerlist of the network game,
  740.     * which is @ref playerList(). The second argument is the player list of
  741.     * the new client who wants to join and the third argument serves as return
  742.     * parameter. All <em>player ID's</em> which are written into this list
  743.     * will be <em>removed</em> from the created game. You do this by an
  744.     * \code
  745.     * inactivate.append(player->id());
  746.     * \endcode
  747.     *
  748.     * @param oldplayer - the list of the network players
  749.     * @param newplayer - the list of the client players
  750.     * @param inactivate - the value list of ids to be deactivated
  751.     *
  752.     **/
  753.     virtual void newPlayersJoin(KGamePlayerList *oldplayer,
  754.                 KGamePlayerList *newplayer,
  755.                 QValueList<int> &inactivate) {
  756.         Q_UNUSED( oldplayer );
  757.         Q_UNUSED( newplayer );
  758.         Q_UNUSED( inactivate );
  759.     }
  760.  
  761.     /**
  762.     * Save the player list to a stream. Used for network game and load/save.
  763.     * Can be overwritten if you know what you are doing
  764.     *
  765.     * @param stream is the stream to save the player ot
  766.     * @param list the optional list is the player list to be saved, default is playerList()
  767.     *
  768.     **/
  769.     void savePlayers(QDataStream &stream,KGamePlayerList *list=0);
  770.  
  771.     /**
  772.      * Prepare a player for being added. Put all data about a player into the
  773.      * stream so that it can be sent to the KGameCommunicationServer using
  774.      * addPlayer (e.g.)
  775.      *
  776.      * This function ensures that the code for adding a player is the same in
  777.      * addPlayer as well as in negotiateNetworkGame
  778.      * @param stream is the stream to add the player
  779.      * @param player The player to add
  780.      **/
  781.     void savePlayer(QDataStream& stream,KPlayer* player);
  782.  
  783.     /**
  784.     * Load the player list from a stream. Used for network game and load/save.
  785.     * Can be overwritten if you know what you are doing
  786.     *
  787.     * @param stream is the stream to save the player to
  788.     * @param isvirtual will set the virtual flag true/false
  789.     *
  790.     **/
  791.     KPlayer *loadPlayer(QDataStream& stream,bool isvirtual=false);
  792.  
  793.  
  794.     /**
  795.      * inactivates player. Use @ref inactivatePlayer instead!
  796.      */
  797.     bool systemInactivatePlayer(KPlayer *player);
  798.  
  799.     /**
  800.      * activates player. Use @ref activatePlayer instead!
  801.      */
  802.     bool systemActivatePlayer(KPlayer *player);
  803.  
  804.     /**
  805.      * Adds a player to the game
  806.      *
  807.      * Use @ref addPlayer to send @ref KGameMessage::IdAddPlayer. As soon as
  808.      * this Id is received this function is called, where the player (see @ref
  809.      * KPlayer::rtti) is added as well as its properties (see @ref KPlayer::save
  810.      * and @ref KPlayer::load)
  811.      *
  812.      * This method calls the overloaded @ref systemAddPlayer with the created
  813.      * player as argument. That method will really add the player.
  814.      * If you need to do some changes to your newly added player just connect to
  815.      * @ref signalPlayerJoinedGame
  816.      */
  817.  
  818.     /**
  819.      * Finally adds a player to the game and therefore to the list.
  820.      **/
  821.     void systemAddPlayer(KPlayer* newplayer);
  822.  
  823.     /**
  824.      * Removes a player from the game
  825.      *
  826.      * Use removePlayer to send KGameMessage::IdRemovePlayer. As soon
  827.      * as this Id is received systemRemovePlayer is called and the player is
  828.      * removed directly.
  829.      **/
  830.     void systemRemovePlayer(KPlayer* player,bool deleteit);
  831.  
  832.     /**
  833.      * This member function will transmit e.g. all players to that client, as well as
  834.      * all properties of these players (at least if they have been added by
  835.      * @ref KPlayer::addProperty) so that the client will finally have the same
  836.      * status as the master. You want to overwrite this function if you expand
  837.      * KGame by any properties which have to be known by all clients.
  838.      *
  839.      * Only the ADMIN is allowed to call this.
  840.      * @param clientID The ID of the message client which has connected
  841.      **/
  842.     virtual void negotiateNetworkGame(Q_UINT32 clientID);
  843.  
  844.     /**
  845.      * syncronise the random numbers with all network clients
  846.      * not used by KGame - if it should be kept then as public method
  847.      */
  848.     void syncRandom();
  849.  
  850.     void deletePlayers();
  851.     void deleteInactivePlayers();
  852.  
  853.     /**
  854.      * @deprecated
  855.      * Use @ref KGameSequence instead.
  856.      *
  857.      * @param player the player who made the last move
  858.      * @return anything else but 0 is considered as game over
  859.      */
  860.     virtual int checkGameOver(KPlayer *player);
  861.  
  862.     /**
  863.      * Load a saved game, from file OR network. Internal. 
  864.      * Warning: loadgame must not rely that all players all already
  865.      * activated. Actually the network will activate a player AFTER
  866.      * the loadgame only. This is not true anymore. But be careful 
  867.      * anyway.
  868.      *
  869.      * @param stream a data stream where you can stream the game from
  870.      * @param network is it a network call -> make players virtual
  871.      * @param reset shall the game be reset before loading
  872.      *
  873.      * @return true?
  874.      */
  875.     virtual bool loadgame(QDataStream &stream, bool network, bool reset);
  876.  
  877.     /**
  878.      * Save a game, to file OR network. Internal. 
  879.      *
  880.      * @param stream a data stream where you can stream the game from
  881.      * @param network is it a call from the network or from a file (unused but informative)
  882.      * @param saveplayers shall the players be saved too (should be TRUE)
  883.      *
  884.      * @return true?
  885.      */
  886.     virtual bool savegame(QDataStream &stream, bool network,bool saveplayers);
  887.  
  888. private:
  889.     //AB: this is to hide the "receiver" parameter from the user. It shouldn't be
  890.     //used if possible (except for init).
  891.     /**
  892.      * This is an overloaded function. Id differs from the public one only in
  893.      * its parameters:
  894.      *
  895.      * @param receiver The Client that will receive the message. You will hardly
  896.      * ever need this. It it internally used to initialize a newly connected
  897.      * client.
  898.      **/
  899.     //void addPlayer(KPlayer* newplayer, Q_UINT32 receiver);
  900.  
  901.     /**
  902.      * Just the same as the public one except receiver:
  903.      * @param receiver 0 for broadcast, otherwise the receiver. Should only be
  904.      * used in special circumstances and not outside KGame.
  905.      **/
  906.     bool removePlayer(KPlayer * player, Q_UINT32 receiver);
  907.  
  908.     /**
  909.      * Helping function - game negotiation
  910.      **/
  911.     void setupGame(Q_UINT32 sender);
  912.  
  913.     /**
  914.      * Helping function - game negotiation
  915.      **/
  916.     void setupGameContinue(QDataStream& msg, Q_UINT32 sender);
  917.  
  918.     /**
  919.      * Removes a player from all lists, removes the @ref KGame pointer from the
  920.      * @ref KPlayer and deletes the player. Used by (e.g.) @ref
  921.      * systemRemovePlayer
  922.      * @return True if the player has been removed, false if the current is not
  923.      * found
  924.      **/
  925.     bool systemRemove(KPlayer* player,bool deleteit);
  926.  
  927.  
  928. private:
  929.     KGamePrivate* d;
  930. };
  931.  
  932. #endif
  933.