971ded5c997e0736f8942bc6f53948728a33c66d
[oweals/minetest.git] / src / environment.h
1 /*
2 Minetest-c55
3 Copyright (C) 2010-2011 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 General Public License as published by
7 the Free Software Foundation; either version 2 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 General Public License for more details.
14
15 You should have received a copy of the GNU 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 ENVIRONMENT_HEADER
21 #define ENVIRONMENT_HEADER
22
23 /*
24         This class is the game's environment.
25         It contains:
26         - The map
27         - Players
28         - Other objects
29         - The current time in the game (actually it only contains the brightness)
30         - etc.
31 */
32
33 #include <list>
34 #include "common_irrlicht.h"
35 #include "player.h"
36 #include "map.h"
37 #include <ostream>
38 #include "utility.h"
39
40 class Environment
41 {
42 public:
43         // Environment will delete the map passed to the constructor
44         Environment();
45         virtual ~Environment();
46
47         /*
48                 Step everything in environment.
49                 - Move players
50                 - Step mobs
51                 - Run timers of map
52         */
53         virtual void step(f32 dtime) = 0;
54
55         virtual Map & getMap() = 0;
56
57         virtual void addPlayer(Player *player);
58         void removePlayer(u16 peer_id);
59         Player * getPlayer(u16 peer_id);
60         Player * getPlayer(const char *name);
61         Player * getRandomConnectedPlayer();
62         Player * getNearestConnectedPlayer(v3f pos);
63         core::list<Player*> getPlayers();
64         core::list<Player*> getPlayers(bool ignore_disconnected);
65         void printPlayers(std::ostream &o);
66         
67         void setDayNightRatio(u32 r);
68         u32 getDayNightRatio();
69
70 protected:
71         // peer_ids in here should be unique, except that there may be many 0s
72         core::list<Player*> m_players;
73         // Brightness
74         u32 m_daynight_ratio;
75 };
76
77 /*
78         The server-side environment.
79
80         This is not thread-safe. Server uses an environment mutex.
81 */
82
83 #include "serverobject.h"
84
85 class Server;
86
87 class ServerEnvironment : public Environment
88 {
89 public:
90         ServerEnvironment(ServerMap *map, Server *server);
91         ~ServerEnvironment();
92
93         Map & getMap()
94         {
95                 return *m_map;
96         }
97
98         ServerMap & getServerMap()
99         {
100                 return *m_map;
101         }
102
103         Server * getServer()
104         {
105                 return m_server;
106         }
107
108         void step(f32 dtime);
109
110         void serializePlayers(const std::string &savedir);
111         void deSerializePlayers(const std::string &savedir);
112
113         /*
114                 ActiveObjects
115         */
116
117         ServerActiveObject* getActiveObject(u16 id);
118
119         /*
120                 Add an active object to the environment.
121                 Environment handles deletion of object.
122                 Object may be deleted by environment immediately.
123                 If id of object is 0, assigns a free id to it.
124                 Returns the id of the object.
125                 Returns 0 if not added and thus deleted.
126         */
127         u16 addActiveObject(ServerActiveObject *object);
128         
129         /*
130                 Find out what new objects have been added to
131                 inside a radius around a position
132         */
133         void getAddedActiveObjects(v3s16 pos, s16 radius,
134                         core::map<u16, bool> &current_objects,
135                         core::map<u16, bool> &added_objects);
136
137         /*
138                 Find out what new objects have been removed from
139                 inside a radius around a position
140         */
141         void getRemovedActiveObjects(v3s16 pos, s16 radius,
142                         core::map<u16, bool> &current_objects,
143                         core::map<u16, bool> &removed_objects);
144         
145         /*
146                 Get the next message emitted by some active object.
147                 Returns a message with id=0 if no messages are available.
148         */
149         ActiveObjectMessage getActiveObjectMessage();
150
151 private:
152         /*
153                 Remove all objects that satisfy (m_removed && m_known_by_count==0)
154         */
155         void removeRemovedObjects();
156         /*
157                 Convert stored objects from blocks near the players to active.
158         */
159         void activateNearObjects(s16 range_blocks);
160         /*
161                 Convert objects that are far away from all the players to static.
162
163                 If range_blocks == -1, convert everything to static even if known
164                 by a player.
165         */
166         void deactivateFarObjects(s16 range_blocks);
167         
168         ServerMap *m_map;
169         Server *m_server;
170         core::map<u16, ServerActiveObject*> m_active_objects;
171         Queue<ActiveObjectMessage> m_active_object_messages;
172         float m_random_spawn_timer;
173         float m_send_recommended_timer;
174         IntervalLimiter m_object_management_interval;
175 };
176
177 #ifndef SERVER
178
179 #include "clientobject.h"
180
181 /*
182         The client-side environment.
183
184         This is not thread-safe.
185         Must be called from main (irrlicht) thread (uses the SceneManager)
186         Client uses an environment mutex.
187 */
188
189 enum ClientEnvEventType
190 {
191         CEE_NONE,
192         CEE_PLAYER_DAMAGE
193 };
194
195 struct ClientEnvEvent
196 {
197         ClientEnvEventType type;
198         union {
199                 struct{
200                 } none;
201                 struct{
202                         u8 amount;
203                 } player_damage;
204         };
205 };
206
207 class ClientEnvironment : public Environment
208 {
209 public:
210         ClientEnvironment(ClientMap *map, scene::ISceneManager *smgr);
211         ~ClientEnvironment();
212
213         Map & getMap()
214         {
215                 return *m_map;
216         }
217
218         ClientMap & getClientMap()
219         {
220                 return *m_map;
221         }
222
223         void step(f32 dtime);
224
225         virtual void addPlayer(Player *player);
226         LocalPlayer * getLocalPlayer();
227
228         void updateMeshes(v3s16 blockpos);
229         void expireMeshes(bool only_daynight_diffed);
230
231         /*
232                 ActiveObjects
233         */
234         
235         ClientActiveObject* getActiveObject(u16 id);
236
237         /*
238                 Adds an active object to the environment.
239                 Environment handles deletion of object.
240                 Object may be deleted by environment immediately.
241                 If id of object is 0, assigns a free id to it.
242                 Returns the id of the object.
243                 Returns 0 if not added and thus deleted.
244         */
245         u16 addActiveObject(ClientActiveObject *object);
246
247         void addActiveObject(u16 id, u8 type, const std::string &init_data);
248         void removeActiveObject(u16 id);
249
250         void processActiveObjectMessage(u16 id, const std::string &data);
251
252         /*
253                 Callbacks for activeobjects
254         */
255
256         void damageLocalPlayer(u8 damage);
257
258         /*
259                 Client likes to call these
260         */
261         
262         // Get all nearby objects
263         void getActiveObjects(v3f origin, f32 max_d,
264                         core::array<DistanceSortedActiveObject> &dest);
265         
266         // Get event from queue. CEE_NONE is returned if queue is empty.
267         ClientEnvEvent getClientEvent();
268         
269 private:
270         ClientMap *m_map;
271         scene::ISceneManager *m_smgr;
272         core::map<u16, ClientActiveObject*> m_active_objects;
273         Queue<ClientEnvEvent> m_client_event_queue;
274 };
275
276 #endif
277
278 #endif
279