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