Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 11606 was 11606, checked in by kohlia, 6 years ago

Pawn killing works too now

File size: 7.3 KB
Line 
1
2#include "scriptable_controller_api.h"
3#include "luatb.h"
4#include "scriptable_controller.h"
5#include "tools/Timer.h"
6
7namespace orxonox
8{
9
10const double ScriptableControllerAPI::periodic_interval = 0.5;
11
12ScriptableControllerAPI::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::killPawn)>::registerFunction<&ScriptableControllerAPI::killPawn>(this, lua, "killPawn");
29
30    this->periodicTimer.setTimer(ScriptableControllerAPI::periodic_interval, true, createExecutor(createFunctor(&ScriptableControllerAPI::periodic, this)), false);
31}
32
33ScriptableControllerAPI::~ScriptableControllerAPI()
34{
35    lua_close(this->lua_);
36}
37
38void ScriptableControllerAPI::orxPrint(std::string msg)
39{
40    orxout(user_info) << msg << std::endl;
41}
42
43void ScriptableControllerAPI::registerAfterTimeout(std::function<void (void)> callback, double timeout)
44{
45    // Kills itself when the timer fires
46    new Timer(timeout, false, callback, true);
47}
48
49void ScriptableControllerAPI::registerAtNearObject(std::function<void (std::string, std::string)> callback, std::string id1, std::string id2, double distance)
50{
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));
56}
57
58void ScriptableControllerAPI::registerAtNearPoint(std::function<void (std::string)> callback, std::string id, double x, double y, double z, double distance)
59{
60    WorldEntity *entity = this->controller_->getWorldEntityByID(id);
61
62    if(entity != nullptr)
63        this->nearPointHandlers_.push_front(NearPointHandler(entity, id, x, y, z, distance, callback));
64}
65
66void ScriptableControllerAPI::registerAtAreaEnter(std::function<void (std::string)> callback, std::string id, int x, int y, int z, int dx, int dy, int dz)
67{
68    WorldEntity *entity = this->controller_->getWorldEntityByID(id);
69
70    if(entity != nullptr)
71        this->areaHandlers_.push_front(AreaHandler(entity, id, x, y, z, dx, dy, dz, true, callback));
72}
73
74void ScriptableControllerAPI::registerAtAreaLeave(std::function<void (std::string)> callback, std::string id, int x, int y, int z, int dx, int dy, int dz)
75{
76    WorldEntity *entity = this->controller_->getWorldEntityByID(id);
77
78    if(entity != nullptr)
79        this->areaHandlers_.push_front(AreaHandler(entity, id, x, y, z, dx, dy, dz, false, callback));
80}
81
82void ScriptableControllerAPI::registerAtPawnKilled(std::function<void (std::string)> callback, std::string id)
83{
84    this->pawnDestroyedHandlers_[id].push_back(callback);
85}
86
87void 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
92void 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
100void ScriptableControllerAPI::pawnKilled(std::string id)
101{
102    for(auto callback : this->pawnDestroyedHandlers_[id])
103        callback(id);
104
105    this->pawnDestroyedHandlers_.erase(id);
106}
107
108void 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
114void ScriptableControllerAPI::periodic()
115{
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    }
130
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    }
176
177    // Pawns to kill
178    for(auto &pawn : this->pawnsToKill_)
179        this->controller_->killPawn(pawn);
180
181    this->pawnsToKill_.clear();
182}
183
184}
Note: See TracBrowser for help on using the repository browser.