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::registerPawn(std::string id, Pawn *pawn) |
---|
67 | { |
---|
68 | this->worldEntities_[id] = pawn; |
---|
69 | this->pawns_[id] = pawn; |
---|
70 | this->pawnsReverse_[pawn] = id; |
---|
71 | } |
---|
72 | |
---|
73 | void ScriptableController::pawnKilled(Pawn *pawn) |
---|
74 | { |
---|
75 | auto pawn_id_iter = this->pawnsReverse_.find(pawn); |
---|
76 | if(pawn_id_iter == this->pawnsReverse_.end()) |
---|
77 | { |
---|
78 | orxout(internal_warning) << "Unregistered pawn reported that it's destroyed" << std::endl; |
---|
79 | return; |
---|
80 | } |
---|
81 | |
---|
82 | for(auto &api : this->apis_) |
---|
83 | api->pawnKilled(pawn_id_iter->second); |
---|
84 | |
---|
85 | this->pawns_.erase(pawn_id_iter->second); |
---|
86 | this->pawnsReverse_.erase(pawn_id_iter); |
---|
87 | } |
---|
88 | |
---|
89 | void ScriptableController::pawnHit(Pawn *target, Pawn *source, double new_health, double new_shield) |
---|
90 | { |
---|
91 | auto target_id_iter = this->pawnsReverse_.find(target); |
---|
92 | auto source_id_iter = this->pawnsReverse_.find(source); |
---|
93 | |
---|
94 | if(target_id_iter == this->pawnsReverse_.end() || |
---|
95 | source_id_iter == this->pawnsReverse_.end() ) |
---|
96 | { |
---|
97 | orxout(internal_warning) << "Unregistered pawn reported that it's hit" << std::endl; |
---|
98 | return; |
---|
99 | } |
---|
100 | |
---|
101 | for(auto &api : this->apis_) |
---|
102 | api->pawnHit(target_id_iter->second, source_id_iter->second, new_health, new_shield); |
---|
103 | } |
---|
104 | |
---|
105 | void ScriptableController::killPawn(std::string id) |
---|
106 | { |
---|
107 | auto pawn = this->pawns_.find(id); |
---|
108 | if(pawn == this->pawns_.end()) |
---|
109 | { |
---|
110 | orxout(user_warning) << "Tried to destroy unknown pawn " << id << std::endl; |
---|
111 | return; |
---|
112 | } |
---|
113 | |
---|
114 | pawn->second->kill(); |
---|
115 | } |
---|
116 | |
---|
117 | WorldEntity *ScriptableController::getWorldEntityByID(std::string id) const |
---|
118 | { |
---|
119 | if(id == "player" || id == "Player" || id == "PLAYER") |
---|
120 | return this->player_->getControllableEntity(); |
---|
121 | |
---|
122 | auto entity = this->worldEntities_.find(id); |
---|
123 | return entity != this->worldEntities_.end() ? entity->second : nullptr; |
---|
124 | } |
---|
125 | |
---|
126 | Pawn *ScriptableController::getPawnByID(std::string id) const |
---|
127 | { |
---|
128 | auto pawn = this->pawns_.find(id); |
---|
129 | return pawn != this->pawns_.end() ? pawn->second : nullptr; |
---|
130 | } |
---|
131 | |
---|
132 | void ScriptableController::printLuaError(lua_State *lua) |
---|
133 | { |
---|
134 | // The error message is on top of the stack. |
---|
135 | // Fetch it, print it and then pop it off the stack. |
---|
136 | // Yes, this is 'const char*' and not 'std::string' because that's what lua gives us. |
---|
137 | const char* message = lua_tostring(lua, -1); |
---|
138 | std::cout << message << std::endl; |
---|
139 | lua_pop(lua, 1); |
---|
140 | } |
---|
141 | |
---|
142 | } |
---|