Reconstruct lab story, interactions, patrol and jet pack practice flow
This commit is contained in:
1 parent
58424120bb
commit
c929f942e1
84 files changed
+7957
-305
No files matched your search
@@ -0,0 +1,240 @@
|
||||
using System.Numerics;
|
||||
using GamePort.Assets;
|
||||
|
||||
namespace GamePort.Game;
|
||||
|
||||
public sealed record LevelTransition(string LevelFile, string StartPoint, float FadeTime, int FadeType);
|
||||
|
||||
/// <summary>
|
||||
/// Recovered star/trophy/lab jetpack contacts, Carl talk dispatch and LOAD gates. Uses source root spheres
|
||||
/// with recovered lookup magnitude (x87 table identity/exclusions/box contacts pending).
|
||||
/// Other pickup effects and general AI sensing remain explicit unsupported cases.
|
||||
/// </summary>
|
||||
public sealed class SceneInteractions
|
||||
{
|
||||
private readonly SceneDirector director;
|
||||
private readonly TaskStates tasks;
|
||||
private readonly OriginalSave initial;
|
||||
private readonly string levelCode;
|
||||
private readonly int[] itemStates;
|
||||
private readonly HashSet<int> contacts = [];
|
||||
private readonly HashSet<string> unsupported = [];
|
||||
public int Score { get; private set; }
|
||||
public float Energy { get; private set; }
|
||||
public int CurrentObjective { get; private set; }
|
||||
public string ReturnLevel { get; private set; }
|
||||
public string ReturnStart { get; private set; }
|
||||
public SceneInventory Inventory { get; }
|
||||
public IReadOnlyCollection<string> Unsupported => unsupported;
|
||||
public event Action<GamEntity, int>? Collected;
|
||||
public event Action<LevelTransition>? Transition;
|
||||
public event Action<int>? SoundEffect;
|
||||
public event Action<int>? PracticeRequested;
|
||||
public event Action<Vector3>? CheckpointCaptured;
|
||||
public event Action<DirectorActor, string>? ConversationStarted;
|
||||
public event Action<int>? ObjectiveChanged;
|
||||
|
||||
public SceneInteractions(SceneDirector director, TaskStates tasks, OriginalSave save, string? levelCode = null)
|
||||
{
|
||||
this.director = director; this.tasks = tasks; initial = save;
|
||||
this.levelCode = levelCode ?? save.LevelCode;
|
||||
Inventory = new SceneInventory(save.InventoryStates);
|
||||
itemStates = save.ItemStates.ToArray(); Score = save.Score;
|
||||
Energy = save.PlayerValue;
|
||||
CurrentObjective = save.OtherCounter;
|
||||
ReturnLevel = save.ReturnLevel; ReturnStart = save.ReturnStart;
|
||||
tasks.Assigned += OnTaskAssigned;
|
||||
Reset();
|
||||
}
|
||||
public void UpdateEnergy(float energy)
|
||||
{
|
||||
if (!float.IsFinite(energy)) throw new ArgumentOutOfRangeException(nameof(energy));
|
||||
Energy = energy;
|
||||
}
|
||||
|
||||
public OriginalSave Snapshot() => initial with { Score = Score, PlayerValue = Energy, ItemStates = itemStates.ToArray(),
|
||||
InventoryStates = Inventory.Snapshot(), Tasks = tasks.Snapshot(), OtherCounter = CurrentObjective,
|
||||
ReturnLevel = ReturnLevel, ReturnStart = ReturnStart };
|
||||
|
||||
public void SetReturnContext(string levelFile, string start)
|
||||
{
|
||||
if (levelFile.Length > 100 || start.Length > 100 || !levelFile.EndsWith(".gam", StringComparison.OrdinalIgnoreCase)
|
||||
|| levelFile.Contains('/') || levelFile.Contains('\\')) throw new ArgumentException("Invalid original return level/start");
|
||||
ReturnLevel = levelFile; ReturnStart = start;
|
||||
}
|
||||
|
||||
/// <summary>Call after SceneDirector.Begin, including a scene reset.</summary>
|
||||
public void Reset()
|
||||
{
|
||||
contacts.Clear();
|
||||
HideCollected();
|
||||
ApplyCarlTask();
|
||||
}
|
||||
|
||||
public void HideCollected()
|
||||
{
|
||||
foreach (var actor in director.Actors)
|
||||
if (actor.SourceGam is { Kind: "3PIC" } source && source.Integer("PickupIndex", -1) is var id
|
||||
&& id > 0 && id < itemStates.Length && itemStates[id] != 0) actor.Active = actor.Visible = false;
|
||||
}
|
||||
|
||||
public void Update(Vector3 activePlayerRoot, float playerRadius = 60)
|
||||
{
|
||||
if (!float.IsFinite(activePlayerRoot.X + activePlayerRoot.Y + activePlayerRoot.Z)
|
||||
|| !float.IsFinite(playerRadius) || playerRadius < 0) throw new ArgumentOutOfRangeException(nameof(activePlayerRoot));
|
||||
if (!director.Controlled) return;
|
||||
HideCollected();
|
||||
foreach (var actor in director.Actors)
|
||||
{
|
||||
if (!director.Controlled) break;
|
||||
if (actor.SourceGam is not { } source || source.Kind is not ("3PIC" or "3TRO" or "LOAD" or "3CAR")) continue;
|
||||
// C3DFriend update41B972..988 calls the talk dispatcher every update
|
||||
// in state0, even when a prior contact found no matching TalkTrigger.
|
||||
if (source.Kind == "3CAR" && actor.Active && actor.AIstate == 0)
|
||||
{
|
||||
TryBeginCarlTalk(actor, source);
|
||||
if (!director.Controlled) break;
|
||||
}
|
||||
// Carl InitObject413E2F..45 sets radius100 and support factor0.75.
|
||||
var radius = playerRadius + (source.Kind == "3TRO" ? 20 : source.Kind == "3CAR" ? 100 : source.Number("Radius"));
|
||||
var overlaps = actor.Active && OriginalBodySupport.QuickMagnitude(activePlayerRoot - actor.Position) < radius;
|
||||
if (!overlaps) { contacts.Remove(source.Offset); continue; }
|
||||
if (!contacts.Add(source.Offset)) continue;
|
||||
switch (source.Kind)
|
||||
{
|
||||
case "3PIC": CollectPickup(actor, source, activePlayerRoot); break;
|
||||
case "3CAR":
|
||||
//40A44C..45C/40A676..68D: target contact in state6 sets0.
|
||||
var player = director.Actors.FirstOrDefault(a => a.Kind == "3JIM");
|
||||
if (actor.AIstate == 6 && Equal(source.Text("TargetName"), player?.Tag))
|
||||
{
|
||||
actor.AIstate = 0;
|
||||
TryBeginCarlTalk(actor, source);
|
||||
}
|
||||
break;
|
||||
case "3TRO":
|
||||
Score = Math.Max(0, unchecked(Score + 300));
|
||||
actor.Active = actor.Visible = false;
|
||||
SoundEffect?.Invoke(230);
|
||||
Collected?.Invoke(source, 300);
|
||||
break;
|
||||
case "LOAD":
|
||||
if (!TaskActivation.Load(source, tasks)) break;
|
||||
var destination = source.Text("LevelName") ?? "none";
|
||||
var start = source.Text("StartPoint") ?? "none";
|
||||
if (string.Equals(destination, "RETURN", StringComparison.OrdinalIgnoreCase))
|
||||
{ destination = ReturnLevel; start = ReturnStart; }
|
||||
if (string.Equals(destination, "none", StringComparison.OrdinalIgnoreCase))
|
||||
{ unsupported.Add("RETURN has no stored destination"); break; }
|
||||
Transition?.Invoke(new(destination, start, source.Number("FadeTime"), source.Integer("FadeType")));
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void CollectPickup(DirectorActor actor, GamEntity source, Vector3 playerRoot)
|
||||
{
|
||||
var id = source.Integer("PickupIndex", -1);
|
||||
// The source tag is SHRINKRAY; original objective art120 calls this
|
||||
// pickup the jet pack, matching group0/item3 and powered-jump ownership.
|
||||
var jetpack = Equal(source.Tag, "SHRINKRAY");
|
||||
var ordinaryStar = id is >= 3000 and < 4000 && source.Integer("PointValue") == 5;
|
||||
if (id is <= 0 or >= OriginalSave.ItemCount || !(ordinaryStar || jetpack && source.Integer("PointValue") == 50)
|
||||
|| source.Integer("PIC_NUMBER", -1) != -1
|
||||
|| source.Integer("PickedUpIndex", -1) != -1 || source.Integer("RequiredPicNum", -1) != -1)
|
||||
{ unsupported.Add($"Pickup {source.Tag}@0x{source.Offset:X}: inventory/special effect not reconstructed"); return; }
|
||||
if (itemStates[id] != 0) return;
|
||||
itemStates[id] = 1; // EXE435E11..31: ordinary acquired state, hide and disable.
|
||||
actor.Active = actor.Visible = false;
|
||||
//435FCB invokes the special hook before the positive PointValue award.
|
||||
if (jetpack && tasks.Get("SCENE") == 35)
|
||||
{
|
||||
//436603..67E: Jimmy checkpoint capture42AF00, task35->50, group0
|
||||
//item3 state1, practice prompt0, then gadget selection3. The checkpoint
|
||||
//event does not assign the still-unidentified TSK StoredVector.
|
||||
CheckpointCaptured?.Invoke(playerRoot);
|
||||
tasks.SetExisting("SCENE", 50);
|
||||
Inventory.SetState(0, 3, 1);
|
||||
PracticeRequested?.Invoke(0);
|
||||
Inventory.SelectGadget(3);
|
||||
}
|
||||
var points = source.Integer("PointValue");
|
||||
Score = Math.Max(0, unchecked(Score + points));
|
||||
var sound = source.Integer("SoundIndex", -1);
|
||||
if (sound >= 0) SoundEffect?.Invoke(sound);
|
||||
Collected?.Invoke(source, points);
|
||||
var next = source.Text("NextTrigger");
|
||||
if (!string.IsNullOrEmpty(next) && !string.Equals(next, "none", StringComparison.OrdinalIgnoreCase))
|
||||
director.Trigger(next, source.Tag);
|
||||
}
|
||||
|
||||
private void TryBeginCarlTalk(DirectorActor actor, GamEntity source)
|
||||
{
|
||||
var state = tasks.Get(source.Text("TaskName"));
|
||||
for (var i = 0; i < 5; i++)
|
||||
{
|
||||
var tag = source.Text("TalkTrigger" + i);
|
||||
var required = source.Integer("TalkState" + i, -1);
|
||||
if (required == -1 || required != state && required != -2 || string.IsNullOrEmpty(tag)
|
||||
|| Equal(tag, "none") || director.FindActor(tag) is null) continue;
|
||||
//41B7B0 calls derived slot180 BEFORE dispatching the selected object.
|
||||
//Carl413E50 calls base41B810 (+100) then changes SCENE milestones.
|
||||
Score = Math.Max(0, unchecked(Score + 100));
|
||||
var scene = tasks.Get("SCENE");
|
||||
var next = scene switch { 50 => 60, 65 => 70, 270 => 280, _ => scene };
|
||||
if (next != scene) tasks.SetExisting("SCENE", next);
|
||||
director.Trigger(tag, tag); //Original target receives itself as caller.
|
||||
actor.AIstate = 9;
|
||||
ConversationStarted?.Invoke(actor, tag);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
private void OnTaskAssigned(string name, int value)
|
||||
{
|
||||
HideCollected();
|
||||
if (Equal(name, "SCENE"))
|
||||
{
|
||||
//45F990 jump tables ->406F50: clear the transient objective flags,
|
||||
//select this objective and set its flag1; TSK writes selector4F8194.
|
||||
var objective = ObjectiveForScene(value);
|
||||
if (objective != 0) { CurrentObjective = objective; ObjectiveChanged?.Invoke(objective); }
|
||||
ApplyCarlTask();
|
||||
}
|
||||
}
|
||||
|
||||
private void ApplyCarlTask()
|
||||
{
|
||||
//Carl's override413F30 follows generic activation; LV1B branch414068.
|
||||
//Other levels' custom visibility/position policies are not ported here.
|
||||
if (levelCode != "LV1B") return;
|
||||
var state = tasks.Get("SCENE");
|
||||
foreach (var actor in director.Actors.Where(a => a.Kind == "3CAR" && Equal(a.SourceGam?.Text("TaskName"), "SCENE")))
|
||||
{
|
||||
if (state is < 0 or > 60) { actor.Visible = actor.Active = false; continue; }
|
||||
if (state > 30 && director.FindActor("CARLESC3") is { } marker)
|
||||
{
|
||||
actor.AIstate = 6;
|
||||
actor.Position = marker.Position;
|
||||
}
|
||||
//At60 the override leaves the generic activation result unchanged.
|
||||
if (state < 60) actor.Active = actor.Visible = true;
|
||||
}
|
||||
}
|
||||
|
||||
private static bool Equal(string? a, string? b) => a is not null && b is not null
|
||||
&& string.Equals(a, b, StringComparison.OrdinalIgnoreCase);
|
||||
|
||||
public static int ObjectiveForScene(int scene) => scene switch
|
||||
{
|
||||
35 => 1, 50 => 2, 60 => 3, 70 => 4, 90 => 5, 100 => 6, 108 => 7,
|
||||
140 or 141 => 8, 160 => 9, 162 => 10, 168 => 11, 178 => 12,
|
||||
200 => 13, 205 => 14, 210 => 15, 220 => 16, 230 => 17,
|
||||
250 => 18, 260 => 19, 270 => 20, 280 => 21, 290 => 22,
|
||||
300 => 23, 310 => 24, 320 => 25, 330 => 26, 340 => 27,
|
||||
350 => 28, 360 => 29, 380 => 30, 390 => 31, 400 => 32,
|
||||
410 => 33, 420 => 34, 430 => 35, 460 => 36, 470 => 37,
|
||||
480 => 38, 500 => 39, 510 => 40, 520 => 41, 540 => 42, 550 => 43,
|
||||
_ => 0,
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user