Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 10304 was 10114, checked in by patrick, 18 years ago

merged network back to trunk

File size: 5.9 KB
Line 
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"
18
19#include "util/loading/load_param.h"
20#include "util/loading/factory.h"
21
22#include "world_entity.h"
23
24#include "compiler.h"
25
26#include "state.h"
27#include "game_rules.h"
28
29#include "shared_network_data.h"
30
31
32/// TODO REMOVE converter.h
33#include "converter.h"
34
35
36ObjectListDefinition(SpawningPoint);
37CREATE_FACTORY( SpawningPoint);
38/**
39 *  constructor
40 */
41SpawningPoint::SpawningPoint( const TiXmlElement * root )
42{
43  this->setAbsCoor( 0, 0, 0 );
44
45  this->init();
46
47  if (root != NULL)
48    this->loadParams(root);
49}
50
51void SpawningPoint::init()
52{
53  this->registerObject(this, SpawningPoint::_objectList);
54  PRINTF(0)("Created SpawningPoint\n");
55
56  this->teamId = -1;
57  this->localTimer = 0.0f;
58
59  this->toList( OM_DEAD_TICK );
60
61  MessageManager::getInstance()->registerMessageHandler( MSGID_RESPAWN, respawnMessageHandler, NULL );
62
63  this->setSynchronized( true );
64}
65
66
67/**
68 *  deconstructor
69 */
70SpawningPoint::~SpawningPoint ()
71{}
72
73
74/**
75 * loads the WorldEntity Specific Parameters.
76 * @param root: the XML-Element to load the Data From
77 */
78void SpawningPoint::loadParams(const TiXmlElement* root)
79{
80  /* let the world entity its stuff first */
81  WorldEntity::loadParams(root);
82
83  /* load teamId */
84  LoadParam(root, "teamId", this, SpawningPoint, setTeamId)
85  .describe("sets teamId");
86}
87
88
89
90/**
91 * pushes a world entity to the spawning queue
92 *  @param entity WorldEntity to be added
93 */
94void SpawningPoint::pushEntity(Playable* entity, float delay)
95{
96  QueueEntry qe;
97  qe.entity = entity;
98  qe.respawnTime = this->localTimer + delay;
99
100  queue.push_back( qe );
101}
102
103
104/**
105 *  spawn the entity
106 */
107void SpawningPoint::spawn(Playable* entity)
108{
109  bool found = false;
110
111  for (ObjectList<Playable>::const_iterator it = Playable::objectList().begin();
112       it != Playable::objectList().end();
113       ++it)
114  {
115    if ( *it == entity )
116    {
117      found = true;
118      break;
119    }
120  }
121
122  if ( !found )
123    return;
124
125  PRINTF(0)("Spawningpoint spawns Entity (%s)\n", entity->getClassCName());
126
127
128  entity->setAbsCoor( this->getAbsCoor() );
129  entity->setAbsDir( this->getAbsDir() );
130
131  //TODO set camera (not smooth)
132
133  if ( State::getGameRules() )
134  {
135    (State::getGameRules())->registerSpawn( entity );
136  }
137
138  entity->respawn();
139}
140
141
142/**
143 *  this method is called every frame
144 * @param time: the time in seconds that has passed since the last tick
145 *
146 * Handle all stuff that should update with time inside this method (movement, animation, etc.)
147 */
148void SpawningPoint::tick(float dt)
149{
150  this->localTimer += dt;
151  std::list<QueueEntry>::iterator it = this->queue.begin();
152  for( ; it != this->queue.end(); )
153  {
154    //PRINTF(0)("%f <= %f\n", it->respawnTime, this->localTimer);
155    if( it->respawnTime <= this->localTimer)
156    {
157      //spawn the player
158      this->spawn(it->entity);
159
160      bool found = false;
161
162      for (ObjectList<Playable>::const_iterator it2 = Playable::objectList().begin();
163           it2 != Playable::objectList().end();
164           ++it2)
165      {
166        if ( *it2 == it->entity )
167        {
168          found = true;
169          break;
170        }
171      }
172
173      if ( found && SharedNetworkData::getInstance()->isMasterServer() /*|| SharedNetworkData::getInstance()->isProxyServerActive()*/)
174        this->sendRespawnMessage( it->entity->getUniqueID() );
175
176      std::list<QueueEntry>::iterator delit = it;
177      it++;
178
179      queue.erase( delit );
180
181      continue;
182    }
183
184    it++;
185  }
186
187}
188
189
190/**
191 *  the entity is drawn onto the screen with this function
192 *
193 * This is a central function of an entity: call it to let the entity painted to the screen.
194 * Just override this function with whatever you want to be drawn.
195 */
196void SpawningPoint::draw() const
197{}
198
199void SpawningPoint::sendRespawnMessage( int uniqueId )
200{
201  byte buf[2*INTSIZE];
202
203  assert( Converter::intToByteArray( this->getUniqueID(), buf, INTSIZE ) == INTSIZE );
204  assert( Converter::intToByteArray( uniqueId, buf + INTSIZE, INTSIZE ) == INTSIZE );
205
206  MessageManager::getInstance()->sendMessage( MSGID_RESPAWN, buf, 2*INTSIZE, RT_ALL_BUT_ME, NET_UNASSIGNED, MP_HIGHBANDWIDTH );
207}
208
209/**
210 * message handler for respawn message
211 */
212bool SpawningPoint::respawnMessageHandler( MessageType messageType, byte * data, int dataLength, void * someData, int senderId, int destinationId  )
213{
214  if ( SharedNetworkData::getInstance()->isMasterServer() /*|| SharedNetworkData::getInstance()->isProxyServerActive()*/)
215  {
216    PRINTF(2)("server received spawn message!\n");
217    return true;
218  }
219
220  int spUniqueId;
221  int uniqueId;
222
223  if ( dataLength != 2*INTSIZE )
224  {
225    PRINTF(2)("spawn message has wrong size: %d\n", dataLength );
226    return true;
227  }
228
229  assert( Converter::byteArrayToInt( data, &spUniqueId ) == INTSIZE );
230  assert( Converter::byteArrayToInt( data+INTSIZE, &uniqueId ) == INTSIZE );
231
232  PRINTF(0)("SPAWNMESSAGE %d\n", uniqueId);
233
234  SpawningPoint * sp = NULL;
235  Playable      * playable = NULL;
236
237  for (ObjectList<SpawningPoint>::const_iterator it = SpawningPoint::objectList().begin();
238       it != SpawningPoint::objectList().end();
239       ++it)
240  {
241    PRINTF(0)("%d:%d\n", (*it)->getUniqueID(), spUniqueId);
242    if ( (*it)->getUniqueID() == spUniqueId )
243    {
244      sp = (*it);
245      break;
246    }
247  }
248
249  if ( !sp )
250  {
251    PRINTF(0)("could not find spawning point\n");
252    return false;
253  }
254
255  for (ObjectList<Playable>::const_iterator it = Playable::objectList().begin();
256       it != Playable::objectList().end();
257       ++it)
258  {
259    if ( (*it)->getUniqueID() == uniqueId )
260    {
261      playable = (*it);
262      break;
263    }
264  }
265
266  if ( !playable )
267  {
268    PRINTF(0)("could not find playable\n");
269    return false;
270  }
271
272  sp->spawn( playable );
273
274  return true;
275}
276
Note: See TracBrowser for help on using the repository browser.