M2 回合制调度:速度/阵营/队伍顺序规则、先制第一轮、4v4 调试入口与调度测试
- BattleRuntime 调度替换为完整规则:速度高优先、同速玩家优先、同阵营同速 PartyOrder 小优先;先制第一轮玩家全先行,第二轮恢复普通排序;RemainingTurnOrder 同步按调度序输出 - TurnBattleComponent 开局行动者为敌人时自动推进,协程等待 UI 监听注册避免先手事件丢失 - 调试入口支持 1v1 / 2v2 / 4v4 与先制开关;新增敌人队伍预设 4005(四普通敌人) - UI:修复 UIForm 复用导致第二次开战按钮监听丢失;EnemySlots/TurnSlots 改为按数量复用 item,图标同一单位不重复加载 - EditMode 新增 10 个调度用例(共 35 个):排序规则、先制两轮、重复敌人分别击杀、4v4 创建、死亡移出候选、固定随机序列复现
This commit is contained in:
@@ -244,12 +244,9 @@ namespace SepCore.Battle
|
||||
List<int> remainingOrder = new List<int>();
|
||||
if (!IsCompleted)
|
||||
{
|
||||
foreach (BattleUnit unit in Units)
|
||||
foreach (BattleUnit unit in GetRemainingCandidates())
|
||||
{
|
||||
if (IsActive(unit) && !ActedUnitIds.Contains(unit.UnitId))
|
||||
{
|
||||
remainingOrder.Add(unit.UnitId);
|
||||
}
|
||||
remainingOrder.Add(unit.UnitId);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -412,9 +409,14 @@ namespace SepCore.Battle
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 选择本轮下一个未行动的活跃单位;全部行动过则进入下一轮。
|
||||
/// M1 按单位创建顺序(玩家在前)轮流,每轮每个单位最多行动一次;
|
||||
/// M2 起由完整调度规则(速度、阵营、队伍顺序、先制)替换。
|
||||
/// 选择本轮下一个未行动的活跃单位;候选集为空时开启下一轮。
|
||||
/// 调度规则(文档 6.1):
|
||||
/// 1. 先制第一轮仍有玩家未行动时只在玩家中选择;
|
||||
/// 2. 当前速度高者优先;
|
||||
/// 3. 同速时玩家优先于敌人;
|
||||
/// 4. 同阵营同速时 PartyOrder 小者优先;
|
||||
/// 5. 已行动单位不因速度变化再次行动(M2 无速度变化效果);
|
||||
/// 6. 候选集为空时开启下一轮,先制限制只适用于第一轮。
|
||||
/// </summary>
|
||||
private void SelectNextActor()
|
||||
{
|
||||
@@ -435,17 +437,93 @@ namespace SepCore.Battle
|
||||
CurrentActorUnitId = next != null ? next.UnitId : 0;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 按调度规则选本轮优先级最高的候选;没有候选时返回 null。
|
||||
/// </summary>
|
||||
private BattleUnit FindNextActor()
|
||||
{
|
||||
BattleUnit best = null;
|
||||
foreach (BattleUnit unit in Units)
|
||||
{
|
||||
if (IsActive(unit) && !ActedUnitIds.Contains(unit.UnitId))
|
||||
if (!IsActive(unit) || ActedUnitIds.Contains(unit.UnitId))
|
||||
{
|
||||
return unit;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (IsPreemptiveRound && unit.Faction == BattleFaction.Enemy && HasUnactedPlayer())
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (best == null || CompareActors(unit, best) < 0)
|
||||
{
|
||||
best = unit;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
return best;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 先制限制只适用于第一轮。
|
||||
/// </summary>
|
||||
private bool IsPreemptiveRound => IsPreemptive && RoundNumber == 1;
|
||||
|
||||
private bool HasUnactedPlayer()
|
||||
{
|
||||
foreach (BattleUnit unit in Units)
|
||||
{
|
||||
if (unit.Faction == BattleFaction.Player && IsActive(unit) && !ActedUnitIds.Contains(unit.UnitId))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 速度高优先;同速玩家优先;同阵营同速 PartyOrder 小优先。返回负值表示 a 优先于 b。
|
||||
/// </summary>
|
||||
private static int CompareActors(BattleUnit a, BattleUnit b)
|
||||
{
|
||||
if (a.Speed != b.Speed)
|
||||
{
|
||||
return b.Speed - a.Speed;
|
||||
}
|
||||
|
||||
if (a.Faction != b.Faction)
|
||||
{
|
||||
return a.Faction == BattleFaction.Player ? -1 : 1;
|
||||
}
|
||||
|
||||
return a.PartyOrder - b.PartyOrder;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 本轮尚未行动的候选单位,按调度优先级排序;当前行动者是首个。
|
||||
/// 先制第一轮只返回玩家候选(敌人等玩家全部行动后进入候选集)。
|
||||
/// </summary>
|
||||
private List<BattleUnit> GetRemainingCandidates()
|
||||
{
|
||||
List<BattleUnit> candidates = new List<BattleUnit>();
|
||||
foreach (BattleUnit unit in Units)
|
||||
{
|
||||
if (!IsActive(unit) || ActedUnitIds.Contains(unit.UnitId))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (IsPreemptiveRound && unit.Faction == BattleFaction.Enemy && HasUnactedPlayer())
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
candidates.Add(unit);
|
||||
}
|
||||
|
||||
candidates.Sort(CompareActors);
|
||||
return candidates;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -156,6 +156,9 @@ namespace SepCore.CustomComponent
|
||||
|
||||
GameEntry.UI.OpenUIForm(UIFormType.BattleForm);
|
||||
Log.Info("Battle started with encounter '{0}'.", encounter.EncounterId);
|
||||
|
||||
// M2:敌人速度更高时开局行动者为敌人,同样按间歇节奏自动推进
|
||||
ScheduleAutoAdvance();
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -283,6 +286,13 @@ namespace SepCore.CustomComponent
|
||||
|
||||
private System.Collections.IEnumerator AutoAdvanceRoutine()
|
||||
{
|
||||
// 战斗 UI 打开是异步资源加载,等推进监听注册后再推第一步,避免敌人先手事件丢失
|
||||
int waitFrames = 0;
|
||||
while (_stepListener == null && _runtime != null && !_runtime.IsCompleted && waitFrames++ < 600)
|
||||
{
|
||||
yield return null;
|
||||
}
|
||||
|
||||
while (_runtime != null && !_runtime.IsCompleted &&
|
||||
_runtime.CurrentActor != null && _runtime.CurrentActor.Faction == BattleFaction.Enemy)
|
||||
{
|
||||
|
||||
@@ -11,11 +11,18 @@ namespace SepCore.Debugger
|
||||
/// <summary>
|
||||
/// 战斗壳层调试入口(仅供 Editor/Development Build,正式构建不注册)。
|
||||
/// 模拟探索层:缺少单局状态时用配表角色与固定种子初始化临时状态,
|
||||
/// 然后通过 TurnBattleComponent 以固定调试遭遇开始/结束战斗,不依赖尚未实现的地图敌人。
|
||||
/// 然后通过 TurnBattleComponent 以调试遭遇开始/结束战斗,不依赖尚未实现的地图敌人。
|
||||
/// 支持 1v1 / 2v2 / 4v4 与先制开关。
|
||||
/// </summary>
|
||||
public class BattleDebuggerWindow : IDebuggerWindow
|
||||
{
|
||||
private const int DebugRunSeed = 240829;
|
||||
private const int DebugEncounterId = 1;
|
||||
|
||||
private int _playerCount = 1;
|
||||
private int _enemyPartyConfigId = 4001;
|
||||
private bool _preemptive;
|
||||
private int _builtPlayerCount = -1;
|
||||
|
||||
public void Initialize(params object[] args)
|
||||
{
|
||||
@@ -40,19 +47,50 @@ namespace SepCore.Debugger
|
||||
public void OnDraw()
|
||||
{
|
||||
GUILayout.Label("<b>Battle Shell</b>");
|
||||
GUILayout.BeginHorizontal("box");
|
||||
{
|
||||
if (GUILayout.Button("Open", GUILayout.Height(30)))
|
||||
{
|
||||
OpenShell();
|
||||
}
|
||||
|
||||
if (GUILayout.Button("Close", GUILayout.Height(30)))
|
||||
GUILayout.BeginVertical("box");
|
||||
{
|
||||
GUILayout.Label("队伍规模(玩家数/敌人队伍):");
|
||||
GUILayout.BeginHorizontal();
|
||||
{
|
||||
GameEntry.TurnBattle.CloseBattle();
|
||||
if (GUILayout.Button("1v1", GUILayout.Height(26)))
|
||||
{
|
||||
_playerCount = 1;
|
||||
_enemyPartyConfigId = 4001;
|
||||
}
|
||||
|
||||
if (GUILayout.Button("2v2", GUILayout.Height(26)))
|
||||
{
|
||||
_playerCount = 2;
|
||||
_enemyPartyConfigId = 4002;
|
||||
}
|
||||
|
||||
if (GUILayout.Button("4v4", GUILayout.Height(26)))
|
||||
{
|
||||
_playerCount = 4;
|
||||
_enemyPartyConfigId = 4005;
|
||||
}
|
||||
}
|
||||
GUILayout.EndHorizontal();
|
||||
GUILayout.Label("当前选择:" + _playerCount + " 玩家 / 敌人队伍 " + _enemyPartyConfigId);
|
||||
|
||||
_preemptive = GUILayout.Toggle(_preemptive, "先制(第一轮玩家全先行)");
|
||||
|
||||
GUILayout.BeginHorizontal();
|
||||
{
|
||||
if (GUILayout.Button("Open", GUILayout.Height(30)))
|
||||
{
|
||||
OpenShell();
|
||||
}
|
||||
|
||||
if (GUILayout.Button("Close", GUILayout.Height(30)))
|
||||
{
|
||||
GameEntry.TurnBattle.CloseBattle();
|
||||
}
|
||||
}
|
||||
GUILayout.EndHorizontal();
|
||||
}
|
||||
GUILayout.EndHorizontal();
|
||||
GUILayout.EndVertical();
|
||||
|
||||
DrawRunState();
|
||||
}
|
||||
@@ -77,32 +115,29 @@ namespace SepCore.Debugger
|
||||
GUILayout.EndVertical();
|
||||
}
|
||||
|
||||
private static void OpenShell()
|
||||
{
|
||||
InitDebugRunStateIfNeeded();
|
||||
|
||||
// 固定调试遭遇:EncounterId=1,敌人队伍预设 4001(单敌人),普通战斗
|
||||
bool started = GameEntry.TurnBattle.TryStartBattle(new BattleEncounter(1, 4001, false), null);
|
||||
if (!started)
|
||||
{
|
||||
Log.Warning("Battle debug shell can not start, maybe a battle is already active.");
|
||||
}
|
||||
}
|
||||
|
||||
private static void InitDebugRunStateIfNeeded()
|
||||
private void OpenShell()
|
||||
{
|
||||
if (GameEntry.Random.Random == null)
|
||||
{
|
||||
GameEntry.Random.BeginRun(DebugRunSeed);
|
||||
}
|
||||
|
||||
if (GameEntry.TurnBattle.Players.Count == 0)
|
||||
// 规模变化时重建玩家列表(重置为满状态);同规模重开保留上次战斗回写的 HP/MP
|
||||
if (_builtPlayerCount != _playerCount)
|
||||
{
|
||||
GameEntry.TurnBattle.ReplacePlayers(BuildDebugPlayers());
|
||||
GameEntry.TurnBattle.ReplacePlayers(BuildDebugPlayers(_playerCount));
|
||||
_builtPlayerCount = _playerCount;
|
||||
}
|
||||
|
||||
BattleEncounter encounter = new BattleEncounter(DebugEncounterId, _enemyPartyConfigId, _preemptive);
|
||||
bool started = GameEntry.TurnBattle.TryStartBattle(encounter, null);
|
||||
if (!started)
|
||||
{
|
||||
Log.Warning("Battle debug shell can not start, maybe a battle is already active.");
|
||||
}
|
||||
}
|
||||
|
||||
private static List<RunPlayerState> BuildDebugPlayers()
|
||||
private static List<RunPlayerState> BuildDebugPlayers(int count)
|
||||
{
|
||||
List<RunPlayerState> players = new List<RunPlayerState>();
|
||||
GlobalConfig global = GameEntry.Luban.Global != null ? GameEntry.Luban.Global.Data : null;
|
||||
@@ -112,7 +147,6 @@ namespace SepCore.Debugger
|
||||
return players;
|
||||
}
|
||||
|
||||
// M1 为 1v1:只取第一个新存档角色作为我方单位
|
||||
if (global.NewGameCharacterIds == null || global.NewGameCharacterIds.Count == 0)
|
||||
{
|
||||
Log.Warning("Can not build debug players without new game character ids.");
|
||||
@@ -122,7 +156,7 @@ namespace SepCore.Debugger
|
||||
int order = 1;
|
||||
foreach (int characterId in global.NewGameCharacterIds)
|
||||
{
|
||||
if (order > 1)
|
||||
if (order > count)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user