#include "filesys.h"
#include "voxel.h"
#include "porting.h"
-#include "mapgen.h"
+#include "serialization.h"
#include "nodemetadata.h"
#include "settings.h"
#include "log.h"
#include "nodedef.h"
#include "gamedef.h"
#include "util/directiontables.h"
+#include "util/mathconstants.h"
#include "rollback_interface.h"
+#include "environment.h"
#include "emerge.h"
#include "mapgen_v6.h"
-#include "mapgen_indev.h"
#include "biome.h"
+#include "config.h"
+#include "server.h"
+#include "database.h"
+#include "database-dummy.h"
+#include "database-sqlite3.h"
+#if USE_LEVELDB
+#include "database-leveldb.h"
+#endif
#define PP(x) "("<<(x).X<<","<<(x).Y<<","<<(x).Z<<")"
m_gamedef(gamedef),
m_sector_cache(NULL)
{
- /*m_sector_mutex.Init();
- assert(m_sector_mutex.IsInitialized());*/
}
Map::~Map()
/*
*/
void Map::addNodeAndUpdate(v3s16 p, MapNode n,
- std::map<v3s16, MapBlock*> &modified_blocks)
+ std::map<v3s16, MapBlock*> &modified_blocks,
+ bool remove_metadata)
{
INodeDefManager *ndef = m_gamedef->ndef();
/*
Remove node metadata
*/
-
- removeNodeMetadata(p);
+ if (remove_metadata) {
+ removeNodeMetadata(p);
+ }
/*
Set the node on the map
}
}
-bool Map::addNodeWithEvent(v3s16 p, MapNode n)
+bool Map::addNodeWithEvent(v3s16 p, MapNode n, bool remove_metadata)
{
MapEditEvent event;
- event.type = MEET_ADDNODE;
+ event.type = remove_metadata ? MEET_ADDNODE : MEET_SWAPNODE;
event.p = p;
event.n = n;
bool succeeded = true;
try{
std::map<v3s16, MapBlock*> modified_blocks;
- addNodeAndUpdate(p, n, modified_blocks);
+ addNodeAndUpdate(p, n, modified_blocks, remove_metadata);
// Copy modified_blocks to event
for(std::map<v3s16, MapBlock*>::iterator
v3s16 p0 = m_transforming_liquid.pop_front();
u16 total_level = 0;
// surrounding flowing liquid nodes
- NodeNeighbor neighbors[7];
+ NodeNeighbor neighbors[7];
// current level of every block
s8 liquid_levels[7] = {-1, -1, -1, -1, -1, -1, -1};
// target levels
if (liquid_kind == CONTENT_IGNORE)
liquid_kind = nb.n.getContent();
if (nb.n.getContent() == liquid_kind) {
- liquid_levels[i] = LIQUID_LEVEL_SOURCE;
+ liquid_levels[i] = nb.n.getLevel(nodemgr); //LIQUID_LEVEL_SOURCE;
nb.l = 1;
nb.i = (nb.n.param2 & LIQUID_INFINITY_MASK);
}
liquid_kind = nodemgr->getId(
nodemgr->get(nb.n).liquid_alternative_source);
if (nb.n.getContent() == liquid_kind_flowing) {
- liquid_levels[i] = (nb.n.param2 & LIQUID_LEVEL_MASK);
+ liquid_levels[i] = nb.n.getLevel(nodemgr); //(nb.n.param2 & LIQUID_LEVEL_MASK);
nb.l = 1;
}
break;
liquid_levels[D_BOTTOM] == LIQUID_LEVEL_SOURCE &&
total_level >= LIQUID_LEVEL_SOURCE * can_liquid_same_level-
(can_liquid_same_level - relax) &&
- can_liquid_same_level >= relax + 1) {
- total_level = LIQUID_LEVEL_SOURCE * can_liquid_same_level;
+ can_liquid_same_level >= relax + 1) {
+ total_level = LIQUID_LEVEL_SOURCE * can_liquid_same_level;
}
// prevent lakes in air above unloaded blocks
}
// calculate self level 5 blocks
- u8 want_level =
+ u8 want_level =
total_level >= LIQUID_LEVEL_SOURCE * can_liquid_same_level
- ? LIQUID_LEVEL_SOURCE
+ ? LIQUID_LEVEL_SOURCE
: total_level / can_liquid_same_level;
total_level -= want_level * can_liquid_same_level;
/*
if (total_level > 0) //|| flowed != volume)
- infostream <<" AFTER level=" << (int)total_level
+ infostream <<" AFTER level=" << (int)total_level
//<< " flowed="<<flowed<< " volume=" << volume
<< " wantsame="<<(int)want_level<< " top="
<< (int)liquid_levels_want[D_TOP]<< " topwas="
<< (int)liquid_levels_want[D_BOTTOM]<<std::endl;
*/
- u8 changed = 0;
+ //u8 changed = 0;
for (u16 i = 0; i < 7; i++) {
- if (liquid_levels_want[i] < 0 || !neighbors[i].l)
+ if (liquid_levels_want[i] < 0 || !neighbors[i].l)
continue;
MapNode & n0 = neighbors[i].n;
p0 = neighbors[i].p;
new_node_content = liquid_kind_flowing;
else
new_node_content = CONTENT_AIR;
-
+
// last level must flow down on stairs
if (liquid_levels_want[i] != liquid_levels[i] &&
liquid_levels[D_TOP] <= 0 && !neighbors[D_BOTTOM].l &&
check if anything has changed.
if not, just continue with the next node.
*/
+ /*
if (
- new_node_content == n0.getContent()
+ new_node_content == n0.getContent()
&& (nodemgr->get(n0.getContent()).liquid_type != LIQUID_FLOWING ||
- ((n0.param2 & LIQUID_LEVEL_MASK) == (u8)new_node_level
+ (n0.getLevel(nodemgr) == (u8)new_node_level
//&& ((n0.param2 & LIQUID_FLOW_DOWN_MASK) ==
//LIQUID_FLOW_DOWN_MASK) == flowing_down
))
(((n0.param2 & LIQUID_INFINITY_MASK) ==
LIQUID_INFINITY_MASK) == neighbors[i].i
))
- ) {
+ )*/
+ if (liquid_levels[i] == new_node_level)
+ {
continue;
}
- ++changed;
+
+ //++changed;
/*
update the current node
*/
+ /*
if (nodemgr->get(new_node_content).liquid_type == LIQUID_FLOWING) {
// set level to last 3 bits, flowing down bit to 4th bit
n0.param2 = (new_node_level & LIQUID_LEVEL_MASK);
//n0.param2 = ~(LIQUID_LEVEL_MASK | LIQUID_FLOW_DOWN_MASK);
n0.param2 = (neighbors[i].i ? LIQUID_INFINITY_MASK : 0x00);
}
+ */
/*
infostream << "set node i=" <<(int)i<<" "<< PP(p0)<< " nc="
<<new_node_content<< " p2="<<(int)n0.param2<< " nl="
<<(int)new_node_level<<std::endl;
*/
- n0.setContent(new_node_content);
+ n0.setContent(liquid_kind_flowing);
+ n0.setLevel(nodemgr, new_node_level);
// Find out whether there is a suspect for this action
std::string suspect;
if(m_gamedef->rollback()){
content_t new_node_content;
s8 new_node_level = -1;
s8 max_node_level = -1;
+ u8 range = rangelim(nodemgr->get(liquid_kind).liquid_range, 0, LIQUID_LEVEL_MAX+1);
if ((num_sources >= 2 && nodemgr->get(liquid_kind).liquid_renewable) || liquid_type == LIQUID_SOURCE) {
// liquid_kind will be set to either the flowing alternative of the node (if it's a liquid)
// or the flowing alternative of the first of the surrounding sources (if it's air), so
// liquid_kind is set properly, see above
new_node_content = liquid_kind;
max_node_level = new_node_level = LIQUID_LEVEL_MAX;
+ if (new_node_level < (LIQUID_LEVEL_MAX+1-range))
+ new_node_content = CONTENT_AIR;
} else {
// no surrounding sources, so get the maximum level that can flow into this node
for (u16 i = 0; i < num_flows; i++) {
} else
new_node_level = max_node_level;
- u8 range = rangelim(nodemgr->get(liquid_kind).liquid_range, 0, LIQUID_LEVEL_MAX+1);
- if (new_node_level >= (LIQUID_LEVEL_MAX+1-range))
+ if (max_node_level >= (LIQUID_LEVEL_MAX+1-range))
new_node_content = liquid_kind;
else
new_node_content = CONTENT_AIR;
updateLighting(lighting_modified_blocks, modified_blocks);
}
-NodeMetadata* Map::getNodeMetadata(v3s16 p)
+NodeMetadata *Map::getNodeMetadata(v3s16 p)
{
v3s16 blockpos = getNodeBlockPos(p);
v3s16 p_rel = p - blockpos*MAP_BLOCKSIZE;
<<PP(blockpos)<<std::endl;
block = emergeBlock(blockpos, false);
}
- if(!block)
- {
+ if(!block){
infostream<<"WARNING: Map::getNodeMetadata(): Block not found"
<<std::endl;
return NULL;
return meta;
}
-void Map::setNodeMetadata(v3s16 p, NodeMetadata *meta)
+bool Map::setNodeMetadata(v3s16 p, NodeMetadata *meta)
{
v3s16 blockpos = getNodeBlockPos(p);
v3s16 p_rel = p - blockpos*MAP_BLOCKSIZE;
<<PP(blockpos)<<std::endl;
block = emergeBlock(blockpos, false);
}
- if(!block)
- {
+ if(!block){
infostream<<"WARNING: Map::setNodeMetadata(): Block not found"
<<std::endl;
- return;
+ return false;
}
block->m_node_metadata.set(p_rel, meta);
+ return true;
}
void Map::removeNodeMetadata(v3s16 p)
<<PP(blockpos)<<std::endl;
block = emergeBlock(blockpos, false);
}
- if(!block)
- {
+ if(!block){
infostream<<"WARNING: Map::getNodeTimer(): Block not found"
<<std::endl;
return NodeTimer();
<<PP(blockpos)<<std::endl;
block = emergeBlock(blockpos, false);
}
- if(!block)
- {
+ if(!block){
infostream<<"WARNING: Map::setNodeTimer(): Block not found"
<<std::endl;
return;
ServerMap::ServerMap(std::string savedir, IGameDef *gamedef, EmergeManager *emerge):
Map(dout_server, gamedef),
m_seed(0),
- m_map_metadata_changed(true),
- m_database(NULL),
- m_database_read(NULL),
- m_database_write(NULL)
+ m_map_metadata_changed(true)
{
verbosestream<<__FUNCTION_NAME<<std::endl;
Try to load map; if not found, create a new one.
*/
+ // Determine which database backend to use
+ std::string conf_path = savedir + DIR_DELIM + "world.mt";
+ Settings conf;
+ bool succeeded = conf.readConfigFile(conf_path.c_str());
+ if (!succeeded || !conf.exists("backend")) {
+ // fall back to sqlite3
+ dbase = new Database_SQLite3(this, savedir);
+ conf.set("backend", "sqlite3");
+ } else {
+ std::string backend = conf.get("backend");
+ if (backend == "dummy")
+ dbase = new Database_Dummy(this);
+ else if (backend == "sqlite3")
+ dbase = new Database_SQLite3(this, savedir);
+ #if USE_LEVELDB
+ else if (backend == "leveldb")
+ dbase = new Database_LevelDB(this, savedir);
+ #endif
+ else
+ throw BaseException("Unknown map backend");
+ }
+
m_savedir = savedir;
m_map_saving_enabled = false;
/*
Close database if it was opened
*/
- if(m_database_read)
- sqlite3_finalize(m_database_read);
- if(m_database_write)
- sqlite3_finalize(m_database_write);
- if(m_database)
- sqlite3_close(m_database);
+ delete(dbase);
#if 0
/*
/*infostream<<"finishBlockMake() done for ("<<blockpos_requested.X
<<","<<blockpos_requested.Y<<","
<<blockpos_requested.Z<<")"<<std::endl;*/
+
+ /*
+ Update weather data in blocks
+ */
+ ServerEnvironment *senv = &((Server *)m_gamedef)->getEnv();
+ for(s16 x=blockpos_min.X-extra_borders.X;
+ x<=blockpos_max.X+extra_borders.X; x++)
+ for(s16 z=blockpos_min.Z-extra_borders.Z;
+ z<=blockpos_max.Z+extra_borders.Z; z++)
+ for(s16 y=blockpos_min.Y-extra_borders.Y;
+ y<=blockpos_max.Y+extra_borders.Y; y++)
+ {
+ v3s16 p(x, y, z);
+ MapBlock *block = getBlockNoCreateNoEx(p);
+ block->heat_last_update = 0;
+ block->humidity_last_update = 0;
+ if (senv->m_use_weather) {
+ updateBlockHeat(senv, p * MAP_BLOCKSIZE, block);
+ updateBlockHumidity(senv, p * MAP_BLOCKSIZE, block);
+ } else {
+ block->heat = HEAT_UNDEFINED;
+ block->humidity = HUMIDITY_UNDEFINED;
+ }
+ }
+
#if 0
if(enable_mapgen_debug_info)
{
return NULL;
}
-s16 ServerMap::findGroundLevel(v2s16 p2d)
-{
-#if 0
- /*
- 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.getContent() != CONTENT_IGNORE)
- break;
- }
- if(p.Y == min)
- goto plan_b;
- // If this node is not air, go to plan b
- if(getNodeNoEx(p).getContent() != 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.getContent() != CONTENT_IGNORE)
- return p.Y;
- }
-
- // Move to plan b
-plan_b:
-#endif
-
- /*
- Determine from map generator noise functions
- */
-
- s16 level = m_emerge->getGroundLevelAtPoint(p2d);
- return level;
+void ServerMap::prepareBlock(MapBlock *block) {
+ ServerEnvironment *senv = &((Server *)m_gamedef)->getEnv();
- //double level = base_rock_level_2d(m_seed, p2d) + AVERAGE_MUD_AMOUNT;
- //return (s16)level;
-}
-
-void ServerMap::createDatabase() {
- int e;
- assert(m_database);
- e = sqlite3_exec(m_database,
- "CREATE TABLE IF NOT EXISTS `blocks` ("
- "`pos` INT NOT NULL PRIMARY KEY,"
- "`data` BLOB"
- ");"
- , NULL, NULL, NULL);
- if(e == SQLITE_ABORT)
- throw FileNotGoodException("Could not create database structure");
- else
- infostream<<"ServerMap: Database structure was created";
+ // Calculate weather conditions
+ block->heat_last_update = 0;
+ block->humidity_last_update = 0;
+ if (senv->m_use_weather) {
+ v3s16 p = block->getPos() * MAP_BLOCKSIZE;
+ updateBlockHeat(senv, p, block);
+ updateBlockHumidity(senv, p, block);
+ } else {
+ block->heat = HEAT_UNDEFINED;
+ block->humidity = HUMIDITY_UNDEFINED;
+ }
}
-void ServerMap::verifyDatabase() {
- if(m_database)
- return;
-
- {
- std::string dbp = m_savedir + DIR_DELIM + "map.sqlite";
- bool needs_create = false;
- int d;
-
- /*
- Open the database connection
- */
-
- createDirs(m_savedir);
-
- if(!fs::PathExists(dbp))
- needs_create = true;
-
- d = sqlite3_open_v2(dbp.c_str(), &m_database, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL);
- if(d != SQLITE_OK) {
- infostream<<"WARNING: Database failed to open: "<<sqlite3_errmsg(m_database)<<std::endl;
- throw FileNotGoodException("Cannot open database file");
- }
+/**
+ * Get the ground level by searching for a non CONTENT_AIR node in a column from top to bottom
+ */
+s16 ServerMap::findGroundLevel(v2s16 p2d, bool cacheBlocks)
+{
+
+ s16 level;
- if(needs_create)
- createDatabase();
+ // The reference height is the original mapgen height
+ s16 referenceHeight = m_emerge->getGroundLevelAtPoint(p2d);
+ s16 maxSearchHeight = 63 + referenceHeight;
+ s16 minSearchHeight = -63 + referenceHeight;
+ v3s16 probePosition(p2d.X, maxSearchHeight, p2d.Y);
+ v3s16 blockPosition = getNodeBlockPos(probePosition);
+ v3s16 prevBlockPosition = blockPosition;
- d = sqlite3_prepare(m_database, "SELECT `data` FROM `blocks` WHERE `pos`=? LIMIT 1", -1, &m_database_read, NULL);
- if(d != SQLITE_OK) {
- infostream<<"WARNING: Database read statment failed to prepare: "<<sqlite3_errmsg(m_database)<<std::endl;
- throw FileNotGoodException("Cannot prepare read statement");
- }
+ // Cache the block to be inspected.
+ if(cacheBlocks) {
+ emergeBlock(blockPosition, true);
+ }
- d = sqlite3_prepare(m_database, "REPLACE INTO `blocks` VALUES(?, ?)", -1, &m_database_write, NULL);
- if(d != SQLITE_OK) {
- infostream<<"WARNING: Database write statment failed to prepare: "<<sqlite3_errmsg(m_database)<<std::endl;
- throw FileNotGoodException("Cannot prepare write statement");
+ // Probes the nodes in the given column
+ for(; probePosition.Y > minSearchHeight; probePosition.Y--)
+ {
+ if(cacheBlocks) {
+ // Calculate the block position of the given node
+ blockPosition = getNodeBlockPos(probePosition);
+
+ // If the node is in an different block, cache it
+ if(blockPosition != prevBlockPosition) {
+ emergeBlock(blockPosition, true);
+ prevBlockPosition = blockPosition;
+ }
}
- d = sqlite3_prepare(m_database, "SELECT `pos` FROM `blocks`", -1, &m_database_list, NULL);
- if(d != SQLITE_OK) {
- infostream<<"WARNING: Database list statment failed to prepare: "<<sqlite3_errmsg(m_database)<<std::endl;
- throw FileNotGoodException("Cannot prepare read statement");
+ MapNode node = getNodeNoEx(probePosition);
+ if (node.getContent() != CONTENT_IGNORE &&
+ node.getContent() != CONTENT_AIR) {
+ break;
}
+ }
- infostream<<"ServerMap: Database opened"<<std::endl;
+ // Could not determine the ground. Use map generator noise functions.
+ if(probePosition.Y == minSearchHeight) {
+ level = referenceHeight;
+ } else {
+ level = probePosition.Y;
}
+
+ return level;
}
bool ServerMap::loadFromFolders() {
- if(!m_database && !fs::PathExists(m_savedir + DIR_DELIM + "map.sqlite"))
+ if(!dbase->Initialized() && !fs::PathExists(m_savedir + DIR_DELIM + "map.sqlite")) // ?
return true;
return false;
}
-sqlite3_int64 ServerMap::getBlockAsInteger(const v3s16 pos) {
- return (sqlite3_int64)pos.Z*16777216 +
- (sqlite3_int64)pos.Y*4096 + (sqlite3_int64)pos.X;
-}
-
void ServerMap::createDirs(std::string path)
{
if(fs::CreateAllDirs(path) == false)
}
}
-static s32 unsignedToSigned(s32 i, s32 max_positive)
-{
- if(i < max_positive)
- return i;
- else
- return i - 2*max_positive;
-}
-
-// modulo of a negative number does not work consistently in C
-static sqlite3_int64 pythonmodulo(sqlite3_int64 i, sqlite3_int64 mod)
-{
- if(i >= 0)
- return i % mod;
- return mod - ((-i) % mod);
-}
-
-v3s16 ServerMap::getIntegerAsBlock(sqlite3_int64 i)
-{
- s32 x = unsignedToSigned(pythonmodulo(i, 4096), 2048);
- i = (i - x) / 4096;
- s32 y = unsignedToSigned(pythonmodulo(i, 4096), 2048);
- i = (i - y) / 4096;
- s32 z = unsignedToSigned(pythonmodulo(i, 4096), 2048);
- return v3s16(x,y,z);
-}
-
void ServerMap::listAllLoadableBlocks(std::list<v3s16> &dst)
{
if(loadFromFolders()){
errorstream<<"Map::listAllLoadableBlocks(): Result will be missing "
<<"all blocks that are stored in flat files"<<std::endl;
}
-
- {
- verifyDatabase();
-
- while(sqlite3_step(m_database_list) == SQLITE_ROW)
- {
- sqlite3_int64 block_i = sqlite3_column_int64(m_database_list, 0);
- v3s16 p = getIntegerAsBlock(block_i);
- //dstream<<"block_i="<<block_i<<" p="<<PP(p)<<std::endl;
- dst.push_back(p);
- }
- }
+ dbase->listAllLoadableBlocks(dst);
}
void ServerMap::listAllLoadedBlocks(std::list<v3s16> &dst)
createDirs(m_savedir);
std::string fullpath = m_savedir + DIR_DELIM + "map_meta.txt";
- std::ofstream os(fullpath.c_str(), std::ios_base::binary);
- if(os.good() == false)
- {
- infostream<<"ERROR: ServerMap::saveMapMeta(): "
- <<"could not open"<<fullpath<<std::endl;
- throw FileNotGoodException("Cannot open chunk metadata");
- }
+ std::ostringstream ss(std::ios_base::binary);
Settings params;
m_emerge->setParamsToSettings(¶ms);
- params.writeLines(os);
+ params.writeLines(ss);
- os<<"[end_of_params]\n";
+ ss<<"[end_of_params]\n";
+
+ if(!fs::safeWriteToFile(fullpath, ss.str()))
+ {
+ infostream<<"ERROR: ServerMap::saveMapMeta(): "
+ <<"could not write "<<fullpath<<std::endl;
+ throw FileNotGoodException("Cannot save chunk metadata");
+ }
m_map_metadata_changed = false;
}
m_seed = mgparams->seed;
} else {
if (params.exists("seed")) {
- m_seed = params.getU64("seed");
+ m_seed = read_seed(params.get("seed").c_str());
m_mgparams->seed = m_seed;
}
}
{
DSTACK(__FUNCTION_NAME);
// Format used for writing
- u8 version = SER_FMT_VER_HIGHEST;
+ u8 version = SER_FMT_VER_HIGHEST_WRITE;
// Get destination
v2s16 pos = sector->getPos();
std::string dir = getSectorDir(pos);
createDirs(dir);
std::string fullpath = dir + DIR_DELIM + "meta";
- std::ofstream o(fullpath.c_str(), std::ios_base::binary);
- if(o.good() == false)
- throw FileNotGoodException("Cannot open sector metafile");
+ std::ostringstream ss(std::ios_base::binary);
+
+ sector->serialize(ss, version);
- sector->serialize(o, version);
+ if(!fs::safeWriteToFile(fullpath, ss.str()))
+ throw FileNotGoodException("Cannot write sector metafile");
sector->differs_from_disk = false;
}
#endif
void ServerMap::beginSave() {
- verifyDatabase();
- if(sqlite3_exec(m_database, "BEGIN;", NULL, NULL, NULL) != SQLITE_OK)
- infostream<<"WARNING: beginSave() failed, saving might be slow.";
+ dbase->beginSave();
}
void ServerMap::endSave() {
- verifyDatabase();
- if(sqlite3_exec(m_database, "COMMIT;", NULL, NULL, NULL) != SQLITE_OK)
- infostream<<"WARNING: endSave() failed, map might not have saved.";
+ dbase->endSave();
}
void ServerMap::saveBlock(MapBlock *block)
{
- DSTACK(__FUNCTION_NAME);
- /*
- Dummy blocks are not written
- */
- if(block->isDummy())
- {
- /*v3s16 p = block->getPos();
- infostream<<"ServerMap::saveBlock(): WARNING: Not writing dummy block "
- <<"("<<p.X<<","<<p.Y<<","<<p.Z<<")"<<std::endl;*/
- return;
- }
-
- // Format used for writing
- u8 version = SER_FMT_VER_HIGHEST;
- // Get destination
- v3s16 p3d = block->getPos();
-
-
-#if 0
- v2s16 p2d(p3d.X, p3d.Z);
- std::string sectordir = getSectorDir(p2d);
-
- createDirs(sectordir);
-
- std::string fullpath = sectordir+DIR_DELIM+getBlockFilename(p3d);
- std::ofstream o(fullpath.c_str(), std::ios_base::binary);
- if(o.good() == false)
- throw FileNotGoodException("Cannot open block data");
-#endif
- /*
- [0] u8 serialization version
- [1] data
- */
-
- verifyDatabase();
-
- std::ostringstream o(std::ios_base::binary);
-
- o.write((char*)&version, 1);
-
- // Write basic data
- block->serialize(o, version, true);
-
- // Write block to database
-
- std::string tmp = o.str();
- const char *bytes = tmp.c_str();
-
- bool success = true;
- if(sqlite3_bind_int64(m_database_write, 1, getBlockAsInteger(p3d)) != SQLITE_OK) {
- infostream<<"WARNING: Block position failed to bind: "<<sqlite3_errmsg(m_database)<<std::endl;
- success = false;
- }
- if(sqlite3_bind_blob(m_database_write, 2, (void *)bytes, o.tellp(), NULL) != SQLITE_OK) { // TODO this mught not be the right length
- infostream<<"WARNING: Block data failed to bind: "<<sqlite3_errmsg(m_database)<<std::endl;
- success = false;
- }
- int written = sqlite3_step(m_database_write);
- if(written != SQLITE_DONE) {
- errorstream<<"WARNING: Block failed to save ("<<p3d.X<<", "<<p3d.Y<<", "<<p3d.Z<<") "
- <<sqlite3_errmsg(m_database)<<std::endl;
- success = false;
- }
- // Make ready for later reuse
- sqlite3_reset(m_database_write);
-
- // We just wrote it to the disk so clear modified flag
- if (success)
- block->resetModified();
+ dbase->saveBlock(block);
}
void ServerMap::loadBlock(std::string sectordir, std::string blockfile, MapSector *sector, bool save_after_load)
Save blocks loaded in old format in new format
*/
- if(version < SER_FMT_VER_HIGHEST || save_after_load)
+ if(version < SER_FMT_VER_HIGHEST_WRITE || save_after_load)
{
saveBlock(block);
Save blocks loaded in old format in new format
*/
- //if(version < SER_FMT_VER_HIGHEST || save_after_load)
+ //if(version < SER_FMT_VER_HIGHEST_READ || save_after_load)
// Only save if asked to; no need to update version
if(save_after_load)
saveBlock(block);
v2s16 p2d(blockpos.X, blockpos.Z);
- if(!loadFromFolders()) {
- verifyDatabase();
+ MapBlock *ret;
- if(sqlite3_bind_int64(m_database_read, 1, getBlockAsInteger(blockpos)) != SQLITE_OK)
- infostream<<"WARNING: Could not bind block position for load: "
- <<sqlite3_errmsg(m_database)<<std::endl;
- if(sqlite3_step(m_database_read) == SQLITE_ROW) {
- /*
- Make sure sector is loaded
- */
- MapSector *sector = createSector(p2d);
-
- /*
- Load block
- */
- const char * data = (const char *)sqlite3_column_blob(m_database_read, 0);
- size_t len = sqlite3_column_bytes(m_database_read, 0);
-
- std::string datastr(data, len);
-
- loadBlock(&datastr, blockpos, sector, false);
-
- sqlite3_step(m_database_read);
- // We should never get more than 1 row, so ok to reset
- sqlite3_reset(m_database_read);
-
- return getBlockNoCreateNoEx(blockpos);
- }
- sqlite3_reset(m_database_read);
-
- // Not found in database, try the files
- }
+ ret = dbase->loadBlock(blockpos);
+ if (ret) return (ret);
+ // Not found in database, try the files
// The directory layout we're going to load from.
// 1 - original sectors/xxxxzzzz/
out<<"ServerMap: ";
}
-s16 ServerMap::getHeat(ServerEnvironment *env, v3s16 p, MapBlock *block)
+s16 ServerMap::updateBlockHeat(ServerEnvironment *env, v3s16 p, MapBlock *block)
{
- if(block == NULL)
- block = getBlockNoCreateNoEx(getNodeBlockPos(p));
- if(block != NULL) {
- if (env->getGameTime() - block->heat_time < 10)
+ u32 gametime = env->getGameTime();
+
+ if (block) {
+ if (gametime - block->heat_last_update < 10)
return block->heat;
+ } else {
+ block = getBlockNoCreateNoEx(getNodeBlockPos(p));
}
- //variant 1: full random
- //f32 heat = NoisePerlin3D(m_emerge->biomedef->np_heat, p.X, env->getGameTime()/100, p.Z, m_emerge->params->seed);
-
- //variant 2: season change based on default heat map
- f32 heat = NoisePerlin2D(m_emerge->biomedef->np_heat, p.X, p.Z, m_emerge->params->seed);
- heat += -30; // -30 - todo REMOVE after fixed NoiseParams nparams_biome_def_heat = {50, 50, -> 20, 50,
- f32 base = (f32)env->getGameTime() * env->getTimeOfDaySpeed();
- base /= ( 86400 * g_settings->getS16("year_days") );
- base += (f32)p.X / 3000;
- heat += 30 * sin(base * M_PI); // season
-
- heat += p.Y / -333; // upper=colder, lower=hotter
+ f32 heat = m_emerge->biomedef->calcBlockHeat(p, m_seed,
+ env->getTimeOfDayF(), gametime * env->getTimeOfDaySpeed());
- // daily change, hotter at sun +4, colder at night -4
- heat += 8 * (sin(cycle_shift(env->getTimeOfDayF(), -0.25) * M_PI) - 0.5);
-
- if(block != NULL) {
+ if(block) {
block->heat = heat;
- block->heat_time = env->getGameTime();
+ block->heat_last_update = gametime;
}
return heat;
}
-s16 ServerMap::getHumidity(ServerEnvironment *env, v3s16 p, MapBlock *block)
+s16 ServerMap::updateBlockHumidity(ServerEnvironment *env, v3s16 p, MapBlock *block)
{
- if(block == NULL)
- block = getBlockNoCreateNoEx(getNodeBlockPos(p));
- if(block != NULL) {
- if (env->getGameTime() - block->humidity_time < 10)
+ u32 gametime = env->getGameTime();
+
+ if (block) {
+ if (gametime - block->humidity_last_update < 10)
return block->humidity;
+ } else {
+ block = getBlockNoCreateNoEx(getNodeBlockPos(p));
}
- f32 humidity = NoisePerlin3D( m_emerge->biomedef->np_humidity,
- p.X, env->getGameTime()/10, p.Z,
- m_emerge->params->seed);
- humidity += -12 * ( sin(cycle_shift(env->getTimeOfDayF(), -0.1) * M_PI) - 0.5);
- //todo like heat//humidity += 20 * ( sin(((f32)p.Z / 300) * M_PI) - 0.5);
-
- if (humidity > 100) humidity = 100;
- if (humidity < 0) humidity = 0;
-
- if(block != NULL) {
+ f32 humidity = m_emerge->biomedef->calcBlockHumidity(p, m_seed,
+ env->getTimeOfDayF(), gametime * env->getTimeOfDaySpeed());
+
+ if(block) {
block->humidity = humidity;
- block->humidity_time = env->getGameTime();
+ block->humidity_last_update = gametime;
}
return humidity;
}