Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/ScriptableController_HS17/src/orxonox/scriptablecontroller/scriptable_controller.h @ 11654

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

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

File size: 4.2 KB
Line 
1#ifndef SCRIPTABLE_CONTROLLER_H
2#define SCRIPTABLE_CONTROLLER_H
3
4#include <lua.hpp>
5#include <string>
6#include <list>
7#include <map>
8#include <memory>
9#include "scriptable_controller_api.h"
10#include "core/CoreIncludes.h"
11#include "worldentities/WorldEntity.h"
12#include "worldentities/ControllableEntity.h"
13#include "tools/Timer.h"
14
15struct lua_State;
16
17namespace orxonox
18{
19
20/**
21 * @brief Runs a scripts on a per-level basis and handles the connection to orxonox
22 *
23 * The script is an attribute of the <Level> element with the name 'script' and should be
24 * the path to a lua script. The script will be run as soon as the player spawns. It can
25 * then register on various events and react to them. See ScriptableControllerAPI for
26 * the complete API.
27 *
28 * \sa ScriptableControllerAPI
29 */
30class ScriptableController
31{
32public:
33    /**
34     * @brief Run a lua script
35     * @param file_path Path to the script
36     * @return A lua error code (0 for success)
37     *
38     * Constructs an API for the script and runs it.
39     */
40    int runScript(const std::string &file_path);
41
42    /**
43     * @brief Set the player object of the current game
44     * @param player The player
45     *
46     * The player is a special object and can perfom special actions.
47     */
48    void setPlayer(PlayerInfo *player);
49
50    /**
51     * @brief Register a WorldEntity to the ScriptableController
52     * @param id The ID of the WorldEntity
53     * @param entity The WorldEntity
54     *
55     * The ScriptableController needs a list of all WorldEntity's so it can
56     * convert an ID to a WorldEntity.
57     */
58    void registerWorldEntity(std::string id, WorldEntity *entity);
59
60    /**
61     * @brief Register a MobileEntity to the ScriptableController
62     * @param id The ID of the MobileEntity
63     * @param entity The MobileEntity
64     *
65     * The ScriptableController needs a list of all MobileEntity's so it can
66     * convert an ID to a MobileEntity.
67     */
68    void registerMobileEntity(std::string id, MobileEntity *entity);
69
70    /**
71     * @brief Register a Pawn to the ScriptableController
72     * @param id The ID of the Pawn
73     * @param pawn The Pawn
74     *
75     * The ScriptableController needs a list of all Pawn's in addition to
76     * the WorldEntity's, because they have additional actions available.
77     */
78    void registerPawn(std::string id, Pawn *pawn);
79
80    /**
81     * @brief Called when a Pawn is killed
82     * @param pawn The Pawn
83     *
84     * Called by the Pawn itself as soon as it's killed.
85     */
86    void pawnKilled(Pawn *pawn);
87
88    /**
89     * @brief Called when a Pawn is hit
90     * @param target The hit Pawn
91     * @param source The shooting Pawn
92     * @param new_health The new health of the hit Pawn
93     * @param new_shield The new shield health of the hit Pawn
94     *
95     * Called by the Pawn itself as soon as it's hit.
96     */
97    void pawnHit(Pawn *target, Pawn *source, double new_health, double new_shield);
98
99    /**
100     * @brief Kill a Pawn
101     * @param id The Pawn to kill
102     */
103    void killPawn(std::string id);
104
105    /**
106     * @brief Convert an ID to a WorldEntity pointer
107     * @param id The ID of the WorldEntity
108     * @return A pointer to the WorldEntity, nullptr if it's not found
109     */
110    WorldEntity *getWorldEntityByID(std::string id) const;
111
112    /**
113     * @brief Convert an ID to a MobileEntity pointer
114     * @param id The ID of the MobileEntity
115     * @return A pointer to the MobileEntity, nullptr if it's not found
116     */
117    MobileEntity *getMobileEntityByID(std::string id) const;
118
119    /**
120     * @brief Convert an ID to a Pawt pointer
121     * @param id The ID of the Pawn
122     * @return A pointer to the Pawn, nullptr if it's not found
123     */
124    Pawn *getPawnByID(std::string id) const;
125
126private:
127    std::list<std::unique_ptr<ScriptableControllerAPI> > apis_;
128    PlayerInfo *player_;
129    std::map<std::string, WorldEntity*> worldEntities_;
130    std::map<std::string, MobileEntity*> mobileEntities_;
131    std::map<std::string, Pawn*> pawns_;
132    std::map<Pawn*, std::string> pawnsReverse_;
133    std::map<std::string, ControllableEntity*> controllabelEntities_;
134
135    /**
136     * @brief Prints a human readable error message if a lua error occurs
137     * @param lua The lua state where the error occured
138     */
139    void printLuaError(lua_State *lua);
140};
141
142}
143
144#endif // SCRIPTABLE_CONTROLLER_H
Note: See TracBrowser for help on using the repository browser.