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 | * Fabian 'x3n' Landau |
---|
24 | * Co-authors: |
---|
25 | * ... |
---|
26 | * |
---|
27 | */ |
---|
28 | |
---|
29 | #include "Gametype.h" |
---|
30 | |
---|
31 | #include "util/Math.h" |
---|
32 | #include "core/CoreIncludes.h" |
---|
33 | #include "core/ConfigValueIncludes.h" |
---|
34 | #include "core/GameMode.h" |
---|
35 | |
---|
36 | #include "objects/infos/PlayerInfo.h" |
---|
37 | #include "objects/infos/Bot.h" |
---|
38 | #include "objects/worldentities/Camera.h" |
---|
39 | #include "objects/worldentities/ControllableEntity.h" |
---|
40 | #include "objects/worldentities/SpawnPoint.h" |
---|
41 | #include "objects/worldentities/pawns/Spectator.h" |
---|
42 | #include "objects/worldentities/pawns/Pawn.h" |
---|
43 | #include "overlays/OverlayGroup.h" |
---|
44 | |
---|
45 | namespace orxonox |
---|
46 | { |
---|
47 | CreateUnloadableFactory(Gametype); |
---|
48 | |
---|
49 | Gametype::Gametype(BaseObject* creator) : BaseObject(creator), gtinfo_(creator) |
---|
50 | { |
---|
51 | RegisterObject(Gametype); |
---|
52 | |
---|
53 | this->setGametype(this); |
---|
54 | |
---|
55 | this->defaultControllableEntity_ = Class(Spectator); |
---|
56 | |
---|
57 | this->bAutoStart_ = false; |
---|
58 | this->bForceSpawn_ = false; |
---|
59 | this->numberOfBots_ = 0; |
---|
60 | |
---|
61 | this->timeLimit_ = 0; |
---|
62 | this->time_ = 0; |
---|
63 | this->timerIsActive_ = false; |
---|
64 | |
---|
65 | this->initialStartCountdown_ = 3; |
---|
66 | |
---|
67 | this->setConfigValues(); |
---|
68 | |
---|
69 | // load the corresponding score board |
---|
70 | if (GameMode::showsGraphics() && this->scoreboardTemplate_ != "") |
---|
71 | { |
---|
72 | this->scoreboard_ = new OverlayGroup(this); |
---|
73 | this->scoreboard_->addTemplate(this->scoreboardTemplate_); |
---|
74 | this->scoreboard_->setGametype(this); |
---|
75 | } |
---|
76 | else |
---|
77 | this->scoreboard_ = 0; |
---|
78 | } |
---|
79 | |
---|
80 | void Gametype::setConfigValues() |
---|
81 | { |
---|
82 | SetConfigValue(initialStartCountdown_, 3.0f); |
---|
83 | SetConfigValue(bAutoStart_, false); |
---|
84 | SetConfigValue(bForceSpawn_, false); |
---|
85 | SetConfigValue(numberOfBots_, 0); |
---|
86 | SetConfigValue(scoreboardTemplate_, "defaultScoreboard"); |
---|
87 | } |
---|
88 | |
---|
89 | void Gametype::tick(float dt) |
---|
90 | { |
---|
91 | SUPER(Gametype, tick, dt); |
---|
92 | |
---|
93 | //count timer |
---|
94 | if (timerIsActive_) |
---|
95 | { |
---|
96 | if (this->timeLimit_ == 0) |
---|
97 | this->time_ += dt; |
---|
98 | else |
---|
99 | this->time_ -= dt; |
---|
100 | } |
---|
101 | |
---|
102 | if (this->gtinfo_.bStartCountdownRunning_ && !this->gtinfo_.bStarted_) |
---|
103 | this->gtinfo_.startCountdown_ -= dt; |
---|
104 | |
---|
105 | if (!this->gtinfo_.bStarted_) |
---|
106 | this->checkStart(); |
---|
107 | else if (!this->gtinfo_.bEnded_) |
---|
108 | this->spawnDeadPlayersIfRequested(); |
---|
109 | |
---|
110 | this->assignDefaultPawnsIfNeeded(); |
---|
111 | } |
---|
112 | |
---|
113 | void Gametype::start() |
---|
114 | { |
---|
115 | this->addBots(this->numberOfBots_); |
---|
116 | |
---|
117 | this->gtinfo_.bStarted_ = true; |
---|
118 | |
---|
119 | this->spawnPlayersIfRequested(); |
---|
120 | } |
---|
121 | |
---|
122 | void Gametype::end() |
---|
123 | { |
---|
124 | this->gtinfo_.bEnded_ = true; |
---|
125 | |
---|
126 | for (std::map<PlayerInfo*, Player>::iterator it = this->players_.begin(); it != this->players_.end(); ++it) |
---|
127 | { |
---|
128 | if (it->first->getControllableEntity()) |
---|
129 | { |
---|
130 | ControllableEntity* oldentity = it->first->getControllableEntity(); |
---|
131 | |
---|
132 | ControllableEntity* entity = this->defaultControllableEntity_.fabricate(oldentity->getCreator()); |
---|
133 | if (oldentity->getCamera()) |
---|
134 | { |
---|
135 | entity->setPosition(oldentity->getCamera()->getWorldPosition()); |
---|
136 | entity->setOrientation(oldentity->getCamera()->getWorldOrientation()); |
---|
137 | } |
---|
138 | else |
---|
139 | { |
---|
140 | entity->setPosition(oldentity->getWorldPosition()); |
---|
141 | entity->setOrientation(oldentity->getWorldOrientation()); |
---|
142 | } |
---|
143 | |
---|
144 | it->first->startControl(entity); |
---|
145 | } |
---|
146 | else |
---|
147 | this->spawnPlayerAsDefaultPawn(it->first); |
---|
148 | } |
---|
149 | } |
---|
150 | |
---|
151 | void Gametype::playerEntered(PlayerInfo* player) |
---|
152 | { |
---|
153 | this->players_[player].state_ = PlayerState::Joined; |
---|
154 | } |
---|
155 | |
---|
156 | bool Gametype::playerLeft(PlayerInfo* player) |
---|
157 | { |
---|
158 | std::map<PlayerInfo*, Player>::iterator it = this->players_.find(player); |
---|
159 | if (it != this->players_.end()) |
---|
160 | { |
---|
161 | this->players_.erase(it); |
---|
162 | return true; |
---|
163 | } |
---|
164 | return false; |
---|
165 | } |
---|
166 | |
---|
167 | void Gametype::playerSwitched(PlayerInfo* player, Gametype* newgametype) |
---|
168 | { |
---|
169 | } |
---|
170 | |
---|
171 | void Gametype::playerSwitchedBack(PlayerInfo* player, Gametype* oldgametype) |
---|
172 | { |
---|
173 | } |
---|
174 | |
---|
175 | bool Gametype::playerChangedName(PlayerInfo* player) |
---|
176 | { |
---|
177 | if (this->players_.find(player) != this->players_.end()) |
---|
178 | { |
---|
179 | if (player->getName() != player->getOldName()) |
---|
180 | { |
---|
181 | return true; |
---|
182 | } |
---|
183 | } |
---|
184 | return false; |
---|
185 | } |
---|
186 | |
---|
187 | void Gametype::pawnPreSpawn(Pawn* pawn) |
---|
188 | { |
---|
189 | } |
---|
190 | |
---|
191 | void Gametype::pawnPostSpawn(Pawn* pawn) |
---|
192 | { |
---|
193 | } |
---|
194 | |
---|
195 | void Gametype::playerPreSpawn(PlayerInfo* player) |
---|
196 | { |
---|
197 | } |
---|
198 | |
---|
199 | void Gametype::playerPostSpawn(PlayerInfo* player) |
---|
200 | { |
---|
201 | } |
---|
202 | |
---|
203 | void Gametype::playerStartsControllingPawn(PlayerInfo* player, Pawn* pawn) |
---|
204 | { |
---|
205 | } |
---|
206 | |
---|
207 | void Gametype::playerStopsControllingPawn(PlayerInfo* player, Pawn* pawn) |
---|
208 | { |
---|
209 | } |
---|
210 | |
---|
211 | bool Gametype::allowPawnHit(Pawn* victim, Pawn* originator) |
---|
212 | { |
---|
213 | return true; |
---|
214 | } |
---|
215 | |
---|
216 | bool Gametype::allowPawnDamage(Pawn* victim, Pawn* originator) |
---|
217 | { |
---|
218 | return true; |
---|
219 | } |
---|
220 | |
---|
221 | bool Gametype::allowPawnDeath(Pawn* victim, Pawn* originator) |
---|
222 | { |
---|
223 | return true; |
---|
224 | } |
---|
225 | |
---|
226 | void Gametype::pawnKilled(Pawn* victim, Pawn* killer) |
---|
227 | { |
---|
228 | if (victim && victim->getPlayer()) |
---|
229 | { |
---|
230 | std::map<PlayerInfo*, Player>::iterator it = this->players_.find(victim->getPlayer()); |
---|
231 | if (it != this->players_.end()) |
---|
232 | { |
---|
233 | it->second.state_ = PlayerState::Dead; |
---|
234 | it->second.killed_++; |
---|
235 | |
---|
236 | // Reward killer |
---|
237 | if (killer && killer->getPlayer()) |
---|
238 | { |
---|
239 | std::map<PlayerInfo*, Player>::iterator it = this->players_.find(killer->getPlayer()); |
---|
240 | if (it != this->players_.end()) |
---|
241 | { |
---|
242 | it->second.frags_++; |
---|
243 | |
---|
244 | if (killer->getPlayer()->getClientID() != CLIENTID_UNKNOWN) |
---|
245 | this->gtinfo_.sendKillMessage("You killed " + victim->getPlayer()->getName(), killer->getPlayer()->getClientID()); |
---|
246 | if (victim->getPlayer()->getClientID() != CLIENTID_UNKNOWN) |
---|
247 | this->gtinfo_.sendDeathMessage("You were killed by " + killer->getPlayer()->getName(), victim->getPlayer()->getClientID()); |
---|
248 | } |
---|
249 | } |
---|
250 | |
---|
251 | ControllableEntity* entity = this->defaultControllableEntity_.fabricate(victim->getCreator()); |
---|
252 | if (victim->getCamera()) |
---|
253 | { |
---|
254 | entity->setPosition(victim->getCamera()->getWorldPosition()); |
---|
255 | entity->setOrientation(victim->getCamera()->getWorldOrientation()); |
---|
256 | } |
---|
257 | else |
---|
258 | { |
---|
259 | entity->setPosition(victim->getWorldPosition()); |
---|
260 | entity->setOrientation(victim->getWorldOrientation()); |
---|
261 | } |
---|
262 | it->first->startControl(entity); |
---|
263 | } |
---|
264 | else |
---|
265 | COUT(2) << "Warning: Killed Pawn was not in the playerlist" << std::endl; |
---|
266 | } |
---|
267 | } |
---|
268 | |
---|
269 | void Gametype::playerScored(PlayerInfo* player) |
---|
270 | { |
---|
271 | std::map<PlayerInfo*, Player>::iterator it = this->players_.find(player); |
---|
272 | if (it != this->players_.end()) |
---|
273 | it->second.frags_++; |
---|
274 | } |
---|
275 | |
---|
276 | int Gametype::getScore(PlayerInfo* player) const |
---|
277 | { |
---|
278 | std::map<PlayerInfo*, Player>::const_iterator it = this->players_.find(player); |
---|
279 | if (it != this->players_.end()) |
---|
280 | return it->second.frags_; |
---|
281 | else |
---|
282 | return 0; |
---|
283 | } |
---|
284 | |
---|
285 | SpawnPoint* Gametype::getBestSpawnPoint(PlayerInfo* player) const |
---|
286 | { |
---|
287 | if (this->spawnpoints_.size() > 0) |
---|
288 | { |
---|
289 | unsigned int randomspawn = static_cast<unsigned int>(rnd(static_cast<float>(this->spawnpoints_.size()))); |
---|
290 | unsigned int index = 0; |
---|
291 | for (std::set<SpawnPoint*>::const_iterator it = this->spawnpoints_.begin(); it != this->spawnpoints_.end(); ++it) |
---|
292 | { |
---|
293 | if (index == randomspawn) |
---|
294 | return (*it); |
---|
295 | |
---|
296 | ++index; |
---|
297 | } |
---|
298 | } |
---|
299 | return 0; |
---|
300 | } |
---|
301 | |
---|
302 | void Gametype::assignDefaultPawnsIfNeeded() |
---|
303 | { |
---|
304 | for (std::map<PlayerInfo*, Player>::iterator it = this->players_.begin(); it != this->players_.end(); ++it) |
---|
305 | { |
---|
306 | if (!it->first->getControllableEntity()) |
---|
307 | { |
---|
308 | it->second.state_ = PlayerState::Dead; |
---|
309 | |
---|
310 | if (!it->first->isReadyToSpawn() || !this->gtinfo_.bStarted_) |
---|
311 | { |
---|
312 | this->spawnPlayerAsDefaultPawn(it->first); |
---|
313 | it->second.state_ = PlayerState::Dead; |
---|
314 | } |
---|
315 | } |
---|
316 | } |
---|
317 | } |
---|
318 | |
---|
319 | void Gametype::checkStart() |
---|
320 | { |
---|
321 | if (!this->gtinfo_.bStarted_) |
---|
322 | { |
---|
323 | if (this->gtinfo_.bStartCountdownRunning_) |
---|
324 | { |
---|
325 | if (this->gtinfo_.startCountdown_ <= 0) |
---|
326 | { |
---|
327 | this->gtinfo_.bStartCountdownRunning_ = false; |
---|
328 | this->gtinfo_.startCountdown_ = 0; |
---|
329 | this->start(); |
---|
330 | } |
---|
331 | } |
---|
332 | else if (this->players_.size() > 0) |
---|
333 | { |
---|
334 | if (this->bAutoStart_) |
---|
335 | { |
---|
336 | this->start(); |
---|
337 | } |
---|
338 | else |
---|
339 | { |
---|
340 | bool allplayersready = true; |
---|
341 | bool hashumanplayers = false; |
---|
342 | for (std::map<PlayerInfo*, Player>::iterator it = this->players_.begin(); it != this->players_.end(); ++it) |
---|
343 | { |
---|
344 | if (!it->first->isReadyToSpawn()) |
---|
345 | allplayersready = false; |
---|
346 | if (it->first->isHumanPlayer()) |
---|
347 | hashumanplayers = true; |
---|
348 | } |
---|
349 | if (allplayersready && hashumanplayers) |
---|
350 | { |
---|
351 | this->gtinfo_.startCountdown_ = this->initialStartCountdown_; |
---|
352 | this->gtinfo_.bStartCountdownRunning_ = true; |
---|
353 | } |
---|
354 | } |
---|
355 | } |
---|
356 | } |
---|
357 | } |
---|
358 | |
---|
359 | void Gametype::spawnPlayersIfRequested() |
---|
360 | { |
---|
361 | for (std::map<PlayerInfo*, Player>::iterator it = this->players_.begin(); it != this->players_.end(); ++it) |
---|
362 | if (it->first->isReadyToSpawn() || this->bForceSpawn_) |
---|
363 | this->spawnPlayer(it->first); |
---|
364 | } |
---|
365 | |
---|
366 | void Gametype::spawnDeadPlayersIfRequested() |
---|
367 | { |
---|
368 | for (std::map<PlayerInfo*, Player>::iterator it = this->players_.begin(); it != this->players_.end(); ++it) |
---|
369 | if (it->second.state_ == PlayerState::Dead) |
---|
370 | if (it->first->isReadyToSpawn() || this->bForceSpawn_) |
---|
371 | this->spawnPlayer(it->first); |
---|
372 | } |
---|
373 | |
---|
374 | void Gametype::spawnPlayer(PlayerInfo* player) |
---|
375 | { |
---|
376 | SpawnPoint* spawnpoint = this->getBestSpawnPoint(player); |
---|
377 | if (spawnpoint) |
---|
378 | { |
---|
379 | this->playerPreSpawn(player); |
---|
380 | player->startControl(spawnpoint->spawn()); |
---|
381 | this->players_[player].state_ = PlayerState::Alive; |
---|
382 | this->playerPostSpawn(player); |
---|
383 | } |
---|
384 | else |
---|
385 | { |
---|
386 | COUT(1) << "Error: No SpawnPoints in current Gametype" << std::endl; |
---|
387 | abort(); |
---|
388 | } |
---|
389 | } |
---|
390 | |
---|
391 | void Gametype::spawnPlayerAsDefaultPawn(PlayerInfo* player) |
---|
392 | { |
---|
393 | SpawnPoint* spawn = this->getBestSpawnPoint(player); |
---|
394 | if (spawn) |
---|
395 | { |
---|
396 | // force spawn at spawnpoint with default pawn |
---|
397 | ControllableEntity* entity = this->defaultControllableEntity_.fabricate(spawn); |
---|
398 | spawn->spawn(entity); |
---|
399 | player->startControl(entity); |
---|
400 | } |
---|
401 | else |
---|
402 | { |
---|
403 | COUT(1) << "Error: No SpawnPoints in current Gametype" << std::endl; |
---|
404 | abort(); |
---|
405 | } |
---|
406 | } |
---|
407 | |
---|
408 | void Gametype::addBots(unsigned int amount) |
---|
409 | { |
---|
410 | for (unsigned int i = 0; i < amount; ++i) |
---|
411 | this->botclass_.fabricate(this); |
---|
412 | } |
---|
413 | |
---|
414 | void Gametype::killBots(unsigned int amount) |
---|
415 | { |
---|
416 | unsigned int i = 0; |
---|
417 | for (ObjectList<Bot>::iterator it = ObjectList<Bot>::begin(); (it != ObjectList<Bot>::end()) && ((amount == 0) || (i < amount)); ) |
---|
418 | { |
---|
419 | if (it->getGametype() == this) |
---|
420 | { |
---|
421 | delete (*(it++)); |
---|
422 | ++i; |
---|
423 | } |
---|
424 | } |
---|
425 | } |
---|
426 | |
---|
427 | void Gametype::addTime(float t) |
---|
428 | { |
---|
429 | if (this->timeLimit_ == 0) |
---|
430 | this->time_ -= t; |
---|
431 | else |
---|
432 | this->time_ += t; |
---|
433 | } |
---|
434 | |
---|
435 | void Gametype::removeTime(float t) |
---|
436 | { |
---|
437 | if (this->timeLimit_ == 0) |
---|
438 | this->time_ += t; |
---|
439 | else |
---|
440 | this->time_ -= t; |
---|
441 | } |
---|
442 | |
---|
443 | void Gametype::resetTimer() |
---|
444 | { |
---|
445 | this->resetTimer(timeLimit_); |
---|
446 | } |
---|
447 | |
---|
448 | void Gametype::resetTimer(float t) |
---|
449 | { |
---|
450 | this->timeLimit_ = t; |
---|
451 | this->time_ = t; |
---|
452 | } |
---|
453 | } |
---|