mapnode.cpp 21 KB

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