Files
bl/logic/service/fight/input/ai.go
xinian c021b40fbe
All checks were successful
ci/woodpecker/push/my-first-workflow Pipeline was successful
feat: 增强踢人逻辑与BOSS脚本支持
优化踢人超时处理和僵尸连接清理,支持BOSS动作脚本并增加测试,修复事件匹配与战斗循环中的并发问题。
2026-04-05 21:59:22 +08:00

113 lines
2.4 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package input
import (
"blazing/logic/service/fight/info"
"blazing/logic/service/player"
configmodel "blazing/modules/config/model"
"github.com/gogf/gf/v2/util/grand"
)
// Shuffle 打乱切片顺序,使用 Fisher-Yates 洗牌算法,泛型支持任意类型
func Shuffle[T any](slice []T) {
// 从后往前遍历,逐个交换
for i := len(slice) - 1; i > 0; i-- {
// 生成 0 到 i 之间的随机索引
j := grand.Intn(i + 1)
// 交换 i 和 j 位置的元素
slice[i], slice[j] = slice[j], slice[i]
}
}
func (our *Input) GetAction() {
next := our.Exec(func(t Effect) bool {
return t.HookAction()
})
if aiPlayer, ok := our.Player.(*player.AI_player); ok && aiPlayer.BossScript != "" {
scriptBoss := &configmodel.BossConfig{Script: aiPlayer.BossScript}
nextByScript, err := scriptBoss.RunHookActionScript(next)
if err != nil {
return
}
next = nextByScript
}
if !next {
return
}
// 获取己方当前宠物和对方当前宠物
selfPet := our.FightC.GetCurrPET(our.Player)
//没血就切换精灵
if selfPet.Info.Hp <= 0 {
for _, v := range our.AllPet {
if v.Info.Hp > 0 {
// println("AI出手,切换宠物")
our.FightC.ChangePet(our.Player, v.Info.CatchTime)
return
}
}
// 如果没有可用宠物,则直接返回,不执行任何操作
return
}
//oppPet := opp.FightC.GetCurrPET(opp.Player)
skills := selfPet.Skills
// 空技能列表直接返回,避免错误
if len(skills) == 0 {
return
}
//println("AI出手,选择技能")
var usedskill *info.SkillEntity
for _, s := range skills {
if s == nil {
continue
}
if !s.CanUse() {
continue
}
// 计算技能对对方的伤害假设CalculatePower返回伤害值或需从技能中获取
s.DamageValue = our.CalculatePower(our.Opp, s)
oppPet := our.Opp.CurrentPet()
if oppPet == nil {
continue
}
// 判断是否能秒杀(伤害 >= 对方当前生命值)
if s.DamageValue.Cmp(oppPet.GetHP()) != -1 { // 假设oppPet.HP为对方当前剩余生命值
if usedskill != nil {
if s.DamageValue.Cmp(usedskill.DamageValue) != -1 {
usedskill = s
}
} else {
usedskill = s
}
}
}
Shuffle(skills)
if usedskill == nil {
for _, s := range skills {
if s == nil {
continue
}
if !s.CanUse() {
continue
}
usedskill = s
}
}
if usedskill != nil {
our.FightC.UseSkill(our.Player, uint32(usedskill.XML.ID))
} else {
our.FightC.UseSkill(our.Player, 0)
}
}