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: Failed 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 | orxout(user_error) << "Failed to load level script " + file_path << std::endl; |
---|
38 | this->printLuaError(lua); |
---|
39 | delete api; |
---|
40 | return ret; |
---|
41 | } |
---|
42 | |
---|
43 | // Execute the script |
---|
44 | if((ret = lua_pcall(lua, 0, LUA_MULTRET, 0)) != 0) |
---|
45 | { |
---|
46 | orxout(user_error) << "Level script returned an error" << std::endl; |
---|
47 | this->printLuaError(lua); |
---|
48 | delete api; |
---|
49 | return ret; |
---|
50 | } |
---|
51 | |
---|
52 | // Remember the api |
---|
53 | this->apis_.push_back(std::unique_ptr<ScriptableControllerAPI>(api)); |
---|
54 | |
---|
55 | return 0; |
---|
56 | } |
---|
57 | |
---|
58 | void ScriptableController::setPlayer(PlayerInfo *player) |
---|
59 | { |
---|
60 | this->player_ = player; |
---|
61 | } |
---|
62 | |
---|
63 | void ScriptableController::registerWorldEntity(std::string id, WorldEntity *entity) |
---|
64 | { |
---|
65 | this->worldEntities_[id] = entity; |
---|
66 | } |
---|
67 | |
---|
68 | void ScriptableController::registerMobileEntity(std::string id, MobileEntity *entity) |
---|
69 | { |
---|
70 | this->mobileEntities_[id] = entity; |
---|
71 | } |
---|
72 | |
---|
73 | void ScriptableController::registerPawn(std::string id, Pawn *pawn) |
---|
74 | { |
---|
75 | this->pawns_[id] = pawn; |
---|
76 | this->pawnsReverse_[pawn] = id; |
---|
77 | } |
---|
78 | |
---|
79 | void ScriptableController::pawnKilled(Pawn *pawn) |
---|
80 | { |
---|
81 | auto pawn_id_iter = this->pawnsReverse_.find(pawn); |
---|
82 | if(pawn_id_iter == this->pawnsReverse_.end()) |
---|
83 | { |
---|
84 | orxout(internal_warning) << "Unregistered pawn reported that it's destroyed" << std::endl; |
---|
85 | return; |
---|
86 | } |
---|
87 | |
---|
88 | // The ID of the pawn should already be invalid when we call the callback |
---|
89 | std::string id = pawn_id_iter->second; |
---|
90 | this->pawns_.erase(pawn_id_iter->second); |
---|
91 | this->pawnsReverse_.erase(pawn_id_iter); |
---|
92 | |
---|
93 | for(auto &api : this->apis_) |
---|
94 | api->pawnKilled(id, pawn); |
---|
95 | } |
---|
96 | |
---|
97 | void ScriptableController::pawnHit(Pawn *target, Pawn *source, double new_health, double new_shield) |
---|
98 | { |
---|
99 | auto target_id_iter = this->pawnsReverse_.find(target); |
---|
100 | auto source_id_iter = this->pawnsReverse_.find(source); |
---|
101 | |
---|
102 | if(target_id_iter == this->pawnsReverse_.end() || |
---|
103 | source_id_iter == this->pawnsReverse_.end() ) |
---|
104 | { |
---|
105 | orxout(internal_warning) << "Unregistered pawn reported that it's hit" << std::endl; |
---|
106 | return; |
---|
107 | } |
---|
108 | |
---|
109 | for(auto &api : this->apis_) |
---|
110 | api->pawnHit(target_id_iter->second, source_id_iter->second, new_health, new_shield); |
---|
111 | } |
---|
112 | |
---|
113 | WorldEntity *ScriptableController::getWorldEntityByID(std::string id) const |
---|
114 | { |
---|
115 | if(id == "player" || id == "Player" || id == "PLAYER") |
---|
116 | return this->player_->getControllableEntity(); |
---|
117 | |
---|
118 | auto entity = this->worldEntities_.find(id); |
---|
119 | return entity != this->worldEntities_.end() ? entity->second : nullptr; |
---|
120 | } |
---|
121 | |
---|
122 | MobileEntity *ScriptableController::getMobileEntityByID(std::string id) const |
---|
123 | { |
---|
124 | if(id == "player" || id == "Player" || id == "PLAYER") |
---|
125 | return this->player_->getControllableEntity(); |
---|
126 | |
---|
127 | auto entity = this->mobileEntities_.find(id); |
---|
128 | return entity != this->mobileEntities_.end() ? entity->second : nullptr; |
---|
129 | } |
---|
130 | |
---|
131 | Pawn *ScriptableController::getPawnByID(std::string id) const |
---|
132 | { |
---|
133 | if(id == "player" || id == "Player" || id == "PLAYER") |
---|
134 | return orxonox_cast<Pawn*>(this->player_->getControllableEntity()); |
---|
135 | |
---|
136 | auto pawn = this->pawns_.find(id); |
---|
137 | return pawn != this->pawns_.end() ? pawn->second : nullptr; |
---|
138 | } |
---|
139 | |
---|
140 | void ScriptableController::printLuaError(lua_State *lua) |
---|
141 | { |
---|
142 | // The error message is on top of the stack. |
---|
143 | // Fetch it, print it and then pop it off the stack. |
---|
144 | // Yes, this is 'const char*' and not 'std::string' because that's what lua gives us. |
---|
145 | const char* message = lua_tostring(lua, -1); |
---|
146 | orxout(user_error) << message << std::endl; |
---|
147 | lua_pop(lua, 1); |
---|
148 | } |
---|
149 | |
---|
150 | } |
---|