functions.lua 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  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.5}
  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=1.0}
  28. table.dug = table.dug or
  29. {name="default_dirt_footstep", gain=1.5}
  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.5}
  39. table.dug = table.dug or
  40. {name="default_sand_footstep", gain=1.0}
  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.5}
  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.5}
  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.35}
  70. table.dug = table.dug or
  71. {name="default_grass_footstep", gain=0.85}
  72. table.dig = table.dig or
  73. {name="default_dig_crumbly", gain=0.4}
  74. table.place = table.place or
  75. {name="default_place_node", gain=1.0}
  76. default.node_sound_defaults(table)
  77. return table
  78. end
  79. function default.node_sound_glass_defaults(table)
  80. table = table or {}
  81. table.footstep = table.footstep 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. --
  89. -- Legacy
  90. --
  91. function default.spawn_falling_node(p, nodename)
  92. spawn_falling_node(p, nodename)
  93. end
  94. -- Horrible crap to support old code
  95. -- Don't use this and never do what this does, it's completely wrong!
  96. -- (More specifically, the client and the C++ code doesn't get the group)
  97. function default.register_falling_node(nodename, texture)
  98. minetest.log("error", debug.traceback())
  99. minetest.log('error', "WARNING: default.register_falling_node is deprecated")
  100. if minetest.registered_nodes[nodename] then
  101. minetest.registered_nodes[nodename].groups.falling_node = 1
  102. end
  103. end
  104. --
  105. -- Global callbacks
  106. --
  107. -- Global environment step function
  108. function on_step(dtime)
  109. -- print("on_step")
  110. end
  111. minetest.register_globalstep(on_step)
  112. function on_placenode(p, node)
  113. --print("on_placenode")
  114. end
  115. minetest.register_on_placenode(on_placenode)
  116. function on_dignode(p, node)
  117. --print("on_dignode")
  118. end
  119. minetest.register_on_dignode(on_dignode)
  120. function on_punchnode(p, node)
  121. end
  122. minetest.register_on_punchnode(on_punchnode)
  123. --
  124. -- Grow trees
  125. --
  126. minetest.register_abm({
  127. nodenames = {"default:sapling"},
  128. interval = 10,
  129. chance = 50,
  130. action = function(pos, node)
  131. local nu = minetest.get_node({x=pos.x, y=pos.y-1, z=pos.z}).name
  132. local is_soil = minetest.get_item_group(nu, "soil")
  133. if is_soil == 0 then
  134. return
  135. end
  136. minetest.log("action", "A sapling grows into a tree at "..minetest.pos_to_string(pos))
  137. local vm = minetest.get_voxel_manip()
  138. local minp, maxp = vm:read_from_map({x=pos.x-16, y=pos.y, z=pos.z-16}, {x=pos.x+16, y=pos.y+16, z=pos.z+16})
  139. local a = VoxelArea:new{MinEdge=minp, MaxEdge=maxp}
  140. local data = vm:get_data()
  141. default.grow_tree(data, a, pos, math.random(1, 4) == 1, math.random(1,100000))
  142. vm:set_data(data)
  143. vm:write_to_map(data)
  144. vm:update_map()
  145. end
  146. })
  147. minetest.register_abm({
  148. nodenames = {"default:junglesapling"},
  149. interval = 10,
  150. chance = 50,
  151. action = function(pos, node)
  152. local nu = minetest.get_node({x=pos.x, y=pos.y-1, z=pos.z}).name
  153. local is_soil = minetest.get_item_group(nu, "soil")
  154. if is_soil == 0 then
  155. return
  156. end
  157. minetest.log("action", "A jungle sapling grows into a tree at "..minetest.pos_to_string(pos))
  158. local vm = minetest.get_voxel_manip()
  159. local minp, maxp = vm:read_from_map({x=pos.x-16, y=pos.y-1, z=pos.z-16}, {x=pos.x+16, y=pos.y+16, z=pos.z+16})
  160. local a = VoxelArea:new{MinEdge=minp, MaxEdge=maxp}
  161. local data = vm:get_data()
  162. default.grow_jungletree(data, a, pos, math.random(1,100000))
  163. vm:set_data(data)
  164. vm:write_to_map(data)
  165. vm:update_map()
  166. end
  167. })
  168. --
  169. -- Lavacooling
  170. --
  171. default.cool_lava_source = function(pos)
  172. minetest.set_node(pos, {name="default:obsidian"})
  173. minetest.sound_play("default_cool_lava", {pos = pos, gain = 0.25})
  174. end
  175. default.cool_lava_flowing = function(pos)
  176. minetest.set_node(pos, {name="default:stone"})
  177. minetest.sound_play("default_cool_lava", {pos = pos, gain = 0.25})
  178. end
  179. minetest.register_abm({
  180. nodenames = {"default:lava_flowing"},
  181. neighbors = {"group:water"},
  182. interval = 1,
  183. chance = 1,
  184. action = function(pos, node, active_object_count, active_object_count_wider)
  185. default.cool_lava_flowing(pos, node, active_object_count, active_object_count_wider)
  186. end,
  187. })
  188. minetest.register_abm({
  189. nodenames = {"default:lava_source"},
  190. neighbors = {"group:water"},
  191. interval = 1,
  192. chance = 1,
  193. action = function(pos, node, active_object_count, active_object_count_wider)
  194. default.cool_lava_source(pos, node, active_object_count, active_object_count_wider)
  195. end,
  196. })
  197. --
  198. -- Papyrus and cactus growing
  199. --
  200. minetest.register_abm({
  201. nodenames = {"default:cactus"},
  202. neighbors = {"group:sand"},
  203. interval = 50,
  204. chance = 20,
  205. action = function(pos, node)
  206. pos.y = pos.y-1
  207. local name = minetest.get_node(pos).name
  208. if minetest.get_item_group(name, "sand") ~= 0 then
  209. pos.y = pos.y+1
  210. local height = 0
  211. while minetest.get_node(pos).name == "default:cactus" and height < 4 do
  212. height = height+1
  213. pos.y = pos.y+1
  214. end
  215. local light_level = minetest.get_node_light(pos)
  216. if not light_level then
  217. return
  218. end
  219. local c = math.ceil(2 * (light_level - 13) ^ 2 + 1)
  220. if light_level > 7 and height < 4 and
  221. (math.random(1, c) == 1 or light_level >= 13) then
  222. if minetest.get_node(pos).name == "air" then
  223. minetest.set_node(pos, {name="default:cactus"})
  224. end
  225. end
  226. end
  227. end,
  228. })
  229. minetest.register_abm({
  230. nodenames = {"default:papyrus"},
  231. neighbors = {"group:soil", "group:sand"},
  232. interval = 50,
  233. chance = 20,
  234. action = function(pos, node)
  235. pos.y = pos.y-1
  236. local name = minetest.get_node(pos).name
  237. if minetest.get_item_group(name, "soil") ~= 0
  238. or minetest.get_item_group(name, "sand") ~= 0 then
  239. if minetest.find_node_near(pos, 3, {"group:water"}) == nil then
  240. return
  241. end
  242. pos.y = pos.y+1
  243. local height = 0
  244. while minetest.get_node(pos).name == "default:papyrus" and height < 4 do
  245. height = height+1
  246. pos.y = pos.y+1
  247. end
  248. local light_level = minetest.get_node_light(pos)
  249. if not light_level then
  250. return
  251. end
  252. local c = math.ceil(2 * (light_level - 13) ^ 2 + 1)
  253. if light_level > 7 and height < 4 and
  254. (math.random(1, c) == 1 or light_level >= 13) then
  255. if minetest.get_node(pos).name == "air" then
  256. minetest.set_node(pos, {name="default:papyrus", param2 = 3})
  257. end
  258. end
  259. end
  260. end,
  261. })
  262. function default.dig_up(pos, node, digger)
  263. if digger == nil then return end
  264. local np = {x = pos.x, y = pos.y + 1, z = pos.z}
  265. local nn = minetest.get_node(np)
  266. if nn.name == node.name then
  267. minetest.node_dig(np, nn, digger)
  268. end
  269. end
  270. --
  271. -- Leafdecay
  272. --
  273. -- To enable leaf decay for a node, add it to the "leafdecay" group.
  274. --
  275. -- The rating of the group determines how far from a node in the group "tree"
  276. -- the node can be without decaying.
  277. --
  278. -- If param2 of the node is ~= 0, the node will always be preserved. Thus, if
  279. -- the player places a node of that kind, you will want to set param2=1 or so.
  280. --
  281. -- If the node is in the leafdecay_drop group then the it will always be dropped
  282. -- as an item
  283. default.leafdecay_trunk_cache = {}
  284. default.leafdecay_enable_cache = true
  285. -- Spread the load of finding trunks
  286. default.leafdecay_trunk_find_allow_accumulator = 0
  287. minetest.register_globalstep(function(dtime)
  288. local finds_per_second = 5000
  289. default.leafdecay_trunk_find_allow_accumulator =
  290. math.floor(dtime * finds_per_second)
  291. end)
  292. minetest.register_abm({
  293. nodenames = {"group:leafdecay"},
  294. neighbors = {"air", "group:liquid"},
  295. -- A low interval and a high inverse chance spreads the load
  296. interval = 2,
  297. chance = 5,
  298. action = function(p0, node, _, _)
  299. --print("leafdecay ABM at "..p0.x..", "..p0.y..", "..p0.z..")")
  300. local do_preserve = false
  301. local d = minetest.registered_nodes[node.name].groups.leafdecay
  302. if not d or d == 0 then
  303. --print("not groups.leafdecay")
  304. return
  305. end
  306. local n0 = minetest.get_node(p0)
  307. if n0.param2 ~= 0 then
  308. --print("param2 ~= 0")
  309. return
  310. end
  311. local p0_hash = nil
  312. if default.leafdecay_enable_cache then
  313. p0_hash = minetest.hash_node_position(p0)
  314. local trunkp = default.leafdecay_trunk_cache[p0_hash]
  315. if trunkp then
  316. local n = minetest.get_node(trunkp)
  317. local reg = minetest.registered_nodes[n.name]
  318. -- Assume ignore is a trunk, to make the thing work at the border of the active area
  319. if n.name == "ignore" or (reg and reg.groups.tree and reg.groups.tree ~= 0) then
  320. --print("cached trunk still exists")
  321. return
  322. end
  323. --print("cached trunk is invalid")
  324. -- Cache is invalid
  325. table.remove(default.leafdecay_trunk_cache, p0_hash)
  326. end
  327. end
  328. if default.leafdecay_trunk_find_allow_accumulator <= 0 then
  329. return
  330. end
  331. default.leafdecay_trunk_find_allow_accumulator =
  332. default.leafdecay_trunk_find_allow_accumulator - 1
  333. -- Assume ignore is a trunk, to make the thing work at the border of the active area
  334. local p1 = minetest.find_node_near(p0, d, {"ignore", "group:tree"})
  335. if p1 then
  336. do_preserve = true
  337. if default.leafdecay_enable_cache then
  338. --print("caching trunk")
  339. -- Cache the trunk
  340. default.leafdecay_trunk_cache[p0_hash] = p1
  341. end
  342. end
  343. if not do_preserve then
  344. -- Drop stuff other than the node itself
  345. local itemstacks = minetest.get_node_drops(n0.name)
  346. for _, itemname in ipairs(itemstacks) do
  347. if minetest.get_item_group(n0.name, "leafdecay_drop") ~= 0 or
  348. itemname ~= n0.name then
  349. local p_drop = {
  350. x = p0.x - 0.5 + math.random(),
  351. y = p0.y - 0.5 + math.random(),
  352. z = p0.z - 0.5 + math.random(),
  353. }
  354. minetest.add_item(p_drop, itemname)
  355. end
  356. end
  357. -- Remove node
  358. minetest.remove_node(p0)
  359. minetest.check_for_falling(p0)
  360. end
  361. end
  362. })
  363. --This allows trees act *almost* like falling nodes, useful for big trees!
  364. local falling_trees = minetest.setting_getbool("falling_trees")
  365. if not falling_trees then
  366. if minetest.is_singleplayer() then
  367. falling_trees = false
  368. else
  369. falling_trees = true
  370. end
  371. end
  372. if falling_trees == true then
  373. function default.dig_tree(pos, node, name, digger, height, radius)
  374. minetest.node_dig(pos, node, digger)
  375. local base_y = pos.y
  376. for i = 1, (height + 5) do
  377. pos.y = base_y + i
  378. local node = minetest.get_node(pos)
  379. if node.name ~= name or i == (height + 5) then
  380. minetest.remove_node({x = pos.x, y = pos.y-1, z = pos.z})
  381. for k = -radius, radius do
  382. for l = -radius, radius do
  383. for j = 0, 1 do
  384. local tree_bellow = minetest.get_node({x = pos.x+k, y = pos.y-1, z = pos.z+l})
  385. if tree_bellow.name ~= name then
  386. local pos1 = {x = pos.x+k, y = pos.y+j, z = pos.z+l}
  387. if minetest.get_node(pos1).name == name then
  388. minetest.spawn_item(pos1, name)
  389. minetest.remove_node(pos1)
  390. end
  391. end
  392. end
  393. end
  394. end
  395. return
  396. elseif node.name == name then
  397. minetest.set_node({x = pos.x, y = pos.y-1, z = pos.z}, {name = name})
  398. end
  399. end
  400. end
  401. else
  402. function default.dig_tree(pos, node, name, digger, height, radius)
  403. minetest.node_dig(pos, node, digger)
  404. return
  405. end
  406. end
  407. minetest.register_on_punchplayer(function(player, hitter, time_from_last_punch, tool_capabilities)
  408. local weapon = hitter:get_wielded_item()
  409. if tool_capabilities ~= nil then
  410. local wear = ((tool_capabilities.full_punch_interval or 1.4) / 75 ) * 9000
  411. weapon:add_wear(wear)
  412. hitter:set_wielded_item(weapon)
  413. end
  414. end)
  415. local dbuf = {}
  416. function default.explode(pos, time, radius, damage, node)
  417. minetest.after(time, function(pos)
  418. if node then
  419. if minetest.get_node(pos).name ~= node then
  420. return
  421. end
  422. end
  423. minetest.sound_play("default_explode", {pos=pos, gain=1.5, max_hear_distance=2*64})
  424. local objects = minetest.get_objects_inside_radius(pos, radius * 2)
  425. for _,obj in ipairs(objects) do
  426. if obj:is_player() or (obj:get_luaentity() and obj:get_luaentity().name ~= "__builtin:item") then
  427. local obj_p = obj:getpos()
  428. local vec = {x=obj_p.x-pos.x, y=obj_p.y-pos.y, z=obj_p.z-pos.z}
  429. local dist = (vec.x^2+vec.y^2+vec.z^2)^0.5
  430. local damage = damage or (80*0.5^dist)*2
  431. obj:punch(obj, 1.0, {
  432. full_punch_interval=1.0,
  433. damage_groups={fleshy=damage},
  434. }, vec)
  435. end
  436. end
  437. local posf = vector.floor(pos)
  438. local pos_min = vector.add(posf, -(radius + 10))
  439. local pos_max = vector.add(posf, radius + 10)
  440. if not minetest.is_area_protected(pos_min, pos_max, "", 7) then
  441. local x, y, z = posf.x, posf.y, posf.z
  442. local c_air = minetest.get_content_id("air")
  443. local c_fire = minetest.get_content_id("fire:basic_flame")
  444. local vm = minetest.get_voxel_manip()
  445. local emin, emax = vm:read_from_map(pos_min, pos_max)
  446. local area = VoxelArea:new{MinEdge=emin, MaxEdge=emax}
  447. local data = vm:get_data(dbuf)
  448. local pr = PseudoRandom(os.time())
  449. for dx = -radius, radius do
  450. for dy = radius, -radius, -1 do
  451. for dz = -radius, radius do
  452. local vi = area:index(x + dx, y + dy, z + dz)
  453. local r = vector.length(vector.new(dx, dy, dz))
  454. if (radius * radius) / (r * r) >= (pr:next(80, 125) / 100) then
  455. if math.random(30) == 1 then
  456. data[vi] = c_fire
  457. else
  458. data[vi] = c_air
  459. end
  460. end
  461. end
  462. end
  463. end
  464. vm:set_data(data)
  465. vm:update_liquids()
  466. vm:write_to_map(true)
  467. end
  468. minetest.add_particlespawner(
  469. 100,
  470. 0.1,
  471. {x=pos.x-3, y=pos.y-3, z=pos.z-3},
  472. {x=pos.x+3, y=pos.y+3, z=pos.z+3},
  473. {x=-0, y=-0, z=-0},
  474. {x=0, y=0, z=0},
  475. {x=-0.5,y=5,z=-0.5},
  476. {x=0.5,y=5,z=0.5},
  477. 0.1,
  478. 1,
  479. 8,
  480. 15,
  481. false,
  482. "tnt_smoke.png"
  483. )
  484. end, pos)
  485. end