]> git.lizzy.rs Git - minetest.git/blob - src/environment.h
merged password change menu
[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         // 0-23999
71         virtual void setTimeOfDay(u32 time)
72         {
73                 m_time_of_day = time;
74         }
75
76         u32 getTimeOfDay()
77         {
78                 return m_time_of_day;
79         }
80
81 protected:
82         // peer_ids in here should be unique, except that there may be many 0s
83         core::list<Player*> m_players;
84         // Brightness
85         //u32 m_daynight_ratio;
86         // Time of day in milli-hours (0-23999); determines day and night
87         u32 m_time_of_day;
88 };
89
90 /*
91         List of active blocks, used by ServerEnvironment
92 */
93
94 class ActiveBlockList
95 {
96 public:
97         void update(core::list<v3s16> &active_positions,
98                         s16 radius,
99                         core::map<v3s16, bool> &blocks_removed,
100                         core::map<v3s16, bool> &blocks_added);
101
102         bool contains(v3s16 p){
103                 return (m_list.find(p) != NULL);
104         }
105
106         void clear(){
107                 m_list.clear();
108         }
109
110         core::map<v3s16, bool> m_list;
111
112 private:
113 };
114
115 /*
116         Active block modifier interface
117 */
118
119 class ActiveBlockModifier
120 {
121 public:
122         ActiveBlockModifier(){};
123         virtual ~ActiveBlockModifier(){};
124         //TODO
125         //virtual void 
126 };
127
128 /*
129         The server-side environment.
130
131         This is not thread-safe. Server uses an environment mutex.
132 */
133
134 #include "serverobject.h"
135
136 class Server;
137
138 class ServerEnvironment : public Environment
139 {
140 public:
141         ServerEnvironment(ServerMap *map, Server *server);
142         ~ServerEnvironment();
143
144         Map & getMap()
145         {
146                 return *m_map;
147         }
148
149         ServerMap & getServerMap()
150         {
151                 return *m_map;
152         }
153
154         Server * getServer()
155         {
156                 return m_server;
157         }
158
159         void step(f32 dtime);
160         
161         /*
162                 Save players
163         */
164         void serializePlayers(const std::string &savedir);
165         void deSerializePlayers(const std::string &savedir);
166
167         /*
168                 Save and load time of day and game timer
169         */
170         void saveMeta(const std::string &savedir);
171         void loadMeta(const std::string &savedir);
172
173         /*
174                 ActiveObjects
175         */
176
177         ServerActiveObject* getActiveObject(u16 id);
178
179         /*
180                 Add an active object to the environment.
181                 Environment handles deletion of object.
182                 Object may be deleted by environment immediately.
183                 If id of object is 0, assigns a free id to it.
184                 Returns the id of the object.
185                 Returns 0 if not added and thus deleted.
186         */
187         u16 addActiveObject(ServerActiveObject *object);
188         
189         /*
190                 Find out what new objects have been added to
191                 inside a radius around a position
192         */
193         void getAddedActiveObjects(v3s16 pos, s16 radius,
194                         core::map<u16, bool> &current_objects,
195                         core::map<u16, bool> &added_objects);
196
197         /*
198                 Find out what new objects have been removed from
199                 inside a radius around a position
200         */
201         void getRemovedActiveObjects(v3s16 pos, s16 radius,
202                         core::map<u16, bool> &current_objects,
203                         core::map<u16, bool> &removed_objects);
204         
205         /*
206                 Get the next message emitted by some active object.
207                 Returns a message with id=0 if no messages are available.
208         */
209         ActiveObjectMessage getActiveObjectMessage();
210
211 private:
212         /*
213                 Remove all objects that satisfy (m_removed && m_known_by_count==0)
214         */
215         void removeRemovedObjects();
216         
217         /*
218                 Convert stored objects from block to active
219         */
220         void activateObjects(MapBlock *block);
221         
222         /*
223                 Convert objects that are not in active blocks to static.
224
225                 If m_known_by_count != 0, active object is not deleted, but static
226                 data is still updated.
227
228                 If force_delete is set, active object is deleted nevertheless. It
229                 shall only be set so in the destructor of the environment.
230         */
231         void deactivateFarObjects(bool force_delete);
232
233         /*
234                 Member variables
235         */
236         
237         // The map
238         ServerMap *m_map;
239         // Pointer to server (which is handling this environment)
240         Server *m_server;
241         // Active object list
242         core::map<u16, ServerActiveObject*> m_active_objects;
243         // Outgoing network message buffer for active objects
244         Queue<ActiveObjectMessage> m_active_object_messages;
245         // Some timers
246         float m_random_spawn_timer; // used for experimental code
247         float m_send_recommended_timer;
248         IntervalLimiter m_object_management_interval;
249         // List of active blocks
250         ActiveBlockList m_active_blocks;
251         IntervalLimiter m_active_blocks_management_interval;
252         IntervalLimiter m_active_blocks_test_interval;
253         // Time from the beginning of the game in seconds.
254         // Incremented in step().
255         u32 m_game_time;
256         // A helper variable for incrementing the latter
257         float m_game_time_fraction_counter;
258 };
259
260 #ifndef SERVER
261
262 #include "clientobject.h"
263
264 /*
265         The client-side environment.
266
267         This is not thread-safe.
268         Must be called from main (irrlicht) thread (uses the SceneManager)
269         Client uses an environment mutex.
270 */
271
272 enum ClientEnvEventType
273 {
274         CEE_NONE,
275         CEE_PLAYER_DAMAGE
276 };
277
278 struct ClientEnvEvent
279 {
280         ClientEnvEventType type;
281         union {
282                 struct{
283                 } none;
284                 struct{
285                         u8 amount;
286                 } player_damage;
287         };
288 };
289
290 class ClientEnvironment : public Environment
291 {
292 public:
293         ClientEnvironment(ClientMap *map, scene::ISceneManager *smgr);
294         ~ClientEnvironment();
295
296         Map & getMap()
297         {
298                 return *m_map;
299         }
300
301         ClientMap & getClientMap()
302         {
303                 return *m_map;
304         }
305
306         void step(f32 dtime);
307
308         virtual void addPlayer(Player *player);
309         LocalPlayer * getLocalPlayer();
310
311         void updateMeshes(v3s16 blockpos);
312         void expireMeshes(bool only_daynight_diffed);
313
314         void setTimeOfDay(u32 time)
315         {
316                 u32 old_dr = getDayNightRatio();
317
318                 Environment::setTimeOfDay(time);
319
320                 if(getDayNightRatio() != old_dr)
321                 {
322                         dout_client<<DTIME<<"ClientEnvironment: DayNightRatio changed"
323                                         <<" -> expiring meshes"<<std::endl;
324                         expireMeshes(true);
325                 }
326         }
327
328         /*
329                 ActiveObjects
330         */
331         
332         ClientActiveObject* getActiveObject(u16 id);
333
334         /*
335                 Adds an active object to the environment.
336                 Environment handles deletion of object.
337                 Object may be deleted by environment immediately.
338                 If id of object is 0, assigns a free id to it.
339                 Returns the id of the object.
340                 Returns 0 if not added and thus deleted.
341         */
342         u16 addActiveObject(ClientActiveObject *object);
343
344         void addActiveObject(u16 id, u8 type, const std::string &init_data);
345         void removeActiveObject(u16 id);
346
347         void processActiveObjectMessage(u16 id, const std::string &data);
348
349         /*
350                 Callbacks for activeobjects
351         */
352
353         void damageLocalPlayer(u8 damage);
354
355         /*
356                 Client likes to call these
357         */
358         
359         // Get all nearby objects
360         void getActiveObjects(v3f origin, f32 max_d,
361                         core::array<DistanceSortedActiveObject> &dest);
362         
363         // Get event from queue. CEE_NONE is returned if queue is empty.
364         ClientEnvEvent getClientEvent();
365         
366 private:
367         ClientMap *m_map;
368         scene::ISceneManager *m_smgr;
369         core::map<u16, ClientActiveObject*> m_active_objects;
370         Queue<ClientEnvEvent> m_client_event_queue;
371 };
372
373 #endif
374
375 #endif
376