Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/objecthierarchy/src/orxonox/objects/infos/HumanPlayer.cc @ 2041

Last change on this file since 2041 was 2041, checked in by landauf, 16 years ago

bugs—
network++
endurance—
tiredness++

but it still doesn't work properly
(commit because oli is very impatient)

File size: 6.0 KB
Line 
1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
3 *                    > www.orxonox.net <
4 *
5 *
6 *   License notice:
7 *
8 *   This program is free software; you can redistribute it and/or
9 *   modify it under the terms of the GNU General Public License
10 *   as published by the Free Software Foundation; either version 2
11 *   of the License, or (at your option) any later version.
12 *
13 *   This program is distributed in the hope that it will be useful,
14 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 *   GNU General Public License for more details.
17 *
18 *   You should have received a copy of the GNU General Public License
19 *   along with this program; if not, write to the Free Software
20 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21 *
22 *   Author:
23 *      Fabian 'x3n' Landau
24 *   Co-authors:
25 *      ...
26 *
27 */
28
29#include "OrxonoxStableHeaders.h"
30#include "HumanPlayer.h"
31
32#include "core/Core.h"
33#include "core/CoreIncludes.h"
34#include "core/ConfigValueIncludes.h"
35#include "network/ClientInformation.h"
36#include "network/Host.h"
37#include "objects/controllers/HumanController.h"
38#include "objects/gametypes/Gametype.h"
39
40namespace orxonox
41{
42    CreateUnloadableFactory(HumanPlayer);
43
44    HumanPlayer::HumanPlayer(BaseObject* creator) : PlayerInfo(creator)
45    {
46        RegisterObject(HumanPlayer);
47
48        this->server_ready_ = Core::isMaster();
49        this->client_ready_ = false;
50
51        this->bHumanPlayer_ = true;
52        this->defaultController_ = Class(HumanController);
53
54        this->setConfigValues();
55        this->registerVariables();
56
57COUT(0) << this->getObjectID() << ": HumanPlayer created" << std::endl;
58        network::Synchronisable* temp = dynamic_cast<network::Synchronisable*>(creator);
59        if (temp)
60        {
61COUT(0) << this->getObjectID() << ": CreatorID: " << temp->getObjectID() << std::endl;
62        }
63        else
64        {
65COUT(0) << this->getObjectID() << ": Creator is no Synchronisable" << std::endl;
66        }
67    unsigned int creatorID = network::OBJECTID_UNKNOWN;
68    searchcreatorID:
69    if (creator)
70    {
71        if (creator->isA(Class(Synchronisable)))
72        {
73            Synchronisable* synchronisable_creator = dynamic_cast<Synchronisable*>(creator);
74            creatorID = synchronisable_creator->getObjectID();
75        }
76        else if (creator != creator->getCreator())
77        {
78            creator = creator->getCreator();
79            goto searchcreatorID;
80        }
81    }
82COUT(0) << this->getObjectID() << ": ### tranmitted creatorID: " << creatorID << std::endl;
83    }
84
85    HumanPlayer::~HumanPlayer()
86    {
87COUT(0) << this->getObjectID() << ": HumanPlayer destroyed" << std::endl;
88    }
89
90    void HumanPlayer::setConfigValues()
91    {
92        SetConfigValue(nick_, "Player").callback(this, &HumanPlayer::configvaluecallback_changednick);
93    }
94
95    void HumanPlayer::registerVariables()
96    {
97        REGISTERSTRING(this->synchronize_nick_, network::direction::toserver, new network::NetworkCallback<HumanPlayer>(this, &HumanPlayer::networkcallback_changednick));
98
99        REGISTERDATA(this->clientID_,     network::direction::toclient, new network::NetworkCallback<HumanPlayer>(this, &HumanPlayer::networkcallback_clientIDchanged));
100        REGISTERDATA(this->server_ready_, network::direction::toclient, new network::NetworkCallback<HumanPlayer>(this, &HumanPlayer::networkcallback_server_ready));
101        REGISTERDATA(this->client_ready_, network::direction::toserver, new network::NetworkCallback<HumanPlayer>(this, &HumanPlayer::networkcallback_client_ready));
102    }
103
104    void HumanPlayer::configvaluecallback_changednick()
105    {
106        if (this->isLocalPlayer())
107        {
108            this->synchronize_nick_ = this->nick_;
109
110            if (Core::isMaster())
111                this->setName(this->nick_);
112        }
113    }
114
115    void HumanPlayer::networkcallback_changednick()
116    {
117        this->setName(this->synchronize_nick_);
118    }
119
120    void HumanPlayer::networkcallback_clientIDchanged()
121    {
122COUT(0) << this->getObjectID() << ": PI: clientID changed to " << this->clientID_ << std::endl;
123        if (this->clientID_ == network::Host::getPlayerID())
124        {
125COUT(0) << this->getObjectID() << ": PI: it's my clientID" << std::endl;
126            this->bLocalPlayer_ = true;
127            this->synchronize_nick_ = this->nick_;
128            this->client_ready_ = true;
129
130            if (!Core::isMaster())
131            {
132                this->setObjectMode(network::direction::bidirectional);
133COUT(0) << this->getObjectID() << ": PI: set objectmode to bidirectional" << std::endl;
134            }
135            else
136                this->setName(this->nick_);
137
138            this->createController();
139        }
140    }
141
142    void HumanPlayer::networkcallback_server_ready()
143    {
144        this->client_ready_ = true;
145COUT(0) << this->getObjectID() << ": PI: server ready, client set ready too" << std::endl;
146    }
147
148    void HumanPlayer::networkcallback_client_ready()
149    {
150COUT(0) << this->getObjectID() << ": PI: client ready" << std::endl;
151        if (this->getGametype())
152        {
153COUT(0) << this->getObjectID() << ": PI: adding client to gametype" << std::endl;
154            this->getGametype()->playerEntered(this);
155        }
156    }
157
158    bool HumanPlayer::isReady() const
159    {
160        return (this->server_ready_ && this->client_ready_);
161    }
162
163    float HumanPlayer::getPing() const
164    {
165        return network::ClientInformation::findClient(this->getClientID())->getRTT();
166    }
167
168    float HumanPlayer::getPacketLossRatio() const
169    {
170        return network::ClientInformation::findClient(this->getClientID())->getPacketLoss();
171    }
172
173    void HumanPlayer::setClientID(unsigned int clientID)
174    {
175COUT(0) << this->getObjectID() << ": PI: set clientID to " << clientID << std::endl;
176        this->clientID_ = clientID;
177        this->networkcallback_clientIDchanged();
178    }
179}
Note: See TracBrowser for help on using the repository browser.