mapblock.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881
  1. /*
  2. Minetest
  3. Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License along
  13. with this program; if not, write to the Free Software Foundation, Inc.,
  14. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  15. */
  16. #include "mapblock.h"
  17. #include <sstream>
  18. #include "map.h"
  19. #include "light.h"
  20. #include "nodedef.h"
  21. #include "nodemetadata.h"
  22. #include "gamedef.h"
  23. #include "log.h"
  24. #include "nameidmapping.h"
  25. #include "content_mapnode.h" // For legacy name-id mapping
  26. #include "content_nodemeta.h" // For legacy deserialization
  27. #include "serialization.h"
  28. #ifndef SERVER
  29. #include "mapblock_mesh.h"
  30. #endif
  31. #include "util/string.h"
  32. #include "util/serialize.h"
  33. #include "util/basic_macros.h"
  34. static const char *modified_reason_strings[] = {
  35. "initial",
  36. "reallocate",
  37. "setIsUnderground",
  38. "setLightingExpired",
  39. "setGenerated",
  40. "setNode",
  41. "setNodeNoCheck",
  42. "setTimestamp",
  43. "NodeMetaRef::reportMetadataChange",
  44. "clearAllObjects",
  45. "Timestamp expired (step)",
  46. "addActiveObjectRaw",
  47. "removeRemovedObjects/remove",
  48. "removeRemovedObjects/deactivate",
  49. "Stored list cleared in activateObjects due to overflow",
  50. "deactivateFarObjects: Static data moved in",
  51. "deactivateFarObjects: Static data moved out",
  52. "deactivateFarObjects: Static data changed considerably",
  53. "finishBlockMake: expireDayNightDiff",
  54. "unknown",
  55. };
  56. /*
  57. MapBlock
  58. */
  59. MapBlock::MapBlock(Map *parent, v3s16 pos, IGameDef *gamedef, bool dummy):
  60. m_parent(parent),
  61. m_pos(pos),
  62. m_pos_relative(pos * MAP_BLOCKSIZE),
  63. m_gamedef(gamedef)
  64. {
  65. if (!dummy)
  66. reallocate();
  67. }
  68. MapBlock::~MapBlock()
  69. {
  70. #ifndef SERVER
  71. {
  72. delete mesh;
  73. mesh = nullptr;
  74. }
  75. #endif
  76. delete[] data;
  77. }
  78. bool MapBlock::isValidPositionParent(v3s16 p)
  79. {
  80. if (isValidPosition(p)) {
  81. return true;
  82. }
  83. return m_parent->isValidPosition(getPosRelative() + p);
  84. }
  85. MapNode MapBlock::getNodeParent(v3s16 p, bool *is_valid_position)
  86. {
  87. if (!isValidPosition(p))
  88. return m_parent->getNodeNoEx(getPosRelative() + p, is_valid_position);
  89. if (!data) {
  90. if (is_valid_position)
  91. *is_valid_position = false;
  92. return {CONTENT_IGNORE};
  93. }
  94. if (is_valid_position)
  95. *is_valid_position = true;
  96. return data[p.Z * zstride + p.Y * ystride + p.X];
  97. }
  98. std::string MapBlock::getModifiedReasonString()
  99. {
  100. std::string reason;
  101. const u32 ubound = MYMIN(sizeof(m_modified_reason) * CHAR_BIT,
  102. ARRLEN(modified_reason_strings));
  103. for (u32 i = 0; i != ubound; i++) {
  104. if ((m_modified_reason & (1 << i)) == 0)
  105. continue;
  106. reason += modified_reason_strings[i];
  107. reason += ", ";
  108. }
  109. if (reason.length() > 2)
  110. reason.resize(reason.length() - 2);
  111. return reason;
  112. }
  113. void MapBlock::copyTo(VoxelManipulator &dst)
  114. {
  115. v3s16 data_size(MAP_BLOCKSIZE, MAP_BLOCKSIZE, MAP_BLOCKSIZE);
  116. VoxelArea data_area(v3s16(0,0,0), data_size - v3s16(1,1,1));
  117. // Copy from data to VoxelManipulator
  118. dst.copyFrom(data, data_area, v3s16(0,0,0),
  119. getPosRelative(), data_size);
  120. }
  121. void MapBlock::copyFrom(VoxelManipulator &dst)
  122. {
  123. v3s16 data_size(MAP_BLOCKSIZE, MAP_BLOCKSIZE, MAP_BLOCKSIZE);
  124. VoxelArea data_area(v3s16(0,0,0), data_size - v3s16(1,1,1));
  125. // Copy from VoxelManipulator to data
  126. dst.copyTo(data, data_area, v3s16(0,0,0),
  127. getPosRelative(), data_size);
  128. }
  129. void MapBlock::actuallyUpdateDayNightDiff()
  130. {
  131. const NodeDefManager *nodemgr = m_gamedef->ndef();
  132. // Running this function un-expires m_day_night_differs
  133. m_day_night_differs_expired = false;
  134. if (!data) {
  135. m_day_night_differs = false;
  136. return;
  137. }
  138. bool differs = false;
  139. /*
  140. Check if any lighting value differs
  141. */
  142. MapNode previous_n(CONTENT_IGNORE);
  143. for (u32 i = 0; i < nodecount; i++) {
  144. MapNode n = data[i];
  145. // If node is identical to previous node, don't verify if it differs
  146. if (n == previous_n)
  147. continue;
  148. differs = !n.isLightDayNightEq(nodemgr);
  149. if (differs)
  150. break;
  151. previous_n = n;
  152. }
  153. /*
  154. If some lighting values differ, check if the whole thing is
  155. just air. If it is just air, differs = false
  156. */
  157. if (differs) {
  158. bool only_air = true;
  159. for (u32 i = 0; i < nodecount; i++) {
  160. MapNode &n = data[i];
  161. if (n.getContent() != CONTENT_AIR) {
  162. only_air = false;
  163. break;
  164. }
  165. }
  166. if (only_air)
  167. differs = false;
  168. }
  169. // Set member variable
  170. m_day_night_differs = differs;
  171. }
  172. void MapBlock::expireDayNightDiff()
  173. {
  174. if (!data) {
  175. m_day_night_differs = false;
  176. m_day_night_differs_expired = false;
  177. return;
  178. }
  179. m_day_night_differs_expired = true;
  180. }
  181. s16 MapBlock::getGroundLevel(v2s16 p2d)
  182. {
  183. if(isDummy())
  184. return -3;
  185. try
  186. {
  187. s16 y = MAP_BLOCKSIZE-1;
  188. for(; y>=0; y--)
  189. {
  190. MapNode n = getNodeRef(p2d.X, y, p2d.Y);
  191. if (m_gamedef->ndef()->get(n).walkable) {
  192. if(y == MAP_BLOCKSIZE-1)
  193. return -2;
  194. return y;
  195. }
  196. }
  197. return -1;
  198. }
  199. catch(InvalidPositionException &e)
  200. {
  201. return -3;
  202. }
  203. }
  204. /*
  205. Serialization
  206. */
  207. // List relevant id-name pairs for ids in the block using nodedef
  208. // Renumbers the content IDs (starting at 0 and incrementing
  209. // use static memory requires about 65535 * sizeof(int) ram in order to be
  210. // sure we can handle all content ids. But it's absolutely worth it as it's
  211. // a speedup of 4 for one of the major time consuming functions on storing
  212. // mapblocks.
  213. static content_t getBlockNodeIdMapping_mapping[USHRT_MAX + 1];
  214. static void getBlockNodeIdMapping(NameIdMapping *nimap, MapNode *nodes,
  215. const NodeDefManager *nodedef)
  216. {
  217. memset(getBlockNodeIdMapping_mapping, 0xFF, (USHRT_MAX + 1) * sizeof(content_t));
  218. std::set<content_t> unknown_contents;
  219. content_t id_counter = 0;
  220. for (u32 i = 0; i < MapBlock::nodecount; i++) {
  221. content_t global_id = nodes[i].getContent();
  222. content_t id = CONTENT_IGNORE;
  223. // Try to find an existing mapping
  224. if (getBlockNodeIdMapping_mapping[global_id] != 0xFFFF) {
  225. id = getBlockNodeIdMapping_mapping[global_id];
  226. }
  227. else
  228. {
  229. // We have to assign a new mapping
  230. id = id_counter++;
  231. getBlockNodeIdMapping_mapping[global_id] = id;
  232. const ContentFeatures &f = nodedef->get(global_id);
  233. const std::string &name = f.name;
  234. if (name.empty())
  235. unknown_contents.insert(global_id);
  236. else
  237. nimap->set(id, name);
  238. }
  239. // Update the MapNode
  240. nodes[i].setContent(id);
  241. }
  242. for (u16 unknown_content : unknown_contents) {
  243. errorstream << "getBlockNodeIdMapping(): IGNORING ERROR: "
  244. << "Name for node id " << unknown_content << " not known" << std::endl;
  245. }
  246. }
  247. // Correct ids in the block to match nodedef based on names.
  248. // Unknown ones are added to nodedef.
  249. // Will not update itself to match id-name pairs in nodedef.
  250. static void correctBlockNodeIds(const NameIdMapping *nimap, MapNode *nodes,
  251. IGameDef *gamedef)
  252. {
  253. const NodeDefManager *nodedef = gamedef->ndef();
  254. // This means the block contains incorrect ids, and we contain
  255. // the information to convert those to names.
  256. // nodedef contains information to convert our names to globally
  257. // correct ids.
  258. std::unordered_set<content_t> unnamed_contents;
  259. std::unordered_set<std::string> unallocatable_contents;
  260. bool previous_exists = false;
  261. content_t previous_local_id = CONTENT_IGNORE;
  262. content_t previous_global_id = CONTENT_IGNORE;
  263. for (u32 i = 0; i < MapBlock::nodecount; i++) {
  264. content_t local_id = nodes[i].getContent();
  265. // If previous node local_id was found and same than before, don't lookup maps
  266. // apply directly previous resolved id
  267. // This permits to massively improve loading performance when nodes are similar
  268. // example: default:air, default:stone are massively present
  269. if (previous_exists && local_id == previous_local_id) {
  270. nodes[i].setContent(previous_global_id);
  271. continue;
  272. }
  273. std::string name;
  274. if (!nimap->getName(local_id, name)) {
  275. unnamed_contents.insert(local_id);
  276. previous_exists = false;
  277. continue;
  278. }
  279. content_t global_id;
  280. if (!nodedef->getId(name, global_id)) {
  281. global_id = gamedef->allocateUnknownNodeId(name);
  282. if (global_id == CONTENT_IGNORE) {
  283. unallocatable_contents.insert(name);
  284. previous_exists = false;
  285. continue;
  286. }
  287. }
  288. nodes[i].setContent(global_id);
  289. // Save previous node local_id & global_id result
  290. previous_local_id = local_id;
  291. previous_global_id = global_id;
  292. previous_exists = true;
  293. }
  294. for (const content_t c: unnamed_contents) {
  295. errorstream << "correctBlockNodeIds(): IGNORING ERROR: "
  296. << "Block contains id " << c
  297. << " with no name mapping" << std::endl;
  298. }
  299. for (const std::string &node_name: unallocatable_contents) {
  300. errorstream << "correctBlockNodeIds(): IGNORING ERROR: "
  301. << "Could not allocate global id for node name \""
  302. << node_name << "\"" << std::endl;
  303. }
  304. }
  305. void MapBlock::serialize(std::ostream &os, u8 version, bool disk)
  306. {
  307. if(!ser_ver_supported(version))
  308. throw VersionMismatchException("ERROR: MapBlock format not supported");
  309. if (!data)
  310. throw SerializationError("ERROR: Not writing dummy block.");
  311. FATAL_ERROR_IF(version < SER_FMT_VER_LOWEST_WRITE, "Serialisation version error");
  312. // First byte
  313. u8 flags = 0;
  314. if(is_underground)
  315. flags |= 0x01;
  316. if(getDayNightDiff())
  317. flags |= 0x02;
  318. if (!m_generated)
  319. flags |= 0x08;
  320. writeU8(os, flags);
  321. if (version >= 27) {
  322. writeU16(os, m_lighting_complete);
  323. }
  324. /*
  325. Bulk node data
  326. */
  327. NameIdMapping nimap;
  328. if(disk)
  329. {
  330. MapNode *tmp_nodes = new MapNode[nodecount];
  331. for(u32 i=0; i<nodecount; i++)
  332. tmp_nodes[i] = data[i];
  333. getBlockNodeIdMapping(&nimap, tmp_nodes, m_gamedef->ndef());
  334. u8 content_width = 2;
  335. u8 params_width = 2;
  336. writeU8(os, content_width);
  337. writeU8(os, params_width);
  338. MapNode::serializeBulk(os, version, tmp_nodes, nodecount,
  339. content_width, params_width, true);
  340. delete[] tmp_nodes;
  341. }
  342. else
  343. {
  344. u8 content_width = 2;
  345. u8 params_width = 2;
  346. writeU8(os, content_width);
  347. writeU8(os, params_width);
  348. MapNode::serializeBulk(os, version, data, nodecount,
  349. content_width, params_width, true);
  350. }
  351. /*
  352. Node metadata
  353. */
  354. std::ostringstream oss(std::ios_base::binary);
  355. m_node_metadata.serialize(oss, version, disk);
  356. compressZlib(oss.str(), os);
  357. /*
  358. Data that goes to disk, but not the network
  359. */
  360. if(disk)
  361. {
  362. if(version <= 24){
  363. // Node timers
  364. m_node_timers.serialize(os, version);
  365. }
  366. // Static objects
  367. m_static_objects.serialize(os);
  368. // Timestamp
  369. writeU32(os, getTimestamp());
  370. // Write block-specific node definition id mapping
  371. nimap.serialize(os);
  372. if(version >= 25){
  373. // Node timers
  374. m_node_timers.serialize(os, version);
  375. }
  376. }
  377. }
  378. void MapBlock::serializeNetworkSpecific(std::ostream &os)
  379. {
  380. if (!data) {
  381. throw SerializationError("ERROR: Not writing dummy block.");
  382. }
  383. writeU8(os, 1); // version
  384. writeF1000(os, 0); // deprecated heat
  385. writeF1000(os, 0); // deprecated humidity
  386. }
  387. void MapBlock::deSerialize(std::istream &is, u8 version, bool disk)
  388. {
  389. if(!ser_ver_supported(version))
  390. throw VersionMismatchException("ERROR: MapBlock format not supported");
  391. TRACESTREAM(<<"MapBlock::deSerialize "<<PP(getPos())<<std::endl);
  392. m_day_night_differs_expired = false;
  393. if(version <= 21)
  394. {
  395. deSerialize_pre22(is, version, disk);
  396. return;
  397. }
  398. u8 flags = readU8(is);
  399. is_underground = (flags & 0x01) != 0;
  400. m_day_night_differs = (flags & 0x02) != 0;
  401. if (version < 27)
  402. m_lighting_complete = 0xFFFF;
  403. else
  404. m_lighting_complete = readU16(is);
  405. m_generated = (flags & 0x08) == 0;
  406. /*
  407. Bulk node data
  408. */
  409. TRACESTREAM(<<"MapBlock::deSerialize "<<PP(getPos())
  410. <<": Bulk node data"<<std::endl);
  411. u8 content_width = readU8(is);
  412. u8 params_width = readU8(is);
  413. if(content_width != 1 && content_width != 2)
  414. throw SerializationError("MapBlock::deSerialize(): invalid content_width");
  415. if(params_width != 2)
  416. throw SerializationError("MapBlock::deSerialize(): invalid params_width");
  417. MapNode::deSerializeBulk(is, version, data, nodecount,
  418. content_width, params_width, true);
  419. /*
  420. NodeMetadata
  421. */
  422. TRACESTREAM(<<"MapBlock::deSerialize "<<PP(getPos())
  423. <<": Node metadata"<<std::endl);
  424. // Ignore errors
  425. try {
  426. std::ostringstream oss(std::ios_base::binary);
  427. decompressZlib(is, oss);
  428. std::istringstream iss(oss.str(), std::ios_base::binary);
  429. if (version >= 23)
  430. m_node_metadata.deSerialize(iss, m_gamedef->idef());
  431. else
  432. content_nodemeta_deserialize_legacy(iss,
  433. &m_node_metadata, &m_node_timers,
  434. m_gamedef->idef());
  435. } catch(SerializationError &e) {
  436. warningstream<<"MapBlock::deSerialize(): Ignoring an error"
  437. <<" while deserializing node metadata at ("
  438. <<PP(getPos())<<": "<<e.what()<<std::endl;
  439. }
  440. /*
  441. Data that is only on disk
  442. */
  443. if(disk)
  444. {
  445. // Node timers
  446. if(version == 23){
  447. // Read unused zero
  448. readU8(is);
  449. }
  450. if(version == 24){
  451. TRACESTREAM(<<"MapBlock::deSerialize "<<PP(getPos())
  452. <<": Node timers (ver==24)"<<std::endl);
  453. m_node_timers.deSerialize(is, version);
  454. }
  455. // Static objects
  456. TRACESTREAM(<<"MapBlock::deSerialize "<<PP(getPos())
  457. <<": Static objects"<<std::endl);
  458. m_static_objects.deSerialize(is);
  459. // Timestamp
  460. TRACESTREAM(<<"MapBlock::deSerialize "<<PP(getPos())
  461. <<": Timestamp"<<std::endl);
  462. setTimestamp(readU32(is));
  463. m_disk_timestamp = m_timestamp;
  464. // Dynamically re-set ids based on node names
  465. TRACESTREAM(<<"MapBlock::deSerialize "<<PP(getPos())
  466. <<": NameIdMapping"<<std::endl);
  467. NameIdMapping nimap;
  468. nimap.deSerialize(is);
  469. correctBlockNodeIds(&nimap, data, m_gamedef);
  470. if(version >= 25){
  471. TRACESTREAM(<<"MapBlock::deSerialize "<<PP(getPos())
  472. <<": Node timers (ver>=25)"<<std::endl);
  473. m_node_timers.deSerialize(is, version);
  474. }
  475. }
  476. TRACESTREAM(<<"MapBlock::deSerialize "<<PP(getPos())
  477. <<": Done."<<std::endl);
  478. }
  479. void MapBlock::deSerializeNetworkSpecific(std::istream &is)
  480. {
  481. try {
  482. int version = readU8(is);
  483. //if(version != 1)
  484. // throw SerializationError("unsupported MapBlock version");
  485. if(version >= 1) {
  486. readF1000(is); // deprecated heat
  487. readF1000(is); // deprecated humidity
  488. }
  489. }
  490. catch(SerializationError &e)
  491. {
  492. warningstream<<"MapBlock::deSerializeNetworkSpecific(): Ignoring an error"
  493. <<": "<<e.what()<<std::endl;
  494. }
  495. }
  496. /*
  497. Legacy serialization
  498. */
  499. void MapBlock::deSerialize_pre22(std::istream &is, u8 version, bool disk)
  500. {
  501. // Initialize default flags
  502. is_underground = false;
  503. m_day_night_differs = false;
  504. m_lighting_complete = 0xFFFF;
  505. m_generated = true;
  506. // Make a temporary buffer
  507. u32 ser_length = MapNode::serializedLength(version);
  508. SharedBuffer<u8> databuf_nodelist(nodecount * ser_length);
  509. // These have no compression
  510. if (version <= 3 || version == 5 || version == 6) {
  511. char tmp;
  512. is.read(&tmp, 1);
  513. if (is.gcount() != 1)
  514. throw SerializationError(std::string(FUNCTION_NAME)
  515. + ": not enough input data");
  516. is_underground = tmp;
  517. is.read((char *)*databuf_nodelist, nodecount * ser_length);
  518. if ((u32)is.gcount() != nodecount * ser_length)
  519. throw SerializationError(std::string(FUNCTION_NAME)
  520. + ": not enough input data");
  521. } else if (version <= 10) {
  522. u8 t8;
  523. is.read((char *)&t8, 1);
  524. is_underground = t8;
  525. {
  526. // Uncompress and set material data
  527. std::ostringstream os(std::ios_base::binary);
  528. decompress(is, os, version);
  529. std::string s = os.str();
  530. if (s.size() != nodecount)
  531. throw SerializationError(std::string(FUNCTION_NAME)
  532. + ": not enough input data");
  533. for (u32 i = 0; i < s.size(); i++) {
  534. databuf_nodelist[i*ser_length] = s[i];
  535. }
  536. }
  537. {
  538. // Uncompress and set param data
  539. std::ostringstream os(std::ios_base::binary);
  540. decompress(is, os, version);
  541. std::string s = os.str();
  542. if (s.size() != nodecount)
  543. throw SerializationError(std::string(FUNCTION_NAME)
  544. + ": not enough input data");
  545. for (u32 i = 0; i < s.size(); i++) {
  546. databuf_nodelist[i*ser_length + 1] = s[i];
  547. }
  548. }
  549. if (version >= 10) {
  550. // Uncompress and set param2 data
  551. std::ostringstream os(std::ios_base::binary);
  552. decompress(is, os, version);
  553. std::string s = os.str();
  554. if (s.size() != nodecount)
  555. throw SerializationError(std::string(FUNCTION_NAME)
  556. + ": not enough input data");
  557. for (u32 i = 0; i < s.size(); i++) {
  558. databuf_nodelist[i*ser_length + 2] = s[i];
  559. }
  560. }
  561. } else { // All other versions (10 to 21)
  562. u8 flags;
  563. is.read((char*)&flags, 1);
  564. is_underground = (flags & 0x01) != 0;
  565. m_day_night_differs = (flags & 0x02) != 0;
  566. if(version >= 18)
  567. m_generated = (flags & 0x08) == 0;
  568. // Uncompress data
  569. std::ostringstream os(std::ios_base::binary);
  570. decompress(is, os, version);
  571. std::string s = os.str();
  572. if (s.size() != nodecount * 3)
  573. throw SerializationError(std::string(FUNCTION_NAME)
  574. + ": decompress resulted in size other than nodecount*3");
  575. // deserialize nodes from buffer
  576. for (u32 i = 0; i < nodecount; i++) {
  577. databuf_nodelist[i*ser_length] = s[i];
  578. databuf_nodelist[i*ser_length + 1] = s[i+nodecount];
  579. databuf_nodelist[i*ser_length + 2] = s[i+nodecount*2];
  580. }
  581. /*
  582. NodeMetadata
  583. */
  584. if (version >= 14) {
  585. // Ignore errors
  586. try {
  587. if (version <= 15) {
  588. std::string data = deSerializeString(is);
  589. std::istringstream iss(data, std::ios_base::binary);
  590. content_nodemeta_deserialize_legacy(iss,
  591. &m_node_metadata, &m_node_timers,
  592. m_gamedef->idef());
  593. } else {
  594. //std::string data = deSerializeLongString(is);
  595. std::ostringstream oss(std::ios_base::binary);
  596. decompressZlib(is, oss);
  597. std::istringstream iss(oss.str(), std::ios_base::binary);
  598. content_nodemeta_deserialize_legacy(iss,
  599. &m_node_metadata, &m_node_timers,
  600. m_gamedef->idef());
  601. }
  602. } catch(SerializationError &e) {
  603. warningstream<<"MapBlock::deSerialize(): Ignoring an error"
  604. <<" while deserializing node metadata"<<std::endl;
  605. }
  606. }
  607. }
  608. // Deserialize node data
  609. for (u32 i = 0; i < nodecount; i++) {
  610. data[i].deSerialize(&databuf_nodelist[i * ser_length], version);
  611. }
  612. if (disk) {
  613. /*
  614. Versions up from 9 have block objects. (DEPRECATED)
  615. */
  616. if (version >= 9) {
  617. u16 count = readU16(is);
  618. // Not supported and length not known if count is not 0
  619. if(count != 0){
  620. warningstream<<"MapBlock::deSerialize_pre22(): "
  621. <<"Ignoring stuff coming at and after MBOs"<<std::endl;
  622. return;
  623. }
  624. }
  625. /*
  626. Versions up from 15 have static objects.
  627. */
  628. if (version >= 15)
  629. m_static_objects.deSerialize(is);
  630. // Timestamp
  631. if (version >= 17) {
  632. setTimestamp(readU32(is));
  633. m_disk_timestamp = m_timestamp;
  634. } else {
  635. setTimestamp(BLOCK_TIMESTAMP_UNDEFINED);
  636. }
  637. // Dynamically re-set ids based on node names
  638. NameIdMapping nimap;
  639. // If supported, read node definition id mapping
  640. if (version >= 21) {
  641. nimap.deSerialize(is);
  642. // Else set the legacy mapping
  643. } else {
  644. content_mapnode_get_name_id_mapping(&nimap);
  645. }
  646. correctBlockNodeIds(&nimap, data, m_gamedef);
  647. }
  648. // Legacy data changes
  649. // This code has to convert from pre-22 to post-22 format.
  650. const NodeDefManager *nodedef = m_gamedef->ndef();
  651. for(u32 i=0; i<nodecount; i++)
  652. {
  653. const ContentFeatures &f = nodedef->get(data[i].getContent());
  654. // Mineral
  655. if(nodedef->getId("default:stone") == data[i].getContent()
  656. && data[i].getParam1() == 1)
  657. {
  658. data[i].setContent(nodedef->getId("default:stone_with_coal"));
  659. data[i].setParam1(0);
  660. }
  661. else if(nodedef->getId("default:stone") == data[i].getContent()
  662. && data[i].getParam1() == 2)
  663. {
  664. data[i].setContent(nodedef->getId("default:stone_with_iron"));
  665. data[i].setParam1(0);
  666. }
  667. // facedir_simple
  668. if(f.legacy_facedir_simple)
  669. {
  670. data[i].setParam2(data[i].getParam1());
  671. data[i].setParam1(0);
  672. }
  673. // wall_mounted
  674. if(f.legacy_wallmounted)
  675. {
  676. u8 wallmounted_new_to_old[8] = {0x04, 0x08, 0x01, 0x02, 0x10, 0x20, 0, 0};
  677. u8 dir_old_format = data[i].getParam2();
  678. u8 dir_new_format = 0;
  679. for(u8 j=0; j<8; j++)
  680. {
  681. if((dir_old_format & wallmounted_new_to_old[j]) != 0)
  682. {
  683. dir_new_format = j;
  684. break;
  685. }
  686. }
  687. data[i].setParam2(dir_new_format);
  688. }
  689. }
  690. }
  691. /*
  692. Get a quick string to describe what a block actually contains
  693. */
  694. std::string analyze_block(MapBlock *block)
  695. {
  696. if(block == NULL)
  697. return "NULL";
  698. std::ostringstream desc;
  699. v3s16 p = block->getPos();
  700. char spos[25];
  701. snprintf(spos, sizeof(spos), "(%2d,%2d,%2d), ", p.X, p.Y, p.Z);
  702. desc<<spos;
  703. switch(block->getModified())
  704. {
  705. case MOD_STATE_CLEAN:
  706. desc<<"CLEAN, ";
  707. break;
  708. case MOD_STATE_WRITE_AT_UNLOAD:
  709. desc<<"WRITE_AT_UNLOAD, ";
  710. break;
  711. case MOD_STATE_WRITE_NEEDED:
  712. desc<<"WRITE_NEEDED, ";
  713. break;
  714. default:
  715. desc<<"unknown getModified()="+itos(block->getModified())+", ";
  716. }
  717. if(block->isGenerated())
  718. desc<<"is_gen [X], ";
  719. else
  720. desc<<"is_gen [ ], ";
  721. if(block->getIsUnderground())
  722. desc<<"is_ug [X], ";
  723. else
  724. desc<<"is_ug [ ], ";
  725. desc<<"lighting_complete: "<<block->getLightingComplete()<<", ";
  726. if(block->isDummy())
  727. {
  728. desc<<"Dummy, ";
  729. }
  730. else
  731. {
  732. bool full_ignore = true;
  733. bool some_ignore = false;
  734. bool full_air = true;
  735. bool some_air = false;
  736. for(s16 z0=0; z0<MAP_BLOCKSIZE; z0++)
  737. for(s16 y0=0; y0<MAP_BLOCKSIZE; y0++)
  738. for(s16 x0=0; x0<MAP_BLOCKSIZE; x0++)
  739. {
  740. v3s16 p(x0,y0,z0);
  741. MapNode n = block->getNodeNoEx(p);
  742. content_t c = n.getContent();
  743. if(c == CONTENT_IGNORE)
  744. some_ignore = true;
  745. else
  746. full_ignore = false;
  747. if(c == CONTENT_AIR)
  748. some_air = true;
  749. else
  750. full_air = false;
  751. }
  752. desc<<"content {";
  753. std::ostringstream ss;
  754. if(full_ignore)
  755. ss<<"IGNORE (full), ";
  756. else if(some_ignore)
  757. ss<<"IGNORE, ";
  758. if(full_air)
  759. ss<<"AIR (full), ";
  760. else if(some_air)
  761. ss<<"AIR, ";
  762. if(ss.str().size()>=2)
  763. desc<<ss.str().substr(0, ss.str().size()-2);
  764. desc<<"}, ";
  765. }
  766. return desc.str().substr(0, desc.str().size()-2);
  767. }
  768. //END