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