init.lua 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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. dirtspread = dirtspread or {}
  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. --minetest.chat_send_player("MustTest", "On timer: " .. minetest.pos_to_string(pos))
  75. -- If `ignore` is nearby, we're next to an unloaded mapchunk.
  76. -- We cannot assume we'll have enough data to execute the active block function.
  77. -- We'll need to restart the timer and try again later.
  78. if utility.find_node_near_not_world_edge(pos, 1, "ignore") then
  79. return true
  80. end
  81. local node = minetest.get_node(pos)
  82. local ndef = dirtspread.get_active_block(node.name)
  83. if ndef and ndef.func then
  84. -- If the function returns `true`, restart the timer.
  85. if ndef.func(table.copy(pos), node) then
  86. local timer = minetest.get_node_timer(pos)
  87. timer:start(ndef.min_time, ndef.max_time)
  88. end
  89. end
  90. end
  91. -- Called to update nodes around the given position (possibly including self).
  92. local minp = {x=0, y=0, z=0}
  93. local maxp = {x=0, y=0, z=0}
  94. function dirtspread.on_notify_around(pos)
  95. --minetest.chat_send_player("MustTest", "Notify: " .. minetest.pos_to_string(pos))
  96. minp.x = pos.x - 1
  97. minp.y = pos.y - 1
  98. minp.z = pos.z - 1
  99. maxp.x = pos.x + 1
  100. maxp.y = pos.y + 1
  101. maxp.z = pos.z + 1
  102. local positions = minetest.find_nodes_in_area(minp, maxp, "group:dirtspread_notify")
  103. --minetest.chat_send_player("MustTest", "Counts: " .. #positions)
  104. for i=1, #positions, 1 do
  105. local p2 = positions[i]
  106. local node = minetest.get_node(p2)
  107. local ndef = dirtspread.get_active_block(node.name)
  108. if ndef then
  109. --minetest.chat_send_player("MustTest", "Got nodedef: " .. minetest.pos_to_string(p2))
  110. local timer = minetest.get_node_timer(p2)
  111. -- Alert: sometimes this can fail because the timer, for some reason, is
  112. -- already started and has a huge timeout value (> 659000). Very odd!
  113. -- In any case, it makes sense to reset the timer whenever something changes.
  114. --[[
  115. if not timer:is_started() then
  116. end
  117. --]]
  118. --minetest.chat_send_player("MustTest", "Started timer: " .. minetest.pos_to_string(p2))
  119. timer:start(ndef.min_time, ndef.max_time)
  120. end
  121. end
  122. end
  123. -- Called whenever a node is added or removed (any node, not just nodes around dirt!).
  124. -- Warning: may be called many times in quick succession (e.g., falling nodes).
  125. function dirtspread.on_environment(pos)
  126. --minetest.chat_send_player("MustTest", "Environment: " .. minetest.pos_to_string(pos))
  127. -- Add position to table of positions to be updated later.
  128. local poss = dirtspread.positions
  129. local idex = dirtspread.index + 1
  130. local p = poss[idex]
  131. if p then
  132. p.x = pos.x
  133. p.y = pos.y
  134. p.z = pos.z
  135. else
  136. poss[idex] = {x=pos.x, y=pos.y, z=pos.z}
  137. end
  138. dirtspread.index = idex
  139. end
  140. -- Called periodically to update nodes.
  141. -- This is not part of the public API!
  142. function dirtspread.periodic_execute()
  143. local endx = dirtspread.index
  144. local count = 0
  145. -- Update just 50 nodes per run.
  146. -- This spreads out the updates over time.
  147. if endx > 0 then
  148. table.shuffle(dirtspread.positions, 1, endx)
  149. end
  150. while endx > 0 and count < 50 do
  151. dirtspread.on_notify_around(dirtspread.positions[endx])
  152. endx = endx - 1
  153. count = count + 1
  154. end
  155. dirtspread.index = endx
  156. minetest.after(dirtspread.delay, dirtspread.periodic_execute)
  157. end
  158. -- Execute any remaining updates on shutdown.
  159. -- This is not part of the public API!
  160. function dirtspread.on_shutdown()
  161. local endx = dirtspread.index
  162. local poss = dirtspread.positions
  163. while endx > 0 do
  164. dirtspread.on_notify_around(poss[endx])
  165. endx = endx - 1
  166. end
  167. dirtspread.index = endx
  168. end
  169. -- Obtain the data for a registered active block.
  170. function dirtspread.get_active_block(name)
  171. return dirtspread.blocks[name] -- May return nil.
  172. end
  173. function dirtspread.register_active_block(name, data)
  174. -- Update node definition. Node must already have been registered!
  175. local ndef = minetest.registered_nodes[name]
  176. assert(ndef)
  177. local newdata = {
  178. min_time = data.min_time or 1,
  179. max_time = data.max_time or 1,
  180. func = data.func,
  181. }
  182. assert(newdata.min_time >= 0)
  183. assert(newdata.max_time >= 0)
  184. assert(newdata.min_time <= newdata.max_time)
  185. dirtspread.blocks[name] = newdata
  186. -- Node must be added to the `dirtspread_notify` group.
  187. local g = table.copy(ndef.groups or {})
  188. g.dirtspread_notify = 1
  189. -- Hook `on_timer`.
  190. -- FIXME: What happens when node already has timer callback?!
  191. -- We end up overriding the original timeout value and messing things up!
  192. local on_timer
  193. if ndef.on_timer then
  194. local old = ndef.on_timer
  195. on_timer = function(pos, elapsed)
  196. dirtspread.on_timer(pos, elapsed)
  197. return old(pos, elapsed)
  198. end
  199. else
  200. on_timer = function(pos, elapsed)
  201. dirtspread.on_timer(pos, elapsed)
  202. end
  203. end
  204. -- TNT uses voxelmanip, need to hook the `on_blast` method.
  205. local on_blast
  206. if ndef.on_blast then
  207. local old = ndef.on_blast
  208. on_blast = function(pos, intensity)
  209. dirtspread.on_environment(pos)
  210. return old(pos, intensity)
  211. end
  212. else
  213. on_blast = function(pos, intensity)
  214. dirtspread.on_environment(pos)
  215. end
  216. end
  217. minetest.override_item(name, {
  218. groups = g,
  219. on_timer = on_timer,
  220. on_blast = on_blast,
  221. })
  222. end
  223. -- File is reloadable.
  224. dofile(dirtspread.modpath .. "/registrations.lua")
  225. if not dirtspread.registered then
  226. -- Hook `minetest.remove_node`. This is called only when player removes a node, not for mods!
  227. local remove_node_copy = minetest.remove_node
  228. function minetest.remove_node(pos)
  229. local res = remove_node_copy(pos)
  230. dirtspread.on_environment(pos)
  231. return res
  232. end
  233. minetest.after(dirtspread.delay, dirtspread.periodic_execute)
  234. minetest.register_on_shutdown(function() dirtspread.on_shutdown() end)
  235. local c = "dirtspread:core"
  236. local f = dirtspread.modpath .. "/init.lua"
  237. reload.register_file(c, f, false)
  238. dirtspread.registered = true
  239. end