EB-1: machines can die - structures get HP, Husks raze them, wounded base persists

Structures (Turret/Wall/Pylon) reuse the combat spine: authoring bakes Health(GhostField)+DamageEvent buffer+a Destructible tag (no HitRadius -> no friendly projectile fire; no EffectiveCharacterStats -> clamp-to-0). HealthApplyDamageSystem destroys a Destructible at 0 (occupancy auto-frees). EnemyAISystem fortress-targets the weighted-nearest of players+structures via the shared EnemyAIMath.PickWeightedNearest (StructureAggroWeight TuningConfig knob, <1 prefers structures, squared factor; snapshot above the early-return so an undefended base is razed). Persistence v3: per-structure HP threaded through 5 sites (SaveData/PendingStructure/scan-guarded/BaseRestore same-ECB born-correct/WorldLauncher via SaveApply.ToPending); SaveService floor-gate [2,3] loads old saves. Loss feedback: proximity-gated StructureFeedbackSystem; CombatFeedbackSystem suppressed for structures. Pre-code review caught the DamageEvent-buffer crash blocker + 8 majors; post-code review clean. See DR-032.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-06-11 23:53:34 -07:00
parent 35d33f12c1
commit 73cfe2943d
21 changed files with 426 additions and 39 deletions
@@ -0,0 +1,14 @@
using Unity.Entities;
namespace ProjectM.Simulation
{
/// <summary>
/// EB-1 — opt-in marker for an entity that <see cref="ProjectM.Server.HealthApplyDamageSystem"/> DESTROYS when
/// its <see cref="Health"/> hits 0 (alongside <c>TrainingDummyTag</c>/<c>EnemyTag</c>). Baked ONLY on the
/// player-built structure ghosts (Turret/Wall/Pylon) so "machines can die". DELIBERATELY a distinct tag rather
/// than gating on bare <see cref="PlacedStructure"/>: that identity is SHARED by the reserved M7 automation
/// machines (Harvester/Fabricator/Conveyor) whose teardown would silently drop in-flight conveyor cargo — the
/// tag lets each destructible opt in explicitly (zero-size, ghost-hash-neutral). See [[DR-031]] follow-on EB-1.
/// </summary>
public struct Destructible : IComponentData { }
}
@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: a0efc019048795541ba13795d724f331
@@ -1,3 +1,4 @@
using Unity.Collections;
using Unity.Mathematics;
namespace ProjectM.Simulation
@@ -68,5 +69,31 @@ namespace ProjectM.Simulation
math.sincos(angle, out float s, out float c);
return center + new float3(c * radius, 0f, s * radius);
}
/// <summary>
/// EB-1 fortress aggro: pick a Husk's target as the weighted-nearest of the living players (weight 1) and
/// the live structures (a SQUARED <paramref name="structureWeight"/> applied to structure distance, so &lt;1
/// makes structures preferred while a sufficiently-closer player 'in the way' still wins). Planar XZ,
/// deterministic (no RNG/wall-clock). Sets <paramref name="index"/> = -1 when there are no targets. Pure so
/// both the Grunt and Charger passes select IDENTICALLY and it is EditMode-unit-testable.
/// </summary>
public static void PickWeightedNearest(float3 from, NativeList<float3> playerPositions,
NativeList<float3> structurePositions, float structureWeight, out bool isStructure, out int index)
{
isStructure = false;
index = -1;
float bestSq = float.MaxValue;
for (int i = 0; i < playerPositions.Length; i++)
{
float sq = math.lengthsq(playerPositions[i].xz - from.xz);
if (sq < bestSq) { bestSq = sq; index = i; isStructure = false; }
}
float w = math.max(0f, structureWeight);
float wsq = w * w; // applied to SQUARED distance so the weight scales true distance
for (int i = 0; i < structurePositions.Length; i++)
{
float sq = math.lengthsq(structurePositions[i].xz - from.xz) * wsq;
if (sq < bestSq) { bestSq = sq; index = i; isStructure = true; }
}
}
}
}
@@ -46,6 +46,10 @@ namespace ProjectM.Simulation
public float MeleeFinisherMult;
public float MeleeComboLength;
// EB-1 fortress aggro: a <1 multiplier on a Husk's SQUARED distance to a structure (so structures are
// preferred targets); a closer player 'in the way' still wins. Read server-side by EnemyAISystem.
public float StructureAggroWeight;
/// <summary>The baked feel defaults == the pre-MC-0 consts. Single source of truth for the fallback path.</summary>
public static TuningConfig Defaults() => new TuningConfig
{
@@ -68,6 +72,7 @@ namespace ProjectM.Simulation
MeleeKnockbackSpeed = 6f,
MeleeFinisherMult = 1.8f, // finisher (last hit) scales dmg/range/recover/knockback
MeleeComboLength = 3f, // light, light, finisher
StructureAggroWeight = 0.7f, // EB-1: <1 prefers structures (fortress aggro); live-tunable
};
/// <summary>Clamp a knob to its safe floor: tick knobs &gt;= 1, value knobs &gt;= 0. Used by every write path
@@ -86,6 +91,7 @@ namespace ProjectM.Simulation
case TuningKnob.MeleeSwingMoveScale:
case TuningKnob.MeleeKnockbackSpeed:
case TuningKnob.MeleeFinisherMult:
case TuningKnob.StructureAggroWeight:
return math.max(0f, value);
// tick knobs: >= 1 (a 0 tick count is degenerate; a 0 i-frame window divides-by-zero in DashSystem)
default:
@@ -118,6 +124,7 @@ namespace ProjectM.Simulation
case TuningKnob.MeleeKnockbackSpeed: c.MeleeKnockbackSpeed = value; break;
case TuningKnob.MeleeFinisherMult: c.MeleeFinisherMult = value; break;
case TuningKnob.MeleeComboLength: c.MeleeComboLength = value; break;
case TuningKnob.StructureAggroWeight: c.StructureAggroWeight = value; break;
// unknown index -> no-op (matches the no-default switch convention in DebugCommandReceiveSystem)
}
}
@@ -146,6 +153,7 @@ namespace ProjectM.Simulation
case TuningKnob.MeleeKnockbackSpeed: return c.MeleeKnockbackSpeed;
case TuningKnob.MeleeFinisherMult: return c.MeleeFinisherMult;
case TuningKnob.MeleeComboLength: return c.MeleeComboLength;
case TuningKnob.StructureAggroWeight: return c.StructureAggroWeight;
default: return 0f;
}
}
@@ -172,6 +180,7 @@ namespace ProjectM.Simulation
MeleeKnockbackSpeed = c.MeleeKnockbackSpeed,
MeleeFinisherMult = c.MeleeFinisherMult,
MeleeComboLength = c.MeleeComboLength,
StructureAggroWeight = c.StructureAggroWeight,
};
/// <summary>Reconstruct the full config from a wire snapshot (FULL state, not a delta).</summary>
@@ -196,6 +205,7 @@ namespace ProjectM.Simulation
MeleeKnockbackSpeed = r.MeleeKnockbackSpeed,
MeleeFinisherMult = r.MeleeFinisherMult,
MeleeComboLength = r.MeleeComboLength,
StructureAggroWeight = r.StructureAggroWeight,
};
}
@@ -221,9 +231,10 @@ namespace ProjectM.Simulation
public const byte MeleeKnockbackSpeed = 16;
public const byte MeleeFinisherMult = 17;
public const byte MeleeComboLength = 18;
public const byte StructureAggroWeight = 19;
/// <summary>Knob count (overlay iteration bound).</summary>
public const byte Count = 19;
public const byte Count = 20;
}
/// <summary>
@@ -253,5 +264,6 @@ namespace ProjectM.Simulation
public float MeleeKnockbackSpeed;
public float MeleeFinisherMult;
public float MeleeComboLength;
public float StructureAggroWeight;
}
}
@@ -15,5 +15,20 @@ namespace ProjectM.Simulation
for (int i = 0; i < src.Length; i++)
dest.Add(new StorageEntry { ItemId = src[i].ItemId, Count = src[i].Count });
}
/// <summary>EB-1: map a serialized <see cref="StructureSave"/> to the staged <see cref="PendingStructure"/>
/// (the menu->ServerWorld copy in WorldLauncher). Pure so the field-for-field copy — including the
/// easy-to-miss HP — is unit-tested; an omitted field here silently restores every structure at full HP.</summary>
public static PendingStructure ToPending(in StructureSave s) => new PendingStructure
{
Type = s.Type,
CellX = s.CellX,
CellZ = s.CellZ,
Direction = s.Direction,
RemainingTicks = s.RemainingTicks,
ConveyorResId = s.ConveyorResId,
ConveyorCount = s.ConveyorCount,
HP = s.HP,
};
}
}
@@ -37,6 +37,7 @@ namespace ProjectM.Simulation
public uint RemainingTicks;
public byte ConveyorResId;
public int ConveyorCount;
public float HP; // EB-1: staged hit points (BaseRestoreSystem restores 0 -> baked Max)
}
/// <summary>One staged machine I/O row (M7), joined to the <see cref="PendingStructure"/> buffer by index.
@@ -25,6 +25,7 @@ namespace ProjectM.Simulation
public uint RemainingTicks; // production/cooldown ticks left at save time
public byte ConveyorResId; // in-flight conveyor item resource (0 = none)
public int ConveyorCount;
public float HP; // EB-1: hit points at save time (0 from a pre-v3 save -> restored to baked Max)
}
/// <summary>
@@ -50,7 +51,10 @@ namespace ProjectM.Simulation
[Serializable]
public class SaveData
{
public const int CurrentVersion = 2;
public const int CurrentVersion = 3; // EB-1: v3 adds StructureSave.HP
/// <summary>Oldest save schema the loader accepts (additive); a v2 save loads with structures at full HP.</summary>
public const int MinLoadableVersion = 2;
public int Version = CurrentVersion;
public int GoalCharge;
@@ -23,7 +23,9 @@ namespace ProjectM.Simulation
{
if (!File.Exists(FilePath)) return null;
var data = JsonUtility.FromJson<SaveData>(File.ReadAllText(FilePath));
if (data == null || data.Version != SaveData.CurrentVersion) return null;
// EB-1: additive floor [MinLoadableVersion, CurrentVersion] so OLD v2 saves still load (a missing HP
// field 0-defaults and the restore guard maps 0 -> baked Max); v0/v1 garbage is still rejected.
if (data == null || data.Version < SaveData.MinLoadableVersion || data.Version > SaveData.CurrentVersion) return null;
data.Ledger ??= Array.Empty<LedgerRow>();
return data;
}
@@ -35,6 +35,8 @@ namespace ProjectM.Simulation
CellX = ps.Cell.x,
CellZ = ps.Cell.y,
RemainingTicks = ProductionMath.RemainingTicks(ps.NextTick, nowTick),
// EB-1: guarded so automation machines (no Health) don't crash the autosave path (no try/catch).
HP = em.HasComponent<Health>(e) ? em.GetComponentData<Health>(e).Current : 0f,
};
if (em.HasComponent<Conveyor>(e))