Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 8067 in orxonox.OLD for branches/network/src/util


Ignore:
Timestamp:
Jun 1, 2006, 2:06:53 PM (19 years ago)
Author:
rennerc
Message:

implemented gamestates for multiplayerTeamDeathmatch

Location:
branches/network/src/util
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • branches/network/src/util/multiplayer_team_deathmatch.cc

    r7984 r8067  
    1515#define DEBUG_MODULE_GAME_RULES
    1616
     17#include <map>
     18
    1719#include "multiplayer_team_deathmatch.h"
    1820
     
    3436#include "space_ships/space_ship.h"
    3537
     38#include "network_game_manager.h"
     39
    3640
    3741using namespace std;
     
    5256  this->deathTimeout = 10.0f;     // 5 seconds
    5357  this->timeout = 0.0f;
     58  this->numTeams = 2;
     59  this->currentGameState = GAMESTATE_PRE_GAME;
     60  this->gameStateTimer = 10.0f;
    5461
    5562  this->deathScreen = new ImagePlane();
     
    8794  LoadParam(root, "death-screen-image", this, MultiplayerTeamDeathmatch, setDeathScreen)
    8895      .describe("sets the death screen image");
     96 
     97  LoadParam(root, "num-teams", this, MultiplayerTeamDeathmatch, setNumTeams)
     98      .describe("sets number of teams");
    8999
    90100}
     
    128138void MultiplayerTeamDeathmatch::tick(float dt)
    129139{
     140  if ( !SharedNetworkData::getInstance()->isGameServer() )
     141    return;
     142 
     143  gameStateTimer -= dt;
     144  PRINTF(0)("TICK %f\n", gameStateTimer);
     145 
     146  if ( currentGameState != GAMESTATE_GAME && gameStateTimer < 0 )
     147    nextGameState();
     148 
     149  this->currentGameState = NetworkGameManager::getInstance()->getGameState();
     150 
     151  if ( currentGameState == GAMESTATE_GAME )
     152  {
     153    handleTeamChanges();
     154  }
     155 
     156  this->calculateTeamScore();
     157 
    130158  this->checkGameRules();
    131159
     
    164192void MultiplayerTeamDeathmatch::checkGameRules()
    165193{
    166 
    167   Vector big_left(-201, 0, 0);
    168   float rBig = 176.0f;
    169   Vector big_right(177, 0, 0);
    170   Vector small_left(10, 0, 0);
    171   Vector small_middle(0, 0, 0);
    172   Vector small_right(-10, 0, 0);
    173   float rSmall = 90.0f;
    174 
    175 
     194  if ( !SharedNetworkData::getInstance()->isGameServer() )
     195    return;
     196 
    176197  // check for max killing count
    177   if( this->teamAKills >= this->maxKills)
    178   {
    179     // team A winns
    180   }
    181   else if( this->teamBKills >= this->maxKills)
    182   {
    183     // team B winns
    184   }
    185 
    186 
    187   if ( SharedNetworkData::getInstance()->isGameServer() )
    188   {
    189     float offsetx = 500.0f;
    190     float offsety = 323.0f;
    191     float offsetz = 200.0f;
    192 
    193     offsetz += 10;
    194 
    195     Terrain * terrain = dynamic_cast<Terrain*>(*(ClassList::getList( CL_TERRAIN )->begin()));
    196     const std::list<BaseObject*> * list = ClassList::getList( CL_SPACE_SHIP );
    197     for ( std::list<BaseObject*>::const_iterator it = list->begin(); it != list->end(); it++)
    198     {
    199       SpaceShip * ss = dynamic_cast<SpaceShip*>(*it);
    200       float terrx = ss->getAbsCoor().x + offsetx;
    201       float terry = ss->getAbsCoor().z + offsetz;
    202       float terrz = ss->getAbsCoor().y + offsety;
    203       if ( terrz < terrain->getHeight( terrx, terry ) && ss->getAbsCoor().x > -1000 )
     198  for ( int i = 0; i<numTeams; i++ )
     199  {
     200    if ( teamScore[i] >= maxKills )
     201    {
     202      //team i wins
     203      //TODO
     204    }
     205  }
     206}
     207
     208/**
     209 * find group for new player
     210 * @return group id
     211 */
     212int MultiplayerTeamDeathmatch::getTeamForNewUser( )
     213{
     214  return TEAM_NOTEAM;
     215}
     216
     217ClassID MultiplayerTeamDeathmatch::getPlayableClassId( int team )
     218{
     219  return CL_SPECTATOR;
     220}
     221
     222std::string MultiplayerTeamDeathmatch::getPlayableModelFileName( int team, ClassID classId )
     223{
     224  return "";
     225}
     226
     227/**
     228 * calculate team score
     229 */
     230void MultiplayerTeamDeathmatch::calculateTeamScore( )
     231{
     232  teamScore.clear();
     233 
     234  for ( int i = 0; i<numTeams; i++ )
     235    teamScore[i] = 0;
     236 
     237   
     238  const std::list<BaseObject*> * list = ClassList::getList( CL_PLAYER_STATS );
     239 
     240  if ( !list )
     241    return;
     242 
     243  for ( std::list<BaseObject*>::const_iterator it = list->begin(); it != list->end(); it++ )
     244  {
     245    PlayerStats & stats = *dynamic_cast<PlayerStats*>(*it);
     246
     247    if ( stats.getTeamId() >= 0 )
     248    {
     249      teamScore[stats.getTeamId()] += stats.getScore();
     250    }
     251  }
     252}
     253
     254/**
     255 * get team for player who choose to join random team
     256 * @return smallest team
     257 */
     258int MultiplayerTeamDeathmatch::getRandomTeam( )
     259{
     260  std::map<int,int> playersInTeam;
     261 
     262  for ( int i = 0; i<numTeams; i++ )
     263    playersInTeam[i] = 0;
     264 
     265  const std::list<BaseObject*> * list = ClassList::getList( CL_PLAYER_STATS );
     266 
     267  if ( !list )
     268    return 0;
     269 
     270  for ( std::list<BaseObject*>::const_iterator it = list->begin(); it != list->end(); it++ )
     271  {
     272    PlayerStats & stats = *dynamic_cast<PlayerStats*>(*it);
     273
     274    if ( stats.getTeamId() >= 0 )
     275    {
     276      playersInTeam[stats.getTeamId()]++;
     277    }
     278  }
     279 
     280 
     281  int minPlayers = 0xFFFF;
     282  int minTeam = -1;
     283 
     284  for ( int i = 0; i<numTeams; i++ )
     285  {
     286    if ( playersInTeam[i] < minPlayers )
     287    {
     288      minTeam = i;
     289      minPlayers = playersInTeam[i];
     290    }
     291  }
     292 
     293  assert( minTeam != -1 );
     294 
     295  return minTeam;
     296}
     297
     298void MultiplayerTeamDeathmatch::nextGameState( )
     299{
     300  if ( currentGameState == GAMESTATE_PRE_GAME )
     301  {
     302    NetworkGameManager::getInstance()->setGameState( GAMESTATE_GAME );
     303   
     304    return;
     305  }
     306 
     307  if ( currentGameState == GAMESTATE_GAME )
     308  {
     309    NetworkGameManager::getInstance()->setGameState( GAMESTATE_POST_GAME );
     310   
     311    return;
     312  }
     313 
     314  if ( currentGameState == GAMESTATE_POST_GAME )
     315  {
     316    //TODO end game
     317   
     318    return;
     319  }
     320}
     321
     322void MultiplayerTeamDeathmatch::handleTeamChanges( )
     323{
     324  const std::list<BaseObject*> * list = ClassList::getList( CL_PLAYER_STATS );
     325 
     326  if ( !list )
     327    return;
     328 
     329  //first server players with choices
     330  for ( std::list<BaseObject*>::const_iterator it = list->begin(); it != list->end(); it++ )
     331  {
     332    PlayerStats & stats = *dynamic_cast<PlayerStats*>(*it);
     333
     334    if ( stats.getTeamId() != stats.getPreferedTeamId() )
     335    {
     336      if ( stats.getPreferedTeamId() == TEAM_SPECTATOR || ( stats.getPreferedTeamId() <= 0 && stats.getPreferedTeamId() < numTeams ) )
    204337      {
    205         //TODO handle this
     338        teamChange( stats.getUserId() );
    206339      }
    207 
    208 
    209       float dist = (dynamic_cast<Playable*>(*it)->getAbsCoor() - big_left).len();
    210       if( (dynamic_cast<Playable*>(*it)->getAbsCoor() - big_left).len() > rBig &&
    211            (dynamic_cast<Playable*>(*it)->getAbsCoor() - big_right).len() > rBig &&
    212            (dynamic_cast<Playable*>(*it)->getAbsCoor() - small_left).len() > rSmall &&
    213            (dynamic_cast<Playable*>(*it)->getAbsCoor() - small_middle).len() > rSmall &&
    214            (dynamic_cast<Playable*>(*it)->getAbsCoor() - small_right).len() > rSmall
    215            && ss->getAbsCoor().x > -1000)
     340    }
     341  }
     342 
     343  //now serve player who want join a random team
     344  for ( std::list<BaseObject*>::const_iterator it = list->begin(); it != list->end(); it++ )
     345  {
     346    PlayerStats & stats = *dynamic_cast<PlayerStats*>(*it);
     347
     348    if ( stats.getTeamId() != stats.getPreferedTeamId() )
     349    {
     350      if ( stats.getPreferedTeamId() == TEAM_RANDOM )
    216351      {
    217         PRINTF(0)("KILLLLLLLL\n");
    218 
    219         if((*it)->isA(CL_SPACE_SHIP))
    220         {
    221           //TODO handle this
    222         }
     352        stats.setPreferedTeamId( getTeamForNewUser() );
     353        teamChange( stats.getUserId() );
    223354      }
    224355    }
    225356  }
    226 
    227 
    228 #if 0
    229   std::list<BaseObject*>::const_iterator it;
    230   const std::list<BaseObject*>* list = ClassList::getList(CL_PLAYABLE);
    231 
    232   if( SharedNetworkData::getInstance()->isGameServer())
    233   {
    234     for(it = list->begin(); it != list->end(); it++)
    235     {
    236       float dist = (dynamic_cast<Playable*>(*it)->getAbsCoor() - big_left).len();
    237       if( (dynamic_cast<Playable*>(*it)->getAbsCoor() - big_left).len() > rBig &&
    238            (dynamic_cast<Playable*>(*it)->getAbsCoor() - big_right).len() > rBig &&
    239            (dynamic_cast<Playable*>(*it)->getAbsCoor() - small_left).len() > rSmall &&
    240            (dynamic_cast<Playable*>(*it)->getAbsCoor() - small_middle).len() > rSmall &&
    241            (dynamic_cast<Playable*>(*it)->getAbsCoor() - small_right).len() > rSmall)
    242       {
    243         PRINTF(0)("KILLLLLLLL\n");
    244 
    245         if((*it)->isA(CL_SPACE_SHIP))
    246           dynamic_cast<SpaceShip*>(*it)->doCollideNetwork(116369220.33434f);
    247       }
    248     }
    249   }
    250 #endif
    251 
    252 }
    253 
    254 
    255 
    256 
    257 
    258 
     357}
     358
     359void MultiplayerTeamDeathmatch::teamChange( int userId )
     360{
     361  assert( PlayerStats::getStats( userId ) );
     362  PlayerStats & stats = *(PlayerStats::getStats( userId ));
     363 
     364  assert(false);
     365}
     366
     367
     368
     369
     370
     371
  • branches/network/src/util/multiplayer_team_deathmatch.h

    r7984 r8067  
    1818class ImagePlane;
    1919
     20enum
     21{
     22  GAMESTATE_PRE_GAME = 0,
     23  GAMESTATE_GAME,
     24  GAMESTATE_POST_GAME
     25};
     26
    2027
    2128class MultiplayerTeamDeathmatch : public NetworkGameRules
     
    2835    virtual void loadParams(const TiXmlElement* root);
    2936
     37    virtual int getTeamForNewUser();
     38    virtual ClassID getPlayableClassId( int team );
     39    virtual std::string getPlayableModelFileName( int team, ClassID classId );
    3040
    3141    virtual void onPlayerSpawn();
     
    3949    inline void setMaxKills(int kills) { this->maxKills = kills; }
    4050    void setDeathScreen(const std::string& imageName);
     51   
     52    inline void setNumTeams( int numTeams ){ this->numTeams = numTeams; }
     53    inline int getNumTeams(){ return this->numTeams; }
     54   
     55    int getRandomTeam();
    4156
    4257  protected:
     
    4964    int                maxKills;                   //!< max kills for winning condition
    5065
    51     int                teamAKills;                 //!< kills of team A
    52     int                teamBKills;                 //!< kills of team B
     66    int                numTeams;                   //!< number of teams
    5367
    54     ImagePlane*         deathScreen;                //!< the death screen
     68    std::map<int,int>  teamScore;                  //!< team score
     69
     70    ImagePlane*        deathScreen;                //!< the death screen
     71
     72    int                currentGameState;           //!< game state
     73    float              gameStateTimer;             //!< if less than 0 -> change game state
     74
     75    void calculateTeamScore();
     76    void nextGameState();
     77    void handleTeamChanges();
     78    void teamChange( int userId );
    5579};
    5680
  • branches/network/src/util/network_game_rules.h

    r8024 r8067  
    2020    virtual ~NetworkGameRules();
    2121   
    22     int getTeamForNewUser();
    23     ClassID getPlayableClassId( int team );
    24     std::string getPlayableModelFileName( int team, ClassID classId );
     22    virtual int getTeamForNewUser();
     23    virtual ClassID getPlayableClassId( int team );
     24    virtual std::string getPlayableModelFileName( int team, ClassID classId );
    2525   
    26     PlayerStats * getNewPlayerStats( int userId ){ return new PlayerStats( userId ); }
     26    virtual PlayerStats * getNewPlayerStats( int userId ){ return new PlayerStats( userId ); }
    2727
    2828
  • branches/network/src/util/signal_handler.cc

    r8024 r8067  
    105105    {
    106106      dup2( fd[0], STDIN_FILENO );
    107       snprintf( command, 255, "gdb -p %d %s 1>" GDB_BT_FILE " 2>&1", pid, getInstance()->appName.c_str() );
     107      snprintf( command, 255, "gdb -p %d %s 1>>" GDB_BT_FILE " 2>&1", pid, getInstance()->appName.c_str() );
    108108    }
    109109    else
Note: See TracChangeset for help on using the changeset viewer.