1 | |
---|
2 | #include "scriptable_controller.h" |
---|
3 | #include "luatb.h" |
---|
4 | #include "infos/PlayerInfo.h" |
---|
5 | |
---|
6 | namespace orxonox |
---|
7 | { |
---|
8 | |
---|
9 | // Used https://csl.name/post/lua-and-cpp/ as a reference |
---|
10 | int 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 | |
---|
52 | void ScriptableController::setPlayer(PlayerInfo *player) |
---|
53 | { |
---|
54 | this->player_ = player; |
---|
55 | } |
---|
56 | |
---|
57 | void ScriptableController::registerWorldEntity(int id, WorldEntity *obj) |
---|
58 | { |
---|
59 | orxout(user_info) << "WorldEntity registered (id: " << id << ")" << std::endl; |
---|
60 | this->worldEntities_[id] = obj; |
---|
61 | } |
---|
62 | |
---|
63 | void ScriptableController::registerControllableEntity(int id, ControllableEntity *obj) |
---|
64 | { |
---|
65 | orxout(user_info) << "ControllableEntity registered (id: " << id << ")" << std::endl; |
---|
66 | this->worldEntities_[id] = obj; |
---|
67 | } |
---|
68 | |
---|
69 | WorldEntity *ScriptableController::getWorldEntityByID(int id) const |
---|
70 | { |
---|
71 | if(id == 0) |
---|
72 | return this->player_->getControllableEntity(); |
---|
73 | |
---|
74 | auto obj = this->worldEntities_.find(id); |
---|
75 | return obj != this->worldEntities_.end() ? obj->second : nullptr; |
---|
76 | } |
---|
77 | |
---|
78 | ControllableEntity *ScriptableController::getControllableEntityByID(int id) const |
---|
79 | { |
---|
80 | if(id == 0) |
---|
81 | return this->player_->getControllableEntity(); |
---|
82 | |
---|
83 | auto obj = this->controllabelEntities_.find(id); |
---|
84 | return obj != this->controllabelEntities_.end() ? obj->second : nullptr; |
---|
85 | } |
---|
86 | |
---|
87 | void ScriptableController::addNearObjectHandler(int obj1, int obj2, double distance, std::function<void (int,int)> callback) |
---|
88 | { |
---|
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(); |
---|
102 | } |
---|
103 | |
---|
104 | void ScriptableController::objectMoved(WorldEntity *obj) |
---|
105 | { |
---|
106 | // Temp |
---|
107 | return; |
---|
108 | |
---|
109 | auto &nearObjectHandlers = this->nearObjectHandlers_[obj]; |
---|
110 | auto handler = nearObjectHandlers.begin(); |
---|
111 | while(handler != nearObjectHandlers.end()) |
---|
112 | { |
---|
113 | WorldEntity *other = (*handler)->otherObject_; |
---|
114 | if((obj->getPosition() - other->getPosition()).length() < (*handler)->distance_) |
---|
115 | { |
---|
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); |
---|
126 | } |
---|
127 | else |
---|
128 | { |
---|
129 | handler++; |
---|
130 | } |
---|
131 | } |
---|
132 | } |
---|
133 | |
---|
134 | void 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 | } |
---|