Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 8802 was 8708, checked in by bensch, 18 years ago

merged network back
merged with command:
svn merge -r8625:HEAD https://svn.orxonox.net/orxonox/branches/network .
no conflicts

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