| [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 |  | 
|---|
| [9008] | 33 | CREATE_FACTORY( SpawningPoint, CL_SPAWNING_POINT ); | 
|---|
 | 34 |  | 
|---|
| [6080] | 35 | /** | 
|---|
| [7357] | 36 |  *  constructor | 
|---|
| [6080] | 37 |  */ | 
|---|
| [9008] | 38 | SpawningPoint::SpawningPoint( const TiXmlElement * root ) | 
|---|
| [6084] | 39 | { | 
|---|
| [9008] | 40 |   this->setAbsCoor( 0, 0, 0 ); | 
|---|
| [6088] | 41 |  | 
|---|
 | 42 |   this->init(); | 
|---|
| [9008] | 43 |    | 
|---|
 | 44 |   if (root != NULL) | 
|---|
 | 45 |     this->loadParams(root); | 
|---|
| [6084] | 46 | } | 
|---|
| [6080] | 47 |  | 
|---|
| [6088] | 48 | void SpawningPoint::init() | 
|---|
 | 49 | { | 
|---|
| [7357] | 50 |   this->setClassID(CL_SPAWNING_POINT, "SpawningPoint"); | 
|---|
| [9008] | 51 |   PRINTF(0)("Created SpawningPoint\n"); | 
|---|
| [8802] | 52 |    | 
|---|
 | 53 |   this->teamId = -1; | 
|---|
| [9008] | 54 |   this->localTimer = 0.0f; | 
|---|
 | 55 |    | 
|---|
 | 56 |   this->toList( OM_DEAD_TICK ); | 
|---|
 | 57 |    | 
|---|
 | 58 |   MessageManager::getInstance()->registerMessageHandler( MSGID_RESPAWN, respawnMessageHandler, NULL ); | 
|---|
| [6081] | 59 | } | 
|---|
 | 60 |  | 
|---|
 | 61 |  | 
|---|
 | 62 | /** | 
|---|
| [6080] | 63 |  *  deconstructor | 
|---|
 | 64 |  */ | 
|---|
 | 65 | SpawningPoint::~SpawningPoint () | 
|---|
 | 66 | {} | 
|---|
 | 67 |  | 
|---|
 | 68 |  | 
|---|
| [6081] | 69 | /** | 
|---|
| [6086] | 70 |  * loads the WorldEntity Specific Parameters. | 
|---|
 | 71 |  * @param root: the XML-Element to load the Data From | 
|---|
 | 72 |  */ | 
|---|
 | 73 | void SpawningPoint::loadParams(const TiXmlElement* root) | 
|---|
 | 74 | { | 
|---|
| [6087] | 75 |   /* let the world entity its stuff first */ | 
|---|
| [6512] | 76 |   WorldEntity::loadParams(root); | 
|---|
| [6086] | 77 |  | 
|---|
| [8802] | 78 |   /* load teamId */ | 
|---|
 | 79 |   LoadParam(root, "teamId", this, SpawningPoint, setTeamId) | 
|---|
 | 80 |       .describe("sets teamId"); | 
|---|
| [6086] | 81 | } | 
|---|
 | 82 |  | 
|---|
 | 83 |  | 
|---|
| [8068] | 84 |  | 
|---|
| [6086] | 85 | /** | 
|---|
| [8068] | 86 |  * pushes a world entity to the spawning queue | 
|---|
 | 87 |  *  @param entity WorldEntity to be added | 
|---|
 | 88 |  */ | 
|---|
| [9008] | 89 | void SpawningPoint::pushEntity(Playable* entity, float delay) | 
|---|
| [8068] | 90 | { | 
|---|
| [8802] | 91 |   QueueEntry qe; | 
|---|
 | 92 |   qe.entity = entity; | 
|---|
 | 93 |   qe.respawnTime = this->localTimer + delay; | 
|---|
 | 94 |    | 
|---|
 | 95 |   queue.push_back( qe ); | 
|---|
| [8068] | 96 | } | 
|---|
 | 97 |  | 
|---|
 | 98 |  | 
|---|
 | 99 | /** | 
|---|
| [6081] | 100 |  *  spawn the entity | 
|---|
 | 101 |  */ | 
|---|
| [9008] | 102 | void SpawningPoint::spawn(Playable* entity) | 
|---|
| [6083] | 103 | { | 
|---|
| [9008] | 104 |   PRINTF(0)("Spawningpoint spawns Entity (%s)\n", entity->getClassName()); | 
|---|
| [6424] | 105 |  | 
|---|
| [8068] | 106 |  | 
|---|
| [8802] | 107 |   entity->setAbsCoor( this->getAbsCoor() ); | 
|---|
 | 108 |   entity->setAbsDir( this->getAbsDir() ); | 
|---|
 | 109 |    | 
|---|
 | 110 |   //TODO set camera (not smooth) | 
|---|
| [9008] | 111 |    | 
|---|
 | 112 |   if ( State::getGameRules() ) | 
|---|
 | 113 |   { | 
|---|
 | 114 |     (State::getGameRules())->registerSpawn( entity ); | 
|---|
 | 115 |   } | 
|---|
 | 116 |    | 
|---|
 | 117 |   entity->respawn(); | 
|---|
| [6083] | 118 | } | 
|---|
| [6080] | 119 |  | 
|---|
 | 120 |  | 
|---|
 | 121 | /** | 
|---|
 | 122 |  *  this method is called every frame | 
|---|
 | 123 |  * @param time: the time in seconds that has passed since the last tick | 
|---|
 | 124 |  * | 
|---|
 | 125 |  * Handle all stuff that should update with time inside this method (movement, animation, etc.) | 
|---|
 | 126 |  */ | 
|---|
 | 127 | void SpawningPoint::tick(float dt) | 
|---|
| [6084] | 128 | { | 
|---|
| [7357] | 129 |   this->localTimer += dt; | 
|---|
| [8802] | 130 |   std::list<QueueEntry>::iterator it = this->queue.begin(); | 
|---|
 | 131 |   for( ; it != this->queue.end(); ) | 
|---|
| [6084] | 132 |   { | 
|---|
| [9008] | 133 |     //PRINTF(0)("%f <= %f\n", it->respawnTime, this->localTimer); | 
|---|
| [8802] | 134 |     if( it->respawnTime <= this->localTimer) | 
|---|
| [8068] | 135 |     { | 
|---|
 | 136 |       //spawn the player | 
|---|
| [8802] | 137 |       this->spawn(it->entity); | 
|---|
 | 138 |        | 
|---|
| [9008] | 139 |       if ( SharedNetworkData::getInstance()->isGameServer() ) | 
|---|
 | 140 |         this->sendRespawnMessage( it->entity->getUniqueID() ); | 
|---|
 | 141 |  | 
|---|
| [8802] | 142 |       std::list<QueueEntry>::iterator delit = it; | 
|---|
 | 143 |       it++; | 
|---|
 | 144 |        | 
|---|
 | 145 |       queue.erase( delit ); | 
|---|
 | 146 |        | 
|---|
 | 147 |       continue; | 
|---|
| [8068] | 148 |     } | 
|---|
| [8802] | 149 |      | 
|---|
 | 150 |     it++; | 
|---|
| [6084] | 151 |   } | 
|---|
| [8068] | 152 |  | 
|---|
| [6084] | 153 | } | 
|---|
| [6080] | 154 |  | 
|---|
 | 155 |  | 
|---|
 | 156 | /** | 
|---|
 | 157 |  *  the entity is drawn onto the screen with this function | 
|---|
 | 158 |  * | 
|---|
 | 159 |  * This is a central function of an entity: call it to let the entity painted to the screen. | 
|---|
 | 160 |  * Just override this function with whatever you want to be drawn. | 
|---|
 | 161 |  */ | 
|---|
 | 162 | void SpawningPoint::draw() | 
|---|
| [9008] | 163 | { | 
|---|
 | 164 | } | 
|---|
 | 165 |  | 
|---|
 | 166 | void SpawningPoint::sendRespawnMessage( int uniqueId ) | 
|---|
 | 167 | { | 
|---|
 | 168 |   byte * buf = new byte[2*INTSIZE]; | 
|---|
 | 169 |    | 
|---|
 | 170 |   assert( Converter::intToByteArray( this->getUniqueID(), buf, INTSIZE ) == INTSIZE ); | 
|---|
 | 171 |   assert( Converter::intToByteArray( uniqueId, buf + INTSIZE, INTSIZE ) == INTSIZE ); | 
|---|
 | 172 |    | 
|---|
 | 173 |   MessageManager::getInstance()->sendMessage( MSGID_RESPAWN, buf, 2*INTSIZE, RT_ALL_NOT_ME, 0, MP_HIGHBANDWIDTH ); | 
|---|
 | 174 | } | 
|---|
 | 175 |  | 
|---|
 | 176 | bool SpawningPoint::respawnMessageHandler( MessageId messageId, byte * data, int dataLength, void * someData, int userId ) | 
|---|
 | 177 | { | 
|---|
 | 178 |   if ( SharedNetworkData::getInstance()->isGameServer() ) | 
|---|
 | 179 |   { | 
|---|
 | 180 |     PRINTF(2)("server received spawn message!\n"); | 
|---|
 | 181 |     return true; | 
|---|
 | 182 |   } | 
|---|
 | 183 |      | 
|---|
 | 184 |   int spUniqueId; | 
|---|
 | 185 |   int uniqueId; | 
|---|
 | 186 |    | 
|---|
 | 187 |   if ( dataLength != 2*INTSIZE ) | 
|---|
 | 188 |   { | 
|---|
 | 189 |     PRINTF(2)("spawn message has wrong size: %d\n", dataLength ); | 
|---|
 | 190 |     return true; | 
|---|
 | 191 |   } | 
|---|
 | 192 |    | 
|---|
 | 193 |   assert( Converter::byteArrayToInt( data, &spUniqueId ) == INTSIZE ); | 
|---|
 | 194 |   assert( Converter::byteArrayToInt( data+INTSIZE, &uniqueId ) == INTSIZE ); | 
|---|
 | 195 |    | 
|---|
 | 196 |   SpawningPoint * sp = NULL; | 
|---|
 | 197 |   Playable      * playable = NULL; | 
|---|
 | 198 |    | 
|---|
 | 199 |   const std::list<BaseObject*> * list = ClassList::getList( CL_SPAWNING_POINT ); | 
|---|
 | 200 |    | 
|---|
 | 201 |   if ( list ) | 
|---|
 | 202 |   { | 
|---|
 | 203 |     for ( std::list<BaseObject*>::const_iterator it = list->begin(); it != list->end(); it++ ) | 
|---|
 | 204 |     { | 
|---|
 | 205 |       if ( dynamic_cast<SpawningPoint*>(*it)->getUniqueID() == uniqueId ) | 
|---|
 | 206 |       { | 
|---|
 | 207 |         sp = dynamic_cast<SpawningPoint*>(*it); | 
|---|
 | 208 |         break; | 
|---|
 | 209 |       } | 
|---|
 | 210 |     } | 
|---|
 | 211 |   } | 
|---|
 | 212 |    | 
|---|
 | 213 |   if ( !sp ) | 
|---|
 | 214 |   { | 
|---|
 | 215 |     PRINTF(2)("could not find spawning point\n"); | 
|---|
 | 216 |     return false; | 
|---|
 | 217 |   } | 
|---|
 | 218 |    | 
|---|
 | 219 |   list = ClassList::getList( CL_PLAYABLE ); | 
|---|
 | 220 |    | 
|---|
 | 221 |   if ( list ) | 
|---|
 | 222 |   { | 
|---|
 | 223 |     for ( std::list<BaseObject*>::const_iterator it = list->begin(); it != list->end(); it++ ) | 
|---|
 | 224 |     { | 
|---|
 | 225 |       if ( dynamic_cast<Playable*>(*it)->getUniqueID() == uniqueId ) | 
|---|
 | 226 |       { | 
|---|
 | 227 |         playable = dynamic_cast<Playable*>(*it); | 
|---|
 | 228 |         break; | 
|---|
 | 229 |       } | 
|---|
 | 230 |     } | 
|---|
 | 231 |   } | 
|---|
 | 232 |    | 
|---|
 | 233 |   if ( !playable ) | 
|---|
 | 234 |   { | 
|---|
 | 235 |     PRINTF(2)("could not find playable\n"); | 
|---|
 | 236 |     return false; | 
|---|
 | 237 |   } | 
|---|
 | 238 |    | 
|---|
 | 239 |   sp->spawn( playable ); | 
|---|
 | 240 |    | 
|---|
 | 241 |   return true; | 
|---|
 | 242 | } | 
|---|
 | 243 |  | 
|---|