hch
2026-01-26 aa84cb62bebb9c8a4e586bcc1ec28eb7a16a8860
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
 
public class CustomHB426CombinePack : GameNetPackBasic
{
 
    public HB426_tagSCTurnFightTag startTag;
 
    public HB426_tagSCTurnFightTag endTag;
 
    public int fromIndex;
 
    public int toIndex;
 
    public string guid;
 
    public List<GameNetPackBasic> packList = new List<GameNetPackBasic>();
 
#if UNITY_EDITOR
    // 编辑器下保存packList的副本,避免distribute过程中remove导致查看器显示不全
    public List<GameNetPackBasic> copiedPackList = new List<GameNetPackBasic>();
#endif
 
    private bool isCreated = false;
 
    public override void ReadFromBytes(byte[] vBytes)
    {
        base.ReadFromBytes(vBytes);
    }
 
    public void SetHB426End(int _toIndex, HB426_tagSCTurnFightTag _endTag)
    {
        if (_endTag.Sign != 1)
        {
            Debug.LogError("HB426_tagSCTurnFightTag Sign must be 1 for end tag, but got: " + _endTag.Sign);
            return;
        }
        endTag = _endTag;
        toIndex = _toIndex;
        PrintAllPack();
    }
 
 
    public void SetHB426Start(int _fromIndex, HB426_tagSCTurnFightTag tag)
    {
        if (tag.Sign != 0)
        {
            Debug.LogError("HB426_tagSCTurnFightTag Sign must be 0 for start tag, but got: " + tag.Sign);
            return;
        }
        fromIndex = _fromIndex;
        startTag = tag;
        
    }
 
    public void PrintAllPack()
    {
        string temp = "CustomHB426CombinePack startTag: " + (startTag != null ? startTag.Tag : "null") +
            " fromIndex: " + fromIndex + " toIndex: " + toIndex + " packList.Count: " + packList.Count + "\n";
        for (int i = 0; i < packList.Count; i++)
        {
            var pack = packList[i];
            if (pack is CustomHB426CombinePack b426Pack)
            {
                temp += "  pack type is " + pack.GetType().Name + " tag is " + (b426Pack.startTag != null ? b426Pack.startTag.Tag : "null") + "\n";
            }
            else
            {
                temp += "  pack type is " + pack.GetType().Name + "\n";
            }
        }
        BattleDebug.LogError(temp);
    }
 
    public void AddPack(GameNetPackBasic pack)
    {
        if (startTag == null)
        {
            Debug.LogError("startTag is null, please set it first.");
            return;
        }
 
        packList.Add(pack);
 
#if UNITY_EDITOR
        copiedPackList.Add(pack);
#endif
 
    }
 
    public bool IsEndPack(HB426_tagSCTurnFightTag tag)
    {
        if (tag == null)
        {
            return false;
        }
 
        if (startTag == null)
        {
            Debug.LogError("startTag is null, please set it first.");
            return false;
        }
 
        return startTag.Tag == tag.Tag && tag.Sign == 1;
    }
 
