using System; using System.Collections.Generic; using System.IO; using System.Text; using UnityEngine; // 自動配置 (v1.1.0 正式錄製協定) : 這次執行的回合順序、進度與亂數種子 // 勾了 StartUI 的「自動配置」才會套排程 ; 自由模式與測試模式也由這裡組出條件,讓輔助腳本只走同一條路徑 // 跨場景共用 (回主畫面再回來,進度接著走) ; 回合存檔成功才前進,所以中途離開會重做同一個條件 // 當機後重開 : 新的種子、進度歸零,不接續 (舊回合由 SessionStorage 的 recovered/ 處理) public static class SessionPlan { // 一回合的條件 : BotSpawner 生成機器人時套到 AimAssist / EnemyPerceptor / 武器,RoundConfig 寫進 config public struct RoundCondition { public int assistMode; // 0 = 無輔助, 1 = 平滑跟槍 public int assistStrength; // 無輔助 0 ; 有輔助 20 / 40 / 60 / 80 / 100 public int assistDelayMs; // 模擬辨識延遲 (自動配置兩類回合都一樣) public int assistDelayJitterMs; public float assistFovDeg; // 作用範圍 : 有輔助 15, 無輔助 0 (frames 的 assist_fov) public int planIndex; // 排程中的第幾項 (熱身 0~4, 正式 5~84) ; 自由 / 測試模式 -1 public int blockIndex; // 區塊 0~7 ; 熱身、自由 / 測試模式 -1 public int orderInBlock; // 區塊內 0~9 ; 熱身、自由 / 測試模式 -1 public bool isWarmup; // 熱身回合照常存檔,分析時排除 public int roundSeed; // 這一回合的亂數種子 (後座力、機器人動作、生成點、延遲抖動) } // ===== 協定常數 : 改任何一個都要升版號,已錄的人全部重錄 ===== public const string ProtocolVersion = "1.1.0"; public const int WarmupRounds = 5; // 熱身 : 無輔助 public const int Blocks = 8; // 試錄超過 20 分鐘就改 7,出包前決定 public const int NoAssistPerBlock = 5; // 每區塊無輔助回合數 public static readonly int[] Strengths = { 20, 40, 60, 80, 100 }; // 每區塊各一回合 public const int RoundsPerBlock = NoAssistPerBlock + 5; // 5 = Strengths 的個數 public const int Total = WarmupRounds + Blocks * RoundsPerBlock; // 85 public const float AssistFovDeg = 15f; public const int DelayMs = 15; public const int JitterMs = 5; public const string Map = "Basement"; public const string Weapon = "Uzi"; public const int Fov = 90; public const int TargetFps = 60; // 這次執行的排程種子 (6 個人各自不同) ; 種子加 Generate() 就能重算整份順序 public static int PlanSeed { get; private set; } // 這次執行的代號 : 跟資料夾、zip 的時間戳相同 public static string SessionId => SessionStorage.StartStamp; // 這次執行有沒有用過自動配置 (StartUI 的回合數顯示用) public static bool Started { get; private set; } // 下一個要做的排程項目 ; 等於已存檔的正式回合數 (含熱身) public static int Progress => progress; public static bool IsComplete => progress >= Total; private static List plan; private static int progress; private static System.Random freeSeedRandom; // 每次進入 Play 都重設 (編輯器關掉 Domain Reload 時,static 不會自動清空) [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)] static void Init() { PlanSeed = Guid.NewGuid().GetHashCode() & 0x7fffffff; // System.Random 會取絕對值,所以只用非負數 plan = Generate(PlanSeed); progress = 0; Started = false; freeSeedRandom = new System.Random(); } public static bool IsAuto(GameSettings s) => s != null && s.autoPlan && !s.isTest; // 取出這一回合的條件 ; 自動配置做完全部回合時回傳 false (不再生成機器人) public static bool TryGetCurrent(GameSettings s, out RoundCondition c) { if (plan == null) Init(); if (IsAuto(s)) { if (!Started) { Started = true; SavePlan(); } if (IsComplete) { c = default; return false; } c = plan[progress]; return true; } c = FromSettings(s); return true; } // 整份順序存成 plan.csv (跟回合放同一個資料夾,會一起打包) ; 種子記在每一回合的 config static void SavePlan() { StringBuilder sb = new StringBuilder("planIndex,blockIndex,orderInBlock,isWarmup,assistMode,assistStrength,roundSeed\n"); foreach (RoundCondition c in plan) { sb.Append($"{c.planIndex},{c.blockIndex},{c.orderInBlock},{(c.isWarmup ? 1 : 0)},{c.assistMode},{c.assistStrength},{c.roundSeed}\n"); } try { File.WriteAllText(Path.Combine(SessionStorage.Folder, "plan.csv"), sb.ToString()); } catch (Exception e) { Debug.LogWarning($"plan.csv 存檔失敗 (每回合的 config 仍有排程資訊) : {e.Message}"); } } // 回合存檔成功才呼叫 ; 只有「目前這一項」存了才前進,重複呼叫或丟棄的回合都不會動到進度 public static void MarkSaved(int planIndex) { if (planIndex >= 0 && planIndex == progress) progress++; } // 畫面上的進度文字 : 不顯示有無輔助或強度 public static string GetProgressText(GameSettings s) { if (s == null || s.isTest) return ""; if (IsAuto(s)) { if (IsComplete) return $"已完成 {Total} 回合,請直接關閉遊戲 (資料會自動打包到桌面)"; return $"第 {progress + 1} / {Total} 回合"; } return $"已記錄 {SessionStorage.NoAssistRounds + SessionStorage.AssistRounds} 回合"; } // 由種子產生整份順序 : 熱身 5 回合無輔助 ; 之後每區塊 10 回合 (無輔助 5、各強度 1),區塊內隨機排序 // 同一個種子一定得到同一份順序 public static List Generate(int seed) { System.Random rng = new System.Random(seed); // 順序 System.Random roundSeeds = new System.Random(seed ^ 0x2545f491); // 每回合的種子 List list = new List(Total); for (int i = 0; i < WarmupRounds; i++) { list.Add(Make(0, 0, list.Count, -1, -1, true, roundSeeds.Next())); } for (int b = 0; b < Blocks; b++) { List strengths = new List(RoundsPerBlock); for (int i = 0; i < NoAssistPerBlock; i++) strengths.Add(0); strengths.AddRange(Strengths); // Fisher-Yates 洗牌 for (int i = strengths.Count - 1; i > 0; i--) { int j = rng.Next(i + 1); (strengths[i], strengths[j]) = (strengths[j], strengths[i]); } for (int k = 0; k < strengths.Count; k++) { int strength = strengths[k]; list.Add(Make(strength > 0 ? 1 : 0, strength, list.Count, b, k, false, roundSeeds.Next())); } } return list; } static RoundCondition Make(int mode, int strength, int planIndex, int block, int order, bool warmup, int roundSeed) { return new RoundCondition { assistMode = mode, assistStrength = strength, assistDelayMs = DelayMs, assistDelayJitterMs = JitterMs, assistFovDeg = mode == 1 ? AssistFovDeg : 0f, planIndex = planIndex, blockIndex = block, orderInBlock = order, isWarmup = warmup, roundSeed = roundSeed, }; } // 自由模式 / 測試模式 : 條件照 StartUI 的設定,每回合一個新的種子 static RoundCondition FromSettings(GameSettings s) { if (freeSeedRandom == null) freeSeedRandom = new System.Random(); int mode = s != null ? s.assistMode : 0; return new RoundCondition { assistMode = mode, assistStrength = s != null && mode != 0 ? s.assistStrength : 0, assistDelayMs = s != null ? s.assistDelayMs : 0, assistDelayJitterMs = s != null ? s.assistDelayJitterMs : 0, assistFovDeg = mode == 1 ? AssistFovDeg : 0f, planIndex = -1, blockIndex = -1, orderInBlock = -1, isWarmup = false, roundSeed = freeSeedRandom.Next(), }; } // 自動配置時固定幀率,讓每個人的幀間隔一致 (實際幀率仍由 frames 的 dt 記錄) ; 其他模式維持不限制 public static void ApplyFrameRate(GameSettings s) { QualitySettings.vSyncCount = 0; Application.targetFrameRate = IsAuto(s) ? TargetFps : -1; } }