bug-fixin'
[oweals/minetest.git] / src / server.cpp
index e210d4d64dd87e94485f5e7058fd1bc7ca7aee08..59d9acee4d033ba8f791b9ba5e3d3be73ee29961 100644 (file)
@@ -31,6 +31,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include "constants.h"
 #include "voxel.h"
 #include "materials.h"
+#include "mineral.h"
 
 #define BLOCK_EMERGE_FLAG_FROMDISK (1<<0)
 
@@ -40,6 +41,8 @@ void * ServerThread::Thread()
 
        DSTACK(__FUNCTION_NAME);
 
+       BEGIN_DEBUG_EXCEPTION_HANDLER
+
        while(getRun())
        {
                try{
@@ -51,19 +54,13 @@ void * ServerThread::Thread()
                catch(con::NoIncomingDataException &e)
                {
                }
-#if CATCH_UNHANDLED_EXCEPTIONS
-               /*
-                       This is what has to be done in threads to get suitable debug info
-               */
-               catch(std::exception &e)
+               catch(con::PeerNotFoundException &e)
                {
-                       dstream<<std::endl<<DTIME<<"An unhandled exception occurred: "
-                                       <<e.what()<<std::endl;
-                       assert(0);
+                       dout_server<<"Server: PeerNotFoundException"<<std::endl;
                }
-#endif
        }
        
+       END_DEBUG_EXCEPTION_HANDLER
 
        return NULL;
 }
@@ -75,11 +72,9 @@ void * EmergeThread::Thread()
        DSTACK(__FUNCTION_NAME);
 
        bool debug=false;
-#if CATCH_UNHANDLED_EXCEPTIONS
-       try
-       {
-#endif
        
+       BEGIN_DEBUG_EXCEPTION_HANDLER
+
        /*
                Get block info from queue, emerge them and send them
                to clients.
@@ -98,7 +93,7 @@ void * EmergeThread::Thread()
                
                //derr_server<<"EmergeThread::Thread(): running"<<std::endl;
 
-               //TimeTaker timer("block emerge", g_device);
+               //TimeTaker timer("block emerge");
                
                /*
                        Try to emerge it from somewhere.
@@ -145,27 +140,76 @@ void * EmergeThread::Thread()
                
                {//envlock
 
+               //TimeTaker envlockwaittimer("block emerge envlock wait time");
+               
+               // 0-50ms
                JMutexAutoLock envlock(m_server->m_env_mutex);
 
-               //TimeTaker timer("block emerge envlock", g_device);
+               //envlockwaittimer.stop();
+
+               //TimeTaker timer("block emerge (while env locked)");
                        
                try{
                        bool only_from_disk = false;
                        
                        if(optional)
                                only_from_disk = true;
-
-                       block = map.emergeBlock(
-                                       p,
-                                       only_from_disk,
-                                       changed_blocks,
-                                       lighting_invalidated_blocks);
                        
+                       // First check if the block already exists
+                       //block = map.getBlockNoCreate(p);
+
+                       if(block == NULL)
+                       {
+                               //dstream<<"Calling emergeBlock"<<std::endl;
+                               block = map.emergeBlock(
+                                               p,
+                                               only_from_disk,
+                                               changed_blocks,
+                                               lighting_invalidated_blocks);
+
+#if 0
+                               /*
+                                       EXPERIMENTAL: Create a few other blocks too
+                               */
+                               
+                               map.emergeBlock(
+                                               p + v3s16(0,1,0),
+                                               only_from_disk,
+                                               changed_blocks,
+                                               lighting_invalidated_blocks);
+
+                               map.emergeBlock(
+                                               p + v3s16(0,-1,0),
+                                               only_from_disk,
+                                               changed_blocks,
+                                               lighting_invalidated_blocks);
+#if 0
+                               map.emergeBlock(
+                                               p + v3s16(0,2,0),
+                                               only_from_disk,
+                                               changed_blocks,
+                                               lighting_invalidated_blocks);
+
+                               map.emergeBlock(
+                                               p + v3s16(0,-2,0),
+                                               only_from_disk,
+                                               changed_blocks,
+                                               lighting_invalidated_blocks);
+#endif
+#endif
+                       }
+
                        // If it is a dummy, block was not found on disk
                        if(block->isDummy())
                        {
                                //dstream<<"EmergeThread: Got a dummy block"<<std::endl;
                                got_block = false;
+
+                               if(only_from_disk == false)
+                               {
+                                       dstream<<"EmergeThread: wanted to generate a block but got a dummy"<<std::endl;
+                                       assert(0);
+                               }
                        }
                }
                catch(InvalidPositionException &e)
@@ -190,21 +234,6 @@ void * EmergeThread::Thread()
                                dout_server<<std::endl;
                        }
 
-                       /*
-                               Update water pressure
-                       */
-
-                       m_server->UpdateBlockWaterPressure(block, modified_blocks);
-
-                       for(core::map<v3s16, MapBlock*>::Iterator i = changed_blocks.getIterator();
-                                       i.atEnd() == false; i++)
-                       {
-                               MapBlock *block = i.getNode()->getValue();
-                               m_server->UpdateBlockWaterPressure(block, modified_blocks);
-                               //v3s16 p = i.getNode()->getKey();
-                               //m_server->UpdateBlockWaterPressure(p, modified_blocks);
-                       }
-
                        /*
                                Collect a list of blocks that have been modified in
                                addition to the fetched one.
@@ -219,8 +248,9 @@ void * EmergeThread::Thread()
                        }
                        
                        /*dstream<<"lighting "<<lighting_invalidated_blocks.size()
-                                       <<" blocks"<<std::endl;
-                       TimeTaker timer("** updateLighting", g_device);*/
+                                       <<" blocks"<<std::endl;*/
+                       
+                       //TimeTaker timer("** updateLighting");
                        
                        // Update lighting without locking the environment mutex,
                        // add modified blocks to changed blocks
@@ -267,18 +297,8 @@ void * EmergeThread::Thread()
                }
                
        }
-#if CATCH_UNHANDLED_EXCEPTIONS
-       }//try
-       /*
-               This is what has to be done in threads to get suitable debug info
-       */
-       catch(std::exception &e)
-       {
-               dstream<<std::endl<<DTIME<<"An unhandled exception occurred: "
-                               <<e.what()<<std::endl;
-               assert(0);
-       }
-#endif
+
+       END_DEBUG_EXCEPTION_HANDLER
 
        return NULL;
 }
