Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/ScriptableController_FS18/src/orxonox/scriptablecontroller/scriptable_controller_api.cc @ 11911

Last change on this file since 11911 was 11911, checked in by adamc, 6 years ago

scriptablecontroller trying to fix the spawning of objects

File size: 16.0 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
13    const double ScriptableControllerAPI::periodic_interval = 0.5;
14
15    ScriptableControllerAPI::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
40
41        LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::myTestFunction)>::registerFunction<&ScriptableControllerAPI::myTestFunction>(this, lua, "mytestfunction");
42        LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::moveControllableEntity)>::registerFunction<&ScriptableControllerAPI::moveControllableEntity>(this, lua, "moveControllableEntity");
43
44
45
46        this->periodicTimer.setTimer(ScriptableControllerAPI::periodic_interval, true, createExecutor(createFunctor(&ScriptableControllerAPI::periodic, this)), false);
47    }
48
49    ScriptableControllerAPI::~ScriptableControllerAPI()
50    {
51        lua_close(this->lua_);
52    }
53
54    void ScriptableControllerAPI::orxPrint(std::string msg)
55    {
56        orxout(user_info) << msg << std::endl;
57    }
58
59    void ScriptableControllerAPI::registerAfterTimeout(std::function<void (void)> callback, double timeout)
60    {
61        // Kills itself when the timer fires
62        new Timer(timeout, false, callback, true);
63    }
64
65    //void ScriptableControllerAPI::registerPeriodically(std::function<void (void)> callback, double timeout)
66    //{
67    //    // Kills itself when the timer fires
68    //    new Timer(timeout, false, callback, true);
69    //}
70
71
72
73
74
75
76
77
78    void ScriptableControllerAPI::registerAtNearObject(std::function<void (std::string, std::string)> callback, std::string id1, std::string id2, double distance)
79    {
80        WorldEntity *entity1 = this->controller_->getWorldEntityByID(id1);
81        WorldEntity *entity2 = this->controller_->getWorldEntityByID(id2);
82
83        if(entity1 != nullptr && entity2 != nullptr)
84            this->nearObjectHandlers_.push_front(NearObjectHandler(entity1, entity2, id1, id2, distance, callback));
85    }
86
87    void ScriptableControllerAPI::registerAtNearPoint(std::function<void (std::string)> callback, std::string id, double x, double y, double z, double distance)
88    {
89        WorldEntity *entity = this->controller_->getWorldEntityByID(id);
90
91        if(entity != nullptr)
92            this->nearPointHandlers_.push_front(NearPointHandler(entity, id, x, y, z, distance, callback));
93    }
94
95    void ScriptableControllerAPI::registerAtAreaEnter(std::function<void (std::string)> callback, std::string id, int x, int y, int z, int dx, int dy, int dz)
96    {
97        WorldEntity *entity = this->controller_->getWorldEntityByID(id);
98
99        if(entity != nullptr)
100            this->areaHandlers_.push_front(AreaHandler(entity, id, x, y, z, dx, dy, dz, true, callback));
101    }
102
103    void ScriptableControllerAPI::registerAtAreaLeave(std::function<void (std::string)> callback, std::string id, int x, int y, int z, int dx, int dy, int dz)
104    {
105        WorldEntity *entity = this->controller_->getWorldEntityByID(id);
106
107        if(entity != nullptr)
108            this->areaHandlers_.push_front(AreaHandler(entity, id, x, y, z, dx, dy, dz, false, callback));
109    }
110
111    void ScriptableControllerAPI::registerAtPawnKilled(std::function<void (std::string)> callback, std::string id)
112    {
113        this->pawnDestroyedHandlers_[id].push_back(callback);
114    }
115
116    void ScriptableControllerAPI::registerAtPawnHit(std::function<void (std::string, std::string, double, double)> callback, std::string id)
117    {
118        this->pawnHitHandlers_[id].push_back(callback);
119    }
120
121    void ScriptableControllerAPI::killPawn(std::string id)
122    {
123        Pawn *pawn = this->controller_->getPawnByID(id);
124        if(pawn == nullptr)
125            orxout(user_warning) << "Trying to kill an unknown pawn" << std::endl;
126        else
127            pawn->kill();
128    }
129
130    void ScriptableControllerAPI::spawn(std::string type, std::string id)
131    {
132        if(this->controller_->getWorldEntityByID(id) != nullptr)
133        {
134            orxout(user_warning) << "Script tried to spawn an object, but an object with the given ID exists already" << std::endl;
135            return;
136        }
137
138        Identifier *identifier = ClassByString(type);
139        if(!identifier)
140        {
141            orxout(user_error) << "Script tried to spawn unknown object" << std::endl;
142            return;
143        }
144
145        if(!identifier->isLoadable())
146        {
147            orxout(user_error) << "Script tried to spawn unloadable object" << std::endl;
148            return;
149        }
150
151        WorldEntity *entity;
152        Identifiable *obj = identifier->fabricate(this->controller_->getWorldEntityByID("Player")->getContext());
153
154        if(obj->isA(ClassIdentifier<WorldEntity>::getIdentifier()))
155        {
156            entity = orxonox_cast<WorldEntity*>(obj);
157        }
158        else if(obj->isA(ClassIdentifier<PlayerInfo>::getIdentifier()))
159        {
160            // TODO This does not work yet because somehow the controllable entity is not set
161            // yet at this stage.
162    //        entity = orxonox_cast<PlayerInfo*>(obj)->getControllableEntity();
163
164
165            //use TEMPLATES in the map to define objects that are not present on the map yet
166            return;
167        }
168        else
169        {
170            orxout(user_warning) << "Script tried to spawn an object that is neither a WorldEntity, nor a PlayerInfo" << std::endl;
171           
172            return;
173        }
174
175        if(entity->isA(ClassIdentifier<MobileEntity>::getIdentifier()))
176            this->controller_->registerMobileEntity(id, orxonox_cast<MobileEntity*>(entity));
177
178        if(entity->isA(ClassIdentifier<Pawn>::getIdentifier()))
179            this->controller_->registerPawn(id, orxonox_cast<Pawn*>(entity));
180
181        this->controller_->registerWorldEntity(id, orxonox_cast<WorldEntity*>(entity));
182    }
183
184    void ScriptableControllerAPI::setPosition(std::string id, double x, double y, double z)
185    {
186        WorldEntity *entity = this->controller_->getWorldEntityByID(id);
187        if(entity == nullptr)
188        {
189            orxout(user_warning) << "Trying to set position of an unknown object" << std::endl;
190            return;
191        }
192
193        const Vector3 &old = entity->getPosition();
194
195        // If one of the values is NaN, don't change that value
196        x = std::isnan(x) ? old.x : x;
197        y = std::isnan(y) ? old.y : y;
198        z = std::isnan(z) ? old.z : z;
199
200        entity->setPosition(x, y, z);
201    }
202
203    void ScriptableControllerAPI::setOrientation(std::string id, double x, double y, double z, double angle)
204    {
205        WorldEntity *entity = this->controller_->getWorldEntityByID(id);
206        if(entity == nullptr)
207        {
208            orxout(user_warning) << "Trying to set orientation of an unknown object" << std::endl;
209            return;
210        }
211
212        Vector3 old_axis;
213        Degree old_angle;
214
215        entity->getOrientation().ToAngleAxis(old_angle, old_axis);
216
217        // If one of the values is NaN, don't change that value
218        x = std::isnan(x) ? old_axis.x : x;
219        y = std::isnan(y) ? old_axis.y : y;
220        z = std::isnan(z) ? old_axis.z : z;
221        angle = std::isnan(x) ? old_angle.valueDegrees() : angle;
222
223
224        entity->setOrientation(Vector3(x, y, z), Degree(angle));
225    }
226
227    void ScriptableControllerAPI::setVelocity(std::string id, double x, double y, double z)
228    {
229        MobileEntity *entity = this->controller_->getMobileEntityByID(id);
230        if(entity == nullptr)
231        {
232            orxout(user_warning) << "Trying to set velocity of an unknown object" << std::endl;
233            return;
234        }
235
236        const Vector3 &old = entity->getVelocity();
237
238        // If one of the values is NaN, don't change that value
239        x = std::isnan(x) ? old.x : x;
240        y = std::isnan(y) ? old.y : y;
241        z = std::isnan(z) ? old.z : z;
242
243        entity->setVelocity(x, y, z);
244    }
245
246    void ScriptableControllerAPI::setAngularVelocity(std::string id, double x, double y, double z)
247    {
248        MobileEntity *entity = this->controller_->getMobileEntityByID(id);
249        if(entity == nullptr)
250        {
251            orxout(user_warning) << "Trying to set angular velocity of an unknown object" << std::endl;
252            return;
253        }
254
255        const Vector3 &old = entity->getAngularVelocity();
256
257        // If one of the values is NaN, don't change that value
258        x = std::isnan(x) ? old.x : x;
259        y = std::isnan(y) ? old.y : y;
260        z = std::isnan(z) ? old.z : z;
261
262        entity->setAngularVelocity(x, y, z);
263    }
264
265    void ScriptableControllerAPI::pawnKilled(std::string id, Pawn *pawn)
266    {
267        for(auto callback : this->pawnDestroyedHandlers_[id])
268            callback(id);
269
270        this->pawnDestroyedHandlers_.erase(id);
271
272        // We need to delete those handlers as well, they're no longer valid
273        auto near_obj_handler = this->nearObjectHandlers_.begin();
274        while(near_obj_handler != this->nearObjectHandlers_.end())
275        {
276            if(near_obj_handler->entity1_ == pawn || near_obj_handler->entity2_ == pawn)
277                near_obj_handler = this->nearObjectHandlers_.erase(near_obj_handler);
278            else
279                near_obj_handler++;
280        }
281
282        auto near_point_handler = this->nearPointHandlers_.begin();
283        while(near_point_handler != this->nearPointHandlers_.end())
284        {
285            if(near_point_handler->entity_ == pawn)
286                near_point_handler = this->nearPointHandlers_.erase(near_point_handler);
287            else
288                near_point_handler++;
289        }
290
291        auto area_handler = this->areaHandlers_.begin();
292        while(area_handler != this->areaHandlers_.end())
293        {
294            if(area_handler->entity_ == pawn)
295                area_handler = this->areaHandlers_.erase(area_handler);
296            else
297                area_handler++;
298        }
299    }
300
301    void ScriptableControllerAPI::pawnHit(std::string target_id, std::string source_id, double new_health, double new_shield)
302    {
303        for(auto callback : this->pawnHitHandlers_[target_id])
304            callback(target_id, source_id, new_health, new_shield);
305    }
306
307    void ScriptableControllerAPI::periodic()
308    {
309        // Near object
310        auto near_obj_handler = this->nearObjectHandlers_.begin();
311        while(near_obj_handler != this->nearObjectHandlers_.end())
312        {
313            if((near_obj_handler->entity1_->getPosition() - near_obj_handler->entity2_->getPosition()).length() < near_obj_handler->distance_)
314            {
315                near_obj_handler->callback_(near_obj_handler->id1_, near_obj_handler->id2_);
316                near_obj_handler = this->nearObjectHandlers_.erase(near_obj_handler);
317            }
318            else
319            {
320                near_obj_handler++;
321            }
322        }
323
324        // Near point
325        auto near_point_handler = this->nearPointHandlers_.begin();
326        while(near_point_handler != this->nearPointHandlers_.end())
327        {
328            if((near_point_handler->entity_->getPosition() - near_point_handler->point_).length() < near_point_handler->distance_)
329            {
330                near_point_handler->callback_(near_point_handler->id_);
331                near_point_handler = this->nearPointHandlers_.erase(near_point_handler);
332            }
333            else
334            {
335                near_point_handler++;
336            }
337        }
338
339        // Areas
340        auto area_handler = this->areaHandlers_.begin();
341        while(area_handler != this->areaHandlers_.end())
342        {
343            if(area_handler->entity_->getPosition() > area_handler->start_point_ &&
344               area_handler->entity_->getPosition() < area_handler->end_point_)
345            {
346                if(area_handler->atEnter_)
347                {
348                    area_handler->callback_(area_handler->id_);
349                    area_handler = this->areaHandlers_.erase(area_handler);
350                }
351                else
352                {
353                    area_handler++;
354                }
355            }
356            else
357            {
358                if(!area_handler->atEnter_)
359                {
360                    area_handler->callback_(area_handler->id_);
361                    area_handler = this->areaHandlers_.erase(area_handler);
362                }
363                else
364                {
365                    area_handler++;
366                }
367            }
368        }
369
370    }
371
372
373    //// TESTTESTTESTTESTTESTTEST
374    double ScriptableControllerAPI::myTestFunction(double x, double y)
375    {
376        double z = x + y;
377        orxout(user_info) << "Result = " << z << endl;
378        return z;
379    }
380
381    void ScriptableControllerAPI::moveControllableEntity(std::string id, double x, double y, double z)
382    {
383        MobileEntity *entity = this->controller_->getMobileEntityByID(id);
384        if(entity == nullptr)
385        {
386            orxout(user_warning) << "Trying to set velocity of an unknown object" << std::endl;
387            return;
388        }
389
390
391
392
393        Identifier *identifier = ClassByString("ControllableEntity");
394       
395        ControllableEntity *controllable_entity;
396       
397        if(identifier->isA(ClassIdentifier<ControllableEntity>::getIdentifier()))
398        {
399            controllable_entity = orxonox_cast<ControllableEntity*>(entity);
400
401            controllable_entity->moveFrontBack(x);
402            controllable_entity->moveRightLeft(y);
403            controllable_entity->moveUpDown(z);
404        }
405
406        return;
407       
408    }
409}
Note: See TracBrowser for help on using the repository browser.