Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 11638 was 11638, checked in by kohlia, 6 years ago

Position/velocity setting works now, relative script paths not, added test script

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