#include "scriptable_controller_api.h" #include "luatb.h" #include "scriptable_controller.h" #include "tools/Timer.h" namespace orxonox { const double ScriptableControllerAPI::periodic_interval = 0.5; ScriptableControllerAPI::ScriptableControllerAPI(lua_State *lua, ScriptableController *controller) { this->lua_ = lua; this->controller_ = controller; // Haven't found a shorter way yet to write that... LuaTB::registerFunction<&ScriptableControllerAPI::orxPrint>(this, lua, "orxPrint"); LuaTB::registerFunction<&ScriptableControllerAPI::registerAfterTimeout>(this, lua, "registerAfterTimeout"); LuaTB::registerFunction<&ScriptableControllerAPI::registerAtNearObject>(this, lua, "registerAtNearObject"); LuaTB::registerFunction<&ScriptableControllerAPI::registerAtNearPoint>(this, lua, "registerAtNearPoint"); LuaTB::registerFunction<&ScriptableControllerAPI::registerAtAreaEnter>(this, lua, "registerAtAreaEnter"); LuaTB::registerFunction<&ScriptableControllerAPI::registerAtAreaLeave>(this, lua, "registerAtAreaLeave"); LuaTB::registerFunction<&ScriptableControllerAPI::registerAtPawnKilled>(this, lua, "registerAtPawnKilled"); LuaTB::registerFunction<&ScriptableControllerAPI::registerAtPawnHit>(this, lua, "registerAtPawnHit"); LuaTB::registerFunction<&ScriptableControllerAPI::killPawn>(this, lua, "killPawn"); this->periodicTimer.setTimer(ScriptableControllerAPI::periodic_interval, true, createExecutor(createFunctor(&ScriptableControllerAPI::periodic, this)), false); } ScriptableControllerAPI::~ScriptableControllerAPI() { lua_close(this->lua_); } void ScriptableControllerAPI::orxPrint(std::string msg) { orxout(user_info) << msg << std::endl; } void ScriptableControllerAPI::registerAfterTimeout(std::function callback, double timeout) { // Kills itself when the timer fires new Timer(timeout, false, callback, true); } void ScriptableControllerAPI::registerAtNearObject(std::function callback, std::string id1, std::string id2, double distance) { WorldEntity *entity1 = this->controller_->getWorldEntityByID(id1); WorldEntity *entity2 = this->controller_->getWorldEntityByID(id2); if(entity1 != nullptr && entity2 != nullptr) this->nearObjectHandlers_.push_front(NearObjectHandler(entity1, entity2, id1, id2, distance, callback)); } void ScriptableControllerAPI::registerAtNearPoint(std::function callback, std::string id, double x, double y, double z, double distance) { WorldEntity *entity = this->controller_->getWorldEntityByID(id); if(entity != nullptr) this->nearPointHandlers_.push_front(NearPointHandler(entity, id, x, y, z, distance, callback)); } void ScriptableControllerAPI::registerAtAreaEnter(std::function callback, std::string id, int x, int y, int z, int dx, int dy, int dz) { WorldEntity *entity = this->controller_->getWorldEntityByID(id); if(entity != nullptr) this->areaHandlers_.push_front(AreaHandler(entity, id, x, y, z, dx, dy, dz, true, callback)); } void ScriptableControllerAPI::registerAtAreaLeave(std::function callback, std::string id, int x, int y, int z, int dx, int dy, int dz) { WorldEntity *entity = this->controller_->getWorldEntityByID(id); if(entity != nullptr) this->areaHandlers_.push_front(AreaHandler(entity, id, x, y, z, dx, dy, dz, false, callback)); } void ScriptableControllerAPI::registerAtPawnKilled(std::function callback, std::string id) { this->pawnDestroyedHandlers_[id].push_back(callback); } void ScriptableControllerAPI::registerAtPawnHit(std::function callback, std::string id) { this->pawnHitHandlers_[id].push_back(callback); } void ScriptableControllerAPI::killPawn(std::string id) { // We don't kill the pawn here directly, because this function is called from LUA and thus // runs in a different thread. So we schedule the kill for later in the main thread // (in 'periodic'). this->pawnsToKill_.push_back(id); } void ScriptableControllerAPI::pawnKilled(std::string id) { for(auto callback : this->pawnDestroyedHandlers_[id]) callback(id); this->pawnDestroyedHandlers_.erase(id); } void ScriptableControllerAPI::pawnHit(std::string target_id, std::string source_id, double new_health, double new_shield) { for(auto callback : this->pawnHitHandlers_[target_id]) callback(target_id, source_id, new_health, new_shield); } void ScriptableControllerAPI::periodic() { // Near object auto near_obj_handler = this->nearObjectHandlers_.begin(); while(near_obj_handler != this->nearObjectHandlers_.end()) { if((near_obj_handler->entity1_->getPosition() - near_obj_handler->entity2_->getPosition()).length() < near_obj_handler->distance_) { near_obj_handler->callback_(near_obj_handler->id1_, near_obj_handler->id2_); near_obj_handler = this->nearObjectHandlers_.erase(near_obj_handler); } else { near_obj_handler++; } } // Near point auto near_point_handler = this->nearPointHandlers_.begin(); while(near_point_handler != this->nearPointHandlers_.end()) { if((near_point_handler->entity_->getPosition() - near_point_handler->point_).length() < near_point_handler->distance_) { near_point_handler->callback_(near_point_handler->id_); near_point_handler = this->nearPointHandlers_.erase(near_point_handler); } else { near_point_handler++; } } // Areas auto area_handler = this->areaHandlers_.begin(); while(area_handler != this->areaHandlers_.end()) { if(area_handler->entity_->getPosition() > area_handler->start_point_ && area_handler->entity_->getPosition() < area_handler->end_point_) { if(area_handler->atEnter_) { area_handler->callback_(area_handler->id_); area_handler = this->areaHandlers_.erase(area_handler); } else { area_handler++; } } else { if(!area_handler->atEnter_) { area_handler->callback_(area_handler->id_); area_handler = this->areaHandlers_.erase(area_handler); } else { area_handler++; } } } // Pawns to kill for(auto &pawn : this->pawnsToKill_) this->controller_->killPawn(pawn); this->pawnsToKill_.clear(); } }