mapblock.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911
  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. /*
  183. Serialization
  184. */
  185. // List relevant id-name pairs for ids in the block using nodedef
  186. // Renumbers the content IDs (starting at 0 and incrementing
  187. // use static memory requires about 65535 * sizeof(int) ram in order to be
  188. // sure we can handle all content ids. But it's absolutely worth it as it's
  189. // a speedup of 4 for one of the major time consuming functions on storing
  190. // mapblocks.
  191. static content_t getBlockNodeIdMapping_mapping[USHRT_MAX + 1];
  192. static void getBlockNodeIdMapping(NameIdMapping *nimap, MapNode *nodes,
  193. const NodeDefManager *nodedef)
  194. {
  195. memset(getBlockNodeIdMapping_mapping, 0xFF, (USHRT_MAX + 1) * sizeof(content_t));
  196. std::set<content_t> unknown_contents;
  197. content_t id_counter = 0;
  198. for (u32 i = 0; i < MapBlock::nodecount; i++) {
  199. content_t global_id = nodes[i].getContent();
  200. content_t id = CONTENT_IGNORE;
  201. // Try to find an existing mapping
  202. if (getBlockNodeIdMapping_mapping[global_id] != 0xFFFF) {
  203. id = getBlockNodeIdMapping_mapping[global_id];
  204. }
  205. else
  206. {
  207. // We have to assign a new mapping
  208. id = id_counter++;
  209. getBlockNodeIdMapping_mapping[global_id] = id;
  210. const ContentFeatures &f = nodedef->get(global_id);
  211. const std::string &name = f.name;
  212. if (name.empty())
  213. unknown_contents.insert(global_id);
  214. else
  215. nimap->set(id, name);
  216. }
  217. // Update the MapNode
  218. nodes[i].setContent(id);
  219. }
  220. for (u16 unknown_content : unknown_contents) {
  221. errorstream << "getBlockNodeIdMapping(): IGNORING ERROR: "
  222. << "Name for node id " << unknown_content << " not known" << std::endl;
  223. }
  224. }
  225. // Correct ids in the block to match nodedef based on names.
  226. // Unknown ones are added to nodedef.
  227. // Will not update itself to match id-name pairs in nodedef.
  228. static void correctBlockNodeIds(const NameIdMapping *nimap, MapNode *nodes,
  229. IGameDef *gamedef)
  230. {
  231. const NodeDefManager *nodedef = gamedef->ndef();
  232. // This means the block contains incorrect ids, and we contain
  233. // the information to convert those to names.
  234. // nodedef contains information to convert our names to globally
  235. // correct ids.
  236. std::unordered_set<content_t> unnamed_contents;
  237. std::unordered_set<std::string> unallocatable_contents;
  238. bool previous_exists = false;
  239. content_t previous_local_id = CONTENT_IGNORE;
  240. content_t previous_global_id = CONTENT_IGNORE;
  241. for (u32 i = 0; i < MapBlock::nodecount; i++) {
  242. content_t local_id = nodes[i].getContent();
  243. // If previous node local_id was found and same than before, don't lookup maps
  244. // apply directly previous resolved id
  245. // This permits to massively improve loading performance when nodes are similar
  246. // example: default:air, default:stone are massively present
  247. if (previous_exists && local_id == previous_local_id) {
  248. nodes[i].setContent(previous_global_id);
  249. continue;
  250. }
  251. std::string name;
  252. if (!nimap->getName(local_id, name)) {
  253. unnamed_contents.insert(local_id);
  254. previous_exists = false;
  255. continue;
  256. }
  257. content_t global_id;
  258. if (!nodedef->getId(name, global_id)) {
  259. global_id = gamedef->allocateUnknownNodeId(name);
  260. if (global_id == CONTENT_IGNORE) {
  261. unallocatable_contents.insert(name);
  262. previous_exists = false;
  263. continue;
  264. }
  265. }
  266. nodes[i].setContent(global_id);
  267. // Save previous node local_id & global_id result
  268. previous_local_id = local_id;
  269. previous_global_id = global_id;
  270. previous_exists = true;
  271. }
  272. for (const content_t c: unnamed_contents) {
  273. errorstream << "correctBlockNodeIds(): IGNORING ERROR: "
  274. << "Block contains id " << c
  275. << " with no name mapping" << std::endl;
  276. }
  277. for (const std::string &node_name: unallocatable_contents) {
  278. errorstream << "correctBlockNodeIds(): IGNORING ERROR: "
  279. << "Could not allocate global id for node name \""
  280. << node_name << "\"" << std::endl;
  281. }
  282. }
  283. void MapBlock::serialize(std::ostream &os_compressed, u8 version, bool disk, int compression_level)
  284. {
  285. if(!ser_ver_supported(version))
  286. throw VersionMismatchException("ERROR: MapBlock format not supported");
  287. if (!data)
  288. throw SerializationError("ERROR: Not writing dummy block.");
  289. FATAL_ERROR_IF(version < SER_FMT_VER_LOWEST_WRITE, "Serialisation version error");
  290. std::ostringstream os_raw(std::ios_base::binary);
  291. std::ostream &os = version >= 29 ? os_raw : os_compressed;
  292. // First byte
  293. u8 flags = 0;
  294. if(is_underground)
  295. flags |= 0x01;
  296. if(getDayNightDiff())
  297. flags |= 0x02;
  298. if (!m_generated)
  299. flags |= 0x08;
  300. writeU8(os, flags);
  301. if (version >= 27) {
  302. writeU16(os, m_lighting_complete);
  303. }
  304. /*
  305. Bulk node data
  306. */
  307. NameIdMapping nimap;
  308. SharedBuffer<u8> buf;
  309. const u8 content_width = 2;
  310. const u8 params_width = 2;
  311. if(disk)
  312. {
  313. MapNode *tmp_nodes = new MapNode[nodecount];
  314. memcpy(tmp_nodes, data, nodecount * sizeof(MapNode));
  315. getBlockNodeIdMapping(&nimap, tmp_nodes, m_gamedef->ndef());
  316. buf = MapNode::serializeBulk(version, tmp_nodes, nodecount,
  317. content_width, params_width);
  318. delete[] tmp_nodes;
  319. // write timestamp and node/id mapping first
  320. if (version >= 29) {
  321. writeU32(os, getTimestamp());
  322. nimap.serialize(os);
  323. }
  324. }
  325. else
  326. {
  327. buf = MapNode::serializeBulk(version, data, nodecount,
  328. content_width, params_width);
  329. }
  330. writeU8(os, content_width);
  331. writeU8(os, params_width);
  332. if (version >= 29) {
  333. os.write(reinterpret_cast<char*>(*buf), buf.getSize());
  334. } else {
  335. // prior to 29 node data was compressed individually
  336. compress(buf, os, version, compression_level);
  337. }
  338. /*
  339. Node metadata
  340. */
  341. if (version >= 29) {
  342. m_node_metadata.serialize(os, version, disk);
  343. } else {
  344. // use os_raw from above to avoid allocating another stream object
  345. m_node_metadata.serialize(os_raw, version, disk);
  346. // prior to 29 node data was compressed individually
  347. compress(os_raw.str(), os, version, compression_level);
  348. }
  349. /*
  350. Data that goes to disk, but not the network
  351. */
  352. if(disk)
  353. {
  354. if(version <= 24){
  355. // Node timers
  356. m_node_timers.serialize(os, version);
  357. }
  358. // Static objects
  359. m_static_objects.serialize(os);
  360. if(version < 29){
  361. // Timestamp
  362. writeU32(os, getTimestamp());
  363. // Write block-specific node definition id mapping
  364. nimap.serialize(os);
  365. }
  366. if(version >= 25){
  367. // Node timers
  368. m_node_timers.serialize(os, version);
  369. }
  370. }
  371. if (version >= 29) {
  372. // now compress the whole thing
  373. compress(os_raw.str(), os_compressed, version, compression_level);
  374. }
  375. }
  376. void MapBlock::serializeNetworkSpecific(std::ostream &os)
  377. {
  378. if (!data) {
  379. throw SerializationError("ERROR: Not writing dummy block.");
  380. }
  381. writeU8(os, 2); // version
  382. }
  383. void MapBlock::deSerialize(std::istream &in_compressed, u8 version, bool disk)
  384. {
  385. if(!ser_ver_supported(version))
  386. throw VersionMismatchException("ERROR: MapBlock format not supported");
  387. TRACESTREAM(<<"MapBlock::deSerialize "<<PP(getPos())<<std::endl);
  388. m_day_night_differs_expired = false;
  389. if(version <= 21)
  390. {
  391. deSerialize_pre22(in_compressed, version, disk);
  392. return;
  393. }
  394. // Decompress the whole block (version >= 29)
  395. std::stringstream in_raw(std::ios_base::binary | std::ios_base::in | std::ios_base::out);
  396. if (version >= 29)
  397. decompress(in_compressed, in_raw, version);
  398. std::istream &is = version >= 29 ? in_raw : in_compressed;
  399. u8 flags = readU8(is);
  400. is_underground = (flags & 0x01) != 0;
  401. m_day_night_differs = (flags & 0x02) != 0;
  402. if (version < 27)
  403. m_lighting_complete = 0xFFFF;
  404. else
  405. m_lighting_complete = readU16(is);
  406. m_generated = (flags & 0x08) == 0;
  407. NameIdMapping nimap;
  408. if (disk && version >= 29) {
  409. // Timestamp
  410. TRACESTREAM(<<"MapBlock::deSerialize "<<PP(getPos())
  411. <<": Timestamp"<<std::endl);
  412. setTimestampNoChangedFlag(readU32(is));
  413. m_disk_timestamp = m_timestamp;
  414. // Node/id mapping
  415. TRACESTREAM(<<"MapBlock::deSerialize "<<PP(getPos())
  416. <<": NameIdMapping"<<std::endl);
  417. nimap.deSerialize(is);
  418. }
  419. TRACESTREAM(<<"MapBlock::deSerialize "<<PP(getPos())
  420. <<": Bulk node data"<<std::endl);
  421. u8 content_width = readU8(is);
  422. u8 params_width = readU8(is);
  423. if(content_width != 1 && content_width != 2)
  424. throw SerializationError("MapBlock::deSerialize(): invalid content_width");
  425. if(params_width != 2)
  426. throw SerializationError("MapBlock::deSerialize(): invalid params_width");
  427. /*
  428. Bulk node data
  429. */
  430. if (version >= 29) {
  431. MapNode::deSerializeBulk(is, version, data, nodecount,
  432. content_width, params_width);
  433. } else {
  434. // use in_raw from above to avoid allocating another stream object
  435. decompress(is, in_raw, version);
  436. MapNode::deSerializeBulk(in_raw, version, data, nodecount,
  437. content_width, params_width);
  438. }
  439. /*
  440. NodeMetadata
  441. */
  442. TRACESTREAM(<<"MapBlock::deSerialize "<<PP(getPos())
  443. <<": Node metadata"<<std::endl);
  444. if (version >= 29) {
  445. m_node_metadata.deSerialize(is, m_gamedef->idef());
  446. } else {
  447. try {
  448. // reuse in_raw
  449. in_raw.str("");
  450. in_raw.clear();
  451. decompress(is, in_raw, version);
  452. if (version >= 23)
  453. m_node_metadata.deSerialize(in_raw, m_gamedef->idef());
  454. else
  455. content_nodemeta_deserialize_legacy(in_raw,
  456. &m_node_metadata, &m_node_timers,
  457. m_gamedef->idef());
  458. } catch(SerializationError &e) {
  459. warningstream<<"MapBlock::deSerialize(): Ignoring an error"
  460. <<" while deserializing node metadata at ("
  461. <<PP(getPos())<<": "<<e.what()<<std::endl;
  462. }
  463. }
  464. /*
  465. Data that is only on disk
  466. */
  467. if(disk)
  468. {
  469. // Node timers
  470. if(version == 23){
  471. // Read unused zero
  472. readU8(is);
  473. }
  474. if(version == 24){
  475. TRACESTREAM(<<"MapBlock::deSerialize "<<PP(getPos())
  476. <<": Node timers (ver==24)"<<std::endl);
  477. m_node_timers.deSerialize(is, version);
  478. }
  479. // Static objects
  480. TRACESTREAM(<<"MapBlock::deSerialize "<<PP(getPos())
  481. <<": Static objects"<<std::endl);
  482. m_static_objects.deSerialize(is);
  483. if(version < 29) {
  484. // Timestamp
  485. TRACESTREAM(<<"MapBlock::deSerialize "<<PP(getPos())
  486. <<": Timestamp"<<std::endl);
  487. setTimestampNoChangedFlag(readU32(is));
  488. m_disk_timestamp = m_timestamp;
  489. // Node/id mapping
  490. TRACESTREAM(<<"MapBlock::deSerialize "<<PP(getPos())
  491. <<": NameIdMapping"<<std::endl);
  492. nimap.deSerialize(is);
  493. }
  494. // Dynamically re-set ids based on node names
  495. correctBlockNodeIds(&nimap, data, m_gamedef);
  496. if(version >= 25){
  497. TRACESTREAM(<<"MapBlock::deSerialize "<<PP(getPos())
  498. <<": Node timers (ver>=25)"<<std::endl);
  499. m_node_timers.deSerialize(is, version);
  500. }
  501. }
  502. TRACESTREAM(<<"MapBlock::deSerialize "<<PP(getPos())
  503. <<": Done."<<std::endl);
  504. }
  505. void MapBlock::deSerializeNetworkSpecific(std::istream &is)
  506. {
  507. try {
  508. readU8(is);
  509. //const u8 version = readU8(is);
  510. //if (version != 1)
  511. //throw SerializationError("unsupported MapBlock version");
  512. } catch(SerializationError &e) {
  513. warningstream<<"MapBlock::deSerializeNetworkSpecific(): Ignoring an error"
  514. <<": "<<e.what()<<std::endl;
  515. }
  516. }
  517. /*
  518. Legacy serialization
  519. */
  520. void MapBlock::deSerialize_pre22(std::istream &is, u8 version, bool disk)
  521. {
  522. // Initialize default flags
  523. is_underground = false;
  524. m_day_night_differs = false;
  525. m_lighting_complete = 0xFFFF;
  526. m_generated = true;
  527. // Make a temporary buffer
  528. u32 ser_length = MapNode::serializedLength(version);
  529. SharedBuffer<u8> databuf_nodelist(nodecount * ser_length);
  530. // These have no compression
  531. if (version <= 3 || version == 5 || version == 6) {
  532. char tmp;
  533. is.read(&tmp, 1);
  534. if (is.gcount() != 1)
  535. throw SerializationError(std::string(FUNCTION_NAME)
  536. + ": not enough input data");
  537. is_underground = tmp;
  538. is.read((char *)*databuf_nodelist, nodecount * ser_length);
  539. if ((u32)is.gcount() != nodecount * ser_length)
  540. throw SerializationError(std::string(FUNCTION_NAME)
  541. + ": not enough input data");
  542. } else if (version <= 10) {
  543. u8 t8;
  544. is.read((char *)&t8, 1);
  545. is_underground = t8;
  546. {
  547. // Uncompress and set material data
  548. std::ostringstream os(std::ios_base::binary);
  549. decompress(is, os, version);
  550. std::string s = os.str();
  551. if (s.size() != nodecount)
  552. throw SerializationError(std::string(FUNCTION_NAME)
  553. + ": not enough input data");
  554. for (u32 i = 0; i < s.size(); i++) {
  555. databuf_nodelist[i*ser_length] = s[i];
  556. }
  557. }
  558. {
  559. // Uncompress and set param data
  560. std::ostringstream os(std::ios_base::binary);
  561. decompress(is, os, version);
  562. std::string s = os.str();
  563. if (s.size() != nodecount)
  564. throw SerializationError(std::string(FUNCTION_NAME)
  565. + ": not enough input data");
  566. for (u32 i = 0; i < s.size(); i++) {
  567. databuf_nodelist[i*ser_length + 1] = s[i];
  568. }
  569. }
  570. if (version >= 10) {
  571. // Uncompress and set param2 data
  572. std::ostringstream os(std::ios_base::binary);
  573. decompress(is, os, version);
  574. std::string s = os.str();
  575. if (s.size() != nodecount)
  576. throw SerializationError(std::string(FUNCTION_NAME)
  577. + ": not enough input data");
  578. for (u32 i = 0; i < s.size(); i++) {
  579. databuf_nodelist[i*ser_length + 2] = s[i];
  580. }
  581. }
  582. } else { // All other versions (10 to 21)
  583. u8 flags;
  584. is.read((char*)&flags, 1);
  585. is_underground = (flags & 0x01) != 0;
  586. m_day_night_differs = (flags & 0x02) != 0;
  587. if(version >= 18)
  588. m_generated = (flags & 0x08) == 0;
  589. // Uncompress data
  590. std::ostringstream os(std::ios_base::binary);
  591. decompress(is, os, version);
  592. std::string s = os.str();
  593. if (s.size() != nodecount * 3)
  594. throw SerializationError(std::string(FUNCTION_NAME)
  595. + ": decompress resulted in size other than nodecount*3");
  596. // deserialize nodes from buffer
  597. for (u32 i = 0; i < nodecount; i++) {
  598. databuf_nodelist[i*ser_length] = s[i];
  599. databuf_nodelist[i*ser_length + 1] = s[i+nodecount];
  600. databuf_nodelist[i*ser_length + 2] = s[i+nodecount*2];
  601. }
  602. /*
  603. NodeMetadata
  604. */
  605. if (version >= 14) {
  606. // Ignore errors
  607. try {
  608. if (version <= 15) {
  609. std::string data = deSerializeString16(is);
  610. std::istringstream iss(data, std::ios_base::binary);
  611. content_nodemeta_deserialize_legacy(iss,
  612. &m_node_metadata, &m_node_timers,
  613. m_gamedef->idef());
  614. } else {
  615. //std::string data = deSerializeString32(is);
  616. std::ostringstream oss(std::ios_base::binary);
  617. decompressZlib(is, oss);
  618. std::istringstream iss(oss.str(), std::ios_base::binary);
  619. content_nodemeta_deserialize_legacy(iss,
  620. &m_node_metadata, &m_node_timers,
  621. m_gamedef->idef());
  622. }
  623. } catch(SerializationError &e) {
  624. warningstream<<"MapBlock::deSerialize(): Ignoring an error"
  625. <<" while deserializing node metadata"<<std::endl;
  626. }
  627. }
  628. }
  629. // Deserialize node data
  630. for (u32 i = 0; i < nodecount; i++) {
  631. data[i].deSerialize(&databuf_nodelist[i * ser_length], version);
  632. }
  633. if (disk) {
  634. /*
  635. Versions up from 9 have block objects. (DEPRECATED)
  636. */
  637. if (version >= 9) {
  638. u16 count = readU16(is);
  639. // Not supported and length not known if count is not 0
  640. if(count != 0){
  641. warningstream<<"MapBlock::deSerialize_pre22(): "
  642. <<"Ignoring stuff coming at and after MBOs"<<std::endl;
  643. return;
  644. }
  645. }
  646. /*
  647. Versions up from 15 have static objects.
  648. */
  649. if (version >= 15)
  650. m_static_objects.deSerialize(is);
  651. // Timestamp
  652. if (version >= 17) {
  653. setTimestampNoChangedFlag(readU32(is));
  654. m_disk_timestamp = m_timestamp;
  655. } else {
  656. setTimestampNoChangedFlag(BLOCK_TIMESTAMP_UNDEFINED);
  657. }
  658. // Dynamically re-set ids based on node names
  659. NameIdMapping nimap;
  660. // If supported, read node definition id mapping
  661. if (version >= 21) {
  662. nimap.deSerialize(is);
  663. // Else set the legacy mapping
  664. } else {
  665. content_mapnode_get_name_id_mapping(&nimap);
  666. }
  667. correctBlockNodeIds(&nimap, data, m_gamedef);
  668. }
  669. // Legacy data changes
  670. // This code has to convert from pre-22 to post-22 format.
  671. const NodeDefManager *nodedef = m_gamedef->ndef();
  672. for(u32 i=0; i<nodecount; i++)
  673. {
  674. const ContentFeatures &f = nodedef->get(data[i].getContent());
  675. // Mineral
  676. if(nodedef->getId("default:stone") == data[i].getContent()
  677. && data[i].getParam1() == 1)
  678. {
  679. data[i].setContent(nodedef->getId("default:stone_with_coal"));
  680. data[i].setParam1(0);
  681. }
  682. else if(nodedef->getId("default:stone") == data[i].getContent()
  683. && data[i].getParam1() == 2)
  684. {
  685. data[i].setContent(nodedef->getId("default:stone_with_iron"));
  686. data[i].setParam1(0);
  687. }
  688. // facedir_simple
  689. if(f.legacy_facedir_simple)
  690. {
  691. data[i].setParam2(data[i].getParam1());
  692. data[i].setParam1(0);
  693. }
  694. // wall_mounted
  695. if(f.legacy_wallmounted)
  696. {
  697. u8 wallmounted_new_to_old[8] = {0x04, 0x08, 0x01, 0x02, 0x10, 0x20, 0, 0};
  698. u8 dir_old_format = data[i].getParam2();
  699. u8 dir_new_format = 0;
  700. for(u8 j=0; j<8; j++)
  701. {
  702. if((dir_old_format & wallmounted_new_to_old[j]) != 0)
  703. {
  704. dir_new_format = j;
  705. break;
  706. }
  707. }
  708. data[i].setParam2(dir_new_format);
  709. }
  710. }
  711. }
  712. /*
  713. Get a quick string to describe what a block actually contains
  714. */
  715. std::string analyze_block(MapBlock *block)
  716. {
  717. if(block == NULL)
  718. return "NULL";
  719. std::ostringstream desc;
  720. v3s16 p = block->getPos();
  721. char spos[25];
  722. porting::mt_snprintf(spos, sizeof(spos), "(%2d,%2d,%2d), ", p.X, p.Y, p.Z);
  723. desc<<spos;
  724. switch(block->getModified())
  725. {
  726. case MOD_STATE_CLEAN:
  727. desc<<"CLEAN, ";
  728. break;
  729. case MOD_STATE_WRITE_AT_UNLOAD:
  730. desc<<"WRITE_AT_UNLOAD, ";
  731. break;
  732. case MOD_STATE_WRITE_NEEDED:
  733. desc<<"WRITE_NEEDED, ";
  734. break;
  735. default:
  736. desc<<"unknown getModified()="+itos(block->getModified())+", ";
  737. }
  738. if(block->isGenerated())
  739. desc<<"is_gen [X], ";
  740. else
  741. desc<<"is_gen [ ], ";
  742. if(block->getIsUnderground())
  743. desc<<"is_ug [X], ";
  744. else
  745. desc<<"is_ug [ ], ";
  746. desc<<"lighting_complete: "<<block->getLightingComplete()<<", ";
  747. if(block->isDummy())
  748. {
  749. desc<<"Dummy, ";
  750. }
  751. else
  752. {
  753. bool full_ignore = true;
  754. bool some_ignore = false;
  755. bool full_air = true;
  756. bool some_air = false;
  757. for(s16 z0=0; z0<MAP_BLOCKSIZE; z0++)
  758. for(s16 y0=0; y0<MAP_BLOCKSIZE; y0++)
  759. for(s16 x0=0; x0<MAP_BLOCKSIZE; x0++)
  760. {
  761. v3s16 p(x0,y0,z0);
  762. MapNode n = block->getNodeNoEx(p);
  763. content_t c = n.getContent();
  764. if(c == CONTENT_IGNORE)
  765. some_ignore = true;
  766. else
  767. full_ignore = false;
  768. if(c == CONTENT_AIR)
  769. some_air = true;
  770. else
  771. full_air = false;
  772. }
  773. desc<<"content {";
  774. std::ostringstream ss;
  775. if(full_ignore)
  776. ss<<"IGNORE (full), ";
  777. else if(some_ignore)
  778. ss<<"IGNORE, ";
  779. if(full_air)
  780. ss<<"AIR (full), ";
  781. else if(some_air)
  782. ss<<"AIR, ";
  783. if(ss.str().size()>=2)
  784. desc<<ss.str().substr(0, ss.str().size()-2);
  785. desc<<"}, ";
  786. }
  787. return desc.str().substr(0, desc.str().size()-2);
  788. }
  789. //END