init.lua 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. --[[
  2. death_messages - A Minetest mod which sends a chat message when a player dies.
  3. Copyright (C) 2016 EvergreenTree
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. --]]
  15. --Carbone death coords
  16. --License of media (textures and sounds) From carbone subgame
  17. --------------------------------------
  18. --mods/default/sounds/player_death.ogg: from OpenArena – GNU GPL v2.
  19. -----------------------------------------------------------------------------------------------
  20. local title = "Death Messages"
  21. local version = "0.1.5"
  22. local mname = "death_messages"
  23. -----------------------------------------------------------------------------------------------
  24. dofile(minetest.get_modpath("death_messages").."/settings.txt")
  25. -----------------------------------------------------------------------------------------------
  26. local LANG = minetest.settings:get("language")
  27. if not (LANG and (LANG ~= "")) then LANG = "en" end
  28. -- check if stamina and/or hbhunger is used and death may occured by exhausting
  29. local mstamina = minetest.get_modpath("stamina")
  30. local mhbhunger = minetest.get_modpath("hbhunger")
  31. local lstamina = 100
  32. -- check if thirsty is used and death may occured by exhausting
  33. local mthirsty = minetest.get_modpath("thirsty")
  34. local lthirsty = 100
  35. local msunburn = minetest.get_modpath("sunburn")
  36. local lsunburn = 0
  37. -- A table of quips for death messages. The first item in each sub table is the
  38. -- default message used when RANDOM_MESSAGES is disabled.
  39. local messages = {}
  40. local swas = { en = " was ", de = " war " }
  41. local sby = { en = " by ", de = " von " }
  42. local swith = {en = " with ", de = " mit " }
  43. -- Default messages
  44. -- Read messages per language file
  45. for _,lan in ipairs({"en","de"}) do
  46. -- local lan=tlan:gsub(".txt",""):gsub("./","")
  47. local infile=minetest.get_modpath("death_messages").."/"..lan..".txt"
  48. local file = io.open(infile, "r")
  49. for line in file:lines() do
  50. if line:sub(1,1) ~= "#" then -- lines starting with # are handled as comment
  51. local attribs = line:gsub("\r",""):split(",",true)
  52. if #attribs>1 then
  53. if messages[attribs[1]]==nil then
  54. messages[attribs[1]]={}
  55. end
  56. if messages[attribs[1]][lan] == nil then
  57. messages[attribs[1]][lan] = {}
  58. end
  59. table.insert(messages[attribs[1]][lan],attribs[2])
  60. end
  61. end
  62. end
  63. end
  64. local function get_message(mtype)
  65. if mtype==nil then mtype="default" end
  66. if RANDOM_MESSAGES then
  67. local lmessages=messages[mtype][LANG]
  68. return lmessages[math.random(1, #lmessages)]
  69. else
  70. return messages[1] -- 1 is the index for the non-random message
  71. end
  72. end
  73. local function get_int_attribute(player, key)
  74. local level = player:get_attribute(key)
  75. if level then
  76. return tonumber(level)
  77. else
  78. return nil
  79. end
  80. end
  81. local function guess_reason(player)
  82. local node = minetest.registered_nodes[minetest.get_node(player:getpos()).name]
  83. local pos = player:getpos()
  84. local reas_meas="default"
  85. local lstamina = 10
  86. local lthirsty = 10
  87. local lsunburn = 0
  88. local lhunger = 0
  89. pos.x = math.floor(pos.x + 0.5)
  90. pos.y = math.floor(pos.y + 0.5)
  91. pos.z = math.floor(pos.z + 0.5)
  92. local param2 = minetest.dir_to_facedir(player:get_look_dir())
  93. if mstamina ~= nil then
  94. lstamina = get_int_attribute(player, "stamina:level")
  95. end
  96. if mhbhunger ~= nil then
  97. lhunger = tonumber(hbhunger.hunger[player_name])
  98. end
  99. if mthirsty ~= nil then
  100. lthirsty = thirsty.get_thirst_factor(player)
  101. end
  102. if msunburn ~= nil then
  103. lsunburn = sunburn.get_sunburn(player)
  104. end
  105. -- Death by lava
  106. if node.name == "default:lava_source" then
  107. reas_meas="lava"
  108. elseif node.name == "default:lava_flowing" then
  109. reas_meas="lava"
  110. -- Death by drowning
  111. elseif player:get_breath() == 0 then
  112. reas_meas="drown"
  113. -- Death by fire
  114. elseif node.name == "fire:basic_flame" then
  115. reas_meas="fire"
  116. -- Death by Toxic water
  117. elseif node.name == "es:toxic_water_source" then
  118. reas_meas="toxic"
  119. elseif node.name == "es:toxic_water_flowing" then
  120. reas_meas="toxic"
  121. elseif node.name == "groups:radioactive" then
  122. reas_meas="toxic"
  123. elseif lthirsty <= 1 then
  124. reas_meas="thirst"
  125. elseif lhunger <= 1 then
  126. reas_meas="hunger"
  127. elseif lstamina <= 1 then
  128. reas_meas="exhausted"
  129. elseif lsunburn >= 19 then
  130. reas_meas="sunburn"
  131. -- Death by something else
  132. else
  133. reas_meas="default"
  134. end
  135. end
  136. minetest.register_on_dieplayer(function(player,reason)
  137. local reas_meas="default"
  138. local player_name = player:get_player_name()
  139. if minetest.is_singleplayer() then
  140. player_name = "You"
  141. end
  142. local node = minetest.registered_nodes[minetest.get_node(player:getpos()).name]
  143. local pos = player:getpos()
  144. minetest.sound_play("player_death", {pos = pos, gain = 1})
  145. if reason == nil then
  146. reas_meas=guess_reason(player)
  147. else
  148. print(dump2(reason))
  149. if reason.type ~= nil then
  150. if (reason.type == "fall") or (reason.type == "punch") or (reason.type == "drawn") then
  151. reas_mean = reason.type
  152. else
  153. reas_mean=guess_reason(player)
  154. end
  155. else
  156. if (#reason > 1) then
  157. reas_meas="default"
  158. else
  159. reas_meas="default"
  160. end
  161. end
  162. end
  163. minetest.chat_send_all(string.char(0x1b).."(c@#00CED1)"..player_name..string.char(0x1b).."(c@#ff0000)"..get_message(reas_meas))
  164. end)
  165. --bigfoot code
  166. -- bigfoot547's death messages
  167. -- hacked by maikerumine
  168. -- get tool/item when hitting get_name() returns item name (e.g. "default:stone")
  169. minetest.register_on_punchplayer(function(player, hitter)
  170. local pos = player:getpos()
  171. local death = {x=0, y=23, z=-1.5}
  172. if not (player or hitter) then
  173. return false
  174. end
  175. if not hitter:get_player_name() == "" then
  176. return false
  177. end
  178. minetest.after(0, function(holding)
  179. if player:get_hp() == 0 and hitter:get_player_name() ~= "" and holding == hitter:get_wielded_item() ~= "" then
  180. local holding = hitter:get_wielded_item()
  181. if holding:to_string() ~= "" then
  182. local weap = holding:get_name(holding:get_name())
  183. if holding then
  184. minetest.chat_send_all(
  185. string.char(0x1b).."(c@#00CED1)"..player:get_player_name()..
  186. string.char(0x1b).."(c@#ff0000)"..swas[LANG]..
  187. string.char(0x1b).."(c@#ff0000)"..get_message("pvp")..
  188. string.char(0x1b).."(c@#ff0000)"..sby[LANG]..
  189. string.char(0x1b).."(c@#00CED1)"..hitter:get_player_name()..
  190. string.char(0x1b).."(c@#ffffff)"..swith[LANG]..
  191. string.char(0x1b).."(c@#FF8C00)"..weap..
  192. string.char(0x1b).."(c@#00bbff)"..get_message("player")) --TODO: make custom mob death messages
  193. end
  194. end
  195. if player=="" or hitter=="" then return end -- no killers/victims
  196. return true
  197. elseif hitter:get_player_name() == "" and player:get_hp() == 0 then
  198. minetest.chat_send_all(
  199. string.char(0x1b).."(c@#00CED1)"..player:get_player_name()..
  200. string.char(0x1b).."(c@#ff0000)"..swas[LANG]..
  201. string.char(0x1b).."(c@#ff0000)"..get_message("pvp")..
  202. string.char(0x1b).."(c@#ff0000)"..sby[LANG]..
  203. string.char(0x1b).."(c@#FF8C00)"..hitter:get_luaentity().name.. --too many mobs add to crash
  204. string.char(0x1b).."(c@#00bbff)"..get_message("mobs")) --TODO: make custom mob death messages
  205. if player=="" or hitter=="" or hitter=="*" then return end -- no mob killers/victims
  206. else
  207. return false
  208. end
  209. end)
  210. end)
  211. -----------------------------------------------------------------------------------------------
  212. print("[Mod] "..title.." ["..version.."] ["..mname.."] ["..LANG.."] Loaded...")
  213. -----------------------------------------------------------------------------------------------