functions.lua 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655
  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.12}
  38. table.dug = table.dug or
  39. {name = "default_sand_footstep", gain = 0.24}
  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.4}
  49. table.dug = table.dug or
  50. {name = "default_gravel_footstep", gain = 1.0}
  51. table.place = table.place or
  52. {name = "default_place_node", gain = 1.0}
  53. default.node_sound_defaults(table)
  54. return table
  55. end
  56. function default.node_sound_wood_defaults(table)
  57. table = table or {}
  58. table.footstep = table.footstep or
  59. {name = "default_wood_footstep", gain = 0.3}
  60. table.dug = table.dug or
  61. {name = "default_wood_footstep", gain = 1.0}
  62. default.node_sound_defaults(table)
  63. return table
  64. end
  65. function default.node_sound_leaves_defaults(table)
  66. table = table or {}
  67. table.footstep = table.footstep or
  68. {name = "default_grass_footstep", gain = 0.45}
  69. table.dug = table.dug or
  70. {name = "default_grass_footstep", gain = 0.7}
  71. table.place = table.place or
  72. {name = "default_place_node", gain = 1.0}
  73. default.node_sound_defaults(table)
  74. return table
  75. end
  76. function default.node_sound_glass_defaults(table)
  77. table = table or {}
  78. table.footstep = table.footstep or
  79. {name = "default_glass_footstep", gain = 0.3}
  80. table.dig = table.dig or
  81. {name = "default_glass_footstep", gain = 0.5}
  82. table.dug = table.dug or
  83. {name = "default_break_glass", gain = 1.0}
  84. default.node_sound_defaults(table)
  85. return table
  86. end
  87. function default.node_sound_metal_defaults(table)
  88. table = table or {}
  89. table.footstep = table.footstep or
  90. {name = "default_metal_footstep", gain = 0.4}
  91. table.dig = table.dig or
  92. {name = "default_dig_metal", gain = 0.5}
  93. table.dug = table.dug or
  94. {name = "default_dug_metal", gain = 0.5}
  95. table.place = table.place or
  96. {name = "default_place_node_metal", gain = 0.5}
  97. default.node_sound_defaults(table)
  98. return table
  99. end
  100. function default.node_sound_water_defaults(table)
  101. table = table or {}
  102. table.footstep = table.footstep or
  103. {name = "default_water_footstep", gain = 0.2}
  104. default.node_sound_defaults(table)
  105. return table
  106. end
  107. function default.node_sound_snow_defaults(table)
  108. table = table or {}
  109. table.footstep = table.footstep or
  110. {name = "default_snow_footstep", gain = 0.2}
  111. table.dig = table.dig or
  112. {name = "default_snow_footstep", gain = 0.3}
  113. table.dug = table.dug or
  114. {name = "default_snow_footstep", gain = 0.3}
  115. table.place = table.place or
  116. {name = "default_place_node", gain = 1.0}
  117. default.node_sound_defaults(table)
  118. return table
  119. end
  120. --
  121. -- Lavacooling
  122. --
  123. default.cool_lava = function(pos, node)
  124. if node.name == "default:lava_source" then
  125. minetest.set_node(pos, {name = "default:obsidian"})
  126. else -- Lava flowing
  127. minetest.set_node(pos, {name = "default:stone"})
  128. end
  129. minetest.sound_play("default_cool_lava",
  130. {pos = pos, max_hear_distance = 16, gain = 0.25})
  131. end
  132. if minetest.settings:get_bool("enable_lavacooling") ~= false then
  133. minetest.register_abm({
  134. label = "Lava cooling",
  135. nodenames = {"default:lava_source", "default:lava_flowing"},
  136. neighbors = {"group:cools_lava", "group:water"},
  137. interval = 2,
  138. chance = 2,
  139. catch_up = false,
  140. action = function(...)
  141. default.cool_lava(...)
  142. end,
  143. })
  144. end
  145. --
  146. -- Optimized helper to put all items in an inventory into a drops list
  147. --
  148. function default.get_inventory_drops(pos, inventory, drops)
  149. local inv = minetest.get_meta(pos):get_inventory()
  150. local n = #drops
  151. for i = 1, inv:get_size(inventory) do
  152. local stack = inv:get_stack(inventory, i)
  153. if stack:get_count() > 0 then
  154. drops[n+1] = stack:to_table()
  155. n = n + 1
  156. end
  157. end
  158. end
  159. --
  160. -- Papyrus and cactus growing
  161. --
  162. -- Wrapping the functions in ABM action is necessary to make overriding them possible
  163. function default.grow_cactus(pos, node)
  164. if node.param2 >= 4 then
  165. return
  166. end
  167. pos.y = pos.y - 1
  168. if minetest.get_item_group(minetest.get_node(pos).name, "sand") == 0 then
  169. return
  170. end
  171. pos.y = pos.y + 1
  172. local height = 0
  173. while node.name == "default:cactus" and height < 4 do
  174. height = height + 1
  175. pos.y = pos.y + 1
  176. node = minetest.get_node(pos)
  177. end
  178. if height == 4 or node.name ~= "air" then
  179. return
  180. end
  181. if minetest.get_node_light(pos) < 13 then
  182. return
  183. end
  184. minetest.set_node(pos, {name = "default:cactus"})
  185. return true
  186. end
  187. function default.grow_papyrus(pos, node)
  188. pos.y = pos.y - 1
  189. local name = minetest.get_node(pos).name
  190. if name ~= "default:dirt_with_grass" and name ~= "default:dirt" then
  191. return
  192. end
  193. if not minetest.find_node_near(pos, 3, {"group:water"}) then
  194. return
  195. end
  196. pos.y = pos.y + 1
  197. local height = 0
  198. while node.name == "default:papyrus" and height < 4 do
  199. height = height + 1
  200. pos.y = pos.y + 1
  201. node = minetest.get_node(pos)
  202. end
  203. if height == 4 or node.name ~= "air" then
  204. return
  205. end
  206. if minetest.get_node_light(pos) < 13 then
  207. return
  208. end
  209. minetest.set_node(pos, {name = "default:papyrus"})
  210. return true
  211. end
  212. minetest.register_abm({
  213. label = "Grow cactus",
  214. nodenames = {"default:cactus"},
  215. neighbors = {"group:sand"},
  216. interval = 12,
  217. chance = 83,
  218. action = function(...)
  219. default.grow_cactus(...)
  220. end
  221. })
  222. minetest.register_abm({
  223. label = "Grow papyrus",
  224. nodenames = {"default:papyrus"},
  225. neighbors = {"default:dirt", "default:dirt_with_grass"},
  226. interval = 14,
  227. chance = 71,
  228. action = function(...)
  229. default.grow_papyrus(...)
  230. end
  231. })
  232. --
  233. -- Dig upwards
  234. --
  235. function default.dig_up(pos, node, digger)
  236. if digger == nil then return end
  237. local np = {x = pos.x, y = pos.y + 1, z = pos.z}
  238. local nn = minetest.get_node(np)
  239. if nn.name == node.name then
  240. minetest.node_dig(np, nn, digger)
  241. end
  242. end
  243. --
  244. -- Fence registration helper
  245. --
  246. function default.register_fence(name, def)
  247. minetest.register_craft({
  248. output = name .. " 4",
  249. recipe = {
  250. { def.material, 'group:stick', def.material },
  251. { def.material, 'group:stick', def.material },
  252. }
  253. })
  254. local fence_texture = "default_fence_overlay.png^" .. def.texture ..
  255. "^default_fence_overlay.png^[makealpha:255,126,126"
  256. -- Allow almost everything to be overridden
  257. local default_fields = {
  258. paramtype = "light",
  259. drawtype = "nodebox",
  260. node_box = {
  261. type = "connected",
  262. fixed = {{-1/8, -1/2, -1/8, 1/8, 1/2, 1/8}},
  263. -- connect_top =
  264. -- connect_bottom =
  265. connect_front = {{-1/16,3/16,-1/2,1/16,5/16,-1/8},
  266. {-1/16,-5/16,-1/2,1/16,-3/16,-1/8}},
  267. connect_left = {{-1/2,3/16,-1/16,-1/8,5/16,1/16},
  268. {-1/2,-5/16,-1/16,-1/8,-3/16,1/16}},
  269. connect_back = {{-1/16,3/16,1/8,1/16,5/16,1/2},
  270. {-1/16,-5/16,1/8,1/16,-3/16,1/2}},
  271. connect_right = {{1/8,3/16,-1/16,1/2,5/16,1/16},
  272. {1/8,-5/16,-1/16,1/2,-3/16,1/16}},
  273. },
  274. connects_to = {"group:fence", "group:wood", "group:tree"},
  275. inventory_image = fence_texture,
  276. wield_image = fence_texture,
  277. tiles = {def.texture},
  278. sunlight_propagates = true,
  279. is_ground_content = false,
  280. groups = {},
  281. }
  282. for k, v in pairs(default_fields) do
  283. if def[k] == nil then
  284. def[k] = v
  285. end
  286. end
  287. -- Always add to the fence group, even if no group provided
  288. def.groups.fence = 1
  289. def.texture = nil
  290. def.material = nil
  291. minetest.register_node(name, def)
  292. end
  293. --
  294. -- Leafdecay
  295. --
  296. -- Prevent decay of placed leaves
  297. default.after_place_leaves = function(pos, placer, itemstack, pointed_thing)
  298. if placer and placer:is_player() and not placer:get_player_control().sneak then
  299. local node = minetest.get_node(pos)
  300. node.param2 = 1
  301. minetest.set_node(pos, node)
  302. end
  303. end
  304. -- Leafdecay
  305. -- for trunks
  306. local function leafdecay_after_destruct(pos, oldnode, trunk)
  307. local radius = default._leafdecay.radii[trunk]
  308. local leaves = default._leafdecay.by_trunk[trunk]
  309. for _, v in pairs(minetest.find_nodes_in_area(vector.subtract(pos, radius),
  310. vector.add(pos, radius), leaves)) do
  311. local node = minetest.get_node(v)
  312. local timer = minetest.get_node_timer(v)
  313. if node.param2 == 0 and not timer:is_started() then
  314. timer:start(math.random(20, 120) / 10)
  315. end
  316. end
  317. end
  318. -- for leaves
  319. local function leafdecay_on_timer(pos, leaf)
  320. local radius = default._leafdecay.radii[leaf]
  321. local trunks = default._leafdecay.by_leaf[leaf]
  322. if minetest.find_node_near(pos, radius, trunks) then
  323. return false
  324. end
  325. local node = minetest.get_node(pos)
  326. local drops = minetest.get_node_drops(node.name)
  327. for _, item in ipairs(drops) do
  328. local is_leaf
  329. if default._leafdecay.by_leaf[item] then
  330. is_leaf = true
  331. end
  332. if minetest.get_item_group(item, "leafdecay_drop") ~= 0 or
  333. not is_leaf then
  334. minetest.add_item({
  335. x = pos.x - 0.5 + math.random(),
  336. y = pos.y - 0.5 + math.random(),
  337. z = pos.z - 0.5 + math.random(),
  338. }, item)
  339. end
  340. end
  341. minetest.remove_node(pos)
  342. minetest.check_for_falling(pos)
  343. end
  344. default._leafdecay = {
  345. by_trunk = {},
  346. by_leaf = {},
  347. radii = {},
  348. }
  349. function concat(t1,t2)
  350. for i=1, #t2 do
  351. t1[#t1+1] = t2[i]
  352. end
  353. return t1
  354. end
  355. function default.register_leafdecay(def)
  356. assert(def.leaves)
  357. assert(def.trunks)
  358. assert(def.radius)
  359. for _, v in pairs(def.trunks) do
  360. -- only override the trunk once
  361. if default._leafdecay.by_trunk[v] == nil then
  362. minetest.override_item(v, {
  363. after_destruct = function(pos, oldnode)
  364. leafdecay_after_destruct(pos, oldnode, v)
  365. end,
  366. })
  367. default._leafdecay.radii[v] = def.radius
  368. default._leafdecay.by_trunk[v] = {}
  369. end
  370. concat(default._leafdecay.by_trunk[v], def.leaves)
  371. end
  372. for _, v in pairs(def.leaves) do
  373. -- only override each leaf once
  374. if default._leafdecay.by_leaf[v] == nil then
  375. minetest.override_item(v, {
  376. on_timer = function(pos)
  377. leafdecay_on_timer(pos, v)
  378. end,
  379. })
  380. default._leafdecay.radii[v] = def.radius
  381. default._leafdecay.by_leaf[v] = {}
  382. end
  383. concat(default._leafdecay.by_leaf[v], def.trunks)
  384. end
  385. end
  386. --
  387. -- Convert dirt to something that fits the environment
  388. --
  389. minetest.register_abm({
  390. label = "Grass spread",
  391. nodenames = {"default:dirt"},
  392. neighbors = {
  393. "air",
  394. "group:grass",
  395. "group:dry_grass",
  396. "default:snow",
  397. },
  398. interval = 6,
  399. chance = 50,
  400. catch_up = false,
  401. action = function(pos, node)
  402. -- Check for darkness: night, shadow or under a light-blocking node
  403. -- Returns if ignore above
  404. local above = {x = pos.x, y = pos.y + 1, z = pos.z}
  405. if (minetest.get_node_light(above) or 0) < 13 then
  406. return
  407. end
  408. -- Look for spreading dirt-type neighbours
  409. local p2 = minetest.find_node_near(pos, 1, "group:spreading_dirt_type")
  410. if p2 then
  411. local n3 = minetest.get_node(p2)
  412. minetest.set_node(pos, {name = n3.name})
  413. return
  414. end
  415. -- Else, any seeding nodes on top?
  416. local name = minetest.get_node(above).name
  417. -- Snow check is cheapest, so comes first
  418. if name == "default:snow" then
  419. minetest.set_node(pos, {name = "default:dirt_with_snow"})
  420. -- Most likely case first
  421. elseif minetest.get_item_group(name, "grass") ~= 0 then
  422. minetest.set_node(pos, {name = "default:dirt_with_grass"})
  423. elseif minetest.get_item_group(name, "dry_grass") ~= 0 then
  424. minetest.set_node(pos, {name = "default:dirt_with_dry_grass"})
  425. end
  426. end
  427. })
  428. --
  429. -- Grass and dry grass removed in darkness
  430. --
  431. minetest.register_abm({
  432. label = "Grass covered",
  433. nodenames = {"group:spreading_dirt_type"},
  434. interval = 8,
  435. chance = 50,
  436. catch_up = false,
  437. action = function(pos, node)
  438. local above = {x = pos.x, y = pos.y + 1, z = pos.z}
  439. local name = minetest.get_node(above).name
  440. local nodedef = minetest.registered_nodes[name]
  441. if name ~= "ignore" and nodedef and not ((nodedef.sunlight_propagates or
  442. nodedef.paramtype == "light") and
  443. nodedef.liquidtype == "none") then
  444. minetest.set_node(pos, {name = "default:dirt"})
  445. end
  446. end
  447. })
  448. --
  449. -- Moss growth on cobble near water
  450. --
  451. minetest.register_abm({
  452. label = "Moss growth",
  453. nodenames = {"default:cobble", "stairs:slab_cobble", "stairs:stair_cobble", "walls:cobble"},
  454. neighbors = {"group:water"},
  455. interval = 16,
  456. chance = 200,
  457. catch_up = false,
  458. action = function(pos, node)
  459. if node.name == "default:cobble" then
  460. minetest.set_node(pos, {name = "default:mossycobble"})
  461. elseif node.name == "stairs:slab_cobble" then
  462. minetest.set_node(pos, {name = "stairs:slab_mossycobble", param2 = node.param2})
  463. elseif node.name == "stairs:stair_cobble" then
  464. minetest.set_node(pos, {name = "stairs:stair_mossycobble", param2 = node.param2})
  465. elseif node.name == "walls:cobble" then
  466. minetest.set_node(pos, {name = "walls:mossycobble", param2 = node.param2})
  467. end
  468. end
  469. })
  470. --
  471. -- Checks if specified volume intersects a protected volume
  472. --
  473. function default.intersects_protection(minp, maxp, player_name, interval)
  474. -- 'interval' is the largest allowed interval for the 3D lattice of checks
  475. -- Compute the optimal float step 'd' for each axis so that all corners and
  476. -- borders are checked. 'd' will be smaller or equal to 'interval'.
  477. -- Subtracting 1e-4 ensures that the max co-ordinate will be reached by the
  478. -- for loop (which might otherwise not be the case due to rounding errors).
  479. local d = {}
  480. for _, c in pairs({"x", "y", "z"}) do
  481. if maxp[c] > minp[c] then
  482. d[c] = (maxp[c] - minp[c]) / math.ceil((maxp[c] - minp[c]) / interval) - 1e-4
  483. elseif maxp[c] == minp[c] then
  484. d[c] = 1 -- Any value larger than 0 to avoid division by zero
  485. else -- maxp[c] < minp[c], print error and treat as protection intersected
  486. minetest.log("error", "maxp < minp in 'default.intersects_protection()'")
  487. return true
  488. end
  489. end
  490. for zf = minp.z, maxp.z, d.z do
  491. local z = math.floor(zf + 0.5)
  492. for yf = minp.y, maxp.y, d.y do
  493. local y = math.floor(yf + 0.5)
  494. for xf = minp.x, maxp.x, d.x do
  495. local x = math.floor(xf + 0.5)
  496. if minetest.is_protected({x = x, y = y, z = z}, player_name) then
  497. return true
  498. end
  499. end
  500. end
  501. end
  502. return false
  503. end
  504. --
  505. -- Coral death near air
  506. --
  507. minetest.register_abm({
  508. nodenames = {"default:coral_brown", "default:coral_orange"},
  509. neighbors = {"air"},
  510. interval = 17,
  511. chance = 5,
  512. catch_up = false,
  513. action = function(pos, node)
  514. minetest.set_node(pos, {name = "default:coral_skeleton"})
  515. end,
  516. })
  517. --
  518. -- Berries growing on bushes
  519. --
  520. minetest.register_abm({
  521. nodenames = {"default:acacia_bush_leaves"},
  522. interval = 17,
  523. chance = 5,
  524. catch_up = true,
  525. action = function(pos, node)
  526. minetest.set_node(pos, {name = "default:acacia_bush_leaves_with_red_berries"})
  527. end,
  528. })
  529. minetest.register_abm({
  530. nodenames = {"default:acacia_bush_leaves"},
  531. interval = 17,
  532. chance = 5,
  533. catch_up = true,
  534. action = function(pos, node)
  535. minetest.set_node(pos, {name = "default:acacia_bush_leaves_with_orange_berries"})
  536. end,
  537. })
  538. --
  539. -- NOTICE: This method is not an official part of the API yet.
  540. -- This method may change in future.
  541. --
  542. function default.can_interact_with_node(player, pos)
  543. if player then
  544. if minetest.check_player_privs(player, "protection_bypass") then
  545. return true
  546. end
  547. else
  548. return false
  549. end
  550. local meta = minetest.get_meta(pos)
  551. local owner = meta:get_string("owner")
  552. if not owner or owner == "" or owner == player:get_player_name() then
  553. return true
  554. end
  555. -- Is player wielding the right key?
  556. local item = player:get_wielded_item()
  557. if item:get_name() == "default:key" then
  558. local key_meta = item:get_meta()
  559. if key_meta:get_string("secret") == "" then
  560. local key_oldmeta = item:get_metadata()
  561. if key_oldmeta == "" or not minetest.parse_json(key_oldmeta) then
  562. return false
  563. end
  564. key_meta:set_string("secret", minetest.parse_json(key_oldmeta).secret)
  565. item:set_metadata("")
  566. end
  567. return meta:get_string("key_lock_secret") == key_meta:get_string("secret")
  568. end
  569. return false
  570. end