@@ -308,12 +328,21 @@ void RemoteClient::GetNextBlocks(Server *server, float dtime,
 
        Player *player = server->m_env.getPlayer(peer_id);
 
+       assert(player != NULL);
+
        v3f playerpos = player->getPosition();
        v3f playerspeed = player->getSpeed();
 
        v3s16 center_nodepos = floatToInt(playerpos);
 
        v3s16 center = getNodeBlockPos(center_nodepos);
+       
+       // Camera position and direction
+       v3f camera_pos =
+                       playerpos + v3f(0, BS+BS/2, 0);
+       v3f camera_dir = v3f(0,0,1);
+       camera_dir.rotateYZBy(player->getPitch());
+       camera_dir.rotateXZBy(player->getYaw());
 
        /*
                Get the starting value of the block finder radius.
@@ -467,12 +496,62 @@ void RemoteClient::GetNextBlocks(Server *server, float dtime,
                        || p.Z < -MAP_GENERATION_LIMIT / MAP_BLOCKSIZE
                        || p.Z > MAP_GENERATION_LIMIT / MAP_BLOCKSIZE)
                                continue;
-
-                       bool generate = d <= d_max_gen;
                
-                       // Limit the generating area vertically to half
-                       if(abs(p.Y - center.Y) > d_max_gen / 2)
-                               generate = false;
+                       // If this is true, inexistent block will be made from scratch
+                       bool generate = d <= d_max_gen;
+                       
+                       {
+                               /*// Limit the generating area vertically to 2/3
+                               if(abs(p.Y - center.Y) > d_max_gen - d_max_gen / 3)
+                                       generate = false;*/
+
+                               // Limit the send area vertically to 2/3
+                               if(abs(p.Y - center.Y) > d_max_gen - d_max_gen / 3)
+                                       continue;
+                       }
+
+#if 0
+                       /*
+                               If block is far away, don't generate it unless it is
+                               near ground level
+
+                               NOTE: We can't know the ground level this way with the
+                               new generator.
+                       */
+                       if(d > 4)
+                       {
+                               v2s16 p2d(p.X, p.Z);
+                               MapSector *sector = NULL;
+                               try
+                               {
+                                       sector = server->m_env.getMap().getSectorNoGenerate(p2d);
+                               }
+                               catch(InvalidPositionException &e)
+                               {
+                               }
+
+                               if(sector != NULL)
+                               {
+                                       // Get center ground height in nodes
+                                       f32 gh = sector->getGroundHeight(
+                                                       v2s16(MAP_BLOCKSIZE/2, MAP_BLOCKSIZE/2));
+                                       // Block center y in nodes
+                                       f32 y = (f32)(p.Y * MAP_BLOCKSIZE + MAP_BLOCKSIZE/2);
+                                       // If differs a lot, don't generate
+                                       if(fabs(gh - y) > MAP_BLOCKSIZE*2)
+                                               generate = false;
+                               }
+                       }
+#endif
+
+                       /*
+                               Don't draw if not in sight
+                       */
+
+                       if(isBlockInSight(p, camera_pos, camera_dir, 10000*BS) == false)
+                       {
+                               continue;
+                       }
                        
                        /*
                                Don't send already sent blocks
@@ -487,16 +566,10 @@ void RemoteClient::GetNextBlocks(Server *server, float dtime,
                        /*
                                Check if map has this block
                        */
-                       MapBlock *block = NULL;
-                       try
-                       {
-                               block = server->m_env.getMap().getBlockNoCreate(p);
-                       }
-                       catch(InvalidPositionException &e)
-                       {
-                       }
+                       MapBlock *block = server->m_env.getMap().getBlockNoCreateNoEx(p);
                        
                        bool surely_not_found_on_disk = false;
+                       bool block_is_invalid = false;
                        if(block != NULL)
                        {
                                /*if(block->isIncomplete())
@@ -509,6 +582,22 @@ void RemoteClient::GetNextBlocks(Server *server, float dtime,
                                {
                                        surely_not_found_on_disk = true;
                                }
+
+                               if(block->isValid() == false)
+                               {
+                                       block_is_invalid = true;
+                               }
+                               
+                               v2s16 p2d(p.X, p.Z);
+                               ServerMap *map = (ServerMap*)(&server->m_env.getMap());
+                               v2s16 chunkpos = map->sector_to_chunk(p2d);
+                               if(map->chunkNonVolatile(chunkpos) == false)
+                                       block_is_invalid = true;
+                               /*MapChunk *chunk = map->getChunk(chunkpos);
+                               if(chunk == NULL)
+                                       block_is_invalid = true;
+                               else if(chunk->getIsVolatile() == true)
+                                       block_is_invalid = true;*/
                        }
 
                        /*
@@ -533,8 +622,10 @@ void RemoteClient::GetNextBlocks(Server *server, float dtime,
                        /*
                                Add inexistent block to emerge queue.
                        */
