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