kplayer.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 #ifndef __KPLAYER_H_
00022 #define __KPLAYER_H_
00023 
00024 #include <qstring.h>
00025 #include <qobject.h>
00026 #include <qptrlist.h>
00027 
00028 #include "kgameproperty.h"
00029 #include <kdemacros.h>
00030 
00031 class KGame;
00032 class KGameIO;
00033 class KGamePropertyBase;
00034 class KGamePropertyHandler;
00035 
00036 class KPlayerPrivate;
00037 
00069 class KDE_EXPORT KPlayer : public QObject
00070 {
00071   Q_OBJECT
00072 
00073 public:
00074       typedef QPtrList<KGameIO> KGameIOList;
00075 
00076       // KPlayer(KGame *,KGameIO * input=0);
00081       KPlayer();
00082 
00088       KPlayer(KGame* game);
00089 
00090       virtual ~KPlayer();
00091 
00098       virtual int rtti() const {return 0;}
00099 
00103       void Debug();
00104 
00105       // properties
00111       KGameIOList *ioList() {return &mInputList;}
00112 
00120       void setGame(KGame *game) {mGame=game;}
00121 
00127       KGame *game() const {return mGame;}
00128 
00136       void setAsyncInput(bool a) {mAsyncInput = a;}
00137 
00144       bool asyncInput() const {return mAsyncInput.value();}
00145 
00155       bool isVirtual() const;
00156 
00164       void setVirtual(bool v);
00165 
00173       bool isActive() const {return mActive;}
00174 
00180       void setActive(bool v) {mActive=v;}
00181 
00187       Q_UINT32 id() const; 
00188 
00189       /* Set the players id. This is done automatically by
00190        * the game object when adding a new player!
00191        *
00192        * @param i the player id
00193        */
00194       void setId(Q_UINT32 i);
00195 
00207       int userId() const {return mUserId.value();} 
00208 
00209       /* Set the user defined players id.
00210        *
00211        * @param i the user defined player id
00212        */
00213       void setUserId(int i) {mUserId = i;}
00214 
00225       int networkPriority() const;
00226 
00243       void setNetworkPriority(int b);
00244 
00250       KPlayer *networkPlayer() const;
00251 
00255       void setNetworkPlayer(KPlayer *p);
00256 
00257       // A name and group the player belongs to
00262       void setGroup(const QString& group);
00263 
00267       virtual const QString& group() const;
00268 
00274       void setName(const QString& name);
00275 
00279       virtual const QString& name() const;
00280 
00281 
00282       // set devices
00291       bool addGameIO(KGameIO *input);
00292 
00303       bool removeGameIO(KGameIO *input=0,bool deleteit=true);
00304 
00312       KGameIO *findRttiIO(int rtti) const;
00313 
00321       bool hasRtti(int rtti) const  {return findRttiIO(rtti)!=0;}
00322 
00323       // Message exchange
00336       virtual bool forwardInput(QDataStream &msg,bool transmit=true, Q_UINT32 sender=0);
00337 
00341       virtual bool forwardMessage(QDataStream &msg,int msgid,Q_UINT32 receiver=0,Q_UINT32 sender=0);
00342 
00343       // Game logic
00349       bool myTurn() const {return mMyTurn.value();}
00350 
00360       bool setTurn(bool b,bool exclusive=true);
00361 
00362 
00363       // load/save
00375       virtual bool load(QDataStream &stream);
00376 
00384       virtual bool save(QDataStream &stream);
00385 
00393       void networkTransmission(QDataStream &stream,int msgid,Q_UINT32 sender);
00394 
00400       KGamePropertyBase* findProperty(int id) const;
00401 
00411       bool addProperty(KGamePropertyBase* data);
00412 
00419       int calcIOValue();
00420 
00424        KGamePropertyHandler* dataHandler();
00425 
00426 signals:
00433        void signalNetworkData(int msgid, const QByteArray& buffer, Q_UINT32 sender, KPlayer *me);
00434 
00441        void signalPropertyChanged(KGamePropertyBase *property,KPlayer *me);
00442 
00443 protected slots:
00447       void sendProperty(int msgid, QDataStream& stream, bool* sent);
00451       void emitSignal(KGamePropertyBase *me);
00452 
00453 
00454 private:
00455       void init();
00456 
00457 private:
00458       KGame *mGame;
00459       bool mActive;      // active player
00460       KGameIOList mInputList;
00461 
00462       // GameProperty // AB: I think we can't move them to KPlayerPrivate - inline
00463       // makes sense here
00464       KGamePropertyBool mAsyncInput;  // async input allowed
00465       KGamePropertyBool mMyTurn;      // Is it my turn to play (only useful if not async)?
00466       KGamePropertyInt  mUserId;      // a user defined id
00467 
00468       KPlayerPrivate* d;
00469 };
00470 
00471 #endif
KDE Home | KDE Accessibility Home | Description of Access Keys