functions.lua 20 KB

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