| 1 | #ifndef SCRIPTABLE_CONTROLLER_API_H |
|---|
| 2 | #define SCRIPTABLE_CONTROLLER_API_H |
|---|
| 3 | |
|---|
| 4 | #include <functional> |
|---|
| 5 | #include "core/CoreIncludes.h" |
|---|
| 6 | #include "tools/Timer.h" |
|---|
| 7 | #include "OgreVector3.h" |
|---|
| 8 | |
|---|
| 9 | struct lua_State; |
|---|
| 10 | |
|---|
| 11 | namespace orxonox |
|---|
| 12 | { |
|---|
| 13 | |
|---|
| 14 | class ScriptableController; |
|---|
| 15 | class WorldEntity; |
|---|
| 16 | |
|---|
| 17 | /** |
|---|
| 18 | * @brief API for ScriptableController's lua-scripts |
|---|
| 19 | * |
|---|
| 20 | * Defines the interface that lua can use in the scripts to communicate with orxonox. |
|---|
| 21 | * |
|---|
| 22 | * \sa ScriptableController |
|---|
| 23 | */ |
|---|
| 24 | class ScriptableControllerAPI |
|---|
| 25 | { |
|---|
| 26 | public: |
|---|
| 27 | /** |
|---|
| 28 | * @brief Constructs the API with the given lua state |
|---|
| 29 | * @param lua The lua state |
|---|
| 30 | * @param controller The parent controller |
|---|
| 31 | * |
|---|
| 32 | * This will not run any scripts, it'll just make the API visible to lua. |
|---|
| 33 | */ |
|---|
| 34 | ScriptableControllerAPI(lua_State *lua, ScriptableController *controller); |
|---|
| 35 | |
|---|
| 36 | /** |
|---|
| 37 | * @brief Destructs the API and closes the lua state. |
|---|
| 38 | */ |
|---|
| 39 | ~ScriptableControllerAPI(); |
|---|
| 40 | |
|---|
| 41 | // --- API ---------------------------------- |
|---|
| 42 | |
|---|
| 43 | /** |
|---|
| 44 | * @brief Print a message |
|---|
| 45 | * @param msg The message |
|---|
| 46 | * |
|---|
| 47 | * Use this function instead of printing from lua directly, because that will mess up the |
|---|
| 48 | * output as it is not synchronized. |
|---|
| 49 | */ |
|---|
| 50 | void orxPrint(std::string msg); |
|---|
| 51 | |
|---|
| 52 | /** |
|---|
| 53 | * @brief Register a function that will be called after a timeout |
|---|
| 54 | * @param callback The function to call after the timeout expired |
|---|
| 55 | * @param timeout The timeout in seconds |
|---|
| 56 | */ |
|---|
| 57 | void registerAfterTimeout(std::function<void (void)> callback, double timeout); |
|---|
| 58 | |
|---|
| 59 | /** |
|---|
| 60 | * @brief Register a function that will be called when two object are close to eachother |
|---|
| 61 | * @param callback The function to call when the objects are close enough |
|---|
| 62 | * @param id1 The first object |
|---|
| 63 | * @param id2 The second object |
|---|
| 64 | * @param distance If the distance between the two objects is smaller than this value, |
|---|
| 65 | * the function is called |
|---|
| 66 | * |
|---|
| 67 | * Note: Distances are only checked every 0.5s! |
|---|
| 68 | */ |
|---|
| 69 | void registerAtNearObject(std::function<void(std::string, std::string)> callback, std::string id1, std::string id2, double distance); |
|---|
| 70 | |
|---|
| 71 | /** |
|---|
| 72 | * @brief Register a function that will be called when an object is close to a certain point |
|---|
| 73 | * @param callback The function to call when the object is close enough |
|---|
| 74 | * @param id The object |
|---|
| 75 | * @param x X-coordinate of the point |
|---|
| 76 | * @param y Y-coordinate of the point |
|---|
| 77 | * @param z Z-coordinate of the point |
|---|
| 78 | * @param distance If the distance between the object and the point is smaller than this value, the |
|---|
| 79 | * function is called. |
|---|
| 80 | * |
|---|
| 81 | * Note: Distances are only checked every 0.5s! |
|---|
| 82 | */ |
|---|
| 83 | void registerAtNearPoint(std::function<void (std::string)> callback, std::string id, double x, double y, double z, double distance); |
|---|
| 84 | |
|---|
| 85 | /** |
|---|
| 86 | * @brief Register a function that will be called when an object enters a cubic area |
|---|
| 87 | * @param callback The function to call when the object entered the area |
|---|
| 88 | * @param id The object |
|---|
| 89 | * @param x X-coordinate of the top-left corner |
|---|
| 90 | * @param y Y-coordinate of the top-left corner |
|---|
| 91 | * @param z Z-coordinate of the top-left corner |
|---|
| 92 | * @param dx Size in X-direction of the cube |
|---|
| 93 | * @param dy Size in Y-direction of the cube |
|---|
| 94 | * @param dz Size in Z-direction of the cube |
|---|
| 95 | * |
|---|
| 96 | * Note: Distances are only checked every 0.5s! |
|---|
| 97 | */ |
|---|
| 98 | void registerAtAreaEnter(std::function<void (std::string)> callback, std::string id, int x, int y, int z, int dx, int dy, int dz); |
|---|
| 99 | |
|---|
| 100 | /** |
|---|
| 101 | * @brief Register a function that will be called when an object leaves a cubic area |
|---|
| 102 | * @param callback The function to call when the object left the area |
|---|
| 103 | * @param id The object |
|---|
| 104 | * @param x X-coordinate of the top-left corner |
|---|
| 105 | * @param y Y-coordinate of the top-left corner |
|---|
| 106 | * @param z Z-coordinate of the top-left corner |
|---|
| 107 | * @param dx Size in X-direction of the cube |
|---|
| 108 | * @param dy Size in Y-direction of the cube |
|---|
| 109 | * @param dz Size in Z-direction of the cube |
|---|
| 110 | * |
|---|
| 111 | * Note: Distances are only checked every 0.5s! |
|---|
| 112 | */ |
|---|
| 113 | void registerAtAreaLeave(std::function<void (std::string)> callback, std::string id, int x, int y, int z, int dx, int dy, int dz); |
|---|
| 114 | |
|---|
| 115 | /** |
|---|
| 116 | * @brief Register a function that will be called when a Pawn is killed |
|---|
| 117 | * @param callback The function to call as soon as the Pawn is dead |
|---|
| 118 | * @param id The Pawn |
|---|
| 119 | * |
|---|
| 120 | * Note: Once a Pawn is dead, the callback is removed, even if the pawn got magically revived. |
|---|
| 121 | */ |
|---|
| 122 | void registerAtPawnKilled(std::function<void (std::string)> callback, std::string id); |
|---|
| 123 | |
|---|
| 124 | /** |
|---|
| 125 | * @brief Register a function that will be called when a Pawn is hit |
|---|
| 126 | * @param callback The function to call as soon as the Pawn is hit |
|---|
| 127 | * @param id The Pawn |
|---|
| 128 | * |
|---|
| 129 | * Note: Once a Pawn is dead, the all hit-callbacks are removed, even if the pawn got magically revived. |
|---|
| 130 | */ |
|---|
| 131 | void registerAtPawnHit(std::function<void (std::string, std::string, double, double)> callback, std::string id); |
|---|
| 132 | |
|---|
| 133 | /** |
|---|
| 134 | * @brief Kill a pawn |
|---|
| 135 | * @param id The pawn to kill |
|---|
| 136 | * |
|---|
| 137 | * Note: It might up to 0.5s until the pawn is actually killed. |
|---|
| 138 | */ |
|---|
| 139 | void killPawn(std::string id); |
|---|
| 140 | |
|---|
| 141 | // ------------------------------------------ |
|---|
| 142 | |
|---|
| 143 | /** |
|---|
| 144 | * @brief Called by ScriptableController when a pawn is killed |
|---|
| 145 | * @param id The dead pawn |
|---|
| 146 | * |
|---|
| 147 | * Calls the lua callbacks associated with this event. |
|---|
| 148 | */ |
|---|
| 149 | void pawnKilled(std::string id); |
|---|
| 150 | |
|---|
| 151 | /** |
|---|
| 152 | * @brief Called by ScriptableController when a Pawn is hit |
|---|
| 153 | * @param target_id The hit Pawn |
|---|
| 154 | * @param source_id The shooting Pawn |
|---|
| 155 | * @param new_health The new health of the hit Pawn |
|---|
| 156 | * @param new_shield The new shield health of the hit Pawn |
|---|
| 157 | */ |
|---|
| 158 | void pawnHit(std::string target_id, std::string source_id, double new_health, double new_shield); |
|---|
| 159 | |
|---|
| 160 | private: |
|---|
| 161 | /** |
|---|
| 162 | * @brief Groups everything together that is needed to handle a near-object event |
|---|
| 163 | */ |
|---|
| 164 | struct NearObjectHandler |
|---|
| 165 | { |
|---|
| 166 | NearObjectHandler(WorldEntity *entity1, WorldEntity *entity2, std::string id1, std::string id2, double distance, std::function<void (std::string, std::string)> callback) |
|---|
| 167 | : entity1_(entity1), entity2_(entity2), id1_(id1), id2_(id2), distance_(distance), callback_(callback) |
|---|
| 168 | {} |
|---|
| 169 | |
|---|
| 170 | WorldEntity *entity1_, *entity2_; |
|---|
| 171 | std::string id1_, id2_; |
|---|
| 172 | double distance_; |
|---|
| 173 | std::function<void (std::string, std::string)> callback_; |
|---|
| 174 | }; |
|---|
| 175 | |
|---|
| 176 | /** |
|---|
| 177 | * @brief Groups everything together that is needed to handle a near-poinb event |
|---|
| 178 | */ |
|---|
| 179 | struct NearPointHandler |
|---|
| 180 | { |
|---|
| 181 | NearPointHandler(WorldEntity *entity, std::string id, double x, double y, double z, double distance, std::function<void (std::string)> callback) |
|---|
| 182 | : entity_(entity), id_(id), point_(x, y, z), distance_(distance), callback_(callback) |
|---|
| 183 | {} |
|---|
| 184 | |
|---|
| 185 | WorldEntity *entity_; |
|---|
| 186 | std::string id_; |
|---|
| 187 | Vector3 point_; |
|---|
| 188 | double distance_; |
|---|
| 189 | std::function<void (std::string)> callback_; |
|---|
| 190 | }; |
|---|
| 191 | |
|---|
| 192 | /** |
|---|
| 193 | * @brief Groups everything together that is needed to handle an area enter/leave event |
|---|
| 194 | */ |
|---|
| 195 | struct AreaHandler |
|---|
| 196 | { |
|---|
| 197 | AreaHandler(WorldEntity *entity, std::string id, double x, double y, double z, double dx, double dy, double dz, bool atEnter, std::function<void (std::string)> callback) |
|---|
| 198 | : entity_(entity), id_(id), start_point_(x, y, z), atEnter_(atEnter), callback_(callback) |
|---|
| 199 | { this-> end_point_ = this->start_point_ + Vector3(dx, dy, dz); } |
|---|
| 200 | |
|---|
| 201 | WorldEntity *entity_; |
|---|
| 202 | std::string id_; |
|---|
| 203 | Vector3 start_point_, end_point_; |
|---|
| 204 | bool atEnter_; |
|---|
| 205 | std::function<void (std::string)> callback_; |
|---|
| 206 | }; |
|---|
| 207 | |
|---|
| 208 | |
|---|
| 209 | lua_State *lua_; |
|---|
| 210 | ScriptableController *controller_; |
|---|
| 211 | std::list<NearObjectHandler> nearObjectHandlers_; |
|---|
| 212 | std::list<NearPointHandler> nearPointHandlers_; |
|---|
| 213 | std::list<AreaHandler> areaHandlers_; |
|---|
| 214 | std::list<std::string> pawnsToKill_; |
|---|
| 215 | std::map<std::string, std::list<std::function<void (std::string)> > > pawnDestroyedHandlers_; |
|---|
| 216 | std::map<std::string, std::list<std::function<void (std::string, std::string, double, double)> > > pawnHitHandlers_; |
|---|
| 217 | Timer periodicTimer; |
|---|
| 218 | static const double periodic_interval; |
|---|
| 219 | |
|---|
| 220 | /** |
|---|
| 221 | * @brief Called every 0.5s |
|---|
| 222 | * |
|---|
| 223 | * This handles things that have to be checked periodically (like area events) and |
|---|
| 224 | * things that are done by lua that have to synchronize with the main thread (like |
|---|
| 225 | * killing a pawn). Doing this in every tick would be an overkill. |
|---|
| 226 | */ |
|---|
| 227 | void periodic(void); |
|---|
| 228 | }; |
|---|
| 229 | |
|---|
| 230 | } |
|---|
| 231 | |
|---|
| 232 | #endif // SCRIPTABLE_CONTROLLER_API_H |
|---|