mapblock.cpp 21 KB

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