Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 8184 was 8147, checked in by bensch, 18 years ago

orxonox/trunk: merged the network branche back here
merged with command:
svn merge -r8070:HEAD https://svn.orxonox.net/orxonox/branches/network .
no conflicts

File size: 6.9 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
116
117/**
118 * remove player from game
119 * @param userID
120 * @return
121 */
122bool NetworkGameManager::signalLeftPlayer(int userID)
123{
124  delete PlayerStats::getStats( userID )->getPlayable();
125  delete PlayerStats::getStats( userID );
126}
127
128
129
130/**
131 * handler for remove synchronizeable messages
132 * @param messageId
133 * @param data
134 * @param dataLength
135 * @param someData
136 * @param userId
137 * @return true on successfull handling else handler will be called again
138 */
139bool NetworkGameManager::delSynchronizeableHandler( MessageId messageId, byte * data, int dataLength, void * someData, int userId )
140{
141  if ( getInstance()->isServer() )
142  {
143    PRINTF(2)("Recieved DeleteSynchronizeable message from client %d!\n", userId);
144    return true;
145  }
146 
147  int uniqueId = 0;
148  int len = Converter::byteArrayToInt( data, &uniqueId );
149 
150  if ( len != dataLength )
151  {
152    PRINTF(2)("Recieved DeleteSynchronizeable message with incorrect size (%d) from client %d!\n", dataLength, userId);
153    return true;
154  }
155 
156  const std::list<BaseObject*> * list = ClassList::getList( CL_SYNCHRONIZEABLE );
157 
158  for ( std::list<BaseObject*>::const_iterator it = list->begin(); it != list->end(); it++ )
159  {
160    if ( dynamic_cast<Synchronizeable*>(*it)->getUniqueID() == uniqueId )
161    {
162      if ( (*it)->isA(CL_PLAYABLE) )
163      {
164        getInstance()->playablesToDelete.push_back( dynamic_cast<Playable*>(*it) );
165        return true;
166      }
167       
168      delete dynamic_cast<Synchronizeable*>(*it);
169      return true;
170    }
171  }
172}
173
174/**
175 * removes synchronizeable (also on clients)
176 * @param uniqueId uniqueid to delete
177 */
178void NetworkGameManager::removeSynchronizeable( int uniqueId )
179{
180  byte buf[INTSIZE];
181 
182  assert( Converter::intToByteArray( uniqueId, buf, INTSIZE ) == INTSIZE );
183 
184  MessageManager::getInstance()->sendMessage( MSGID_DELETESYNCHRONIZEABLE, buf, INTSIZE, RT_ALL_NOT_ME, 0, MP_HIGHBANDWIDTH );
185}
186
187
188
189/**
190 * handler for MSGID_PREFEREDTEAM message
191 * @param messageId
192 * @param data
193 * @param dataLength
194 * @param someData
195 * @param userId
196 * @return
197 */
198bool NetworkGameManager::preferedTeamHandler( MessageId messageId, byte * data, int dataLength, void * someData, int userId )
199{
200  assert( NetworkGameManager::getInstance()->isServer() );
201 
202  int teamId = 0;
203  int len = Converter::byteArrayToInt( data, &teamId );
204 
205  if ( len != dataLength )
206  {
207    PRINTF(2)("Recieved DeleteSynchronizeable message with incorrect size (%d) from client %d!\n", dataLength, userId);
208    return true;
209  }
210 
211  NetworkGameManager::getInstance()->setPreferedTeam( userId, teamId );
212}
213
214void NetworkGameManager::setPreferedTeam( int userId, int teamId )
215{
216  if ( !PlayerStats::getStats( userId ) )
217    return;
218 
219  PlayerStats & stats = *(PlayerStats::getStats( userId ));
220 
221  stats.setPreferedTeamId( teamId );
222 
223}
224
225/**
226 * set prefered team for this host
227 * @param teamId
228 */
229void NetworkGameManager::prefereTeam( int teamId )
230{
231  if ( isServer() )
232    setPreferedTeam( getHostID(), teamId );
233  else
234  {
235    byte buf[INTSIZE];
236   
237    assert( Converter::intToByteArray( teamId, buf, INTSIZE) == INTSIZE );
238   
239    MessageManager::getInstance()->sendMessage( MSGID_PREFEREDTEAM, buf, INTSIZE, RT_USER, 0, MP_HIGHBANDWIDTH );
240  }
241}
242
243/**
244 * this function will be called periodically by networkManager
245 * @param ds time elapsed since last call of tick
246 */
247void NetworkGameManager::tick( float ds )
248{
249  //delete playables if they are not assigned to local player anymore
250  for ( std::list<Playable*>::iterator it = playablesToDelete.begin(); it != playablesToDelete.end();  )
251  {
252    if ( State::getPlayer()->getPlayable() != *it )
253    {
254      PRINTF(0)("Delete unused playable: %s owner: %d\n", (*it)->getClassName(), (*it)->getOwner() );
255      std::list<Playable*>::iterator delit = it;
256      it++;
257      delete *delit;
258      playablesToDelete.erase( delit );
259      continue;
260    }
261    it++;
262  }
263}
264
265
266
267
Note: See TracBrowser for help on using the repository browser.