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