ThreeKingdomsSlingshot/Assets/Tests/PlayMode/CollisionAuditProbe.cs

243 lines
16 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Text;
using TKS.OutGame;
using UnityEngine;
using UnityEngine.SceneManagement;
namespace TKS.Tests
{
/// <summary>
/// 충돌 피해 감사(PD 2026-11-26 「튕김 피해가 한 템포 늦음 · 밀린 아군 · 적 쪽 · 장애물 튕김 · 반격 · 원거리 충돌에서 피해가 빠지는 경우」) — 판정이 아니라 측정 도구:
/// 정해 둔 장면을 실제 속도로 쏘고 유닛마다 체력 변화 · 상대 진영과 처음 닿은 시각 · 체력이 처음 준 시각(= 닿은 뒤 몇 초 늦게 피해가 들어갔나)을
/// Temp/SlingProbe/collision_&lt;라벨&gt;.txt 로 남긴다. 기대(누가 피해를 받아야 하나)를 장면마다 적어 PASS/FAIL 을 같이 쓴다.
/// Play 중 `script eval --code "TKS.Tests.CollisionAuditRunner.Launch(\"before\");"` → 끝나면 Temp/SlingProbe/collision_done.txt.
/// </summary>
public class CollisionAuditRunner : MonoBehaviour
{
BattleManager bm;
string label = "run";
public static void Launch(string label)
{
var go = new GameObject("CollisionAuditRunner");
DontDestroyOnLoad(go);
var r = go.AddComponent<CollisionAuditRunner>();
r.label = string.IsNullOrEmpty(label) ? "run" : label;
r.StartCoroutine(r.Routine());
}
static string Dir
{
get { return Path.Combine(Path.GetDirectoryName(Application.dataPath), "Temp", "SlingProbe"); }
}
static string F(float v)
{
return v.ToString("0.00", CultureInfo.InvariantCulture);
}
struct Sp
{
public UnitType Type;
public Faction Side;
public Vector2 Pos;
public string Name;
public bool MustHurt; // 이 장면에서 피해를 받아야 한다
}
static Sp S(UnitType t, Faction f, float x, float y, string n, bool mustHurt = false)
{
return new Sp { Type = t, Side = f, Pos = new Vector2(x, y), Name = n, MustHurt = mustHurt };
}
class Track
{
public Unit U;
public Sp Def;
public int Hp0;
public float TContact = -1f; // 상대 진영 유닛과 처음 닿은 시각
public float THit = -1f; // 체력이 처음 준 시각
}
IEnumerator Routine()
{
Directory.CreateDirectory(Dir);
string done = Path.Combine(Dir, "collision_done.txt");
if (File.Exists(done)) File.Delete(done);
int errors = 0;
var errLog = new List<string>();
Application.LogCallback onLog = (msg, stack, type) =>
{
if (type != LogType.Error && type != LogType.Exception) return;
errors++;
if (errLog.Count < 6) errLog.Add(msg + " | " + (stack ?? "").Split('\n')[0]);
};
Application.logMessageReceived += onLog;
WorldProgress.Reset();
SaveStore.UseMemory(true);
SceneManager.LoadScene("Battle");
yield return null;
yield return null;
bm = BattleManager.Instance;
var wm = WorldMapScreen.Instance;
if (wm != null && wm.HasData) wm.EnterStage(0, 0);
yield return null;
Time.timeScale = 1f;
var sb = new StringBuilder();
sb.Append("# collision audit · ").Append(label).Append('\n');
Vector2 up = Vector2.up;
const UnitType I = UnitType.Infantry, C = UnitType.Cavalry, A = UnitType.Archer, P = UnitType.Spear, T = UnitType.Strategist;
const Faction Me = Faction.Player, Foe = Faction.Enemy;
Vector2 dl = new Vector2(0.6f, 0.8f);
// ① 피해 시점 — 정면 · 튕겨 두 번째 적(캐롬) · 뭉친 적
yield return Shot(sb, "t_front", "보병 → 적 정면", up, 1f, false, null, S(I, Me, 0f, -3f, "보병", true), S(I, Foe, 0f, 0f, "적", true));
yield return Shot(sb, "t_carom", "보병 반 두께 → 튕겨 옆 적", up, 1f, false, null, S(I, Me, 0f, -3f, "보병", true), S(I, Foe, 0.45f, 0f, "적1", true), S(I, Foe, -1.8f, -1.6f, "적2", true));
yield return Shot(sb, "t_rack", "보병 → 뭉친 적 3", up, 1f, false, null, S(I, Me, 0f, -3f, "보병", true), S(I, Foe, 0f, 0f, "적1", true), S(I, Foe, -0.5f, 0.9f, "적2"), S(I, Foe, 0.5f, 0.9f, "적3"));
// ② 아군을 쳐서 적에게
yield return Shot(sb, "ally_front", "보병 → 아군 → 적(정면)", up, 1f, false, null, S(I, Me, 0f, -3f, "보병"), S(I, Me, 0f, 0f, "아군", true), S(I, Foe, 0f, 2f, "적", true));
yield return Shot(sb, "ally_angle", "보병 → 아군(비스듬) → 적", up, 1f, false, null, S(I, Me, 0f, -3f, "보병"), S(I, Me, 0.3f, 0f, "아군", true), S(I, Foe, 0.9f, 1.8f, "적", true));
yield return Shot(sb, "ally_touch", "보병 → 적에 붙은 아군", up, 1f, false, null, S(I, Me, 0f, -3f, "보병"), S(I, Me, 0f, 0f, "아군", true), S(I, Foe, 0f, 0.9f, "적", true));
yield return Shot(sb, "ally_far", "보병 → 아군 → 먼 적(4 · 밀린 힘이 못 닿으면 피해 없음이 맞음)", up, 1f, false, null, S(I, Me, 0f, -3f, "보병"), S(I, Me, 0f, 0f, "아군"), S(I, Foe, 0f, 4f, "적"));
yield return Shot(sb, "ally_chain", "보병 → 아군1 → 아군2 → 적", up, 1f, false, null, S(I, Me, 0f, -3f, "보병"), S(I, Me, 0f, 0f, "아군1"), S(I, Me, 0f, 1.2f, "아군2", true), S(I, Foe, 0f, 2.6f, "적", true));
yield return Shot(sb, "ally_weak", "보병 → 아군 → 적(절반 세기)", up, 0.5f, false, null, S(I, Me, 0f, -3f, "보병"), S(I, Me, 0f, -1f, "아군", true), S(I, Foe, 0f, 0.6f, "적", true));
// ③ 적의 차례 — 적이 적 아군을 쳐서 플레이어에게 · 적이 적 아군에게 튕겨 플레이어에게
yield return Shot(sb, "foe_ally", "적 → 적 아군 → 플레이어", Vector2.down, 1f, false, null, S(I, Foe, 0f, 3f, "적"), S(I, Foe, 0f, 0f, "적아군", true), S(I, Me, 0f, -2f, "플레이어", true));
yield return Shot(sb, "foe_ally_touch", "적 → 플레이어에 붙은 적 아군", Vector2.down, 1f, false, null, S(I, Foe, 0f, 3f, "적"), S(I, Foe, 0f, 0f, "적아군", true), S(I, Me, 0f, -0.9f, "플레이어", true));
yield return Shot(sb, "foe_carom", "적 → 적 아군 반 두께 → 튕겨 플레이어", Vector2.down, 1f, false, null, S(I, Foe, 0f, 3f, "적", true), S(I, Foe, 0.45f, 0f, "적아군"), S(I, Me, -1.8f, 1.6f, "플레이어", true));
yield return Shot(sb, "foe_carom_hit", "적 → 플레이어1 반 두께 → 튕겨 플레이어2", Vector2.down, 1f, false, null, S(I, Foe, 0f, 3f, "적", true), S(I, Me, 0.45f, 0f, "플레이어1", true), S(I, Me, -1.8f, 1.6f, "플레이어2", true));
yield return Shot(sb, "foe_cav_arc", "적 기병 → 플레이어 궁병(반격)", Vector2.down, 1f, false, null, S(C, Foe, 0f, 4f, "적기병", true), S(A, Me, 0f, 0f, "궁병", true));
yield return Shot(sb, "foe_rng_move", "적 궁병 이동 → 플레이어", Vector2.down, 1f, true, null, S(A, Foe, 0f, 3f, "적궁병", true), S(I, Me, 0f, 0f, "플레이어", true));
yield return Shot(sb, "foe_front", "적 → 플레이어 정면(반격)", Vector2.down, 1f, false, null, S(I, Foe, 0f, 3f, "적", true), S(I, Me, 0f, 0f, "플레이어", true));
// ④ 장애물에 튕겨 적에게 — 처음 장애물(HitGimmick) · 적을 친 뒤 튕겨 장애물 → 적
yield return Shot(sb, "obs_first", "보병 → 성벽 튕김 → 적", dl, 1f, false, new[] { "wall@0,0.2|6,0.8" }, S(I, Me, 0f, -3f, "보병", true), S(I, Foe, 2.4f, -1.9f, "적", true));
yield return Shot(sb, "obs_after", "보병 → 적1 정면 → 튕겨 아래 성벽 반사 → 적1 다시(적2 = 반사 길 밖 · 피해 없음이 맞음)", up, 1f, false, new[] { "wall@0,-5.6|8,0.8" }, S(I, Me, 0f, -3f, "보병", true), S(I, Foe, 0f, -1.2f, "적1", true), S(I, Foe, 0.9f, -3.4f, "적2"));
yield return Shot(sb, "obs_side", "보병 → 적1 비스듬 → 옆 성벽 → 적2", up, 1f, false, new[] { "wall@-3.4,-1.8|0.8,6" }, S(I, Me, 0f, -3f, "보병", true), S(I, Foe, 0.45f, 0f, "적1", true), S(I, Foe, -1.35f, -2.55f, "적2", true));
// ④-2 밀려가는 유닛이 장애물에 튕겨 적에게(PD 2026-11-26 「밀려가는 유닛도 장애물에 닿으면 튕기게 해」)
yield return Shot(sb, "push_obs", "보병 → 아군 → 목책 튕김 → 적", up, 1f, false, new[] { "palisade@0.5,1.6|r0.5" }, S(I, Me, 0f, -3f, "보병"), S(I, Me, 0f, 0f, "아군", true), S(I, Foe, -0.94f, 0.31f, "적", true));
yield return Shot(sb, "foe_push_obs", "적 → 적 아군 → 목책 튕김 → 플레이어", Vector2.down, 1f, false, new[] { "palisade@-0.5,-1.6|r0.5" }, S(I, Foe, 0f, 3f, "적"), S(I, Foe, 0f, 0f, "적아군", true), S(I, Me, 0.94f, -0.31f, "플레이어", true));
// ⑤ 반격 — 병종별 공격자 · 방어자
yield return Shot(sb, "cnt_inf_arc", "보병 → 적 궁병(반격)", up, 1f, false, null, S(I, Me, 0f, -3f, "보병", true), S(A, Foe, 0f, 0f, "적궁병", true));
yield return Shot(sb, "cnt_spear", "창병 → 적 보병(반격)", up, 1f, false, null, S(P, Me, 0f, -3f, "창병", true), S(I, Foe, 0f, 0f, "적", true));
yield return Shot(sb, "cnt_cav_inf", "기병 관통 → 적 보병(반격)", up, 1f, false, null, S(C, Me, 0f, -4f, "기병", true), S(I, Foe, 0f, 0f, "적", true));
yield return Shot(sb, "cnt_cav_arc", "기병 관통 → 적 궁병(반격)", up, 1f, false, null, S(C, Me, 0f, -4f, "기병", true), S(A, Foe, 0f, 0f, "적궁병", true));
yield return Shot(sb, "cnt_spear_vs_spear", "창병 → 적 창병(반격)", up, 1f, false, null, S(P, Me, 0f, -3f, "창병", true), S(P, Foe, 0f, 0f, "적창병", true));
// ⑥ 원거리 유닛 이동 · 밀림 충돌(근접 패널티로 주고받기)
yield return Shot(sb, "rng_move", "궁병 이동 → 적", up, 1f, true, null, S(A, Me, 0f, -3f, "궁병", true), S(I, Foe, 0f, 0f, "적", true));
yield return Shot(sb, "rng_move_str", "책사 이동 → 적", up, 1f, true, null, S(T, Me, 0f, -3f, "책사", true), S(I, Foe, 0f, 0f, "적", true));
yield return Shot(sb, "rng_pushed", "보병 → 아군 궁병 → 적", up, 1f, false, null, S(I, Me, 0f, -3f, "보병"), S(A, Me, 0f, 0f, "궁병", true), S(I, Foe, 0f, 2f, "적", true));
yield return Shot(sb, "rng_foe_pushed", "적 → 적 궁병 → 플레이어", Vector2.down, 1f, false, null, S(I, Foe, 0f, 3f, "적"), S(A, Foe, 0f, 0f, "적궁병", true), S(I, Me, 0f, -2f, "플레이어", true));
yield return Shot(sb, "rng_bounced_into", "보병 → 적1 → 튕겨 아군 궁병 → 적2", up, 1f, false, null, S(I, Me, 0f, -3f, "보병"), S(I, Foe, 0.45f, 0f, "적1"), S(A, Me, -1.8f, -1.6f, "궁병", true), S(I, Foe, -3.0f, -2.2f, "적2", true));
Application.logMessageReceived -= onLog;
sb.Append("# errors ").Append(errors).Append('\n');
foreach (var e in errLog) sb.Append("# ").Append(e).Append('\n');
File.WriteAllText(Path.Combine(Dir, "collision_" + label + ".txt"), sb.ToString(), new UTF8Encoding(false));
File.WriteAllText(done, label, new UTF8Encoding(false));
Destroy(gameObject);
}
IEnumerator Shot(StringBuilder sb, string id, string desc, Vector2 aimDir, float launchRatio, bool moveOnly, string[] walls, params Sp[] spawns)
{
bm.StopAllCoroutines();
bm.ClearUnits();
var layer = bm.Gimmicks;
if (layer != null) layer.Clear();
bm.ForcePhase(spawns[0].Side == Faction.Player ? BattlePhase.PlayerTurn : BattlePhase.EnemyTurn);
yield return null;
yield return null;
if (walls != null && layer != null)
foreach (var w in walls)
{
// 「id@x,y|w,h」 사각 · 「id@x,y|r반지름」 원
var a = w.Split('@');
var b = a[1].Split('|');
var c = b[0].Split(',');
var center = new Vector2(P(c[0]), P(c[1]));
if (b[1].StartsWith("r")) layer.Spawn(a[0], GimmickShape.Circle(center, P(b[1].Substring(1))));
else
{
var s = b[1].Split(',');
layer.Spawn(a[0], GimmickShape.Box(center, new Vector2(P(s[0]), P(s[1]))));
}
}
var tracks = new List<Track>();
foreach (var s in spawns)
{
var u = bm.SpawnUnit(s.Type, s.Side, s.Pos, s.Name);
tracks.Add(new Track { U = u, Def = s, Hp0 = u.Hp });
}
yield return null;
var actor = tracks[0].U;
aimDir = aimDir.normalized;
float launch = SkillMods.SlingMax(actor) * launchRatio;
Vector2 target = actor.Position + aimDir * launch;
var o = new ActionOutcome();
bool fin = false;
float t0 = Time.time;
bm.StartCoroutine(Run(actor, target, moveOnly, o, () => fin = true));
int frame = 0;
float settle = -1f;
while (true)
{
float t = Time.time - t0;
foreach (var tr in tracks)
{
if (tr.U == null) continue;
if (tr.THit < 0f && tr.U.Hp < tr.Hp0) tr.THit = t;
if (tr.TContact < 0f)
foreach (var other in tracks)
{
if (other == tr || other.U == null || other.Def.Side == tr.Def.Side) continue;
float d = Vector2.Distance(tr.U.transform.position, other.U.transform.position);
if (d <= tr.U.Radius + other.U.Radius + 0.06f)
{
tr.TContact = t;
break;
}
}
}
if (fin)
{
if (settle < 0f) settle = t;
if (t - settle > 0.3f) break;
}
if (++frame > 4000) break;
yield return null;
}
int fails = 0;
var lines = new StringBuilder();
foreach (var tr in tracks)
{
int hp = tr.U != null ? tr.U.Hp : 0;
int lost = tr.Hp0 - hp;
bool fail = tr.Def.MustHurt && lost <= 0;
if (fail) fails++;
string delay = tr.THit >= 0f && tr.TContact >= 0f ? F(tr.THit - tr.TContact) : "-";
lines.Append(" ").Append(fail ? "FAIL " : " ").Append(tr.Def.Name).Append(tr.Def.Side == Faction.Player ? "(아)" : "(적)")
.Append(" hp ").Append(tr.Hp0).Append("→").Append(hp).Append(" (−").Append(lost).Append(")")
.Append(" 닿음 ").Append(tr.TContact >= 0f ? F(tr.TContact) : "-").Append(" 피해 ").Append(tr.THit >= 0f ? F(tr.THit) : "-")
.Append(" 늦음 ").Append(delay)
.Append(" 끝 ").Append(tr.U != null ? ((Vector2)tr.U.transform.position).ToString("0.0") : "-").Append(tr.Def.MustHurt ? " [피해 필요]" : "").Append('\n');
}
sb.Append(fails == 0 ? "PASS " : "FAIL ").Append(id).Append(" — ").Append(desc).Append(" · 끝 ").Append(F(settle)).Append("초 · 반격받음 ").Append(o.CounterTaken)
.Append(" · ").Append(string.Join(" / ", o.Notes)).Append('\n').Append(lines);
}
static float P(string s)
{
return float.Parse(s, CultureInfo.InvariantCulture);
}
IEnumerator Run(Unit u, Vector2 target, bool moveOnly, ActionOutcome o, System.Action onDone)
{
yield return bm.ResolveForTest(u, target, moveOnly, o);
onDone();
}
}
}