1 | |
---|
2 | #include "scriptable_controller.h" |
---|
3 | #include "luatb.h" |
---|
4 | #include "infos/PlayerInfo.h" |
---|
5 | #include "core/command/Executor.h" |
---|
6 | |
---|
7 | namespace orxonox |
---|
8 | { |
---|
9 | |
---|
10 | // Used https://csl.name/post/lua-and-cpp/ as a reference |
---|
11 | int ScriptableController::runScript(const std::string &file_path) |
---|
12 | { |
---|
13 | int ret; |
---|
14 | |
---|
15 | // Not having a script specified at all is not an error |
---|
16 | if(file_path.empty()) |
---|
17 | return 0; |
---|
18 | |
---|
19 | // Create a lua object |
---|
20 | lua_State *lua = luaL_newstate(); |
---|
21 | if(lua == nullptr) |
---|
22 | { |
---|
23 | orxout(internal_warning) << "ScriptableController: Falied to load script " << file_path << std::endl; |
---|
24 | return LUA_ERRMEM; |
---|
25 | } |
---|
26 | |
---|
27 | // Make standard libraries available in the Lua object |
---|
28 | luaL_openlibs(lua); |
---|
29 | |
---|
30 | // Register the API |
---|
31 | ScriptableControllerAPI *api = new ScriptableControllerAPI(lua, this); |
---|
32 | |
---|
33 | // Load the program; this supports both source code and bytecode files. |
---|
34 | if((ret = luaL_loadfile(lua, file_path.c_str())) != 0) |
---|
35 | { |
---|
36 | this->printLuaError(lua); |
---|
37 | delete api; |
---|
38 | return ret; |
---|
39 | } |
---|
40 | |
---|
41 | // Execute the script |
---|
42 | if((ret = lua_pcall(lua, 0, LUA_MULTRET, 0)) != 0) |
---|
43 | { |
---|
44 | this->printLuaError(lua); |
---|
45 | delete api; |
---|
46 | return ret; |
---|
47 | } |
---|
48 | |
---|
49 | // Remeber the api |
---|
50 | this->apis_.push_back(std::unique_ptr<ScriptableControllerAPI>(api)); |
---|
51 | |
---|
52 | return 0; |
---|
53 | } |
---|
54 | |
---|
55 | void ScriptableController::setPlayer(PlayerInfo *player) |
---|
56 | { |
---|
57 | this->player_ = player; |
---|
58 | } |
---|
59 | |
---|
60 | void ScriptableController::registerWorldEntity(std::string id, WorldEntity *entity) |
---|
61 | { |
---|
62 | this->worldEntities_[id] = entity; |
---|
63 | } |
---|
64 | |
---|
65 | void ScriptableController::registerControllableEntity(std::string id, ControllableEntity *entity) |
---|
66 | { |
---|
67 | this->worldEntities_[id] = entity; |
---|
68 | } |
---|
69 | |
---|
70 | WorldEntity *ScriptableController::getWorldEntityByID(std::string id) const |
---|
71 | { |
---|
72 | if(id == "player" || id == "Player" || id == "PLAYER") |
---|
73 | return this->player_->getControllableEntity(); |
---|
74 | |
---|
75 | auto entity = this->worldEntities_.find(id); |
---|
76 | return entity != this->worldEntities_.end() ? entity->second : nullptr; |
---|
77 | } |
---|
78 | |
---|
79 | ControllableEntity *ScriptableController::getControllableEntityByID(std::string id) const |
---|
80 | { |
---|
81 | if(id == "player" || id == "Player" || id == "PLAYER") |
---|
82 | return this->player_->getControllableEntity(); |
---|
83 | |
---|
84 | auto entity = this->controllabelEntities_.find(id); |
---|
85 | return entity != this->controllabelEntities_.end() ? entity->second : nullptr; |
---|
86 | } |
---|
87 | |
---|
88 | void ScriptableController::printLuaError(lua_State *lua) |
---|
89 | { |
---|
90 | // The error message is on top of the stack. |
---|
91 | // Fetch it, print it and then pop it off the stack. |
---|
92 | // Yes, this is 'const char*' and not 'std::string' because that's what lua gives us. |
---|
93 | const char* message = lua_tostring(lua, -1); |
---|
94 | std::cout << message << std::endl; |
---|
95 | lua_pop(lua, 1); |
---|
96 | } |
---|
97 | |
---|
98 | } |
---|