Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/ScriptableController_HS17/src/orxonox/scriptablecontroller/scriptable_controller_api.h @ 12206

Last change on this file since 12206 was 11854, checked in by adamc, 7 years ago

scriptablecontroller testlevel fixed

File size: 10.7 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    /**
155     * @brief Set the position of an object
156     * @param id The ID of the object
157     * @param x The position on the x-axis
158     * @param y The position on the y-axis
159     * @param z The position on the z-axis
160     */
161    void setPosition(std::string id, double x, double y, double z);
162
163    /**
164     * @brief Set the orientation of an object
165     * @param id The ID of the object
166     * @param x The x component of the axis vector
167     * @param y The y component of the axis vector
168     * @param z The z component of the axis vector
169     * @param angle The angle around the axis
170     *
171     * To set the orientation, you have to specify the direction that the
172     * object should be facing with the vector (x, y, z) and the rotation
173     * of the object around this axis with 'angle', which has to be given
174     * in degrees, NOT radian. The vector does not have to be normalized.
175     */
176    void setOrientation(std::string id, double x, double y, double z, double angle);
177
178    /**
179     * @brief Set the velocity of an object
180     * @param id The ID of the object
181     * @param x The velocity in x-direction
182     * @param y The velocity in y-direction
183     * @param z The velocity in z-direction
184     *
185     * The velocity is in units per second.
186     */
187    void setVelocity(std::string id, double x, double y, double z);
188
189    /**
190     * @brief Set the angular velocity of an object
191     * @param id The ID of the object
192     * @param x The rotation velocity around the x-axis
193     * @param y The rotation velocity around the y-axis
194     * @param z The rotation velocity around the z-axis
195     */
196    void setAngularVelocity(std::string id, double x, double y, double z);
197
198
199    /**
200     * @brief Set the angular velocity of an object
201     * @param id The ID of the object
202     * @param x The rotation velocity around the x-axis
203     * @param y The rotation velocity around the y-axis
204     * @param z The rotation velocity around the z-axis
205     */
206    double myTestFunction(double x, double y);
207
208// ### API END ################################################################
209
210private:
211    /**
212     * @brief Groups everything together that is needed to handle a near-object event
213     */
214    struct NearObjectHandler
215    {
216        NearObjectHandler(WorldEntity *entity1, WorldEntity *entity2, std::string id1, std::string id2, double distance, std::function<void (std::string, std::string)> callback)
217            : entity1_(entity1), entity2_(entity2), id1_(id1), id2_(id2), distance_(distance), callback_(callback)
218        {}
219
220        WorldEntity *entity1_, *entity2_;
221        std::string id1_, id2_;
222        double distance_;
223        std::function<void (std::string, std::string)> callback_;
224    };
225
226    /**
227     * @brief Groups everything together that is needed to handle a near-poinb event
228     */
229    struct NearPointHandler
230    {
231        NearPointHandler(WorldEntity *entity, std::string id, double x, double y, double z, double distance, std::function<void (std::string)> callback)
232            : entity_(entity), id_(id), point_(x, y, z), distance_(distance), callback_(callback)
233        {}
234
235        WorldEntity *entity_;
236        std::string id_;
237        Vector3 point_;
238        double distance_;
239        std::function<void (std::string)> callback_;
240    };
241
242    /**
243     * @brief Groups everything together that is needed to handle an area enter/leave event
244     */
245    struct AreaHandler
246    {
247        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)
248            : entity_(entity), id_(id), start_point_(x, y, z), atEnter_(atEnter), callback_(callback)
249        { this-> end_point_ = this->start_point_ + Vector3(dx, dy, dz); }
250
251        WorldEntity *entity_;
252        std::string id_;
253        Vector3 start_point_, end_point_;
254        bool atEnter_;
255        std::function<void (std::string)> callback_;
256    };
257
258    lua_State *lua_;
259    ScriptableController *controller_;
260    std::list<NearObjectHandler> nearObjectHandlers_;
261    std::list<NearPointHandler> nearPointHandlers_;
262    std::list<AreaHandler> areaHandlers_;
263    std::map<std::string, std::list<std::function<void (std::string)> > > pawnDestroyedHandlers_;
264    std::map<std::string, std::list<std::function<void (std::string, std::string, double, double)> > > pawnHitHandlers_;
265    Timer periodicTimer;
266    static const double periodic_interval;
267
268    /**
269     * @brief Called by ScriptableController when a pawn is killed
270     * @param id The dead pawn
271     *
272     * Calls the lua callbacks associated with this event.
273     */
274    void pawnKilled(std::string id, Pawn *pawn);
275
276    /**
277     * @brief Called by ScriptableController when a Pawn is hit
278     * @param target_id The hit Pawn
279     * @param source_id The shooting Pawn
280     * @param new_health The new health of the hit Pawn
281     * @param new_shield The new shield health of the hit Pawn
282     */
283    void pawnHit(std::string target_id, std::string source_id, double new_health, double new_shield);
284
285    /**
286     * @brief Called every 0.5s
287     *
288     * This handles things that have to be checked periodically (like area events)
289     * but doing this in every tick would be an overkill.
290     */
291    void periodic(void);
292};
293
294}
295
296#endif // SCRIPTABLE_CONTROLLER_API_H
Note: See TracBrowser for help on using the repository browser.