libkdegames Library API Documentation

KPlayer Class Reference

Base class for a game player. More...

#include <kplayer.h>

Inheritance diagram for KPlayer:

QObject List of all members.

Public Types

typedef QPtrList< KGameIOKGameIOList

Signals

void signalNetworkData (int msgid, const QByteArray &buffer, Q_UINT32 sender, KPlayer *me)
void signalPropertyChanged (KGamePropertyBase *property, KPlayer *me)

Public Methods

 KPlayer ()
 KPlayer (KGame *game)
virtual ~KPlayer ()
virtual int rtti () const
void Debug ()
KGameIOList * ioList ()
void setGame (KGame *game)
KGamegame () const
void setAsyncInput (bool a)
bool asyncInput () const
bool isVirtual () const
void setVirtual (bool v)
bool isActive () const
void setActive (bool v)
Q_UINT32 id () const
void setId (Q_UINT32 i)
int userId () const
void setUserId (int i)
int networkPriority () const
void setNetworkPriority (int b)
KPlayer * networkPlayer () const
void setNetworkPlayer (KPlayer *p)
void setGroup (const QString &group)
virtual const QStringgroup () const
void setName (const QString &name)
virtual const QStringname () const
bool addGameIO (KGameIO *input)
bool removeGameIO (KGameIO *input=0, bool deleteit=true)
KGameIOfindRttiIO (int rtti) const
bool hasRtti (int rtti) const
virtual bool forwardInput (QDataStream &msg, bool transmit=true, Q_UINT32 sender=0)
virtual bool forwardMessage (QDataStream &msg, int msgid, Q_UINT32 receiver=0, Q_UINT32 sender=0)
bool myTurn () const
bool setTurn (bool b, bool exclusive=true)
virtual bool load (QDataStream &stream)
virtual bool save (QDataStream &stream)
void networkTransmission (QDataStream &stream, int msgid, Q_UINT32 sender)
KGamePropertyBasefindProperty (int id) const
bool addProperty (KGamePropertyBase *data)
int calcIOValue ()
KGamePropertyHandlerdataHandler ()

Protected Slots

void sendProperty (int msgid, QDataStream &stream, bool *sent)
void emitSignal (KGamePropertyBase *me)

Detailed Description

Base class for a game player.

The KPlayer class is the central player object. It holds information about the player and is responsible for any input the player does. For this arbitrary many KGameIO modules can be plugged into it. Main features are:

A KPlayer depends on a KGame object. Call KGame::addPlayer() to plug a KPlayer into a KGame object. Note that you cannot do much with a KPlayer object before it has been plugged into a KGame. This is because most properties of KPlayer are KGameProperty which need to send messages through a KGame object to be changed.

A KGameIO represents the input methods of a player and you should make all player inputs through it. So call something like playerInput->move(4); instead which should call KGameIO::sendInput() to actually move. This way you gain a *very* big advantage: you can exchange a KGameIO whenever you want! You can e.g. remove the KGameIO of a local (human) player and just replace it by a computerIO on the fly! So from that point on all playerInputs are done by the computerIO instead of the human player. You also can replace all network players by computer players when the network connection is broken or a player wants to quit. So remember: use KGameIO whenever possible! A KPlayer should just contain all data of the player (KGameIO must not!) and several common functions which are shared by all of your KGameIOs.

Definition at line 68 of file kplayer.h.


Constructor & Destructor Documentation

KPlayer::KPlayer  
 

Create a new player object.

It will be automatically deleted if the game it belongs to is deleted.

Definition at line 62 of file kplayer.cpp.

KPlayer::KPlayer KGame   game
 

Create a new player object.

It will be automatically deleted if the game it belongs to is deleted. This constructor automatically adds the player to the game using KGame::addPlayer()

Definition at line 67 of file kplayer.cpp.

References KGame::addPlayer(), and game().


Member Function Documentation

virtual int KPlayer::rtti   const [inline, virtual]
 

The idendification of the player.

Overwrite this in classes inherting KPlayer to run time identify them.

Returns:
0 for default KPlayer.

Definition at line 97 of file kplayer.h.

Referenced by Debug(), findRttiIO(), hasRtti(), and KGame::savePlayer().

void KPlayer::Debug  
 

Gives debug output of the game status.

Definition at line 428 of file kplayer.cpp.

References asyncInput(), QPtrList< KGameIO >::count(), game(), group(), id(), isActive(), isVirtual(), myTurn(), name(), networkPriority(), and rtti().

