Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/world_entities/spawning_point.cc @ 9571

Last change on this file since 9571 was 9494, checked in by bensch, 18 years ago

merged the proxy back

File size: 6.3 KB
RevLine 
[6080]1
2/*
3   orxonox - the future of 3D-vertical-scrollers
4
5   Copyright (C) 2004 orx
6
7   This program is free software; you can redistribute it and/or modify
8   it under the terms of the GNU General Public License as published by
9   the Free Software Foundation; either version 2, or (at your option)
10   any later version.
11
12### File Specific:
13   main-programmer: Patrick Boenzli
14   co-programmer:
15*/
16
17#include "spawning_point.h"
[6424]18
[7193]19#include "util/loading/load_param.h"
20#include "util/loading/factory.h"
[6424]21
[8068]22#include "world_entity.h"
23
[9008]24#include "class_list.h"
25
[6093]26#include "compiler.h"
[6080]27
[8802]28#include "state.h"
29#include "game_rules.h"
[6424]30
[9008]31#include "shared_network_data.h"
[8068]32
[9406]33
34/// TODO REMOVE converter.h
35#include "converter.h"
36
[9008]37CREATE_FACTORY( SpawningPoint, CL_SPAWNING_POINT );
38
[6080]39/**
[7357]40 *  constructor
[6080]41 */
[9008]42SpawningPoint::SpawningPoint( const TiXmlElement * root )
[6084]43{
[9008]44  this->setAbsCoor( 0, 0, 0 );
[6088]45
46  this->init();
[9406]47
[9008]48  if (root != NULL)
49    this->loadParams(root);
[6084]50}
[6080]51
[6088]52void SpawningPoint::init()
53{
[7357]54  this->setClassID(CL_SPAWNING_POINT, "SpawningPoint");
[9008]55  PRINTF(0)("Created SpawningPoint\n");
[9406]56
[8802]57  this->teamId = -1;
[9008]58  this->localTimer = 0.0f;
[9406]59
[9008]60  this->toList( OM_DEAD_TICK );
[9406]61
[9008]62  MessageManager::getInstance()->registerMessageHandler( MSGID_RESPAWN, respawnMessageHandler, NULL );
[9406]63
[9235]64  this->setSynchronized( true );
[6081]65}
66
67
68/**
[6080]69 *  deconstructor
70 */
71SpawningPoint::~SpawningPoint ()
72{}
73
74
[6081]75/**
[6086]76 * loads the WorldEntity Specific Parameters.
77 * @param root: the XML-Element to load the Data From
78 */
79void SpawningPoint::loadParams(const TiXmlElement* root)
80{
[6087]81  /* let the world entity its stuff first */
[6512]82  WorldEntity::loadParams(root);
[6086]83
[8802]84  /* load teamId */
85  LoadParam(root, "teamId", this, SpawningPoint, setTeamId)
86      .describe("sets teamId");
[6086]87}
88
89
[8068]90
[6086]91/**
[8068]92 * pushes a world entity to the spawning queue
93 *  @param entity WorldEntity to be added
94 */
[9008]95void SpawningPoint::pushEntity(Playable* entity, float delay)
[8068]96{
[8802]97  QueueEntry qe;
98  qe.entity = entity;
99  qe.respawnTime = this->localTimer + delay;
[9406]100
[8802]101  queue.push_back( qe );
[8068]102}
103
104
105/**
[6081]106 *  spawn the entity
107 */
[9008]108void SpawningPoint::spawn(Playable* entity)
[6083]109{
[9235]110  const std::list<BaseObject*> * list = ClassList::getList( CL_PLAYABLE );
[9406]111
[9235]112  bool found = false;
[9406]113
[9235]114  if ( !list )
115    return;
[9406]116
[9235]117  for ( std::list<BaseObject*>::const_iterator it = list->begin(); it != list->end(); it++ )
118  {
119    if ( *it == entity )
120    {
121      found = true;
122      break;
123    }
124  }
[9406]125
[9235]126  if ( !found )
127    return;
128
[9406]129  PRINTF(0)("Spawningpoint spawns Entity (%s)\n", entity->getClassCName());
[6424]130
[8068]131
[8802]132  entity->setAbsCoor( this->getAbsCoor() );
133  entity->setAbsDir( this->getAbsDir() );
[9406]134
[8802]135  //TODO set camera (not smooth)
[9406]136
[9008]137  if ( State::getGameRules() )
138  {
139    (State::getGameRules())->registerSpawn( entity );
140  }
[9406]141
[9008]142  entity->respawn();
[6083]143}
[6080]144
145
146/**
147 *  this method is called every frame
148 * @param time: the time in seconds that has passed since the last tick
149 *
150 * Handle all stuff that should update with time inside this method (movement, animation, etc.)
151 */
152void SpawningPoint::tick(float dt)
[6084]153{
[7357]154  this->localTimer += dt;
[8802]155  std::list<QueueEntry>::iterator it = this->queue.begin();
156  for( ; it != this->queue.end(); )
[6084]157  {
[9008]158    //PRINTF(0)("%f <= %f\n", it->respawnTime, this->localTimer);
[8802]159    if( it->respawnTime <= this->localTimer)
[8068]160    {
161      //spawn the player
[8802]162      this->spawn(it->entity);
[9406]163
[9235]164      const std::list<BaseObject*> * list = ClassList::getList( CL_PLAYABLE );
[9406]165
[9235]166      bool found = false;
[9406]167
[9235]168      if ( !list )
169        return;
[9406]170
[9235]171      for ( std::list<BaseObject*>::const_iterator it2 = list->begin(); it2 != list->end(); it2++ )
172      {
173        if ( *it2 == it->entity )
174        {
175          found = true;
176          break;
177        }
178      }
[9406]179
[9494]180      if ( found && SharedNetworkData::getInstance()->isMasterServer() /*|| SharedNetworkData::getInstance()->isProxyServerActive()*/)
[9008]181        this->sendRespawnMessage( it->entity->getUniqueID() );
182
[8802]183      std::list<QueueEntry>::iterator delit = it;
184      it++;
[9406]185
[8802]186      queue.erase( delit );
[9406]187
[8802]188      continue;
[8068]189    }
[9406]190
[8802]191    it++;
[6084]192  }
[8068]193
[6084]194}
[6080]195
196
197/**
198 *  the entity is drawn onto the screen with this function
199 *
200 * This is a central function of an entity: call it to let the entity painted to the screen.
201 * Just override this function with whatever you want to be drawn.
202 */
[9494]203void SpawningPoint::draw() const
[9008]204{
205}
206
207void SpawningPoint::sendRespawnMessage( int uniqueId )
208{
[9406]209#warning this byte array is not being deleted according to valginrd
[9008]210  byte * buf = new byte[2*INTSIZE];
[9406]211
[9008]212  assert( Converter::intToByteArray( this->getUniqueID(), buf, INTSIZE ) == INTSIZE );
213  assert( Converter::intToByteArray( uniqueId, buf + INTSIZE, INTSIZE ) == INTSIZE );
[9406]214
[9494]215  MessageManager::getInstance()->sendMessage( MSGID_RESPAWN, buf, 2*INTSIZE, RT_ALL_BUT_ME, 0, MP_HIGHBANDWIDTH );
[9008]216}
217
218bool SpawningPoint::respawnMessageHandler( MessageId messageId, byte * data, int dataLength, void * someData, int userId )
219{
[9494]220  if ( SharedNetworkData::getInstance()->isMasterServer() /*|| SharedNetworkData::getInstance()->isProxyServerActive()*/)
[9008]221  {
222    PRINTF(2)("server received spawn message!\n");
223    return true;
224  }
[9406]225
[9008]226  int spUniqueId;
227  int uniqueId;
[9406]228
[9008]229  if ( dataLength != 2*INTSIZE )
230  {
231    PRINTF(2)("spawn message has wrong size: %d\n", dataLength );
232    return true;
233  }
[9406]234
[9008]235  assert( Converter::byteArrayToInt( data, &spUniqueId ) == INTSIZE );
236  assert( Converter::byteArrayToInt( data+INTSIZE, &uniqueId ) == INTSIZE );
[9406]237
[9235]238  PRINTF(0)("SPAWNMESSAGE %d\n", uniqueId);
[9406]239
[9008]240  SpawningPoint * sp = NULL;
241  Playable      * playable = NULL;
[9406]242
[9008]243  const std::list<BaseObject*> * list = ClassList::getList( CL_SPAWNING_POINT );
[9406]244
[9008]245  if ( list )
246  {
247    for ( std::list<BaseObject*>::const_iterator it = list->begin(); it != list->end(); it++ )
248    {
[9235]249      PRINTF(0)("%d:%d\n", dynamic_cast<SpawningPoint*>(*it)->getUniqueID(), spUniqueId);
250      if ( dynamic_cast<SpawningPoint*>(*it)->getUniqueID() == spUniqueId )
[9008]251      {
252        sp = dynamic_cast<SpawningPoint*>(*it);
253        break;
254      }
255    }
256  }
[9406]257
[9008]258  if ( !sp )
259  {
[9235]260    PRINTF(0)("could not find spawning point\n");
[9008]261    return false;
262  }
[9406]263
[9008]264  list = ClassList::getList( CL_PLAYABLE );
[9406]265
[9008]266  if ( list )
267  {
268    for ( std::list<BaseObject*>::const_iterator it = list->begin(); it != list->end(); it++ )
269    {
270      if ( dynamic_cast<Playable*>(*it)->getUniqueID() == uniqueId )
271      {
272        playable = dynamic_cast<Playable*>(*it);
273        break;
274      }
275    }
276  }
[9406]277
[9008]278  if ( !playable )
279  {
[9235]280    PRINTF(0)("could not find playable\n");
[9008]281    return false;
282  }
[9406]283
[9008]284  sp->spawn( playable );
[9406]285
[9008]286  return true;
287}
288
Note: See TracBrowser for help on using the repository browser.