Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/ScriptableController_HS17/src/orxonox/scriptablecontroller/scriptable_controller.cc @ 11562

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

Figuring out when the different things are ready in orxonox

File size: 4.4 KB
RevLine 
[11519]1
2#include "scriptable_controller.h"
3#include "luatb.h"
[11562]4#include "infos/PlayerInfo.h"
[11519]5
6namespace orxonox
7{
8
9// Used https://csl.name/post/lua-and-cpp/ as a reference
10int ScriptableController::runScript(const std::string &file_path)
11{
12    int ret;
13
14    // Create a lua object
15    lua_State *lua = luaL_newstate();
16    if(lua == nullptr)
17    {
18        orxout(internal_warning) << "ScriptableController: Falied to load script " << file_path << std::endl;
19        return LUA_ERRMEM;
20    }
21
22    // Make standard libraries available in the Lua object
23    luaL_openlibs(lua);
24
25    // Register the API
26    ScriptableControllerAPI *api = new ScriptableControllerAPI(lua, this);
27
28    // Load the program; this supports both source code and bytecode files.
29    if((ret = luaL_loadfile(lua, file_path.c_str())) != 0)
30    {
31        this->printLuaError(lua);
32        delete api;
33        return ret;
34    }
35
36    // Execute the script
37    if((ret = lua_pcall(lua, 0, LUA_MULTRET, 0)) != 0)
38    {
39        this->printLuaError(lua);
40        delete api;
41        return ret;
42    }
43
44    // Remeber the api
45    this->apis_.push_back(std::unique_ptr<ScriptableControllerAPI>(api));
46
47    // Only the caller knows, when the end-of-life of the script is reached,
48    // so we give him control over when to destroy it.
49    return 0;
50}
51
[11562]52void ScriptableController::setPlayer(PlayerInfo *player)
53{
54    this->player_ = player;
55}
56
[11519]57void ScriptableController::registerWorldEntity(int id, WorldEntity *obj)
58{
[11562]59    orxout(user_info) << "WorldEntity registered (id: " << id << ")" << std::endl;
[11519]60    this->worldEntities_[id] = obj;
61}
62
63void ScriptableController::registerControllableEntity(int id, ControllableEntity *obj)
64{
[11562]65    orxout(user_info) << "ControllableEntity registered (id: " << id << ")" << std::endl;
[11519]66    this->worldEntities_[id] = obj;
67}
68
69WorldEntity *ScriptableController::getWorldEntityByID(int id) const
70{
[11562]71    if(id == 0)
72        return this->player_->getControllableEntity();
73
[11519]74    auto obj = this->worldEntities_.find(id);
75    return obj != this->worldEntities_.end() ? obj->second : nullptr;
76}
77
78ControllableEntity *ScriptableController::getControllableEntityByID(int id) const
79{
[11562]80    if(id == 0)
81        return this->player_->getControllableEntity();
82
[11519]83    auto obj = this->controllabelEntities_.find(id);
84    return obj != this->controllabelEntities_.end() ? obj->second : nullptr;
85}
86
[11562]87void ScriptableController::addNearObjectHandler(int obj1, int obj2, double distance, std::function<void (int,int)> callback)
[11549]88{
[11562]89    orxout(user_info) << "NearObject registering (id 1: " << obj1 << ", id 2: " << obj2 << ")" << std::endl;
90
91    WorldEntity *entity1 = this->getWorldEntityByID(obj1);
92    WorldEntity *entity2 = this->getWorldEntityByID(obj2);
93
94    NearObjectHandler *handler1 = new NearObjectHandler(entity2, distance, callback);
95    NearObjectHandler *handler2 = new NearObjectHandler(entity1, distance, callback);
96
97    this->nearObjectHandlers_[entity1].push_front(std::unique_ptr<NearObjectHandler>(handler1));
98    this->nearObjectHandlers_[entity2].push_front(std::unique_ptr<NearObjectHandler>(handler2));
99
100    handler1->otherHandler_ = this->nearObjectHandlers_[entity2].begin();
101    handler2->otherHandler_ = this->nearObjectHandlers_[entity1].begin();
[11549]102}
103
[11562]104void ScriptableController::objectMoved(WorldEntity *obj)
[11549]105{
[11562]106    // Temp
107    return;
[11549]108
[11562]109    auto &nearObjectHandlers = this->nearObjectHandlers_[obj];
110    auto handler = nearObjectHandlers.begin();
111    while(handler != nearObjectHandlers.end())
[11549]112    {
[11562]113        WorldEntity *other = (*handler)->otherObject_;
114        if((obj->getPosition() - other->getPosition()).length() < (*handler)->distance_)
[11549]115        {
[11562]116            (*handler)->callback_(obj->getID(), other->getID());
117
118            auto otherHandler = (*handler)->otherHandler_;
119            handler = nearObjectHandlers.erase(handler);
120            if(nearObjectHandlers.empty())
121                this->nearObjectHandlers_.erase(obj);
122
123            this->nearObjectHandlers_[other].erase(otherHandler);
124            if(this->nearObjectHandlers_[other].empty())
125                this->nearObjectHandlers_.erase(other);
[11549]126        }
127        else
128        {
[11562]129            handler++;
[11549]130        }
131    }
132}
133
[11519]134void ScriptableController::printLuaError(lua_State *lua)
135{
136    // The error message is on top of the stack.
137    // Fetch it, print it and then pop it off the stack.
138    // Yes, this is 'const char*' and not 'std::string' because that's what lua gives us.
139    const char* message = lua_tostring(lua, -1);
140    std::cout << message << std::endl;
141    lua_pop(lua, 1);
142}
143
144}
Note: See TracBrowser for help on using the repository browser.