    public static List<GameNetPackBasic> CombineToSkillPackFromList(string _guid, List<GameNetPackBasic> b421SeriesPackList)
    {
        Dictionary<string, CustomHB426CombinePack> combineDict = new Dictionary<string, CustomHB426CombinePack>();
 
        Dictionary<int, GameNetPackBasic> indexDict = new Dictionary<int, GameNetPackBasic>();
 
        for (int i = 0; i < b421SeriesPackList.Count; i++)
        {
            var pack = b421SeriesPackList[i];
 
            if (pack is HB426_tagSCTurnFightTag)
            {
                var tag = pack as HB426_tagSCTurnFightTag;
                CustomHB426CombinePack combinePack;
 
                if (!combineDict.TryGetValue(tag.Tag, out combinePack))
                {
                    combinePack = new CustomHB426CombinePack();
                    combinePack.guid = _guid;
                    combineDict.Add(tag.Tag, combinePack);
 
                    indexDict.Add(i, combinePack);
                }
 
                if (tag.Sign == 0)
                {
                    combinePack.SetHB426Start(i, tag);
                    continue;
                }
                //  找到对应的开始标签
                if (combinePack.IsEndPack(tag))
                {
                    combinePack.SetHB426End(i, tag);
                    continue;
                }
 
                Debug.LogError("No matching start tag or end tag found: " + tag.Tag);
                continue;
            }
            else
            {
                indexDict.Add(i, pack);
            }
        }
 
        // 如果是被 HB426_tagSCTurnFightTag 包裹的第一个技能包 并且前面没有CustomHB426CombinePack 
        // 翻译一下就是 如果CombinePack里的主体Skill包 skillPack.RelatedSkillID > 0
 
 
 
        //  嵌套包内的包合并
        foreach (var combinePack in combineDict.Values)
        {
            for (int i = combinePack.fromIndex + 1; i < combinePack.toIndex; i++)
            {
                if (indexDict.TryGetValue(i, out var pack))
                {
                    indexDict.Remove(i);
                    combinePack.AddPack(pack);
                    if (pack is CustomHB426CombinePack cbPack)
                    {
                        //  如果是嵌套的包 加入之后 调整i
                        i = cbPack.toIndex;
                    }
                }
            }
        }
 
        List<GameNetPackBasic> newPackList = new List<GameNetPackBasic>();
 
        // string temp = string.Empty;
 
        for (int i = 0; i < b421SeriesPackList.Count; i++)
        {
            if (indexDict.TryGetValue(i, out var pack))
            {
                newPackList.Add(pack);
            }
        }
 
        // newPackList = BattleManager.ParseBattlePackList(_guid, newPackList);
 
        // Debug.LogError(temp);
 
        return newPackList;
    }
 
    public HB427_tagSCUseSkill GetMainHB427SkillPack()
    {
        for (int i = 0; i < packList.Count; i++)
        {
            var pack = packList[i];
            if (pack is HB427_tagSCUseSkill skillPack)
            {
                return skillPack;
            }
            else if (pack is CustomHB426CombinePack)
            {
                return null;
            }
        }
 
        return null;
    }
 
    public bool IsInnerCBPackContainsSkill(HB427_tagSCUseSkill skill, ref CustomHB426CombinePack innerCBPack)
    {
        for (int i = 0; i < packList.Count; i++)
        {
            var pack = packList[i];
            if (pack is CustomHB426CombinePack cbPack)
            {
                if (cbPack.packList.Contains(skill))
                {
                    innerCBPack = cbPack;
                    return true;
                }
            }
        }
        return false;
    }
    public bool Distribute(RecordAction parentAction = null)
    {
        BattleField battleField = BattleManager.Instance.GetBattleField(guid);
 
        if (null == battleField)
        {
            Debug.LogError("BattleField not found for guid: " + guid);
            return false;
        }
 
        var skillAction = CreateSkillAction();
        if (null != skillAction)
        {
            if (parentAction != null)
            {
                parentAction.GetInnerRecordPlayer().PlayRecord(skillAction);
            }
            else
            {
                battleField.PlayRecord(skillAction);
            }
        }
        else
        {
            battleField.DistributeNextPackage();
            return false;
        }
 
        return true;
 
    }
 
    public static SkillRecordAction CreateSkillAction(string guid, List<GameNetPackBasic> _packList)
    {
        BattleField battleField = BattleManager.Instance.GetBattleField(guid);
 
        if (null == battleField)
        {
            Debug.LogError("BattleField not found for guid: " + guid);
            return null;
        }
 
        while (_packList.Count > 0)
        {
            var pack = _packList[0];
            _packList.RemoveAt(0);
 
            if (pack is HB427_tagSCUseSkill)
            {
                _packList.Insert(0, pack);
                break;
            }
            else if (pack is CustomHB426CombinePack)
            {
                Debug.LogError("无法找到Skill包,先发现了嵌套包");
                return null;
            }
            else
            {
                pack.commonMark = true;
                // 【使用 BattleField.recordPlayer】
                // 原因:这里是在CreateSkillAction静态方法中,还没有创建SkillRecordAction
                // 这些包是在找到技能包之前遇到的,需要提前处理掉
                // 此时没有RecordAction上下文,应该使用BattleField的主RecordPlayer
                PackageRegedit.Distribute(pack);
            }
        }
 
        HB427_tagSCUseSkill skill = _packList[0] as HB427_tagSCUseSkill;
        _packList.RemoveAt(0);
        if (null == skill)
        {
            Debug.LogError("No HB427_tagSCUseSkill found in packList.");
            return null;
        }
 
        BattleObject _caster = battleField.battleObjMgr.GetBattleObject((int)skill.ObjID);
 
        SkillRecordAction skillAction = new SkillRecordAction(battleField, _caster, skill, _packList);
        skillAction.tracePackUID = skill.packUID;
        return skillAction;
 
    }
 
