]> git.lizzy.rs Git - minetest.git/blob - src/clientenvironment.h
Client & ClientEnvirnment: don't create fake events (#5676)
[minetest.git] / src / clientenvironment.h
1 /*
2 Minetest
3 Copyright (C) 2010-2017 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #ifndef CLIENT_ENVIRONMENT_HEADER
21 #define CLIENT_ENVIRONMENT_HEADER
22
23 #include <IrrlichtDevice.h>
24 #include <ISceneManager.h>
25 #include "environment.h"
26 #include "clientobject.h"
27
28 class ClientSimpleObject;
29 class ClientMap;
30 class ClientScripting;
31 class ClientActiveObject;
32 class GenericCAO;
33 class LocalPlayer;
34 struct PointedThing;
35
36 /*
37         The client-side environment.
38
39         This is not thread-safe.
40         Must be called from main (irrlicht) thread (uses the SceneManager)
41         Client uses an environment mutex.
42 */
43
44 enum ClientEnvEventType
45 {
46         CEE_NONE,
47         CEE_PLAYER_DAMAGE,
48         CEE_PLAYER_BREATH
49 };
50
51 struct ClientEnvEvent
52 {
53         ClientEnvEventType type;
54         union {
55                 //struct{
56                 //} none;
57                 struct{
58                         u8 amount;
59                         bool send_to_server;
60                 } player_damage;
61                 struct{
62                         u16 amount;
63                 } player_breath;
64         };
65 };
66
67 class ClientEnvironment : public Environment
68 {
69 public:
70         ClientEnvironment(ClientMap *map, scene::ISceneManager *smgr,
71                 ITextureSource *texturesource, Client *client,
72                 IrrlichtDevice *device);
73         ~ClientEnvironment();
74
75         Map & getMap();
76         ClientMap & getClientMap();
77
78         Client *getGameDef() { return m_client; }
79         void setScript(ClientScripting *script) { m_script = script; }
80
81         void step(f32 dtime);
82
83         virtual void setLocalPlayer(LocalPlayer *player);
84         LocalPlayer *getLocalPlayer() { return m_local_player; }
85
86         /*
87                 ClientSimpleObjects
88         */
89
90         void addSimpleObject(ClientSimpleObject *simple);
91
92         /*
93                 ActiveObjects
94         */
95
96         GenericCAO* getGenericCAO(u16 id);
97         ClientActiveObject* getActiveObject(u16 id);
98
99         /*
100                 Adds an active object to the environment.
101                 Environment handles deletion of object.
102                 Object may be deleted by environment immediately.
103                 If id of object is 0, assigns a free id to it.
104                 Returns the id of the object.
105                 Returns 0 if not added and thus deleted.
106         */
107         u16 addActiveObject(ClientActiveObject *object);
108
109         void addActiveObject(u16 id, u8 type, const std::string &init_data);
110         void removeActiveObject(u16 id);
111
112         void processActiveObjectMessage(u16 id, const std::string &data);
113
114         /*
115                 Callbacks for activeobjects
116         */
117
118         void damageLocalPlayer(u8 damage, bool handle_hp=true);
119         void updateLocalPlayerBreath(u16 breath);
120
121         /*
122                 Client likes to call these
123         */
124
125         // Get all nearby objects
126         void getActiveObjects(v3f origin, f32 max_d,
127                 std::vector<DistanceSortedActiveObject> &dest);
128
129         bool hasClientEnvEvents() const { return !m_client_event_queue.empty(); }
130         // Get event from queue. If queue is empty, it triggers an assertion failure.
131         ClientEnvEvent getClientEnvEvent();
132
133         /*!
134          * Gets closest object pointed by the shootline.
135          * Returns NULL if not found.
136          *
137          * \param[in]  shootline_on_map    the shootline for
138          * the test in world coordinates
139          * \param[out] intersection_point  the first point where
140          * the shootline meets the object. Valid only if
141          * not NULL is returned.
142          * \param[out] intersection_normal the normal vector of
143          * the intersection, pointing outwards. Zero vector if
144          * the shootline starts in an active object.
145          * Valid only if not NULL is returned.
146          */
147         ClientActiveObject * getSelectedActiveObject(
148                 const core::line3d<f32> &shootline_on_map,
149                 v3f *intersection_point,
150                 v3s16 *intersection_normal
151         );
152
153         /*!
154          * Performs a raycast on the world.
155          * Returns the first thing the shootline meets.
156          *
157          * @param[in]  shootline         the shootline, starting from
158          * the camera position. This also gives the maximal distance
159          * of the search.
160          * @param[in]  liquids_pointable if false, liquids are ignored
161          * @param[in]  look_for_object   if false, objects are ignored
162          */
163         PointedThing getPointedThing(
164                 core::line3d<f32> shootline,
165                 bool liquids_pointable,
166                 bool look_for_object);
167
168         u16 attachement_parent_ids[USHRT_MAX + 1];
169
170         const std::list<std::string> &getPlayerNames() { return m_player_names; }
171         void addPlayerName(const std::string &name) { m_player_names.push_back(name); }
172         void removePlayerName(const std::string &name) { m_player_names.remove(name); }
173         void updateCameraOffset(v3s16 camera_offset)
174         { m_camera_offset = camera_offset; }
175         v3s16 getCameraOffset() const { return m_camera_offset; }
176 private:
177         ClientMap *m_map;
178         LocalPlayer *m_local_player;
179         scene::ISceneManager *m_smgr;
180         ITextureSource *m_texturesource;
181         Client *m_client;
182         ClientScripting *m_script;
183         IrrlichtDevice *m_irr;
184         UNORDERED_MAP<u16, ClientActiveObject*> m_active_objects;
185         std::vector<ClientSimpleObject*> m_simple_objects;
186         std::queue<ClientEnvEvent> m_client_event_queue;
187         IntervalLimiter m_active_object_light_update_interval;
188         IntervalLimiter m_lava_hurt_interval;
189         IntervalLimiter m_drowning_interval;
190         IntervalLimiter m_breathing_interval;
191         std::list<std::string> m_player_names;
192         v3s16 m_camera_offset;
193 };
194
195 #endif