1 | |
---|
2 | #include "scriptable_controller_api.h" |
---|
3 | #include "luatb.h" |
---|
4 | #include "scriptable_controller.h" |
---|
5 | #include "tools/Timer.h" |
---|
6 | |
---|
7 | namespace orxonox |
---|
8 | { |
---|
9 | |
---|
10 | const double ScriptableControllerAPI::periodic_interval = 0.5; |
---|
11 | |
---|
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... |
---|
18 | LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::orxPrint)>::registerFunction<&ScriptableControllerAPI::orxPrint>(this, lua, "orxPrint"); |
---|
19 | |
---|
20 | LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::registerAfterTimeout)>::registerFunction<&ScriptableControllerAPI::registerAfterTimeout>(this, lua, "registerAfterTimeout"); |
---|
21 | LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::registerAtNearObject)>::registerFunction<&ScriptableControllerAPI::registerAtNearObject>(this, lua, "registerAtNearObject"); |
---|
22 | LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::registerAtNearPoint)>::registerFunction<&ScriptableControllerAPI::registerAtNearPoint>(this, lua, "registerAtNearPoint"); |
---|
23 | LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::registerAtAreaEnter)>::registerFunction<&ScriptableControllerAPI::registerAtAreaEnter>(this, lua, "registerAtAreaEnter"); |
---|
24 | LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::registerAtAreaLeave)>::registerFunction<&ScriptableControllerAPI::registerAtAreaLeave>(this, lua, "registerAtAreaLeave"); |
---|
25 | LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::registerAtPawnKilled)>::registerFunction<&ScriptableControllerAPI::registerAtPawnKilled>(this, lua, "registerAtPawnKilled"); |
---|
26 | LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::registerAtPawnHit)>::registerFunction<&ScriptableControllerAPI::registerAtPawnHit>(this, lua, "registerAtPawnHit"); |
---|
27 | |
---|
28 | LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::setPosition)>::registerFunction<&ScriptableControllerAPI::setPosition>(this, lua, "setPosition"); |
---|
29 | LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::setOrientation)>::registerFunction<&ScriptableControllerAPI::setOrientation>(this, lua, "setOrientation"); |
---|
30 | LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::setVelocity)>::registerFunction<&ScriptableControllerAPI::setVelocity>(this, lua, "setVelocity"); |
---|
31 | LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::setAngularVelocity)>::registerFunction<&ScriptableControllerAPI::setAngularVelocity>(this, lua, "setAngularVelocity"); |
---|
32 | |
---|
33 | LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::killPawn)>::registerFunction<&ScriptableControllerAPI::killPawn>(this, lua, "killPawn"); |
---|
34 | |
---|
35 | this->periodicTimer.setTimer(ScriptableControllerAPI::periodic_interval, true, createExecutor(createFunctor(&ScriptableControllerAPI::periodic, this)), false); |
---|
36 | } |
---|
37 | |
---|
38 | ScriptableControllerAPI::~ScriptableControllerAPI() |
---|
39 | { |
---|
40 | lua_close(this->lua_); |
---|
41 | } |
---|
42 | |
---|
43 | void ScriptableControllerAPI::orxPrint(std::string msg) |
---|
44 | { |
---|
45 | orxout(user_info) << msg << std::endl; |
---|
46 | } |
---|
47 | |
---|
48 | void ScriptableControllerAPI::registerAfterTimeout(std::function<void (void)> callback, double timeout) |
---|
49 | { |
---|
50 | // Kills itself when the timer fires |
---|
51 | new Timer(timeout, false, callback, true); |
---|
52 | } |
---|
53 | |
---|
54 | void ScriptableControllerAPI::registerAtNearObject(std::function<void (std::string, std::string)> callback, std::string id1, std::string id2, double distance) |
---|
55 | { |
---|
56 | WorldEntity *entity1 = this->controller_->getWorldEntityByID(id1); |
---|
57 | WorldEntity *entity2 = this->controller_->getWorldEntityByID(id2); |
---|
58 | |
---|
59 | if(entity1 != nullptr && entity2 != nullptr) |
---|
60 | this->nearObjectHandlers_.push_front(NearObjectHandler(entity1, entity2, id1, id2, distance, callback)); |
---|
61 | } |
---|
62 | |
---|
63 | void ScriptableControllerAPI::registerAtNearPoint(std::function<void (std::string)> callback, std::string id, double x, double y, double z, double distance) |
---|
64 | { |
---|
65 | WorldEntity *entity = this->controller_->getWorldEntityByID(id); |
---|
66 | |
---|
67 | if(entity != nullptr) |
---|
68 | this->nearPointHandlers_.push_front(NearPointHandler(entity, id, x, y, z, distance, callback)); |
---|
69 | } |
---|
70 | |
---|
71 | void ScriptableControllerAPI::registerAtAreaEnter(std::function<void (std::string)> callback, std::string id, int x, int y, int z, int dx, int dy, int dz) |
---|
72 | { |
---|
73 | WorldEntity *entity = this->controller_->getWorldEntityByID(id); |
---|
74 | |
---|
75 | if(entity != nullptr) |
---|
76 | this->areaHandlers_.push_front(AreaHandler(entity, id, x, y, z, dx, dy, dz, true, callback)); |
---|
77 | } |
---|
78 | |
---|
79 | void ScriptableControllerAPI::registerAtAreaLeave(std::function<void (std::string)> callback, std::string id, int x, int y, int z, int dx, int dy, int dz) |
---|
80 | { |
---|
81 | WorldEntity *entity = this->controller_->getWorldEntityByID(id); |
---|
82 | |
---|
83 | if(entity != nullptr) |
---|
84 | this->areaHandlers_.push_front(AreaHandler(entity, id, x, y, z, dx, dy, dz, false, callback)); |
---|
85 | } |
---|
86 | |
---|
87 | void ScriptableControllerAPI::registerAtPawnKilled(std::function<void (std::string)> callback, std::string id) |
---|
88 | { |
---|
89 | this->pawnDestroyedHandlers_[id].push_back(callback); |
---|
90 | } |
---|
91 | |
---|
92 | void ScriptableControllerAPI::registerAtPawnHit(std::function<void (std::string, std::string, double, double)> callback, std::string id) |
---|
93 | { |
---|
94 | this->pawnHitHandlers_[id].push_back(callback); |
---|
95 | } |
---|
96 | |
---|
97 | void ScriptableControllerAPI::killPawn(std::string id) |
---|
98 | { |
---|
99 | // We don't kill the pawn here directly, because this function is called from LUA and thus |
---|
100 | // runs in a different thread. So we schedule the kill for later in the main thread |
---|
101 | // (in 'periodic'). |
---|
102 | this->pawnsToKill_.push_back(id); |
---|
103 | } |
---|
104 | |
---|
105 | void ScriptableControllerAPI::setPosition(std::string id, double x, double y, double z) |
---|
106 | { |
---|
107 | WorldEntity *entity = this->controller_->getWorldEntityByID(id); |
---|
108 | if(entity == nullptr) |
---|
109 | { |
---|
110 | orxout(user_warning) << "Trying to set position of an unknown object" << std::endl; |
---|
111 | return; |
---|
112 | } |
---|
113 | |
---|
114 | const Vector3 &old = entity->getPosition(); |
---|
115 | |
---|
116 | x = std::isnan(x) ? old.x : x; |
---|
117 | y = std::isnan(y) ? old.y : y; |
---|
118 | z = std::isnan(z) ? old.z : z; |
---|
119 | |
---|
120 | entity->setPosition(x, y, z); |
---|
121 | } |
---|
122 | |
---|
123 | void ScriptableControllerAPI::setOrientation(std::string id, double x, double y, double z, double angle) |
---|
124 | { |
---|
125 | WorldEntity *entity = this->controller_->getWorldEntityByID(id); |
---|
126 | if(entity == nullptr) |
---|
127 | { |
---|
128 | orxout(user_warning) << "Trying to set orientation of an unknown object" << std::endl; |
---|
129 | return; |
---|
130 | } |
---|
131 | |
---|
132 | Vector3 old_axis; |
---|
133 | Degree old_angle; |
---|
134 | |
---|
135 | entity->getOrientation().ToAngleAxis(old_angle, old_axis); |
---|
136 | |
---|
137 | x = std::isnan(x) ? old_axis.x : x; |
---|
138 | y = std::isnan(y) ? old_axis.y : y; |
---|
139 | z = std::isnan(z) ? old_axis.z : z; |
---|
140 | angle = std::isnan(x) ? old_angle.valueDegrees() : angle; |
---|
141 | |
---|
142 | entity->setOrientation(Vector3(x, y, z), Degree(angle)); |
---|
143 | } |
---|
144 | |
---|
145 | void ScriptableControllerAPI::setVelocity(std::string id, double x, double y, double z) |
---|
146 | { |
---|
147 | MobileEntity *entity = this->controller_->getMobileEntityByID(id); |
---|
148 | if(entity == nullptr) |
---|
149 | { |
---|
150 | orxout(user_warning) << "Trying to set velocity of an unknown object" << std::endl; |
---|
151 | return; |
---|
152 | } |
---|
153 | |
---|
154 | const Vector3 &old = entity->getVelocity(); |
---|
155 | |
---|
156 | x = std::isnan(x) ? old.x : x; |
---|
157 | y = std::isnan(y) ? old.y : y; |
---|
158 | z = std::isnan(z) ? old.z : z; |
---|
159 | |
---|
160 | entity->setVelocity(x, y, z); |
---|
161 | } |
---|
162 | |
---|
163 | void ScriptableControllerAPI::setAngularVelocity(std::string id, double x, double y, double z) |
---|
164 | { |
---|
165 | MobileEntity *entity = this->controller_->getMobileEntityByID(id); |
---|
166 | if(entity == nullptr) |
---|
167 | { |
---|
168 | orxout(user_warning) << "Trying to set angular velocity of an unknown object" << std::endl; |
---|
169 | return; |
---|
170 | } |
---|
171 | |
---|
172 | const Vector3 &old = entity->getAngularVelocity(); |
---|
173 | |
---|
174 | x = std::isnan(x) ? old.x : x; |
---|
175 | y = std::isnan(y) ? old.y : y; |
---|
176 | z = std::isnan(z) ? old.z : z; |
---|
177 | |
---|
178 | entity->setAngularVelocity(x, y, z); |
---|
179 | } |
---|
180 | |
---|
181 | void ScriptableControllerAPI::pawnKilled(std::string id) |
---|
182 | { |
---|
183 | for(auto callback : this->pawnDestroyedHandlers_[id]) |
---|
184 | callback(id); |
---|
185 | |
---|
186 | this->pawnDestroyedHandlers_.erase(id); |
---|
187 | } |
---|
188 | |
---|
189 | void ScriptableControllerAPI::pawnHit(std::string target_id, std::string source_id, double new_health, double new_shield) |
---|
190 | { |
---|
191 | for(auto callback : this->pawnHitHandlers_[target_id]) |
---|
192 | callback(target_id, source_id, new_health, new_shield); |
---|
193 | } |
---|
194 | |
---|
195 | void ScriptableControllerAPI::periodic() |
---|
196 | { |
---|
197 | // Near object |
---|
198 | auto near_obj_handler = this->nearObjectHandlers_.begin(); |
---|
199 | while(near_obj_handler != this->nearObjectHandlers_.end()) |
---|
200 | { |
---|
201 | if((near_obj_handler->entity1_->getPosition() - near_obj_handler->entity2_->getPosition()).length() < near_obj_handler->distance_) |
---|
202 | { |
---|
203 | near_obj_handler->callback_(near_obj_handler->id1_, near_obj_handler->id2_); |
---|
204 | near_obj_handler = this->nearObjectHandlers_.erase(near_obj_handler); |
---|
205 | } |
---|
206 | else |
---|
207 | { |
---|
208 | near_obj_handler++; |
---|
209 | } |
---|
210 | } |
---|
211 | |
---|
212 | // Near point |
---|
213 | auto near_point_handler = this->nearPointHandlers_.begin(); |
---|
214 | while(near_point_handler != this->nearPointHandlers_.end()) |
---|
215 | { |
---|
216 | if((near_point_handler->entity_->getPosition() - near_point_handler->point_).length() < near_point_handler->distance_) |
---|
217 | { |
---|
218 | near_point_handler->callback_(near_point_handler->id_); |
---|
219 | near_point_handler = this->nearPointHandlers_.erase(near_point_handler); |
---|
220 | } |
---|
221 | else |
---|
222 | { |
---|
223 | near_point_handler++; |
---|
224 | } |
---|
225 | } |
---|
226 | |
---|
227 | // Areas |
---|
228 | auto area_handler = this->areaHandlers_.begin(); |
---|
229 | while(area_handler != this->areaHandlers_.end()) |
---|
230 | { |
---|
231 | if(area_handler->entity_->getPosition() > area_handler->start_point_ && |
---|
232 | area_handler->entity_->getPosition() < area_handler->end_point_) |
---|
233 | { |
---|
234 | if(area_handler->atEnter_) |
---|
235 | { |
---|
236 | area_handler->callback_(area_handler->id_); |
---|
237 | area_handler = this->areaHandlers_.erase(area_handler); |
---|
238 | } |
---|
239 | else |
---|
240 | { |
---|
241 | area_handler++; |
---|
242 | } |
---|
243 | } |
---|
244 | else |
---|
245 | { |
---|
246 | if(!area_handler->atEnter_) |
---|
247 | { |
---|
248 | area_handler->callback_(area_handler->id_); |
---|
249 | area_handler = this->areaHandlers_.erase(area_handler); |
---|
250 | } |
---|
251 | else |
---|
252 | { |
---|
253 | area_handler++; |
---|
254 | } |
---|
255 | } |
---|
256 | } |
---|
257 | |
---|
258 | // Pawns to kill |
---|
259 | for(auto &pawn : this->pawnsToKill_) |
---|
260 | this->controller_->killPawn(pawn); |
---|
261 | |
---|
262 | this->pawnsToKill_.clear(); |
---|
263 | } |
---|
264 | |
---|
265 | } |
---|