fix: restore battle entry and character profile initialization

This commit is contained in:
CAtlas0784 committed 2026-10-17 11:58:20 +07:00
1 parent cfbbc7b970
commit 5c4d0d4149
16 files changed
+888 -228

No files matched your search

+113 -186
View File
@@ -10,6 +10,10 @@ import (
goproto "gac-hkrpg-go/protocol/proto"
)
// PearlSR 005d2c1 protocol/src/protocol.pb.zig: MonsterBattleType.
// The Go schema retains obfuscated enum labels; zero is NONE, not TRIGGER.
const monsterBattleTrigger goproto.MonsterBattleType = 1
func init() {
handle("StartCocoonStage", func(s *Session, req *goproto.StartCocoonStageCsReq, rsp *goproto.StartCocoonStageScRsp) error {
Log(fmt.Sprintf("[BATTLE] StartCocoonStage: CocoonId=%d Wave=%d WorldLevel=%d PropEntityId=%d", req.CocoonId, req.Wave, req.WorldLevel, req.PropEntityId))
@@ -17,7 +21,10 @@ func init() {
rsp.PropEntityId = req.PropEntityId
rsp.CocoonId = req.CocoonId
rsp.Wave = req.Wave
battleInfo := createBattleInfo(s, 0, 0)
battleInfo, err := createBattleInfo(s, 0, 0)
if err != nil {
return err
}
CURRENT_BATTLE.Store(s.Token, battleInfo)
rsp.BattleInfo = battleInfo
return nil
@@ -29,7 +36,10 @@ func init() {
rsp.Retcode = 0
rsp.CocoonId = req.CocoonId
rsp.Wave = req.Wave
battleInfo := createBattleInfo(s, 0, 0)
battleInfo, err := createBattleInfo(s, 0, 0)
if err != nil {
return err
}
CURRENT_BATTLE.Store(s.Token, battleInfo)
rsp.BattleInfo = battleInfo
@@ -38,7 +48,10 @@ func init() {
handleNamed("StartQuickFarmElement", "StartQuickFarmElementRsp", func(s *Session, req *goproto.StartQuickFarmElementCsReq, rsp *goproto.StartQuickFarmElementRsp) error {
Log(fmt.Sprintf("[BATTLE] StartQuickFarmElement: FarmID=%d WorldLevel=%d", req.PAOFHFLFFHD, req.WorldLevel))
battleInfo := createBattleInfo(s, 0, 0)
battleInfo, err := createBattleInfo(s, 0, 0)
if err != nil {
return err
}
CURRENT_BATTLE.Store(s.Token, battleInfo)
rsp.Retcode = 0
@@ -86,7 +99,10 @@ func init() {
})
handle("SceneEnterStage", func(s *Session, req *goproto.SceneEnterStageCsReq, rsp *goproto.SceneEnterStageScRsp) error {
battleInfo := createBattleInfo(s, 0, 0)
battleInfo, err := createBattleInfo(s, 0, 0)
if err != nil {
return err
}
CURRENT_BATTLE.Store(s.Token, battleInfo)
rsp.BattleInfo = battleInfo
return nil
@@ -100,26 +116,8 @@ func init() {
return nil
})
// ================================================================
// SceneCastSkill — THE CAMERA-DETACH BUG FIX
// (บัคกล้องหลุดจากตัวละครตอนกดตี)
//
// อาการ: กดโจมตีใน overworld แล้วกล้องหลุดออกจากตัวละคร
//
// สาเหตุ (จากโค้ด Rust เดิม): handler ตอบ_info + monster_battle_info
// ให้ client เข้าสู่การต่อสู้กับ target ที่ "ผ่าน filter แบบหลวมๆ"
// filter เดิม: id > 30000 (monster ในฉาก) || id < 1000 (avatar entity ของผู้เล่น!)
// ถ้า targets ว่างและอยู่ใน arena (entry >= 3000000) ยัง "หลอก" target
// เป็น 30001 อีกชั้น
// พอ client ได้_info กับ "monster" ที่จริงคือ prop/NPC/ตัวผู้เล่นเอง
// หรือ monster ที่ไม่มี event_id มันจะเริ่ม transition ที่โหลดไม่สำเร็จ
// → camera ไม่มี target ให้ตาม → กล้องหลุดจากตัวละคร
//
// FIX: ยอมรับเฉพาะ entity ที่ server spawn เป็น "monster จริง" ในฉากปัจจุบัน
// (เก็บไว้ใน Session.MonsterEntities ตอน load_scene) ถ้าไม่มี monster จริง
// ถูกโจมตี → ตอบ retcode 0 + cast_entity_id เฉยๆ ไม่เข้า
// client จะเล่นแอนิเมชันตีปกติ กล้องค้างกับตัวละคร
// ================================================================
// Consume every target carrier in the current schema. Only entities sent
// as monsters in this scene may start a battle; never fabricate a target.
handle("SceneCastSkill", func(s *Session, req *goproto.SceneCastSkillCsReq, rsp *goproto.SceneCastSkillScRsp) error {
rsp.CastEntityId = req.CastEntityId
@@ -138,6 +136,14 @@ func init() {
for _, id := range req.AssistMonsterEntityIdList {
addTarget(id)
}
for _, group := range req.AssistMonsterEntityInfo {
for _, id := range group.GetEntityIdList() {
addTarget(id)
}
}
SceneDebugLog("SCENE_CAST_SKILL cast_entity=%d caster=%d skill_index=%d hits=%v assists=%v groups=%v scene_monsters=%v matched=%v",
req.CastEntityId, req.CasterId, req.SkillIndex, req.HitTargetEntityIdList,
req.AssistMonsterEntityIdList, req.AssistMonsterEntityInfo, s.MonsterEntities, targets)
if len(targets) == 0 {
// ไม่มี monster จริงถูกตี — ไม่เข้า (นี่คือ fix หลัก)
@@ -150,12 +156,16 @@ func init() {
SceneDebugLog("SCENE_CAST_SKILL cast_entity=%d caster=%d -> with %d monster(s)",
req.CastEntityId, req.CasterId, len(targets))
battleInfo := createBattleInfo(s, req.CasterId, req.SkillIndex)
battleInfo, err := createBattleInfo(s, req.CasterId, req.SkillIndex)
if err != nil {
return err
}
CURRENT_BATTLE.Store(s.Token, battleInfo)
for _, id := range targets {
rsp.MonsterBattleInfo = append(rsp.MonsterBattleInfo, &goproto.HitMonsterBattleInfo{
TargetMonsterEntityId: id,
MonsterBattleType: monsterBattleTrigger,
})
}
rsp.BattleInfo = battleInfo
@@ -183,23 +193,26 @@ func init() {
}
// createBattleInfo is the Go port of.rs create_battle_info.
func createBattleInfo(s *Session, casterID, skillIndex uint32) *goproto.SceneBattleInfo {
func createBattleInfo(s *Session, casterID, skillIndex uint32) (*goproto.SceneBattleInfo, error) {
player := s.JSON()
if player == nil {
Log("data is not set!")
return &goproto.SceneBattleInfo{}
return nil, fmt.Errorf("battle: player data is not loaded")
}
rules, err := loadBattleRules()
if err != nil {
return nil, err
}
battleInfo := &goproto.SceneBattleInfo{
StageId: player.BattleConfig.StageID,
LogicRandomSeed: randUint32(),
BattleId: 1,
RoundsLimit: player.BattleConfig.CycleCount,
WorldLevel: 6,
StageId: player.BattleConfig.StageID,
LogicRandomSeed: randUint32(),
BattleId: 1,
RoundsLimit: player.BattleConfig.CycleCount,
WorldLevel: 6,
BattleTargetInfo: make(map[uint32]*goproto.BattleTargetList),
}
isCalyx := casterID == 0 && skillIndex == 0
hasDahlia := false
firstAvatarID := uint32(0)
firstAvatarIdx := uint32(9999)
@@ -217,14 +230,6 @@ func createBattleInfo(s *Session, casterID, skillIndex uint32) *goproto.SceneBat
// avatars
lineup(func(avatarIndex, lineupID uint32) {
if firstAvatarID == 0 {
firstAvatarID = lineupID
firstAvatarIdx = avatarIndex
}
if !hasDahlia {
hasDahlia = lineupID == 1321
}
avatarID := lineupID
isTrailblazer := lineupID == 8001
isMarch := lineupID == 1001
@@ -239,81 +244,51 @@ func createBattleInfo(s *Session, casterID, skillIndex uint32) *goproto.SceneBat
return
}
if firstAvatarID == 0 {
firstAvatarID = avatarID
firstAvatarIdx = avatarIndex
}
battleAvatar, techs := avatar.ToBattleAvatarProto(
avatarIndex,
player.FindLightconeFor(avatarID),
player.FindRelicsFor(avatarID),
)
battleInfo.BuffList = append(battleInfo.BuffList, techs...)
res, err := common.GameRes()
if casterID > 0 && avatarIndex == casterID-1 && err == nil {
if avatarConfig, okc := res.AvatarConfigs[avatarID]; okc && !containsU32(avatar.Techniques, 1000119) {
battleInfo.BuffList = append(battleInfo.BuffList, &goproto.BattleBuff{
Id: avatarConfig.WeaknessBuffID,
Level: 1,
OwnerId: avatarIndex,
WaveFlag: 0xffffffff,
DynamicValues: map[string]float32{"SkillIndex": float32(skillIndex)},
})
}
}
battleInfo.BuffList = append(battleInfo.BuffList, rules.techniqueBuffs(avatar, avatarIndex, firstAvatarIdx, techs)...)
battleInfo.BattleAvatarList = append(battleInfo.BattleAvatarList, battleAvatar)
// hardcoded march buff
if avatar.AvatarID == 1224 {
battleInfo.BuffList = append(battleInfo.BuffList, &goproto.BattleBuff{
Id: 122401,
Level: 3,
WaveFlag: 0xffffffff,
OwnerId: avatarIndex,
DynamicValues: map[string]float32{"#ADF_1": 3, "#ADF_2": 3},
TargetIndexList: []uint32{0},
})
}
})
// resolve character-specific technique buffs dynamically
firstAvatarAttackID := uint32(0)
if res, err := common.GameRes(); err == nil {
if cfg, ok := res.AvatarConfigs[firstAvatarID]; ok {
firstAvatarAttackID = cfg.WeaknessBuffID
}
if len(battleInfo.BattleAvatarList) == 0 {
return nil, fmt.Errorf("battle: lineup has no available avatars")
}
len0 := len(battleInfo.BuffList)
hasReplaced := false
var dahliaBuffs []*goproto.BattleBuff
for i, buff := range battleInfo.BuffList {
isLast := i == len0-1
if buff.Id == 141202 || buff.Id == 141403 {
buff.OwnerId = firstAvatarIdx
continue
}
if hasDahlia && isCalyx && buff.Id >= 1000111 && buff.Id <= 1000117 && !hasReplaced && firstAvatarAttackID != 0 {
buff.Id = firstAvatarAttackID
buff.OwnerId = firstAvatarIdx
hasReplaced = true
}
if hasDahlia && isCalyx && !hasReplaced && firstAvatarAttackID != 0 && isLast {
dahliaBuffs = append(dahliaBuffs,
&goproto.BattleBuff{
Id: 1000121, Level: 1, WaveFlag: 0xffffffff,
TargetIndexList: []uint32{firstAvatarIdx},
},
&goproto.BattleBuff{
Id: firstAvatarAttackID, Level: 1, WaveFlag: 0xffffffff,
TargetIndexList: []uint32{firstAvatarIdx},
DynamicValues: map[string]float32{"SkillIndex": float32(firstAvatarIdx)},
},
)
// PearlSR always supplies an entry attack, including Calyx. Resolve the
// actor from the scene's actual slot IDs, rather than assuming cast_entity
// identifies a lineup position.
attackSlot, attackID := firstAvatarIdx, firstAvatarID
for _, avatar := range battleInfo.BattleAvatarList {
if casterID != 0 && avatar.Index+1 == casterID {
attackSlot, attackID = avatar.Index, avatar.Id
break
}
}
battleInfo.BuffList = append(battleInfo.BuffList, dahliaBuffs...)
res, err := common.GameRes()
if err != nil {
return nil, fmt.Errorf("battle: load avatar resources: %w", err)
}
cfg := res.AvatarConfigs[attackID]
attackBuffID := rules.EntryAttackBuffs[cfg.DamageType]
if attackBuffID == 0 {
attackBuffID = cfg.WeaknessBuffID
}
if attackBuffID != 0 {
entrySkillIndex := skillIndex
if casterID == 0 {
entrySkillIndex = 1
}
battleInfo.BuffList = append(battleInfo.BuffList, battleBuff(attackBuffID, attackSlot, map[string]float32{"SkillIndex": float32(entrySkillIndex)}))
} else {
return nil, fmt.Errorf("battle: avatar %d has no entry attack in res.json", attackID)
}
// custom stats for avatars
for _, stat := range player.BattleConfig.CustomStats {
@@ -344,15 +319,16 @@ func createBattleInfo(s *Session, casterID, skillIndex uint32) *goproto.SceneBat
// blessings
for _, blessing := range player.BattleConfig.Blessings {
buff := &goproto.BattleBuff{
Id: blessing.ID,
Level: blessing.Level,
WaveFlag: 0xffffffff,
OwnerId: 0xffffffff,
Id: blessing.ID,
Level: blessing.Level,
WaveFlag: 0xffffffff,
OwnerId: 0xffffffff,
TargetIndexList: []uint32{0},
}
if blessing.DynamicKey != nil {
buff.DynamicValues = map[string]float32{blessing.DynamicKey.Key: float32(blessing.DynamicKey.Value)}
} else {
buff.DynamicValues = map[string]float32{}
buff.DynamicValues = map[string]float32{"SkillIndex": 0}
}
for _, dv := range blessing.DynamicValues {
if _, exists := buff.DynamicValues[dv.Key]; exists {
@@ -363,31 +339,7 @@ func createBattleInfo(s *Session, casterID, skillIndex uint32) *goproto.SceneBat
battleInfo.BuffList = append(battleInfo.BuffList, buff)
}
// pf score object
if player.BattleConfig.BattleType == common.BattleTypePF {
if battleInfo.StageId >= 30309011 {
battleInfo.BattleTargetInfo[1] = &goproto.BattleTargetList{
BattleTargetList: []*goproto.BattleTarget{{Id: 10003}},
}
} else {
battleInfo.BattleTargetInfo[1] = &goproto.BattleTargetList{
BattleTargetList: []*goproto.BattleTarget{{Id: 10002}},
}
}
for i := uint32(2); i <= 4; i++ {
battleInfo.BattleTargetInfo[i] = &goproto.BattleTargetList{}
}
battleInfo.BattleTargetInfo[5] = &goproto.BattleTargetList{
BattleTargetList: []*goproto.BattleTarget{{Id: 2001}, {Id: 2002}},
}
}
// Apocalyptic Shadow
if player.BattleConfig.BattleType == common.BattleTypeAS {
battleInfo.BattleTargetInfo[1] = &goproto.BattleTargetList{
BattleTargetList: []*goproto.BattleTarget{{Id: 90005}},
}
}
rules.addTargets(battleInfo, player.BattleConfig.BattleType)
// SU
if player.BattleConfig.BattleType == common.BattleTypeSU {
@@ -403,58 +355,33 @@ func createBattleInfo(s *Session, casterID, skillIndex uint32) *goproto.SceneBat
battleInfo.MonsterWaveList = common.ToSceneMonsterWaves(player.BattleConfig.Monsters)
battleInfo.JAOIOKBBMFC = uint32(len(battleInfo.MonsterWaveList))
// global buffs
hasCastorice, hasSilverWolf := false, false
hasAha := false
for _, buff := range battleInfo.BuffList {
if buff.Id == 140703 {
hasCastorice = true
}
if buff.Id == 150602 {
hasSilverWolf = true
}
if buff.Id == 151104 {
hasAha = true
}
}
if !hasCastorice && (player.EnableCastoriceGlobal == nil || *player.EnableCastoriceGlobal) {
battleInfo.BuffList = append(battleInfo.BuffList, &goproto.BattleBuff{
Id: 140703, Level: 1, OwnerId: 0xffffffff, WaveFlag: 0xffffffff,
})
}
if !hasSilverWolf && (player.EnableSWGlobal == nil || *player.EnableSWGlobal) {
battleInfo.BuffList = append(battleInfo.BuffList, &goproto.BattleBuff{
Id: 150602, Level: 1, OwnerId: 0xffffffff, WaveFlag: 0xffffffff,
})
}
if !hasAha && (player.EnableAhaGlobal == nil || *player.EnableAhaGlobal) {
battleInfo.BuffList = append(battleInfo.BuffList, &goproto.BattleBuff{
Id: 151104, Level: 1, OwnerId: 0xffffffff, WaveFlag: 0xffffffff,
})
}
rules.addGlobalPassives(battleInfo, player)
// Stage invasion buff (pearl-sr 4.6.53: e.g. Peak 11 stage 4 30511021/30511022 -> buff 3034004)
if invasionID := getStageInvasionID(battleInfo.StageId); invasionID != 0 {
battleInfo.BuffList = append(battleInfo.BuffList, &goproto.BattleBuff{
Id: 3034000 + invasionID,
Level: 1,
OwnerId: firstAvatarIdx,
WaveFlag: 0xffffffff,
TargetIndexList: []uint32{0},
DynamicValues: map[string]float32{"SkillIndex": 1},
})
// Aha Invasion support buff
battleInfo.BuffList = append(battleInfo.BuffList, &goproto.BattleBuff{
Id: 3034013,
Level: 1,
OwnerId: firstAvatarIdx,
WaveFlag: 0xffffffff,
TargetIndexList: []uint32{0},
DynamicValues: map[string]float32{"SkillIndex": 1},
})
// Invasion support belongs to an active challenge, not a custom battle
// merely reusing the same stage. The scene loader records that state.
if s.ChallengeStageID != 0 {
if invasionID := getStageInvasionID(s.ChallengeStageID); invasionID != 0 {
buffID, ok := rules.InvasionBuffs[invasionID]
if !ok {
return nil, fmt.Errorf("battle: invasion %d has no buff resource", invasionID)
}
battleInfo.BuffList = append(battleInfo.BuffList, battleBuff(buffID, firstAvatarIdx, map[string]float32{"SkillIndex": 1}))
if player.BattleConfig.EnableInvasionSupport {
battleInfo.BuffList = append(battleInfo.BuffList, battleBuff(rules.InvasionSupportBuff, firstAvatarIdx, map[string]float32{"SkillIndex": 1}))
}
}
}
if len(battleInfo.MonsterWaveList) == 0 {
return nil, fmt.Errorf("battle: stage %d has no configured waves", battleInfo.StageId)
}
for i, wave := range battleInfo.MonsterWaveList {
if len(wave.MonsterList) == 0 {
return nil, fmt.Errorf("battle: stage %d wave %d is empty", battleInfo.StageId, i)
}
}
Log(fmt.Sprintf("[BATTLE] prepared stage=%d avatars=%d waves=%d buffs=%d challenge_stage=%d", battleInfo.StageId, len(battleInfo.BattleAvatarList), len(battleInfo.MonsterWaveList), len(battleInfo.BuffList), s.ChallengeStageID))
return battleInfo
return battleInfo, nil
}
type stageInvasionConfigData struct {