Referenced by KGame::slotServerDisconnected().

KGameIOList* KPlayer::ioList   [inline]
 

Returns a list of input devices.

Returns:
list of devices

Definition at line 110 of file kplayer.h.

void KPlayer::setGame KGame   game [inline]
 

sets the game the player belongs to.

This is usually automatically done when adding a player

Parameters:
game  the game

Definition at line 119 of file kplayer.h.

References game().

Referenced by KGame::systemAddPlayer().

KGame* KPlayer::game   const [inline]
 

Query to which game the player belongs to.

Returns:
the game

Definition at line 126 of file kplayer.h.

Referenced by Debug(), forwardInput(), forwardMessage(), KGameIO::game(), KPlayer(), networkTransmission(), sendProperty(), setGame(), and setTurn().

void KPlayer::setAsyncInput bool    a [inline]
 

Set whether this player can make turns/input all the time (true) or only when it is its turn (false) as it is used in turn based games.

Parameters:
a  async=true turn based=false

Definition at line 135 of file kplayer.h.

bool KPlayer::asyncInput   const [inline]
 

Query whether this player does asynchronous input.

Returns:
true/false

Definition at line 143 of file kplayer.h.

References KGameProperty< Q_INT8 >::value().

Referenced by Debug(), forwardInput(), KGame::playerInputFinished(), and KGame::systemPlayerInput().

bool KPlayer::isVirtual   const
 

Is this player a virtual player, ie is it created by mirroring a real player from another network game.

This mirroring is done autmatically as soon as a network connection is build and it affects all players regardless what type

Returns:
true/false

Definition at line 204 of file kplayer.cpp.

Referenced by Debug(), KGame::playerDeleted(), KGame::systemAddPlayer(), and KGame::systemInactivatePlayer().

bool KPlayer::isActive   const [inline]
 

Is this player an active player.

An player is usually inactivated if it is replaced by a network connection. But this could also be called manually

Returns:
true/false

Definition at line 172 of file kplayer.h.

Referenced by Debug(), forwardInput(), forwardMessage(), KGame::networkTransmission(), setTurn(), KGame::systemActivatePlayer(), and KGame::systemInactivatePlayer().

void KPlayer::setActive bool    v [inline]
 

Set an player as active (true) or inactive (false).

Parameters:
v  true=active, false=inactive

Definition at line 179 of file kplayer.h.

Referenced by KGame::systemActivatePlayer(), and KGame::systemInactivatePlayer().

Q_UINT32 KPlayer::id   const
 

Returns the id of the player.

Returns:
the player id

Definition at line 195 of file kplayer.cpp.

Referenced by KGame::activatePlayer(), KGame::addPlayer(), Debug(), findProperty(), KGame::inactivatePlayer(), KGameProcessIO::initIO(), load(), KGameSequence::nextPlayer(), KGameProcessIO::notifyTurn(), KGameIO::notifyTurn(), KGame::playerDeleted(), KGame::playerInputFinished(), KGameProcessIO::receivedMessage(), save(), KGame::savePlayer(), KGame::sendGroupMessage(), KGame::sendPlayerInput(), sendProperty(), setTurn(), KGame::slotClientDisconnected(), KGameChat::slotReceivePrivateMessage(), KGame::slotServerDisconnected(), KGame::systemActivatePlayer(), KGame::systemAddPlayer(), KGame::systemInactivatePlayer(), and KGame::systemRemovePlayer().

int KPlayer::userId   const [inline]
 

Returns the user defined id of the player This value can be used arbitrary by you to have some user idendification for your player, e.g.

0 for a white chess player, 1 for a black one. This value is more reliable than the player id whcih can even change when you make a network connection.

Returns:
the user defined player id

Definition at line 206 of file kplayer.h.

References KGameProperty< int >::value().

Referenced by KGameProcessIO::initIO().

int KPlayer::networkPriority   const
 

Returns whether this player can be replaced by a network connection player.

The name of this function can be improved ;-) If you do not overwrite the function to select what players shall play in a network the KGame does an automatic selection based on the networkPriority This is not a terrible important function at the moment.

Returns:
true/false

Definition at line 213 of file kplayer.cpp.

Referenced by Debug(), and save().

void KPlayer::setNetworkPriority int    b
 

Set whether this player can be replaced by a network player.

