/*
Minetest-c55
-Copyright (C) 2010 celeron55, Perttu Ahola <celeron55@gmail.com>
+Copyright (C) 2010-2011 celeron55, Perttu Ahola <celeron55@gmail.com>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
}
else
{
- // Turn mud into grass
+ /*// Turn mud into grass
if(n.d == CONTENT_MUD)
{
n.d = CONTENT_GRASS;
block->setNode(relpos, n);
modified_blocks.insert(blockpos, block);
- }
+ }*/
// Sunlight goes no further
break;
}
/*
- This is called after changing a node from transparent to opaque.
- The lighting value of the node should be left as-is after changing
- other values. This sets the lighting value to 0.
*/
void Map::addNodeAndUpdate(v3s16 p, MapNode n,
core::map<v3s16, MapBlock*> &modified_blocks)
catch(InvalidPositionException &e)
{
}
-
+
+#if 1
/*
- If the new node doesn't propagate sunlight and there is
- grass below, change it to mud
+ If the new node is solid and there is grass below, change it to mud
*/
- if(content_features(n.d).sunlight_propagates == false)
+ if(content_features(n.d).walkable == true)
{
try{
MapNode bottomnode = getNode(bottompos);
{
}
}
+#endif
+#if 0
/*
If the new node is mud and it is under sunlight, change it
to grass
{
n.d = CONTENT_GRASS;
}
+#endif
/*
Remove all light that has come out of this node
}
}
+void make_cactus(VoxelManipulator &vmanip, v3s16 p0)
+{
+ MapNode cactusnode(CONTENT_CACTUS);
+
+ s16 trunk_h = 3;
+ v3s16 p1 = p0;
+ for(s16 ii=0; ii<trunk_h; ii++)
+ {
+ if(vmanip.m_area.contains(p1))
+ vmanip.m_data[vmanip.m_area.index(p1)] = cactusnode;
+ p1.Y++;
+ }
+}
+
/*
Noise functions. Make sure seed is mangled differently in each one.
*/
u32 i = data->vmanip.m_area.index(v3s16(p2d.X, surface_y, p2d.Y));
MapNode *n = &data->vmanip.m_data[i];
if(n->d == CONTENT_GRASS)
- n->d = CONTENT_MUD;
+ *n = MapNode(CONTENT_MUD);
+ //n->d = CONTENT_MUD;
}
/*
break;
MapNode &n = data->vmanip.m_data[i];
- n.d = CONTENT_MUD;
+ n = MapNode(CONTENT_MUD);
+ //n.d = CONTENT_MUD;
mudcount++;
data->vmanip.m_area.add_y(em, i, 1);
if(have_sand == false)
continue;
+ // Determine whether to have clay in the sand here
+ double claynoise = noise2d_perlin(
+ 0.5+(float)p2d.X/500, 0.5+(float)p2d.Y/500,
+ data->seed+4321, 8, 0.95);
+
+ bool have_clay = have_sand && (claynoise > 0.95);
+
// Find ground level
s16 surface_y = find_ground_level_clever(data->vmanip, p2d);
MapNode *n = &data->vmanip.m_data[i];
if(n->d == CONTENT_MUD || n->d == CONTENT_GRASS)
{
- n->d = CONTENT_SAND;
+ if(have_clay && (surface_y == WATER_LEVEL))
+ n->d = CONTENT_CLAY;
+ else
+ n->d = CONTENT_SAND;
}
else
{
if(y > y_nodes_max - 6)
continue;
v3s16 p(x,y,z);
- /*
- Trees grow only on mud and grass
- */
{
u32 i = data->vmanip.m_area.index(v3s16(p));
MapNode *n = &data->vmanip.m_data[i];
- if(n->d != CONTENT_MUD && n->d != CONTENT_GRASS)
+ if(n->d != CONTENT_MUD && n->d != CONTENT_GRASS && n->d != CONTENT_SAND)
continue;
+ // Trees grow only on mud and grass
+ if(n->d == CONTENT_MUD || n->d == CONTENT_GRASS)
+ {
+ p.Y++;
+ make_tree(data->vmanip, p);
+ }
+ // Cactii grow only on sand
+ if(n->d == CONTENT_SAND)
+ {
+ p.Y++;
+ make_cactus(data->vmanip, p);
+ }
}
- p.Y++;
- // Make a tree
- make_tree(data->vmanip, p);
}
}
/*u32 tree_max = relative_area / 60;
sectorpos_base - v2s16(1,1) * max_spread_amount_sectors;
s16 sectorpos_bigbase_size =
sectorpos_base_size + 2 * max_spread_amount_sectors;
-
+
+ // Check limits
+ const s16 limit = MAP_GENERATION_LIMIT / MAP_BLOCKSIZE;
+ if(sectorpos_bigbase.X < -limit
+ || sectorpos_bigbase.X + sectorpos_bigbase_size >= limit
+ || sectorpos_bigbase.Y < -limit
+ || sectorpos_bigbase.Y + sectorpos_bigbase_size >= limit)
+ {
+ data.no_op = true;
+ return;
+ }
+
data.seed = m_seed;
data.chunkpos = chunkpos;
data.y_blocks_min = y_blocks_min;
ServerMapSector * ServerMap::createSector(v2s16 p2d)
{
- DSTACK("%s: p2d=(%d,%d)",
+ DSTACKF("%s: p2d=(%d,%d)",
__FUNCTION_NAME,
p2d.X, p2d.Y);
core::map<v3s16, MapBlock*> &lighting_invalidated_blocks
)
{
- DSTACK("%s: p=(%d,%d,%d)",
+ DSTACKF("%s: p=(%d,%d,%d)",
__FUNCTION_NAME,
p.X, p.Y, p.Z);
MapBlock * ServerMap::createBlock(v3s16 p)
{
- DSTACK("%s: p=(%d,%d,%d)",
+ DSTACKF("%s: p=(%d,%d,%d)",
__FUNCTION_NAME, p.X, p.Y, p.Z);
/*
core::map<v3s16, MapBlock*> &lighting_invalidated_blocks
)
{
- DSTACK("%s: p=(%d,%d,%d), only_from_disk=%d",
+ DSTACKF("%s: p=(%d,%d,%d), only_from_disk=%d",
__FUNCTION_NAME,
p.X, p.Y, p.Z, only_from_disk);
return (s16)level;
}
-void ServerMap::createDir(std::string path)
+void ServerMap::createDirs(std::string path)
{
- if(fs::CreateDir(path) == false)
+ if(fs::CreateAllDirs(path) == false)
{
m_dout<<DTIME<<"ServerMap: Failed to create directory "
<<"\""<<path<<"\""<<std::endl;
}
}
-std::string ServerMap::getSectorSubDir(v2s16 pos)
+std::string ServerMap::getSectorDir(v2s16 pos, int layout)
{
char cc[9];
- snprintf(cc, 9, "%.4x%.4x",
- (unsigned int)pos.X&0xffff,
- (unsigned int)pos.Y&0xffff);
+ switch(layout)
+ {
+ case 1:
+ snprintf(cc, 9, "%.4x%.4x",
+ (unsigned int)pos.X&0xffff,
+ (unsigned int)pos.Y&0xffff);
- return std::string(cc);
-}
+ return m_savedir + "/sectors/" + cc;
+ case 2:
+ snprintf(cc, 9, "%.3x/%.3x",
+ (unsigned int)pos.X&0xfff,
+ (unsigned int)pos.Y&0xfff);
-std::string ServerMap::getSectorDir(v2s16 pos)
-{
- return m_savedir + "/sectors/" + getSectorSubDir(pos);
+ return m_savedir + "/sectors2/" + cc;
+ default:
+ assert(false);
+ }
}
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");
+ int r;
+ size_t spos = dirname.rfind('/') + 1;
+ assert(spos != std::string::npos);
+ if(dirname.size() - spos == 8)
+ {
+ // Old layout
+ r = sscanf(dirname.substr(spos).c_str(), "%4x%4x", &x, &y);
+ }
+ else if(dirname.size() - spos == 3)
+ {
+ // New layout
+ r = sscanf(dirname.substr(spos-4).c_str(), "%3x/%3x", &x, &y);
+ // Sign-extend the 12 bit values up to 16 bits...
+ if(x&0x800) x|=0xF000;
+ if(y&0x800) y|=0xF000;
+ }
+ else
+ {
+ assert(false);
+ }
+ assert(r == 2);
v2s16 pos((s16)x, (s16)y);
return pos;
}
<<"seed="<<m_seed<<", chunksize="<<m_chunksize
<<std::endl;
- createDir(m_savedir);
+ createDirs(m_savedir);
std::string fullpath = m_savedir + "/map_meta.txt";
std::ofstream os(fullpath.c_str(), std::ios_base::binary);
dstream<<"INFO: ServerMap::saveChunkMeta(): Saving metadata of "
<<count<<" chunks"<<std::endl;
- createDir(m_savedir);
+ createDirs(m_savedir);
std::string fullpath = m_savedir + "/chunk_meta";
std::ofstream os(fullpath.c_str(), std::ios_base::binary);
u8 version = SER_FMT_VER_HIGHEST;
// Get destination
v2s16 pos = sector->getPos();
- createDir(m_savedir);
- createDir(m_savedir+"/sectors");
std::string dir = getSectorDir(pos);
- createDir(dir);
+ createDirs(dir);
std::string fullpath = dir + "/meta";
std::ofstream o(fullpath.c_str(), std::ios_base::binary);
sector->differs_from_disk = false;
}
-MapSector* ServerMap::loadSectorMeta(std::string dirname)
+MapSector* ServerMap::loadSectorMeta(std::string sectordir, bool save_after_load)
{
DSTACK(__FUNCTION_NAME);
// Get destination
- v2s16 p2d = getSectorPos(dirname);
- std::string dir = m_savedir + "/sectors/" + dirname;
+ v2s16 p2d = getSectorPos(sectordir);
ServerMapSector *sector = NULL;
-
- std::string fullpath = dir + "/meta";
+
+ std::string fullpath = sectordir + "/meta";
std::ifstream is(fullpath.c_str(), std::ios_base::binary);
if(is.good() == false)
{
// If the directory exists anyway, it probably is in some old
// format. Just go ahead and create the sector.
- if(fs::PathExists(dir))
+ if(fs::PathExists(sectordir))
{
dstream<<"ServerMap::loadSectorMeta(): Sector metafile "
<<fullpath<<" doesn't exist but directory does."
m_sectors.insert(p2d, sector);
}
else
+ {
throw FileNotGoodException("Cannot open sector metafile");
+ }
}
else
{
sector = ServerMapSector::deSerialize
(is, this, p2d, m_sectors);
+ if(save_after_load)
+ saveSectorMeta(sector);
}
sector->differs_from_disk = false;
bool ServerMap::loadSectorFull(v2s16 p2d)
{
DSTACK(__FUNCTION_NAME);
- std::string sectorsubdir = getSectorSubDir(p2d);
MapSector *sector = NULL;
+ // The directory layout we're going to load from.
+ // 1 - original sectors/xxxxzzzz/
+ // 2 - new sectors2/xxx/zzz/
+ // If we load from anything but the latest structure, we will
+ // immediately save to the new one, and remove the old.
+ int loadlayout = 1;
+ std::string sectordir1 = getSectorDir(p2d, 1);
+ std::string sectordir;
+ if(fs::PathExists(sectordir1))
+ {
+ sectordir = sectordir1;
+ }
+ else
+ {
+ loadlayout = 2;
+ sectordir = getSectorDir(p2d, 2);
+ }
+
//JMutexAutoLock lock(m_sector_mutex); // Bulk comment-out
try{
- sector = loadSectorMeta(sectorsubdir);
+ sector = loadSectorMeta(sectordir, loadlayout != 2);
}
catch(InvalidFilenameException &e)
{
Load blocks
*/
std::vector<fs::DirListNode> list2 = fs::GetDirListing
- (m_savedir+"/sectors/"+sectorsubdir);
+ (sectordir);
std::vector<fs::DirListNode>::iterator i2;
for(i2=list2.begin(); i2!=list2.end(); i2++)
{
if(i2->dir)
continue;
try{
- loadBlock(sectorsubdir, i2->name, sector);
+ loadBlock(sectordir, i2->name, sector, loadlayout != 2);
}
catch(InvalidFilenameException &e)
{
// This catches unknown crap in directory
}
}
+
+ if(loadlayout != 2)
+ {
+ dstream<<"Sector converted to new layout - deleting "<<
+ sectordir1<<std::endl;
+ fs::RecursiveDelete(sectordir1);
+ }
+
return true;
}
+
void ServerMap::saveBlock(MapBlock *block)
{
DSTACK(__FUNCTION_NAME);
// Get destination
v3s16 p3d = block->getPos();
v2s16 p2d(p3d.X, p3d.Z);
- createDir(m_savedir);
- createDir(m_savedir+"/sectors");
std::string dir = getSectorDir(p2d);
- createDir(dir);
+ createDirs(dir);
- // Block file is map/sectors/xxxxxxxx/xxxx
char cc[5];
snprintf(cc, 5, "%.4x", (unsigned int)p3d.Y&0xffff);
std::string fullpath = dir + "/" + cc;
*/
o.write((char*)&version, 1);
+ // Write basic data
block->serialize(o, version);
-
- /*
- Versions up from 9 have block objects.
- */
- if(version >= 9)
- {
- block->serializeObjects(o, version);
- }
-
- /*
- Versions up from 15 have static objects.
- */
- if(version >= 15)
- {
- block->m_static_objects.serialize(o);
- }
- // We just wrote it to the disk
+ // Write extra data stored on disk
+ block->serializeDiskExtra(o, version);
+
+ // We just wrote it to the disk so clear modified flag
block->resetChangedFlag();
}
-void ServerMap::loadBlock(std::string sectordir, std::string blockfile, MapSector *sector)
+void ServerMap::loadBlock(std::string sectordir, std::string blockfile, MapSector *sector, bool save_after_load)
{
DSTACK(__FUNCTION_NAME);
- // Block file is map/sectors/xxxxxxxx/xxxx
- std::string fullpath = m_savedir+"/sectors/"+sectordir+"/"+blockfile;
+ std::string fullpath = sectordir+"/"+blockfile;
try{
std::ifstream is(fullpath.c_str(), std::ios_base::binary);
created_new = true;
}
- // deserialize block data
+ // Read basic data
block->deSerialize(is, version);
-
- /*
- Versions up from 9 have block objects.
- */
- if(version >= 9)
- {
- block->updateObjects(is, version, NULL, 0);
- }
- /*
- Versions up from 15 have static objects.
- */
- if(version >= 15)
- {
- block->m_static_objects.deSerialize(is);
- }
+ // Read extra data stored on disk
+ block->deSerializeDiskExtra(is, version);
+ // If it's a new block, insert it to the map
if(created_new)
sector->insertBlock(block);
/*
- Convert old formats to new and save
+ Save blocks loaded in old format in new format
*/
- // Save old format blocks in new format
- if(version < SER_FMT_VER_HIGHEST)
+ if(version < SER_FMT_VER_HIGHEST || save_after_load)
{
saveBlock(block);
}