Files
SVSimServer/SVSim.BattleEngine/Engine/Wizard/AISinglePlayptnRecord.cs
gamer147 2d9a6eea4b engine cleanup passes 4-7 + multi-instancing ambient rip
Squashes 146 commits from battle-engine-extraction. Net: 2,045 files changed,
+11,896 / -158,687 lines. Ships engine passes 4-7 (dead-code cull, view-layer
stub, receive-path shrink) plus the Phase-5 AsyncLocal ambient deletion that
turns concurrent battles into a type-system property rather than a scope
contract.

## What landed

**Passes 4-7 (chunks 1-34):** Extended the Phase-4 const-false collapse into a
cascading cull across the skill graph, view layer, and receive-path periphery.
Six mode flags (IsWatchBattle/IsReplayBattle/IsAdmin/IsAdminWatch/IsPuzzleQuest/
IsAINetwork) became `const false`, every guarded block deleted. Field*.cs
subclass ctors + BackGroundBase + ObjectChecker culled to no-ops. Mulligan
family reworked to take a mgr param through IMulliganMgr.InitMulligan.
Emotion/Recovery/Resource clusters null-stubbed. Prediction/OperationSimulator/
skill filters converted from static ambient reads to per-mgr reads via
SkillPrm.ownerCard.SelfBattlePlayer.BattleMgr / ins.BattleMgr / this.BattleMgr.

**Phase-5 ambient rip (chunks 35-47):** Deleted BattleAmbient / BattleAmbient-
Context / TestBattleScope in full. Every per-battle mutable slot now lives on
the mgr instance itself:
  mgr.InstanceIsForecast / InstanceIsRandomDraw / InstanceRecoveryInfo /
  InstanceViewerId / InstanceNetworkAgent / GameMgr
BattleManagerBase.GetIns() returns null unconditionally; the residual static
flags + 3 façades (Certification.ViewerId, Data.BattleRecoveryInfo,
ToolboxGame.RealTimeNetworkAgent) are null-tolerant defaults kept for the
handful of engine-internal readers that still reference their types. Zero
BattleAmbient references anywhere in engine + node + tests.

Added pre-seeded GameMgr ctor overload threaded through the mgr chain
(BattleManagerBase → SingleBattleMgr / NetworkBattleManagerBase → NetworkStandard-
BattleMgr → HeadlessBattleMgr / HeadlessNetworkBattleMgr). Fixtures build a
GameMgr, seed it via HeadlessEngineEnv.SeedCharaIds/SeedNetUser, and pass it
to the mgr's ctor — no ambient reach.

Node side (SVSim.BattleNode/SessionBattleEngine): _ctx replaced with a plain
GameMgr field; 34 `using var _ambient = BattleAmbient.Enter(_ctx)` scope wraps
ripped from every accessor and mutator; EngineGlobalInit.WirePerSessionGameMgr
takes GameMgr as a param and runs from SessionBattleEngine.SetupInternal
BEFORE mgr construction.

Test side: TestBattleScope deleted; 18 fixture [SetUp]s migrated to
`HeadlessEngineEnv.EnsureProcessGlobals()`; MultiInstanceEngineTests rewritten
around per-mgr construction (GetIns() → null is the pinned invariant).

## Regression fixes

- **chunk-48** (MulliganCtrl): chunk-35's `= null` stubs on card lookups broke
  the live receive-driven Deal path (BattlePlayerBase.DrawCard NRE'd downstream
  of NetworkPlayerMulliganCtrl.StartMulliganVfx). Restored the three lookups
  via `_battlePlayer.BattleMgr.GetBattleCardIdx`. Engine tests were satisfied
  by the WireMulliganPhase seam; unit tests exposed the live-path gap.

## Ship state

- SVSim.BattleEngine.Tests: 56/56 pass, 2 skip
- SVSim.UnitTests: 1554/1554 pass (was 1523/31-fail before chunk 48)
- Solution build: 0 source warnings (40 pre-existing NU1902 MessagePack CVEs
  in SVSim.EmulatedEntrypoint, unrelated)
- Sequential PVP smoke: verified live (two back-to-back battles, no regression
  on cleanup/spinup)
- Concurrent PVP smoke: verified live

Adds tools/engine-port/ClosureAnalyzer/ — the Roslyn transitive-type-closure
analyzer needed to make future cascade cleanup safe (per feedback memory
"Engine cleanup needs closure tool" from the 2026-06-28 pass-3 failure).

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-07-03 19:18:54 -04:00

235 lines
5.4 KiB
C#

