Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/ScriptableController_HS17/src/orxonox/scriptablecontroller/scriptable_controller_api.cc @ 11828

Last change on this file since 11828 was 11673, checked in by kohlia, 7 years ago

The ScriptableController should work now. A demo level called scriptableControllerTest exists as well.

File size: 13.1 KB
Line 
1
2#include "scriptable_controller_api.h"
3#include "luatb.h"
4#include "scriptable_controller.h"
5#include "tools/Timer.h"
6#include "worldentities/pawns/Pawn.h"
7#include "infos/Bot.h"
8#include "worldentities/pawns/ModularSpaceShip.h"
9
10namespace orxonox
11{
12
13const double ScriptableControllerAPI::periodic_interval = 0.5;
14
15ScriptableControllerAPI::ScriptableControllerAPI(lua_State *lua, ScriptableController *controller)
16{
17    this->lua_ = lua;
18    this->controller_ = controller;
19
20    // Haven't found a shorter way yet to write that... We need C++17!
21    LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::orxPrint)>::registerFunction<&ScriptableControllerAPI::orxPrint>(this, lua, "orxPrint");
22
23    LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::registerAfterTimeout)>::registerFunction<&ScriptableControllerAPI::registerAfterTimeout>(this, lua, "registerAfterTimeout");
24    LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::registerAtNearObject)>::registerFunction<&ScriptableControllerAPI::registerAtNearObject>(this, lua, "registerAtNearObject");
25    LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::registerAtNearPoint)>::registerFunction<&ScriptableControllerAPI::registerAtNearPoint>(this, lua, "registerAtNearPoint");
26    LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::registerAtAreaEnter)>::registerFunction<&ScriptableControllerAPI::registerAtAreaEnter>(this, lua, "registerAtAreaEnter");
27    LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::registerAtAreaLeave)>::registerFunction<&ScriptableControllerAPI::registerAtAreaLeave>(this, lua, "registerAtAreaLeave");
28    LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::registerAtPawnKilled)>::registerFunction<&ScriptableControllerAPI::registerAtPawnKilled>(this, lua, "registerAtPawnKilled");
29    LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::registerAtPawnHit)>::registerFunction<&ScriptableControllerAPI::registerAtPawnHit>(this, lua, "registerAtPawnHit");
30
31    LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::setPosition)>::registerFunction<&ScriptableControllerAPI::setPosition>(this, lua, "setPosition");
32    LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::setOrientation)>::registerFunction<&ScriptableControllerAPI::setOrientation>(this, lua, "setOrientation");
33    LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::setVelocity)>::registerFunction<&ScriptableControllerAPI::setVelocity>(this, lua, "setVelocity");
34    LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::setAngularVelocity)>::registerFunction<&ScriptableControllerAPI::setAngularVelocity>(this, lua, "setAngularVelocity");
35
36    LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::killPawn)>::registerFunction<&ScriptableControllerAPI::killPawn>(this, lua, "killPawn");
37    LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::spawn)>::registerFunction<&ScriptableControllerAPI::spawn>(this, lua, "spawn");
38
39    this->periodicTimer.setTimer(ScriptableControllerAPI::periodic_interval, true, createExecutor(createFunctor(&ScriptableControllerAPI::periodic, this)), false);
40}
41
42ScriptableControllerAPI::~ScriptableControllerAPI()
43{
44    lua_close(this->lua_);
45}
46
47void ScriptableControllerAPI::orxPrint(std::string msg)
48{
49    orxout(user_info) << msg << std::endl;
50}
51
52void ScriptableControllerAPI::registerAfterTimeout(std::function<void (void)> callback, double timeout)
53{
54    // Kills itself when the timer fires
55    new Timer(timeout, false, callback, true);
56}
57
58void ScriptableControllerAPI::registerAtNearObject(std::function<void (std::string, std::string)> callback, std::string id1, std::string id2, double distance)
59{
60    WorldEntity *entity1 = this->controller_->getWorldEntityByID(id1);
61    WorldEntity *entity2 = this->controller_->getWorldEntityByID(id2);
62
63    if(entity1 != nullptr && entity2 != nullptr)
64        this->nearObjectHandlers_.push_front(NearObjectHandler(entity1, entity2, id1, id2, distance, callback));
65}
66
67void ScriptableControllerAPI::registerAtNearPoint(std::function<void (std::string)> callback, std::string id, double x, double y, double z, double distance)
68{
69    WorldEntity *entity = this->controller_->getWorldEntityByID(id);
70
71    if(entity != nullptr)
72        this->nearPointHandlers_.push_front(NearPointHandler(entity, id, x, y, z, distance, callback));
73}
74
75void ScriptableControllerAPI::registerAtAreaEnter(std::function<void (std::string)> callback, std::string id, int x, int y, int z, int dx, int dy, int dz)
76{
77    WorldEntity *entity = this->controller_->getWorldEntityByID(id);
78
79    if(entity != nullptr)
80        this->areaHandlers_.push_front(AreaHandler(entity, id, x, y, z, dx, dy, dz, true, callback));
81}
82
83void ScriptableControllerAPI::registerAtAreaLeave(std::function<void (std::string)> callback, std::string id, int x, int y, int z, int dx, int dy, int dz)
84{
85    WorldEntity *entity = this->controller_->getWorldEntityByID(id);
86
87    if(entity != nullptr)
88        this->areaHandlers_.push_front(AreaHandler(entity, id, x, y, z, dx, dy, dz, false, callback));
89}
90
91void ScriptableControllerAPI::registerAtPawnKilled(std::function<void (std::string)> callback, std::string id)
92{
93    this->pawnDestroyedHandlers_[id].push_back(callback);
94}
95
96void ScriptableControllerAPI::registerAtPawnHit(std::function<void (std::string, std::string, double, double)> callback, std::string id)
97{
98    this->pawnHitHandlers_[id].push_back(callback);
99}
100
101void ScriptableControllerAPI::killPawn(std::string id)
102{
103    Pawn *pawn = this->controller_->getPawnByID(id);
104    if(pawn == nullptr)
105        orxout(user_warning) << "Trying to kill an unknown pawn" << std::endl;
106    else
107        pawn->kill();
108}
109
110void ScriptableControllerAPI::spawn(std::string type, std::string id)
111{
112    if(this->controller_->getWorldEntityByID(id) != nullptr)
113    {
114        orxout(user_warning) << "Script tried to spawn an object, but an object with the given ID exists already" << std::endl;
115        return;
116    }
117
118    Identifier *identifier = ClassByString(type);
119    if(!identifier)
120    {
121        orxout(user_error) << "Script tried to spawn unknown object" << std::endl;
122        return;
123    }
124
125    if(!identifier->isLoadable())
126    {
127        orxout(user_error) << "Script tried to spawn unloadable object" << std::endl;
128        return;
129    }
130
131    WorldEntity *entity;
132    Identifiable *obj = identifier->fabricate(this->controller_->getWorldEntityByID("Player")->getContext());
133
134    if(obj->isA(ClassIdentifier<WorldEntity>::getIdentifier()))
135    {
136        entity = orxonox_cast<WorldEntity*>(obj);
137    }
138    else if(obj->isA(ClassIdentifier<PlayerInfo>::getIdentifier()))
139    {
140        // TODO This does not work yet because somehow the controllable entity is not set
141        // yet at this stage.
142//        entity = orxonox_cast<PlayerInfo*>(obj)->getControllableEntity();
143        return;
144    }
145    else
146    {
147        orxout(user_warning) << "Script tried to spawn an object that is neither a WorldEntity, nor a PlayerInfo" << std::endl;
148        return;
149    }
150
151    if(entity->isA(ClassIdentifier<MobileEntity>::getIdentifier()))
152        this->controller_->registerMobileEntity(id, orxonox_cast<MobileEntity*>(entity));
153
154    if(entity->isA(ClassIdentifier<Pawn>::getIdentifier()))
155        this->controller_->registerPawn(id, orxonox_cast<Pawn*>(entity));
156
157    this->controller_->registerWorldEntity(id, orxonox_cast<WorldEntity*>(entity));
158}
159
160void ScriptableControllerAPI::setPosition(std::string id, double x, double y, double z)
161{
162    WorldEntity *entity = this->controller_->getWorldEntityByID(id);
163    if(entity == nullptr)
164    {
165        orxout(user_warning) << "Trying to set position of an unknown object" << std::endl;
166        return;
167    }
168
169    const Vector3 &old = entity->getPosition();
170
171    // If one of the values is NaN, don't change that value
172    x = std::isnan(x) ? old.x : x;
173    y = std::isnan(y) ? old.y : y;
174    z = std::isnan(z) ? old.z : z;
175
176    entity->setPosition(x, y, z);
177}
178
179void ScriptableControllerAPI::setOrientation(std::string id, double x, double y, double z, double angle)
180{
181    WorldEntity *entity = this->controller_->getWorldEntityByID(id);
182    if(entity == nullptr)
183    {
184        orxout(user_warning) << "Trying to set orientation of an unknown object" << std::endl;
185        return;
186    }
187
188    Vector3 old_axis;
189    Degree old_angle;
190
191    entity->getOrientation().ToAngleAxis(old_angle, old_axis);
192
193    // If one of the values is NaN, don't change that value
194    x = std::isnan(x) ? old_axis.x : x;
195    y = std::isnan(y) ? old_axis.y : y;
196    z = std::isnan(z) ? old_axis.z : z;
197    angle = std::isnan(x) ? old_angle.valueDegrees() : angle;
198
199
200    entity->setOrientation(Vector3(x, y, z), Degree(angle));
201}
202
203void ScriptableControllerAPI::setVelocity(std::string id, double x, double y, double z)
204{
205    MobileEntity *entity = this->controller_->getMobileEntityByID(id);
206    if(entity == nullptr)
207    {
208        orxout(user_warning) << "Trying to set velocity of an unknown object" << std::endl;
209        return;
210    }
211
212    const Vector3 &old = entity->getVelocity();
213
214    // If one of the values is NaN, don't change that value
215    x = std::isnan(x) ? old.x : x;
216    y = std::isnan(y) ? old.y : y;
217    z = std::isnan(z) ? old.z : z;
218
219    entity->setVelocity(x, y, z);
220}
221
222void ScriptableControllerAPI::setAngularVelocity(std::string id, double x, double y, double z)
223{
224    MobileEntity *entity = this->controller_->getMobileEntityByID(id);
225    if(entity == nullptr)
226    {
227        orxout(user_warning) << "Trying to set angular velocity of an unknown object" << std::endl;
228        return;
229    }
230
231    const Vector3 &old = entity->getAngularVelocity();
232
233    // If one of the values is NaN, don't change that value
234    x = std::isnan(x) ? old.x : x;
235    y = std::isnan(y) ? old.y : y;
236    z = std::isnan(z) ? old.z : z;
237
238    entity->setAngularVelocity(x, y, z);
239}
240
241void ScriptableControllerAPI::pawnKilled(std::string id, Pawn *pawn)
242{
243    for(auto callback : this->pawnDestroyedHandlers_[id])
244        callback(id);
245
246    this->pawnDestroyedHandlers_.erase(id);
247
248    // We need to delete those handlers as well, they're no longer valid
249    auto near_obj_handler = this->nearObjectHandlers_.begin();
250    while(near_obj_handler != this->nearObjectHandlers_.end())
251    {
252        if(near_obj_handler->entity1_ == pawn || near_obj_handler->entity2_ == pawn)
253            near_obj_handler = this->nearObjectHandlers_.erase(near_obj_handler);
254        else
255            near_obj_handler++;
256    }
257
258    auto near_point_handler = this->nearPointHandlers_.begin();
259    while(near_point_handler != this->nearPointHandlers_.end())
260    {
261        if(near_point_handler->entity_ == pawn)
262            near_point_handler = this->nearPointHandlers_.erase(near_point_handler);
263        else
264            near_point_handler++;
265    }
266
267    auto area_handler = this->areaHandlers_.begin();
268    while(area_handler != this->areaHandlers_.end())
269    {
270        if(area_handler->entity_ == pawn)
271            area_handler = this->areaHandlers_.erase(area_handler);
272        else
273            area_handler++;
274    }
275}
276
277void ScriptableControllerAPI::pawnHit(std::string target_id, std::string source_id, double new_health, double new_shield)
278{
279    for(auto callback : this->pawnHitHandlers_[target_id])
280        callback(target_id, source_id, new_health, new_shield);
281}
282
283void ScriptableControllerAPI::periodic()
284{
285    // Near object
286    auto near_obj_handler = this->nearObjectHandlers_.begin();
287    while(near_obj_handler != this->nearObjectHandlers_.end())
288    {
289        if((near_obj_handler->entity1_->getPosition() - near_obj_handler->entity2_->getPosition()).length() < near_obj_handler->distance_)
290        {
291            near_obj_handler->callback_(near_obj_handler->id1_, near_obj_handler->id2_);
292            near_obj_handler = this->nearObjectHandlers_.erase(near_obj_handler);
293        }
294        else
295        {
296            near_obj_handler++;
297        }
298    }
299
300    // Near point
301    auto near_point_handler = this->nearPointHandlers_.begin();
302    while(near_point_handler != this->nearPointHandlers_.end())
303    {
304        if((near_point_handler->entity_->getPosition() - near_point_handler->point_).length() < near_point_handler->distance_)
305        {
306            near_point_handler->callback_(near_point_handler->id_);
307            near_point_handler = this->nearPointHandlers_.erase(near_point_handler);
308        }
309        else
310        {
311            near_point_handler++;
312        }
313    }
314
315    // Areas
316    auto area_handler = this->areaHandlers_.begin();
317    while(area_handler != this->areaHandlers_.end())
318    {
319        if(area_handler->entity_->getPosition() > area_handler->start_point_ &&
320           area_handler->entity_->getPosition() < area_handler->end_point_)
321        {
322            if(area_handler->atEnter_)
323            {
324                area_handler->callback_(area_handler->id_);
325                area_handler = this->areaHandlers_.erase(area_handler);
326            }
327            else
328            {
329                area_handler++;
330            }
331        }
332        else
333        {
334            if(!area_handler->atEnter_)
335            {
336                area_handler->callback_(area_handler->id_);
337                area_handler = this->areaHandlers_.erase(area_handler);
338            }
339            else
340            {
341                area_handler++;
342            }
343        }
344    }
345}
346
347}
Note: See TracBrowser for help on using the repository browser.