mapblock.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056
  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 == false)
  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. {
  82. return true;
  83. }
  84. else{
  85. return m_parent->isValidPosition(getPosRelative() + p);
  86. }
  87. }
  88. MapNode MapBlock::getNodeParent(v3s16 p, bool *is_valid_position)
  89. {
  90. if (isValidPosition(p) == false)
  91. return m_parent->getNodeNoEx(getPosRelative() + p, is_valid_position);
  92. if (!data) {
  93. if (is_valid_position)
  94. *is_valid_position = false;
  95. return MapNode(CONTENT_IGNORE);
  96. }
  97. if (is_valid_position)
  98. *is_valid_position = true;
  99. return data[p.Z * zstride + p.Y * ystride + p.X];
  100. }
  101. std::string MapBlock::getModifiedReasonString()
  102. {
  103. std::string reason;
  104. const u32 ubound = MYMIN(sizeof(m_modified_reason) * CHAR_BIT,
  105. ARRLEN(modified_reason_strings));
  106. for (u32 i = 0; i != ubound; i++) {
  107. if ((m_modified_reason & (1 << i)) == 0)
  108. continue;
  109. reason += modified_reason_strings[i];
  110. reason += ", ";
  111. }
  112. if (reason.length() > 2)
  113. reason.resize(reason.length() - 2);
  114. return reason;
  115. }
  116. /*
  117. Propagates sunlight down through the block.
  118. Doesn't modify nodes that are not affected by sunlight.
  119. Returns false if sunlight at bottom block is invalid.
  120. Returns true if sunlight at bottom block is valid.
  121. Returns true if bottom block doesn't exist.
  122. If there is a block above, continues from it.
  123. If there is no block above, assumes there is sunlight, unless
  124. is_underground is set or highest node is water.
  125. All sunlighted nodes are added to light_sources.
  126. if remove_light==true, sets non-sunlighted nodes black.
  127. if black_air_left!=NULL, it is set to true if non-sunlighted
  128. air is left in block.
  129. */
  130. bool MapBlock::propagateSunlight(std::set<v3s16> & light_sources,
  131. bool remove_light, bool *black_air_left)
  132. {
  133. INodeDefManager *nodemgr = m_gamedef->ndef();
  134. // Whether the sunlight at the top of the bottom block is valid
  135. bool block_below_is_valid = true;
  136. v3s16 pos_relative = getPosRelative();
  137. for(s16 x=0; x<MAP_BLOCKSIZE; x++)
  138. {
  139. for(s16 z=0; z<MAP_BLOCKSIZE; z++)
  140. {
  141. #if 1
  142. bool no_sunlight = false;
  143. //bool no_top_block = false;
  144. // Check if node above block has sunlight
  145. bool is_valid_position;
  146. MapNode n = getNodeParent(v3s16(x, MAP_BLOCKSIZE, z),
  147. &is_valid_position);
  148. if (is_valid_position)
  149. {
  150. if(n.getContent() == CONTENT_IGNORE)
  151. {
  152. // Trust heuristics
  153. no_sunlight = is_underground;
  154. }
  155. else if(n.getLight(LIGHTBANK_DAY, m_gamedef->ndef()) != LIGHT_SUN)
  156. {
  157. no_sunlight = true;
  158. }
  159. }
  160. else
  161. {
  162. //no_top_block = true;
  163. // NOTE: This makes over-ground roofed places sunlighted
  164. // Assume sunlight, unless is_underground==true
  165. if(is_underground)
  166. {
  167. no_sunlight = true;
  168. }
  169. else
  170. {
  171. MapNode n = getNodeNoEx(v3s16(x, MAP_BLOCKSIZE-1, z));
  172. if(m_gamedef->ndef()->get(n).sunlight_propagates == false)
  173. {
  174. no_sunlight = true;
  175. }
  176. }
  177. // NOTE: As of now, this just would make everything dark.
  178. // No sunlight here
  179. //no_sunlight = true;
  180. }
  181. #endif
  182. #if 0 // Doesn't work; nothing gets light.
  183. bool no_sunlight = true;
  184. bool no_top_block = false;
  185. // Check if node above block has sunlight
  186. try{
  187. MapNode n = getNodeParent(v3s16(x, MAP_BLOCKSIZE, z));
  188. if(n.getLight(LIGHTBANK_DAY) == LIGHT_SUN)
  189. {
  190. no_sunlight = false;
  191. }
  192. }
  193. catch(InvalidPositionException &e)
  194. {
  195. no_top_block = true;
  196. }
  197. #endif
  198. /*std::cout<<"("<<x<<","<<z<<"): "
  199. <<"no_top_block="<<no_top_block
  200. <<", is_underground="<<is_underground
  201. <<", no_sunlight="<<no_sunlight
  202. <<std::endl;*/
  203. s16 y = MAP_BLOCKSIZE-1;
  204. // This makes difference to diminishing in water.
  205. bool stopped_to_solid_object = false;
  206. u8 current_light = no_sunlight ? 0 : LIGHT_SUN;
  207. for(; y >= 0; y--)
  208. {
  209. v3s16 pos(x, y, z);
  210. MapNode &n = getNodeRef(pos);
  211. if(current_light == 0)
  212. {
  213. // Do nothing
  214. }
  215. else if(current_light == LIGHT_SUN && nodemgr->get(n).sunlight_propagates)
  216. {
  217. // Do nothing: Sunlight is continued
  218. }
  219. else if(nodemgr->get(n).light_propagates == false)
  220. {
  221. // A solid object is on the way.
  222. stopped_to_solid_object = true;
  223. // Light stops.
  224. current_light = 0;
  225. }
  226. else
  227. {
  228. // Diminish light
  229. current_light = diminish_light(current_light);
  230. }
  231. u8 old_light = n.getLight(LIGHTBANK_DAY, nodemgr);
  232. if(current_light > old_light || remove_light)
  233. {
  234. n.setLight(LIGHTBANK_DAY, current_light, nodemgr);
  235. }
  236. if(diminish_light(current_light) != 0)
  237. {
  238. light_sources.insert(pos_relative + pos);
  239. }
  240. if(current_light == 0 && stopped_to_solid_object)
  241. {
  242. if(black_air_left)
  243. {
  244. *black_air_left = true;
  245. }
  246. }
  247. }
  248. // Whether or not the block below should see LIGHT_SUN
  249. bool sunlight_should_go_down = (current_light == LIGHT_SUN);
  250. /*
  251. If the block below hasn't already been marked invalid:
  252. Check if the node below the block has proper sunlight at top.
  253. If not, the block below is invalid.
  254. Ignore non-transparent nodes as they always have no light
  255. */
  256. if(block_below_is_valid)
  257. {
  258. MapNode n = getNodeParent(v3s16(x, -1, z), &is_valid_position);
  259. if (is_valid_position) {
  260. if(nodemgr->get(n).light_propagates)
  261. {
  262. if(n.getLight(LIGHTBANK_DAY, nodemgr) == LIGHT_SUN
  263. && sunlight_should_go_down == false)
  264. block_below_is_valid = false;
  265. else if(n.getLight(LIGHTBANK_DAY, nodemgr) != LIGHT_SUN
  266. && sunlight_should_go_down == true)
  267. block_below_is_valid = false;
  268. }
  269. }
  270. else
  271. {
  272. /*std::cout<<"InvalidBlockException for bottom block node"
  273. <<std::endl;*/
  274. // Just no block below, no need to panic.
  275. }
  276. }
  277. }
  278. }
  279. return block_below_is_valid;
  280. }
  281. void MapBlock::copyTo(VoxelManipulator &dst)
  282. {
  283. v3s16 data_size(MAP_BLOCKSIZE, MAP_BLOCKSIZE, MAP_BLOCKSIZE);
  284. VoxelArea data_area(v3s16(0,0,0), data_size - v3s16(1,1,1));
  285. // Copy from data to VoxelManipulator
  286. dst.copyFrom(data, data_area, v3s16(0,0,0),
  287. getPosRelative(), data_size);
  288. }
  289. void MapBlock::copyFrom(VoxelManipulator &dst)
  290. {
  291. v3s16 data_size(MAP_BLOCKSIZE, MAP_BLOCKSIZE, MAP_BLOCKSIZE);
  292. VoxelArea data_area(v3s16(0,0,0), data_size - v3s16(1,1,1));
  293. // Copy from VoxelManipulator to data
  294. dst.copyTo(data, data_area, v3s16(0,0,0),
  295. getPosRelative(), data_size);
  296. }
  297. void MapBlock::actuallyUpdateDayNightDiff()
  298. {
  299. INodeDefManager *nodemgr = m_gamedef->ndef();
  300. // Running this function un-expires m_day_night_differs
  301. m_day_night_differs_expired = false;
  302. if (!data) {
  303. m_day_night_differs = false;
  304. return;
  305. }
  306. bool differs;
  307. /*
  308. Check if any lighting value differs
  309. */
  310. for (u32 i = 0; i < nodecount; i++) {
  311. MapNode &n = data[i];
  312. differs = !n.isLightDayNightEq(nodemgr);
  313. if (differs)
  314. break;
  315. }
  316. /*
  317. If some lighting values differ, check if the whole thing is
  318. just air. If it is just air, differs = false
  319. */
  320. if (differs) {
  321. bool only_air = true;
  322. for (u32 i = 0; i < nodecount; i++) {
  323. MapNode &n = data[i];
  324. if (n.getContent() != CONTENT_AIR) {
  325. only_air = false;
  326. break;
  327. }
  328. }
  329. if (only_air)
  330. differs = false;
  331. }
  332. // Set member variable
  333. m_day_night_differs = differs;
  334. }
  335. void MapBlock::expireDayNightDiff()
  336. {
  337. if (!data) {
  338. m_day_night_differs = false;
  339. m_day_night_differs_expired = false;
  340. return;
  341. }
  342. m_day_night_differs_expired = true;
  343. }
  344. s16 MapBlock::getGroundLevel(v2s16 p2d)
  345. {
  346. if(isDummy())
  347. return -3;
  348. try
  349. {
  350. s16 y = MAP_BLOCKSIZE-1;
  351. for(; y>=0; y--)
  352. {
  353. MapNode n = getNodeRef(p2d.X, y, p2d.Y);
  354. if(m_gamedef->ndef()->get(n).walkable)
  355. {
  356. if(y == MAP_BLOCKSIZE-1)
  357. return -2;
  358. else
  359. return y;
  360. }
  361. }
  362. return -1;
  363. }
  364. catch(InvalidPositionException &e)
  365. {
  366. return -3;
  367. }
  368. }
  369. /*
  370. Serialization
  371. */
  372. // List relevant id-name pairs for ids in the block using nodedef
  373. // Renumbers the content IDs (starting at 0 and incrementing
  374. // use static memory requires about 65535 * sizeof(int) ram in order to be
  375. // sure we can handle all content ids. But it's absolutely worth it as it's
  376. // a speedup of 4 for one of the major time consuming functions on storing
  377. // mapblocks.
  378. static content_t getBlockNodeIdMapping_mapping[USHRT_MAX + 1];
  379. static void getBlockNodeIdMapping(NameIdMapping *nimap, MapNode *nodes,
  380. INodeDefManager *nodedef)
  381. {
  382. memset(getBlockNodeIdMapping_mapping, 0xFF, (USHRT_MAX + 1) * sizeof(content_t));
  383. std::set<content_t> unknown_contents;
  384. content_t id_counter = 0;
  385. for (u32 i = 0; i < MapBlock::nodecount; i++) {
  386. content_t global_id = nodes[i].getContent();
  387. content_t id = CONTENT_IGNORE;
  388. // Try to find an existing mapping
  389. if (getBlockNodeIdMapping_mapping[global_id] != 0xFFFF) {
  390. id = getBlockNodeIdMapping_mapping[global_id];
  391. }
  392. else
  393. {
  394. // We have to assign a new mapping
  395. id = id_counter++;
  396. getBlockNodeIdMapping_mapping[global_id] = id;
  397. const ContentFeatures &f = nodedef->get(global_id);
  398. const std::string &name = f.name;
  399. if(name == "")
  400. unknown_contents.insert(global_id);
  401. else
  402. nimap->set(id, name);
  403. }
  404. // Update the MapNode
  405. nodes[i].setContent(id);
  406. }
  407. for(std::set<content_t>::const_iterator
  408. i = unknown_contents.begin();
  409. i != unknown_contents.end(); ++i){
  410. errorstream<<"getBlockNodeIdMapping(): IGNORING ERROR: "
  411. <<"Name for node id "<<(*i)<<" not known"<<std::endl;
  412. }
  413. }
  414. // Correct ids in the block to match nodedef based on names.
  415. // Unknown ones are added to nodedef.
  416. // Will not update itself to match id-name pairs in nodedef.
  417. static void correctBlockNodeIds(const NameIdMapping *nimap, MapNode *nodes,
  418. IGameDef *gamedef)
  419. {
  420. INodeDefManager *nodedef = gamedef->ndef();
  421. // This means the block contains incorrect ids, and we contain
  422. // the information to convert those to names.
  423. // nodedef contains information to convert our names to globally
  424. // correct ids.
  425. std::set<content_t> unnamed_contents;
  426. std::set<std::string> unallocatable_contents;
  427. for (u32 i = 0; i < MapBlock::nodecount; i++) {
  428. content_t local_id = nodes[i].getContent();
  429. std::string name;
  430. bool found = nimap->getName(local_id, name);
  431. if(!found){
  432. unnamed_contents.insert(local_id);
  433. continue;
  434. }
  435. content_t global_id;
  436. found = nodedef->getId(name, global_id);
  437. if(!found){
  438. global_id = gamedef->allocateUnknownNodeId(name);
  439. if(global_id == CONTENT_IGNORE){
  440. unallocatable_contents.insert(name);
  441. continue;
  442. }
  443. }
  444. nodes[i].setContent(global_id);
  445. }
  446. for(std::set<content_t>::const_iterator
  447. i = unnamed_contents.begin();
  448. i != unnamed_contents.end(); ++i){
  449. errorstream<<"correctBlockNodeIds(): IGNORING ERROR: "
  450. <<"Block contains id "<<(*i)
  451. <<" with no name mapping"<<std::endl;
  452. }
  453. for(std::set<std::string>::const_iterator
  454. i = unallocatable_contents.begin();
  455. i != unallocatable_contents.end(); ++i){
  456. errorstream<<"correctBlockNodeIds(): IGNORING ERROR: "
  457. <<"Could not allocate global id for node name \""
  458. <<(*i)<<"\""<<std::endl;
  459. }
  460. }
  461. void MapBlock::serialize(std::ostream &os, u8 version, bool disk)
  462. {
  463. if(!ser_ver_supported(version))
  464. throw VersionMismatchException("ERROR: MapBlock format not supported");
  465. if (!data)
  466. throw SerializationError("ERROR: Not writing dummy block.");
  467. FATAL_ERROR_IF(version < SER_FMT_VER_LOWEST_WRITE, "Serialisation version error");
  468. // First byte
  469. u8 flags = 0;
  470. if(is_underground)
  471. flags |= 0x01;
  472. if(getDayNightDiff())
  473. flags |= 0x02;
  474. if(m_generated == false)
  475. flags |= 0x08;
  476. writeU8(os, flags);
  477. if (version >= 27) {
  478. writeU16(os, m_lighting_complete);
  479. }
  480. /*
  481. Bulk node data
  482. */
  483. NameIdMapping nimap;
  484. if(disk)
  485. {
  486. MapNode *tmp_nodes = new MapNode[nodecount];
  487. for(u32 i=0; i<nodecount; i++)
  488. tmp_nodes[i] = data[i];
  489. getBlockNodeIdMapping(&nimap, tmp_nodes, m_gamedef->ndef());
  490. u8 content_width = 2;
  491. u8 params_width = 2;
  492. writeU8(os, content_width);
  493. writeU8(os, params_width);
  494. MapNode::serializeBulk(os, version, tmp_nodes, nodecount,
  495. content_width, params_width, true);
  496. delete[] tmp_nodes;
  497. }
  498. else
  499. {
  500. u8 content_width = 2;
  501. u8 params_width = 2;
  502. writeU8(os, content_width);
  503. writeU8(os, params_width);
  504. MapNode::serializeBulk(os, version, data, nodecount,
  505. content_width, params_width, true);
  506. }
  507. /*
  508. Node metadata
  509. */
  510. std::ostringstream oss(std::ios_base::binary);
  511. m_node_metadata.serialize(oss, version, disk);
  512. compressZlib(oss.str(), os);
  513. /*
  514. Data that goes to disk, but not the network
  515. */
  516. if(disk)
  517. {
  518. if(version <= 24){
  519. // Node timers
  520. m_node_timers.serialize(os, version);
  521. }
  522. // Static objects
  523. m_static_objects.serialize(os);
  524. // Timestamp
  525. writeU32(os, getTimestamp());
  526. // Write block-specific node definition id mapping
  527. nimap.serialize(os);
  528. if(version >= 25){
  529. // Node timers
  530. m_node_timers.serialize(os, version);
  531. }
  532. }
  533. }
  534. void MapBlock::serializeNetworkSpecific(std::ostream &os)
  535. {
  536. if (!data) {
  537. throw SerializationError("ERROR: Not writing dummy block.");
  538. }
  539. writeU8(os, 1); // version
  540. writeF1000(os, 0); // deprecated heat
  541. writeF1000(os, 0); // deprecated humidity
  542. }
  543. void MapBlock::deSerialize(std::istream &is, u8 version, bool disk)
  544. {
  545. if(!ser_ver_supported(version))
  546. throw VersionMismatchException("ERROR: MapBlock format not supported");
  547. TRACESTREAM(<<"MapBlock::deSerialize "<<PP(getPos())<<std::endl);
  548. m_day_night_differs_expired = false;
  549. if(version <= 21)
  550. {
  551. deSerialize_pre22(is, version, disk);
  552. return;
  553. }
  554. u8 flags = readU8(is);
  555. is_underground = (flags & 0x01) ? true : false;
  556. m_day_night_differs = (flags & 0x02) ? true : false;
  557. if (version < 27)
  558. m_lighting_complete = 0xFFFF;
  559. else
  560. m_lighting_complete = readU16(is);
  561. m_generated = (flags & 0x08) ? false : true;
  562. /*
  563. Bulk node data
  564. */
  565. TRACESTREAM(<<"MapBlock::deSerialize "<<PP(getPos())
  566. <<": Bulk node data"<<std::endl);
  567. u8 content_width = readU8(is);
  568. u8 params_width = readU8(is);
  569. if(content_width != 1 && content_width != 2)
  570. throw SerializationError("MapBlock::deSerialize(): invalid content_width");
  571. if(params_width != 2)
  572. throw SerializationError("MapBlock::deSerialize(): invalid params_width");
  573. MapNode::deSerializeBulk(is, version, data, nodecount,
  574. content_width, params_width, true);
  575. /*
  576. NodeMetadata
  577. */
  578. TRACESTREAM(<<"MapBlock::deSerialize "<<PP(getPos())
  579. <<": Node metadata"<<std::endl);
  580. // Ignore errors
  581. try {
  582. std::ostringstream oss(std::ios_base::binary);
  583. decompressZlib(is, oss);
  584. std::istringstream iss(oss.str(), std::ios_base::binary);
  585. if (version >= 23)
  586. m_node_metadata.deSerialize(iss, m_gamedef->idef());
  587. else
  588. content_nodemeta_deserialize_legacy(iss,
  589. &m_node_metadata, &m_node_timers,
  590. m_gamedef->idef());
  591. } catch(SerializationError &e) {
  592. warningstream<<"MapBlock::deSerialize(): Ignoring an error"
  593. <<" while deserializing node metadata at ("
  594. <<PP(getPos())<<": "<<e.what()<<std::endl;
  595. }
  596. /*
  597. Data that is only on disk
  598. */
  599. if(disk)
  600. {
  601. // Node timers
  602. if(version == 23){
  603. // Read unused zero
  604. readU8(is);
  605. }
  606. if(version == 24){
  607. TRACESTREAM(<<"MapBlock::deSerialize "<<PP(getPos())
  608. <<": Node timers (ver==24)"<<std::endl);
  609. m_node_timers.deSerialize(is, version);
  610. }
  611. // Static objects
  612. TRACESTREAM(<<"MapBlock::deSerialize "<<PP(getPos())
  613. <<": Static objects"<<std::endl);
  614. m_static_objects.deSerialize(is);
  615. // Timestamp
  616. TRACESTREAM(<<"MapBlock::deSerialize "<<PP(getPos())
  617. <<": Timestamp"<<std::endl);
  618. setTimestamp(readU32(is));
  619. m_disk_timestamp = m_timestamp;
  620. // Dynamically re-set ids based on node names
  621. TRACESTREAM(<<"MapBlock::deSerialize "<<PP(getPos())
  622. <<": NameIdMapping"<<std::endl);
  623. NameIdMapping nimap;
  624. nimap.deSerialize(is);
  625. correctBlockNodeIds(&nimap, data, m_gamedef);
  626. if(version >= 25){
  627. TRACESTREAM(<<"MapBlock::deSerialize "<<PP(getPos())
  628. <<": Node timers (ver>=25)"<<std::endl);
  629. m_node_timers.deSerialize(is, version);
  630. }
  631. }
  632. TRACESTREAM(<<"MapBlock::deSerialize "<<PP(getPos())
  633. <<": Done."<<std::endl);
  634. }
  635. void MapBlock::deSerializeNetworkSpecific(std::istream &is)
  636. {
  637. try {
  638. int version = readU8(is);
  639. //if(version != 1)
  640. // throw SerializationError("unsupported MapBlock version");
  641. if(version >= 1) {
  642. readF1000(is); // deprecated heat
  643. readF1000(is); // deprecated humidity
  644. }
  645. }
  646. catch(SerializationError &e)
  647. {
  648. warningstream<<"MapBlock::deSerializeNetworkSpecific(): Ignoring an error"
  649. <<": "<<e.what()<<std::endl;
  650. }
  651. }
  652. /*
  653. Legacy serialization
  654. */
  655. void MapBlock::deSerialize_pre22(std::istream &is, u8 version, bool disk)
  656. {
  657. // Initialize default flags
  658. is_underground = false;
  659. m_day_night_differs = false;
  660. m_lighting_complete = 0xFFFF;
  661. m_generated = true;
  662. // Make a temporary buffer
  663. u32 ser_length = MapNode::serializedLength(version);
  664. SharedBuffer<u8> databuf_nodelist(nodecount * ser_length);
  665. // These have no compression
  666. if (version <= 3 || version == 5 || version == 6) {
  667. char tmp;
  668. is.read(&tmp, 1);
  669. if (is.gcount() != 1)
  670. throw SerializationError(std::string(FUNCTION_NAME)
  671. + ": not enough input data");
  672. is_underground = tmp;
  673. is.read((char *)*databuf_nodelist, nodecount * ser_length);
  674. if ((u32)is.gcount() != nodecount * ser_length)
  675. throw SerializationError(std::string(FUNCTION_NAME)
  676. + ": not enough input data");
  677. } else if (version <= 10) {
  678. u8 t8;
  679. is.read((char *)&t8, 1);
  680. is_underground = t8;
  681. {
  682. // Uncompress and set material data
  683. std::ostringstream os(std::ios_base::binary);
  684. decompress(is, os, version);
  685. std::string s = os.str();
  686. if (s.size() != nodecount)
  687. throw SerializationError(std::string(FUNCTION_NAME)
  688. + ": not enough input data");
  689. for (u32 i = 0; i < s.size(); i++) {
  690. databuf_nodelist[i*ser_length] = s[i];
  691. }
  692. }
  693. {
  694. // Uncompress and set param data
  695. std::ostringstream os(std::ios_base::binary);
  696. decompress(is, os, version);
  697. std::string s = os.str();
  698. if (s.size() != nodecount)
  699. throw SerializationError(std::string(FUNCTION_NAME)
  700. + ": not enough input data");
  701. for (u32 i = 0; i < s.size(); i++) {
  702. databuf_nodelist[i*ser_length + 1] = s[i];
  703. }
  704. }
  705. if (version >= 10) {
  706. // Uncompress and set param2 data
  707. std::ostringstream os(std::ios_base::binary);
  708. decompress(is, os, version);
  709. std::string s = os.str();
  710. if (s.size() != nodecount)
  711. throw SerializationError(std::string(FUNCTION_NAME)
  712. + ": not enough input data");
  713. for (u32 i = 0; i < s.size(); i++) {
  714. databuf_nodelist[i*ser_length + 2] = s[i];
  715. }
  716. }
  717. } else { // All other versions (10 to 21)
  718. u8 flags;
  719. is.read((char*)&flags, 1);
  720. is_underground = (flags & 0x01) ? true : false;
  721. m_day_night_differs = (flags & 0x02) ? true : false;
  722. if(version >= 18)
  723. m_generated = (flags & 0x08) ? false : true;
  724. // Uncompress data
  725. std::ostringstream os(std::ios_base::binary);
  726. decompress(is, os, version);
  727. std::string s = os.str();
  728. if (s.size() != nodecount * 3)
  729. throw SerializationError(std::string(FUNCTION_NAME)
  730. + ": decompress resulted in size other than nodecount*3");
  731. // deserialize nodes from buffer
  732. for (u32 i = 0; i < nodecount; i++) {
  733. databuf_nodelist[i*ser_length] = s[i];
  734. databuf_nodelist[i*ser_length + 1] = s[i+nodecount];
  735. databuf_nodelist[i*ser_length + 2] = s[i+nodecount*2];
  736. }
  737. /*
  738. NodeMetadata
  739. */
  740. if (version >= 14) {
  741. // Ignore errors
  742. try {
  743. if (version <= 15) {
  744. std::string data = deSerializeString(is);
  745. std::istringstream iss(data, std::ios_base::binary);
  746. content_nodemeta_deserialize_legacy(iss,
  747. &m_node_metadata, &m_node_timers,
  748. m_gamedef->idef());
  749. } else {
  750. //std::string data = deSerializeLongString(is);
  751. std::ostringstream oss(std::ios_base::binary);
  752. decompressZlib(is, oss);
  753. std::istringstream iss(oss.str(), std::ios_base::binary);
  754. content_nodemeta_deserialize_legacy(iss,
  755. &m_node_metadata, &m_node_timers,
  756. m_gamedef->idef());
  757. }
  758. } catch(SerializationError &e) {
  759. warningstream<<"MapBlock::deSerialize(): Ignoring an error"
  760. <<" while deserializing node metadata"<<std::endl;
  761. }
  762. }
  763. }
  764. // Deserialize node data
  765. for (u32 i = 0; i < nodecount; i++) {
  766. data[i].deSerialize(&databuf_nodelist[i * ser_length], version);
  767. }
  768. if (disk) {
  769. /*
  770. Versions up from 9 have block objects. (DEPRECATED)
  771. */
  772. if (version >= 9) {
  773. u16 count = readU16(is);
  774. // Not supported and length not known if count is not 0
  775. if(count != 0){
  776. warningstream<<"MapBlock::deSerialize_pre22(): "
  777. <<"Ignoring stuff coming at and after MBOs"<<std::endl;
  778. return;
  779. }
  780. }
  781. /*
  782. Versions up from 15 have static objects.
  783. */
  784. if (version >= 15)
  785. m_static_objects.deSerialize(is);
  786. // Timestamp
  787. if (version >= 17) {
  788. setTimestamp(readU32(is));
  789. m_disk_timestamp = m_timestamp;
  790. } else {
  791. setTimestamp(BLOCK_TIMESTAMP_UNDEFINED);
  792. }
  793. // Dynamically re-set ids based on node names
  794. NameIdMapping nimap;
  795. // If supported, read node definition id mapping
  796. if (version >= 21) {
  797. nimap.deSerialize(is);
  798. // Else set the legacy mapping
  799. } else {
  800. content_mapnode_get_name_id_mapping(&nimap);
  801. }
  802. correctBlockNodeIds(&nimap, data, m_gamedef);
  803. }
  804. // Legacy data changes
  805. // This code has to convert from pre-22 to post-22 format.
  806. INodeDefManager *nodedef = m_gamedef->ndef();
  807. for(u32 i=0; i<nodecount; i++)
  808. {
  809. const ContentFeatures &f = nodedef->get(data[i].getContent());
  810. // Mineral
  811. if(nodedef->getId("default:stone") == data[i].getContent()
  812. && data[i].getParam1() == 1)
  813. {
  814. data[i].setContent(nodedef->getId("default:stone_with_coal"));
  815. data[i].setParam1(0);
  816. }
  817. else if(nodedef->getId("default:stone") == data[i].getContent()
  818. && data[i].getParam1() == 2)
  819. {
  820. data[i].setContent(nodedef->getId("default:stone_with_iron"));
  821. data[i].setParam1(0);
  822. }
  823. // facedir_simple
  824. if(f.legacy_facedir_simple)
  825. {
  826. data[i].setParam2(data[i].getParam1());
  827. data[i].setParam1(0);
  828. }
  829. // wall_mounted
  830. if(f.legacy_wallmounted)
  831. {
  832. u8 wallmounted_new_to_old[8] = {0x04, 0x08, 0x01, 0x02, 0x10, 0x20, 0, 0};
  833. u8 dir_old_format = data[i].getParam2();
  834. u8 dir_new_format = 0;
  835. for(u8 j=0; j<8; j++)
  836. {
  837. if((dir_old_format & wallmounted_new_to_old[j]) != 0)
  838. {
  839. dir_new_format = j;
  840. break;
  841. }
  842. }
  843. data[i].setParam2(dir_new_format);
  844. }
  845. }
  846. }
  847. /*
  848. Get a quick string to describe what a block actually contains
  849. */
  850. std::string analyze_block(MapBlock *block)
  851. {
  852. if(block == NULL)
  853. return "NULL";
  854. std::ostringstream desc;
  855. v3s16 p = block->getPos();
  856. char spos[20];
  857. snprintf(spos, 20, "(%2d,%2d,%2d), ", p.X, p.Y, p.Z);
  858. desc<<spos;
  859. switch(block->getModified())
  860. {
  861. case MOD_STATE_CLEAN:
  862. desc<<"CLEAN, ";
  863. break;
  864. case MOD_STATE_WRITE_AT_UNLOAD:
  865. desc<<"WRITE_AT_UNLOAD, ";
  866. break;
  867. case MOD_STATE_WRITE_NEEDED:
  868. desc<<"WRITE_NEEDED, ";
  869. break;
  870. default:
  871. desc<<"unknown getModified()="+itos(block->getModified())+", ";
  872. }
  873. if(block->isGenerated())
  874. desc<<"is_gen [X], ";
  875. else
  876. desc<<"is_gen [ ], ";
  877. if(block->getIsUnderground())
  878. desc<<"is_ug [X], ";
  879. else
  880. desc<<"is_ug [ ], ";
  881. desc<<"lighting_complete: "<<block->getLightingComplete()<<", ";
  882. if(block->isDummy())
  883. {
  884. desc<<"Dummy, ";
  885. }
  886. else
  887. {
  888. bool full_ignore = true;
  889. bool some_ignore = false;
  890. bool full_air = true;
  891. bool some_air = false;
  892. for(s16 z0=0; z0<MAP_BLOCKSIZE; z0++)
  893. for(s16 y0=0; y0<MAP_BLOCKSIZE; y0++)
  894. for(s16 x0=0; x0<MAP_BLOCKSIZE; x0++)
  895. {
  896. v3s16 p(x0,y0,z0);
  897. MapNode n = block->getNodeNoEx(p);
  898. content_t c = n.getContent();
  899. if(c == CONTENT_IGNORE)
  900. some_ignore = true;
  901. else
  902. full_ignore = false;
  903. if(c == CONTENT_AIR)
  904. some_air = true;
  905. else
  906. full_air = false;
  907. }
  908. desc<<"content {";
  909. std::ostringstream ss;
  910. if(full_ignore)
  911. ss<<"IGNORE (full), ";
  912. else if(some_ignore)
  913. ss<<"IGNORE, ";
  914. if(full_air)
  915. ss<<"AIR (full), ";
  916. else if(some_air)
  917. ss<<"AIR, ";
  918. if(ss.str().size()>=2)
  919. desc<<ss.str().substr(0, ss.str().size()-2);
  920. desc<<"}, ";
  921. }
  922. return desc.str().substr(0, desc.str().size()-2);
  923. }
  924. //END