voxelalgorithms.cpp 45 KB

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