-                       if(block == NULL || surely_not_found_on_disk)
+                       if(block == NULL || surely_not_found_on_disk || block_is_invalid)
                        {
+                               //dstream<<"asd"<<std::endl;
+                               
                                /*SharedPtr<JMutexAutoLock> lock
                                                (m_num_blocks_in_emerge_queue.getLock());*/
                                
@@ -542,6 +633,8 @@ void RemoteClient::GetNextBlocks(Server *server, float dtime,
                                // Allow only one block in emerge queue
                                if(server->m_emerge_queue.peerItemCount(peer_id) < 1)
                                {
+                                       //dstream<<"Adding block to emerge queue"<<std::endl;
+                                       
                                        // Add it to the emerge queue and trigger the thread
                                        
                                        u8 flags = 0;
@@ -618,8 +711,9 @@ void RemoteClient::SendObjectData(
        /*
                Get and write player data
        */
-
-       core::list<Player*> players = server->m_env.getPlayers();
+       
+       // Get connected players
+       core::list<Player*> players = server->m_env.getPlayers(true);
 
        // Write player count
        u16 playercount = players.size();
@@ -670,6 +764,8 @@ void RemoteClient::SendObjectData(
 
        Player *player = server->m_env.getPlayer(peer_id);
 
+       assert(player);
+
        v3f playerpos = player->getPosition();
        v3f playerspeed = player->getSpeed();
 
@@ -796,20 +892,23 @@ void RemoteClient::GotBlock(v3s16 p)
        if(m_blocks_sending.find(p) != NULL)
                m_blocks_sending.remove(p);
        else
-               dstream<<"RemoteClient::GotBlock(): Didn't find in"
-                               " m_blocks_sending"<<std::endl;
+       {
+               /*dstream<<"RemoteClient::GotBlock(): Didn't find in"
+                               " m_blocks_sending"<<std::endl;*/
+               m_excess_gotblocks++;
+       }
        m_blocks_sent.insert(p, true);
 }
 
 void RemoteClient::SentBlock(v3s16 p)
 {
        JMutexAutoLock lock(m_blocks_sending_mutex);
-       if(m_blocks_sending.size() > 15)
+       /*if(m_blocks_sending.size() > 15)
        {
                dstream<<"RemoteClient::SentBlock(): "
                                <<"m_blocks_sending.size()="
                                <<m_blocks_sending.size()<<std::endl;
-       }
+       }*/
        if(m_blocks_sending.find(p) == NULL)
                m_blocks_sending.insert(p, 0.0);
        else
@@ -861,9 +960,10 @@ PlayerInfo::PlayerInfo()
 
 void PlayerInfo::PrintLine(std::ostream *s)
 {
-       (*s)<<id<<": \""<<name<<"\" ("
-                       <<position.X<<","<<position.Y
-                       <<","<<position.Z<<") ";
+       (*s)<<id<<": ";
+       (*s)<<"\""<<name<<"\" ("
+                       <<(position.X/10)<<","<<(position.Y/10)
+                       <<","<<(position.Z/10)<<") ";
        address.print(s);
        (*s)<<" avg_rtt="<<avg_rtt;
        (*s)<<std::endl;
@@ -888,20 +988,20 @@ u32 PIChecksum(core::list<PlayerInfo> &l)
 */
 
 Server::Server(
-               std::string mapsavedir,
-               HMParams hm_params,
-               MapParams map_params
+               std::string mapsavedir
        ):
-       m_env(new ServerMap(mapsavedir, hm_params, map_params), dout_server),
+       m_env(new ServerMap(mapsavedir), dout_server),
        m_con(PROTOCOL_ID, 512, CONNECTION_TIMEOUT, this),
        m_thread(this),
        m_emergethread(this),
-       m_time_of_day(8000),
+       m_time_of_day(9000),
        m_time_counter(0),
        m_time_of_day_send_timer(0),
-       m_uptime(0)
+       m_uptime(0),
+       m_mapsavedir(mapsavedir)
 {
-       m_flowwater_timer = 0.0;
+       //m_flowwater_timer = 0.0;
+       m_liquid_transform_timer = 0.0;
        m_print_info_timer = 0.0;
        m_objectdata_timer = 0.0;
        m_emergethread_trigger_timer = 0.0;
@@ -911,10 +1011,16 @@ Server::Server(
        m_con_mutex.Init();
        m_step_dtime_mutex.Init();
        m_step_dtime = 0.0;
+
+       // Load players
+       m_env.deSerializePlayers(m_mapsavedir);
 }
 
 Server::~Server()
 {
+       // Save players
+       m_env.serializePlayers(m_mapsavedir);
+       
        // Stop threads
        stop();
 
@@ -924,13 +1030,13 @@ Server::~Server()
                i = m_clients.getIterator();
                i.atEnd() == false; i++)
        {
-               u16 peer_id = i.getNode()->getKey();
-
-               // Delete player
+               /*// Delete player
+               // NOTE: These are removed by env destructor
                {
+                       u16 peer_id = i.getNode()->getKey();
                        JMutexAutoLock envlock(m_env_mutex);
                        m_env.removePlayer(peer_id);
-               }
+               }*/
                
                // Delete client
                delete i.getNode()->getValue();
@@ -1070,58 +1176,36 @@ void Server::AsyncRunStep()
        /*
                Do background stuff
        */
-
+       
        /*
-               Flow water
+               Transform liquids
        */
+       m_liquid_transform_timer += dtime;
+       if(m_liquid_transform_timer >= 1.00)
        {
-               float interval;
+               m_liquid_transform_timer -= 1.00;
                
-               if(g_settings.getBool("endless_water") == false)
-                       interval = 1.0;
-               else
-                       interval = 0.25;
-
-               float &counter = m_flowwater_timer;
-               counter += dtime;
-               if(counter >= 0.25 && m_flow_active_nodes.size() > 0)
-               {
+               JMutexAutoLock lock(m_env_mutex);
                
-               counter = 0.0;
-
                core::map<v3s16, MapBlock*> modified_blocks;
-
+               m_env.getMap().transformLiquids(modified_blocks);
+#if 0          
+               /*
+                       Update lighting
+               */
+               core::map<v3s16, MapBlock*> lighting_modified_blocks;
+               ServerMap &map = ((ServerMap&)m_env.getMap());
+               map.updateLighting(modified_blocks, lighting_modified_blocks);
+               
+               // Add blocks modified by lighting to modified_blocks
+               for(core::map<v3s16, MapBlock*>::Iterator
+                               i = lighting_modified_blocks.getIterator();
+                               i.atEnd() == false; i++)
                {
-
-                       JMutexAutoLock envlock(m_env_mutex);
-                       
-                       MapVoxelManipulator v(&m_env.getMap());
-                       v.m_disable_water_climb =
-                                       g_settings.getBool("disable_water_climb");
-                       
-                       if(g_settings.getBool("endless_water") == false)
-                               v.flowWater(m_flow_active_nodes, 0, false, 250);
-                       else
-                               v.flowWater(m_flow_active_nodes, 0, false, 50);
-
-                       v.blitBack(modified_blocks);
-
-                       ServerMap &map = ((ServerMap&)m_env.getMap());
-                       
-                       // Update lighting
-                       core::map<v3s16, MapBlock*> lighting_modified_blocks;
-                       map.updateLighting(modified_blocks, lighting_modified_blocks);
-                       
-                       // Add blocks modified by lighting to modified_blocks
-                       for(core::map<v3s16, MapBlock*>::Iterator
-                                       i = lighting_modified_blocks.getIterator();
-                                       i.atEnd() == false; i++)
-                       {
-                               MapBlock *block = i.getNode()->getValue();
-                               modified_blocks.insert(block->getPos(), block);
-                       }
-               } // envlock
-
+                       MapBlock *block = i.getNode()->getValue();
+                       modified_blocks.insert(block->getPos(), block);
+               }
+#endif
                /*
                        Set the modified blocks unsent for all the clients
                */
@@ -1140,10 +1224,8 @@ void Server::AsyncRunStep()
                                client->SetBlocksNotSent(modified_blocks);
                        }
                }
-
-               } // interval counter
        }
-       
+
        // Periodically print some info
        {
                float &counter = m_print_info_timer;
@@ -1288,8 +1370,10 @@ void Server::AsyncRunStep()
                }
        }
        
-       // Trigger emergethread (it gets somehow gets to a
-       // non-triggered but bysy state sometimes)
+       /*
+               Trigger emergethread (it somehow gets to a non-triggered but
+               bysy state sometimes)
+       */
        {
                float &counter = m_emergethread_trigger_timer;
                counter += dtime;
@@ -1322,6 +1406,9 @@ void Server::AsyncRunStep()
                                dout_server<<"Server: Unloaded "<<deleted_count
                                                <<" sectors from memory"<<std::endl;
                        }
+
+                       // Save players
+                       m_env.serializePlayers(m_mapsavedir);
                }
        }
 }
@@ -1433,20 +1520,63 @@ void Server::ProcessData(u8 *data, u32 datasize, u16 peer_id)
                /*
                        Set up player
                */
+               
+               // Get player name
+               const u32 playername_size = 20;
+               char playername[playername_size];
+               for(u32 i=0; i<playername_size-1; i++)
+               {
+                       playername[i] = data[3+i];
+               }
+               playername[playername_size-1] = 0;
+               
+               // Get player
+               Player *player = emergePlayer(playername, "", peer_id);
+               //Player *player = m_env.getPlayer(peer_id);
+
+               /*{
+                       // DEBUG: Test serialization
+                       std::ostringstream test_os;
+                       player->serialize(test_os);
+                       dstream<<"Player serialization test: \""<<test_os.str()
+                                       <<"\""<<std::endl;
+                       std::istringstream test_is(test_os.str());
+                       player->deSerialize(test_is);
+               }*/
+
+               // If failed, cancel
+               if(player == NULL)
+               {
+                       derr_server<<DTIME<<"Server: peer_id="<<peer_id
+                                       <<": failed to emerge player"<<std::endl;
+                       return;
+               }
 
-               Player *player = m_env.getPlayer(peer_id);
+               /*
+               // If a client is already connected to the player, cancel
+               if(player->peer_id != 0)
+               {
+                       derr_server<<DTIME<<"Server: peer_id="<<peer_id
+                                       <<" tried to connect to "
+                                       "an already connected player (peer_id="
+                                       <<player->peer_id<<")"<<std::endl;
+                       return;
+               }
+               // Set client of player
+               player->peer_id = peer_id;
+               */
 
                // Check if player doesn't exist
                if(player == NULL)
                        throw con::InvalidIncomingDataException
                                ("Server::ProcessData(): INIT: Player doesn't exist");
 
-               // update name if it was supplied
+               /*// update name if it was supplied
                if(datasize >= 20+3)
                {
                        data[20+3-1] = 0;
                        player->updateName((const char*)&data[3]);
-               }
+               }*/
 
                // Now answer with a TOCLIENT_INIT
                
@@ -1502,9 +1632,10 @@ void Server::ProcessData(u8 *data, u32 datasize, u16 peer_id)
                                assert(client->peer_id == i.getNode()->getKey());
                                if(client->serialization_version == SER_FMT_VER_INVALID)
                                        continue;
+                               // Get player
+                               Player *player = m_env.getPlayer(client->peer_id);
                                // Get name of player
                                std::wstring name = L"unknown";
-                               Player *player = m_env.getPlayer(client->peer_id);
                                if(player != NULL)
                                        name = narrow_to_wide(player->getName());
                                // Add name to information string
@@ -1764,11 +1895,13 @@ void Server::ProcessData(u8 *data, u32 datasize, u16 peer_id)
                        core::map<v3s16, MapBlock*> modified_blocks;
 
                        u8 material;
+                       u8 mineral = MINERAL_NONE;
 
                        try
                        {
+                               MapNode n = m_env.getMap().getNode(p_under);
                                // Get material at position
-                               material = m_env.getMap().getNode(p_under).d;
+                               material = n.d;
                                // If it's not diggable, do nothing
                                if(content_diggable(material) == false)
                                {
@@ -1776,16 +1909,19 @@ void Server::ProcessData(u8 *data, u32 datasize, u16 peer_id)
                                                        <<std::endl;
                                        return;
                                }
+                               // Get mineral
+                               mineral = n.getMineral();
                        }
                        catch(InvalidPositionException &e)
                        {
-                               derr_server<<"Server: Not finishing digging: Node not found"
+                               derr_server<<"Server: Not finishing digging: Node not found."
+                                               <<" Adding block to emerge queue."
                                                <<std::endl;
+                               m_emerge_queue.addBlock(peer_id,
+                                               getNodeBlockPos(p_over), BLOCK_EMERGE_FLAG_FROMDISK);
                                return;
                        }
                        
-                       //TODO: Send to only other clients
-                       
                        /*
                                Send the removal to all other clients
                        */
@@ -1857,7 +1993,15 @@ void Server::ProcessData(u8 *data, u32 datasize, u16 peer_id)
                                /*
                                        Add digged item to inventory
                                */
-                               InventoryItem *item = new MaterialItem(material, 1);
+
+                               InventoryItem *item = NULL;
+
+                               if(mineral != MINERAL_NONE)
+                                       item = getDiggedMineralItem(mineral);
+
+                               if(item == NULL)
+                                       item = new MaterialItem(material, 1);
+
                                player->inventory.addItem("main", item);
 
                                /*
@@ -1871,7 +2015,8 @@ void Server::ProcessData(u8 *data, u32 datasize, u16 peer_id)
                                (this takes some time so it is done after the quick stuff)
                        */
                        m_env.getMap().removeNodeAndUpdate(p_under, modified_blocks);
-                       
+
+#if 0
                        /*
                                Update water
                        */
@@ -1895,6 +2040,7 @@ void Server::ProcessData(u8 *data, u32 datasize, u16 peer_id)
                        }
                        
                        v.blitBack(modified_blocks);
