+ else
+ {
+ not_sand_counter++;
+ if(not_sand_counter > 3)
+ break;
+ }
+
+ vmanip.m_area.add_y(em, i, -1);
+ }
+ }
+
+ }
+
+ }//timer1
+ {
+ // 1ms @cs=8
+ //TimeTaker timer1("generate trees");
+
+ /*
+ Generate some trees
+ */
+ {
+ // Divide area into parts
+ s16 div = 8;
+ s16 sidelen = sectorpos_base_size*MAP_BLOCKSIZE / div;
+ double area = sidelen * sidelen;
+ for(s16 x0=0; x0<div; x0++)
+ for(s16 z0=0; z0<div; z0++)
+ {
+ // Center position of part of division
+ v2s16 p2d_center(
+ sectorpos_base.X*MAP_BLOCKSIZE + sidelen/2 + sidelen*x0,
+ sectorpos_base.Y*MAP_BLOCKSIZE + sidelen/2 + sidelen*z0
+ );
+ // Minimum edge of part of division
+ v2s16 p2d_min(
+ sectorpos_base.X*MAP_BLOCKSIZE + sidelen*x0,
+ sectorpos_base.Y*MAP_BLOCKSIZE + sidelen*z0
+ );
+ // Maximum edge of part of division
+ v2s16 p2d_max(
+ sectorpos_base.X*MAP_BLOCKSIZE + sidelen + sidelen*x0 - 1,
+ sectorpos_base.Y*MAP_BLOCKSIZE + sidelen + sidelen*z0 - 1
+ );
+ // Amount of trees
+ u32 tree_count = area * tree_amount_2d(m_seed, p2d_center);
+ // Put trees in random places on part of division
+ for(u32 i=0; i<tree_count; i++)
+ {
+ s16 x = myrand_range(p2d_min.X, p2d_max.X);
+ s16 z = myrand_range(p2d_min.Y, p2d_max.Y);
+ s16 y = find_ground_level(vmanip, v2s16(x,z));
+ // Don't make a tree under water level
+ if(y < WATER_LEVEL)
+ continue;
+ // Don't make a tree so high that it doesn't fit
+ if(y > y_nodes_max - 6)
+ continue;
+ v3s16 p(x,y,z);
+ /*
+ Trees grow only on mud and grass
+ */
+ {
+ u32 i = vmanip.m_area.index(v3s16(p));
+ MapNode *n = &vmanip.m_data[i];
+ if(n->d != CONTENT_MUD && n->d != CONTENT_GRASS)
+ continue;
+ }
+ p.Y++;
+ // Make a tree
+ make_tree(vmanip, p);
+ }
+ }
+ /*u32 tree_max = relative_area / 60;
+ //u32 count = myrand_range(0, tree_max);
+ for(u32 i=0; i<count; i++)
+ {
+ s16 x = myrand_range(0, sectorpos_base_size*MAP_BLOCKSIZE-1);
+ s16 z = myrand_range(0, sectorpos_base_size*MAP_BLOCKSIZE-1);
+ x += sectorpos_base.X*MAP_BLOCKSIZE;
+ z += sectorpos_base.Y*MAP_BLOCKSIZE;
+ s16 y = find_ground_level(vmanip, v2s16(x,z));
+ // Don't make a tree under water level
+ if(y < WATER_LEVEL)
+ continue;
+ v3s16 p(x,y+1,z);
+ // Make a tree
+ make_tree(vmanip, p);
+ }*/
+ }
+
+ }//timer1
+
+ {
+ // 19ms @cs=8
+ //TimeTaker timer1("grow grass");
+
+ /*
+ Grow grass
+ */
+
+ /*for(s16 x=0-4; x<sectorpos_base_size*MAP_BLOCKSIZE+4; x++)
+ for(s16 z=0-4; z<sectorpos_base_size*MAP_BLOCKSIZE+4; z++)*/
+ for(s16 x=0-max_spread_amount;
+ x<sectorpos_base_size*MAP_BLOCKSIZE+max_spread_amount;
+ x++)
+ for(s16 z=0-max_spread_amount;
+ z<sectorpos_base_size*MAP_BLOCKSIZE+max_spread_amount;
+ z++)
+ {
+ // Node position in 2d
+ v2s16 p2d = sectorpos_base*MAP_BLOCKSIZE + v2s16(x,z);
+
+ /*
+ Find the lowest surface to which enough light ends up
+ to make grass grow.
+
+ Basically just wait until not air and not leaves.
+ */
+ s16 surface_y = 0;
+ {
+ v3s16 em = vmanip.m_area.getExtent();
+ u32 i = vmanip.m_area.index(v3s16(p2d.X, y_nodes_max, p2d.Y));
+ s16 y;
+ // Go to ground level
+ for(y=y_nodes_max; y>=y_nodes_min; y--)
+ {
+ MapNode &n = vmanip.m_data[i];
+ if(n.d != CONTENT_AIR
+ && n.d != CONTENT_LEAVES)
+ break;
+ vmanip.m_area.add_y(em, i, -1);
+ }
+ if(y >= y_nodes_min)
+ surface_y = y;
+ else
+ surface_y = y_nodes_min;
+ }
+
+ u32 i = vmanip.m_area.index(p2d.X, surface_y, p2d.Y);
+ MapNode *n = &vmanip.m_data[i];
+ if(n->d == CONTENT_MUD)
+ n->d = CONTENT_GRASS;
+ }
+
+ }//timer1
+
+ /*
+ Initial lighting (sunlight)
+ */
+
+ core::map<v3s16, bool> light_sources;
+
+ {
+ // 750ms @cs=8, can't optimize more
+ TimeTaker timer1("initial lighting");
+
+#if 0
+ /*
+ Go through the edges and add all nodes that have light to light_sources
+ */
+
+ // Four edges
+ for(s16 i=0; i<4; i++)
+ // Edge length
+ for(s16 j=lighting_min_d;
+ j<=lighting_max_d;
+ j++)
+ {
+ s16 x;
+ s16 z;
+ // +-X
+ if(i == 0 || i == 1)
+ {
+ x = (i==0) ? lighting_min_d : lighting_max_d;
+ if(i == 0)
+ z = lighting_min_d;
+ else
+ z = lighting_max_d;
+ }
+ // +-Z
+ else
+ {
+ z = (i==0) ? lighting_min_d : lighting_max_d;
+ if(i == 0)
+ x = lighting_min_d;
+ else
+ x = lighting_max_d;
+ }
+
+ // Node position in 2d
+ v2s16 p2d = sectorpos_base*MAP_BLOCKSIZE + v2s16(x,z);
+
+ {
+ v3s16 em = vmanip.m_area.getExtent();
+ s16 y_start = y_nodes_max;
+ u32 i = vmanip.m_area.index(v3s16(p2d.X, y_start, p2d.Y));
+ for(s16 y=y_start; y>=y_nodes_min; y--)
+ {
+ MapNode *n = &vmanip.m_data[i];
+ if(n->getLight(LIGHTBANK_DAY) != 0)
+ {
+ light_sources.insert(v3s16(p2d.X, y, p2d.Y), true);
+ }
+ //NOTE: This is broken, at least the index has to
+ // be incremented
+ }
+ }
+ }
+#endif
+
+#if 1
+ /*
+ Go through the edges and apply sunlight to them, not caring
+ about neighbors
+ */
+
+ // Four edges
+ for(s16 i=0; i<4; i++)
+ // Edge length
+ for(s16 j=lighting_min_d;
+ j<=lighting_max_d;
+ j++)
+ {
+ s16 x;
+ s16 z;
+ // +-X
+ if(i == 0 || i == 1)
+ {
+ x = (i==0) ? lighting_min_d : lighting_max_d;
+ if(i == 0)
+ z = lighting_min_d;
+ else
+ z = lighting_max_d;
+ }
+ // +-Z
+ else
+ {
+ z = (i==0) ? lighting_min_d : lighting_max_d;
+ if(i == 0)
+ x = lighting_min_d;
+ else
+ x = lighting_max_d;
+ }
+
+ // Node position in 2d
+ v2s16 p2d = sectorpos_base*MAP_BLOCKSIZE + v2s16(x,z);
+
+ // Loop from top to down
+ {
+ u8 light = LIGHT_SUN;
+ v3s16 em = vmanip.m_area.getExtent();
+ s16 y_start = y_nodes_max;
+ u32 i = vmanip.m_area.index(v3s16(p2d.X, y_start, p2d.Y));
+ for(s16 y=y_start; y>=y_nodes_min; y--)
+ {
+ MapNode *n = &vmanip.m_data[i];
+ if(light_propagates_content(n->d) == false)
+ {
+ light = 0;
+ }
+ else if(light != LIGHT_SUN
+ || sunlight_propagates_content(n->d) == false)
+ {
+ if(light > 0)
+ light--;
+ }
+
+ n->setLight(LIGHTBANK_DAY, light);
+ n->setLight(LIGHTBANK_NIGHT, 0);
+
+ if(light != 0)
+ {
+ // Insert light source
+ light_sources.insert(v3s16(p2d.X, y, p2d.Y), true);
+ }
+
+ // Increment index by y
+ vmanip.m_area.add_y(em, i, -1);
+ }
+ }
+ }
+#endif
+
+ /*for(s16 x=0; x<sectorpos_base_size*MAP_BLOCKSIZE; x++)
+ for(s16 z=0; z<sectorpos_base_size*MAP_BLOCKSIZE; z++)*/
+ /*for(s16 x=0-max_spread_amount+1;
+ x<sectorpos_base_size*MAP_BLOCKSIZE+max_spread_amount-1;
+ x++)
+ for(s16 z=0-max_spread_amount+1;
+ z<sectorpos_base_size*MAP_BLOCKSIZE+max_spread_amount-1;
+ z++)*/
+#if 1
+ /*
+ This has to be 1 smaller than the actual area, because
+ neighboring nodes are checked.
+ */
+ for(s16 x=lighting_min_d+1;
+ x<=lighting_max_d-1;
+ x++)
+ for(s16 z=lighting_min_d+1;
+ z<=lighting_max_d-1;
+ z++)
+ {
+ // Node position in 2d
+ v2s16 p2d = sectorpos_base*MAP_BLOCKSIZE + v2s16(x,z);
+
+ /*
+ Apply initial sunlight
+ */
+ {
+ u8 light = LIGHT_SUN;
+ bool add_to_sources = false;
+ v3s16 em = vmanip.m_area.getExtent();
+ s16 y_start = y_nodes_max;
+ u32 i = vmanip.m_area.index(v3s16(p2d.X, y_start, p2d.Y));
+ for(s16 y=y_start; y>=y_nodes_min; y--)
+ {
+ MapNode *n = &vmanip.m_data[i];
+
+ if(light_propagates_content(n->d) == false)
+ {
+ light = 0;
+ }
+ else if(light != LIGHT_SUN
+ || sunlight_propagates_content(n->d) == false)
+ {
+ if(light > 0)
+ light--;
+ }
+
+ // This doesn't take much time
+ if(add_to_sources == false)
+ {
+ /*
+ Check sides. If side is not air or water, start
+ adding to light_sources.
+ */
+ v3s16 dirs4[4] = {
+ v3s16(0,0,1), // back
+ v3s16(1,0,0), // right
+ v3s16(0,0,-1), // front
+ v3s16(-1,0,0), // left
+ };
+ for(u32 di=0; di<4; di++)
+ {
+ v3s16 dirp = dirs4[di];
+ u32 i2 = i;
+ vmanip.m_area.add_p(em, i2, dirp);
+ MapNode *n2 = &vmanip.m_data[i2];
+ if(
+ n2->d != CONTENT_AIR
+ && n2->d != CONTENT_WATERSOURCE
+ && n2->d != CONTENT_WATER
+ ){
+ add_to_sources = true;
+ break;
+ }
+ }
+ }
+
+ n->setLight(LIGHTBANK_DAY, light);
+ n->setLight(LIGHTBANK_NIGHT, 0);
+
+ // This doesn't take much time
+ if(light != 0 && add_to_sources)
+ {
+ // Insert light source
+ light_sources.insert(v3s16(p2d.X, y, p2d.Y), true);
+ }
+
+ // Increment index by y
+ vmanip.m_area.add_y(em, i, -1);
+ }
+ }
+ }
+#endif
+
+#if 0
+ for(s16 x=lighting_min_d+1;
+ x<=lighting_max_d-1;
+ x++)
+ for(s16 z=lighting_min_d+1;
+ z<=lighting_max_d-1;
+ z++)
+ {
+ // Node position in 2d
+ v2s16 p2d = sectorpos_base*MAP_BLOCKSIZE + v2s16(x,z);
+
+ /*
+ Apply initial sunlight
+ */
+ {
+ u8 light = LIGHT_SUN;
+ v3s16 em = vmanip.m_area.getExtent();
+ s16 y_start = y_nodes_max;
+ u32 i = vmanip.m_area.index(v3s16(p2d.X, y_start, p2d.Y));
+ for(s16 y=y_start; y>=y_nodes_min; y--)
+ {
+ MapNode *n = &vmanip.m_data[i];
+
+ if(light_propagates_content(n->d) == false)
+ {
+ light = 0;
+ }
+ else if(light != LIGHT_SUN
+ || sunlight_propagates_content(n->d) == false)
+ {
+ if(light > 0)
+ light--;
+ }
+
+ n->setLight(LIGHTBANK_DAY, light);
+ n->setLight(LIGHTBANK_NIGHT, 0);
+
+ // This doesn't take much time
+ if(light != 0)
+ {
+ // Insert light source
+ light_sources.insert(v3s16(p2d.X, y, p2d.Y), true);
+ }
+
+ // Increment index by y
+ vmanip.m_area.add_y(em, i, -1);
+ }
+ }
+ }
+#endif
+
+ }//timer1
+
+ // Spread light around
+ {
+ TimeTaker timer("generateChunkRaw() spreadLight");
+ vmanip.spreadLight(LIGHTBANK_DAY, light_sources);
+ }
+
+ /*
+ Generation ended
+ */
+
+ timer_generate.stop();
+
+ /*
+ Blit generated stuff to map
+ */
+ {
+ // 70ms @cs=8
+ //TimeTaker timer("generateChunkRaw() blitBackAll");
+ vmanip.blitBackAll(&changed_blocks);
+ }
+
+ /*
+ Update day/night difference cache of the MapBlocks
+ */
+ {
+ for(core::map<v3s16, MapBlock*>::Iterator i = changed_blocks.getIterator();
+ i.atEnd() == false; i++)
+ {
+ MapBlock *block = i.getNode()->getValue();
+ block->updateDayNightDiff();
+ }
+ }
+
+
+ /*
+ Create chunk metadata
+ */
+
+ for(s16 x=-1; x<=1; x++)
+ for(s16 y=-1; y<=1; y++)
+ {
+ v2s16 chunkpos0 = chunkpos + v2s16(x,y);
+ // Add chunk meta information
+ MapChunk *chunk = getChunk(chunkpos0);
+ if(chunk == NULL)
+ {
+ chunk = new MapChunk();
+ m_chunks.insert(chunkpos0, chunk);
+ }
+ //chunk->setIsVolatile(true);
+ if(chunk->getGenLevel() > GENERATED_PARTLY)
+ chunk->setGenLevel(GENERATED_PARTLY);
+ }
+
+ /*
+ Set central chunk non-volatile
+ */
+ MapChunk *chunk = getChunk(chunkpos);
+ assert(chunk);
+ // Set non-volatile
+ //chunk->setIsVolatile(false);
+ chunk->setGenLevel(GENERATED_FULLY);
+
+ /*
+ Save changed parts of map
+ */
+ save(true);
+
+ /*
+ Return central chunk (which was requested)
+ */
+ return chunk;
+}
+
+MapChunk* ServerMap::generateChunk(v2s16 chunkpos1,
+ core::map<v3s16, MapBlock*> &changed_blocks)
+{
+ dstream<<"generateChunk(): Generating chunk "
+ <<"("<<chunkpos1.X<<","<<chunkpos1.Y<<")"
+ <<std::endl;
+
+ /*for(s16 x=-1; x<=1; x++)
+ for(s16 y=-1; y<=1; y++)*/
+ for(s16 x=-0; x<=0; x++)
+ for(s16 y=-0; y<=0; y++)
+ {
+ v2s16 chunkpos0 = chunkpos1 + v2s16(x,y);
+ MapChunk *chunk = getChunk(chunkpos0);
+ // Skip if already generated
+ if(chunk != NULL && chunk->getGenLevel() == GENERATED_FULLY)
+ continue;
+ generateChunkRaw(chunkpos0, changed_blocks);
+ }
+
+ assert(chunkNonVolatile(chunkpos1));
+
+ MapChunk *chunk = getChunk(chunkpos1);
+ return chunk;
+}
+
+ServerMapSector * ServerMap::createSector(v2s16 p2d)
+{
+ DSTACK("%s: p2d=(%d,%d)",
+ __FUNCTION_NAME,
+ p2d.X, p2d.Y);
+
+ /*
+ Check if it exists already in memory
+ */
+ ServerMapSector *sector = (ServerMapSector*)getSectorNoGenerateNoEx(p2d);
+ if(sector != NULL)
+ return sector;
+
+ /*
+ Try to load it from disk (with blocks)
+ */
+ if(loadSectorFull(p2d) == true)
+ {
+ ServerMapSector *sector = (ServerMapSector*)getSectorNoGenerateNoEx(p2d);
+ if(sector == NULL)
+ {
+ dstream<<"ServerMap::createSector(): loadSectorFull didn't make a sector"<<std::endl;
+ throw InvalidPositionException("");
+ }
+ return sector;
+ }
+
+ /*
+ Do not create over-limit
+ */
+ if(p2d.X < -MAP_GENERATION_LIMIT / MAP_BLOCKSIZE
+ || p2d.X > MAP_GENERATION_LIMIT / MAP_BLOCKSIZE
+ || p2d.Y < -MAP_GENERATION_LIMIT / MAP_BLOCKSIZE
+ || p2d.Y > MAP_GENERATION_LIMIT / MAP_BLOCKSIZE)
+ throw InvalidPositionException("createSector(): pos. over limit");
+
+ /*
+ Generate blank sector
+ */
+
+ sector = new ServerMapSector(this, p2d);
+
+ // Sector position on map in nodes
+ v2s16 nodepos2d = p2d * MAP_BLOCKSIZE;
+
+ /*
+ Insert to container
+ */
+ m_sectors.insert(p2d, sector);
+
+ return sector;
+}
+
+MapSector * ServerMap::emergeSector(v2s16 p2d,
+ core::map<v3s16, MapBlock*> &changed_blocks)
+{
+ DSTACK("%s: p2d=(%d,%d)",
+ __FUNCTION_NAME,
+ p2d.X, p2d.Y);
+
+ /*
+ Check chunk status
+ */
+ v2s16 chunkpos = sector_to_chunk(p2d);
+ /*bool chunk_nonvolatile = false;
+ MapChunk *chunk = getChunk(chunkpos);
+ if(chunk && chunk->getIsVolatile() == false)
+ chunk_nonvolatile = true;*/
+ bool chunk_nonvolatile = chunkNonVolatile(chunkpos);
+
+ /*
+ If chunk is not fully generated, generate chunk
+ */
+ if(chunk_nonvolatile == false)
+ {
+ // Generate chunk and neighbors
+ generateChunk(chunkpos, changed_blocks);
+ }
+
+ /*
+ Return sector if it exists now
+ */
+ MapSector *sector = getSectorNoGenerateNoEx(p2d);
+ if(sector != NULL)
+ return sector;
+
+ /*
+ Try to load it from disk
+ */
+ if(loadSectorFull(p2d) == true)
+ {
+ MapSector *sector = getSectorNoGenerateNoEx(p2d);
+ if(sector == NULL)
+ {
+ dstream<<"ServerMap::emergeSector(): loadSectorFull didn't make a sector"<<std::endl;
+ throw InvalidPositionException("");
+ }
+ return sector;
+ }
+
+ /*
+ generateChunk should have generated the sector
+ */
+ //assert(0);
+
+ dstream<<"WARNING: ServerMap::emergeSector: Cannot find sector ("
+ <<p2d.X<<","<<p2d.Y<<" and chunk is already generated. "
+ <<std::endl;
+
+#if 0
+ dstream<<"WARNING: Creating an empty sector."<<std::endl;
+
+ return createSector(p2d);
+
+#endif
+
+#if 1
+ dstream<<"WARNING: Forcing regeneration of chunk."<<std::endl;
+
+ // Generate chunk
+ generateChunkRaw(chunkpos, changed_blocks, true);
+
+ /*
+ Return sector if it exists now
+ */
+ sector = getSectorNoGenerateNoEx(p2d);
+ if(sector != NULL)
+ return sector;
+
+ dstream<<"ERROR: Could not get sector from anywhere."<<std::endl;
+
+ assert(0);
+#endif
+
+ /*
+ Generate directly
+ */
+ //return generateSector();
+}
+
+/*
+ NOTE: This is not used for main map generation, only for blocks
+ that are very high or low
+*/
+MapBlock * ServerMap::generateBlock(
+ v3s16 p,
+ MapBlock *original_dummy,
+ ServerMapSector *sector,
+ core::map<v3s16, MapBlock*> &changed_blocks,
+ core::map<v3s16, MapBlock*> &lighting_invalidated_blocks
+)
+{
+ DSTACK("%s: p=(%d,%d,%d)",
+ __FUNCTION_NAME,
+ p.X, p.Y, p.Z);
+
+ /*dstream<<"generateBlock(): "
+ <<"("<<p.X<<","<<p.Y<<","<<p.Z<<")"
+ <<std::endl;*/
+
+ MapBlock *block = original_dummy;
+
+ v2s16 p2d(p.X, p.Z);
+ s16 block_y = p.Y;
+ v2s16 p2d_nodes = p2d * MAP_BLOCKSIZE;
+
+ /*
+ Do not generate over-limit
+ */
+ if(blockpos_over_limit(p))
+ {
+ dstream<<__FUNCTION_NAME<<": Block position over limit"<<std::endl;
+ throw InvalidPositionException("generateBlock(): pos. over limit");
+ }
+
+ /*
+ If block doesn't exist, create one.
+ If it exists, it is a dummy. In that case unDummify() it.
+
+ NOTE: This already sets the map as the parent of the block
+ */
+ if(block == NULL)
+ {
+ block = sector->createBlankBlockNoInsert(block_y);
+ }
+ else
+ {
+ // Remove the block so that nobody can get a half-generated one.
+ sector->removeBlock(block);
+ // Allocate the block to contain the generated data
+ block->unDummify();
+ }
+
+ u8 water_material = CONTENT_WATERSOURCE;
+
+ s32 lowest_ground_y = 32767;
+ s32 highest_ground_y = -32768;
+
+ for(s16 z0=0; z0<MAP_BLOCKSIZE; z0++)
+ for(s16 x0=0; x0<MAP_BLOCKSIZE; x0++)
+ {
+ //dstream<<"generateBlock: x0="<<x0<<", z0="<<z0<<std::endl;
+
+ //s16 surface_y = 0;
+
+ s16 surface_y = base_rock_level_2d(m_seed, p2d_nodes+v2s16(x0,z0))
+ + AVERAGE_MUD_AMOUNT;
+
+ if(surface_y < lowest_ground_y)
+ lowest_ground_y = surface_y;
+ if(surface_y > highest_ground_y)
+ highest_ground_y = surface_y;
+
+ s32 surface_depth = AVERAGE_MUD_AMOUNT;
+
+ for(s16 y0=0; y0<MAP_BLOCKSIZE; y0++)
+ {
+ s16 real_y = block_y * MAP_BLOCKSIZE + y0;
+ MapNode n;
+ /*
+ Calculate lighting
+
+ NOTE: If there are some man-made structures above the
+ newly created block, they won't be taken into account.
+ */
+ if(real_y > surface_y)
+ n.setLight(LIGHTBANK_DAY, LIGHT_SUN);
+
+ /*
+ Calculate material
+ */
+
+ // If node is over heightmap y, it's air or water
+ if(real_y > surface_y)
+ {
+ // If under water level, it's water
+ if(real_y < WATER_LEVEL)
+ {
+ n.d = water_material;
+ n.setLight(LIGHTBANK_DAY,
+ diminish_light(LIGHT_SUN, WATER_LEVEL-real_y+1));
+ /*
+ Add to transforming liquid queue (in case it'd
+ start flowing)
+ */
+ v3s16 real_pos = v3s16(x0,y0,z0) + p*MAP_BLOCKSIZE;
+ m_transforming_liquid.push_back(real_pos);
+ }
+ // else air
+ else
+ n.d = CONTENT_AIR;
+ }
+ // Else it's ground or dungeons (air)
+ else
+ {
+ // If it's surface_depth under ground, it's stone
+ if(real_y <= surface_y - surface_depth)
+ {
+ n.d = CONTENT_STONE;
+ }
+ else
+ {
+ // It is mud if it is under the first ground
+ // level or under water
+ if(real_y < WATER_LEVEL || real_y <= surface_y - 1)
+ {
+ n.d = CONTENT_MUD;
+ }
+ else
+ {
+ n.d = CONTENT_GRASS;
+ }
+
+ //n.d = CONTENT_MUD;
+
+ /*// If under water level, it's mud
+ if(real_y < WATER_LEVEL)
+ n.d = CONTENT_MUD;
+ // Only the topmost node is grass
+ else if(real_y <= surface_y - 1)
+ n.d = CONTENT_MUD;
+ else
+ n.d = CONTENT_GRASS;*/
+ }
+ }
+
+ block->setNode(v3s16(x0,y0,z0), n);
+ }
+ }
+
+ /*
+ Calculate some helper variables
+ */
+
+ // Completely underground if the highest part of block is under lowest
+ // ground height.
+ // This has to be very sure; it's probably one too strict now but
+ // that's just better.
+ bool completely_underground =
+ block_y * MAP_BLOCKSIZE + MAP_BLOCKSIZE < lowest_ground_y;
+
+ bool some_part_underground = block_y * MAP_BLOCKSIZE <= highest_ground_y;
+
+ bool mostly_underwater_surface = false;
+ if(highest_ground_y < WATER_LEVEL
+ && some_part_underground && !completely_underground)
+ mostly_underwater_surface = true;
+
+ /*
+ Get local attributes
+ */
+
+ //dstream<<"generateBlock(): Getting local attributes"<<std::endl;
+
+ float caves_amount = 0.5;
+
+#if 0
+ {
+ /*
+ NOTE: BEWARE: Too big amount of attribute points slows verything
+ down by a lot.
+ 1 interpolation from 5000 points takes 2-3ms.
+ */
+ //TimeTaker timer("generateBlock() local attribute retrieval");
+ v2s16 nodepos2d = p2d * MAP_BLOCKSIZE;
+ PointAttributeList *list_caves_amount = m_padb.getList("caves_amount");
+ caves_amount = list_caves_amount->getInterpolatedFloat(nodepos2d);
+ }
+#endif
+
+ //dstream<<"generateBlock(): Done"<<std::endl;
+
+ /*
+ Generate dungeons
+ */
+
+ // Initialize temporary table
+ const s32 ued = MAP_BLOCKSIZE;
+ bool underground_emptiness[ued*ued*ued];
+ for(s32 i=0; i<ued*ued*ued; i++)
+ {
+ underground_emptiness[i] = 0;
+ }
+
+ // Fill table
+#if 1
+ {
+ /*
+ Initialize orp and ors. Try to find if some neighboring
+ MapBlock has a tunnel ended in its side
+ */
+
+ v3f orp(
+ (float)(myrand()%ued)+0.5,
+ (float)(myrand()%ued)+0.5,
+ (float)(myrand()%ued)+0.5
+ );
+
+ bool found_existing = false;
+
+ // Check z-
+ try
+ {
+ s16 z = -1;
+ for(s16 y=0; y<ued; y++)
+ for(s16 x=0; x<ued; x++)
+ {
+ v3s16 ap = v3s16(x,y,z) + block->getPosRelative();
+ if(getNode(ap).d == CONTENT_AIR)
+ {
+ orp = v3f(x+1,y+1,0);
+ found_existing = true;
+ goto continue_generating;
+ }
+ }
+ }
+ catch(InvalidPositionException &e){}
+
+ // Check z+
+ try
+ {
+ s16 z = ued;
+ for(s16 y=0; y<ued; y++)
+ for(s16 x=0; x<ued; x++)
+ {
+ v3s16 ap = v3s16(x,y,z) + block->getPosRelative();
+ if(getNode(ap).d == CONTENT_AIR)
+ {
+ orp = v3f(x+1,y+1,ued-1);
+ found_existing = true;
+ goto continue_generating;
+ }
+ }
+ }
+ catch(InvalidPositionException &e){}
+
+ // Check x-
+ try
+ {
+ s16 x = -1;
+ for(s16 y=0; y<ued; y++)
+ for(s16 z=0; z<ued; z++)
+ {
+ v3s16 ap = v3s16(x,y,z) + block->getPosRelative();
+ if(getNode(ap).d == CONTENT_AIR)
+ {
+ orp = v3f(0,y+1,z+1);
+ found_existing = true;
+ goto continue_generating;
+ }
+ }
+ }
+ catch(InvalidPositionException &e){}
+
+ // Check x+
+ try
+ {
+ s16 x = ued;
+ for(s16 y=0; y<ued; y++)
+ for(s16 z=0; z<ued; z++)
+ {
+ v3s16 ap = v3s16(x,y,z) + block->getPosRelative();
+ if(getNode(ap).d == CONTENT_AIR)
+ {
+ orp = v3f(ued-1,y+1,z+1);
+ found_existing = true;
+ goto continue_generating;
+ }
+ }
+ }
+ catch(InvalidPositionException &e){}
+
+ // Check y-
+ try
+ {
+ s16 y = -1;
+ for(s16 x=0; x<ued; x++)
+ for(s16 z=0; z<ued; z++)
+ {
+ v3s16 ap = v3s16(x,y,z) + block->getPosRelative();
+ if(getNode(ap).d == CONTENT_AIR)
+ {
+ orp = v3f(x+1,0,z+1);
+ found_existing = true;
+ goto continue_generating;
+ }
+ }
+ }
+ catch(InvalidPositionException &e){}
+
+ // Check y+
+ try
+ {
+ s16 y = ued;
+ for(s16 x=0; x<ued; x++)
+ for(s16 z=0; z<ued; z++)
+ {
+ v3s16 ap = v3s16(x,y,z) + block->getPosRelative();
+ if(getNode(ap).d == CONTENT_AIR)
+ {
+ orp = v3f(x+1,ued-1,z+1);
+ found_existing = true;
+ goto continue_generating;
+ }
+ }
+ }
+ catch(InvalidPositionException &e){}
+
+continue_generating:
+
+ /*
+ Choose whether to actually generate dungeon
+ */
+ bool do_generate_dungeons = true;
+ // Don't generate if no part is underground
+ if(!some_part_underground)
+ {
+ do_generate_dungeons = false;
+ }
+ // Don't generate if mostly underwater surface
+ /*else if(mostly_underwater_surface)
+ {
+ do_generate_dungeons = false;
+ }*/
+ // Partly underground = cave
+ else if(!completely_underground)
+ {
+ do_generate_dungeons = (rand() % 100 <= (s32)(caves_amount*100));
+ }
+ // Found existing dungeon underground
+ else if(found_existing && completely_underground)
+ {
+ do_generate_dungeons = (rand() % 100 <= (s32)(caves_amount*100));
+ }
+ // Underground and no dungeons found
+ else
+ {
+ do_generate_dungeons = (rand() % 300 <= (s32)(caves_amount*100));
+ }
+
+ if(do_generate_dungeons)
+ {
+ /*
+ Generate some tunnel starting from orp and ors
+ */
+ for(u16 i=0; i<3; i++)
+ {
+ v3f rp(
+ (float)(myrand()%ued)+0.5,
+ (float)(myrand()%ued)+0.5,
+ (float)(myrand()%ued)+0.5
+ );
+ s16 min_d = 0;
+ s16 max_d = 4;
+ s16 rs = (myrand()%(max_d-min_d+1))+min_d;
+
+ v3f vec = rp - orp;
+
+ for(float f=0; f<1.0; f+=0.04)
+ {
+ v3f fp = orp + vec * f;
+ v3s16 cp(fp.X, fp.Y, fp.Z);
+ s16 d0 = -rs/2;
+ s16 d1 = d0 + rs - 1;
+ for(s16 z0=d0; z0<=d1; z0++)
+ {
+ s16 si = rs - abs(z0);
+ for(s16 x0=-si; x0<=si-1; x0++)
+ {
+ s16 si2 = rs - abs(x0);
+ for(s16 y0=-si2+1; y0<=si2-1; y0++)
+ {
+ s16 z = cp.Z + z0;
+ s16 y = cp.Y + y0;
+ s16 x = cp.X + x0;
+ v3s16 p(x,y,z);
+ if(isInArea(p, ued) == false)
+ continue;
+ underground_emptiness[ued*ued*z + ued*y + x] = 1;
+ }
+ }
+ }
+ }
+
+ orp = rp;
+ }
+ }
+ }
+#endif
+
+ // Set to true if has caves.
+ // Set when some non-air is changed to air when making caves.
+ bool has_dungeons = false;
+
+ /*
+ Apply temporary cave data to block
+ */
+
+ for(s16 z0=0; z0<MAP_BLOCKSIZE; z0++)
+ for(s16 x0=0; x0<MAP_BLOCKSIZE; x0++)
+ {
+ for(s16 y0=0; y0<MAP_BLOCKSIZE; y0++)
+ {
+ MapNode n = block->getNode(v3s16(x0,y0,z0));
+
+ // Create dungeons
+ if(underground_emptiness[
+ ued*ued*(z0*ued/MAP_BLOCKSIZE)
+ +ued*(y0*ued/MAP_BLOCKSIZE)
+ +(x0*ued/MAP_BLOCKSIZE)])
+ {
+ if(content_features(n.d).walkable/*is_ground_content(n.d)*/)
+ {
+ // Has now caves
+ has_dungeons = true;
+ // Set air to node
+ n.d = CONTENT_AIR;
+ }
+ }
+
+ block->setNode(v3s16(x0,y0,z0), n);
+ }
+ }
+
+ /*
+ This is used for guessing whether or not the block should
+ receive sunlight from the top if the block above doesn't exist
+ */
+ block->setIsUnderground(completely_underground);
+
+ /*
+ Force lighting update if some part of block is partly
+ underground and has caves.
+ */
+ /*if(some_part_underground && !completely_underground && has_dungeons)
+ {
+ //dstream<<"Half-ground caves"<<std::endl;
+ lighting_invalidated_blocks[block->getPos()] = block;
+ }*/
+
+ // DEBUG: Always update lighting
+ //lighting_invalidated_blocks[block->getPos()] = block;
+
+ /*
+ Add some minerals
+ */
+
+ if(some_part_underground)
+ {
+ s16 underground_level = (lowest_ground_y/MAP_BLOCKSIZE - block_y)+1;
+
+ /*
+ Add meseblocks
+ */
+ for(s16 i=0; i<underground_level/4 + 1; i++)
+ {
+ if(myrand()%50 == 0)
+ {
+ v3s16 cp(
+ (myrand()%(MAP_BLOCKSIZE-2))+1,
+ (myrand()%(MAP_BLOCKSIZE-2))+1,
+ (myrand()%(MAP_BLOCKSIZE-2))+1
+ );
+
+ MapNode n;
+ n.d = CONTENT_MESE;
+
+ for(u16 i=0; i<27; i++)
+ {
+ if(block->getNode(cp+g_27dirs[i]).d == CONTENT_STONE)
+ if(myrand()%8 == 0)
+ block->setNode(cp+g_27dirs[i], n);
+ }
+ }
+ }
+
+ /*
+ Add coal
+ */
+ u16 coal_amount = 30;
+ u16 coal_rareness = 60 / coal_amount;
+ if(coal_rareness == 0)
+ coal_rareness = 1;
+ if(myrand()%coal_rareness == 0)
+ {
+ u16 a = myrand() % 16;
+ u16 amount = coal_amount * a*a*a / 1000;
+ for(s16 i=0; i<amount; i++)
+ {
+ v3s16 cp(
+ (myrand()%(MAP_BLOCKSIZE-2))+1,
+ (myrand()%(MAP_BLOCKSIZE-2))+1,
+ (myrand()%(MAP_BLOCKSIZE-2))+1
+ );
+
+ MapNode n;
+ n.d = CONTENT_STONE;
+ n.param = MINERAL_COAL;
+
+ for(u16 i=0; i<27; i++)
+ {
+ if(block->getNode(cp+g_27dirs[i]).d == CONTENT_STONE)
+ if(myrand()%8 == 0)
+ block->setNode(cp+g_27dirs[i], n);
+ }
+ }
+ }
+
+ /*
+ Add iron
+ */
+ //TODO: change to iron_amount or whatever
+ u16 iron_amount = 15;
+ u16 iron_rareness = 60 / iron_amount;
+ if(iron_rareness == 0)
+ iron_rareness = 1;
+ if(myrand()%iron_rareness == 0)
+ {
+ u16 a = myrand() % 16;
+ u16 amount = iron_amount * a*a*a / 1000;
+ for(s16 i=0; i<amount; i++)
+ {
+ v3s16 cp(
+ (myrand()%(MAP_BLOCKSIZE-2))+1,
+ (myrand()%(MAP_BLOCKSIZE-2))+1,
+ (myrand()%(MAP_BLOCKSIZE-2))+1
+ );
+
+ MapNode n;
+ n.d = CONTENT_STONE;
+ n.param = MINERAL_IRON;
+
+ for(u16 i=0; i<27; i++)
+ {
+ if(block->getNode(cp+g_27dirs[i]).d == CONTENT_STONE)
+ if(myrand()%8 == 0)
+ block->setNode(cp+g_27dirs[i], n);
+ }
+ }
+ }
+ }
+
+ /*
+ Create a few rats in empty blocks underground
+ */
+ if(completely_underground)
+ {
+ //for(u16 i=0; i<2; i++)
+ {
+ v3s16 cp(
+ (myrand()%(MAP_BLOCKSIZE-2))+1,
+ (myrand()%(MAP_BLOCKSIZE-2))+1,
+ (myrand()%(MAP_BLOCKSIZE-2))+1
+ );
+
+ // Check that the place is empty
+ //if(!is_ground_content(block->getNode(cp).d))
+ if(1)
+ {
+ RatObject *obj = new RatObject(NULL, -1, intToFloat(cp, BS));
+ block->addObject(obj);
+ }
+ }
+ }
+
+ /*
+ Add block to sector.
+ */
+ sector->insertBlock(block);
+
+ // Lighting is invalid after generation.
+ block->setLightingExpired(true);
+
+#if 0
+ /*
+ Debug information
+ */
+ dstream
+ <<"lighting_invalidated_blocks.size()"
+ <<", has_dungeons"
+ <<", completely_ug"
+ <<", some_part_ug"
+ <<" "<<lighting_invalidated_blocks.size()
+ <<", "<<has_dungeons
+ <<", "<<completely_underground
+ <<", "<<some_part_underground
+ <<std::endl;
+#endif
+
+ return block;
+}
+
+MapBlock * ServerMap::createBlock(v3s16 p)
+{
+ DSTACK("%s: p=(%d,%d,%d)",
+ __FUNCTION_NAME, p.X, p.Y, p.Z);
+
+ /*
+ Do not create over-limit
+ */
+ if(p.X < -MAP_GENERATION_LIMIT / MAP_BLOCKSIZE
+ || p.X > MAP_GENERATION_LIMIT / MAP_BLOCKSIZE
+ || p.Y < -MAP_GENERATION_LIMIT / MAP_BLOCKSIZE
+ || p.Y > MAP_GENERATION_LIMIT / MAP_BLOCKSIZE
+ || p.Z < -MAP_GENERATION_LIMIT / MAP_BLOCKSIZE
+ || p.Z > MAP_GENERATION_LIMIT / MAP_BLOCKSIZE)
+ throw InvalidPositionException("createBlock(): pos. over limit");
+
+ v2s16 p2d(p.X, p.Z);
+ s16 block_y = p.Y;
+ /*
+ This will create or load a sector if not found in memory.
+ If block exists on disk, it will be loaded.
+
+ NOTE: On old save formats, this will be slow, as it generates
+ lighting on blocks for them.
+ */
+ ServerMapSector *sector;
+ try{
+ sector = (ServerMapSector*)createSector(p2d);
+ assert(sector->getId() == MAPSECTOR_SERVER);
+ }
+ catch(InvalidPositionException &e)
+ {
+ dstream<<"createBlock: createSector() failed"<<std::endl;
+ throw e;
+ }
+ /*
+ NOTE: This should not be done, or at least the exception
+ should not be passed on as std::exception, because it
+ won't be catched at all.
+ */
+ /*catch(std::exception &e)
+ {
+ dstream<<"createBlock: createSector() failed: "
+ <<e.what()<<std::endl;
+ throw e;
+ }*/
+
+ /*
+ Try to get a block from the sector
+ */
+
+ MapBlock *block = sector->getBlockNoCreateNoEx(block_y);
+ if(block)
+ return block;
+ // Create blank
+ block = sector->createBlankBlock(block_y);
+ return block;
+}
+
+MapBlock * ServerMap::emergeBlock(
+ v3s16 p,
+ bool only_from_disk,
+ core::map<v3s16, MapBlock*> &changed_blocks,
+ core::map<v3s16, MapBlock*> &lighting_invalidated_blocks
+)
+{
+ DSTACK("%s: p=(%d,%d,%d), only_from_disk=%d",
+ __FUNCTION_NAME,
+ p.X, p.Y, p.Z, only_from_disk);
+
+ /*
+ Do not generate over-limit
+ */
+ if(p.X < -MAP_GENERATION_LIMIT / MAP_BLOCKSIZE
+ || p.X > MAP_GENERATION_LIMIT / MAP_BLOCKSIZE
+ || p.Y < -MAP_GENERATION_LIMIT / MAP_BLOCKSIZE
+ || p.Y > MAP_GENERATION_LIMIT / MAP_BLOCKSIZE
+ || p.Z < -MAP_GENERATION_LIMIT / MAP_BLOCKSIZE
+ || p.Z > MAP_GENERATION_LIMIT / MAP_BLOCKSIZE)
+ throw InvalidPositionException("emergeBlock(): pos. over limit");
+
+ v2s16 p2d(p.X, p.Z);
+ s16 block_y = p.Y;
+ /*
+ This will create or load a sector if not found in memory.
+ If block exists on disk, it will be loaded.
+ */
+ ServerMapSector *sector;
+ try{
+ sector = (ServerMapSector*)emergeSector(p2d, changed_blocks);
+ assert(sector->getId() == MAPSECTOR_SERVER);
+ }
+ catch(InvalidPositionException &e)
+ {
+ dstream<<"emergeBlock: emergeSector() failed: "
+ <<e.what()<<std::endl;
+ dstream<<"Path to failed sector: "<<getSectorDir(p2d)
+ <<std::endl
+ <<"You could try to delete it."<<std::endl;
+ throw e;
+ }
+ catch(VersionMismatchException &e)
+ {
+ dstream<<"emergeBlock: emergeSector() failed: "
+ <<e.what()<<std::endl;
+ dstream<<"Path to failed sector: "<<getSectorDir(p2d)
+ <<std::endl
+ <<"You could try to delete it."<<std::endl;
+ throw e;
+ }
+ /*
+ NOTE: This should not be done, or at least the exception
+ should not be passed on as std::exception, because it
+ won't be catched at all.
+ */
+ /*catch(std::exception &e)
+ {
+ dstream<<"emergeBlock: emergeSector() failed: "
+ <<e.what()<<std::endl;
+ dstream<<"Path to failed sector: "<<getSectorDir(p2d)
+ <<std::endl
+ <<"You could try to delete it."<<std::endl;
+ throw e;
+ }*/
+
+ /*
+ Try to get a block from the sector
+ */
+
+ bool does_not_exist = false;
+ bool lighting_expired = false;
+ MapBlock *block = sector->getBlockNoCreateNoEx(block_y);
+
+ if(block == NULL)
+ {
+ does_not_exist = true;
+ }
+ else if(block->isDummy() == true)
+ {
+ does_not_exist = true;
+ }
+ else if(block->getLightingExpired())
+ {
+ lighting_expired = true;
+ }
+ else
+ {
+ // Valid block
+ //dstream<<"emergeBlock(): Returning already valid block"<<std::endl;
+ return block;
+ }
+
+ /*
+ If block was not found on disk and not going to generate a
+ new one, make sure there is a dummy block in place.
+ */
+ if(only_from_disk && (does_not_exist || lighting_expired))
+ {
+ //dstream<<"emergeBlock(): Was not on disk but not generating"<<std::endl;
+
+ if(block == NULL)
+ {
+ // Create dummy block
+ block = new MapBlock(this, p, true);
+
+ // Add block to sector
+ sector->insertBlock(block);
+ }
+ // Done.
+ return block;
+ }
+
+ //dstream<<"Not found on disk, generating."<<std::endl;
+ // 0ms
+ //TimeTaker("emergeBlock() generate");
+
+ //dstream<<"emergeBlock(): Didn't find valid block -> making one"<<std::endl;
+
+ /*
+ If the block doesn't exist, generate the block.
+ */
+ if(does_not_exist)
+ {
+ block = generateBlock(p, block, sector, changed_blocks,
+ lighting_invalidated_blocks);
+ }
+
+ if(lighting_expired)
+ {
+ lighting_invalidated_blocks.insert(p, block);
+ }
+
+ /*
+ Initially update sunlight
+ */
+
+ {
+ core::map<v3s16, bool> light_sources;
+ bool black_air_left = false;
+ bool bottom_invalid =
+ block->propagateSunlight(light_sources, true,
+ &black_air_left, true);
+
+ // If sunlight didn't reach everywhere and part of block is
+ // above ground, lighting has to be properly updated
+ //if(black_air_left && some_part_underground)
+ if(black_air_left)
+ {
+ lighting_invalidated_blocks[block->getPos()] = block;
+ }
+
+ if(bottom_invalid)
+ {
+ lighting_invalidated_blocks[block->getPos()] = block;
+ }
+ }
+
+ return block;
+}
+
+s16 ServerMap::findGroundLevel(v2s16 p2d)
+{
+ /*
+ Uh, just do something random...
+ */
+ // Find existing map from top to down
+ s16 max=63;
+ s16 min=-64;
+ v3s16 p(p2d.X, max, p2d.Y);
+ for(; p.Y>min; p.Y--)
+ {
+ MapNode n = getNodeNoEx(p);
+ if(n.d != CONTENT_IGNORE)
+ break;
+ }
+ if(p.Y == min)
+ goto plan_b;
+ // If this node is not air, go to plan b
+ if(getNodeNoEx(p).d != CONTENT_AIR)
+ goto plan_b;
+ // Search existing walkable and return it
+ for(; p.Y>min; p.Y--)
+ {
+ MapNode n = getNodeNoEx(p);
+ if(content_walkable(n.d) && n.d != CONTENT_IGNORE)
+ return p.Y;
+ }
+ // Move to plan b
+plan_b:
+ /*
+ Plan B: Get from map generator perlin noise function
+ */
+ double level = base_rock_level_2d(m_seed, p2d);
+ return (s16)level;
+}
+
+void ServerMap::createDir(std::string path)
+{
+ if(fs::CreateDir(path) == false)
+ {
+ m_dout<<DTIME<<"ServerMap: Failed to create directory "
+ <<"\""<<path<<"\""<<std::endl;
+ throw BaseException("ServerMap failed to create directory");
+ }
+}
+
+std::string ServerMap::getSectorSubDir(v2s16 pos)
+{
+ char cc[9];
+ snprintf(cc, 9, "%.4x%.4x",
+ (unsigned int)pos.X&0xffff,
+ (unsigned int)pos.Y&0xffff);
+
+ return std::string(cc);
+}
+
+std::string ServerMap::getSectorDir(v2s16 pos)
+{
+ return m_savedir + "/sectors/" + getSectorSubDir(pos);
+}
+
+v2s16 ServerMap::getSectorPos(std::string dirname)
+{
+ if(dirname.size() != 8)
+ throw InvalidFilenameException("Invalid sector directory name");
+ unsigned int x, y;
+ int r = sscanf(dirname.c_str(), "%4x%4x", &x, &y);
+ if(r != 2)
+ throw InvalidFilenameException("Invalid sector directory name");
+ v2s16 pos((s16)x, (s16)y);
+ return pos;
+}
+
+v3s16 ServerMap::getBlockPos(std::string sectordir, std::string blockfile)
+{
+ v2s16 p2d = getSectorPos(sectordir);
+
+ if(blockfile.size() != 4){
+ throw InvalidFilenameException("Invalid block filename");
+ }
+ unsigned int y;
+ int r = sscanf(blockfile.c_str(), "%4x", &y);
+ if(r != 1)
+ throw InvalidFilenameException("Invalid block filename");
+ return v3s16(p2d.X, y, p2d.Y);
+}
+
+void ServerMap::save(bool only_changed)
+{
+ DSTACK(__FUNCTION_NAME);
+ if(m_map_saving_enabled == false)
+ {
+ dstream<<DTIME<<"WARNING: Not saving map, saving disabled."<<std::endl;
+ return;
+ }
+
+ if(only_changed == false)
+ dstream<<DTIME<<"ServerMap: Saving whole map, this can take time."
+ <<std::endl;
+
+ saveMapMeta();
+ saveChunkMeta();
+
+ u32 sector_meta_count = 0;
+ u32 block_count = 0;
+
+ { //sectorlock
+ JMutexAutoLock lock(m_sector_mutex);
+
+ core::map<v2s16, MapSector*>::Iterator i = m_sectors.getIterator();
+ for(; i.atEnd() == false; i++)
+ {
+ ServerMapSector *sector = (ServerMapSector*)i.getNode()->getValue();
+ assert(sector->getId() == MAPSECTOR_SERVER);
+
+ if(sector->differs_from_disk || only_changed == false)
+ {
+ saveSectorMeta(sector);
+ sector_meta_count++;
+ }
+ core::list<MapBlock*> blocks;
+ sector->getBlocks(blocks);
+ core::list<MapBlock*>::Iterator j;
+ for(j=blocks.begin(); j!=blocks.end(); j++)
+ {
+ MapBlock *block = *j;
+ if(block->getChangedFlag() || only_changed == false)
+ {
+ saveBlock(block);
+ block_count++;
+
+ /*dstream<<"ServerMap: Written block ("
+ <<block->getPos().X<<","
+ <<block->getPos().Y<<","
+ <<block->getPos().Z<<")"
+ <<std::endl;*/
+ }
+ }
+ }
+
+ }//sectorlock
+
+ /*
+ Only print if something happened or saved whole map
+ */
+ if(only_changed == false || sector_meta_count != 0
+ || block_count != 0)
+ {
+ dstream<<DTIME<<"ServerMap: Written: "
+ <<sector_meta_count<<" sector metadata files, "
+ <<block_count<<" block files"
+ <<std::endl;
+ }
+}
+
+void ServerMap::loadAll()
+{
+ DSTACK(__FUNCTION_NAME);
+ dstream<<DTIME<<"ServerMap: Loading map..."<<std::endl;
+
+ loadMapMeta();
+ loadChunkMeta();
+
+ std::vector<fs::DirListNode> list = fs::GetDirListing(m_savedir+"/sectors/");
+
+ dstream<<DTIME<<"There are "<<list.size()<<" sectors."<<std::endl;
+
+ JMutexAutoLock lock(m_sector_mutex);
+
+ s32 counter = 0;
+ s32 printed_counter = -100000;
+ s32 count = list.size();
+
+ std::vector<fs::DirListNode>::iterator i;
+ for(i=list.begin(); i!=list.end(); i++)
+ {
+ if(counter > printed_counter + 10)
+ {
+ dstream<<DTIME<<counter<<"/"<<count<<std::endl;
+ printed_counter = counter;
+ }
+ counter++;
+
+ MapSector *sector = NULL;
+
+ // We want directories
+ if(i->dir == false)
+ continue;
+ try{
+ sector = loadSectorMeta(i->name);
+ }
+ catch(InvalidFilenameException &e)
+ {
+ // This catches unknown crap in directory
+ }
+
+ std::vector<fs::DirListNode> list2 = fs::GetDirListing
+ (m_savedir+"/sectors/"+i->name);
+ std::vector<fs::DirListNode>::iterator i2;
+ for(i2=list2.begin(); i2!=list2.end(); i2++)
+ {
+ // We want files
+ if(i2->dir)
+ continue;
+ try{
+ loadBlock(i->name, i2->name, sector);
+ }
+ catch(InvalidFilenameException &e)
+ {
+ // This catches unknown crap in directory