2025-09-14 03:36:26 +08:00
|
|
|
|
package fight
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"blazing/common/data/xmlres"
|
2025-11-09 04:37:15 +00:00
|
|
|
|
"blazing/cool"
|
2025-11-15 22:17:43 +00:00
|
|
|
|
|
2025-09-14 03:36:26 +08:00
|
|
|
|
"blazing/logic/service/common"
|
2025-09-28 08:13:42 +00:00
|
|
|
|
"blazing/logic/service/fight/action"
|
2025-09-14 03:36:26 +08:00
|
|
|
|
"blazing/logic/service/fight/info"
|
2025-09-24 20:17:44 +00:00
|
|
|
|
"blazing/logic/service/fight/input"
|
2025-11-09 04:37:15 +00:00
|
|
|
|
"context"
|
2025-11-14 23:09:16 +08:00
|
|
|
|
"log"
|
2025-09-14 03:36:26 +08:00
|
|
|
|
|
|
|
|
|
|
"github.com/gogf/gf/v2/util/gconv"
|
|
|
|
|
|
"github.com/jinzhu/copier"
|
|
|
|
|
|
"github.com/panjf2000/ants/v2"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
2025-09-28 08:13:42 +00:00
|
|
|
|
// Compare 比较两个1v1战斗动作的执行优先级(核心逻辑)
|
|
|
|
|
|
func (f *FightC) Compare(a, b action.BattleActionI) (action.BattleActionI, action.BattleActionI) {
|
|
|
|
|
|
// 动作本身的优先级比较
|
|
|
|
|
|
p1 := b.Priority() - a.Priority()
|
|
|
|
|
|
if p1 > 0 { // 对手优先级更高
|
|
|
|
|
|
return b, a
|
|
|
|
|
|
} else if p1 < 0 {
|
|
|
|
|
|
return a, b
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return a, b // 速度相同时,发起方优先
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-23 16:42:10 +00:00
|
|
|
|
// 玩家逃跑/无响应/掉线
|
2025-11-12 01:19:24 +08:00
|
|
|
|
func (f *FightC) Over(c common.PlayerI, res info.EnumBattleOverReason) {
|
2025-11-09 04:37:15 +00:00
|
|
|
|
if f.closefight {
|
|
|
|
|
|
cool.Loger.Debug(context.Background(), " 战斗chan已关闭")
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
2025-11-11 11:45:09 +00:00
|
|
|
|
// case *action.EscapeAction:
|
|
|
|
|
|
// f.FightOverInfo.WinnerId = b2.GetPlayerID() //对方胜利
|
|
|
|
|
|
// f.FightOverInfo.Reason = a.Reason
|
|
|
|
|
|
|
|
|
|
|
|
// f.closefight = true
|
|
|
|
|
|
// ret := &action.EscapeAction{
|
|
|
|
|
|
// BaseAction: action.NewBaseAction(c.GetInfo().UserID),
|
|
|
|
|
|
// Reason: res,
|
|
|
|
|
|
// }
|
2025-09-14 03:36:26 +08:00
|
|
|
|
|
2025-11-11 15:21:45 +00:00
|
|
|
|
f.overl.Do(func() {
|
|
|
|
|
|
f.Reason = res
|
|
|
|
|
|
f.WinnerId = f.GetInputByPlayer(c, true).UserID
|
|
|
|
|
|
close(f.quit)
|
2025-11-11 11:45:09 +00:00
|
|
|
|
|
2025-11-11 15:21:45 +00:00
|
|
|
|
})
|
|
|
|
|
|
|
2025-09-14 03:36:26 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 切换精灵 主动和被驱逐
|
|
|
|
|
|
func (f *FightC) ChangePet(c common.PlayerI, id uint32) {
|
2025-11-13 02:43:00 +08:00
|
|
|
|
|
2025-11-09 04:37:15 +00:00
|
|
|
|
if f.closefight {
|
|
|
|
|
|
cool.Loger.Debug(context.Background(), " 战斗chan已关闭")
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
2025-11-13 02:43:00 +08:00
|
|
|
|
|
2025-11-13 23:06:55 +08:00
|
|
|
|
selfinput := f.GetInputByPlayer(c, false)
|
|
|
|
|
|
InitAttackValue := *selfinput.AttackValue
|
2025-11-13 02:43:00 +08:00
|
|
|
|
oldpet := selfinput.CurrentPet
|
2025-09-28 08:13:42 +00:00
|
|
|
|
ret := &action.ActiveSwitchAction{
|
|
|
|
|
|
BaseAction: action.NewBaseAction(c.GetInfo().UserID),
|
2025-09-14 03:36:26 +08:00
|
|
|
|
}
|
2025-11-13 02:43:00 +08:00
|
|
|
|
selfinput.CurrentPet, ret.Reason = selfinput.GetPet(id)
|
|
|
|
|
|
|
2025-09-19 06:58:42 +00:00
|
|
|
|
f.Switch = append(f.Switch, ret)
|
2025-11-13 23:06:55 +08:00
|
|
|
|
selfinput.InitAttackValue() //切换精灵消除能力提升
|
2025-11-13 02:43:00 +08:00
|
|
|
|
//这时候精灵已经切换过了,可以直接给新精灵加效果
|
|
|
|
|
|
|
|
|
|
|
|
f.Broadcast(func(ff *input.Input) {
|
2025-09-23 13:24:40 +08:00
|
|
|
|
|
2025-11-13 02:43:00 +08:00
|
|
|
|
ff.Exec(func(t input.Effect) bool {
|
2025-09-24 20:17:44 +00:00
|
|
|
|
|
2025-11-13 23:06:55 +08:00
|
|
|
|
t.Switch(selfinput, InitAttackValue, oldpet)
|
2025-09-24 20:17:44 +00:00
|
|
|
|
|
2025-11-13 02:43:00 +08:00
|
|
|
|
return true
|
|
|
|
|
|
})
|
2025-09-24 20:17:44 +00:00
|
|
|
|
})
|
2025-11-13 02:43:00 +08:00
|
|
|
|
|
|
|
|
|
|
f.Broadcast(func(ff *input.Input) { //先给自身广播
|
2025-10-10 00:40:32 +08:00
|
|
|
|
if ff.Player.GetInfo().UserID == c.GetInfo().UserID {
|
2025-11-19 16:11:02 +08:00
|
|
|
|
ff.Player.SendPackCmd(2407, &ret.Reason)
|
2025-10-10 00:40:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
})
|
2025-09-24 20:17:44 +00:00
|
|
|
|
|
2025-11-10 02:29:00 +08:00
|
|
|
|
f.actionChan <- ret
|
2025-09-14 03:36:26 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 玩家使用技能
|
|
|
|
|
|
func (f *FightC) UseSkill(c common.PlayerI, id int32) {
|
2025-11-09 04:37:15 +00:00
|
|
|
|
if f.closefight {
|
|
|
|
|
|
cool.Loger.Debug(context.Background(), " 战斗chan已关闭")
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
2025-09-28 08:13:42 +00:00
|
|
|
|
ret := &action.SelectSkillAction{
|
2025-10-05 00:29:22 +08:00
|
|
|
|
BaseAction: action.NewBaseAction(c.GetInfo().UserID),
|
2025-09-14 03:36:26 +08:00
|
|
|
|
}
|
2025-11-11 08:28:18 +00:00
|
|
|
|
if f.GetInputByPlayer(c, false).CurrentPet == nil {
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
if f.GetInputByPlayer(c, false).CurrentPet.Info.Hp <= 0 {
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
2025-09-30 18:32:15 +08:00
|
|
|
|
for _, v := range f.GetInputByPlayer(c, false).CurrentPet.Skills {
|
2025-09-14 03:36:26 +08:00
|
|
|
|
|
|
|
|
|
|
if v != nil && v.ID == int(id) {
|
2025-10-26 20:56:03 +08:00
|
|
|
|
ret.SkillEntity = v
|
2025-09-24 12:40:13 +08:00
|
|
|
|
break
|
2025-09-14 03:36:26 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
2025-11-10 02:29:00 +08:00
|
|
|
|
f.actionChan <- ret
|
2025-09-14 03:36:26 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 玩家使用技能
|
|
|
|
|
|
func (f *FightC) Capture(c common.PlayerI, id uint32) {
|
2025-11-09 04:37:15 +00:00
|
|
|
|
if f.closefight {
|
|
|
|
|
|
cool.Loger.Debug(context.Background(), " 战斗chan已关闭")
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
2025-11-10 02:29:00 +08:00
|
|
|
|
f.actionChan <- &action.UseItemAction{BaseAction: action.NewBaseAction(c.GetInfo().UserID), ItemID: id}
|
2025-09-14 03:36:26 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-11-08 01:30:53 +08:00
|
|
|
|
func (f *FightC) UseItem(c common.PlayerI, cacthid, itemid uint32) {
|
2025-11-09 04:37:15 +00:00
|
|
|
|
if f.closefight {
|
|
|
|
|
|
cool.Loger.Debug(context.Background(), " 战斗chan已关闭")
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
2025-11-10 02:29:00 +08:00
|
|
|
|
f.actionChan <- &action.UseItemAction{BaseAction: action.NewBaseAction(c.GetInfo().UserID), ItemID: itemid, CacthTime: cacthid}
|
2025-11-08 01:30:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-11-14 23:09:16 +08:00
|
|
|
|
// ReadyFight 处理玩家战斗准备逻辑,当满足条件时启动战斗循环
|
2025-09-14 03:36:26 +08:00
|
|
|
|
func (f *FightC) ReadyFight(c common.PlayerI) {
|
2025-11-14 23:09:16 +08:00
|
|
|
|
// 1. 构建战斗开始信息(整理双方初始宠物信息)
|
|
|
|
|
|
fightStartInfo := f.buildFightStartInfo()
|
2025-11-01 18:36:21 +08:00
|
|
|
|
|
2025-11-14 23:09:16 +08:00
|
|
|
|
// 2. 标记当前玩家已准备完成
|
|
|
|
|
|
input := f.GetInputByPlayer(c, false)
|
|
|
|
|
|
input.Finished = true
|
2025-09-14 03:36:26 +08:00
|
|
|
|
|
2025-11-14 23:09:16 +08:00
|
|
|
|
// 3. 根据战斗类型判断是否满足战斗启动条件,满足则启动
|
|
|
|
|
|
switch f.Info.Status {
|
|
|
|
|
|
case info.BattleStatus.FIGHT_WITH_PLAYER: // PVP战斗:需双方都准备完成
|
|
|
|
|
|
if f.checkBothPlayersReady(c) {
|
|
|
|
|
|
f.startBattle(fightStartInfo)
|
|
|
|
|
|
}
|
|
|
|
|
|
case info.BattleStatus.FIGHT_WITH_BOSS: // BOSS战:单方准备完成即可启动
|
|
|
|
|
|
f.startBattle(fightStartInfo)
|
|
|
|
|
|
case info.BattleStatus.FIGHT_WITH_NPC: // NPC/野怪战斗:处理捕捉相关逻辑后启动
|
|
|
|
|
|
f.handleNPCFightSpecial(&fightStartInfo)
|
|
|
|
|
|
f.startBattle(fightStartInfo)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-09-14 03:36:26 +08:00
|
|
|
|
|
2025-11-14 23:09:16 +08:00
|
|
|
|
// buildFightStartInfo 构建战斗开始时需要发送给双方的信息
|
|
|
|
|
|
func (f *FightC) buildFightStartInfo() info.FightStartOutboundInfo {
|
|
|
|
|
|
var startInfo info.FightStartOutboundInfo
|
2025-11-13 02:43:00 +08:00
|
|
|
|
|
2025-11-14 23:09:16 +08:00
|
|
|
|
// 复制双方初始宠物信息(取列表第一个宠物)
|
2025-11-16 20:30:17 +00:00
|
|
|
|
if len(f.ReadyInfo.OurPetList) > 0 {
|
|
|
|
|
|
_ = copier.Copy(&startInfo.Info1, &f.ReadyInfo.OurPetList[0])
|
|
|
|
|
|
startInfo.Info1.UserID = f.ReadyInfo.OurInfo.UserID
|
2025-11-14 23:09:16 +08:00
|
|
|
|
}
|
2025-11-16 20:30:17 +00:00
|
|
|
|
if len(f.ReadyInfo.OpponentPetList) > 0 {
|
|
|
|
|
|
_ = copier.Copy(&startInfo.Info2, &f.ReadyInfo.OpponentPetList[0])
|
|
|
|
|
|
startInfo.Info2.UserID = f.ReadyInfo.OpponentInfo.UserID
|
2025-11-14 23:09:16 +08:00
|
|
|
|
}
|
2025-11-13 02:43:00 +08:00
|
|
|
|
|
2025-11-14 23:09:16 +08:00
|
|
|
|
return startInfo
|
|
|
|
|
|
}
|
2025-09-14 03:36:26 +08:00
|
|
|
|
|
2025-11-14 23:09:16 +08:00
|
|
|
|
// checkBothPlayersReady 检查PVP战斗中双方是否都已准备完成
|
|
|
|
|
|
// 参数c为当前准备的玩家,返回true表示双方均准备完成
|
|
|
|
|
|
func (f *FightC) checkBothPlayersReady(currentPlayer common.PlayerI) bool {
|
|
|
|
|
|
// 这里的第二个参数true含义需结合业务确认(推测为"检查对手"),建议用常量替代
|
|
|
|
|
|
opponentInput := f.GetInputByPlayer(currentPlayer, true)
|
|
|
|
|
|
return opponentInput.Finished
|
|
|
|
|
|
}
|
2025-11-13 02:43:00 +08:00
|
|
|
|
|
2025-11-14 23:09:16 +08:00
|
|
|
|
// handleNPCFightSpecial 处理NPC战斗的特殊逻辑(如可捕捉标记)
|
|
|
|
|
|
func (f *FightC) handleNPCFightSpecial(startInfo *info.FightStartOutboundInfo) {
|
|
|
|
|
|
// 检查野怪是否可捕捉(根据宠物ID获取捕捉率)
|
2025-11-16 20:30:17 +00:00
|
|
|
|
if len(f.ReadyInfo.OpponentPetList) == 0 {
|
2025-11-14 23:09:16 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
2025-11-16 20:30:17 +00:00
|
|
|
|
npcPetID := int(f.ReadyInfo.OpponentPetList[0].ID)
|
2025-11-14 23:09:16 +08:00
|
|
|
|
petCfg, ok := xmlres.PetMAP[npcPetID]
|
|
|
|
|
|
if !ok {
|
|
|
|
|
|
// log.Error(context.Background(), "NPC宠物配置不存在", "petID", npcPetID)
|
|
|
|
|
|
return
|
2025-09-14 03:36:26 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-11-14 23:09:16 +08:00
|
|
|
|
catchRate := gconv.Int(petCfg.CatchRate)
|
|
|
|
|
|
if catchRate > 0 {
|
|
|
|
|
|
startInfo.Info2.Catchable = 1 // 标记为可捕捉
|
|
|
|
|
|
// 标记AI对手允许被捕捉(类型断言确保安全)
|
2025-11-19 16:11:02 +08:00
|
|
|
|
f.Opp.CanCapture = true
|
|
|
|
|
|
|
2025-11-14 23:09:16 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-09-14 03:36:26 +08:00
|
|
|
|
|
2025-11-14 23:09:16 +08:00
|
|
|
|
// startBattle 启动战斗核心逻辑:提交战斗循环任务并通知双方
|
|
|
|
|
|
func (f *FightC) startBattle(startInfo info.FightStartOutboundInfo) {
|
|
|
|
|
|
// 提交战斗循环到战斗池(处理战斗池容量问题)
|
|
|
|
|
|
if err := Fightpool.Submit(f.battleLoop); err != nil {
|
|
|
|
|
|
log.Panic(context.Background(), "战斗循环提交失败", "error", err)
|
|
|
|
|
|
}
|
2025-09-14 03:36:26 +08:00
|
|
|
|
|
2025-11-18 20:52:04 +00:00
|
|
|
|
f.Broadcast(func(ff *input.Input) {
|
|
|
|
|
|
|
|
|
|
|
|
// 通知双方玩家准备完成,即将开始战斗
|
|
|
|
|
|
|
2025-11-19 16:11:02 +08:00
|
|
|
|
ff.Player.SendPackCmd(2504, &startInfo)
|
2025-11-18 20:52:04 +00:00
|
|
|
|
})
|
2025-09-14 03:36:26 +08:00
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-11-13 05:05:05 +08:00
|
|
|
|
var Fightpool *ants.Pool
|
2025-09-14 03:36:26 +08:00
|
|
|
|
|
|
|
|
|
|
func init() {
|
2025-11-13 05:05:05 +08:00
|
|
|
|
Fightpool, _ = ants.NewPool(-1)
|
2025-09-14 03:36:26 +08:00
|
|
|
|
//defer p.Release()
|
|
|
|
|
|
}
|