Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/Boxhead_FS19/src/orxonox/scriptablecontroller/scriptable_controller_api.h @ 12394

Last change on this file since 12394 was 12390, checked in by cwaupoti, 6 years ago

Zombie can now approach, tried to add waves

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