+#endif
                }
                
                /*
@@ -1923,12 +2069,26 @@ void Server::ProcessData(u8 *data, u32 datasize, u16 peer_id)
                                        // Don't add a node if this is not a free space
                                        MapNode n2 = m_env.getMap().getNode(p_over);
                                        if(content_buildable_to(n2.d) == false)
+                                       {
+                                               // Client probably has wrong data.
+                                               // Set block not sent, so that client will get
+                                               // a valid one.
+                                               dstream<<"Client "<<peer_id<<" tried to place"
+                                                               <<" node in invalid position; setting"
+                                                               <<" MapBlock not sent."<<std::endl;
+                                               RemoteClient *client = getClient(peer_id);
+                                               v3s16 blockpos = getNodeBlockPos(p_over);
+                                               client->SetBlockNotSent(blockpos);
                                                return;
+                                       }
                                }
                                catch(InvalidPositionException &e)
                                {
                                        derr_server<<"Server: Ignoring ADDNODE: Node not found"
+                                                       <<" Adding block to emerge queue."
                                                        <<std::endl;
+                                       m_emerge_queue.addBlock(peer_id,
+                                                       getNodeBlockPos(p_over), BLOCK_EMERGE_FLAG_FROMDISK);
                                        return;
                                }
 
@@ -1939,7 +2099,7 @@ void Server::ProcessData(u8 *data, u32 datasize, u16 peer_id)
                                MaterialItem *mitem = (MaterialItem*)item;
                                MapNode n;
                                n.d = mitem->getMaterial();
-                               if(content_directional(n.d))
+                               if(content_features(n.d).wall_mounted)
                                        n.dir = packDir(p_under - p_over);
 
 #if 1
@@ -2020,7 +2180,8 @@ void Server::ProcessData(u8 *data, u32 datasize, u16 peer_id)
                                        }
                                }
 #endif
-                               
+
+#if 0
                                /*
                                        Update water
                                */
@@ -2044,6 +2205,7 @@ void Server::ProcessData(u8 *data, u32 datasize, u16 peer_id)
                                }
                                
                                v.blitBack(modified_blocks);
+#endif
                        }
                        /*
                                Handle other items
@@ -2215,13 +2377,13 @@ void Server::ProcessData(u8 *data, u32 datasize, u16 peer_id)
        }
        else if(command == TOSERVER_INVENTORY_ACTION)
        {
-               // Ignore inventory changes if in creative mode
+               /*// Ignore inventory changes if in creative mode
                if(g_settings.getBool("creative_mode") == true)
                {
                        dstream<<"TOSERVER_INVENTORY_ACTION: ignoring in creative mode"
                                        <<std::endl;
                        return;
-               }
+               }*/
                // Strip command and create a stream
                std::string datastring((char*)&data[2], datasize-2);
                dstream<<"TOSERVER_INVENTORY_ACTION: data="<<datastring<<std::endl;
