mapnode.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863
  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 auto &fixed = nodebox.fixed;
  235. int facedir = n.getFaceDir(nodemgr, true);
  236. u8 axisdir = facedir>>2;
  237. facedir&=0x03;
  238. boxes.reserve(boxes.size() + fixed.size());
  239. for (aabb3f box : fixed) {
  240. if (nodebox.type == NODEBOX_LEVELED)
  241. box.MaxEdge.Y = (-0.5f + n.getLevel(nodemgr) / 64.0f) * BS;
  242. switch (axisdir) {
  243. case 0:
  244. if(facedir == 1)
  245. {
  246. box.MinEdge.rotateXZBy(-90);
  247. box.MaxEdge.rotateXZBy(-90);
  248. }
  249. else if(facedir == 2)
  250. {
  251. box.MinEdge.rotateXZBy(180);
  252. box.MaxEdge.rotateXZBy(180);
  253. }
  254. else if(facedir == 3)
  255. {
  256. box.MinEdge.rotateXZBy(90);
  257. box.MaxEdge.rotateXZBy(90);
  258. }
  259. break;
  260. case 1: // z+
  261. box.MinEdge.rotateYZBy(90);
  262. box.MaxEdge.rotateYZBy(90);
  263. if(facedir == 1)
  264. {
  265. box.MinEdge.rotateXYBy(90);
  266. box.MaxEdge.rotateXYBy(90);
  267. }
  268. else if(facedir == 2)
  269. {
  270. box.MinEdge.rotateXYBy(180);
  271. box.MaxEdge.rotateXYBy(180);
  272. }
  273. else if(facedir == 3)
  274. {
  275. box.MinEdge.rotateXYBy(-90);
  276. box.MaxEdge.rotateXYBy(-90);
  277. }
  278. break;
  279. case 2: //z-
  280. box.MinEdge.rotateYZBy(-90);
  281. box.MaxEdge.rotateYZBy(-90);
  282. if(facedir == 1)
  283. {
  284. box.MinEdge.rotateXYBy(-90);
  285. box.MaxEdge.rotateXYBy(-90);
  286. }
  287. else if(facedir == 2)
  288. {
  289. box.MinEdge.rotateXYBy(180);
  290. box.MaxEdge.rotateXYBy(180);
  291. }
  292. else if(facedir == 3)
  293. {
  294. box.MinEdge.rotateXYBy(90);
  295. box.MaxEdge.rotateXYBy(90);
  296. }
  297. break;
  298. case 3: //x+
  299. box.MinEdge.rotateXYBy(-90);
  300. box.MaxEdge.rotateXYBy(-90);
  301. if(facedir == 1)
  302. {
  303. box.MinEdge.rotateYZBy(90);
  304. box.MaxEdge.rotateYZBy(90);
  305. }
  306. else if(facedir == 2)
  307. {
  308. box.MinEdge.rotateYZBy(180);
  309. box.MaxEdge.rotateYZBy(180);
  310. }
  311. else if(facedir == 3)
  312. {
  313. box.MinEdge.rotateYZBy(-90);
  314. box.MaxEdge.rotateYZBy(-90);
  315. }
  316. break;
  317. case 4: //x-
  318. box.MinEdge.rotateXYBy(90);
  319. box.MaxEdge.rotateXYBy(90);
  320. if(facedir == 1)
  321. {
  322. box.MinEdge.rotateYZBy(-90);
  323. box.MaxEdge.rotateYZBy(-90);
  324. }
  325. else if(facedir == 2)
  326. {
  327. box.MinEdge.rotateYZBy(180);
  328. box.MaxEdge.rotateYZBy(180);
  329. }
  330. else if(facedir == 3)
  331. {
  332. box.MinEdge.rotateYZBy(90);
  333. box.MaxEdge.rotateYZBy(90);
  334. }
  335. break;
  336. case 5:
  337. box.MinEdge.rotateXYBy(-180);
  338. box.MaxEdge.rotateXYBy(-180);
  339. if(facedir == 1)
  340. {
  341. box.MinEdge.rotateXZBy(90);
  342. box.MaxEdge.rotateXZBy(90);
  343. }
  344. else if(facedir == 2)
  345. {
  346. box.MinEdge.rotateXZBy(180);
  347. box.MaxEdge.rotateXZBy(180);
  348. }
  349. else if(facedir == 3)
  350. {
  351. box.MinEdge.rotateXZBy(-90);
  352. box.MaxEdge.rotateXZBy(-90);
  353. }
  354. break;
  355. default:
  356. break;
  357. }
  358. box.repair();
  359. boxes.push_back(box);
  360. }
  361. }
  362. else if(nodebox.type == NODEBOX_WALLMOUNTED)
  363. {
  364. v3s16 dir = n.getWallMountedDir(nodemgr);
  365. // top
  366. if(dir == v3s16(0,1,0))
  367. {
  368. boxes.push_back(nodebox.wall_top);
  369. }
  370. // bottom
  371. else if(dir == v3s16(0,-1,0))
  372. {
  373. boxes.push_back(nodebox.wall_bottom);
  374. }
  375. // side
  376. else
  377. {
  378. v3f vertices[2] =
  379. {
  380. nodebox.wall_side.MinEdge,
  381. nodebox.wall_side.MaxEdge
  382. };
  383. for (v3f &vertex : vertices) {
  384. if(dir == v3s16(-1,0,0))
  385. vertex.rotateXZBy(0);
  386. if(dir == v3s16(1,0,0))
  387. vertex.rotateXZBy(180);
  388. if(dir == v3s16(0,0,-1))
  389. vertex.rotateXZBy(90);
  390. if(dir == v3s16(0,0,1))
  391. vertex.rotateXZBy(-90);
  392. }
  393. aabb3f box = aabb3f(vertices[0]);
  394. box.addInternalPoint(vertices[1]);
  395. boxes.push_back(box);
  396. }
  397. }
  398. else if (nodebox.type == NODEBOX_CONNECTED)
  399. {
  400. size_t boxes_size = boxes.size();
  401. boxes_size += nodebox.fixed.size();
  402. const auto &c = nodebox.getConnected();
  403. if (neighbors & 1)
  404. boxes_size += c.connect_top.size();
  405. else
  406. boxes_size += c.disconnected_top.size();
  407. if (neighbors & 2)
  408. boxes_size += c.connect_bottom.size();
  409. else
  410. boxes_size += c.disconnected_bottom.size();
  411. if (neighbors & 4)
  412. boxes_size += c.connect_front.size();
  413. else
  414. boxes_size += c.disconnected_front.size();
  415. if (neighbors & 8)
  416. boxes_size += c.connect_left.size();
  417. else
  418. boxes_size += c.disconnected_left.size();
  419. if (neighbors & 16)
  420. boxes_size += c.connect_back.size();
  421. else
  422. boxes_size += c.disconnected_back.size();
  423. if (neighbors & 32)
  424. boxes_size += c.connect_right.size();
  425. else
  426. boxes_size += c.disconnected_right.size();
  427. if (neighbors == 0)
  428. boxes_size += c.disconnected.size();
  429. if (neighbors < 4)
  430. boxes_size += c.disconnected_sides.size();
  431. boxes.reserve(boxes_size);
  432. #define BOXESPUSHBACK(c) \
  433. for (std::vector<aabb3f>::const_iterator \
  434. it = (c).begin(); \
  435. it != (c).end(); ++it) \
  436. (boxes).push_back(*it);
  437. BOXESPUSHBACK(nodebox.fixed);
  438. if (neighbors & 1) {
  439. BOXESPUSHBACK(c.connect_top);
  440. } else {
  441. BOXESPUSHBACK(c.disconnected_top);
  442. }
  443. if (neighbors & 2) {
  444. BOXESPUSHBACK(c.connect_bottom);
  445. } else {
  446. BOXESPUSHBACK(c.disconnected_bottom);
  447. }
  448. if (neighbors & 4) {
  449. BOXESPUSHBACK(c.connect_front);
  450. } else {
  451. BOXESPUSHBACK(c.disconnected_front);
  452. }
  453. if (neighbors & 8) {
  454. BOXESPUSHBACK(c.connect_left);
  455. } else {
  456. BOXESPUSHBACK(c.disconnected_left);
  457. }
  458. if (neighbors & 16) {
  459. BOXESPUSHBACK(c.connect_back);
  460. } else {
  461. BOXESPUSHBACK(c.disconnected_back);
  462. }
  463. if (neighbors & 32) {
  464. BOXESPUSHBACK(c.connect_right);
  465. } else {
  466. BOXESPUSHBACK(c.disconnected_right);
  467. }
  468. if (neighbors == 0) {
  469. BOXESPUSHBACK(c.disconnected);
  470. }
  471. if (neighbors < 4) {
  472. BOXESPUSHBACK(c.disconnected_sides);
  473. }
  474. }
  475. else // NODEBOX_REGULAR
  476. {
  477. boxes.emplace_back(-BS/2,-BS/2,-BS/2,BS/2,BS/2,BS/2);
  478. }
  479. }
  480. static inline void getNeighborConnectingFace(
  481. const v3s16 &p, const NodeDefManager *nodedef,
  482. Map *map, MapNode n, u8 bitmask, u8 *neighbors)
  483. {
  484. MapNode n2 = map->getNode(p);
  485. if (nodedef->nodeboxConnects(n, n2, bitmask))
  486. *neighbors |= bitmask;
  487. }
  488. u8 MapNode::getNeighbors(v3s16 p, Map *map) const
  489. {
  490. const NodeDefManager *nodedef = map->getNodeDefManager();
  491. u8 neighbors = 0;
  492. const ContentFeatures &f = nodedef->get(*this);
  493. // locate possible neighboring nodes to connect to
  494. if (f.drawtype == NDT_NODEBOX && f.node_box.type == NODEBOX_CONNECTED) {
  495. v3s16 p2 = p;
  496. p2.Y++;
  497. getNeighborConnectingFace(p2, nodedef, map, *this, 1, &neighbors);
  498. p2 = p;
  499. p2.Y--;
  500. getNeighborConnectingFace(p2, nodedef, map, *this, 2, &neighbors);
  501. p2 = p;
  502. p2.Z--;
  503. getNeighborConnectingFace(p2, nodedef, map, *this, 4, &neighbors);
  504. p2 = p;
  505. p2.X--;
  506. getNeighborConnectingFace(p2, nodedef, map, *this, 8, &neighbors);
  507. p2 = p;
  508. p2.Z++;
  509. getNeighborConnectingFace(p2, nodedef, map, *this, 16, &neighbors);
  510. p2 = p;
  511. p2.X++;
  512. getNeighborConnectingFace(p2, nodedef, map, *this, 32, &neighbors);
  513. }
  514. return neighbors;
  515. }
  516. void MapNode::getNodeBoxes(const NodeDefManager *nodemgr,
  517. std::vector<aabb3f> *boxes, u8 neighbors) const
  518. {
  519. const ContentFeatures &f = nodemgr->get(*this);
  520. transformNodeBox(*this, f.node_box, nodemgr, boxes, neighbors);
  521. }
  522. void MapNode::getCollisionBoxes(const NodeDefManager *nodemgr,
  523. std::vector<aabb3f> *boxes, u8 neighbors) const
  524. {
  525. const ContentFeatures &f = nodemgr->get(*this);
  526. if (f.collision_box.fixed.empty())
  527. transformNodeBox(*this, f.node_box, nodemgr, boxes, neighbors);
  528. else
  529. transformNodeBox(*this, f.collision_box, nodemgr, boxes, neighbors);
  530. }
  531. void MapNode::getSelectionBoxes(const NodeDefManager *nodemgr,
  532. std::vector<aabb3f> *boxes, u8 neighbors) const
  533. {
  534. const ContentFeatures &f = nodemgr->get(*this);
  535. transformNodeBox(*this, f.selection_box, nodemgr, boxes, neighbors);
  536. }
  537. u8 MapNode::getMaxLevel(const NodeDefManager *nodemgr) const
  538. {
  539. const ContentFeatures &f = nodemgr->get(*this);
  540. // todo: after update in all games leave only if (f.param_type_2 ==
  541. if( f.liquid_type == LIQUID_FLOWING || f.param_type_2 == CPT2_FLOWINGLIQUID)
  542. return LIQUID_LEVEL_MAX;
  543. if(f.leveled || f.param_type_2 == CPT2_LEVELED)
  544. return f.leveled_max;
  545. return 0;
  546. }
  547. u8 MapNode::getLevel(const NodeDefManager *nodemgr) const
  548. {
  549. const ContentFeatures &f = nodemgr->get(*this);
  550. // todo: after update in all games leave only if (f.param_type_2 ==
  551. if(f.liquid_type == LIQUID_SOURCE)
  552. return LIQUID_LEVEL_SOURCE;
  553. if (f.param_type_2 == CPT2_FLOWINGLIQUID)
  554. return getParam2() & LIQUID_LEVEL_MASK;
  555. if(f.liquid_type == LIQUID_FLOWING) // can remove if all param_type_2 setted
  556. return getParam2() & LIQUID_LEVEL_MASK;
  557. if (f.param_type_2 == CPT2_LEVELED) {
  558. u8 level = getParam2() & LEVELED_MASK;
  559. if (level)
  560. return level;
  561. }
  562. // Return static value from nodedef if param2 isn't used for level
  563. if (f.leveled > f.leveled_max)
  564. return f.leveled_max;
  565. return f.leveled;
  566. }
  567. s8 MapNode::setLevel(const NodeDefManager *nodemgr, s16 level)
  568. {
  569. s8 rest = 0;
  570. const ContentFeatures &f = nodemgr->get(*this);
  571. if (f.param_type_2 == CPT2_FLOWINGLIQUID
  572. || f.liquid_type == LIQUID_FLOWING
  573. || f.liquid_type == LIQUID_SOURCE) {
  574. if (level <= 0) { // liquid can’t exist with zero level
  575. setContent(CONTENT_AIR);
  576. return 0;
  577. }
  578. if (level >= LIQUID_LEVEL_SOURCE) {
  579. rest = level - LIQUID_LEVEL_SOURCE;
  580. setContent(f.liquid_alternative_source_id);
  581. setParam2(0);
  582. } else {
  583. setContent(f.liquid_alternative_flowing_id);
  584. setParam2((level & LIQUID_LEVEL_MASK) | (getParam2() & ~LIQUID_LEVEL_MASK));
  585. }
  586. } else if (f.param_type_2 == CPT2_LEVELED) {
  587. if (level < 0) { // zero means default for a leveled nodebox
  588. rest = level;
  589. level = 0;
  590. } else if (level > f.leveled_max) {
  591. rest = level - f.leveled_max;
  592. level = f.leveled_max;
  593. }
  594. setParam2((level & LEVELED_MASK) | (getParam2() & ~LEVELED_MASK));
  595. }
  596. return rest;
  597. }
  598. s8 MapNode::addLevel(const NodeDefManager *nodemgr, s16 add)
  599. {
  600. s16 level = getLevel(nodemgr);
  601. level += add;
  602. return setLevel(nodemgr, level);
  603. }
  604. u32 MapNode::serializedLength(u8 version)
  605. {
  606. if(!ser_ver_supported(version))
  607. throw VersionMismatchException("ERROR: MapNode format not supported");
  608. if (version == 0)
  609. return 1;
  610. if (version <= 9)
  611. return 2;
  612. if (version <= 23)
  613. return 3;
  614. return 4;
  615. }
  616. void MapNode::serialize(u8 *dest, u8 version) const
  617. {
  618. if(!ser_ver_supported(version))
  619. throw VersionMismatchException("ERROR: MapNode format not supported");
  620. // Can't do this anymore; we have 16-bit dynamically allocated node IDs
  621. // in memory; conversion just won't work in this direction.
  622. if(version < 24)
  623. throw SerializationError("MapNode::serialize: serialization to "
  624. "version < 24 not possible");
  625. writeU16(dest+0, param0);
  626. writeU8(dest+2, param1);
  627. writeU8(dest+3, param2);
  628. }
  629. void MapNode::deSerialize(u8 *source, u8 version)
  630. {
  631. if(!ser_ver_supported(version))
  632. throw VersionMismatchException("ERROR: MapNode format not supported");
  633. if(version <= 21)
  634. {
  635. deSerialize_pre22(source, version);
  636. return;
  637. }
  638. if(version >= 24){
  639. param0 = readU16(source+0);
  640. param1 = readU8(source+2);
  641. param2 = readU8(source+3);
  642. }else{
  643. param0 = readU8(source+0);
  644. param1 = readU8(source+1);
  645. param2 = readU8(source+2);
  646. if(param0 > 0x7F){
  647. param0 |= ((param2&0xF0)<<4);
  648. param2 &= 0x0F;
  649. }
  650. }
  651. }
  652. SharedBuffer<u8> MapNode::serializeBulk(int version,
  653. const MapNode *nodes, u32 nodecount,
  654. u8 content_width, u8 params_width)
  655. {
  656. if (!ser_ver_supported(version))
  657. throw VersionMismatchException("ERROR: MapNode format not supported");
  658. sanity_check(content_width == 2);
  659. sanity_check(params_width == 2);
  660. // Can't do this anymore; we have 16-bit dynamically allocated node IDs
  661. // in memory; conversion just won't work in this direction.
  662. if (version < 24)
  663. throw SerializationError("MapNode::serializeBulk: serialization to "
  664. "version < 24 not possible");
  665. SharedBuffer<u8> databuf(nodecount * (content_width + params_width));
  666. u32 start1 = content_width * nodecount;
  667. u32 start2 = (content_width + 1) * nodecount;
  668. // Serialize content
  669. for (u32 i = 0; i < nodecount; i++) {
  670. writeU16(&databuf[i * 2], nodes[i].param0);
  671. writeU8(&databuf[start1 + i], nodes[i].param1);
  672. writeU8(&databuf[start2 + i], nodes[i].param2);
  673. }
  674. return databuf;
  675. }
  676. // Deserialize bulk node data
  677. void MapNode::deSerializeBulk(std::istream &is, int version,
  678. MapNode *nodes, u32 nodecount,
  679. u8 content_width, u8 params_width)
  680. {
  681. if(!ser_ver_supported(version))
  682. throw VersionMismatchException("ERROR: MapNode format not supported");
  683. if (version < 22
  684. || (content_width != 1 && content_width != 2)
  685. || params_width != 2)
  686. FATAL_ERROR("Deserialize bulk node data error");
  687. // read data
  688. const u32 len = nodecount * (content_width + params_width);
  689. Buffer<u8> databuf(len);
  690. is.read(reinterpret_cast<char*>(*databuf), len);
  691. // Deserialize content
  692. if(content_width == 1)
  693. {
  694. for(u32 i=0; i<nodecount; i++)
  695. nodes[i].param0 = readU8(&databuf[i]);
  696. }
  697. else if(content_width == 2)
  698. {
  699. for(u32 i=0; i<nodecount; i++)
  700. nodes[i].param0 = readU16(&databuf[i*2]);
  701. }
  702. // Deserialize param1
  703. u32 start1 = content_width * nodecount;
  704. for(u32 i=0; i<nodecount; i++)
  705. nodes[i].param1 = readU8(&databuf[start1 + i]);
  706. // Deserialize param2
  707. u32 start2 = (content_width + 1) * nodecount;
  708. if(content_width == 1)
  709. {
  710. for(u32 i=0; i<nodecount; i++) {
  711. nodes[i].param2 = readU8(&databuf[start2 + i]);
  712. if(nodes[i].param0 > 0x7F){
  713. nodes[i].param0 <<= 4;
  714. nodes[i].param0 |= (nodes[i].param2&0xF0)>>4;
  715. nodes[i].param2 &= 0x0F;
  716. }
  717. }
  718. }
  719. else if(content_width == 2)
  720. {
  721. for(u32 i=0; i<nodecount; i++)
  722. nodes[i].param2 = readU8(&databuf[start2 + i]);
  723. }
  724. }
  725. /*
  726. Legacy serialization
  727. */
  728. void MapNode::deSerialize_pre22(const u8 *source, u8 version)
  729. {
  730. if(version <= 1)
  731. {
  732. param0 = source[0];
  733. }
  734. else if(version <= 9)
  735. {
  736. param0 = source[0];
  737. param1 = source[1];
  738. }
  739. else
  740. {
  741. param0 = source[0];
  742. param1 = source[1];
  743. param2 = source[2];
  744. if(param0 > 0x7f){
  745. param0 <<= 4;
  746. param0 |= (param2&0xf0)>>4;
  747. param2 &= 0x0f;
  748. }
  749. }
  750. // Convert special values from old version to new
  751. if(version <= 19)
  752. {
  753. // In these versions, CONTENT_IGNORE and CONTENT_AIR
  754. // are 255 and 254
  755. // Version 19 is messed up with sometimes the old values and sometimes not
  756. if(param0 == 255)
  757. param0 = CONTENT_IGNORE;
  758. else if(param0 == 254)
  759. param0 = CONTENT_AIR;
  760. }
  761. // Translate to our known version
  762. *this = mapnode_translate_to_internal(*this, version);
  763. }