Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/captureTheFlag/src/modules/gametypes/CaptureTheFlag.cc @ 9234

Last change on this file since 9234 was 9234, checked in by decapitb, 12 years ago

resolving conflict… i hope

File size: 11.6 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 *      Nino Weingart
24 *   Co-authors:
25 *      ...
26 *
27 */
28#include "CaptureTheFlag.h"
29
30#include "core/CoreIncludes.h"
31#include "core/ConfigValueIncludes.h"
32#include "interfaces/TeamColourable.h"
33#include "worldentities/TeamSpawnPoint.h"
34#include "worldentities/pawns/Pawn.h"
35
36
37
38namespace orxonox
39{
40    CreateUnloadableFactory(CaptureTheFlag);
41
42    CaptureTheFlag::CaptureTheFlag(BaseObject* creator) : TeamBaseMatch(creator)
43    {
44        RegisterObject(CaptureTheFlag);
45       
46        this->teams_ = 2;
47        this->pointsTeam1_ = 0;
48        this->pointsTeam2_ = 0;
49       
50        //this->flagTeamBlue = new FlagPickup(this);
51        //this->flagTeamRed = new FlagPickup(this);
52
53        this->setConfigValues();
54    }
55
56    void CaptureTheFlag::tick(float dt){
57        if(this->flagTeamBlue->isPickedUp()){
58                if(getTeam(this->flagTeamBlue->pickedUpBy()->getPlayer()) == 1){
59                    this->flagTeamBlue->ignorePickedUp();
60                   
61                        if(this->flagTeamBlue->pickedUpBy()->hasFlag()){
62                                pointsTeam1_ += 1000;
63                                this->flagTeamBlue->pickedUpBy()->setHasFlag(false);
64                        }
65                        this->flagTeamBlue->setPickedUp(false);
66                }
67                else{
68                        this->flagTeamBlue->pickedUpBy()->setHasFlag(true);
69                }
70        }
71        if(this->flagTeamRed->isPickedUp()){
72                if(getTeam(this->flagTeamRed->pickedUpBy()->getPlayer()) == 0){
73                    this->flagTeamRed->ignorePickedUp();
74                   
75                        if(this->flagTeamRed->pickedUpBy()->hasFlag()){
76                                pointsTeam1_ += 1000;
77                                this->flagTeamRed->pickedUpBy()->setHasFlag(false);
78                        }
79                        this->flagTeamRed->setPickedUp(false);
80                }
81                else{
82                        this->flagTeamRed->pickedUpBy()->setHasFlag(true);
83                }
84        }
85    }
86    void CaptureTheFlag::setConfigValues()
87    {
88        SetConfigValue(teams_, 2);
89
90        static ColourValue colours[] =
91        {
92            ColourValue(1.0f, 0.3f, 0.3f),
93            ColourValue(0.3f, 0.3f, 1.0f),
94            ColourValue(0.3f, 1.0f, 0.3f),
95            ColourValue(1.0f, 1.0f, 0.0f)
96        };
97        static std::vector<ColourValue> defaultcolours(colours, colours + sizeof(colours) / sizeof(ColourValue));
98
99        SetConfigValue(teamcolours_, defaultcolours);
100    }
101
102    void CaptureTheFlag::playerEntered(PlayerInfo* player)
103    {
104        Deathmatch::playerEntered(player);
105
106        std::vector<unsigned int> playersperteam(this->teams_, 0);
107
108        for (std::map<PlayerInfo*, int>::iterator it = this->teamnumbers_.begin(); it != this->teamnumbers_.end(); ++it)
109            if (it->second < static_cast<int>(this->teams_) && it->second >= 0)
110                playersperteam[it->second]++;
111
112        unsigned int minplayers = static_cast<unsigned int>(-1);
113        size_t minplayersteam = 0;
114        for (size_t i = 0; i < this->teams_; ++i)
115        {
116            if (playersperteam[i] < minplayers)
117            {
118                minplayers = playersperteam[i];
119                minplayersteam = i;
120            }
121        }
122
123        this->teamnumbers_[player] = minplayersteam;
124    }
125
126    bool CaptureTheFlag::playerLeft(PlayerInfo* player)
127    {
128        bool valid_player = Deathmatch::playerLeft(player);
129
130        if (valid_player)
131            this->teamnumbers_.erase(player);
132
133        return valid_player;
134    }
135
136    bool CaptureTheFlag::allowPawnHit(Pawn* victim, Pawn* originator)
137    {
138        return (!this->pawnsAreInTheSameTeam(victim, originator) || !originator);
139    }
140
141    bool CaptureTheFlag::allowPawnDamage(Pawn* victim, Pawn* originator)
142    {
143        return (!this->pawnsAreInTheSameTeam(victim, originator) || !originator);
144    }
145
146    bool CaptureTheFlag::allowPawnDeath(Pawn* victim, Pawn* originator)
147    {
148        return (!this->pawnsAreInTheSameTeam(victim, originator) || !originator);
149    }
150
151    // collect Points for killing oppenents
152        void CaptureTheFlag::playerScored(PlayerInfo* player)
153        {
154            int teamnr = this->getTeam(player);
155            this->addTeamPoints(teamnr, 5);
156        }
157       
158       
159        // show points or each interval of time
160        void CaptureTheFlag::showPoints()
161        {
162            if (!this->hasStarted() || this->hasEnded())
163                return;
164
165            orxout(message) << "Points standing:" << '\n' << "Team Blue: "<< pointsTeam1_ << '\n' << "Team Red: " << pointsTeam2_ << endl;
166            if(pointsTeam1_ >=1700 && pointsTeam1_ < 2000) orxout(message) << "Team Blue is near victory!" << endl;
167            if(pointsTeam2_ >=1700 && pointsTeam2_ < 2000) orxout(message) << "Team Red is near victory!" << endl;
168        }
169       
170        // end game if one team reaches 2000 points
171        void CaptureTheFlag::endGame()
172        {
173            if (this->pointsTeam1_ >= 2000 || this->pointsTeam2_ >= 2000)
174            {
175                int winningteam = -1;
176
177                if (this->pointsTeam1_ > this->pointsTeam2_)
178                {
179                    orxout(message) << "Team Blue has won the match" << endl;
180                    winningteam = 0;
181                }
182                else
183                {
184                    orxout(message) << "Team Red has won the match" << endl;
185                    winningteam = 1;
186                }
187
188                for (std::map<PlayerInfo*, int>::iterator it = this->teamnumbers_.begin(); it != this->teamnumbers_.end(); ++it)
189                {
190                    if (it->first->getClientID() == NETWORK_PEER_ID_UNKNOWN)
191                        continue;
192
193                    if (it->second == winningteam)
194                        this->gtinfo_->sendAnnounceMessage("You have won the match!", it->first->getClientID());
195                    else
196                        this->gtinfo_->sendAnnounceMessage("You have lost the match!", it->first->getClientID());
197                }
198
199                this->end();
200                this->scoreTimer_.stopTimer();
201                this->outputTimer_.stopTimer();
202            }
203        }
204       
205        // this function is called by the function winPoints() which adds points to the teams for every base and killed openents at a certain time
206        void CaptureTheFlag::addTeamPoints(int team, int points)
207        {
208            if(team == 0)
209            {
210                this->pointsTeam1_ += points;
211            }
212            if(team == 1)
213            {
214                this->pointsTeam2_ += points;
215            }
216
217            this->endGame();
218        }
219
220        int TeamBaseMatch::getTeamPoints(int team)
221        {
222            if(team == 0)
223            {
224                return this->pointsTeam1_;
225            }
226            if(team == 1)
227            {
228                return this->pointsTeam2_;
229            }
230
231            return 0;
232        }
233       
234    SpawnPoint* CaptureTheFlag::getBestSpawnPoint(PlayerInfo* player) const
235    {
236        int desiredTeamNr = -1;
237        std::map<PlayerInfo*, int>::const_iterator it_player = this->teamnumbers_.find(player);
238        if (it_player != this->teamnumbers_.end())
239            desiredTeamNr = it_player->second;
240
241        // Only use spawnpoints of the own team (or non-team-spawnpoints)
242        std::set<SpawnPoint*> teamSpawnPoints = this->spawnpoints_;
243        for (std::set<SpawnPoint*>::iterator it = teamSpawnPoints.begin(); it != teamSpawnPoints.end(); )
244        {
245            if ((*it)->isA(Class(TeamSpawnPoint)))
246            {
247                TeamSpawnPoint* tsp = orxonox_cast<TeamSpawnPoint*>(*it);
248                if (tsp && static_cast<int>(tsp->getTeamNumber()) != desiredTeamNr)
249                {
250                    teamSpawnPoints.erase(it++);
251                    continue;
252                }
253            }
254
255            ++it;
256        }
257
258        SpawnPoint* fallbackSpawnPoint = NULL;
259        if (teamSpawnPoints.size() > 0)
260        {
261            unsigned int randomspawn = static_cast<unsigned int>(rnd(static_cast<float>(teamSpawnPoints.size())));
262            unsigned int index = 0;
263            // Get random fallback spawnpoint in case there is no active SpawnPoint.
264            for (std::set<SpawnPoint*>::const_iterator it = teamSpawnPoints.begin(); it != teamSpawnPoints.end(); ++it)
265            {
266                if (index == randomspawn)
267                {
268                    fallbackSpawnPoint = (*it);
269                    break;
270                }
271
272                ++index;
273            }
274
275            // Remove all inactive SpawnPoints from the list.
276            for (std::set<SpawnPoint*>::iterator it = teamSpawnPoints.begin(); it != teamSpawnPoints.end(); )
277            {
278                if(!(*it)->isActive())
279                {
280                    teamSpawnPoints.erase(it++);
281                    continue;
282                }
283
284                ++it;
285            }
286
287            randomspawn = static_cast<unsigned int>(rnd(static_cast<float>(teamSpawnPoints.size())));
288            index = 0;
289            for (std::set<SpawnPoint*>::const_iterator it = teamSpawnPoints.begin(); it != teamSpawnPoints.end(); ++it)
290            {
291                if (index == randomspawn)
292                    return (*it);
293
294                ++index;
295            }
296
297            return fallbackSpawnPoint;
298        }
299
300        return 0;
301    }
302
303    void CaptureTheFlag::playerStartsControllingPawn(PlayerInfo* player, Pawn* pawn)
304    {
305        if (!player)
306            return;
307
308        // Set the team colour
309        std::map<PlayerInfo*, int>::const_iterator it_player = this->teamnumbers_.find(player);
310        if (it_player != this->teamnumbers_.end() && it_player->second >= 0 && it_player->second < static_cast<int>(this->teamcolours_.size()))
311        {
312            if (pawn)
313            {
314                pawn->setRadarObjectColour(this->teamcolours_[it_player->second]);
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_[it_player->second]);
323                    }
324                }
325            }
326        }
327    }
328
329    bool CaptureTheFlag::pawnsAreInTheSameTeam(Pawn* pawn1, Pawn* pawn2)
330    {
331        if (pawn1 && pawn2)
332        {
333            std::map<PlayerInfo*, int>::const_iterator it1 = this->teamnumbers_.find(pawn1->getPlayer());
334            std::map<PlayerInfo*, int>::const_iterator it2 = this->teamnumbers_.find(pawn2->getPlayer());
335
336            if (it1 != this->teamnumbers_.end() && it2 != this->teamnumbers_.end())
337                return (it1->second == it2->second);
338        }
339        return false;
340    }
341
342    int CaptureTheFlag::getTeam(PlayerInfo* player)
343    {
344        std::map<PlayerInfo*, int>::const_iterator it_player = this->teamnumbers_.find(player);
345        if (it_player != this->teamnumbers_.end())
346            return it_player->second;
347        else
348            return -1;
349    }
350}
Note: See TracBrowser for help on using the repository browser.