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