]> git.lizzy.rs Git - minetest.git/blob - src/server.cpp
Merge pull request #503 from RealBadAngel/master
[minetest.git] / src / server.cpp
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 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 #include "server.h"
21 #include <iostream>
22 #include <queue>
23 #include "clientserver.h"
24 #include "map.h"
25 #include "jmutexautolock.h"
26 #include "main.h"
27 #include "constants.h"
28 #include "voxel.h"
29 #include "config.h"
30 #include "filesys.h"
31 #include "mapblock.h"
32 #include "serverobject.h"
33 #include "settings.h"
34 #include "profiler.h"
35 #include "log.h"
36 #include "script.h"
37 #include "scriptapi.h"
38 #include "nodedef.h"
39 #include "itemdef.h"
40 #include "craftdef.h"
41 #include "mapgen.h"
42 #include "biome.h"
43 #include "content_mapnode.h"
44 #include "content_nodemeta.h"
45 #include "content_abm.h"
46 #include "content_sao.h"
47 #include "mods.h"
48 #include "sha1.h"
49 #include "base64.h"
50 #include "tool.h"
51 #include "sound.h" // dummySoundManager
52 #include "event_manager.h"
53 #include "hex.h"
54 #include "serverlist.h"
55 #include "util/string.h"
56 #include "util/pointedthing.h"
57 #include "util/mathconstants.h"
58 #include "rollback.h"
59 #include "util/serialize.h"
60
61 #define PP(x) "("<<(x).X<<","<<(x).Y<<","<<(x).Z<<")"
62
63 #define BLOCK_EMERGE_FLAG_FROMDISK (1<<0)
64
65 class MapEditEventIgnorer
66 {
67 public:
68         MapEditEventIgnorer(bool *flag):
69                 m_flag(flag)
70         {
71                 if(*m_flag == false)
72                         *m_flag = true;
73                 else
74                         m_flag = NULL;
75         }
76
77         ~MapEditEventIgnorer()
78         {
79                 if(m_flag)
80                 {
81                         assert(*m_flag);
82                         *m_flag = false;
83                 }
84         }
85
86 private:
87         bool *m_flag;
88 };
89
90 class MapEditEventAreaIgnorer
91 {
92 public:
93         MapEditEventAreaIgnorer(VoxelArea *ignorevariable, const VoxelArea &a):
94                 m_ignorevariable(ignorevariable)
95         {
96                 if(m_ignorevariable->getVolume() == 0)
97                         *m_ignorevariable = a;
98                 else
99                         m_ignorevariable = NULL;
100         }
101
102         ~MapEditEventAreaIgnorer()
103         {
104                 if(m_ignorevariable)
105                 {
106                         assert(m_ignorevariable->getVolume() != 0);
107                         *m_ignorevariable = VoxelArea();
108                 }
109         }
110
111 private:
112         VoxelArea *m_ignorevariable;
113 };
114
115 void * ServerThread::Thread()
116 {
117         ThreadStarted();
118
119         log_register_thread("ServerThread");
120
121         DSTACK(__FUNCTION_NAME);
122
123         BEGIN_DEBUG_EXCEPTION_HANDLER
124
125         while(getRun())
126         {
127                 try{
128                         //TimeTaker timer("AsyncRunStep() + Receive()");
129
130                         {
131                                 //TimeTaker timer("AsyncRunStep()");
132                                 m_server->AsyncRunStep();
133                         }
134
135                         //infostream<<"Running m_server->Receive()"<<std::endl;
136                         m_server->Receive();
137                 }
138                 catch(con::NoIncomingDataException &e)
139                 {
140                 }
141                 catch(con::PeerNotFoundException &e)
142                 {
143                         infostream<<"Server: PeerNotFoundException"<<std::endl;
144                 }
145                 catch(con::ConnectionBindFailed &e)
146                 {
147                         m_server->setAsyncFatalError(e.what());
148                 }
149                 catch(LuaError &e)
150                 {
151                         m_server->setAsyncFatalError(e.what());
152                 }
153         }
154
155         END_DEBUG_EXCEPTION_HANDLER(errorstream)
156
157         return NULL;
158 }
159
160 void * EmergeThread::Thread()
161 {
162         ThreadStarted();
163
164         log_register_thread("EmergeThread");
165
166         DSTACK(__FUNCTION_NAME);
167
168         BEGIN_DEBUG_EXCEPTION_HANDLER
169
170         bool enable_mapgen_debug_info = g_settings->getBool("enable_mapgen_debug_info");
171
172         v3s16 last_tried_pos(-32768,-32768,-32768); // For error output
173
174         ServerMap &map = ((ServerMap&)m_server->m_env->getMap());
175         EmergeManager *emerge = m_server->m_emerge;
176         Mapgen *mapgen = emerge->getMapgen();
177
178         /*
179                 Get block info from queue, emerge them and send them
180                 to clients.
181
182                 After queue is empty, exit.
183         */
184         while(getRun())
185         try{
186                 QueuedBlockEmerge *qptr = m_server->m_emerge_queue.pop();
187                 if(qptr == NULL)
188                         break;
189
190                 SharedPtr<QueuedBlockEmerge> q(qptr);
191
192                 v3s16 &p = q->pos;
193                 v2s16 p2d(p.X,p.Z);
194
195                 last_tried_pos = p;
196
197                 /*
198                         Do not generate over-limit
199                 */
200                 if(blockpos_over_limit(p))
201                         continue;
202
203                 //infostream<<"EmergeThread::Thread(): running"<<std::endl;
204
205                 //TimeTaker timer("block emerge");
206
207                 /*
208                         Try to emerge it from somewhere.
209
210                         If it is only wanted as optional, only loading from disk
211                         will be allowed.
212                 */
213
214                 /*
215                         Check if any peer wants it as non-optional. In that case it
216                         will be generated.
217
218                         Also decrement the emerge queue count in clients.
219                 */
220
221                 bool only_from_disk = true;
222
223                 {
224                         core::map<u16, u8>::Iterator i;
225                         for(i=q->peer_ids.getIterator(); i.atEnd()==false; i++)
226                         {
227                                 //u16 peer_id = i.getNode()->getKey();
228
229                                 // Check flags
230                                 u8 flags = i.getNode()->getValue();
231                                 if((flags & BLOCK_EMERGE_FLAG_FROMDISK) == false)
232                                         only_from_disk = false;
233
234                         }
235                 }
236
237                 if(enable_mapgen_debug_info)
238                         infostream<<"EmergeThread: p="
239                                         <<"("<<p.X<<","<<p.Y<<","<<p.Z<<") "
240                                         <<"only_from_disk="<<only_from_disk<<std::endl;
241
242
243
244                 MapBlock *block = NULL;
245                 bool got_block = true;
246                 core::map<v3s16, MapBlock*> modified_blocks;
247
248                 /*
249                         Try to fetch block from memory or disk.
250                         If not found and asked to generate, initialize generator.
251                 */
252
253                 bool started_generate = false;
254                 BlockMakeData data;
255
256                 {
257                         JMutexAutoLock envlock(m_server->m_env_mutex);
258
259                         // Load sector if it isn't loaded
260                         if(map.getSectorNoGenerateNoEx(p2d) == NULL)
261                                 map.loadSectorMeta(p2d);
262
263                         // Attempt to load block
264                         block = map.getBlockNoCreateNoEx(p);
265                         if(!block || block->isDummy() || !block->isGenerated())
266                         {
267                                 if(enable_mapgen_debug_info)
268                                         infostream<<"EmergeThread: not in memory, "
269                                                         <<"attempting to load from disk"<<std::endl;
270
271                                 block = map.loadBlock(p);
272                         }
273
274                         // If could not load and allowed to generate, start generation
275                         // inside this same envlock
276                         if(only_from_disk == false &&
277                                         (block == NULL || block->isGenerated() == false)){
278                                 if(enable_mapgen_debug_info)
279                                         infostream<<"EmergeThread: generating"<<std::endl;
280                                 started_generate = true;
281
282                                 map.initBlockMake(&data, p);
283                         }
284                 }
285
286                 /*
287                         If generator was initialized, generate now when envlock is free.
288                 */
289                 if(started_generate)
290                 {
291                         {
292                                 ScopeProfiler sp(g_profiler, "EmergeThread: mapgen::make_block",
293                                                 SPT_AVG);
294                                 TimeTaker t("mapgen::make_block()");
295
296                                 mapgen->makeChunk(&data);
297                                 //mapgen::make_block(&data);
298
299                                 if(enable_mapgen_debug_info == false)
300                                         t.stop(true); // Hide output
301                         }
302
303                         do{ // enable break
304                                 // Lock environment again to access the map
305                                 JMutexAutoLock envlock(m_server->m_env_mutex);
306
307                                 ScopeProfiler sp(g_profiler, "EmergeThread: after "
308                                                 "mapgen::make_block (envlock)", SPT_AVG);
309
310                                 // Blit data back on map, update lighting, add mobs and
311                                 // whatever this does
312                                 map.finishBlockMake(&data, modified_blocks);
313
314                                 // Get central block
315                                 block = map.getBlockNoCreateNoEx(p);
316
317                                 // If block doesn't exist, don't try doing anything with it
318                                 // This happens if the block is not in generation boundaries
319                                 if(!block)
320                                         break;
321
322                                 /*
323                                         Do some post-generate stuff
324                                 */
325
326                                 v3s16 minp = data.blockpos_min*MAP_BLOCKSIZE;
327                                 v3s16 maxp = data.blockpos_max*MAP_BLOCKSIZE +
328                                                 v3s16(1,1,1)*(MAP_BLOCKSIZE-1);
329
330                                 /*
331                                         Ignore map edit events, they will not need to be
332                                         sent to anybody because the block hasn't been sent
333                                         to anybody
334                                 */
335                                 //MapEditEventIgnorer ign(&m_server->m_ignore_map_edit_events);
336                                 MapEditEventAreaIgnorer ign(
337                                                 &m_server->m_ignore_map_edit_events_area,
338                                                 VoxelArea(minp, maxp));
339                                 {
340                                         TimeTaker timer("on_generated");
341                                         scriptapi_environment_on_generated(m_server->m_lua,
342                                                         minp, maxp, emerge->getBlockSeed(minp));
343                                         /*int t = timer.stop(true);
344                                         dstream<<"on_generated took "<<t<<"ms"<<std::endl;*/
345                                 }
346
347                                 if(enable_mapgen_debug_info)
348                                         infostream<<"EmergeThread: ended up with: "
349                                                         <<analyze_block(block)<<std::endl;
350
351                                 // Activate objects and stuff
352                                 m_server->m_env->activateBlock(block, 0);
353                         }while(false);
354                 }
355
356                 if(block == NULL)
357                         got_block = false;
358
359                 /*
360                         Set sent status of modified blocks on clients
361                 */
362
363                 // NOTE: Server's clients are also behind the connection mutex
364                 JMutexAutoLock lock(m_server->m_con_mutex);
365
366                 /*
367                         Add the originally fetched block to the modified list
368                 */
369                 if(got_block)
370                 {
371                         modified_blocks.insert(p, block);
372                 }
373
374                 /*
375                         Set the modified blocks unsent for all the clients
376                 */
377
378                 for(core::map<u16, RemoteClient*>::Iterator
379                                 i = m_server->m_clients.getIterator();
380                                 i.atEnd() == false; i++)
381                 {
382                         RemoteClient *client = i.getNode()->getValue();
383
384                         if(modified_blocks.size() > 0)
385                         {
386                                 // Remove block from sent history
387                                 client->SetBlocksNotSent(modified_blocks);
388                         }
389                 }
390         }
391         catch(VersionMismatchException &e)
392         {
393                 std::ostringstream err;
394                 err<<"World data version mismatch in MapBlock "<<PP(last_tried_pos)<<std::endl;
395                 err<<"----"<<std::endl;
396                 err<<"\""<<e.what()<<"\""<<std::endl;
397                 err<<"See debug.txt."<<std::endl;
398                 err<<"World probably saved by a newer version of Minetest."<<std::endl;
399                 m_server->setAsyncFatalError(err.str());
400         }
401         catch(SerializationError &e)
402         {
403                 std::ostringstream err;
404                 err<<"Invalid data in MapBlock "<<PP(last_tried_pos)<<std::endl;
405                 err<<"----"<<std::endl;
406                 err<<"\""<<e.what()<<"\""<<std::endl;
407                 err<<"See debug.txt."<<std::endl;
408                 err<<"You can ignore this using [ignore_world_load_errors = true]."<<std::endl;
409                 m_server->setAsyncFatalError(err.str());
410         }
411
412         END_DEBUG_EXCEPTION_HANDLER(errorstream)
413
414         log_deregister_thread();
415
416         return NULL;
417 }
418
419 v3f ServerSoundParams::getPos(ServerEnvironment *env, bool *pos_exists) const
420 {
421         if(pos_exists) *pos_exists = false;
422         switch(type){
423         case SSP_LOCAL:
424                 return v3f(0,0,0);
425         case SSP_POSITIONAL:
426                 if(pos_exists) *pos_exists = true;
427                 return pos;
428         case SSP_OBJECT: {
429                 if(object == 0)
430                         return v3f(0,0,0);
431                 ServerActiveObject *sao = env->getActiveObject(object);
432                 if(!sao)
433                         return v3f(0,0,0);
434                 if(pos_exists) *pos_exists = true;
435                 return sao->getBasePosition(); }
436         }
437         return v3f(0,0,0);
438 }
439
440 void RemoteClient::GetNextBlocks(Server *server, float dtime,
441                 core::array<PrioritySortedBlockTransfer> &dest)
442 {
443         DSTACK(__FUNCTION_NAME);
444
445         /*u32 timer_result;
446         TimeTaker timer("RemoteClient::GetNextBlocks", &timer_result);*/
447
448         // Increment timers
449         m_nothing_to_send_pause_timer -= dtime;
450         m_nearest_unsent_reset_timer += dtime;
451
452         if(m_nothing_to_send_pause_timer >= 0)
453                 return;
454
455         Player *player = server->m_env->getPlayer(peer_id);
456         // This can happen sometimes; clients and players are not in perfect sync.
457         if(player == NULL)
458                 return;
459
460         // Won't send anything if already sending
461         if(m_blocks_sending.size() >= g_settings->getU16
462                         ("max_simultaneous_block_sends_per_client"))
463         {
464                 //infostream<<"Not sending any blocks, Queue full."<<std::endl;
465                 return;
466         }
467
468         //TimeTaker timer("RemoteClient::GetNextBlocks");
469
470         v3f playerpos = player->getPosition();
471         v3f playerspeed = player->getSpeed();
472         v3f playerspeeddir(0,0,0);
473         if(playerspeed.getLength() > 1.0*BS)
474                 playerspeeddir = playerspeed / playerspeed.getLength();
475         // Predict to next block
476         v3f playerpos_predicted = playerpos + playerspeeddir*MAP_BLOCKSIZE*BS;
477
478         v3s16 center_nodepos = floatToInt(playerpos_predicted, BS);
479
480         v3s16 center = getNodeBlockPos(center_nodepos);
481
482         // Camera position and direction
483         v3f camera_pos = player->getEyePosition();
484         v3f camera_dir = v3f(0,0,1);
485         camera_dir.rotateYZBy(player->getPitch());
486         camera_dir.rotateXZBy(player->getYaw());
487
488         /*infostream<<"camera_dir=("<<camera_dir.X<<","<<camera_dir.Y<<","
489                         <<camera_dir.Z<<")"<<std::endl;*/
490
491         /*
492                 Get the starting value of the block finder radius.
493         */
494
495         if(m_last_center != center)
496         {
497                 m_nearest_unsent_d = 0;
498                 m_last_center = center;
499         }
500
501         /*infostream<<"m_nearest_unsent_reset_timer="
502                         <<m_nearest_unsent_reset_timer<<std::endl;*/
503
504         // Reset periodically to workaround for some bugs or stuff
505         if(m_nearest_unsent_reset_timer > 20.0)
506         {
507                 m_nearest_unsent_reset_timer = 0;
508                 m_nearest_unsent_d = 0;
509                 //infostream<<"Resetting m_nearest_unsent_d for "
510                 //              <<server->getPlayerName(peer_id)<<std::endl;
511         }
512
513         //s16 last_nearest_unsent_d = m_nearest_unsent_d;
514         s16 d_start = m_nearest_unsent_d;
515
516         //infostream<<"d_start="<<d_start<<std::endl;
517
518         u16 max_simul_sends_setting = g_settings->getU16
519                         ("max_simultaneous_block_sends_per_client");
520         u16 max_simul_sends_usually = max_simul_sends_setting;
521
522         /*
523                 Check the time from last addNode/removeNode.
524
525                 Decrease send rate if player is building stuff.
526         */
527         m_time_from_building += dtime;
528         if(m_time_from_building < g_settings->getFloat(
529                                 "full_block_send_enable_min_time_from_building"))
530         {
531                 max_simul_sends_usually
532                         = LIMITED_MAX_SIMULTANEOUS_BLOCK_SENDS;
533         }
534
535         /*
536                 Number of blocks sending + number of blocks selected for sending
537         */
538         u32 num_blocks_selected = m_blocks_sending.size();
539
540         /*
541                 next time d will be continued from the d from which the nearest
542                 unsent block was found this time.
543
544                 This is because not necessarily any of the blocks found this
545                 time are actually sent.
546         */
547         s32 new_nearest_unsent_d = -1;
548
549         s16 d_max = g_settings->getS16("max_block_send_distance");
550         s16 d_max_gen = g_settings->getS16("max_block_generate_distance");
551
552         // Don't loop very much at a time
553         s16 max_d_increment_at_time = 2;
554         if(d_max > d_start + max_d_increment_at_time)
555                 d_max = d_start + max_d_increment_at_time;
556         /*if(d_max_gen > d_start+2)
557                 d_max_gen = d_start+2;*/
558
559         //infostream<<"Starting from "<<d_start<<std::endl;
560
561         s32 nearest_emerged_d = -1;
562         s32 nearest_emergefull_d = -1;
563         s32 nearest_sent_d = -1;
564         bool queue_is_full = false;
565
566         s16 d;
567         for(d = d_start; d <= d_max; d++)
568         {
569                 /*errorstream<<"checking d="<<d<<" for "
570                                 <<server->getPlayerName(peer_id)<<std::endl;*/
571                 //infostream<<"RemoteClient::SendBlocks(): d="<<d<<std::endl;
572
573                 /*
574                         If m_nearest_unsent_d was changed by the EmergeThread
575                         (it can change it to 0 through SetBlockNotSent),
576                         update our d to it.
577                         Else update m_nearest_unsent_d
578                 */
579                 /*if(m_nearest_unsent_d != last_nearest_unsent_d)
580                 {
581                         d = m_nearest_unsent_d;
582                         last_nearest_unsent_d = m_nearest_unsent_d;
583                 }*/
584
585                 /*
586                         Get the border/face dot coordinates of a "d-radiused"
587                         box
588                 */
589                 core::list<v3s16> list;
590                 getFacePositions(list, d);
591
592                 core::list<v3s16>::Iterator li;
593                 for(li=list.begin(); li!=list.end(); li++)
594                 {
595                         v3s16 p = *li + center;
596
597                         /*
598                                 Send throttling
599                                 - Don't allow too many simultaneous transfers
600                                 - EXCEPT when the blocks are very close
601
602                                 Also, don't send blocks that are already flying.
603                         */
604
605                         // Start with the usual maximum
606                         u16 max_simul_dynamic = max_simul_sends_usually;
607
608                         // If block is very close, allow full maximum
609                         if(d <= BLOCK_SEND_DISABLE_LIMITS_MAX_D)
610                                 max_simul_dynamic = max_simul_sends_setting;
611
612                         // Don't select too many blocks for sending
613                         if(num_blocks_selected >= max_simul_dynamic)
614                         {
615                                 queue_is_full = true;
616                                 goto queue_full_break;
617                         }
618
619                         // Don't send blocks that are currently being transferred
620                         if(m_blocks_sending.find(p) != NULL)
621                                 continue;
622
623                         /*
624                                 Do not go over-limit
625                         */
626                         if(p.X < -MAP_GENERATION_LIMIT / MAP_BLOCKSIZE
627                         || p.X > MAP_GENERATION_LIMIT / MAP_BLOCKSIZE
628                         || p.Y < -MAP_GENERATION_LIMIT / MAP_BLOCKSIZE
629                         || p.Y > MAP_GENERATION_LIMIT / MAP_BLOCKSIZE
630                         || p.Z < -MAP_GENERATION_LIMIT / MAP_BLOCKSIZE
631                         || p.Z > MAP_GENERATION_LIMIT / MAP_BLOCKSIZE)
632                                 continue;
633
634                         // If this is true, inexistent block will be made from scratch
635                         bool generate = d <= d_max_gen;
636
637                         {
638                                 /*// Limit the generating area vertically to 2/3
639                                 if(abs(p.Y - center.Y) > d_max_gen - d_max_gen / 3)
640                                         generate = false;*/
641
642                                 // Limit the send area vertically to 1/2
643                                 if(abs(p.Y - center.Y) > d_max / 2)
644                                         continue;
645                         }
646
647 #if 0
648                         /*
649                                 If block is far away, don't generate it unless it is
650                                 near ground level.
651                         */
652                         if(d >= 4)
653                         {
654         #if 1
655                                 // Block center y in nodes
656                                 f32 y = (f32)(p.Y * MAP_BLOCKSIZE + MAP_BLOCKSIZE/2);
657                                 // Don't generate if it's very high or very low
658                                 if(y < -64 || y > 64)
659                                         generate = false;
660         #endif
661         #if 0
662                                 v2s16 p2d_nodes_center(
663                                         MAP_BLOCKSIZE*p.X,
664                                         MAP_BLOCKSIZE*p.Z);
665
666                                 // Get ground height in nodes
667                                 s16 gh = server->m_env->getServerMap().findGroundLevel(
668                                                 p2d_nodes_center);
669
670                                 // If differs a lot, don't generate
671                                 if(fabs(gh - y) > MAP_BLOCKSIZE*2)
672                                         generate = false;
673                                         // Actually, don't even send it
674                                         //continue;
675         #endif
676                         }
677 #endif
678
679                         //infostream<<"d="<<d<<std::endl;
680 #if 1
681                         /*
682                                 Don't generate or send if not in sight
683                                 FIXME This only works if the client uses a small enough
684                                 FOV setting. The default of 72 degrees is fine.
685                         */
686
687                         float camera_fov = (72.0*M_PI/180) * 4./3.;
688                         if(isBlockInSight(p, camera_pos, camera_dir, camera_fov, 10000*BS) == false)
689                         {
690                                 continue;
691                         }
692 #endif
693                         /*
694                                 Don't send already sent blocks
695                         */
696                         {
697                                 if(m_blocks_sent.find(p) != NULL)
698                                 {
699                                         continue;
700                                 }
701                         }
702
703                         /*
704                                 Check if map has this block
705                         */
706                         MapBlock *block = server->m_env->getMap().getBlockNoCreateNoEx(p);
707
708                         bool surely_not_found_on_disk = false;
709                         bool block_is_invalid = false;
710                         if(block != NULL)
711                         {
712                                 // Reset usage timer, this block will be of use in the future.
713                                 block->resetUsageTimer();
714
715                                 // Block is dummy if data doesn't exist.
716                                 // It means it has been not found from disk and not generated
717                                 if(block->isDummy())
718                                 {
719                                         surely_not_found_on_disk = true;
720                                 }
721
722                                 // Block is valid if lighting is up-to-date and data exists
723                                 if(block->isValid() == false)
724                                 {
725                                         block_is_invalid = true;
726                                 }
727
728                                 /*if(block->isFullyGenerated() == false)
729                                 {
730                                         block_is_invalid = true;
731                                 }*/
732
733 #if 0
734                                 v2s16 p2d(p.X, p.Z);
735                                 ServerMap *map = (ServerMap*)(&server->m_env->getMap());
736                                 v2s16 chunkpos = map->sector_to_chunk(p2d);
737                                 if(map->chunkNonVolatile(chunkpos) == false)
738                                         block_is_invalid = true;
739 #endif
740                                 if(block->isGenerated() == false)
741                                         block_is_invalid = true;
742 #if 1
743                                 /*
744                                         If block is not close, don't send it unless it is near
745                                         ground level.
746
747                                         Block is near ground level if night-time mesh
748                                         differs from day-time mesh.
749                                 */
750                                 if(d >= 4)
751                                 {
752                                         if(block->getDayNightDiff() == false)
753                                                 continue;
754                                 }
755 #endif
756                         }
757
758                         /*
759                                 If block has been marked to not exist on disk (dummy)
760                                 and generating new ones is not wanted, skip block.
761                         */
762                         if(generate == false && surely_not_found_on_disk == true)
763                         {
764                                 // get next one.
765                                 continue;
766                         }
767
768                         /*
769                                 Add inexistent block to emerge queue.
770                         */
771                         if(block == NULL || surely_not_found_on_disk || block_is_invalid)
772                         {
773                                 //TODO: Get value from somewhere
774                                 // Allow only one block in emerge queue
775                                 //if(server->m_emerge_queue.peerItemCount(peer_id) < 1)
776                                 // Allow two blocks in queue per client
777                                 //if(server->m_emerge_queue.peerItemCount(peer_id) < 2)
778                                 u32 max_emerge = 5;
779                                 // Make it more responsive when needing to generate stuff
780                                 if(surely_not_found_on_disk)
781                                         max_emerge = 1;
782                                 if(server->m_emerge_queue.peerItemCount(peer_id) < max_emerge)
783                                 {
784                                         //infostream<<"Adding block to emerge queue"<<std::endl;
785
786                                         // Add it to the emerge queue and trigger the thread
787
788                                         u8 flags = 0;
789                                         if(generate == false)
790                                                 flags |= BLOCK_EMERGE_FLAG_FROMDISK;
791
792                                         server->m_emerge_queue.addBlock(peer_id, p, flags);
793                                         server->m_emergethread.trigger();
794
795                                         if(nearest_emerged_d == -1)
796                                                 nearest_emerged_d = d;
797                                 } else {
798                                         if(nearest_emergefull_d == -1)
799                                                 nearest_emergefull_d = d;
800                                         goto queue_full_break;
801                                 }
802
803                                 // get next one.
804                                 continue;
805                         }
806
807                         if(nearest_sent_d == -1)
808                                 nearest_sent_d = d;
809
810                         /*
811                                 Add block to send queue
812                         */
813
814                         /*errorstream<<"sending from d="<<d<<" to "
815                                         <<server->getPlayerName(peer_id)<<std::endl;*/
816
817                         PrioritySortedBlockTransfer q((float)d, p, peer_id);
818
819                         dest.push_back(q);
820
821                         num_blocks_selected += 1;
822                 }
823         }
824 queue_full_break:
825
826         //infostream<<"Stopped at "<<d<<std::endl;
827
828         // If nothing was found for sending and nothing was queued for
829         // emerging, continue next time browsing from here
830         if(nearest_emerged_d != -1){
831                 new_nearest_unsent_d = nearest_emerged_d;
832         } else if(nearest_emergefull_d != -1){
833                 new_nearest_unsent_d = nearest_emergefull_d;
834         } else {
835                 if(d > g_settings->getS16("max_block_send_distance")){
836                         new_nearest_unsent_d = 0;
837                         m_nothing_to_send_pause_timer = 2.0;
838                         /*infostream<<"GetNextBlocks(): d wrapped around for "
839                                         <<server->getPlayerName(peer_id)
840                                         <<"; setting to 0 and pausing"<<std::endl;*/
841                 } else {
842                         if(nearest_sent_d != -1)
843                                 new_nearest_unsent_d = nearest_sent_d;
844                         else
845                                 new_nearest_unsent_d = d;
846                 }
847         }
848
849         if(new_nearest_unsent_d != -1)
850                 m_nearest_unsent_d = new_nearest_unsent_d;
851
852         /*timer_result = timer.stop(true);
853         if(timer_result != 0)
854                 infostream<<"GetNextBlocks timeout: "<<timer_result<<" (!=0)"<<std::endl;*/
855 }
856
857 void RemoteClient::GotBlock(v3s16 p)
858 {
859         if(m_blocks_sending.find(p) != NULL)
860                 m_blocks_sending.remove(p);
861         else
862         {
863                 /*infostream<<"RemoteClient::GotBlock(): Didn't find in"
864                                 " m_blocks_sending"<<std::endl;*/
865                 m_excess_gotblocks++;
866         }
867         m_blocks_sent.insert(p, true);
868 }
869
870 void RemoteClient::SentBlock(v3s16 p)
871 {
872         if(m_blocks_sending.find(p) == NULL)
873                 m_blocks_sending.insert(p, 0.0);
874         else
875                 infostream<<"RemoteClient::SentBlock(): Sent block"
876                                 " already in m_blocks_sending"<<std::endl;
877 }
878
879 void RemoteClient::SetBlockNotSent(v3s16 p)
880 {
881         m_nearest_unsent_d = 0;
882
883         if(m_blocks_sending.find(p) != NULL)
884                 m_blocks_sending.remove(p);
885         if(m_blocks_sent.find(p) != NULL)
886                 m_blocks_sent.remove(p);
887 }
888
889 void RemoteClient::SetBlocksNotSent(core::map<v3s16, MapBlock*> &blocks)
890 {
891         m_nearest_unsent_d = 0;
892
893         for(core::map<v3s16, MapBlock*>::Iterator
894                         i = blocks.getIterator();
895                         i.atEnd()==false; i++)
896         {
897                 v3s16 p = i.getNode()->getKey();
898
899                 if(m_blocks_sending.find(p) != NULL)
900                         m_blocks_sending.remove(p);
901                 if(m_blocks_sent.find(p) != NULL)
902                         m_blocks_sent.remove(p);
903         }
904 }
905
906 /*
907         PlayerInfo
908 */
909
910 PlayerInfo::PlayerInfo()
911 {
912         name[0] = 0;
913         avg_rtt = 0;
914 }
915
916 void PlayerInfo::PrintLine(std::ostream *s)
917 {
918         (*s)<<id<<": ";
919         (*s)<<"\""<<name<<"\" ("
920                         <<(position.X/10)<<","<<(position.Y/10)
921                         <<","<<(position.Z/10)<<") ";
922         address.print(s);
923         (*s)<<" avg_rtt="<<avg_rtt;
924         (*s)<<std::endl;
925 }
926
927 /*
928         Server
929 */
930
931 Server::Server(
932                 const std::string &path_world,
933                 const std::string &path_config,
934                 const SubgameSpec &gamespec,
935                 bool simple_singleplayer_mode
936         ):
937         m_path_world(path_world),
938         m_path_config(path_config),
939         m_gamespec(gamespec),
940         m_simple_singleplayer_mode(simple_singleplayer_mode),
941         m_async_fatal_error(""),
942         m_env(NULL),
943         m_con(PROTOCOL_ID, 512, CONNECTION_TIMEOUT, this),
944         m_banmanager(path_world+DIR_DELIM+"ipban.txt"),
945         m_rollback(NULL),
946         m_rollback_sink_enabled(true),
947         m_enable_rollback_recording(false),
948         m_emerge(NULL),
949         m_biomedef(NULL),
950         m_lua(NULL),
951         m_itemdef(createItemDefManager()),
952         m_nodedef(createNodeDefManager()),
953         m_craftdef(createCraftDefManager()),
954         m_event(new EventManager()),
955         m_thread(this),
956         m_emergethread(this),
957         m_time_of_day_send_timer(0),
958         m_uptime(0),
959         m_shutdown_requested(false),
960         m_ignore_map_edit_events(false),
961         m_ignore_map_edit_events_peer_id(0)
962 {
963         m_liquid_transform_timer = 0.0;
964         m_print_info_timer = 0.0;
965         m_masterserver_timer = 0.0;
966         m_objectdata_timer = 0.0;
967         m_emergethread_trigger_timer = 0.0;
968         m_savemap_timer = 0.0;
969         m_clients_number = 0;
970
971         m_env_mutex.Init();
972         m_con_mutex.Init();
973         m_step_dtime_mutex.Init();
974         m_step_dtime = 0.0;
975
976         if(path_world == "")
977                 throw ServerError("Supplied empty world path");
978
979         if(!gamespec.isValid())
980                 throw ServerError("Supplied invalid gamespec");
981
982         infostream<<"Server created for gameid \""<<m_gamespec.id<<"\"";
983         if(m_simple_singleplayer_mode)
984                 infostream<<" in simple singleplayer mode"<<std::endl;
985         else
986                 infostream<<std::endl;
987         infostream<<"- world:  "<<m_path_world<<std::endl;
988         infostream<<"- config: "<<m_path_config<<std::endl;
989         infostream<<"- game:   "<<m_gamespec.path<<std::endl;
990
991         // Create biome definition manager
992         m_biomedef = new BiomeDefManager(this);
993
994         // Create rollback manager
995         std::string rollback_path = m_path_world+DIR_DELIM+"rollback.txt";
996         m_rollback = createRollbackManager(rollback_path, this);
997
998         // Create world if it doesn't exist
999         if(!initializeWorld(m_path_world, m_gamespec.id))
1000                 throw ServerError("Failed to initialize world");
1001
1002         ModConfiguration modconf(m_path_world);
1003         m_mods = modconf.getMods();
1004         std::list<ModSpec> unsatisfied_mods = modconf.getUnsatisfiedMods();
1005         // complain about mods with unsatisfied dependencies
1006         if(!modconf.isConsistent())     
1007         {
1008                 for(std::list<ModSpec>::iterator it = unsatisfied_mods.begin();
1009                         it != unsatisfied_mods.end(); ++it)
1010                 {
1011                         ModSpec mod = *it;
1012                         errorstream << "mod \"" << mod.name << "\" has unsatisfied dependencies: ";
1013                         for(std::set<std::string>::iterator dep_it = mod.unsatisfied_depends.begin();
1014                                 dep_it != mod.unsatisfied_depends.end(); ++dep_it)
1015                                 errorstream << " \"" << *dep_it << "\"";
1016                         errorstream << std::endl;
1017                 }
1018         }
1019
1020         Settings worldmt_settings;
1021         std::string worldmt = m_path_world + DIR_DELIM + "world.mt";
1022         worldmt_settings.readConfigFile(worldmt.c_str());
1023         std::vector<std::string> names = worldmt_settings.getNames();
1024         std::set<std::string> exclude_mod_names;
1025         std::set<std::string> load_mod_names;
1026         for(std::vector<std::string>::iterator it = names.begin(); 
1027                 it != names.end(); ++it)
1028         {       
1029                 std::string name = *it;  
1030                 if (name.compare(0,9,"load_mod_")==0)
1031                 {
1032                         if(worldmt_settings.getBool(name))
1033                                 load_mod_names.insert(name.substr(9));
1034                         else                    
1035                                 exclude_mod_names.insert(name.substr(9));
1036                 }
1037         }
1038         // complain about mods declared to be loaded, but not found
1039         for(std::vector<ModSpec>::iterator it = m_mods.begin();
1040                 it != m_mods.end(); ++it)
1041                 load_mod_names.erase((*it).name);
1042         for(std::list<ModSpec>::iterator it = unsatisfied_mods.begin();
1043                 it != unsatisfied_mods.end(); ++it)
1044                 load_mod_names.erase((*it).name);
1045         if(!load_mod_names.empty())
1046         {               
1047                 errorstream << "The following mods could not be found:";
1048                 for(std::set<std::string>::iterator it = load_mod_names.begin();
1049                         it != load_mod_names.end(); ++it)
1050                         errorstream << " \"" << (*it) << "\"";
1051                 errorstream << std::endl;
1052         }
1053
1054         // Path to builtin.lua
1055         std::string builtinpath = getBuiltinLuaPath() + DIR_DELIM + "builtin.lua";
1056
1057         // Lock environment
1058         JMutexAutoLock envlock(m_env_mutex);
1059         JMutexAutoLock conlock(m_con_mutex);
1060
1061         // Initialize scripting
1062
1063         infostream<<"Server: Initializing Lua"<<std::endl;
1064         m_lua = script_init();
1065         assert(m_lua);
1066         // Export API
1067         scriptapi_export(m_lua, this);
1068         // Load and run builtin.lua
1069         infostream<<"Server: Loading builtin.lua [\""
1070                         <<builtinpath<<"\"]"<<std::endl;
1071         bool success = scriptapi_loadmod(m_lua, builtinpath, "__builtin");
1072         if(!success){
1073                 errorstream<<"Server: Failed to load and run "
1074                                 <<builtinpath<<std::endl;
1075                 throw ModError("Failed to load and run "+builtinpath);
1076         }
1077         // Print 'em
1078         infostream<<"Server: Loading mods: ";
1079         for(std::vector<ModSpec>::iterator i = m_mods.begin();
1080                         i != m_mods.end(); i++){
1081                 const ModSpec &mod = *i;
1082                 infostream<<mod.name<<" ";
1083         }
1084         infostream<<std::endl;
1085         // Load and run "mod" scripts
1086         for(std::vector<ModSpec>::iterator i = m_mods.begin();
1087                         i != m_mods.end(); i++){
1088                 const ModSpec &mod = *i;
1089                 std::string scriptpath = mod.path + DIR_DELIM + "init.lua";
1090                 infostream<<"  ["<<padStringRight(mod.name, 12)<<"] [\""
1091                                 <<scriptpath<<"\"]"<<std::endl;
1092                 bool success = scriptapi_loadmod(m_lua, scriptpath, mod.name);
1093                 if(!success){
1094                         errorstream<<"Server: Failed to load and run "
1095                                         <<scriptpath<<std::endl;
1096                         throw ModError("Failed to load and run "+scriptpath);
1097                 }
1098         }
1099
1100         // Read Textures and calculate sha1 sums
1101         fillMediaCache();
1102
1103         // Apply item aliases in the node definition manager
1104         m_nodedef->updateAliases(m_itemdef);
1105
1106         // Add default biomes after nodedef had its aliases added
1107         m_biomedef->addDefaultBiomes();
1108
1109         // Create emerge manager
1110         m_emerge = new EmergeManager(this, m_biomedef);
1111
1112         // Initialize Environment
1113         ServerMap *servermap = new ServerMap(path_world, this, m_emerge);
1114         m_env = new ServerEnvironment(servermap, m_lua, this, this);
1115         
1116         m_emerge->initMapgens(servermap->getMapgenParams());
1117
1118         // Give environment reference to scripting api
1119         scriptapi_add_environment(m_lua, m_env);
1120
1121         // Register us to receive map edit events
1122         servermap->addEventReceiver(this);
1123
1124         // If file exists, load environment metadata
1125         if(fs::PathExists(m_path_world+DIR_DELIM+"env_meta.txt"))
1126         {
1127                 infostream<<"Server: Loading environment metadata"<<std::endl;
1128                 m_env->loadMeta(m_path_world);
1129         }
1130
1131         // Load players
1132         infostream<<"Server: Loading players"<<std::endl;
1133         m_env->deSerializePlayers(m_path_world);
1134
1135         /*
1136                 Add some test ActiveBlockModifiers to environment
1137         */
1138         add_legacy_abms(m_env, m_nodedef);
1139 }
1140
1141 Server::~Server()
1142 {
1143         infostream<<"Server destructing"<<std::endl;
1144
1145         /*
1146                 Send shutdown message
1147         */
1148         {
1149                 JMutexAutoLock conlock(m_con_mutex);
1150
1151                 std::wstring line = L"*** Server shutting down";
1152
1153                 /*
1154                         Send the message to clients
1155                 */
1156                 for(core::map<u16, RemoteClient*>::Iterator
1157                         i = m_clients.getIterator();
1158                         i.atEnd() == false; i++)
1159                 {
1160                         // Get client and check that it is valid
1161                         RemoteClient *client = i.getNode()->getValue();
1162                         assert(client->peer_id == i.getNode()->getKey());
1163                         if(client->serialization_version == SER_FMT_VER_INVALID)
1164                                 continue;
1165
1166                         try{
1167                                 SendChatMessage(client->peer_id, line);
1168                         }
1169                         catch(con::PeerNotFoundException &e)
1170                         {}
1171                 }
1172         }
1173
1174         {
1175                 JMutexAutoLock envlock(m_env_mutex);
1176                 JMutexAutoLock conlock(m_con_mutex);
1177
1178                 /*
1179                         Execute script shutdown hooks
1180                 */
1181                 scriptapi_on_shutdown(m_lua);
1182         }
1183
1184         {
1185                 JMutexAutoLock envlock(m_env_mutex);
1186
1187                 /*
1188                         Save players
1189                 */
1190                 infostream<<"Server: Saving players"<<std::endl;
1191                 m_env->serializePlayers(m_path_world);
1192
1193                 /*
1194                         Save environment metadata
1195                 */
1196                 infostream<<"Server: Saving environment metadata"<<std::endl;
1197                 m_env->saveMeta(m_path_world);
1198         }
1199
1200         /*
1201                 Stop threads
1202         */
1203         stop();
1204
1205         /*
1206                 Delete clients
1207         */
1208         {
1209                 JMutexAutoLock clientslock(m_con_mutex);
1210
1211                 for(core::map<u16, RemoteClient*>::Iterator
1212                         i = m_clients.getIterator();
1213                         i.atEnd() == false; i++)
1214                 {
1215
1216                         // Delete client
1217                         delete i.getNode()->getValue();
1218                 }
1219         }
1220
1221         // Delete things in the reverse order of creation
1222         delete m_env;
1223         delete m_rollback;
1224         delete m_emerge;
1225         delete m_event;
1226         delete m_itemdef;
1227         delete m_nodedef;
1228         delete m_craftdef;
1229
1230         // Deinitialize scripting
1231         infostream<<"Server: Deinitializing scripting"<<std::endl;
1232         script_deinit(m_lua);
1233
1234         // Delete detached inventories
1235         {
1236                 for(std::map<std::string, Inventory*>::iterator
1237                                 i = m_detached_inventories.begin();
1238                                 i != m_detached_inventories.end(); i++){
1239                         delete i->second;
1240                 }
1241         }
1242 }
1243
1244 void Server::start(unsigned short port)
1245 {
1246         DSTACK(__FUNCTION_NAME);
1247         infostream<<"Starting server on port "<<port<<"..."<<std::endl;
1248
1249         // Stop thread if already running
1250         m_thread.stop();
1251
1252         // Initialize connection
1253         m_con.SetTimeoutMs(30);
1254         m_con.Serve(port);
1255
1256         // Start thread
1257         m_thread.setRun(true);
1258         m_thread.Start();
1259
1260         // ASCII art for the win!
1261         actionstream
1262         <<"        .__               __                   __   "<<std::endl
1263         <<"  _____ |__| ____   _____/  |_  ____   _______/  |_ "<<std::endl
1264         <<" /     \\|  |/    \\_/ __ \\   __\\/ __ \\ /  ___/\\   __\\"<<std::endl
1265         <<"|  Y Y  \\  |   |  \\  ___/|  | \\  ___/ \\___ \\  |  |  "<<std::endl
1266         <<"|__|_|  /__|___|  /\\___  >__|  \\___  >____  > |__|  "<<std::endl
1267         <<"      \\/        \\/     \\/          \\/     \\/        "<<std::endl;
1268         actionstream<<"World at ["<<m_path_world<<"]"<<std::endl;
1269         actionstream<<"Server for gameid=\""<<m_gamespec.id
1270                         <<"\" listening on port "<<port<<"."<<std::endl;
1271 }
1272
1273 void Server::stop()
1274 {
1275         DSTACK(__FUNCTION_NAME);
1276
1277         infostream<<"Server: Stopping and waiting threads"<<std::endl;
1278
1279         // Stop threads (set run=false first so both start stopping)
1280         m_thread.setRun(false);
1281         m_emergethread.setRun(false);
1282         m_thread.stop();
1283         m_emergethread.stop();
1284
1285         infostream<<"Server: Threads stopped"<<std::endl;
1286 }
1287
1288 void Server::step(float dtime)
1289 {
1290         DSTACK(__FUNCTION_NAME);
1291         // Limit a bit
1292         if(dtime > 2.0)
1293                 dtime = 2.0;
1294         {
1295                 JMutexAutoLock lock(m_step_dtime_mutex);
1296                 m_step_dtime += dtime;
1297         }
1298         // Throw if fatal error occurred in thread
1299         std::string async_err = m_async_fatal_error.get();
1300         if(async_err != ""){
1301                 throw ServerError(async_err);
1302         }
1303 }
1304
1305 void Server::AsyncRunStep()
1306 {
1307         DSTACK(__FUNCTION_NAME);
1308
1309         g_profiler->add("Server::AsyncRunStep (num)", 1);
1310
1311         float dtime;
1312         {
1313                 JMutexAutoLock lock1(m_step_dtime_mutex);
1314                 dtime = m_step_dtime;
1315         }
1316
1317         {
1318                 // Send blocks to clients
1319                 SendBlocks(dtime);
1320         }
1321
1322         if(dtime < 0.001)
1323                 return;
1324
1325         g_profiler->add("Server::AsyncRunStep with dtime (num)", 1);
1326
1327         //infostream<<"Server steps "<<dtime<<std::endl;
1328         //infostream<<"Server::AsyncRunStep(): dtime="<<dtime<<std::endl;
1329
1330         {
1331                 JMutexAutoLock lock1(m_step_dtime_mutex);
1332                 m_step_dtime -= dtime;
1333         }
1334
1335         /*
1336                 Update uptime
1337         */
1338         {
1339                 m_uptime.set(m_uptime.get() + dtime);
1340         }
1341
1342         {
1343                 // Process connection's timeouts
1344                 JMutexAutoLock lock2(m_con_mutex);
1345                 ScopeProfiler sp(g_profiler, "Server: connection timeout processing");
1346                 m_con.RunTimeouts(dtime);
1347         }
1348
1349         {
1350                 // This has to be called so that the client list gets synced
1351                 // with the peer list of the connection
1352                 handlePeerChanges();
1353         }
1354
1355         /*
1356                 Update time of day and overall game time
1357         */
1358         {
1359                 JMutexAutoLock envlock(m_env_mutex);
1360
1361                 m_env->setTimeOfDaySpeed(g_settings->getFloat("time_speed"));
1362
1363                 /*
1364                         Send to clients at constant intervals
1365                 */
1366
1367                 m_time_of_day_send_timer -= dtime;
1368                 if(m_time_of_day_send_timer < 0.0)
1369                 {
1370                         m_time_of_day_send_timer = g_settings->getFloat("time_send_interval");
1371
1372                         //JMutexAutoLock envlock(m_env_mutex);
1373                         JMutexAutoLock conlock(m_con_mutex);
1374
1375                         for(core::map<u16, RemoteClient*>::Iterator
1376                                 i = m_clients.getIterator();
1377                                 i.atEnd() == false; i++)
1378                         {
1379                                 RemoteClient *client = i.getNode()->getValue();
1380                                 SharedBuffer<u8> data = makePacket_TOCLIENT_TIME_OF_DAY(
1381                                                 m_env->getTimeOfDay(), g_settings->getFloat("time_speed"));
1382                                 // Send as reliable
1383                                 m_con.Send(client->peer_id, 0, data, true);
1384                         }
1385                 }
1386         }
1387
1388         {
1389                 JMutexAutoLock lock(m_env_mutex);
1390                 // Step environment
1391                 ScopeProfiler sp(g_profiler, "SEnv step");
1392                 ScopeProfiler sp2(g_profiler, "SEnv step avg", SPT_AVG);
1393                 m_env->step(dtime);
1394         }
1395
1396         const float map_timer_and_unload_dtime = 2.92;
1397         if(m_map_timer_and_unload_interval.step(dtime, map_timer_and_unload_dtime))
1398         {
1399                 JMutexAutoLock lock(m_env_mutex);
1400                 // Run Map's timers and unload unused data
1401                 ScopeProfiler sp(g_profiler, "Server: map timer and unload");
1402                 m_env->getMap().timerUpdate(map_timer_and_unload_dtime,
1403                                 g_settings->getFloat("server_unload_unused_data_timeout"));
1404         }
1405
1406         /*
1407                 Do background stuff
1408         */
1409
1410         /*
1411                 Handle players
1412         */
1413         {
1414                 JMutexAutoLock lock(m_env_mutex);
1415                 JMutexAutoLock lock2(m_con_mutex);
1416
1417                 ScopeProfiler sp(g_profiler, "Server: handle players");
1418
1419                 for(core::map<u16, RemoteClient*>::Iterator
1420                         i = m_clients.getIterator();
1421                         i.atEnd() == false; i++)
1422                 {
1423                         RemoteClient *client = i.getNode()->getValue();
1424                         PlayerSAO *playersao = getPlayerSAO(client->peer_id);
1425                         if(playersao == NULL)
1426                                 continue;
1427
1428                         /*
1429                                 Handle player HPs (die if hp=0)
1430                         */
1431                         if(playersao->m_hp_not_sent && g_settings->getBool("enable_damage"))
1432                         {
1433                                 if(playersao->getHP() == 0)
1434                                         DiePlayer(client->peer_id);
1435                                 else
1436                                         SendPlayerHP(client->peer_id);
1437                         }
1438
1439                         /*
1440                                 Send player inventories if necessary
1441                         */
1442                         if(playersao->m_moved){
1443                                 SendMovePlayer(client->peer_id);
1444                                 playersao->m_moved = false;
1445                         }
1446                         if(playersao->m_inventory_not_sent){
1447                                 UpdateCrafting(client->peer_id);
1448                                 SendInventory(client->peer_id);
1449                         }
1450                 }
1451         }
1452
1453         /* Transform liquids */
1454         m_liquid_transform_timer += dtime;
1455         if(m_liquid_transform_timer >= 1.00)
1456         {
1457                 m_liquid_transform_timer -= 1.00;
1458
1459                 JMutexAutoLock lock(m_env_mutex);
1460
1461                 ScopeProfiler sp(g_profiler, "Server: liquid transform");
1462
1463                 core::map<v3s16, MapBlock*> modified_blocks;
1464                 m_env->getMap().transformLiquids(modified_blocks);
1465 #if 0
1466                 /*
1467                         Update lighting
1468                 */
1469                 core::map<v3s16, MapBlock*> lighting_modified_blocks;
1470                 ServerMap &map = ((ServerMap&)m_env->getMap());
1471                 map.updateLighting(modified_blocks, lighting_modified_blocks);
1472
1473                 // Add blocks modified by lighting to modified_blocks
1474                 for(core::map<v3s16, MapBlock*>::Iterator
1475                                 i = lighting_modified_blocks.getIterator();
1476                                 i.atEnd() == false; i++)
1477                 {
1478                         MapBlock *block = i.getNode()->getValue();
1479                         modified_blocks.insert(block->getPos(), block);
1480                 }
1481 #endif
1482                 /*
1483                         Set the modified blocks unsent for all the clients
1484                 */
1485
1486                 JMutexAutoLock lock2(m_con_mutex);
1487
1488                 for(core::map<u16, RemoteClient*>::Iterator
1489                                 i = m_clients.getIterator();
1490                                 i.atEnd() == false; i++)
1491                 {
1492                         RemoteClient *client = i.getNode()->getValue();
1493
1494                         if(modified_blocks.size() > 0)
1495                         {
1496                                 // Remove block from sent history
1497                                 client->SetBlocksNotSent(modified_blocks);
1498                         }
1499                 }
1500         }
1501
1502         // Periodically print some info
1503         {
1504                 float &counter = m_print_info_timer;
1505                 counter += dtime;
1506                 if(counter >= 30.0)
1507                 {
1508                         counter = 0.0;
1509
1510                         JMutexAutoLock lock2(m_con_mutex);
1511                         m_clients_number = 0;
1512                         if(m_clients.size() != 0)
1513                                 infostream<<"Players:"<<std::endl;
1514                         for(core::map<u16, RemoteClient*>::Iterator
1515                                 i = m_clients.getIterator();
1516                                 i.atEnd() == false; i++)
1517                         {
1518                                 //u16 peer_id = i.getNode()->getKey();
1519                                 RemoteClient *client = i.getNode()->getValue();
1520                                 Player *player = m_env->getPlayer(client->peer_id);
1521                                 if(player==NULL)
1522                                         continue;
1523                                 infostream<<"* "<<player->getName()<<"\t";
1524                                 client->PrintInfo(infostream);
1525                                 ++m_clients_number;
1526                         }
1527                 }
1528         }
1529
1530
1531 #if USE_CURL
1532         // send masterserver announce
1533         {
1534                 float &counter = m_masterserver_timer;
1535                 if((!counter || counter >= 300.0) && g_settings->getBool("server_announce") == true)
1536                 {
1537                         ServerList::sendAnnounce(!counter ? "start" : "update", m_clients_number);
1538                         counter = 0.01;
1539                 }
1540                 counter += dtime;
1541         }
1542 #endif
1543
1544         //if(g_settings->getBool("enable_experimental"))
1545         {
1546
1547         /*
1548                 Check added and deleted active objects
1549         */
1550         {
1551                 //infostream<<"Server: Checking added and deleted active objects"<<std::endl;
1552                 JMutexAutoLock envlock(m_env_mutex);
1553                 JMutexAutoLock conlock(m_con_mutex);
1554
1555                 ScopeProfiler sp(g_profiler, "Server: checking added and deleted objs");
1556
1557                 // Radius inside which objects are active
1558                 s16 radius = g_settings->getS16("active_object_send_range_blocks");
1559                 radius *= MAP_BLOCKSIZE;
1560
1561                 for(core::map<u16, RemoteClient*>::Iterator
1562                         i = m_clients.getIterator();
1563                         i.atEnd() == false; i++)
1564                 {
1565                         RemoteClient *client = i.getNode()->getValue();
1566
1567                         // If definitions and textures have not been sent, don't
1568                         // send objects either
1569                         if(!client->definitions_sent)
1570                                 continue;
1571
1572                         Player *player = m_env->getPlayer(client->peer_id);
1573                         if(player==NULL)
1574                         {
1575                                 // This can happen if the client timeouts somehow
1576                                 /*infostream<<"WARNING: "<<__FUNCTION_NAME<<": Client "
1577                                                 <<client->peer_id
1578                                                 <<" has no associated player"<<std::endl;*/
1579                                 continue;
1580                         }
1581                         v3s16 pos = floatToInt(player->getPosition(), BS);
1582
1583                         core::map<u16, bool> removed_objects;
1584                         core::map<u16, bool> added_objects;
1585                         m_env->getRemovedActiveObjects(pos, radius,
1586                                         client->m_known_objects, removed_objects);
1587                         m_env->getAddedActiveObjects(pos, radius,
1588                                         client->m_known_objects, added_objects);
1589
1590                         // Ignore if nothing happened
1591                         if(removed_objects.size() == 0 && added_objects.size() == 0)
1592                         {
1593                                 //infostream<<"active objects: none changed"<<std::endl;
1594                                 continue;
1595                         }
1596
1597                         std::string data_buffer;
1598
1599                         char buf[4];
1600
1601                         // Handle removed objects
1602                         writeU16((u8*)buf, removed_objects.size());
1603                         data_buffer.append(buf, 2);
1604                         for(core::map<u16, bool>::Iterator
1605                                         i = removed_objects.getIterator();
1606                                         i.atEnd()==false; i++)
1607                         {
1608                                 // Get object
1609                                 u16 id = i.getNode()->getKey();
1610                                 ServerActiveObject* obj = m_env->getActiveObject(id);
1611
1612                                 // Add to data buffer for sending
1613                                 writeU16((u8*)buf, i.getNode()->getKey());
1614                                 data_buffer.append(buf, 2);
1615
1616                                 // Remove from known objects
1617                                 client->m_known_objects.remove(i.getNode()->getKey());
1618
1619                                 if(obj && obj->m_known_by_count > 0)
1620                                         obj->m_known_by_count--;
1621                         }
1622
1623                         // Handle added objects
1624                         writeU16((u8*)buf, added_objects.size());
1625                         data_buffer.append(buf, 2);
1626                         for(core::map<u16, bool>::Iterator
1627                                         i = added_objects.getIterator();
1628                                         i.atEnd()==false; i++)
1629                         {
1630                                 // Get object
1631                                 u16 id = i.getNode()->getKey();
1632                                 ServerActiveObject* obj = m_env->getActiveObject(id);
1633
1634                                 // Get object type
1635                                 u8 type = ACTIVEOBJECT_TYPE_INVALID;
1636                                 if(obj == NULL)
1637                                         infostream<<"WARNING: "<<__FUNCTION_NAME
1638                                                         <<": NULL object"<<std::endl;
1639                                 else
1640                                         type = obj->getSendType();
1641
1642                                 // Add to data buffer for sending
1643                                 writeU16((u8*)buf, id);
1644                                 data_buffer.append(buf, 2);
1645                                 writeU8((u8*)buf, type);
1646                                 data_buffer.append(buf, 1);
1647
1648                                 if(obj)
1649                                         data_buffer.append(serializeLongString(
1650                                                         obj->getClientInitializationData(client->net_proto_version)));
1651                                 else
1652                                         data_buffer.append(serializeLongString(""));
1653
1654                                 // Add to known objects
1655                                 client->m_known_objects.insert(i.getNode()->getKey(), false);
1656
1657                                 if(obj)
1658                                         obj->m_known_by_count++;
1659                         }
1660
1661                         // Send packet
1662                         SharedBuffer<u8> reply(2 + data_buffer.size());
1663                         writeU16(&reply[0], TOCLIENT_ACTIVE_OBJECT_REMOVE_ADD);
1664                         memcpy((char*)&reply[2], data_buffer.c_str(),
1665                                         data_buffer.size());
1666                         // Send as reliable
1667                         m_con.Send(client->peer_id, 0, reply, true);
1668
1669                         verbosestream<<"Server: Sent object remove/add: "
1670                                         <<removed_objects.size()<<" removed, "
1671                                         <<added_objects.size()<<" added, "
1672                                         <<"packet size is "<<reply.getSize()<<std::endl;
1673                 }
1674
1675 #if 0
1676                 /*
1677                         Collect a list of all the objects known by the clients
1678                         and report it back to the environment.
1679                 */
1680
1681                 core::map<u16, bool> all_known_objects;
1682
1683                 for(core::map<u16, RemoteClient*>::Iterator
1684                         i = m_clients.getIterator();
1685                         i.atEnd() == false; i++)
1686                 {
1687                         RemoteClient *client = i.getNode()->getValue();
1688                         // Go through all known objects of client
1689                         for(core::map<u16, bool>::Iterator
1690                                         i = client->m_known_objects.getIterator();
1691                                         i.atEnd()==false; i++)
1692                         {
1693                                 u16 id = i.getNode()->getKey();
1694                                 all_known_objects[id] = true;
1695                         }
1696                 }
1697
1698                 m_env->setKnownActiveObjects(whatever);
1699 #endif
1700
1701         }
1702
1703         /*
1704                 Send object messages
1705         */
1706         {
1707                 JMutexAutoLock envlock(m_env_mutex);
1708                 JMutexAutoLock conlock(m_con_mutex);
1709
1710                 ScopeProfiler sp(g_profiler, "Server: sending object messages");
1711
1712                 // Key = object id
1713                 // Value = data sent by object
1714                 core::map<u16, core::list<ActiveObjectMessage>* > buffered_messages;
1715
1716                 // Get active object messages from environment
1717                 for(;;)
1718                 {
1719                         ActiveObjectMessage aom = m_env->getActiveObjectMessage();
1720                         if(aom.id == 0)
1721                                 break;
1722
1723                         core::list<ActiveObjectMessage>* message_list = NULL;
1724                         core::map<u16, core::list<ActiveObjectMessage>* >::Node *n;
1725                         n = buffered_messages.find(aom.id);
1726                         if(n == NULL)
1727                         {
1728                                 message_list = new core::list<ActiveObjectMessage>;
1729                                 buffered_messages.insert(aom.id, message_list);
1730                         }
1731                         else
1732                         {
1733                                 message_list = n->getValue();
1734                         }
1735                         message_list->push_back(aom);
1736                 }
1737
1738                 // Route data to every client
1739                 for(core::map<u16, RemoteClient*>::Iterator
1740                         i = m_clients.getIterator();
1741                         i.atEnd()==false; i++)
1742                 {
1743                         RemoteClient *client = i.getNode()->getValue();
1744                         std::string reliable_data;
1745                         std::string unreliable_data;
1746                         // Go through all objects in message buffer
1747                         for(core::map<u16, core::list<ActiveObjectMessage>* >::Iterator
1748                                         j = buffered_messages.getIterator();
1749                                         j.atEnd()==false; j++)
1750                         {
1751                                 // If object is not known by client, skip it
1752                                 u16 id = j.getNode()->getKey();
1753                                 if(client->m_known_objects.find(id) == NULL)
1754                                         continue;
1755                                 // Get message list of object
1756                                 core::list<ActiveObjectMessage>* list = j.getNode()->getValue();
1757                                 // Go through every message
1758                                 for(core::list<ActiveObjectMessage>::Iterator
1759                                                 k = list->begin(); k != list->end(); k++)
1760                                 {
1761                                         // Compose the full new data with header
1762                                         ActiveObjectMessage aom = *k;
1763                                         std::string new_data;
1764                                         // Add object id
1765                                         char buf[2];
1766                                         writeU16((u8*)&buf[0], aom.id);
1767                                         new_data.append(buf, 2);
1768                                         // Add data
1769                                         new_data += serializeString(aom.datastring);
1770                                         // Add data to buffer
1771                                         if(aom.reliable)
1772                                                 reliable_data += new_data;
1773                                         else
1774                                                 unreliable_data += new_data;
1775                                 }
1776                         }
1777                         /*
1778                                 reliable_data and unreliable_data are now ready.
1779                                 Send them.
1780                         */
1781                         if(reliable_data.size() > 0)
1782                         {
1783                                 SharedBuffer<u8> reply(2 + reliable_data.size());
1784                                 writeU16(&reply[0], TOCLIENT_ACTIVE_OBJECT_MESSAGES);
1785                                 memcpy((char*)&reply[2], reliable_data.c_str(),
1786                                                 reliable_data.size());
1787                                 // Send as reliable
1788                                 m_con.Send(client->peer_id, 0, reply, true);
1789                         }
1790                         if(unreliable_data.size() > 0)
1791                         {
1792                                 SharedBuffer<u8> reply(2 + unreliable_data.size());
1793                                 writeU16(&reply[0], TOCLIENT_ACTIVE_OBJECT_MESSAGES);
1794                                 memcpy((char*)&reply[2], unreliable_data.c_str(),
1795                                                 unreliable_data.size());
1796                                 // Send as unreliable
1797                                 m_con.Send(client->peer_id, 0, reply, false);
1798                         }
1799
1800                         /*if(reliable_data.size() > 0 || unreliable_data.size() > 0)
1801                         {
1802                                 infostream<<"Server: Size of object message data: "
1803                                                 <<"reliable: "<<reliable_data.size()
1804                                                 <<", unreliable: "<<unreliable_data.size()
1805                                                 <<std::endl;
1806                         }*/
1807                 }
1808
1809                 // Clear buffered_messages
1810                 for(core::map<u16, core::list<ActiveObjectMessage>* >::Iterator
1811                                 i = buffered_messages.getIterator();
1812                                 i.atEnd()==false; i++)
1813                 {
1814                         delete i.getNode()->getValue();
1815                 }
1816         }
1817
1818         } // enable_experimental
1819
1820         /*
1821                 Send queued-for-sending map edit events.
1822         */
1823         {
1824                 // We will be accessing the environment and the connection
1825                 JMutexAutoLock lock(m_env_mutex);
1826                 JMutexAutoLock conlock(m_con_mutex);
1827
1828                 // Don't send too many at a time
1829                 //u32 count = 0;
1830
1831                 // Single change sending is disabled if queue size is not small
1832                 bool disable_single_change_sending = false;
1833                 if(m_unsent_map_edit_queue.size() >= 4)
1834                         disable_single_change_sending = true;
1835
1836                 int event_count = m_unsent_map_edit_queue.size();
1837
1838                 // We'll log the amount of each
1839                 Profiler prof;
1840
1841                 while(m_unsent_map_edit_queue.size() != 0)
1842                 {
1843                         MapEditEvent* event = m_unsent_map_edit_queue.pop_front();
1844
1845                         // Players far away from the change are stored here.
1846                         // Instead of sending the changes, MapBlocks are set not sent
1847                         // for them.
1848                         core::list<u16> far_players;
1849
1850                         if(event->type == MEET_ADDNODE)
1851                         {
1852                                 //infostream<<"Server: MEET_ADDNODE"<<std::endl;
1853                                 prof.add("MEET_ADDNODE", 1);
1854                                 if(disable_single_change_sending)
1855                                         sendAddNode(event->p, event->n, event->already_known_by_peer,
1856                                                         &far_players, 5);
1857                                 else
1858                                         sendAddNode(event->p, event->n, event->already_known_by_peer,
1859                                                         &far_players, 30);
1860                         }
1861                         else if(event->type == MEET_REMOVENODE)
1862                         {
1863                                 //infostream<<"Server: MEET_REMOVENODE"<<std::endl;
1864                                 prof.add("MEET_REMOVENODE", 1);
1865                                 if(disable_single_change_sending)
1866                                         sendRemoveNode(event->p, event->already_known_by_peer,
1867                                                         &far_players, 5);
1868                                 else
1869                                         sendRemoveNode(event->p, event->already_known_by_peer,
1870                                                         &far_players, 30);
1871                         }
1872                         else if(event->type == MEET_BLOCK_NODE_METADATA_CHANGED)
1873                         {
1874                                 infostream<<"Server: MEET_BLOCK_NODE_METADATA_CHANGED"<<std::endl;
1875                                 prof.add("MEET_BLOCK_NODE_METADATA_CHANGED", 1);
1876                                 setBlockNotSent(event->p);
1877                         }
1878                         else if(event->type == MEET_OTHER)
1879                         {
1880                                 infostream<<"Server: MEET_OTHER"<<std::endl;
1881                                 prof.add("MEET_OTHER", 1);
1882                                 for(core::map<v3s16, bool>::Iterator
1883                                                 i = event->modified_blocks.getIterator();
1884                                                 i.atEnd()==false; i++)
1885                                 {
1886                                         v3s16 p = i.getNode()->getKey();
1887                                         setBlockNotSent(p);
1888                                 }
1889                         }
1890                         else
1891                         {
1892                                 prof.add("unknown", 1);
1893                                 infostream<<"WARNING: Server: Unknown MapEditEvent "
1894                                                 <<((u32)event->type)<<std::endl;
1895                         }
1896
1897                         /*
1898                                 Set blocks not sent to far players
1899                         */
1900                         if(far_players.size() > 0)
1901                         {
1902                                 // Convert list format to that wanted by SetBlocksNotSent
1903                                 core::map<v3s16, MapBlock*> modified_blocks2;
1904                                 for(core::map<v3s16, bool>::Iterator
1905                                                 i = event->modified_blocks.getIterator();
1906                                                 i.atEnd()==false; i++)
1907                                 {
1908                                         v3s16 p = i.getNode()->getKey();
1909                                         modified_blocks2.insert(p,
1910                                                         m_env->getMap().getBlockNoCreateNoEx(p));
1911                                 }
1912                                 // Set blocks not sent
1913                                 for(core::list<u16>::Iterator
1914                                                 i = far_players.begin();
1915                                                 i != far_players.end(); i++)
1916                                 {
1917                                         u16 peer_id = *i;
1918                                         RemoteClient *client = getClient(peer_id);
1919                                         if(client==NULL)
1920                                                 continue;
1921                                         client->SetBlocksNotSent(modified_blocks2);
1922                                 }
1923                         }
1924
1925                         delete event;
1926
1927                         /*// Don't send too many at a time
1928                         count++;
1929                         if(count >= 1 && m_unsent_map_edit_queue.size() < 100)
1930                                 break;*/
1931                 }
1932
1933                 if(event_count >= 5){
1934                         infostream<<"Server: MapEditEvents:"<<std::endl;
1935                         prof.print(infostream);
1936                 } else if(event_count != 0){
1937                         verbosestream<<"Server: MapEditEvents:"<<std::endl;
1938                         prof.print(verbosestream);
1939                 }
1940
1941         }
1942
1943         /*
1944                 Trigger emergethread (it somehow gets to a non-triggered but
1945                 bysy state sometimes)
1946         */
1947         {
1948                 float &counter = m_emergethread_trigger_timer;
1949                 counter += dtime;
1950                 if(counter >= 2.0)
1951                 {
1952                         counter = 0.0;
1953
1954                         m_emergethread.trigger();
1955
1956                         // Update m_enable_rollback_recording here too
1957                         m_enable_rollback_recording =
1958                                         g_settings->getBool("enable_rollback_recording");
1959                 }
1960         }
1961
1962         // Save map, players and auth stuff
1963         {
1964                 float &counter = m_savemap_timer;
1965                 counter += dtime;
1966                 if(counter >= g_settings->getFloat("server_map_save_interval"))
1967                 {
1968                         counter = 0.0;
1969                         JMutexAutoLock lock(m_env_mutex);
1970
1971                         ScopeProfiler sp(g_profiler, "Server: saving stuff");
1972
1973                         //Ban stuff
1974                         if(m_banmanager.isModified())
1975                                 m_banmanager.save();
1976
1977                         // Save changed parts of map
1978                         m_env->getMap().save(MOD_STATE_WRITE_NEEDED);
1979
1980                         // Save players
1981                         m_env->serializePlayers(m_path_world);
1982
1983                         // Save environment metadata
1984                         m_env->saveMeta(m_path_world);
1985                 }
1986         }
1987 }
1988
1989 void Server::Receive()
1990 {
1991         DSTACK(__FUNCTION_NAME);
1992         SharedBuffer<u8> data;
1993         u16 peer_id;
1994         u32 datasize;
1995         try{
1996                 {
1997                         JMutexAutoLock conlock(m_con_mutex);
1998                         datasize = m_con.Receive(peer_id, data);
1999                 }
2000
2001                 // This has to be called so that the client list gets synced
2002                 // with the peer list of the connection
2003                 handlePeerChanges();
2004
2005                 ProcessData(*data, datasize, peer_id);
2006         }
2007         catch(con::InvalidIncomingDataException &e)
2008         {
2009                 infostream<<"Server::Receive(): "
2010                                 "InvalidIncomingDataException: what()="
2011                                 <<e.what()<<std::endl;
2012         }
2013         catch(con::PeerNotFoundException &e)
2014         {
2015                 //NOTE: This is not needed anymore
2016
2017                 // The peer has been disconnected.
2018                 // Find the associated player and remove it.
2019
2020                 /*JMutexAutoLock envlock(m_env_mutex);
2021
2022                 infostream<<"ServerThread: peer_id="<<peer_id
2023                                 <<" has apparently closed connection. "
2024                                 <<"Removing player."<<std::endl;
2025
2026                 m_env->removePlayer(peer_id);*/
2027         }
2028 }
2029
2030 void Server::ProcessData(u8 *data, u32 datasize, u16 peer_id)
2031 {
2032         DSTACK(__FUNCTION_NAME);
2033         // Environment is locked first.
2034         JMutexAutoLock envlock(m_env_mutex);
2035         JMutexAutoLock conlock(m_con_mutex);
2036
2037         ScopeProfiler sp(g_profiler, "Server::ProcessData");
2038
2039         try{
2040                 Address address = m_con.GetPeerAddress(peer_id);
2041                 std::string addr_s = address.serializeString();
2042
2043                 // drop player if is ip is banned
2044                 if(m_banmanager.isIpBanned(addr_s)){
2045                         infostream<<"Server: A banned client tried to connect from "
2046                                         <<addr_s<<"; banned name was "
2047                                         <<m_banmanager.getBanName(addr_s)<<std::endl;
2048                         // This actually doesn't seem to transfer to the client
2049                         SendAccessDenied(m_con, peer_id,
2050                                         L"Your ip is banned. Banned name was "
2051                                         +narrow_to_wide(m_banmanager.getBanName(addr_s)));
2052                         m_con.DeletePeer(peer_id);
2053                         return;
2054                 }
2055         }
2056         catch(con::PeerNotFoundException &e)
2057         {
2058                 infostream<<"Server::ProcessData(): Cancelling: peer "
2059                                 <<peer_id<<" not found"<<std::endl;
2060                 return;
2061         }
2062
2063         std::string addr_s = m_con.GetPeerAddress(peer_id).serializeString();
2064
2065         u8 peer_ser_ver = getClient(peer_id)->serialization_version;
2066
2067         try
2068         {
2069
2070         if(datasize < 2)
2071                 return;
2072
2073         ToServerCommand command = (ToServerCommand)readU16(&data[0]);
2074
2075         if(command == TOSERVER_INIT)
2076         {
2077                 // [0] u16 TOSERVER_INIT
2078                 // [2] u8 SER_FMT_VER_HIGHEST
2079                 // [3] u8[20] player_name
2080                 // [23] u8[28] password <--- can be sent without this, from old versions
2081
2082                 if(datasize < 2+1+PLAYERNAME_SIZE)
2083                         return;
2084
2085                 verbosestream<<"Server: Got TOSERVER_INIT from "
2086                                 <<peer_id<<std::endl;
2087
2088                 // First byte after command is maximum supported
2089                 // serialization version
2090                 u8 client_max = data[2];
2091                 u8 our_max = SER_FMT_VER_HIGHEST;
2092                 // Use the highest version supported by both
2093                 u8 deployed = core::min_(client_max, our_max);
2094                 // If it's lower than the lowest supported, give up.
2095                 if(deployed < SER_FMT_VER_LOWEST)
2096                         deployed = SER_FMT_VER_INVALID;
2097
2098                 //peer->serialization_version = deployed;
2099                 getClient(peer_id)->pending_serialization_version = deployed;
2100
2101                 if(deployed == SER_FMT_VER_INVALID)
2102                 {
2103                         actionstream<<"Server: A mismatched client tried to connect from "
2104                                         <<addr_s<<std::endl;
2105                         infostream<<"Server: Cannot negotiate "
2106                                         "serialization version with peer "
2107                                         <<peer_id<<std::endl;
2108                         SendAccessDenied(m_con, peer_id, std::wstring(
2109                                         L"Your client's version is not supported.\n"
2110                                         L"Server version is ")
2111                                         + narrow_to_wide(VERSION_STRING) + L"."
2112                         );
2113                         return;
2114                 }
2115
2116                 /*
2117                         Read and check network protocol version
2118                 */
2119
2120                 u16 min_net_proto_version = 0;
2121                 if(datasize >= 2+1+PLAYERNAME_SIZE+PASSWORD_SIZE+2)
2122                         min_net_proto_version = readU16(&data[2+1+PLAYERNAME_SIZE+PASSWORD_SIZE]);
2123
2124                 // Use same version as minimum and maximum if maximum version field
2125                 // doesn't exist (backwards compatibility)
2126                 u16 max_net_proto_version = min_net_proto_version;
2127                 if(datasize >= 2+1+PLAYERNAME_SIZE+PASSWORD_SIZE+2+2)
2128                         max_net_proto_version = readU16(&data[2+1+PLAYERNAME_SIZE+PASSWORD_SIZE+2]);
2129
2130                 // Start with client's maximum version
2131                 u16 net_proto_version = max_net_proto_version;
2132
2133                 // Figure out a working version if it is possible at all
2134                 if(max_net_proto_version >= SERVER_PROTOCOL_VERSION_MIN ||
2135                                 min_net_proto_version <= SERVER_PROTOCOL_VERSION_MAX)
2136                 {
2137                         // If maximum is larger than our maximum, go with our maximum
2138                         if(max_net_proto_version > SERVER_PROTOCOL_VERSION_MAX)
2139                                 net_proto_version = SERVER_PROTOCOL_VERSION_MAX;
2140                         // Else go with client's maximum
2141                         else
2142                                 net_proto_version = max_net_proto_version;
2143                 }
2144
2145                 verbosestream<<"Server: "<<peer_id<<" Protocol version: min: "
2146                                 <<min_net_proto_version<<", max: "<<max_net_proto_version
2147                                 <<", chosen: "<<net_proto_version<<std::endl;
2148
2149                 getClient(peer_id)->net_proto_version = net_proto_version;
2150
2151                 if(net_proto_version < SERVER_PROTOCOL_VERSION_MIN ||
2152                                 net_proto_version > SERVER_PROTOCOL_VERSION_MAX)
2153                 {
2154                         actionstream<<"Server: A mismatched client tried to connect from "<<addr_s
2155                                         <<std::endl;
2156                         SendAccessDenied(m_con, peer_id, std::wstring(
2157                                         L"Your client's version is not supported.\n"
2158                                         L"Server version is ")
2159                                         + narrow_to_wide(VERSION_STRING) + L",\n"
2160                                         + L"server's PROTOCOL_VERSION is "
2161                                         + narrow_to_wide(itos(SERVER_PROTOCOL_VERSION_MIN))
2162                                         + L"..."
2163                                         + narrow_to_wide(itos(SERVER_PROTOCOL_VERSION_MAX))
2164                                         + L", client's PROTOCOL_VERSION is "
2165                                         + narrow_to_wide(itos(min_net_proto_version))
2166                                         + L"..."
2167                                         + narrow_to_wide(itos(max_net_proto_version))
2168                         );
2169                         return;
2170                 }
2171
2172                 if(g_settings->getBool("strict_protocol_version_checking"))
2173                 {
2174                         if(net_proto_version != LATEST_PROTOCOL_VERSION)
2175                         {
2176                                 actionstream<<"Server: A mismatched (strict) client tried to "
2177                                                 <<"connect from "<<addr_s<<std::endl;
2178                                 SendAccessDenied(m_con, peer_id, std::wstring(
2179                                                 L"Your client's version is not supported.\n"
2180                                                 L"Server version is ")
2181                                                 + narrow_to_wide(VERSION_STRING) + L",\n"
2182                                                 + L"server's PROTOCOL_VERSION (strict) is "
2183                                                 + narrow_to_wide(itos(LATEST_PROTOCOL_VERSION))
2184                                                 + L", client's PROTOCOL_VERSION is "
2185                                                 + narrow_to_wide(itos(min_net_proto_version))
2186                                                 + L"..."
2187                                                 + narrow_to_wide(itos(max_net_proto_version))
2188                                 );
2189                                 return;
2190                         }
2191                 }
2192
2193                 /*
2194                         Set up player
2195                 */
2196
2197                 // Get player name
2198                 char playername[PLAYERNAME_SIZE];
2199                 for(u32 i=0; i<PLAYERNAME_SIZE-1; i++)
2200                 {
2201                         playername[i] = data[3+i];
2202                 }
2203                 playername[PLAYERNAME_SIZE-1] = 0;
2204
2205                 if(playername[0]=='\0')
2206                 {
2207                         actionstream<<"Server: Player with an empty name "
2208                                         <<"tried to connect from "<<addr_s<<std::endl;
2209                         SendAccessDenied(m_con, peer_id,
2210                                         L"Empty name");
2211                         return;
2212                 }
2213
2214                 if(string_allowed(playername, PLAYERNAME_ALLOWED_CHARS)==false)
2215                 {
2216                         actionstream<<"Server: Player with an invalid name "
2217                                         <<"tried to connect from "<<addr_s<<std::endl;
2218                         SendAccessDenied(m_con, peer_id,
2219                                         L"Name contains unallowed characters");
2220                         return;
2221                 }
2222
2223                 infostream<<"Server: New connection: \""<<playername<<"\" from "
2224                                 <<m_con.GetPeerAddress(peer_id).serializeString()<<std::endl;
2225
2226                 // Get password
2227                 char given_password[PASSWORD_SIZE];
2228                 if(datasize < 2+1+PLAYERNAME_SIZE+PASSWORD_SIZE)
2229                 {
2230                         // old version - assume blank password
2231                         given_password[0] = 0;
2232                 }
2233                 else
2234                 {
2235                         for(u32 i=0; i<PASSWORD_SIZE-1; i++)
2236                         {
2237                                 given_password[i] = data[23+i];
2238                         }
2239                         given_password[PASSWORD_SIZE-1] = 0;
2240                 }
2241
2242                 if(!base64_is_valid(given_password)){
2243                         infostream<<"Server: "<<playername
2244                                         <<" supplied invalid password hash"<<std::endl;
2245                         SendAccessDenied(m_con, peer_id, L"Invalid password hash");
2246                         return;
2247                 }
2248
2249                 std::string checkpwd; // Password hash to check against
2250                 bool has_auth = scriptapi_get_auth(m_lua, playername, &checkpwd, NULL);
2251
2252                 // If no authentication info exists for user, create it
2253                 if(!has_auth){
2254                         if(!isSingleplayer() &&
2255                                         g_settings->getBool("disallow_empty_password") &&
2256                                         std::string(given_password) == ""){
2257                                 SendAccessDenied(m_con, peer_id, L"Empty passwords are "
2258                                                 L"disallowed. Set a password and try again.");
2259                                 return;
2260                         }
2261                         std::wstring raw_default_password =
2262                                 narrow_to_wide(g_settings->get("default_password"));
2263                         std::string initial_password =
2264                                 translatePassword(playername, raw_default_password);
2265
2266                         // If default_password is empty, allow any initial password
2267                         if (raw_default_password.length() == 0)
2268                                 initial_password = given_password;
2269
2270                         scriptapi_create_auth(m_lua, playername, initial_password);
2271                 }
2272
2273                 has_auth = scriptapi_get_auth(m_lua, playername, &checkpwd, NULL);
2274
2275                 if(!has_auth){
2276                         SendAccessDenied(m_con, peer_id, L"Not allowed to login");
2277                         return;
2278                 }
2279
2280                 if(given_password != checkpwd){
2281                         infostream<<"Server: peer_id="<<peer_id
2282                                         <<": supplied invalid password for "
2283                                         <<playername<<std::endl;
2284                         SendAccessDenied(m_con, peer_id, L"Invalid password");
2285                         return;
2286                 }
2287
2288                 // Do not allow multiple players in simple singleplayer mode.
2289                 // This isn't a perfect way to do it, but will suffice for now.
2290                 if(m_simple_singleplayer_mode && m_clients.size() > 1){
2291                         infostream<<"Server: Not allowing another client to connect in"
2292                                         <<" simple singleplayer mode"<<std::endl;
2293                         SendAccessDenied(m_con, peer_id,
2294                                         L"Running in simple singleplayer mode.");
2295                         return;
2296                 }
2297
2298                 // Enforce user limit.
2299                 // Don't enforce for users that have some admin right
2300                 if(m_clients.size() >= g_settings->getU16("max_users") &&
2301                                 !checkPriv(playername, "server") &&
2302                                 !checkPriv(playername, "ban") &&
2303                                 !checkPriv(playername, "privs") &&
2304                                 !checkPriv(playername, "password") &&
2305                                 playername != g_settings->get("name"))
2306                 {
2307                         actionstream<<"Server: "<<playername<<" tried to join, but there"
2308                                         <<" are already max_users="
2309                                         <<g_settings->getU16("max_users")<<" players."<<std::endl;
2310                         SendAccessDenied(m_con, peer_id, L"Too many users.");
2311                         return;
2312                 }
2313
2314                 // Get player
2315                 PlayerSAO *playersao = emergePlayer(playername, peer_id);
2316
2317                 // If failed, cancel
2318                 if(playersao == NULL)
2319                 {
2320                         errorstream<<"Server: peer_id="<<peer_id
2321                                         <<": failed to emerge player"<<std::endl;
2322                         return;
2323                 }
2324
2325                 /*
2326                         Answer with a TOCLIENT_INIT
2327                 */
2328                 {
2329                         SharedBuffer<u8> reply(2+1+6+8+4);
2330                         writeU16(&reply[0], TOCLIENT_INIT);
2331                         writeU8(&reply[2], deployed);
2332                         writeV3S16(&reply[2+1], floatToInt(playersao->getPlayer()->getPosition()+v3f(0,BS/2,0), BS));
2333                         writeU64(&reply[2+1+6], m_env->getServerMap().getSeed());
2334                         writeF1000(&reply[2+1+6+8], g_settings->getFloat("dedicated_server_step"));
2335
2336                         // Send as reliable
2337                         m_con.Send(peer_id, 0, reply, true);
2338                 }
2339
2340                 /*
2341                         Send complete position information
2342                 */
2343                 SendMovePlayer(peer_id);
2344
2345                 return;
2346         }
2347
2348         if(command == TOSERVER_INIT2)
2349         {
2350                 verbosestream<<"Server: Got TOSERVER_INIT2 from "
2351                                 <<peer_id<<std::endl;
2352
2353                 Player *player = m_env->getPlayer(peer_id);
2354                 if(!player){
2355                         verbosestream<<"Server: TOSERVER_INIT2: "
2356                                         <<"Player not found; ignoring."<<std::endl;
2357                         return;
2358                 }
2359
2360                 RemoteClient *client = getClient(peer_id);
2361                 client->serialization_version =
2362                                 getClient(peer_id)->pending_serialization_version;
2363
2364                 /*
2365                         Send some initialization data
2366                 */
2367
2368                 infostream<<"Server: Sending content to "
2369                                 <<getPlayerName(peer_id)<<std::endl;
2370
2371                 // Send player movement settings
2372                 SendMovement(m_con, peer_id);
2373
2374                 // Send item definitions
2375                 SendItemDef(m_con, peer_id, m_itemdef);
2376
2377                 // Send node definitions
2378                 SendNodeDef(m_con, peer_id, m_nodedef, client->net_proto_version);
2379
2380                 // Send media announcement
2381                 sendMediaAnnouncement(peer_id);
2382
2383                 // Send privileges
2384                 SendPlayerPrivileges(peer_id);
2385
2386                 // Send inventory formspec
2387                 SendPlayerInventoryFormspec(peer_id);
2388
2389                 // Send inventory
2390                 UpdateCrafting(peer_id);
2391                 SendInventory(peer_id);
2392
2393                 // Send HP
2394                 if(g_settings->getBool("enable_damage"))
2395                         SendPlayerHP(peer_id);
2396
2397                 // Send detached inventories
2398                 sendDetachedInventories(peer_id);
2399
2400                 // Show death screen if necessary
2401                 if(player->hp == 0)
2402                         SendDeathscreen(m_con, peer_id, false, v3f(0,0,0));
2403
2404                 // Send time of day
2405                 {
2406                         SharedBuffer<u8> data = makePacket_TOCLIENT_TIME_OF_DAY(
2407                                         m_env->getTimeOfDay(), g_settings->getFloat("time_speed"));
2408                         m_con.Send(peer_id, 0, data, true);
2409                 }
2410
2411                 // Note things in chat if not in simple singleplayer mode
2412                 if(!m_simple_singleplayer_mode)
2413                 {
2414                         // Send information about server to player in chat
2415                         SendChatMessage(peer_id, getStatusString());
2416
2417                         // Send information about joining in chat
2418                         {
2419                                 std::wstring name = L"unknown";
2420                                 Player *player = m_env->getPlayer(peer_id);
2421                                 if(player != NULL)
2422                                         name = narrow_to_wide(player->getName());
2423
2424                                 std::wstring message;
2425                                 message += L"*** ";
2426                                 message += name;
2427                                 message += L" joined the game.";
2428                                 BroadcastChatMessage(message);
2429                         }
2430                 }
2431
2432                 // Warnings about protocol version can be issued here
2433                 if(getClient(peer_id)->net_proto_version < LATEST_PROTOCOL_VERSION)
2434                 {
2435                         SendChatMessage(peer_id, L"# Server: WARNING: YOUR CLIENT'S "
2436                                         L"VERSION MAY NOT BE FULLY COMPATIBLE WITH THIS SERVER!");
2437                 }
2438
2439                 /*
2440                         Print out action
2441                 */
2442                 {
2443                         std::ostringstream os(std::ios_base::binary);
2444                         for(core::map<u16, RemoteClient*>::Iterator
2445                                 i = m_clients.getIterator();
2446                                 i.atEnd() == false; i++)
2447                         {
2448                                 RemoteClient *client = i.getNode()->getValue();
2449                                 assert(client->peer_id == i.getNode()->getKey());
2450                                 if(client->serialization_version == SER_FMT_VER_INVALID)
2451                                         continue;
2452                                 // Get player
2453                                 Player *player = m_env->getPlayer(client->peer_id);
2454                                 if(!player)
2455                                         continue;
2456                                 // Get name of player
2457                                 os<<player->getName()<<" ";
2458                         }
2459
2460                         actionstream<<player->getName()<<" joins game. List of players: "
2461                                         <<os.str()<<std::endl;
2462                 }
2463
2464                 return;
2465         }
2466
2467         if(peer_ser_ver == SER_FMT_VER_INVALID)
2468         {
2469                 infostream<<"Server::ProcessData(): Cancelling: Peer"
2470                                 " serialization format invalid or not initialized."
2471                                 " Skipping incoming command="<<command<<std::endl;
2472                 return;
2473         }
2474
2475         Player *player = m_env->getPlayer(peer_id);
2476         if(player == NULL){
2477                 infostream<<"Server::ProcessData(): Cancelling: "
2478                                 "No player for peer_id="<<peer_id
2479                                 <<std::endl;
2480                 return;
2481         }
2482
2483         PlayerSAO *playersao = player->getPlayerSAO();
2484         if(playersao == NULL){
2485                 infostream<<"Server::ProcessData(): Cancelling: "
2486                                 "No player object for peer_id="<<peer_id
2487                                 <<std::endl;
2488                 return;
2489         }
2490
2491         if(command == TOSERVER_PLAYERPOS)
2492         {
2493                 if(datasize < 2+12+12+4+4)
2494                         return;
2495
2496                 u32 start = 0;
2497                 v3s32 ps = readV3S32(&data[start+2]);
2498                 v3s32 ss = readV3S32(&data[start+2+12]);
2499                 f32 pitch = (f32)readS32(&data[2+12+12]) / 100.0;
2500                 f32 yaw = (f32)readS32(&data[2+12+12+4]) / 100.0;
2501                 u32 keyPressed = 0;
2502                 if(datasize >= 2+12+12+4+4+4)
2503                         keyPressed = (u32)readU32(&data[2+12+12+4+4]);
2504                 v3f position((f32)ps.X/100., (f32)ps.Y/100., (f32)ps.Z/100.);
2505                 v3f speed((f32)ss.X/100., (f32)ss.Y/100., (f32)ss.Z/100.);
2506                 pitch = wrapDegrees(pitch);
2507                 yaw = wrapDegrees(yaw);
2508
2509                 player->setPosition(position);
2510                 player->setSpeed(speed);
2511                 player->setPitch(pitch);
2512                 player->setYaw(yaw);
2513                 player->keyPressed=keyPressed;
2514                 player->control.up = (bool)(keyPressed&1);
2515                 player->control.down = (bool)(keyPressed&2);
2516                 player->control.left = (bool)(keyPressed&4);
2517                 player->control.right = (bool)(keyPressed&8);
2518                 player->control.jump = (bool)(keyPressed&16);
2519                 player->control.aux1 = (bool)(keyPressed&32);
2520                 player->control.sneak = (bool)(keyPressed&64);
2521                 player->control.LMB = (bool)(keyPressed&128);
2522                 player->control.RMB = (bool)(keyPressed&256);
2523
2524                 /*infostream<<"Server::ProcessData(): Moved player "<<peer_id<<" to "
2525                                 <<"("<<position.X<<","<<position.Y<<","<<position.Z<<")"
2526                                 <<" pitch="<<pitch<<" yaw="<<yaw<<std::endl;*/
2527         }
2528         else if(command == TOSERVER_GOTBLOCKS)
2529         {
2530                 if(datasize < 2+1)
2531                         return;
2532
2533                 /*
2534                         [0] u16 command
2535                         [2] u8 count
2536                         [3] v3s16 pos_0
2537                         [3+6] v3s16 pos_1
2538                         ...
2539                 */
2540
2541                 u16 count = data[2];
2542                 for(u16 i=0; i<count; i++)
2543                 {
2544                         if((s16)datasize < 2+1+(i+1)*6)
2545                                 throw con::InvalidIncomingDataException
2546                                         ("GOTBLOCKS length is too short");
2547                         v3s16 p = readV3S16(&data[2+1+i*6]);
2548                         /*infostream<<"Server: GOTBLOCKS ("
2549                                         <<p.X<<","<<p.Y<<","<<p.Z<<")"<<std::endl;*/
2550                         RemoteClient *client = getClient(peer_id);
2551                         client->GotBlock(p);
2552                 }
2553         }
2554         else if(command == TOSERVER_DELETEDBLOCKS)
2555         {
2556                 if(datasize < 2+1)
2557                         return;
2558
2559                 /*
2560                         [0] u16 command
2561                         [2] u8 count
2562                         [3] v3s16 pos_0
2563                         [3+6] v3s16 pos_1
2564                         ...
2565                 */
2566
2567                 u16 count = data[2];
2568                 for(u16 i=0; i<count; i++)
2569                 {
2570                         if((s16)datasize < 2+1+(i+1)*6)
2571                                 throw con::InvalidIncomingDataException
2572                                         ("DELETEDBLOCKS length is too short");
2573                         v3s16 p = readV3S16(&data[2+1+i*6]);
2574                         /*infostream<<"Server: DELETEDBLOCKS ("
2575                                         <<p.X<<","<<p.Y<<","<<p.Z<<")"<<std::endl;*/
2576                         RemoteClient *client = getClient(peer_id);
2577                         client->SetBlockNotSent(p);
2578                 }
2579         }
2580         else if(command == TOSERVER_CLICK_OBJECT)
2581         {
2582                 infostream<<"Server: CLICK_OBJECT not supported anymore"<<std::endl;
2583                 return;
2584         }
2585         else if(command == TOSERVER_CLICK_ACTIVEOBJECT)
2586         {
2587                 infostream<<"Server: CLICK_ACTIVEOBJECT not supported anymore"<<std::endl;
2588                 return;
2589         }
2590         else if(command == TOSERVER_GROUND_ACTION)
2591         {
2592                 infostream<<"Server: GROUND_ACTION not supported anymore"<<std::endl;
2593                 return;
2594
2595         }
2596         else if(command == TOSERVER_RELEASE)
2597         {
2598                 infostream<<"Server: RELEASE not supported anymore"<<std::endl;
2599                 return;
2600         }
2601         else if(command == TOSERVER_SIGNTEXT)
2602         {
2603                 infostream<<"Server: SIGNTEXT not supported anymore"
2604                                 <<std::endl;
2605                 return;
2606         }
2607         else if(command == TOSERVER_SIGNNODETEXT)
2608         {
2609                 infostream<<"Server: SIGNNODETEXT not supported anymore"
2610                                 <<std::endl;
2611                 return;
2612         }
2613         else if(command == TOSERVER_INVENTORY_ACTION)
2614         {
2615                 // Strip command and create a stream
2616                 std::string datastring((char*)&data[2], datasize-2);
2617                 verbosestream<<"TOSERVER_INVENTORY_ACTION: data="<<datastring<<std::endl;
2618                 std::istringstream is(datastring, std::ios_base::binary);
2619                 // Create an action
2620                 InventoryAction *a = InventoryAction::deSerialize(is);
2621                 if(a == NULL)
2622                 {
2623                         infostream<<"TOSERVER_INVENTORY_ACTION: "
2624                                         <<"InventoryAction::deSerialize() returned NULL"
2625                                         <<std::endl;
2626                         return;
2627                 }
2628
2629                 // If something goes wrong, this player is to blame
2630                 RollbackScopeActor rollback_scope(m_rollback,
2631                                 std::string("player:")+player->getName());
2632
2633                 /*
2634                         Note: Always set inventory not sent, to repair cases
2635                         where the client made a bad prediction.
2636                 */
2637
2638                 /*
2639                         Handle restrictions and special cases of the move action
2640                 */
2641                 if(a->getType() == IACTION_MOVE)
2642                 {
2643                         IMoveAction *ma = (IMoveAction*)a;
2644
2645                         ma->from_inv.applyCurrentPlayer(player->getName());
2646                         ma->to_inv.applyCurrentPlayer(player->getName());
2647
2648                         setInventoryModified(ma->from_inv);
2649                         setInventoryModified(ma->to_inv);
2650
2651                         bool from_inv_is_current_player =
2652                                 (ma->from_inv.type == InventoryLocation::PLAYER) &&
2653                                 (ma->from_inv.name == player->getName());
2654
2655                         bool to_inv_is_current_player =
2656                                 (ma->to_inv.type == InventoryLocation::PLAYER) &&
2657                                 (ma->to_inv.name == player->getName());
2658
2659                         /*
2660                                 Disable moving items out of craftpreview
2661                         */
2662                         if(ma->from_list == "craftpreview")
2663                         {
2664                                 infostream<<"Ignoring IMoveAction from "
2665                                                 <<(ma->from_inv.dump())<<":"<<ma->from_list
2666                                                 <<" to "<<(ma->to_inv.dump())<<":"<<ma->to_list
2667                                                 <<" because src is "<<ma->from_list<<std::endl;
2668                                 delete a;
2669                                 return;
2670                         }
2671
2672                         /*
2673                                 Disable moving items into craftresult and craftpreview
2674                         */
2675                         if(ma->to_list == "craftpreview" || ma->to_list == "craftresult")
2676                         {
2677                                 infostream<<"Ignoring IMoveAction from "
2678                                                 <<(ma->from_inv.dump())<<":"<<ma->from_list
2679                                                 <<" to "<<(ma->to_inv.dump())<<":"<<ma->to_list
2680                                                 <<" because dst is "<<ma->to_list<<std::endl;
2681                                 delete a;
2682                                 return;
2683                         }
2684
2685                         // Disallow moving items in elsewhere than player's inventory
2686                         // if not allowed to interact
2687                         if(!checkPriv(player->getName(), "interact") &&
2688                                         (!from_inv_is_current_player ||
2689                                         !to_inv_is_current_player))
2690                         {
2691                                 infostream<<"Cannot move outside of player's inventory: "
2692                                                 <<"No interact privilege"<<std::endl;
2693                                 delete a;
2694                                 return;
2695                         }
2696                 }
2697                 /*
2698                         Handle restrictions and special cases of the drop action
2699                 */
2700                 else if(a->getType() == IACTION_DROP)
2701                 {
2702                         IDropAction *da = (IDropAction*)a;
2703
2704                         da->from_inv.applyCurrentPlayer(player->getName());
2705
2706                         setInventoryModified(da->from_inv);
2707
2708                         // Disallow dropping items if not allowed to interact
2709                         if(!checkPriv(player->getName(), "interact"))
2710                         {
2711                                 delete a;
2712                                 return;
2713                         }
2714                 }
2715                 /*
2716                         Handle restrictions and special cases of the craft action
2717                 */
2718                 else if(a->getType() == IACTION_CRAFT)
2719                 {
2720                         ICraftAction *ca = (ICraftAction*)a;
2721
2722                         ca->craft_inv.applyCurrentPlayer(player->getName());
2723
2724                         setInventoryModified(ca->craft_inv);
2725
2726                         //bool craft_inv_is_current_player =
2727                         //      (ca->craft_inv.type == InventoryLocation::PLAYER) &&
2728                         //      (ca->craft_inv.name == player->getName());
2729
2730                         // Disallow crafting if not allowed to interact
2731                         if(!checkPriv(player->getName(), "interact"))
2732                         {
2733                                 infostream<<"Cannot craft: "
2734                                                 <<"No interact privilege"<<std::endl;
2735                                 delete a;
2736                                 return;
2737                         }
2738                 }
2739
2740                 // Do the action
2741                 a->apply(this, playersao, this);
2742                 // Eat the action
2743                 delete a;
2744         }
2745         else if(command == TOSERVER_CHAT_MESSAGE)
2746         {
2747                 /*
2748                         u16 command
2749                         u16 length
2750                         wstring message
2751                 */
2752                 u8 buf[6];
2753                 std::string datastring((char*)&data[2], datasize-2);
2754                 std::istringstream is(datastring, std::ios_base::binary);
2755
2756                 // Read stuff
2757                 is.read((char*)buf, 2);
2758                 u16 len = readU16(buf);
2759
2760                 std::wstring message;
2761                 for(u16 i=0; i<len; i++)
2762                 {
2763                         is.read((char*)buf, 2);
2764                         message += (wchar_t)readU16(buf);
2765                 }
2766
2767                 // If something goes wrong, this player is to blame
2768                 RollbackScopeActor rollback_scope(m_rollback,
2769                                 std::string("player:")+player->getName());
2770
2771                 // Get player name of this client
2772                 std::wstring name = narrow_to_wide(player->getName());
2773
2774                 // Run script hook
2775                 bool ate = scriptapi_on_chat_message(m_lua, player->getName(),
2776                                 wide_to_narrow(message));
2777                 // If script ate the message, don't proceed
2778                 if(ate)
2779                         return;
2780
2781                 // Line to send to players
2782                 std::wstring line;
2783                 // Whether to send to the player that sent the line
2784                 bool send_to_sender = false;
2785                 // Whether to send to other players
2786                 bool send_to_others = false;
2787
2788                 // Commands are implemented in Lua, so only catch invalid
2789                 // commands that were not "eaten" and send an error back
2790                 if(message[0] == L'/')
2791                 {
2792                         message = message.substr(1);
2793                         send_to_sender = true;
2794                         if(message.length() == 0)
2795                                 line += L"-!- Empty command";
2796                         else
2797                                 line += L"-!- Invalid command: " + str_split(message, L' ')[0];
2798                 }
2799                 else
2800                 {
2801                         if(checkPriv(player->getName(), "shout")){
2802                                 line += L"<";
2803                                 line += name;
2804                                 line += L"> ";
2805                                 line += message;
2806                                 send_to_others = true;
2807                         } else {
2808                                 line += L"-!- You don't have permission to shout.";
2809                                 send_to_sender = true;
2810                         }
2811                 }
2812
2813                 if(line != L"")
2814                 {
2815                         if(send_to_others)
2816                                 actionstream<<"CHAT: "<<wide_to_narrow(line)<<std::endl;
2817
2818                         /*
2819                                 Send the message to clients
2820                         */
2821                         for(core::map<u16, RemoteClient*>::Iterator
2822                                 i = m_clients.getIterator();
2823                                 i.atEnd() == false; i++)
2824                         {
2825                                 // Get client and check that it is valid
2826                                 RemoteClient *client = i.getNode()->getValue();
2827                                 assert(client->peer_id == i.getNode()->getKey());
2828                                 if(client->serialization_version == SER_FMT_VER_INVALID)
2829                                         continue;
2830
2831                                 // Filter recipient
2832                                 bool sender_selected = (peer_id == client->peer_id);
2833                                 if(sender_selected == true && send_to_sender == false)
2834                                         continue;
2835                                 if(sender_selected == false && send_to_others == false)
2836                                         continue;
2837
2838                                 SendChatMessage(client->peer_id, line);
2839                         }
2840                 }
2841         }
2842         else if(command == TOSERVER_DAMAGE)
2843         {
2844                 std::string datastring((char*)&data[2], datasize-2);
2845                 std::istringstream is(datastring, std::ios_base::binary);
2846                 u8 damage = readU8(is);
2847
2848                 if(g_settings->getBool("enable_damage"))
2849                 {
2850                         actionstream<<player->getName()<<" damaged by "
2851                                         <<(int)damage<<" hp at "<<PP(player->getPosition()/BS)
2852                                         <<std::endl;
2853
2854                         playersao->setHP(playersao->getHP() - damage);
2855
2856                         if(playersao->getHP() == 0 && playersao->m_hp_not_sent)
2857                                 DiePlayer(peer_id);
2858
2859                         if(playersao->m_hp_not_sent)
2860                                 SendPlayerHP(peer_id);
2861                 }
2862         }
2863         else if(command == TOSERVER_PASSWORD)
2864         {
2865                 /*
2866                         [0] u16 TOSERVER_PASSWORD
2867                         [2] u8[28] old password
2868                         [30] u8[28] new password
2869                 */
2870
2871                 if(datasize != 2+PASSWORD_SIZE*2)
2872                         return;
2873                 /*char password[PASSWORD_SIZE];
2874                 for(u32 i=0; i<PASSWORD_SIZE-1; i++)
2875                         password[i] = data[2+i];
2876                 password[PASSWORD_SIZE-1] = 0;*/
2877                 std::string oldpwd;
2878                 for(u32 i=0; i<PASSWORD_SIZE-1; i++)
2879                 {
2880                         char c = data[2+i];
2881                         if(c == 0)
2882                                 break;
2883                         oldpwd += c;
2884                 }
2885                 std::string newpwd;
2886                 for(u32 i=0; i<PASSWORD_SIZE-1; i++)
2887                 {
2888                         char c = data[2+PASSWORD_SIZE+i];
2889                         if(c == 0)
2890                                 break;
2891                         newpwd += c;
2892                 }
2893
2894                 if(!base64_is_valid(newpwd)){
2895                         infostream<<"Server: "<<player->getName()<<" supplied invalid password hash"<<std::endl;
2896                         // Wrong old password supplied!!
2897                         SendChatMessage(peer_id, L"Invalid new password hash supplied. Password NOT changed.");
2898                         return;
2899                 }
2900
2901                 infostream<<"Server: Client requests a password change from "
2902                                 <<"'"<<oldpwd<<"' to '"<<newpwd<<"'"<<std::endl;
2903
2904                 std::string playername = player->getName();
2905
2906                 std::string checkpwd;
2907                 scriptapi_get_auth(m_lua, playername, &checkpwd, NULL);
2908
2909                 if(oldpwd != checkpwd)
2910                 {
2911                         infostream<<"Server: invalid old password"<<std::endl;
2912                         // Wrong old password supplied!!
2913                         SendChatMessage(peer_id, L"Invalid old password supplied. Password NOT changed.");
2914                         return;
2915                 }
2916
2917                 bool success = scriptapi_set_password(m_lua, playername, newpwd);
2918                 if(success){
2919                         actionstream<<player->getName()<<" changes password"<<std::endl;
2920                         SendChatMessage(peer_id, L"Password change successful.");
2921                 } else {
2922                         actionstream<<player->getName()<<" tries to change password but "
2923                                         <<"it fails"<<std::endl;
2924                         SendChatMessage(peer_id, L"Password change failed or inavailable.");
2925                 }
2926         }
2927         else if(command == TOSERVER_PLAYERITEM)
2928         {
2929                 if (datasize < 2+2)
2930                         return;
2931
2932                 u16 item = readU16(&data[2]);
2933                 playersao->setWieldIndex(item);
2934         }
2935         else if(command == TOSERVER_RESPAWN)
2936         {
2937                 if(player->hp != 0 || !g_settings->getBool("enable_damage"))
2938                         return;
2939
2940                 RespawnPlayer(peer_id);
2941
2942                 actionstream<<player->getName()<<" respawns at "
2943                                 <<PP(player->getPosition()/BS)<<std::endl;
2944
2945                 // ActiveObject is added to environment in AsyncRunStep after
2946                 // the previous addition has been succesfully removed
2947         }
2948         else if(command == TOSERVER_REQUEST_MEDIA) {
2949                 std::string datastring((char*)&data[2], datasize-2);
2950                 std::istringstream is(datastring, std::ios_base::binary);
2951
2952                 core::list<MediaRequest> tosend;
2953                 u16 numfiles = readU16(is);
2954
2955                 infostream<<"Sending "<<numfiles<<" files to "
2956                                 <<getPlayerName(peer_id)<<std::endl;
2957                 verbosestream<<"TOSERVER_REQUEST_MEDIA: "<<std::endl;
2958
2959                 for(int i = 0; i < numfiles; i++) {
2960                         std::string name = deSerializeString(is);
2961                         tosend.push_back(MediaRequest(name));
2962                         verbosestream<<"TOSERVER_REQUEST_MEDIA: requested file "
2963                                         <<name<<std::endl;
2964                 }
2965
2966                 sendRequestedMedia(peer_id, tosend);
2967
2968                 // Now the client should know about everything
2969                 // (definitions and files)
2970                 getClient(peer_id)->definitions_sent = true;
2971         }
2972         else if(command == TOSERVER_RECEIVED_MEDIA) {
2973                 getClient(peer_id)->definitions_sent = true;
2974         }
2975         else if(command == TOSERVER_INTERACT)
2976         {
2977                 std::string datastring((char*)&data[2], datasize-2);
2978                 std::istringstream is(datastring, std::ios_base::binary);
2979
2980                 /*
2981                         [0] u16 command
2982                         [2] u8 action
2983                         [3] u16 item
2984                         [5] u32 length of the next item
2985                         [9] serialized PointedThing
2986                         actions:
2987                         0: start digging (from undersurface) or use
2988                         1: stop digging (all parameters ignored)
2989                         2: digging completed
2990                         3: place block or item (to abovesurface)
2991                         4: use item
2992                 */
2993                 u8 action = readU8(is);
2994                 u16 item_i = readU16(is);
2995                 std::istringstream tmp_is(deSerializeLongString(is), std::ios::binary);
2996                 PointedThing pointed;
2997                 pointed.deSerialize(tmp_is);
2998
2999                 verbosestream<<"TOSERVER_INTERACT: action="<<(int)action<<", item="
3000                                 <<item_i<<", pointed="<<pointed.dump()<<std::endl;
3001
3002                 if(player->hp == 0)
3003                 {
3004                         verbosestream<<"TOSERVER_INTERACT: "<<player->getName()
3005                                 <<" tried to interact, but is dead!"<<std::endl;
3006                         return;
3007                 }
3008
3009                 v3f player_pos = playersao->getLastGoodPosition();
3010
3011                 // Update wielded item
3012                 playersao->setWieldIndex(item_i);
3013
3014                 // Get pointed to node (undefined if not POINTEDTYPE_NODE)
3015                 v3s16 p_under = pointed.node_undersurface;
3016                 v3s16 p_above = pointed.node_abovesurface;
3017
3018                 // Get pointed to object (NULL if not POINTEDTYPE_OBJECT)
3019                 ServerActiveObject *pointed_object = NULL;
3020                 if(pointed.type == POINTEDTHING_OBJECT)
3021                 {
3022                         pointed_object = m_env->getActiveObject(pointed.object_id);
3023                         if(pointed_object == NULL)
3024                         {
3025                                 verbosestream<<"TOSERVER_INTERACT: "
3026                                         "pointed object is NULL"<<std::endl;
3027                                 return;
3028                         }
3029
3030                 }
3031
3032                 v3f pointed_pos_under = player_pos;
3033                 v3f pointed_pos_above = player_pos;
3034                 if(pointed.type == POINTEDTHING_NODE)
3035                 {
3036                         pointed_pos_under = intToFloat(p_under, BS);
3037                         pointed_pos_above = intToFloat(p_above, BS);
3038                 }
3039                 else if(pointed.type == POINTEDTHING_OBJECT)
3040                 {
3041                         pointed_pos_under = pointed_object->getBasePosition();
3042                         pointed_pos_above = pointed_pos_under;
3043                 }
3044
3045                 /*
3046                         Check that target is reasonably close
3047                         (only when digging or placing things)
3048                 */
3049                 if(action == 0 || action == 2 || action == 3)
3050                 {
3051                         float d = player_pos.getDistanceFrom(pointed_pos_under);
3052                         float max_d = BS * 14; // Just some large enough value
3053                         if(d > max_d){
3054                                 actionstream<<"Player "<<player->getName()
3055                                                 <<" tried to access "<<pointed.dump()
3056                                                 <<" from too far: "
3057                                                 <<"d="<<d<<", max_d="<<max_d
3058                                                 <<". ignoring."<<std::endl;
3059                                 // Re-send block to revert change on client-side
3060                                 RemoteClient *client = getClient(peer_id);
3061                                 v3s16 blockpos = getNodeBlockPos(floatToInt(pointed_pos_under, BS));
3062                                 client->SetBlockNotSent(blockpos);
3063                                 // Do nothing else
3064                                 return;
3065                         }
3066                 }
3067
3068                 /*
3069                         Make sure the player is allowed to do it
3070                 */
3071                 if(!checkPriv(player->getName(), "interact"))
3072                 {
3073                         actionstream<<player->getName()<<" attempted to interact with "
3074                                         <<pointed.dump()<<" without 'interact' privilege"
3075                                         <<std::endl;
3076                         // Re-send block to revert change on client-side
3077                         RemoteClient *client = getClient(peer_id);
3078                         // Digging completed -> under
3079                         if(action == 2){
3080                                 v3s16 blockpos = getNodeBlockPos(floatToInt(pointed_pos_under, BS));
3081                                 client->SetBlockNotSent(blockpos);
3082                         }
3083                         // Placement -> above
3084                         if(action == 3){
3085                                 v3s16 blockpos = getNodeBlockPos(floatToInt(pointed_pos_above, BS));
3086                                 client->SetBlockNotSent(blockpos);
3087                         }
3088                         return;
3089                 }
3090
3091                 /*
3092                         If something goes wrong, this player is to blame
3093                 */
3094                 RollbackScopeActor rollback_scope(m_rollback,
3095                                 std::string("player:")+player->getName());
3096
3097                 /*
3098                         0: start digging or punch object
3099                 */
3100                 if(action == 0)
3101                 {
3102                         if(pointed.type == POINTEDTHING_NODE)
3103                         {
3104                                 /*
3105                                         NOTE: This can be used in the future to check if
3106                                         somebody is cheating, by checking the timing.
3107                                 */
3108                                 MapNode n(CONTENT_IGNORE);
3109                                 try
3110                                 {
3111                                         n = m_env->getMap().getNode(p_under);
3112                                 }
3113                                 catch(InvalidPositionException &e)
3114                                 {
3115                                         infostream<<"Server: Not punching: Node not found."
3116                                                         <<" Adding block to emerge queue."
3117                                                         <<std::endl;
3118                                         m_emerge_queue.addBlock(peer_id,
3119                                                         getNodeBlockPos(p_above), BLOCK_EMERGE_FLAG_FROMDISK);
3120                                 }
3121                                 if(n.getContent() != CONTENT_IGNORE)
3122                                         scriptapi_node_on_punch(m_lua, p_under, n, playersao);
3123                                 // Cheat prevention
3124                                 playersao->noCheatDigStart(p_under);
3125                         }
3126                         else if(pointed.type == POINTEDTHING_OBJECT)
3127                         {
3128                                 // Skip if object has been removed
3129                                 if(pointed_object->m_removed)
3130                                         return;
3131
3132                                 actionstream<<player->getName()<<" punches object "
3133                                                 <<pointed.object_id<<": "
3134                                                 <<pointed_object->getDescription()<<std::endl;
3135
3136                                 ItemStack punchitem = playersao->getWieldedItem();
3137                                 ToolCapabilities toolcap =
3138                                                 punchitem.getToolCapabilities(m_itemdef);
3139                                 v3f dir = (pointed_object->getBasePosition() -
3140                                                 (player->getPosition() + player->getEyeOffset())
3141                                                         ).normalize();
3142                                 float time_from_last_punch =
3143                                         playersao->resetTimeFromLastPunch();
3144                                 pointed_object->punch(dir, &toolcap, playersao,
3145                                                 time_from_last_punch);
3146                         }
3147
3148                 } // action == 0
3149
3150                 /*
3151                         1: stop digging
3152                 */
3153                 else if(action == 1)
3154                 {
3155                 } // action == 1
3156
3157                 /*
3158                         2: Digging completed
3159                 */
3160                 else if(action == 2)
3161                 {
3162                         // Only digging of nodes
3163                         if(pointed.type == POINTEDTHING_NODE)
3164                         {
3165                                 MapNode n(CONTENT_IGNORE);
3166                                 try
3167                                 {
3168                                         n = m_env->getMap().getNode(p_under);
3169                                 }
3170                                 catch(InvalidPositionException &e)
3171                                 {
3172                                         infostream<<"Server: Not finishing digging: Node not found."
3173                                                         <<" Adding block to emerge queue."
3174                                                         <<std::endl;
3175                                         m_emerge_queue.addBlock(peer_id,
3176                                                         getNodeBlockPos(p_above), BLOCK_EMERGE_FLAG_FROMDISK);
3177                                 }
3178
3179                                 /* Cheat prevention */
3180                                 bool is_valid_dig = true;
3181                                 if(!isSingleplayer() && !g_settings->getBool("disable_anticheat"))
3182                                 {
3183                                         v3s16 nocheat_p = playersao->getNoCheatDigPos();
3184                                         float nocheat_t = playersao->getNoCheatDigTime();
3185                                         playersao->noCheatDigEnd();
3186                                         // If player didn't start digging this, ignore dig
3187                                         if(nocheat_p != p_under){
3188                                                 infostream<<"Server: NoCheat: "<<player->getName()
3189                                                                 <<" started digging "
3190                                                                 <<PP(nocheat_p)<<" and completed digging "
3191                                                                 <<PP(p_under)<<"; not digging."<<std::endl;
3192                                                 is_valid_dig = false;
3193                                         }
3194                                         // Get player's wielded item
3195                                         ItemStack playeritem;
3196                                         InventoryList *mlist = playersao->getInventory()->getList("main");
3197                                         if(mlist != NULL)
3198                                                 playeritem = mlist->getItem(playersao->getWieldIndex());
3199                                         ToolCapabilities playeritem_toolcap =
3200                                                         playeritem.getToolCapabilities(m_itemdef);
3201                                         // Get diggability and expected digging time
3202                                         DigParams params = getDigParams(m_nodedef->get(n).groups,
3203                                                         &playeritem_toolcap);
3204                                         // If can't dig, try hand
3205                                         if(!params.diggable){
3206                                                 const ItemDefinition &hand = m_itemdef->get("");
3207                                                 const ToolCapabilities *tp = hand.tool_capabilities;
3208                                                 if(tp)
3209                                                         params = getDigParams(m_nodedef->get(n).groups, tp);
3210                                         }
3211                                         // If can't dig, ignore dig
3212                                         if(!params.diggable){
3213                                                 infostream<<"Server: NoCheat: "<<player->getName()
3214                                                                 <<" completed digging "<<PP(p_under)
3215                                                                 <<", which is not diggable with tool. not digging."
3216                                                                 <<std::endl;
3217                                                 is_valid_dig = false;
3218                                         }
3219                                         // If time is considerably too short, ignore dig
3220                                         // Check time only for medium and slow timed digs
3221                                         if(params.diggable && params.time > 0.3 && nocheat_t < 0.5 * params.time){
3222                                                 infostream<<"Server: NoCheat: "<<player->getName()
3223                                                                 <<" completed digging "
3224                                                                 <<PP(p_under)<<" in "<<nocheat_t<<"s; expected "
3225                                                                 <<params.time<<"s; not digging."<<std::endl;
3226                                                 is_valid_dig = false;
3227                                         }
3228                                 }
3229
3230                                 /* Actually dig node */
3231
3232                                 if(is_valid_dig && n.getContent() != CONTENT_IGNORE)
3233                                         scriptapi_node_on_dig(m_lua, p_under, n, playersao);
3234
3235                                 // Send unusual result (that is, node not being removed)
3236                                 if(m_env->getMap().getNodeNoEx(p_under).getContent() != CONTENT_AIR)
3237                                 {
3238                                         // Re-send block to revert change on client-side
3239                                         RemoteClient *client = getClient(peer_id);
3240                                         v3s16 blockpos = getNodeBlockPos(floatToInt(pointed_pos_under, BS));
3241                                         client->SetBlockNotSent(blockpos);
3242                                 }
3243                         }
3244                 } // action == 2
3245
3246                 /*
3247                         3: place block or right-click object
3248                 */
3249                 else if(action == 3)
3250                 {
3251                         ItemStack item = playersao->getWieldedItem();
3252
3253                         // Reset build time counter
3254                         if(pointed.type == POINTEDTHING_NODE &&
3255                                         item.getDefinition(m_itemdef).type == ITEM_NODE)
3256                                 getClient(peer_id)->m_time_from_building = 0.0;
3257
3258                         if(pointed.type == POINTEDTHING_OBJECT)
3259                         {
3260                                 // Right click object
3261
3262                                 // Skip if object has been removed
3263                                 if(pointed_object->m_removed)
3264                                         return;
3265
3266                                 actionstream<<player->getName()<<" right-clicks object "
3267                                                 <<pointed.object_id<<": "
3268                                                 <<pointed_object->getDescription()<<std::endl;
3269
3270                                 // Do stuff
3271                                 pointed_object->rightClick(playersao);
3272                         }
3273                         else if(scriptapi_item_on_place(m_lua,
3274                                         item, playersao, pointed))
3275                         {
3276                                 // Placement was handled in lua
3277
3278                                 // Apply returned ItemStack
3279                                 playersao->setWieldedItem(item);
3280                         }
3281
3282                         // If item has node placement prediction, always send the above
3283                         // node to make sure the client knows what exactly happened
3284                         if(item.getDefinition(m_itemdef).node_placement_prediction != ""){
3285                                 RemoteClient *client = getClient(peer_id);
3286                                 v3s16 blockpos = getNodeBlockPos(floatToInt(pointed_pos_above, BS));
3287                                 client->SetBlockNotSent(blockpos);
3288                         }
3289                 } // action == 3
3290
3291                 /*
3292                         4: use
3293                 */
3294                 else if(action == 4)
3295                 {
3296                         ItemStack item = playersao->getWieldedItem();
3297
3298                         actionstream<<player->getName()<<" uses "<<item.name
3299                                         <<", pointing at "<<pointed.dump()<<std::endl;
3300
3301                         if(scriptapi_item_on_use(m_lua,
3302                                         item, playersao, pointed))
3303                         {
3304                                 // Apply returned ItemStack
3305                                 playersao->setWieldedItem(item);
3306                         }
3307
3308                 } // action == 4
3309                 
3310
3311                 /*
3312                         Catch invalid actions
3313                 */
3314                 else
3315                 {
3316                         infostream<<"WARNING: Server: Invalid action "
3317                                         <<action<<std::endl;
3318                 }
3319         }
3320         else if(command == TOSERVER_REMOVED_SOUNDS)
3321         {
3322                 std::string datastring((char*)&data[2], datasize-2);
3323                 std::istringstream is(datastring, std::ios_base::binary);
3324
3325                 int num = readU16(is);
3326                 for(int k=0; k<num; k++){
3327                         s32 id = readS32(is);
3328                         std::map<s32, ServerPlayingSound>::iterator i =
3329                                         m_playing_sounds.find(id);
3330                         if(i == m_playing_sounds.end())
3331                                 continue;
3332                         ServerPlayingSound &psound = i->second;
3333                         psound.clients.erase(peer_id);
3334                         if(psound.clients.size() == 0)
3335                                 m_playing_sounds.erase(i++);
3336                 }
3337         }
3338         else if(command == TOSERVER_NODEMETA_FIELDS)
3339         {
3340                 std::string datastring((char*)&data[2], datasize-2);
3341                 std::istringstream is(datastring, std::ios_base::binary);
3342
3343                 v3s16 p = readV3S16(is);
3344                 std::string formname = deSerializeString(is);
3345                 int num = readU16(is);
3346                 std::map<std::string, std::string> fields;
3347                 for(int k=0; k<num; k++){
3348                         std::string fieldname = deSerializeString(is);
3349                         std::string fieldvalue = deSerializeLongString(is);
3350                         fields[fieldname] = fieldvalue;
3351                 }
3352
3353                 // If something goes wrong, this player is to blame
3354                 RollbackScopeActor rollback_scope(m_rollback,
3355                                 std::string("player:")+player->getName());
3356
3357                 // Check the target node for rollback data; leave others unnoticed
3358                 RollbackNode rn_old(&m_env->getMap(), p, this);
3359
3360                 scriptapi_node_on_receive_fields(m_lua, p, formname, fields,
3361                                 playersao);
3362
3363                 // Report rollback data
3364                 RollbackNode rn_new(&m_env->getMap(), p, this);
3365                 if(rollback() && rn_new != rn_old){
3366                         RollbackAction action;
3367                         action.setSetNode(p, rn_old, rn_new);
3368                         rollback()->reportAction(action);
3369                 }
3370         }
3371         else if(command == TOSERVER_INVENTORY_FIELDS)
3372         {
3373                 std::string datastring((char*)&data[2], datasize-2);
3374                 std::istringstream is(datastring, std::ios_base::binary);
3375
3376                 std::string formname = deSerializeString(is);
3377                 int num = readU16(is);
3378                 std::map<std::string, std::string> fields;
3379                 for(int k=0; k<num; k++){
3380                         std::string fieldname = deSerializeString(is);
3381                         std::string fieldvalue = deSerializeLongString(is);
3382                         fields[fieldname] = fieldvalue;
3383                 }
3384
3385                 scriptapi_on_player_receive_fields(m_lua, playersao, formname, fields);
3386         }
3387         else
3388         {
3389                 infostream<<"Server::ProcessData(): Ignoring "
3390                                 "unknown command "<<command<<std::endl;
3391         }
3392
3393         } //try
3394         catch(SendFailedException &e)
3395         {
3396                 errorstream<<"Server::ProcessData(): SendFailedException: "
3397                                 <<"what="<<e.what()
3398                                 <<std::endl;
3399         }
3400 }
3401
3402 void Server::onMapEditEvent(MapEditEvent *event)
3403 {
3404         //infostream<<"Server::onMapEditEvent()"<<std::endl;
3405         if(m_ignore_map_edit_events)
3406                 return;
3407         if(m_ignore_map_edit_events_area.contains(event->getArea()))
3408                 return;
3409         MapEditEvent *e = event->clone();
3410         m_unsent_map_edit_queue.push_back(e);
3411 }
3412
3413 Inventory* Server::getInventory(const InventoryLocation &loc)
3414 {
3415         switch(loc.type){
3416         case InventoryLocation::UNDEFINED:
3417         {}
3418         break;
3419         case InventoryLocation::CURRENT_PLAYER:
3420         {}
3421         break;
3422         case InventoryLocation::PLAYER:
3423         {
3424                 Player *player = m_env->getPlayer(loc.name.c_str());
3425                 if(!player)
3426                         return NULL;
3427                 PlayerSAO *playersao = player->getPlayerSAO();
3428                 if(!playersao)
3429                         return NULL;
3430                 return playersao->getInventory();
3431         }
3432         break;
3433         case InventoryLocation::NODEMETA:
3434         {
3435                 NodeMetadata *meta = m_env->getMap().getNodeMetadata(loc.p);
3436                 if(!meta)
3437                         return NULL;
3438                 return meta->getInventory();
3439         }
3440         break;
3441         case InventoryLocation::DETACHED:
3442         {
3443                 if(m_detached_inventories.count(loc.name) == 0)
3444                         return NULL;
3445                 return m_detached_inventories[loc.name];
3446         }
3447         break;
3448         default:
3449                 assert(0);
3450         }
3451         return NULL;
3452 }
3453 void Server::setInventoryModified(const InventoryLocation &loc)
3454 {
3455         switch(loc.type){
3456         case InventoryLocation::UNDEFINED:
3457         {}
3458         break;
3459         case InventoryLocation::PLAYER:
3460         {
3461                 Player *player = m_env->getPlayer(loc.name.c_str());
3462                 if(!player)
3463                         return;
3464                 PlayerSAO *playersao = player->getPlayerSAO();
3465                 if(!playersao)
3466                         return;
3467                 playersao->m_inventory_not_sent = true;
3468                 playersao->m_wielded_item_not_sent = true;
3469         }
3470         break;
3471         case InventoryLocation::NODEMETA:
3472         {
3473                 v3s16 blockpos = getNodeBlockPos(loc.p);
3474
3475                 MapBlock *block = m_env->getMap().getBlockNoCreateNoEx(blockpos);
3476                 if(block)
3477                         block->raiseModified(MOD_STATE_WRITE_NEEDED);
3478
3479                 setBlockNotSent(blockpos);
3480         }
3481         break;
3482         case InventoryLocation::DETACHED:
3483         {
3484                 sendDetachedInventoryToAll(loc.name);
3485         }
3486         break;
3487         default:
3488                 assert(0);
3489         }
3490 }
3491
3492 core::list<PlayerInfo> Server::getPlayerInfo()
3493 {
3494         DSTACK(__FUNCTION_NAME);
3495         JMutexAutoLock envlock(m_env_mutex);
3496         JMutexAutoLock conlock(m_con_mutex);
3497
3498         core::list<PlayerInfo> list;
3499
3500         core::list<Player*> players = m_env->getPlayers();
3501
3502         core::list<Player*>::Iterator i;
3503         for(i = players.begin();
3504                         i != players.end(); i++)
3505         {
3506                 PlayerInfo info;
3507
3508                 Player *player = *i;
3509
3510                 try{
3511                         // Copy info from connection to info struct
3512                         info.id = player->peer_id;
3513                         info.address = m_con.GetPeerAddress(player->peer_id);
3514                         info.avg_rtt = m_con.GetPeerAvgRTT(player->peer_id);
3515                 }
3516                 catch(con::PeerNotFoundException &e)
3517                 {
3518                         // Set dummy peer info
3519                         info.id = 0;
3520                         info.address = Address(0,0,0,0,0);
3521                         info.avg_rtt = 0.0;
3522                 }
3523
3524                 snprintf(info.name, PLAYERNAME_SIZE, "%s", player->getName());
3525                 info.position = player->getPosition();
3526
3527                 list.push_back(info);
3528         }
3529
3530         return list;
3531 }
3532
3533
3534 void Server::peerAdded(con::Peer *peer)
3535 {
3536         DSTACK(__FUNCTION_NAME);
3537         verbosestream<<"Server::peerAdded(): peer->id="
3538                         <<peer->id<<std::endl;
3539
3540         PeerChange c;
3541         c.type = PEER_ADDED;
3542         c.peer_id = peer->id;
3543         c.timeout = false;
3544         m_peer_change_queue.push_back(c);
3545 }
3546
3547 void Server::deletingPeer(con::Peer *peer, bool timeout)
3548 {
3549         DSTACK(__FUNCTION_NAME);
3550         verbosestream<<"Server::deletingPeer(): peer->id="
3551                         <<peer->id<<", timeout="<<timeout<<std::endl;
3552
3553         PeerChange c;
3554         c.type = PEER_REMOVED;
3555         c.peer_id = peer->id;
3556         c.timeout = timeout;
3557         m_peer_change_queue.push_back(c);
3558 }
3559
3560 /*
3561         Static send methods
3562 */
3563
3564 void Server::SendMovement(con::Connection &con, u16 peer_id)
3565 {
3566         DSTACK(__FUNCTION_NAME);
3567         std::ostringstream os(std::ios_base::binary);
3568
3569         writeU16(os, TOCLIENT_MOVEMENT);
3570         writeF1000(os, g_settings->getFloat("movement_acceleration_default"));
3571         writeF1000(os, g_settings->getFloat("movement_acceleration_air"));
3572         writeF1000(os, g_settings->getFloat("movement_acceleration_fast"));
3573         writeF1000(os, g_settings->getFloat("movement_speed_walk"));
3574         writeF1000(os, g_settings->getFloat("movement_speed_crouch"));
3575         writeF1000(os, g_settings->getFloat("movement_speed_fast"));
3576         writeF1000(os, g_settings->getFloat("movement_speed_climb"));
3577         writeF1000(os, g_settings->getFloat("movement_speed_jump"));
3578         writeF1000(os, g_settings->getFloat("movement_liquid_fluidity"));
3579         writeF1000(os, g_settings->getFloat("movement_liquid_fluidity_smooth"));
3580         writeF1000(os, g_settings->getFloat("movement_liquid_sink"));
3581         writeF1000(os, g_settings->getFloat("movement_gravity"));
3582
3583         // Make data buffer
3584         std::string s = os.str();
3585         SharedBuffer<u8> data((u8*)s.c_str(), s.size());
3586         // Send as reliable
3587         con.Send(peer_id, 0, data, true);
3588 }
3589
3590 void Server::SendHP(con::Connection &con, u16 peer_id, u8 hp)
3591 {
3592         DSTACK(__FUNCTION_NAME);
3593         std::ostringstream os(std::ios_base::binary);
3594
3595         writeU16(os, TOCLIENT_HP);
3596         writeU8(os, hp);
3597
3598         // Make data buffer
3599         std::string s = os.str();
3600         SharedBuffer<u8> data((u8*)s.c_str(), s.size());
3601         // Send as reliable
3602         con.Send(peer_id, 0, data, true);
3603 }
3604
3605 void Server::SendAccessDenied(con::Connection &con, u16 peer_id,
3606                 const std::wstring &reason)
3607 {
3608         DSTACK(__FUNCTION_NAME);
3609         std::ostringstream os(std::ios_base::binary);
3610
3611         writeU16(os, TOCLIENT_ACCESS_DENIED);
3612         os<<serializeWideString(reason);
3613
3614         // Make data buffer
3615         std::string s = os.str();
3616         SharedBuffer<u8> data((u8*)s.c_str(), s.size());
3617         // Send as reliable
3618         con.Send(peer_id, 0, data, true);
3619 }
3620
3621 void Server::SendDeathscreen(con::Connection &con, u16 peer_id,
3622                 bool set_camera_point_target, v3f camera_point_target)
3623 {
3624         DSTACK(__FUNCTION_NAME);
3625         std::ostringstream os(std::ios_base::binary);
3626
3627         writeU16(os, TOCLIENT_DEATHSCREEN);
3628         writeU8(os, set_camera_point_target);
3629         writeV3F1000(os, camera_point_target);
3630
3631         // Make data buffer
3632         std::string s = os.str();
3633         SharedBuffer<u8> data((u8*)s.c_str(), s.size());
3634         // Send as reliable
3635         con.Send(peer_id, 0, data, true);
3636 }
3637
3638 void Server::SendItemDef(con::Connection &con, u16 peer_id,
3639                 IItemDefManager *itemdef)
3640 {
3641         DSTACK(__FUNCTION_NAME);
3642         std::ostringstream os(std::ios_base::binary);
3643
3644         /*
3645                 u16 command
3646                 u32 length of the next item
3647                 zlib-compressed serialized ItemDefManager
3648         */
3649         writeU16(os, TOCLIENT_ITEMDEF);
3650         std::ostringstream tmp_os(std::ios::binary);
3651         itemdef->serialize(tmp_os);
3652         std::ostringstream tmp_os2(std::ios::binary);
3653         compressZlib(tmp_os.str(), tmp_os2);
3654         os<<serializeLongString(tmp_os2.str());
3655
3656         // Make data buffer
3657         std::string s = os.str();
3658         verbosestream<<"Server: Sending item definitions to id("<<peer_id
3659                         <<"): size="<<s.size()<<std::endl;
3660         SharedBuffer<u8> data((u8*)s.c_str(), s.size());
3661         // Send as reliable
3662         con.Send(peer_id, 0, data, true);
3663 }
3664
3665 void Server::SendNodeDef(con::Connection &con, u16 peer_id,
3666                 INodeDefManager *nodedef, u16 protocol_version)
3667 {
3668         DSTACK(__FUNCTION_NAME);
3669         std::ostringstream os(std::ios_base::binary);
3670
3671         /*
3672                 u16 command
3673                 u32 length of the next item
3674                 zlib-compressed serialized NodeDefManager
3675         */
3676         writeU16(os, TOCLIENT_NODEDEF);
3677         std::ostringstream tmp_os(std::ios::binary);
3678         nodedef->serialize(tmp_os, protocol_version);
3679         std::ostringstream tmp_os2(std::ios::binary);
3680         compressZlib(tmp_os.str(), tmp_os2);
3681         os<<serializeLongString(tmp_os2.str());
3682
3683         // Make data buffer
3684         std::string s = os.str();
3685         verbosestream<<"Server: Sending node definitions to id("<<peer_id
3686                         <<"): size="<<s.size()<<std::endl;
3687         SharedBuffer<u8> data((u8*)s.c_str(), s.size());
3688         // Send as reliable
3689         con.Send(peer_id, 0, data, true);
3690 }
3691
3692 /*
3693         Non-static send methods
3694 */
3695
3696 void Server::SendInventory(u16 peer_id)
3697 {
3698         DSTACK(__FUNCTION_NAME);
3699
3700         PlayerSAO *playersao = getPlayerSAO(peer_id);
3701         assert(playersao);
3702
3703         playersao->m_inventory_not_sent = false;
3704
3705         /*
3706                 Serialize it
3707         */
3708
3709         std::ostringstream os;
3710         playersao->getInventory()->serialize(os);
3711
3712         std::string s = os.str();
3713
3714         SharedBuffer<u8> data(s.size()+2);
3715         writeU16(&data[0], TOCLIENT_INVENTORY);
3716         memcpy(&data[2], s.c_str(), s.size());
3717
3718         // Send as reliable
3719         m_con.Send(peer_id, 0, data, true);
3720 }
3721
3722 void Server::SendChatMessage(u16 peer_id, const std::wstring &message)
3723 {
3724         DSTACK(__FUNCTION_NAME);
3725
3726         std::ostringstream os(std::ios_base::binary);
3727         u8 buf[12];
3728
3729         // Write command
3730         writeU16(buf, TOCLIENT_CHAT_MESSAGE);
3731         os.write((char*)buf, 2);
3732
3733         // Write length
3734         writeU16(buf, message.size());
3735         os.write((char*)buf, 2);
3736
3737         // Write string
3738         for(u32 i=0; i<message.size(); i++)
3739         {
3740                 u16 w = message[i];
3741                 writeU16(buf, w);
3742                 os.write((char*)buf, 2);
3743         }
3744
3745         // Make data buffer
3746         std::string s = os.str();
3747         SharedBuffer<u8> data((u8*)s.c_str(), s.size());
3748         // Send as reliable
3749         m_con.Send(peer_id, 0, data, true);
3750 }
3751 void Server::SendShowFormspecMessage(u16 peer_id, const std::string formspec, const std::string formname)
3752 {
3753         DSTACK(__FUNCTION_NAME);
3754
3755         std::ostringstream os(std::ios_base::binary);
3756         u8 buf[12];
3757
3758         // Write command
3759         writeU16(buf, TOCLIENT_SHOW_FORMSPEC);
3760         os.write((char*)buf, 2);
3761         os<<serializeLongString(formspec);
3762         os<<serializeString(formname);
3763
3764         // Make data buffer
3765         std::string s = os.str();
3766         SharedBuffer<u8> data((u8*)s.c_str(), s.size());
3767         // Send as reliable
3768         m_con.Send(peer_id, 0, data, true);
3769 }
3770
3771 void Server::BroadcastChatMessage(const std::wstring &message)
3772 {
3773         for(core::map<u16, RemoteClient*>::Iterator
3774                 i = m_clients.getIterator();
3775                 i.atEnd() == false; i++)
3776         {
3777                 // Get client and check that it is valid
3778                 RemoteClient *client = i.getNode()->getValue();
3779                 assert(client->peer_id == i.getNode()->getKey());
3780                 if(client->serialization_version == SER_FMT_VER_INVALID)
3781                         continue;
3782
3783                 SendChatMessage(client->peer_id, message);
3784         }
3785 }
3786
3787 void Server::SendPlayerHP(u16 peer_id)
3788 {
3789         DSTACK(__FUNCTION_NAME);
3790         PlayerSAO *playersao = getPlayerSAO(peer_id);
3791         assert(playersao);
3792         playersao->m_hp_not_sent = false;
3793         SendHP(m_con, peer_id, playersao->getHP());
3794 }
3795
3796 void Server::SendMovePlayer(u16 peer_id)
3797 {
3798         DSTACK(__FUNCTION_NAME);
3799         Player *player = m_env->getPlayer(peer_id);
3800         assert(player);
3801
3802         std::ostringstream os(std::ios_base::binary);
3803         writeU16(os, TOCLIENT_MOVE_PLAYER);
3804         writeV3F1000(os, player->getPosition());
3805         writeF1000(os, player->getPitch());
3806         writeF1000(os, player->getYaw());
3807
3808         {
3809                 v3f pos = player->getPosition();
3810                 f32 pitch = player->getPitch();
3811                 f32 yaw = player->getYaw();
3812                 verbosestream<<"Server: Sending TOCLIENT_MOVE_PLAYER"
3813                                 <<" pos=("<<pos.X<<","<<pos.Y<<","<<pos.Z<<")"
3814                                 <<" pitch="<<pitch
3815                                 <<" yaw="<<yaw
3816                                 <<std::endl;
3817         }
3818
3819         // Make data buffer
3820         std::string s = os.str();
3821         SharedBuffer<u8> data((u8*)s.c_str(), s.size());
3822         // Send as reliable
3823         m_con.Send(peer_id, 0, data, true);
3824 }
3825
3826 void Server::SendPlayerPrivileges(u16 peer_id)
3827 {
3828         Player *player = m_env->getPlayer(peer_id);
3829         assert(player);
3830         if(player->peer_id == PEER_ID_INEXISTENT)
3831                 return;
3832
3833         std::set<std::string> privs;
3834         scriptapi_get_auth(m_lua, player->getName(), NULL, &privs);
3835
3836         std::ostringstream os(std::ios_base::binary);
3837         writeU16(os, TOCLIENT_PRIVILEGES);
3838         writeU16(os, privs.size());
3839         for(std::set<std::string>::const_iterator i = privs.begin();
3840                         i != privs.end(); i++){
3841                 os<<serializeString(*i);
3842         }
3843
3844         // Make data buffer
3845         std::string s = os.str();
3846         SharedBuffer<u8> data((u8*)s.c_str(), s.size());
3847         // Send as reliable
3848         m_con.Send(peer_id, 0, data, true);
3849 }
3850
3851 void Server::SendPlayerInventoryFormspec(u16 peer_id)
3852 {
3853         Player *player = m_env->getPlayer(peer_id);
3854         assert(player);
3855         if(player->peer_id == PEER_ID_INEXISTENT)
3856                 return;
3857
3858         std::ostringstream os(std::ios_base::binary);
3859         writeU16(os, TOCLIENT_INVENTORY_FORMSPEC);
3860         os<<serializeLongString(player->inventory_formspec);
3861
3862         // Make data buffer
3863         std::string s = os.str();
3864         SharedBuffer<u8> data((u8*)s.c_str(), s.size());
3865         // Send as reliable
3866         m_con.Send(peer_id, 0, data, true);
3867 }
3868
3869 s32 Server::playSound(const SimpleSoundSpec &spec,
3870                 const ServerSoundParams &params)
3871 {
3872         // Find out initial position of sound
3873         bool pos_exists = false;
3874         v3f pos = params.getPos(m_env, &pos_exists);
3875         // If position is not found while it should be, cancel sound
3876         if(pos_exists != (params.type != ServerSoundParams::SSP_LOCAL))
3877                 return -1;
3878         // Filter destination clients
3879         std::set<RemoteClient*> dst_clients;
3880         if(params.to_player != "")
3881         {
3882                 Player *player = m_env->getPlayer(params.to_player.c_str());
3883                 if(!player){
3884                         infostream<<"Server::playSound: Player \""<<params.to_player
3885                                         <<"\" not found"<<std::endl;
3886                         return -1;
3887                 }
3888                 if(player->peer_id == PEER_ID_INEXISTENT){
3889                         infostream<<"Server::playSound: Player \""<<params.to_player
3890                                         <<"\" not connected"<<std::endl;
3891                         return -1;
3892                 }
3893                 RemoteClient *client = getClient(player->peer_id);
3894                 dst_clients.insert(client);
3895         }
3896         else
3897         {
3898                 for(core::map<u16, RemoteClient*>::Iterator
3899                                 i = m_clients.getIterator(); i.atEnd() == false; i++)
3900                 {
3901                         RemoteClient *client = i.getNode()->getValue();
3902                         Player *player = m_env->getPlayer(client->peer_id);
3903                         if(!player)
3904                                 continue;
3905                         if(pos_exists){
3906                                 if(player->getPosition().getDistanceFrom(pos) >
3907                                                 params.max_hear_distance)
3908                                         continue;
3909                         }
3910                         dst_clients.insert(client);
3911                 }
3912         }
3913         if(dst_clients.size() == 0)
3914                 return -1;
3915         // Create the sound
3916         s32 id = m_next_sound_id++;
3917         // The sound will exist as a reference in m_playing_sounds
3918         m_playing_sounds[id] = ServerPlayingSound();
3919         ServerPlayingSound &psound = m_playing_sounds[id];
3920         psound.params = params;
3921         for(std::set<RemoteClient*>::iterator i = dst_clients.begin();
3922                         i != dst_clients.end(); i++)
3923                 psound.clients.insert((*i)->peer_id);
3924         // Create packet
3925         std::ostringstream os(std::ios_base::binary);
3926         writeU16(os, TOCLIENT_PLAY_SOUND);
3927         writeS32(os, id);
3928         os<<serializeString(spec.name);
3929         writeF1000(os, spec.gain * params.gain);
3930         writeU8(os, params.type);
3931         writeV3F1000(os, pos);
3932         writeU16(os, params.object);
3933         writeU8(os, params.loop);
3934         // Make data buffer
3935         std::string s = os.str();
3936         SharedBuffer<u8> data((u8*)s.c_str(), s.size());
3937         // Send
3938         for(std::set<RemoteClient*>::iterator i = dst_clients.begin();
3939                         i != dst_clients.end(); i++){
3940                 // Send as reliable
3941                 m_con.Send((*i)->peer_id, 0, data, true);
3942         }
3943         return id;
3944 }
3945 void Server::stopSound(s32 handle)
3946 {
3947         // Get sound reference
3948         std::map<s32, ServerPlayingSound>::iterator i =
3949                         m_playing_sounds.find(handle);
3950         if(i == m_playing_sounds.end())
3951                 return;
3952         ServerPlayingSound &psound = i->second;
3953         // Create packet
3954         std::ostringstream os(std::ios_base::binary);
3955         writeU16(os, TOCLIENT_STOP_SOUND);
3956         writeS32(os, handle);
3957         // Make data buffer
3958         std::string s = os.str();
3959         SharedBuffer<u8> data((u8*)s.c_str(), s.size());
3960         // Send
3961         for(std::set<u16>::iterator i = psound.clients.begin();
3962                         i != psound.clients.end(); i++){
3963                 // Send as reliable
3964                 m_con.Send(*i, 0, data, true);
3965         }
3966         // Remove sound reference
3967         m_playing_sounds.erase(i);
3968 }
3969
3970 void Server::sendRemoveNode(v3s16 p, u16 ignore_id,
3971         core::list<u16> *far_players, float far_d_nodes)
3972 {
3973         float maxd = far_d_nodes*BS;
3974         v3f p_f = intToFloat(p, BS);
3975
3976         // Create packet
3977         u32 replysize = 8;
3978         SharedBuffer<u8> reply(replysize);
3979         writeU16(&reply[0], TOCLIENT_REMOVENODE);
3980         writeS16(&reply[2], p.X);
3981         writeS16(&reply[4], p.Y);
3982         writeS16(&reply[6], p.Z);
3983
3984         for(core::map<u16, RemoteClient*>::Iterator
3985                 i = m_clients.getIterator();
3986                 i.atEnd() == false; i++)
3987         {
3988                 // Get client and check that it is valid
3989                 RemoteClient *client = i.getNode()->getValue();
3990                 assert(client->peer_id == i.getNode()->getKey());
3991                 if(client->serialization_version == SER_FMT_VER_INVALID)
3992                         continue;
3993
3994                 // Don't send if it's the same one
3995                 if(client->peer_id == ignore_id)
3996                         continue;
3997
3998                 if(far_players)
3999                 {
4000                         // Get player
4001                         Player *player = m_env->getPlayer(client->peer_id);
4002                         if(player)
4003                         {
4004                                 // If player is far away, only set modified blocks not sent
4005                                 v3f player_pos = player->getPosition();
4006                                 if(player_pos.getDistanceFrom(p_f) > maxd)
4007                                 {
4008                                         far_players->push_back(client->peer_id);
4009                                         continue;
4010                                 }
4011                         }
4012                 }
4013
4014                 // Send as reliable
4015                 m_con.Send(client->peer_id, 0, reply, true);
4016         }
4017 }
4018
4019 void Server::sendAddNode(v3s16 p, MapNode n, u16 ignore_id,
4020                 core::list<u16> *far_players, float far_d_nodes)
4021 {
4022         float maxd = far_d_nodes*BS;
4023         v3f p_f = intToFloat(p, BS);
4024
4025         for(core::map<u16, RemoteClient*>::Iterator
4026                 i = m_clients.getIterator();
4027                 i.atEnd() == false; i++)
4028         {
4029                 // Get client and check that it is valid
4030                 RemoteClient *client = i.getNode()->getValue();
4031                 assert(client->peer_id == i.getNode()->getKey());
4032                 if(client->serialization_version == SER_FMT_VER_INVALID)
4033                         continue;
4034
4035                 // Don't send if it's the same one
4036                 if(client->peer_id == ignore_id)
4037                         continue;
4038
4039                 if(far_players)
4040                 {
4041                         // Get player
4042                         Player *player = m_env->getPlayer(client->peer_id);
4043                         if(player)
4044                         {
4045                                 // If player is far away, only set modified blocks not sent
4046                                 v3f player_pos = player->getPosition();
4047                                 if(player_pos.getDistanceFrom(p_f) > maxd)
4048                                 {
4049                                         far_players->push_back(client->peer_id);
4050                                         continue;
4051                                 }
4052                         }
4053                 }
4054
4055                 // Create packet
4056                 u32 replysize = 8 + MapNode::serializedLength(client->serialization_version);
4057                 SharedBuffer<u8> reply(replysize);
4058                 writeU16(&reply[0], TOCLIENT_ADDNODE);
4059                 writeS16(&reply[2], p.X);
4060                 writeS16(&reply[4], p.Y);
4061                 writeS16(&reply[6], p.Z);
4062                 n.serialize(&reply[8], client->serialization_version);
4063
4064                 // Send as reliable
4065                 m_con.Send(client->peer_id, 0, reply, true);
4066         }
4067 }
4068
4069 void Server::setBlockNotSent(v3s16 p)
4070 {
4071         for(core::map<u16, RemoteClient*>::Iterator
4072                 i = m_clients.getIterator();
4073                 i.atEnd()==false; i++)
4074         {
4075                 RemoteClient *client = i.getNode()->getValue();
4076                 client->SetBlockNotSent(p);
4077         }
4078 }
4079
4080 void Server::SendBlockNoLock(u16 peer_id, MapBlock *block, u8 ver)
4081 {
4082         DSTACK(__FUNCTION_NAME);
4083
4084         v3s16 p = block->getPos();
4085
4086 #if 0
4087         // Analyze it a bit
4088         bool completely_air = true;
4089         for(s16 z0=0; z0<MAP_BLOCKSIZE; z0++)
4090         for(s16 x0=0; x0<MAP_BLOCKSIZE; x0++)
4091         for(s16 y0=0; y0<MAP_BLOCKSIZE; y0++)
4092         {
4093                 if(block->getNodeNoEx(v3s16(x0,y0,z0)).d != CONTENT_AIR)
4094                 {
4095                         completely_air = false;
4096                         x0 = y0 = z0 = MAP_BLOCKSIZE; // Break out
4097                 }
4098         }
4099
4100         // Print result
4101         infostream<<"Server: Sending block ("<<p.X<<","<<p.Y<<","<<p.Z<<"): ";
4102         if(completely_air)
4103                 infostream<<"[completely air] ";
4104         infostream<<std::endl;
4105 #endif
4106
4107         /*
4108                 Create a packet with the block in the right format
4109         */
4110
4111         std::ostringstream os(std::ios_base::binary);
4112         block->serialize(os, ver, false);
4113         std::string s = os.str();
4114         SharedBuffer<u8> blockdata((u8*)s.c_str(), s.size());
4115
4116         u32 replysize = 8 + blockdata.getSize();
4117         SharedBuffer<u8> reply(replysize);
4118         writeU16(&reply[0], TOCLIENT_BLOCKDATA);
4119         writeS16(&reply[2], p.X);
4120         writeS16(&reply[4], p.Y);
4121         writeS16(&reply[6], p.Z);
4122         memcpy(&reply[8], *blockdata, blockdata.getSize());
4123
4124         /*infostream<<"Server: Sending block ("<<p.X<<","<<p.Y<<","<<p.Z<<")"
4125                         <<":  \tpacket size: "<<replysize<<std::endl;*/
4126
4127         /*
4128                 Send packet
4129         */
4130         m_con.Send(peer_id, 1, reply, true);
4131 }
4132
4133 void Server::SendBlocks(float dtime)
4134 {
4135         DSTACK(__FUNCTION_NAME);
4136
4137         JMutexAutoLock envlock(m_env_mutex);
4138         JMutexAutoLock conlock(m_con_mutex);
4139
4140         ScopeProfiler sp(g_profiler, "Server: sel and send blocks to clients");
4141
4142         core::array<PrioritySortedBlockTransfer> queue;
4143
4144         s32 total_sending = 0;
4145
4146         {
4147                 ScopeProfiler sp(g_profiler, "Server: selecting blocks for sending");
4148
4149                 for(core::map<u16, RemoteClient*>::Iterator
4150                         i = m_clients.getIterator();
4151                         i.atEnd() == false; i++)
4152                 {
4153                         RemoteClient *client = i.getNode()->getValue();
4154                         assert(client->peer_id == i.getNode()->getKey());
4155
4156                         // If definitions and textures have not been sent, don't
4157                         // send MapBlocks either
4158                         if(!client->definitions_sent)
4159                                 continue;
4160
4161                         total_sending += client->SendingCount();
4162
4163                         if(client->serialization_version == SER_FMT_VER_INVALID)
4164                                 continue;
4165
4166                         client->GetNextBlocks(this, dtime, queue);
4167                 }
4168         }
4169
4170         // Sort.
4171         // Lowest priority number comes first.
4172         // Lowest is most important.
4173         queue.sort();
4174
4175         for(u32 i=0; i<queue.size(); i++)
4176         {
4177                 //TODO: Calculate limit dynamically
4178                 if(total_sending >= g_settings->getS32
4179                                 ("max_simultaneous_block_sends_server_total"))
4180                         break;
4181
4182                 PrioritySortedBlockTransfer q = queue[i];
4183
4184                 MapBlock *block = NULL;
4185                 try
4186                 {
4187                         block = m_env->getMap().getBlockNoCreate(q.pos);
4188                 }
4189                 catch(InvalidPositionException &e)
4190                 {
4191                         continue;
4192                 }
4193
4194                 RemoteClient *client = getClient(q.peer_id);
4195
4196                 SendBlockNoLock(q.peer_id, block, client->serialization_version);
4197
4198                 client->SentBlock(q.pos);
4199
4200                 total_sending++;
4201         }
4202 }
4203
4204 void Server::fillMediaCache()
4205 {
4206         DSTACK(__FUNCTION_NAME);
4207
4208         infostream<<"Server: Calculating media file checksums"<<std::endl;
4209
4210         // Collect all media file paths
4211         std::list<std::string> paths;
4212         for(std::vector<ModSpec>::iterator i = m_mods.begin();
4213                         i != m_mods.end(); i++){
4214                 const ModSpec &mod = *i;
4215                 paths.push_back(mod.path + DIR_DELIM + "textures");
4216                 paths.push_back(mod.path + DIR_DELIM + "sounds");
4217                 paths.push_back(mod.path + DIR_DELIM + "media");
4218                 paths.push_back(mod.path + DIR_DELIM + "models");
4219         }
4220         std::string path_all = "textures";
4221         paths.push_back(path_all + DIR_DELIM + "all");
4222
4223         // Collect media file information from paths into cache
4224         for(std::list<std::string>::iterator i = paths.begin();
4225                         i != paths.end(); i++)
4226         {
4227                 std::string mediapath = *i;
4228                 std::vector<fs::DirListNode> dirlist = fs::GetDirListing(mediapath);
4229                 for(u32 j=0; j<dirlist.size(); j++){
4230                         if(dirlist[j].dir) // Ignode dirs
4231                                 continue;
4232                         std::string filename = dirlist[j].name;
4233                         // If name contains illegal characters, ignore the file
4234                         if(!string_allowed(filename, TEXTURENAME_ALLOWED_CHARS)){
4235                                 infostream<<"Server: ignoring illegal file name: \""
4236                                                 <<filename<<"\""<<std::endl;
4237                                 continue;
4238                         }
4239                         // If name is not in a supported format, ignore it
4240                         const char *supported_ext[] = {
4241                                 ".png", ".jpg", ".bmp", ".tga",
4242                                 ".pcx", ".ppm", ".psd", ".wal", ".rgb",
4243                                 ".ogg",
4244                                 ".x", ".b3d", ".md2", ".obj",
4245                                 NULL
4246                         };
4247                         if(removeStringEnd(filename, supported_ext) == ""){
4248                                 infostream<<"Server: ignoring unsupported file extension: \""
4249                                                 <<filename<<"\""<<std::endl;
4250                                 continue;
4251                         }
4252                         // Ok, attempt to load the file and add to cache
4253                         std::string filepath = mediapath + DIR_DELIM + filename;
4254                         // Read data
4255                         std::ifstream fis(filepath.c_str(), std::ios_base::binary);
4256                         if(fis.good() == false){
4257                                 errorstream<<"Server::fillMediaCache(): Could not open \""
4258                                                 <<filename<<"\" for reading"<<std::endl;
4259                                 continue;
4260                         }
4261                         std::ostringstream tmp_os(std::ios_base::binary);
4262                         bool bad = false;
4263                         for(;;){
4264                                 char buf[1024];
4265                                 fis.read(buf, 1024);
4266                                 std::streamsize len = fis.gcount();
4267                                 tmp_os.write(buf, len);
4268                                 if(fis.eof())
4269                                         break;
4270                                 if(!fis.good()){
4271                                         bad = true;
4272                                         break;
4273                                 }
4274                         }
4275                         if(bad){
4276                                 errorstream<<"Server::fillMediaCache(): Failed to read \""
4277                                                 <<filename<<"\""<<std::endl;
4278                                 continue;
4279                         }
4280                         if(tmp_os.str().length() == 0){
4281                                 errorstream<<"Server::fillMediaCache(): Empty file \""
4282                                                 <<filepath<<"\""<<std::endl;
4283                                 continue;
4284                         }
4285
4286                         SHA1 sha1;
4287                         sha1.addBytes(tmp_os.str().c_str(), tmp_os.str().length());
4288
4289                         unsigned char *digest = sha1.getDigest();
4290                         std::string sha1_base64 = base64_encode(digest, 20);
4291                         std::string sha1_hex = hex_encode((char*)digest, 20);
4292                         free(digest);
4293
4294                         // Put in list
4295                         this->m_media[filename] = MediaInfo(filepath, sha1_base64);
4296                         verbosestream<<"Server: "<<sha1_hex<<" is "<<filename<<std::endl;
4297                 }
4298         }
4299 }
4300
4301 struct SendableMediaAnnouncement
4302 {
4303         std::string name;
4304         std::string sha1_digest;
4305
4306         SendableMediaAnnouncement(const std::string name_="",
4307                         const std::string sha1_digest_=""):
4308                 name(name_),
4309                 sha1_digest(sha1_digest_)
4310         {}
4311 };
4312
4313 void Server::sendMediaAnnouncement(u16 peer_id)
4314 {
4315         DSTACK(__FUNCTION_NAME);
4316
4317         verbosestream<<"Server: Announcing files to id("<<peer_id<<")"
4318                         <<std::endl;
4319
4320         core::list<SendableMediaAnnouncement> file_announcements;
4321
4322         for(std::map<std::string, MediaInfo>::iterator i = m_media.begin();
4323                         i != m_media.end(); i++){
4324                 // Put in list
4325                 file_announcements.push_back(
4326                                 SendableMediaAnnouncement(i->first, i->second.sha1_digest));
4327         }
4328
4329         // Make packet
4330         std::ostringstream os(std::ios_base::binary);
4331
4332         /*
4333                 u16 command
4334                 u32 number of files
4335                 for each texture {
4336                         u16 length of name
4337                         string name
4338                         u16 length of sha1_digest
4339                         string sha1_digest
4340                 }
4341         */
4342
4343         writeU16(os, TOCLIENT_ANNOUNCE_MEDIA);
4344         writeU16(os, file_announcements.size());
4345
4346         for(core::list<SendableMediaAnnouncement>::Iterator
4347                         j = file_announcements.begin();
4348                         j != file_announcements.end(); j++){
4349                 os<<serializeString(j->name);
4350                 os<<serializeString(j->sha1_digest);
4351         }
4352         os<<serializeString(g_settings->get("remote_media"));
4353
4354         // Make data buffer
4355         std::string s = os.str();
4356         SharedBuffer<u8> data((u8*)s.c_str(), s.size());
4357
4358         // Send as reliable
4359         m_con.Send(peer_id, 0, data, true);
4360 }
4361
4362 struct SendableMedia
4363 {
4364         std::string name;
4365         std::string path;
4366         std::string data;
4367
4368         SendableMedia(const std::string &name_="", const std::string path_="",
4369                         const std::string &data_=""):
4370                 name(name_),
4371                 path(path_),
4372                 data(data_)
4373         {}
4374 };
4375
4376 void Server::sendRequestedMedia(u16 peer_id,
4377                 const core::list<MediaRequest> &tosend)
4378 {
4379         DSTACK(__FUNCTION_NAME);
4380
4381         verbosestream<<"Server::sendRequestedMedia(): "
4382                         <<"Sending files to client"<<std::endl;
4383
4384         /* Read files */
4385
4386         // Put 5kB in one bunch (this is not accurate)
4387         u32 bytes_per_bunch = 5000;
4388
4389         core::array< core::list<SendableMedia> > file_bunches;
4390         file_bunches.push_back(core::list<SendableMedia>());
4391
4392         u32 file_size_bunch_total = 0;
4393
4394         for(core::list<MediaRequest>::ConstIterator i = tosend.begin();
4395                         i != tosend.end(); i++)
4396         {
4397                 if(m_media.find(i->name) == m_media.end()){
4398                         errorstream<<"Server::sendRequestedMedia(): Client asked for "
4399                                         <<"unknown file \""<<(i->name)<<"\""<<std::endl;
4400                         continue;
4401                 }
4402
4403                 //TODO get path + name
4404                 std::string tpath = m_media[(*i).name].path;
4405
4406                 // Read data
4407                 std::ifstream fis(tpath.c_str(), std::ios_base::binary);
4408                 if(fis.good() == false){
4409                         errorstream<<"Server::sendRequestedMedia(): Could not open \""
4410                                         <<tpath<<"\" for reading"<<std::endl;
4411                         continue;
4412                 }
4413                 std::ostringstream tmp_os(std::ios_base::binary);
4414                 bool bad = false;
4415                 for(;;){
4416                         char buf[1024];
4417                         fis.read(buf, 1024);
4418                         std::streamsize len = fis.gcount();
4419                         tmp_os.write(buf, len);
4420                         file_size_bunch_total += len;
4421                         if(fis.eof())
4422                                 break;
4423                         if(!fis.good()){
4424                                 bad = true;
4425                                 break;
4426                         }
4427                 }
4428                 if(bad){
4429                         errorstream<<"Server::sendRequestedMedia(): Failed to read \""
4430                                         <<(*i).name<<"\""<<std::endl;
4431                         continue;
4432                 }
4433                 /*infostream<<"Server::sendRequestedMedia(): Loaded \""
4434                                 <<tname<<"\""<<std::endl;*/
4435                 // Put in list
4436                 file_bunches[file_bunches.size()-1].push_back(
4437                                 SendableMedia((*i).name, tpath, tmp_os.str()));
4438
4439                 // Start next bunch if got enough data
4440                 if(file_size_bunch_total >= bytes_per_bunch){
4441                         file_bunches.push_back(core::list<SendableMedia>());
4442                         file_size_bunch_total = 0;
4443                 }
4444
4445         }
4446
4447         /* Create and send packets */
4448
4449         u32 num_bunches = file_bunches.size();
4450         for(u32 i=0; i<num_bunches; i++)
4451         {
4452                 std::ostringstream os(std::ios_base::binary);
4453
4454                 /*
4455                         u16 command
4456                         u16 total number of texture bunches
4457                         u16 index of this bunch
4458                         u32 number of files in this bunch
4459                         for each file {
4460                                 u16 length of name
4461                                 string name
4462                                 u32 length of data
4463                                 data
4464                         }
4465                 */
4466
4467                 writeU16(os, TOCLIENT_MEDIA);
4468                 writeU16(os, num_bunches);
4469                 writeU16(os, i);
4470                 writeU32(os, file_bunches[i].size());
4471
4472                 for(core::list<SendableMedia>::Iterator
4473                                 j = file_bunches[i].begin();
4474                                 j != file_bunches[i].end(); j++){
4475                         os<<serializeString(j->name);
4476                         os<<serializeLongString(j->data);
4477                 }
4478
4479                 // Make data buffer
4480                 std::string s = os.str();
4481                 verbosestream<<"Server::sendRequestedMedia(): bunch "
4482                                 <<i<<"/"<<num_bunches
4483                                 <<" files="<<file_bunches[i].size()
4484                                 <<" size=" <<s.size()<<std::endl;
4485                 SharedBuffer<u8> data((u8*)s.c_str(), s.size());
4486                 // Send as reliable
4487                 m_con.Send(peer_id, 0, data, true);
4488         }
4489 }
4490
4491 void Server::sendDetachedInventory(const std::string &name, u16 peer_id)
4492 {
4493         if(m_detached_inventories.count(name) == 0){
4494                 errorstream<<__FUNCTION_NAME<<": \""<<name<<"\" not found"<<std::endl;
4495                 return;
4496         }
4497         Inventory *inv = m_detached_inventories[name];
4498
4499         std::ostringstream os(std::ios_base::binary);
4500         writeU16(os, TOCLIENT_DETACHED_INVENTORY);
4501         os<<serializeString(name);
4502         inv->serialize(os);
4503
4504         // Make data buffer
4505         std::string s = os.str();
4506         SharedBuffer<u8> data((u8*)s.c_str(), s.size());
4507         // Send as reliable
4508         m_con.Send(peer_id, 0, data, true);
4509 }
4510
4511 void Server::sendDetachedInventoryToAll(const std::string &name)
4512 {
4513         DSTACK(__FUNCTION_NAME);
4514
4515         for(core::map<u16, RemoteClient*>::Iterator
4516                         i = m_clients.getIterator();
4517                         i.atEnd() == false; i++){
4518                 RemoteClient *client = i.getNode()->getValue();
4519                 sendDetachedInventory(name, client->peer_id);
4520         }
4521 }
4522
4523 void Server::sendDetachedInventories(u16 peer_id)
4524 {
4525         DSTACK(__FUNCTION_NAME);
4526
4527         for(std::map<std::string, Inventory*>::iterator
4528                         i = m_detached_inventories.begin();
4529                         i != m_detached_inventories.end(); i++){
4530                 const std::string &name = i->first;
4531                 //Inventory *inv = i->second;
4532                 sendDetachedInventory(name, peer_id);
4533         }
4534 }
4535
4536 /*
4537         Something random
4538 */
4539
4540 void Server::DiePlayer(u16 peer_id)
4541 {
4542         DSTACK(__FUNCTION_NAME);
4543
4544         PlayerSAO *playersao = getPlayerSAO(peer_id);
4545         assert(playersao);
4546
4547         infostream<<"Server::DiePlayer(): Player "
4548                         <<playersao->getPlayer()->getName()
4549                         <<" dies"<<std::endl;
4550
4551         playersao->setHP(0);
4552
4553         // Trigger scripted stuff
4554         scriptapi_on_dieplayer(m_lua, playersao);
4555
4556         SendPlayerHP(peer_id);
4557         SendDeathscreen(m_con, peer_id, false, v3f(0,0,0));
4558 }
4559
4560 void Server::RespawnPlayer(u16 peer_id)
4561 {
4562         DSTACK(__FUNCTION_NAME);
4563
4564         PlayerSAO *playersao = getPlayerSAO(peer_id);
4565         assert(playersao);
4566
4567         infostream<<"Server::RespawnPlayer(): Player "
4568                         <<playersao->getPlayer()->getName()
4569                         <<" respawns"<<std::endl;
4570
4571         playersao->setHP(PLAYER_MAX_HP);
4572
4573         bool repositioned = scriptapi_on_respawnplayer(m_lua, playersao);
4574         if(!repositioned){
4575                 v3f pos = findSpawnPos(m_env->getServerMap());
4576                 playersao->setPos(pos);
4577         }
4578 }
4579
4580 void Server::UpdateCrafting(u16 peer_id)
4581 {
4582         DSTACK(__FUNCTION_NAME);
4583
4584         Player* player = m_env->getPlayer(peer_id);
4585         assert(player);
4586
4587         // Get a preview for crafting
4588         ItemStack preview;
4589         getCraftingResult(&player->inventory, preview, false, this);
4590
4591         // Put the new preview in
4592         InventoryList *plist = player->inventory.getList("craftpreview");
4593         assert(plist);
4594         assert(plist->getSize() >= 1);
4595         plist->changeItem(0, preview);
4596 }
4597
4598 RemoteClient* Server::getClient(u16 peer_id)
4599 {
4600         DSTACK(__FUNCTION_NAME);
4601         //JMutexAutoLock lock(m_con_mutex);
4602         core::map<u16, RemoteClient*>::Node *n;
4603         n = m_clients.find(peer_id);
4604         // A client should exist for all peers
4605         assert(n != NULL);
4606         return n->getValue();
4607 }
4608
4609 std::wstring Server::getStatusString()
4610 {
4611         std::wostringstream os(std::ios_base::binary);
4612         os<<L"# Server: ";
4613         // Version
4614         os<<L"version="<<narrow_to_wide(VERSION_STRING);
4615         // Uptime
4616         os<<L", uptime="<<m_uptime.get();
4617         // Information about clients
4618         core::map<u16, RemoteClient*>::Iterator i;
4619         bool first;
4620         os<<L", clients={";
4621         for(i = m_clients.getIterator(), first = true;
4622                 i.atEnd() == false; i++)
4623         {
4624                 // Get client and check that it is valid
4625                 RemoteClient *client = i.getNode()->getValue();
4626                 assert(client->peer_id == i.getNode()->getKey());
4627                 if(client->serialization_version == SER_FMT_VER_INVALID)
4628                         continue;
4629                 // Get player
4630                 Player *player = m_env->getPlayer(client->peer_id);
4631                 // Get name of player
4632                 std::wstring name = L"unknown";
4633                 if(player != NULL)
4634                         name = narrow_to_wide(player->getName());
4635                 // Add name to information string
4636                 if(!first)
4637                         os<<L",";
4638                 else
4639                         first = false;
4640                 os<<name;
4641         }
4642         os<<L"}";
4643         if(((ServerMap*)(&m_env->getMap()))->isSavingEnabled() == false)
4644                 os<<std::endl<<L"# Server: "<<" WARNING: Map saving is disabled.";
4645         if(g_settings->get("motd") != "")
4646                 os<<std::endl<<L"# Server: "<<narrow_to_wide(g_settings->get("motd"));
4647         return os.str();
4648 }
4649
4650 std::set<std::string> Server::getPlayerEffectivePrivs(const std::string &name)
4651 {
4652         std::set<std::string> privs;
4653         scriptapi_get_auth(m_lua, name, NULL, &privs);
4654         return privs;
4655 }
4656
4657 bool Server::checkPriv(const std::string &name, const std::string &priv)
4658 {
4659         std::set<std::string> privs = getPlayerEffectivePrivs(name);
4660         return (privs.count(priv) != 0);
4661 }
4662
4663 void Server::reportPrivsModified(const std::string &name)
4664 {
4665         if(name == ""){
4666                 for(core::map<u16, RemoteClient*>::Iterator
4667                                 i = m_clients.getIterator();
4668                                 i.atEnd() == false; i++){
4669                         RemoteClient *client = i.getNode()->getValue();
4670                         Player *player = m_env->getPlayer(client->peer_id);
4671                         reportPrivsModified(player->getName());
4672                 }
4673         } else {
4674                 Player *player = m_env->getPlayer(name.c_str());
4675                 if(!player)
4676                         return;
4677                 SendPlayerPrivileges(player->peer_id);
4678                 PlayerSAO *sao = player->getPlayerSAO();
4679                 if(!sao)
4680                         return;
4681                 sao->updatePrivileges(
4682                                 getPlayerEffectivePrivs(name),
4683                                 isSingleplayer());
4684         }
4685 }
4686
4687 void Server::reportInventoryFormspecModified(const std::string &name)
4688 {
4689         Player *player = m_env->getPlayer(name.c_str());
4690         if(!player)
4691                 return;
4692         SendPlayerInventoryFormspec(player->peer_id);
4693 }
4694
4695 // Saves g_settings to configpath given at initialization
4696 void Server::saveConfig()
4697 {
4698         if(m_path_config != "")
4699                 g_settings->updateConfigFile(m_path_config.c_str());
4700 }
4701
4702 void Server::notifyPlayer(const char *name, const std::wstring msg)
4703 {
4704         Player *player = m_env->getPlayer(name);
4705         if(!player)
4706                 return;
4707         SendChatMessage(player->peer_id, std::wstring(L"Server: -!- ")+msg);
4708 }
4709
4710 bool Server::showFormspec(const char *playername, const std::string &formspec, const std::string &formname)
4711 {
4712         Player *player = m_env->getPlayer(playername);
4713
4714         if(!player)
4715         {
4716                 infostream<<"showFormspec: couldn't find player:"<<playername<<std::endl;
4717                 return false;
4718         }
4719
4720         SendShowFormspecMessage(player->peer_id, formspec, formname);
4721         return true;
4722 }
4723
4724 void Server::notifyPlayers(const std::wstring msg)
4725 {
4726         BroadcastChatMessage(msg);
4727 }
4728
4729 void Server::queueBlockEmerge(v3s16 blockpos, bool allow_generate)
4730 {
4731         u8 flags = 0;
4732         if(!allow_generate)
4733                 flags |= BLOCK_EMERGE_FLAG_FROMDISK;
4734         m_emerge_queue.addBlock(PEER_ID_INEXISTENT, blockpos, flags);
4735 }
4736
4737 Inventory* Server::createDetachedInventory(const std::string &name)
4738 {
4739         if(m_detached_inventories.count(name) > 0){
4740                 infostream<<"Server clearing detached inventory \""<<name<<"\""<<std::endl;
4741                 delete m_detached_inventories[name];
4742         } else {
4743                 infostream<<"Server creating detached inventory \""<<name<<"\""<<std::endl;
4744         }
4745         Inventory *inv = new Inventory(m_itemdef);
4746         assert(inv);
4747         m_detached_inventories[name] = inv;
4748         sendDetachedInventoryToAll(name);
4749         return inv;
4750 }
4751
4752 class BoolScopeSet
4753 {
4754 public:
4755         BoolScopeSet(bool *dst, bool val):
4756                 m_dst(dst)
4757         {
4758                 m_orig_state = *m_dst;
4759                 *m_dst = val;
4760         }
4761         ~BoolScopeSet()
4762         {
4763                 *m_dst = m_orig_state;
4764         }
4765 private:
4766         bool *m_dst;
4767         bool m_orig_state;
4768 };
4769
4770 // actions: time-reversed list
4771 // Return value: success/failure
4772 bool Server::rollbackRevertActions(const std::list<RollbackAction> &actions,
4773                 std::list<std::string> *log)
4774 {
4775         infostream<<"Server::rollbackRevertActions(len="<<actions.size()<<")"<<std::endl;
4776         ServerMap *map = (ServerMap*)(&m_env->getMap());
4777         // Disable rollback report sink while reverting
4778         BoolScopeSet rollback_scope_disable(&m_rollback_sink_enabled, false);
4779
4780         // Fail if no actions to handle
4781         if(actions.empty()){
4782                 log->push_back("Nothing to do.");
4783                 return false;
4784         }
4785
4786         int num_tried = 0;
4787         int num_failed = 0;
4788
4789         for(std::list<RollbackAction>::const_iterator
4790                         i = actions.begin();
4791                         i != actions.end(); i++)
4792         {
4793                 const RollbackAction &action = *i;
4794                 num_tried++;
4795                 bool success = action.applyRevert(map, this, this);
4796                 if(!success){
4797                         num_failed++;
4798                         std::ostringstream os;
4799                         os<<"Revert of step ("<<num_tried<<") "<<action.toString()<<" failed";
4800                         infostream<<"Map::rollbackRevertActions(): "<<os.str()<<std::endl;
4801                         if(log)
4802                                 log->push_back(os.str());
4803                 }else{
4804                         std::ostringstream os;
4805                         os<<"Successfully reverted step ("<<num_tried<<") "<<action.toString();
4806                         infostream<<"Map::rollbackRevertActions(): "<<os.str()<<std::endl;
4807                         if(log)
4808                                 log->push_back(os.str());
4809                 }
4810         }
4811
4812         infostream<<"Map::rollbackRevertActions(): "<<num_failed<<"/"<<num_tried
4813                         <<" failed"<<std::endl;
4814
4815         // Call it done if less than half failed
4816         return num_failed <= num_tried/2;
4817 }
4818
4819 // IGameDef interface
4820 // Under envlock
4821 IItemDefManager* Server::getItemDefManager()
4822 {
4823         return m_itemdef;
4824 }
4825 INodeDefManager* Server::getNodeDefManager()
4826 {
4827         return m_nodedef;
4828 }
4829 ICraftDefManager* Server::getCraftDefManager()
4830 {
4831         return m_craftdef;
4832 }
4833 ITextureSource* Server::getTextureSource()
4834 {
4835         return NULL;
4836 }
4837 IShaderSource* Server::getShaderSource()
4838 {
4839         return NULL;
4840 }
4841 u16 Server::allocateUnknownNodeId(const std::string &name)
4842 {
4843         return m_nodedef->allocateDummy(name);
4844 }
4845 ISoundManager* Server::getSoundManager()
4846 {
4847         return &dummySoundManager;
4848 }
4849 MtEventManager* Server::getEventManager()
4850 {
4851         return m_event;
4852 }
4853 IRollbackReportSink* Server::getRollbackReportSink()
4854 {
4855         if(!m_enable_rollback_recording)
4856                 return NULL;
4857         if(!m_rollback_sink_enabled)
4858                 return NULL;
4859         return m_rollback;
4860 }
4861
4862 IWritableItemDefManager* Server::getWritableItemDefManager()
4863 {
4864         return m_itemdef;
4865 }
4866 IWritableNodeDefManager* Server::getWritableNodeDefManager()
4867 {
4868         return m_nodedef;
4869 }
4870 IWritableCraftDefManager* Server::getWritableCraftDefManager()
4871 {
4872         return m_craftdef;
4873 }
4874
4875 const ModSpec* Server::getModSpec(const std::string &modname)
4876 {
4877         for(std::vector<ModSpec>::iterator i = m_mods.begin();
4878                         i != m_mods.end(); i++){
4879                 const ModSpec &mod = *i;
4880                 if(mod.name == modname)
4881                         return &mod;
4882         }
4883         return NULL;
4884 }
4885 void Server::getModNames(core::list<std::string> &modlist)
4886 {
4887         for(std::vector<ModSpec>::iterator i = m_mods.begin(); i != m_mods.end(); i++)
4888         {
4889                 modlist.push_back((*i).name);
4890         }
4891 }
4892 std::string Server::getBuiltinLuaPath()
4893 {
4894         return porting::path_share + DIR_DELIM + "builtin";
4895 }
4896
4897 v3f findSpawnPos(ServerMap &map)
4898 {
4899         //return v3f(50,50,50)*BS;
4900
4901         v3s16 nodepos;
4902
4903 #if 0
4904         nodepos = v2s16(0,0);
4905         groundheight = 20;
4906 #endif
4907
4908 #if 1
4909         s16 water_level = map.m_mgparams->water_level;
4910
4911         // Try to find a good place a few times
4912         for(s32 i=0; i<1000; i++)
4913         {
4914                 s32 range = 1 + i;
4915                 // We're going to try to throw the player to this position
4916                 v2s16 nodepos2d = v2s16(-range + (myrand()%(range*2)),
4917                                 -range + (myrand()%(range*2)));
4918                 //v2s16 sectorpos = getNodeSectorPos(nodepos2d);
4919                 // Get ground height at point (fallbacks to heightmap function)
4920                 s16 groundheight = map.findGroundLevel(nodepos2d);
4921                 // Don't go underwater
4922                 if(groundheight <= water_level)
4923                 {
4924                         //infostream<<"-> Underwater"<<std::endl;
4925                         continue;
4926                 }
4927                 // Don't go to high places
4928                 if(groundheight > water_level + 6)
4929                 {
4930                         //infostream<<"-> Underwater"<<std::endl;
4931                         continue;
4932                 }
4933
4934                 nodepos = v3s16(nodepos2d.X, groundheight-2, nodepos2d.Y);
4935                 bool is_good = false;
4936                 s32 air_count = 0;
4937                 for(s32 i=0; i<10; i++){
4938                         v3s16 blockpos = getNodeBlockPos(nodepos);
4939                         map.emergeBlock(blockpos, true);
4940                         MapNode n = map.getNodeNoEx(nodepos);
4941                         if(n.getContent() == CONTENT_AIR){
4942                                 air_count++;
4943                                 if(air_count >= 2){
4944                                         is_good = true;
4945                                         nodepos.Y -= 1;
4946                                         break;
4947                                 }
4948                         }
4949                         nodepos.Y++;
4950                 }
4951                 if(is_good){
4952                         // Found a good place
4953                         //infostream<<"Searched through "<<i<<" places."<<std::endl;
4954                         break;
4955                 }
4956         }
4957 #endif
4958
4959         return intToFloat(nodepos, BS);
4960 }
4961
4962 PlayerSAO* Server::emergePlayer(const char *name, u16 peer_id)
4963 {
4964         RemotePlayer *player = NULL;
4965         bool newplayer = false;
4966
4967         /*
4968                 Try to get an existing player
4969         */
4970         player = static_cast<RemotePlayer*>(m_env->getPlayer(name));
4971
4972         // If player is already connected, cancel
4973         if(player != NULL && player->peer_id != 0)
4974         {
4975                 infostream<<"emergePlayer(): Player already connected"<<std::endl;
4976                 return NULL;
4977         }
4978
4979         /*
4980                 If player with the wanted peer_id already exists, cancel.
4981         */
4982         if(m_env->getPlayer(peer_id) != NULL)
4983         {
4984                 infostream<<"emergePlayer(): Player with wrong name but same"
4985                                 " peer_id already exists"<<std::endl;
4986                 return NULL;
4987         }
4988
4989         /*
4990                 Create a new player if it doesn't exist yet
4991         */
4992         if(player == NULL)
4993         {
4994                 newplayer = true;
4995                 player = new RemotePlayer(this);
4996                 player->updateName(name);
4997
4998                 /* Set player position */
4999                 infostream<<"Server: Finding spawn place for player \""
5000                                 <<name<<"\""<<std::endl;
5001                 v3f pos = findSpawnPos(m_env->getServerMap());
5002                 player->setPosition(pos);
5003
5004                 /* Add player to environment */
5005                 m_env->addPlayer(player);
5006         }
5007
5008         /*
5009                 Create a new player active object
5010         */
5011         PlayerSAO *playersao = new PlayerSAO(m_env, player, peer_id,
5012                         getPlayerEffectivePrivs(player->getName()),
5013                         isSingleplayer());
5014
5015         /* Add object to environment */
5016         m_env->addActiveObject(playersao);
5017
5018         /* Run scripts */
5019         if(newplayer)
5020                 scriptapi_on_newplayer(m_lua, playersao);
5021
5022         scriptapi_on_joinplayer(m_lua, playersao);
5023
5024         return playersao;
5025 }
5026
5027 void Server::handlePeerChange(PeerChange &c)
5028 {
5029         JMutexAutoLock envlock(m_env_mutex);
5030         JMutexAutoLock conlock(m_con_mutex);
5031
5032         if(c.type == PEER_ADDED)
5033         {
5034                 /*
5035                         Add
5036                 */
5037
5038                 // Error check
5039                 core::map<u16, RemoteClient*>::Node *n;
5040                 n = m_clients.find(c.peer_id);
5041                 // The client shouldn't already exist
5042                 assert(n == NULL);
5043
5044                 // Create client
5045                 RemoteClient *client = new RemoteClient();
5046                 client->peer_id = c.peer_id;
5047                 m_clients.insert(client->peer_id, client);
5048
5049         } // PEER_ADDED
5050         else if(c.type == PEER_REMOVED)
5051         {
5052                 /*
5053                         Delete
5054                 */
5055
5056                 // Error check
5057                 core::map<u16, RemoteClient*>::Node *n;
5058                 n = m_clients.find(c.peer_id);
5059                 // The client should exist
5060                 assert(n != NULL);
5061
5062                 /*
5063                         Mark objects to be not known by the client
5064                 */
5065                 RemoteClient *client = n->getValue();
5066                 // Handle objects
5067                 for(core::map<u16, bool>::Iterator
5068                                 i = client->m_known_objects.getIterator();
5069                                 i.atEnd()==false; i++)
5070                 {
5071                         // Get object
5072                         u16 id = i.getNode()->getKey();
5073                         ServerActiveObject* obj = m_env->getActiveObject(id);
5074
5075                         if(obj && obj->m_known_by_count > 0)
5076                                 obj->m_known_by_count--;
5077                 }
5078
5079                 /*
5080                         Clear references to playing sounds
5081                 */
5082                 for(std::map<s32, ServerPlayingSound>::iterator
5083                                 i = m_playing_sounds.begin();
5084                                 i != m_playing_sounds.end();)
5085                 {
5086                         ServerPlayingSound &psound = i->second;
5087                         psound.clients.erase(c.peer_id);
5088                         if(psound.clients.size() == 0)
5089                                 m_playing_sounds.erase(i++);
5090                         else
5091                                 i++;
5092                 }
5093
5094                 Player *player = m_env->getPlayer(c.peer_id);
5095
5096                 // Collect information about leaving in chat
5097                 std::wstring message;
5098                 {
5099                         if(player != NULL)
5100                         {
5101                                 std::wstring name = narrow_to_wide(player->getName());
5102                                 message += L"*** ";
5103                                 message += name;
5104                                 message += L" left the game.";
5105                                 if(c.timeout)
5106                                         message += L" (timed out)";
5107                         }
5108                 }
5109
5110                 /* Run scripts and remove from environment */
5111                 {
5112                         if(player != NULL)
5113                         {
5114                                 PlayerSAO *playersao = player->getPlayerSAO();
5115                                 assert(playersao);
5116
5117                                 scriptapi_on_leaveplayer(m_lua, playersao);
5118
5119                                 playersao->disconnected();
5120                         }
5121                 }
5122
5123                 /*
5124                         Print out action
5125                 */
5126                 {
5127                         if(player != NULL)
5128                         {
5129                                 std::ostringstream os(std::ios_base::binary);
5130                                 for(core::map<u16, RemoteClient*>::Iterator
5131                                         i = m_clients.getIterator();
5132                                         i.atEnd() == false; i++)
5133                                 {
5134                                         RemoteClient *client = i.getNode()->getValue();
5135                                         assert(client->peer_id == i.getNode()->getKey());
5136                                         if(client->serialization_version == SER_FMT_VER_INVALID)
5137                                                 continue;
5138                                         // Get player
5139                                         Player *player = m_env->getPlayer(client->peer_id);
5140                                         if(!player)
5141                                                 continue;
5142                                         // Get name of player
5143                                         os<<player->getName()<<" ";
5144                                 }
5145
5146                                 actionstream<<player->getName()<<" "
5147                                                 <<(c.timeout?"times out.":"leaves game.")
5148                                                 <<" List of players: "
5149                                                 <<os.str()<<std::endl;
5150                         }
5151                 }
5152
5153                 // Delete client
5154                 delete m_clients[c.peer_id];
5155                 m_clients.remove(c.peer_id);
5156
5157                 // Send player info to all remaining clients
5158                 //SendPlayerInfos();
5159
5160                 // Send leave chat message to all remaining clients
5161                 if(message.length() != 0)
5162                         BroadcastChatMessage(message);
5163
5164         } // PEER_REMOVED
5165         else
5166         {
5167                 assert(0);
5168         }
5169 }
5170
5171 void Server::handlePeerChanges()
5172 {
5173         while(m_peer_change_queue.size() > 0)
5174         {
5175                 PeerChange c = m_peer_change_queue.pop_front();
5176
5177                 verbosestream<<"Server: Handling peer change: "
5178                                 <<"id="<<c.peer_id<<", timeout="<<c.timeout
5179                                 <<std::endl;
5180
5181                 handlePeerChange(c);
5182         }
5183 }
5184
5185 void dedicated_server_loop(Server &server, bool &kill)
5186 {
5187         DSTACK(__FUNCTION_NAME);
5188
5189         verbosestream<<"dedicated_server_loop()"<<std::endl;
5190
5191         IntervalLimiter m_profiler_interval;
5192
5193         for(;;)
5194         {
5195                 float steplen = g_settings->getFloat("dedicated_server_step");
5196                 // This is kind of a hack but can be done like this
5197                 // because server.step() is very light
5198                 {
5199                         ScopeProfiler sp(g_profiler, "dedicated server sleep");
5200                         sleep_ms((int)(steplen*1000.0));
5201                 }
5202                 server.step(steplen);
5203
5204                 if(server.getShutdownRequested() || kill)
5205                 {
5206                         infostream<<"Dedicated server quitting"<<std::endl;
5207 #if USE_CURL
5208                         if(g_settings->getBool("server_announce") == true)
5209                                 ServerList::sendAnnounce("delete");
5210 #endif
5211                         break;
5212                 }
5213
5214                 /*
5215                         Profiler
5216                 */
5217                 float profiler_print_interval =
5218                                 g_settings->getFloat("profiler_print_interval");
5219                 if(profiler_print_interval != 0)
5220                 {
5221                         if(m_profiler_interval.step(steplen, profiler_print_interval))
5222                         {
5223                                 infostream<<"Profiler:"<<std::endl;
5224                                 g_profiler->print(infostream);
5225                                 g_profiler->clear();
5226                         }
5227                 }
5228         }
5229 }
5230
5231