feat(03-04): FallDamageSuppressorSystem + wiring + cleanup [DBG throttle] (Task 3)

- FallDamageSuppressorSystem: subclass DamageEventSystem, cancel Damage
  cause=Fall quand guard.shouldSuppressFallDamage; FALL_INDEX lazy via
  DamageCause.getAssetMap().getIndex; groupe inspectDamageGroup
- GravityApplier: 3-arg constructor avec FallDamageGuard; first-match
  region, filtres AffectPlayers/Npcs/Items avant wake, seed addForce
  paramétré par VerticalForce (remplace hardcode 0.1); notifie
  guard.markInRegion / markExit (via lastKnownRegion map pour Pass 2)
- Cleanup: retire les AtomicInteger counters + log [DBG tick=...]
  throttle 1s; conserve logs one-shot [DBG npc.woken]/[DBG npc.ctrlNull]
- GravityFlipPlugin.setup(): instancie FallDamageGuard, registerSystem
  sur entityStoreRegistry; start() passe guard au GravityApplier
- Imports Query + CommandBuffer alignés sur FlockMembershipSystems
This commit is contained in:
2026-04-23 14:03:58 +02:00
parent f9d5595837
commit d43aa4a98c
3 changed files with 218 additions and 91 deletions
@@ -25,8 +25,6 @@ import java.util.HashSet;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
import java.util.function.Consumer;
/**
@@ -35,18 +33,31 @@ import java.util.function.Consumer;
* {@code CommandBuffer.replaceComponent(...)} inside a {@code Store.forEachEntityParallel(...)}
* lambda — the ECS engine commits them on the main thread after the parallel pass.
*
* <p>Phase 03-02: in addition to the ECS toggle (still required for items, consumed by
* {@code ItemPrePhysicsSystem}), we wake up the per-entity cached movement settings:
* <p>Phase 03-02: wake-ups per-entity :
* <ul>
* <li>Players: {@code MovementManager.setDefaultSettings + applyDefaultSettings + update(packetHandler)}
* — sends the {@code UpdateMovementSettings} packet (ID 110) to the client.</li>
* <li>NPCs: {@code Role.getActiveMotionController().updatePhysicsValues(PhysicsValues)}
* — re-applies {@code MovementManager.MASTER_DEFAULT.apply} on the cached settings.</li>
* <li>Players: {@code MovementManager.setDefaultSettings + applyDefaultSettings + update(packetHandler)}</li>
* <li>NPCs: {@code Role.getActiveMotionController().updatePhysicsValues(PhysicsValues)}</li>
* </ul>
*
* <p>Dedup: a thread-safe {@link Set}{@code <UUID>} tracks entities flipped at the previous tick.
* Entities that left an active region are restored to {@code invertedGravity=false} in a second
* pass (trade-off documented in the plan — second O(N) scan accepted v1).
* <p>Phase 03-03: seed {@code addForce(0, +0.1, 0)} on NPCs each tick in-region to activate
* {@code computeNewFallSpeed} path which honours {@code invertedGravity}.
*
* <p>Phase 03-04:
* <ul>
* <li>Per-region tuning consumed : {@code AffectPlayers} / {@code AffectNpcs} / {@code AffectItems}
* filter BEFORE any wake / cmdBuf flip ; {@code VerticalForce} replaces the hardcoded 0.1.</li>
* <li>{@link FallDamageGuard} notified on entry (pass 1) and exit (pass 2) with the
* first-matched region to drive {@link FallDamageSuppressorSystem}.</li>
* <li>Debug throttle logs {@code [DBG tick=...]} removed. One-shot {@code [DBG npc.woken]}
* and {@code [DBG npc.ctrlNull]} preserved — useful when diagnosing new NPC role classes.</li>
* </ul>
*
* <p><b>Multi-region precedence :</b> for an entity simultaneously inside N regions, the FIRST
* region encountered in the iteration of {@code snapshot.byRegion().keySet()} (Java insertion
* order via the underlying {@code LinkedHashMap}) drives the config values read this tick
* (VerticalForce, AffectXxx, FallDamage, GracePeriodMs). Rule applies consistently to
* {@link FallDamageGuard#markInRegion} (same first-matched region) and {@link FallDamageGuard#markExit}
* (last-known first-matched region at the previous in-region tick).
*/
public final class GravityApplier {
// Lazy ComponentType holders — pattern identique à RegionRegistry.transform()
@@ -132,38 +143,33 @@ public final class GravityApplier {
// THREADING: écrit/lu depuis les workers ECS via forEachEntityParallel → ConcurrentHashMap.newKeySet obligatoire.
private final Set<UUID> previouslyInverted = ConcurrentHashMap.newKeySet();
/** First-matched region per UUID at the previous tick — consulted in Pass 2 for markExit. */
private final ConcurrentHashMap<UUID, GravityFlipRegion> lastKnownRegion = new ConcurrentHashMap<>();
private final Consumer<Throwable> errorHandler;
private final Consumer<String> infoHandler;
private final FallDamageGuard guard;
// --- Debug diag (throttled counters + per-UUID one-shot) ---
private final AtomicLong tickCounter = new AtomicLong();
private final AtomicInteger entitiesInRegionTick = new AtomicInteger();
private final AtomicInteger playersWokenTick = new AtomicInteger();
private final AtomicInteger npcsSeenTick = new AtomicInteger();
private final AtomicInteger npcsRoleNullTick = new AtomicInteger();
private final AtomicInteger npcsControllerNullTick = new AtomicInteger();
private final AtomicInteger npcsWokenTick = new AtomicInteger();
private final AtomicInteger otherEntitiesTick = new AtomicInteger();
private final AtomicInteger wakeExceptionsTick = new AtomicInteger();
/** One-shot per-UUID log tracking (diagnostic prod) — retained after Plan 03-04 cleanup. */
private final Set<UUID> loggedNpcUuids = ConcurrentHashMap.newKeySet();
public GravityApplier(Consumer<Throwable> errorHandler) {
this(errorHandler, null);
this(errorHandler, null, new FallDamageGuard());
}
public GravityApplier(Consumer<Throwable> errorHandler, Consumer<String> infoHandler) {
this(errorHandler, infoHandler, new FallDamageGuard());
}
public GravityApplier(Consumer<Throwable> errorHandler, Consumer<String> infoHandler, FallDamageGuard guard) {
this.errorHandler = errorHandler == null ? t -> {} : errorHandler;
this.infoHandler = infoHandler == null ? m -> {} : infoHandler;
this.guard = guard == null ? new FallDamageGuard() : guard;
}
/**
* Construit un nouveau {@link PhysicsValues} en copiant mass/drag de la source et en fixant
* {@code invertedGravity=target}. Pure data — pas d'effet de bord.
*
* <p>NOTE test : le constructeur statique de {@link PhysicsValues} dépend du runtime Hytale
* (ModuleRegistry / PluginBase) et échoue hors serveur. La décomposition pure est exposée par
* {@link #buildFlaggedDecision(double, double, boolean)} pour les tests unitaires — Rule 3
* fallback documenté dans le plan 03-02 (section Task 1 action).
*/
static PhysicsValues buildPhysicsValuesWithFlag(PhysicsValues source, boolean target) {
FlaggedDecision d = buildFlaggedDecision(source.getMass(), source.getDragCoefficient(), target);
@@ -188,11 +194,6 @@ public final class GravityApplier {
/** Tick entry point. NO-OP si world ou snapshot est null. */
public void apply(World world, RegionSnapshot snapshot) {
if (world == null || snapshot == null) return;
// THREADING (fix WorldThread assert 2026-04-23) : `Store.forEachEntityParallel` exige
// d'être initié depuis la WorldThread (assertThread @Store.java:2362). Le tick loop tourne
// sur `GravityFlip-Detect` (ScheduledExecutorService) → on dispatch tout le travail ECS
// via `world.execute(Runnable)`. Le CommandBuffer reste utilisé côté mutations, mais
// ne résout PAS l'assert côté appelant (amendement D-04 du plan 03-01).
world.execute(() -> applyOnWorldThread(world, snapshot));
}
@@ -226,34 +227,51 @@ public final class GravityApplier {
com.hypixel.hytale.math.vector.Vector3d pos = t.getPosition();
double x = pos.x, y = pos.y, z = pos.z;
boolean inAnyRegion = false;
// First-match wins for multi-region precedence (Plan 03-04).
GravityFlipRegion matchedRegion = null;
for (GravityFlipRegion r : enabledRegions) {
if (r.asBox().containsPosition(x, y, z)) { inAnyRegion = true; break; }
if (r.asBox().containsPosition(x, y, z)) { matchedRegion = r; break; }
}
if (!inAnyRegion) return;
if (matchedRegion == null) return;
UUID u = uc.getUuid();
currentlyInRegion.add(u);
entitiesInRegionTick.incrementAndGet();
// --- existant (plan 03-01) : flip ECS native ---
// --- Plan 03-04 : AffectXxx filters applied BEFORE wake ---
EntityKind kind = classify(chunk, index, MMT, PRT, PLT, NPCT);
boolean allowed;
switch (kind) {
case PLAYER: allowed = matchedRegion.isAffectPlayers(); break;
case NPC: allowed = matchedRegion.isAffectNpcs(); break;
default: allowed = matchedRegion.isAffectItems(); break;
}
if (!allowed) {
// Entité filtrée : ne PAS la compter dans currentlyInRegion, et
// ne PAS notifier le guard — le filtre se comporte comme hors-zone.
return;
}
currentlyInRegion.add(u);
lastKnownRegion.put(u, matchedRegion);
guard.markInRegion(u, matchedRegion);
// --- Flip ECS native (plan 03-01) ---
if (!v.isInvertedGravity()) {
Ref<EntityStore> ref = chunk.getReferenceTo(index);
cmdBuf.replaceComponent(ref, PHYST,
new PhysicsValues(v.getMass(), v.getDragCoefficient(), true));
}
// --- NOUVEAU (plan 03-02) : wake-up joueur/NPC dans le même lambda ---
wakePlayerOrNpc(chunk, index, v, true, MMT, PRT, PLT, NPCT);
// --- Wake-up joueur/NPC (plan 03-02) + seed VerticalForce (plan 03-03 paramétré 03-04) ---
wakePlayerOrNpc(chunk, index, v, true, matchedRegion,
MMT, PRT, PLT, NPCT);
});
// PASS 2 — restore : pour chaque UUID dans previouslyInverted \ currentlyInRegion,
// re-localiser l'entité (second scan) et queue le flip OFF via cmdBuf +
// wake-up joueur/NPC avec flag=false.
// PASS 2 — restore : pour chaque UUID dans previouslyInverted \ currentlyInRegion.
Set<UUID> toRestore = ConcurrentHashMap.newKeySet();
toRestore.addAll(previouslyInverted);
toRestore.removeAll(currentlyInRegion);
if (!toRestore.isEmpty()) {
long now = System.currentTimeMillis();
store.forEachEntityParallel(PHYST, (index, chunk, cmdBuf) -> {
UUIDComponent uc;
PhysicsValues v;
@@ -262,7 +280,8 @@ public final class GravityApplier {
v = chunk.getComponent(index, PHYST);
} catch (Throwable ignored) { return; }
if (uc == null || v == null) return;
if (!toRestore.contains(uc.getUuid())) return;
UUID u = uc.getUuid();
if (!toRestore.contains(u)) return;
if (v.isInvertedGravity()) {
Ref<EntityStore> ref = chunk.getReferenceTo(index);
@@ -270,49 +289,63 @@ public final class GravityApplier {
new PhysicsValues(v.getMass(), v.getDragCoefficient(), false));
}
// --- NOUVEAU (plan 03-02) : wake-up joueur/NPC avec flag=false ---
wakePlayerOrNpc(chunk, index, v, false, MMT, PRT, PLT, NPCT);
// Wake-up avec flag=false pour restaurer les settings natifs.
wakePlayerOrNpc(chunk, index, v, false, null, MMT, PRT, PLT, NPCT);
// Plan 03-04 : notifier guard.markExit avec la région last-known
// (première région matchée au tick précédent).
GravityFlipRegion lastRegion = lastKnownRegion.remove(u);
if (lastRegion != null) {
guard.markExit(u, lastRegion, now);
}
});
}
// Update tracker — ces ops sont sur le tick thread après la fin du pass parallel.
previouslyInverted.clear();
previouslyInverted.addAll(currentlyInRegion);
// --- DEBUG : dump 1×/sec (10 ticks @100ms) ---
long tick = tickCounter.incrementAndGet();
if (tick % 10 == 0) {
int inReg = entitiesInRegionTick.getAndSet(0);
int players = playersWokenTick.getAndSet(0);
int npcs = npcsSeenTick.getAndSet(0);
int roleNull = npcsRoleNullTick.getAndSet(0);
int ctrlNull = npcsControllerNullTick.getAndSet(0);
int npcsWoken = npcsWokenTick.getAndSet(0);
int others = otherEntitiesTick.getAndSet(0);
int excs = wakeExceptionsTick.getAndSet(0);
if (inReg > 0 || npcs > 0 || players > 0 || excs > 0) {
infoHandler.accept(String.format(
"[DBG tick=%d] entitiesInRegion=%d players=%d npcs=%d (roleNull=%d ctrlNull=%d woken=%d) others=%d excs=%d regions=%d",
tick, inReg, players, npcs, roleNull, ctrlNull, npcsWoken, others, excs, enabledRegions.size()));
}
}
} catch (Throwable th) {
errorHandler.accept(th);
}
}
private enum EntityKind { PLAYER, NPC, OTHER }
/** Classify the entity at {@code index} into player / NPC / other (items fall into other). */
private EntityKind classify(ArchetypeChunk<EntityStore> chunk, int index,
ComponentType<EntityStore, MovementManager> MMT,
ComponentType<EntityStore, PlayerRef> PRT,
ComponentType<EntityStore, Player> PLT,
ComponentType<EntityStore, NPCEntity> NPCT) {
try {
MovementManager mm = chunk.getComponent(index, MMT);
if (mm != null) {
PlayerRef pr = null; Player pl = null;
try { pr = chunk.getComponent(index, PRT); } catch (Throwable ignored) {}
try { pl = chunk.getComponent(index, PLT); } catch (Throwable ignored) {}
if (pr != null && pl != null) return EntityKind.PLAYER;
}
} catch (Throwable ignored) {}
try {
NPCEntity npc = chunk.getComponent(index, NPCT);
if (npc != null) return EntityKind.NPC;
} catch (Throwable ignored) {}
return EntityKind.OTHER;
}
/**
* Wake-up dans le MÊME lambda parallèle :
* - joueur (MovementManager + PlayerRef + Player) → setDefaultSettings + applyDefaultSettings + update(ph)
* - NPC (NPCEntity avec role non-null et active controller non-null) → updatePhysicsValues(targetValues)
* - sinon (items, autres) : no-op (le flip cmdBuf.replaceComponent du pass 1 suffit)
*
* <p>On NE relit PAS le PhysicsValues via l'ECS — le cmdBuf n'est pas encore commit. On construit
* localement le PhysicsValues cible via {@link #buildPhysicsValuesWithFlag}.
* <p>Plan 03-04 : le paramètre {@code matchedRegion} (non-null en entrée, null en sortie)
* fournit {@code VerticalForce} — remplace le hardcode 0.1 du plan 03-03.
*/
private void wakePlayerOrNpc(
ArchetypeChunk<EntityStore> chunk, int index,
PhysicsValues sourceValues, boolean targetFlag,
GravityFlipRegion matchedRegion,
ComponentType<EntityStore, MovementManager> MMT,
ComponentType<EntityStore, PlayerRef> PRT,
ComponentType<EntityStore, Player> PLT,
@@ -332,12 +365,10 @@ public final class GravityApplier {
mm.applyDefaultSettings();
PacketHandler ph = pr.getPacketHandler();
mm.update(ph);
playersWokenTick.incrementAndGet();
} catch (Throwable th) {
wakeExceptionsTick.incrementAndGet();
errorHandler.accept(th);
}
return; // un joueur n'est pas un NPC — court-circuit
return; // un joueur n'est pas un NPC
}
}
@@ -345,16 +376,14 @@ public final class GravityApplier {
NPCEntity npc = null;
try { npc = chunk.getComponent(index, NPCT); } catch (Throwable ignored) {}
if (npc != null) {
npcsSeenTick.incrementAndGet();
try {
Role role = npc.getRole();
if (role == null) {
npcsRoleNullTick.incrementAndGet();
// rôle non résolu — rien à faire, no-op
} else {
MotionController active = role.getActiveMotionController();
if (active == null) {
npcsControllerNullTick.incrementAndGet();
// log one-shot : rôle non-null mais controller null
// log one-shot : rôle non-null mais controller null (utile diag prod)
UUIDComponent uc = null;
try { uc = chunk.getComponent(index, uuidType()); } catch (Throwable ignored) {}
if (uc != null && loggedNpcUuids.add(uc.getUuid())) {
@@ -364,29 +393,22 @@ public final class GravityApplier {
}
} else {
active.updatePhysicsValues(targetValues);
npcsWokenTick.incrementAndGet();
// --- NOUVEAU (plan 03-03) : seed forceVelocity.y pour activer le path
// MotionControllerWalk ligne ~881 qui appelle computeNewFallSpeed
// seule fonction qui HONORE movementSettings.invertedGravity pour NPCs.
// Le path normal WALKING/DESCENDING utilise this.gravity (non-inverse,
// clampé ≥0), d'où l'absence d'effet du flip natif sur les moutons.
// Un seed de +0.1 à chaque tick suffit pour que forceVelocity.y ≠ 0 ;
// ensuite computeNewFallSpeed applique l'accel terminal inversée.
// Au sortie (targetFlag=false), on n'ajoute rien — la damping
// +epsilon (cf. MotionControllerBase line 635-637) zéroe naturellement.
if (targetFlag) {
// Plan 03-04 : seed forceVelocity.y paramétré par VerticalForce (remplace
// le hardcode 0.1 de Plan 03-03). Uniquement en entrée (targetFlag=true)
// à la sortie le damping natif zéroe forceVelocity.
if (targetFlag && matchedRegion != null) {
double vf = matchedRegion.getVerticalForce();
try {
active.addForce(
new com.hypixel.hytale.math.vector.Vector3d(0, 0.1, 0),
new com.hypixel.hytale.math.vector.Vector3d(0, vf, 0),
null);
} catch (Throwable th) {
wakeExceptionsTick.incrementAndGet();
errorHandler.accept(th);
}
}
// log one-shot : première fois qu'on voit cet UUID NPC — on dump le controller class
// log one-shot : première fois qu'on voit cet UUID NPC
UUIDComponent uc = null;
try { uc = chunk.getComponent(index, uuidType()); } catch (Throwable ignored) {}
if (uc != null && loggedNpcUuids.add(uc.getUuid())) {
@@ -397,14 +419,12 @@ public final class GravityApplier {
}
}
} catch (Throwable th) {
wakeExceptionsTick.incrementAndGet();
errorHandler.accept(th);
}
return;
}
// sinon : item / autre — pas de wake-up, le cmdBuf.replaceComponent du pass 1 suffit
otherEntitiesTick.incrementAndGet();
// sinon : item / autre — no-op
}
/** Pure data-diff utilitaire pour tests unitaires (pas de runtime Hytale). */