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>
258 lines
8.3 KiB
C#
258 lines
8.3 KiB
C#
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using UnityEngine;
|
|
|
|
namespace Wizard;
|
|
|
|
public static class AIDiscardUtility
|
|
{
|
|
private class DiscardCandidateOrder : IComparer<Tuple<AIVirtualCard, float>>
|
|
{
|
|
public enum SortType
|
|
{
|
|
Ascending,
|
|
Dscending
|
|
}
|
|
|
|
private SortType _sortOrder;
|
|
|
|
public DiscardCandidateOrder(SortType sortType)
|
|
{
|
|
_sortOrder = sortType;
|
|
}
|
|
|
|
public int Compare(Tuple<AIVirtualCard, float> left, Tuple<AIVirtualCard, float> right)
|
|
{
|
|
if (left.second > right.second)
|
|
{
|
|
if (_sortOrder != SortType.Ascending)
|
|
{
|
|
return -1;
|
|
}
|
|
return 1;
|
|
}
|
|
if (left.second < right.second)
|
|
{
|
|
if (_sortOrder != SortType.Ascending)
|
|
{
|
|
return 1;
|
|
}
|
|
return -1;
|
|
}
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
public static float CalcAllDiscardedBonus(AIVirtualField field, AISituationInfo playSituation, List<int> playPtn)
|
|
{
|
|
AIDiscardInfo discardInfo = playSituation.DiscardInfo;
|
|
if (discardInfo == null || !discardInfo.IsValuable)
|
|
{
|
|
return 0f;
|
|
}
|
|
float num = 0f;
|
|
for (int i = 0; i < discardInfo.TargetList.Count; i++)
|
|
{
|
|
num += EvaluateDiscardedBonus(discardInfo.TargetList[i], playPtn, playSituation, field, isIgnoreInBattle: false, isCalcCostDiff: false, isCalcTokenValue: false);
|
|
}
|
|
if (field.CardListSet.HasAfterDiscardTagHolder)
|
|
{
|
|
for (int j = 0; j < field.CardListSet.AfterDiscardTagHolders.Count; j++)
|
|
{
|
|
AIVirtualCard tagOwner = field.CardListSet.AfterDiscardTagHolders[j];
|
|
num += tagOwner.GetAllyDiscardBonus(playPtn, playSituation, useIgnoreInBattle: false);
|
|
}
|
|
}
|
|
return num;
|
|
}
|
|
|
|
public static float EvaluateDiscardedBonus(AIVirtualCard targetCard, List<int> playPtn, AISituationInfo situation, AIVirtualField field, bool isIgnoreInBattle, bool isCalcCostDiff, bool isCalcTokenValue)
|
|
{
|
|
float num = 0f;
|
|
num += targetCard.GetDiscardedBonus(playPtn, situation, isIgnoreInBattle);
|
|
if (isCalcTokenValue)
|
|
{
|
|
num += targetCard.GetDiscardedTokenBonus(field, playPtn, situation, isIgnoreInBattle);
|
|
}
|
|
num -= targetCard.GetHandBonus(playPtn, situation, isIgnoreInFusion: false);
|
|
if (isCalcCostDiff)
|
|
{
|
|
num += (float)Mathf.Abs(targetCard.SelfField.AllyPpTotal - targetCard.Cost) * 0.001f;
|
|
}
|
|
if (playPtn != null && playPtn.Count > 0)
|
|
{
|
|
for (int i = 0; i < playPtn.Count; i++)
|
|
{
|
|
if (field.AllyHandCards[playPtn[i]].IsSameCard(targetCard))
|
|
{
|
|
num += -1000f;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
return num;
|
|
}
|
|
|
|
public static List<AITokenInformation> GetAllDiscardedTokenIds(AIDiscardInfo discardInfo, AIVirtualField field, List<int> playPtn, AISituationInfo situation)
|
|
{
|
|
if (discardInfo == null || !discardInfo.IsValuable)
|
|
{
|
|
return null;
|
|
}
|
|
List<AITokenInformation> list = null;
|
|
for (int i = 0; i < discardInfo.TargetList.Count; i++)
|
|
{
|
|
List<AITokenInformation> discardedTokenIds = discardInfo.TargetList[i].GetDiscardedTokenIds(field, playPtn, situation);
|
|
if (discardedTokenIds != null)
|
|
{
|
|
list = AIParamQuery.AddRangeToList(discardedTokenIds, list);
|
|
}
|
|
}
|
|
return list;
|
|
}
|
|
|
|
public static List<AIVirtualCard> SelectBestDiscardTarget(AIVirtualCard tagOwner, AIVirtualField field, List<AIVirtualCard> discardCandidates, int discardCount, List<int> playPtn, AISituationInfo situation)
|
|
{
|
|
return SelectDiscardTargets(tagOwner, field, discardCandidates, discardCount, playPtn, situation, DiscardCandidateOrder.SortType.Dscending);
|
|
}
|
|
|
|
public static List<AIVirtualCard> SelectWorstDiscardTarget(AIVirtualCard tagOwner, AIVirtualField field, List<AIVirtualCard> discardCandidates, int discardCount, List<int> playPtn, AISituationInfo situation)
|
|
{
|
|
return SelectDiscardTargets(tagOwner, field, discardCandidates, discardCount, playPtn, situation, DiscardCandidateOrder.SortType.Ascending);
|
|
}
|
|
|
|
private static List<AIVirtualCard> SelectDiscardTargets(AIVirtualCard tagOwner, AIVirtualField field, List<AIVirtualCard> discardCandidates, int discardCount, List<int> playPtn, AISituationInfo situation, DiscardCandidateOrder.SortType sortType)
|
|
{
|
|
if (discardCandidates == null)
|
|
{
|
|
return null;
|
|
}
|
|
List<AIVirtualCard> list;
|
|
if (discardCandidates.Count <= discardCount)
|
|
{
|
|
list = new List<AIVirtualCard>(discardCandidates.Count);
|
|
for (int i = 0; i < discardCandidates.Count; i++)
|
|
{
|
|
list.Add(discardCandidates[i]);
|
|
}
|
|
return list;
|
|
}
|
|
list = SortDiscardCandidats(discardCandidates, playPtn, situation, field, sortType);
|
|
return list.GetRange(0, discardCount);
|
|
}
|
|
|
|
private static List<AIVirtualCard> SortDiscardCandidats(List<AIVirtualCard> candidates, List<int> playPtn, AISituationInfo situation, AIVirtualField field, DiscardCandidateOrder.SortType sortType)
|
|
{
|
|
List<Tuple<AIVirtualCard, float>> list = new List<Tuple<AIVirtualCard, float>>();
|
|
for (int i = 0; i < candidates.Count; i++)
|
|
{
|
|
list.Add(new Tuple<AIVirtualCard, float>
|
|
{
|
|
first = candidates[i],
|
|
second = EvaluateDiscardedBonus(candidates[i], playPtn, situation, field, isIgnoreInBattle: false, isCalcCostDiff: true, isCalcTokenValue: true)
|
|
});
|
|
}
|
|
DiscardCandidateOrder comparer = new DiscardCandidateOrder(sortType);
|
|
list.Sort(comparer);
|
|
List<AIVirtualCard> list2 = new List<AIVirtualCard>(list.Count);
|
|
for (int j = 0; j < list.Count; j++)
|
|
{
|
|
list2.Add(list[j].first);
|
|
}
|
|
return list2;
|
|
}
|
|
|
|
public static bool IsMatchedDiscardTarget(AIVirtualCard tagOwner, List<AIScriptTokenBase> filters, List<int> playPtn, AISituationInfo situation)
|
|
{
|
|
if (tagOwner == null || situation == null)
|
|
{
|
|
return false;
|
|
}
|
|
AIDiscardInfo discardInfo = situation.DiscardInfo;
|
|
if (discardInfo == null || !discardInfo.IsValuable)
|
|
{
|
|
return false;
|
|
}
|
|
List<AIVirtualCard> targetList = discardInfo.TargetList;
|
|
for (int i = 0; i < targetList.Count; i++)
|
|
{
|
|
if (AIFilteringUtility.CheckMatchTargetFiltering(targetList[i], targetList, filters, playPtn, tagOwner, situation))
|
|
{
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public static int GetDiscardCount(AIScriptTokenArgType discardListType, AIVirtualCard ownerCard, List<int> playPtn, AISituationInfo situation)
|
|
{
|
|
AIVirtualField selfField = ownerCard.SelfField;
|
|
switch (discardListType)
|
|
{
|
|
case AIScriptTokenArgType.PLAYED:
|
|
return GetDiscardCountFromDiscardedList(selfField);
|
|
case AIScriptTokenArgType.BEFORE_PLAYPTN:
|
|
return GetDiscardCountFromBeforePlayPtnNew(ownerCard, selfField, playPtn, selfField.AI.PlayPtnRecorder);
|
|
case AIScriptTokenArgType.NOW:
|
|
{
|
|
AIDiscardInfo discardInfo = situation.DiscardInfo;
|
|
if (discardInfo == null || !discardInfo.IsValuable)
|
|
{
|
|
return 0;
|
|
}
|
|
return discardInfo.TargetList.Count;
|
|
}
|
|
default:
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
private static int GetDiscardCountFromDiscardedList(AIVirtualField field)
|
|
{
|
|
return field.AI.DiscardedCards.Count((AIVirtualCard c) => c.IsSelfTurn == field.AllyBattlePlayer.IsSelfTurn && field.AI.BattleMgr.CurrentTurn == c.DestroyedTurn);
|
|
}
|
|
|
|
private static int GetDiscardCountFromBeforePlayPtnNew(AIVirtualCard ownerCard, AIVirtualField field, List<int> playPtn, AIPlayptnRecorder recorder)
|
|
{
|
|
AISinglePlayptnRecord playptnRecordOnSim = field.GetPlayptnRecordOnSim(playPtn);
|
|
if (playptnRecordOnSim == null)
|
|
{
|
|
AIConsoleUtility.LogError("GetDiscardCountFromPlayPtn error!! Cannot find playPtn record!!!!!");
|
|
return 0;
|
|
}
|
|
int num = 0;
|
|
List<PlayedCardInfo> playedCardList = playptnRecordOnSim.PlayedCardList;
|
|
for (int i = 0; i < playedCardList.Count; i++)
|
|
{
|
|
PlayedCardInfo playedCardInfo = playedCardList[i];
|
|
if (playedCardInfo.Card.IsSameCard(ownerCard))
|
|
{
|
|
break;
|
|
}
|
|
if (playedCardInfo.DiscardInfo != null && playedCardInfo.DiscardInfo.TargetList != null)
|
|
{
|
|
num += playedCardInfo.DiscardInfo.TargetList.Count;
|
|
}
|
|
}
|
|
return num;
|
|
}
|
|
|
|
public static bool CheckAttackDiscardTargetInPlayPtn(AIVirtualField field, List<int> playPtn, AISituationInfo situation)
|
|
{
|
|
if (situation.ActionType != AIOperationType.ATTACK)
|
|
{
|
|
return false;
|
|
}
|
|
AIVirtualCard actor = situation.Actor;
|
|
if (!actor.TagCollectionContainer.HasTag(AIPlayTagType.AttackDiscard))
|
|
{
|
|
return false;
|
|
}
|
|
if (actor.TagCollectionContainer.AttackTags.CheckAttackDiscardTargetInPlayPtn(actor, field.AllyHandCards, playPtn, situation))
|
|
{
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
}
|