[11519] | 1 | |
---|
| 2 | #include "scriptable_controller_api.h" |
---|
| 3 | #include "luatb.h" |
---|
| 4 | #include "scriptable_controller.h" |
---|
[11562] | 5 | #include "tools/Timer.h" |
---|
[11519] | 6 | |
---|
| 7 | namespace orxonox |
---|
| 8 | { |
---|
| 9 | |
---|
[11606] | 10 | const double ScriptableControllerAPI::periodic_interval = 0.5; |
---|
| 11 | |
---|
[11519] | 12 | ScriptableControllerAPI::ScriptableControllerAPI(lua_State *lua, ScriptableController *controller) |
---|
| 13 | { |
---|
| 14 | this->lua_ = lua; |
---|
| 15 | this->controller_ = controller; |
---|
| 16 | |
---|
| 17 | // Haven't found a shorter way yet to write that... |
---|
[11562] | 18 | LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::orxPrint)>::registerFunction<&ScriptableControllerAPI::orxPrint>(this, lua, "orxPrint"); |
---|
[11606] | 19 | |
---|
[11519] | 20 | LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::registerAfterTimeout)>::registerFunction<&ScriptableControllerAPI::registerAfterTimeout>(this, lua, "registerAfterTimeout"); |
---|
| 21 | LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::registerAtNearObject)>::registerFunction<&ScriptableControllerAPI::registerAtNearObject>(this, lua, "registerAtNearObject"); |
---|
[11583] | 22 | LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::registerAtNearPoint)>::registerFunction<&ScriptableControllerAPI::registerAtNearPoint>(this, lua, "registerAtNearPoint"); |
---|
[11519] | 23 | LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::registerAtAreaEnter)>::registerFunction<&ScriptableControllerAPI::registerAtAreaEnter>(this, lua, "registerAtAreaEnter"); |
---|
| 24 | LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::registerAtAreaLeave)>::registerFunction<&ScriptableControllerAPI::registerAtAreaLeave>(this, lua, "registerAtAreaLeave"); |
---|
[11606] | 25 | LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::registerAtPawnKilled)>::registerFunction<&ScriptableControllerAPI::registerAtPawnKilled>(this, lua, "registerAtPawnKilled"); |
---|
| 26 | LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::registerAtPawnHit)>::registerFunction<&ScriptableControllerAPI::registerAtPawnHit>(this, lua, "registerAtPawnHit"); |
---|
[11583] | 27 | |
---|
[11606] | 28 | LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::killPawn)>::registerFunction<&ScriptableControllerAPI::killPawn>(this, lua, "killPawn"); |
---|
| 29 | |
---|
| 30 | this->periodicTimer.setTimer(ScriptableControllerAPI::periodic_interval, true, createExecutor(createFunctor(&ScriptableControllerAPI::periodic, this)), false); |
---|
[11519] | 31 | } |
---|
| 32 | |
---|
| 33 | ScriptableControllerAPI::~ScriptableControllerAPI() |
---|
| 34 | { |
---|
| 35 | lua_close(this->lua_); |
---|
| 36 | } |
---|
| 37 | |
---|
[11562] | 38 | void ScriptableControllerAPI::orxPrint(std::string msg) |
---|
| 39 | { |
---|
| 40 | orxout(user_info) << msg << std::endl; |
---|
| 41 | } |
---|
| 42 | |
---|
[11549] | 43 | void ScriptableControllerAPI::registerAfterTimeout(std::function<void (void)> callback, double timeout) |
---|
[11519] | 44 | { |
---|
[11562] | 45 | // Kills itself when the timer fires |
---|
| 46 | new Timer(timeout, false, callback, true); |
---|
[11519] | 47 | } |
---|
| 48 | |
---|
[11583] | 49 | void ScriptableControllerAPI::registerAtNearObject(std::function<void (std::string, std::string)> callback, std::string id1, std::string id2, double distance) |
---|
[11519] | 50 | { |
---|
[11583] | 51 | WorldEntity *entity1 = this->controller_->getWorldEntityByID(id1); |
---|
| 52 | WorldEntity *entity2 = this->controller_->getWorldEntityByID(id2); |
---|
| 53 | |
---|
| 54 | if(entity1 != nullptr && entity2 != nullptr) |
---|
| 55 | this->nearObjectHandlers_.push_front(NearObjectHandler(entity1, entity2, id1, id2, distance, callback)); |
---|
[11519] | 56 | } |
---|
| 57 | |
---|
[11583] | 58 | void ScriptableControllerAPI::registerAtNearPoint(std::function<void (std::string)> callback, std::string id, double x, double y, double z, double distance) |
---|
[11519] | 59 | { |
---|
[11583] | 60 | WorldEntity *entity = this->controller_->getWorldEntityByID(id); |
---|
[11519] | 61 | |
---|
[11583] | 62 | if(entity != nullptr) |
---|
| 63 | this->nearPointHandlers_.push_front(NearPointHandler(entity, id, x, y, z, distance, callback)); |
---|
[11519] | 64 | } |
---|
| 65 | |
---|
[11583] | 66 | void ScriptableControllerAPI::registerAtAreaEnter(std::function<void (std::string)> callback, std::string id, int x, int y, int z, int dx, int dy, int dz) |
---|
[11519] | 67 | { |
---|
[11583] | 68 | WorldEntity *entity = this->controller_->getWorldEntityByID(id); |
---|
[11519] | 69 | |
---|
[11583] | 70 | if(entity != nullptr) |
---|
| 71 | this->areaHandlers_.push_front(AreaHandler(entity, id, x, y, z, dx, dy, dz, true, callback)); |
---|
[11519] | 72 | } |
---|
| 73 | |
---|
[11583] | 74 | void ScriptableControllerAPI::registerAtAreaLeave(std::function<void (std::string)> callback, std::string id, int x, int y, int z, int dx, int dy, int dz) |
---|
[11519] | 75 | { |
---|
[11583] | 76 | WorldEntity *entity = this->controller_->getWorldEntityByID(id); |
---|
[11519] | 77 | |
---|
[11583] | 78 | if(entity != nullptr) |
---|
| 79 | this->areaHandlers_.push_front(AreaHandler(entity, id, x, y, z, dx, dy, dz, false, callback)); |
---|
[11519] | 80 | } |
---|
| 81 | |
---|
[11606] | 82 | void ScriptableControllerAPI::registerAtPawnKilled(std::function<void (std::string)> callback, std::string id) |
---|
[11519] | 83 | { |
---|
[11606] | 84 | this->pawnDestroyedHandlers_[id].push_back(callback); |
---|
| 85 | } |
---|
| 86 | |
---|
| 87 | void ScriptableControllerAPI::registerAtPawnHit(std::function<void (std::string, std::string, double, double)> callback, std::string id) |
---|
| 88 | { |
---|
| 89 | this->pawnHitHandlers_[id].push_back(callback); |
---|
| 90 | } |
---|
| 91 | |
---|
| 92 | void ScriptableControllerAPI::killPawn(std::string id) |
---|
| 93 | { |
---|
| 94 | // We don't kill the pawn here directly, because this function is called from LUA and thus |
---|
| 95 | // runs in a different thread. So we schedule the kill for later in the main thread |
---|
| 96 | // (in 'periodic'). |
---|
| 97 | this->pawnsToKill_.push_back(id); |
---|
| 98 | } |
---|
| 99 | |
---|
| 100 | void ScriptableControllerAPI::pawnKilled(std::string id) |
---|
| 101 | { |
---|
| 102 | for(auto callback : this->pawnDestroyedHandlers_[id]) |
---|
| 103 | callback(id); |
---|
| 104 | |
---|
| 105 | this->pawnDestroyedHandlers_.erase(id); |
---|
| 106 | } |
---|
| 107 | |
---|
| 108 | void ScriptableControllerAPI::pawnHit(std::string target_id, std::string source_id, double new_health, double new_shield) |
---|
| 109 | { |
---|
| 110 | for(auto callback : this->pawnHitHandlers_[target_id]) |
---|
| 111 | callback(target_id, source_id, new_health, new_shield); |
---|
| 112 | } |
---|
| 113 | |
---|
| 114 | void ScriptableControllerAPI::periodic() |
---|
| 115 | { |
---|
[11583] | 116 | // Near object |
---|
| 117 | auto near_obj_handler = this->nearObjectHandlers_.begin(); |
---|
| 118 | while(near_obj_handler != this->nearObjectHandlers_.end()) |
---|
| 119 | { |
---|
| 120 | if((near_obj_handler->entity1_->getPosition() - near_obj_handler->entity2_->getPosition()).length() < near_obj_handler->distance_) |
---|
| 121 | { |
---|
| 122 | near_obj_handler->callback_(near_obj_handler->id1_, near_obj_handler->id2_); |
---|
| 123 | near_obj_handler = this->nearObjectHandlers_.erase(near_obj_handler); |
---|
| 124 | } |
---|
| 125 | else |
---|
| 126 | { |
---|
| 127 | near_obj_handler++; |
---|
| 128 | } |
---|
| 129 | } |
---|
[11519] | 130 | |
---|
[11583] | 131 | // Near point |
---|
| 132 | auto near_point_handler = this->nearPointHandlers_.begin(); |
---|
| 133 | while(near_point_handler != this->nearPointHandlers_.end()) |
---|
| 134 | { |
---|
| 135 | if((near_point_handler->entity_->getPosition() - near_point_handler->point_).length() < near_point_handler->distance_) |
---|
| 136 | { |
---|
| 137 | near_point_handler->callback_(near_point_handler->id_); |
---|
| 138 | near_point_handler = this->nearPointHandlers_.erase(near_point_handler); |
---|
| 139 | } |
---|
| 140 | else |
---|
| 141 | { |
---|
| 142 | near_point_handler++; |
---|
| 143 | } |
---|
| 144 | } |
---|
| 145 | |
---|
| 146 | // Areas |
---|
| 147 | auto area_handler = this->areaHandlers_.begin(); |
---|
| 148 | while(area_handler != this->areaHandlers_.end()) |
---|
| 149 | { |
---|
| 150 | if(area_handler->entity_->getPosition() > area_handler->start_point_ && |
---|
| 151 | area_handler->entity_->getPosition() < area_handler->end_point_) |
---|
| 152 | { |
---|
| 153 | if(area_handler->atEnter_) |
---|
| 154 | { |
---|
| 155 | area_handler->callback_(area_handler->id_); |
---|
| 156 | area_handler = this->areaHandlers_.erase(area_handler); |
---|
| 157 | } |
---|
| 158 | else |
---|
| 159 | { |
---|
| 160 | area_handler++; |
---|
| 161 | } |
---|
| 162 | } |
---|
| 163 | else |
---|
| 164 | { |
---|
| 165 | if(!area_handler->atEnter_) |
---|
| 166 | { |
---|
| 167 | area_handler->callback_(area_handler->id_); |
---|
| 168 | area_handler = this->areaHandlers_.erase(area_handler); |
---|
| 169 | } |
---|
| 170 | else |
---|
| 171 | { |
---|
| 172 | area_handler++; |
---|
| 173 | } |
---|
| 174 | } |
---|
| 175 | } |
---|
[11606] | 176 | |
---|
| 177 | // Pawns to kill |
---|
| 178 | for(auto &pawn : this->pawnsToKill_) |
---|
| 179 | this->controller_->killPawn(pawn); |
---|
| 180 | |
---|
| 181 | this->pawnsToKill_.clear(); |
---|
[11519] | 182 | } |
---|
| 183 | |
---|
| 184 | } |
---|