voxelalgorithms.cpp 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332
  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. // A dummy boolean
  243. bool is_valid_position;
  244. // Direction of the brightest neighbor of the node
  245. direction source_dir;
  246. while (from_nodes.next(current_light, current)) {
  247. // For all nodes that need unlighting
  248. // There is no brightest neighbor
  249. source_dir = 6;
  250. // The current node
  251. const MapNode &node = current.block->getNodeNoCheck(
  252. current.rel_position, &is_valid_position);
  253. const ContentFeatures &f = nodemgr->get(node);
  254. // If the node emits light, it behaves like it had a
  255. // brighter neighbor.
  256. u8 brightest_neighbor_light = f.light_source + 1;
  257. for (direction i = 0; i < 6; i++) {
  258. //For each neighbor
  259. // The node that changed this node has already zero light
  260. // and it can't give light to this node
  261. if (current.source_direction + i == 5) {
  262. continue;
  263. }
  264. // Get the neighbor's position and block
  265. neighbor_rel_pos = current.rel_position;
  266. neighbor_block_pos = current.block_position;
  267. MapBlock *neighbor_block;
  268. if (step_rel_block_pos(i, neighbor_rel_pos, neighbor_block_pos)) {
  269. neighbor_block = map->getBlockNoCreateNoEx(neighbor_block_pos);
  270. if (neighbor_block == NULL) {
  271. current.block->setLightingComplete(bank, i, false);
  272. continue;
  273. }
  274. } else {
  275. neighbor_block = current.block;
  276. }
  277. // Get the neighbor itself
  278. MapNode neighbor = neighbor_block->getNodeNoCheck(neighbor_rel_pos,
  279. &is_valid_position);
  280. const ContentFeatures &neighbor_f = nodemgr->get(
  281. neighbor.getContent());
  282. u8 neighbor_light = neighbor.getLightRaw(bank, neighbor_f);
  283. // If the neighbor has at least as much light as this node, then
  284. // it won't lose its light, since it should have been added to
  285. // from_nodes earlier, so its light would be zero.
  286. if (neighbor_f.light_propagates && neighbor_light < current_light) {
  287. // Unlight, but only if the node has light.
  288. if (neighbor_light > 0) {
  289. neighbor.setLight(bank, 0, neighbor_f);
  290. neighbor_block->setNodeNoCheck(neighbor_rel_pos, neighbor);
  291. from_nodes.push(neighbor_light, neighbor_rel_pos,
  292. neighbor_block_pos, neighbor_block, i);
  293. // The current node was modified earlier, so its block
  294. // is in modified_blocks.
  295. if (current.block != neighbor_block) {
  296. modified_blocks[neighbor_block_pos] = neighbor_block;
  297. }
  298. }
  299. } else {
  300. // The neighbor can light up this node.
  301. if (neighbor_light < neighbor_f.light_source) {
  302. neighbor_light = neighbor_f.light_source;
  303. }
  304. if (brightest_neighbor_light < neighbor_light) {
  305. brightest_neighbor_light = neighbor_light;
  306. source_dir = i;
  307. }
  308. }
  309. }
  310. // If the brightest neighbor is able to light up this node,
  311. // then add this node to the output nodes.
  312. if (brightest_neighbor_light > 1 && f.light_propagates) {
  313. brightest_neighbor_light--;
  314. light_sources.push(brightest_neighbor_light, current.rel_position,
  315. current.block_position, current.block,
  316. (source_dir == 6) ? 6 : 5 - source_dir
  317. /* with opposite direction*/);
  318. }
  319. }
  320. }
  321. /*
  322. * Spreads light from the specified starting nodes.
  323. *
  324. * Before calling this procedure, make sure that all ChangingLights
  325. * in light_sources have as much light on the map as they have in
  326. * light_sources (if the queue contains a node multiple times, the brightest
  327. * occurrence counts).
  328. *
  329. * \param bank the light bank in which the procedure operates
  330. * \param light_sources starting nodes
  331. * \param modified_blocks output, all modified map blocks are added to this
  332. */
  333. void spread_light(Map *map, const NodeDefManager *nodemgr, LightBank bank,
  334. LightQueue &light_sources,
  335. std::map<v3s16, MapBlock*> &modified_blocks)
  336. {
  337. // The light the current node can provide to its neighbors.
  338. u8 spreading_light;
  339. // The ChangingLight for the current node.
  340. ChangingLight current;
  341. // Position of the current neighbor.
  342. mapblock_v3 neighbor_block_pos;
  343. relative_v3 neighbor_rel_pos;
  344. // A dummy boolean.
  345. bool is_valid_position;
  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. &is_valid_position);
  369. const ContentFeatures &f = nodemgr->get(neighbor.getContent());
  370. if (f.light_propagates) {
  371. // Light up the neighbor, if it has less light than it should.
  372. u8 neighbor_light = neighbor.getLightRaw(bank, f);
  373. if (neighbor_light < spreading_light) {
  374. neighbor.setLight(bank, spreading_light, f);
  375. neighbor_block->setNodeNoCheck(neighbor_rel_pos, neighbor);
  376. light_sources.push(spreading_light, neighbor_rel_pos,
  377. neighbor_block_pos, neighbor_block, i);
  378. // The current node was modified earlier, so its block
  379. // is in modified_blocks.
  380. if (current.block != neighbor_block) {
  381. modified_blocks[neighbor_block_pos] = neighbor_block;
  382. }
  383. }
  384. }
  385. }
  386. }
  387. }
  388. struct SunlightPropagationUnit{
  389. v2s16 relative_pos;
  390. bool is_sunlit;
  391. SunlightPropagationUnit(v2s16 relpos, bool sunlit):
  392. relative_pos(relpos),
  393. is_sunlit(sunlit)
  394. {}
  395. };
  396. struct SunlightPropagationData{
  397. std::vector<SunlightPropagationUnit> data;
  398. v3s16 target_block;
  399. };
  400. /*!
  401. * Returns true if the node gets sunlight from the
  402. * node above it.
  403. *
  404. * \param pos position of the node.
  405. */
  406. bool is_sunlight_above(Map *map, v3s16 pos, const NodeDefManager *ndef)
  407. {
  408. bool sunlight = true;
  409. mapblock_v3 source_block_pos;
  410. relative_v3 source_rel_pos;
  411. getNodeBlockPosWithOffset(pos + v3s16(0, 1, 0), source_block_pos,
  412. source_rel_pos);
  413. // If the node above has sunlight, this node also can get it.
  414. MapBlock *source_block = map->getBlockNoCreateNoEx(source_block_pos);
  415. if (source_block == NULL) {
  416. // But if there is no node above, then use heuristics
  417. MapBlock *node_block = map->getBlockNoCreateNoEx(getNodeBlockPos(pos));
  418. if (node_block == NULL) {
  419. sunlight = false;
  420. } else {
  421. sunlight = !node_block->getIsUnderground();
  422. }
  423. } else {
  424. bool is_valid_position;
  425. MapNode above = source_block->getNodeNoCheck(source_rel_pos,
  426. &is_valid_position);
  427. if (is_valid_position) {
  428. if (above.getContent() == CONTENT_IGNORE) {
  429. // Trust heuristics
  430. if (source_block->getIsUnderground()) {
  431. sunlight = false;
  432. }
  433. } else if (above.getLight(LIGHTBANK_DAY, ndef) != LIGHT_SUN) {
  434. // If the node above doesn't have sunlight, this
  435. // node is in shadow.
  436. sunlight = false;
  437. }
  438. }
  439. }
  440. return sunlight;
  441. }
  442. static const LightBank banks[] = { LIGHTBANK_DAY, LIGHTBANK_NIGHT };
  443. void update_lighting_nodes(Map *map,
  444. const std::vector<std::pair<v3s16, MapNode>> &oldnodes,
  445. std::map<v3s16, MapBlock*> &modified_blocks)
  446. {
  447. const NodeDefManager *ndef = map->getNodeDefManager();
  448. // For node getter functions
  449. bool is_valid_position;
  450. // Process each light bank separately
  451. for (LightBank bank : banks) {
  452. UnlightQueue disappearing_lights(256);
  453. ReLightQueue light_sources(256);
  454. // Nodes that are brighter than the brightest modified node was
  455. // won't change, since they didn't get their light from a
  456. // modified node.
  457. u8 min_safe_light = 0;
  458. for (auto it = oldnodes.cbegin(); it < oldnodes.cend(); ++it) {
  459. u8 old_light = it->second.getLight(bank, ndef);
  460. if (old_light > min_safe_light) {
  461. min_safe_light = old_light;
  462. }
  463. }
  464. // If only one node changed, even nodes with the same brightness
  465. // didn't get their light from the changed node.
  466. if (oldnodes.size() > 1) {
  467. min_safe_light++;
  468. }
  469. // For each changed node process sunlight and initialize
  470. for (auto it = oldnodes.cbegin(); it < oldnodes.cend(); ++it) {
  471. // Get position and block of the changed node
  472. v3s16 p = it->first;
  473. relative_v3 rel_pos;
  474. mapblock_v3 block_pos;
  475. getNodeBlockPosWithOffset(p, block_pos, rel_pos);
  476. MapBlock *block = map->getBlockNoCreateNoEx(block_pos);
  477. if (block == NULL || block->isDummy()) {
  478. continue;
  479. }
  480. // Get the new node
  481. MapNode n = block->getNodeNoCheck(rel_pos, &is_valid_position);
  482. if (!is_valid_position) {
  483. break;
  484. }
  485. // Light of the old node
  486. u8 old_light = it->second.getLight(bank, ndef);
  487. // Add the block of the added node to modified_blocks
  488. modified_blocks[block_pos] = block;
  489. // Get new light level of the node
  490. u8 new_light = 0;
  491. if (ndef->get(n).light_propagates) {
  492. if (bank == LIGHTBANK_DAY && ndef->get(n).sunlight_propagates
  493. && is_sunlight_above(map, p, ndef)) {
  494. new_light = LIGHT_SUN;
  495. } else {
  496. new_light = ndef->get(n).light_source;
  497. for (const v3s16 &neighbor_dir : neighbor_dirs) {
  498. v3s16 p2 = p + neighbor_dir;
  499. bool is_valid;
  500. MapNode n2 = map->getNode(p2, &is_valid);
  501. if (is_valid) {
  502. u8 spread = n2.getLight(bank, ndef);
  503. // If it is sure that the neighbor won't be
  504. // unlighted, its light can spread to this node.
  505. if (spread > new_light && spread >= min_safe_light) {
  506. new_light = spread - 1;
  507. }
  508. }
  509. }
  510. }
  511. } else {
  512. // If this is an opaque node, it still can emit light.
  513. new_light = ndef->get(n).light_source;
  514. }
  515. if (new_light > 0) {
  516. light_sources.push(new_light, rel_pos, block_pos, block, 6);
  517. }
  518. if (new_light < old_light) {
  519. // The node became opaque or doesn't provide as much
  520. // light as the previous one, so it must be unlighted.
  521. // Add to unlight queue
  522. n.setLight(bank, 0, ndef);
  523. block->setNodeNoCheck(rel_pos, n);
  524. disappearing_lights.push(old_light, rel_pos, block_pos, block,
  525. 6);
  526. // Remove sunlight, if there was any
  527. if (bank == LIGHTBANK_DAY && old_light == LIGHT_SUN) {
  528. for (s16 y = p.Y - 1;; y--) {
  529. v3s16 n2pos(p.X, y, p.Z);
  530. MapNode n2;
  531. n2 = map->getNode(n2pos, &is_valid_position);
  532. if (!is_valid_position)
  533. break;
  534. // If this node doesn't have sunlight, the nodes below
  535. // it don't have too.
  536. if (n2.getLight(LIGHTBANK_DAY, ndef) != LIGHT_SUN) {
  537. break;
  538. }
  539. // Remove sunlight and add to unlight queue.
  540. n2.setLight(LIGHTBANK_DAY, 0, ndef);
  541. map->setNode(n2pos, n2);
  542. relative_v3 rel_pos2;
  543. mapblock_v3 block_pos2;
  544. getNodeBlockPosWithOffset(n2pos, block_pos2, rel_pos2);
  545. MapBlock *block2 = map->getBlockNoCreateNoEx(
  546. block_pos2);
  547. disappearing_lights.push(LIGHT_SUN, rel_pos2,
  548. block_pos2, block2,
  549. 4 /* The node above caused the change */);
  550. }
  551. }
  552. } else if (new_light > old_light) {
  553. // It is sure that the node provides more light than the previous
  554. // one, unlighting is not necessary.
  555. // Propagate sunlight
  556. if (bank == LIGHTBANK_DAY && new_light == LIGHT_SUN) {
  557. for (s16 y = p.Y - 1;; y--) {
  558. v3s16 n2pos(p.X, y, p.Z);
  559. MapNode n2;
  560. n2 = map->getNode(n2pos, &is_valid_position);
  561. if (!is_valid_position)
  562. break;
  563. // This should not happen, but if the node has sunlight
  564. // then the iteration should stop.
  565. if (n2.getLight(LIGHTBANK_DAY, ndef) == LIGHT_SUN) {
  566. break;
  567. }
  568. // If the node terminates sunlight, stop.
  569. if (!ndef->get(n2).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 std::vector<ChangingLight> &lights = light_sources.lights[i];
  590. for (std::vector<ChangingLight>::const_iterator it = lights.begin();
  591. it < lights.end(); ++it) {
  592. MapNode n = it->block->getNodeNoCheck(it->rel_position,
  593. &is_valid_position);
  594. n.setLight(bank, i, ndef);
  595. it->block->setNodeNoCheck(it->rel_position, n);
  596. }
  597. }
  598. // Spread lights.
  599. spread_light(map, ndef, bank, light_sources, modified_blocks);
  600. }
  601. }
  602. /*!
  603. * Borders of a map block in relative node coordinates.
  604. * Compatible with type 'direction'.
  605. */
  606. const VoxelArea block_borders[] = {
  607. VoxelArea(v3s16(15, 0, 0), v3s16(15, 15, 15)), //X+
  608. VoxelArea(v3s16(0, 15, 0), v3s16(15, 15, 15)), //Y+
  609. VoxelArea(v3s16(0, 0, 15), v3s16(15, 15, 15)), //Z+
  610. VoxelArea(v3s16(0, 0, 0), v3s16(15, 15, 0)), //Z-
  611. VoxelArea(v3s16(0, 0, 0), v3s16(15, 0, 15)), //Y-
  612. VoxelArea(v3s16(0, 0, 0), v3s16(0, 15, 15)) //X-
  613. };
  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. const ContentFeatures &f = ndef->get(n);
  628. if (f.param_type != CPT_LIGHT) {
  629. return true;
  630. }
  631. u8 light = n.getLightNoChecks(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);
  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. bool is_valid_position;
  650. for (LightBank bank : banks) {
  651. // Since invalid light is not common, do not allocate
  652. // memory if not needed.
  653. UnlightQueue disappearing_lights(0);
  654. ReLightQueue light_sources(0);
  655. // Get incorrect lights
  656. for (direction d = 0; d < 6; d++) {
  657. // For each direction
  658. // Get neighbor block
  659. v3s16 otherpos = block->getPos() + neighbor_dirs[d];
  660. MapBlock *other = map->getBlockNoCreateNoEx(otherpos);
  661. if (other == NULL) {
  662. continue;
  663. }
  664. // Only update if lighting was not completed.
  665. if (block->isLightingComplete(bank, d) &&
  666. other->isLightingComplete(bank, 5 - d))
  667. continue;
  668. // Reset flags
  669. block->setLightingComplete(bank, d, true);
  670. other->setLightingComplete(bank, 5 - d, true);
  671. // The two blocks and their connecting surfaces
  672. MapBlock *blocks[] = {block, other};
  673. VoxelArea areas[] = {block_borders[d], block_borders[5 - d]};
  674. // For both blocks
  675. for (u8 blocknum = 0; blocknum < 2; blocknum++) {
  676. MapBlock *b = blocks[blocknum];
  677. VoxelArea a = areas[blocknum];
  678. // For all nodes
  679. for (s32 x = a.MinEdge.X; x <= a.MaxEdge.X; x++)
  680. for (s32 z = a.MinEdge.Z; z <= a.MaxEdge.Z; z++)
  681. for (s32 y = a.MinEdge.Y; y <= a.MaxEdge.Y; y++) {
  682. MapNode n = b->getNodeNoCheck(x, y, z,
  683. &is_valid_position);
  684. u8 light = n.getLight(bank, ndef);
  685. // Sunlight is fixed
  686. if (light < LIGHT_SUN) {
  687. // Unlight if not correct
  688. if (!is_light_locally_correct(map, ndef, bank,
  689. v3s16(x, y, z) + b->getPosRelative())) {
  690. // Initialize for unlighting
  691. n.setLight(bank, 0, ndef);
  692. b->setNodeNoCheck(x, y, z, n);
  693. modified_blocks[b->getPos()]=b;
  694. disappearing_lights.push(light,
  695. relative_v3(x, y, z), b->getPos(), b,
  696. 6);
  697. }
  698. }
  699. }
  700. }
  701. }
  702. // Remove lights
  703. unspread_light(map, ndef, bank, disappearing_lights, light_sources,
  704. modified_blocks);
  705. // Initialize light values for light spreading.
  706. for (u8 i = 0; i <= LIGHT_SUN; i++) {
  707. const std::vector<ChangingLight> &lights = light_sources.lights[i];
  708. for (std::vector<ChangingLight>::const_iterator it = lights.begin();
  709. it < lights.end(); ++it) {
  710. MapNode n = it->block->getNodeNoCheck(it->rel_position,
  711. &is_valid_position);
  712. n.setLight(bank, i, ndef);
  713. it->block->setNodeNoCheck(it->rel_position, n);
  714. }
  715. }
  716. // Spread lights.
  717. spread_light(map, ndef, bank, light_sources, modified_blocks);
  718. }
  719. }
  720. /*!
  721. * Resets the lighting of the given VoxelManipulator to
  722. * complete darkness and full sunlight.
  723. * Operates in one map sector.
  724. *
  725. * \param offset contains the least x and z node coordinates
  726. * of the map sector.
  727. * \param light incoming sunlight, light[x][z] is true if there
  728. * is sunlight above the voxel manipulator at the given x-z coordinates.
  729. * The array's indices are relative node coordinates in the sector.
  730. * After the procedure returns, this contains outgoing light at
  731. * the bottom of the voxel manipulator.
  732. */
  733. void fill_with_sunlight(MMVManip *vm, const NodeDefManager *ndef, v2s16 offset,
  734. bool light[MAP_BLOCKSIZE][MAP_BLOCKSIZE])
  735. {
  736. // Distance in array between two nodes on top of each other.
  737. s16 ystride = vm->m_area.getExtent().X;
  738. // Cache the ignore node.
  739. MapNode ignore = MapNode(CONTENT_IGNORE);
  740. // For each column of nodes:
  741. for (s16 z = 0; z < MAP_BLOCKSIZE; z++)
  742. for (s16 x = 0; x < MAP_BLOCKSIZE; x++) {
  743. // Position of the column on the map.
  744. v2s16 realpos = offset + v2s16(x, z);
  745. // Array indices in the voxel manipulator
  746. s32 maxindex = vm->m_area.index(realpos.X, vm->m_area.MaxEdge.Y,
  747. realpos.Y);
  748. s32 minindex = vm->m_area.index(realpos.X, vm->m_area.MinEdge.Y,
  749. realpos.Y);
  750. // True if the current node has sunlight.
  751. bool lig = light[z][x];
  752. // For each node, downwards:
  753. for (s32 i = maxindex; i >= minindex; i -= ystride) {
  754. MapNode *n;
  755. if (vm->m_flags[i] & VOXELFLAG_NO_DATA)
  756. n = &ignore;
  757. else
  758. n = &vm->m_data[i];
  759. // Ignore IGNORE nodes, these are not generated yet.
  760. if(n->getContent() == CONTENT_IGNORE)
  761. continue;
  762. const ContentFeatures &f = ndef->get(n->getContent());
  763. if (lig && !f.sunlight_propagates)
  764. // Sunlight is stopped.
  765. lig = false;
  766. // Reset light
  767. n->setLight(LIGHTBANK_DAY, lig ? 15 : 0, f);
  768. n->setLight(LIGHTBANK_NIGHT, 0, f);
  769. }
  770. // Output outgoing light.
  771. light[z][x] = lig;
  772. }
  773. }
  774. /*!
  775. * Returns incoming sunlight for one map block.
  776. * If block above is not found, it is loaded.
  777. *
  778. * \param pos position of the map block that gets the sunlight.
  779. * \param light incoming sunlight, light[z][x] is true if there
  780. * is sunlight above the block at the given z-x relative
  781. * node coordinates.
  782. */
  783. void is_sunlight_above_block(ServerMap *map, mapblock_v3 pos,
  784. const NodeDefManager *ndef, bool light[MAP_BLOCKSIZE][MAP_BLOCKSIZE])
  785. {
  786. mapblock_v3 source_block_pos = pos + v3s16(0, 1, 0);
  787. // Get or load source block.
  788. // It might take a while to load, but correcting incorrect
  789. // sunlight may be even slower.
  790. MapBlock *source_block = map->emergeBlock(source_block_pos, false);
  791. // Trust only generated blocks.
  792. if (source_block == NULL || source_block->isDummy()
  793. || !source_block->isGenerated()) {
  794. // But if there is no block above, then use heuristics
  795. bool sunlight = true;
  796. MapBlock *node_block = map->getBlockNoCreateNoEx(pos);
  797. if (node_block == NULL)
  798. // This should not happen.
  799. sunlight = false;
  800. else
  801. sunlight = !node_block->getIsUnderground();
  802. for (s16 z = 0; z < MAP_BLOCKSIZE; z++)
  803. for (s16 x = 0; x < MAP_BLOCKSIZE; x++)
  804. light[z][x] = sunlight;
  805. } else {
  806. // Dummy boolean, the position is valid.
  807. bool is_valid_position;
  808. // For each column:
  809. for (s16 z = 0; z < MAP_BLOCKSIZE; z++)
  810. for (s16 x = 0; x < MAP_BLOCKSIZE; x++) {
  811. // Get the bottom block.
  812. MapNode above = source_block->getNodeNoCheck(x, 0, z,
  813. &is_valid_position);
  814. light[z][x] = above.getLight(LIGHTBANK_DAY, ndef) == LIGHT_SUN;
  815. }
  816. }
  817. }
  818. /*!
  819. * Propagates sunlight down in a given map block.
  820. *
  821. * \param data contains incoming sunlight and shadow and
  822. * the coordinates of the target block.
  823. * \param unlight propagated shadow is inserted here
  824. * \param relight propagated sunlight is inserted here
  825. *
  826. * \returns true if the block was modified, false otherwise.
  827. */
  828. bool propagate_block_sunlight(Map *map, const NodeDefManager *ndef,
  829. SunlightPropagationData *data, UnlightQueue *unlight, ReLightQueue *relight)
  830. {
  831. bool modified = false;
  832. // Get the block.
  833. MapBlock *block = map->getBlockNoCreateNoEx(data->target_block);
  834. if (block == NULL || block->isDummy()) {
  835. // The work is done if the block does not contain data.
  836. data->data.clear();
  837. return false;
  838. }
  839. // Dummy boolean
  840. bool is_valid;
  841. // For each changing column of nodes:
  842. size_t index;
  843. for (index = 0; index < data->data.size(); index++) {
  844. SunlightPropagationUnit it = data->data[index];
  845. // Relative position of the currently inspected node.
  846. relative_v3 current_pos(it.relative_pos.X, MAP_BLOCKSIZE - 1,
  847. it.relative_pos.Y);
  848. if (it.is_sunlit) {
  849. // Propagate sunlight.
  850. // For each node downwards:
  851. for (; current_pos.Y >= 0; current_pos.Y--) {
  852. MapNode n = block->getNodeNoCheck(current_pos, &is_valid);
  853. const ContentFeatures &f = ndef->get(n);
  854. if (n.getLightRaw(LIGHTBANK_DAY, f) < LIGHT_SUN
  855. && f.sunlight_propagates) {
  856. // This node gets sunlight.
  857. n.setLight(LIGHTBANK_DAY, LIGHT_SUN, f);
  858. block->setNodeNoCheck(current_pos, n);
  859. modified = true;
  860. relight->push(LIGHT_SUN, current_pos, data->target_block,
  861. block, 4);
  862. } else {
  863. // Light already valid, propagation stopped.
  864. break;
  865. }
  866. }
  867. } else {
  868. // Propagate shadow.
  869. // For each node downwards:
  870. for (; current_pos.Y >= 0; current_pos.Y--) {
  871. MapNode n = block->getNodeNoCheck(current_pos, &is_valid);
  872. const ContentFeatures &f = ndef->get(n);
  873. if (n.getLightRaw(LIGHTBANK_DAY, f) == LIGHT_SUN) {
  874. // The sunlight is no longer valid.
  875. n.setLight(LIGHTBANK_DAY, 0, f);
  876. block->setNodeNoCheck(current_pos, n);
  877. modified = true;
  878. unlight->push(LIGHT_SUN, current_pos, data->target_block,
  879. block, 4);
  880. } else {
  881. // Reached shadow, propagation stopped.
  882. break;
  883. }
  884. }
  885. }
  886. if (current_pos.Y >= 0) {
  887. // Propagation stopped, remove from data.
  888. data->data[index] = data->data.back();
  889. data->data.pop_back();
  890. index--;
  891. }
  892. }
  893. return modified;
  894. }
  895. /*!
  896. * Borders of a map block in relative node coordinates.
  897. * The areas do not overlap.
  898. * Compatible with type 'direction'.
  899. */
  900. const VoxelArea block_pad[] = {
  901. VoxelArea(v3s16(15, 0, 0), v3s16(15, 15, 15)), //X+
  902. VoxelArea(v3s16(1, 15, 0), v3s16(14, 15, 15)), //Y+
  903. VoxelArea(v3s16(1, 1, 15), v3s16(14, 14, 15)), //Z+
  904. VoxelArea(v3s16(1, 1, 0), v3s16(14, 14, 0)), //Z-
  905. VoxelArea(v3s16(1, 0, 0), v3s16(14, 0, 15)), //Y-
  906. VoxelArea(v3s16(0, 0, 0), v3s16(0, 15, 15)) //X-
  907. };
  908. /*!
  909. * The common part of bulk light updates - it is always executed.
  910. * The procedure takes the nodes that should be unlit, and the
  911. * full modified area.
  912. *
  913. * The procedure handles the correction of all lighting except
  914. * direct sunlight spreading.
  915. *
  916. * \param minblock least coordinates of the changed area in block
  917. * coordinates
  918. * \param maxblock greatest coordinates of the changed area in block
  919. * coordinates
  920. * \param unlight the first queue is for day light, the second is for
  921. * night light. Contains all nodes on the borders that need to be unlit.
  922. * \param relight the first queue is for day light, the second is for
  923. * night light. Contains nodes that were not modified, but got sunlight
  924. * because the changes.
  925. * \param modified_blocks the procedure adds all modified blocks to
  926. * this map
  927. */
  928. void finish_bulk_light_update(Map *map, mapblock_v3 minblock,
  929. mapblock_v3 maxblock, UnlightQueue unlight[2], ReLightQueue relight[2],
  930. std::map<v3s16, MapBlock*> *modified_blocks)
  931. {
  932. const NodeDefManager *ndef = map->getNodeDefManager();
  933. // dummy boolean
  934. bool is_valid;
  935. // --- STEP 1: Do unlighting
  936. for (size_t bank = 0; bank < 2; bank++) {
  937. LightBank b = banks[bank];
  938. unspread_light(map, ndef, b, unlight[bank], relight[bank],
  939. *modified_blocks);
  940. }
  941. // --- STEP 2: Get all newly inserted light sources
  942. // For each block:
  943. v3s16 blockpos;
  944. v3s16 relpos;
  945. for (blockpos.X = minblock.X; blockpos.X <= maxblock.X; blockpos.X++)
  946. for (blockpos.Y = minblock.Y; blockpos.Y <= maxblock.Y; blockpos.Y++)
  947. for (blockpos.Z = minblock.Z; blockpos.Z <= maxblock.Z; blockpos.Z++) {
  948. MapBlock *block = map->getBlockNoCreateNoEx(blockpos);
  949. if (!block || block->isDummy())
  950. // Skip not existing blocks
  951. continue;
  952. // For each node in the block:
  953. for (relpos.X = 0; relpos.X < MAP_BLOCKSIZE; relpos.X++)
  954. for (relpos.Z = 0; relpos.Z < MAP_BLOCKSIZE; relpos.Z++)
  955. for (relpos.Y = 0; relpos.Y < MAP_BLOCKSIZE; relpos.Y++) {
  956. MapNode node = block->getNodeNoCheck(relpos.X, relpos.Y, relpos.Z, &is_valid);
  957. const ContentFeatures &f = ndef->get(node);
  958. // For each light bank
  959. for (size_t b = 0; b < 2; b++) {
  960. LightBank bank = banks[b];
  961. u8 light = f.param_type == CPT_LIGHT ?
  962. node.getLightNoChecks(bank, &f):
  963. f.light_source;
  964. if (light > 1)
  965. relight[b].push(light, relpos, blockpos, block, 6);
  966. } // end of banks
  967. } // end of nodes
  968. } // end of blocks
  969. // --- STEP 3: do light spreading
  970. // For each light bank:
  971. for (size_t b = 0; b < 2; b++) {
  972. LightBank bank = banks[b];
  973. // Sunlight is already initialized.
  974. u8 maxlight = (b == 0) ? LIGHT_MAX : LIGHT_SUN;
  975. // Initialize light values for light spreading.
  976. for (u8 i = 0; i <= maxlight; i++) {
  977. const std::vector<ChangingLight> &lights = relight[b].lights[i];
  978. for (std::vector<ChangingLight>::const_iterator it = lights.begin();
  979. it < lights.end(); ++it) {
  980. MapNode n = it->block->getNodeNoCheck(it->rel_position,
  981. &is_valid);
  982. n.setLight(bank, i, ndef);
  983. it->block->setNodeNoCheck(it->rel_position, n);
  984. }
  985. }
  986. // Spread lights.
  987. spread_light(map, ndef, bank, relight[b], *modified_blocks);
  988. }
  989. }
  990. void blit_back_with_light(ServerMap *map, MMVManip *vm,
  991. std::map<v3s16, MapBlock*> *modified_blocks)
  992. {
  993. const NodeDefManager *ndef = map->getNodeDefManager();
  994. mapblock_v3 minblock = getNodeBlockPos(vm->m_area.MinEdge);
  995. mapblock_v3 maxblock = getNodeBlockPos(vm->m_area.MaxEdge);
  996. // First queue is for day light, second is for night light.
  997. UnlightQueue unlight[] = { UnlightQueue(256), UnlightQueue(256) };
  998. ReLightQueue relight[] = { ReLightQueue(256), ReLightQueue(256) };
  999. // Will hold sunlight data.
  1000. bool lights[MAP_BLOCKSIZE][MAP_BLOCKSIZE];
  1001. SunlightPropagationData data;
  1002. // Dummy boolean.
  1003. bool is_valid;
  1004. // --- STEP 1: reset everything to sunlight
  1005. // For each map block:
  1006. for (s16 x = minblock.X; x <= maxblock.X; x++)
  1007. for (s16 z = minblock.Z; z <= maxblock.Z; z++) {
  1008. // Extract sunlight above.
  1009. is_sunlight_above_block(map, v3s16(x, maxblock.Y, z), ndef, lights);
  1010. v2s16 offset(x, z);
  1011. offset *= MAP_BLOCKSIZE;
  1012. // Reset the voxel manipulator.
  1013. fill_with_sunlight(vm, ndef, offset, lights);
  1014. // Copy sunlight data
  1015. data.target_block = v3s16(x, minblock.Y - 1, z);
  1016. for (s16 z = 0; z < MAP_BLOCKSIZE; z++)
  1017. for (s16 x = 0; x < MAP_BLOCKSIZE; x++)
  1018. data.data.emplace_back(v2s16(x, z), lights[z][x]);
  1019. // Propagate sunlight and shadow below the voxel manipulator.
  1020. while (!data.data.empty()) {
  1021. if (propagate_block_sunlight(map, ndef, &data, &unlight[0],
  1022. &relight[0]))
  1023. (*modified_blocks)[data.target_block] =
  1024. map->getBlockNoCreateNoEx(data.target_block);
  1025. // Step downwards.
  1026. data.target_block.Y--;
  1027. }
  1028. }
  1029. // --- STEP 2: Get nodes from borders to unlight
  1030. v3s16 blockpos;
  1031. v3s16 relpos;
  1032. // In case there are unloaded holes in the voxel manipulator
  1033. // unlight each block.
  1034. // For each block:
  1035. for (blockpos.X = minblock.X; blockpos.X <= maxblock.X; blockpos.X++)
  1036. for (blockpos.Y = minblock.Y; blockpos.Y <= maxblock.Y; blockpos.Y++)
  1037. for (blockpos.Z = minblock.Z; blockpos.Z <= maxblock.Z; blockpos.Z++) {
  1038. MapBlock *block = map->getBlockNoCreateNoEx(blockpos);
  1039. if (!block || block->isDummy())
  1040. // Skip not existing blocks.
  1041. continue;
  1042. v3s16 offset = block->getPosRelative();
  1043. // For each border of the block:
  1044. for (const VoxelArea &a : block_pad) {
  1045. // For each node of the border:
  1046. for (relpos.X = a.MinEdge.X; relpos.X <= a.MaxEdge.X; relpos.X++)
  1047. for (relpos.Z = a.MinEdge.Z; relpos.Z <= a.MaxEdge.Z; relpos.Z++)
  1048. for (relpos.Y = a.MinEdge.Y; relpos.Y <= a.MaxEdge.Y; relpos.Y++) {
  1049. // Get old and new node
  1050. MapNode oldnode = block->getNodeNoCheck(relpos, &is_valid);
  1051. const ContentFeatures &oldf = ndef->get(oldnode);
  1052. MapNode newnode = vm->getNodeNoExNoEmerge(relpos + offset);
  1053. const ContentFeatures &newf = oldnode == newnode ? oldf :
  1054. ndef->get(newnode);
  1055. // For each light bank
  1056. for (size_t b = 0; b < 2; b++) {
  1057. LightBank bank = banks[b];
  1058. u8 oldlight = oldf.param_type == CPT_LIGHT ?
  1059. oldnode.getLightNoChecks(bank, &oldf):
  1060. LIGHT_SUN; // no light information, force unlighting
  1061. u8 newlight = newf.param_type == CPT_LIGHT ?
  1062. newnode.getLightNoChecks(bank, &newf):
  1063. newf.light_source;
  1064. // If the new node is dimmer, unlight.
  1065. if (oldlight > newlight) {
  1066. unlight[b].push(
  1067. oldlight, relpos, blockpos, block, 6);
  1068. }
  1069. } // end of banks
  1070. } // end of nodes
  1071. } // end of borders
  1072. } // end of blocks
  1073. // --- STEP 3: All information extracted, overwrite
  1074. vm->blitBackAll(modified_blocks, true);
  1075. // --- STEP 4: Finish light update
  1076. finish_bulk_light_update(map, minblock, maxblock, unlight, relight,
  1077. modified_blocks);
  1078. }
  1079. /*!
  1080. * Resets the lighting of the given map block to
  1081. * complete darkness and full sunlight.
  1082. *
  1083. * \param light incoming sunlight, light[x][z] is true if there
  1084. * is sunlight above the map block at the given x-z coordinates.
  1085. * The array's indices are relative node coordinates in the block.
  1086. * After the procedure returns, this contains outgoing light at
  1087. * the bottom of the map block.
  1088. */
  1089. void fill_with_sunlight(MapBlock *block, const NodeDefManager *ndef,
  1090. bool light[MAP_BLOCKSIZE][MAP_BLOCKSIZE])
  1091. {
  1092. if (block->isDummy())
  1093. return;
  1094. // dummy boolean
  1095. bool is_valid;
  1096. // For each column of nodes:
  1097. for (s16 z = 0; z < MAP_BLOCKSIZE; z++)
  1098. for (s16 x = 0; x < MAP_BLOCKSIZE; x++) {
  1099. // True if the current node has sunlight.
  1100. bool lig = light[z][x];
  1101. // For each node, downwards:
  1102. for (s16 y = MAP_BLOCKSIZE - 1; y >= 0; y--) {
  1103. MapNode n = block->getNodeNoCheck(x, y, z, &is_valid);
  1104. // Ignore IGNORE nodes, these are not generated yet.
  1105. if (n.getContent() == CONTENT_IGNORE)
  1106. continue;
  1107. const ContentFeatures &f = ndef->get(n.getContent());
  1108. if (lig && !f.sunlight_propagates) {
  1109. // Sunlight is stopped.
  1110. lig = false;
  1111. }
  1112. // Reset light
  1113. n.setLight(LIGHTBANK_DAY, lig ? 15 : 0, f);
  1114. n.setLight(LIGHTBANK_NIGHT, 0, f);
  1115. block->setNodeNoCheck(x, y, z, n);
  1116. }
  1117. // Output outgoing light.
  1118. light[z][x] = lig;
  1119. }
  1120. }
  1121. void repair_block_light(ServerMap *map, MapBlock *block,
  1122. std::map<v3s16, MapBlock*> *modified_blocks)
  1123. {
  1124. if (!block || block->isDummy())
  1125. return;
  1126. const NodeDefManager *ndef = map->getNodeDefManager();
  1127. // First queue is for day light, second is for night light.
  1128. UnlightQueue unlight[] = { UnlightQueue(256), UnlightQueue(256) };
  1129. ReLightQueue relight[] = { ReLightQueue(256), ReLightQueue(256) };
  1130. // Will hold sunlight data.
  1131. bool lights[MAP_BLOCKSIZE][MAP_BLOCKSIZE];
  1132. SunlightPropagationData data;
  1133. // Dummy boolean.
  1134. bool is_valid;
  1135. // --- STEP 1: reset everything to sunlight
  1136. mapblock_v3 blockpos = block->getPos();
  1137. (*modified_blocks)[blockpos] = block;
  1138. // For each map block:
  1139. // Extract sunlight above.
  1140. is_sunlight_above_block(map, blockpos, ndef, lights);
  1141. // Reset the voxel manipulator.
  1142. fill_with_sunlight(block, ndef, lights);
  1143. // Copy sunlight data
  1144. data.target_block = v3s16(blockpos.X, blockpos.Y - 1, blockpos.Z);
  1145. for (s16 z = 0; z < MAP_BLOCKSIZE; z++)
  1146. for (s16 x = 0; x < MAP_BLOCKSIZE; x++) {
  1147. data.data.emplace_back(v2s16(x, z), lights[z][x]);
  1148. }
  1149. // Propagate sunlight and shadow below the voxel manipulator.
  1150. while (!data.data.empty()) {
  1151. if (propagate_block_sunlight(map, ndef, &data, &unlight[0],
  1152. &relight[0]))
  1153. (*modified_blocks)[data.target_block] =
  1154. map->getBlockNoCreateNoEx(data.target_block);
  1155. // Step downwards.
  1156. data.target_block.Y--;
  1157. }
  1158. // --- STEP 2: Get nodes from borders to unlight
  1159. // For each border of the block:
  1160. for (const VoxelArea &a : block_pad) {
  1161. v3s16 relpos;
  1162. // For each node of the border:
  1163. for (relpos.X = a.MinEdge.X; relpos.X <= a.MaxEdge.X; relpos.X++)
  1164. for (relpos.Z = a.MinEdge.Z; relpos.Z <= a.MaxEdge.Z; relpos.Z++)
  1165. for (relpos.Y = a.MinEdge.Y; relpos.Y <= a.MaxEdge.Y; relpos.Y++) {
  1166. // Get node
  1167. MapNode node = block->getNodeNoCheck(relpos, &is_valid);
  1168. const ContentFeatures &f = ndef->get(node);
  1169. // For each light bank
  1170. for (size_t b = 0; b < 2; b++) {
  1171. LightBank bank = banks[b];
  1172. u8 light = f.param_type == CPT_LIGHT ?
  1173. node.getLightNoChecks(bank, &f):
  1174. f.light_source;
  1175. // If the new node is dimmer than sunlight, unlight.
  1176. // (if it has maximal light, it is pointless to remove
  1177. // surrounding light, as it can only become brighter)
  1178. if (LIGHT_SUN > light) {
  1179. unlight[b].push(
  1180. LIGHT_SUN, relpos, blockpos, block, 6);
  1181. }
  1182. } // end of banks
  1183. } // end of nodes
  1184. } // end of borders
  1185. // STEP 3: Remove and spread light
  1186. finish_bulk_light_update(map, blockpos, blockpos, unlight, relight,
  1187. modified_blocks);
  1188. }
  1189. VoxelLineIterator::VoxelLineIterator(const v3f &start_position, const v3f &line_vector) :
  1190. m_start_position(start_position),
  1191. m_line_vector(line_vector)
  1192. {
  1193. m_current_node_pos = floatToInt(m_start_position, 1);
  1194. m_start_node_pos = m_current_node_pos;
  1195. m_last_index = getIndex(floatToInt(start_position + line_vector, 1));
  1196. if (m_line_vector.X > 0) {
  1197. m_next_intersection_multi.X = (floorf(m_start_position.X - 0.5) + 1.5
  1198. - m_start_position.X) / m_line_vector.X;
  1199. m_intersection_multi_inc.X = 1 / m_line_vector.X;
  1200. } else if (m_line_vector.X < 0) {
  1201. m_next_intersection_multi.X = (floorf(m_start_position.X - 0.5)
  1202. - m_start_position.X + 0.5) / m_line_vector.X;
  1203. m_intersection_multi_inc.X = -1 / m_line_vector.X;
  1204. m_step_directions.X = -1;
  1205. }
  1206. if (m_line_vector.Y > 0) {
  1207. m_next_intersection_multi.Y = (floorf(m_start_position.Y - 0.5) + 1.5
  1208. - m_start_position.Y) / m_line_vector.Y;
  1209. m_intersection_multi_inc.Y = 1 / m_line_vector.Y;
  1210. } else if (m_line_vector.Y < 0) {
  1211. m_next_intersection_multi.Y = (floorf(m_start_position.Y - 0.5)
  1212. - m_start_position.Y + 0.5) / m_line_vector.Y;
  1213. m_intersection_multi_inc.Y = -1 / m_line_vector.Y;
  1214. m_step_directions.Y = -1;
  1215. }
  1216. if (m_line_vector.Z > 0) {
  1217. m_next_intersection_multi.Z = (floorf(m_start_position.Z - 0.5) + 1.5
  1218. - m_start_position.Z) / m_line_vector.Z;
  1219. m_intersection_multi_inc.Z = 1 / m_line_vector.Z;
  1220. } else if (m_line_vector.Z < 0) {
  1221. m_next_intersection_multi.Z = (floorf(m_start_position.Z - 0.5)
  1222. - m_start_position.Z + 0.5) / m_line_vector.Z;
  1223. m_intersection_multi_inc.Z = -1 / m_line_vector.Z;
  1224. m_step_directions.Z = -1;
  1225. }
  1226. }
  1227. void VoxelLineIterator::next()
  1228. {
  1229. m_current_index++;
  1230. if ((m_next_intersection_multi.X < m_next_intersection_multi.Y)
  1231. && (m_next_intersection_multi.X < m_next_intersection_multi.Z)) {
  1232. m_next_intersection_multi.X += m_intersection_multi_inc.X;
  1233. m_current_node_pos.X += m_step_directions.X;
  1234. } else if ((m_next_intersection_multi.Y < m_next_intersection_multi.Z)) {
  1235. m_next_intersection_multi.Y += m_intersection_multi_inc.Y;
  1236. m_current_node_pos.Y += m_step_directions.Y;
  1237. } else {
  1238. m_next_intersection_multi.Z += m_intersection_multi_inc.Z;
  1239. m_current_node_pos.Z += m_step_directions.Z;
  1240. }
  1241. }
  1242. s16 VoxelLineIterator::getIndex(v3s16 voxel){
  1243. return
  1244. abs(voxel.X - m_start_node_pos.X) +
  1245. abs(voxel.Y - m_start_node_pos.Y) +
  1246. abs(voxel.Z - m_start_node_pos.Z);
  1247. }
  1248. } // namespace voxalgo