Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/test/src/util/multiplayer_team_deathmatch.cc @ 10609

Last change on this file since 10609 was 9869, checked in by bensch, 18 years ago

orxonox/trunk: merged the new_class_id branche back to the trunk.
merged with command:
svn merge https://svn.orxonox.net/orxonox/branches/new_class_id trunk -r9683:HEAD
no conflicts… puh..

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