VoxeLibre/mods/CORE/mcl_damage/init.lua

117 lines
3.3 KiB
Lua
Raw Normal View History

2021-04-14 15:46:52 +02:00
mcl_damage = {
modifiers = {},
types = {
in_fire = {is_fire = true},
lightning_bolt = {is_lightning = true},
on_fire = {is_fire = true},
lava = {is_fire = true},
hot_floor = {is_fire = true},
in_wall = {bypasses_armor = true},
drown = {bypasses_armor = true},
starve = {bypasses_armor = true, bypasses_magic = true},
cactus = {},
fall = {bypasses_armor = true},
fly_into_wall = {bypasses_armor = true}, -- unused
out_of_world = {bypasses_armor = true, bypasses_invulnerability = true},
2021-04-14 15:46:52 +02:00
generic = {bypasses_armor = true},
magic = {is_magic = true, bypasses_armor = true},
2021-04-18 20:08:08 +02:00
wither = {bypasses_armor = true}, -- unused
2021-04-14 15:46:52 +02:00
anvil = {},
falling_node = {}, -- unused
dragon_breath = {bypasses_armor = true}, -- unused
mob = {},
player = {},
arrow = {is_projectile = true},
fireball = {is_projectile = true, is_fire = true},
thorns = {is_magic = true},
explosion = {is_explosion = true},
cramming = {bypasses_armor = true}, -- unused
fireworks = {is_explosion = true}, -- unused
command = {bypasses_armor = true, bypasses_invulnerability = true, bypasses_magic = true},
2021-04-14 15:46:52 +02:00
}
}
2021-04-18 20:08:08 +02:00
function mcl_damage.register_modifier(func, priority)
table.insert(mcl_damage.modifiers, {func = func, priority = priority or 0})
end
function mcl_damage.do_modifiers(player, damage, reason)
for _, modf in ipairs(mcl_damage.modifiers) do
damage = modf.func(player, damage, reason) or damage
if damage == 0 then
return 0
end
end
return damage
end
2021-04-14 15:46:52 +02:00
2021-04-18 20:08:08 +02:00
function mcl_damage.from_punch(mcl_reason, object)
mcl_reason.direct = object
local luaentity = mcl_reason.direct:get_luaentity()
if luaentity then
if luaentity._is_arrow then
mcl_reason.type = "arrow"
elseif luaentity._is_fireball then
mcl_reason.type = "fireball"
elseif luaentity._cmi_is_mob then
mcl_reason.type = "mob"
end
mcl_reason.source = mcl_reason.source or luaentity._source_object
2021-04-14 15:46:52 +02:00
else
2021-04-18 20:08:08 +02:00
mcl_reason.type = "player"
2021-04-14 15:46:52 +02:00
end
end
2021-04-18 20:08:08 +02:00
function mcl_damage.finish_reason(mcl_reason)
mcl_reason.source = mcl_reason.source or mcl_reason.direct
mcl_reason.flags = mcl_damage.types[mcl_reason.type]
2021-04-14 15:46:52 +02:00
end
2021-04-18 20:08:08 +02:00
function mcl_damage.from_mt(mt_reason)
local mcl_reason = {type = "generic"}
2021-04-14 15:46:52 +02:00
if mt_reason._mcl_type then
mcl_reason.type = mt_reason._mcl_type
elseif mt_reason.type == "fall" then
mcl_reason.type = "fall"
elseif mt_reason.type == "drown" then
mcl_reason.type = "drown"
elseif mt_reason.type == "punch" then
2021-04-18 20:08:08 +02:00
mcl_damage.from_punch(mcl_reason, mt_reason.object)
2021-04-14 17:20:51 +02:00
elseif mt_reason.type == "node_damage" and mt_reason.node then
if minetest.get_item_group(mt_reason.node, "fire") > 0 then
2021-04-14 15:46:52 +02:00
mcl_reason.type = "in_fire"
end
2021-04-14 17:20:51 +02:00
if minetest.get_item_group(mt_reason.node, "lava") > 0 then
mcl_reason.type = "lava"
end
2021-04-14 15:46:52 +02:00
end
for key, value in pairs(mt_reason) do
if key:find("_mcl_") == 1 then
mcl_reason[key:sub(6, #key)] = value
end
end
2021-04-18 20:08:08 +02:00
mcl_damage.finish_reason(mcl_reason)
2021-04-14 19:06:11 +02:00
return mcl_reason
2021-04-14 15:46:52 +02:00
end
function mcl_damage.register_type(name, def)
mcl_damage.types[name] = def
end
2021-04-18 20:08:08 +02:00
minetest.register_on_player_hpchange(function(player, hp_change, mt_reason)
if hp_change < 0 then
hp_change = -mcl_damage.do_modifiers(player, -hp_change, mcl_damage.from_mt(mt_reason))
2021-04-14 15:46:52 +02:00
end
return hp_change
end, true)
minetest.register_on_mods_loaded(function()
table.sort(mcl_damage.modifiers, function(a, b) return a.priority < b.priority end)
end)