| 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" |
|---|
| 13 | #include "tools/Timer.h" |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | struct lua_State; |
|---|
| 17 | |
|---|
| 18 | namespace orxonox |
|---|
| 19 | { |
|---|
| 20 | |
|---|
| 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 | */ |
|---|
| 31 | class ScriptableController |
|---|
| 32 | { |
|---|
| 33 | public: |
|---|
| 34 | /** |
|---|
| 35 | * @brief Run a lua script |
|---|
| 36 | * @param file_path Path to the script |
|---|
| 37 | * @return A lua error code (0 for success) |
|---|
| 38 | * |
|---|
| 39 | * Constructs an API for the script and runs it. |
|---|
| 40 | */ |
|---|
| 41 | int runScript(const std::string &file_path); |
|---|
| 42 | |
|---|
| 43 | /** |
|---|
| 44 | * @brief Set the player object of the current game |
|---|
| 45 | * @param player The player |
|---|
| 46 | * |
|---|
| 47 | * The player is a special object and can perfom special actions. |
|---|
| 48 | */ |
|---|
| 49 | void setPlayer(PlayerInfo *player); |
|---|
| 50 | |
|---|
| 51 | /** |
|---|
| 52 | * @brief Register a WorldEntity to the ScriptableController |
|---|
| 53 | * @param id The ID of the WorldEntity |
|---|
| 54 | * @param entity The WorldEntity |
|---|
| 55 | * |
|---|
| 56 | * The ScriptableController needs a list of all WorldEntity's so it can |
|---|
| 57 | * convert an ID to a WorldEntity. |
|---|
| 58 | */ |
|---|
| 59 | void registerWorldEntity(std::string id, WorldEntity *entity); |
|---|
| 60 | |
|---|
| 61 | /** |
|---|
| 62 | * @brief Register a MobileEntity to the ScriptableController |
|---|
| 63 | * @param id The ID of the MobileEntity |
|---|
| 64 | * @param entity The MobileEntity |
|---|
| 65 | * |
|---|
| 66 | * The ScriptableController needs a list of all MobileEntity's so it can |
|---|
| 67 | * convert an ID to a MobileEntity. |
|---|
| 68 | */ |
|---|
| 69 | void registerMobileEntity(std::string id, MobileEntity *entity); |
|---|
| 70 | |
|---|
| 71 | /** |
|---|
| 72 | * @brief Register a Pawn to the ScriptableController |
|---|
| 73 | * @param id The ID of the Pawn |
|---|
| 74 | * @param pawn The Pawn |
|---|
| 75 | * |
|---|
| 76 | * The ScriptableController needs a list of all Pawn's in addition to |
|---|
| 77 | * the WorldEntity's, because they have additional actions available. |
|---|
| 78 | */ |
|---|
| 79 | void registerPawn(std::string id, Pawn *pawn); |
|---|
| 80 | |
|---|
| 81 | /** |
|---|
| 82 | * @brief Called when a Pawn is killed |
|---|
| 83 | * @param pawn The Pawn |
|---|
| 84 | * |
|---|
| 85 | * Called by the Pawn itself as soon as it's killed. |
|---|
| 86 | */ |
|---|
| 87 | void pawnKilled(Pawn *pawn); |
|---|
| 88 | |
|---|
| 89 | /** |
|---|
| 90 | * @brief Called when a Pawn is hit |
|---|
| 91 | * @param target The hit Pawn |
|---|
| 92 | * @param source The shooting Pawn |
|---|
| 93 | * @param new_health The new health of the hit Pawn |
|---|
| 94 | * @param new_shield The new shield health of the hit Pawn |
|---|
| 95 | * |
|---|
| 96 | * Called by the Pawn itself as soon as it's hit. |
|---|
| 97 | */ |
|---|
| 98 | void pawnHit(Pawn *target, Pawn *source, double new_health, double new_shield); |
|---|
| 99 | |
|---|
| 100 | /** |
|---|
| 101 | * @brief Convert an ID to a WorldEntity pointer |
|---|
| 102 | * @param id The ID of the WorldEntity |
|---|
| 103 | * @return A pointer to the WorldEntity, nullptr if it's not found |
|---|
| 104 | */ |
|---|
| 105 | WorldEntity *getWorldEntityByID(std::string id) const; |
|---|
| 106 | |
|---|
| 107 | /** |
|---|
| 108 | * @brief Convert an ID to a MobileEntity pointer |
|---|
| 109 | * @param id The ID of the MobileEntity |
|---|
| 110 | * @return A pointer to the MobileEntity, nullptr if it's not found |
|---|
| 111 | */ |
|---|
| 112 | MobileEntity *getMobileEntityByID(std::string id) const; |
|---|
| 113 | |
|---|
| 114 | /** |
|---|
| 115 | * @brief Convert an ID to a Pawt pointer |
|---|
| 116 | * @param id The ID of the Pawn |
|---|
| 117 | * @return A pointer to the Pawn, nullptr if it's not found |
|---|
| 118 | */ |
|---|
| 119 | Pawn *getPawnByID(std::string id) const; |
|---|
| 120 | |
|---|
| 121 | private: |
|---|
| 122 | std::list<std::unique_ptr<ScriptableControllerAPI> > apis_; |
|---|
| 123 | PlayerInfo *player_; |
|---|
| 124 | std::map<std::string, WorldEntity*> worldEntities_; |
|---|
| 125 | std::map<std::string, MobileEntity*> mobileEntities_; |
|---|
| 126 | std::map<std::string, Pawn*> pawns_; |
|---|
| 127 | std::map<Pawn*, std::string> pawnsReverse_; |
|---|
| 128 | std::map<std::string, ControllableEntity*> controllabelEntities_; |
|---|
| 129 | |
|---|
| 130 | /** |
|---|
| 131 | * @brief Prints a human readable error message if a lua error occurs |
|---|
| 132 | * @param lua The lua state where the error occured |
|---|
| 133 | */ |
|---|
| 134 | void printLuaError(lua_State *lua); |
|---|
| 135 | }; |
|---|
| 136 | |
|---|
| 137 | } |
|---|
| 138 | |
|---|
| 139 | #endif // SCRIPTABLE_CONTROLLER_H |
|---|