Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/network/src/util/multiplayer_team_deathmatch.cc @ 9911

Last change on this file since 9911 was 9911, checked in by rennerc, 17 years ago

enabled playing for server

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