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