voxelalgorithms.cpp 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306
  1. /*
  2. Minetest
  3. Copyright (C) 2010-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 "voxelalgorithms.h"
  17. #include "nodedef.h"
  18. #include "mapblock.h"
  19. #include "map.h"
  20. namespace voxalgo
  21. {
  22. /*!
  23. * A direction.
  24. * 0=X+
  25. * 1=Y+
  26. * 2=Z+
  27. * 3=Z-
  28. * 4=Y-
  29. * 5=X-
  30. * 6=no direction
  31. * Two directions are opposite only if their sum is 5.
  32. */
  33. typedef u8 direction;
  34. /*!
  35. * Relative node position.
  36. * This represents a node's position in its map block.
  37. * All coordinates must be between 0 and 15.
  38. */
  39. typedef v3s16 relative_v3;
  40. /*!
  41. * Position of a map block (block coordinates).
  42. * One block_pos unit is as long as 16 node position units.
  43. */
  44. typedef v3s16 mapblock_v3;
  45. //! Contains information about a node whose light is about to change.
  46. struct ChangingLight {
  47. //! Relative position of the node in its map block.
  48. relative_v3 rel_position;
  49. //! Position of the node's block.
  50. mapblock_v3 block_position;
  51. //! Pointer to the node's block.
  52. MapBlock *block = NULL;
  53. /*!
  54. * Direction from the node that caused this node's changing
  55. * to this node.
  56. */
  57. direction source_direction = 6;
  58. ChangingLight() = default;
  59. ChangingLight(relative_v3 rel_pos, mapblock_v3 block_pos,
  60. MapBlock *b, direction source_dir) :
  61. rel_position(rel_pos),
  62. block_position(block_pos),
  63. block(b),
  64. source_direction(source_dir)
  65. {}
  66. };
  67. /*!
  68. * A fast, priority queue-like container to contain ChangingLights.
  69. * The ChangingLights are ordered by the given light levels.
  70. * The brightest ChangingLight is returned first.
  71. */
  72. struct LightQueue {
  73. //! For each light level there is a vector.
  74. std::vector<ChangingLight> lights[LIGHT_SUN + 1];
  75. //! Light of the brightest ChangingLight in the queue.
  76. u8 max_light;
  77. /*!
  78. * Creates a LightQueue.
  79. * \param reserve for each light level that many slots are reserved.
  80. */
  81. LightQueue(size_t reserve)
  82. {
  83. max_light = LIGHT_SUN;
  84. for (u8 i = 0; i <= LIGHT_SUN; i++) {
  85. lights[i].reserve(reserve);
  86. }
  87. }
  88. /*!
  89. * Returns the next brightest ChangingLight and
  90. * removes it from the queue.
  91. * If there were no elements in the queue, the given parameters
  92. * remain unmodified.
  93. * \param light light level of the popped ChangingLight
  94. * \param data the ChangingLight that was popped
  95. * \returns true if there was a ChangingLight in the queue.
  96. */
  97. bool next(u8 &light, ChangingLight &data)
  98. {
  99. while (lights[max_light].empty()) {
  100. if (max_light == 0) {
  101. return false;
  102. }
  103. max_light--;
  104. }
  105. light = max_light;
  106. data = lights[max_light].back();
  107. lights[max_light].pop_back();
  108. return true;
  109. }
  110. /*!
  111. * Adds an element to the queue.
  112. * The parameters are the same as in ChangingLight's constructor.
  113. * \param light light level of the ChangingLight
  114. */
  115. inline void push(u8 light, relative_v3 rel_pos,
  116. mapblock_v3 block_pos, MapBlock *block,
  117. direction source_dir)
  118. {
  119. assert(light <= LIGHT_SUN);
  120. lights[light].emplace_back(rel_pos, block_pos, block, source_dir);
  121. }
  122. };
  123. /*!
  124. * This type of light queue is for unlighting.
  125. * A node can be pushed in it only if its raw light is zero.
  126. * This prevents pushing nodes twice into this queue.
  127. * The light of the pushed ChangingLight must be the
  128. * light of the node before unlighting it.
  129. */
  130. typedef LightQueue UnlightQueue;
  131. /*!
  132. * This type of light queue is for spreading lights.
  133. * While spreading lights, all the nodes in it must
  134. * have the same light as the light level the ChangingLights
  135. * were pushed into this queue with. This prevents unnecessary
  136. * re-pushing of the nodes into the queue.
  137. * If a node doesn't let light trough but emits light, it can be added
  138. * too.
  139. */
  140. typedef LightQueue ReLightQueue;
  141. /*!
  142. * neighbor_dirs[i] points towards
  143. * the direction i.
  144. * See the definition of the type "direction"
  145. */
  146. const static v3s16 neighbor_dirs[6] = {
  147. v3s16(1, 0, 0), // right
  148. v3s16(0, 1, 0), // top
  149. v3s16(0, 0, 1), // back
  150. v3s16(0, 0, -1), // front
  151. v3s16(0, -1, 0), // bottom
  152. v3s16(-1, 0, 0), // left
  153. };
  154. /*!
  155. * Transforms the given map block offset by one node towards
  156. * the specified direction.
  157. * \param dir the direction of the transformation
  158. * \param rel_pos the node's relative position in its map block
  159. * \param block_pos position of the node's block
  160. */
  161. bool step_rel_block_pos(direction dir, relative_v3 &rel_pos,
  162. mapblock_v3 &block_pos)
  163. {
  164. switch (dir) {
  165. case 0:
  166. if (rel_pos.X < MAP_BLOCKSIZE - 1) {
  167. rel_pos.X++;
  168. } else {
  169. rel_pos.X = 0;
  170. block_pos.X++;
  171. return true;
  172. }
  173. break;
  174. case 1:
  175. if (rel_pos.Y < MAP_BLOCKSIZE - 1) {
  176. rel_pos.Y++;
  177. } else {
  178. rel_pos.Y = 0;
  179. block_pos.Y++;
  180. return true;
  181. }
  182. break;
  183. case 2:
  184. if (rel_pos.Z < MAP_BLOCKSIZE - 1) {
  185. rel_pos.Z++;
  186. } else {
  187. rel_pos.Z = 0;
  188. block_pos.Z++;
  189. return true;
  190. }
  191. break;
  192. case 3:
  193. if (rel_pos.Z > 0) {
  194. rel_pos.Z--;
  195. } else {
  196. rel_pos.Z = MAP_BLOCKSIZE - 1;
  197. block_pos.Z--;
  198. return true;
  199. }
  200. break;
  201. case 4:
  202. if (rel_pos.Y > 0) {
  203. rel_pos.Y--;
  204. } else {
  205. rel_pos.Y = MAP_BLOCKSIZE - 1;
  206. block_pos.Y--;
  207. return true;
  208. }
  209. break;
  210. case 5:
  211. if (rel_pos.X > 0) {
  212. rel_pos.X--;
  213. } else {
  214. rel_pos.X = MAP_BLOCKSIZE - 1;
  215. block_pos.X--;
  216. return true;
  217. }
  218. break;
  219. }
  220. return false;
  221. }
  222. /*
  223. * Removes all light that is potentially emitted by the specified
  224. * light sources. These nodes will have zero light.
  225. * Returns all nodes whose light became zero but should be re-lighted.
  226. *
  227. * \param bank the light bank in which the procedure operates
  228. * \param from_nodes nodes whose light is removed
  229. * \param light_sources nodes that should be re-lighted
  230. * \param modified_blocks output, all modified map blocks are added to this
  231. */
  232. void unspread_light(Map *map, const NodeDefManager *nodemgr, LightBank bank,
  233. UnlightQueue &from_nodes, ReLightQueue &light_sources,
  234. std::map<v3s16, MapBlock*> &modified_blocks)
  235. {
  236. // Stores data popped from from_nodes
  237. u8 current_light;
  238. ChangingLight current;
  239. // Data of the current neighbor
  240. mapblock_v3 neighbor_block_pos;
  241. relative_v3 neighbor_rel_pos;
  242. // Direction of the brightest neighbor of the node
  243. direction source_dir;
  244. while (from_nodes.next(current_light, current)) {
  245. // For all nodes that need unlighting
  246. // There is no brightest neighbor
  247. source_dir = 6;
  248. // The current node
  249. const MapNode &node = current.block->getNodeNoCheck(current.rel_position);
  250. ContentLightingFlags f = nodemgr->getLightingFlags(node);
  251. // If the node emits light, it behaves like it had a
  252. // brighter neighbor.
  253. u8 brightest_neighbor_light = f.light_source + 1;
  254. for (direction i = 0; i < 6; i++) {
  255. //For each neighbor
  256. // The node that changed this node has already zero light
  257. // and it can't give light to this node
  258. if (current.source_direction + i == 5) {
  259. continue;
  260. }
  261. // Get the neighbor's position and block
  262. neighbor_rel_pos = current.rel_position;
  263. neighbor_block_pos = current.block_position;
  264. MapBlock *neighbor_block;
  265. if (step_rel_block_pos(i, neighbor_rel_pos, neighbor_block_pos)) {
  266. neighbor_block = map->getBlockNoCreateNoEx(neighbor_block_pos);
  267. if (neighbor_block == NULL) {
  268. current.block->setLightingComplete(bank, i, false);
  269. continue;
  270. }
  271. } else {
  272. neighbor_block = current.block;
  273. }
  274. // Get the neighbor itself
  275. MapNode neighbor = neighbor_block->getNodeNoCheck(neighbor_rel_pos);
  276. ContentLightingFlags neighbor_f = nodemgr->getLightingFlags(
  277. neighbor.getContent());
  278. u8 neighbor_light = neighbor.getLightRaw(bank, neighbor_f);
  279. // If the neighbor has at least as much light as this node, then
  280. // it won't lose its light, since it should have been added to
  281. // from_nodes earlier, so its light would be zero.
  282. if (neighbor_f.light_propagates && neighbor_light < current_light) {
  283. // Unlight, but only if the node has light.
  284. if (neighbor_light > 0) {
  285. neighbor.setLight(bank, 0, neighbor_f);
  286. neighbor_block->setNodeNoCheck(neighbor_rel_pos, neighbor);
  287. from_nodes.push(neighbor_light, neighbor_rel_pos,
  288. neighbor_block_pos, neighbor_block, i);
  289. // The current node was modified earlier, so its block
  290. // is in modified_blocks.
  291. if (current.block != neighbor_block) {
  292. modified_blocks[neighbor_block_pos] = neighbor_block;
  293. }
  294. }
  295. } else {
  296. // The neighbor can light up this node.
  297. if (neighbor_light < neighbor_f.light_source) {
  298. neighbor_light = neighbor_f.light_source;
  299. }
  300. if (brightest_neighbor_light < neighbor_light) {
  301. brightest_neighbor_light = neighbor_light;
  302. source_dir = i;
  303. }
  304. }
  305. }
  306. // If the brightest neighbor is able to light up this node,
  307. // then add this node to the output nodes.
  308. if (brightest_neighbor_light > 1 && f.light_propagates) {
  309. brightest_neighbor_light--;
  310. light_sources.push(brightest_neighbor_light, current.rel_position,
  311. current.block_position, current.block,
  312. (source_dir == 6) ? 6 : 5 - source_dir
  313. /* with opposite direction*/);
  314. }
  315. }
  316. }
  317. /*
  318. * Spreads light from the specified starting nodes.
  319. *
  320. * Before calling this procedure, make sure that all ChangingLights
  321. * in light_sources have as much light on the map as they have in
  322. * light_sources (if the queue contains a node multiple times, the brightest
  323. * occurrence counts).
  324. *
  325. * \param bank the light bank in which the procedure operates
  326. * \param light_sources starting nodes
  327. * \param modified_blocks output, all modified map blocks are added to this
  328. */
  329. void spread_light(Map *map, const NodeDefManager *nodemgr, LightBank bank,
  330. LightQueue &light_sources,
  331. std::map<v3s16, MapBlock*> &modified_blocks)
  332. {
  333. // The light the current node can provide to its neighbors.
  334. u8 spreading_light;
  335. // The ChangingLight for the current node.
  336. ChangingLight current;
  337. // Position of the current neighbor.
  338. mapblock_v3 neighbor_block_pos;
  339. relative_v3 neighbor_rel_pos;
  340. while (light_sources.next(spreading_light, current)) {
  341. spreading_light--;
  342. for (direction i = 0; i < 6; i++) {
  343. // This node can't light up its light source
  344. if (current.source_direction + i == 5) {
  345. continue;
  346. }
  347. // Get the neighbor's position and block
  348. neighbor_rel_pos = current.rel_position;
  349. neighbor_block_pos = current.block_position;
  350. MapBlock *neighbor_block;
  351. if (step_rel_block_pos(i, neighbor_rel_pos, neighbor_block_pos)) {
  352. neighbor_block = map->getBlockNoCreateNoEx(neighbor_block_pos);
  353. if (neighbor_block == NULL) {
  354. current.block->setLightingComplete(bank, i, false);
  355. continue;
  356. }
  357. } else {
  358. neighbor_block = current.block;
  359. }
  360. // Get the neighbor itself
  361. MapNode neighbor = neighbor_block->getNodeNoCheck(neighbor_rel_pos);
  362. ContentLightingFlags f = nodemgr->getLightingFlags(neighbor);
  363. if (f.light_propagates) {
  364. // Light up the neighbor, if it has less light than it should.
  365. u8 neighbor_light = neighbor.getLightRaw(bank, f);
  366. if (neighbor_light < spreading_light) {
  367. neighbor.setLight(bank, spreading_light, f);
  368. neighbor_block->setNodeNoCheck(neighbor_rel_pos, neighbor);
  369. light_sources.push(spreading_light, neighbor_rel_pos,
  370. neighbor_block_pos, neighbor_block, i);
  371. // The current node was modified earlier, so its block
  372. // is in modified_blocks.
  373. if (current.block != neighbor_block) {
  374. modified_blocks[neighbor_block_pos] = neighbor_block;
  375. }
  376. }
  377. }
  378. }
  379. }
  380. }
  381. struct SunlightPropagationUnit{
  382. v2s16 relative_pos;
  383. bool is_sunlit;
  384. SunlightPropagationUnit(v2s16 relpos, bool sunlit):
  385. relative_pos(relpos),
  386. is_sunlit(sunlit)
  387. {}
  388. };
  389. struct SunlightPropagationData{
  390. std::vector<SunlightPropagationUnit> data;
  391. v3s16 target_block;
  392. };
  393. /*!
  394. * Returns true if the node gets sunlight from the
  395. * node above it.
  396. *
  397. * \param pos position of the node.
  398. */
  399. bool is_sunlight_above(Map *map, v3s16 pos, const NodeDefManager *ndef)
  400. {
  401. bool sunlight = true;
  402. mapblock_v3 source_block_pos;
  403. relative_v3 source_rel_pos;
  404. getNodeBlockPosWithOffset(pos + v3s16(0, 1, 0), source_block_pos,
  405. source_rel_pos);
  406. // If the node above has sunlight, this node also can get it.
  407. MapBlock *source_block = map->getBlockNoCreateNoEx(source_block_pos);
  408. if (source_block == NULL) {
  409. // But if there is no node above, then use heuristics
  410. MapBlock *node_block = map->getBlockNoCreateNoEx(getNodeBlockPos(pos));
  411. if (node_block == NULL) {
  412. sunlight = false;
  413. } else {
  414. sunlight = !node_block->getIsUnderground();
  415. }
  416. } else {
  417. MapNode above = source_block->getNodeNoCheck(source_rel_pos);
  418. if (above.getContent() == CONTENT_IGNORE) {
  419. // Trust heuristics
  420. if (source_block->getIsUnderground()) {
  421. sunlight = false;
  422. }
  423. } else {
  424. ContentLightingFlags above_f = ndef->getLightingFlags(above);
  425. if (above.getLight(LIGHTBANK_DAY, above_f) != LIGHT_SUN) {
  426. // If the node above doesn't have sunlight, this
  427. // node is in shadow.
  428. sunlight = false;
  429. }
  430. }
  431. }
  432. return sunlight;
  433. }
  434. static const LightBank banks[] = { LIGHTBANK_DAY, LIGHTBANK_NIGHT };
  435. void update_lighting_nodes(Map *map,
  436. const std::vector<std::pair<v3s16, MapNode>> &oldnodes,
  437. std::map<v3s16, MapBlock*> &modified_blocks)
  438. {
  439. const NodeDefManager *ndef = map->getNodeDefManager();
  440. // For node getter functions
  441. bool is_valid_position;
  442. // Process each light bank separately
  443. for (LightBank bank : banks) {
  444. UnlightQueue disappearing_lights(256);
  445. ReLightQueue light_sources(256);
  446. // Nodes that are brighter than the brightest modified node was
  447. // won't change, since they didn't get their light from a
  448. // modified node.
  449. u8 min_safe_light = 0;
  450. for (auto it = oldnodes.cbegin(); it < oldnodes.cend(); ++it) {
  451. u8 old_light = it->second.getLight(bank, ndef->getLightingFlags(it->second));
  452. if (old_light > min_safe_light) {
  453. min_safe_light = old_light;
  454. }
  455. }
  456. // If only one node changed, even nodes with the same brightness
  457. // didn't get their light from the changed node.
  458. if (oldnodes.size() > 1) {
  459. min_safe_light++;
  460. }
  461. // For each changed node process sunlight and initialize
  462. for (auto it = oldnodes.cbegin(); it < oldnodes.cend(); ++it) {
  463. // Get position and block of the changed node
  464. v3s16 p = it->first;
  465. relative_v3 rel_pos;
  466. mapblock_v3 block_pos;
  467. getNodeBlockPosWithOffset(p, block_pos, rel_pos);
  468. MapBlock *block = map->getBlockNoCreateNoEx(block_pos);
  469. if (block == NULL) {
  470. continue;
  471. }
  472. // Get the new node
  473. MapNode n = block->getNodeNoCheck(rel_pos);
  474. // Light of the old node
  475. u8 old_light = it->second.getLight(bank, ndef->getLightingFlags(it->second));
  476. // Add the block of the added node to modified_blocks
  477. modified_blocks[block_pos] = block;
  478. // Get new light level of the node
  479. u8 new_light = 0;
  480. ContentLightingFlags f = ndef->getLightingFlags(n);
  481. if (f.light_propagates) {
  482. if (bank == LIGHTBANK_DAY && f.sunlight_propagates
  483. && is_sunlight_above(map, p, ndef)) {
  484. new_light = LIGHT_SUN;
  485. } else {
  486. new_light = f.light_source;
  487. for (const v3s16 &neighbor_dir : neighbor_dirs) {
  488. v3s16 p2 = p + neighbor_dir;
  489. MapNode n2 = map->getNode(p2, &is_valid_position);
  490. if (is_valid_position) {
  491. u8 spread = n2.getLight(bank, ndef->getLightingFlags(n2));
  492. // If it is sure that the neighbor won't be
  493. // unlighted, its light can spread to this node.
  494. if (spread > new_light && spread >= min_safe_light) {
  495. new_light = spread - 1;
  496. }
  497. }
  498. }
  499. }
  500. } else {
  501. // If this is an opaque node, it still can emit light.
  502. new_light = f.light_source;
  503. }
  504. if (new_light > 0) {
  505. light_sources.push(new_light, rel_pos, block_pos, block, 6);
  506. }
  507. if (new_light < old_light) {
  508. // The node became opaque or doesn't provide as much
  509. // light as the previous one, so it must be unlighted.
  510. // Add to unlight queue
  511. n.setLight(bank, 0, f);
  512. block->setNodeNoCheck(rel_pos, n);
  513. disappearing_lights.push(old_light, rel_pos, block_pos, block,
  514. 6);
  515. // Remove sunlight, if there was any
  516. if (bank == LIGHTBANK_DAY && old_light == LIGHT_SUN) {
  517. for (s16 y = p.Y - 1;; y--) {
  518. v3s16 n2pos(p.X, y, p.Z);
  519. MapNode n2;
  520. n2 = map->getNode(n2pos, &is_valid_position);
  521. if (!is_valid_position)
  522. break;
  523. // If this node doesn't have sunlight, the nodes below
  524. // it don't have too.
  525. ContentLightingFlags f2 = ndef->getLightingFlags(n2);
  526. if (n2.getLight(LIGHTBANK_DAY, f2) != LIGHT_SUN) {
  527. break;
  528. }
  529. // Remove sunlight and add to unlight queue.
  530. n2.setLight(LIGHTBANK_DAY, 0, f2);
  531. map->setNode(n2pos, n2);
  532. relative_v3 rel_pos2;
  533. mapblock_v3 block_pos2;
  534. getNodeBlockPosWithOffset(n2pos, block_pos2, rel_pos2);
  535. MapBlock *block2 = map->getBlockNoCreateNoEx(
  536. block_pos2);
  537. disappearing_lights.push(LIGHT_SUN, rel_pos2,
  538. block_pos2, block2,
  539. 4 /* The node above caused the change */);
  540. }
  541. }
  542. } else if (new_light > old_light) {
  543. // It is sure that the node provides more light than the previous
  544. // one, unlighting is not necessary.
  545. // Propagate sunlight
  546. if (bank == LIGHTBANK_DAY && new_light == LIGHT_SUN) {
  547. for (s16 y = p.Y - 1;; y--) {
  548. v3s16 n2pos(p.X, y, p.Z);
  549. MapNode n2;
  550. n2 = map->getNode(n2pos, &is_valid_position);
  551. if (!is_valid_position)
  552. break;
  553. // This should not happen, but if the node has sunlight
  554. // then the iteration should stop.
  555. ContentLightingFlags f2 = ndef->getLightingFlags(n2);
  556. if (n2.getLight(LIGHTBANK_DAY, f2) == LIGHT_SUN) {
  557. break;
  558. }
  559. // If the node terminates sunlight, stop.
  560. if (!f2.sunlight_propagates) {
  561. break;
  562. }
  563. relative_v3 rel_pos2;
  564. mapblock_v3 block_pos2;
  565. getNodeBlockPosWithOffset(n2pos, block_pos2, rel_pos2);
  566. MapBlock *block2 = map->getBlockNoCreateNoEx(
  567. block_pos2);
  568. // Mark node for lighting.
  569. light_sources.push(LIGHT_SUN, rel_pos2, block_pos2,
  570. block2, 4);
  571. }
  572. }
  573. }
  574. }
  575. // Remove lights
  576. unspread_light(map, ndef, bank, disappearing_lights, light_sources,
  577. modified_blocks);
  578. // Initialize light values for light spreading.
  579. for (u8 i = 0; i <= LIGHT_SUN; i++) {
  580. const std::vector<ChangingLight> &lights = light_sources.lights[i];
  581. for (std::vector<ChangingLight>::const_iterator it = lights.begin();
  582. it < lights.end(); ++it) {
  583. MapNode n = it->block->getNodeNoCheck(it->rel_position);
  584. n.setLight(bank, i, ndef->getLightingFlags(n));
  585. it->block->setNodeNoCheck(it->rel_position, n);
  586. }
  587. }
  588. // Spread lights.
  589. spread_light(map, ndef, bank, light_sources, modified_blocks);
  590. }
  591. }
  592. /*!
  593. * Borders of a map block in relative node coordinates.
  594. * Compatible with type 'direction'.
  595. */
  596. const VoxelArea block_borders[] = {
  597. VoxelArea(v3s16(15, 0, 0), v3s16(15, 15, 15)), //X+
  598. VoxelArea(v3s16(0, 15, 0), v3s16(15, 15, 15)), //Y+
  599. VoxelArea(v3s16(0, 0, 15), v3s16(15, 15, 15)), //Z+
  600. VoxelArea(v3s16(0, 0, 0), v3s16(15, 15, 0)), //Z-
  601. VoxelArea(v3s16(0, 0, 0), v3s16(15, 0, 15)), //Y-
  602. VoxelArea(v3s16(0, 0, 0), v3s16(0, 15, 15)) //X-
  603. };
  604. /*!
  605. * Returns true if:
  606. * -the node has unloaded neighbors
  607. * -the node doesn't have light
  608. * -the node's light is the same as the maximum of
  609. * its light source and its brightest neighbor minus one.
  610. * .
  611. */
  612. bool is_light_locally_correct(Map *map, const NodeDefManager *ndef,
  613. LightBank bank, v3s16 pos)
  614. {
  615. bool is_valid_position;
  616. MapNode n = map->getNode(pos, &is_valid_position);
  617. ContentLightingFlags f = ndef->getLightingFlags(n);
  618. if (!f.has_light) {
  619. return true;
  620. }
  621. u8 light = n.getLight(bank, f);
  622. assert(f.light_source <= LIGHT_MAX);
  623. u8 brightest_neighbor = f.light_source + 1;
  624. for (const v3s16 &neighbor_dir : neighbor_dirs) {
  625. MapNode n2 = map->getNode(pos + neighbor_dir,
  626. &is_valid_position);
  627. u8 light2 = n2.getLight(bank, ndef->getLightingFlags(n2));
  628. if (brightest_neighbor < light2) {
  629. brightest_neighbor = light2;
  630. }
  631. }
  632. assert(light <= LIGHT_SUN);
  633. return brightest_neighbor == light + 1;
  634. }
  635. void update_block_border_lighting(Map *map, MapBlock *block,
  636. std::map<v3s16, MapBlock*> &modified_blocks)
  637. {
  638. const NodeDefManager *ndef = map->getNodeDefManager();
  639. for (LightBank bank : banks) {
  640. // Since invalid light is not common, do not allocate
  641. // memory if not needed.
  642. UnlightQueue disappearing_lights(0);
  643. ReLightQueue light_sources(0);
  644. // Get incorrect lights
  645. for (direction d = 0; d < 6; d++) {
  646. // For each direction
  647. // Get neighbor block
  648. v3s16 otherpos = block->getPos() + neighbor_dirs[d];
  649. MapBlock *other = map->getBlockNoCreateNoEx(otherpos);
  650. if (other == NULL) {
  651. continue;
  652. }
  653. // Only update if lighting was not completed.
  654. if (block->isLightingComplete(bank, d) &&
  655. other->isLightingComplete(bank, 5 - d))
  656. continue;
  657. // Reset flags
  658. block->setLightingComplete(bank, d, true);
  659. other->setLightingComplete(bank, 5 - d, true);
  660. // The two blocks and their connecting surfaces
  661. MapBlock *blocks[] = {block, other};
  662. VoxelArea areas[] = {block_borders[d], block_borders[5 - d]};
  663. // For both blocks
  664. for (u8 blocknum = 0; blocknum < 2; blocknum++) {
  665. MapBlock *b = blocks[blocknum];
  666. VoxelArea a = areas[blocknum];
  667. // For all nodes
  668. for (s32 x = a.MinEdge.X; x <= a.MaxEdge.X; x++)
  669. for (s32 z = a.MinEdge.Z; z <= a.MaxEdge.Z; z++)
  670. for (s32 y = a.MinEdge.Y; y <= a.MaxEdge.Y; y++) {
  671. MapNode n = b->getNodeNoCheck(x, y, z);
  672. ContentLightingFlags f = ndef->getLightingFlags(n);
  673. u8 light = n.getLight(bank, f);
  674. // Sunlight is fixed
  675. if (light < LIGHT_SUN) {
  676. // Unlight if not correct
  677. if (!is_light_locally_correct(map, ndef, bank,
  678. v3s16(x, y, z) + b->getPosRelative())) {
  679. // Initialize for unlighting
  680. n.setLight(bank, 0, ndef->getLightingFlags(n));
  681. b->setNodeNoCheck(x, y, z, n);
  682. modified_blocks[b->getPos()]=b;
  683. disappearing_lights.push(light,
  684. relative_v3(x, y, z), b->getPos(), b,
  685. 6);
  686. }
  687. }
  688. }
  689. }
  690. }
  691. // Remove lights
  692. unspread_light(map, ndef, bank, disappearing_lights, light_sources,
  693. modified_blocks);
  694. // Initialize light values for light spreading.
  695. for (u8 i = 0; i <= LIGHT_SUN; i++) {
  696. const std::vector<ChangingLight> &lights = light_sources.lights[i];
  697. for (std::vector<ChangingLight>::const_iterator it = lights.begin();
  698. it < lights.end(); ++it) {
  699. MapNode n = it->block->getNodeNoCheck(it->rel_position);
  700. n.setLight(bank, i, ndef->getLightingFlags(n));
  701. it->block->setNodeNoCheck(it->rel_position, n);
  702. }
  703. }
  704. // Spread lights.
  705. spread_light(map, ndef, bank, light_sources, modified_blocks);
  706. }
  707. }
  708. /*!
  709. * Resets the lighting of the given VoxelManipulator to
  710. * complete darkness and full sunlight.
  711. * Operates in one map sector.
  712. *
  713. * \param offset contains the least x and z node coordinates
  714. * of the map sector.
  715. * \param light incoming sunlight, light[x][z] is true if there
  716. * is sunlight above the voxel manipulator at the given x-z coordinates.
  717. * The array's indices are relative node coordinates in the sector.
  718. * After the procedure returns, this contains outgoing light at
  719. * the bottom of the voxel manipulator.
  720. */
  721. void fill_with_sunlight(MMVManip *vm, const NodeDefManager *ndef, v2s16 offset,
  722. bool light[MAP_BLOCKSIZE][MAP_BLOCKSIZE])
  723. {
  724. // Distance in array between two nodes on top of each other.
  725. s16 ystride = vm->m_area.getExtent().X;
  726. // Cache the ignore node.
  727. MapNode ignore = MapNode(CONTENT_IGNORE);
  728. // For each column of nodes:
  729. for (s16 z = 0; z < MAP_BLOCKSIZE; z++)
  730. for (s16 x = 0; x < MAP_BLOCKSIZE; x++) {
  731. // Position of the column on the map.
  732. v2s16 realpos = offset + v2s16(x, z);
  733. // Array indices in the voxel manipulator
  734. s32 maxindex = vm->m_area.index(realpos.X, vm->m_area.MaxEdge.Y,
  735. realpos.Y);
  736. s32 minindex = vm->m_area.index(realpos.X, vm->m_area.MinEdge.Y,
  737. realpos.Y);
  738. // True if the current node has sunlight.
  739. bool lig = light[z][x];
  740. // For each node, downwards:
  741. for (s32 i = maxindex; i >= minindex; i -= ystride) {
  742. MapNode *n;
  743. if (vm->m_flags[i] & VOXELFLAG_NO_DATA)
  744. n = &ignore;
  745. else
  746. n = &vm->m_data[i];
  747. // Ignore IGNORE nodes, these are not generated yet.
  748. if(n->getContent() == CONTENT_IGNORE)
  749. continue;
  750. ContentLightingFlags f = ndef->getLightingFlags(*n);
  751. if (lig && !f.sunlight_propagates)
  752. // Sunlight is stopped.
  753. lig = false;
  754. // Reset light
  755. n->setLight(LIGHTBANK_DAY, lig ? 15 : 0, f);
  756. n->setLight(LIGHTBANK_NIGHT, 0, f);
  757. }
  758. // Output outgoing light.
  759. light[z][x] = lig;
  760. }
  761. }
  762. /*!
  763. * Returns incoming sunlight for one map block.
  764. * If block above is not found, it is loaded.
  765. *
  766. * \param pos position of the map block that gets the sunlight.
  767. * \param light incoming sunlight, light[z][x] is true if there
  768. * is sunlight above the block at the given z-x relative
  769. * node coordinates.
  770. */
  771. void is_sunlight_above_block(Map *map, mapblock_v3 pos,
  772. const NodeDefManager *ndef, bool light[MAP_BLOCKSIZE][MAP_BLOCKSIZE])
  773. {
  774. mapblock_v3 source_block_pos = pos + v3s16(0, 1, 0);
  775. // Get or load source block.
  776. // It might take a while to load, but correcting incorrect
  777. // sunlight may be even slower.
  778. MapBlock *source_block = map->emergeBlock(source_block_pos, false);
  779. // Trust only generated blocks.
  780. if (source_block == NULL || !source_block->isGenerated()) {
  781. // But if there is no block above, then use heuristics
  782. bool sunlight = true;
  783. MapBlock *node_block = map->getBlockNoCreateNoEx(pos);
  784. if (node_block == NULL)
  785. // This should not happen.
  786. sunlight = false;
  787. else
  788. sunlight = !node_block->getIsUnderground();
  789. for (s16 z = 0; z < MAP_BLOCKSIZE; z++)
  790. for (s16 x = 0; x < MAP_BLOCKSIZE; x++)
  791. light[z][x] = sunlight;
  792. } else {
  793. // For each column:
  794. for (s16 z = 0; z < MAP_BLOCKSIZE; z++)
  795. for (s16 x = 0; x < MAP_BLOCKSIZE; x++) {
  796. // Get the bottom block.
  797. MapNode above = source_block->getNodeNoCheck(x, 0, z);
  798. ContentLightingFlags above_f = ndef->getLightingFlags(above);
  799. light[z][x] = above.getLight(LIGHTBANK_DAY, above_f) == LIGHT_SUN;
  800. }
  801. }
  802. }
  803. /*!
  804. * Propagates sunlight down in a given map block.
  805. *
  806. * \param data contains incoming sunlight and shadow and
  807. * the coordinates of the target block.
  808. * \param unlight propagated shadow is inserted here
  809. * \param relight propagated sunlight is inserted here
  810. *
  811. * \returns true if the block was modified, false otherwise.
  812. */
  813. bool propagate_block_sunlight(Map *map, const NodeDefManager *ndef,
  814. SunlightPropagationData *data, UnlightQueue *unlight, ReLightQueue *relight)
  815. {
  816. bool modified = false;
  817. // Get the block.
  818. MapBlock *block = map->getBlockNoCreateNoEx(data->target_block);
  819. if (block == NULL) {
  820. // The work is done if the block does not contain data.
  821. data->data.clear();
  822. return false;
  823. }
  824. // For each changing column of nodes:
  825. size_t index;
  826. for (index = 0; index < data->data.size(); index++) {
  827. SunlightPropagationUnit it = data->data[index];
  828. // Relative position of the currently inspected node.
  829. relative_v3 current_pos(it.relative_pos.X, MAP_BLOCKSIZE - 1,
  830. it.relative_pos.Y);
  831. if (it.is_sunlit) {
  832. // Propagate sunlight.
  833. // For each node downwards:
  834. for (; current_pos.Y >= 0; current_pos.Y--) {
  835. MapNode n = block->getNodeNoCheck(current_pos);
  836. ContentLightingFlags f = ndef->getLightingFlags(n);
  837. if (n.getLightRaw(LIGHTBANK_DAY, f) < LIGHT_SUN
  838. && f.sunlight_propagates) {
  839. // This node gets sunlight.
  840. n.setLight(LIGHTBANK_DAY, LIGHT_SUN, f);
  841. block->setNodeNoCheck(current_pos, n);
  842. modified = true;
  843. relight->push(LIGHT_SUN, current_pos, data->target_block,
  844. block, 4);
  845. } else {
  846. // Light already valid, propagation stopped.
  847. break;
  848. }
  849. }
  850. } else {
  851. // Propagate shadow.
  852. // For each node downwards:
  853. for (; current_pos.Y >= 0; current_pos.Y--) {
  854. MapNode n = block->getNodeNoCheck(current_pos);
  855. ContentLightingFlags f = ndef->getLightingFlags(n);
  856. if (n.getLightRaw(LIGHTBANK_DAY, f) == LIGHT_SUN) {
  857. // The sunlight is no longer valid.
  858. n.setLight(LIGHTBANK_DAY, 0, f);
  859. block->setNodeNoCheck(current_pos, n);
  860. modified = true;
  861. unlight->push(LIGHT_SUN, current_pos, data->target_block,
  862. block, 4);
  863. } else {
  864. // Reached shadow, propagation stopped.
  865. break;
  866. }
  867. }
  868. }
  869. if (current_pos.Y >= 0) {
  870. // Propagation stopped, remove from data.
  871. data->data[index] = data->data.back();
  872. data->data.pop_back();
  873. index--;
  874. }
  875. }
  876. return modified;
  877. }
  878. /*!
  879. * Borders of a map block in relative node coordinates.
  880. * The areas do not overlap.
  881. * Compatible with type 'direction'.
  882. */
  883. const VoxelArea block_pad[] = {
  884. VoxelArea(v3s16(15, 0, 0), v3s16(15, 15, 15)), //X+
  885. VoxelArea(v3s16(1, 15, 0), v3s16(14, 15, 15)), //Y+
  886. VoxelArea(v3s16(1, 1, 15), v3s16(14, 14, 15)), //Z+
  887. VoxelArea(v3s16(1, 1, 0), v3s16(14, 14, 0)), //Z-
  888. VoxelArea(v3s16(1, 0, 0), v3s16(14, 0, 15)), //Y-
  889. VoxelArea(v3s16(0, 0, 0), v3s16(0, 15, 15)) //X-
  890. };
  891. /*!
  892. * The common part of bulk light updates - it is always executed.
  893. * The procedure takes the nodes that should be unlit, and the
  894. * full modified area.
  895. *
  896. * The procedure handles the correction of all lighting except
  897. * direct sunlight spreading.
  898. *
  899. * \param minblock least coordinates of the changed area in block
  900. * coordinates
  901. * \param maxblock greatest coordinates of the changed area in block
  902. * coordinates
  903. * \param unlight the first queue is for day light, the second is for
  904. * night light. Contains all nodes on the borders that need to be unlit.
  905. * \param relight the first queue is for day light, the second is for
  906. * night light. Contains nodes that were not modified, but got sunlight
  907. * because the changes.
  908. * \param modified_blocks the procedure adds all modified blocks to
  909. * this map
  910. */
  911. void finish_bulk_light_update(Map *map, mapblock_v3 minblock,
  912. mapblock_v3 maxblock, UnlightQueue unlight[2], ReLightQueue relight[2],
  913. std::map<v3s16, MapBlock*> *modified_blocks)
  914. {
  915. const NodeDefManager *ndef = map->getNodeDefManager();
  916. // --- STEP 1: Do unlighting
  917. for (size_t bank = 0; bank < 2; bank++) {
  918. LightBank b = banks[bank];
  919. unspread_light(map, ndef, b, unlight[bank], relight[bank],
  920. *modified_blocks);
  921. }
  922. // --- STEP 2: Get all newly inserted light sources
  923. // For each block:
  924. v3s16 blockpos;
  925. v3s16 relpos;
  926. for (blockpos.X = minblock.X; blockpos.X <= maxblock.X; blockpos.X++)
  927. for (blockpos.Y = minblock.Y; blockpos.Y <= maxblock.Y; blockpos.Y++)
  928. for (blockpos.Z = minblock.Z; blockpos.Z <= maxblock.Z; blockpos.Z++) {
  929. MapBlock *block = map->getBlockNoCreateNoEx(blockpos);
  930. if (!block)
  931. // Skip not existing blocks
  932. continue;
  933. // For each node in the block:
  934. for (relpos.X = 0; relpos.X < MAP_BLOCKSIZE; relpos.X++)
  935. for (relpos.Z = 0; relpos.Z < MAP_BLOCKSIZE; relpos.Z++)
  936. for (relpos.Y = 0; relpos.Y < MAP_BLOCKSIZE; relpos.Y++) {
  937. MapNode node = block->getNodeNoCheck(relpos.X, relpos.Y, relpos.Z);
  938. ContentLightingFlags f = ndef->getLightingFlags(node);
  939. // For each light bank
  940. for (size_t b = 0; b < 2; b++) {
  941. LightBank bank = banks[b];
  942. u8 light = f.has_light ?
  943. node.getLight(bank, f):
  944. f.light_source;
  945. if (light > 1)
  946. relight[b].push(light, relpos, blockpos, block, 6);
  947. } // end of banks
  948. } // end of nodes
  949. } // end of blocks
  950. // --- STEP 3: do light spreading
  951. // For each light bank:
  952. for (size_t b = 0; b < 2; b++) {
  953. LightBank bank = banks[b];
  954. // Sunlight is already initialized.
  955. u8 maxlight = (b == 0) ? LIGHT_MAX : LIGHT_SUN;
  956. // Initialize light values for light spreading.
  957. for (u8 i = 0; i <= maxlight; i++) {
  958. const std::vector<ChangingLight> &lights = relight[b].lights[i];
  959. for (std::vector<ChangingLight>::const_iterator it = lights.begin();
  960. it < lights.end(); ++it) {
  961. MapNode n = it->block->getNodeNoCheck(it->rel_position);
  962. n.setLight(bank, i, ndef->getLightingFlags(n));
  963. it->block->setNodeNoCheck(it->rel_position, n);
  964. }
  965. }
  966. // Spread lights.
  967. spread_light(map, ndef, bank, relight[b], *modified_blocks);
  968. }
  969. }
  970. void blit_back_with_light(Map *map, MMVManip *vm,
  971. std::map<v3s16, MapBlock*> *modified_blocks)
  972. {
  973. const NodeDefManager *ndef = map->getNodeDefManager();
  974. if (vm->m_area.hasEmptyExtent())
  975. return;
  976. mapblock_v3 minblock = getNodeBlockPos(vm->m_area.MinEdge);
  977. mapblock_v3 maxblock = getNodeBlockPos(vm->m_area.MaxEdge);
  978. // First queue is for day light, second is for night light.
  979. UnlightQueue unlight[] = { UnlightQueue(256), UnlightQueue(256) };
  980. ReLightQueue relight[] = { ReLightQueue(256), ReLightQueue(256) };
  981. // Will hold sunlight data.
  982. bool lights[MAP_BLOCKSIZE][MAP_BLOCKSIZE];
  983. SunlightPropagationData data;
  984. // --- STEP 1: reset everything to sunlight
  985. // For each map block:
  986. for (s16 x = minblock.X; x <= maxblock.X; x++)
  987. for (s16 z = minblock.Z; z <= maxblock.Z; z++) {
  988. // Extract sunlight above.
  989. is_sunlight_above_block(map, v3s16(x, maxblock.Y, z), ndef, lights);
  990. v2s16 offset(x, z);
  991. offset *= MAP_BLOCKSIZE;
  992. // Reset the voxel manipulator.
  993. fill_with_sunlight(vm, ndef, offset, lights);
  994. // Copy sunlight data
  995. data.target_block = v3s16(x, minblock.Y - 1, z);
  996. for (s16 z = 0; z < MAP_BLOCKSIZE; z++)
  997. for (s16 x = 0; x < MAP_BLOCKSIZE; x++)
  998. data.data.emplace_back(v2s16(x, z), lights[z][x]);
  999. // Propagate sunlight and shadow below the voxel manipulator.
  1000. while (!data.data.empty()) {
  1001. if (propagate_block_sunlight(map, ndef, &data, &unlight[0],
  1002. &relight[0]))
  1003. (*modified_blocks)[data.target_block] =
  1004. map->getBlockNoCreateNoEx(data.target_block);
  1005. // Step downwards.
  1006. data.target_block.Y--;
  1007. }
  1008. }
  1009. // --- STEP 2: Get nodes from borders to unlight
  1010. v3s16 blockpos;
  1011. v3s16 relpos;
  1012. // In case there are unloaded holes in the voxel manipulator
  1013. // unlight each block.
  1014. // For each block:
  1015. for (blockpos.X = minblock.X; blockpos.X <= maxblock.X; blockpos.X++)
  1016. for (blockpos.Y = minblock.Y; blockpos.Y <= maxblock.Y; blockpos.Y++)
  1017. for (blockpos.Z = minblock.Z; blockpos.Z <= maxblock.Z; blockpos.Z++) {
  1018. MapBlock *block = map->getBlockNoCreateNoEx(blockpos);
  1019. if (!block)
  1020. // Skip not existing blocks.
  1021. continue;
  1022. v3s16 offset = block->getPosRelative();
  1023. // For each border of the block:
  1024. for (const VoxelArea &a : block_pad) {
  1025. // For each node of the border:
  1026. for (relpos.X = a.MinEdge.X; relpos.X <= a.MaxEdge.X; relpos.X++)
  1027. for (relpos.Z = a.MinEdge.Z; relpos.Z <= a.MaxEdge.Z; relpos.Z++)
  1028. for (relpos.Y = a.MinEdge.Y; relpos.Y <= a.MaxEdge.Y; relpos.Y++) {
  1029. // Get old and new node
  1030. MapNode oldnode = block->getNodeNoCheck(relpos);
  1031. ContentLightingFlags oldf = ndef->getLightingFlags(oldnode);
  1032. MapNode newnode = vm->getNodeNoExNoEmerge(relpos + offset);
  1033. ContentLightingFlags newf = ndef->getLightingFlags(newnode);
  1034. // For each light bank
  1035. for (size_t b = 0; b < 2; b++) {
  1036. LightBank bank = banks[b];
  1037. u8 oldlight = oldf.has_light ?
  1038. oldnode.getLight(bank, oldf):
  1039. LIGHT_SUN; // no light information, force unlighting
  1040. u8 newlight = newf.has_light ?
  1041. newnode.getLight(bank, newf):
  1042. newf.light_source;
  1043. // If the new node is dimmer, unlight.
  1044. if (oldlight > newlight) {
  1045. unlight[b].push(
  1046. oldlight, relpos, blockpos, block, 6);
  1047. }
  1048. } // end of banks
  1049. } // end of nodes
  1050. } // end of borders
  1051. } // end of blocks
  1052. // --- STEP 3: All information extracted, overwrite
  1053. vm->blitBackAll(modified_blocks, true);
  1054. // --- STEP 4: Finish light update
  1055. finish_bulk_light_update(map, minblock, maxblock, unlight, relight,
  1056. modified_blocks);
  1057. }
  1058. /*!
  1059. * Resets the lighting of the given map block to
  1060. * complete darkness and full sunlight.
  1061. *
  1062. * \param light incoming sunlight, light[x][z] is true if there
  1063. * is sunlight above the map block at the given x-z coordinates.
  1064. * The array's indices are relative node coordinates in the block.
  1065. * After the procedure returns, this contains outgoing light at
  1066. * the bottom of the map block.
  1067. */
  1068. void fill_with_sunlight(MapBlock *block, const NodeDefManager *ndef,
  1069. bool light[MAP_BLOCKSIZE][MAP_BLOCKSIZE])
  1070. {
  1071. // For each column of nodes:
  1072. for (s16 z = 0; z < MAP_BLOCKSIZE; z++)
  1073. for (s16 x = 0; x < MAP_BLOCKSIZE; x++) {
  1074. // True if the current node has sunlight.
  1075. bool lig = light[z][x];
  1076. // For each node, downwards:
  1077. for (s16 y = MAP_BLOCKSIZE - 1; y >= 0; y--) {
  1078. MapNode n = block->getNodeNoCheck(x, y, z);
  1079. // Ignore IGNORE nodes, these are not generated yet.
  1080. if (n.getContent() == CONTENT_IGNORE)
  1081. continue;
  1082. ContentLightingFlags f = ndef->getLightingFlags(n);
  1083. if (lig && !f.sunlight_propagates) {
  1084. // Sunlight is stopped.
  1085. lig = false;
  1086. }
  1087. // Reset light
  1088. n.setLight(LIGHTBANK_DAY, lig ? 15 : 0, f);
  1089. n.setLight(LIGHTBANK_NIGHT, 0, f);
  1090. block->setNodeNoCheck(x, y, z, n);
  1091. }
  1092. // Output outgoing light.
  1093. light[z][x] = lig;
  1094. }
  1095. }
  1096. void repair_block_light(Map *map, MapBlock *block,
  1097. std::map<v3s16, MapBlock*> *modified_blocks)
  1098. {
  1099. if (!block)
  1100. return;
  1101. const NodeDefManager *ndef = map->getNodeDefManager();
  1102. // First queue is for day light, second is for night light.
  1103. UnlightQueue unlight[] = { UnlightQueue(256), UnlightQueue(256) };
  1104. ReLightQueue relight[] = { ReLightQueue(256), ReLightQueue(256) };
  1105. // Will hold sunlight data.
  1106. bool lights[MAP_BLOCKSIZE][MAP_BLOCKSIZE];
  1107. SunlightPropagationData data;
  1108. // --- STEP 1: reset everything to sunlight
  1109. mapblock_v3 blockpos = block->getPos();
  1110. (*modified_blocks)[blockpos] = block;
  1111. // For each map block:
  1112. // Extract sunlight above.
  1113. is_sunlight_above_block(map, blockpos, ndef, lights);
  1114. // Reset the voxel manipulator.
  1115. fill_with_sunlight(block, ndef, lights);
  1116. // Copy sunlight data
  1117. data.target_block = v3s16(blockpos.X, blockpos.Y - 1, blockpos.Z);
  1118. for (s16 z = 0; z < MAP_BLOCKSIZE; z++)
  1119. for (s16 x = 0; x < MAP_BLOCKSIZE; x++) {
  1120. data.data.emplace_back(v2s16(x, z), lights[z][x]);
  1121. }
  1122. // Propagate sunlight and shadow below the voxel manipulator.
  1123. while (!data.data.empty()) {
  1124. if (propagate_block_sunlight(map, ndef, &data, &unlight[0],
  1125. &relight[0]))
  1126. (*modified_blocks)[data.target_block] =
  1127. map->getBlockNoCreateNoEx(data.target_block);
  1128. // Step downwards.
  1129. data.target_block.Y--;
  1130. }
  1131. // --- STEP 2: Get nodes from borders to unlight
  1132. // For each border of the block:
  1133. for (const VoxelArea &a : block_pad) {
  1134. v3s16 relpos;
  1135. // For each node of the border:
  1136. for (relpos.X = a.MinEdge.X; relpos.X <= a.MaxEdge.X; relpos.X++)
  1137. for (relpos.Z = a.MinEdge.Z; relpos.Z <= a.MaxEdge.Z; relpos.Z++)
  1138. for (relpos.Y = a.MinEdge.Y; relpos.Y <= a.MaxEdge.Y; relpos.Y++) {
  1139. // Get node
  1140. MapNode node = block->getNodeNoCheck(relpos);
  1141. ContentLightingFlags f = ndef->getLightingFlags(node);
  1142. // For each light bank
  1143. for (size_t b = 0; b < 2; b++) {
  1144. LightBank bank = banks[b];
  1145. u8 light = f.has_light ?
  1146. node.getLight(bank, f):
  1147. f.light_source;
  1148. // If the new node is dimmer than sunlight, unlight.
  1149. // (if it has maximal light, it is pointless to remove
  1150. // surrounding light, as it can only become brighter)
  1151. if (LIGHT_SUN > light) {
  1152. unlight[b].push(
  1153. LIGHT_SUN, relpos, blockpos, block, 6);
  1154. }
  1155. } // end of banks
  1156. } // end of nodes
  1157. } // end of borders
  1158. // STEP 3: Remove and spread light
  1159. finish_bulk_light_update(map, blockpos, blockpos, unlight, relight,
  1160. modified_blocks);
  1161. }
  1162. VoxelLineIterator::VoxelLineIterator(const v3f &start_position, const v3f &line_vector) :
  1163. m_start_position(start_position),
  1164. m_line_vector(line_vector)
  1165. {
  1166. m_current_node_pos = floatToInt(m_start_position, 1);
  1167. m_start_node_pos = m_current_node_pos;
  1168. m_last_index = getIndex(floatToInt(start_position + line_vector, 1));
  1169. if (m_line_vector.X > 0) {
  1170. m_next_intersection_multi.X = (m_current_node_pos.X + 0.5f
  1171. - m_start_position.X) / m_line_vector.X;
  1172. m_intersection_multi_inc.X = 1 / m_line_vector.X;
  1173. } else if (m_line_vector.X < 0) {
  1174. m_next_intersection_multi.X = (m_current_node_pos.X - 0.5f
  1175. - m_start_position.X) / m_line_vector.X;
  1176. m_intersection_multi_inc.X = -1 / m_line_vector.X;
  1177. m_step_directions.X = -1;
  1178. }
  1179. if (m_line_vector.Y > 0) {
  1180. m_next_intersection_multi.Y = (m_current_node_pos.Y + 0.5f
  1181. - m_start_position.Y) / m_line_vector.Y;
  1182. m_intersection_multi_inc.Y = 1 / m_line_vector.Y;
  1183. } else if (m_line_vector.Y < 0) {
  1184. m_next_intersection_multi.Y = (m_current_node_pos.Y - 0.5f
  1185. - m_start_position.Y) / m_line_vector.Y;
  1186. m_intersection_multi_inc.Y = -1 / m_line_vector.Y;
  1187. m_step_directions.Y = -1;
  1188. }
  1189. if (m_line_vector.Z > 0) {
  1190. m_next_intersection_multi.Z = (m_current_node_pos.Z + 0.5f
  1191. - m_start_position.Z) / m_line_vector.Z;
  1192. m_intersection_multi_inc.Z = 1 / m_line_vector.Z;
  1193. } else if (m_line_vector.Z < 0) {
  1194. m_next_intersection_multi.Z = (m_current_node_pos.Z - 0.5f
  1195. - m_start_position.Z) / m_line_vector.Z;
  1196. m_intersection_multi_inc.Z = -1 / m_line_vector.Z;
  1197. m_step_directions.Z = -1;
  1198. }
  1199. }
  1200. void VoxelLineIterator::next()
  1201. {
  1202. m_current_index++;
  1203. if ((m_next_intersection_multi.X < m_next_intersection_multi.Y)
  1204. && (m_next_intersection_multi.X < m_next_intersection_multi.Z)) {
  1205. m_next_intersection_multi.X += m_intersection_multi_inc.X;
  1206. m_current_node_pos.X += m_step_directions.X;
  1207. } else if ((m_next_intersection_multi.Y < m_next_intersection_multi.Z)) {
  1208. m_next_intersection_multi.Y += m_intersection_multi_inc.Y;
  1209. m_current_node_pos.Y += m_step_directions.Y;
  1210. } else {
  1211. m_next_intersection_multi.Z += m_intersection_multi_inc.Z;
  1212. m_current_node_pos.Z += m_step_directions.Z;
  1213. }
  1214. }
  1215. s16 VoxelLineIterator::getIndex(v3s16 voxel){
  1216. return
  1217. abs(voxel.X - m_start_node_pos.X) +
  1218. abs(voxel.Y - m_start_node_pos.Y) +
  1219. abs(voxel.Z - m_start_node_pos.Z);
  1220. }
  1221. } // namespace voxalgo