1 | /* |
---|
2 | orxonox - the future of 3D-vertical-scrollers |
---|
3 | |
---|
4 | Copyright (C) 2004 orx |
---|
5 | |
---|
6 | This program is free software; you can redistribute it and/or modify |
---|
7 | it under the terms of the GNU General Public License as published by |
---|
8 | the Free Software Foundation; either version 2, or (at your option) |
---|
9 | any later version. |
---|
10 | |
---|
11 | ### File Specific: |
---|
12 | main-programmer: Patrick Boenzli |
---|
13 | */ |
---|
14 | |
---|
15 | #define DEBUG_MODULE_GAME_RULES |
---|
16 | |
---|
17 | #include <map> |
---|
18 | |
---|
19 | #include "multiplayer_team_deathmatch.h" |
---|
20 | |
---|
21 | #include "util/loading/load_param.h" |
---|
22 | #include "util/loading/factory.h" |
---|
23 | |
---|
24 | #include "render2D/image_plane.h" |
---|
25 | #include "state.h" |
---|
26 | #include "class_list.h" |
---|
27 | |
---|
28 | #include "player.h" |
---|
29 | #include "playable.h" |
---|
30 | #include "space_ships/space_ship.h" |
---|
31 | |
---|
32 | |
---|
33 | #include "shared_network_data.h" |
---|
34 | #include "terrain.h" |
---|
35 | #include "class_list.h" |
---|
36 | #include "space_ships/space_ship.h" |
---|
37 | |
---|
38 | #include "network_game_manager.h" |
---|
39 | |
---|
40 | #include "event_handler.h" |
---|
41 | |
---|
42 | #include "glgui.h" |
---|
43 | |
---|
44 | #include "story_entity.h" |
---|
45 | |
---|
46 | #include "shell_command.h" |
---|
47 | |
---|
48 | #include "spawning_point.h" |
---|
49 | |
---|
50 | |
---|
51 | using namespace std; |
---|
52 | |
---|
53 | |
---|
54 | CREATE_FACTORY(MultiplayerTeamDeathmatch, CL_MULTIPLAYER_TEAM_DEATHMATCH); |
---|
55 | |
---|
56 | |
---|
57 | /** |
---|
58 | * constructor |
---|
59 | */ |
---|
60 | MultiplayerTeamDeathmatch::MultiplayerTeamDeathmatch(const TiXmlElement* root) |
---|
61 | : NetworkGameRules(root) |
---|
62 | { |
---|
63 | this->setClassID(CL_MULTIPLAYER_TEAM_DEATHMATCH, "MultiplayerTeamDeathmatch"); |
---|
64 | |
---|
65 | this->bLocalPlayerDead = false; |
---|
66 | this->deathTimeout = 10.0f; // 5 seconds |
---|
67 | this->timeout = 0.0f; |
---|
68 | this->numTeams = 2; |
---|
69 | this->currentGameState = GAMESTATE_PRE_GAME; |
---|
70 | this->gameStateTimer = 10.0f; |
---|
71 | this->bShowTeamChange = false; |
---|
72 | |
---|
73 | this->box = NULL; |
---|
74 | this->table = NULL; |
---|
75 | this->statsBox = NULL; |
---|
76 | |
---|
77 | this->localPlayer = State::getPlayer(); |
---|
78 | |
---|
79 | if( root != NULL) |
---|
80 | this->loadParams(root); |
---|
81 | |
---|
82 | subscribeEvent( ES_GAME, SDLK_o ); |
---|
83 | subscribeEvent( ES_GAME, SDLK_t ); |
---|
84 | subscribeEvent( ES_GAME, SDLK_F1 ); |
---|
85 | subscribeEvent( ES_MENU, SDLK_F1 ); |
---|
86 | subscribeEvent( ES_MENU, KeyMapper::PEV_FIRE1 ); |
---|
87 | |
---|
88 | this->notifier = new OrxGui::GLGuiNotifier(); |
---|
89 | this->notifier->show(); |
---|
90 | this->notifier->setAbsCoor2D(5, 30); |
---|
91 | this->notifier->setFadeAge( 6.0 ); |
---|
92 | this->notifier->setHideAge( 8.0 ); |
---|
93 | this->input = new OrxGui::GLGuiInputLine(); |
---|
94 | this->input->setAbsCoor2D(180, 5); |
---|
95 | this->input->connect(SIGNAL(input, enterPushed), this, SLOT(MultiplayerTeamDeathmatch, onInputEnter)); |
---|
96 | } |
---|
97 | |
---|
98 | /** |
---|
99 | * decontsructor |
---|
100 | */ |
---|
101 | MultiplayerTeamDeathmatch::~MultiplayerTeamDeathmatch() |
---|
102 | { |
---|
103 | unsubscribeEvent( ES_GAME, SDLK_o ); |
---|
104 | unsubscribeEvent( ES_GAME, SDLK_t ); |
---|
105 | unsubscribeEvent( ES_GAME, SDLK_F1 ); |
---|
106 | unsubscribeEvent( ES_MENU, SDLK_F1 ); |
---|
107 | unsubscribeEvent( ES_MENU, KeyMapper::PEV_FIRE1 ); |
---|
108 | |
---|
109 | if ( this->notifier ) |
---|
110 | { |
---|
111 | delete this->notifier; |
---|
112 | this->notifier = NULL; |
---|
113 | } |
---|
114 | |
---|
115 | if ( this->input ) |
---|
116 | { |
---|
117 | delete this->input; |
---|
118 | this->input = NULL; |
---|
119 | } |
---|
120 | } |
---|
121 | |
---|
122 | |
---|
123 | |
---|
124 | void MultiplayerTeamDeathmatch::loadParams(const TiXmlElement* root) |
---|
125 | { |
---|
126 | GameRules::loadParams(root) ; |
---|
127 | |
---|
128 | LoadParam(root, "death-penalty-timeout", this, MultiplayerTeamDeathmatch, setDeathPenaltyTimeout) |
---|
129 | .describe("sets the time in seconds a player has to wait for respawn"); |
---|
130 | |
---|
131 | LoadParam(root, "max-kills", this, MultiplayerTeamDeathmatch, setMaxKills) |
---|
132 | .describe("sets the maximal kills for winning condition"); |
---|
133 | |
---|
134 | LoadParam(root, "num-teams", this, MultiplayerTeamDeathmatch, setNumTeams) |
---|
135 | .describe("sets number of teams"); |
---|
136 | |
---|
137 | } |
---|
138 | |
---|
139 | |
---|
140 | /** |
---|
141 | * time tick |
---|
142 | * @param dt time |
---|
143 | */ |
---|
144 | void MultiplayerTeamDeathmatch::tick(float dt) |
---|
145 | { |
---|
146 | tickStatsTable(); |
---|
147 | //on client side hostId is -1 until hanshake finished |
---|
148 | if ( SharedNetworkData::getInstance()->getHostID() < 0 ) |
---|
149 | return; |
---|
150 | |
---|
151 | if ( currentGameState == GAMESTATE_PRE_GAME || currentGameState == GAMESTATE_GAME ) |
---|
152 | { |
---|
153 | if ( PlayerStats::getStats( SharedNetworkData::getInstance()->getHostID() ) |
---|
154 | && box == NULL |
---|
155 | && (PlayerStats::getStats( SharedNetworkData::getInstance()->getHostID() )->getPreferedTeamId() == TEAM_NOTEAM |
---|
156 | || bShowTeamChange ) |
---|
157 | |
---|
158 | ) |
---|
159 | { |
---|
160 | EventHandler::getInstance()->pushState( ES_MENU ); |
---|
161 | |
---|
162 | OrxGui::GLGuiHandler::getInstance()->activateCursor(); |
---|
163 | |
---|
164 | box = new OrxGui::GLGuiBox(); |
---|
165 | box->setAbsCoor2D( 300, 100 ); |
---|
166 | |
---|
167 | OrxGui::GLGuiPushButton * buttonSpectator = new OrxGui::GLGuiPushButton("Spectator"); |
---|
168 | box->pack( buttonSpectator ); |
---|
169 | buttonSpectator->connect(SIGNAL(buttonSpectator, released), this, SLOT(MultiplayerTeamDeathmatch, onButtonSpectator)); |
---|
170 | |
---|
171 | OrxGui::GLGuiPushButton * buttonRandom = new OrxGui::GLGuiPushButton("Random"); |
---|
172 | box->pack( buttonRandom ); |
---|
173 | buttonRandom->connect(SIGNAL(buttonRandom, released), this, SLOT(MultiplayerTeamDeathmatch, onButtonRandom)); |
---|
174 | |
---|
175 | OrxGui::GLGuiPushButton * buttonTeam0 = new OrxGui::GLGuiPushButton("Blue Team"); |
---|
176 | box->pack( buttonTeam0 ); |
---|
177 | buttonTeam0->connect(SIGNAL(buttonTeam0, released), this, SLOT(MultiplayerTeamDeathmatch, onButtonTeam0)); |
---|
178 | |
---|
179 | OrxGui::GLGuiPushButton * buttonTeam1 = new OrxGui::GLGuiPushButton("Red Team"); |
---|
180 | box->pack( buttonTeam1 ); |
---|
181 | buttonTeam1->connect(SIGNAL(buttonTeam1, released), this, SLOT(MultiplayerTeamDeathmatch, onButtonTeam1)); |
---|
182 | |
---|
183 | if ( bShowTeamChange ) |
---|
184 | { |
---|
185 | OrxGui::GLGuiPushButton * buttonCancel = new OrxGui::GLGuiPushButton("Cancel"); |
---|
186 | box->pack( buttonCancel ); |
---|
187 | buttonCancel->connect(SIGNAL(buttonCancel, released), this, SLOT(MultiplayerTeamDeathmatch, onButtonCancel)); |
---|
188 | } |
---|
189 | |
---|
190 | OrxGui::GLGuiPushButton * buttonExit = new OrxGui::GLGuiPushButton("Exit"); |
---|
191 | box->pack( buttonExit ); |
---|
192 | buttonExit->connect(SIGNAL(buttonExit, released), this, SLOT(MultiplayerTeamDeathmatch, onButtonExit)); |
---|
193 | |
---|
194 | box->showAll(); |
---|
195 | } |
---|
196 | } |
---|
197 | |
---|
198 | if ( box != NULL |
---|
199 | && PlayerStats::getStats( SharedNetworkData::getInstance()->getHostID() ) |
---|
200 | && PlayerStats::getStats( SharedNetworkData::getInstance()->getHostID() )->getPreferedTeamId() != TEAM_NOTEAM |
---|
201 | && !bShowTeamChange |
---|
202 | ) |
---|
203 | { |
---|
204 | delete box; |
---|
205 | box = NULL; |
---|
206 | |
---|
207 | OrxGui::GLGuiHandler::getInstance()->deactivateCursor( true ); |
---|
208 | |
---|
209 | EventHandler::getInstance()->popState(); |
---|
210 | } |
---|
211 | |
---|
212 | if ( box != NULL ) |
---|
213 | { |
---|
214 | OrxGui::GLGuiHandler::getInstance()->tick( dt ); |
---|
215 | } |
---|
216 | |
---|
217 | assignPlayable(); |
---|
218 | |
---|
219 | if ( !SharedNetworkData::getInstance()->isGameServer() ) |
---|
220 | return; |
---|
221 | |
---|
222 | //handle kills |
---|
223 | for ( std::vector<Kill>::iterator it = this->killList.begin(); it != this->killList.end(); ) |
---|
224 | { |
---|
225 | std::vector<Kill>::iterator delit = it; |
---|
226 | |
---|
227 | onKill( it->getKiller()->getOwner(), it->getVictim()->getOwner() ); |
---|
228 | |
---|
229 | it++; |
---|
230 | killList.erase( delit ); |
---|
231 | } |
---|
232 | |
---|
233 | gameStateTimer -= dt; |
---|
234 | //PRINTF(0)("TICK %f\n", gameStateTimer); |
---|
235 | |
---|
236 | if ( currentGameState != GAMESTATE_GAME && gameStateTimer < 0 ) |
---|
237 | nextGameState(); |
---|
238 | |
---|
239 | this->currentGameState = NetworkGameManager::getInstance()->getGameState(); |
---|
240 | |
---|
241 | if ( currentGameState == GAMESTATE_GAME ) |
---|
242 | { |
---|
243 | handleTeamChanges(); |
---|
244 | } |
---|
245 | |
---|
246 | this->calculateTeamScore(); |
---|
247 | |
---|
248 | this->checkGameRules(); |
---|
249 | |
---|
250 | // is the local player dead and inactive |
---|
251 | if( unlikely(this->bLocalPlayerDead)) |
---|
252 | { |
---|
253 | this->timeout += dt; |
---|
254 | PRINTF(0)("TICK DEATH: %f of %f\n", this->timeout, this->deathTimeout); |
---|
255 | // long enough dead? |
---|
256 | if( this->timeout >= this->deathTimeout) |
---|
257 | { |
---|
258 | this->timeout = 0.0f; |
---|
259 | // respawn |
---|
260 | PRINTF(0)("RESPAWN\n"); |
---|
261 | (State::getPlayer())->getPlayable()->respawn(); |
---|
262 | } |
---|
263 | } |
---|
264 | } |
---|
265 | |
---|
266 | |
---|
267 | /** |
---|
268 | * draws the stuff |
---|
269 | */ |
---|
270 | void MultiplayerTeamDeathmatch::draw() |
---|
271 | { |
---|
272 | if( unlikely( this->bLocalPlayerDead)) |
---|
273 | { |
---|
274 | |
---|
275 | } |
---|
276 | } |
---|
277 | |
---|
278 | |
---|
279 | /** |
---|
280 | * check the game rules for consistency |
---|
281 | */ |
---|
282 | void MultiplayerTeamDeathmatch::checkGameRules() |
---|
283 | { |
---|
284 | if ( !SharedNetworkData::getInstance()->isGameServer() ) |
---|
285 | return; |
---|
286 | |
---|
287 | // check for max killing count |
---|
288 | for ( int i = 0; i<numTeams; i++ ) |
---|
289 | { |
---|
290 | if ( teamScore[i] >= maxKills ) |
---|
291 | { |
---|
292 | nextGameState(); |
---|
293 | } |
---|
294 | } |
---|
295 | } |
---|
296 | |
---|
297 | /** |
---|
298 | * find group for new player |
---|
299 | * @return group id |
---|
300 | */ |
---|
301 | int MultiplayerTeamDeathmatch::getTeamForNewUser() |
---|
302 | { |
---|
303 | return TEAM_NOTEAM; |
---|
304 | } |
---|
305 | |
---|
306 | ClassID MultiplayerTeamDeathmatch::getPlayableClassId( int userId, int team ) |
---|
307 | { |
---|
308 | if ( team == TEAM_NOTEAM || team == TEAM_SPECTATOR ) |
---|
309 | return CL_SPECTATOR; |
---|
310 | |
---|
311 | if ( team == 0 || team == 1 ) |
---|
312 | return CL_SPACE_SHIP; |
---|
313 | |
---|
314 | assert( false ); |
---|
315 | } |
---|
316 | |
---|
317 | std::string MultiplayerTeamDeathmatch::getPlayableModelFileName( int userId, int team, ClassID classId ) |
---|
318 | { |
---|
319 | if ( team == 0 ) |
---|
320 | return "models/ships/reap_#.obj"; |
---|
321 | else if ( team == 1 ) |
---|
322 | return "models/ships/fighter.obj"; |
---|
323 | else |
---|
324 | return ""; |
---|
325 | } |
---|
326 | |
---|
327 | /** |
---|
328 | * calculate team score |
---|
329 | */ |
---|
330 | void MultiplayerTeamDeathmatch::calculateTeamScore( ) |
---|
331 | { |
---|
332 | teamScore.clear(); |
---|
333 | |
---|
334 | for ( int i = 0; i<numTeams; i++ ) |
---|
335 | teamScore[i] = 0; |
---|
336 | |
---|
337 | |
---|
338 | const std::list<BaseObject*> * list = ClassList::getList( CL_PLAYER_STATS ); |
---|
339 | |
---|
340 | if ( !list ) |
---|
341 | return; |
---|
342 | |
---|
343 | for ( std::list<BaseObject*>::const_iterator it = list->begin(); it != list->end(); it++ ) |
---|
344 | { |
---|
345 | PlayerStats & stats = *dynamic_cast<PlayerStats*>(*it); |
---|
346 | |
---|
347 | if ( stats.getTeamId() >= 0 ) |
---|
348 | { |
---|
349 | teamScore[stats.getTeamId()] += stats.getScore(); |
---|
350 | } |
---|
351 | } |
---|
352 | } |
---|
353 | |
---|
354 | /** |
---|
355 | * get team for player who choose to join random team |
---|
356 | * @return smallest team |
---|
357 | */ |
---|
358 | int MultiplayerTeamDeathmatch::getRandomTeam( ) |
---|
359 | { |
---|
360 | std::map<int,int> playersInTeam; |
---|
361 | |
---|
362 | for ( int i = 0; i<numTeams; i++ ) |
---|
363 | playersInTeam[i] = 0; |
---|
364 | |
---|
365 | const std::list<BaseObject*> * list = ClassList::getList( CL_PLAYER_STATS ); |
---|
366 | |
---|
367 | if ( !list ) |
---|
368 | return 0; |
---|
369 | |
---|
370 | for ( std::list<BaseObject*>::const_iterator it = list->begin(); it != list->end(); it++ ) |
---|
371 | { |
---|
372 | PlayerStats & stats = *dynamic_cast<PlayerStats*>(*it); |
---|
373 | |
---|
374 | if ( stats.getTeamId() >= 0 ) |
---|
375 | { |
---|
376 | playersInTeam[stats.getTeamId()]++; |
---|
377 | } |
---|
378 | } |
---|
379 | |
---|
380 | |
---|
381 | int minPlayers = 0xFFFF; |
---|
382 | int minTeam = -1; |
---|
383 | |
---|
384 | for ( int i = 0; i<numTeams; i++ ) |
---|
385 | { |
---|
386 | if ( playersInTeam[i] < minPlayers ) |
---|
387 | { |
---|
388 | minTeam = i; |
---|
389 | minPlayers = playersInTeam[i]; |
---|
390 | } |
---|
391 | } |
---|
392 | |
---|
393 | assert( minTeam != -1 ); |
---|
394 | |
---|
395 | return minTeam; |
---|
396 | } |
---|
397 | |
---|
398 | void MultiplayerTeamDeathmatch::nextGameState( ) |
---|
399 | { |
---|
400 | if ( currentGameState == GAMESTATE_PRE_GAME ) |
---|
401 | { |
---|
402 | NetworkGameManager::getInstance()->setGameState( GAMESTATE_GAME ); |
---|
403 | |
---|
404 | return; |
---|
405 | } |
---|
406 | |
---|
407 | if ( currentGameState == GAMESTATE_GAME ) |
---|
408 | { |
---|
409 | NetworkGameManager::getInstance()->setGameState( GAMESTATE_POST_GAME ); |
---|
410 | |
---|
411 | return; |
---|
412 | } |
---|
413 | |
---|
414 | if ( currentGameState == GAMESTATE_POST_GAME ) |
---|
415 | { |
---|
416 | State::getCurrentStoryEntity()->stop(); |
---|
417 | this->bShowTeamChange = false; |
---|
418 | |
---|
419 | return; |
---|
420 | } |
---|
421 | } |
---|
422 | |
---|
423 | void MultiplayerTeamDeathmatch::handleTeamChanges( ) |
---|
424 | { |
---|
425 | const std::list<BaseObject*> * list = ClassList::getList( CL_PLAYER_STATS ); |
---|
426 | |
---|
427 | if ( !list ) |
---|
428 | return; |
---|
429 | |
---|
430 | //first server players with choices |
---|
431 | for ( std::list<BaseObject*>::const_iterator it = list->begin(); it != list->end(); it++ ) |
---|
432 | { |
---|
433 | PlayerStats & stats = *dynamic_cast<PlayerStats*>(*it); |
---|
434 | |
---|
435 | if ( stats.getTeamId() != stats.getPreferedTeamId() ) |
---|
436 | { |
---|
437 | if ( stats.getPreferedTeamId() == TEAM_SPECTATOR || ( stats.getPreferedTeamId() >= 0 && stats.getPreferedTeamId() < numTeams ) ) |
---|
438 | { |
---|
439 | teamChange( stats.getUserId() ); |
---|
440 | } |
---|
441 | } |
---|
442 | } |
---|
443 | |
---|
444 | //now serve player who want join a random team |
---|
445 | for ( std::list<BaseObject*>::const_iterator it = list->begin(); it != list->end(); it++ ) |
---|
446 | { |
---|
447 | PlayerStats & stats = *dynamic_cast<PlayerStats*>(*it); |
---|
448 | |
---|
449 | if ( stats.getTeamId() != stats.getPreferedTeamId() ) |
---|
450 | { |
---|
451 | if ( stats.getPreferedTeamId() == TEAM_RANDOM ) |
---|
452 | { |
---|
453 | stats.setPreferedTeamId( getRandomTeam() ); |
---|
454 | teamChange( stats.getUserId() ); |
---|
455 | } |
---|
456 | } |
---|
457 | } |
---|
458 | } |
---|
459 | |
---|
460 | void MultiplayerTeamDeathmatch::teamChange( int userId ) |
---|
461 | { |
---|
462 | assert( PlayerStats::getStats( userId ) ); |
---|
463 | PlayerStats & stats = *(PlayerStats::getStats( userId )); |
---|
464 | |
---|
465 | stats.setTeamId( stats.getPreferedTeamId() ); |
---|
466 | |
---|
467 | Playable * oldPlayable = stats.getPlayable(); |
---|
468 | |
---|
469 | |
---|
470 | ClassID playableClassId = getPlayableClassId( userId, stats.getPreferedTeamId() ); |
---|
471 | std::string playableModel = getPlayableModelFileName( userId, stats.getPreferedTeamId(), playableClassId ); |
---|
472 | |
---|
473 | BaseObject * bo = Factory::fabricate( playableClassId ); |
---|
474 | |
---|
475 | assert( bo != NULL ); |
---|
476 | assert( bo->isA( CL_PLAYABLE ) ); |
---|
477 | |
---|
478 | Playable & playable = *(dynamic_cast<Playable*>(bo)); |
---|
479 | |
---|
480 | playable.loadModel( playableModel ); |
---|
481 | playable.setOwner( userId ); |
---|
482 | playable.setUniqueID( SharedNetworkData::getInstance()->getNewUniqueID() ); |
---|
483 | playable.setSynchronized( true ); |
---|
484 | |
---|
485 | stats.setTeamId( stats.getPreferedTeamId() ); |
---|
486 | stats.setPlayableClassId( playableClassId ); |
---|
487 | stats.setPlayableUniqueId( playable.getUniqueID() ); |
---|
488 | stats.setModelFileName( playableModel ); |
---|
489 | |
---|
490 | this->respawnPlayable( &playable, stats.getPreferedTeamId(), 0.0f ); |
---|
491 | |
---|
492 | if ( oldPlayable ) |
---|
493 | { |
---|
494 | //if ( userId == SharedNetworkData::getInstance()->getHostID() ) |
---|
495 | // State::getPlayer()->setPlayable( NULL ); |
---|
496 | delete oldPlayable; |
---|
497 | } |
---|
498 | } |
---|
499 | |
---|
500 | void MultiplayerTeamDeathmatch::onButtonExit( ) |
---|
501 | { |
---|
502 | State::getCurrentStoryEntity()->stop(); |
---|
503 | this->bShowTeamChange = false; |
---|
504 | } |
---|
505 | |
---|
506 | void MultiplayerTeamDeathmatch::onButtonRandom( ) |
---|
507 | { |
---|
508 | NetworkGameManager::getInstance()->prefereTeam( TEAM_RANDOM ); |
---|
509 | this->bShowTeamChange = false; |
---|
510 | } |
---|
511 | |
---|
512 | void MultiplayerTeamDeathmatch::onButtonTeam0( ) |
---|
513 | { |
---|
514 | NetworkGameManager::getInstance()->prefereTeam( 0 ); |
---|
515 | this->bShowTeamChange = false; |
---|
516 | } |
---|
517 | |
---|
518 | void MultiplayerTeamDeathmatch::onButtonTeam1( ) |
---|
519 | { |
---|
520 | NetworkGameManager::getInstance()->prefereTeam( 1 ); |
---|
521 | this->bShowTeamChange = false; |
---|
522 | } |
---|
523 | |
---|
524 | void MultiplayerTeamDeathmatch::onButtonSpectator( ) |
---|
525 | { |
---|
526 | NetworkGameManager::getInstance()->prefereTeam( TEAM_SPECTATOR ); |
---|
527 | this->bShowTeamChange = false; |
---|
528 | } |
---|
529 | |
---|
530 | void MultiplayerTeamDeathmatch::assignPlayable( ) |
---|
531 | { |
---|
532 | if ( PlayerStats::getStats( SharedNetworkData::getInstance()->getHostID() ) ) |
---|
533 | PlayerStats::getStats( SharedNetworkData::getInstance()->getHostID() )->getPlayable(); |
---|
534 | } |
---|
535 | |
---|
536 | /** |
---|
537 | * function that processes events from the handler |
---|
538 | * @param event: the event |
---|
539 | * @todo replace SDLK_o with something from KeyMapper |
---|
540 | */ |
---|
541 | void MultiplayerTeamDeathmatch::process( const Event & event ) |
---|
542 | { |
---|
543 | if ( event.type == SDLK_o ) |
---|
544 | { |
---|
545 | if ( event.bPressed ) |
---|
546 | this->bShowTeamChange = true; |
---|
547 | } else if ( event.type == SDLK_F1 ) |
---|
548 | { |
---|
549 | if ( !this->statsBox && event.bPressed ) |
---|
550 | { |
---|
551 | PRINTF(0)("show stats\n"); |
---|
552 | this->showStats(); |
---|
553 | } |
---|
554 | if ( this->statsBox && !this->bLocalPlayerDead && event.bPressed && this->table->rowCount() != 0 && this->table->columnCount() != 0 ) |
---|
555 | { |
---|
556 | PRINTF(0)("hide stats\n"); |
---|
557 | this->hideStats(); |
---|
558 | } |
---|
559 | } |
---|
560 | else if ( event.type == SDLK_t ) |
---|
561 | { |
---|
562 | if ( !event.bPressed ) |
---|
563 | { |
---|
564 | EventHandler::getInstance()->pushState( ES_MENU ); |
---|
565 | OrxGui::GLGuiHandler::getInstance()->activateCursor(); |
---|
566 | OrxGui::GLGuiHandler::getInstance()->deactivateCursor(); |
---|
567 | input->show(); |
---|
568 | input->giveMouseFocus(); |
---|
569 | input->setText("say "); |
---|
570 | } |
---|
571 | } |
---|
572 | else if ( this->bLocalPlayerDead && statsBox && event.type == KeyMapper::PEV_FIRE1 ) |
---|
573 | { |
---|
574 | this->hideStats(); |
---|
575 | } |
---|
576 | } |
---|
577 | |
---|
578 | void MultiplayerTeamDeathmatch::onButtonCancel( ) |
---|
579 | { |
---|
580 | this->bShowTeamChange = false; |
---|
581 | } |
---|
582 | |
---|
583 | |
---|
584 | |
---|
585 | /** |
---|
586 | * this method is called by NetworkGameManger when he recieved a chat message |
---|
587 | * @param userId senders user id |
---|
588 | * @param message message string |
---|
589 | * @param messageType some int |
---|
590 | */ |
---|
591 | void MultiplayerTeamDeathmatch::handleChatMessage( int userId, const std::string & message, int messageType ) |
---|
592 | { |
---|
593 | std::string name = "unknown"; |
---|
594 | |
---|
595 | if ( PlayerStats::getStats( userId ) ) |
---|
596 | { |
---|
597 | name = PlayerStats::getStats( userId )->getNickName(); |
---|
598 | } |
---|
599 | |
---|
600 | PRINTF(0)("CHATMESSAGE %s (%d): %s\n", name.c_str(), userId, message.c_str() ); |
---|
601 | notifier->pushNotifyMessage(name + ": " + message); |
---|
602 | } |
---|
603 | |
---|
604 | void MultiplayerTeamDeathmatch::onInputEnter( const std::string & text ) |
---|
605 | { |
---|
606 | EventHandler::getInstance()->popState(); |
---|
607 | input->breakMouseFocus(); |
---|
608 | input->hide(); |
---|
609 | input->setText(""); |
---|
610 | |
---|
611 | std::string command = text; |
---|
612 | |
---|
613 | //HACK insert " in say commands so user doesn't have to type them |
---|
614 | if ( command.length() >= 4 && command[0] == 's' && command[1] == 'a' && command[2] == 'y' && command[3] == ' ' ) |
---|
615 | { |
---|
616 | command.insert( 4, "\"" ); |
---|
617 | command = command + "\""; |
---|
618 | } |
---|
619 | |
---|
620 | OrxShell::ShellCommand::execute( command ); |
---|
621 | } |
---|
622 | |
---|
623 | /** |
---|
624 | * show table with frags |
---|
625 | */ |
---|
626 | void MultiplayerTeamDeathmatch::showStats( ) |
---|
627 | { |
---|
628 | EventHandler::getInstance()->pushState( ES_MENU ); |
---|
629 | statsBox = new OrxGui::GLGuiBox(); |
---|
630 | statsBox->setAbsCoor2D( 300, 100 ); |
---|
631 | |
---|
632 | this->table = new OrxGui::GLGuiTable(0,0); |
---|
633 | |
---|
634 | statsBox->pack( this->table ); |
---|
635 | |
---|
636 | statsBox->showAll(); |
---|
637 | } |
---|
638 | |
---|
639 | /** |
---|
640 | * hide table with frags |
---|
641 | */ |
---|
642 | void MultiplayerTeamDeathmatch::hideStats( ) |
---|
643 | { |
---|
644 | if ( statsBox ) |
---|
645 | { |
---|
646 | delete statsBox; |
---|
647 | statsBox = NULL; |
---|
648 | } |
---|
649 | |
---|
650 | EventHandler::getInstance()->popState(); |
---|
651 | } |
---|
652 | |
---|
653 | /** |
---|
654 | * fill stats table with values |
---|
655 | */ |
---|
656 | void MultiplayerTeamDeathmatch::tickStatsTable( ) |
---|
657 | { |
---|
658 | if ( !this->statsBox ) |
---|
659 | return; |
---|
660 | |
---|
661 | std::vector<std::string> headers; |
---|
662 | headers.push_back("Red Team"); |
---|
663 | headers.push_back(""); |
---|
664 | headers.push_back(""); |
---|
665 | headers.push_back("Blue Team"); |
---|
666 | headers.push_back(""); |
---|
667 | this->table->setHeader(headers); |
---|
668 | |
---|
669 | std::map<int,std::string> fragsTeam0; |
---|
670 | std::map<int,std::string> fragsTeam1; |
---|
671 | |
---|
672 | const std::list<BaseObject*> * list = ClassList::getList( CL_PLAYER_STATS ); |
---|
673 | |
---|
674 | if ( !list ) |
---|
675 | return; |
---|
676 | |
---|
677 | for ( std::list<BaseObject*>::const_iterator it = list->begin(); it != list->end(); it++ ) |
---|
678 | { |
---|
679 | PlayerStats & stats = *dynamic_cast<PlayerStats*>(*it); |
---|
680 | |
---|
681 | if ( stats.getTeamId() == 0 || stats.getTeamId() == 1 ) |
---|
682 | { |
---|
683 | if ( stats.getTeamId() == 0 ) |
---|
684 | fragsTeam0[stats.getScore()] = stats.getNickName(); |
---|
685 | else |
---|
686 | fragsTeam1[stats.getScore()] = stats.getNickName(); |
---|
687 | } |
---|
688 | } |
---|
689 | |
---|
690 | char st[10]; |
---|
691 | int i = 0; |
---|
692 | |
---|
693 | |
---|
694 | i = 2; |
---|
695 | for ( std::map<int,std::string>::const_iterator it = fragsTeam0.begin(); it != fragsTeam0.end(); it++ ) |
---|
696 | { |
---|
697 | this->table->setEntry( 0, i, it->second ); |
---|
698 | snprintf( st, 10, "%d", it->first ); |
---|
699 | this->table->setEntry( 1, i, st ); |
---|
700 | this->table->setEntry( 2, i, "" ); |
---|
701 | i++; |
---|
702 | } |
---|
703 | |
---|
704 | i = 2; |
---|
705 | for ( std::map<int,std::string>::const_iterator it = fragsTeam1.begin(); it != fragsTeam1.end(); it++ ) |
---|
706 | { |
---|
707 | this->table->setEntry( 3, i, it->second ); |
---|
708 | snprintf( st, 10, "%d", it->first ); |
---|
709 | this->table->setEntry( 4, i, st ); |
---|
710 | i++; |
---|
711 | } |
---|
712 | } |
---|
713 | |
---|
714 | /** |
---|
715 | * this function is called when a player kills another one or himself |
---|
716 | * @param killedUserId |
---|
717 | * @param userId |
---|
718 | */ |
---|
719 | void MultiplayerTeamDeathmatch::onKill( int killedUserId, int userId ) |
---|
720 | { |
---|
721 | assert( PlayerStats::getStats( killedUserId ) ); |
---|
722 | assert( PlayerStats::getStats( userId ) ); |
---|
723 | |
---|
724 | PlayerStats & killedStats = *PlayerStats::getStats( killedUserId ); |
---|
725 | PlayerStats & stats = *PlayerStats::getStats( userId ); |
---|
726 | |
---|
727 | if ( killedUserId != userId ) |
---|
728 | stats.setScore( stats.getScore() + 1 ); |
---|
729 | else |
---|
730 | stats.setScore( stats.getScore() - 1 ); |
---|
731 | |
---|
732 | if ( killedUserId == SharedNetworkData::getInstance()->getHostID() ) |
---|
733 | { |
---|
734 | this->bLocalPlayerDead = true; |
---|
735 | this->showStats(); |
---|
736 | } |
---|
737 | |
---|
738 | this->respawnPlayable( killedStats.getPlayable(), killedStats.getTeamId(), 3.0f ); |
---|
739 | } |
---|
740 | |
---|
741 | /** |
---|
742 | * this function is called on player respawn |
---|
743 | * @param userId |
---|
744 | */ |
---|
745 | void MultiplayerTeamDeathmatch::onRespawn( int userId ) |
---|
746 | { |
---|
747 | if ( userId == SharedNetworkData::getInstance()->getHostID() ) |
---|
748 | { |
---|
749 | this->bLocalPlayerDead = false; |
---|
750 | this->hideStats(); |
---|
751 | } |
---|
752 | } |
---|
753 | |
---|
754 | /** |
---|
755 | * this function is called on player respawn |
---|
756 | * @param we |
---|
757 | */ |
---|
758 | void MultiplayerTeamDeathmatch::registerSpawn( WorldEntity * we ) |
---|
759 | { |
---|
760 | onRespawn( we->getOwner() ); |
---|
761 | } |
---|
762 | |
---|
763 | |
---|
764 | void MultiplayerTeamDeathmatch::respawnPlayable( Playable * playable, int teamId, float delay ) |
---|
765 | { |
---|
766 | const std::list<BaseObject*> * list = ClassList::getList( CL_SPAWNING_POINT ); |
---|
767 | |
---|
768 | assert( list ); |
---|
769 | |
---|
770 | std::vector<SpawningPoint*> spList; |
---|
771 | |
---|
772 | for ( std::list<BaseObject*>::const_iterator it = list->begin(); it != list->end(); it++ ) |
---|
773 | { |
---|
774 | SpawningPoint * sp = dynamic_cast<SpawningPoint*>(*it); |
---|
775 | |
---|
776 | if ( sp->getTeamId() == teamId ) |
---|
777 | spList.push_back( sp ); |
---|
778 | } |
---|
779 | |
---|
780 | if ( spList.size() == 0 ) |
---|
781 | { |
---|
782 | for ( std::list<BaseObject*>::const_iterator it = list->begin(); it != list->end(); it++ ) |
---|
783 | { |
---|
784 | SpawningPoint * sp = dynamic_cast<SpawningPoint*>(*it); |
---|
785 | |
---|
786 | if ( sp->getTeamId() < 0 ) |
---|
787 | spList.push_back( sp ); |
---|
788 | } |
---|
789 | } |
---|
790 | |
---|
791 | assert( spList.size() != 0 ); |
---|
792 | |
---|
793 | int n = (int)((float)spList.size() * (float)rand()/(float)RAND_MAX); |
---|
794 | |
---|
795 | spList[n]->pushEntity( playable, delay ); |
---|
796 | } |
---|
797 | |
---|