init.lua 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. -- The algorithm here is useful for much more than dirt management. It can also
  2. -- handle any kind of behavior for any node similarly to an ABM, *except* liquids.
  3. -- The node must be in the `dirtspread_notify` group, and be registered somewhere.
  4. -- If the node's logic changes it, make sure to use `minetest.add_node` in order
  5. -- to make the update logic cascade.
  6. if not minetest.global_exists("dirtspread") then dirtspread = {} end
  7. dirtspread.modpath = minetest.get_modpath("dirtspread")
  8. dirtspread.delay = 1.5
  9. dirtspread.index = dirtspread.index or 0
  10. dirtspread.positions = dirtspread.positions or {} -- Indexed cache table.
  11. dirtspread.blocks = dirtspread.blocks or {}
  12. -- Groups:
  13. --
  14. -- `dirt_type` (any and all dirt types in the game).
  15. -- `sterile_dirt_type` (dirt which cannot grow anything).
  16. -- `raw_dirt_type` (dirt without snow, grass, or other decoration).
  17. -- `non_sterile_dirt_type` (dirt theoretically capable of growing plants).
  18. -- `hoed_dirt_type` (dirt which has been hoed into rows).
  19. -- `dry_dirt_type` (dirt which is dry).
  20. -- `grassy_dirt_type` (dirt with grassy decoration).
  21. -- `snowy_dirt_type` (dirt with snow on top).
  22. -- `leafy_dirt_type` (dirt with leaf-litter on top).
  23. -- `non_raw_dirt_type` (any dirt with decoration, grass or otherwise).
  24. -- `permafrost_type` (all permafrost nodes).
  25. --
  26. -- Other Notable Groups:
  27. --
  28. -- `water`
  29. -- `lava`
  30. -- `snow`
  31. -- `snowy`
  32. -- `cold`
  33. -- `wet`
  34. -- `sand`
  35. -- `gravel`
  36. -- `leaves`
  37. --
  38. -- Names:
  39. --
  40. -- `default:dirt`
  41. -- `darkage:darkdirt`
  42. -- `default:dirt_with_grass`
  43. -- `default:dirt_with_grass_footsteps`
  44. -- `moregrass:darkgrass`
  45. -- `default:dirt_with_dry_grass`
  46. -- `default:dirt_with_snow`
  47. -- `default:dark_dirt_with_snow`
  48. -- `default:dry_dirt_with_snow`
  49. -- `default:dirt_with_rainforest_litter`
  50. -- `default:dark_dirt_with_rainforest_litter`
  51. -- `default:dry_dirt_with_rainforest_litter`
  52. -- `default:dirt_with_coniferous_litter`
  53. -- `default:dark_dirt_with_coniferous_litter`
  54. -- `default:dry_dirt_with_coniferous_litter`
  55. -- `default:dry_dirt`
  56. -- `default:dry_dirt_with_dry_grass`
  57. -- `farming:soil`
  58. -- `farming:soil_wet`
  59. -- `farming:desert_sand_soil`
  60. -- `farming:desert_sand_soil_wet`
  61. -- `default:permafrost`
  62. -- `default:permafrost_with_snow`
  63. -- `default:permafrost_with_stones`
  64. -- `default:permafrost_with_snow_and_stones`
  65. -- `default:permafrost_with_moss`
  66. -- `default:permafrost_with_moss_and_stones`
  67. -- `sand:sand_with_ice_crystals`
  68. -- `default:sand`
  69. -- `default:desert_sand`
  70. -- `default:gravel`
  71. -- `default:snowblock`
  72. -- Called whenever a timer on any active node expires.
  73. function dirtspread.on_timer(pos, elapsed)
  74. -- If `ignore` is nearby, we're next to an unloaded mapchunk.
  75. -- We cannot assume we'll have enough data to execute the active block function.
  76. -- We'll need to restart the timer and try again later.
  77. if utility.find_node_near_not_world_edge(pos, 1, "ignore") then
  78. return true
  79. end
  80. local node = minetest.get_node(pos)
  81. local ndef = dirtspread.get_active_block(node.name)
  82. if ndef and ndef.func then
  83. -- If the function returns `true`, restart the timer.
  84. if ndef.func(table.copy(pos), node) then
  85. local timer = minetest.get_node_timer(pos)
  86. timer:start(math.random(ndef.min_time, ndef.max_time))
  87. end
  88. end
  89. end
  90. -- Called to update nodes around the given position (possibly including self).
  91. local minp = {x=0, y=0, z=0}
  92. local maxp = {x=0, y=0, z=0}
  93. function dirtspread.on_notify_around(pos)
  94. minp.x = pos.x - 1
  95. minp.y = pos.y - 1
  96. minp.z = pos.z - 1
  97. maxp.x = pos.x + 1
  98. maxp.y = pos.y + 1
  99. maxp.z = pos.z + 1
  100. local positions = minetest.find_nodes_in_area(minp, maxp, "group:dirtspread_notify")
  101. for i=1, #positions, 1 do
  102. local p2 = positions[i]
  103. local node = minetest.get_node(p2)
  104. local ndef = dirtspread.get_active_block(node.name)
  105. if ndef then
  106. local timer = minetest.get_node_timer(p2)
  107. -- Alert: sometimes this can fail because the timer, for some reason, is
  108. -- already started and has a huge timeout value (> 659000). Very odd!
  109. -- In any case, it makes sense to reset the timer whenever something changes.
  110. --[[
  111. if not timer:is_started() then
  112. end
  113. --]]
  114. timer:start(math.random(ndef.min_time, ndef.max_time))
  115. end
  116. end
  117. end
  118. -- Called whenever a node is added or removed (any node, not just nodes around dirt!).
  119. -- Warning: may be called many times in quick succession (e.g., falling nodes).
  120. function dirtspread.on_environment(pos)
  121. -- Add position to table of positions to be updated later.
  122. local poss = dirtspread.positions
  123. local idex = dirtspread.index + 1
  124. local p = poss[idex]
  125. if p then
  126. p.x = pos.x
  127. p.y = pos.y
  128. p.z = pos.z
  129. else
  130. poss[idex] = {x=pos.x, y=pos.y, z=pos.z}
  131. end
  132. dirtspread.index = idex
  133. end
  134. -- Called periodically to update nodes.
  135. -- This is not part of the public API!
  136. function dirtspread.periodic_execute()
  137. local endx = dirtspread.index
  138. local count = 0
  139. -- Update just 50 nodes per run.
  140. -- This spreads out the updates over time.
  141. if endx > 0 then
  142. table.shuffle(dirtspread.positions, 1, endx)
  143. end
  144. while endx > 0 and count < 50 do
  145. dirtspread.on_notify_around(dirtspread.positions[endx])
  146. endx = endx - 1
  147. count = count + 1
  148. end
  149. dirtspread.index = endx
  150. minetest.after(dirtspread.delay, dirtspread.periodic_execute)
  151. end
  152. -- Execute any remaining updates on shutdown.
  153. -- This is not part of the public API!
  154. function dirtspread.on_shutdown()
  155. local endx = dirtspread.index
  156. local poss = dirtspread.positions
  157. while endx > 0 do
  158. dirtspread.on_notify_around(poss[endx])
  159. endx = endx - 1
  160. end
  161. dirtspread.index = endx
  162. end
  163. -- Obtain the data for a registered active block.
  164. function dirtspread.get_active_block(name)
  165. return dirtspread.blocks[name] -- May return nil.
  166. end
  167. function dirtspread.register_active_block(name, data)
  168. -- Update node definition. Node must already have been registered!
  169. local ndef = minetest.registered_nodes[name]
  170. assert(ndef)
  171. local newdata = {
  172. min_time = data.min_time or 1,
  173. max_time = data.max_time or 1,
  174. func = data.func,
  175. }
  176. assert(newdata.min_time >= 0)
  177. assert(newdata.max_time >= 0)
  178. assert(newdata.min_time <= newdata.max_time)
  179. dirtspread.blocks[name] = newdata
  180. -- Node must be added to the `dirtspread_notify` group.
  181. local g = table.copy(ndef.groups or {})
  182. g.dirtspread_notify = 1
  183. -- Hook `on_timer`.
  184. -- FIXME: What happens when node already has timer callback?!
  185. -- We end up overriding the original timeout value and messing things up!
  186. local on_timer
  187. if ndef.on_timer then
  188. local old = ndef.on_timer
  189. on_timer = function(pos, elapsed)
  190. -- FIXME: what happens if the dirtspread timer does NOT return true
  191. -- (e.g., it set a new random expiry time), and the original nodetimer (old)
  192. -- DOES return true (e.g., it has its own idea of when the next timeout
  193. -- should happen)? This is a logic/time conflict which might cause strange
  194. -- bugs with certain nodes if they BOTH have their own nodetimer, and are
  195. -- also registered with the dirtspread code.
  196. local b1 = dirtspread.on_timer(pos, elapsed)
  197. local b2 = old(pos, elapsed)
  198. -- If either returns true, we have to restart the timer.
  199. if b1 or b2 then
  200. return true
  201. end
  202. end
  203. else
  204. on_timer = function(pos, elapsed)
  205. -- If the dirtspread timer func returns true, we have to restart the timer.
  206. if dirtspread.on_timer(pos, elapsed) then
  207. return true
  208. end
  209. end
  210. end
  211. -- TNT uses voxelmanip, need to hook the `on_blast` method.
  212. -- Update 12/10/23: Why do we do this? Can't remember now ...
  213. -- Disabling this causes a HUGE performance improvement when blasting
  214. -- dirt/sand.
  215. --
  216. -- I still can't remember why I hooked this function in the first place.
  217. -- All it did was add the position to the update queue, but since the node
  218. -- would most likely fall, the position would be out-of-date anyway!
  219. --[[
  220. local on_blast
  221. if ndef.on_blast then
  222. local old = ndef.on_blast
  223. on_blast = function(pos, intensity)
  224. dirtspread.on_environment(pos)
  225. return old(pos, intensity)
  226. end
  227. else
  228. on_blast = function(pos, intensity)
  229. dirtspread.on_environment(pos)
  230. end
  231. end
  232. --]]
  233. minetest.override_item(name, {
  234. groups = g,
  235. on_timer = on_timer,
  236. --on_blast = on_blast,
  237. })
  238. end
  239. -- File is reloadable.
  240. dofile(dirtspread.modpath .. "/registrations.lua")
  241. if not dirtspread.registered then
  242. -- Hook `minetest.remove_node`. This is called only when player removes a node, not for mods!
  243. local remove_node_copy = minetest.remove_node
  244. function minetest.remove_node(pos)
  245. local res = remove_node_copy(pos)
  246. dirtspread.on_environment(pos)
  247. return res
  248. end
  249. minetest.after(dirtspread.delay, dirtspread.periodic_execute)
  250. minetest.register_on_shutdown(function() dirtspread.on_shutdown() end)
  251. local c = "dirtspread:core"
  252. local f = dirtspread.modpath .. "/init.lua"
  253. reload.register_file(c, f, false)
  254. dirtspread.registered = true
  255. end