refactor(fight): 优化战斗系统中的数值计算和逻辑处理

- 将GetProp方法返回类型从int改为alpacadecimal.Decimal,
避免精度丢失问题
- 修改战斗中速度比较逻辑,使用Decimal的Cmp方法进行比较
- 修正BattlePetEntity中属性计算公式,将乘法改为除法
- 调整伤害累加逻辑,修复SumDamage叠加问题
- 更新攻击力和防御力计算,直接使用Decimal数值
- 移除Effect178、Effect501等未使用的技能效果
- 重构回合处理逻辑,调整死亡判断时机和流程
- 添加TrueFirst字段用于正确跟踪实际先手方
```
This commit is contained in:
昔念
2026-03-09 20:55:04 +08:00
parent d16e079725
commit d360a85963
10 changed files with 98 additions and 87 deletions

View File

@@ -0,0 +1,35 @@
package effect
import (
"blazing/logic/service/fight/action"
"blazing/logic/service/fight/input"
"blazing/logic/service/fight/node"
"github.com/alpacahq/alpacadecimal"
)
// 178 - 造成伤害的1/n回复自身体力若属性相同则造成伤害的1/m回复自身体力
type Effect178 struct {
node.EffectNode
}
func (e *Effect178) Skill_Use_ex() bool {
damageDone := e.Ctx().Our.SumDamage
var healAmount alpacadecimal.Decimal
if e.Ctx().Our.CurrentPet.Type == e.Ctx().Opp.CurrentPet.Type {
// 属性相同1/m
healAmount = damageDone.Div(e.Args()[1])
} else {
// 属性不同1/n
healAmount = damageDone.Div(e.Args()[0])
}
e.Ctx().Our.Heal(e.Ctx().Our, &action.SelectSkillAction{}, healAmount)
return true
}
func init() {
input.InitEffect(input.EffectType.Skill, 178, &Effect178{})
}

View File

@@ -0,0 +1,29 @@
package effect
import (
"blazing/logic/service/fight/input"
"blazing/logic/service/fight/node"
)
// 501 - 若造成的伤害不足m则对手XX等级-n
type Effect501 struct {
node.EffectNode
}
func (e *Effect501) Skill_Use_ex() bool {
damageThreshold := int(e.Args()[0].IntPart())
damageDone := e.Ctx().Our.SumDamage
if damageDone.IntPart() < int64(damageThreshold) {
effectType := int8(e.Args()[1].IntPart()) // XX类型
effectValue := int8(e.Args()[2].IntPart()) // 等级-n
e.Ctx().Opp.SetProp(e.Ctx().Our, effectType, effectValue)
}
return true
}
func init() {
input.InitEffect(input.EffectType.Skill, 501, &Effect501{})
}

View File

@@ -206,28 +206,6 @@ func (e *Effect177) SetArgs(t *input.Input, a ...int) {
e.EffectNode.Duration(a[0]) // 持续n回合
}
// 178 - 造成伤害的1/n回复自身体力若属性相同则造成伤害的1/m回复自身体力
type Effect178 struct {
node.EffectNode
}
func (e *Effect178) SkillHit_ex() bool {
damageDone := e.Ctx().Our.SumDamage
var healAmount alpacadecimal.Decimal
if e.Ctx().Our.CurrentPet.Type == e.Ctx().Opp.CurrentPet.Type {
// 属性相同1/m
healAmount = damageDone.Div(e.Args()[1])
} else {
// 属性不同1/n
healAmount = damageDone.Div(e.Args()[0])
}
e.Ctx().Our.Heal(e.Ctx().Our, &action.SelectSkillAction{}, healAmount)
return true
}
// 475 - 若造成的伤害不足m则下n回合的攻击必定致命一击
type Effect475 struct {
node.EffectNode
@@ -253,29 +231,6 @@ func (e *Effect475) SkillHit_ex() bool {
return true
}
// 501 - 若造成的伤害不足m则对手XX等级-n
type Effect501 struct {
node.EffectNode
}
func (e *Effect501) SkillHit_ex() bool {
damageThreshold := int(e.Args()[0].IntPart())
damageDone := e.Ctx().Our.SumDamage
if damageDone.IntPart() < int64(damageThreshold) {
effectType := int(e.Args()[1].IntPart()) // XX类型
effectValue := int(e.Args()[2].IntPart()) // 等级-n
statusEffect := e.Ctx().Our.InitEffect(input.EffectType.Status, effectType)
if statusEffect != nil {
statusEffect.SetArgs(e.Ctx().Our, -effectValue) // 负值表示降低
e.Ctx().Opp.AddEffect(e.Ctx().Our, statusEffect)
}
}
return true
}
// 440 - n回合内对手使用技能消耗的PP值变为m倍
type Effect440 struct {
node.EffectNode

View File

@@ -30,7 +30,7 @@ func (e *Effect115) Skill_Use() bool {
if !ok {
return true
}
rr := alpacadecimal.NewFromInt(int64(e.Ctx().Our.GetProp(4))).Div(alpacadecimal.NewFromInt(int64(e.SideEffectArgs[1])))
rr := e.Ctx().Our.GetProp(4).Div(alpacadecimal.NewFromInt(int64(e.SideEffectArgs[1])))
e.Ctx().Opp.Damage(e.Ctx().Our, &info.DamageZone{
Type: info.DamageType.Fixed,

View File

@@ -170,11 +170,11 @@ func (f *FightC) enterturn(firstAttack, secondAttack *action.SelectSkillAction)
firstAttack, secondAttack = secondAttack, firstAttack //互换先手权
f.First, f.Second = f.Second, f.First
case firstAttack.SkillEntity.XML.Priority == secondAttack.SkillEntity.XML.Priority:
if f.Second.GetProp(4) > f.First.GetProp(4) {
if f.Second.GetProp(4).Cmp(f.First.GetProp(4)) > 0 {
firstAttack, secondAttack = secondAttack, firstAttack //互换先手权
f.First, f.Second = f.Second, f.First
}
if f.Second.GetProp(4) == f.First.GetProp(4) {
if f.Second.GetProp(4).Cmp(f.First.GetProp(4)) == 0 {
if grand.Meet(1, 2) { //随机出手
firstAttack, secondAttack = secondAttack, firstAttack //互换先手权
f.First, f.Second = f.Second, f.First
@@ -188,7 +188,7 @@ func (f *FightC) enterturn(firstAttack, secondAttack *action.SelectSkillAction)
f.First, f.Second = f.Second, f.First
}
var attacker, defender *input.Input
f.TrueFirst = f.First
//开始回合操作
for i := 0; i < 2; i++ {
var originalSkill *info.SkillEntity //原始技能
@@ -229,17 +229,6 @@ func (f *FightC) enterturn(firstAttack, secondAttack *action.SelectSkillAction)
}
//先手权不一定出手
// if i == 0 {
// //不能使用,所以这时候取消后手
// defender.ReactvieEffect()
// firstAttack, secondAttack = secondAttack, firstAttack //互换先手权
// f.First, f.Second = f.Second, f.First
// //反转先后手
// originalSkill = f.copySkill(firstAttack)
// currentSkill = originalSkill
// attacker, defender = defender, attacker
// }
} else {
@@ -256,16 +245,7 @@ func (f *FightC) enterturn(firstAttack, secondAttack *action.SelectSkillAction)
skill.PP--
}
}
if defender.CurrentPet.Info.Hp <= 0 && attacker.CurrentPet.Info.Hp <= 0 { //先手方死亡,触发反同归于尽
attacker.CurrentPet.Info.Hp = 1
}
if defender.CurrentPet.Info.Hp <= 0 {
f.TURNOVER(defender)
break
} else {
if defender.CurrentPet.Info.Hp > 0 {
//技能使用后
defender.Exec(func(effect input.Effect) bool { //技能使用后的我方效果
effect.Ctx().SkillEntity = currentSkill
@@ -273,18 +253,14 @@ func (f *FightC) enterturn(firstAttack, secondAttack *action.SelectSkillAction)
return true
})
}
if attacker.CurrentPet.Info.Hp <= 0 {
f.TURNOVER(attacker)
break
} else {
if attacker.CurrentPet.Info.Hp > 0 {
//技能使用后
attacker.Exec(func(effect input.Effect) bool { //技能使用后的我方效果
effect.Ctx().SkillEntity = currentSkill
effect.Skill_Use()
return true
})
}
//技能使用后
@@ -299,6 +275,21 @@ func (f *FightC) enterturn(firstAttack, secondAttack *action.SelectSkillAction)
effect.Action_end_ex()
return true
})
if defender.CurrentPet.Info.Hp <= 0 && attacker.CurrentPet.Info.Hp <= 0 { //先手方死亡,触发反同归于尽
attacker.CurrentPet.Info.Hp = 1
}
if defender.CurrentPet.Info.Hp <= 0 {
f.TURNOVER(defender)
break
}
if attacker.CurrentPet.Info.Hp <= 0 {
f.TURNOVER(attacker)
break
}
}
f.Broadcast(func(ff *input.Input) {

View File

@@ -61,8 +61,9 @@ func CalculateRealValue(value alpacadecimal.Decimal, stat1 int8) alpacadecimal.D
two := alpacadecimal.NewFromInt(2)
stat := alpacadecimal.NewFromInt(int64(stat1))
if stat.IsPositive() {
value.Mul(stat.Add(two).Mul(two))
r := value.Mul(stat.Add(two).Mul(two))
r := value.Mul(stat.Add(two).Div(two))
// println(value.IntPart(), r.IntPart(), "强化后数值")
return r
} else {

View File

@@ -77,7 +77,7 @@ func CreateSkill(skill *model.SkillInfo, rand *rand.Rand, pet *BattlePetEntity)
ret.XML = move
}
ret.Accuracy = alpacadecimal.NewFromInt(int64(move.Accuracy))
println(ret.Accuracy.IntPart())
// println(ret.Accuracy.IntPart())
ret.Info = skill
return &ret

View File

@@ -92,7 +92,7 @@ func (our *Input) InitEffect(etype EnumEffectType, id int, a ...int) Effect {
// * battle_lv: atk(0), def(1), sp_atk(2), sp_def(3), spd(4), accuracy(5)
// 是否需要真实提升
func (our *Input) GetProp(id int) int {
func (our *Input) GetProp(id int) alpacadecimal.Decimal {
// 计算实际值(这里可以插入后续优化的函数调用)
realValue := info.CalculateRealValue(alpacadecimal.NewFromInt(int64(our.CurrentPet.Info.Prop[id])), our.AttackValue.Prop[id])
@@ -100,7 +100,7 @@ func (our *Input) GetProp(id int) int {
// todo: 插入获取后处理函数,例如:
// realValue = postProcessValue(realValue, id, c)
return int(realValue.IntPart())
return realValue
}

View File

@@ -154,8 +154,8 @@ func (our *Input) Damage(in *Input, sub *info.DamageZone) {
}
if sub.Type == info.DamageType.Red { //红才会产生造成伤害
our.Opp.SumDamage = sub.Damage // 叠加总伤害 这里相当于记录红伤
our.Opp.AttackValue.LostHp = uint32(our.Opp.SumDamage.IntPart()) //红伤落实
our.Opp.SumDamage = sub.Damage.Add(our.Opp.SumDamage) // 叠加总伤害 这里相当于记录红伤
}
if uint32(sub.Damage.IntPart()) > our.CurrentPet.Info.Hp {
@@ -184,13 +184,13 @@ func (our *Input) CalculatePower(deftype *Input, skill *info.SkillEntity) alpaca
switch skill.Category() { //判断技能类型
case info.Category.PHYSICAL:
attackDec = alpacadecimal.NewFromInt(int64(our.GetProp(0)))
defenseDec = alpacadecimal.NewFromInt(int64(deftype.GetProp(1)))
attackDec = our.GetProp(0)
defenseDec = our.GetProp(1)
case info.Category.SPECIAL:
attackDec = alpacadecimal.NewFromInt(int64(our.GetProp(2)))
defenseDec = alpacadecimal.NewFromInt(int64(deftype.GetProp(3)))
attackDec = our.GetProp(2)
defenseDec = our.GetProp(3)
default:
return alpacadecimal.Zero

View File

@@ -165,7 +165,7 @@ func (our *Input) GenInfo() {
our.RemainHp = int32(our.CurrentPet.Info.Hp)
our.SkillList = our.CurrentPet.Info.SkillList
our.AttackValue.LostHp = uint32(our.SumDamage.IntPart()) //红伤落实
// f.Second.SkillList = f.Second.CurrentPet.Info.SkillList
// f.Second.RemainHp = int32(f.Second.CurrentPet.Info.Hp)
// ret.FAttack = *f.First.AttackValue