Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

teamchoice works now

File size: 6.1 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      delete dynamic_cast<Synchronizeable*>(*it);
163      return true;
164    }
165  }
166}
167
168/**
169 * removes synchronizeable (also on clients)
170 * @param uniqueId uniqueid to delete
171 */
172void NetworkGameManager::removeSynchronizeable( int uniqueId )
173{
174  byte buf[INTSIZE];
175 
176  assert( Converter::intToByteArray( uniqueId, buf, INTSIZE ) == INTSIZE );
177 
178  MessageManager::getInstance()->sendMessage( MSGID_DELETESYNCHRONIZEABLE, buf, INTSIZE, RT_ALL_NOT_ME, 0, MP_HIGHBANDWIDTH );
179}
180
181
182
183/**
184 * handler for MSGID_PREFEREDTEAM message
185 * @param messageId
186 * @param data
187 * @param dataLength
188 * @param someData
189 * @param userId
190 * @return
191 */
192bool NetworkGameManager::preferedTeamHandler( MessageId messageId, byte * data, int dataLength, void * someData, int userId )
193{
194  assert( NetworkGameManager::getInstance()->isServer() );
195 
196  int teamId = 0;
197  int len = Converter::byteArrayToInt( data, &teamId );
198 
199  if ( len != dataLength )
200  {
201    PRINTF(2)("Recieved DeleteSynchronizeable message with incorrect size (%d) from client %d!\n", dataLength, userId);
202    return true;
203  }
204 
205  NetworkGameManager::getInstance()->setPreferedTeam( userId, teamId );
206}
207
208void NetworkGameManager::setPreferedTeam( int userId, int teamId )
209{
210  if ( !PlayerStats::getStats( userId ) )
211    return;
212 
213  PlayerStats & stats = *(PlayerStats::getStats( userId ));
214 
215  stats.setPreferedTeamId( teamId );
216 
217}
218
219/**
220 * set prefered team for this host
221 * @param teamId
222 */
223void NetworkGameManager::prefereTeam( int teamId )
224{
225  if ( isServer() )
226    setPreferedTeam( getHostID(), teamId );
227  else
228  {
229    byte buf[INTSIZE];
230   
231    assert( Converter::intToByteArray( teamId, buf, INTSIZE) == INTSIZE );
232   
233    MessageManager::getInstance()->sendMessage( MSGID_PREFEREDTEAM, buf, INTSIZE, RT_USER, 0, MP_HIGHBANDWIDTH );
234  }
235}
236
237
238
239
Note: See TracBrowser for help on using the repository browser.