functions.lua 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836
  1. --
  2. -- Sounds
  3. --
  4. function default.node_sound_defaults(tbl)
  5. tbl = tbl or {}
  6. tbl.footstep = tbl.footstep or
  7. {name = "", gain = 1.0}
  8. tbl.dug = tbl.dug or
  9. {name = "default_dug_node", gain = 0.25}
  10. tbl.place = tbl.place or
  11. {name = "default_place_node_hard", gain = 1.0}
  12. return tbl
  13. end
  14. function default.node_sound_stone_defaults(tbl)
  15. tbl = tbl or {}
  16. tbl.footstep = tbl.footstep or
  17. {name = "default_hard_footstep", gain = 0.2}
  18. tbl.dug = tbl.dug or
  19. {name = "default_hard_footstep", gain = 1.0}
  20. default.node_sound_defaults(tbl)
  21. return tbl
  22. end
  23. function default.node_sound_dirt_defaults(tbl)
  24. tbl = tbl or {}
  25. tbl.footstep = tbl.footstep or
  26. {name = "default_dirt_footstep", gain = 0.25}
  27. tbl.dig = tbl.dig or
  28. {name = "default_dig_crumbly", gain = 0.4}
  29. tbl.dug = tbl.dug or
  30. {name = "default_dirt_footstep", gain = 1.0}
  31. tbl.place = tbl.place or
  32. {name = "default_place_node", gain = 1.0}
  33. default.node_sound_defaults(tbl)
  34. return tbl
  35. end
  36. function default.node_sound_sand_defaults(tbl)
  37. tbl = tbl or {}
  38. tbl.footstep = tbl.footstep or
  39. {name = "default_sand_footstep", gain = 0.05}
  40. tbl.dug = tbl.dug or
  41. {name = "default_sand_footstep", gain = 0.15}
  42. tbl.place = tbl.place or
  43. {name = "default_place_node", gain = 1.0}
  44. default.node_sound_defaults(tbl)
  45. return tbl
  46. end
  47. function default.node_sound_gravel_defaults(tbl)
  48. tbl = tbl or {}
  49. tbl.footstep = tbl.footstep or
  50. {name = "default_gravel_footstep", gain = 0.25}
  51. tbl.dig = tbl.dig or
  52. {name = "default_gravel_dig", gain = 0.35}
  53. tbl.dug = tbl.dug or
  54. {name = "default_gravel_dug", gain = 1.0}
  55. tbl.place = tbl.place or
  56. {name = "default_place_node", gain = 1.0}
  57. default.node_sound_defaults(tbl)
  58. return tbl
  59. end
  60. function default.node_sound_wood_defaults(tbl)
  61. tbl = tbl or {}
  62. tbl.footstep = tbl.footstep or
  63. {name = "default_wood_footstep", gain = 0.15}
  64. tbl.dig = tbl.dig or
  65. {name = "default_dig_choppy", gain = 0.4}
  66. tbl.dug = tbl.dug or
  67. {name = "default_wood_footstep", gain = 1.0}
  68. default.node_sound_defaults(tbl)
  69. return tbl
  70. end
  71. function default.node_sound_leaves_defaults(tbl)
  72. tbl = tbl or {}
  73. tbl.footstep = tbl.footstep or
  74. {name = "default_grass_footstep", gain = 0.45}
  75. tbl.dug = tbl.dug or
  76. {name = "default_grass_footstep", gain = 0.7}
  77. tbl.place = tbl.place or
  78. {name = "default_place_node", gain = 1.0}
  79. default.node_sound_defaults(tbl)
  80. return tbl
  81. end
  82. function default.node_sound_glass_defaults(tbl)
  83. tbl = tbl or {}
  84. tbl.footstep = tbl.footstep or
  85. {name = "default_glass_footstep", gain = 0.3}
  86. tbl.dig = tbl.dig or
  87. {name = "default_glass_footstep", gain = 0.5}
  88. tbl.dug = tbl.dug or
  89. {name = "default_break_glass", gain = 1.0}
  90. default.node_sound_defaults(tbl)
  91. return tbl
  92. end
  93. function default.node_sound_ice_defaults(tbl)
  94. tbl = tbl or {}
  95. tbl.footstep = tbl.footstep or
  96. {name = "default_ice_footstep", gain = 0.15}
  97. tbl.dig = tbl.dig or
  98. {name = "default_ice_dig", gain = 0.5}
  99. tbl.dug = tbl.dug or
  100. {name = "default_ice_dug", gain = 0.5}
  101. default.node_sound_defaults(tbl)
  102. return tbl
  103. end
  104. function default.node_sound_metal_defaults(tbl)
  105. tbl = tbl or {}
  106. tbl.footstep = tbl.footstep or
  107. {name = "default_metal_footstep", gain = 0.2}
  108. tbl.dig = tbl.dig or
  109. {name = "default_dig_metal", gain = 0.5}
  110. tbl.dug = tbl.dug or
  111. {name = "default_dug_metal", gain = 0.5}
  112. tbl.place = tbl.place or
  113. {name = "default_place_node_metal", gain = 0.5}
  114. default.node_sound_defaults(tbl)
  115. return tbl
  116. end
  117. function default.node_sound_water_defaults(tbl)
  118. tbl = tbl or {}
  119. tbl.footstep = tbl.footstep or
  120. {name = "default_water_footstep", gain = 0.2}
  121. default.node_sound_defaults(tbl)
  122. return tbl
  123. end
  124. function default.node_sound_snow_defaults(tbl)
  125. tbl = tbl or {}
  126. tbl.footstep = tbl.footstep or
  127. {name = "default_snow_footstep", gain = 0.2}
  128. tbl.dig = tbl.dig or
  129. {name = "default_snow_footstep", gain = 0.3}
  130. tbl.dug = tbl.dug or
  131. {name = "default_snow_footstep", gain = 0.3}
  132. tbl.place = tbl.place or
  133. {name = "default_place_node", gain = 1.0}
  134. default.node_sound_defaults(tbl)
  135. return tbl
  136. end
  137. --
  138. -- Lavacooling
  139. --
  140. default.cool_lava = function(pos, node)
  141. if node.name == "default:lava_source" then
  142. minetest.set_node(pos, {name = "default:obsidian"})
  143. else -- Lava flowing
  144. minetest.set_node(pos, {name = "default:stone"})
  145. end
  146. minetest.sound_play("default_cool_lava",
  147. {pos = pos, max_hear_distance = 16, gain = 0.2}, true)
  148. end
  149. if minetest.settings:get_bool("enable_lavacooling") ~= false then
  150. minetest.register_abm({
  151. label = "Lava cooling",
  152. nodenames = {"default:lava_source", "default:lava_flowing"},
  153. neighbors = {"group:cools_lava", "group:water"},
  154. interval = 2,
  155. chance = 2,
  156. catch_up = false,
  157. action = function(...)
  158. default.cool_lava(...)
  159. end,
  160. })
  161. end
  162. --
  163. -- Optimized helper to put all items in an inventory into a drops list
  164. --
  165. function default.get_inventory_drops(pos, inventory, drops)
  166. local inv = minetest.get_meta(pos):get_inventory()
  167. local n = #drops
  168. for i = 1, inv:get_size(inventory) do
  169. local stack = inv:get_stack(inventory, i)
  170. if stack:get_count() > 0 then
  171. drops[n+1] = stack:to_table()
  172. n = n + 1
  173. end
  174. end
  175. end
  176. --
  177. -- Papyrus and cactus growing
  178. --
  179. -- Wrapping the functions in ABM action is necessary to make overriding them possible
  180. function default.grow_cactus(pos, node)
  181. if node.param2 >= 4 then
  182. return
  183. end
  184. pos.y = pos.y - 1
  185. if minetest.get_item_group(minetest.get_node(pos).name, "sand") == 0 then
  186. return
  187. end
  188. pos.y = pos.y + 1
  189. local height = 0
  190. while node.name == "default:cactus" and height < 4 do
  191. height = height + 1
  192. pos.y = pos.y + 1
  193. node = minetest.get_node(pos)
  194. end
  195. if height == 4 or node.name ~= "air" then
  196. return
  197. end
  198. if minetest.get_node_light(pos) < 13 then
  199. return
  200. end
  201. minetest.set_node(pos, {name = "default:cactus"})
  202. return true
  203. end
  204. function default.grow_papyrus(pos, node)
  205. pos.y = pos.y - 1
  206. local name = minetest.get_node(pos).name
  207. if name ~= "default:dirt" and
  208. name ~= "default:dirt_with_grass" and
  209. name ~= "default:dirt_with_dry_grass" and
  210. name ~= "default:dirt_with_rainforest_litter" and
  211. name ~= "default:dry_dirt" and
  212. name ~= "default:dry_dirt_with_dry_grass" then
  213. return
  214. end
  215. if not minetest.find_node_near(pos, 3, {"group:water"}) then
  216. return
  217. end
  218. pos.y = pos.y + 1
  219. local height = 0
  220. while node.name == "default:papyrus" and height < 4 do
  221. height = height + 1
  222. pos.y = pos.y + 1
  223. node = minetest.get_node(pos)
  224. end
  225. if height == 4 or node.name ~= "air" then
  226. return
  227. end
  228. if minetest.get_node_light(pos) < 13 then
  229. return
  230. end
  231. minetest.set_node(pos, {name = "default:papyrus"})
  232. return true
  233. end
  234. minetest.register_abm({
  235. label = "Grow cactus",
  236. nodenames = {"default:cactus"},
  237. neighbors = {"group:sand"},
  238. interval = 12,
  239. chance = 83,
  240. action = function(...)
  241. default.grow_cactus(...)
  242. end
  243. })
  244. minetest.register_abm({
  245. label = "Grow papyrus",
  246. nodenames = {"default:papyrus"},
  247. -- Grows on the dirt and surface dirt nodes of the biomes papyrus appears in,
  248. -- including the old savanna nodes.
  249. -- 'default:dirt_with_grass' is here only because it was allowed before.
  250. neighbors = {
  251. "default:dirt",
  252. "default:dirt_with_grass",
  253. "default:dirt_with_dry_grass",
  254. "default:dirt_with_rainforest_litter",
  255. "default:dry_dirt",
  256. "default:dry_dirt_with_dry_grass",
  257. },
  258. interval = 14,
  259. chance = 71,
  260. action = function(...)
  261. default.grow_papyrus(...)
  262. end
  263. })
  264. --
  265. -- Dig upwards
  266. --
  267. local in_dig_up = false
  268. function default.dig_up(pos, node, digger, max_height)
  269. if in_dig_up then return end -- Do not recurse
  270. if digger == nil then return end
  271. max_height = max_height or 100
  272. in_dig_up = true
  273. for y = 1, max_height do
  274. local up_pos = vector.offset(pos, 0, y, 0)
  275. local up_node = minetest.get_node(up_pos)
  276. if up_node.name ~= node.name then
  277. break
  278. end
  279. if not minetest.node_dig(up_pos, up_node, digger) then
  280. break
  281. end
  282. end
  283. in_dig_up = false
  284. end
  285. -- errors are hard to handle, instead we rely on resetting this value the next step
  286. minetest.register_globalstep(function()
  287. in_dig_up = false
  288. end)
  289. --
  290. -- Fence registration helper
  291. --
  292. local fence_collision_extra = minetest.settings:get_bool("enable_fence_tall") and 3/8 or 0
  293. function default.register_fence(name, def)
  294. local fence_texture = "default_fence_overlay.png^" .. def.texture ..
  295. "^default_fence_overlay.png^[makealpha:255,126,126"
  296. -- Allow almost everything to be overridden
  297. local default_fields = {
  298. paramtype = "light",
  299. drawtype = "nodebox",
  300. node_box = {
  301. type = "connected",
  302. fixed = {-1/8, -1/2, -1/8, 1/8, 1/2, 1/8},
  303. -- connect_top =
  304. -- connect_bottom =
  305. connect_front = {{-1/16, 3/16, -1/2, 1/16, 5/16, -1/8 },
  306. {-1/16, -5/16, -1/2, 1/16, -3/16, -1/8 }},
  307. connect_left = {{-1/2, 3/16, -1/16, -1/8, 5/16, 1/16},
  308. {-1/2, -5/16, -1/16, -1/8, -3/16, 1/16}},
  309. connect_back = {{-1/16, 3/16, 1/8, 1/16, 5/16, 1/2 },
  310. {-1/16, -5/16, 1/8, 1/16, -3/16, 1/2 }},
  311. connect_right = {{ 1/8, 3/16, -1/16, 1/2, 5/16, 1/16},
  312. { 1/8, -5/16, -1/16, 1/2, -3/16, 1/16}}
  313. },
  314. collision_box = {
  315. type = "connected",
  316. fixed = {-1/8, -1/2, -1/8, 1/8, 1/2 + fence_collision_extra, 1/8},
  317. -- connect_top =
  318. -- connect_bottom =
  319. connect_front = {-1/8, -1/2, -1/2, 1/8, 1/2 + fence_collision_extra, -1/8},
  320. connect_left = {-1/2, -1/2, -1/8, -1/8, 1/2 + fence_collision_extra, 1/8},
  321. connect_back = {-1/8, -1/2, 1/8, 1/8, 1/2 + fence_collision_extra, 1/2},
  322. connect_right = { 1/8, -1/2, -1/8, 1/2, 1/2 + fence_collision_extra, 1/8}
  323. },
  324. connects_to = {"group:fence", "group:wood", "group:tree", "group:wall"},
  325. inventory_image = fence_texture,
  326. wield_image = fence_texture,
  327. tiles = {def.texture},
  328. sunlight_propagates = true,
  329. is_ground_content = false,
  330. groups = {},
  331. }
  332. for k, v in pairs(default_fields) do
  333. if def[k] == nil then
  334. def[k] = v
  335. end
  336. end
  337. -- Always add to the fence group, even if no group provided
  338. def.groups.fence = 1
  339. local material = def.material
  340. def.texture = nil
  341. def.material = nil
  342. minetest.register_node(name, def)
  343. -- Register crafting recipe, trim away starting colon if any
  344. if not material then return end
  345. name = string.gsub(name, "^:", "")
  346. minetest.register_craft({
  347. output = name .. " 4",
  348. recipe = {
  349. { material, 'group:stick', material },
  350. { material, 'group:stick', material },
  351. }
  352. })
  353. end
  354. --
  355. -- Fence rail registration helper
  356. --
  357. function default.register_fence_rail(name, def)
  358. local fence_rail_texture = "default_fence_rail_overlay.png^" .. def.texture ..
  359. "^default_fence_rail_overlay.png^[makealpha:255,126,126"
  360. -- Allow almost everything to be overridden
  361. local default_fields = {
  362. paramtype = "light",
  363. drawtype = "nodebox",
  364. node_box = {
  365. type = "connected",
  366. fixed = {{-1/16, 3/16, -1/16, 1/16, 5/16, 1/16},
  367. {-1/16, -3/16, -1/16, 1/16, -5/16, 1/16}},
  368. -- connect_top =
  369. -- connect_bottom =
  370. connect_front = {{-1/16, 3/16, -1/2, 1/16, 5/16, -1/16},
  371. {-1/16, -5/16, -1/2, 1/16, -3/16, -1/16}},
  372. connect_left = {{-1/2, 3/16, -1/16, -1/16, 5/16, 1/16},
  373. {-1/2, -5/16, -1/16, -1/16, -3/16, 1/16}},
  374. connect_back = {{-1/16, 3/16, 1/16, 1/16, 5/16, 1/2 },
  375. {-1/16, -5/16, 1/16, 1/16, -3/16, 1/2 }},
  376. connect_right = {{ 1/16, 3/16, -1/16, 1/2, 5/16, 1/16},
  377. { 1/16, -5/16, -1/16, 1/2, -3/16, 1/16}}
  378. },
  379. collision_box = {
  380. type = "connected",
  381. fixed = {-1/8, -1/2, -1/8, 1/8, 1/2 + fence_collision_extra, 1/8},
  382. -- connect_top =
  383. -- connect_bottom =
  384. connect_front = {-1/8, -1/2, -1/2, 1/8, 1/2 + fence_collision_extra, -1/8},
  385. connect_left = {-1/2, -1/2, -1/8, -1/8, 1/2 + fence_collision_extra, 1/8},
  386. connect_back = {-1/8, -1/2, 1/8, 1/8, 1/2 + fence_collision_extra, 1/2},
  387. connect_right = { 1/8, -1/2, -1/8, 1/2, 1/2 + fence_collision_extra, 1/8}
  388. },
  389. connects_to = {"group:fence", "group:wall"},
  390. inventory_image = fence_rail_texture,
  391. wield_image = fence_rail_texture,
  392. tiles = {def.texture},
  393. sunlight_propagates = true,
  394. is_ground_content = false,
  395. groups = {},
  396. }
  397. for k, v in pairs(default_fields) do
  398. if def[k] == nil then
  399. def[k] = v
  400. end
  401. end
  402. -- Always add to the fence group, even if no group provided
  403. def.groups.fence = 1
  404. local material = def.material
  405. def.texture = nil
  406. def.material = nil
  407. minetest.register_node(name, def)
  408. -- Register crafting recipe, trim away starting colon if any
  409. if not material then return end
  410. name = string.gsub(name, "^:", "")
  411. minetest.register_craft({
  412. output = name .. " 16",
  413. recipe = {
  414. { material, material },
  415. { "", ""},
  416. { material, material },
  417. }
  418. })
  419. end
  420. --
  421. -- Mese post registration helper
  422. --
  423. function default.register_mesepost(name, def)
  424. local post_texture = def.texture .. "^default_mese_post_light_side.png^[makealpha:0,0,0"
  425. local post_texture_dark = def.texture .. "^default_mese_post_light_side_dark.png^[makealpha:0,0,0"
  426. -- Allow almost everything to be overridden
  427. local default_fields = {
  428. wield_image = post_texture,
  429. drawtype = "nodebox",
  430. node_box = {
  431. type = "fixed",
  432. fixed = {
  433. {-2 / 16, -8 / 16, -2 / 16, 2 / 16, 8 / 16, 2 / 16},
  434. },
  435. },
  436. paramtype = "light",
  437. tiles = {def.texture, def.texture, post_texture_dark, post_texture_dark, post_texture, post_texture},
  438. use_texture_alpha = "opaque",
  439. light_source = default.LIGHT_MAX,
  440. sunlight_propagates = true,
  441. is_ground_content = false,
  442. groups = {choppy = 2, oddly_breakable_by_hand = 2, flammable = 2},
  443. sounds = default.node_sound_wood_defaults(),
  444. }
  445. for k, v in pairs(default_fields) do
  446. if def[k] == nil then
  447. def[k] = v
  448. end
  449. end
  450. local material = def.material
  451. def.texture = nil
  452. def.material = nil
  453. minetest.register_node(name, def)
  454. -- Register crafting recipe, trim away starting colon if any
  455. if not material then return end
  456. name = string.gsub(name, "^:", "")
  457. minetest.register_craft({
  458. output = name .. " 4",
  459. recipe = {
  460. {'', 'default:glass', ''},
  461. {'default:mese_crystal', 'default:mese_crystal', 'default:mese_crystal'},
  462. {'', material, ''},
  463. }
  464. })
  465. end
  466. --
  467. -- Leafdecay
  468. --
  469. -- Prevent decay of placed leaves
  470. default.after_place_leaves = function(pos, placer, itemstack, pointed_thing)
  471. if placer and placer:is_player() then
  472. local node = minetest.get_node(pos)
  473. node.param2 = 1
  474. minetest.set_node(pos, node)
  475. end
  476. end
  477. -- Leafdecay
  478. local function leafdecay_after_destruct(pos, oldnode, def)
  479. for _, v in pairs(minetest.find_nodes_in_area(vector.subtract(pos, def.radius),
  480. vector.add(pos, def.radius), def.leaves)) do
  481. local node = minetest.get_node(v)
  482. local timer = minetest.get_node_timer(v)
  483. if node.param2 ~= 1 and not timer:is_started() then
  484. timer:start(math.random(20, 120) / 10)
  485. end
  486. end
  487. end
  488. local movement_gravity = tonumber(
  489. minetest.settings:get("movement_gravity")) or 9.81
  490. local function leafdecay_on_timer(pos, def)
  491. if minetest.find_node_near(pos, def.radius, def.trunks) then
  492. return false
  493. end
  494. local node = minetest.get_node(pos)
  495. local drops = minetest.get_node_drops(node.name)
  496. for _, item in ipairs(drops) do
  497. local is_leaf
  498. for _, v in pairs(def.leaves) do
  499. if v == item then
  500. is_leaf = true
  501. end
  502. end
  503. if minetest.get_item_group(item, "leafdecay_drop") ~= 0 or
  504. not is_leaf then
  505. minetest.add_item({
  506. x = pos.x - 0.5 + math.random(),
  507. y = pos.y - 0.5 + math.random(),
  508. z = pos.z - 0.5 + math.random(),
  509. }, item)
  510. end
  511. end
  512. minetest.remove_node(pos)
  513. minetest.check_for_falling(pos)
  514. -- spawn a few particles for the removed node
  515. minetest.add_particlespawner({
  516. amount = 8,
  517. time = 0.001,
  518. minpos = vector.subtract(pos, {x=0.5, y=0.5, z=0.5}),
  519. maxpos = vector.add(pos, {x=0.5, y=0.5, z=0.5}),
  520. minvel = vector.new(-0.5, -1, -0.5),
  521. maxvel = vector.new(0.5, 0, 0.5),
  522. minacc = vector.new(0, -movement_gravity, 0),
  523. maxacc = vector.new(0, -movement_gravity, 0),
  524. minsize = 0,
  525. maxsize = 0,
  526. node = node,
  527. })
  528. end
  529. function default.register_leafdecay(def)
  530. assert(def.leaves)
  531. assert(def.trunks)
  532. assert(def.radius)
  533. for _, v in pairs(def.trunks) do
  534. minetest.override_item(v, {
  535. after_destruct = function(pos, oldnode)
  536. leafdecay_after_destruct(pos, oldnode, def)
  537. end,
  538. })
  539. end
  540. for _, v in pairs(def.leaves) do
  541. minetest.override_item(v, {
  542. on_timer = function(pos)
  543. leafdecay_on_timer(pos, def)
  544. end,
  545. })
  546. end
  547. end
  548. --
  549. -- Convert default:dirt to something that fits the environment
  550. --
  551. minetest.register_abm({
  552. label = "Grass spread",
  553. nodenames = {"default:dirt"},
  554. neighbors = {
  555. "air",
  556. "group:grass",
  557. "group:dry_grass",
  558. "default:snow",
  559. },
  560. interval = 6,
  561. chance = 50,
  562. catch_up = false,
  563. action = function(pos, node)
  564. -- Check for darkness: night, shadow or under a light-blocking node
  565. -- Returns if ignore above
  566. local above = {x = pos.x, y = pos.y + 1, z = pos.z}
  567. if (minetest.get_node_light(above) or 0) < 13 then
  568. return
  569. end
  570. -- Look for spreading dirt-type neighbours
  571. local p2 = minetest.find_node_near(pos, 1, "group:spreading_dirt_type")
  572. if p2 then
  573. local n3 = minetest.get_node(p2)
  574. minetest.set_node(pos, {name = n3.name})
  575. return
  576. end
  577. -- Else, any seeding nodes on top?
  578. local name = minetest.get_node(above).name
  579. -- Snow check is cheapest, so comes first
  580. if name == "default:snow" then
  581. minetest.set_node(pos, {name = "default:dirt_with_snow"})
  582. elseif minetest.get_item_group(name, "grass") ~= 0 then
  583. minetest.set_node(pos, {name = "default:dirt_with_grass"})
  584. elseif minetest.get_item_group(name, "dry_grass") ~= 0 then
  585. minetest.set_node(pos, {name = "default:dirt_with_dry_grass"})
  586. end
  587. end
  588. })
  589. --
  590. -- Grass and dry grass removed in darkness
  591. --
  592. minetest.register_abm({
  593. label = "Grass covered",
  594. nodenames = {"group:spreading_dirt_type", "default:dry_dirt_with_dry_grass"},
  595. interval = 8,
  596. chance = 50,
  597. catch_up = false,
  598. action = function(pos, node)
  599. local above = {x = pos.x, y = pos.y + 1, z = pos.z}
  600. local name = minetest.get_node(above).name
  601. local nodedef = minetest.registered_nodes[name]
  602. if name ~= "ignore" and nodedef and not ((nodedef.sunlight_propagates or
  603. nodedef.paramtype == "light") and
  604. nodedef.liquidtype == "none") then
  605. if node.name == "default:dry_dirt_with_dry_grass" then
  606. minetest.set_node(pos, {name = "default:dry_dirt"})
  607. else
  608. minetest.set_node(pos, {name = "default:dirt"})
  609. end
  610. end
  611. end
  612. })
  613. --
  614. -- Moss growth on cobble near water
  615. --
  616. local moss_correspondences = {
  617. ["default:cobble"] = "default:mossycobble",
  618. ["stairs:slab_cobble"] = "stairs:slab_mossycobble",
  619. ["stairs:stair_cobble"] = "stairs:stair_mossycobble",
  620. ["stairs:stair_inner_cobble"] = "stairs:stair_inner_mossycobble",
  621. ["stairs:stair_outer_cobble"] = "stairs:stair_outer_mossycobble",
  622. ["walls:cobble"] = "walls:mossycobble",
  623. }
  624. minetest.register_abm({
  625. label = "Moss growth",
  626. nodenames = {"default:cobble", "stairs:slab_cobble", "stairs:stair_cobble",
  627. "stairs:stair_inner_cobble", "stairs:stair_outer_cobble",
  628. "walls:cobble"},
  629. neighbors = {"group:water"},
  630. interval = 16,
  631. chance = 200,
  632. catch_up = false,
  633. action = function(pos, node)
  634. node.name = moss_correspondences[node.name]
  635. if node.name then
  636. minetest.set_node(pos, node)
  637. end
  638. end
  639. })
  640. --
  641. -- Register a craft to copy the metadata of items
  642. --
  643. function default.register_craft_metadata_copy(ingredient, result)
  644. minetest.register_craft({
  645. type = "shapeless",
  646. output = result,
  647. recipe = {ingredient, result}
  648. })
  649. minetest.register_on_craft(function(itemstack, player, old_craft_grid, craft_inv)
  650. if itemstack:get_name() ~= result then
  651. return
  652. end
  653. local original
  654. local index
  655. for i = 1, #old_craft_grid do
  656. if old_craft_grid[i]:get_name() == result then
  657. original = old_craft_grid[i]
  658. index = i
  659. end
  660. end
  661. if not original then
  662. return
  663. end
  664. local copymeta = original:get_meta():to_table()
  665. itemstack:get_meta():from_table(copymeta)
  666. -- put the book with metadata back in the craft grid
  667. craft_inv:set_stack("craft", index, original)
  668. end)
  669. end
  670. --
  671. -- Log API / helpers
  672. --
  673. local log_non_player_actions = minetest.settings:get_bool("log_non_player_actions", false)
  674. local is_pos = function(v)
  675. return type(v) == "table" and
  676. type(v.x) == "number" and type(v.y) == "number" and type(v.z) == "number"
  677. end
  678. function default.log_player_action(player, ...)
  679. local msg = player:get_player_name()
  680. if player.is_fake_player or not player:is_player() then
  681. if not log_non_player_actions then
  682. return
  683. end
  684. msg = msg .. "(" .. (type(player.is_fake_player) == "string"
  685. and player.is_fake_player or "*") .. ")"
  686. end
  687. for _, v in ipairs({...}) do
  688. -- translate pos
  689. local part = is_pos(v) and minetest.pos_to_string(v) or v
  690. -- no leading spaces before punctuation marks
  691. msg = msg .. (string.match(part, "^[;,.]") and "" or " ") .. part
  692. end
  693. minetest.log("action", msg)
  694. end
  695. local nop = function() end
  696. function default.set_inventory_action_loggers(def, name)
  697. local on_move = def.on_metadata_inventory_move or nop
  698. def.on_metadata_inventory_move = function(pos, from_list, from_index,
  699. to_list, to_index, count, player)
  700. default.log_player_action(player, "moves stuff in", name, "at", pos)
  701. return on_move(pos, from_list, from_index, to_list, to_index, count, player)
  702. end
  703. local on_put = def.on_metadata_inventory_put or nop
  704. def.on_metadata_inventory_put = function(pos, listname, index, stack, player)
  705. default.log_player_action(player, "moves", stack:get_name(), stack:get_count(), "to", name, "at", pos)
  706. return on_put(pos, listname, index, stack, player)
  707. end
  708. local on_take = def.on_metadata_inventory_take or nop
  709. def.on_metadata_inventory_take = function(pos, listname, index, stack, player)
  710. default.log_player_action(player, "takes", stack:get_name(), stack:get_count(), "from", name, "at", pos)
  711. return on_take(pos, listname, index, stack, player)
  712. end
  713. end
  714. --
  715. -- NOTICE: This method is not an official part of the API yet.
  716. -- This method may change in future.
  717. --
  718. function default.can_interact_with_node(player, pos)
  719. if player and player:is_player() then
  720. if minetest.check_player_privs(player, "protection_bypass") then
  721. return true
  722. end
  723. else
  724. return false
  725. end
  726. local meta = minetest.get_meta(pos)
  727. local owner = meta:get_string("owner")
  728. if not owner or owner == "" or owner == player:get_player_name() then
  729. return true
  730. end
  731. -- Is player wielding the right key?
  732. local item = player:get_wielded_item()
  733. if minetest.get_item_group(item:get_name(), "key") == 1 then
  734. local key_meta = item:get_meta()
  735. if key_meta:get_string("secret") == "" then
  736. local key_oldmeta = item:get_meta():get_string("")
  737. if key_oldmeta == "" or not minetest.parse_json(key_oldmeta) then
  738. return false
  739. end
  740. key_meta:set_string("secret", minetest.parse_json(key_oldmeta).secret)
  741. item:set_metadata("")
  742. end
  743. return meta:get_string("key_lock_secret") == key_meta:get_string("secret")
  744. end
  745. return false
  746. end