/* Minetest Copyright (C) 2013 celeron55, Perttu Ahola 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 the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ #include "mapblock.h" #include #include "map.h" #include "light.h" #include "nodedef.h" #include "nodemetadata.h" #include "gamedef.h" #include "log.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<<")" /* MapBlock */ MapBlock::MapBlock(Map *parent, v3s16 pos, IGameDef *gamedef, bool dummy): m_parent(parent), m_pos(pos), m_gamedef(gamedef), m_modified(MOD_STATE_WRITE_NEEDED), m_modified_reason("initial"), m_modified_reason_too_long(false), is_underground(false), m_lighting_expired(true), m_day_night_differs(false), m_day_night_differs_expired(true), m_generated(false), m_timestamp(BLOCK_TIMESTAMP_UNDEFINED), m_disk_timestamp(BLOCK_TIMESTAMP_UNDEFINED), m_usage_timer(0), m_refcount(0) { data = NULL; if(dummy == false) reallocate(); #ifndef SERVER mesh = NULL; #endif } MapBlock::~MapBlock() { #ifndef SERVER { //JMutexAutoLock lock(mesh_mutex); if(mesh) { delete mesh; mesh = NULL; } } #endif if(data) delete[] data; } bool MapBlock::isValidPositionParent(v3s16 p) { if(isValidPosition(p)) { return true; } else{ return m_parent->isValidPosition(getPosRelative() + p); } } MapNode MapBlock::getNodeParent(v3s16 p, bool *is_valid_position) { if (isValidPosition(p) == false) return m_parent->getNodeNoEx(getPosRelative() + p, is_valid_position); 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]; } /* Propagates sunlight down through the block. Doesn't modify nodes that are not affected by sunlight. Returns false if sunlight at bottom block is invalid. Returns true if sunlight at bottom block is valid. Returns true if bottom block doesn't exist. If there is a block above, continues from it. If there is no block above, assumes there is sunlight, unless is_underground is set or highest node is water. All sunlighted nodes are added to light_sources. if remove_light==true, sets non-sunlighted nodes black. if black_air_left!=NULL, it is set to true if non-sunlighted air is left in block. */ bool MapBlock::propagateSunlight(std::set & light_sources, bool remove_light, bool *black_air_left) { INodeDefManager *nodemgr = m_gamedef->ndef(); // Whether the sunlight at the top of the bottom block is valid bool block_below_is_valid = true; v3s16 pos_relative = getPosRelative(); for(s16 x=0; xndef()) != LIGHT_SUN) { no_sunlight = true; } } else { //no_top_block = true; // NOTE: This makes over-ground roofed places sunlighted // Assume sunlight, unless is_underground==true if(is_underground) { no_sunlight = true; } else { MapNode n = getNodeNoEx(v3s16(x, MAP_BLOCKSIZE-1, z)); if(m_gamedef->ndef()->get(n).sunlight_propagates == false) { no_sunlight = true; } } // NOTE: As of now, this just would make everything dark. // No sunlight here //no_sunlight = true; } #endif #if 0 // Doesn't work; nothing gets light. bool no_sunlight = true; bool no_top_block = false; // Check if node above block has sunlight try{ MapNode n = getNodeParent(v3s16(x, MAP_BLOCKSIZE, z)); if(n.getLight(LIGHTBANK_DAY) == LIGHT_SUN) { no_sunlight = false; } } catch(InvalidPositionException &e) { no_top_block = true; } #endif /*std::cout<<"("<ndef(); // Running this function un-expires m_day_night_differs m_day_night_differs_expired = false; if(data == NULL) { m_day_night_differs = false; return; } bool differs = false; /* Check if any lighting value differs */ for(u32 i=0; indef(); if(data == NULL){ m_day_night_differs = false; m_day_night_differs_expired = false; return; } m_day_night_differs_expired = true; } s16 MapBlock::getGroundLevel(v2s16 p2d) { if(isDummy()) return -3; try { s16 y = MAP_BLOCKSIZE-1; for(; y>=0; y--) { MapNode n = getNodeRef(p2d.X, y, p2d.Y); if(m_gamedef->ndef()->get(n).walkable) { if(y == MAP_BLOCKSIZE-1) return -2; else return y; } } return -1; } catch(InvalidPositionException &e) { return -3; } } /* Serialization */ // 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) { memset(getBlockNodeIdMapping_mapping, 0xFF, USHRT_MAX * sizeof(content_t)); std::set unknown_contents; content_t id_counter = 0; for(u32 i=0; iget(global_id); const std::string &name = f.name; if(name == "") unknown_contents.insert(global_id); else nimap->set(id, name); } // Update the MapNode nodes[i].setContent(id); } for(std::set::const_iterator i = unknown_contents.begin(); i != unknown_contents.end(); i++){ errorstream<<"getBlockNodeIdMapping(): IGNORING ERROR: " <<"Name for node id "<<(*i)<<" not known"<ndef(); // This means the block contains incorrect ids, and we contain // the information to convert those to names. // nodedef contains information to convert our names to globally // correct ids. std::set unnamed_contents; std::set unallocatable_contents; for(u32 i=0; igetName(local_id, name); if(!found){ unnamed_contents.insert(local_id); continue; } content_t global_id; found = nodedef->getId(name, global_id); if(!found){ global_id = gamedef->allocateUnknownNodeId(name); if(global_id == CONTENT_IGNORE){ unallocatable_contents.insert(name); continue; } } nodes[i].setContent(global_id); } for(std::set::const_iterator i = unnamed_contents.begin(); i != unnamed_contents.end(); i++){ errorstream<<"correctBlockNodeIds(): IGNORING ERROR: " <<"Block contains id "<<(*i) <<" with no name mapping"<::const_iterator i = unallocatable_contents.begin(); i != unallocatable_contents.end(); i++){ errorstream<<"correctBlockNodeIds(): IGNORING ERROR: " <<"Could not allocate global id for node name \"" <<(*i)<<"\""<ndef()); u8 content_width = 2; u8 params_width = 2; writeU8(os, content_width); writeU8(os, params_width); MapNode::serializeBulk(os, version, tmp_nodes, nodecount, content_width, params_width, true); delete[] tmp_nodes; } else { u8 content_width = 2; u8 params_width = 2; writeU8(os, content_width); writeU8(os, params_width); MapNode::serializeBulk(os, version, data, nodecount, content_width, params_width, true); } /* Node metadata */ std::ostringstream oss(std::ios_base::binary); m_node_metadata.serialize(oss); compressZlib(oss.str(), os); /* Data that goes to disk, but not the network */ if(disk) { if(version <= 24){ // Node timers m_node_timers.serialize(os, version); } // Static objects m_static_objects.serialize(os); // Timestamp writeU32(os, getTimestamp()); // Write block-specific node definition id mapping nimap.serialize(os); if(version >= 25){ // Node timers m_node_timers.serialize(os, version); } } } 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)) throw VersionMismatchException("ERROR: MapBlock format not supported"); TRACESTREAM(<<"MapBlock::deSerialize "<= 23) m_node_metadata.deSerialize(iss, m_gamedef); else content_nodemeta_deserialize_legacy(iss, &m_node_metadata, &m_node_timers, m_gamedef); } catch(SerializationError &e) { errorstream<<"WARNING: MapBlock::deSerialize(): Ignoring an error" <<" while deserializing node metadata at (" <= 25){ TRACESTREAM(<<"MapBlock::deSerialize "<=25)"<= 1) { readF1000(is); // deprecated heat readF1000(is); // deprecated humidity } } catch(SerializationError &e) { errorstream<<"WARNING: MapBlock::deSerializeNetworkSpecific(): Ignoring an error" <<": "< databuf_nodelist(nodecount * ser_length); // These have no compression if(version <= 3 || version == 5 || version == 6) { char tmp; is.read(&tmp, 1); if(is.gcount() != 1) throw SerializationError ("MapBlock::deSerialize: no enough input data"); is_underground = tmp; is.read((char*)*databuf_nodelist, nodecount * ser_length); if((u32)is.gcount() != nodecount * ser_length) throw SerializationError ("MapBlock::deSerialize: no enough input data"); } else if(version <= 10) { u8 t8; is.read((char*)&t8, 1); is_underground = t8; { // Uncompress and set material data std::ostringstream os(std::ios_base::binary); decompress(is, os, version); std::string s = os.str(); if(s.size() != nodecount) throw SerializationError void GUIPasswordChange::regenerateGui(v2u32 screensize) { /* save current input */ acceptInput(); /* Remove stuff */ removeChildren(); /* Calculate new sizes and positions */ #ifdef __ANDROID__ const float s = m_gui_scale * porting::getDisplayDensity() / 2; #else const float s = m_gui_scale; #endif DesiredRect = core::rect<s32>( screensize.X / 2 - 580 * s / 2, screensize.Y / 2 - 300 * s / 2, screensize.X / 2 + 580 * s / 2, screensize.Y / 2 + 300 * s / 2 ); recalculateAbsolutePosition(false); v2s32 size = DesiredRect.getSize(); v2s32 topleft_client(40 * s, 0); const wchar_t *text; /* Add stuff */ s32 ypos = 50 * s; { core::rect<s32> rect(0, 0, 150 * s, 20 * s); rect += topleft_client + v2s32(25 * s, ypos + 6 * s); text = wgettext("Old Password"); Environment->addStaticText(text, rect, false, true, this, -1); delete[] text; } { core::rect<s32> rect(0, 0, 230 * s, 30 * s); rect += topleft_client + v2s32(160 * s, ypos); gui::IGUIEditBox *e = Environment->addEditBox( m_oldpass.c_str(), rect, true, this, ID_oldPassword);