Implement timed Invisible gadget and shared energy warnings

This commit is contained in:
UnixNight committed 2026-11-02 09:21:56 -05:00
1 parent 3527d33810
commit 1897dd2b90
44 files changed
+2466 -39

No files matched your search

+1 -1
View File
@@ -90,7 +90,7 @@ public sealed class ActorPatrol
if (!float.IsFinite(actor.Speed)) throw new InvalidOperationException("Nonfinite Goddard speed");
// EXE408023->40E050: the body consumes the *previous* velocity first.
var body = integrateBody(new(actor.Position, Velocity), dt);
var body = actor.BodyEnabled ? integrateBody(new(actor.Position, Velocity), dt) : new PatrolBodyState(actor.Position, Velocity);
if (!Finite(body.Position) || !Finite(body.Velocity))
throw new InvalidOperationException("Goddard body solver produced nonfinite state");
actor.Position = body.Position;
+87
View File
@@ -0,0 +1,87 @@
namespace GamePort.Game;
/// <summary>Jimmy424E80 timed Invisible device; material state and source
/// targetable55D are distinct from raw control and authored visibility.</summary>
public sealed class InvisibleGadget
{
public const float Duration = 7;
public bool Equipped { get; private set; }
public float Remaining { get; private set; }
public float Recharge { get; private set; }
public bool Targetable { get; private set; } = true;
public float Opacity { get; private set; } = 1;
public bool ConventionalBlend { get; private set; }
public bool MeterVisible { get; private set; }
public bool MeterReady { get; private set; }
public float MeterPercent { get; private set; }
public int Activations { get; private set; }
public bool Active => Equipped && !Targetable;
public event Action<bool>? EquippedChanged;
public bool Equip(float energy, bool energyOverride)
{
if (!float.IsFinite(energy)) throw new ArgumentOutOfRangeException(nameof(energy));
if (energy <= 0 && !energyOverride) return false;
Equipped = true; Remaining = Recharge = 0; Targetable = true;
Opacity = 1; ConventionalBlend = false; MeterReady = false; MeterVisible = true;
EquippedChanged?.Invoke(true);
return true;
}
public void Release()
{
Equipped = false; Remaining = Duration; Recharge = 0;
Targetable = true; Opacity = 1; ConventionalBlend = false;
MeterVisible = false; MeterReady = false;
EquippedChanged?.Invoke(false);
}
public void Reset()
{
Release(); Remaining = MeterPercent = 0; Activations = 0;
}
public void Step(float seconds, bool anyControlled, bool primary, bool jump,
float energy, bool energyOverride, Action<float> adjustEnergy, bool suppressed = false)
{
if (!float.IsFinite(seconds) || seconds < 0 || seconds >= 1)
throw new ArgumentOutOfRangeException(nameof(seconds));
if (!float.IsFinite(energy)) throw new ArgumentOutOfRangeException(nameof(energy));
ArgumentNullException.ThrowIfNull(adjustEnergy);
//Any controlled pointer, not necessarily Jimmy's identity. A NULL
//owner freezes timers and hides both meter sprites without cleanup.
if (!anyControlled) { MeterVisible = false; return; }
if (!Equipped) return;
MeterVisible = true;
if (primary && !jump)
{
if (suppressed) return;
if (Remaining == 0 && Recharge == Duration)
{
if (energy <= 2 && !energyOverride) return;
adjustEnergy(-2);
Remaining = Duration; Recharge = 0; Activations++;
ConventionalBlend = false; Opacity = .7f; Targetable = false;
}
}
if (Remaining == 0 && (energy > 0 || energyOverride))
{
var sum = (double)Recharge + seconds;
Recharge = (float)sum;
MeterPercent = (float)(sum * (1f / 7) * 100);
if (Recharge >= Duration) { Recharge = Duration; MeterReady = true; }
}
if (Remaining <= 0) return;
var remaining = (double)Remaining - seconds;
Remaining = (float)remaining;
MeterPercent = (float)Math.Max(remaining * (1f / 7) * 100, 0);
if (Remaining <= 0)
{
Remaining = 0; Opacity = 1; Targetable = true;
ConventionalBlend = false; MeterReady = false;
return;
}
if (Remaining <= 3)
ConventionalBlend = ((int)((double)Remaining * 5) & 1) != 0;
}
}
+28 -5
View File
@@ -19,6 +19,7 @@ public sealed class LabSimulation
private readonly Func<Vector3, Vector3, SupportHit?>? cameraRay;
private readonly Func<Vector3, Vector3, SupportHit?>? ceilingRay;
private readonly OriginalBodyPhysics? originalBody;
private readonly PlayerEnergy energy;
private readonly Vector3 sourceStart;
private readonly float startYaw, modelMinimumY, rootHeight, startFloor;
private float targetYaw, extraAirAcceleration, shadowDistance, lateralContribution;
@@ -28,13 +29,28 @@ public sealed class LabSimulation
private bool forceFall;
private Vector3 noclipEntry;
private float noclipEntryYaw;
private int gadgetMode = -1;
public bool Noclip { get; private set; }
public bool HasNoclipEntry { get; private set; }
public float NoclipSpeed { get; set; } = 600;
public Func<int>? JetpackInventoryState { get; set; }
public string LevelCode { get; set; } = "LV1B";
public int GadgetMode { get; set; } = -1;
public float Energy { get; set; } = 20;
public InvisibleGadget Invisible { get; } = new();
public int GadgetMode
{
get => gadgetMode;
set
{
if (gadgetMode == 2) Invisible.Release();
gadgetMode = -1;
if (value == 2 && !Invisible.Equip(Energy, IgnoresGadgetEnergy)) return;
gadgetMode = value;
}
}
public bool IgnoresGadgetEnergy => PlayerEnergy.IsTutorial(LevelCode);
public float Energy { get => energy.Value; set => energy.Value = value; }
public bool LowEnergyWarning => energy.LowWarning;
public event Action<int>? EnergySound;
//Source260/live15C serves both body collision and contact tests. The name
//predates source-body integration; burst100 is not an interaction-only size.
public float InteractionRadius { get; private set; } = 60;
@@ -84,12 +100,13 @@ public sealed class LabSimulation
public LabSimulation(SceneCollision collision, Mesh standingPose, Vector3 start, float yaw,
Func<Vector3, Vector3, SupportHit?>? cameraRay = null,
Func<Vector3, Vector3, SupportHit?>? ceilingRay = null,
OriginalStaticCollision? originalCollision = null)
OriginalStaticCollision? originalCollision = null, PlayerEnergy? energy = null)
{
this.collision = collision;
this.cameraRay = cameraRay;
this.ceilingRay = ceilingRay;
originalBody = originalCollision == null ? null : new OriginalBodyPhysics(originalCollision);
this.energy = energy ?? new PlayerEnergy();
sourceStart = start;
startYaw = yaw;
var bounds = standingPose.Bounds();
@@ -107,6 +124,8 @@ public sealed class LabSimulation
public void Reset()
{
if (gadgetMode == 2) gadgetMode = -1;
Invisible.Reset();
var floor = collision.GroundHeight(sourceStart, 2500)!.Value;
Position = new(sourceStart.X, floor + (UsesOriginalBody ? 0 : SceneCollision.Skin), sourceStart.Z);
Velocity = Vector3.Zero;
@@ -162,7 +181,7 @@ public sealed class LabSimulation
return ((float)Math.Sin(radians), (float)Math.Cos(radians));
}
public void Step(PlayerInput input, bool controlled = true)
public void Step(PlayerInput input, bool controlled = true, bool? anyControlled = null)
{
if (Noclip) { StepNoclip(input); return; }
//47E554 global body integration precedes473740 registry callbacks.
@@ -176,8 +195,11 @@ public sealed class LabSimulation
//424BE0..424C34 runs this wrapper before437940's ownership gate.
StepCeiling();
if (controlled) StepPlayer(input);
Invisible.Step(FixedStepClock.Step, anyControlled ?? controlled, input.Fire, input.Jump,
Energy, IgnoresGadgetEnergy, AdjustEnergy);
var effect = JetpackBurst.StepEffect(jumpState, Grounded, FixedStepClock.Step, Energy, LevelCode);
jumpState = effect.State; Energy = effect.Energy; JetpackEffectVisible = effect.Visible;
jumpState = effect.State; JetpackEffectVisible = effect.Visible;
if (effect.Visible) AdjustEnergy(-FixedStepClock.Step * JetpackBurst.EnergyPerSecond);
EffectSteps++;
if (controlled && InputAfterPlayerUpdate) StepInput(input);
Steps++;
@@ -192,6 +214,7 @@ public sealed class LabSimulation
public void StopForPrimaryFire() => Speed = 0;
public void SetShooting(bool shooting) => Shooting = shooting;
public void AdjustEnergy(float delta) => energy.Adjust(delta, LevelCode, id => EnergySound?.Invoke(id));
private void StepCeiling()
{
+50
View File
@@ -0,0 +1,50 @@
namespace GamePort.Game;
/// <summary>42A920 global energy and low-warning latch. Retained across ordinary
/// level loads; the original TSK stores the scalar, not this warning latch.</summary>
public sealed class PlayerEnergy
{
private float value = 20;
public float Value
{
get => value;
set
{
if (!float.IsFinite(value)) throw new ArgumentOutOfRangeException(nameof(value));
this.value = value;
}
}
public bool LowWarning { get; private set; }
public void Adjust(float delta, string level, Action<int>? sound = null)
{
if (!float.IsFinite(delta)) throw new ArgumentOutOfRangeException(nameof(delta));
ArgumentNullException.ThrowIfNull(level);
if (IsTutorial(level)) return;
//The first x87 sum is compared before a float store. Sound80 precedes
//the write; the helper then reloads the live global for the actual add.
if (value > 0 && (double)value + delta <= 0) sound?.Invoke(80);
var sum = (double)value + delta;
value = (float)sum;
if (sum > 100) { value = 100; LowWarning = false; return; }
if (value < 0) value = 0;
if (value < 19)
{
if (!LowWarning)
{
sound?.Invoke(182);
LowWarning = true;
}
}
else if (value > 20) LowWarning = false;
}
public static bool IsTutorial(string level)
{
ArgumentNullException.ThrowIfNull(level);
if (level.Length != 4) return false;
var code = ((uint)level[0] << 24) | ((uint)level[1] << 16)
| ((uint)level[2] << 8) | level[3];
return code is >= 0x56523031 and <= 0x56523038;
}
}
+25 -8
View File
@@ -17,14 +17,16 @@ public sealed class PlayerSceneBinding : IDisposable
this.director = director; this.player = player; this.inventory = inventory; this.tag = tag;
director.ActorPreparing += Prepare;
director.AuthoredTransformChanged += Transform;
director.GadgetReleaseRequested += Release;
inventory.GadgetSelected += Selected;
player.Invisible.EquippedChanged += InvisibleEquipped;
Selected(inventory.SelectedGadget);
}
public bool Step(PlayerInput input)
{
var acceptsInput = director.Controlled || player.Noclip;
player.Step(input, acceptsInput);
player.Step(input, acceptsInput, director.ControlledTag != null);
var rotation = director.FindActor(tag)?.Rotation ?? Vector3.Zero;
director.SyncActor(tag, player.ActorRoot, rotation with { Y = player.Yaw });
return acceptsInput;
@@ -35,17 +37,30 @@ public sealed class PlayerSceneBinding : IDisposable
if (!string.Equals(actor.Tag, tag, StringComparison.OrdinalIgnoreCase)) return;
if (action == ScriptPreparation.StopMotion) player.PrepareForScript();
else if (action == ScriptPreparation.ReleaseGadget)
{
//427FF0 has distinct cases. Only no-selection/jetpack cleanup is
//implemented; do not turn the other gadget cases into blanket resets.
if (inventory.SelectedGadget is -1 or 3)
{ inventory.SelectGadget(-1); player.GadgetMode = -1; }
else UnsupportedGadgetCleanups++;
}
Release(actor);
}
private void Release(DirectorActor actor)
{
if (!string.Equals(actor.Tag, tag, StringComparison.OrdinalIgnoreCase)) return;
//427FF0 switches raw mode, not display selection. A rejected Invisible
//equip can leave display2 with raw mode-1; cleanup does not equip it.
if (player.GadgetMode == 2 || inventory.SelectedGadget is -1 or 3)
{ inventory.SelectGadget(-1); player.GadgetMode = -1; }
else if (player.GadgetMode != -1) UnsupportedGadgetCleanups++;
}
private void Selected(int item) => player.GadgetMode = GadgetScreen.EquippedMode(item);
private void InvisibleEquipped(bool equipped)
{
if (director.FindActor("C3DGODDARD") is not { } companion) return;
//427FF0 requires Goddard complete9E8==false before showing/enabling.
//These six PostLoad levels set that flag (retained-companion.md).
if (!equipped && player.LevelCode is "LEV6" or "LEV7" or "LV1C" or "LV1D" or "LV4A" or "LV6A") return;
companion.Visible = companion.MovementEnabled = companion.BodyEnabled = !equipped;
}
private void Transform(DirectorActor actor)
{
if (string.Equals(actor.Tag, tag, StringComparison.OrdinalIgnoreCase))
@@ -56,6 +71,8 @@ public sealed class PlayerSceneBinding : IDisposable
{
director.ActorPreparing -= Prepare;
director.AuthoredTransformChanged -= Transform;
director.GadgetReleaseRequested -= Release;
inventory.GadgetSelected -= Selected;
player.Invisible.EquippedChanged -= InvisibleEquipped;
}
}
+17 -1
View File
@@ -14,6 +14,10 @@ public sealed class DirectorActor
public Vector3 Rotation { get; internal set; }
public bool Visible { get; internal set; }
public bool Active { get; internal set; }
//Source body integration and movement-enable flags are separate from AI
//activation. Only the bounded companion gadget consumer writes them here.
public bool BodyEnabled { get; internal set; } = true;
public bool MovementEnabled { get; internal set; } = true;
public string Animation { get; internal set; } = "STOP";
public int AIstate { get; internal set; }
public float Speed { get; internal set; }
@@ -93,6 +97,7 @@ public sealed class SceneDirector
public event Action? MusicRestarted;
public event Action<DirectorActor>? ActorChanged;
public event Action<DirectorActor, ScriptPreparation>? ActorPreparing;
public event Action<DirectorActor>? GadgetReleaseRequested;
public event Action<DirectorActor>? AuthoredTransformChanged;
public event Action<string, string>? Unsupported;
@@ -138,6 +143,7 @@ public sealed class SceneDirector
var initial = new DirectorActor(source, actor.Tag, actor.Kind, source.Position);
actor.Position = initial.Position; actor.Rotation = initial.Rotation;
actor.Visible = initial.Visible; actor.Active = initial.Active;
actor.BodyEnabled = initial.BodyEnabled; actor.MovementEnabled = initial.MovementEnabled;
actor.Animation = initial.Animation; actor.AIstate = initial.AIstate;
actor.Speed = initial.Speed; actor.Patrol = initial.Patrol;
actor.SpritePlaying = initial.SpritePlaying; actor.SpriteRevision++;
@@ -336,6 +342,7 @@ public sealed class SceneDirector
private void StartMulti(GamEntity entity)
{
DeactivateInvention(entity); //43129A: explicit Jimmy lookup, before first target check.
// MCA's PlayerControlled is its completion target (4311F0), not an
// activation grant. Only initial activation clears control; later shots
//use431750 without repeating431502..4315A7 preparation.
@@ -353,11 +360,12 @@ public sealed class SceneDirector
{
if (applyCommon)
{
DeactivateInvention(entity); //4165B7, distinct from raw-owner preparation.
if (Equal(entity.Text("PlayerControlled"), "NULL"))
PrepareOwner(ScriptPreparation.ReleaseGadget, ScriptPreparation.StopMotion, ScriptPreparation.FlattenOrientation);
SetControl(entity); Toggle(entity);
}
if (entity.Integer("DeactivateInv") != 0) InventoryEnabled = false;
if (entity.Integer("DeactivateInv", 1) != 0) InventoryEnabled = false;
if (skipping)
{
if (index >= 0) FinishMulti(entity);
@@ -512,6 +520,14 @@ public sealed class SceneDirector
}
}
private void DeactivateInvention(GamEntity source)
{
//Both CAM/MCA constructors default this property to true. The source
//explicitly looks up Jimmy even when a different actor owns control.
if (source.Integer("DeactivateInv", 1) != 0 && actors.TryGetValue("JIM1", out var jimmy))
GadgetReleaseRequested?.Invoke(jimmy);
}
private void SetControl(GamEntity entity, bool prepareOwner = false)
{
var control = entity.Text("PlayerControlled");
+2 -3
View File
@@ -52,12 +52,11 @@ public sealed class SceneGadgets : IDisposable
player.SetShooting(player.Grounded);
if (!loop) { Sound?.Invoke(0,true); loop = true; }
const float dt = FixedStepClock.Step;
if (!(player.LevelCode.Length == 4 && player.LevelCode.StartsWith("VR0",StringComparison.Ordinal)
&& player.LevelCode[3] is >= '1' and <= '8')) player.Energy = Math.Clamp(player.Energy - dt,0,100);
player.AdjustEnergy(-dt);
//Bridge modern signed float yaw to the source's nonnegative Euler domain.
var yaw = (player.Yaw % 360 + 360) % 360;
var hits = Ray.Fire(player.ActorRoot,yaw,dt,
Soldiers.Select(s => new ShrinkCandidate(s.Actor.Tag,s.Actor.Position,Shrunk:s.Shrunk)),
Soldiers.Select(s => new ShrinkCandidate(s.Actor.Tag,s.Actor.Position,BodyEnabled:s.Actor.BodyEnabled,Shrunk:s.Shrunk)),
(start,end) => geometry.TraceGeometry(start,end));
foreach (var tag in hits)
{