]> git.lizzy.rs Git - minetest.git/blob - src/clientenvironment.h
c8b192ee99b889d972bdd60c9d0f98525bddb674
[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 typedef std::unordered_map<u16, ClientActiveObject*> ClientActiveObjectMap;
68 class ClientEnvironment : public Environment
69 {
70 public:
71         ClientEnvironment(ClientMap *map, scene::ISceneManager *smgr,
72                 ITextureSource *texturesource, Client *client,
73                 IrrlichtDevice *device);
74         ~ClientEnvironment();
75
76         Map & getMap();
77         ClientMap & getClientMap();
78
79         Client *getGameDef() { return m_client; }
80         void setScript(ClientScripting *script) { m_script = script; }
81
82         void step(f32 dtime);
83
84         virtual void setLocalPlayer(LocalPlayer *player);
85         LocalPlayer *getLocalPlayer() { return m_local_player; }
86
87         /*
88                 ClientSimpleObjects
89         */
90
91         void addSimpleObject(ClientSimpleObject *simple);
92
93         /*
94                 ActiveObjects
95         */
96
97         GenericCAO* getGenericCAO(u16 id);
98         ClientActiveObject* getActiveObject(u16 id);
99
100         /*
101                 Adds an active object to the environment.
102                 Environment handles deletion of object.
103                 Object may be deleted by environment immediately.
104                 If id of object is 0, assigns a free id to it.
105                 Returns the id of the object.
106                 Returns 0 if not added and thus deleted.
107         */
108         u16 addActiveObject(ClientActiveObject *object);
109
110         void addActiveObject(u16 id, u8 type, const std::string &init_data);
111         void removeActiveObject(u16 id);
112
113         void processActiveObjectMessage(u16 id, const std::string &data);
114
115         /*
116                 Callbacks for activeobjects
117         */
118
119         void damageLocalPlayer(u8 damage, bool handle_hp=true);
120         void updateLocalPlayerBreath(u16 breath);
121
122         /*
123                 Client likes to call these
124         */
125
126         // Get all nearby objects
127         void getActiveObjects(v3f origin, f32 max_d,
128                 std::vector<DistanceSortedActiveObject> &dest);
129
130         bool hasClientEnvEvents() const { return !m_client_event_queue.empty(); }
131         // Get event from queue. If queue is empty, it triggers an assertion failure.
132         ClientEnvEvent getClientEnvEvent();
133
134         /*!
135          * Gets closest object pointed by the shootline.
136          * Returns NULL if not found.
137          *
138          * \param[in]  shootline_on_map    the shootline for
139          * the test in world coordinates
140          * \param[out] intersection_point  the first point where
141          * the shootline meets the object. Valid only if
142          * not NULL is returned.
143          * \param[out] intersection_normal the normal vector of
144          * the intersection, pointing outwards. Zero vector if
145          * the shootline starts in an active object.
146          * Valid only if not NULL is returned.
147          */
148         ClientActiveObject * getSelectedActiveObject(
149                 const core::line3d<f32> &shootline_on_map,
150                 v3f *intersection_point,
151                 v3s16 *intersection_normal
152         );
153
154         /*!
155          * Performs a raycast on the world.
156          * Returns the first thing the shootline meets.
157          *
158          * @param[in]  shootline         the shootline, starting from
159          * the camera position. This also gives the maximal distance
160          * of the search.
161          * @param[in]  liquids_pointable if false, liquids are ignored
162          * @param[in]  look_for_object   if false, objects are ignored
163          */
164         PointedThing getPointedThing(
165                 core::line3d<f32> shootline,
166                 bool liquids_pointable,
167                 bool look_for_object);
168
169         u16 attachement_parent_ids[USHRT_MAX + 1];
170
171         const std::list<std::string> &getPlayerNames() { return m_player_names; }
172         void addPlayerName(const std::string &name) { m_player_names.push_back(name); }
173         void removePlayerName(const std::string &name) { m_player_names.remove(name); }
174         void updateCameraOffset(v3s16 camera_offset)
175         { m_camera_offset = camera_offset; }
176         v3s16 getCameraOffset() const { return m_camera_offset; }
177 private:
178         ClientMap *m_map;
179         LocalPlayer *m_local_player = nullptr;
180         scene::ISceneManager *m_smgr;
181         ITextureSource *m_texturesource;
182         Client *m_client;
183         ClientScripting *m_script = nullptr;
184         IrrlichtDevice *m_irr;
185         ClientActiveObjectMap m_active_objects;
186         std::vector<ClientSimpleObject*> m_simple_objects;
187         std::queue<ClientEnvEvent> m_client_event_queue;
188         IntervalLimiter m_active_object_light_update_interval;
189         IntervalLimiter m_lava_hurt_interval;
190         IntervalLimiter m_drowning_interval;
191         IntervalLimiter m_breathing_interval;
192         std::list<std::string> m_player_names;
193         v3s16 m_camera_offset;
194 };
195
196 #endif