Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/multi_player_map/src/lib/network/network_game_manager.cc @ 8827

Last change on this file since 8827 was 8827, checked in by rennerc, 18 years ago

SpawningPoint works now

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