Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 3103 was 3103, checked in by scheusso, 15 years ago

some fixes and changes (also signalhandler)

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