Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/ScriptableController_FS18/src/orxonox/scriptablecontroller/scriptable_controller.h @ 11974

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

can

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