[11519] | 1 | |
---|
| 2 | #include "scriptable_controller.h" |
---|
| 3 | #include "luatb.h" |
---|
[11562] | 4 | #include "infos/PlayerInfo.h" |
---|
[11583] | 5 | #include "core/command/Executor.h" |
---|
[11606] | 6 | #include "worldentities/pawns/Pawn.h" |
---|
[11519] | 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 | |
---|
[11583] | 16 | // Not having a script specified at all is not an error |
---|
| 17 | if(file_path.empty()) |
---|
| 18 | return 0; |
---|
| 19 | |
---|
[11519] | 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 | { |
---|
[11662] | 37 | orxout(user_error) << "Failed to load level script " + file_path << std::endl; |
---|
[11519] | 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 | { |
---|
[11662] | 46 | orxout(user_error) << "Level script returned an error" << std::endl; |
---|
[11519] | 47 | this->printLuaError(lua); |
---|
| 48 | delete api; |
---|
| 49 | return ret; |
---|
| 50 | } |
---|
| 51 | |
---|
[11673] | 52 | // Remember the api |
---|
[11519] | 53 | this->apis_.push_back(std::unique_ptr<ScriptableControllerAPI>(api)); |
---|
| 54 | |
---|
| 55 | return 0; |
---|
| 56 | } |
---|
| 57 | |
---|
[11562] | 58 | void ScriptableController::setPlayer(PlayerInfo *player) |
---|
| 59 | { |
---|
| 60 | this->player_ = player; |
---|
| 61 | } |
---|
| 62 | |
---|
[11583] | 63 | void ScriptableController::registerWorldEntity(std::string id, WorldEntity *entity) |
---|
[11519] | 64 | { |
---|
[11583] | 65 | this->worldEntities_[id] = entity; |
---|
[11519] | 66 | } |
---|
| 67 | |
---|
[11638] | 68 | void ScriptableController::registerMobileEntity(std::string id, MobileEntity *entity) |
---|
| 69 | { |
---|
| 70 | this->mobileEntities_[id] = entity; |
---|
| 71 | } |
---|
| 72 | |
---|
[11606] | 73 | void ScriptableController::registerPawn(std::string id, Pawn *pawn) |
---|
[11519] | 74 | { |
---|
[11606] | 75 | this->pawns_[id] = pawn; |
---|
| 76 | this->pawnsReverse_[pawn] = id; |
---|
[11519] | 77 | } |
---|
| 78 | |
---|
[11606] | 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 | |
---|
[11673] | 88 | // The ID of the pawn should already be invalid when we call the callback |
---|
| 89 | std::string id = pawn_id_iter->second; |
---|
[11606] | 90 | this->pawns_.erase(pawn_id_iter->second); |
---|
| 91 | this->pawnsReverse_.erase(pawn_id_iter); |
---|
[11673] | 92 | |
---|
| 93 | for(auto &api : this->apis_) |
---|
| 94 | api->pawnKilled(id, pawn); |
---|
[11606] | 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 | |
---|
[11583] | 113 | WorldEntity *ScriptableController::getWorldEntityByID(std::string id) const |
---|
[11519] | 114 | { |
---|
[11583] | 115 | if(id == "player" || id == "Player" || id == "PLAYER") |
---|
[11562] | 116 | return this->player_->getControllableEntity(); |
---|
| 117 | |
---|
[11583] | 118 | auto entity = this->worldEntities_.find(id); |
---|
| 119 | return entity != this->worldEntities_.end() ? entity->second : nullptr; |
---|
[11519] | 120 | } |
---|
| 121 | |
---|
[11638] | 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 | |
---|
[11606] | 131 | Pawn *ScriptableController::getPawnByID(std::string id) const |
---|
[11519] | 132 | { |
---|
[11662] | 133 | if(id == "player" || id == "Player" || id == "PLAYER") |
---|
| 134 | return orxonox_cast<Pawn*>(this->player_->getControllableEntity()); |
---|
| 135 | |
---|
[11606] | 136 | auto pawn = this->pawns_.find(id); |
---|
| 137 | return pawn != this->pawns_.end() ? pawn->second : nullptr; |
---|
[11519] | 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); |
---|
[11662] | 146 | orxout(user_error) << message << std::endl; |
---|
[11519] | 147 | lua_pop(lua, 1); |
---|
| 148 | } |
---|
| 149 | |
---|
| 150 | } |
---|