Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/pch/src/orxonox/objects/gametypes/TeamDeathmatch.cc @ 3183

Last change on this file since 3183 was 3183, checked in by rgrieder, 15 years ago

Renamed Teamcolourable to TeamColourable

  • Property svn:eol-style set to native
File size: 6.4 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 "TeamDeathmatch.h"
30
31#include "core/CoreIncludes.h"
32#include "core/ConfigValueIncludes.h"
33#include "interfaces/TeamColourable.h"
34#include "objects/worldentities/TeamSpawnPoint.h"
35
36namespace orxonox
37{
38    CreateUnloadableFactory(TeamDeathmatch);
39
40    TeamDeathmatch::TeamDeathmatch(BaseObject* creator) : Deathmatch(creator)
41    {
42        RegisterObject(TeamDeathmatch);
43
44        this->teams_ = 2;
45
46        this->setConfigValues();
47    }
48
49    void TeamDeathmatch::setConfigValues()
50    {
51        SetConfigValue(teams_, 2);
52
53        static ColourValue colours[] =
54        {
55            ColourValue(1.0, 0.3, 0.3),
56            ColourValue(0.3, 0.3, 1.0),
57            ColourValue(0.3, 1.0, 0.3),
58            ColourValue(1.0, 1.0, 0.0)
59        };
60        static std::vector<ColourValue> defaultcolours(colours, colours + sizeof(colours) / sizeof(ColourValue));
61
62        SetConfigValueVector(teamcolours_, defaultcolours);
63    }
64
65    void TeamDeathmatch::playerEntered(PlayerInfo* player)
66    {
67        Deathmatch::playerEntered(player);
68
69        std::vector<unsigned int> playersperteam(this->teams_, 0);
70
71        for (std::map<PlayerInfo*, int>::iterator it = this->teamnumbers_.begin(); it != this->teamnumbers_.end(); ++it)
72            if (it->second < (int)this->teams_ && it->second >= 0)
73                playersperteam[it->second]++;
74
75        unsigned int minplayers = (unsigned int)-1;
76        size_t minplayersteam = 0;
77        for (size_t i = 0; i < this->teams_; ++i)
78        {
79            if (playersperteam[i] < minplayers)
80            {
81                minplayers = playersperteam[i];
82                minplayersteam = i;
83            }
84        }
85
86        this->teamnumbers_[player] = minplayersteam;
87    }
88
89    bool TeamDeathmatch::playerLeft(PlayerInfo* player)
90    {
91        bool valid_player = Deathmatch::playerLeft(player);
92
93        if (valid_player)
94            this->players_.erase(player);
95
96        return valid_player;
97    }
98
99    bool TeamDeathmatch::allowPawnHit(Pawn* victim, Pawn* originator)
100    {
101        return (!this->pawnsAreInTheSameTeam(victim, originator));
102    }
103
104    bool TeamDeathmatch::allowPawnDamage(Pawn* victim, Pawn* originator)
105    {
106        return (!this->pawnsAreInTheSameTeam(victim, originator));
107    }
108
109    bool TeamDeathmatch::allowPawnDeath(Pawn* victim, Pawn* originator)
110    {
111        return (!this->pawnsAreInTheSameTeam(victim, originator));
112    }
113
114    SpawnPoint* TeamDeathmatch::getBestSpawnPoint(PlayerInfo* player) const
115    {
116        int desiredTeamNr = -1;
117        std::map<PlayerInfo*, int>::const_iterator it_player = this->teamnumbers_.find(player);
118        if (it_player != this->teamnumbers_.end())
119            desiredTeamNr = it_player->second;
120
121        // Only use spawnpoints of the own team (or non-team-spawnpoints)
122        std::set<SpawnPoint*> teamSpawnPoints = this->spawnpoints_;
123        for (std::set<SpawnPoint*>::iterator it = teamSpawnPoints.begin(); it != teamSpawnPoints.end(); )
124        {
125            if ((*it)->isA(Class(TeamSpawnPoint)))
126            {
127                TeamSpawnPoint* tsp = dynamic_cast<TeamSpawnPoint*>(*it);
128                if (tsp && (int)tsp->getTeamNumber() != desiredTeamNr)
129                {
130                    teamSpawnPoints.erase(it++);
131                    continue;
132                }
133            }
134
135            ++it;
136        }
137
138        if (teamSpawnPoints.size() > 0)
139        {
140            unsigned int randomspawn = (unsigned int)rnd(teamSpawnPoints.size());
141            unsigned int index = 0;
142            for (std::set<SpawnPoint*>::const_iterator it = teamSpawnPoints.begin(); it != teamSpawnPoints.end(); ++it)
143            {
144                if (index == randomspawn)
145                    return (*it);
146
147                ++index;
148            }
149        }
150
151        return 0;
152    }
153
154    void TeamDeathmatch::playerStartsControllingPawn(PlayerInfo* player, Pawn* pawn)
155    {
156        if (!player)
157            return;
158
159        // Set the team colour
160        std::map<PlayerInfo*, int>::const_iterator it_player = this->teamnumbers_.find(player);
161        if (it_player != this->teamnumbers_.end() && it_player->second >= 0 && it_player->second < (int)this->teamcolours_.size())
162        {
163            if (pawn)
164            {
165                pawn->setRadarObjectColour(this->teamcolours_[it_player->second]);
166
167                std::set<WorldEntity*> pawnAttachments = pawn->getAttachedObjects();
168                for (std::set<WorldEntity*>::iterator it = pawnAttachments.begin(); it != pawnAttachments.end(); ++it)
169                {
170                    if ((*it)->isA(Class(TeamColourable)))
171                    {
172                        TeamColourable* tc = dynamic_cast<TeamColourable*>(*it);
173                        tc->setTeamColour(this->teamcolours_[it_player->second]);
174                    }
175                }
176            }
177        }
178    }
179
180    bool TeamDeathmatch::pawnsAreInTheSameTeam(Pawn* pawn1, Pawn* pawn2)
181    {
182        if (pawn1 && pawn2)
183        {
184            std::map<PlayerInfo*, int>::const_iterator it1 = this->teamnumbers_.find(pawn1->getPlayer());
185            std::map<PlayerInfo*, int>::const_iterator it2 = this->teamnumbers_.find(pawn2->getPlayer());
186
187            if (it1 != this->teamnumbers_.end() && it2 != this->teamnumbers_.end())
188                return (it1->second == it2->second);
189        }
190        return false;
191    }
192
193    int TeamDeathmatch::getTeam(PlayerInfo* player)
194    {
195        std::map<PlayerInfo*, int>::const_iterator it_player = this->teamnumbers_.find(player);
196        if (it_player != this->teamnumbers_.end())
197            return it_player->second;
198        else
199            return -1;
200    }
201}
Note: See TracBrowser for help on using the repository browser.