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 | * Nino Weingart |
---|
24 | * Co-authors: |
---|
25 | * ... |
---|
26 | * |
---|
27 | */ |
---|
28 | #include "CaptureTheFlag.h" |
---|
29 | |
---|
30 | #include "core/ConfigValueIncludes.h" |
---|
31 | #include "interfaces/TeamColourable.h" |
---|
32 | #include "worldentities/TeamSpawnPoint.h" |
---|
33 | #include "worldentities/pawns/Pawn.h" |
---|
34 | |
---|
35 | |
---|
36 | |
---|
37 | namespace orxonox |
---|
38 | { |
---|
39 | CreateUnloadableFactory(CaptureTheFlag); |
---|
40 | |
---|
41 | CaptureTheFlag::CaptureTheFlag(BaseObject* creator) : TeamBaseMatch(creator) |
---|
42 | { |
---|
43 | RegisterObject(CaptureTheFlag); |
---|
44 | |
---|
45 | this->teams_ = 2; |
---|
46 | this->pointsTeam1_ = 0; |
---|
47 | this->pointsTeam2_ = 0; |
---|
48 | |
---|
49 | // this->flagTeamBlue = new FlagPickup(this); |
---|
50 | // this->flagTeamRed = new FlagPickup(this); |
---|
51 | |
---|
52 | this->setConfigValues(); |
---|
53 | } |
---|
54 | |
---|
55 | void CaptureTheFlag::tick(float dt){ |
---|
56 | |
---|
57 | SUPER(CaptureTheFlag, tick, dt); |
---|
58 | |
---|
59 | if(this->flagTeamBlue->isPickedUp()){ |
---|
60 | teamnr = getTeam(this->flagTeamBlue->pickedUpBy()->getPlayer()); |
---|
61 | if(teamnr == 1){ |
---|
62 | this->flagTeamBlue->ignorePickedUp(); |
---|
63 | |
---|
64 | if(this->flagTeamBlue->pickedUpBy()->hasFlag()){ |
---|
65 | this->addTeamPoints(teamnr, 1000); |
---|
66 | this->flagTeamBlue->pickedUpBy()->setHasFlag(false); |
---|
67 | } |
---|
68 | this->flagTeamBlue->setPickedUp(false); |
---|
69 | } |
---|
70 | else{ |
---|
71 | this->flagTeamBlue->pickedUpBy()->setHasFlag(true); |
---|
72 | } |
---|
73 | } |
---|
74 | if(this->flagTeamRed->isPickedUp()){ |
---|
75 | teamnr = getTeam(this->flagTeamBlue->pickedUpBy()->getPlayer()); |
---|
76 | if(teamnr == 0){ |
---|
77 | this->flagTeamRed->ignorePickedUp(); |
---|
78 | |
---|
79 | if(this->flagTeamRed->pickedUpBy()->hasFlag()){ |
---|
80 | this->addTeamPoints(teamnr, 1000); |
---|
81 | this->flagTeamRed->pickedUpBy()->setHasFlag(false); |
---|
82 | } |
---|
83 | this->flagTeamRed->setPickedUp(false); |
---|
84 | } |
---|
85 | else{ |
---|
86 | this->flagTeamRed->pickedUpBy()->setHasFlag(true); |
---|
87 | } |
---|
88 | } |
---|
89 | |
---|
90 | } |
---|
91 | void CaptureTheFlag::setConfigValues() |
---|
92 | { |
---|
93 | SetConfigValue(teams_, 2); |
---|
94 | |
---|
95 | static ColourValue colours[] = |
---|
96 | { |
---|
97 | ColourValue(1.0f, 0.3f, 0.3f), |
---|
98 | ColourValue(0.3f, 0.3f, 1.0f), |
---|
99 | ColourValue(0.3f, 1.0f, 0.3f), |
---|
100 | ColourValue(1.0f, 1.0f, 0.0f) |
---|
101 | }; |
---|
102 | static std::vector<ColourValue> defaultcolours(colours, colours + sizeof(colours) / sizeof(ColourValue)); |
---|
103 | |
---|
104 | SetConfigValue(teamcolours_, defaultcolours); |
---|
105 | } |
---|
106 | |
---|
107 | void CaptureTheFlag::playerEntered(PlayerInfo* player) |
---|
108 | { |
---|
109 | TeamBaseMatch::playerEntered(player); |
---|
110 | |
---|
111 | std::vector<unsigned int> playersperteam(this->teams_, 0); |
---|
112 | |
---|
113 | for (std::map<PlayerInfo*, int>::iterator it = this->teamnumbers_.begin(); it != this->teamnumbers_.end(); ++it) |
---|
114 | if (it->second < static_cast<int>(this->teams_) && it->second >= 0) |
---|
115 | playersperteam[it->second]++; |
---|
116 | |
---|
117 | unsigned int minplayers = static_cast<unsigned int>(-1); |
---|
118 | size_t minplayersteam = 0; |
---|
119 | for (size_t i = 0; i < this->teams_; ++i) |
---|
120 | { |
---|
121 | if (playersperteam[i] < minplayers) |
---|
122 | { |
---|
123 | minplayers = playersperteam[i]; |
---|
124 | minplayersteam = i; |
---|
125 | } |
---|
126 | } |
---|
127 | |
---|
128 | this->teamnumbers_[player] = minplayersteam; |
---|
129 | } |
---|
130 | |
---|
131 | bool CaptureTheFlag::playerLeft(PlayerInfo* player) |
---|
132 | { |
---|
133 | bool valid_player = Deathmatch::playerLeft(player); |
---|
134 | |
---|
135 | if (valid_player) |
---|
136 | this->teamnumbers_.erase(player); |
---|
137 | |
---|
138 | return valid_player; |
---|
139 | } |
---|
140 | |
---|
141 | bool CaptureTheFlag::allowPawnHit(Pawn* victim, Pawn* originator) |
---|
142 | { |
---|
143 | return (!this->pawnsAreInTheSameTeam(victim, originator) || !originator); |
---|
144 | } |
---|
145 | |
---|
146 | bool CaptureTheFlag::allowPawnDamage(Pawn* victim, Pawn* originator) |
---|
147 | { |
---|
148 | return (!this->pawnsAreInTheSameTeam(victim, originator) || !originator); |
---|
149 | } |
---|
150 | |
---|
151 | bool CaptureTheFlag::allowPawnDeath(Pawn* victim, Pawn* originator) |
---|
152 | { |
---|
153 | return (!this->pawnsAreInTheSameTeam(victim, originator) || !originator); |
---|
154 | } |
---|
155 | |
---|
156 | // collect Points for killing oppenents |
---|
157 | void CaptureTheFlag::playerScored(PlayerInfo* player) |
---|
158 | { |
---|
159 | int teamnr = this->getTeam(player); |
---|
160 | this->addTeamPoints(teamnr, 5); |
---|
161 | } |
---|
162 | |
---|
163 | |
---|
164 | // show points or each interval of time |
---|
165 | void CaptureTheFlag::showPoints() |
---|
166 | { |
---|
167 | if (!this->hasStarted() || this->hasEnded()) |
---|
168 | return; |
---|
169 | |
---|
170 | orxout(message) << "Points standing:" << '\n' << "Team Blue: "<< pointsTeam1_ << '\n' << "Team Red: " << pointsTeam2_ << endl; |
---|
171 | if(pointsTeam1_ >=1700 && pointsTeam1_ < 2000) orxout(message) << "Team Blue is near victory!" << endl; |
---|
172 | if(pointsTeam2_ >=1700 && pointsTeam2_ < 2000) orxout(message) << "Team Red is near victory!" << endl; |
---|
173 | } |
---|
174 | |
---|
175 | // end game if one team reaches 2000 points |
---|
176 | void CaptureTheFlag::endGame() |
---|
177 | { |
---|
178 | if (this->pointsTeam1_ >= 2000 || this->pointsTeam2_ >= 2000) |
---|
179 | { |
---|
180 | int winningteam = -1; |
---|
181 | |
---|
182 | if (this->pointsTeam1_ > this->pointsTeam2_) |
---|
183 | { |
---|
184 | orxout(message) << "Team Blue has won the match" << endl; |
---|
185 | winningteam = 0; |
---|
186 | } |
---|
187 | else |
---|
188 | { |
---|
189 | orxout(message) << "Team Red has won the match" << endl; |
---|
190 | winningteam = 1; |
---|
191 | } |
---|
192 | |
---|
193 | for (std::map<PlayerInfo*, int>::iterator it = this->teamnumbers_.begin(); it != this->teamnumbers_.end(); ++it) |
---|
194 | { |
---|
195 | if (it->first->getClientID() == NETWORK_PEER_ID_UNKNOWN) |
---|
196 | continue; |
---|
197 | |
---|
198 | if (it->second == winningteam) |
---|
199 | this->gtinfo_->sendAnnounceMessage("You have won the match!", it->first->getClientID()); |
---|
200 | else |
---|
201 | this->gtinfo_->sendAnnounceMessage("You have lost the match!", it->first->getClientID()); |
---|
202 | } |
---|
203 | |
---|
204 | this->end(); |
---|
205 | this->scoreTimer_.stopTimer(); |
---|
206 | this->outputTimer_.stopTimer(); |
---|
207 | } |
---|
208 | } |
---|
209 | |
---|
210 | // this function is called by the function winPoints() which adds points to the teams for every base and killed openents at a certain time |
---|
211 | void CaptureTheFlag::addTeamPoints(int team, int points) |
---|
212 | { |
---|
213 | if(team == 0) |
---|
214 | { |
---|
215 | this->pointsTeam1_ += points; |
---|
216 | } |
---|
217 | if(team == 1) |
---|
218 | { |
---|
219 | this->pointsTeam2_ += points; |
---|
220 | } |
---|
221 | |
---|
222 | this->endGame(); |
---|
223 | } |
---|
224 | |
---|
225 | int TeamBaseMatch::getTeamPoints(int team) |
---|
226 | { |
---|
227 | if(team == 0) |
---|
228 | { |
---|
229 | return this->pointsTeam1_; |
---|
230 | } |
---|
231 | if(team == 1) |
---|
232 | { |
---|
233 | return this->pointsTeam2_; |
---|
234 | } |
---|
235 | |
---|
236 | return 0; |
---|
237 | } |
---|
238 | |
---|
239 | SpawnPoint* CaptureTheFlag::getBestSpawnPoint(PlayerInfo* player) const |
---|
240 | { |
---|
241 | int desiredTeamNr = -1; |
---|
242 | std::map<PlayerInfo*, int>::const_iterator it_player = this->teamnumbers_.find(player); |
---|
243 | if (it_player != this->teamnumbers_.end()) |
---|
244 | desiredTeamNr = it_player->second; |
---|
245 | |
---|
246 | // Only use spawnpoints of the own team (or non-team-spawnpoints) |
---|
247 | std::set<SpawnPoint*> teamSpawnPoints = this->spawnpoints_; |
---|
248 | for (std::set<SpawnPoint*>::iterator it = teamSpawnPoints.begin(); it != teamSpawnPoints.end(); ) |
---|
249 | { |
---|
250 | if ((*it)->isA(Class(TeamSpawnPoint))) |
---|
251 | { |
---|
252 | TeamSpawnPoint* tsp = orxonox_cast<TeamSpawnPoint*>(*it); |
---|
253 | if (tsp && static_cast<int>(tsp->getTeamNumber()) != desiredTeamNr) |
---|
254 | { |
---|
255 | teamSpawnPoints.erase(it++); |
---|
256 | continue; |
---|
257 | } |
---|
258 | } |
---|
259 | |
---|
260 | ++it; |
---|
261 | } |
---|
262 | |
---|
263 | SpawnPoint* fallbackSpawnPoint = NULL; |
---|
264 | if (teamSpawnPoints.size() > 0) |
---|
265 | { |
---|
266 | unsigned int randomspawn = static_cast<unsigned int>(rnd(static_cast<float>(teamSpawnPoints.size()))); |
---|
267 | unsigned int index = 0; |
---|
268 | // Get random fallback spawnpoint in case there is no active SpawnPoint. |
---|
269 | for (std::set<SpawnPoint*>::const_iterator it = teamSpawnPoints.begin(); it != teamSpawnPoints.end(); ++it) |
---|
270 | { |
---|
271 | if (index == randomspawn) |
---|
272 | { |
---|
273 | fallbackSpawnPoint = (*it); |
---|
274 | break; |
---|
275 | } |
---|
276 | |
---|
277 | ++index; |
---|
278 | } |
---|
279 | |
---|
280 | // Remove all inactive SpawnPoints from the list. |
---|
281 | for (std::set<SpawnPoint*>::iterator it = teamSpawnPoints.begin(); it != teamSpawnPoints.end(); ) |
---|
282 | { |
---|
283 | if(!(*it)->isActive()) |
---|
284 | { |
---|
285 | teamSpawnPoints.erase(it++); |
---|
286 | continue; |
---|
287 | } |
---|
288 | |
---|
289 | ++it; |
---|
290 | } |
---|
291 | |
---|
292 | randomspawn = static_cast<unsigned int>(rnd(static_cast<float>(teamSpawnPoints.size()))); |
---|
293 | index = 0; |
---|
294 | for (std::set<SpawnPoint*>::const_iterator it = teamSpawnPoints.begin(); it != teamSpawnPoints.end(); ++it) |
---|
295 | { |
---|
296 | if (index == randomspawn) |
---|
297 | return (*it); |
---|
298 | |
---|
299 | ++index; |
---|
300 | } |
---|
301 | |
---|
302 | return fallbackSpawnPoint; |
---|
303 | } |
---|
304 | |
---|
305 | return 0; |
---|
306 | } |
---|
307 | |
---|
308 | void CaptureTheFlag::playerStartsControllingPawn(PlayerInfo* player, Pawn* pawn) |
---|
309 | { |
---|
310 | if (!player) |
---|
311 | return; |
---|
312 | |
---|
313 | // Set the team colour |
---|
314 | std::map<PlayerInfo*, int>::const_iterator it_player = this->teamnumbers_.find(player); |
---|
315 | if (it_player != this->teamnumbers_.end() && it_player->second >= 0 && it_player->second < static_cast<int>(this->teamcolours_.size())) |
---|
316 | { |
---|
317 | if (pawn) |
---|
318 | { |
---|
319 | pawn->setRadarObjectColour(this->teamcolours_[it_player->second]); |
---|
320 | |
---|
321 | std::set<WorldEntity*> pawnAttachments = pawn->getAttachedObjects(); |
---|
322 | for (std::set<WorldEntity*>::iterator it = pawnAttachments.begin(); it != pawnAttachments.end(); ++it) |
---|
323 | { |
---|
324 | if ((*it)->isA(Class(TeamColourable))) |
---|
325 | { |
---|
326 | TeamColourable* tc = orxonox_cast<TeamColourable*>(*it); |
---|
327 | tc->setTeamColour(this->teamcolours_[it_player->second]); |
---|
328 | } |
---|
329 | } |
---|
330 | } |
---|
331 | } |
---|
332 | } |
---|
333 | |
---|
334 | bool CaptureTheFlag::pawnsAreInTheSameTeam(Pawn* pawn1, Pawn* pawn2) |
---|
335 | { |
---|
336 | if (pawn1 && pawn2) |
---|
337 | { |
---|
338 | std::map<PlayerInfo*, int>::const_iterator it1 = this->teamnumbers_.find(pawn1->getPlayer()); |
---|
339 | std::map<PlayerInfo*, int>::const_iterator it2 = this->teamnumbers_.find(pawn2->getPlayer()); |
---|
340 | |
---|
341 | if (it1 != this->teamnumbers_.end() && it2 != this->teamnumbers_.end()) |
---|
342 | return (it1->second == it2->second); |
---|
343 | } |
---|
344 | return false; |
---|
345 | } |
---|
346 | |
---|
347 | int CaptureTheFlag::getTeam(PlayerInfo* player) |
---|
348 | { |
---|
349 | std::map<PlayerInfo*, int>::const_iterator it_player = this->teamnumbers_.find(player); |
---|
350 | if (it_player != this->teamnumbers_.end()) |
---|
351 | return it_player->second; |
---|
352 | else |
---|
353 | return -1; |
---|
354 | } |
---|
355 | } |
---|