606070e3aca5502d089198195dc3ea1374816b8a
[oweals/minetest.git] / src / client / 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 #pragma once
21
22 #include "environment.h"
23 #include <ISceneManager.h>
24 #include "clientobject.h"
25 #include "util/numeric.h"
26
27 class ClientSimpleObject;
28 class ClientMap;
29 class ClientScripting;
30 class ClientActiveObject;
31 class GenericCAO;
32 class LocalPlayer;
33
34 /*
35         The client-side environment.
36
37         This is not thread-safe.
38         Must be called from main (irrlicht) thread (uses the SceneManager)
39         Client uses an environment mutex.
40 */
41
42 enum ClientEnvEventType
43 {
44         CEE_NONE,
45         CEE_PLAYER_DAMAGE
46 };
47
48 struct ClientEnvEvent
49 {
50         ClientEnvEventType type;
51         union {
52                 //struct{
53                 //} none;
54                 struct{
55                         u8 amount;
56                         bool send_to_server;
57                 } player_damage;
58         };
59 };
60
61 typedef std::unordered_map<u16, ClientActiveObject*> ClientActiveObjectMap;
62 class ClientEnvironment : public Environment
63 {
64 public:
65         ClientEnvironment(ClientMap *map, ITextureSource *texturesource, Client *client);
66         ~ClientEnvironment();
67
68         Map & getMap();
69         ClientMap & getClientMap();
70
71         Client *getGameDef() { return m_client; }
72         void setScript(ClientScripting *script) { m_script = script; }
73
74         void step(f32 dtime);
75
76         virtual void setLocalPlayer(LocalPlayer *player);
77         LocalPlayer *getLocalPlayer() const { return m_local_player; }
78
79         /*
80                 ClientSimpleObjects
81         */
82
83         void addSimpleObject(ClientSimpleObject *simple);
84
85         /*
86                 ActiveObjects
87         */
88
89         GenericCAO* getGenericCAO(u16 id);
90         ClientActiveObject* getActiveObject(u16 id);
91
92         /*
93                 Adds an active object to the environment.
94                 Environment handles deletion of object.
95                 Object may be deleted by environment immediately.
96                 If id of object is 0, assigns a free id to it.
97                 Returns the id of the object.
98                 Returns 0 if not added and thus deleted.
99         */
100         u16 addActiveObject(ClientActiveObject *object);
101
102         void addActiveObject(u16 id, u8 type, const std::string &init_data);
103         void removeActiveObject(u16 id);
104
105         void processActiveObjectMessage(u16 id, const std::string &data);
106
107         /*
108                 Callbacks for activeobjects
109         */
110
111         void damageLocalPlayer(u8 damage, bool handle_hp=true);
112
113         /*
114                 Client likes to call these
115         */
116
117         // Get all nearby objects
118         void getActiveObjects(v3f origin, f32 max_d,
119                 std::vector<DistanceSortedActiveObject> &dest);
120
121         bool hasClientEnvEvents() const { return !m_client_event_queue.empty(); }
122
123         // Get event from queue. If queue is empty, it triggers an assertion failure.
124         ClientEnvEvent getClientEnvEvent();
125
126         virtual void getSelectedActiveObjects(
127                 const core::line3d<f32> &shootline_on_map,
128                 std::vector<PointedThing> &objects
129         );
130
131         u16 attachement_parent_ids[USHRT_MAX + 1];
132
133         const std::list<std::string> &getPlayerNames() { return m_player_names; }
134         void addPlayerName(const std::string &name) { m_player_names.push_back(name); }
135         void removePlayerName(const std::string &name) { m_player_names.remove(name); }
136         void updateCameraOffset(const v3s16 &camera_offset)
137         { m_camera_offset = camera_offset; }
138         v3s16 getCameraOffset() const { return m_camera_offset; }
139 private:
140         ClientMap *m_map;
141         LocalPlayer *m_local_player = nullptr;
142         ITextureSource *m_texturesource;
143         Client *m_client;
144         ClientScripting *m_script = nullptr;
145         ClientActiveObjectMap m_active_objects;
146         std::vector<ClientSimpleObject*> m_simple_objects;
147         std::queue<ClientEnvEvent> m_client_event_queue;
148         IntervalLimiter m_active_object_light_update_interval;
149         std::list<std::string> m_player_names;
150         v3s16 m_camera_offset;
151 };