Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

removed some output of heightMap. commented out old code in networkGameManager

File size: 23.1 KB
RevLine 
[6067]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
[7193]22#include "util/loading/factory.h"
[7349]23#include "state.h"
24#include "class_list.h"
25
[6341]26#include "network_stream.h"
[7349]27#include "shared_network_data.h"
[6341]28#include "converter.h"
29
[6498]30#include "playable.h"
31#include "player.h"
[6424]32
[7349]33#include "game_world.h"
[6424]34
[6116]35#include "network_game_manager.h"
[6067]36
37
38/* using namespace std is default, this needs to be here */
39using namespace std;
40
[6341]41NetworkGameManager* NetworkGameManager::singletonRef = NULL;
42
[6067]43/*!
44 * Standard constructor
45 */
[6116]46NetworkGameManager::NetworkGameManager()
[6695]47  : Synchronizeable()
[6067]48{
[6341]49  PRINTF(0)("START\n");
50
[6067]51  /* set the class id for the base object */
[6341]52  this->setClassID(CL_NETWORK_GAME_MANAGER, "NetworkGameManager");
53
54  hasRequestedWorld = false;
[6695]55  this->setSynchronized(true);
[6067]56}
57
58/*!
59 * Standard destructor
60 */
[6116]61NetworkGameManager::~NetworkGameManager()
[6067]62{
[7567]63#if 0
[6341]64  for ( int i = 0; i<outBuffer.size(); i++)
65  {
66    if ( outBuffer[i].buffer )
67      delete outBuffer[i].buffer;
68  }
[7567]69#endif
[6341]70
[6067]71}
72
[7567]73#if 0
[6341]74int NetworkGameManager::writeBytes(const byte* data, int length, int sender)
[6067]75{
[6341]76  int i = 0;
77  byte b;
78
79  while ( i<length )
80  {
81    b = data[i++];
82
[6695]83    /**************** Commands only processed by servers ****************/
[6341]84    if ( isServer() )
85    {
[6695]86      if ( b == NET_REQUEST_CREATE )
[6341]87      {
88        if ( !handleRequestCreate( i, data, length, sender ) )
89          return i;
90        continue;
91      }
[6695]92      else if ( b == NET_REQUEST_REMOVE )
[6341]93      {
94        if ( !handleRequestRemove( i, data, length, sender ) )
95          return i;
96        continue;
97      }
[6695]98      else if ( b == NET_REQUEST_PNODE_PATH )
99      {
100        if ( !handleRequestPNodePath( i, data, length, sender ) )
101          return i;
102        continue;
103      }
[6341]104    }
105    else
106    {
[6695]107      /**************** Commands only processed by clients ****************/
108      if ( b == NET_CREATE_ENTITY )
[6341]109      {
[6634]110        PRINTF(0)("CREATE_ENTITY\n");
[6341]111        if ( !handleCreateEntity( i, data, length, sender ) )
112          return i;
113        continue;
114      }
[6695]115      else if ( b == NET_REMOVE_ENTITY )
[6341]116      {
117        if ( !handleRemoveEntity( i, data, length, sender ) )
118          return i;
119        continue;
120      }
[6695]121      else if ( b == NET_CREATE_ENTITY_LIST )
[6341]122      {
123        if ( !handleCreateEntityList( i, data, length, sender ) )
124          return i;
125        continue;
126      }
[6695]127      else if ( b == NET_REMOVE_ENTITY_LIST )
[6341]128      {
129        if ( !handleRemoveEntityList( i, data, length, sender ) )
130          return i;
131        continue;
132      }
[6695]133      else if ( b == NET_YOU_ARE_ENTITY )
[6341]134      {
135        if ( !handleYouAreEntity( i, data, length, sender ) )
136          return i;
137        continue;
138      }
139    }
140
[6695]141    /**************** Commands processed by servers and clients ****************/
142    if ( b == NET_REQUEST_ENTITY_LIST )
[6341]143    {
[6634]144      sendEntityList( sender );
[6341]145      continue;
146    }
147
[6695]148
149    PRINTF(1)("Network is asynchronous: couldn't decode the command sent by %i\n", sender);
150    PRINTF(1)("Probably this is because the network protocol has different \n");
151    PRINTF(1)("versions or there occured an error in the sending algorithm\n");
[6341]152    PRINTF(1)("Data is not in the right format! i=%d\n", i);
153    return i;
154  }
155
156  return i;
[6067]157}
[7567]158#endif
[6067]159
[7567]160#if 0
[6116]161int NetworkGameManager::readBytes(byte* data, int maxLength, int * reciever)
[6067]162{
[6341]163  if ( !isServer() && !hasRequestedWorld )
164  {
[6815]165    assert( maxLength >= 1 );
166    data[0] = NET_REQUEST_ENTITY_LIST;
[6341]167    hasRequestedWorld = true;
[6815]168    return 1;
[6341]169  }
[6498]170
[6341]171  for ( int i = 0; i<outBuffer.size(); i++ )
172  {
173    *reciever = i;
174    if ( outBuffer[i].length>0 )
175    {
176      int nbytes = outBuffer[i].length;
177      outBuffer[i].length = 0;
178
179      if ( nbytes > maxLength )
180      {
181        PRINTF(1)("OutBuffer.length (%d) > (%d) networkStreamBuffer.maxLength\n", nbytes, maxLength);
182        return 0;
183      }
184
185      memcpy(data, outBuffer[i].buffer, nbytes);
186      return nbytes;
187    }
188  }
189
[6634]190  return 0;
[6067]191}
[7567]192#endif
[6067]193
[7567]194#if 0
[6116]195void NetworkGameManager::writeDebug() const
[6067]196{
197}
198
[6116]199void NetworkGameManager::readDebug() const
[6067]200{
201}
[7567]202#endif
[6067]203
204
205/*!
206 * Checks whether this is connected to a server or a client
[6341]207 * and afterwards creates the needed entity
[6067]208 * @param classID: The ID of the class of which an entity should be created
209 */
[6498]210int NetworkGameManager::createEntity( ClassID classID, int owner )
[6067]211{
[6695]212  if ( this->isServer())
[6341]213  {
[6695]214    if ( SharedNetworkData::getInstance()->getNewUniqueID() < 0 )
[6341]215    {
216      PRINTF(1)("Cannot create entity! There are no more uniqueIDs left!\n");
[6498]217      return -1;
[6341]218    }
[6695]219    return this->executeCreateEntity( classID, SharedNetworkData::getInstance()->getNewUniqueID(), owner );
[6341]220  }
221  else
222  {
[7567]223#if 0
[6341]224    this->requestCreateEntity( classID );
[7567]225#endif
[6498]226    return -1;
[6341]227  }
[6067]228}
229
[6341]230
[6067]231/*!
232 * Checks whether this is connected to a server or a client
[6341]233 * and afterwards creates the needed entity
234 * @param classID: The ID of the class of which an entity should be created
235 */
[6424]236BaseObject* NetworkGameManager::createEntity(const TiXmlElement* element)
[6341]237{
238  if ( this->isServer() )
239  {
[6695]240    if ( SharedNetworkData::getInstance()->getNewUniqueID() < 0 )
[6341]241    {
242      PRINTF(1)("Cannot create entity! There are no more uniqueIDs left!\n");
243      return NULL;
244    }
245
246    BaseObject * b = Factory::fabricate( element );
247
248    if ( !b )
249    {
[6424]250      PRINTF(1)("Could not fabricate Object with className %s\n", element->Value() );
[6341]251      return NULL;
252    }
253
254
255    if ( b->isA(CL_SYNCHRONIZEABLE) )
256    {
257      Synchronizeable * s = dynamic_cast<Synchronizeable*>(b);
[6695]258      s->setUniqueID( SharedNetworkData::getInstance()->getNewUniqueID() );
[6341]259      s->setOwner( 0 );
[6695]260      // all entities created via this function are added automaticaly to the synchronizeable list
261      s->setSynchronized(true);
[6341]262      return b;
263    }
264    else
265    {
266      PRINTF(1)("Class %s is not a synchronizeable!\n", b->getClassName() );
267      delete b;
268    }
[6498]269
[6341]270  }
271  else
[6695]272
[6341]273  {
274    PRINTF(1)("This node is not a server and cannot create id %x\n", element->Value());
275  }
276  return NULL;
277}
278
279
280/*!
281 * Checks whether this is connected to a server or a client
[6067]282 * and afterwards removes the specified entity
283 * @param uniqueID: The ID of the entity object which should be removed
284 */
[6116]285void NetworkGameManager::removeEntity(int uniqueID)
[6067]286{
[6341]287  if ( this->isServer() )
288  {
289    this->executeRemoveEntity( uniqueID );
290  }
291  else
292  {
[7567]293#if 0
[6341]294    this->requestRemoveEntity( uniqueID );
[7567]295#endif
[6341]296  }
[6067]297}
298
299
[7567]300#if 0
[6067]301/*!
302 * Creates the needed entity on the server if possible
303 * @param classID: The ID of the class of which an entity should be created
304 */
[6341]305void NetworkGameManager::requestCreateEntity(ClassID classID)
[6067]306{
[6634]307  for ( int i = 0; i<outBuffer.size(); i++)
308  {
309    if ( !this->networkStream->isUserIdActive( i ) )
310      continue;
311
[6695]312    if ( !writeToClientBuffer( outBuffer[i], (byte)NET_REQUEST_CREATE ) )
[6634]313      return;
314    if ( !writeToClientBuffer( outBuffer[i], (int)classID ) )
315      return;
316  }
[6067]317}
[7567]318#endif
[6067]319
[7567]320#if 0
[6067]321/*!
322 * Removes the specified entity on the server
323 * @param uniqueID: The ID of the entity object which should be removed
324 */
[6116]325void NetworkGameManager::requestRemoveEntity(int uniqueID)
[6067]326{
[6634]327  for ( int i = 0; i<outBuffer.size(); i++)
328  {
329    if ( !this->networkStream->isUserIdActive( i ) )
330      continue;
331
[6695]332    if ( !writeToClientBuffer( outBuffer[i], (byte)NET_REQUEST_REMOVE ) )
[6634]333      return;
334    if ( !writeToClientBuffer( outBuffer[i], uniqueID ) )
335      return;
336  }
[6067]337}
[7567]338#endif
[6067]339
340/*!
341 * Creates the needed entity if possible
342 * This function is called if this is a server
343 * @param classID: The ID of the class of which an entity should be created
344 */
[6498]345int NetworkGameManager::executeCreateEntity(ClassID classID, int uniqueID, int owner)
[6067]346{
[7567]347#if 0
[6634]348  for ( int i = 0; i<outBuffer.size(); i++)
349  {
350    if ( !this->networkStream->isUserIdActive( i ) )
351      continue;
[6341]352
[6695]353    if ( !writeToClientBuffer( outBuffer[i], (byte)NET_CREATE_ENTITY ) )
[6634]354      return -1;
355    if ( !writeToClientBuffer( outBuffer[i], (int)classID ) )
356      return -1;
357    if ( !writeToClientBuffer( outBuffer[i], uniqueID ) )
358      return -1;
359    if ( !writeToClientBuffer( outBuffer[i], owner ) )
360      return -1;
361  }
[7567]362#endif
[6695]363  PRINTF(0)("ExecuteCreateEntity: server side: classID: %x, uniqueID: %i, owner: %i\n", classID, uniqueID, owner);
[6341]364  doCreateEntity( classID, uniqueID, owner );
[6498]365
366  return uniqueID;
[6067]367}
368
369/*!
370 * Removes the specified entity
371 * This function is called if this is a server
372 * @param uniqueID: The ID of the entity object which should be removed
373 */
[6116]374void NetworkGameManager::executeRemoveEntity(int uniqueID)
[6067]375{
[7567]376#if 0
[6634]377  for ( int i = 0; i<outBuffer.size(); i++)
378  {
379    if ( !this->networkStream->isUserIdActive( i ) )
380      continue;
[6341]381
[6695]382    if ( !writeToClientBuffer( outBuffer[i], (byte)NET_REMOVE_ENTITY ) )
[6634]383      return;
384    if ( !writeToClientBuffer( outBuffer[i], uniqueID ) )
385      return;
386  }
[7567]387#endif
[6634]388
[6341]389  doRemoveEntity(uniqueID);
[6067]390}
391
392/*!
393 * Checks whether it is possible to create an entity of a given class
394 * @return: true if the entity can be created, false otherwise
395 */
[6341]396bool NetworkGameManager::canCreateEntity(ClassID classID)
[6067]397{
[6341]398  return true;
[6067]399}
[6341]400
[7567]401#if 0
[6341]402/*!
403 * Sends the Entities to the new connected client
404 * @param userID: The ID of the user
405 */
406void NetworkGameManager::sendEntityList( int userID )
407{
408  if ( !isServer() )
409    return;
410
411  if ( userID >= outBuffer.size() )
412    resizeBufferVector( userID );
413
414  SynchronizeableList::const_iterator it, e;
415
416  it = this->networkStream->getSyncBegin();
417  e = this->networkStream->getSyncEnd();
418
[6695]419  // send the packet header
420  if ( !writeToClientBuffer( outBuffer[userID], (byte)NET_CREATE_ENTITY_LIST ) )
[6341]421    return;
422
[6695]423  // send the number of entities: -2 because you must not send network_game_manager and handshake
[6341]424  if ( !writeToClientBuffer( outBuffer[userID], networkStream->getSyncCount() ) )
425    return;
426
427  //PRINTF(0)("SendEntityList: n = %d\n", networkStream->getSyncCount()-2 );
428
[6695]429  // first send the NullParent
430  if ( !writeToClientBuffer( outBuffer[userID], (int)PNode::getNullParent()->getLeafClassID()) )
431    return;
432  if ( !writeToClientBuffer( outBuffer[userID], (int)PNode::getNullParent()->getUniqueID()) )
433    return;
434  if ( !writeToClientBuffer( outBuffer[userID], (int)PNode::getNullParent()->getOwner()) )
435    return;
[6424]436
[6695]437  // now send the rest of the entities
[6341]438  while ( it != e )
439  {
[6695]440    if( (*it)->beSynchronized() && (*it) != PNode::getNullParent())
441    {
442      PRINTF(0)("SENDING ENTITY %s classid: %x, uniqueid %d\n", (*it)->getClassName(), (*it)->getLeafClassID(), (*it)->getUniqueID() );
443      if ( !writeToClientBuffer( outBuffer[userID], (int)((*it)->getLeafClassID()) ) )
444        return;
[6341]445
[6695]446      if ( !writeToClientBuffer( outBuffer[userID], (int)((*it)->getUniqueID()) ) )
447        return;
[6341]448
[6695]449      if ( !writeToClientBuffer( outBuffer[userID], (int)((*it)->getOwner()) ) )
450        return;
451    }
[6498]452    it++;
453  }
[6424]454
[6498]455  signalNewPlayer( userID );
456}
[7567]457#endif
[6424]458
459
460
[6498]461bool NetworkGameManager::signalNewPlayer(int userId)
462{
[7567]463#if 0
[6634]464  if ( userId >= outBuffer.size() )
465    resizeBufferVector( userId );
[6341]466
[6498]467  /* create new playable for Player*/
468  PRINTF(0)("Request for creation: %i\n", userId);
469  int uniqueId = this->createEntity(CL_SPACE_SHIP, userId);
470  PRINTF(0)("Request for creation: userid: %i, uniqueid: %i\n", userId, uniqueId);
471  this->sendYouAre(uniqueId, userId);
[7567]472#endif
[6341]473}
474
[6498]475
[6737]476
477bool NetworkGameManager::signalLeftPlayer(int userID)
478{
479  const std::list<BaseObject*>* playableList = ClassList::getList(CL_PLAYABLE);
480  std::list<BaseObject*>::const_iterator it = playableList->begin();
481
482  for(; it != playableList->end(); it++)
483  {
484    if( dynamic_cast<Synchronizeable*>(*it)->getOwner() == userID )
485    {
486      PRINTF(0)("remove playable from %i\n", userID);
487      this->removeEntity(dynamic_cast<Synchronizeable*>(*it)->getUniqueID());
488      return true;
489    }
490  }
491  return false;
492}
493
[7567]494#if 0
[6341]495/**
496 * Creates a buffer for user n
497 * @param n The ID of the user
498 */
499void NetworkGameManager::resizeBufferVector( int n )
500{
501  for ( int i = outBuffer.size(); i<=n; i++)
502  {
503    clientBuffer outBuf;
504
505    outBuf.length = 0;
506
507    outBuf.maxLength = 5*1024;
508
509    outBuf.buffer = new byte[5*1014];
510
511    outBuffer.push_back(outBuf);
512  }
513}
[7567]514#endif
[6341]515
516/**
517 * Creates the entity on this host
518 * @param classID: ClassID of the entity to create
519 * @param uniqueID: Unique ID to assign to the synchronizeable
520 * @param owner: owner of this synchronizealbe
521 */
[6424]522BaseObject* NetworkGameManager::doCreateEntity( ClassID classID, int uniqueID, int owner )
[6341]523{
[6695]524  PRINTF(0)("Creating Entity via Factory: classid: %x, uniqueID: %i, owner: %i\n", classID, uniqueID, owner);
[6341]525
[6695]526  BaseObject * b;
527  /* These are some small exeptions in creation: Not all objects can/should be created via Factory */
528  /* Exception 1: NullParent */
529  if( classID == CL_NULL_PARENT)
530  {
531    b = (BaseObject*)PNode::getNullParent();
532  }
533  else
534    b = Factory::fabricate( classID );
535
[6341]536  if ( !b )
537  {
538    PRINTF(1)("Could not fabricate Object with classID %x\n", classID);
[6424]539    return NULL;
[6341]540  }
541
542  if ( b->isA(CL_SYNCHRONIZEABLE) )
543  {
544    Synchronizeable * s = dynamic_cast<Synchronizeable*>(b);
545    s->setUniqueID( uniqueID );
546    s->setOwner( owner );
[6695]547    s->setSynchronized(true);
548    //this->networkStream->connectSynchronizeable( *s );
[7444]549
[6341]550    PRINTF(0)("Fabricated %s with id %d\n", s->getClassName(), s->getUniqueID());
[6498]551
[6994]552    //TODO HACK: hack to prevent collision
[6634]553    if ( b->isA(CL_WORLD_ENTITY) && !b->isA(CL_PLAYABLE) )
[6498]554    {
[6959]555      if ( SharedNetworkData::getInstance()->getHostID()!=0 )
[6498]556      {
557        static Vector pos = Vector(1000.0, 1000.0, 1000.0);
558        PNode *p = dynamic_cast<PNode*>(b);
[6634]559        p->setAbsCoor(pos);
[6737]560        p->updateNode(0);
[6498]561        pos += Vector(1000.0, 1000.0, 1000.0);
562      }
563    }
[6994]564    ///TODO HACK this is only for network multiplayer games.
[7354]565    if( b->isA(CL_PLAYABLE))
[6959]566    {
[7349]567      Playable* ss = dynamic_cast<Playable*>(b);
[6959]568      if( owner%2 == 0)
569      {
[6498]570
[7071]571        ss->loadModel("models/ships/reap_0.obj");
[6959]572        ss->toList(OM_GROUP_00);
[7078]573        ss->setAbsCoor(213.37, 57.71, -47.98);
574        ss->setAbsDir(Quaternion(0.16, 0.98, -0.10));
[6959]575      }
576      else
577      {
578        ss->loadModel( "models/ships/fighter.obj" );
579        ss->toList(OM_GROUP_01);
[7078]580        ss->setAbsCoor(-314.450, 40.701, 83.554);
581      }
[6959]582    }
583
[6424]584    return b;
[6341]585  }
586  else
587  {
588    PRINTF(1)("Class with ID %x is not a synchronizeable!", (int)classID);
589    delete b;
590  }
[6424]591  return NULL;
[6341]592}
593
594/**
595 * Removes a entity on this host
596 * @param uniqueID: unique ID assigned with the entity to remove
597 */
598void NetworkGameManager::doRemoveEntity( int uniqueID )
599{
600  SynchronizeableList::const_iterator it,e;
601  it = this->networkStream->getSyncBegin();
602  e = this->networkStream->getSyncEnd();
603
604  while ( it != e )
605  {
606    if ( (*it)->getUniqueID() == uniqueID )
607    {
[6868]608      assert((*it)->isA(CL_WORLD_ENTITY));
[6959]609      dynamic_cast<WorldEntity*>(*it)->leaveWorld();
[6868]610      dynamic_cast<WorldEntity*>(*it)->toList(OM_DEAD);
[6341]611      break;
612    }
613    it++;
614  }
615}
616
[7567]617#if 0
[6341]618/**
619 * Copies length bytes to the clientBuffer with error checking
620 * @param clientBuffer: the clientBuffer to write to
621 * @param data: buffer to the data
622 * @param length: length of data
623 * @return false on error true else
624 */
625bool NetworkGameManager::writeToClientBuffer( clientBuffer &cb, byte * data, int length )
626{
627  if ( length > cb.maxLength-cb.length )
628  {
629    PRINTF(1)("No space left in clientBuffer\n");
630    return false;
631  }
632
633  memcpy( cb.buffer+cb.length, data, length );
634  return true;
635}
[7567]636#endif
[6341]637
[7567]638#if 0
[6341]639/**
640 * Reads data from clientBuffer with error checking
641 * @param clientBuffer: the clientBuffer to read from
642 * @param data: pointer to the buffer
643 * @param length:
644 * @return
645 */
646bool NetworkGameManager::readFromClientBuffer( clientBuffer &cb, byte * data, int length )
647{
648  if ( cb.length < length )
649  {
650    PRINTF(0)("There is not enough data in clientBuffer\n");
651    return 0;
652  }
653
654  memcpy( data, cb.buffer+cb.length-length, length );
655  return true;
656}
[7567]657#endif
[6341]658
659/**
660 * Tells this client that he has to control this entity
661 * @param uniqueID: the entity's uniqeID
662 */
663void NetworkGameManager::doYouAre( int uniqueID )
664{
[6498]665
666  SynchronizeableList::const_iterator it = this->networkStream->getSyncBegin();
667
668  Playable *p = NULL;
[6634]669  Synchronizeable *s = NULL;
[6498]670
671  for ( ; it !=networkStream->getSyncEnd(); it++ )
672  {
673    if ( (*it)->getUniqueID()==uniqueID )
674    {
[6634]675      if ( (*it)->isA( CL_SYNCHRONIZEABLE ) )
676      {
677        s = dynamic_cast<Synchronizeable*>(*it);
678      }
[6498]679      if ( (*it)->isA( CL_PLAYABLE ) )
680      {
681        p = dynamic_cast<Playable*>(*it);
682        break;
683      } else
684      {
685        PRINTF(1)("UniqueID is not a Playable\n");
686      }
687    }
688  }
689
690  Player* player = State::getPlayer();
691  assert(p != NULL);
[6634]692  assert(s != NULL);
[6498]693  assert(player != NULL);
694
[6634]695  PRINTF(0)("uniqueID = %d\n", s->getUniqueID());
696
[6985]697  player->setPlayable(p);
[6498]698
699
[6341]700}
701
702/**
703 * Tells a remote client that he has to control this entity
704 * @param uniqueID: the entity's uniqeID
705 * @param userID: the users ID
706 */
707void NetworkGameManager::sendYouAre( int uniqueID, int userID )
708{
709  if ( !isServer() )
710    return;
[7567]711#if 0
[6341]712  if ( userID != 0 )
713  {
[6695]714    if ( !writeToClientBuffer( outBuffer[userID], (byte)NET_YOU_ARE_ENTITY ) )
[6341]715      return;
716
717    if ( !writeToClientBuffer( outBuffer[userID], uniqueID ) )
718      return;
719  }
720  else
721  {
[7567]722#endif
[6341]723    doYouAre(uniqueID);
[7567]724#if 0
[6341]725  }
[7567]726#endif
727
[6341]728}
729
730bool NetworkGameManager::handleRequestCreate( int & i, const byte * data, int length, int sender )
731{
732  if ( INTSIZE > length-i )
733  {
734    PRINTF(1)("Cannot read classID from buffer! Not enough data left!\n");
735    return false;
736  }
737  int classID;
738  i += Converter::byteArrayToInt( &data[i], &classID );
739
740  createEntity( (ClassID)classID );
741
742  return true;
743}
744
745bool NetworkGameManager::handleRequestRemove( int & i, const byte * data, int length, int sender )
746{
747  if ( INTSIZE > length-i )
748  {
749    PRINTF(1)("Cannot read uniqueID from buffer! Not enough data left!\n");
750    return false;
751  }
752  int uniqueID;
753  i += Converter::byteArrayToInt( &data[i], &uniqueID );
754
755  removeEntity( uniqueID );
756
757  return true;
758}
759
760bool NetworkGameManager::handleCreateEntity( int & i, const byte * data, int length, int sender )
761{
762  if ( INTSIZE > length-i )
763  {
764    PRINTF(1)("Cannot read classID from buffer! Not enough data left!\n");
765    return false;
766  }
767  int classID;
768  i += Converter::byteArrayToInt( &data[i], &classID );
769
770  if ( INTSIZE > length-i )
771  {
772    PRINTF(1)("Cannot read uniqueID from buffer! Not enough data left!\n");
773    return false;
774  }
775  int uniqueID;
776  i += Converter::byteArrayToInt( &data[i], &uniqueID );
777
778  if ( INTSIZE > length-i )
779  {
780    PRINTF(1)("Cannot read owner from buffer! Not enough data left!\n");
781    return false;
782  }
783  int owner;
784  i += Converter::byteArrayToInt( &data[i], &owner );
785
[6695]786  PRINTF(0)("handleCreateEntity: client side: classID: %x, uniqueID: %i, owner: %i\n", classID, uniqueID, owner);
[6341]787  doCreateEntity( (ClassID)classID, uniqueID, owner );
788
789  return true;
790}
791
792bool NetworkGameManager::handleRemoveEntity( int & i, const byte * data, int length, int sender )
793{
794  if ( INTSIZE > length-i )
795  {
796    PRINTF(1)("Cannot read uniqueID from buffer! Not enough data left!\n");
797    return false;
798  }
799  int uniqueID;
800  i += Converter::byteArrayToInt( &data[i], &uniqueID );
801
802  doRemoveEntity( uniqueID );
803
804  return true;
805}
806
807bool NetworkGameManager::handleCreateEntityList( int & i, const byte * data, int length, int sender )
808{
809  if ( INTSIZE > length-i )
810  {
811    PRINTF(1)("Cannot read n from buffer! Not enough data left!\n");
812    return false;
813  }
814
815  PRINTF(0)("HandleCreateEntityList:  data[i..i+3] = %d %d %d %d\n", data[i], data[i+1], data[i+2], data[i+3]);
816
817  int n;
818  i += Converter::byteArrayToInt( &data[i], &n );
819
820
821  PRINTF(0)("HandleCreateEntityList: n = %d\n", n);
822
823  int classID, uniqueID, owner;
824
825  for ( int j = 0; j<n; j++ )
826  {
827
828    if ( INTSIZE > length-i )
829    {
830      PRINTF(1)("Cannot read classID from buffer! Not enough data left!\n");
831      return false;
832    }
833    i += Converter::byteArrayToInt( &data[i], &classID );
834
835    if ( INTSIZE > length-i )
836    {
837      PRINTF(1)("Cannot read uniqueID from buffer! Not enough data left!\n");
838      return false;
839    }
840    i += Converter::byteArrayToInt( &data[i], &uniqueID );
841
842    if ( INTSIZE > length-i )
843    {
844      PRINTF(1)("Cannot read owner from buffer! Not enough data left!\n");
845      return false;
846    }
847    i += Converter::byteArrayToInt( &data[i], &owner );
848
849    if ( classID != CL_NETWORK_GAME_MANAGER && classID != CL_HANDSHAKE )
[6424]850    {
851      BaseObject* b = doCreateEntity( (ClassID)classID, uniqueID, owner );
852    }
853
[6341]854  }
[6424]855
[6341]856  return true;
857}
858
859bool NetworkGameManager::handleRemoveEntityList( int & i, const byte * data, int length, int sender )
860{
861  if ( INTSIZE > length-i )
862  {
863    PRINTF(1)("Cannot read n from buffer! Not enough data left!\n");
864    return false;
865  }
866  int n;
867  i += Converter::byteArrayToInt( &data[i], &n );
868
869  int uniqueID;
870
871  for ( int j = 0; j<n; j++ )
872  {
873
874    if ( INTSIZE > length-i )
875    {
876      PRINTF(1)("Cannot read uniqueID from buffer! Not enough data left!\n");
877      return false;
878    }
879    i += Converter::byteArrayToInt( &data[i], &uniqueID );
880
881    doRemoveEntity( uniqueID );
882  }
883
884  return true;
885}
886
887bool NetworkGameManager::handleYouAreEntity( int & i, const byte * data, int length, int sender )
888{
889  if ( INTSIZE > length-i )
890  {
891    PRINTF(1)("Cannot read uniqueID from buffer! Not enough data left!\n");
892    return false;
893  }
894
895  int uniqueID;
896  i += Converter::byteArrayToInt( &data[i], &uniqueID );
897
898  doYouAre( uniqueID );
899
900  return true;
901}
902
[6695]903/**
904 *  handles the network signal NET_REQUEST_PNODE_PATH
905 * @param i byte offset in the buffer
906 * @param data data array
907 * @param length length of the data arary
908 * @param sender the sender id
909 * @return true if process terminated sucessfully
910 */
911bool NetworkGameManager::handleRequestPNodePath(int& i, const byte* data, int length, int sender)
912{
913  if( INTSIZE > length-i )
914  {
915    PRINTF(1)("Cannot read n from buffer! Not enough data left!\n");
916    return false;
917  }
918  PRINTF(0)("HandleRequestPNodePath:  data[i..i+3] = %d %d %d %d\n", data[i], data[i+1], data[i+2], data[i+3]);
919
920  int uniqueID1, uniqueID2;
921  if( INTSIZE > length-i )
922  {
923    PRINTF(1)("Cannot read uniqueID from buffer! Not enough data left!\n");
924    return false;
925  }
926  i += Converter::byteArrayToInt( &data[i], &uniqueID1 );
927
928  if( INTSIZE > length-i )
929  {
930    PRINTF(1)("Cannot read uniqueID from buffer! Not enough data left!\n");
931    return false;
932  }
933  i += Converter::byteArrayToInt( &data[i], &uniqueID2 );
934
935
936  PRINTF(0)("HandleRequestPNodePath: got a request for path from uid %i to uid %i\n", uniqueID1, uniqueID2);
937
938  return true;
939}
940
[7567]941#if 0
[6341]942bool NetworkGameManager::writeToClientBuffer( clientBuffer & cb, byte b )
943{
944  if ( cb.maxLength-cb.length < 1 )
945  {
946    PRINTF(1)("Cannot write to clientBuffer! Not enough space for 1 byte\n");
947    return false;
948  }
949
950  cb.buffer[cb.length++] = b;
951
952  return true;
953}
954
[7567]955
[6341]956bool NetworkGameManager::writeToClientBuffer( clientBuffer & cb, int i )
957{
958  int n = Converter::intToByteArray( i, cb.buffer+cb.length, cb.maxLength-cb.length );
959  cb.length += n;
960
961  if ( n <= 0 )
962  {
963    PRINTF(1)("Cannot write to clientBuffer! Not enough space for 1 int\n");
964    return false;
965  }
966
967  return true;
968}
[7567]969#endif
[6341]970
971
Note: See TracBrowser for help on using the repository browser.