@@ -2231,10 +2393,11 @@ void Server::ProcessData(u8 *data, u32 datasize, u16 peer_id)
                if(a != NULL)
                {
                        /*
-                               Handle craftresult specially
+                               Handle craftresult specially if not in creative mode
                        */
                        bool disable_action = false;
-                       if(a->getType() == IACTION_MOVE)
+                       if(a->getType() == IACTION_MOVE
+                                       && g_settings.getBool("creative_mode") == false)
                        {
                                IMoveAction *ma = (IMoveAction*)a;
                                // Don't allow moving anything to craftresult
@@ -2311,13 +2474,13 @@ void Server::ProcessData(u8 *data, u32 datasize, u16 peer_id)
                        message += (wchar_t)readU16(buf);
                }
 
-               dstream<<"CHAT: "<<wide_to_narrow(message)<<std::endl;
-
                // Get player name of this client
                std::wstring name = narrow_to_wide(player->getName());
 
                std::wstring line = std::wstring(L"<")+name+L"> "+message;
                
+               dstream<<"CHAT: "<<wide_to_narrow(line)<<std::endl;
+
                /*
                        Send the message to all other clients
                */
@@ -2407,15 +2570,17 @@ core::list<PlayerInfo> Server::getPlayerInfo()
                PlayerInfo info;
 
                Player *player = *i;
+
                try{
                        con::Peer *peer = m_con.GetPeer(player->peer_id);
+                       // Copy info from peer to info struct
                        info.id = peer->id;
                        info.address = peer->address;
                        info.avg_rtt = peer->avg_rtt;
                }
                catch(con::PeerNotFoundException &e)
                {
-                       // Outdated peer info
+                       // Set dummy peer info
                        info.id = 0;
                        info.address = Address(0,0,0,0,0);
                        info.avg_rtt = 0.0;
@@ -2483,7 +2648,8 @@ void Server::SendPlayerInfos()
 
        //JMutexAutoLock envlock(m_env_mutex);
        
-       core::list<Player*> players = m_env.getPlayers();
+       // Get connected players
+       core::list<Player*> players = m_env.getPlayers(true);
        
        u32 player_count = players.getSize();
        u32 datasize = 2+(2+PLAYERNAME_SIZE)*player_count;
@@ -2502,6 +2668,7 @@ void Server::SendPlayerInfos()
                                "peer_id="<<player->peer_id<<std::endl;*/
                
                writeU16(&data[start], player->peer_id);
+               memset((char*)&data[start+2], 0, PLAYERNAME_SIZE);
                snprintf((char*)&data[start+2], PLAYERNAME_SIZE, "%s", player->getName());
                start += 2+PLAYERNAME_SIZE;
        }
@@ -2674,127 +2841,128 @@ void Server::SendInventory(u16 peer_id)
        /*
                Calculate crafting stuff
        */
-
-       InventoryList *clist = player->inventory.getList("craft");
-       InventoryList *rlist = player->inventory.getList("craftresult");
-       if(rlist)
-       {
-               rlist->clearItems();
-       }
-       if(clist && rlist)
+       if(g_settings.getBool("creative_mode") == false)
        {
-               InventoryItem *items[9];
-               for(u16 i=0; i<9; i++)
+               InventoryList *clist = player->inventory.getList("craft");
+               InventoryList *rlist = player->inventory.getList("craftresult");
+               if(rlist)
                {
-                       items[i] = clist->getItem(i);
+                       rlist->clearItems();
                }
-               
-               bool found = false;
-
-               // Wood
-               if(!found)
+               if(clist && rlist)
                {
-                       ItemSpec specs[9];
-                       specs[0] = ItemSpec(ITEM_MATERIAL, CONTENT_TREE);
-                       if(checkItemCombination(items, specs))
+                       InventoryItem *items[9];
+                       for(u16 i=0; i<9; i++)
                        {
-                               rlist->addItem(new MaterialItem(CONTENT_WOOD, 4));
-                               found = true;
+                               items[i] = clist->getItem(i);
                        }
-               }
+                       
+                       bool found = false;
 
-               // Stick
-               if(!found)
-               {
-                       ItemSpec specs[9];
-                       specs[0] = ItemSpec(ITEM_MATERIAL, CONTENT_WOOD);
-                       if(checkItemCombination(items, specs))
+                       // Wood
+                       if(!found)
                        {
-                               rlist->addItem(new CraftItem("Stick", 4));
-                               found = true;
+                               ItemSpec specs[9];
+                               specs[0] = ItemSpec(ITEM_MATERIAL, CONTENT_TREE);
+                               if(checkItemCombination(items, specs))
+                               {
+                                       rlist->addItem(new MaterialItem(CONTENT_WOOD, 4));
+                                       found = true;
+                               }
                        }
-               }
 
-               // Sign
-               if(!found)
-               {
-                       ItemSpec specs[9];
-                       specs[0] = ItemSpec(ITEM_MATERIAL, CONTENT_WOOD);
-                       specs[1] = ItemSpec(ITEM_MATERIAL, CONTENT_WOOD);
-                       specs[2] = ItemSpec(ITEM_MATERIAL, CONTENT_WOOD);
-                       specs[3] = ItemSpec(ITEM_MATERIAL, CONTENT_WOOD);
-                       specs[4] = ItemSpec(ITEM_MATERIAL, CONTENT_WOOD);
-                       specs[5] = ItemSpec(ITEM_MATERIAL, CONTENT_WOOD);
-                       specs[7] = ItemSpec(ITEM_CRAFT, "Stick");
-                       if(checkItemCombination(items, specs))
+                       // Stick
+                       if(!found)
+                       {
+                               ItemSpec specs[9];
+                               specs[0] = ItemSpec(ITEM_MATERIAL, CONTENT_WOOD);
+                               if(checkItemCombination(items, specs))
+                               {
+                                       rlist->addItem(new CraftItem("Stick", 4));
+                                       found = true;
+                               }
+                       }
+
+                       // Sign
+                       if(!found)
                        {
-                               rlist->addItem(new MapBlockObjectItem("Sign"));
-                               found = true;
+                               ItemSpec specs[9];
+                               specs[0] = ItemSpec(ITEM_MATERIAL, CONTENT_WOOD);
+                               specs[1] = ItemSpec(ITEM_MATERIAL, CONTENT_WOOD);
+                               specs[2] = ItemSpec(ITEM_MATERIAL, CONTENT_WOOD);
+                               specs[3] = ItemSpec(ITEM_MATERIAL, CONTENT_WOOD);
+                               specs[4] = ItemSpec(ITEM_MATERIAL, CONTENT_WOOD);
+                               specs[5] = ItemSpec(ITEM_MATERIAL, CONTENT_WOOD);
+                               specs[7] = ItemSpec(ITEM_CRAFT, "Stick");
+                               if(checkItemCombination(items, specs))
+                               {
+                                       rlist->addItem(new MapBlockObjectItem("Sign"));
+                                       found = true;
+                               }
                        }
-               }
 
-               // Torch
-               if(!found)
-               {
-                       ItemSpec specs[9];
-                       specs[0] = ItemSpec(ITEM_MATERIAL, CONTENT_COALSTONE);
-                       specs[3] = ItemSpec(ITEM_CRAFT, "Stick");
-                       if(checkItemCombination(items, specs))
+                       // Torch
+                       if(!found)
                        {
-                               rlist->addItem(new MaterialItem(CONTENT_TORCH, 4));
-                               found = true;
+                               ItemSpec specs[9];
+                               specs[0] = ItemSpec(ITEM_CRAFT, "lump_of_coal");
+                               specs[3] = ItemSpec(ITEM_CRAFT, "Stick");
+                               if(checkItemCombination(items, specs))
+                               {
+                                       rlist->addItem(new MaterialItem(CONTENT_TORCH, 4));
+                                       found = true;
+                               }
                        }
-               }
 
-               // Wooden pick
-               if(!found)
-               {
-                       ItemSpec specs[9];
-                       specs[0] = ItemSpec(ITEM_MATERIAL, CONTENT_WOOD);
-                       specs[1] = ItemSpec(ITEM_MATERIAL, CONTENT_WOOD);
-                       specs[2] = ItemSpec(ITEM_MATERIAL, CONTENT_WOOD);
-                       specs[4] = ItemSpec(ITEM_CRAFT, "Stick");
-                       specs[7] = ItemSpec(ITEM_CRAFT, "Stick");
-                       if(checkItemCombination(items, specs))
+                       // Wooden pick
+                       if(!found)
                        {
-                               rlist->addItem(new ToolItem("WPick", 0));
-                               found = true;
+                               ItemSpec specs[9];
+                               specs[0] = ItemSpec(ITEM_MATERIAL, CONTENT_WOOD);
+                               specs[1] = ItemSpec(ITEM_MATERIAL, CONTENT_WOOD);
+                               specs[2] = ItemSpec(ITEM_MATERIAL, CONTENT_WOOD);
+                               specs[4] = ItemSpec(ITEM_CRAFT, "Stick");
+                               specs[7] = ItemSpec(ITEM_CRAFT, "Stick");
+                               if(checkItemCombination(items, specs))
+                               {
+                                       rlist->addItem(new ToolItem("WPick", 0));
+                                       found = true;
+                               }
                        }
-               }
 
-               // Stone pick
-               if(!found)
-               {
-                       ItemSpec specs[9];
-                       specs[0] = ItemSpec(ITEM_MATERIAL, CONTENT_STONE);
-                       specs[1] = ItemSpec(ITEM_MATERIAL, CONTENT_STONE);
-                       specs[2] = ItemSpec(ITEM_MATERIAL, CONTENT_STONE);
-                       specs[4] = ItemSpec(ITEM_CRAFT, "Stick");
-                       specs[7] = ItemSpec(ITEM_CRAFT, "Stick");
-                       if(checkItemCombination(items, specs))
+                       // Stone pick
+                       if(!found)
                        {
-                               rlist->addItem(new ToolItem("STPick", 0));
-                               found = true;
+                               ItemSpec specs[9];
+                               specs[0] = ItemSpec(ITEM_MATERIAL, CONTENT_STONE);
+                               specs[1] = ItemSpec(ITEM_MATERIAL, CONTENT_STONE);
+                               specs[2] = ItemSpec(ITEM_MATERIAL, CONTENT_STONE);
+                               specs[4] = ItemSpec(ITEM_CRAFT, "Stick");
+                               specs[7] = ItemSpec(ITEM_CRAFT, "Stick");
+                               if(checkItemCombination(items, specs))
+                               {
+                                       rlist->addItem(new ToolItem("STPick", 0));
+                                       found = true;
+                               }
                        }
-               }
 
-               // Mese pick
-               if(!found)
-               {
-                       ItemSpec specs[9];
-                       specs[0] = ItemSpec(ITEM_MATERIAL, CONTENT_MESE);
-                       specs[1] = ItemSpec(ITEM_MATERIAL, CONTENT_MESE);
-                       specs[2] = ItemSpec(ITEM_MATERIAL, CONTENT_MESE);
-                       specs[4] = ItemSpec(ITEM_CRAFT, "Stick");
-                       specs[7] = ItemSpec(ITEM_CRAFT, "Stick");
-                       if(checkItemCombination(items, specs))
+                       // Mese pick
+                       if(!found)
                        {
-                               rlist->addItem(new ToolItem("MesePick", 0));
-                               found = true;
+                               ItemSpec specs[9];
+                               specs[0] = ItemSpec(ITEM_MATERIAL, CONTENT_MESE);
+                               specs[1] = ItemSpec(ITEM_MATERIAL, CONTENT_MESE);
+                               specs[2] = ItemSpec(ITEM_MATERIAL, CONTENT_MESE);
+                               specs[4] = ItemSpec(ITEM_CRAFT, "Stick");
+                               specs[7] = ItemSpec(ITEM_CRAFT, "Stick");
+                               if(checkItemCombination(items, specs))
+                               {
+                                       rlist->addItem(new ToolItem("MesePick", 0));
+                                       found = true;
+                               }
                        }
                }
-
-       }
+       } // if creative_mode == false
 
        /*
                Serialize it
@@ -2934,6 +3102,258 @@ RemoteClient* Server::getClient(u16 peer_id)
        return n->getValue();
 }
 
+void setCreativeInventory(Player *player)
+{
+       player->resetInventory();
+       
+       // Give some good picks
+       {
+               InventoryItem *item = new ToolItem("STPick", 0);
+               void* r = player->inventory.addItem("main", item);
+               assert(r == NULL);
+       }
+       {
+               InventoryItem *item = new ToolItem("MesePick", 0);
+               void* r = player->inventory.addItem("main", item);
+               assert(r == NULL);
+       }
+
+       /*
+               Give materials
+       */
+       assert(USEFUL_CONTENT_COUNT <= PLAYER_INVENTORY_SIZE);
+       
+       // add torch first
+       InventoryItem *item = new MaterialItem(CONTENT_TORCH, 1);
+       player->inventory.addItem("main", item);
+       
+       // Then others
+       for(u16 i=0; i<USEFUL_CONTENT_COUNT; i++)
+       {
+               // Skip some materials
+               if(i == CONTENT_WATER || i == CONTENT_TORCH
+                       || i == CONTENT_COALSTONE)
+                       continue;
+
+               InventoryItem *item = new MaterialItem(i, 1);
+               player->inventory.addItem("main", item);
+       }
+       // Sign
+       {
+               InventoryItem *item = new MapBlockObjectItem("Sign Example text");
+               void* r = player->inventory.addItem("main", item);
+               assert(r == NULL);
+       }
+}
+
+Player *Server::emergePlayer(const char *name, const char *password,
+               u16 peer_id)
+{
+       /*
+               Try to get an existing player
+       */
+       Player *player = m_env.getPlayer(name);
+       if(player != NULL)
+       {
+               // If player is already connected, cancel
+               if(player->peer_id != 0)
+               {
+                       dstream<<"emergePlayer(): Player already connected"<<std::endl;
+                       return NULL;
+               }
+
+               // Got one.
+               player->peer_id = peer_id;
+               
+               // Reset inventory to creative if in creative mode
+               if(g_settings.getBool("creative_mode"))
+               {
+                       setCreativeInventory(player);
+               }
+
+               return player;
+       }
+
+       /*
+               If player with the wanted peer_id already exists, cancel.
+       */
+       if(m_env.getPlayer(peer_id) != NULL)
+       {
+               dstream<<"emergePlayer(): Player with wrong name but same"
+                               " peer_id already exists"<<std::endl;
+               return NULL;
+       }
+       
+       /*
+               Create a new player
+       */
+       {
+               player = new ServerRemotePlayer();
+               //player->peer_id = c.peer_id;
+               //player->peer_id = PEER_ID_INEXISTENT;
+               player->peer_id = peer_id;
+               player->updateName(name);
+
+               /*
+                       Set player position
+               */
+               
+               dstream<<"Server: Finding spawn place for player \""
+                               <<player->getName()<<"\""<<std::endl;
+
+               v2s16 nodepos;
+#if 1
+               player->setPosition(intToFloat(v3s16(
+                               0,
+                               45, //64,
+                               0
+               )));
+#endif
+#if 0
+               f32 groundheight = 0;
+#if 0
+               // Try to find a good place a few times
+               for(s32 i=0; i<500; i++)
+               {
+                       s32 range = 1 + i;
+                       // We're going to try to throw the player to this position
+                       nodepos = v2s16(-range + (myrand()%(range*2)),
+                                       -range + (myrand()%(range*2)));
+                       v2s16 sectorpos = getNodeSectorPos(nodepos);
+                       // Get sector
+                       m_env.getMap().emergeSector(sectorpos);
+                       // Get ground height at point
+                       groundheight = m_env.getMap().getGroundHeight(nodepos, true);
+                       // The sector should have been generated -> groundheight exists
+                       assert(groundheight > GROUNDHEIGHT_VALID_MINVALUE);
+                       // Don't go underwater
+                       if(groundheight < WATER_LEVEL)
+                       {
+                               //dstream<<"-> Underwater"<<std::endl;
+                               continue;
+                       }
+#if 0 // Doesn't work, generating blocks is a bit too complicated for doing here
+                       // Get block at point
+                       v3s16 nodepos3d;
+                       nodepos3d = v3s16(nodepos.X, groundheight+1, nodepos.Y);
+                       v3s16 blockpos = getNodeBlockPos(nodepos3d);
+                       ((ServerMap*)(&m_env.getMap()))->emergeBlock(blockpos);
+                       // Don't go inside ground
+                       try{
+                               /*v3s16 footpos(nodepos.X, groundheight+1, nodepos.Y);
+                               v3s16 headpos(nodepos.X, groundheight+2, nodepos.Y);*/
+                               v3s16 footpos = nodepos3d + v3s16(0,0,0);
+                               v3s16 headpos = nodepos3d + v3s16(0,1,0);
+                               if(m_env.getMap().getNode(footpos).d != CONTENT_AIR
+                                       || m_env.getMap().getNode(headpos).d != CONTENT_AIR)
+                               {
+                                       dstream<<"-> Inside ground"<<std::endl;
+                                       // In ground
+                                       continue;
+                               }
+                       }catch(InvalidPositionException &e)
+                       {
+                               dstream<<"-> Invalid position"<<std::endl;
+                               // Ignore invalid position
+                               continue;
+                       }
+#endif
+                       // Found a good place
+                       dstream<<"Searched through "<<i<<" places."<<std::endl;
+                       break;
+               }
+#endif
+               
+               // If no suitable place was not found, go above water at least.
+               if(groundheight < WATER_LEVEL)
+                       groundheight = WATER_LEVEL;
+
+               player->setPosition(intToFloat(v3s16(
+                               nodepos.X,
+                               //groundheight + 1,
+                               groundheight + 15,
+                               nodepos.Y
+               )));
+#endif
+
+               /*
+                       Add player to environment
+               */
+
+               m_env.addPlayer(player);
+
+               /*
+                       Add stuff to inventory
+               */
+               
+               if(g_settings.getBool("creative_mode"))
+               {
+                       setCreativeInventory(player);
+               }
+               else
+               {
+                       /*{
+                               InventoryItem *item = new ToolItem("WPick", 32000);
+                               void* r = player->inventory.addItem("main", item);
+                               assert(r == NULL);
+                       }*/
+                       /*{
+                               InventoryItem *item = new MaterialItem(CONTENT_MESE, 6);
+                               void* r = player->inventory.addItem("main", item);
+                               assert(r == NULL);
+                       }
+                       {
+                               InventoryItem *item = new MaterialItem(CONTENT_COALSTONE, 6);
+                               void* r = player->inventory.addItem("main", item);
+                               assert(r == NULL);
+                       }
+                       {
+                               InventoryItem *item = new MaterialItem(CONTENT_WOOD, 6);
+                               void* r = player->inventory.addItem("main", item);
+                               assert(r == NULL);
+                       }
+                       {
+                               InventoryItem *item = new CraftItem("Stick", 4);
+                               void* r = player->inventory.addItem("main", item);
+                               assert(r == NULL);
+                       }
+                       {
+                               InventoryItem *item = new ToolItem("WPick", 32000);
+                               void* r = player->inventory.addItem("main", item);
+                               assert(r == NULL);
+                       }
+                       {
+                               InventoryItem *item = new ToolItem("STPick", 32000);
+                               void* r = player->inventory.addItem("main", item);
+                               assert(r == NULL);
+                       }*/
+                       /*// Give some lights
+                       {
+                               InventoryItem *item = new MaterialItem(CONTENT_TORCH, 999);
+                               bool r = player->inventory.addItem("main", item);
+                               assert(r == true);
+                       }
+                       // and some signs
+                       for(u16 i=0; i<4; i++)
+                       {
+                               InventoryItem *item = new MapBlockObjectItem("Sign Example text");
+                               bool r = player->inventory.addItem("main", item);
+                               assert(r == true);
+                       }*/
+                       /*// Give some other stuff
+                       {
+                               InventoryItem *item = new MaterialItem(CONTENT_TREE, 999);
+                               bool r = player->inventory.addItem("main", item);
+                               assert(r == true);
+                       }*/
+               }
+
+               return player;
+               
+       } // create new player
+}
+
+#if 0
 void Server::UpdateBlockWaterPressure(MapBlock *block,
                        core::map<v3s16, MapBlock*> &modified_blocks)
 {
@@ -2955,6 +3375,7 @@ void Server::UpdateBlockWaterPressure(MapBlock *block,
        
        v.blitBack(modified_blocks);
 }
+#endif
 
 void Server::handlePeerChange(PeerChange &c)
 {
@@ -2978,137 +3399,6 @@ void Server::handlePeerChange(PeerChange &c)
                client->peer_id = c.peer_id;
                m_clients.insert(client->peer_id, client);
 
-               // Create player
-               {
-                       Player *player = m_env.getPlayer(c.peer_id);
-                       
-                       // The player shouldn't already exist
-                       assert(player == NULL);
-
-                       player = new ServerRemotePlayer();
-                       player->peer_id = c.peer_id;
-
-                       /*
-                               Set player position
-                       */
-                       
-                       // We're going to throw the player to this position
-                       //v2s16 nodepos(29990,29990);
-                       //v2s16 nodepos(9990,9990);
-                       v2s16 nodepos(0,0);
-                       v2s16 sectorpos = getNodeSectorPos(nodepos);
-                       // Get zero sector (it could have been unloaded to disk)
-                       m_env.getMap().emergeSector(sectorpos);
-                       // Get ground height at origin
-                       f32 groundheight = m_env.getMap().getGroundHeight(nodepos, true);
-                       // The sector should have been generated -> groundheight exists
-                       assert(groundheight > GROUNDHEIGHT_VALID_MINVALUE);
-                       // Don't go underwater
-                       if(groundheight < WATER_LEVEL)
-                               groundheight = WATER_LEVEL;
-
-                       player->setPosition(intToFloat(v3s16(
-                                       nodepos.X,
-                                       groundheight + 1,
-                                       nodepos.Y
-                       )));
-
-                       /*
-                               Add player to environment
-                       */
-
-                       m_env.addPlayer(player);
-
-                       /*
-                               Add stuff to inventory
-                       */
-                       
-                       if(g_settings.getBool("creative_mode"))
-                       {
-                               // Give a good pick
-                               {
-                                       InventoryItem *item = new ToolItem("STPick", 32000);
-                                       void* r = player->inventory.addItem("main", item);
-                                       assert(r == NULL);
-                               }
-                               // Give all materials
-                               assert(USEFUL_CONTENT_COUNT <= PLAYER_INVENTORY_SIZE);
-                               for(u16 i=0; i<USEFUL_CONTENT_COUNT; i++)
-                               {
-                                       // Skip some materials
-                                       if(i == CONTENT_OCEAN)
-                                               continue;
-
-                                       InventoryItem *item = new MaterialItem(i, 1);
-                                       player->inventory.addItem("main", item);
-                               }
-                               // Sign
-                               {
-                                       InventoryItem *item = new MapBlockObjectItem("Sign Example text");
-                                       void* r = player->inventory.addItem("main", item);
-                                       assert(r == NULL);
-                               }
-                               /*// Rat
-                               {
-                                       InventoryItem *item = new MapBlockObjectItem("Rat");
-                                       bool r = player->inventory.addItem("main", item);
-                                       assert(r == true);
-                               }*/
-                       }
-                       else
-                       {
-                               /*{
-                                       InventoryItem *item = new MaterialItem(CONTENT_MESE, 6);
-                                       void* r = player->inventory.addItem("main", item);
-                                       assert(r == NULL);
-                               }
-                               {
-                                       InventoryItem *item = new MaterialItem(CONTENT_COALSTONE, 6);
-                                       void* r = player->inventory.addItem("main", item);
-                                       assert(r == NULL);
-                               }
-                               {
-                                       InventoryItem *item = new MaterialItem(CONTENT_WOOD, 6);
-                                       void* r = player->inventory.addItem("main", item);
-                                       assert(r == NULL);
-                               }
-                               {
-                                       InventoryItem *item = new CraftItem("Stick", 4);
-                                       void* r = player->inventory.addItem("main", item);
-                                       assert(r == NULL);
-                               }
-                               {
-                                       InventoryItem *item = new ToolItem("WPick", 32000);
-                                       void* r = player->inventory.addItem("main", item);
-                                       assert(r == NULL);
-                               }
-                               {
-                                       InventoryItem *item = new ToolItem("STPick", 32000);
-                                       void* r = player->inventory.addItem("main", item);
-                                       assert(r == NULL);
-                               }*/
-                               /*// Give some lights
-                               {
-                                       InventoryItem *item = new MaterialItem(CONTENT_TORCH, 999);
-                                       bool r = player->inventory.addItem("main", item);
-                                       assert(r == true);
-                               }
-                               // and some signs
-                               for(u16 i=0; i<4; i++)
-                               {
-                                       InventoryItem *item = new MapBlockObjectItem("Sign Example text");
-                                       bool r = player->inventory.addItem("main", item);
-                                       assert(r == true);
-                               }*/
-                               /*// Give some other stuff
-                               {
-                                       InventoryItem *item = new MaterialItem(CONTENT_TREE, 999);
-                                       bool r = player->inventory.addItem("main", item);
-                                       assert(r == true);
-                               }*/
-                       }
-               }
-
        } // PEER_ADDED
        else if(c.type == PEER_REMOVED)
        {
@@ -3133,11 +3423,20 @@ void Server::handlePeerChange(PeerChange &c)
                        message += L"*** ";
                        message += name;
                        message += L" left game";
+                       if(c.timeout)
+                               message += L" (timed out)";
                }
 
-               // Delete player
+               /*// Delete player
                {
                        m_env.removePlayer(c.peer_id);
+               }*/
+
+               // Set player client disconnected
+               {
+                       Player *player = m_env.getPlayer(c.peer_id);
+                       if(player != NULL)
+                               player->peer_id = 0;
                }
                
                // Delete client
@@ -3171,4 +3470,44 @@ void Server::handlePeerChanges()
        }
 }
 
+void dedicated_server_loop(Server &server)
+{
+       DSTACK(__FUNCTION_NAME);
+       
+       std::cout<<std::endl;
+       std::cout<<"========================"<<std::endl;
+       std::cout<<"Running dedicated server"<<std::endl;
+       std::cout<<"========================"<<std::endl;
+       std::cout<<std::endl;
+
+       for(;;)
+       {
+               // This is kind of a hack but can be done like this
+               // because server.step() is very light
+               sleep_ms(30);
+               server.step(0.030);
+
+               static int counter = 0;
+               counter--;
+               if(counter <= 0)
+               {
+                       counter = 10;
+
+                       core::list<PlayerInfo> list = server.getPlayerInfo();
+                       core::list<PlayerInfo>::Iterator i;
+                       static u32 sum_old = 0;
+                       u32 sum = PIChecksum(list);
+                       if(sum != sum_old)
+                       {
+                               std::cout<<DTIME<<"Player info:"<<std::endl;
+                               for(i=list.begin(); i!=list.end(); i++)
+                               {
+                                       i->PrintLine(&std::cout);
+                               }
+                       }
+                       sum_old = sum;
+               }
+       }
+}
+