Files
SVSimServer/SVSim.BattleEngine/Engine/Wizard/AIScriptArgumentExpressions.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

224 lines
5.9 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
namespace Wizard;
public class AIScriptArgumentExpressions
{
protected List<AIPolishConvertedExpression> _exprList;
private List<int> _referringIds;
protected AIScriptTokenArgType[] LegalSelectTypes { get; set; }
public AIScriptArgumentExpressions(string text)
{
CreateLegalSelectTypes();
if (!(text == ""))
{
InitExpressions(text);
}
}
protected virtual void InitExpressions(string text)
{
InitExprList(text);
}
protected void InitExprList(string text)
{
string[] array = text.Split(';');
_exprList = new List<AIPolishConvertedExpression>();
foreach (string expression in array)
{
AIPolishConvertedExpression aIPolishConvertedExpression = CreateArgumentExpression(expression);
_exprList.Add(aIPolishConvertedExpression);
List<int> referringIDLists = aIPolishConvertedExpression.GetReferringIDLists();
if (referringIDLists != null)
{
if (_referringIds == null)
{
_referringIds = new List<int>();
}
_referringIds.AddRange(referringIDLists);
}
}
}
private AIPolishConvertedExpression CreateArgumentExpression(string expression)
{
string text = expression.TrimStart();
if (text[0] == '*')
{
return new AIPolishConvertedExpression(text.Substring(1), isMultiplyMarked: true);
}
return new AIPolishConvertedExpression(expression);
}
public int EvalID(int index)
{
if (_exprList == null || index >= _exprList.Count)
{
return 0;
}
return _exprList[index].EvalID();
}
public List<int> EvalIDList(int startIndex)
{
List<int> list = new List<int>();
if (_exprList != null && startIndex < _exprList.Count)
{
for (int i = startIndex; i < _exprList.Count; i++)
{
list.Add(_exprList[i].EvalID());
}
}
return list;
}
public string EvalText(int index)
{
if (_exprList == null || index >= _exprList.Count)
{
return "";
}
return _exprList[index].EvalText();
}
public virtual List<int> GetReferringOtherInplayIds()
{
return _referringIds;
}
public float EvalArg(int index, AIVirtualCard tagOwner, List<int> playPtn, AIVirtualField field, AISituationInfo situation)
{
if (_exprList == null || index >= _exprList.Count)
{
return 0f;
}
return _exprList[index].EvalArg(tagOwner, playPtn, field, situation);
}
public bool IsHoldingEVAL()
{
if (_exprList != null)
{
return _exprList.Any((AIPolishConvertedExpression expr) => expr.IsHoldingEVAL());
}
return false;
}
protected string[] SplitTextToWords(string text)
{
string pattern = "\\s\\;\\s(?=\\{)";
return Regex.Split(text, pattern);
}
protected List<AIScriptTokenBase> GetFilters(List<AIPolishConvertedExpression> exprs)
{
List<AIScriptTokenBase> list = new List<AIScriptTokenBase>();
for (int i = 0; i < exprs.Count; i++)
{
AIPolishConvertedExpression aIPolishConvertedExpression = exprs[i];
if (aIPolishConvertedExpression.TokenList == null)
{
continue;
}
if (aIPolishConvertedExpression.TokenList.Count > 1)
{
list.Add(aIPolishConvertedExpression.CreateCalculationToken());
continue;
}
AIScriptTokenBase aIScriptTokenBase = aIPolishConvertedExpression.TokenList[0];
if (!(aIScriptTokenBase is AIScriptArgumentToken { ArgumentType: AIScriptTokenArgType.FILTER_END }))
{
list.Add(aIScriptTokenBase);
}
}
return list;
}
public virtual void Execute(AIVirtualCard tagOwner, AIVirtualField field, List<int> playPtn, AISituationInfo situation = null)
{
}
public virtual void ExecuteWhenRemove(AIVirtualCard tagOwner, AIVirtualField field, AIPlayTag removingTag)
{
}
protected AIScriptTokenArgType GetFirstTokenArgType(AIPolishConvertedExpression arg)
{
AIScriptTokenArgType result = AIScriptTokenArgType.NONE;
if (arg.TokenList != null && arg.TokenList.Count > 0)
{
AIScriptTokenBase aIScriptTokenBase = arg.TokenList[0];
if (aIScriptTokenBase is AIScriptArgumentToken)
{
result = ((AIScriptArgumentToken)aIScriptTokenBase).ArgumentType;
}
}
return result;
}
public virtual AITokenIdCollection GetAllRegisterTokenPoolInfo(AIVirtualCard owner)
{
if (_exprList == null || _exprList.Count <= 0)
{
return null;
}
List<int> list = null;
for (int i = 0; i < _exprList.Count; i++)
{
List<AIScriptTokenBase> tokenList = _exprList[i].TokenList;
if (tokenList != null && tokenList.Count > 0 && tokenList.Count == 1 && tokenList[0] is AIScriptIDToken aIScriptIDToken)
{
list = AIParamQuery.AddElementToList(aIScriptIDToken.ID, list);
}
}
if (list == null)
{
return null;
}
return CreateRegisterTokenPoolInfo(owner, list);
}
protected virtual AITokenIdCollection CreateRegisterTokenPoolInfo(AIVirtualCard owner, List<int> idList)
{
return AISummonTokenUtility.CreateTokenIdCollectionFromIdList(owner, AIScriptTokenArgType.ALLY, idList, AITokenType.Default);
}
protected bool IsSideTokenArgType(AIPolishConvertedExpression arg, out AIScriptTokenArgType dstTokenARgType)
{
dstTokenARgType = GetFirstTokenArgType(arg);
AIScriptTokenArgType aIScriptTokenArgType = dstTokenARgType;
if ((uint)(aIScriptTokenArgType - 84) <= 2u)
{
return true;
}
return false;
}
protected bool IsLegalSelectType(AIPolishConvertedExpression arg, out AIScriptTokenArgType selectType)
{
selectType = AIScriptTokenArgType.NONE;
if (arg.TokenList != null && arg.TokenList.Count > 0 && arg.TokenList[0] is AIScriptArgumentToken { ArgumentType: var argumentType } && Array.IndexOf(LegalSelectTypes, argumentType) >= 0)
{
selectType = argumentType;
return true;
}
return false;
}
protected virtual void CreateLegalSelectTypes()
{
LegalSelectTypes = new AIScriptTokenArgType[2]
{
AIScriptTokenArgType.ALL_SELECT,
AIScriptTokenArgType.RANDOM_SELECT
};
}
}