init.lua 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. default = default or {}
  2. coresounds = coresounds or {}
  3. coresounds.modpath = minetest.get_modpath("coresounds")
  4. -- Currently used by both death and teleport-sickness code.
  5. function coresounds.play_death_sound(player, pname)
  6. if skins.get_player_sex(pname) == "female" then
  7. ambiance.sound_play("death_female", player:get_pos(), 1.0, 30)
  8. else
  9. ambiance.sound_play("hungry_games_death", player:get_pos(), 1.0, 30)
  10. end
  11. end
  12. function coresounds.play_sound_node_place(pos, nn)
  13. local def = minetest.reg_ns_nodes[nn] or minetest.registered_nodes[nn]
  14. if not def then return end
  15. if def and def.sounds and def.sounds.place then
  16. local sound = def.sounds.place
  17. if not sound.name or not sound.gain then return end
  18. ambiance.sound_play(sound.name, pos, sound.gain, 20)
  19. end
  20. end
  21. function coresounds.play_sound_node_dug(pos, nn)
  22. local def = minetest.reg_ns_nodes[nn] or minetest.registered_nodes[nn]
  23. if not def then return end
  24. if def and def.sounds and def.sounds.dug then
  25. local sound = def.sounds.dug
  26. if not sound.name or not sound.gain then return end
  27. ambiance.sound_play(sound.name, pos, sound.gain, 20)
  28. end
  29. end
  30. function default.node_sound_defaults(table)
  31. table = table or {}
  32. table.footstep = table.footstep or {name = "", gain = 1.0}
  33. table.dug = table.dug or {name = "default_dug_node", gain = 0.25}
  34. table.place = table.place or {name = "default_place_node_hard", gain = 1.0}
  35. return table
  36. end
  37. function default.node_sound_stone_defaults(table)
  38. table = table or {}
  39. table.footstep = table.footstep or {name = "default_hard_footstep", gain = 0.5}
  40. table.dug = table.dug or {name = "default_hard_footstep", gain = 1.0}
  41. default.node_sound_defaults(table)
  42. return table
  43. end
  44. function default.node_sound_dirt_defaults(table)
  45. table = table or {}
  46. table.footstep = table.footstep or {name = "default_dirt_footstep", gain = 1.0}
  47. table.dug = table.dug or {name = "default_dirt_footstep", gain = 1.5}
  48. table.place = table.place or {name = "default_place_node", gain = 1.0}
  49. default.node_sound_defaults(table)
  50. return table
  51. end
  52. function default.node_sound_sand_defaults(table)
  53. table = table or {}
  54. table.footstep = table.footstep or {name = "default_sand_footstep", gain = 0.12}
  55. table.dug = table.dug or {name = "default_sand_footstep", gain = 0.24}
  56. table.place = table.place or {name = "default_place_node", gain = 1.0}
  57. default.node_sound_defaults(table)
  58. return table
  59. end
  60. function default.node_sound_gravel_defaults(table)
  61. table = table or {}
  62. table.footstep = table.footstep or {name = "default_gravel_footstep", gain = 0.5}
  63. table.dug = table.dug or {name = "default_gravel_footstep", gain = 1.0}
  64. table.place = table.place or {name = "default_place_node", gain = 1.0}
  65. default.node_sound_defaults(table)
  66. return table
  67. end
  68. function default.node_sound_wood_defaults(table)
  69. table = table or {}
  70. table.footstep = table.footstep or {name = "default_wood_footstep", gain = 0.5}
  71. table.dug = table.dug or {name = "default_wood_footstep", gain = 1.0}
  72. default.node_sound_defaults(table)
  73. return table
  74. end
  75. function default.node_sound_leaves_defaults(table)
  76. table = table or {}
  77. table.footstep = table.footstep or {name = "default_grass_footstep", gain = 0.35}
  78. table.dug = table.dug or {name = "default_grass_footstep", gain = 0.7}
  79. table.dig = table.dig or {name = "default_dig_snappy", gain = 0.4}
  80. table.place = table.place or {name = "default_place_node", gain = 1.0}
  81. default.node_sound_defaults(table)
  82. return table
  83. end
  84. function default.node_sound_glass_defaults(table)
  85. table = table or {}
  86. table.footstep = table.footstep or {name = "default_glass_footstep", gain = 0.5}
  87. table.dug = table.dug or {name = "default_break_glass", gain = 1.0}
  88. default.node_sound_defaults(table)
  89. return table
  90. end
  91. function default.node_sound_metal_defaults(table)
  92. table = table or {}
  93. table.footstep = table.footstep or {name = "default_metal_footstep", gain = 0.5}
  94. table.dig = table.dig or {name = "default_dig_metal", gain = 0.5}
  95. table.dug = table.dug or {name = "default_dug_metal", gain = 0.5}
  96. table.place = table.place or {name = "default_place_node_metal", gain = 0.5}
  97. default.node_sound_defaults(table)
  98. return table
  99. end
  100. function default.node_sound_snow_defaults(table)
  101. table = table or {}
  102. table.footstep = table.footstep or
  103. {name = "default_snow_footstep", gain = 0.2}
  104. table.dig = table.dig or
  105. {name = "default_snow_footstep", gain = 0.3}
  106. table.dug = table.dug or
  107. {name = "default_snow_footstep", gain = 0.3}
  108. table.place = table.place or
  109. {name = "default_place_node", gain = 1.0}
  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 {name = "default_water_footstep", gain = 0.2}
  116. default.node_sound_defaults(table)
  117. return table
  118. end
  119. minetest.register_on_dignode(function(pos, oldnode, digger)
  120. if not digger then return end
  121. if not digger:is_player() then return end
  122. local n = minetest.reg_ns_nodes[oldnode.name] or
  123. minetest.registered_nodes[oldnode.name]
  124. if not n then return end
  125. if n.sounds then
  126. local file = ""
  127. local gain = 1.0
  128. if n.sounds.dug then
  129. local s = n.sounds.dug
  130. file = s.name
  131. gain = s.gain
  132. elseif n.sounds.dig then
  133. local s = n.sounds.dig
  134. file = s.name
  135. gain = s.gain
  136. elseif n.sounds.footstep then
  137. local s = n.sounds.footstep
  138. file = s.name
  139. gain = s.gain
  140. end
  141. if file ~= "" then
  142. ambiance.sound_play(file, pos, gain, 20, digger:get_player_name())
  143. end
  144. end
  145. end)
  146. minetest.register_on_placenode(function(pos, oldnode, digger)
  147. if not digger then return end
  148. if not digger:is_player() then return end
  149. local n = minetest.reg_ns_nodes[oldnode.name] or
  150. minetest.registered_nodes[oldnode.name]
  151. if not n then return end
  152. if n.sounds then
  153. local file = ""
  154. local gain = 1.0
  155. if n.sounds.place then
  156. local s = n.sounds.place
  157. file = s.name
  158. gain = s.gain
  159. elseif n.sounds.dig then
  160. local s = n.sounds.dig
  161. file = s.name
  162. gain = s.gain
  163. elseif n.sounds.footstep then
  164. local s = n.sounds.footstep
  165. file = s.name
  166. gain = s.gain
  167. end
  168. if file ~= "" then
  169. ambiance.sound_play(file, pos, gain, 30, digger:get_player_name())
  170. end
  171. end
  172. end)
  173. local random = math.random
  174. minetest.register_on_punchnode(function(pos, node, puncher, pointed_thing)
  175. if not puncher then return end
  176. if not puncher:is_player() then return end
  177. if random(1, 5) == 1 then
  178. ambiance.particles_on_punch(pos, node)
  179. end
  180. local ndef = minetest.registered_items[node.name]
  181. if not ndef then return end
  182. local ngroups = ndef.groups or {}
  183. local nsounds = ndef.sounds or {}
  184. local file = ""
  185. local gain = 1.0
  186. -- By default, the punch sound is the sound the tool makes, if the node is diggable by the tool.
  187. local wielded = puncher:get_wielded_item()
  188. local wieldef = minetest.registered_items[wielded:get_name()]
  189. if wieldef then
  190. local tool_capabilities = wieldef.tool_capabilities
  191. if tool_capabilities then
  192. local groupcaps = tool_capabilities.groupcaps
  193. if groupcaps then
  194. if groupcaps.oddly_breakable_by_hand then
  195. if ngroups.oddly_breakable_by_hand and ngroups.oddly_breakable_by_hand > 0 then
  196. file = "default_dig_oddly_breakable_by_hand"
  197. -- Node digging sounds (if defined) override tool digging sounds.
  198. if nsounds.dig then
  199. file = nsounds.dig.name
  200. gain = nsounds.dig.gain
  201. end
  202. end
  203. elseif groupcaps.cracky then
  204. if ngroups.cracky and ngroups.cracky > 0 then
  205. file = "default_dig_cracky"
  206. -- Node digging sounds (if defined) override tool digging sounds.
  207. if nsounds.dig then
  208. file = nsounds.dig.name
  209. gain = nsounds.dig.gain
  210. end
  211. end
  212. elseif groupcaps.choppy then
  213. if ngroups.choppy and ngroups.choppy > 0 then
  214. file = "default_dig_choppy"
  215. -- Node digging sounds (if defined) override tool digging sounds.
  216. if nsounds.dig then
  217. file = nsounds.dig.name
  218. gain = nsounds.dig.gain
  219. end
  220. end
  221. elseif groupcaps.crumbly then
  222. if ngroups.crumbly and ngroups.crumbly > 0 then
  223. file = "default_dig_crumbly"
  224. -- Node digging sounds (if defined) override tool digging sounds.
  225. if nsounds.dig then
  226. file = nsounds.dig.name
  227. gain = nsounds.dig.gain
  228. end
  229. end
  230. elseif groupcaps.snappy then
  231. if ngroups.snappy and ngroups.snappy > 0 then
  232. file = "default_dig_snappy"
  233. -- Node digging sounds (if defined) override tool digging sounds.
  234. if nsounds.dig then
  235. file = nsounds.dig.name
  236. gain = nsounds.dig.gain
  237. end
  238. end
  239. end
  240. end
  241. else
  242. -- If the wielded item has no tool capabilities defined, assume it is the wieldhand.
  243. if ngroups.oddly_breakable_by_hand and ngroups.oddly_breakable_by_hand > 0 then
  244. file = "default_dig_oddly_breakable_by_hand"
  245. -- Node digging sounds (if defined) override tool digging sounds.
  246. if nsounds.dig then
  247. file = nsounds.dig.name
  248. gain = nsounds.dig.gain
  249. end
  250. end
  251. end
  252. else
  253. -- If the wielded item does not appear to be defined, assume it is the wieldhand.
  254. if ngroups.oddly_breakable_by_hand and ngroups.oddly_breakable_by_hand > 0 then
  255. file = "default_dig_oddly_breakable_by_hand"
  256. -- Node digging sounds (if defined) override tool digging sounds.
  257. if nsounds.dig then
  258. file = nsounds.dig.name
  259. gain = nsounds.dig.gain
  260. end
  261. end
  262. end
  263. -- If the node is breakable by hand, and no sound has been defined yet,
  264. -- then just use the `oddly_breakable_by_hand` digging sound. This catches
  265. -- cases where the player uses an incorrect tool for a given node, but the
  266. -- node is still diggable by hand (the server allows the node to be dug).
  267. if file == "" and ngroups.oddly_breakable_by_hand and ngroups.oddly_breakable_by_hand > 0 then
  268. file = "default_dig_oddly_breakable_by_hand"
  269. elseif file == "" and ngroups.crumbly and ngroups.crumbly > 0 then
  270. file = "default_dig_crumbly"
  271. elseif file == "" and ngroups.snappy and ngroups.snappy > 0 then
  272. file = "default_dig_snappy"
  273. end
  274. if file ~= "" then
  275. local pname = puncher:get_player_name()
  276. ambiance.sound_play(file, pos, gain, 30, pname)
  277. end
  278. end)