Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/gamecontent/src/orxonox/gametypes/TeamGametype.cc @ 8956

Last change on this file since 8956 was 8956, checked in by jo, 12 years ago

implementing first player control parameter.

File size: 10.8 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 *      Johannes Ritz
24 *   Co-authors:
25 *      ...
26 *
27 */
28
29#include "TeamGametype.h"
30
31#include "core/CoreIncludes.h"
32#include "core/ConfigValueIncludes.h"
33#include "infos/PlayerInfo.h"
34#include "interfaces/TeamColourable.h"
35#include "worldentities/TeamSpawnPoint.h"
36#include "worldentities/pawns/Pawn.h"
37#include "worldentities/ControllableEntity.h"
38#include "controllers/ArtificialController.h"
39
40namespace orxonox
41{
42    CreateUnloadableFactory(TeamGametype);
43
44    TeamGametype::TeamGametype(BaseObject* creator) : Gametype(creator)
45    {
46        RegisterObject(TeamGametype);
47
48        this->teams_ = 2; //most team gametypes use two different teams
49        this->allowFriendlyFire_ = false;
50        //this->playersPerTeam_ = 0;
51        this->maxPlayers_ = 2; //TEST
52        this->setConfigValues();
53    }
54
55    void TeamGametype::setConfigValues()
56    {
57        SetConfigValue(teams_, 2);
58
59        static ColourValue colours[] =
60        {
61            ColourValue(0.2f, 0.2f, 1.0f),
62            ColourValue(1.0f, 0.1f, 0.1f),
63            ColourValue(0.3f, 1.0f, 0.3f),
64            ColourValue(1.0f, 1.0f, 0.0f),
65            ColourValue(0.0f, 1.0f, 1.0f),
66            ColourValue(1.0f, 0.0f, 1.0f),
67            ColourValue(7.0f, 7.0f, 7.0f),
68            ColourValue(2.0f, 2.0f, 2.0f)
69        };
70        static std::vector<ColourValue> defaultcolours(colours, colours + sizeof(colours) / sizeof(ColourValue));
71
72        SetConfigValue(teamcolours_, defaultcolours);
73    }
74
75    void TeamGametype::playerEntered(PlayerInfo* player)
76    {
77        Gametype::playerEntered(player);
78        if(player == NULL) return;
79        this->findAndSetTeam(player);
80        if( this->players_.size() <= maxPlayers_ || maxPlayers_  ==  0)
81        {
82            this->allowedInGame_[player]= true;
83        }
84        else
85        {
86            this->allowedInGame_[player]= false;
87            orxout() << "not allowed in game: players = " << this->players_.size() << " > maximum: " << maxPlayers_ << endl;
88        }
89    }
90
91    void TeamGametype::findAndSetTeam(PlayerInfo* player)
92    {
93         std::vector<unsigned int> playersperteam(this->teams_, 0);
94
95        for (std::map<PlayerInfo*, int>::iterator it = this->teamnumbers_.begin(); it != this->teamnumbers_.end(); ++it)
96            if (it->second < static_cast<int>(this->teams_) && it->second >= 0)
97                playersperteam[it->second]++;
98
99        unsigned int minplayers = static_cast<unsigned int>(-1);
100        size_t minplayersteam = 0;
101        for (size_t i = 0; i < this->teams_; ++i)
102        {
103            if (playersperteam[i] < minplayers)
104            {
105                minplayers = playersperteam[i];
106                minplayersteam = i;
107            }
108        }
109
110        this->teamnumbers_[player] = minplayersteam;
111
112    }
113
114    bool TeamGametype::playerLeft(PlayerInfo* player)
115    {
116        bool valid_player = Gametype::playerLeft(player);
117        if( (this->players_.size() >= maxPlayers_) && (allowedInGame_[player] == true) ) // if there's a "waiting list"
118        {
119            for (std::map<PlayerInfo*, bool>::iterator it = this->allowedInGame_.begin(); it != this->allowedInGame_.end(); ++it)
120            {
121                 if(it->second == false) // waiting player found
122                 {it->second = true; break;} // allow player to enter
123                        }
124                }
125
126        if (valid_player)
127        {   // clean up the maps
128            this->teamnumbers_.erase(player);
129            this->allowedInGame_.erase(player);
130        }
131
132
133        return valid_player;
134    }
135
136    void TeamGametype::spawnDeadPlayersIfRequested()
137    {
138        for (std::map<PlayerInfo*, Player>::iterator it = this->players_.begin(); it != this->players_.end(); ++it)\
139        {
140            if(allowedInGame_[it->first] == false)//check if dead player is allowed to enter
141            {
142                                continue;
143            }
144            if (it->second.state_ == PlayerState::Dead)
145            {
146                if ((it->first->isReadyToSpawn() || this->bForceSpawn_))
147                {
148                   this->spawnPlayer(it->first);
149                }
150            }
151        }
152    }
153
154
155    bool TeamGametype::allowPawnHit(Pawn* victim, Pawn* originator)
156    {// hit allowed: if victim & originator are foes or if originator doesnot exist or if friendlyfire is allowed
157        return (!this->pawnsAreInTheSameTeam(victim, originator) || !originator || this->allowFriendlyFire_);
158    }
159
160    bool TeamGametype::allowPawnDamage(Pawn* victim, Pawn* originator)
161    {
162        return (!this->pawnsAreInTheSameTeam(victim, originator) || !originator || this->allowFriendlyFire_);
163    }
164
165    bool TeamGametype::allowPawnDeath(Pawn* victim, Pawn* originator)
166    {
167        return (!this->pawnsAreInTheSameTeam(victim, originator) || !originator || this->allowFriendlyFire_);
168    }
169
170    SpawnPoint* TeamGametype::getBestSpawnPoint(PlayerInfo* player) const
171    {
172        int desiredTeamNr = -1;
173        std::map<PlayerInfo*, int>::const_iterator it_player = this->teamnumbers_.find(player);
174        if (it_player != this->teamnumbers_.end())
175            desiredTeamNr = it_player->second;
176
177        // Only use spawnpoints of the own team (or non-team-spawnpoints)
178        std::set<SpawnPoint*> teamSpawnPoints = this->spawnpoints_;
179        for (std::set<SpawnPoint*>::iterator it = teamSpawnPoints.begin(); it != teamSpawnPoints.end(); )
180        {
181            if ((*it)->isA(Class(TeamSpawnPoint)))
182            {
183                TeamSpawnPoint* tsp = orxonox_cast<TeamSpawnPoint*>(*it);
184                if (tsp && static_cast<int>(tsp->getTeamNumber()) != desiredTeamNr)
185                {
186                    teamSpawnPoints.erase(it++);
187                    continue;
188                }
189            }
190
191            ++it;
192        }
193
194        SpawnPoint* fallbackSpawnPoint = NULL;
195        if (teamSpawnPoints.size() > 0)
196        {
197            unsigned int randomspawn = static_cast<unsigned int>(rnd(static_cast<float>(teamSpawnPoints.size())));
198            unsigned int index = 0;
199            // Get random fallback spawnpoint in case there is no active SpawnPoint.
200            for (std::set<SpawnPoint*>::const_iterator it = teamSpawnPoints.begin(); it != teamSpawnPoints.end(); ++it)
201            {
202                if (index == randomspawn)
203                {
204                    fallbackSpawnPoint = (*it);
205                    break;
206                }
207
208                ++index;
209            }
210
211            // Remove all inactive SpawnPoints from the list.
212            for (std::set<SpawnPoint*>::iterator it = teamSpawnPoints.begin(); it != teamSpawnPoints.end(); )
213            {
214                if(!(*it)->isActive())
215                {
216                    teamSpawnPoints.erase(it++);
217                    continue;
218                }
219
220                ++it;
221            }
222
223            randomspawn = static_cast<unsigned int>(rnd(static_cast<float>(teamSpawnPoints.size())));
224            index = 0;
225            for (std::set<SpawnPoint*>::const_iterator it = teamSpawnPoints.begin(); it != teamSpawnPoints.end(); ++it)
226            {
227                if (index == randomspawn)
228                    return (*it);
229
230                ++index;
231            }
232
233            return fallbackSpawnPoint;
234        }
235
236        return 0;
237    }
238
239    void TeamGametype::playerStartsControllingPawn(PlayerInfo* player, Pawn* pawn)
240    {
241        if (!player)
242            return;
243
244        this->setTeamColour(player,pawn);
245    }
246
247    bool TeamGametype::pawnsAreInTheSameTeam(Pawn* pawn1, Pawn* pawn2)
248    {
249        if (pawn1 && pawn2)
250        {
251            std::map<PlayerInfo*, int>::const_iterator it1 = this->teamnumbers_.find(pawn1->getPlayer());
252            std::map<PlayerInfo*, int>::const_iterator it2 = this->teamnumbers_.find(pawn2->getPlayer());
253
254            if (it1 != this->teamnumbers_.end() && it2 != this->teamnumbers_.end())
255                return (it1->second == it2->second);
256        }
257        return false;
258    }
259
260    int TeamGametype::getTeam(PlayerInfo* player)
261    {
262        std::map<PlayerInfo*, int>::const_iterator it_player = this->teamnumbers_.find(player);
263        if (it_player != this->teamnumbers_.end())
264            return it_player->second;
265        else
266            return -1;
267    }
268
269    void TeamGametype::setTeamColour(PlayerInfo* player, Pawn* pawn)
270    {
271        // Set the team colour
272        std::map<PlayerInfo*, int>::const_iterator it_player = this->teamnumbers_.find(player);
273        if (it_player != this->teamnumbers_.end() && it_player->second >= 0 && it_player->second < static_cast<int>(this->teamcolours_.size()))
274        {
275            this->colourPawn(pawn, it_player->second);
276        }
277    }
278
279    void TeamGametype::setDefaultObjectColour(Pawn* pawn)
280    {
281        if(pawn == NULL)
282            return;
283
284        int teamnumber = pawn->getTeam();
285
286        if(teamnumber >= 0)
287        {
288            this->colourPawn(pawn, teamnumber); return;
289        }
290        //get Pawn's controller
291        ControllableEntity* entity = orxonox_cast<ControllableEntity*>(pawn);
292
293        Controller* controller = 0;
294        if (entity->getController())
295            controller = entity->getController();
296        else if (entity->getXMLController())
297            controller = entity->getXMLController();
298        else
299            return;
300
301        ArtificialController* artificial =  orxonox_cast<ArtificialController*>(controller);
302        //get Teamnumber - get the data
303        if(artificial == NULL)
304            return;
305        teamnumber= artificial->getTeam();
306
307        //set ObjectColour
308        this->colourPawn(pawn, teamnumber);
309    }
310
311    void TeamGametype::colourPawn(Pawn* pawn, int teamNr)
312    {// catch no-colouring-case and wrong input
313        if(teamNr < 0 || pawn == NULL) return;
314        pawn->setRadarObjectColour(this->teamcolours_[teamNr]);
315
316        std::set<WorldEntity*> pawnAttachments = pawn->getAttachedObjects();
317        for (std::set<WorldEntity*>::iterator it = pawnAttachments.begin(); it != pawnAttachments.end(); ++it)
318        {
319            if ((*it)->isA(Class(TeamColourable)))
320            {
321                TeamColourable* tc = orxonox_cast<TeamColourable*>(*it);
322                tc->setTeamColour(this->teamcolours_[teamNr]);
323            }
324         }
325    }
326
327}
Note: See TracBrowser for help on using the repository browser.