Changeset 11583 for code/branches/ScriptableController_HS17/src
- Timestamp:
- Nov 20, 2017, 4:48:03 PM (7 years ago)
- Location:
- code/branches/ScriptableController_HS17/src
- Files:
-
- 14 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/ScriptableController_HS17/src/libraries/core/BaseObject.h
r11518 r11583 193 193 static void loadAllEventStates(Element& xmlelement, XMLPort::Mode mode, BaseObject* object, Identifier* identifier); 194 194 195 virtual void registerToScriptableController(ScriptableController *controller) {}196 195 197 196 protected: -
code/branches/ScriptableController_HS17/src/orxonox/Level.cc
r11562 r11583 172 172 { 173 173 this->objects_.push_back(object); 174 object->registerToScriptableController(this->controller_.get());175 174 } 176 175 … … 208 207 orxout(internal_info) << "player entered level (id: " << player->getClientID() << ", name: " << player->getName() << ')' << endl; 209 208 player->switchGametype(this->getGametype()); 210 211 if(player->isHumanPlayer() && player->isLocalPlayer())212 {213 this->getScriptableController()->setPlayer(player);214 this->controller_->runScript(this->level_script_);215 }216 209 } 217 210 -
code/branches/ScriptableController_HS17/src/orxonox/Level.h
r11562 r11583 60 60 { return this->controller_.get(); } 61 61 62 inline const std::string &getScript(void) 63 { return this->level_script_; } 64 62 65 63 66 private: -
code/branches/ScriptableController_HS17/src/orxonox/gamestates/GSLevel.cc
r11355 r11583 100 100 if (GameMode::isMaster()) 101 101 { 102 orxout(user_info) << "Load level" << std::endl; 102 103 this->loadLevel(); 103 104 } … … 113 114 ModifyConsoleCommand(__CC_startMainMenu_name).activate(); 114 115 } 116 orxout(user_info) << "All loaded" << std::endl; 115 117 116 118 if (GameMode::isStandalone()) -
code/branches/ScriptableController_HS17/src/orxonox/infos/GametypeInfo.cc
r11099 r11583 43 43 #include "interfaces/GametypeMessageListener.h" 44 44 #include "interfaces/NotificationListener.h" 45 #include "scriptablecontroller/scriptable_controller.h" 46 #include "Level.h" 45 47 46 48 #include "PlayerInfo.h" … … 310 312 { 311 313 if(this->hasStarted() && !this->hasEnded()) 312 313 314 this->setSpawnedHelper(player, true); 315 } 316 317 if(player->isHumanPlayer() && player->isLocalPlayer()) 318 { 319 this->getLevel()->getScriptableController()->setPlayer(player); 320 this-getLevel()->getScriptableController()->runScript(this->getLevel()->getScript()); 314 321 } 315 322 } -
code/branches/ScriptableController_HS17/src/orxonox/scriptablecontroller/scriptable_controller.cc
r11562 r11583 3 3 #include "luatb.h" 4 4 #include "infos/PlayerInfo.h" 5 #include "core/command/Executor.h" 5 6 6 7 namespace orxonox … … 11 12 { 12 13 int ret; 14 15 // Not having a script specified at all is not an error 16 if(file_path.empty()) 17 return 0; 13 18 14 19 // Create a lua object … … 45 50 this->apis_.push_back(std::unique_ptr<ScriptableControllerAPI>(api)); 46 51 47 // Only the caller knows, when the end-of-life of the script is reached,48 // so we give him control over when to destroy it.49 52 return 0; 50 53 } … … 55 58 } 56 59 57 void ScriptableController::registerWorldEntity( int id, WorldEntity *obj)60 void ScriptableController::registerWorldEntity(std::string id, WorldEntity *entity) 58 61 { 59 orxout(user_info) << "WorldEntity registered (id: " << id << ")" << std::endl; 60 this->worldEntities_[id] = obj; 62 this->worldEntities_[id] = entity; 61 63 } 62 64 63 void ScriptableController::registerControllableEntity( int id, ControllableEntity *obj)65 void ScriptableController::registerControllableEntity(std::string id, ControllableEntity *entity) 64 66 { 65 orxout(user_info) << "ControllableEntity registered (id: " << id << ")" << std::endl; 66 this->worldEntities_[id] = obj; 67 this->worldEntities_[id] = entity; 67 68 } 68 69 69 WorldEntity *ScriptableController::getWorldEntityByID( intid) const70 WorldEntity *ScriptableController::getWorldEntityByID(std::string id) const 70 71 { 71 if(id == 0)72 if(id == "player" || id == "Player" || id == "PLAYER") 72 73 return this->player_->getControllableEntity(); 73 74 74 auto obj= this->worldEntities_.find(id);75 return obj != this->worldEntities_.end() ? obj->second : nullptr;75 auto entity = this->worldEntities_.find(id); 76 return entity != this->worldEntities_.end() ? entity->second : nullptr; 76 77 } 77 78 78 ControllableEntity *ScriptableController::getControllableEntityByID( intid) const79 ControllableEntity *ScriptableController::getControllableEntityByID(std::string id) const 79 80 { 80 if(id == 0)81 if(id == "player" || id == "Player" || id == "PLAYER") 81 82 return this->player_->getControllableEntity(); 82 83 83 auto obj = this->controllabelEntities_.find(id); 84 return obj != this->controllabelEntities_.end() ? obj->second : nullptr; 85 } 86 87 void ScriptableController::addNearObjectHandler(int obj1, int obj2, double distance, std::function<void (int,int)> callback) 88 { 89 orxout(user_info) << "NearObject registering (id 1: " << obj1 << ", id 2: " << obj2 << ")" << std::endl; 90 91 WorldEntity *entity1 = this->getWorldEntityByID(obj1); 92 WorldEntity *entity2 = this->getWorldEntityByID(obj2); 93 94 NearObjectHandler *handler1 = new NearObjectHandler(entity2, distance, callback); 95 NearObjectHandler *handler2 = new NearObjectHandler(entity1, distance, callback); 96 97 this->nearObjectHandlers_[entity1].push_front(std::unique_ptr<NearObjectHandler>(handler1)); 98 this->nearObjectHandlers_[entity2].push_front(std::unique_ptr<NearObjectHandler>(handler2)); 99 100 handler1->otherHandler_ = this->nearObjectHandlers_[entity2].begin(); 101 handler2->otherHandler_ = this->nearObjectHandlers_[entity1].begin(); 102 } 103 104 void ScriptableController::objectMoved(WorldEntity *obj) 105 { 106 // Temp 107 return; 108 109 auto &nearObjectHandlers = this->nearObjectHandlers_[obj]; 110 auto handler = nearObjectHandlers.begin(); 111 while(handler != nearObjectHandlers.end()) 112 { 113 WorldEntity *other = (*handler)->otherObject_; 114 if((obj->getPosition() - other->getPosition()).length() < (*handler)->distance_) 115 { 116 (*handler)->callback_(obj->getID(), other->getID()); 117 118 auto otherHandler = (*handler)->otherHandler_; 119 handler = nearObjectHandlers.erase(handler); 120 if(nearObjectHandlers.empty()) 121 this->nearObjectHandlers_.erase(obj); 122 123 this->nearObjectHandlers_[other].erase(otherHandler); 124 if(this->nearObjectHandlers_[other].empty()) 125 this->nearObjectHandlers_.erase(other); 126 } 127 else 128 { 129 handler++; 130 } 131 } 84 auto entity = this->controllabelEntities_.find(id); 85 return entity != this->controllabelEntities_.end() ? entity->second : nullptr; 132 86 } 133 87 -
code/branches/ScriptableController_HS17/src/orxonox/scriptablecontroller/scriptable_controller.h
r11562 r11583 11 11 #include "worldentities/WorldEntity.h" 12 12 #include "worldentities/ControllableEntity.h" 13 #include "tools/Timer.h" 13 14 14 15 struct lua_State; … … 20 21 { 21 22 public: 22 struct NearObjectHandler23 {24 NearObjectHandler(WorldEntity *otherObject, double distance, std::function<void (int, int)> callback)25 : otherObject_(otherObject), distance_(distance), callback_(callback)26 {}27 28 WorldEntity *otherObject_;29 double distance_;30 std::function<void (int, int)> callback_;31 std::list<std::unique_ptr<NearObjectHandler> >::iterator otherHandler_;32 };33 34 23 int runScript(const std::string &file_path); 35 24 36 25 void setPlayer(PlayerInfo *player); 37 void registerWorldEntity( int id, WorldEntity *obj);38 void registerControllableEntity( int id, ControllableEntity *obj);26 void registerWorldEntity(std::string id, WorldEntity *entity); 27 void registerControllableEntity(std::string id, ControllableEntity *entity); 39 28 40 WorldEntity *getWorldEntityByID(int id) const; 41 ControllableEntity *getControllableEntityByID(int id) const; 42 43 void addNearObjectHandler(int obj1, int obj2, double distance, std::function<void (int, int)> callback); 44 45 void objectMoved(WorldEntity *obj); 29 WorldEntity *getWorldEntityByID(std::string id) const; 30 ControllableEntity *getControllableEntityByID(std::string id) const; 46 31 47 32 private: 48 33 std::list<std::unique_ptr<ScriptableControllerAPI> > apis_; 49 34 PlayerInfo *player_; 50 std::map<int, WorldEntity*> worldEntities_; 51 std::map<int, ControllableEntity*> controllabelEntities_; 52 53 std::map<WorldEntity*, std::list<std::unique_ptr<NearObjectHandler> > > nearObjectHandlers_; 35 std::map<std::string, WorldEntity*> worldEntities_; 36 std::map<std::string, ControllableEntity*> controllabelEntities_; 54 37 55 38 void printLuaError(lua_State *lua); -
code/branches/ScriptableController_HS17/src/orxonox/scriptablecontroller/scriptable_controller_api.cc
r11562 r11583 17 17 LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::registerAfterTimeout)>::registerFunction<&ScriptableControllerAPI::registerAfterTimeout>(this, lua, "registerAfterTimeout"); 18 18 LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::registerAtNearObject)>::registerFunction<&ScriptableControllerAPI::registerAtNearObject>(this, lua, "registerAtNearObject"); 19 LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::registerAtNearPoint)>::registerFunction<&ScriptableControllerAPI::registerAtNearPoint>(this, lua, "registerAtNearPoint"); 19 20 LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::registerAtAreaEnter)>::registerFunction<&ScriptableControllerAPI::registerAtAreaEnter>(this, lua, "registerAtAreaEnter"); 20 21 LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::registerAtAreaLeave)>::registerFunction<&ScriptableControllerAPI::registerAtAreaLeave>(this, lua, "registerAtAreaLeave"); 21 LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::registerAtObjectDestroyed)>::registerFunction<&ScriptableControllerAPI::registerAtObjectDestroyed>(this, lua, "registerAtObjectDestroyed"); 22 LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::registerAtPickup)>::registerFunction<&ScriptableControllerAPI::registerAtPickup>(this, lua, "registerAtPickup"); 22 // LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::registerAtObjectDestroyed)>::registerFunction<&ScriptableControllerAPI::registerAtObjectDestroyed>(this, lua, "registerAtObjectDestroyed"); 23 // LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::registerAtPickup)>::registerFunction<&ScriptableControllerAPI::registerAtPickup>(this, lua, "registerAtPickup"); 24 25 // Checks for area enter, area leave and near object events 26 this->areaCheckTimer.setTimer(0.5, true, createExecutor(createFunctor(&ScriptableControllerAPI::checkAreas, this)), false); 23 27 } 24 28 … … 39 43 } 40 44 41 int ScriptableControllerAPI::registerAtNearObject(std::function<void (int, int)> callback, int obj1, int obj2, double distance)45 void ScriptableControllerAPI::registerAtNearObject(std::function<void (std::string, std::string)> callback, std::string id1, std::string id2, double distance) 42 46 { 43 //this->controller_->addNearObjectHandler(obj1, obj1, distance, callback); 47 WorldEntity *entity1 = this->controller_->getWorldEntityByID(id1); 48 WorldEntity *entity2 = this->controller_->getWorldEntityByID(id2); 49 50 if(entity1 != nullptr && entity2 != nullptr) 51 this->nearObjectHandlers_.push_front(NearObjectHandler(entity1, entity2, id1, id2, distance, callback)); 44 52 } 45 53 46 int ScriptableControllerAPI::registerAtAreaEnter(std::function<void (int)> callback, int obj, int x, int y, int z, int dx, int dy, int dz)54 void ScriptableControllerAPI::registerAtNearPoint(std::function<void (std::string)> callback, std::string id, double x, double y, double z, double distance) 47 55 { 56 WorldEntity *entity = this->controller_->getWorldEntityByID(id); 48 57 58 if(entity != nullptr) 59 this->nearPointHandlers_.push_front(NearPointHandler(entity, id, x, y, z, distance, callback)); 49 60 } 50 61 51 int ScriptableControllerAPI::registerAtAreaLeave(std::function<void (int)> callback, int obj, int x, int y, int z, int dx, int dy, int dz)62 void ScriptableControllerAPI::registerAtAreaEnter(std::function<void (std::string)> callback, std::string id, int x, int y, int z, int dx, int dy, int dz) 52 63 { 64 WorldEntity *entity = this->controller_->getWorldEntityByID(id); 53 65 66 if(entity != nullptr) 67 this->areaHandlers_.push_front(AreaHandler(entity, id, x, y, z, dx, dy, dz, true, callback)); 54 68 } 55 69 56 int ScriptableControllerAPI::registerAtObjectDestroyed(std::function<void (int)> callback, int obj)70 void ScriptableControllerAPI::registerAtAreaLeave(std::function<void (std::string)> callback, std::string id, int x, int y, int z, int dx, int dy, int dz) 57 71 { 72 WorldEntity *entity = this->controller_->getWorldEntityByID(id); 58 73 74 if(entity != nullptr) 75 this->areaHandlers_.push_front(AreaHandler(entity, id, x, y, z, dx, dy, dz, false, callback)); 59 76 } 60 77 61 int ScriptableControllerAPI::registerAtPickup(std::function<void (int)> callback, int pickup_id)78 void ScriptableControllerAPI::checkAreas() 62 79 { 80 // Near object 81 auto near_obj_handler = this->nearObjectHandlers_.begin(); 82 while(near_obj_handler != this->nearObjectHandlers_.end()) 83 { 84 if((near_obj_handler->entity1_->getPosition() - near_obj_handler->entity2_->getPosition()).length() < near_obj_handler->distance_) 85 { 86 near_obj_handler->callback_(near_obj_handler->id1_, near_obj_handler->id2_); 87 near_obj_handler = this->nearObjectHandlers_.erase(near_obj_handler); 88 } 89 else 90 { 91 near_obj_handler++; 92 } 93 } 63 94 95 // Near point 96 auto near_point_handler = this->nearPointHandlers_.begin(); 97 while(near_point_handler != this->nearPointHandlers_.end()) 98 { 99 if((near_point_handler->entity_->getPosition() - near_point_handler->point_).length() < near_point_handler->distance_) 100 { 101 near_point_handler->callback_(near_point_handler->id_); 102 near_point_handler = this->nearPointHandlers_.erase(near_point_handler); 103 } 104 else 105 { 106 near_point_handler++; 107 } 108 } 109 110 // Areas 111 auto area_handler = this->areaHandlers_.begin(); 112 while(area_handler != this->areaHandlers_.end()) 113 { 114 if(area_handler->entity_->getPosition() > area_handler->start_point_ && 115 area_handler->entity_->getPosition() < area_handler->end_point_) 116 { 117 if(area_handler->atEnter_) 118 { 119 area_handler->callback_(area_handler->id_); 120 area_handler = this->areaHandlers_.erase(area_handler); 121 } 122 else 123 { 124 area_handler++; 125 } 126 } 127 else 128 { 129 if(!area_handler->atEnter_) 130 { 131 area_handler->callback_(area_handler->id_); 132 area_handler = this->areaHandlers_.erase(area_handler); 133 } 134 else 135 { 136 area_handler++; 137 } 138 } 139 } 64 140 } 65 141 -
code/branches/ScriptableController_HS17/src/orxonox/scriptablecontroller/scriptable_controller_api.h
r11562 r11583 4 4 #include <functional> 5 5 #include "core/CoreIncludes.h" 6 7 // TODO Is pos int or double? 6 #include "tools/Timer.h" 7 #include "OgreVector3.h" 8 8 9 9 struct lua_State; … … 13 13 14 14 class ScriptableController; 15 class WorldEntity; 15 16 16 17 class ScriptableControllerAPI … … 24 25 void orxPrint(std::string msg); 25 26 void registerAfterTimeout(std::function<void (void)> callback, double timeout); 26 int registerAtNearObject(std::function<void(int, int)> callback, int obj1, int obj2, double distance);27 int registerAtAreaEnter(std::function<void (int)> callback, int obj, int x, int y, int z, int dx, int dy, int dz);28 int registerAtAreaLeave(std::function<void (int)> callback, int obj, int x, int y, int z, int dx, int dy, int dz);29 int registerAtObjectDestroyed(std::function<void (int)> callback, int obj);30 int registerAtPickup(std::function<void (int)> callback, int pickup_id);31 27 32 int destroyObject(int obj); 33 void removeObject(int obj); 34 int setObjectPosition(int obj, double x, double y, double z); 28 void registerAtNearObject(std::function<void(std::string, std::string)> callback, std::string id1, std::string id2, double distance); 29 void registerAtNearPoint(std::function<void (std::string)> callback, std::string id, double x, double y, double z, double distance); 30 void registerAtAreaEnter(std::function<void (std::string)> callback, std::string obj, int x, int y, int z, int dx, int dy, int dz); 31 void registerAtAreaLeave(std::function<void (std::string)> callback, std::string obj, int x, int y, int z, int dx, int dy, int dz); 32 33 void registerAtObjectDestroyed(std::function<void (std::string)> callback, std::string obj); 34 void registerAtPickup(std::function<void (int)> callback, int pickup_id); 35 36 void destroyObject(std::string obj); 37 void removeObject(std::string obj); 38 void setObjectPosition(std::string obj, double x, double y, double z); 35 39 36 40 private: 41 struct NearObjectHandler 42 { 43 NearObjectHandler(WorldEntity *entity1, WorldEntity *entity2, std::string id1, std::string id2, double distance, std::function<void (std::string, std::string)> callback) 44 : entity1_(entity1), entity2_(entity2), id1_(id1), id2_(id2), distance_(distance), callback_(callback) 45 {} 46 47 WorldEntity *entity1_, *entity2_; 48 std::string id1_, id2_; 49 double distance_; 50 std::function<void (std::string, std::string)> callback_; 51 }; 52 53 struct NearPointHandler 54 { 55 NearPointHandler(WorldEntity *entity, std::string id, double x, double y, double z, double distance, std::function<void (std::string)> callback) 56 : entity_(entity), id_(id), point_(x, y, z), distance_(distance), callback_(callback) 57 {} 58 59 WorldEntity *entity_; 60 std::string id_; 61 Vector3 point_; 62 double distance_; 63 std::function<void (std::string)> callback_; 64 }; 65 66 struct AreaHandler 67 { 68 AreaHandler(WorldEntity *entity, std::string id, double x, double y, double z, double dx, double dy, double dz, bool atEnter, std::function<void (std::string)> callback) 69 : entity_(entity), id_(id), start_point_(x, y, z), atEnter_(atEnter), callback_(callback) 70 { this-> end_point_ = this->start_point_ + Vector3(dx, dy, dz); } 71 72 WorldEntity *entity_; 73 std::string id_; 74 Vector3 start_point_, end_point_; 75 bool atEnter_; 76 std::function<void (std::string)> callback_; 77 }; 78 79 37 80 lua_State *lua_; 38 81 ScriptableController *controller_; 82 std::list<NearObjectHandler> nearObjectHandlers_; 83 std::list<NearPointHandler> nearPointHandlers_; 84 std::list<AreaHandler> areaHandlers_; 85 Timer areaCheckTimer; 86 87 void checkAreas(void); 39 88 }; 40 89 -
code/branches/ScriptableController_HS17/src/orxonox/worldentities/ControllableEntity.cc
r11518 r11583 39 39 40 40 #include "Scene.h" 41 #include "Level.h" 41 42 #include "infos/PlayerInfo.h" 42 43 #include "controllers/NewHumanController.h" … … 68 69 this->camera_ = nullptr; 69 70 this->xmlcontroller_ = nullptr; 70 //this->controller_ = nullptr;71 71 this->reverseCamera_ = nullptr; 72 72 this->bDestroyWhenPlayerLeft_ = false; … … 127 127 XMLPortObject(ControllableEntity, CameraPosition, "camerapositions", addCameraPosition, getCameraPosition, xmlelement, mode); 128 128 XMLPortObject(ControllableEntity, Controller, "controller", setXMLController, getXMLController, xmlelement, mode); 129 130 if(!this->id_.empty() && this->getLevel() != nullptr) 131 this->getLevel()->getScriptableController()->registerControllableEntity(this->id_, this); 129 132 } 130 133 … … 338 341 } 339 342 340 void ControllableEntity::registerToScriptableController(ScriptableController *controller)341 {342 controller->registerControllableEntity(this->id_, this);343 }344 345 343 void ControllableEntity::setPlayer(PlayerInfo* player) 346 344 { -
code/branches/ScriptableController_HS17/src/orxonox/worldentities/ControllableEntity.h
r11518 r11583 176 176 { return this->team_; } 177 177 178 virtual void registerToScriptableController(ScriptableController *controller) override;179 178 180 179 protected: -
code/branches/ScriptableController_HS17/src/orxonox/worldentities/MobileEntity.cc
r11562 r11583 115 115 116 116 this->node_->setPosition(position); 117 this->getLevel()->getScriptableController()->objectMoved(this);118 117 } 119 118 -
code/branches/ScriptableController_HS17/src/orxonox/worldentities/WorldEntity.cc
r11562 r11583 44 44 #include "core/XMLPort.h" 45 45 #include "Scene.h" 46 #include "Level.h" 46 47 #include "collisionshapes/WorldEntityCollisionShape.h" 47 48 #include "scriptablecontroller/scriptable_controller.h" … … 185 186 XMLPortObject(WorldEntity, CollisionShape, "collisionShapes", attachCollisionShape, getAttachedCollisionShape, xmlelement, mode); 186 187 187 orxout(user_info) << "ID loaded" << std::endl; 188 if(!this->id_.empty() && this->getLevel() != nullptr) 189 this->getLevel()->getScriptableController()->registerWorldEntity(this->id_, this); 188 190 } 189 191 … … 280 282 } 281 283 282 void WorldEntity::registerToScriptableController(ScriptableController *controller)283 {284 orxout(user_info) << "Registering object to SC (" << this->id_ << ")" << std::endl;285 controller->registerWorldEntity(this->id_, this);286 }287 288 284 /** 289 285 @brief -
code/branches/ScriptableController_HS17/src/orxonox/worldentities/WorldEntity.h
r11562 r11583 111 111 virtual void changedVisibility(void) override; 112 112 113 inline intgetID(void)113 inline std::string getID(void) 114 114 { return this->id_; } 115 115 116 inline void setID( intid)116 inline void setID(std::string id) 117 117 { this->id_ = id; } 118 119 virtual void registerToScriptableController(ScriptableController *controller) override;120 118 121 119 virtual void setPosition(const Vector3& position) = 0; … … 450 448 451 449 btRigidBody* physicalBody_; //!< Bullet rigid body. Everything physical is applied to this instance. 452 intid_; //!< Used by the ScriptableController to identify objects450 std::string id_; //!< Used by the ScriptableController to identify objects 453 451 454 452 private:
Note: See TracChangeset
for help on using the changeset viewer.