Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/lib/network/network_game_manager.cc @ 8305

Last change on this file since 8305 was 8228, checked in by patrick, 18 years ago

trunk: merged the network branche back to trunk with command: svn merge branches/network trunk -r8150:HEAD

File size: 7.1 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: Benjamin Wuest
13   co-programmer: ...
14*/
15
16
17/* this is for debug output. It just says, that all calls to PRINT() belong to the DEBUG_MODULE_NETWORK module
18   For more information refere to https://www.orxonox.net/cgi-bin/trac.cgi/wiki/DebugOutput
19*/
20#define DEBUG_MODULE_NETWORK
21
22#include "util/loading/factory.h"
23#include "state.h"
24#include "class_list.h"
25
26#include "network_stream.h"
27#include "shared_network_data.h"
28#include "converter.h"
29#include "message_manager.h"
30
31#include "playable.h"
32#include "player.h"
33
34#include "game_world.h"
35
36#include "game_rules.h"
37#include "network_game_rules.h"
38
39#include "network_game_manager.h"
40
41
42/* using namespace std is default, this needs to be here */
43using namespace std;
44
45NetworkGameManager* NetworkGameManager::singletonRef = NULL;
46
47/*!
48 * Standard constructor
49 */
50NetworkGameManager::NetworkGameManager()
51  : Synchronizeable()
52{
53  PRINTF(0)("START\n");
54
55  /* set the class id for the base object */
56  this->setClassID(CL_NETWORK_GAME_MANAGER, "NetworkGameManager");
57
58  this->setSynchronized(true);
59 
60  MessageManager::getInstance()->registerMessageHandler( MSGID_DELETESYNCHRONIZEABLE, delSynchronizeableHandler, NULL );
61  MessageManager::getInstance()->registerMessageHandler( MSGID_PREFEREDTEAM, preferedTeamHandler, NULL );
62 
63  this->gameState = 0;
64  registerVar( new SynchronizeableInt( &gameState, &gameState, "gameState" ) );
65}
66
67/*!
68 * Standard destructor
69 */
70NetworkGameManager::~NetworkGameManager()
71{
72}
73
74
75/**
76 * insert new player into game
77 * @param userId
78 * @return
79 */
80bool NetworkGameManager::signalNewPlayer( int userId )
81{
82  assert( SharedNetworkData::getInstance()->isGameServer() );
83  assert( State::getGameRules() );
84  assert( State::getGameRules()->isA( CL_NETWORK_GAME_RULES ) );
85 
86  NetworkGameRules & rules = *(dynamic_cast<NetworkGameRules*>(State::getGameRules()));
87 
88  int team = rules.getTeamForNewUser();
89  ClassID playableClassId = rules.getPlayableClassId( userId, team );
90  std::string playableModel = rules.getPlayableModelFileName( userId, team, playableClassId );
91 
92  BaseObject * bo = Factory::fabricate( playableClassId );
93 
94  assert( bo != NULL );
95  assert( bo->isA( CL_PLAYABLE ) );
96 
97  Playable & playable = *(dynamic_cast<Playable*>(bo));
98 
99  playable.loadModel( playableModel );
100  playable.setOwner( userId );
101  playable.setUniqueID( SharedNetworkData::getInstance()->getNewUniqueID() );
102  playable.setSynchronized( true );
103 
104  PlayerStats * stats = rules.getNewPlayerStats( userId );
105 
106  stats->setUniqueID( SharedNetworkData::getInstance()->getNewUniqueID() );
107  stats->setSynchronized( true );
108  stats->setOwner( getHostID() );
109 
110  stats->setTeamId( team );
111  stats->setPlayableClassId( playableClassId );
112  stats->setPlayableUniqueId( playable.getUniqueID() );
113  stats->setModelFileName( playableModel );
114 
115  return true;
116}
117
118
119/**
120 * remove player from game
121 * @param userID
122 * @return
123 */
124bool NetworkGameManager::signalLeftPlayer(int userID)
125{
126  if ( PlayerStats::getStats( userID ) )
127  {
128    if ( PlayerStats::getStats( userID )->getPlayable() )
129      delete PlayerStats::getStats( userID )->getPlayable();
130    delete PlayerStats::getStats( userID );
131  }
132 
133  return true;
134}
135
136
137
138/**
139 * handler for remove synchronizeable messages
140 * @param messageId
141 * @param data
142 * @param dataLength
143 * @param someData
144 * @param userId
145 * @return true on successfull handling else handler will be called again
146 */
147bool NetworkGameManager::delSynchronizeableHandler( MessageId messageId, byte * data, int dataLength, void * someData, int userId )
148{
149  if ( getInstance()->isServer() )
150  {
151    PRINTF(2)("Recieved DeleteSynchronizeable message from client %d!\n", userId);
152    return true;
153  }
154 
155  int uniqueId = 0;
156  int len = Converter::byteArrayToInt( data, &uniqueId );
157 
158  if ( len != dataLength )
159  {
160    PRINTF(2)("Recieved DeleteSynchronizeable message with incorrect size (%d) from client %d!\n", dataLength, userId);
161    return true;
162  }
163 
164  const std::list<BaseObject*> * list = ClassList::getList( CL_SYNCHRONIZEABLE );
165 
166  for ( std::list<BaseObject*>::const_iterator it = list->begin(); it != list->end(); it++ )
167  {
168    if ( dynamic_cast<Synchronizeable*>(*it)->getUniqueID() == uniqueId )
169    {
170      if ( (*it)->isA(CL_PLAYABLE) )
171      {
172        getInstance()->playablesToDelete.push_back( dynamic_cast<Playable*>(*it) );
173        return true;
174      }
175     
176      delete dynamic_cast<Synchronizeable*>(*it);
177      return true;
178    }
179  }
180 
181  return true;
182}
183
184/**
185 * removes synchronizeable (also on clients)
186 * @param uniqueId uniqueid to delete
187 */
188void NetworkGameManager::removeSynchronizeable( int uniqueId )
189{
190  byte buf[INTSIZE];
191 
192  assert( Converter::intToByteArray( uniqueId, buf, INTSIZE ) == INTSIZE );
193 
194  MessageManager::getInstance()->sendMessage( MSGID_DELETESYNCHRONIZEABLE, buf, INTSIZE, RT_ALL_NOT_ME, 0, MP_HIGHBANDWIDTH );
195}
196
197
198
199/**
200 * handler for MSGID_PREFEREDTEAM message
201 * @param messageId
202 * @param data
203 * @param dataLength
204 * @param someData
205 * @param userId
206 * @return
207 */
208bool NetworkGameManager::preferedTeamHandler( MessageId messageId, byte * data, int dataLength, void * someData, int userId )
209{
210  assert( NetworkGameManager::getInstance()->isServer() );
211 
212  int teamId = 0;
213  int len = Converter::byteArrayToInt( data, &teamId );
214 
215  if ( len != dataLength )
216  {
217    PRINTF(2)("Recieved DeleteSynchronizeable message with incorrect size (%d) from client %d!\n", dataLength, userId);
218    return true;
219  }
220 
221  NetworkGameManager::getInstance()->setPreferedTeam( userId, teamId );
222 
223  return true;
224}
225
226void NetworkGameManager::setPreferedTeam( int userId, int teamId )
227{
228  if ( !PlayerStats::getStats( userId ) )
229    return;
230 
231  PlayerStats & stats = *(PlayerStats::getStats( userId ));
232 
233  stats.setPreferedTeamId( teamId );
234}
235
236/**
237 * set prefered team for this host
238 * @param teamId
239 */
240void NetworkGameManager::prefereTeam( int teamId )
241{
242  if ( isServer() )
243    setPreferedTeam( getHostID(), teamId );
244  else
245  {
246    byte buf[INTSIZE];
247   
248    assert( Converter::intToByteArray( teamId, buf, INTSIZE) == INTSIZE );
249   
250    MessageManager::getInstance()->sendMessage( MSGID_PREFEREDTEAM, buf, INTSIZE, RT_USER, 0, MP_HIGHBANDWIDTH );
251  }
252}
253
254/**
255 * this function will be called periodically by networkManager
256 * @param ds time elapsed since last call of tick
257 */
258void NetworkGameManager::tick( float ds )
259{
260  //delete playables if they are not assigned to local player anymore
261  for ( std::list<Playable*>::iterator it = playablesToDelete.begin(); it != playablesToDelete.end();  )
262  {
263    if ( State::getPlayer()->getPlayable() != *it )
264    {
265      PRINTF(0)("Delete unused playable: %s owner: %d\n", (*it)->getClassName(), (*it)->getOwner() );
266      std::list<Playable*>::iterator delit = it;
267      it++;
268      delete *delit;
269      playablesToDelete.erase( delit );
270      continue;
271    }
272    it++;
273  }
274}
275
276
277
278
Note: See TracBrowser for help on using the repository browser.