]> git.lizzy.rs Git - minetest.git/blob - src/environment.h
bb9fb0136017a42bdb3f475b73418aa65cd7bfd3
[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         The server-side environment.
117
118         This is not thread-safe. Server uses an environment mutex.
119 */
120
121 #include "serverobject.h"
122
123 class Server;
124 class ActiveBlockModifier;
125
126 class ServerEnvironment : public Environment
127 {
128 public:
129         ServerEnvironment(ServerMap *map, Server *server);
130         ~ServerEnvironment();
131
132         Map & getMap()
133         {
134                 return *m_map;
135         }
136
137         ServerMap & getServerMap()
138         {
139                 return *m_map;
140         }
141
142         Server * getServer()
143         {
144                 return m_server;
145         }
146
147         void step(f32 dtime);
148         
149         /*
150                 Save players
151         */
152         void serializePlayers(const std::string &savedir);
153         void deSerializePlayers(const std::string &savedir);
154
155         /*
156                 Save and load time of day and game timer
157         */
158         void saveMeta(const std::string &savedir);
159         void loadMeta(const std::string &savedir);
160
161         /*
162                 External ActiveObject interface
163                 -------------------------------------------
164         */
165
166         ServerActiveObject* getActiveObject(u16 id);
167
168         /*
169                 Add an active object to the environment.
170                 Environment handles deletion of object.
171                 Object may be deleted by environment immediately.
172                 If id of object is 0, assigns a free id to it.
173                 Returns the id of the object.
174                 Returns 0 if not added and thus deleted.
175         */
176         u16 addActiveObject(ServerActiveObject *object);
177         
178         /*
179                 Find out what new objects have been added to
180                 inside a radius around a position
181         */
182         void getAddedActiveObjects(v3s16 pos, s16 radius,
183                         core::map<u16, bool> &current_objects,
184                         core::map<u16, bool> &added_objects);
185
186         /*
187                 Find out what new objects have been removed from
188                 inside a radius around a position
189         */
190         void getRemovedActiveObjects(v3s16 pos, s16 radius,
191                         core::map<u16, bool> &current_objects,
192                         core::map<u16, bool> &removed_objects);
193         
194         /*
195                 Get the next message emitted by some active object.
196                 Returns a message with id=0 if no messages are available.
197         */
198         ActiveObjectMessage getActiveObjectMessage();
199
200         /*
201                 ActiveBlockModifiers (TODO)
202                 -------------------------------------------
203         */
204
205         void addActiveBlockModifier(ActiveBlockModifier *abm);
206
207 private:
208
209         /*
210                 Internal ActiveObject interface
211                 -------------------------------------------
212         */
213
214         /*
215                 Add an active object to the environment.
216
217                 Called by addActiveObject.
218
219                 Object may be deleted by environment immediately.
220                 If id of object is 0, assigns a free id to it.
221                 Returns the id of the object.
222                 Returns 0 if not added and thus deleted.
223         */
224         u16 addActiveObjectRaw(ServerActiveObject *object, bool set_changed);
225         
226         /*
227                 Remove all objects that satisfy (m_removed && m_known_by_count==0)
228         */
229         void removeRemovedObjects();
230         
231         /*
232                 Convert stored objects from block to active
233         */
234         void activateObjects(MapBlock *block);
235         
236         /*
237                 Convert objects that are not in active blocks to static.
238
239                 If m_known_by_count != 0, active object is not deleted, but static
240                 data is still updated.
241
242                 If force_delete is set, active object is deleted nevertheless. It
243                 shall only be set so in the destructor of the environment.
244         */
245         void deactivateFarObjects(bool force_delete);
246
247         /*
248                 Member variables
249         */
250         
251         // The map
252         ServerMap *m_map;
253         // Pointer to server (which is handling this environment)
254         Server *m_server;
255         // Active object list
256         core::map<u16, ServerActiveObject*> m_active_objects;
257         // Outgoing network message buffer for active objects
258         Queue<ActiveObjectMessage> m_active_object_messages;
259         // Some timers
260         float m_random_spawn_timer; // used for experimental code
261         float m_send_recommended_timer;
262         IntervalLimiter m_object_management_interval;
263         // List of active blocks
264         ActiveBlockList m_active_blocks;
265         IntervalLimiter m_active_blocks_management_interval;
266         IntervalLimiter m_active_blocks_test_interval;
267         IntervalLimiter m_active_blocks_nodemetadata_interval;
268         // Time from the beginning of the game in seconds.
269         // Incremented in step().
270         u32 m_game_time;
271         // A helper variable for incrementing the latter
272         float m_game_time_fraction_counter;
273 };
274
275 /*
276         Active block modifier interface.
277
278         These are fed into ServerEnvironment at initialization time;
279         ServerEnvironment handles deleting them.
280 */
281
282 class ActiveBlockModifier
283 {
284 public:
285         ActiveBlockModifier(){};
286         virtual ~ActiveBlockModifier(){};
287
288         //virtual core::list<u8> update(ServerEnvironment *env) = 0;
289         virtual u32 getTriggerContentCount(){ return 1;}
290         virtual u8 getTriggerContent(u32 i) = 0;
291         virtual float getActiveInterval() = 0;
292         // chance of (1 / return value), 0 is disallowed
293         virtual u32 getActiveChance() = 0;
294         // This is called usually at interval for 1/chance of the nodes
295         virtual void triggerEvent(ServerEnvironment *env, v3s16 p) = 0;
296 };
297
298 #ifndef SERVER
299
300 #include "clientobject.h"
301
302 /*
303         The client-side environment.
304
305         This is not thread-safe.
306         Must be called from main (irrlicht) thread (uses the SceneManager)
307         Client uses an environment mutex.
308 */
309
310 enum ClientEnvEventType
311 {
312         CEE_NONE,
313         CEE_PLAYER_DAMAGE
314 };
315
316 struct ClientEnvEvent
317 {
318         ClientEnvEventType type;
319         union {
320                 struct{
321                 } none;
322                 struct{
323                         u8 amount;
324                 } player_damage;
325         };
326 };
327
328 class ClientEnvironment : public Environment
329 {
330 public:
331         ClientEnvironment(ClientMap *map, scene::ISceneManager *smgr);
332         ~ClientEnvironment();
333
334         Map & getMap()
335         {
336                 return *m_map;
337         }
338
339         ClientMap & getClientMap()
340         {
341                 return *m_map;
342         }
343
344         void step(f32 dtime);
345
346         virtual void addPlayer(Player *player);
347         LocalPlayer * getLocalPlayer();
348
349         void updateMeshes(v3s16 blockpos);
350         void expireMeshes(bool only_daynight_diffed);
351
352         void setTimeOfDay(u32 time)
353         {
354                 u32 old_dr = getDayNightRatio();
355
356                 Environment::setTimeOfDay(time);
357
358                 if(getDayNightRatio() != old_dr)
359                 {
360                         dout_client<<DTIME<<"ClientEnvironment: DayNightRatio changed"
361                                         <<" -> expiring meshes"<<std::endl;
362                         expireMeshes(true);
363                 }
364         }
365
366         /*
367                 ActiveObjects
368         */
369         
370         ClientActiveObject* getActiveObject(u16 id);
371
372         /*
373                 Adds an active object to the environment.
374                 Environment handles deletion of object.
375                 Object may be deleted by environment immediately.
376                 If id of object is 0, assigns a free id to it.
377                 Returns the id of the object.
378                 Returns 0 if not added and thus deleted.
379         */
380         u16 addActiveObject(ClientActiveObject *object);
381
382         void addActiveObject(u16 id, u8 type, const std::string &init_data);
383         void removeActiveObject(u16 id);
384
385         void processActiveObjectMessage(u16 id, const std::string &data);
386
387         /*
388                 Callbacks for activeobjects
389         */
390
391         void damageLocalPlayer(u8 damage);
392
393         /*
394                 Client likes to call these
395         */
396         
397         // Get all nearby objects
398         void getActiveObjects(v3f origin, f32 max_d,
399                         core::array<DistanceSortedActiveObject> &dest);
400         
401         // Get event from queue. CEE_NONE is returned if queue is empty.
402         ClientEnvEvent getClientEvent();
403         
404 private:
405         ClientMap *m_map;
406         scene::ISceneManager *m_smgr;
407         core::map<u16, ClientActiveObject*> m_active_objects;
408         Queue<ClientEnvEvent> m_client_event_queue;
409 };
410
411 #endif
412
413 #endif
414