There are to possible games. The first type of game has arbitrary many players. As soon as a network players connects the game runs with more players (not tagged situation). The other type is e.g. games like chess which require a constant player number. In a network game situation you would tag one or both players of all participants. As soon as the connect the tagged player will then be replaced by the network partner and it is then controlled over the network. On connection loss the old situation is automatically restored.

The name of this function can be improved;-)

Parameters:
b  should this player be tagged

Definition at line 216 of file kplayer.cpp.

Referenced by load().

KPlayer * KPlayer::networkPlayer   const
 

Returns the player which got inactivated to allow this player to be set up via network.

Mostly internal function

Definition at line 210 of file kplayer.cpp.

void KPlayer::setNetworkPlayer KPlayer *    p
 

Sets this network player replacement.

Internal stuff

Definition at line 207 of file kplayer.cpp.

void KPlayer::setGroup const QString   group
 

A group the player belongs to.

This Can be set arbitrary by you.

Definition at line 183 of file kplayer.cpp.

const QString & KPlayer::group   const [virtual]
 

Query the group the player belongs to.

Definition at line 186 of file kplayer.cpp.

Referenced by Debug(), KGame::sendGroupMessage(), and KGameChat::setFromPlayer().

void KPlayer::setName const QString   name
 

Sets the name of the player.

This can be chosen arbitrary.

Parameters:
name  The player's name

Definition at line 189 of file kplayer.cpp.

const QString & KPlayer::name   const [virtual]
 

Returns:
The name of the player.

Reimplemented from QObject.

Definition at line 192 of file kplayer.cpp.

Referenced by Debug().

bool KPlayer::addGameIO KGameIO   input
 

Adds an IO device for the player.

Possible KGameIO devices can either be taken from the existing ones or be self written. Existing are e.g. Keyboard, Mouse, Computerplayer

Parameters:
input  the inut device
Returns:
true if ok

Definition at line 219 of file kplayer.cpp.

References QPtrList< KGameIO >::append(), and KGameIO::initIO().

bool KPlayer::removeGameIO KGameIO   input = 0,
bool    deleteit = true
 

remove (and delete) a game IO device.

The remove IO(s) is/are deleted by default. If you do not want this set the parameter deleteit to false

Parameters:
input  the device to be removed or 0 for all devices
deleteit  true (default) to delete the device otherwisse just remove it
Returns:
true on ok

Definition at line 231 of file kplayer.cpp.

References QPtrList< KGameIO >::first(), QPtrList< KGameIO >::remove(), and KGameIO::setPlayer().

Referenced by KGameProcessIO::~KGameProcessIO().

KGameIO * KPlayer::findRttiIO int    rtti const
 

Finds the KGameIO devies with the given rtti code.

E.g. find the mouse or network device

Parameters:
rtti  the rtti code to be searched for
Returns:
the KGameIO device

Definition at line 259 of file kplayer.cpp.

References QPtrListIterator::current(), and rtti().

Referenced by hasRtti().

bool KPlayer::hasRtti int    rtti const [inline]
 

Checks whether this player has a IO device of the given rtti type.

Parameters:
rtti  the rtti typed to be checked for
Returns:
true if it exists

Definition at line 320 of file kplayer.h.

References findRttiIO(), and rtti().

bool KPlayer::forwardInput QDataStream   msg,
bool    transmit = true,
Q_UINT32    sender = 0
[virtual]
 

Forwards input to the game object..internal use only.

This method is used by KGameIO::sendInput(). Use that function instead to send player inputs!

This function forwards a player input (see KGameIO classes) to the game object, see KGame, either to KGame::sendPlayerInput() (if transmit=true, ie the message has just been created) or to KGame::playerInput() (if player=false, ie the message *was* sent through KGame::sendPlayerInput).

Definition at line 142 of file kplayer.cpp.

References asyncInput(), game(), isActive(), myTurn(), QObject::sender(), KGame::sendPlayerInput(), and KGame::systemPlayerInput().

Referenced by networkTransmission(), and KGameIO::sendInput().

bool KPlayer::forwardMessage QDataStream   msg,
int    msgid,
Q_UINT32    receiver = 0,
Q_UINT32    sender = 0
[virtual]
 

Forwards Message to the game object..internal use only.

Definition at line 128 of file kplayer.cpp.

References game(), isActive(), QObject::sender(), and KGameNetwork::sendSystemMessage().

Referenced by KGameProcessIO::receivedMessage().

bool KPlayer::myTurn   const [inline]
 

is it my turn to go.

Returns:
true/false

