Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/release2012/src/orxonox/gametypes/TeamGametype.cc @ 9363

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

Teamdeathmatch ends if a certain number of opponents have been killed. (friendlyfire is not concerned)

  • Property svn:eol-style set to native
File size: 12.5 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_ = 0; //Value "0": no limit is set.
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        if(player == NULL) return; // catch null pointers
78        Gametype::playerEntered(player);
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    /**
92    @brief
93        Function that determines the player's team number when entering the game for the first time.
94        Override this function for other team structures.
95    */
96    void TeamGametype::findAndSetTeam(PlayerInfo* player)
97    {
98        if(player == NULL) return; // catch null pointers
99        std::vector<unsigned int> playersperteam(this->teams_, 0);
100
101        for (std::map<PlayerInfo*, int>::iterator it = this->teamnumbers_.begin(); it != this->teamnumbers_.end(); ++it)
102            if (it->second < static_cast<int>(this->teams_) && it->second >= 0)
103                playersperteam[it->second]++;
104
105        unsigned int minplayers = static_cast<unsigned int>(-1);
106        size_t minplayersteam = 0;
107        for (size_t i = 0; i < this->teams_; ++i)
108        {
109            if (playersperteam[i] < minplayers)
110            {
111                minplayers = playersperteam[i];
112                minplayersteam = i;
113            }
114        }
115
116        this->teamnumbers_[player] = minplayersteam;
117
118    }
119
120    bool TeamGametype::playerLeft(PlayerInfo* player)
121    {
122        bool valid_player = Gametype::playerLeft(player);
123        if( (this->players_.size() >= maxPlayers_) && (allowedInGame_[player] == true) ) // if there's a "waiting list"
124        {
125            for (std::map<PlayerInfo*, bool>::iterator it = this->allowedInGame_.begin(); it != this->allowedInGame_.end(); ++it)
126            {
127                 if(it->second == false) // waiting player found
128                 {it->second = true; break;} // allow player to enter
129            }
130        }
131
132        if (valid_player)
133        {   // clean up the maps
134            this->teamnumbers_.erase(player);
135            this->allowedInGame_.erase(player);
136        }
137
138        return valid_player;
139    }
140
141    void TeamGametype::spawnDeadPlayersIfRequested()
142    {
143        for (std::map<PlayerInfo*, Player>::iterator it = this->players_.begin(); it != this->players_.end(); ++it)\
144        {
145            if(allowedInGame_[it->first] == false)//check if dead player is allowed to enter
146            {
147                continue;
148            }
149            if (it->second.state_ == PlayerState::Dead)
150            {
151                if ((it->first->isReadyToSpawn() || this->bForceSpawn_))
152                {
153                   this->spawnPlayer(it->first);
154                }
155            }
156        }
157    }
158
159
160    bool TeamGametype::allowPawnHit(Pawn* victim, Pawn* originator)
161    {// hit allowed: if victim & originator are foes or if originator doesnot exist or if friendlyfire is allowed
162        return (!this->pawnsAreInTheSameTeam(victim, originator) || !originator || this->allowFriendlyFire_);
163    }
164
165    bool TeamGametype::allowPawnDamage(Pawn* victim, Pawn* originator)
166    {
167        return (!this->pawnsAreInTheSameTeam(victim, originator) || !originator || this->allowFriendlyFire_);
168    }
169
170    bool TeamGametype::allowPawnDeath(Pawn* victim, Pawn* originator)
171    {
172        return (!this->pawnsAreInTheSameTeam(victim, originator) || !originator || this->allowFriendlyFire_);
173    }
174
175    int TeamGametype::getTeamScore(PlayerInfo* player)
176    {
177        int teamscore = 0;
178        if(!player || this->getTeam(player) == -1)
179            return 0;
180        for (std::map<PlayerInfo*, Player>::iterator it = this->players_.begin(); it != this->players_.end(); ++it)
181        {
182            if ( this->getTeam(it->first) ==  this->getTeam(player) )
183            {
184                teamscore += it->second.frags_;
185            }
186        }
187        return teamscore;
188    }
189
190    SpawnPoint* TeamGametype::getBestSpawnPoint(PlayerInfo* player) const
191    {
192        int desiredTeamNr = -1;
193        std::map<PlayerInfo*, int>::const_iterator it_player = this->teamnumbers_.find(player);
194        if (it_player != this->teamnumbers_.end())
195            desiredTeamNr = it_player->second;
196
197        // Only use spawnpoints of the own team (or non-team-spawnpoints)
198        std::set<SpawnPoint*> teamSpawnPoints = this->spawnpoints_;
199        for (std::set<SpawnPoint*>::iterator it = teamSpawnPoints.begin(); it != teamSpawnPoints.end(); )
200        {
201            if ((*it)->isA(Class(TeamSpawnPoint)))
202            {
203                TeamSpawnPoint* tsp = orxonox_cast<TeamSpawnPoint*>(*it);
204                if (tsp && static_cast<int>(tsp->getTeamNumber()) != desiredTeamNr)
205                {
206                    teamSpawnPoints.erase(it++);
207                    continue;
208                }
209            }
210
211            ++it;
212        }
213
214        SpawnPoint* fallbackSpawnPoint = NULL;
215        if (teamSpawnPoints.size() > 0)
216        {
217            unsigned int randomspawn = static_cast<unsigned int>(rnd(static_cast<float>(teamSpawnPoints.size())));
218            unsigned int index = 0;
219            // Get random fallback spawnpoint in case there is no active SpawnPoint.
220            for (std::set<SpawnPoint*>::const_iterator it = teamSpawnPoints.begin(); it != teamSpawnPoints.end(); ++it)
221            {
222                if (index == randomspawn)
223                {
224                    fallbackSpawnPoint = (*it);
225                    break;
226                }
227
228                ++index;
229            }
230
231            // Remove all inactive SpawnPoints from the list.
232            for (std::set<SpawnPoint*>::iterator it = teamSpawnPoints.begin(); it != teamSpawnPoints.end(); )
233            {
234                if(!(*it)->isActive())
235                {
236                    teamSpawnPoints.erase(it++);
237                    continue;
238                }
239
240                ++it;
241            }
242
243            randomspawn = static_cast<unsigned int>(rnd(static_cast<float>(teamSpawnPoints.size())));
244            index = 0;
245            for (std::set<SpawnPoint*>::const_iterator it = teamSpawnPoints.begin(); it != teamSpawnPoints.end(); ++it)
246            {
247                if (index == randomspawn)
248                    return (*it);
249
250                ++index;
251            }
252
253            return fallbackSpawnPoint;
254        }
255
256        return 0;
257    }
258
259    void TeamGametype::playerStartsControllingPawn(PlayerInfo* player, Pawn* pawn)
260    {
261        if (!player)
262            return;
263
264        this->setTeamColour(player,pawn);
265    }
266
267    bool TeamGametype::pawnsAreInTheSameTeam(Pawn* pawn1, Pawn* pawn2)
268    {
269        if (pawn1 && pawn2)
270        {
271            std::map<PlayerInfo*, int>::const_iterator it1 = this->teamnumbers_.find(pawn1->getPlayer());
272            std::map<PlayerInfo*, int>::const_iterator it2 = this->teamnumbers_.find(pawn2->getPlayer());
273
274            if (it1 != this->teamnumbers_.end() && it2 != this->teamnumbers_.end())
275                return (it1->second == it2->second);
276        }
277        return false;
278    }
279
280    int TeamGametype::getTeam(PlayerInfo* player)
281    {
282        std::map<PlayerInfo*, int>::const_iterator it_player = this->teamnumbers_.find(player);
283        if (it_player != this->teamnumbers_.end())
284            return it_player->second;
285        else
286            return -1;
287    }
288
289    void TeamGametype::setTeamColour(PlayerInfo* player, Pawn* pawn)
290    {
291        // Set the team colour
292        std::map<PlayerInfo*, int>::const_iterator it_player = this->teamnumbers_.find(player);
293        if (it_player != this->teamnumbers_.end() && it_player->second >= 0 && it_player->second < static_cast<int>(this->teamcolours_.size()))
294        {
295            this->colourPawn(pawn, it_player->second);
296        }
297    }
298
299    /**
300    @brief
301        Colours a pawn depending on the team values set in XML.
302        A pawn is coloured depending on it's team set via XML.
303        If there is a controller the pawn is coloured depending on its team which also can be set via XML.
304    */
305    void TeamGametype::setDefaultObjectColour(Pawn* pawn)
306    {
307        if(pawn == NULL)
308            return;
309
310        int teamnumber = pawn->getTeam();
311
312        if(teamnumber >= 0)
313        {
314            this->colourPawn(pawn, teamnumber); return;
315        }
316        //get Pawn's controller
317        ControllableEntity* entity = orxonox_cast<ControllableEntity*>(pawn);
318
319        Controller* controller = 0;
320        if (entity->getController())
321            controller = entity->getController();
322        else if (entity->getXMLController())
323            controller = entity->getXMLController();
324        else
325            return;
326
327        ArtificialController* artificial =  orxonox_cast<ArtificialController*>(controller);
328        //get Teamnumber - get the data
329        if(artificial == NULL)
330            return;
331        teamnumber= artificial->getTeam();
332
333        //set ObjectColour
334        this->colourPawn(pawn, teamnumber);
335    }
336
337    void TeamGametype::colourPawn(Pawn* pawn, int teamNr)
338    {// catch: no-colouring-case and wrong input
339        if(teamNr < 0 || teamNr+1 > int(this->teamcolours_.size()) ||pawn == NULL) return;
340        pawn->setRadarObjectColour(this->teamcolours_[teamNr]);
341
342        std::set<WorldEntity*> pawnAttachments = pawn->getAttachedObjects();
343        for (std::set<WorldEntity*>::iterator it = pawnAttachments.begin(); it != pawnAttachments.end(); ++it)
344        {
345            if ((*it)->isA(Class(TeamColourable)))
346            {
347                TeamColourable* tc = orxonox_cast<TeamColourable*>(*it);
348                tc->setTeamColour(this->teamcolours_[teamNr]);
349            }
350         }
351    }
352
353    void TeamGametype::announceTeamWin(int winnerTeam)
354    {
355        for (std::map<PlayerInfo*, int>::iterator it3 = this->teamnumbers_.begin(); it3 != this->teamnumbers_.end(); ++it3)
356        {
357            if (it3->first->getClientID() == NETWORK_PEER_ID_UNKNOWN)
358                continue;
359            if (it3->second == winnerTeam)
360            {
361                this->gtinfo_->sendAnnounceMessage("Your team has won the match!", it3->first->getClientID());
362            }
363            else
364            {
365                this->gtinfo_->sendAnnounceMessage("Your team has lost the match!", it3->first->getClientID());
366            }
367        }   
368    }
369
370}
Note: See TracBrowser for help on using the repository browser.