ef0c45a6ae0a096f62e4bdbf74191700afe8e4aa
[oweals/minetest.git] / src / server.h
1 /*
2 Minetest
3 Copyright (C) 2010-2013 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 SERVER_HEADER
21 #define SERVER_HEADER
22
23 #include "connection.h"
24 #include "environment.h"
25 #include "irrlichttypes_bloated.h"
26 #include <string>
27 #include "porting.h"
28 #include "map.h"
29 #include "inventory.h"
30 #include "ban.h"
31 #include "gamedef.h"
32 #include "serialization.h" // For SER_FMT_VER_INVALID
33 #include "mods.h"
34 #include "inventorymanager.h"
35 #include "subgame.h"
36 #include "sound.h"
37 #include "util/thread.h"
38 #include "util/string.h"
39 #include "rollback_interface.h" // Needed for rollbackRevertActions()
40 #include <list> // Needed for rollbackRevertActions()
41 #include <algorithm>
42
43 #define PP(x) "("<<(x).X<<","<<(x).Y<<","<<(x).Z<<")"
44
45 struct LuaState;
46 typedef struct lua_State lua_State;
47 class IWritableItemDefManager;
48 class IWritableNodeDefManager;
49 class IWritableCraftDefManager;
50 class EventManager;
51 class PlayerSAO;
52 class IRollbackManager;
53 class EmergeManager;
54
55 class ServerError : public std::exception
56 {
57 public:
58         ServerError(const std::string &s)
59         {
60                 m_s = "ServerError: ";
61                 m_s += s;
62         }
63         virtual ~ServerError() throw()
64         {}
65         virtual const char * what() const throw()
66         {
67                 return m_s.c_str();
68         }
69         std::string m_s;
70 };
71
72 /*
73         Some random functions
74 */
75 v3f findSpawnPos(ServerMap &map);
76
77
78 class MapEditEventIgnorer
79 {
80 public:
81         MapEditEventIgnorer(bool *flag):
82                 m_flag(flag)
83         {
84                 if(*m_flag == false)
85                         *m_flag = true;
86                 else
87                         m_flag = NULL;
88         }
89
90         ~MapEditEventIgnorer()
91         {
92                 if(m_flag)
93                 {
94                         assert(*m_flag);
95                         *m_flag = false;
96                 }
97         }
98
99 private:
100         bool *m_flag;
101 };
102
103 class MapEditEventAreaIgnorer
104 {
105 public:
106         MapEditEventAreaIgnorer(VoxelArea *ignorevariable, const VoxelArea &a):
107                 m_ignorevariable(ignorevariable)
108         {
109                 if(m_ignorevariable->getVolume() == 0)
110                         *m_ignorevariable = a;
111                 else
112                         m_ignorevariable = NULL;
113         }
114
115         ~MapEditEventAreaIgnorer()
116         {
117                 if(m_ignorevariable)
118                 {
119                         assert(m_ignorevariable->getVolume() != 0);
120                         *m_ignorevariable = VoxelArea();
121                 }
122         }
123
124 private:
125         VoxelArea *m_ignorevariable;
126 };
127
128 class Server;
129
130 class ServerThread : public SimpleThread
131 {
132         Server *m_server;
133
134 public:
135
136         ServerThread(Server *server):
137                 SimpleThread(),
138                 m_server(server)
139         {
140         }
141
142         void * Thread();
143 };
144
145 struct PlayerInfo
146 {
147         u16 id;
148         char name[PLAYERNAME_SIZE];
149         v3f position;
150         Address address;
151         float avg_rtt;
152
153         PlayerInfo();
154         void PrintLine(std::ostream *s);
155 };
156
157 /*
158         Used for queueing and sorting block transfers in containers
159
160         Lower priority number means higher priority.
161 */
162 struct PrioritySortedBlockTransfer
163 {
164         PrioritySortedBlockTransfer(float a_priority, v3s16 a_pos, u16 a_peer_id)
165         {
166                 priority = a_priority;
167                 pos = a_pos;
168                 peer_id = a_peer_id;
169         }
170         bool operator < (const PrioritySortedBlockTransfer &other) const
171         {
172                 return priority < other.priority;
173         }
174         float priority;
175         v3s16 pos;
176         u16 peer_id;
177 };
178
179 struct MediaRequest
180 {
181         std::string name;
182
183         MediaRequest(const std::string &name_=""):
184                 name(name_)
185         {}
186 };
187
188 struct MediaInfo
189 {
190         std::string path;
191         std::string sha1_digest;
192
193         MediaInfo(const std::string path_="",
194                         const std::string sha1_digest_=""):
195                 path(path_),
196                 sha1_digest(sha1_digest_)
197         {
198         }
199 };
200
201 struct ServerSoundParams
202 {
203         float gain;
204         std::string to_player;
205         enum Type{
206                 SSP_LOCAL=0,
207                 SSP_POSITIONAL=1,
208                 SSP_OBJECT=2
209         } type;
210         v3f pos;
211         u16 object;
212         float max_hear_distance;
213         bool loop;
214
215         ServerSoundParams():
216                 gain(1.0),
217                 to_player(""),
218                 type(SSP_LOCAL),
219                 pos(0,0,0),
220                 object(0),
221                 max_hear_distance(32*BS),
222                 loop(false)
223         {}
224
225         v3f getPos(ServerEnvironment *env, bool *pos_exists) const;
226 };
227
228 struct ServerPlayingSound
229 {
230         ServerSoundParams params;
231         std::set<u16> clients; // peer ids
232 };
233
234 class RemoteClient
235 {
236 public:
237         // peer_id=0 means this client has no associated peer
238         // NOTE: If client is made allowed to exist while peer doesn't,
239         //       this has to be set to 0 when there is no peer.
240         //       Also, the client must be moved to some other container.
241         u16 peer_id;
242         // The serialization version to use with the client
243         u8 serialization_version;
244         //
245         u16 net_proto_version;
246         // Version is stored in here after INIT before INIT2
247         u8 pending_serialization_version;
248
249         bool definitions_sent;
250
251         RemoteClient():
252                 m_time_from_building(9999),
253                 m_excess_gotblocks(0)
254         {
255                 peer_id = 0;
256                 serialization_version = SER_FMT_VER_INVALID;
257                 net_proto_version = 0;
258                 pending_serialization_version = SER_FMT_VER_INVALID;
259                 definitions_sent = false;
260                 m_nearest_unsent_d = 0;
261                 m_nearest_unsent_reset_timer = 0.0;
262                 m_nothing_to_send_counter = 0;
263                 m_nothing_to_send_pause_timer = 0;
264         }
265         ~RemoteClient()
266         {
267         }
268
269         /*
270                 Finds block that should be sent next to the client.
271                 Environment should be locked when this is called.
272                 dtime is used for resetting send radius at slow interval
273         */
274         void GetNextBlocks(Server *server, float dtime,
275                         std::vector<PrioritySortedBlockTransfer> &dest);
276
277         void GotBlock(v3s16 p);
278
279         void SentBlock(v3s16 p);
280
281         void SetBlockNotSent(v3s16 p);
282         void SetBlocksNotSent(std::map<v3s16, MapBlock*> &blocks);
283
284         s32 SendingCount()
285         {
286                 return m_blocks_sending.size();
287         }
288
289         // Increments timeouts and removes timed-out blocks from list
290         // NOTE: This doesn't fix the server-not-sending-block bug
291         //       because it is related to emerging, not sending.
292         //void RunSendingTimeouts(float dtime, float timeout);
293
294         void PrintInfo(std::ostream &o)
295         {
296                 o<<"RemoteClient "<<peer_id<<": "
297                                 <<"m_blocks_sent.size()="<<m_blocks_sent.size()
298                                 <<", m_blocks_sending.size()="<<m_blocks_sending.size()
299                                 <<", m_nearest_unsent_d="<<m_nearest_unsent_d
300                                 <<", m_excess_gotblocks="<<m_excess_gotblocks
301                                 <<std::endl;
302                 m_excess_gotblocks = 0;
303         }
304
305         // Time from last placing or removing blocks
306         float m_time_from_building;
307
308         /*JMutex m_dig_mutex;
309         float m_dig_time_remaining;
310         // -1 = not digging
311         s16 m_dig_tool_item;
312         v3s16 m_dig_position;*/
313
314         /*
315                 List of active objects that the client knows of.
316                 Value is dummy.
317         */
318         std::set<u16> m_known_objects;
319
320 private:
321         /*
322                 Blocks that have been sent to client.
323                 - These don't have to be sent again.
324                 - A block is cleared from here when client says it has
325                   deleted it from it's memory
326
327                 Key is position, value is dummy.
328                 No MapBlock* is stored here because the blocks can get deleted.
329         */
330         std::set<v3s16> m_blocks_sent;
331         s16 m_nearest_unsent_d;
332         v3s16 m_last_center;
333         float m_nearest_unsent_reset_timer;
334
335         /*
336                 Blocks that are currently on the line.
337                 This is used for throttling the sending of blocks.
338                 - The size of this list is limited to some value
339                 Block is added when it is sent with BLOCKDATA.
340                 Block is removed when GOTBLOCKS is received.
341                 Value is time from sending. (not used at the moment)
342         */
343         std::map<v3s16, float> m_blocks_sending;
344
345         /*
346                 Count of excess GotBlocks().
347                 There is an excess amount because the client sometimes
348                 gets a block so late that the server sends it again,
349                 and the client then sends two GOTBLOCKs.
350                 This is resetted by PrintInfo()
351         */
352         u32 m_excess_gotblocks;
353
354         // CPU usage optimization
355         u32 m_nothing_to_send_counter;
356         float m_nothing_to_send_pause_timer;
357 };
358
359 class Server : public con::PeerHandler, public MapEventReceiver,
360                 public InventoryManager, public IGameDef,
361                 public IBackgroundBlockEmerger
362 {
363 public:
364         /*
365                 NOTE: Every public method should be thread-safe
366         */
367
368         Server(
369                 const std::string &path_world,
370                 const std::string &path_config,
371                 const SubgameSpec &gamespec,
372                 bool simple_singleplayer_mode
373         );
374         ~Server();
375         void start(unsigned short port);
376         void stop();
377         // This is mainly a way to pass the time to the server.
378         // Actual processing is done in an another thread.
379         void step(float dtime);
380         // This is run by ServerThread and does the actual processing
381         void AsyncRunStep();
382         void Receive();
383         void ProcessData(u8 *data, u32 datasize, u16 peer_id);
384
385         std::list<PlayerInfo> getPlayerInfo();
386
387         // Environment must be locked when called
388         void setTimeOfDay(u32 time)
389         {
390                 m_env->setTimeOfDay(time);
391                 m_time_of_day_send_timer = 0;
392         }
393
394         bool getShutdownRequested()
395         {
396                 return m_shutdown_requested;
397         }
398
399         /*
400                 Shall be called with the environment locked.
401                 This is accessed by the map, which is inside the environment,
402                 so it shouldn't be a problem.
403         */
404         void onMapEditEvent(MapEditEvent *event);
405
406         /*
407                 Shall be called with the environment and the connection locked.
408         */
409         Inventory* getInventory(const InventoryLocation &loc);
410         void setInventoryModified(const InventoryLocation &loc);
411
412         // Connection must be locked when called
413         std::wstring getStatusString();
414
415         void requestShutdown(void)
416         {
417                 m_shutdown_requested = true;
418         }
419
420         // Returns -1 if failed, sound handle on success
421         // Envlock + conlock
422         s32 playSound(const SimpleSoundSpec &spec, const ServerSoundParams &params);
423         void stopSound(s32 handle);
424
425         // Envlock + conlock
426         std::set<std::string> getPlayerEffectivePrivs(const std::string &name);
427         bool checkPriv(const std::string &name, const std::string &priv);
428         void reportPrivsModified(const std::string &name=""); // ""=all
429         void reportInventoryFormspecModified(const std::string &name);
430
431         // Saves g_settings to configpath given at initialization
432         void saveConfig();
433
434         void setIpBanned(const std::string &ip, const std::string &name)
435         {
436                 m_banmanager.add(ip, name);
437                 return;
438         }
439
440         void unsetIpBanned(const std::string &ip_or_name)
441         {
442                 m_banmanager.remove(ip_or_name);
443                 return;
444         }
445
446         std::string getBanDescription(const std::string &ip_or_name)
447         {
448                 return m_banmanager.getBanDescription(ip_or_name);
449         }
450
451         Address getPeerAddress(u16 peer_id)
452         {
453                 return m_con.GetPeerAddress(peer_id);
454         }
455
456         // Envlock and conlock should be locked when calling this
457         void notifyPlayer(const char *name, const std::wstring msg);
458         void notifyPlayers(const std::wstring msg);
459         void spawnParticle(const char *playername,
460                 v3f pos, v3f velocity, v3f acceleration,
461                 float expirationtime, float size,
462                 bool collisiondetection, std::string texture);
463
464         void spawnParticleAll(v3f pos, v3f velocity, v3f acceleration,
465                 float expirationtime, float size,
466                 bool collisiondetection, std::string texture);
467
468         u32 addParticleSpawner(const char *playername,
469                 u16 amount, float spawntime,
470                 v3f minpos, v3f maxpos,
471                 v3f minvel, v3f maxvel,
472                 v3f minacc, v3f maxacc,
473                 float minexptime, float maxexptime,
474                 float minsize, float maxsize,
475                 bool collisiondetection, std::string texture);
476
477         u32 addParticleSpawnerAll(u16 amount, float spawntime,
478                 v3f minpos, v3f maxpos,
479                 v3f minvel, v3f maxvel,
480                 v3f minacc, v3f maxacc,
481                 float minexptime, float maxexptime,
482                 float minsize, float maxsize,
483                 bool collisiondetection, std::string texture);
484
485         void deleteParticleSpawner(const char *playername, u32 id);
486         void deleteParticleSpawnerAll(u32 id);
487
488         void queueBlockEmerge(v3s16 blockpos, bool allow_generate);
489
490         // Creates or resets inventory
491         Inventory* createDetachedInventory(const std::string &name);
492
493         // Envlock and conlock should be locked when using Lua
494         lua_State *getLua(){ return m_lua; }
495
496         // Envlock should be locked when using the rollback manager
497         IRollbackManager *getRollbackManager(){ return m_rollback; }
498
499         //TODO: determine what (if anything) should be locked to access EmergeManager
500         EmergeManager *getEmergeManager(){ return m_emerge; }
501
502         // actions: time-reversed list
503         // Return value: success/failure
504         bool rollbackRevertActions(const std::list<RollbackAction> &actions,
505                         std::list<std::string> *log);
506
507         // IGameDef interface
508         // Under envlock
509         virtual IItemDefManager* getItemDefManager();
510         virtual INodeDefManager* getNodeDefManager();
511         virtual ICraftDefManager* getCraftDefManager();
512         virtual ITextureSource* getTextureSource();
513         virtual IShaderSource* getShaderSource();
514         virtual u16 allocateUnknownNodeId(const std::string &name);
515         virtual ISoundManager* getSoundManager();
516         virtual MtEventManager* getEventManager();
517         virtual IRollbackReportSink* getRollbackReportSink();
518
519         IWritableItemDefManager* getWritableItemDefManager();
520         IWritableNodeDefManager* getWritableNodeDefManager();
521         IWritableCraftDefManager* getWritableCraftDefManager();
522
523         const ModSpec* getModSpec(const std::string &modname);
524         void getModNames(std::list<std::string> &modlist);
525         std::string getBuiltinLuaPath();
526
527         std::string getWorldPath(){ return m_path_world; }
528
529         bool isSingleplayer(){ return m_simple_singleplayer_mode; }
530
531         void setAsyncFatalError(const std::string &error)
532         {
533                 m_async_fatal_error.set(error);
534         }
535
536         bool showFormspec(const char *name, const std::string &formspec, const std::string &formname);
537         bool hudadd(const char *pname, const u32 &id, HudElement *element);
538         bool hudrm(const char *pname, const u32 &id);
539         bool hudchange(const char *pname, const u32 &id, const u8 &stat, v2f data);
540         bool hudchange(const char *pname, const u32 &id, const u8 &stat, std::string data);
541         bool hudchange(const char *pname, const u32 &id, const u8 &stat, u32 data);
542 private:
543
544         // con::PeerHandler implementation.
545         // These queue stuff to be processed by handlePeerChanges().
546         // As of now, these create and remove clients and players.
547         void peerAdded(con::Peer *peer);
548         void deletingPeer(con::Peer *peer, bool timeout);
549
550         /*
551                 Static send methods
552         */
553
554         static void SendMovement(con::Connection &con, u16 peer_id);
555         static void SendHP(con::Connection &con, u16 peer_id, u8 hp);
556         static void SendAccessDenied(con::Connection &con, u16 peer_id,
557                         const std::wstring &reason);
558         static void SendDeathscreen(con::Connection &con, u16 peer_id,
559                         bool set_camera_point_target, v3f camera_point_target);
560         static void SendItemDef(con::Connection &con, u16 peer_id,
561                         IItemDefManager *itemdef, u16 protocol_version);
562         static void SendNodeDef(con::Connection &con, u16 peer_id,
563                         INodeDefManager *nodedef, u16 protocol_version);
564
565         /*
566                 Non-static send methods.
567                 Conlock should be always used.
568                 Envlock usage is documented badly but it's easy to figure out
569                 which ones access the environment.
570         */
571
572         // Envlock and conlock should be locked when calling these
573         void SendInventory(u16 peer_id);
574         void SendChatMessage(u16 peer_id, const std::wstring &message);
575         void BroadcastChatMessage(const std::wstring &message);
576         void SendPlayerHP(u16 peer_id);
577         void SendMovePlayer(u16 peer_id);
578         void SendPlayerPrivileges(u16 peer_id);
579         void SendPlayerInventoryFormspec(u16 peer_id);
580         void SendShowFormspecMessage(u16 peer_id, const std::string formspec, const std::string formname);
581         void SendHUDAdd(u16 peer_id, const u32 id, HudElement* form);
582         void SendHUDRm(u16 peer_id, const u32 id);
583         void SendHUDChange(u16 peer_id, const u32 id, const u8 stat, v2f data);
584         void SendHUDChange(u16 peer_id, const u32 id, const u8 stat, std::string data);
585         void SendHUDChange(u16 peer_id, const u32 id, const u8 stat, u32 data);
586         /*
587                 Send a node removal/addition event to all clients except ignore_id.
588                 Additionally, if far_players!=NULL, players further away than
589                 far_d_nodes are ignored and their peer_ids are added to far_players
590         */
591         // Envlock and conlock should be locked when calling these
592         void sendRemoveNode(v3s16 p, u16 ignore_id=0,
593                         std::list<u16> *far_players=NULL, float far_d_nodes=100);
594         void sendAddNode(v3s16 p, MapNode n, u16 ignore_id=0,
595                         std::list<u16> *far_players=NULL, float far_d_nodes=100);
596         void setBlockNotSent(v3s16 p);
597
598         // Environment and Connection must be locked when called
599         void SendBlockNoLock(u16 peer_id, MapBlock *block, u8 ver);
600
601         // Sends blocks to clients (locks env and con on its own)
602         void SendBlocks(float dtime);
603
604         void fillMediaCache();
605         void sendMediaAnnouncement(u16 peer_id);
606         void sendRequestedMedia(u16 peer_id,
607                         const std::list<MediaRequest> &tosend);
608
609         void sendDetachedInventory(const std::string &name, u16 peer_id);
610         void sendDetachedInventoryToAll(const std::string &name);
611         void sendDetachedInventories(u16 peer_id);
612
613         // Adds a ParticleSpawner on peer with peer_id
614         void SendAddParticleSpawner(u16 peer_id, u16 amount, float spawntime,
615                 v3f minpos, v3f maxpos,
616                 v3f minvel, v3f maxvel,
617                 v3f minacc, v3f maxacc,
618                 float minexptime, float maxexptime,
619                 float minsize, float maxsize,
620                 bool collisiondetection, std::string texture, u32 id);
621
622         // Adds a ParticleSpawner on all peers
623         void SendAddParticleSpawnerAll(u16 amount, float spawntime,
624                 v3f minpos, v3f maxpos,
625                 v3f minvel, v3f maxvel,
626                 v3f minacc, v3f maxacc,
627                 float minexptime, float maxexptime,
628                 float minsize, float maxsize,
629                 bool collisiondetection, std::string texture, u32 id);
630
631         // Deletes ParticleSpawner on a single client
632         void SendDeleteParticleSpawner(u16 peer_id, u32 id);
633
634         // Deletes ParticleSpawner on all clients
635         void SendDeleteParticleSpawnerAll(u32 id);
636
637         // Spawns particle on single client
638         void SendSpawnParticle(u16 peer_id,
639                 v3f pos, v3f velocity, v3f acceleration,
640                 float expirationtime, float size,
641                 bool collisiondetection, std::string texture);
642
643         // Spawns particle on all clients
644         void SendSpawnParticleAll(v3f pos, v3f velocity, v3f acceleration,
645                 float expirationtime, float size,
646                 bool collisiondetection, std::string texture);
647
648         /*
649                 Something random
650         */
651
652         void DiePlayer(u16 peer_id);
653         void RespawnPlayer(u16 peer_id);
654
655         void UpdateCrafting(u16 peer_id);
656
657         // When called, connection mutex should be locked
658         RemoteClient* getClient(u16 peer_id);
659
660         // When called, environment mutex should be locked
661         std::string getPlayerName(u16 peer_id)
662         {
663                 Player *player = m_env->getPlayer(peer_id);
664                 if(player == NULL)
665                         return "[id="+itos(peer_id)+"]";
666                 return player->getName();
667         }
668
669         // When called, environment mutex should be locked
670         PlayerSAO* getPlayerSAO(u16 peer_id)
671         {
672                 Player *player = m_env->getPlayer(peer_id);
673                 if(player == NULL)
674                         return NULL;
675                 return player->getPlayerSAO();
676         }
677
678         /*
679                 Get a player from memory or creates one.
680                 If player is already connected, return NULL
681                 Does not verify/modify auth info and password.
682
683                 Call with env and con locked.
684         */
685         PlayerSAO *emergePlayer(const char *name, u16 peer_id);
686
687         // Locks environment and connection by its own
688         struct PeerChange;
689         void handlePeerChange(PeerChange &c);
690         void handlePeerChanges();
691
692         /*
693                 Variables
694         */
695
696         // World directory
697         std::string m_path_world;
698         // Path to user's configuration file ("" = no configuration file)
699         std::string m_path_config;
700         // Subgame specification
701         SubgameSpec m_gamespec;
702         // If true, do not allow multiple players and hide some multiplayer
703         // functionality
704         bool m_simple_singleplayer_mode;
705
706         // Thread can set; step() will throw as ServerError
707         MutexedVariable<std::string> m_async_fatal_error;
708
709         // Some timers
710         float m_liquid_transform_timer;
711         float m_liquid_transform_every;
712         float m_print_info_timer;
713         float m_masterserver_timer;
714         float m_objectdata_timer;
715         float m_emergethread_trigger_timer;
716         float m_savemap_timer;
717         IntervalLimiter m_map_timer_and_unload_interval;
718
719         // NOTE: If connection and environment are both to be locked,
720         // environment shall be locked first.
721
722         // Environment
723         ServerEnvironment *m_env;
724         JMutex m_env_mutex;
725
726         // Connection
727         con::Connection m_con;
728         JMutex m_con_mutex;
729         // Connected clients (behind the con mutex)
730         std::map<u16, RemoteClient*> m_clients;
731         u16 m_clients_number; //for announcing masterserver
732
733         // Bann checking
734         BanManager m_banmanager;
735
736         // Rollback manager (behind m_env_mutex)
737         IRollbackManager *m_rollback;
738         bool m_rollback_sink_enabled;
739         bool m_enable_rollback_recording; // Updated once in a while
740
741         // Emerge manager
742         EmergeManager *m_emerge;
743
744         // Scripting
745         // Envlock and conlock should be locked when using Lua
746         lua_State *m_lua;
747
748         // Item definition manager
749         IWritableItemDefManager *m_itemdef;
750
751         // Node definition manager
752         IWritableNodeDefManager *m_nodedef;
753
754         // Craft definition manager
755         IWritableCraftDefManager *m_craftdef;
756
757         // Event manager
758         EventManager *m_event;
759
760         // Mods
761         std::vector<ModSpec> m_mods;
762
763         /*
764                 Threads
765         */
766
767         // A buffer for time steps
768         // step() increments and AsyncRunStep() run by m_thread reads it.
769         float m_step_dtime;
770         JMutex m_step_dtime_mutex;
771
772         // The server mainly operates in this thread
773         ServerThread m_thread;
774
775         /*
776                 Time related stuff
777         */
778
779         // Timer for sending time of day over network
780         float m_time_of_day_send_timer;
781         // Uptime of server in seconds
782         MutexedVariable<double> m_uptime;
783
784         /*
785                 Peer change queue.
786                 Queues stuff from peerAdded() and deletingPeer() to
787                 handlePeerChanges()
788         */
789         enum PeerChangeType
790         {
791                 PEER_ADDED,
792                 PEER_REMOVED
793         };
794         struct PeerChange
795         {
796                 PeerChangeType type;
797                 u16 peer_id;
798                 bool timeout;
799         };
800         Queue<PeerChange> m_peer_change_queue;
801
802         /*
803                 Random stuff
804         */
805
806         // Mod parent directory paths
807         std::list<std::string> m_modspaths;
808
809         bool m_shutdown_requested;
810
811         /*
812                 Map edit event queue. Automatically receives all map edits.
813                 The constructor of this class registers us to receive them through
814                 onMapEditEvent
815
816                 NOTE: Should these be moved to actually be members of
817                 ServerEnvironment?
818         */
819
820         /*
821                 Queue of map edits from the environment for sending to the clients
822                 This is behind m_env_mutex
823         */
824         Queue<MapEditEvent*> m_unsent_map_edit_queue;
825         /*
826                 Set to true when the server itself is modifying the map and does
827                 all sending of information by itself.
828                 This is behind m_env_mutex
829         */
830         bool m_ignore_map_edit_events;
831         /*
832                 If a non-empty area, map edit events contained within are left
833                 unsent. Done at map generation time to speed up editing of the
834                 generated area, as it will be sent anyway.
835                 This is behind m_env_mutex
836         */
837         VoxelArea m_ignore_map_edit_events_area;
838         /*
839                 If set to !=0, the incoming MapEditEvents are modified to have
840                 this peed id as the disabled recipient
841                 This is behind m_env_mutex
842         */
843         u16 m_ignore_map_edit_events_peer_id;
844
845         friend class EmergeThread;
846         friend class RemoteClient;
847
848         std::map<std::string,MediaInfo> m_media;
849
850         /*
851                 Sounds
852         */
853         std::map<s32, ServerPlayingSound> m_playing_sounds;
854         s32 m_next_sound_id;
855
856         /*
857                 Detached inventories (behind m_env_mutex)
858         */
859         // key = name
860         std::map<std::string, Inventory*> m_detached_inventories;
861
862         /*
863                 Particles
864         */
865         std::vector<u32> m_particlespawner_ids;
866 };
867
868 /*
869         Runs a simple dedicated server loop.
870
871         Shuts down when run is set to false.
872 */
873 void dedicated_server_loop(Server &server, bool &run);
874
875 #endif
876