2023-04-09 05:35:35 +00:00
|
|
|
-- SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
|
2023-04-13 12:17:39 +00:00
|
|
|
local damage_nature_table = {
|
|
|
|
[fk.NormalDamage] = "normal_damage",
|
|
|
|
[fk.FireDamage] = "fire_damage",
|
|
|
|
[fk.ThunderDamage] = "thunder_damage",
|
|
|
|
[fk.IceDamage] = "ice_damage",
|
|
|
|
}
|
|
|
|
|
|
|
|
local function sendDamageLog(room, damageStruct)
|
|
|
|
if damageStruct.from then
|
|
|
|
room:sendLog{
|
|
|
|
type = "#Damage",
|
|
|
|
to = {damageStruct.from.id},
|
|
|
|
from = damageStruct.to.id,
|
|
|
|
arg = damageStruct.damage,
|
|
|
|
arg2 = damage_nature_table[damageStruct.damageType],
|
|
|
|
}
|
|
|
|
else
|
|
|
|
room:sendLog{
|
|
|
|
type = "#DamageWithNoFrom",
|
|
|
|
from = damageStruct.to.id,
|
|
|
|
arg = damageStruct.damage,
|
|
|
|
arg2 = damage_nature_table[damageStruct.damageType],
|
|
|
|
}
|
|
|
|
end
|
|
|
|
room:sendLogEvent("Damage", {
|
|
|
|
to = damageStruct.to.id,
|
|
|
|
damageType = damage_nature_table[damageStruct.damageType],
|
|
|
|
damageNum = damageStruct.damage,
|
|
|
|
})
|
|
|
|
end
|
|
|
|
|
2023-02-28 17:43:44 +00:00
|
|
|
GameEvent.functions[GameEvent.ChangeHp] = function(self)
|
|
|
|
local player, num, reason, skillName, damageStruct = table.unpack(self.data)
|
2023-08-10 19:19:59 +00:00
|
|
|
local room = self.room
|
|
|
|
local logic = room.logic
|
2023-02-28 17:43:44 +00:00
|
|
|
if num == 0 then
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
assert(reason == nil or table.contains({ "loseHp", "damage", "recover" }, reason))
|
|
|
|
|
|
|
|
---@type HpChangedData
|
|
|
|
local data = {
|
|
|
|
num = num,
|
|
|
|
reason = reason,
|
|
|
|
skillName = skillName,
|
2023-03-20 12:49:23 +00:00
|
|
|
damageEvent = damageStruct,
|
2023-02-28 17:43:44 +00:00
|
|
|
}
|
|
|
|
|
2023-12-10 10:55:16 +00:00
|
|
|
if reason == "damage" then
|
|
|
|
data.shield_lost = math.min(-num, player.shield)
|
|
|
|
data.num = num + data.shield_lost
|
|
|
|
end
|
|
|
|
|
2023-08-10 19:19:59 +00:00
|
|
|
if logic:trigger(fk.BeforeHpChanged, player, data) then
|
|
|
|
logic:breakEvent(false)
|
2023-02-28 17:43:44 +00:00
|
|
|
end
|
|
|
|
|
2023-12-10 10:55:16 +00:00
|
|
|
if reason == "damage" and data.shield_lost > 0 and not damageStruct.isVirtualDMG then
|
|
|
|
room:changeShield(player, -data.shield_lost)
|
|
|
|
end
|
2023-02-28 17:43:44 +00:00
|
|
|
|
|
|
|
if reason == "damage" then
|
2023-08-10 19:19:59 +00:00
|
|
|
sendDamageLog(room, damageStruct)
|
2023-12-10 10:55:16 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
if not (reason == "damage" and (data.num == 0 or damageStruct.isVirtualDMG)) then
|
|
|
|
assert(not (data.reason == "recover" and data.num < 0))
|
|
|
|
player.hp = math.min(player.hp + data.num, player.maxHp)
|
|
|
|
room:broadcastProperty(player, "hp")
|
|
|
|
|
|
|
|
if reason == "loseHp" then
|
|
|
|
room:sendLog{
|
|
|
|
type = "#LoseHP",
|
|
|
|
from = player.id,
|
|
|
|
arg = 0 - num,
|
|
|
|
}
|
|
|
|
room:sendLogEvent("LoseHP", {})
|
|
|
|
elseif reason == "recover" then
|
|
|
|
room:sendLog{
|
|
|
|
type = "#HealHP",
|
|
|
|
from = player.id,
|
|
|
|
arg = num,
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2023-08-10 19:19:59 +00:00
|
|
|
room:sendLog{
|
2023-12-10 10:55:16 +00:00
|
|
|
type = "#ShowHPAndMaxHP",
|
2023-02-28 17:43:44 +00:00
|
|
|
from = player.id,
|
2023-12-10 10:55:16 +00:00
|
|
|
arg = player.hp,
|
|
|
|
arg2 = player.maxHp,
|
2023-02-28 17:43:44 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2023-08-10 19:19:59 +00:00
|
|
|
logic:trigger(fk.HpChanged, player, data)
|
2023-02-28 17:43:44 +00:00
|
|
|
|
|
|
|
if player.hp < 1 then
|
2023-04-04 08:25:37 +00:00
|
|
|
if num < 0 and not data.preventDying then
|
2023-02-28 17:43:44 +00:00
|
|
|
---@type DyingStruct
|
|
|
|
local dyingStruct = {
|
|
|
|
who = player.id,
|
|
|
|
damage = damageStruct,
|
|
|
|
}
|
2023-08-10 19:19:59 +00:00
|
|
|
room:enterDying(dyingStruct)
|
2023-02-28 17:43:44 +00:00
|
|
|
end
|
|
|
|
elseif player.dying then
|
|
|
|
player.dying = false
|
2023-08-10 19:19:59 +00:00
|
|
|
room:broadcastProperty(player, "dying")
|
2023-02-28 17:43:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
GameEvent.functions[GameEvent.Damage] = function(self)
|
|
|
|
local damageStruct = table.unpack(self.data)
|
2023-08-10 19:19:59 +00:00
|
|
|
local room = self.room
|
|
|
|
local logic = room.logic
|
2023-05-28 10:45:54 +00:00
|
|
|
if damageStruct.card and damageStruct.skillName == damageStruct.card.name .. "_skill" and not damageStruct.chain then
|
2023-08-10 19:19:59 +00:00
|
|
|
local cardEffectData = logic:getCurrentEvent():findParent(GameEvent.CardEffect)
|
2023-05-28 10:45:54 +00:00
|
|
|
if cardEffectData then
|
|
|
|
local cardEffectEvent = cardEffectData.data[1]
|
|
|
|
damageStruct.damage = damageStruct.damage + (cardEffectEvent.additionalDamage or 0)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-02-28 17:43:44 +00:00
|
|
|
if damageStruct.damage < 1 then
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
damageStruct.damageType = damageStruct.damageType or fk.NormalDamage
|
|
|
|
|
2023-07-16 07:29:20 +00:00
|
|
|
if damageStruct.from and damageStruct.from.dead then
|
2023-02-28 17:43:44 +00:00
|
|
|
damageStruct.from = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
assert(damageStruct.to:isInstanceOf(ServerPlayer))
|
|
|
|
|
|
|
|
local stages = {
|
|
|
|
{fk.PreDamage, damageStruct.from},
|
|
|
|
}
|
|
|
|
|
2023-12-10 10:55:16 +00:00
|
|
|
if not damageStruct.isVirtualDMG then
|
|
|
|
table.insertTable(stages, { { fk.DamageCaused, damageStruct.from }, { fk.DamageInflicted, damageStruct.to } })
|
|
|
|
end
|
|
|
|
|
2023-02-28 17:43:44 +00:00
|
|
|
for _, struct in ipairs(stages) do
|
|
|
|
local event, player = table.unpack(struct)
|
2023-08-10 19:19:59 +00:00
|
|
|
if logic:trigger(event, player, damageStruct) or damageStruct.damage < 1 then
|
|
|
|
logic:breakEvent(false)
|
2023-02-28 17:43:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
assert(damageStruct.to:isInstanceOf(ServerPlayer))
|
|
|
|
end
|
|
|
|
|
2023-07-16 07:29:20 +00:00
|
|
|
if damageStruct.to.dead then
|
2023-02-28 17:43:44 +00:00
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
2023-12-10 10:55:16 +00:00
|
|
|
damageStruct.dealtRecorderId = room.logic.specific_events_id[GameEvent.Damage]
|
|
|
|
room.logic.specific_events_id[GameEvent.Damage] = room.logic.specific_events_id[GameEvent.Damage] + 1
|
|
|
|
|
2023-05-28 10:45:54 +00:00
|
|
|
if damageStruct.card and damageStruct.damage > 0 then
|
2023-08-10 19:19:59 +00:00
|
|
|
local parentUseData = logic:getCurrentEvent():findParent(GameEvent.UseCard)
|
2023-05-28 10:45:54 +00:00
|
|
|
if parentUseData then
|
|
|
|
local cardUseEvent = parentUseData.data[1]
|
|
|
|
cardUseEvent.damageDealt = cardUseEvent.damageDealt or {}
|
|
|
|
cardUseEvent.damageDealt[damageStruct.to.id] = (cardUseEvent.damageDealt[damageStruct.to.id] or 0) + damageStruct.damage
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-06-11 04:45:12 +00:00
|
|
|
if damageStruct.damageType ~= fk.NormalDamage and damageStruct.to.chained then
|
|
|
|
damageStruct.beginnerOfTheDamage = true
|
|
|
|
damageStruct.to:setChainState(false)
|
|
|
|
end
|
|
|
|
|
2023-12-10 10:55:16 +00:00
|
|
|
if not room:changeHp(
|
|
|
|
damageStruct.to,
|
|
|
|
-damageStruct.damage,
|
|
|
|
"damage",
|
|
|
|
damageStruct.skillName,
|
|
|
|
damageStruct) then
|
|
|
|
logic:breakEvent(false)
|
2023-02-28 17:43:44 +00:00
|
|
|
end
|
|
|
|
|
2023-12-10 10:55:16 +00:00
|
|
|
|
2023-02-28 17:43:44 +00:00
|
|
|
stages = {
|
|
|
|
{fk.Damage, damageStruct.from},
|
|
|
|
{fk.Damaged, damageStruct.to},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, struct in ipairs(stages) do
|
|
|
|
local event, player = table.unpack(struct)
|
2023-08-10 19:19:59 +00:00
|
|
|
logic:trigger(event, player, damageStruct)
|
2023-02-28 17:43:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
2023-06-11 04:45:12 +00:00
|
|
|
GameEvent.exit_funcs[GameEvent.Damage] = function(self)
|
|
|
|
local room = self.room
|
2023-08-10 19:19:59 +00:00
|
|
|
local logic = room.logic
|
2023-06-11 04:45:12 +00:00
|
|
|
local damageStruct = self.data[1]
|
|
|
|
|
2023-08-10 19:19:59 +00:00
|
|
|
logic:trigger(fk.DamageFinished, damageStruct.to, damageStruct)
|
2023-06-11 04:45:12 +00:00
|
|
|
|
|
|
|
if damageStruct.beginnerOfTheDamage and not damageStruct.chain then
|
2023-07-16 11:18:43 +00:00
|
|
|
local targets = table.filter(room:getOtherPlayers(damageStruct.to), function(p)
|
2023-06-11 04:45:12 +00:00
|
|
|
return p.chained
|
|
|
|
end)
|
|
|
|
for _, p in ipairs(targets) do
|
|
|
|
room:sendLog{
|
|
|
|
type = "#ChainDamage",
|
|
|
|
from = p.id
|
|
|
|
}
|
2023-12-10 10:56:50 +00:00
|
|
|
|
2023-12-10 10:55:16 +00:00
|
|
|
local dmg = {
|
|
|
|
from = damageStruct.from,
|
|
|
|
to = p,
|
|
|
|
damage = damageStruct.damage,
|
2023-12-12 12:06:07 +00:00
|
|
|
damageType = damageStruct.damageType,
|
2023-12-10 10:55:16 +00:00
|
|
|
card = damageStruct.card,
|
|
|
|
skillName = damageStruct.skillName,
|
|
|
|
chain = true,
|
|
|
|
}
|
|
|
|
|
2023-06-11 04:45:12 +00:00
|
|
|
room:damage(dmg)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-02-28 17:43:44 +00:00
|
|
|
GameEvent.functions[GameEvent.LoseHp] = function(self)
|
|
|
|
local player, num, skillName = table.unpack(self.data)
|
2023-08-10 19:19:59 +00:00
|
|
|
local room = self.room
|
|
|
|
local logic = room.logic
|
|
|
|
|
2023-02-28 17:43:44 +00:00
|
|
|
if num == nil then
|
|
|
|
num = 1
|
|
|
|
elseif num < 1 then
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
---@type HpLostData
|
|
|
|
local data = {
|
|
|
|
num = num,
|
|
|
|
skillName = skillName,
|
|
|
|
}
|
2023-08-10 19:19:59 +00:00
|
|
|
if logic:trigger(fk.PreHpLost, player, data) or data.num < 1 then
|
|
|
|
logic:breakEvent(false)
|
2023-02-28 17:43:44 +00:00
|
|
|
end
|
|
|
|
|
2023-08-10 19:19:59 +00:00
|
|
|
if not room:changeHp(player, -data.num, "loseHp", skillName) then
|
|
|
|
logic:breakEvent(false)
|
2023-02-28 17:43:44 +00:00
|
|
|
end
|
|
|
|
|
2023-08-10 19:19:59 +00:00
|
|
|
logic:trigger(fk.HpLost, player, data)
|
2023-02-28 17:43:44 +00:00
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
GameEvent.functions[GameEvent.Recover] = function(self)
|
|
|
|
local recoverStruct = table.unpack(self.data)
|
2023-08-10 19:19:59 +00:00
|
|
|
local room = self.room
|
|
|
|
local logic = room.logic
|
|
|
|
|
2023-05-28 10:45:54 +00:00
|
|
|
if recoverStruct.card then
|
2023-08-10 19:19:59 +00:00
|
|
|
local cardEffectData = logic:getCurrentEvent():findParent(GameEvent.CardEffect)
|
2023-05-28 10:45:54 +00:00
|
|
|
if cardEffectData then
|
|
|
|
local cardEffectEvent = cardEffectData.data[1]
|
|
|
|
recoverStruct.num = recoverStruct.num + (cardEffectEvent.additionalRecover or 0)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-02-28 17:43:44 +00:00
|
|
|
if recoverStruct.num < 1 then
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
local who = recoverStruct.who
|
2023-07-16 07:29:20 +00:00
|
|
|
|
2023-08-10 19:19:59 +00:00
|
|
|
if logic:trigger(fk.PreHpRecover, who, recoverStruct) or recoverStruct.num < 1 then
|
|
|
|
logic:breakEvent(false)
|
2023-02-28 17:43:44 +00:00
|
|
|
end
|
|
|
|
|
2023-08-10 19:19:59 +00:00
|
|
|
if not room:changeHp(who, recoverStruct.num, "recover", recoverStruct.skillName) then
|
|
|
|
logic:breakEvent(false)
|
2023-02-28 17:43:44 +00:00
|
|
|
end
|
|
|
|
|
2023-08-10 19:19:59 +00:00
|
|
|
logic:trigger(fk.HpRecover, who, recoverStruct)
|
2023-02-28 17:43:44 +00:00
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
GameEvent.functions[GameEvent.ChangeMaxHp] = function(self)
|
|
|
|
local player, num = table.unpack(self.data)
|
2023-08-10 19:19:59 +00:00
|
|
|
local room = self.room
|
2023-02-28 17:43:44 +00:00
|
|
|
if num == 0 then
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
player.maxHp = math.max(player.maxHp + num, 0)
|
2023-08-10 19:19:59 +00:00
|
|
|
room:broadcastProperty(player, "maxHp")
|
|
|
|
room:sendLogEvent("ChangeMaxHp", {
|
2023-06-04 11:31:44 +00:00
|
|
|
player = player.id,
|
|
|
|
num = num,
|
|
|
|
})
|
2023-08-10 19:19:59 +00:00
|
|
|
room:sendLog{
|
2023-07-16 07:29:20 +00:00
|
|
|
type = num > 0 and "#HealMaxHP" or "#LoseMaxHP",
|
|
|
|
from = player.id,
|
|
|
|
arg = num > 0 and num or - num,
|
|
|
|
}
|
2023-03-18 15:37:21 +00:00
|
|
|
if player.maxHp == 0 then
|
2023-07-16 07:29:20 +00:00
|
|
|
player.hp = 0
|
2023-08-10 19:19:59 +00:00
|
|
|
room:broadcastProperty(player, "hp")
|
|
|
|
room:sendLog{
|
2023-07-16 07:29:20 +00:00
|
|
|
type = "#ShowHPAndMaxHP",
|
|
|
|
from = player.id,
|
|
|
|
arg = 0,
|
|
|
|
arg2 = 0,
|
|
|
|
}
|
2023-08-10 19:19:59 +00:00
|
|
|
room:killPlayer({ who = player.id })
|
2023-07-16 07:29:20 +00:00
|
|
|
return false
|
2023-03-18 15:37:21 +00:00
|
|
|
end
|
|
|
|
|
2023-02-28 17:43:44 +00:00
|
|
|
local diff = player.hp - player.maxHp
|
|
|
|
if diff > 0 then
|
2023-08-10 19:19:59 +00:00
|
|
|
if not room:changeHp(player, -diff) then
|
2023-02-28 17:43:44 +00:00
|
|
|
player.hp = player.hp - diff
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-08-10 19:19:59 +00:00
|
|
|
room:sendLog{
|
2023-05-13 05:23:18 +00:00
|
|
|
type = "#ShowHPAndMaxHP",
|
|
|
|
from = player.id,
|
|
|
|
arg = player.hp,
|
|
|
|
arg2 = player.maxHp,
|
|
|
|
}
|
|
|
|
|
2023-08-10 19:19:59 +00:00
|
|
|
room.logic:trigger(fk.MaxHpChanged, player, { num = num })
|
2023-02-28 17:43:44 +00:00
|
|
|
return true
|
|
|
|
end
|