voxelalgorithms.cpp 41 KB

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