functions.lua 15 KB

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