Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/miniprojects/src/orxonox/objects/gametypes/TeamGametype.cc @ 2768

Last change on this file since 2768 was 2768, checked in by landauf, 15 years ago

added TeamGametype

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