mapnode.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785
  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 "irrlichttypes_extrabloated.h"
  17. #include "mapnode.h"
  18. #include "porting.h"
  19. #include "nodedef.h"
  20. #include "map.h"
  21. #include "content_mapnode.h" // For mapnode_translate_*_internal
  22. #include "serialization.h" // For ser_ver_supported
  23. #include "util/serialize.h"
  24. #include "log.h"
  25. #include "util/directiontables.h"
  26. #include "util/numeric.h"
  27. #include <string>
  28. #include <sstream>
  29. static const Rotation wallmounted_to_rot[] = {
  30. ROTATE_0, ROTATE_180, ROTATE_90, ROTATE_270
  31. };
  32. static const u8 rot_to_wallmounted[] = {
  33. 2, 4, 3, 5
  34. };
  35. /*
  36. MapNode
  37. */
  38. void MapNode::getColor(const ContentFeatures &f, video::SColor *color) const
  39. {
  40. if (f.palette) {
  41. *color = (*f.palette)[param2];
  42. return;
  43. }
  44. *color = f.color;
  45. }
  46. u8 MapNode::getFaceDir(const NodeDefManager *nodemgr,
  47. bool allow_wallmounted) const
  48. {
  49. const ContentFeatures &f = nodemgr->get(*this);
  50. if (f.param_type_2 == CPT2_FACEDIR ||
  51. f.param_type_2 == CPT2_COLORED_FACEDIR)
  52. return (getParam2() & 0x1F) % 24;
  53. if (f.param_type_2 == CPT2_4DIR ||
  54. f.param_type_2 == CPT2_COLORED_4DIR)
  55. return getParam2() & 0x03;
  56. if (allow_wallmounted && (f.param_type_2 == CPT2_WALLMOUNTED ||
  57. f.param_type_2 == CPT2_COLORED_WALLMOUNTED))
  58. return wallmounted_to_facedir[getParam2() & 0x07];
  59. return 0;
  60. }
  61. u8 MapNode::getWallMounted(const NodeDefManager *nodemgr) const
  62. {
  63. const ContentFeatures &f = nodemgr->get(*this);
  64. if (f.param_type_2 == CPT2_WALLMOUNTED ||
  65. f.param_type_2 == CPT2_COLORED_WALLMOUNTED) {
  66. return getParam2() & 0x07;
  67. } else if (f.drawtype == NDT_SIGNLIKE || f.drawtype == NDT_TORCHLIKE ||
  68. f.drawtype == NDT_PLANTLIKE ||
  69. f.drawtype == NDT_PLANTLIKE_ROOTED) {
  70. return 1;
  71. }
  72. return 0;
  73. }
  74. v3s16 MapNode::getWallMountedDir(const NodeDefManager *nodemgr) const
  75. {
  76. switch(getWallMounted(nodemgr))
  77. {
  78. case 0: default: return v3s16(0,1,0);
  79. case 1: return v3s16(0,-1,0);
  80. case 2: return v3s16(1,0,0);
  81. case 3: return v3s16(-1,0,0);
  82. case 4: return v3s16(0,0,1);
  83. case 5: return v3s16(0,0,-1);
  84. }
  85. }
  86. u8 MapNode::getDegRotate(const NodeDefManager *nodemgr) const
  87. {
  88. const ContentFeatures &f = nodemgr->get(*this);
  89. if (f.param_type_2 == CPT2_DEGROTATE)
  90. return getParam2() % 240;
  91. if (f.param_type_2 == CPT2_COLORED_DEGROTATE)
  92. return 10 * ((getParam2() & 0x1F) % 24);
  93. return 0;
  94. }
  95. void MapNode::rotateAlongYAxis(const NodeDefManager *nodemgr, Rotation rot)
  96. {
  97. ContentParamType2 cpt2 = nodemgr->get(*this).param_type_2;
  98. if (cpt2 == CPT2_FACEDIR || cpt2 == CPT2_COLORED_FACEDIR ||
  99. cpt2 == CPT2_4DIR || cpt2 == CPT2_COLORED_4DIR) {
  100. static const u8 rotate_facedir[24 * 4] = {
  101. // Table value = rotated facedir
  102. // Columns: 0, 90, 180, 270 degrees rotation around vertical axis
  103. // Rotation is anticlockwise as seen from above (+Y)
  104. 0, 1, 2, 3, // Initial facedir 0 to 3
  105. 1, 2, 3, 0,
  106. 2, 3, 0, 1,
  107. 3, 0, 1, 2,
  108. 4, 13, 10, 19, // 4 to 7
  109. 5, 14, 11, 16,
  110. 6, 15, 8, 17,
  111. 7, 12, 9, 18,
  112. 8, 17, 6, 15, // 8 to 11
  113. 9, 18, 7, 12,
  114. 10, 19, 4, 13,
  115. 11, 16, 5, 14,
  116. 12, 9, 18, 7, // 12 to 15
  117. 13, 10, 19, 4,
  118. 14, 11, 16, 5,
  119. 15, 8, 17, 6,
  120. 16, 5, 14, 11, // 16 to 19
  121. 17, 6, 15, 8,
  122. 18, 7, 12, 9,
  123. 19, 4, 13, 10,
  124. 20, 23, 22, 21, // 20 to 23
  125. 21, 20, 23, 22,
  126. 22, 21, 20, 23,
  127. 23, 22, 21, 20
  128. };
  129. if (cpt2 == CPT2_FACEDIR || cpt2 == CPT2_COLORED_FACEDIR) {
  130. u8 facedir = (param2 & 31) % 24;
  131. u8 index = facedir * 4 + rot;
  132. param2 &= ~31;
  133. param2 |= rotate_facedir[index];
  134. } else if (cpt2 == CPT2_4DIR || cpt2 == CPT2_COLORED_4DIR) {
  135. u8 fourdir = param2 & 3;
  136. u8 index = fourdir + rot;
  137. param2 &= ~3;
  138. param2 |= rotate_facedir[index];
  139. }
  140. } else if (cpt2 == CPT2_WALLMOUNTED ||
  141. cpt2 == CPT2_COLORED_WALLMOUNTED) {
  142. u8 wmountface = (param2 & 7);
  143. if (wmountface <= 1)
  144. return;
  145. Rotation oldrot = wallmounted_to_rot[wmountface - 2];
  146. param2 &= ~7;
  147. param2 |= rot_to_wallmounted[(oldrot - rot) & 3];
  148. } else if (cpt2 == CPT2_DEGROTATE) {
  149. int angle = param2; // in 1.5°
  150. angle += 60 * rot; // don’t do that on u8
  151. angle %= 240;
  152. param2 = angle;
  153. } else if (cpt2 == CPT2_COLORED_DEGROTATE) {
  154. int angle = param2 & 0x1F; // in 15°
  155. int color = param2 & 0xE0;
  156. angle += 6 * rot;
  157. angle %= 24;
  158. param2 = color | angle;
  159. }
  160. }
  161. void transformNodeBox(const MapNode &n, const NodeBox &nodebox,
  162. const NodeDefManager *nodemgr, std::vector<aabb3f> *p_boxes,
  163. u8 neighbors = 0)
  164. {
  165. std::vector<aabb3f> &boxes = *p_boxes;
  166. if (nodebox.type == NODEBOX_FIXED || nodebox.type == NODEBOX_LEVELED) {
  167. const auto &fixed = nodebox.fixed;
  168. int facedir = n.getFaceDir(nodemgr, true);
  169. u8 axisdir = facedir>>2;
  170. facedir&=0x03;
  171. boxes.reserve(boxes.size() + fixed.size());
  172. for (aabb3f box : fixed) {
  173. if (nodebox.type == NODEBOX_LEVELED)
  174. box.MaxEdge.Y = (-0.5f + n.getLevel(nodemgr) / 64.0f) * BS;
  175. switch (axisdir) {
  176. case 0:
  177. if(facedir == 1)
  178. {
  179. box.MinEdge.rotateXZBy(-90);
  180. box.MaxEdge.rotateXZBy(-90);
  181. }
  182. else if(facedir == 2)
  183. {
  184. box.MinEdge.rotateXZBy(180);
  185. box.MaxEdge.rotateXZBy(180);
  186. }
  187. else if(facedir == 3)
  188. {
  189. box.MinEdge.rotateXZBy(90);
  190. box.MaxEdge.rotateXZBy(90);
  191. }
  192. break;
  193. case 1: // z+
  194. box.MinEdge.rotateYZBy(90);
  195. box.MaxEdge.rotateYZBy(90);
  196. if(facedir == 1)
  197. {
  198. box.MinEdge.rotateXYBy(90);
  199. box.MaxEdge.rotateXYBy(90);
  200. }
  201. else if(facedir == 2)
  202. {
  203. box.MinEdge.rotateXYBy(180);
  204. box.MaxEdge.rotateXYBy(180);
  205. }
  206. else if(facedir == 3)
  207. {
  208. box.MinEdge.rotateXYBy(-90);
  209. box.MaxEdge.rotateXYBy(-90);
  210. }
  211. break;
  212. case 2: //z-
  213. box.MinEdge.rotateYZBy(-90);
  214. box.MaxEdge.rotateYZBy(-90);
  215. if(facedir == 1)
  216. {
  217. box.MinEdge.rotateXYBy(-90);
  218. box.MaxEdge.rotateXYBy(-90);
  219. }
  220. else if(facedir == 2)
  221. {
  222. box.MinEdge.rotateXYBy(180);
  223. box.MaxEdge.rotateXYBy(180);
  224. }
  225. else if(facedir == 3)
  226. {
  227. box.MinEdge.rotateXYBy(90);
  228. box.MaxEdge.rotateXYBy(90);
  229. }
  230. break;
  231. case 3: //x+
  232. box.MinEdge.rotateXYBy(-90);
  233. box.MaxEdge.rotateXYBy(-90);
  234. if(facedir == 1)
  235. {
  236. box.MinEdge.rotateYZBy(90);
  237. box.MaxEdge.rotateYZBy(90);
  238. }
  239. else if(facedir == 2)
  240. {
  241. box.MinEdge.rotateYZBy(180);
  242. box.MaxEdge.rotateYZBy(180);
  243. }
  244. else if(facedir == 3)
  245. {
  246. box.MinEdge.rotateYZBy(-90);
  247. box.MaxEdge.rotateYZBy(-90);
  248. }
  249. break;
  250. case 4: //x-
  251. box.MinEdge.rotateXYBy(90);
  252. box.MaxEdge.rotateXYBy(90);
  253. if(facedir == 1)
  254. {
  255. box.MinEdge.rotateYZBy(-90);
  256. box.MaxEdge.rotateYZBy(-90);
  257. }
  258. else if(facedir == 2)
  259. {
  260. box.MinEdge.rotateYZBy(180);
  261. box.MaxEdge.rotateYZBy(180);
  262. }
  263. else if(facedir == 3)
  264. {
  265. box.MinEdge.rotateYZBy(90);
  266. box.MaxEdge.rotateYZBy(90);
  267. }
  268. break;
  269. case 5:
  270. box.MinEdge.rotateXYBy(-180);
  271. box.MaxEdge.rotateXYBy(-180);
  272. if(facedir == 1)
  273. {
  274. box.MinEdge.rotateXZBy(90);
  275. box.MaxEdge.rotateXZBy(90);
  276. }
  277. else if(facedir == 2)
  278. {
  279. box.MinEdge.rotateXZBy(180);
  280. box.MaxEdge.rotateXZBy(180);
  281. }
  282. else if(facedir == 3)
  283. {
  284. box.MinEdge.rotateXZBy(-90);
  285. box.MaxEdge.rotateXZBy(-90);
  286. }
  287. break;
  288. default:
  289. break;
  290. }
  291. box.repair();
  292. boxes.push_back(box);
  293. }
  294. }
  295. else if(nodebox.type == NODEBOX_WALLMOUNTED)
  296. {
  297. v3s16 dir = n.getWallMountedDir(nodemgr);
  298. // top
  299. if(dir == v3s16(0,1,0))
  300. {
  301. boxes.push_back(nodebox.wall_top);
  302. }
  303. // bottom
  304. else if(dir == v3s16(0,-1,0))
  305. {
  306. boxes.push_back(nodebox.wall_bottom);
  307. }
  308. // side
  309. else
  310. {
  311. v3f vertices[2] =
  312. {
  313. nodebox.wall_side.MinEdge,
  314. nodebox.wall_side.MaxEdge
  315. };
  316. for (v3f &vertex : vertices) {
  317. if(dir == v3s16(-1,0,0))
  318. vertex.rotateXZBy(0);
  319. if(dir == v3s16(1,0,0))
  320. vertex.rotateXZBy(180);
  321. if(dir == v3s16(0,0,-1))
  322. vertex.rotateXZBy(90);
  323. if(dir == v3s16(0,0,1))
  324. vertex.rotateXZBy(-90);
  325. }
  326. aabb3f box = aabb3f(vertices[0]);
  327. box.addInternalPoint(vertices[1]);
  328. boxes.push_back(box);
  329. }
  330. }
  331. else if (nodebox.type == NODEBOX_CONNECTED)
  332. {
  333. size_t boxes_size = boxes.size();
  334. boxes_size += nodebox.fixed.size();
  335. const auto &c = nodebox.getConnected();
  336. if (neighbors & 1)
  337. boxes_size += c.connect_top.size();
  338. else
  339. boxes_size += c.disconnected_top.size();
  340. if (neighbors & 2)
  341. boxes_size += c.connect_bottom.size();
  342. else
  343. boxes_size += c.disconnected_bottom.size();
  344. if (neighbors & 4)
  345. boxes_size += c.connect_front.size();
  346. else
  347. boxes_size += c.disconnected_front.size();
  348. if (neighbors & 8)
  349. boxes_size += c.connect_left.size();
  350. else
  351. boxes_size += c.disconnected_left.size();
  352. if (neighbors & 16)
  353. boxes_size += c.connect_back.size();
  354. else
  355. boxes_size += c.disconnected_back.size();
  356. if (neighbors & 32)
  357. boxes_size += c.connect_right.size();
  358. else
  359. boxes_size += c.disconnected_right.size();
  360. if (neighbors == 0)
  361. boxes_size += c.disconnected.size();
  362. if (neighbors < 4)
  363. boxes_size += c.disconnected_sides.size();
  364. boxes.reserve(boxes_size);
  365. #define BOXESPUSHBACK(c) \
  366. for (std::vector<aabb3f>::const_iterator \
  367. it = (c).begin(); \
  368. it != (c).end(); ++it) \
  369. (boxes).push_back(*it);
  370. BOXESPUSHBACK(nodebox.fixed);
  371. if (neighbors & 1) {
  372. BOXESPUSHBACK(c.connect_top);
  373. } else {
  374. BOXESPUSHBACK(c.disconnected_top);
  375. }
  376. if (neighbors & 2) {
  377. BOXESPUSHBACK(c.connect_bottom);
  378. } else {
  379. BOXESPUSHBACK(c.disconnected_bottom);
  380. }
  381. if (neighbors & 4) {
  382. BOXESPUSHBACK(c.connect_front);
  383. } else {
  384. BOXESPUSHBACK(c.disconnected_front);
  385. }
  386. if (neighbors & 8) {
  387. BOXESPUSHBACK(c.connect_left);
  388. } else {
  389. BOXESPUSHBACK(c.disconnected_left);
  390. }
  391. if (neighbors & 16) {
  392. BOXESPUSHBACK(c.connect_back);
  393. } else {
  394. BOXESPUSHBACK(c.disconnected_back);
  395. }
  396. if (neighbors & 32) {
  397. BOXESPUSHBACK(c.connect_right);
  398. } else {
  399. BOXESPUSHBACK(c.disconnected_right);
  400. }
  401. if (neighbors == 0) {
  402. BOXESPUSHBACK(c.disconnected);
  403. }
  404. if (neighbors < 4) {
  405. BOXESPUSHBACK(c.disconnected_sides);
  406. }
  407. }
  408. else // NODEBOX_REGULAR
  409. {
  410. boxes.emplace_back(-BS/2,-BS/2,-BS/2,BS/2,BS/2,BS/2);
  411. }
  412. }
  413. static inline void getNeighborConnectingFace(
  414. const v3s16 &p, const NodeDefManager *nodedef,
  415. Map *map, MapNode n, u8 bitmask, u8 *neighbors)
  416. {
  417. MapNode n2 = map->getNode(p);
  418. if (nodedef->nodeboxConnects(n, n2, bitmask))
  419. *neighbors |= bitmask;
  420. }
  421. u8 MapNode::getNeighbors(v3s16 p, Map *map) const
  422. {
  423. const NodeDefManager *nodedef = map->getNodeDefManager();
  424. u8 neighbors = 0;
  425. const ContentFeatures &f = nodedef->get(*this);
  426. // locate possible neighboring nodes to connect to
  427. if (f.drawtype == NDT_NODEBOX && f.node_box.type == NODEBOX_CONNECTED) {
  428. v3s16 p2 = p;
  429. p2.Y++;
  430. getNeighborConnectingFace(p2, nodedef, map, *this, 1, &neighbors);
  431. p2 = p;
  432. p2.Y--;
  433. getNeighborConnectingFace(p2, nodedef, map, *this, 2, &neighbors);
  434. p2 = p;
  435. p2.Z--;
  436. getNeighborConnectingFace(p2, nodedef, map, *this, 4, &neighbors);
  437. p2 = p;
  438. p2.X--;
  439. getNeighborConnectingFace(p2, nodedef, map, *this, 8, &neighbors);
  440. p2 = p;
  441. p2.Z++;
  442. getNeighborConnectingFace(p2, nodedef, map, *this, 16, &neighbors);
  443. p2 = p;
  444. p2.X++;
  445. getNeighborConnectingFace(p2, nodedef, map, *this, 32, &neighbors);
  446. }
  447. return neighbors;
  448. }
  449. void MapNode::getNodeBoxes(const NodeDefManager *nodemgr,
  450. std::vector<aabb3f> *boxes, u8 neighbors) const
  451. {
  452. const ContentFeatures &f = nodemgr->get(*this);
  453. transformNodeBox(*this, f.node_box, nodemgr, boxes, neighbors);
  454. }
  455. void MapNode::getCollisionBoxes(const NodeDefManager *nodemgr,
  456. std::vector<aabb3f> *boxes, u8 neighbors) const
  457. {
  458. const ContentFeatures &f = nodemgr->get(*this);
  459. if (f.collision_box.fixed.empty())
  460. transformNodeBox(*this, f.node_box, nodemgr, boxes, neighbors);
  461. else
  462. transformNodeBox(*this, f.collision_box, nodemgr, boxes, neighbors);
  463. }
  464. void MapNode::getSelectionBoxes(const NodeDefManager *nodemgr,
  465. std::vector<aabb3f> *boxes, u8 neighbors) const
  466. {
  467. const ContentFeatures &f = nodemgr->get(*this);
  468. transformNodeBox(*this, f.selection_box, nodemgr, boxes, neighbors);
  469. }
  470. u8 MapNode::getMaxLevel(const NodeDefManager *nodemgr) const
  471. {
  472. const ContentFeatures &f = nodemgr->get(*this);
  473. // todo: after update in all games leave only if (f.param_type_2 ==
  474. if( f.liquid_type == LIQUID_FLOWING || f.param_type_2 == CPT2_FLOWINGLIQUID)
  475. return LIQUID_LEVEL_MAX;
  476. if(f.leveled || f.param_type_2 == CPT2_LEVELED)
  477. return f.leveled_max;
  478. return 0;
  479. }
  480. u8 MapNode::getLevel(const NodeDefManager *nodemgr) const
  481. {
  482. const ContentFeatures &f = nodemgr->get(*this);
  483. // todo: after update in all games leave only if (f.param_type_2 ==
  484. if(f.liquid_type == LIQUID_SOURCE)
  485. return LIQUID_LEVEL_SOURCE;
  486. if (f.param_type_2 == CPT2_FLOWINGLIQUID)
  487. return getParam2() & LIQUID_LEVEL_MASK;
  488. if(f.liquid_type == LIQUID_FLOWING) // can remove if all param_type_2 set
  489. return getParam2() & LIQUID_LEVEL_MASK;
  490. if (f.param_type_2 == CPT2_LEVELED) {
  491. u8 level = getParam2() & LEVELED_MASK;
  492. if (level)
  493. return level;
  494. }
  495. // Return static value from nodedef if param2 isn't used for level
  496. if (f.leveled > f.leveled_max)
  497. return f.leveled_max;
  498. return f.leveled;
  499. }
  500. s8 MapNode::setLevel(const NodeDefManager *nodemgr, s16 level)
  501. {
  502. s8 rest = 0;
  503. const ContentFeatures &f = nodemgr->get(*this);
  504. if (f.param_type_2 == CPT2_FLOWINGLIQUID
  505. || f.liquid_type == LIQUID_FLOWING
  506. || f.liquid_type == LIQUID_SOURCE) {
  507. if (level <= 0) { // liquid can’t exist with zero level
  508. setContent(CONTENT_AIR);
  509. return 0;
  510. }
  511. if (level >= LIQUID_LEVEL_SOURCE) {
  512. rest = level - LIQUID_LEVEL_SOURCE;
  513. setContent(f.liquid_alternative_source_id);
  514. setParam2(0);
  515. } else {
  516. setContent(f.liquid_alternative_flowing_id);
  517. setParam2((level & LIQUID_LEVEL_MASK) | (getParam2() & ~LIQUID_LEVEL_MASK));
  518. }
  519. } else if (f.param_type_2 == CPT2_LEVELED) {
  520. if (level < 0) { // zero means default for a leveled nodebox
  521. rest = level;
  522. level = 0;
  523. } else if (level > f.leveled_max) {
  524. rest = level - f.leveled_max;
  525. level = f.leveled_max;
  526. }
  527. setParam2((level & LEVELED_MASK) | (getParam2() & ~LEVELED_MASK));
  528. }
  529. return rest;
  530. }
  531. s8 MapNode::addLevel(const NodeDefManager *nodemgr, s16 add)
  532. {
  533. s16 level = getLevel(nodemgr);
  534. level += add;
  535. return setLevel(nodemgr, level);
  536. }
  537. u32 MapNode::serializedLength(u8 version)
  538. {
  539. if(!ser_ver_supported(version))
  540. throw VersionMismatchException("ERROR: MapNode format not supported");
  541. if (version == 0)
  542. return 1;
  543. if (version <= 9)
  544. return 2;
  545. if (version <= 23)
  546. return 3;
  547. return 4;
  548. }
  549. void MapNode::serialize(u8 *dest, u8 version) const
  550. {
  551. if(!ser_ver_supported(version))
  552. throw VersionMismatchException("ERROR: MapNode format not supported");
  553. // Can't do this anymore; we have 16-bit dynamically allocated node IDs
  554. // in memory; conversion just won't work in this direction.
  555. if(version < 24)
  556. throw SerializationError("MapNode::serialize: serialization to "
  557. "version < 24 not possible");
  558. writeU16(dest+0, param0);
  559. writeU8(dest+2, param1);
  560. writeU8(dest+3, param2);
  561. }
  562. void MapNode::deSerialize(u8 *source, u8 version)
  563. {
  564. if(!ser_ver_supported(version))
  565. throw VersionMismatchException("ERROR: MapNode format not supported");
  566. if(version <= 21)
  567. {
  568. deSerialize_pre22(source, version);
  569. return;
  570. }
  571. if(version >= 24){
  572. param0 = readU16(source+0);
  573. param1 = readU8(source+2);
  574. param2 = readU8(source+3);
  575. }else{
  576. param0 = readU8(source+0);
  577. param1 = readU8(source+1);
  578. param2 = readU8(source+2);
  579. if(param0 > 0x7F){
  580. param0 |= ((param2&0xF0)<<4);
  581. param2 &= 0x0F;
  582. }
  583. }
  584. }
  585. SharedBuffer<u8> MapNode::serializeBulk(int version,
  586. const MapNode *nodes, u32 nodecount,
  587. u8 content_width, u8 params_width)
  588. {
  589. if (!ser_ver_supported(version))
  590. throw VersionMismatchException("ERROR: MapNode format not supported");
  591. sanity_check(content_width == 2);
  592. sanity_check(params_width == 2);
  593. // Can't do this anymore; we have 16-bit dynamically allocated node IDs
  594. // in memory; conversion just won't work in this direction.
  595. if (version < 24)
  596. throw SerializationError("MapNode::serializeBulk: serialization to "
  597. "version < 24 not possible");
  598. SharedBuffer<u8> databuf(nodecount * (content_width + params_width));
  599. u32 start1 = content_width * nodecount;
  600. u32 start2 = (content_width + 1) * nodecount;
  601. // Serialize content
  602. for (u32 i = 0; i < nodecount; i++) {
  603. writeU16(&databuf[i * 2], nodes[i].param0);
  604. writeU8(&databuf[start1 + i], nodes[i].param1);
  605. writeU8(&databuf[start2 + i], nodes[i].param2);
  606. }
  607. return databuf;
  608. }
  609. // Deserialize bulk node data
  610. void MapNode::deSerializeBulk(std::istream &is, int version,
  611. MapNode *nodes, u32 nodecount,
  612. u8 content_width, u8 params_width)
  613. {
  614. if(!ser_ver_supported(version))
  615. throw VersionMismatchException("ERROR: MapNode format not supported");
  616. if (version < 22
  617. || (content_width != 1 && content_width != 2)
  618. || params_width != 2)
  619. FATAL_ERROR("Deserialize bulk node data error");
  620. // read data
  621. const u32 len = nodecount * (content_width + params_width);
  622. Buffer<u8> databuf(len);
  623. is.read(reinterpret_cast<char*>(*databuf), len);
  624. // Deserialize content
  625. if(content_width == 1)
  626. {
  627. for(u32 i=0; i<nodecount; i++)
  628. nodes[i].param0 = readU8(&databuf[i]);
  629. }
  630. else if(content_width == 2)
  631. {
  632. for(u32 i=0; i<nodecount; i++)
  633. nodes[i].param0 = readU16(&databuf[i*2]);
  634. }
  635. // Deserialize param1
  636. u32 start1 = content_width * nodecount;
  637. for(u32 i=0; i<nodecount; i++)
  638. nodes[i].param1 = readU8(&databuf[start1 + i]);
  639. // Deserialize param2
  640. u32 start2 = (content_width + 1) * nodecount;
  641. if(content_width == 1)
  642. {
  643. for(u32 i=0; i<nodecount; i++) {
  644. nodes[i].param2 = readU8(&databuf[start2 + i]);
  645. if(nodes[i].param0 > 0x7F){
  646. nodes[i].param0 <<= 4;
  647. nodes[i].param0 |= (nodes[i].param2&0xF0)>>4;
  648. nodes[i].param2 &= 0x0F;
  649. }
  650. }
  651. }
  652. else if(content_width == 2)
  653. {
  654. for(u32 i=0; i<nodecount; i++)
  655. nodes[i].param2 = readU8(&databuf[start2 + i]);
  656. }
  657. }
  658. /*
  659. Legacy serialization
  660. */
  661. void MapNode::deSerialize_pre22(const u8 *source, u8 version)
  662. {
  663. if(version <= 1)
  664. {
  665. param0 = source[0];
  666. }
  667. else if(version <= 9)
  668. {
  669. param0 = source[0];
  670. param1 = source[1];
  671. }
  672. else
  673. {
  674. param0 = source[0];
  675. param1 = source[1];
  676. param2 = source[2];
  677. if(param0 > 0x7f){
  678. param0 <<= 4;
  679. param0 |= (param2&0xf0)>>4;
  680. param2 &= 0x0f;
  681. }
  682. }
  683. // Convert special values from old version to new
  684. if(version <= 19)
  685. {
  686. // In these versions, CONTENT_IGNORE and CONTENT_AIR
  687. // are 255 and 254
  688. // Version 19 is messed up with sometimes the old values and sometimes not
  689. if(param0 == 255)
  690. param0 = CONTENT_IGNORE;
  691. else if(param0 == 254)
  692. param0 = CONTENT_AIR;
  693. }
  694. // Translate to our known version
  695. *this = mapnode_translate_to_internal(*this, version);
  696. }