    public SkillRecordAction CreateSkillAction()
    {
        BattleField battleField = BattleManager.Instance.GetBattleField(guid);
 
        if (null == battleField)
        {
            Debug.LogError("BattleField not found for guid: " + guid);
            return null;
        }
 
        if (isCreated)
        {
            Debug.LogError("create skill twice check the code");
            return null;
        }
 
        //  服务器修改了 现在第一个不一定是B427 也有可能是B428 先放Buff
        while (packList.Count > 0)
        {
            var pack = packList[0];
            packList.RemoveAt(0);
 
            if (pack is HB427_tagSCUseSkill)
            {
                packList.Insert(0, pack);
                break;
            }
            else if (pack is CustomHB426CombinePack)
            {
                Debug.LogError("无法找到Skill包,先发现了嵌套包 " + startTag.Tag);
                //  保持原样 不去动他
                packList.Insert(0, pack);
                return null;
            }
            else
            {
                pack.commonMark = true;
                // 【使用 BattleField.recordPlayer】
                // 原因:这里是在CreateSkillAction方法中,还没有创建SkillRecordAction
                // 这些包是在找到技能包之前遇到的(如Buff包),需要提前处理
                // 此时没有RecordAction上下文,应该使用BattleField的主RecordPlayer
                PackageRegedit.Distribute(pack);
            }
        }
 
        if (startTag.Tag.StartsWith("Skill_"))
        {
            if (packList.Count <= 0)
            {
                Debug.LogError("No HB427_tagSCUseSkill found in packList." + startTag.Tag);
                return null;
            }
            HB427_tagSCUseSkill skill = packList[0] as HB427_tagSCUseSkill;
            if (null == skill)
            {
                Debug.LogError("No HB427_tagSCUseSkill found in packList.");
                return null;
            }
 
            packList.RemoveAt(0);
 
 
            BattleObject _caster = battleField.battleObjMgr.GetBattleObject((int)skill.ObjID);
            // BattleDebug.LogError("_caster == null : " + (_caster == null) + " skill.ObjID : " + skill.ObjID);
            isCreated = true;
            SkillRecordAction skillAction = new SkillRecordAction(battleField, _caster, skill, packList);
            skillAction.tracePackUID = packUID;
            return skillAction;
        }
 
        return null;
    }
 
    public bool NeedWaiting()
    {
        bool needWaiting = false;
 
        HB427_tagSCUseSkill hB427_TagSCUseSkill = GetMainHB427SkillPack();
 
        for (int i = 0; i < packList.Count; i++)
        {
            var pack = packList[i];
            if (pack is HB427_tagSCUseSkill skillPack && skillPack != hB427_TagSCUseSkill)
            {
                SkillConfig ssc = SkillConfig.Get((int)skillPack.SkillID);
                if (!string.IsNullOrEmpty(ssc.SkillMotionName))
                {
                    needWaiting = true;
                    break;
                }
            }
            else if (pack is HB422_tagMCTurnFightObjDead dead)
            {
                needWaiting = true;
                break;
            }
            else if (pack is CustomHB426CombinePack combinePack)
            {
                if (combinePack.NeedWaiting())
                {
                    needWaiting = true;
                    break;
                }
            }
        }
 
 
        return needWaiting;
    }
}