using System.Collections.Generic;
namespace Wizard;
public class AISinglePlayptnRecord
{
public List<int> PlayPtn;
public ulong PlayPtnHash;
public List<PlayedCardInfo> PlayedCardList;
public bool IsValid;
public int LastRestPp;
public int TotalDrawCount;
public float FirstPlayCardPriority;
public AIVirtualCard FirstSummonedAllyFollower;
public int ReferenceFieldIndex { get; private set; }
public List<AIVirtualCard> UsableHandCardList { get; private set; }
public List<AIVirtualCard> RestHandCardList { get; private set; }
public List<AIVirtualCard> AllDiscardedCardList { get; private set; }
public int PlayPtnCount
{
get
{
if (PlayPtn != null)
{
return PlayPtn.Count;
}
return 0;
}
}
public AISinglePlayptnRecord(List<int> playPtn, AIVirtualField field, int fieldIndex)
{
PlayPtn = playPtn;
PlayPtnHash = AIHandPtnCalculator.CalculatePlayPtnHash(field, playPtn);
IsValid = true;
LastRestPp = field.AllyPp;
ReferenceFieldIndex = fieldIndex;
TotalDrawCount = 0;
InitializeHandCardList(field);
AllDiscardedCardList = null;
PlayedCardList = new List<PlayedCardInfo>();
for (int i = 0; i < playPtn.Count; i++)
{
int num = playPtn[i];
AIVirtualCard card = field.AllyHandCards[num];
PlayedCardList.Add(new PlayedCardInfo(num, card));
if (i == 0)
{
FirstPlayCardPriority = card.GetPriority(playPtn);
}
}
}
public void UpdatePlayPtnRecord(PlayedCardInfo info)
{
TotalDrawCount += info.DrawCount;
LastRestPp = info.RestPp;
if (!info.IsPlayable)
{
IsValid = false;
}
RestHandCardList.Remove(info.Card);
info.IsProcessed = true;
}
public bool IsToBeRegister()
{
return PlayPtn.Count == PlayedCardList.Count;
}
public AIVirtualCard FindRealActor(AIVirtualCard originalCard)
{
List<PlayedCardInfo> playedCardList = PlayedCardList;
for (int i = 0; i < playedCardList.Count; i++)
{
PlayedCardInfo playedCardInfo = playedCardList[i];
if (originalCard.IsSameCard(playedCardInfo.Card) && playedCardInfo.TransformCard != null)
{
return playedCardInfo.TransformCard;
}
}
return originalCard;
}
public bool IsMatchedPattern(List<int> playPtn)
{
for (int i = 0; i < PlayPtn.Count; i++)
{
int num = PlayPtn[i];
int num2 = playPtn[i];
if (num != num2)
{
return false;
}
}
return true;
}
private void InitializeHandCardList(AIVirtualField field)
{
UsableHandCardList = new List<AIVirtualCard>();
RestHandCardList = new List<AIVirtualCard>();
for (int i = 0; i < field.AllyHandCards.Count; i++)
{
AIVirtualCard item = field.AllyHandCards[i];
RestHandCardList.Add(item);
if (!PlayPtn.Contains(i))
{
UsableHandCardList.Add(item);
}
}
}
public bool IsAllTargetsUsableHandCard(List<AIVirtualCard> targets)
{
if (UsableHandCardList == null)
{
return false;
}
for (int i = 0; i < targets.Count; i++)
{
AIVirtualCard item = targets[i];
if (!UsableHandCardList.Contains(item))
{
return false;
}
}
return true;
}
public bool IsUsableHandCard(AIVirtualCard card)
{
if (UsableHandCardList == null)
{
return false;
}
for (int i = 0; i < UsableHandCardList.Count; i++)
{
if (UsableHandCardList[i].IsSameCard(card))
{
return true;
}
}
return false;
}
public void CheckRegisteredDiscardInfo(AIDiscardInfo discardInfo)
{
if (discardInfo.IsNGByAI)
{
IsValid = false;
}
else if (discardInfo.IsSuccess)
{
List<AIVirtualCard> targetList = discardInfo.TargetList;
UpdateHandCardList(targetList);
AllDiscardedCardList = AIParamQuery.AddRangeToList(targetList, AllDiscardedCardList);
}
}
public void UpdateHandCardList(List<AIVirtualCard> usedHandCardList)
{
if (usedHandCardList != null && usedHandCardList.Count > 0)
{
for (int i = 0; i < usedHandCardList.Count; i++)
{
AIVirtualCard item = usedHandCardList[i];
UsableHandCardList.Remove(item);
RestHandCardList.Remove(item);
}
}
}
public void RegisterPreprocess(PlayedCardInfo info, AIVirtualTargetSelectAction situation, AIVirtualField field)
{
AISimulationPreprocessRecorder preprocessRecorder = situation.PreprocessRecorder;
info.RegisterPreprocess(preprocessRecorder);
AIVirtualCard card = info.Card;
if (preprocessRecorder.TotalBurialCount <= 0)
{
return;
}
AIVirtualTargetSelectInfo burialSelectInfo = card.GetBurialSelectInfo(field, situation);
bool flag = false;
bool isBreakPlayptn = false;
if (burialSelectInfo != null)
{
AISelectedTargetInfo burialSelectTargets = burialSelectInfo.GetBurialSelectTargets(situation, field, burialSelectInfo, this, out isBreakPlayptn);
if (burialSelectTargets != null)
{
info.RegisterPreDecidedPreprocessTarget(burialSelectTargets);
UpdateHandCardList(burialSelectTargets.Targets);
flag = true;
}
}
if (flag)
{
IsValid = !isBreakPlayptn;
}
else
{
preprocessRecorder.ClearBurialCount();
}
}
public PlayedCardInfo FindPlayedCardInfo(AIVirtualCard actor)
{
if (PlayedCardList == null || PlayedCardList.Count <= 0)
{
return null;
}
for (int i = 0; i < PlayedCardList.Count; i++)
{
PlayedCardInfo playedCardInfo = PlayedCardList[i];
if (actor.IsSameCard(playedCardInfo.Card))
{
return playedCardInfo;
}
}
return null;
}
}