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