[11519] | 1 | #ifndef SCRIPTABLE_CONTROLLER_H |
---|
| 2 | #define SCRIPTABLE_CONTROLLER_H |
---|
| 3 | |
---|
| 4 | #include <lua.hpp> |
---|
| 5 | #include <string> |
---|
| 6 | #include <list> |
---|
| 7 | #include <map> |
---|
| 8 | #include <memory> |
---|
| 9 | #include "scriptable_controller_api.h" |
---|
| 10 | #include "core/CoreIncludes.h" |
---|
| 11 | #include "worldentities/WorldEntity.h" |
---|
| 12 | #include "worldentities/ControllableEntity.h" |
---|
[11583] | 13 | #include "tools/Timer.h" |
---|
[11974] | 14 | #include "Level.h" |
---|
[11519] | 15 | |
---|
| 16 | struct lua_State; |
---|
| 17 | |
---|
| 18 | namespace orxonox |
---|
| 19 | { |
---|
| 20 | |
---|
[11606] | 21 | /** |
---|
| 22 | * @brief Runs a scripts on a per-level basis and handles the connection to orxonox |
---|
| 23 | * |
---|
| 24 | * The script is an attribute of the <Level> element with the name 'script' and should be |
---|
| 25 | * the path to a lua script. The script will be run as soon as the player spawns. It can |
---|
| 26 | * then register on various events and react to them. See ScriptableControllerAPI for |
---|
| 27 | * the complete API. |
---|
| 28 | * |
---|
| 29 | * \sa ScriptableControllerAPI |
---|
| 30 | */ |
---|
[11562] | 31 | class ScriptableController |
---|
[11519] | 32 | { |
---|
| 33 | public: |
---|
[11974] | 34 | Level* level_ = nullptr; |
---|
[11606] | 35 | /** |
---|
| 36 | * @brief Run a lua script |
---|
| 37 | * @param file_path Path to the script |
---|
| 38 | * @return A lua error code (0 for success) |
---|
| 39 | * |
---|
| 40 | * Constructs an API for the script and runs it. |
---|
| 41 | */ |
---|
[11519] | 42 | int runScript(const std::string &file_path); |
---|
| 43 | |
---|
[11606] | 44 | /** |
---|
| 45 | * @brief Set the player object of the current game |
---|
| 46 | * @param player The player |
---|
| 47 | * |
---|
| 48 | * The player is a special object and can perfom special actions. |
---|
| 49 | */ |
---|
[11562] | 50 | void setPlayer(PlayerInfo *player); |
---|
[11606] | 51 | |
---|
| 52 | /** |
---|
| 53 | * @brief Register a WorldEntity to the ScriptableController |
---|
| 54 | * @param id The ID of the WorldEntity |
---|
| 55 | * @param entity The WorldEntity |
---|
| 56 | * |
---|
| 57 | * The ScriptableController needs a list of all WorldEntity's so it can |
---|
| 58 | * convert an ID to a WorldEntity. |
---|
| 59 | */ |
---|
[11583] | 60 | void registerWorldEntity(std::string id, WorldEntity *entity); |
---|
[11519] | 61 | |
---|
[11606] | 62 | /** |
---|
[11638] | 63 | * @brief Register a MobileEntity to the ScriptableController |
---|
| 64 | * @param id The ID of the MobileEntity |
---|
| 65 | * @param entity The MobileEntity |
---|
| 66 | * |
---|
| 67 | * The ScriptableController needs a list of all MobileEntity's so it can |
---|
| 68 | * convert an ID to a MobileEntity. |
---|
| 69 | */ |
---|
| 70 | void registerMobileEntity(std::string id, MobileEntity *entity); |
---|
| 71 | |
---|
| 72 | /** |
---|
[11606] | 73 | * @brief Register a Pawn to the ScriptableController |
---|
| 74 | * @param id The ID of the Pawn |
---|
| 75 | * @param pawn The Pawn |
---|
| 76 | * |
---|
| 77 | * The ScriptableController needs a list of all Pawn's in addition to |
---|
| 78 | * the WorldEntity's, because they have additional actions available. |
---|
| 79 | */ |
---|
| 80 | void registerPawn(std::string id, Pawn *pawn); |
---|
| 81 | |
---|
| 82 | /** |
---|
| 83 | * @brief Called when a Pawn is killed |
---|
| 84 | * @param pawn The Pawn |
---|
| 85 | * |
---|
| 86 | * Called by the Pawn itself as soon as it's killed. |
---|
| 87 | */ |
---|
| 88 | void pawnKilled(Pawn *pawn); |
---|
| 89 | |
---|
| 90 | /** |
---|
| 91 | * @brief Called when a Pawn is hit |
---|
| 92 | * @param target The hit Pawn |
---|
| 93 | * @param source The shooting Pawn |
---|
| 94 | * @param new_health The new health of the hit Pawn |
---|
| 95 | * @param new_shield The new shield health of the hit Pawn |
---|
| 96 | * |
---|
| 97 | * Called by the Pawn itself as soon as it's hit. |
---|
| 98 | */ |
---|
| 99 | void pawnHit(Pawn *target, Pawn *source, double new_health, double new_shield); |
---|
| 100 | |
---|
| 101 | /** |
---|
| 102 | * @brief Convert an ID to a WorldEntity pointer |
---|
| 103 | * @param id The ID of the WorldEntity |
---|
| 104 | * @return A pointer to the WorldEntity, nullptr if it's not found |
---|
| 105 | */ |
---|
[11583] | 106 | WorldEntity *getWorldEntityByID(std::string id) const; |
---|
[11519] | 107 | |
---|
[11606] | 108 | /** |
---|
[11638] | 109 | * @brief Convert an ID to a MobileEntity pointer |
---|
| 110 | * @param id The ID of the MobileEntity |
---|
| 111 | * @return A pointer to the MobileEntity, nullptr if it's not found |
---|
| 112 | */ |
---|
| 113 | MobileEntity *getMobileEntityByID(std::string id) const; |
---|
| 114 | |
---|
| 115 | /** |
---|
[11606] | 116 | * @brief Convert an ID to a Pawt pointer |
---|
| 117 | * @param id The ID of the Pawn |
---|
| 118 | * @return A pointer to the Pawn, nullptr if it's not found |
---|
| 119 | */ |
---|
| 120 | Pawn *getPawnByID(std::string id) const; |
---|
| 121 | |
---|
[11519] | 122 | private: |
---|
| 123 | std::list<std::unique_ptr<ScriptableControllerAPI> > apis_; |
---|
[11562] | 124 | PlayerInfo *player_; |
---|
[11583] | 125 | std::map<std::string, WorldEntity*> worldEntities_; |
---|
[11638] | 126 | std::map<std::string, MobileEntity*> mobileEntities_; |
---|
[11606] | 127 | std::map<std::string, Pawn*> pawns_; |
---|
| 128 | std::map<Pawn*, std::string> pawnsReverse_; |
---|
[11583] | 129 | std::map<std::string, ControllableEntity*> controllabelEntities_; |
---|
[11519] | 130 | |
---|
[11606] | 131 | /** |
---|
| 132 | * @brief Prints a human readable error message if a lua error occurs |
---|
| 133 | * @param lua The lua state where the error occured |
---|
| 134 | */ |
---|
[11519] | 135 | void printLuaError(lua_State *lua); |
---|
| 136 | }; |
---|
| 137 | |
---|
| 138 | } |
---|
| 139 | |
---|
| 140 | #endif // SCRIPTABLE_CONTROLLER_H |
---|