Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 8404 was 8362, checked in by bensch, 18 years ago

orxonox/trunk: removed stupid included in base_object.h
this should lead to faster compile-times

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