Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/pch/src/orxonox/objects/gametypes/TeamBaseMatch.cc @ 3179

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

Cleanup in gametypes, infos and items

  • Property svn:eol-style set to native
File size: 8.2 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 *      Val Mikos
24 *   Co-authors:
25 *      ...
26 *
27 */
28
29#include "TeamBaseMatch.h"
30
31#include "util/Debug.h"
32#include "core/CoreIncludes.h"
33#include "core/Executor.h"
34#include "objects/worldentities/pawns/TeamBaseMatchBase.h"
35#include "objects/infos/PlayerInfo.h"
36
37namespace orxonox
38{
39    CreateUnloadableFactory(TeamBaseMatch);
40
41    TeamBaseMatch::TeamBaseMatch(BaseObject* creator) : TeamDeathmatch(creator)
42    {
43        RegisterObject(TeamBaseMatch);
44
45        this->scoreTimer_.setTimer(10, true, this, createExecutor(createFunctor(&TeamBaseMatch::winPoints)));
46        this->outputTimer_.setTimer(10, true, this, createExecutor(createFunctor(&TeamBaseMatch::showPoints)));
47
48        this->pointsTeam1_ = 0;
49        this->pointsTeam2_ = 0;
50
51        this->setHUDTemplate("TeamBaseMatchHUD");
52    }
53
54    // Change the control of the defeated base and respawn it with its initial health
55    bool TeamBaseMatch::allowPawnDeath(Pawn* victim, Pawn* originator)
56    {
57        TeamBaseMatchBase* base = dynamic_cast<TeamBaseMatchBase*>(victim);
58        if (base)
59        {
60            if (!originator)
61                return false;
62
63            std::set<TeamBaseMatchBase*>::const_iterator it = this->bases_.find(base);
64            if (it != this->bases_.end())
65            {
66                int teamnr = this->getTeam(originator->getPlayer());
67                if (teamnr == 0)
68                {
69                    base->setState(BaseState::controlTeam1);
70                    this->gtinfo_.sendAnnounceMessage("The red team captured a base");
71                }
72                if (teamnr == 1)
73                {
74                    base->setState(BaseState::controlTeam2);
75                    this->gtinfo_.sendAnnounceMessage("The blue team captured a base");
76                }
77            }
78
79            victim->setHealth(victim->getInitialHealth());
80            return false;
81        }
82
83        return TeamDeathmatch::allowPawnDeath(victim, originator);
84    }
85
86
87    // if the player is in the same team as the base, he can't make any damage to it
88    bool TeamBaseMatch::allowPawnDamage(Pawn* victim, Pawn* originator)
89    {
90        TeamBaseMatchBase* base = dynamic_cast<TeamBaseMatchBase*>(victim);
91        if (base)
92        {
93            std::set<TeamBaseMatchBase*>::const_iterator it = this->bases_.find(base);
94            if (it != this->bases_.end())
95                return (!this->pawnsAreInTheSameTeam(originator, base));
96        }
97        return TeamDeathmatch::allowPawnDamage(victim, originator);
98    }
99
100    bool TeamBaseMatch::pawnsAreInTheSameTeam(Pawn* pawn1, TeamBaseMatchBase* base)
101    {
102        if (pawn1 && base)
103        {
104            std::map<PlayerInfo*, int>::const_iterator it1 = this->teamnumbers_.find(pawn1->getPlayer());
105            int teamnrbase = -1;
106            int teamnrplayer = this->getTeam(pawn1->getPlayer());
107
108            switch (base->getState())
109            {
110                case BaseState::controlTeam1:
111                    teamnrbase = 0;
112                    break;
113                case BaseState::controlTeam2:
114                    teamnrbase = 1;
115                    break;
116                case BaseState::uncontrolled:
117                default:
118                    teamnrbase = -1;
119            }
120
121            if (teamnrbase == teamnrplayer)
122                return true;
123        }
124        return false;
125    }
126
127
128
129
130
131    // collect Points for killing oppenents
132    void TeamBaseMatch::playerScored(PlayerInfo* player)
133    {
134        int teamnr = this->getTeam(player);
135        this->addTeamPoints(teamnr, 5);
136    }
137
138    // show points or each interval of time
139    void TeamBaseMatch::showPoints()
140    {
141        if (!this->hasStarted() || this->hasEnded())
142            return;
143
144        COUT(0) << "Points standing:" << std::endl << "Team 1: "<< pointsTeam1_ << std::endl << "Team 2: " << pointsTeam2_ << std::endl;
145        if(pointsTeam1_ >=1700 && pointsTeam1_ < 2000) COUT(0) << "Team 1 is near victory!" << std::endl;
146        if(pointsTeam2_ >=1700 && pointsTeam2_ < 2000) COUT(0) << "Team 2 is near victory!" << std::endl;
147    }
148
149
150    // collect Points while controlling Bases
151    void TeamBaseMatch::winPoints()
152    {
153        int amountControlled = 0;
154        int amountControlled2 = 0;
155
156        for (std::set<TeamBaseMatchBase*>::const_iterator it = this->bases_.begin(); it != this->bases_.end(); ++it)
157        {
158            if((*it)->getState() == BaseState::controlTeam1)
159            {
160                amountControlled++;
161            }
162            if((*it)->getState() == BaseState::controlTeam2)
163            {
164                amountControlled2++;
165            }
166        }
167
168        this->addTeamPoints(0, (amountControlled * 30));
169        this->addTeamPoints(1, (amountControlled2 * 30));
170    }
171
172
173    // end game if one team reaches 2000 points
174    void TeamBaseMatch::endGame()
175    {
176        if (this->pointsTeam1_ >= 2000 || this->pointsTeam2_ >= 2000)
177        {
178            int winningteam = -1;
179
180            if (this->pointsTeam1_ > this->pointsTeam2_)
181            {
182                COUT(0) << "Team 1 has won the match" << std::endl;
183                winningteam = 0;
184            }
185            else
186            {
187                COUT(0) << "Team 2 has won the match" << std::endl;
188                winningteam = 1;
189            }
190
191            for (std::map<PlayerInfo*, int>::iterator it = this->teamnumbers_.begin(); it != this->teamnumbers_.end(); ++it)
192            {
193                if (it->first->getClientID() == CLIENTID_UNKNOWN)
194                    continue;
195
196                if (it->second == winningteam)
197                    this->gtinfo_.sendAnnounceMessage("You have won the match!", it->first->getClientID());
198                else
199                    this->gtinfo_.sendAnnounceMessage("You have lost the match!", it->first->getClientID());
200            }
201
202            this->end();
203            this->scoreTimer_.stopTimer();
204            this->outputTimer_.stopTimer();
205        }
206    }
207
208
209    // this function is called by the function winPoints() which adds points to the teams for every base and killed openents at a certain time
210    void TeamBaseMatch::addTeamPoints(int team, int points)
211    {
212        if(team == 0)
213        {
214            this->pointsTeam1_ += points;
215        }
216        if(team == 1)
217        {
218            this->pointsTeam2_ += points;
219        }
220
221        this->endGame();
222    }
223
224    int TeamBaseMatch::getTeamPoints(int team)
225    {
226        if(team == 0)
227        {
228            return this->pointsTeam1_;
229        }
230        if(team == 1)
231        {
232            return this->pointsTeam2_;
233        }
234
235        return 0;
236    }
237
238    int TeamBaseMatch::getTeamBases(int team)
239    {
240        int count = 0;
241
242        for (std::set<TeamBaseMatchBase*>::const_iterator it = this->bases_.begin(); it != this->bases_.end(); ++it)
243        {
244            if ((*it)->getState() == BaseState::controlTeam1 && team == 0)
245                count++;
246            if ((*it)->getState() == BaseState::controlTeam2 && team == 1)
247                count++;
248        }
249
250        return count;
251    }
252
253    void TeamBaseMatch::addBase(TeamBaseMatchBase* base)
254    {
255        this->bases_.insert(base);
256        base->setState(BaseState::uncontrolled);
257    }
258
259    TeamBaseMatchBase* TeamBaseMatch::getBase(unsigned int index) const
260    {
261        unsigned int i = 0;
262        for (std::set<TeamBaseMatchBase*>::const_iterator it = this->bases_.begin(); it != this->bases_.end(); ++it)
263        {
264            i++;
265            if (i > index)
266                return (*it);
267        }
268        return 0;
269    }
270
271}
272
273
Note: See TracBrowser for help on using the repository browser.