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