Changeset 11662 for code/branches/ScriptableController_HS17/src
- Timestamp:
- Dec 11, 2017, 4:35:38 PM (7 years ago)
- Location:
- code/branches/ScriptableController_HS17/src/orxonox
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/ScriptableController_HS17/src/orxonox/infos/GametypeInfo.cc
r11638 r11662 318 318 { 319 319 this->getLevel()->getScriptableController()->setPlayer(player); 320 // TODO Fix for relative paths 321 this->getLevel()->getScriptableController()->runScript(this->getLevel()->getFilename() + "/" + this->getLevel()->getScript()); 320 321 std::string script = this->getLevel()->getScript(); 322 if(script.at(0) != '/') 323 script = "../levels/" + script; // Not very dynamic 324 this->getLevel()->getScriptableController()->runScript(script); 322 325 } 323 326 } -
code/branches/ScriptableController_HS17/src/orxonox/scriptablecontroller/scriptable_controller.cc
r11638 r11662 35 35 if((ret = luaL_loadfile(lua, file_path.c_str())) != 0) 36 36 { 37 orxout(user_error) << "Failed to load level script " + file_path << std::endl; 37 38 this->printLuaError(lua); 38 39 delete api; … … 43 44 if((ret = lua_pcall(lua, 0, LUA_MULTRET, 0)) != 0) 44 45 { 46 orxout(user_error) << "Level script returned an error" << std::endl; 45 47 this->printLuaError(lua); 46 48 delete api; … … 86 88 87 89 for(auto &api : this->apis_) 88 api->pawnKilled(pawn_id_iter->second );90 api->pawnKilled(pawn_id_iter->second, pawn); 89 91 90 92 this->pawns_.erase(pawn_id_iter->second); … … 110 112 void ScriptableController::killPawn(std::string id) 111 113 { 112 auto pawn = this-> pawns_.find(id);113 if(pawn == this->pawns_.end())114 auto pawn = this->getPawnByID(id); 115 if(pawn == nullptr) 114 116 { 115 117 orxout(user_warning) << "Tried to destroy unknown pawn " << id << std::endl; … … 117 119 } 118 120 119 pawn-> second->kill();121 pawn->kill(); 120 122 } 121 123 … … 141 143 Pawn *ScriptableController::getPawnByID(std::string id) const 142 144 { 145 if(id == "player" || id == "Player" || id == "PLAYER") 146 return orxonox_cast<Pawn*>(this->player_->getControllableEntity()); 147 143 148 auto pawn = this->pawns_.find(id); 144 149 return pawn != this->pawns_.end() ? pawn->second : nullptr; … … 151 156 // Yes, this is 'const char*' and not 'std::string' because that's what lua gives us. 152 157 const char* message = lua_tostring(lua, -1); 153 std::cout<< message << std::endl;158 orxout(user_error) << message << std::endl; 154 159 lua_pop(lua, 1); 155 160 } -
code/branches/ScriptableController_HS17/src/orxonox/scriptablecontroller/scriptable_controller_api.cc
r11638 r11662 4 4 #include "scriptable_controller.h" 5 5 #include "tools/Timer.h" 6 #include "worldentities/pawns/Pawn.h" 7 #include "infos/Bot.h" 8 #include "worldentities/pawns/ModularSpaceShip.h" 6 9 7 10 namespace orxonox … … 32 35 33 36 LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::killPawn)>::registerFunction<&ScriptableControllerAPI::killPawn>(this, lua, "killPawn"); 37 LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::spawn)>::registerFunction<&ScriptableControllerAPI::spawn>(this, lua, "spawn"); 34 38 35 39 this->periodicTimer.setTimer(ScriptableControllerAPI::periodic_interval, true, createExecutor(createFunctor(&ScriptableControllerAPI::periodic, this)), false); … … 103 107 } 104 108 109 void ScriptableControllerAPI::spawn(std::string type, std::string id) 110 { 111 if(this->controller_->getWorldEntityByID(id) != nullptr) 112 { 113 orxout(user_warning) << "Script tried to spawn an object, but an object with the given ID exists already" << std::endl; 114 return; 115 } 116 117 Identifier *identifier = ClassByString(type); 118 if(!identifier) 119 { 120 orxout(user_error) << "Script tried to spawn unknown object" << std::endl; 121 return; 122 } 123 124 if(!identifier->isLoadable()) 125 { 126 orxout(user_error) << "Script tried to spawn unloadable object" << std::endl; 127 return; 128 } 129 130 WorldEntity *entity; 131 Identifiable *obj = identifier->fabricate(this->controller_->getWorldEntityByID("Player")->getContext()); 132 133 if(obj->isA(ClassIdentifier<WorldEntity>::getIdentifier())) 134 { 135 entity = orxonox_cast<WorldEntity*>(obj); 136 } 137 else if(obj->isA(ClassIdentifier<PlayerInfo>::getIdentifier())) 138 { 139 // TODO This does not work yet because somehow the controllable entity is not set 140 // yet at this stage. 141 // entity = orxonox_cast<PlayerInfo*>(obj)->getControllableEntity(); 142 return; 143 } 144 else 145 { 146 orxout(user_warning) << "Script tried to spawn an object that is neither a WorldEntity, nor a PlayerInfo" << std::endl; 147 return; 148 } 149 150 if(entity->isA(ClassIdentifier<MobileEntity>::getIdentifier())) 151 this->controller_->registerMobileEntity(id, orxonox_cast<MobileEntity*>(entity)); 152 153 if(entity->isA(ClassIdentifier<Pawn>::getIdentifier())) 154 this->controller_->registerPawn(id, orxonox_cast<Pawn*>(entity)); 155 156 this->controller_->registerWorldEntity(id, orxonox_cast<WorldEntity*>(entity)); 157 } 158 105 159 void ScriptableControllerAPI::setPosition(std::string id, double x, double y, double z) 106 160 { … … 179 233 } 180 234 181 void ScriptableControllerAPI::pawnKilled(std::string id )235 void ScriptableControllerAPI::pawnKilled(std::string id, Pawn *pawn) 182 236 { 183 237 for(auto callback : this->pawnDestroyedHandlers_[id]) … … 185 239 186 240 this->pawnDestroyedHandlers_.erase(id); 241 242 // We need to delete those handlers as well, they're no longer valid 243 auto near_obj_handler = this->nearObjectHandlers_.begin(); 244 while(near_obj_handler != this->nearObjectHandlers_.end()) 245 { 246 if(near_obj_handler->entity1_ == pawn || near_obj_handler->entity2_ == pawn) 247 near_obj_handler = this->nearObjectHandlers_.erase(near_obj_handler); 248 else 249 near_obj_handler++; 250 } 187 251 } 188 252 … … 257 321 258 322 // Pawns to kill 323 // TODO Possible race condidtion when the player destroys the pawn 324 // between the callback and the next periodic call. 259 325 for(auto &pawn : this->pawnsToKill_) 260 326 this->controller_->killPawn(pawn); -
code/branches/ScriptableController_HS17/src/orxonox/scriptablecontroller/scriptable_controller_api.h
r11638 r11662 14 14 class ScriptableController; 15 15 class WorldEntity; 16 class Pawn; 16 17 17 18 /** … … 24 25 class ScriptableControllerAPI 25 26 { 27 friend class ScriptableController; 28 26 29 public: 27 30 /** … … 140 143 141 144 /** 145 * @brief Spawn an object 146 * @param type Name of the class of the object you want to spawn 147 * @param id The newly created ID that can be used to access this object 148 */ 149 void spawn(std::string type, std::string id); 150 151 /** 142 152 * @brief Set the position of an object 143 153 * @param id The ID of the object … … 185 195 // ### API END ################################################################ 186 196 197 private: 187 198 /** 188 199 * @brief Called by ScriptableController when a pawn is killed … … 191 202 * Calls the lua callbacks associated with this event. 192 203 */ 193 void pawnKilled(std::string id );204 void pawnKilled(std::string id, Pawn *pawn); 194 205 195 206 /** … … 202 213 void pawnHit(std::string target_id, std::string source_id, double new_health, double new_shield); 203 214 204 private:205 215 /** 206 216 * @brief Groups everything together that is needed to handle a near-object event
Note: See TracChangeset
for help on using the changeset viewer.