1
0
Fork 0
VoxeLibre/mods/HUD/mcl_death_messages/init.lua

272 lines
9.4 KiB
Lua
Raw Normal View History

2019-03-07 21:10:39 +01:00
local S = minetest.get_translator("mcl_death_messages")
local N = function(s) return s end
2019-03-07 21:10:39 +01:00
mcl_death_messages = {}
2017-07-24 18:19:24 +02:00
-- Death messages
local msgs = {
["arrow"] = {
N("@1 was fatally hit by an arrow."),
2019-12-09 10:11:15 +01:00
N("@1 has been killed by an arrow."),
2017-07-24 18:19:24 +02:00
},
2017-07-24 20:57:23 +02:00
["arrow_name"] = {
2019-03-19 08:43:11 +01:00
N("@1 was shot by an arrow from @2."),
2017-07-24 20:57:23 +02:00
},
2019-12-09 10:11:15 +01:00
["arrow_skeleton"] = {
N("@1 was shot by an arrow from a skeleton."),
},
["arrow_stray"] = {
N("@1 was shot by an arrow from a stray."),
},
["arrow_illusioner"] = {
N("@1 was shot by an arrow from an illusioner."),
},
["arrow_mob"] = {
N("@1 was shot by an arrow."),
},
2017-07-24 18:19:24 +02:00
["drown"] = {
N("@1 forgot to breathe."),
N("@1 drowned."),
N("@1 ran out of oxygen."),
2017-07-24 18:19:24 +02:00
},
["murder"] = {
2019-03-19 08:43:11 +01:00
N("@1 was killed by @2."),
2017-07-24 18:19:24 +02:00
},
["mob_kill"] = {
N("@1 was killed by a mob."),
2017-07-24 18:19:24 +02:00
},
2017-07-24 18:36:41 +02:00
["blaze_fireball"] = {
N("@1 was burned to death by a blaze's fireball."),
N("@1 was killed by a fireball from a blaze."),
2017-07-24 18:36:41 +02:00
},
2017-07-24 19:53:03 +02:00
["fire_charge"] = {
N("@1 was burned by a fire charge."),
2017-07-24 19:53:03 +02:00
},
2017-07-24 18:36:41 +02:00
["ghast_fireball"] = {
N("A ghast scared @1 to death."),
N("@1 has been fireballed by a ghast."),
2017-07-24 18:36:41 +02:00
},
2019-03-08 20:22:01 +01:00
["fall"] = {
N("@1 fell from a high cliff."),
N("@1 took fatal fall damage."),
N("@1 fell victim to gravity."),
2017-07-24 18:19:24 +02:00
},
["other"] = {
N("@1 died."),
2017-07-24 18:19:24 +02:00
}
}
local mobkills = {
["mobs_mc:zombie"] = N("@1 was killed by a zombie."),
["mobs_mc:baby_zombie"] = N("@1 was killed by a baby zombie."),
["mobs_mc:blaze"] = N("@1 was killed by a blaze."),
["mobs_mc:slime"] = N("@1 was killed by a slime."),
["mobs_mc:witch"] = N("@1 was killed by a witch."),
["mobs_mc:magma_cube_tiny"] = N("@1 was killed by a magma cube."),
["mobs_mc:magma_cube_small"] = N("@1 was killed by a magma cube."),
["mobs_mc:magma_cube_big"] = N("@1 was killed by a magma cube."),
["mobs_mc:wolf"] = N("@1 was killed by a wolf."),
["mobs_mc:cat"] = N("@1 was killed by a cat."),
["mobs_mc:ocelot"] = N("@1 was killed by an ocelot."),
2019-12-10 01:01:04 +01:00
["mobs_mc:enderdragon"] = N("@1 was killed by an ender dragon."),
["mobs_mc:wither"] = N("@1 was killed by a wither."),
["mobs_mc:enderman"] = N("@1 was killed by an enderman."),
["mobs_mc:endermite"] = N("@1 was killed by an endermite."),
["mobs_mc:ghast"] = N("@1 was killed by a ghast."),
["mobs_mc:guardian_elder"] = N("@1 was killed by an elder guardian."),
["mobs_mc:guardian"] = N("@1 was killed by a guardian."),
["mobs_mc:iron_golem"] = N("@1 was killed by an iron golem."),
["mobs_mc:polar_bear"] = N("@1 was killed by a polar_bear."),
["mobs_mc:killer_bunny"] = N("@1 was killed by a killer bunny."),
["mobs_mc:shulker"] = N("@1 was killed by a shulker."),
["mobs_mc:silverfish"] = N("@1 was killed by a silverfish."),
["mobs_mc:skeleton"] = N("@1 was killed by a skeleton."),
["mobs_mc:stray"] = N("@1 was killed by a stray."),
["mobs_mc:slime_tiny"] = N("@1 was killed by a slime."),
["mobs_mc:slime_small"] = N("@1 was killed by a slime."),
["mobs_mc:slime_big"] = N("@1 was killed by a slime."),
["mobs_mc:spider"] = N("@1 was killed by a spider."),
["mobs_mc:cave_spider"] = N("@1 was killed by a cave spider."),
["mobs_mc:vex"] = N("@1 was killed by a vex."),
["mobs_mc:evoker"] = N("@1 was killed by an evoker."),
["mobs_mc:illusioner"] = N("@1 was killed by an illusioner."),
["mobs_mc:vindicator"] = N("@1 was killed by a vindicator."),
["mobs_mc:villager_zombie"] = N("@1 was killed by a zombie villager."),
["mobs_mc:husk"] = N("@1 was killed by a husk."),
["mobs_mc:baby_husk"] = N("@1 was killed by a baby husk."),
["mobs_mc:pigman"] = N("@1 was killed by a zombie pigman."),
["mobs_mc:baby_pigman"] = N("@1 was killed by a baby zombie pigman."),
2017-07-24 18:19:24 +02:00
}
-- Select death message
local dmsg = function(mtype, ...)
local r = math.random(1, #msgs[mtype])
return S(msgs[mtype][r], ...)
2017-07-24 18:19:24 +02:00
end
-- Select death message for death by mob
local mmsg = function(mtype, ...)
if mobkills[mtype] then
return S(mobkills[mtype], ...)
2017-07-24 18:19:24 +02:00
else
return dmsg("mob_kill", ...)
end
end
local last_damages = { }
2017-07-24 18:19:24 +02:00
2019-03-08 20:22:01 +01:00
minetest.register_on_dieplayer(function(player, reason)
2017-06-09 13:39:06 +02:00
-- Death message
local message = minetest.settings:get_bool("mcl_showDeathMessages")
2019-03-08 20:22:01 +01:00
if message == nil then
message = true
end
2017-06-09 13:39:06 +02:00
if message then
local name = player:get_player_name()
2017-07-24 18:19:24 +02:00
if not name then
return
2017-06-09 13:39:06 +02:00
end
local msg
2019-03-08 20:22:01 +01:00
if reason.type == "node_damage" then
local pos = player:get_pos()
-- Check multiple nodes because players occupy multiple nodes
-- (we add one additional node because the check may fail if the player was
-- just barely touching the node with the head)
local posses = { pos, {x=pos.x,y=pos.y+1,z=pos.z}, {x=pos.x,y=pos.y+2,z=pos.z}}
2019-03-08 21:59:16 +01:00
local highest_damage = 0
local highest_damage_def = nil
-- Show message for node that dealt the most damage
2019-03-08 20:22:01 +01:00
for p=1, #posses do
2019-03-08 21:59:16 +01:00
local def = minetest.registered_nodes[minetest.get_node(posses[p]).name]
local dmg = def.damage_per_second
if dmg and dmg > highest_damage then
highest_damage = dmg
highest_damage_def = def
end
end
if highest_damage_def and highest_damage_def._mcl_node_death_message then
local field = highest_damage_def._mcl_node_death_message
local field_msg
if type(field) == "table" then
field_msg = field[math.random(1, #field)]
else
field_msg = field
2019-03-08 20:22:01 +01:00
end
local textdomain
if highest_damage_def.mod_origin then
textdomain = highest_damage_def.mod_origin
else
textdomain = "mcl_death_messages"
end
-- We assume the textdomain of the death message in the node definition
-- equals the modname.
msg = minetest.translate(textdomain, field_msg, name)
2019-03-08 20:22:01 +01:00
end
elseif reason.type == "drown" then
2017-06-09 13:39:06 +02:00
msg = dmsg("drown", name)
2019-03-08 20:22:01 +01:00
elseif reason.type == "punch" then
-- Punches
local hitter = reason.object
local hittername, hittertype, hittersubtype, shooter
-- Unknown hitter
if hitter == nil then
msg = dmsg("murder_any")
-- Player
elseif hitter:is_player() then
hittername = hitter:get_player_name()
if hittername ~= nil then
msg = dmsg("murder", name, hittername)
else
msg = dmsg("murder_any", name)
end
-- Mob (according to Common Mob Interface)
elseif hitter:get_luaentity()._cmi_is_mob then
if hitter:get_luaentity().nametag and hitter:get_luaentity().nametag ~= "" then
hittername = hitter:get_luaentity().nametag
end
hittersubtype = hitter:get_luaentity().name
if hittername then
msg = dmsg("murder", name, hittername)
elseif hittersubtype ~= nil and hittersubtype ~= "" then
msg = mmsg(hittersubtype, name)
else
msg = dmsg("murder_any", name)
end
-- Arrow
elseif hitter:get_luaentity().name == "mcl_bows:arrow_entity" or hitter:get_luaentity().name == "mobs_mc:arrow_entity" then
local shooter
if hitter:get_luaentity()._shooter then
shooter = hitter:get_luaentity()._shooter
end
2019-12-09 10:11:15 +01:00
local s_ent = shooter:get_luaentity()
2019-03-08 20:22:01 +01:00
if shooter == nil then
msg = dmsg("arrow", name)
elseif shooter:is_player() then
msg = dmsg("arrow_name", name, shooter:get_player_name())
2019-12-09 10:11:15 +01:00
elseif s_ent._cmi_is_mob then
if s_ent.nametag ~= "" then
2019-03-08 20:22:01 +01:00
msg = dmsg("arrow_name", name, shooter:get_player_name())
2019-12-09 10:11:15 +01:00
elseif s_ent.name == "mobs_mc:skeleton" then
msg = dmsg("arrow_skeleton", name)
elseif s_ent.name == "mobs_mc:stray" then
msg = dmsg("arrow_stray", name)
elseif s_ent.name == "mobs_mc:illusioner" then
msg = dmsg("arrow_illusioner", name)
2017-07-24 20:57:23 +02:00
else
2019-12-09 10:11:15 +01:00
msg = dmsg("arrow_mob", name)
2017-07-24 20:57:23 +02:00
end
2019-03-08 20:22:01 +01:00
else
msg = dmsg("arrow", name)
end
-- Blaze fireball
elseif hitter:get_luaentity().name == "mobs_mc:blaze_fireball" then
if hitter:get_luaentity()._shot_from_dispenser then
2017-07-24 19:53:03 +02:00
msg = dmsg("fire_charge", name)
2019-03-08 20:22:01 +01:00
else
msg = dmsg("blaze_fireball", name)
2017-07-24 18:19:24 +02:00
end
2019-03-08 20:22:01 +01:00
-- Ghast fireball
elseif hitter:get_luaentity().name == "mobs_monster:fireball" then
msg = dmsg("ghast_fireball", name)
end
-- Falling
elseif reason.type == "fall" then
msg = dmsg("fall", name)
-- Other
elseif reason.type == "set_hp" then
if last_damages[name] then
2019-03-08 20:22:01 +01:00
msg = last_damages[name].message
2017-07-24 18:19:24 +02:00
end
end
2019-03-08 20:22:01 +01:00
if not msg then
msg = dmsg("other", name)
2017-07-24 18:19:24 +02:00
end
2019-03-08 20:22:01 +01:00
minetest.chat_send_all(msg)
last_damages[name] = nil
2017-07-24 18:19:24 +02:00
end
end)
-- dmg_sequence_number is used to discard old damage events
local dmg_sequence_number = 0
local start_damage_reset_countdown = function (player, sequence_number)
minetest.after(1, function(playername, sequence_number)
if last_damages[playername] and last_damages[playername].sequence_number == sequence_number then
2019-03-08 20:22:01 +01:00
last_damages[playername] = nil
end
end, player:get_player_name(), sequence_number)
end
2017-07-24 18:19:24 +02:00
-- Send a custom death mesage when damaging a player via set_hp.
-- To be called directly BEFORE damaging a player via set_hp.
-- The next time the player dies due to a set_hp, the message will be shown.
-- The player must die via set_hp within 0.1 seconds, otherwise the message will be discarded.
function mcl_death_messages.player_damage(player, message)
last_damages[player:get_player_name()] = { message = message, sequence_number = dmg_sequence_number }
start_damage_reset_countdown(player, dmg_sequence_number)
dmg_sequence_number = dmg_sequence_number + 1
if dmg_sequence_number >= 65535 then
dmg_sequence_number = 0
end
end
2019-03-08 21:59:16 +01:00