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