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 MobileEntity to the ScriptableController |
---|
62 | * @param id The ID of the MobileEntity |
---|
63 | * @param entity The MobileEntity |
---|
64 | * |
---|
65 | * The ScriptableController needs a list of all MobileEntity's so it can |
---|
66 | * convert an ID to a MobileEntity. |
---|
67 | */ |
---|
68 | void registerMobileEntity(std::string id, MobileEntity *entity); |
---|
69 | |
---|
70 | /** |
---|
71 | * @brief Register a Pawn to the ScriptableController |
---|
72 | * @param id The ID of the Pawn |
---|
73 | * @param pawn The Pawn |
---|
74 | * |
---|
75 | * The ScriptableController needs a list of all Pawn's in addition to |
---|
76 | * the WorldEntity's, because they have additional actions available. |
---|
77 | */ |
---|
78 | void registerPawn(std::string id, Pawn *pawn); |
---|
79 | |
---|
80 | /** |
---|
81 | * @brief Called when a Pawn is killed |
---|
82 | * @param pawn The Pawn |
---|
83 | * |
---|
84 | * Called by the Pawn itself as soon as it's killed. |
---|
85 | */ |
---|
86 | void pawnKilled(Pawn *pawn); |
---|
87 | |
---|
88 | /** |
---|
89 | * @brief Called when a Pawn is hit |
---|
90 | * @param target The hit Pawn |
---|
91 | * @param source The shooting Pawn |
---|
92 | * @param new_health The new health of the hit Pawn |
---|
93 | * @param new_shield The new shield health of the hit Pawn |
---|
94 | * |
---|
95 | * Called by the Pawn itself as soon as it's hit. |
---|
96 | */ |
---|
97 | void pawnHit(Pawn *target, Pawn *source, double new_health, double new_shield); |
---|
98 | |
---|
99 | /** |
---|
100 | * @brief Convert an ID to a WorldEntity pointer |
---|
101 | * @param id The ID of the WorldEntity |
---|
102 | * @return A pointer to the WorldEntity, nullptr if it's not found |
---|
103 | */ |
---|
104 | WorldEntity *getWorldEntityByID(std::string id) const; |
---|
105 | |
---|
106 | /** |
---|
107 | * @brief Convert an ID to a MobileEntity pointer |
---|
108 | * @param id The ID of the MobileEntity |
---|
109 | * @return A pointer to the MobileEntity, nullptr if it's not found |
---|
110 | */ |
---|
111 | MobileEntity *getMobileEntityByID(std::string id) const; |
---|
112 | |
---|
113 | /** |
---|
114 | * @brief Convert an ID to a Pawt pointer |
---|
115 | * @param id The ID of the Pawn |
---|
116 | * @return A pointer to the Pawn, nullptr if it's not found |
---|
117 | */ |
---|
118 | Pawn *getPawnByID(std::string id) const; |
---|
119 | |
---|
120 | private: |
---|
121 | std::list<std::unique_ptr<ScriptableControllerAPI> > apis_; |
---|
122 | PlayerInfo *player_; |
---|
123 | std::map<std::string, WorldEntity*> worldEntities_; |
---|
124 | std::map<std::string, MobileEntity*> mobileEntities_; |
---|
125 | std::map<std::string, Pawn*> pawns_; |
---|
126 | std::map<Pawn*, std::string> pawnsReverse_; |
---|
127 | std::map<std::string, ControllableEntity*> controllabelEntities_; |
---|
128 | |
---|
129 | /** |
---|
130 | * @brief Prints a human readable error message if a lua error occurs |
---|
131 | * @param lua The lua state where the error occured |
---|
132 | */ |
---|
133 | void printLuaError(lua_State *lua); |
---|
134 | }; |
---|
135 | |
---|
136 | } |
---|
137 | |
---|
138 | #endif // SCRIPTABLE_CONTROLLER_H |
---|