]> git.lizzy.rs Git - dragonfireclient.git/blob - src/clientenvironment.h
Cleanup various headers to reduce compilation times (#6255)
[dragonfireclient.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 #include "util/numeric.h"
27
28 class ClientSimpleObject;
29 class ClientMap;
30 class ClientScripting;
31 class ClientActiveObject;
32 class GenericCAO;
33 class LocalPlayer;
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, ITextureSource *texturesource, Client *client);
71         ~ClientEnvironment();
72
73         Map & getMap();
74         ClientMap & getClientMap();
75
76         Client *getGameDef() { return m_client; }
77         void setScript(ClientScripting *script) { m_script = script; }
78
79         void step(f32 dtime);
80
81         virtual void setLocalPlayer(LocalPlayer *player);
82         LocalPlayer *getLocalPlayer() { return m_local_player; }
83
84         /*
85                 ClientSimpleObjects
86         */
87
88         void addSimpleObject(ClientSimpleObject *simple);
89
90         /*
91                 ActiveObjects
92         */
93
94         GenericCAO* getGenericCAO(u16 id);
95         ClientActiveObject* getActiveObject(u16 id);
96
97         /*
98                 Adds an active object to the environment.
99                 Environment handles deletion of object.
100                 Object may be deleted by environment immediately.
101                 If id of object is 0, assigns a free id to it.
102                 Returns the id of the object.
103                 Returns 0 if not added and thus deleted.
104         */
105         u16 addActiveObject(ClientActiveObject *object);
106
107         void addActiveObject(u16 id, u8 type, const std::string &init_data);
108         void removeActiveObject(u16 id);
109
110         void processActiveObjectMessage(u16 id, const std::string &data);
111
112         /*
113                 Callbacks for activeobjects
114         */
115
116         void damageLocalPlayer(u8 damage, bool handle_hp=true);
117         void updateLocalPlayerBreath(u16 breath);
118
119         /*
120                 Client likes to call these
121         */
122
123         // Get all nearby objects
124         void getActiveObjects(v3f origin, f32 max_d,
125                 std::vector<DistanceSortedActiveObject> &dest);
126
127         bool hasClientEnvEvents() const { return !m_client_event_queue.empty(); }
128
129         // Get event from queue. If queue is empty, it triggers an assertion failure.
130         ClientEnvEvent getClientEnvEvent();
131
132         virtual void getSelectedActiveObjects(
133                 const core::line3d<f32> &shootline_on_map,
134                 std::vector<PointedThing> &objects
135         );
136
137         u16 attachement_parent_ids[USHRT_MAX + 1];
138
139         const std::list<std::string> &getPlayerNames() { return m_player_names; }
140         void addPlayerName(const std::string &name) { m_player_names.push_back(name); }
141         void removePlayerName(const std::string &name) { m_player_names.remove(name); }
142         void updateCameraOffset(v3s16 camera_offset)
143         { m_camera_offset = camera_offset; }
144         v3s16 getCameraOffset() const { return m_camera_offset; }
145 private:
146         ClientMap *m_map;
147         LocalPlayer *m_local_player = nullptr;
148         ITextureSource *m_texturesource;
149         Client *m_client;
150         ClientScripting *m_script = nullptr;
151         ClientActiveObjectMap m_active_objects;
152         std::vector<ClientSimpleObject*> m_simple_objects;
153         std::queue<ClientEnvEvent> m_client_event_queue;
154         IntervalLimiter m_active_object_light_update_interval;
155         IntervalLimiter m_lava_hurt_interval;
156         IntervalLimiter m_drowning_interval;
157         IntervalLimiter m_breathing_interval;
158         std::list<std::string> m_player_names;
159         v3s16 m_camera_offset;
160 };
161
162 #endif