functions.lua 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807
  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.2}
  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.25}
  27. table.dig = table.dig or
  28. {name = "default_dig_crumbly", gain = 0.4}
  29. table.dug = table.dug or
  30. {name = "default_dirt_footstep", gain = 1.0}
  31. table.place = table.place or
  32. {name = "default_place_node", gain = 1.0}
  33. default.node_sound_defaults(table)
  34. return table
  35. end
  36. function default.node_sound_sand_defaults(table)
  37. table = table or {}
  38. table.footstep = table.footstep or
  39. {name = "default_sand_footstep", gain = 0.05}
  40. table.dug = table.dug or
  41. {name = "default_sand_footstep", gain = 0.15}
  42. table.place = table.place or
  43. {name = "default_place_node", gain = 1.0}
  44. default.node_sound_defaults(table)
  45. return table
  46. end
  47. function default.node_sound_gravel_defaults(table)
  48. table = table or {}
  49. table.footstep = table.footstep or
  50. {name = "default_gravel_footstep", gain = 0.25}
  51. table.dig = table.dig or
  52. {name = "default_gravel_dig", gain = 0.35}
  53. table.dug = table.dug or
  54. {name = "default_gravel_dug", gain = 1.0}
  55. table.place = table.place or
  56. {name = "default_place_node", gain = 1.0}
  57. default.node_sound_defaults(table)
  58. return table
  59. end
  60. function default.node_sound_wood_defaults(table)
  61. table = table or {}
  62. table.footstep = table.footstep or
  63. {name = "default_wood_footstep", gain = 0.15}
  64. table.dig = table.dig or
  65. {name = "default_dig_choppy", gain = 0.4}
  66. table.dug = table.dug or
  67. {name = "default_wood_footstep", gain = 1.0}
  68. default.node_sound_defaults(table)
  69. return table
  70. end
  71. function default.node_sound_leaves_defaults(table)
  72. table = table or {}
  73. table.footstep = table.footstep or
  74. {name = "default_grass_footstep", gain = 0.45}
  75. table.dug = table.dug or
  76. {name = "default_grass_footstep", gain = 0.7}
  77. table.place = table.place or
  78. {name = "default_place_node", gain = 1.0}
  79. default.node_sound_defaults(table)
  80. return table
  81. end
  82. function default.node_sound_glass_defaults(table)
  83. table = table or {}
  84. table.footstep = table.footstep or
  85. {name = "default_glass_footstep", gain = 0.3}
  86. table.dig = table.dig or
  87. {name = "default_glass_footstep", gain = 0.5}
  88. table.dug = table.dug or
  89. {name = "default_break_glass", gain = 1.0}
  90. default.node_sound_defaults(table)
  91. return table
  92. end
  93. function default.node_sound_ice_defaults(table)
  94. table = table or {}
  95. table.footstep = table.footstep or
  96. {name = "default_ice_footstep", gain = 0.15}
  97. table.dig = table.dig or
  98. {name = "default_ice_dig", gain = 0.5}
  99. table.dug = table.dug or
  100. {name = "default_ice_dug", gain = 0.5}
  101. default.node_sound_defaults(table)
  102. return table
  103. end
  104. function default.node_sound_metal_defaults(table)
  105. table = table or {}
  106. table.footstep = table.footstep or
  107. {name = "default_metal_footstep", gain = 0.2}
  108. table.dig = table.dig or
  109. {name = "default_dig_metal", gain = 0.5}
  110. table.dug = table.dug or
  111. {name = "default_dug_metal", gain = 0.5}
  112. table.place = table.place or
  113. {name = "default_place_node_metal", gain = 0.5}
  114. default.node_sound_defaults(table)
  115. return table
  116. end
  117. function default.node_sound_water_defaults(table)
  118. table = table or {}
  119. table.footstep = table.footstep or
  120. {name = "default_water_footstep", gain = 0.2}
  121. default.node_sound_defaults(table)
  122. return table
  123. end
  124. function default.node_sound_snow_defaults(table)
  125. table = table or {}
  126. table.footstep = table.footstep or
  127. {name = "default_snow_footstep", gain = 0.2}
  128. table.dig = table.dig or
  129. {name = "default_snow_footstep", gain = 0.3}
  130. table.dug = table.dug or
  131. {name = "default_snow_footstep", gain = 0.3}
  132. table.place = table.place or
  133. {name = "default_place_node", gain = 1.0}
  134. default.node_sound_defaults(table)
  135. return table
  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. minetest.register_craft({
  281. output = name .. " 4",
  282. recipe = {
  283. { def.material, 'group:stick', def.material },
  284. { def.material, 'group:stick', def.material },
  285. }
  286. })
  287. local fence_texture = "default_fence_overlay.png^" .. def.texture ..
  288. "^default_fence_overlay.png^[makealpha:255,126,126"
  289. -- Allow almost everything to be overridden
  290. local default_fields = {
  291. paramtype = "light",
  292. drawtype = "nodebox",
  293. node_box = {
  294. type = "connected",
  295. fixed = {-1/8, -1/2, -1/8, 1/8, 1/2, 1/8},
  296. -- connect_top =
  297. -- connect_bottom =
  298. connect_front = {{-1/16, 3/16, -1/2, 1/16, 5/16, -1/8 },
  299. {-1/16, -5/16, -1/2, 1/16, -3/16, -1/8 }},
  300. connect_left = {{-1/2, 3/16, -1/16, -1/8, 5/16, 1/16},
  301. {-1/2, -5/16, -1/16, -1/8, -3/16, 1/16}},
  302. connect_back = {{-1/16, 3/16, 1/8, 1/16, 5/16, 1/2 },
  303. {-1/16, -5/16, 1/8, 1/16, -3/16, 1/2 }},
  304. connect_right = {{ 1/8, 3/16, -1/16, 1/2, 5/16, 1/16},
  305. { 1/8, -5/16, -1/16, 1/2, -3/16, 1/16}}
  306. },
  307. collision_box = {
  308. type = "connected",
  309. fixed = {-1/8, -1/2, -1/8, 1/8, 1/2 + fence_collision_extra, 1/8},
  310. -- connect_top =
  311. -- connect_bottom =
  312. connect_front = {-1/8, -1/2, -1/2, 1/8, 1/2 + fence_collision_extra, -1/8},
  313. connect_left = {-1/2, -1/2, -1/8, -1/8, 1/2 + fence_collision_extra, 1/8},
  314. connect_back = {-1/8, -1/2, 1/8, 1/8, 1/2 + fence_collision_extra, 1/2},
  315. connect_right = { 1/8, -1/2, -1/8, 1/2, 1/2 + fence_collision_extra, 1/8}
  316. },
  317. connects_to = {"group:fence", "group:wood", "group:tree", "group:wall"},
  318. inventory_image = fence_texture,
  319. wield_image = fence_texture,
  320. tiles = {def.texture},
  321. sunlight_propagates = true,
  322. is_ground_content = false,
  323. groups = {},
  324. }
  325. for k, v in pairs(default_fields) do
  326. if def[k] == nil then
  327. def[k] = v
  328. end
  329. end
  330. -- Always add to the fence group, even if no group provided
  331. def.groups.fence = 1
  332. def.texture = nil
  333. def.material = nil
  334. minetest.register_node(name, def)
  335. end
  336. --
  337. -- Fence rail registration helper
  338. --
  339. function default.register_fence_rail(name, def)
  340. minetest.register_craft({
  341. output = name .. " 16",
  342. recipe = {
  343. { def.material, def.material },
  344. { "", ""},
  345. { def.material, def.material },
  346. }
  347. })
  348. local fence_rail_texture = "default_fence_rail_overlay.png^" .. def.texture ..
  349. "^default_fence_rail_overlay.png^[makealpha:255,126,126"
  350. -- Allow almost everything to be overridden
  351. local default_fields = {
  352. paramtype = "light",
  353. drawtype = "nodebox",
  354. node_box = {
  355. type = "connected",
  356. fixed = {{-1/16, 3/16, -1/16, 1/16, 5/16, 1/16},
  357. {-1/16, -3/16, -1/16, 1/16, -5/16, 1/16}},
  358. -- connect_top =
  359. -- connect_bottom =
  360. connect_front = {{-1/16, 3/16, -1/2, 1/16, 5/16, -1/16},
  361. {-1/16, -5/16, -1/2, 1/16, -3/16, -1/16}},
  362. connect_left = {{-1/2, 3/16, -1/16, -1/16, 5/16, 1/16},
  363. {-1/2, -5/16, -1/16, -1/16, -3/16, 1/16}},
  364. connect_back = {{-1/16, 3/16, 1/16, 1/16, 5/16, 1/2 },
  365. {-1/16, -5/16, 1/16, 1/16, -3/16, 1/2 }},
  366. connect_right = {{ 1/16, 3/16, -1/16, 1/2, 5/16, 1/16},
  367. { 1/16, -5/16, -1/16, 1/2, -3/16, 1/16}}
  368. },
  369. collision_box = {
  370. type = "connected",
  371. fixed = {-1/8, -1/2, -1/8, 1/8, 1/2 + fence_collision_extra, 1/8},
  372. -- connect_top =
  373. -- connect_bottom =
  374. connect_front = {-1/8, -1/2, -1/2, 1/8, 1/2 + fence_collision_extra, -1/8},
  375. connect_left = {-1/2, -1/2, -1/8, -1/8, 1/2 + fence_collision_extra, 1/8},
  376. connect_back = {-1/8, -1/2, 1/8, 1/8, 1/2 + fence_collision_extra, 1/2},
  377. connect_right = { 1/8, -1/2, -1/8, 1/2, 1/2 + fence_collision_extra, 1/8}
  378. },
  379. connects_to = {"group:fence", "group:wall"},
  380. inventory_image = fence_rail_texture,
  381. wield_image = fence_rail_texture,
  382. tiles = {def.texture},
  383. sunlight_propagates = true,
  384. is_ground_content = false,
  385. groups = {},
  386. }
  387. for k, v in pairs(default_fields) do
  388. if def[k] == nil then
  389. def[k] = v
  390. end
  391. end
  392. -- Always add to the fence group, even if no group provided
  393. def.groups.fence = 1
  394. def.texture = nil
  395. def.material = nil
  396. minetest.register_node(name, def)
  397. end
  398. --
  399. -- Mese post registration helper
  400. --
  401. function default.register_mesepost(name, def)
  402. minetest.register_craft({
  403. output = name .. " 4",
  404. recipe = {
  405. {'', 'default:glass', ''},
  406. {'default:mese_crystal', 'default:mese_crystal', 'default:mese_crystal'},
  407. {'', def.material, ''},
  408. }
  409. })
  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. def.texture = nil
  437. def.material = nil
  438. minetest.register_node(name, def)
  439. end
  440. --
  441. -- Leafdecay
  442. --
  443. -- Prevent decay of placed leaves
  444. default.after_place_leaves = function(pos, placer, itemstack, pointed_thing)
  445. if placer and placer:is_player() then
  446. local node = minetest.get_node(pos)
  447. node.param2 = 1
  448. minetest.set_node(pos, node)
  449. end
  450. end
  451. -- Leafdecay
  452. local function leafdecay_after_destruct(pos, oldnode, def)
  453. for _, v in pairs(minetest.find_nodes_in_area(vector.subtract(pos, def.radius),
  454. vector.add(pos, def.radius), def.leaves)) do
  455. local node = minetest.get_node(v)
  456. local timer = minetest.get_node_timer(v)
  457. if node.param2 ~= 1 and not timer:is_started() then
  458. timer:start(math.random(20, 120) / 10)
  459. end
  460. end
  461. end
  462. local movement_gravity = tonumber(
  463. minetest.settings:get("movement_gravity")) or 9.81
  464. local function leafdecay_on_timer(pos, def)
  465. if minetest.find_node_near(pos, def.radius, def.trunks) then
  466. return false
  467. end
  468. local node = minetest.get_node(pos)
  469. local drops = minetest.get_node_drops(node.name)
  470. for _, item in ipairs(drops) do
  471. local is_leaf
  472. for _, v in pairs(def.leaves) do
  473. if v == item then
  474. is_leaf = true
  475. end
  476. end
  477. if minetest.get_item_group(item, "leafdecay_drop") ~= 0 or
  478. not is_leaf then
  479. minetest.add_item({
  480. x = pos.x - 0.5 + math.random(),
  481. y = pos.y - 0.5 + math.random(),
  482. z = pos.z - 0.5 + math.random(),
  483. }, item)
  484. end
  485. end
  486. minetest.remove_node(pos)
  487. minetest.check_for_falling(pos)
  488. -- spawn a few particles for the removed node
  489. minetest.add_particlespawner({
  490. amount = 8,
  491. time = 0.001,
  492. minpos = vector.subtract(pos, {x=0.5, y=0.5, z=0.5}),
  493. maxpos = vector.add(pos, {x=0.5, y=0.5, z=0.5}),
  494. minvel = vector.new(-0.5, -1, -0.5),
  495. maxvel = vector.new(0.5, 0, 0.5),
  496. minacc = vector.new(0, -movement_gravity, 0),
  497. maxacc = vector.new(0, -movement_gravity, 0),
  498. minsize = 0,
  499. maxsize = 0,
  500. node = node,
  501. })
  502. end
  503. function default.register_leafdecay(def)
  504. assert(def.leaves)
  505. assert(def.trunks)
  506. assert(def.radius)
  507. for _, v in pairs(def.trunks) do
  508. minetest.override_item(v, {
  509. after_destruct = function(pos, oldnode)
  510. leafdecay_after_destruct(pos, oldnode, def)
  511. end,
  512. })
  513. end
  514. for _, v in pairs(def.leaves) do
  515. minetest.override_item(v, {
  516. on_timer = function(pos)
  517. leafdecay_on_timer(pos, def)
  518. end,
  519. })
  520. end
  521. end
  522. --
  523. -- Convert default:dirt to something that fits the environment
  524. --
  525. minetest.register_abm({
  526. label = "Grass spread",
  527. nodenames = {"default:dirt"},
  528. neighbors = {
  529. "air",
  530. "group:grass",
  531. "group:dry_grass",
  532. "default:snow",
  533. },
  534. interval = 6,
  535. chance = 50,
  536. catch_up = false,
  537. action = function(pos, node)
  538. -- Check for darkness: night, shadow or under a light-blocking node
  539. -- Returns if ignore above
  540. local above = {x = pos.x, y = pos.y + 1, z = pos.z}
  541. if (minetest.get_node_light(above) or 0) < 13 then
  542. return
  543. end
  544. -- Look for spreading dirt-type neighbours
  545. local p2 = minetest.find_node_near(pos, 1, "group:spreading_dirt_type")
  546. if p2 then
  547. local n3 = minetest.get_node(p2)
  548. minetest.set_node(pos, {name = n3.name})
  549. return
  550. end
  551. -- Else, any seeding nodes on top?
  552. local name = minetest.get_node(above).name
  553. -- Snow check is cheapest, so comes first
  554. if name == "default:snow" then
  555. minetest.set_node(pos, {name = "default:dirt_with_snow"})
  556. elseif minetest.get_item_group(name, "grass") ~= 0 then
  557. minetest.set_node(pos, {name = "default:dirt_with_grass"})
  558. elseif minetest.get_item_group(name, "dry_grass") ~= 0 then
  559. minetest.set_node(pos, {name = "default:dirt_with_dry_grass"})
  560. end
  561. end
  562. })
  563. --
  564. -- Grass and dry grass removed in darkness
  565. --
  566. minetest.register_abm({
  567. label = "Grass covered",
  568. nodenames = {"group:spreading_dirt_type", "default:dry_dirt_with_dry_grass"},
  569. interval = 8,
  570. chance = 50,
  571. catch_up = false,
  572. action = function(pos, node)
  573. local above = {x = pos.x, y = pos.y + 1, z = pos.z}
  574. local name = minetest.get_node(above).name
  575. local nodedef = minetest.registered_nodes[name]
  576. if name ~= "ignore" and nodedef and not ((nodedef.sunlight_propagates or
  577. nodedef.paramtype == "light") and
  578. nodedef.liquidtype == "none") then
  579. if node.name == "default:dry_dirt_with_dry_grass" then
  580. minetest.set_node(pos, {name = "default:dry_dirt"})
  581. else
  582. minetest.set_node(pos, {name = "default:dirt"})
  583. end
  584. end
  585. end
  586. })
  587. --
  588. -- Moss growth on cobble near water
  589. --
  590. local moss_correspondences = {
  591. ["default:cobble"] = "default:mossycobble",
  592. ["stairs:slab_cobble"] = "stairs:slab_mossycobble",
  593. ["stairs:stair_cobble"] = "stairs:stair_mossycobble",
  594. ["stairs:stair_inner_cobble"] = "stairs:stair_inner_mossycobble",
  595. ["stairs:stair_outer_cobble"] = "stairs:stair_outer_mossycobble",
  596. ["walls:cobble"] = "walls:mossycobble",
  597. }
  598. minetest.register_abm({
  599. label = "Moss growth",
  600. nodenames = {"default:cobble", "stairs:slab_cobble", "stairs:stair_cobble",
  601. "stairs:stair_inner_cobble", "stairs:stair_outer_cobble",
  602. "walls:cobble"},
  603. neighbors = {"group:water"},
  604. interval = 16,
  605. chance = 200,
  606. catch_up = false,
  607. action = function(pos, node)
  608. node.name = moss_correspondences[node.name]
  609. if node.name then
  610. minetest.set_node(pos, node)
  611. end
  612. end
  613. })
  614. --
  615. -- Register a craft to copy the metadata of items
  616. --
  617. function default.register_craft_metadata_copy(ingredient, result)
  618. minetest.register_craft({
  619. type = "shapeless",
  620. output = result,
  621. recipe = {ingredient, result}
  622. })
  623. minetest.register_on_craft(function(itemstack, player, old_craft_grid, craft_inv)
  624. if itemstack:get_name() ~= result then
  625. return
  626. end
  627. local original
  628. local index
  629. for i = 1, #old_craft_grid do
  630. if old_craft_grid[i]:get_name() == result then
  631. original = old_craft_grid[i]
  632. index = i
  633. end
  634. end
  635. if not original then
  636. return
  637. end
  638. local copymeta = original:get_meta():to_table()
  639. itemstack:get_meta():from_table(copymeta)
  640. -- put the book with metadata back in the craft grid
  641. craft_inv:set_stack("craft", index, original)
  642. end)
  643. end
  644. --
  645. -- Log API / helpers
  646. --
  647. local log_non_player_actions = minetest.settings:get_bool("log_non_player_actions", false)
  648. local is_pos = function(v)
  649. return type(v) == "table" and
  650. type(v.x) == "number" and type(v.y) == "number" and type(v.z) == "number"
  651. end
  652. function default.log_player_action(player, ...)
  653. local msg = player:get_player_name()
  654. if player.is_fake_player or not player:is_player() then
  655. if not log_non_player_actions then
  656. return
  657. end
  658. msg = msg .. "(" .. (type(player.is_fake_player) == "string"
  659. and player.is_fake_player or "*") .. ")"
  660. end
  661. for _, v in ipairs({...}) do
  662. -- translate pos
  663. local part = is_pos(v) and minetest.pos_to_string(v) or v
  664. -- no leading spaces before punctuation marks
  665. msg = msg .. (string.match(part, "^[;,.]") and "" or " ") .. part
  666. end
  667. minetest.log("action", msg)
  668. end
  669. local nop = function() end
  670. function default.set_inventory_action_loggers(def, name)
  671. local on_move = def.on_metadata_inventory_move or nop
  672. def.on_metadata_inventory_move = function(pos, from_list, from_index,
  673. to_list, to_index, count, player)
  674. default.log_player_action(player, "moves stuff in", name, "at", pos)
  675. return on_move(pos, from_list, from_index, to_list, to_index, count, player)
  676. end
  677. local on_put = def.on_metadata_inventory_put or nop
  678. def.on_metadata_inventory_put = function(pos, listname, index, stack, player)
  679. default.log_player_action(player, "moves", stack:get_name(), stack:get_count(), "to", name, "at", pos)
  680. return on_put(pos, listname, index, stack, player)
  681. end
  682. local on_take = def.on_metadata_inventory_take or nop
  683. def.on_metadata_inventory_take = function(pos, listname, index, stack, player)
  684. default.log_player_action(player, "takes", stack:get_name(), stack:get_count(), "from", name, "at", pos)
  685. return on_take(pos, listname, index, stack, player)
  686. end
  687. end
  688. --
  689. -- NOTICE: This method is not an official part of the API yet.
  690. -- This method may change in future.
  691. --
  692. function default.can_interact_with_node(player, pos)
  693. if player and player:is_player() then
  694. if minetest.check_player_privs(player, "protection_bypass") then
  695. return true
  696. end
  697. else
  698. return false
  699. end
  700. local meta = minetest.get_meta(pos)
  701. local owner = meta:get_string("owner")
  702. if not owner or owner == "" or owner == player:get_player_name() then
  703. return true
  704. end
  705. -- Is player wielding the right key?
  706. local item = player:get_wielded_item()
  707. if minetest.get_item_group(item:get_name(), "key") == 1 then
  708. local key_meta = item:get_meta()
  709. if key_meta:get_string("secret") == "" then
  710. local key_oldmeta = item:get_metadata()
  711. if key_oldmeta == "" or not minetest.parse_json(key_oldmeta) then
  712. return false
  713. end
  714. key_meta:set_string("secret", minetest.parse_json(key_oldmeta).secret)
  715. item:set_metadata("")
  716. end
  717. return meta:get_string("key_lock_secret") == key_meta:get_string("secret")
  718. end
  719. return false
  720. end