/*
-Minetest-c55
-Copyright (C) 2010 celeron55, Perttu Ahola <celeron55@gmail.com>
+Minetest
+Copyright (C) 2013 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 Lesser General Public License as published by
#include <sstream>
#include "map.h"
-// For g_settings
-#include "main.h"
#include "light.h"
#include "nodedef.h"
#include "nodemetadata.h"
#include "nameidmapping.h"
#include "content_mapnode.h" // For legacy name-id mapping
#include "content_nodemeta.h" // For legacy deserialization
+#include "serialization.h"
#ifndef SERVER
#include "mapblock_mesh.h"
#endif
#include "util/string.h"
+#include "util/serialize.h"
#define PP(x) "("<<(x).X<<","<<(x).Y<<","<<(x).Z<<")"
m_generated(false),
m_timestamp(BLOCK_TIMESTAMP_UNDEFINED),
m_disk_timestamp(BLOCK_TIMESTAMP_UNDEFINED),
- m_usage_timer(0)
+ m_usage_timer(0),
+ m_refcount(0)
{
data = NULL;
if(dummy == false)
reallocate();
#ifndef SERVER
- //mesh_mutex.Init();
mesh = NULL;
#endif
}
}
}
-MapNode MapBlock::getNodeParent(v3s16 p)
+MapNode MapBlock::getNodeParent(v3s16 p, bool *is_valid_position)
{
- if(isValidPosition(p) == false)
- {
- return m_parent->getNode(getPosRelative() + p);
- }
- else
- {
- if(data == NULL)
- throw InvalidPositionException();
- return data[p.Z*MAP_BLOCKSIZE*MAP_BLOCKSIZE + p.Y*MAP_BLOCKSIZE + p.X];
- }
-}
+ if (isValidPosition(p) == false)
+ return m_parent->getNodeNoEx(getPosRelative() + p, is_valid_position);
-void MapBlock::setNodeParent(v3s16 p, MapNode & n)
-{
- if(isValidPosition(p) == false)
- {
- m_parent->setNode(getPosRelative() + p, n);
- }
- else
- {
- if(data == NULL)
- throw InvalidPositionException();
- data[p.Z*MAP_BLOCKSIZE*MAP_BLOCKSIZE + p.Y*MAP_BLOCKSIZE + p.X] = n;
- }
-}
-
-MapNode MapBlock::getNodeParentNoEx(v3s16 p)
-{
- if(isValidPosition(p) == false)
- {
- try{
- return m_parent->getNode(getPosRelative() + p);
- }
- catch(InvalidPositionException &e)
- {
- return MapNode(CONTENT_IGNORE);
- }
- }
- else
- {
- if(data == NULL)
- {
- return MapNode(CONTENT_IGNORE);
- }
- return data[p.Z*MAP_BLOCKSIZE*MAP_BLOCKSIZE + p.Y*MAP_BLOCKSIZE + p.X];
+ if (data == NULL) {
+ if (is_valid_position)
+ *is_valid_position = false;
+ return MapNode(CONTENT_IGNORE);
}
+ if (is_valid_position)
+ *is_valid_position = true;
+ return data[p.Z*MAP_BLOCKSIZE*MAP_BLOCKSIZE + p.Y*MAP_BLOCKSIZE + p.X];
}
/*
if black_air_left!=NULL, it is set to true if non-sunlighted
air is left in block.
*/
-bool MapBlock::propagateSunlight(core::map<v3s16, bool> & light_sources,
+bool MapBlock::propagateSunlight(std::set<v3s16> & light_sources,
bool remove_light, bool *black_air_left)
{
INodeDefManager *nodemgr = m_gamedef->ndef();
#if 1
bool no_sunlight = false;
bool no_top_block = false;
+
// Check if node above block has sunlight
- try{
- MapNode n = getNodeParent(v3s16(x, MAP_BLOCKSIZE, z));
+
+ bool is_valid_position;
+ MapNode n = getNodeParent(v3s16(x, MAP_BLOCKSIZE, z),
+ &is_valid_position);
+ if (is_valid_position)
+ {
if(n.getContent() == CONTENT_IGNORE)
{
// Trust heuristics
no_sunlight = true;
}
}
- catch(InvalidPositionException &e)
+ else
{
no_top_block = true;
}
else
{
- MapNode n = getNode(v3s16(x, MAP_BLOCKSIZE-1, z));
+ MapNode n = getNodeNoEx(v3s16(x, MAP_BLOCKSIZE-1, z));
if(m_gamedef->ndef()->get(n).sunlight_propagates == false)
{
no_sunlight = true;
if(diminish_light(current_light) != 0)
{
- light_sources.insert(pos_relative + pos, true);
+ light_sources.insert(pos_relative + pos);
}
if(current_light == 0 && stopped_to_solid_object)
Ignore non-transparent nodes as they always have no light
*/
- try
- {
+
if(block_below_is_valid)
{
- MapNode n = getNodeParent(v3s16(x, -1, z));
- if(nodemgr->get(n).light_propagates)
+ MapNode n = getNodeParent(v3s16(x, -1, z), &is_valid_position);
+ if (is_valid_position) {
+ if(nodemgr->get(n).light_propagates)
+ {
+ if(n.getLight(LIGHTBANK_DAY, nodemgr) == LIGHT_SUN
+ && sunlight_should_go_down == false)
+ block_below_is_valid = false;
+ else if(n.getLight(LIGHTBANK_DAY, nodemgr) != LIGHT_SUN
+ && sunlight_should_go_down == true)
+ block_below_is_valid = false;
+ }
+ }
+ else
{
- if(n.getLight(LIGHTBANK_DAY, nodemgr) == LIGHT_SUN
- && sunlight_should_go_down == false)
- block_below_is_valid = false;
- else if(n.getLight(LIGHTBANK_DAY, nodemgr) != LIGHT_SUN
- && sunlight_should_go_down == true)
- block_below_is_valid = false;
+ /*std::cout<<"InvalidBlockException for bottom block node"
+ <<std::endl;*/
+ // Just no block below, no need to panic.
}
- }//if
- }//try
- catch(InvalidPositionException &e)
- {
- /*std::cout<<"InvalidBlockException for bottom block node"
- <<std::endl;*/
- // Just no block below, no need to panic.
}
}
}
*/
// List relevant id-name pairs for ids in the block using nodedef
// Renumbers the content IDs (starting at 0 and incrementing
+// use static memory requires about 65535 * sizeof(int) ram in order to be
+// sure we can handle all content ids. But it's absolutely worth it as it's
+// a speedup of 4 for one of the major time consuming functions on storing
+// mapblocks.
+static content_t getBlockNodeIdMapping_mapping[USHRT_MAX];
static void getBlockNodeIdMapping(NameIdMapping *nimap, MapNode *nodes,
INodeDefManager *nodedef)
{
- std::map<content_t, content_t> mapping;
+ memset(getBlockNodeIdMapping_mapping, 0xFF, USHRT_MAX * sizeof(content_t));
+
std::set<content_t> unknown_contents;
content_t id_counter = 0;
for(u32 i=0; i<MAP_BLOCKSIZE*MAP_BLOCKSIZE*MAP_BLOCKSIZE; i++)
content_t id = CONTENT_IGNORE;
// Try to find an existing mapping
- std::map<content_t, content_t>::iterator j = mapping.find(global_id);
- if(j != mapping.end())
- {
- id = j->second;
+ if (getBlockNodeIdMapping_mapping[global_id] != 0xFFFF) {
+ id = getBlockNodeIdMapping_mapping[global_id];
}
else
{
// We have to assign a new mapping
id = id_counter++;
- mapping.insert(std::make_pair(global_id, id));
+ getBlockNodeIdMapping_mapping[global_id] = id;
const ContentFeatures &f = nodedef->get(global_id);
const std::string &name = f.name;
}
}
+void MapBlock::serializeNetworkSpecific(std::ostream &os, u16 net_proto_version)
+{
+ if(data == NULL)
+ {
+ throw SerializationError("ERROR: Not writing dummy block.");
+ }
+
+ if(net_proto_version >= 21){
+ int version = 1;
+ writeU8(os, version);
+ writeF1000(os, 0); // deprecated heat
+ writeF1000(os, 0); // deprecated humidity
+ }
+}
+
void MapBlock::deSerialize(std::istream &is, u8 version, bool disk)
{
if(!ser_ver_supported(version))
<<": Done."<<std::endl);
}
+void MapBlock::deSerializeNetworkSpecific(std::istream &is)
+{
+ try {
+ int version = readU8(is);
+ //if(version != 1)
+ // throw SerializationError("unsupported MapBlock version");
+ if(version >= 1) {
+ readF1000(is); // deprecated heat
+ readF1000(is); // deprecated humidity
+ }
+ }
+ catch(SerializationError &e)
+ {
+ errorstream<<"WARNING: MapBlock::deSerializeNetworkSpecific(): Ignoring an error"
+ <<": "<<e.what()<<std::endl;
+ }
+}
+
/*
Legacy serialization
*/
for(s16 x0=0; x0<MAP_BLOCKSIZE; x0++)
{
v3s16 p(x0,y0,z0);
- MapNode n = block->getNode(p);
+ MapNode n = block->getNodeNoEx(p);
content_t c = n.getContent();
if(c == CONTENT_IGNORE)
some_ignore = true;