1 | |
---|
2 | #include "scriptable_controller.h" |
---|
3 | #include "luatb.h" |
---|
4 | #include "infos/PlayerInfo.h" |
---|
5 | #include "core/command/Executor.h" |
---|
6 | #include "worldentities/pawns/Pawn.h" |
---|
7 | |
---|
8 | namespace orxonox |
---|
9 | { |
---|
10 | |
---|
11 | // Used https://csl.name/post/lua-and-cpp/ as a reference |
---|
12 | int ScriptableController::runScript(const std::string &file_path) |
---|
13 | { |
---|
14 | int ret; |
---|
15 | |
---|
16 | // Not having a script specified at all is not an error |
---|
17 | if(file_path.empty()) |
---|
18 | return 0; |
---|
19 | |
---|
20 | // Create a lua object |
---|
21 | lua_State *lua = luaL_newstate(); |
---|
22 | if(lua == nullptr) |
---|
23 | { |
---|
24 | orxout(internal_warning) << "ScriptableController: Falied to load script " << file_path << std::endl; |
---|
25 | return LUA_ERRMEM; |
---|
26 | } |
---|
27 | |
---|
28 | // Make standard libraries available in the Lua object |
---|
29 | luaL_openlibs(lua); |
---|
30 | |
---|
31 | // Register the API |
---|
32 | ScriptableControllerAPI *api = new ScriptableControllerAPI(lua, this); |
---|
33 | |
---|
34 | // Load the program; this supports both source code and bytecode files. |
---|
35 | if((ret = luaL_loadfile(lua, file_path.c_str())) != 0) |
---|
36 | { |
---|
37 | this->printLuaError(lua); |
---|
38 | delete api; |
---|
39 | return ret; |
---|
40 | } |
---|
41 | |
---|
42 | // Execute the script |
---|
43 | if((ret = lua_pcall(lua, 0, LUA_MULTRET, 0)) != 0) |
---|
44 | { |
---|
45 | this->printLuaError(lua); |
---|
46 | delete api; |
---|
47 | return ret; |
---|
48 | } |
---|
49 | |
---|
50 | // Remeber the api |
---|
51 | this->apis_.push_back(std::unique_ptr<ScriptableControllerAPI>(api)); |
---|
52 | |
---|
53 | return 0; |
---|
54 | } |
---|
55 | |
---|
56 | void ScriptableController::setPlayer(PlayerInfo *player) |
---|
57 | { |
---|
58 | this->player_ = player; |
---|
59 | } |
---|
60 | |
---|
61 | void ScriptableController::registerWorldEntity(std::string id, WorldEntity *entity) |
---|
62 | { |
---|
63 | this->worldEntities_[id] = entity; |
---|
64 | } |
---|
65 | |
---|
66 | void ScriptableController::registerMobileEntity(std::string id, MobileEntity *entity) |
---|
67 | { |
---|
68 | this->mobileEntities_[id] = entity; |
---|
69 | } |
---|
70 | |
---|
71 | void ScriptableController::registerPawn(std::string id, Pawn *pawn) |
---|
72 | { |
---|
73 | this->worldEntities_[id] = pawn; |
---|
74 | this->pawns_[id] = pawn; |
---|
75 | this->pawnsReverse_[pawn] = id; |
---|
76 | } |
---|
77 | |
---|
78 | void ScriptableController::pawnKilled(Pawn *pawn) |
---|
79 | { |
---|
80 | auto pawn_id_iter = this->pawnsReverse_.find(pawn); |
---|
81 | if(pawn_id_iter == this->pawnsReverse_.end()) |
---|
82 | { |
---|
83 | orxout(internal_warning) << "Unregistered pawn reported that it's destroyed" << std::endl; |
---|
84 | return; |
---|
85 | } |
---|
86 | |
---|
87 | for(auto &api : this->apis_) |
---|
88 | api->pawnKilled(pawn_id_iter->second); |
---|
89 | |
---|
90 | this->pawns_.erase(pawn_id_iter->second); |
---|
91 | this->pawnsReverse_.erase(pawn_id_iter); |
---|
92 | } |
---|
93 | |
---|
94 | void ScriptableController::pawnHit(Pawn *target, Pawn *source, double new_health, double new_shield) |
---|
95 | { |
---|
96 | auto target_id_iter = this->pawnsReverse_.find(target); |
---|
97 | auto source_id_iter = this->pawnsReverse_.find(source); |
---|
98 | |
---|
99 | if(target_id_iter == this->pawnsReverse_.end() || |
---|
100 | source_id_iter == this->pawnsReverse_.end() ) |
---|
101 | { |
---|
102 | orxout(internal_warning) << "Unregistered pawn reported that it's hit" << std::endl; |
---|
103 | return; |
---|
104 | } |
---|
105 | |
---|
106 | for(auto &api : this->apis_) |
---|
107 | api->pawnHit(target_id_iter->second, source_id_iter->second, new_health, new_shield); |
---|
108 | } |
---|
109 | |
---|
110 | void ScriptableController::killPawn(std::string id) |
---|
111 | { |
---|
112 | auto pawn = this->pawns_.find(id); |
---|
113 | if(pawn == this->pawns_.end()) |
---|
114 | { |
---|
115 | orxout(user_warning) << "Tried to destroy unknown pawn " << id << std::endl; |
---|
116 | return; |
---|
117 | } |
---|
118 | |
---|
119 | pawn->second->kill(); |
---|
120 | } |
---|
121 | |
---|
122 | WorldEntity *ScriptableController::getWorldEntityByID(std::string id) const |
---|
123 | { |
---|
124 | if(id == "player" || id == "Player" || id == "PLAYER") |
---|
125 | return this->player_->getControllableEntity(); |
---|
126 | |
---|
127 | auto entity = this->worldEntities_.find(id); |
---|
128 | return entity != this->worldEntities_.end() ? entity->second : nullptr; |
---|
129 | } |
---|
130 | |
---|
131 | MobileEntity *ScriptableController::getMobileEntityByID(std::string id) const |
---|
132 | { |
---|
133 | if(id == "player" || id == "Player" || id == "PLAYER") |
---|
134 | return this->player_->getControllableEntity(); |
---|
135 | |
---|
136 | auto entity = this->mobileEntities_.find(id); |
---|
137 | return entity != this->mobileEntities_.end() ? entity->second : nullptr; |
---|
138 | |
---|
139 | } |
---|
140 | |
---|
141 | Pawn *ScriptableController::getPawnByID(std::string id) const |
---|
142 | { |
---|
143 | auto pawn = this->pawns_.find(id); |
---|
144 | return pawn != this->pawns_.end() ? pawn->second : nullptr; |
---|
145 | } |
---|
146 | |
---|
147 | void ScriptableController::printLuaError(lua_State *lua) |
---|
148 | { |
---|
149 | // The error message is on top of the stack. |
---|
150 | // Fetch it, print it and then pop it off the stack. |
---|
151 | // Yes, this is 'const char*' and not 'std::string' because that's what lua gives us. |
---|
152 | const char* message = lua_tostring(lua, -1); |
---|
153 | std::cout << message << std::endl; |
---|
154 | lua_pop(lua, 1); |
---|
155 | } |
---|
156 | |
---|
157 | } |
---|