Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/orxonox/gametypes/TeamBaseMatch.cc @ 5738

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

merged libraries2 back to trunk

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