feat(fight): 增加战斗模式枚举并重构战斗逻辑判断

- 引入完整的 BattleMode 枚举定义,替代原有的 BattleStatus,明确区分各类战斗场景
- 在多个控制器中替换对旧 Status 字段的依赖,统一使用 Mode 判断战斗状态
- 修复部分函数调用前未检查 FightC 是否为空的问题,增加 ErrBattleEnded 错误返回
- 调整
This commit is contained in:
2025-11-21 02:40:27 +08:00
parent 105c6f5a23
commit e54d4bacaa
18 changed files with 217 additions and 123 deletions

View File

@@ -11,27 +11,34 @@ import (
// 准备战斗
func (h Controller) OnReadyToFight(data *fight.ReadyToFightInboundInfo, c *player.Player) (result *fight.NullOutboundInfo, err errorcode.ErrorCode) {
if c.FightC == nil {
return nil, errorcode.ErrorCodes.ErrBattleEnded
}
c.FightC.ReadyFight(c)
return nil, -1
}
// 使用技能包
func (h Controller) UseSkill(data *fight.UseSkillInInfo, c *player.Player) (result *fight.NullOutboundInfo, err errorcode.ErrorCode) {
if c.FightC != nil {
c.FightC.UseSkill(c, int32(data.SkillId))
if c.FightC == nil {
return nil, errorcode.ErrorCodes.ErrBattleEnded
}
c.FightC.UseSkill(c, int32(data.SkillId))
return nil, 0
}
// 战斗逃跑
func (h Controller) Escape(data *fight.EscapeFightInboundInfo, c *player.Player) (result *fight.NullOutboundInfo, err errorcode.ErrorCode) {
if atomic.LoadUint32(&c.Fightinfo.Status) == 0 {
if c.FightC == nil {
return nil, errorcode.ErrorCodes.ErrBattleEnded
}
if atomic.LoadUint32(&c.Fightinfo.Mode) == 0 {
return nil, errorcode.ErrorCodes.ErrBattleNotStarted //,没开始对战
}
if atomic.LoadUint32(&c.Fightinfo.Status) == 1 { //用户对战不能逃跑
if atomic.LoadUint32(&c.Fightinfo.Mode) == 1 { //用户对战不能逃跑
return nil, errorcode.ErrorCodes.ErrCannotFleePlayerBattle
}
@@ -41,33 +48,39 @@ func (h Controller) Escape(data *fight.EscapeFightInboundInfo, c *player.Player)
// 切换精灵
func (h Controller) ChangePet(data *fight.ChangePetInboundInfo, c *player.Player) (result *fight.NullOutboundInfo, err errorcode.ErrorCode) {
if c.FightC != nil {
c.FightC.ChangePet(c, data.CatchTime)
if c.FightC == nil {
return nil, errorcode.ErrorCodes.ErrBattleEnded
}
c.FightC.ChangePet(c, data.CatchTime)
return nil, -1
}
// 切换精灵
func (h Controller) Capture(data *fight.CatchMonsterInboundInfo, c *player.Player) (result *info.CatchMonsterOutboundInfo, err errorcode.ErrorCode) {
if c.FightC == nil {
return nil, errorcode.ErrorCodes.ErrBattleEnded
}
c.FightC.Capture(c, data.CapsuleId)
return nil, -1
}
// 加载进度
func (h Controller) LoadPercent(data *fight.LoadPercentInboundInfo, c *player.Player) (result *info.LoadPercentOutboundInfo, err errorcode.ErrorCode) {
if c.FightC != nil {
c.FightC.LoadPercent(c, int32(data.Percent))
if c.FightC == nil {
return nil, errorcode.ErrorCodes.ErrBattleEnded
}
c.FightC.LoadPercent(c, int32(data.Percent))
return nil, -1
}
func (h Controller) UsePetItemInboundInfo(data *fight.UsePetItemInboundInfo, c *player.Player) (result *info.UsePetIteminfo, err errorcode.ErrorCode) {
if c.FightC != nil {
c.FightC.UseItem(c, data.CatchTime, data.ItemId)
if c.FightC == nil {
return nil, errorcode.ErrorCodes.ErrBattleEnded
}
c.FightC.UseItem(c, data.CatchTime, data.ItemId)
return nil, -1
}

View File

@@ -3,6 +3,7 @@ package controller
import (
"blazing/common/data/xmlres"
"blazing/common/socket/errorcode"
"fmt"
"math/rand"
"strings"
@@ -97,17 +98,23 @@ func (h Controller) PlayerFightBoss(data *fight.ChallengeBossInboundInfo, c *pla
}
c.Fightinfo.Status = info.BattleStatus.FIGHT_WITH_BOSS
c.Fightinfo.Status = info.BattleMode.FIGHT_WITH_NPC
c.Fightinfo.Mode = info.BattleMode.MULTI_MODE
ai := player.NewAI_player(moinfo)
//给予打过一次的奖励
event := c.Done.SPT(c.Info.MapID, data.BossId, 1, func() {
event := c.Done.SPT(c.Info.MapID, data.BossId, 1, func() bool {
fmt.Println("触发事件", "第一次奖励")
return true
})
event1 := c.Done.SPT(c.Info.MapID, data.BossId, 2, func() bool {
fmt.Println("触发事件", "第二次奖励")
return true
})
fight.NewFight(c, ai, func(foi *info.FightOverInfo) {
c.Done.Exec(model.MilestoneMode.BOSS, []uint32{data.BossId})
c.Done.Exec(model.MilestoneMode.BOSS, []uint32{c.Info.MapID, data.BossId})
event.Cancel() //取消事件
event1.Cancel()
})
@@ -133,9 +140,10 @@ func (h Controller) OnPlayerFightNpcMonster(data *fight.FightNpcMonsterInboundIn
moinfo.Nick = xmlres.PetMAP[int(mo.ID)].DefName
moinfo.PetList = append(moinfo.PetList, *mo)
ai := player.NewAI_player(moinfo)
ai.CanCapture = handleNPCFightSpecial(mo.ID)
c.Fightinfo.Status = info.BattleStatus.FIGHT_WITH_NPC
c.Fightinfo.Mode = info.BattleMode.MULTI_MODE
c.Fightinfo.Status = info.BattleMode.FIGHT_WITH_NPC //打野怪
c.Fightinfo.Mode = info.BattleMode.MULTI_MODE //多人模式
fight.NewFight(c, ai, func(foi *info.FightOverInfo) {
@@ -143,3 +151,15 @@ func (h Controller) OnPlayerFightNpcMonster(data *fight.FightNpcMonsterInboundIn
return nil, -1
}
func handleNPCFightSpecial(petid uint32) int {
npcPetID := int(petid)
petCfg, ok := xmlres.PetMAP[npcPetID]
if !ok {
// log.Error(context.Background(), "NPC宠物配置不存在", "petID", npcPetID)
return 0
}
catchRate := gconv.Int(petCfg.CatchRate)
return catchRate
}

View File

@@ -17,6 +17,10 @@ import (
// 都需要通过2419包广播更新擂台状态
func (h Controller) ARENA_SET_OWENR(data *fight.ARENA_SET_OWENR, c *player.Player) (result *fight.NullOutboundInfo, err errorcode.ErrorCode) {
if !c.CanFight() {
return nil, errorcode.ErrorCodes.ErrPokemonNotEligible
}
c.Fightinfo.Mode = 0 //取消队列匹配
if atomic.CompareAndSwapUint32(&c.GetSpace().Owner.Flag, 0, 1) {
c.GetSpace().Owner.UserID = c.GetInfo().UserID //添加用户ID
@@ -42,14 +46,28 @@ func (h Controller) ARENA_FIGHT_OWENR(data *fight.ARENA_FIGHT_OWENR, c *player.P
if !c.CanFight() {
return nil, errorcode.ErrorCodes.ErrNoEligiblePokemon
}
if c.Info.UserID == c.GetSpace().Owner.UserID {
return nil, errorcode.ErrorCodes.ErrNoEligiblePokemon
}
//原子操作,修改擂台状态
if atomic.CompareAndSwapUint32(&c.GetSpace().Owner.Flag, 1, 2) {
//成功发起擂台挑战后才修改我放状态
c.Fightinfo.Mode = info.BattleMode.SINGLE_MODE
c.Fightinfo.Status = info.BattleStatus.FIGHT_WITH_PLAYER
c.Fightinfo.Status = info.BattleMode.FIGHT_ARENA
_, err = fight.NewFight(c, c.GetSpace().ARENA_Player, func(foi *info.FightOverInfo) { //我方邀请擂主挑战,我方先手
if foi.Reason != 0 && foi.WinnerId == c.GetInfo().UserID { //异常退出
c.GetSpace().ARENA_Player = nil
c.GetSpace().Owner.Reset()
c.GetSpace().Broadcast(c, 2419, &c.GetSpace().Owner)
c.SendPackCmd(2419, &c.GetSpace().Owner)
}
//todo 擂主输了,那就直接让他放弃擂台
}) ///开始对战,房主方以及被邀请方
if err <= 0 { //发起战斗成功
@@ -58,10 +76,10 @@ func (h Controller) ARENA_FIGHT_OWENR(data *fight.ARENA_FIGHT_OWENR, c *player.P
//c.SendPackCmd(2419, &c.GetSpace().Owner)
} else {
//发起失败,改回1
c.GetSpace().Owner.HostWins = 0 //连胜重置
c.GetSpace().Owner.UserID = c.GetInfo().UserID //添加用户ID
c.GetSpace().Owner.Nick = c.GetInfo().Nick
c.GetSpace().ARENA_Player = c //添加用户
// c.GetSpace().Owner.HostWins = 0 //连胜重置
// c.GetSpace().Owner.UserID = c.GetInfo().UserID //添加用户ID
// c.GetSpace().Owner.Nick = c.GetInfo().Nick
// c.GetSpace().ARENA_Player = c //添加用户
atomic.StoreUint32(&c.GetSpace().Owner.Flag, 1)
}

View File

@@ -14,10 +14,10 @@ import (
func (h Controller) PET_MELEE(data *fight.StartPetWarInboundInfo, c *player.Player) (result *fight.NullOutboundInfo, err errorcode.ErrorCode) {
c.Fightinfo.Mode = info.BattleMode.PET_MELEE
c.Fightinfo.Status = info.BattleStatus.FIGHT_WITH_PLAYER
c.Fightinfo.Status = info.BattleMode.PET_MELEE
err = c.JoinFight(func(p common.PlayerI) bool {
fight.NewFight(p, c, func(foi *info.FightOverInfo) {
_, err = fight.NewFight(p, c, func(foi *info.FightOverInfo) {
if foi.Reason == 0 { //我放获胜
if foi.WinnerId == c.GetInfo().UserID {
@@ -29,6 +29,10 @@ func (h Controller) PET_MELEE(data *fight.StartPetWarInboundInfo, c *player.Play
}
}) ///开始对战,房主方以及被邀请方
if err > 0 { //说明有报错
return false
}
return true
})
@@ -37,7 +41,7 @@ func (h Controller) PET_MELEE(data *fight.StartPetWarInboundInfo, c *player.Play
}
func (h Controller) PET_King(data *fight.PetKingJoinInboundInfo, c *player.Player) (result *fight.NullOutboundInfo, err errorcode.ErrorCode) {
c.Fightinfo.Status = info.BattleStatus.FIGHT_WITH_PLAYER
c.Fightinfo.Status = info.BattleMode.PET_TOPLEVEL
switch data.Type {
case 5:

View File

@@ -12,28 +12,32 @@ import (
// 接收战斗或者取消战斗的包
func (h Controller) OnPlayerHandleFightInvite(data *fight.HandleFightInviteInboundInfo, c *player.Player) (result *fight.NullOutboundInfo, err errorcode.ErrorCode) {
if !atomic.CompareAndSwapUint32(&c.Fightinfo.Status, 0, 1) { //邀请前提是自己没在战斗
if c.GetSpace().Owner.UserID == c.Info.UserID {
return nil, errorcode.ErrorCodes.ErrSystemError
}
if c.GetSpace().Owner.UserID == data.UserID {
return nil, errorcode.ErrorCodes.ErrSystemError
}
if !atomic.CompareAndSwapUint32(&c.Fightinfo.Mode, 0, data.Mode) { //邀请前提是自己没在战斗
return nil, errorcode.ErrorCodes.ErrInBattle
}
//c.Fightinfo.PlayerID = c.Info.UserID
c.Fightinfo.Mode = data.Mode
c.Fightinfo.Status = 1
c.Fightinfo.Type = 0
//c.Fightinfo.Status = info.BattleMode.FIGHT_WITH_NPC
resp := &info.S2C_NOTE_HANDLE_FIGHT_INVITE{
UserID: c.Info.UserID,
Nick: c.Info.Nick,
}
for _, v := range c.HavePVPinfo {
if v.GetInfo().UserID == data.UserID {
if v.GetInfo().UserID == data.UserID && v.Getfightinfo().Mode == data.Mode {
resp.Result = data.Flag
// 检查邀请者的邀请是否有效(对方已取消邀请)
if v.Getfightinfo().Status == 0 {
resp.Result = 4 // 邀请已取消
v.SendPackCmd(2502, &resp)
atomic.StoreUint32(&c.Fightinfo.Status, 0)
atomic.StoreUint32(&c.Fightinfo.Mode, 0)
return
}
@@ -47,7 +51,7 @@ func (h Controller) OnPlayerHandleFightInvite(data *fight.HandleFightInviteInbou
} else {
resp.Result = 3
v.SendPackCmd(2502, &resp)
atomic.StoreUint32(&c.Fightinfo.Status, 0)
atomic.StoreUint32(&c.Fightinfo.Mode, 0)
return
}
@@ -59,7 +63,15 @@ func (h Controller) OnPlayerHandleFightInvite(data *fight.HandleFightInviteInbou
// 邀请其他人进行战斗
func (h Controller) OnPlayerInviteOtherFight(data *fight.InviteToFightInboundInfo, c *player.Player) (result *fight.NullOutboundInfo, err errorcode.ErrorCode) {
if c.GetSpace().Owner.UserID == c.Info.UserID {
return nil, errorcode.ErrorCodes.ErrSystemError
}
if c.GetSpace().Owner.ChallengerID == c.Info.UserID {
return nil, errorcode.ErrorCodes.ErrSystemError
}
if c.GetSpace().Owner.UserID == data.UserID {
return nil, errorcode.ErrorCodes.ErrSystemError
}
// c.Fightinfo.PlayerID = data.UserID
c.Fightinfo.Mode = data.Mode
c.Fightinfo.Status = 1
@@ -76,6 +88,6 @@ func (h Controller) OnPlayerInviteOtherFight(data *fight.InviteToFightInboundInf
// 取消队列
func (h Controller) OnPlayerCanceledOtherInviteFight(data *fight.InviteFightCancelInboundInfo, c *player.Player) (result *fight.NullOutboundInfo, err errorcode.ErrorCode) {
atomic.StoreUint32(&c.Fightinfo.Status, 0) //设置状态为0
atomic.StoreUint32(&c.Fightinfo.Mode, 0) //设置状态为0
return
}

View File

@@ -23,6 +23,7 @@ func (h *Controller) GetPetInfo(
result = &pet.OutInfo{
PetInfo: pi,
}
return result, 0
}
}