Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 9068 was 9059, checked in by patrick, 18 years ago

merged the network branche with the trunk

File size: 9.8 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#include "debug.h"
26
27#include "network_stream.h"
28#include "shared_network_data.h"
29#include "converter.h"
30#include "message_manager.h"
31
32#include "playable.h"
33#include "player.h"
34
35#include "game_world.h"
36
37#include "game_rules.h"
38#include "network_game_rules.h"
39
40#include "network_game_manager.h"
41
42#include "multiplayer_team_deathmatch.h"
43
44
45/* using namespace std is default, this needs to be here */
46using namespace std;
47
48NetworkGameManager* NetworkGameManager::singletonRef = NULL;
49
50/*!
51 * Standard constructor
52 */
53NetworkGameManager::NetworkGameManager()
54  : Synchronizeable()
55{
56  PRINTF(0)("START\n");
57
58  /* set the class id for the base object */
59  this->setClassID(CL_NETWORK_GAME_MANAGER, "NetworkGameManager");
60
61  this->setSynchronized(true);
62 
63  MessageManager::getInstance()->registerMessageHandler( MSGID_DELETESYNCHRONIZEABLE, delSynchronizeableHandler, NULL );
64  MessageManager::getInstance()->registerMessageHandler( MSGID_PREFEREDTEAM, preferedTeamHandler, NULL );
65  MessageManager::getInstance()->registerMessageHandler( MSGID_CHATMESSAGE, chatMessageHandler, NULL );
66 
67  this->gameState = 0;
68  registerVar( new SynchronizeableInt( &gameState, &gameState, "gameState" ) );
69}
70
71/*!
72 * Standard destructor
73 */
74NetworkGameManager::~NetworkGameManager()
75{
76  delete MessageManager::getInstance();
77 
78  PlayerStats::deleteAllPlayerStats();
79 
80  NetworkGameManager::singletonRef = NULL;
81}
82
83
84/**
85 * insert new player into game
86 * @param userId
87 * @return
88 */
89bool NetworkGameManager::signalNewPlayer( int userId )
90{
91  assert( SharedNetworkData::getInstance()->isGameServer() );
92  assert( State::getGameRules() );
93  assert( State::getGameRules()->isA( CL_NETWORK_GAME_RULES ) );
94 
95  NetworkGameRules & rules = *(dynamic_cast<NetworkGameRules*>(State::getGameRules()));
96 
97  int team = rules.getTeamForNewUser();
98  ClassID playableClassId = rules.getPlayableClassId( userId, team );
99  std::string playableModel = rules.getPlayableModelFileName( userId, team, playableClassId );
100 
101  BaseObject * bo = Factory::fabricate( playableClassId );
102 
103  assert( bo != NULL );
104  assert( bo->isA( CL_PLAYABLE ) );
105 
106  Playable & playable = *(dynamic_cast<Playable*>(bo));
107 
108  if (  playableModel != "" )
109    playable.loadModel( playableModel );
110  playable.setOwner( userId );
111  playable.setUniqueID( SharedNetworkData::getInstance()->getNewUniqueID() );
112  playable.setSynchronized( true );
113 
114  PlayerStats * stats = rules.getNewPlayerStats( userId );
115 
116  stats->setUniqueID( SharedNetworkData::getInstance()->getNewUniqueID() );
117  stats->setSynchronized( true );
118  stats->setOwner( SharedNetworkData::getInstance()->getHostID() );
119 
120  stats->setTeamId( team );
121  stats->setPlayableClassId( playableClassId );
122  stats->setPlayableUniqueId( playable.getUniqueID() );
123  stats->setModelFileName( playableModel );
124 
125  if ( rules.isA( CL_MULTIPLAYER_TEAM_DEATHMATCH ) )
126    dynamic_cast<MultiplayerTeamDeathmatch*>(&rules)->respawnPlayable( &playable, team, 0.0f );
127 
128  return true;
129}
130
131
132/**
133 * remove player from game
134 * @param userID
135 * @return
136 */
137bool NetworkGameManager::signalLeftPlayer(int userID)
138{
139  if ( PlayerStats::getStats( userID ) )
140  {
141    if ( PlayerStats::getStats( userID )->getPlayable() )
142      delete PlayerStats::getStats( userID )->getPlayable();
143    delete PlayerStats::getStats( userID );
144  }
145 
146  return true;
147}
148
149
150
151/**
152 * handler for remove synchronizeable messages
153 * @param messageId
154 * @param data
155 * @param dataLength
156 * @param someData
157 * @param userId
158 * @return true on successfull handling else handler will be called again
159 */
160bool NetworkGameManager::delSynchronizeableHandler( MessageId messageId, byte * data, int dataLength, void * someData, int userId )
161{
162  if ( getInstance()->isServer() )
163  {
164    PRINTF(2)("Recieved DeleteSynchronizeable message from client %d!\n", userId);
165    return true;
166  }
167 
168  int uniqueId = 0;
169  int len = Converter::byteArrayToInt( data, &uniqueId );
170 
171  if ( len != dataLength )
172  {
173    PRINTF(2)("Recieved DeleteSynchronizeable message with incorrect size (%d) from client %d!\n", dataLength, userId);
174    return true;
175  }
176 
177  const std::list<BaseObject*> * list = ClassList::getList( CL_SYNCHRONIZEABLE );
178 
179  for ( std::list<BaseObject*>::const_iterator it = list->begin(); it != list->end(); it++ )
180  {
181    if ( dynamic_cast<Synchronizeable*>(*it)->getUniqueID() == uniqueId )
182    {
183      if ( (*it)->isA(CL_PLAYABLE) )
184      {
185        getInstance()->playablesToDelete.push_back( dynamic_cast<Playable*>(*it) );
186        return true;
187      }
188     
189      delete dynamic_cast<Synchronizeable*>(*it);
190      return true;
191    }
192  }
193 
194  return true;
195}
196
197/**
198 * removes synchronizeable (also on clients)
199 * @param uniqueId uniqueid to delete
200 */
201void NetworkGameManager::removeSynchronizeable( int uniqueId )
202{
203  byte buf[INTSIZE];
204 
205  assert( Converter::intToByteArray( uniqueId, buf, INTSIZE ) == INTSIZE );
206
207  MessageManager::getInstance()->sendMessage( MSGID_DELETESYNCHRONIZEABLE, buf, INTSIZE, RT_ALL_NOT_ME, 0, MP_HIGHBANDWIDTH );
208}
209
210
211
212/**
213 * handler for MSGID_PREFEREDTEAM message
214 * @param messageId
215 * @param data
216 * @param dataLength
217 * @param someData
218 * @param userId
219 * @return
220 */
221bool NetworkGameManager::preferedTeamHandler( MessageId messageId, byte * data, int dataLength, void * someData, int userId )
222{
223  assert( NetworkGameManager::getInstance()->isServer() );
224 
225  int teamId = 0;
226  int len = Converter::byteArrayToInt( data, &teamId );
227 
228  if ( len != dataLength )
229  {
230    PRINTF(2)("Recieved DeleteSynchronizeable message with incorrect size (%d) from client %d!\n", dataLength, userId);
231    return true;
232  }
233 
234  NetworkGameManager::getInstance()->setPreferedTeam( userId, teamId );
235 
236  return true;
237}
238
239void NetworkGameManager::setPreferedTeam( int userId, int teamId )
240{
241  if ( !PlayerStats::getStats( userId ) )
242    return;
243 
244  PlayerStats & stats = *(PlayerStats::getStats( userId ));
245 
246  stats.setPreferedTeamId( teamId );
247}
248
249/**
250 * set prefered team for this host
251 * @param teamId
252 */
253void NetworkGameManager::prefereTeam( int teamId )
254{
255  if ( isServer() )
256    setPreferedTeam( SharedNetworkData::getInstance()->getHostID(), teamId );
257  else
258  {
259    byte buf[INTSIZE];
260   
261    assert( Converter::intToByteArray( teamId, buf, INTSIZE) == INTSIZE );
262   
263    MessageManager::getInstance()->sendMessage( MSGID_PREFEREDTEAM, buf, INTSIZE, RT_USER, 0, MP_HIGHBANDWIDTH );
264  }
265}
266
267/**
268 * this function will be called periodically by networkManager
269 * @param ds time elapsed since last call of tick
270 */
271void NetworkGameManager::tick( float ds )
272{
273  //delete playables if they are not assigned to local player anymore
274  for ( std::list<Playable*>::iterator it = playablesToDelete.begin(); it != playablesToDelete.end();  )
275  {
276    if ( State::getPlayer()->getPlayable() != *it )
277    {
278      const std::list<BaseObject*> * list = ClassList::getList( CL_PLAYABLE );
279       
280      if ( list && std::find( list->begin(), list->end(), *it ) != list->end() )
281      {     
282        PRINTF(0)("Delete unused playable: %s owner: %d\n", (*it)->getClassName(), (*it)->getOwner() );
283        std::list<Playable*>::iterator delit = it;
284        it++;
285        delete *delit;
286        playablesToDelete.erase( delit );
287        continue;
288      }
289    }
290    it++;
291  }
292}
293
294
295
296bool NetworkGameManager::chatMessageHandler( MessageId messageId, byte * data, int dataLength, void * someData, int userId )
297{
298  PRINTF(0)("NetworkGameManager::chatMessageHandler %d %d\n", userId, SharedNetworkData::getInstance()->getHostID() );
299  if ( NetworkGameManager::getInstance()->isServer() && userId !=  SharedNetworkData::getInstance()->getHostID() )
300  {
301    MessageManager::getInstance()->sendMessage( messageId, data, dataLength, RT_ALL_NOT_ME, 0, MP_HIGHBANDWIDTH );
302  }
303 
304  assert( State::getGameRules() );
305  assert( State::getGameRules()->isA( CL_NETWORK_GAME_RULES ) );
306 
307  NetworkGameRules & rules = *(dynamic_cast<NetworkGameRules*>(State::getGameRules()));
308 
309  if ( dataLength < 3*INTSIZE )
310  {
311    PRINTF(2)("got too small chatmessage from client %d\n", userId);
312   
313    return true;
314  }
315 
316  int messageType = 0;
317  Converter::byteArrayToInt( data, &messageType );
318  int senderUserId = 0;
319  Converter::byteArrayToInt( data+INTSIZE, &senderUserId );
320  std::string message;
321  Converter::byteArrayToString( data+2*INTSIZE, message, dataLength-2*INTSIZE );
322 
323  rules.handleChatMessage( senderUserId, message, messageType );
324
325  return true;
326}
327
328/**
329 * send chat message
330 * @param message message text
331 * @param messageType some int
332 */
333void NetworkGameManager::sendChatMessage( const std::string & message, int messageType )
334{
335  byte * buf = new byte[message.length()+3*INTSIZE];
336
337  assert( Converter::intToByteArray( messageType, buf, INTSIZE ) == INTSIZE );
338  assert( Converter::intToByteArray( SharedNetworkData::getInstance()->getHostID(), buf+INTSIZE, INTSIZE ) == INTSIZE );
339  assert( Converter::stringToByteArray(message, buf+2*INTSIZE, message.length()+INTSIZE) == message.length()+INTSIZE );
340 
341  if ( this->isServer() )
342    MessageManager::getInstance()->sendMessage( MSGID_CHATMESSAGE, buf, message.length()+3*INTSIZE, RT_ALL_ME, 0, MP_HIGHBANDWIDTH );
343  else
344    MessageManager::getInstance()->sendMessage( MSGID_CHATMESSAGE, buf, message.length()+3*INTSIZE, RT_ALL_NOT_ME, 0, MP_HIGHBANDWIDTH );
345
346 
347  delete [] buf;
348}
349
350
351
Note: See TracBrowser for help on using the repository browser.