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