Definition at line 348 of file kplayer.h.

References KGameProperty< Q_INT8 >::value().

Referenced by Debug(), and forwardInput().

bool KPlayer::setTurn bool    b,
bool    exclusive = true
 

Sets whether this player is the next to turn.

If exclusive is given all other players are set to setTurn(false) and only this player can move

Parameters:
b  true/false
exclusive  true (default)/ false
Returns:
should be void

Definition at line 285 of file kplayer.cpp.

References game(), id(), isActive(), and KGame::playerList().

Referenced by KGameSequence::nextPlayer(), KGame::playerInputFinished(), and KGame::systemPlayerInput().

bool KPlayer::load QDataStream   stream [virtual]
 

Load a saved player, from file OR network.

By default all KGameProperty objects in the dataHandler of this player are loaded and saved when using load or save. If you need to save/load more you have to replace this function (and save). You will probably still want to call the default implementation additionally!

Parameters:
stream  a data stream where you can stream the player from
Returns:
true?

Definition at line 314 of file kplayer.cpp.

References id(), and setNetworkPriority().

Referenced by KGame::loadPlayer().

bool KPlayer::save QDataStream   stream [virtual]
 

Save a player to a file OR to network.

See also load

Parameters:
stream  a data stream to load the player from
Returns:
true?

Definition at line 340 of file kplayer.cpp.

References id(), and networkPriority().

Referenced by KGame::savePlayer().

void KPlayer::networkTransmission QDataStream   stream,
int    msgid,
Q_UINT32    sender
 

Receives a message.

Parameters:
msgid  The kind of the message. See messages.txt for further information
stream  The message itself
sender 

Definition at line 353 of file kplayer.cpp.

References QDataStream::device(), forwardInput(), game(), KGameNetwork::gameId(), QObject::sender(), and signalNetworkData().

Referenced by KGame::networkTransmission().

KGamePropertyBase * KPlayer::findProperty int    id const
 

Searches for a property of the player given its id.

Parameters:
id  The id of the property
Returns:
The property with the specified id

Definition at line 389 of file kplayer.cpp.

References id().

bool KPlayer::addProperty KGamePropertyBase   data
 

Adds a property to a player.

You would add all your player specific game data as KGameProperty and they are automatically saved and exchanged over network.

Parameters:
data  The property to be added. Must have an unique id!
Returns:
false if the given id is not valid (ie another property owns the id) or true if the property could be added successfully

Definition at line 394 of file kplayer.cpp.

int KPlayer::calcIOValue  
 

Calculates a checksum over the IO devices.

Can be used to restore the IO handlers. The value returned is the 'or'ed value of the KGameIO rtti's. this is itnernally used for saving and restorign a player.

Definition at line 273 of file kplayer.cpp.

References QPtrListIterator::current().

Referenced by KGame::savePlayer().

KGamePropertyHandler * KPlayer::dataHandler  
 

Returns:
the property handler

Definition at line 198 of file kplayer.cpp.

Referenced by KGame::loadgame(), and KGamePropertyBase::registerData().

void KPlayer::signalNetworkData int    msgid,
const QByteArray   buffer,
Q_UINT32    sender,
KPlayer *    me
[signal]
 

The player object got a message which was targeted at it but has no default method to process it.

This means probably a user message. Connecting to this signal allowed to process it.

Referenced by networkTransmission().

void KPlayer::signalPropertyChanged KGamePropertyBase   property,
KPlayer *    me
[signal]
 

This signal is emmited if a player property changes its value and the property is set to notify this change.

This is an important signal as you should base the actions on a reaction to this property changes.

Referenced by emitSignal().

void KPlayer::sendProperty int    msgid,
QDataStream   stream,
bool *    sent
[protected, slot]
 

Called by KGameProperty only! Internal function!

Definition at line 399 of file kplayer.cpp.

References game(), id(), and KGame::sendPlayerProperty().

void KPlayer::emitSignal KGamePropertyBase   me [protected, slot]
 

Called by KGameProperty only! Internal function!

Definition at line 411 of file kplayer.cpp.

References QPtrListIterator::current(), KGamePropertyBase::id(), signalPropertyChanged(), and KGameProperty< Q_INT8 >::value().


The documentation for this class was generated from the following files:
KDE Logo
This file is part of the documentation for libkdegames Library Version 3.2.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Sat May 1 10:06:40 2004 by doxygen 1.2.15 written by Dimitri van Heesch, © 1997-2003