functions.lua 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819
  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. function default.dig_up(pos, node, digger)
  268. if digger == nil then return end
  269. local np = {x = pos.x, y = pos.y + 1, z = pos.z}
  270. local nn = minetest.get_node(np)
  271. if nn.name == node.name then
  272. minetest.node_dig(np, nn, digger)
  273. end
  274. end
  275. --
  276. -- Fence registration helper
  277. --
  278. local fence_collision_extra = minetest.settings:get_bool("enable_fence_tall") and 3/8 or 0
  279. function default.register_fence(name, def)
  280. local fence_texture = "default_fence_overlay.png^" .. def.texture ..
  281. "^default_fence_overlay.png^[makealpha:255,126,126"
  282. -- Allow almost everything to be overridden
  283. local default_fields = {
  284. paramtype = "light",
  285. drawtype = "nodebox",
  286. node_box = {
  287. type = "connected",
  288. fixed = {-1/8, -1/2, -1/8, 1/8, 1/2, 1/8},
  289. -- connect_top =
  290. -- connect_bottom =
  291. connect_front = {{-1/16, 3/16, -1/2, 1/16, 5/16, -1/8 },
  292. {-1/16, -5/16, -1/2, 1/16, -3/16, -1/8 }},
  293. connect_left = {{-1/2, 3/16, -1/16, -1/8, 5/16, 1/16},
  294. {-1/2, -5/16, -1/16, -1/8, -3/16, 1/16}},
  295. connect_back = {{-1/16, 3/16, 1/8, 1/16, 5/16, 1/2 },
  296. {-1/16, -5/16, 1/8, 1/16, -3/16, 1/2 }},
  297. connect_right = {{ 1/8, 3/16, -1/16, 1/2, 5/16, 1/16},
  298. { 1/8, -5/16, -1/16, 1/2, -3/16, 1/16}}
  299. },
  300. collision_box = {
  301. type = "connected",
  302. fixed = {-1/8, -1/2, -1/8, 1/8, 1/2 + fence_collision_extra, 1/8},
  303. -- connect_top =
  304. -- connect_bottom =
  305. connect_front = {-1/8, -1/2, -1/2, 1/8, 1/2 + fence_collision_extra, -1/8},
  306. connect_left = {-1/2, -1/2, -1/8, -1/8, 1/2 + fence_collision_extra, 1/8},
  307. connect_back = {-1/8, -1/2, 1/8, 1/8, 1/2 + fence_collision_extra, 1/2},
  308. connect_right = { 1/8, -1/2, -1/8, 1/2, 1/2 + fence_collision_extra, 1/8}
  309. },
  310. connects_to = {"group:fence", "group:wood", "group:tree", "group:wall"},
  311. inventory_image = fence_texture,
  312. wield_image = fence_texture,
  313. tiles = {def.texture},
  314. sunlight_propagates = true,
  315. is_ground_content = false,
  316. groups = {},
  317. }
  318. for k, v in pairs(default_fields) do
  319. if def[k] == nil then
  320. def[k] = v
  321. end
  322. end
  323. -- Always add to the fence group, even if no group provided
  324. def.groups.fence = 1
  325. local material = def.material
  326. def.texture = nil
  327. def.material = nil
  328. minetest.register_node(name, def)
  329. -- Register crafting recipe, trim away starting colon if any
  330. if not material then return end
  331. name = string.gsub(name, "^:", "")
  332. minetest.register_craft({
  333. output = name .. " 4",
  334. recipe = {
  335. { material, 'group:stick', material },
  336. { material, 'group:stick', material },
  337. }
  338. })
  339. end
  340. --
  341. -- Fence rail registration helper
  342. --
  343. function default.register_fence_rail(name, def)
  344. local fence_rail_texture = "default_fence_rail_overlay.png^" .. def.texture ..
  345. "^default_fence_rail_overlay.png^[makealpha:255,126,126"
  346. -- Allow almost everything to be overridden
  347. local default_fields = {
  348. paramtype = "light",
  349. drawtype = "nodebox",
  350. node_box = {
  351. type = "connected",
  352. fixed = {{-1/16, 3/16, -1/16, 1/16, 5/16, 1/16},
  353. {-1/16, -3/16, -1/16, 1/16, -5/16, 1/16}},
  354. -- connect_top =
  355. -- connect_bottom =
  356. connect_front = {{-1/16, 3/16, -1/2, 1/16, 5/16, -1/16},
  357. {-1/16, -5/16, -1/2, 1/16, -3/16, -1/16}},
  358. connect_left = {{-1/2, 3/16, -1/16, -1/16, 5/16, 1/16},
  359. {-1/2, -5/16, -1/16, -1/16, -3/16, 1/16}},
  360. connect_back = {{-1/16, 3/16, 1/16, 1/16, 5/16, 1/2 },
  361. {-1/16, -5/16, 1/16, 1/16, -3/16, 1/2 }},
  362. connect_right = {{ 1/16, 3/16, -1/16, 1/2, 5/16, 1/16},
  363. { 1/16, -5/16, -1/16, 1/2, -3/16, 1/16}}
  364. },
  365. collision_box = {
  366. type = "connected",
  367. fixed = {-1/8, -1/2, -1/8, 1/8, 1/2 + fence_collision_extra, 1/8},
  368. -- connect_top =
  369. -- connect_bottom =
  370. connect_front = {-1/8, -1/2, -1/2, 1/8, 1/2 + fence_collision_extra, -1/8},
  371. connect_left = {-1/2, -1/2, -1/8, -1/8, 1/2 + fence_collision_extra, 1/8},
  372. connect_back = {-1/8, -1/2, 1/8, 1/8, 1/2 + fence_collision_extra, 1/2},
  373. connect_right = { 1/8, -1/2, -1/8, 1/2, 1/2 + fence_collision_extra, 1/8}
  374. },
  375. connects_to = {"group:fence", "group:wall"},
  376. inventory_image = fence_rail_texture,
  377. wield_image = fence_rail_texture,
  378. tiles = {def.texture},
  379. sunlight_propagates = true,
  380. is_ground_content = false,
  381. groups = {},
  382. }
  383. for k, v in pairs(default_fields) do
  384. if def[k] == nil then
  385. def[k] = v
  386. end
  387. end
  388. -- Always add to the fence group, even if no group provided
  389. def.groups.fence = 1
  390. local material = def.material
  391. def.texture = nil
  392. def.material = nil
  393. minetest.register_node(name, def)
  394. -- Register crafting recipe, trim away starting colon if any
  395. if not material then return end
  396. name = string.gsub(name, "^:", "")
  397. minetest.register_craft({
  398. output = name .. " 16",
  399. recipe = {
  400. { material, material },
  401. { "", ""},
  402. { material, material },
  403. }
  404. })
  405. end
  406. --
  407. -- Mese post registration helper
  408. --
  409. function default.register_mesepost(name, def)
  410. local post_texture = def.texture .. "^default_mese_post_light_side.png^[makealpha:0,0,0"
  411. local post_texture_dark = def.texture .. "^default_mese_post_light_side_dark.png^[makealpha:0,0,0"
  412. -- Allow almost everything to be overridden
  413. local default_fields = {
  414. wield_image = post_texture,
  415. drawtype = "nodebox",
  416. node_box = {
  417. type = "fixed",
  418. fixed = {
  419. {-2 / 16, -8 / 16, -2 / 16, 2 / 16, 8 / 16, 2 / 16},
  420. },
  421. },
  422. paramtype = "light",
  423. tiles = {def.texture, def.texture, post_texture_dark, post_texture_dark, post_texture, post_texture},
  424. use_texture_alpha = "opaque",
  425. light_source = default.LIGHT_MAX,
  426. sunlight_propagates = true,
  427. is_ground_content = false,
  428. groups = {choppy = 2, oddly_breakable_by_hand = 2, flammable = 2},
  429. sounds = default.node_sound_wood_defaults(),
  430. }
  431. for k, v in pairs(default_fields) do
  432. if def[k] == nil then
  433. def[k] = v
  434. end
  435. end
  436. local material = def.material
  437. def.texture = nil
  438. def.material = nil
  439. minetest.register_node(name, def)
  440. -- Register crafting recipe, trim away starting colon if any
  441. if not material then return end
  442. name = string.gsub(name, "^:", "")
  443. minetest.register_craft({
  444. output = name .. " 4",
  445. recipe = {
  446. {'', 'default:glass', ''},
  447. {'default:mese_crystal', 'default:mese_crystal', 'default:mese_crystal'},
  448. {'', material, ''},
  449. }
  450. })
  451. end
  452. --
  453. -- Leafdecay
  454. --
  455. -- Prevent decay of placed leaves
  456. default.after_place_leaves = function(pos, placer, itemstack, pointed_thing)
  457. if placer and placer:is_player() then
  458. local node = minetest.get_node(pos)
  459. node.param2 = 1
  460. minetest.set_node(pos, node)
  461. end
  462. end
  463. -- Leafdecay
  464. local function leafdecay_after_destruct(pos, oldnode, def)
  465. for _, v in pairs(minetest.find_nodes_in_area(vector.subtract(pos, def.radius),
  466. vector.add(pos, def.radius), def.leaves)) do
  467. local node = minetest.get_node(v)
  468. local timer = minetest.get_node_timer(v)
  469. if node.param2 ~= 1 and not timer:is_started() then
  470. timer:start(math.random(20, 120) / 10)
  471. end
  472. end
  473. end
  474. local movement_gravity = tonumber(
  475. minetest.settings:get("movement_gravity")) or 9.81
  476. local function leafdecay_on_timer(pos, def)
  477. if minetest.find_node_near(pos, def.radius, def.trunks) then
  478. return false
  479. end
  480. local node = minetest.get_node(pos)
  481. local drops = minetest.get_node_drops(node.name)
  482. for _, item in ipairs(drops) do
  483. local is_leaf
  484. for _, v in pairs(def.leaves) do
  485. if v == item then
  486. is_leaf = true
  487. end
  488. end
  489. if minetest.get_item_group(item, "leafdecay_drop") ~= 0 or
  490. not is_leaf then
  491. minetest.add_item({
  492. x = pos.x - 0.5 + math.random(),
  493. y = pos.y - 0.5 + math.random(),
  494. z = pos.z - 0.5 + math.random(),
  495. }, item)
  496. end
  497. end
  498. minetest.remove_node(pos)
  499. minetest.check_for_falling(pos)
  500. -- spawn a few particles for the removed node
  501. minetest.add_particlespawner({
  502. amount = 8,
  503. time = 0.001,
  504. minpos = vector.subtract(pos, {x=0.5, y=0.5, z=0.5}),
  505. maxpos = vector.add(pos, {x=0.5, y=0.5, z=0.5}),
  506. minvel = vector.new(-0.5, -1, -0.5),
  507. maxvel = vector.new(0.5, 0, 0.5),
  508. minacc = vector.new(0, -movement_gravity, 0),
  509. maxacc = vector.new(0, -movement_gravity, 0),
  510. minsize = 0,
  511. maxsize = 0,
  512. node = node,
  513. })
  514. end
  515. function default.register_leafdecay(def)
  516. assert(def.leaves)
  517. assert(def.trunks)
  518. assert(def.radius)
  519. for _, v in pairs(def.trunks) do
  520. minetest.override_item(v, {
  521. after_destruct = function(pos, oldnode)
  522. leafdecay_after_destruct(pos, oldnode, def)
  523. end,
  524. })
  525. end
  526. for _, v in pairs(def.leaves) do
  527. minetest.override_item(v, {
  528. on_timer = function(pos)
  529. leafdecay_on_timer(pos, def)
  530. end,
  531. })
  532. end
  533. end
  534. --
  535. -- Convert default:dirt to something that fits the environment
  536. --
  537. minetest.register_abm({
  538. label = "Grass spread",
  539. nodenames = {"default:dirt"},
  540. neighbors = {
  541. "air",
  542. "group:grass",
  543. "group:dry_grass",
  544. "default:snow",
  545. },
  546. interval = 6,
  547. chance = 50,
  548. catch_up = false,
  549. action = function(pos, node)
  550. -- Check for darkness: night, shadow or under a light-blocking node
  551. -- Returns if ignore above
  552. local above = {x = pos.x, y = pos.y + 1, z = pos.z}
  553. if (minetest.get_node_light(above) or 0) < 13 then
  554. return
  555. end
  556. -- Look for spreading dirt-type neighbours
  557. local p2 = minetest.find_node_near(pos, 1, "group:spreading_dirt_type")
  558. if p2 then
  559. local n3 = minetest.get_node(p2)
  560. minetest.set_node(pos, {name = n3.name})
  561. return
  562. end
  563. -- Else, any seeding nodes on top?
  564. local name = minetest.get_node(above).name
  565. -- Snow check is cheapest, so comes first
  566. if name == "default:snow" then
  567. minetest.set_node(pos, {name = "default:dirt_with_snow"})
  568. elseif minetest.get_item_group(name, "grass") ~= 0 then
  569. minetest.set_node(pos, {name = "default:dirt_with_grass"})
  570. elseif minetest.get_item_group(name, "dry_grass") ~= 0 then
  571. minetest.set_node(pos, {name = "default:dirt_with_dry_grass"})
  572. end
  573. end
  574. })
  575. --
  576. -- Grass and dry grass removed in darkness
  577. --
  578. minetest.register_abm({
  579. label = "Grass covered",
  580. nodenames = {"group:spreading_dirt_type", "default:dry_dirt_with_dry_grass"},
  581. interval = 8,
  582. chance = 50,
  583. catch_up = false,
  584. action = function(pos, node)
  585. local above = {x = pos.x, y = pos.y + 1, z = pos.z}
  586. local name = minetest.get_node(above).name
  587. local nodedef = minetest.registered_nodes[name]
  588. if name ~= "ignore" and nodedef and not ((nodedef.sunlight_propagates or
  589. nodedef.paramtype == "light") and
  590. nodedef.liquidtype == "none") then
  591. if node.name == "default:dry_dirt_with_dry_grass" then
  592. minetest.set_node(pos, {name = "default:dry_dirt"})
  593. else
  594. minetest.set_node(pos, {name = "default:dirt"})
  595. end
  596. end
  597. end
  598. })
  599. --
  600. -- Moss growth on cobble near water
  601. --
  602. local moss_correspondences = {
  603. ["default:cobble"] = "default:mossycobble",
  604. ["stairs:slab_cobble"] = "stairs:slab_mossycobble",
  605. ["stairs:stair_cobble"] = "stairs:stair_mossycobble",
  606. ["stairs:stair_inner_cobble"] = "stairs:stair_inner_mossycobble",
  607. ["stairs:stair_outer_cobble"] = "stairs:stair_outer_mossycobble",
  608. ["walls:cobble"] = "walls:mossycobble",
  609. }
  610. minetest.register_abm({
  611. label = "Moss growth",
  612. nodenames = {"default:cobble", "stairs:slab_cobble", "stairs:stair_cobble",
  613. "stairs:stair_inner_cobble", "stairs:stair_outer_cobble",
  614. "walls:cobble"},
  615. neighbors = {"group:water"},
  616. interval = 16,
  617. chance = 200,
  618. catch_up = false,
  619. action = function(pos, node)
  620. node.name = moss_correspondences[node.name]
  621. if node.name then
  622. minetest.set_node(pos, node)
  623. end
  624. end
  625. })
  626. --
  627. -- Register a craft to copy the metadata of items
  628. --
  629. function default.register_craft_metadata_copy(ingredient, result)
  630. minetest.register_craft({
  631. type = "shapeless",
  632. output = result,
  633. recipe = {ingredient, result}
  634. })
  635. minetest.register_on_craft(function(itemstack, player, old_craft_grid, craft_inv)
  636. if itemstack:get_name() ~= result then
  637. return
  638. end
  639. local original
  640. local index
  641. for i = 1, #old_craft_grid do
  642. if old_craft_grid[i]:get_name() == result then
  643. original = old_craft_grid[i]
  644. index = i
  645. end
  646. end
  647. if not original then
  648. return
  649. end
  650. local copymeta = original:get_meta():to_table()
  651. itemstack:get_meta():from_table(copymeta)
  652. -- put the book with metadata back in the craft grid
  653. craft_inv:set_stack("craft", index, original)
  654. end)
  655. end
  656. --
  657. -- Log API / helpers
  658. --
  659. local log_non_player_actions = minetest.settings:get_bool("log_non_player_actions", false)
  660. local is_pos = function(v)
  661. return type(v) == "table" and
  662. type(v.x) == "number" and type(v.y) == "number" and type(v.z) == "number"
  663. end
  664. function default.log_player_action(player, ...)
  665. local msg = player:get_player_name()
  666. if player.is_fake_player or not player:is_player() then
  667. if not log_non_player_actions then
  668. return
  669. end
  670. msg = msg .. "(" .. (type(player.is_fake_player) == "string"
  671. and player.is_fake_player or "*") .. ")"
  672. end
  673. for _, v in ipairs({...}) do
  674. -- translate pos
  675. local part = is_pos(v) and minetest.pos_to_string(v) or v
  676. -- no leading spaces before punctuation marks
  677. msg = msg .. (string.match(part, "^[;,.]") and "" or " ") .. part
  678. end
  679. minetest.log("action", msg)
  680. end
  681. local nop = function() end
  682. function default.set_inventory_action_loggers(def, name)
  683. local on_move = def.on_metadata_inventory_move or nop
  684. def.on_metadata_inventory_move = function(pos, from_list, from_index,
  685. to_list, to_index, count, player)
  686. default.log_player_action(player, "moves stuff in", name, "at", pos)
  687. return on_move(pos, from_list, from_index, to_list, to_index, count, player)
  688. end
  689. local on_put = def.on_metadata_inventory_put or nop
  690. def.on_metadata_inventory_put = function(pos, listname, index, stack, player)
  691. default.log_player_action(player, "moves", stack:get_name(), stack:get_count(), "to", name, "at", pos)
  692. return on_put(pos, listname, index, stack, player)
  693. end
  694. local on_take = def.on_metadata_inventory_take or nop
  695. def.on_metadata_inventory_take = function(pos, listname, index, stack, player)
  696. default.log_player_action(player, "takes", stack:get_name(), stack:get_count(), "from", name, "at", pos)
  697. return on_take(pos, listname, index, stack, player)
  698. end
  699. end
  700. --
  701. -- NOTICE: This method is not an official part of the API yet.
  702. -- This method may change in future.
  703. --
  704. function default.can_interact_with_node(player, pos)
  705. if player and player:is_player() then
  706. if minetest.check_player_privs(player, "protection_bypass") then
  707. return true
  708. end
  709. else
  710. return false
  711. end
  712. local meta = minetest.get_meta(pos)
  713. local owner = meta:get_string("owner")
  714. if not owner or owner == "" or owner == player:get_player_name() then
  715. return true
  716. end
  717. -- Is player wielding the right key?
  718. local item = player:get_wielded_item()
  719. if minetest.get_item_group(item:get_name(), "key") == 1 then
  720. local key_meta = item:get_meta()
  721. if key_meta:get_string("secret") == "" then
  722. local key_oldmeta = item:get_meta():get_string("")
  723. if key_oldmeta == "" or not minetest.parse_json(key_oldmeta) then
  724. return false
  725. end
  726. key_meta:set_string("secret", minetest.parse_json(key_oldmeta).secret)
  727. item:set_metadata("")
  728. end
  729. return meta:get_string("key_lock_secret") == key_meta:get_string("secret")
  730. end
  731. return false
  732. end