Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/BossFight_FS19/src/orxonox/scriptablecontroller/scriptable_controller_api.h @ 12373

Last change on this file since 12373 was 11974, checked in by adamc, 7 years ago

can

File size: 10.8 KB
Line 
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
9struct lua_State;
10
11namespace orxonox
12{
13
14class ScriptableController;
15class WorldEntity;
16class Pawn;
17
18/**
19 * @brief API for ScriptableController's lua-scripts
20 *
21 * Defines the interface that lua can use in the scripts to communicate with orxonox.
22 *
23 * \sa ScriptableController
24 */
25class ScriptableControllerAPI
26{
27friend class ScriptableController;
28
29public:
30    /**
31     * @brief Constructs the API with the given lua state
32     * @param lua The lua state
33     * @param controller The parent controller
34     *
35     * This will not run any scripts, it'll just make the API visible to lua.
36     */
37    ScriptableControllerAPI(lua_State *lua, ScriptableController *controller);
38
39    /**
40     * @brief Destructs the API and closes the lua state.
41     */
42    ~ScriptableControllerAPI();
43
44// ### API ####################################################################
45
46    /**
47     * @brief Print a message
48     * @param msg The message
49     *
50     * Use this function instead of printing from lua directly, because that will mess up the
51     * output as it is not synchronized.
52     */
53    void orxPrint(std::string msg);
54
55    /**
56     * @brief Register a function that will be called after a timeout
57     * @param callback The function to call after the timeout expired
58     * @param timeout The timeout in seconds
59     */
60    void registerAfterTimeout(std::function<void (void)> callback, double timeout);
61
62    /**
63     * @brief Register a function that will be called when two object are close to eachother
64     * @param callback The function to call when the objects are close enough
65     * @param id1 The first object
66     * @param id2 The second object
67     * @param distance If the distance between the two objects is smaller than this value,
68     * the function is called
69     *
70     * Note: Distances are only checked every 0.5s!
71     */
72    void registerAtNearObject(std::function<void(std::string, std::string)> callback, std::string id1, std::string id2, double distance);
73
74    /**
75     * @brief Register a function that will be called when an object is close to a certain point
76     * @param callback The function to call when the object is close enough
77     * @param id The object
78     * @param x X-coordinate of the point
79     * @param y Y-coordinate of the point
80     * @param z Z-coordinate of the point
81     * @param distance If the distance between the object and the point is smaller than this value, the
82     * function is called.
83     *
84     * Note: Distances are only checked every 0.5s!
85     */
86    void registerAtNearPoint(std::function<void (std::string)> callback, std::string id, double x, double y, double z, double distance);
87
88    /**
89     * @brief Register a function that will be called when an object enters a cubic area
90     * @param callback The function to call when the object entered the area
91     * @param id The object
92     * @param x X-coordinate of the top-left corner
93     * @param y Y-coordinate of the top-left corner
94     * @param z Z-coordinate of the top-left corner
95     * @param dx Size in X-direction of the cube
96     * @param dy Size in Y-direction of the cube
97     * @param dz Size in Z-direction of the cube
98     *
99     * Note: Distances are only checked every 0.5s!
100     */
101    void registerAtAreaEnter(std::function<void (std::string)> callback, std::string id, int x, int y, int z, int dx, int dy, int dz);
102
103    /**
104     * @brief Register a function that will be called when an object leaves a cubic area
105     * @param callback The function to call when the object left the area
106     * @param id The object
107     * @param x X-coordinate of the top-left corner
108     * @param y Y-coordinate of the top-left corner
109     * @param z Z-coordinate of the top-left corner
110     * @param dx Size in X-direction of the cube
111     * @param dy Size in Y-direction of the cube
112     * @param dz Size in Z-direction of the cube
113     *
114     * Note: Distances are only checked every 0.5s!
115     */
116    void registerAtAreaLeave(std::function<void (std::string)> callback, std::string id, int x, int y, int z, int dx, int dy, int dz);
117
118    /**
119     * @brief Register a function that will be called when a Pawn is killed
120     * @param callback The function to call as soon as the Pawn is dead
121     * @param id The Pawn
122     *
123     * Note: Once a Pawn is dead, the callback is removed, even if the pawn got magically revived.
124     */
125    void registerAtPawnKilled(std::function<void (std::string)> callback, std::string id);
126
127    /**
128     * @brief Register a function that will be called when a Pawn is hit
129     * @param callback The function to call as soon as the Pawn is hit
130     * @param id The Pawn
131     *
132     * Note: Once a Pawn is dead, the all hit-callbacks are removed, even if the pawn got magically revived.
133     */
134    void registerAtPawnHit(std::function<void (std::string, std::string, double, double)> callback, std::string id);
135
136    /**
137     * @brief Kill a pawn
138     * @param id The pawn to kill
139     *
140     * Note: It might up to 0.5s until the pawn is actually killed.
141     */
142    void killPawn(std::string id);
143
144    /**
145     * @brief Spawn an object
146     * @param type Name of the class of the object you want to spawn
147     * @param id The newly created ID that can be used to access this object
148     *
149     * IMPORTANT: Do not use this function yet, it only has minimal functionality and is not
150     * really helpful as it is.
151     */
152    void spawn(std::string type, std::string id);
153
154    void spawnTest(std::string id);
155
156    /**
157     * @brief Set the position of an object
158     * @param id The ID of the object
159     * @param x The position on the x-axis
160     * @param y The position on the y-axis
161     * @param z The position on the z-axis
162     */
163    void setPosition(std::string id, double x, double y, double z);
164
165    /**
166     * @brief Set the orientation of an object
167     * @param id The ID of the object
168     * @param x The x component of the axis vector
169     * @param y The y component of the axis vector
170     * @param z The z component of the axis vector
171     * @param angle The angle around the axis
172     *
173     * To set the orientation, you have to specify the direction that the
174     * object should be facing with the vector (x, y, z) and the rotation
175     * of the object around this axis with 'angle', which has to be given
176     * in degrees, NOT radian. The vector does not have to be normalized.
177     */
178    void setOrientation(std::string id, double x, double y, double z, double angle);
179
180    /**
181     * @brief Set the velocity of an object
182     * @param id The ID of the object
183     * @param x The velocity in x-direction
184     * @param y The velocity in y-direction
185     * @param z The velocity in z-direction
186     *
187     * The velocity is in units per second.
188     */
189    void setVelocity(std::string id, double x, double y, double z);
190
191    /**
192     * @brief Set the angular velocity of an object
193     * @param id The ID of the object
194     * @param x The rotation velocity around the x-axis
195     * @param y The rotation velocity around the y-axis
196     * @param z The rotation velocity around the z-axis
197     */
198    void setAngularVelocity(std::string id, double x, double y, double z);
199
200
201    /**
202     * @brief Set the angular velocity of an object
203     * @param id The ID of the object
204     * @param x The rotation velocity around the x-axis
205     * @param y The rotation velocity around the y-axis
206     * @param z The rotation velocity around the z-axis
207     */
208    double myTestFunction(double x, double y);
209
210    void moveControllableEntity(std::string id, double x, double y, double z);
211
212// ### API END ################################################################
213
214private:
215    /**
216     * @brief Groups everything together that is needed to handle a near-object event
217     */
218    struct NearObjectHandler
219    {
220        NearObjectHandler(WorldEntity *entity1, WorldEntity *entity2, std::string id1, std::string id2, double distance, std::function<void (std::string, std::string)> callback)
221            : entity1_(entity1), entity2_(entity2), id1_(id1), id2_(id2), distance_(distance), callback_(callback)
222        {}
223
224        WorldEntity *entity1_, *entity2_;
225        std::string id1_, id2_;
226        double distance_;
227        std::function<void (std::string, std::string)> callback_;
228    };
229
230    /**
231     * @brief Groups everything together that is needed to handle a near-poinb event
232     */
233    struct NearPointHandler
234    {
235        NearPointHandler(WorldEntity *entity, std::string id, double x, double y, double z, double distance, std::function<void (std::string)> callback)
236            : entity_(entity), id_(id), point_(x, y, z), distance_(distance), callback_(callback)
237        {}
238
239        WorldEntity *entity_;
240        std::string id_;
241        Vector3 point_;
242        double distance_;
243        std::function<void (std::string)> callback_;
244    };
245
246    /**
247     * @brief Groups everything together that is needed to handle an area enter/leave event
248     */
249    struct AreaHandler
250    {
251        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)
252            : entity_(entity), id_(id), start_point_(x, y, z), atEnter_(atEnter), callback_(callback)
253        { this-> end_point_ = this->start_point_ + Vector3(dx, dy, dz); }
254
255        WorldEntity *entity_;
256        std::string id_;
257        Vector3 start_point_, end_point_;
258        bool atEnter_;
259        std::function<void (std::string)> callback_;
260    };
261
262    lua_State *lua_;
263    ScriptableController *controller_;
264    std::list<NearObjectHandler> nearObjectHandlers_;
265    std::list<NearPointHandler> nearPointHandlers_;
266    std::list<AreaHandler> areaHandlers_;
267    std::map<std::string, std::list<std::function<void (std::string)> > > pawnDestroyedHandlers_;
268    std::map<std::string, std::list<std::function<void (std::string, std::string, double, double)> > > pawnHitHandlers_;
269    Timer periodicTimer;
270    static const double periodic_interval;
271
272    /**
273     * @brief Called by ScriptableController when a pawn is killed
274     * @param id The dead pawn
275     *
276     * Calls the lua callbacks associated with this event.
277     */
278    void pawnKilled(std::string id, Pawn *pawn);
279
280    /**
281     * @brief Called by ScriptableController when a Pawn is hit
282     * @param target_id The hit Pawn
283     * @param source_id The shooting Pawn
284     * @param new_health The new health of the hit Pawn
285     * @param new_shield The new shield health of the hit Pawn
286     */
287    void pawnHit(std::string target_id, std::string source_id, double new_health, double new_shield);
288
289    /**
290     * @brief Called every 0.5s
291     *
292     * This handles things that have to be checked periodically (like area events)
293     * but doing this in every tick would be an overkill.
294     */
295    void periodic(void);
296};
297
298}
299
300#endif // SCRIPTABLE_CONTROLLER_API_H
Note: See TracBrowser for help on using the repository browser.