mapnode.cpp 21 KB

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