Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 2985 was 2985, checked in by vmikos, 15 years ago

final version

File size: 6.8 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: Val Mikos
23 */
24 
25 
26
27#include "TeamBaseMatch.h"
28
29
30#include "objects/worldentities/pawns/TeamBaseMatchBase.h"
31#include "core/CoreIncludes.h"
32#include "core/XMLPort.h"
33
34 
35namespace orxonox
36{
37    CreateUnloadableFactory(TeamBaseMatch);
38
39
40    // Timer and Creator
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(30, true, this, createExecutor(createFunctor(&TeamBaseMatch::showPoints)));
47
48        this->pointsTeam1_ = 0;
49        this->pointsTeam2_ = 0;
50    }
51     
52   
53    // set the Bases positions using XML
54    void TeamBaseMatch::XMLPort(Element& xmlelement, XMLPort::Mode mode)
55    {
56        SUPER(TeamBaseMatch, XMLPort, xmlelement, mode);
57
58//        XMLPortObject(TeamBaseMatch, WorldEntity, setNeutralshape, getNeturalshape, xmlelement, mode);
59//        XMLPortObject(TeamBaseMatch, WorldEntity, setTeam1shape, getTeam1shape, xmlelement, mode);
60//        XMLPortObject(TeamBaseMatch, WorldEntity, setTeam2shape, getTeam2shape, xmlelement, mode);
61
62//        XMLPortObject(TeamBaseMatch, TeamBaseMatchBase,  addBase, getBase, xmlelement, mode);
63    }
64   
65/*
66    // pretty useless at the moment...should be implemented in the TeamBaseMatchBase class headerfile
67    // State of the Base (controlled, uncontrolled)
68    int TeamBaseMatch::baseState(Base)
69    {
70        if(Enum state_==uncontrolled) return 0;
71        if(Enum state_==controlTeam1) return 1;
72        if(Enum state_==controlTeam2) return 2;
73    }
74*/ 
75
76
77    // Change the control of the defeated base and respawn it with its initial health
78    bool TeamBaseMatch::allowPawnDeath(Pawn* victim, Pawn* originator)
79    {
80        TeamBaseMatchBase* base = dynamic_cast<TeamBaseMatchBase*>(victim);
81        if (base)
82        {
83            std::set<TeamBaseMatchBase*>::const_iterator it = this->bases_.find(base);
84            if (it != this->bases_.end())
85            {
86                int teamnr = this->getTeam(originator->getPlayer());
87                if (teamnr == 0)
88                    base->setState(BaseState::controlTeam1);
89                if (teamnr == 1)
90                    base->setState(BaseState::controlTeam2);
91            }
92
93            victim->setHealth(victim->getInitialHealth());
94            return false;
95        }
96
97        return TeamDeathmatch::allowPawnDeath(victim, originator);
98    }
99
100
101    // if the player is in the same team as the base, he can't make any damage to it
102    bool TeamBaseMatch::allowPawnDamage(Pawn* victim, Pawn* originator)
103    {
104        TeamBaseMatchBase* base = dynamic_cast<TeamBaseMatchBase*>(victim);
105        if (base)
106        {
107            std::set<TeamBaseMatchBase*>::const_iterator it = this->bases_.find(base);
108            if (it != this->bases_.end())
109                return (!this->pawnsAreInTheSameTeam(victim, base));
110        }
111        return (!this->pawnsAreInTheSameTeam(victim, originator));
112    }
113
114    bool TeamBaseMatch::pawnsAreInTheSameTeam(Pawn* pawn1, TeamBaseMatchBase* base)
115    {
116        if (pawn1 && base)
117        {
118            std::map<PlayerInfo*, int>::const_iterator it1 = this->teamnumbers_.find(pawn1->getPlayer());
119            int teamnrbase = -1;
120            int teamnrplayer = getTeam(pawn1->getPlayer());
121
122            switch(base->getState())
123            {
124                case BaseState::controlTeam1:
125                    teamnrbase = 0;
126                    break;
127                case BaseState::controlTeam2:
128                    teamnrbase = 1;
129                    break;
130                case BaseState::uncontrolled:
131                default:
132                    teamnrbase = -1;
133            }
134
135
136            if(teamnrbase == teamnrplayer){
137                return false;
138            }
139        }
140        return true;
141    }
142
143
144
145
146
147    // collect Points for killing oppenents
148    void TeamBaseMatch::playerScored(PlayerInfo* player)
149    {
150        int teamnr = this->getTeam(player);
151        this->addTeamPoints(teamnr, 5);
152    }
153
154    // show points or each interval of time
155    void TeamBaseMatch::showPoints()
156    {
157       
158        COUT(0) << "Points standing:" << std::endl << "Team 1: "<< pointsTeam1_ << std::endl << "Team 2: " << pointsTeam2_ << std::endl;
159        if(pointsTeam1_ >=1700) COUT(0) << "Team 1 is near victory!" << std::endl;
160        if(pointsTeam2_ >=1700) COUT(0) << "Team 2 is near victory!" << std::endl;
161    }
162
163
164    // collect Points while controlling Bases
165    void TeamBaseMatch::winPoints()
166    {
167        int amountControlled = 0;
168        int amountControlled2 = 0;
169
170        for (std::set<TeamBaseMatchBase*>::const_iterator it = this->bases_.begin(); it != this->bases_.end(); ++it)
171        {
172            if((*it)->getState() == BaseState::controlTeam1)
173            {
174                amountControlled++;
175            }
176            if((*it)->getState() == BaseState::controlTeam2)
177            {
178                amountControlled2++;
179            }
180        }
181
182        this->addTeamPoints(0, (amountControlled * 30));
183        this->addTeamPoints(1, (amountControlled2 * 30));
184    }
185
186
187    // end game if one team reaches 2000 points
188    void TeamBaseMatch::endGame()
189    {
190        if(this->pointsTeam1_>=2000 || this->pointsTeam2_ >=2000)
191        {
192            this->end();
193        }
194    }
195
196
197    // this function is called by the function winPoints() which adds points to the teams for every base and killed openents at a certain time
198    void TeamBaseMatch::addTeamPoints(int team, int points)
199    {
200        if(team == 0)
201        {
202            this->pointsTeam1_ += points;
203        }
204        if(team == 1)
205        {
206            this->pointsTeam2_ += points;
207        }     
208
209        this->endGame();
210    }
211
212    void TeamBaseMatch::addBase(TeamBaseMatchBase* base)
213    {
214        this->bases_.insert(base);
215        base->setState(BaseState::uncontrolled);
216    }
217
218    TeamBaseMatchBase* TeamBaseMatch::getBase(unsigned int index) const
219    {
220        unsigned int i = 0;
221        for (std::set<TeamBaseMatchBase*>::const_iterator it = this->bases_.begin(); it != this->bases_.end(); ++it)
222        {
223            i++;
224            if (i > index)
225                return (*it);
226        }
227        return 0;
228    }
229   
230}
231
232 
Note: See TracBrowser for help on using the repository browser.