232 lines
12 KiB
C#
232 lines
12 KiB
C#
using System.Numerics;
|
|
using GamePort.Assets;
|
|
|
|
namespace GamePort.Game;
|
|
|
|
public sealed record LevelTransition(string LevelFile, string StartPoint, float FadeTime, int FadeType, bool Returning = false);
|
|
|
|
/// <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 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 void ChangeScore(int delta) => Score = Math.Max(0,unchecked(Score + delta));
|
|
|
|
public SceneInteractions(SceneDirector director, TaskStates tasks, OriginalSave save)
|
|
{
|
|
this.director = director; this.tasks = tasks; initial = save;
|
|
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.Assigning += OnTaskAssigning;
|
|
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();
|
|
}
|
|
|
|
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, DirectorActor? vehicle = null)
|
|
{
|
|
if (!float.IsFinite(activePlayerRoot.X + activePlayerRoot.Y + activePlayerRoot.Z)
|
|
|| !float.IsFinite(playerRadius) || playerRadius < 0) throw new ArgumentOutOfRangeException(nameof(activePlayerRoot));
|
|
bool VehicleControlled()=>vehicle is{Kind:"3ROC" or "3JEE"}&&director.AuthoredControlledTag==vehicle.Tag;
|
|
bool Controlled()=>director.Controlled||VehicleControlled();
|
|
if (!Controlled()) return;
|
|
HideCollected();
|
|
foreach (var actor in director.Actors)
|
|
{
|
|
if (!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.
|
|
//435D08 pickups require the raw controlled actor.457EE6 LOAD requires
|
|
//the real Jimmy passenger; the coincident actors keep distinct radii.
|
|
var contactRadius=source.Kind=="3PIC"&&VehicleControlled()?(vehicle!.Kind=="3JEE"?150:50):playerRadius;
|
|
var radius = contactRadius + (source.Kind == "3TRO" ? 20 : source.Kind == "3CAR" ? 100 : source.Number("Radius"));
|
|
var overlaps = actor.Active && (source.Kind!="3TRO" || actor.BodyEnabled)
|
|
&& 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";
|
|
var returning = string.Equals(destination, "RETURN", StringComparison.OrdinalIgnoreCase);
|
|
if (returning)
|
|
{ destination = ReturnLevel; start = ReturnStart; }
|
|
if (string.Equals(destination, "none", StringComparison.OrdinalIgnoreCase))
|
|
{ unsupported.Add("RETURN has no stored destination"); break; }
|
|
if(returning)director.ReleasePlayerGadget(); //457FF7 selected cleanup precedes RETURN.
|
|
Transition?.Invoke(new(destination, start, source.Number("FadeTime"), source.Integer("FadeType"), returning));
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
private void CollectPickup(DirectorActor actor, GamEntity source, Vector3 playerRoot)
|
|
{
|
|
var id = source.Integer("PickupIndex", -1);
|
|
//Group0/item3 is the combined shrink ray/jetpack. Objective120 names its
|
|
//jetpack action; original gadget tile14 and controls113 also name SHRINK.
|
|
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 OnTaskAssigning(string name, int value)
|
|
{
|
|
if (Equal(name, "SCENE"))
|
|
{
|
|
//45FB5B ->406F50 runs BEFORE records change at45FB7F and before
|
|
//entity slot424 callbacks at45FBD9. TSK writes selector4F8194.
|
|
var objective = ObjectiveForScene(value);
|
|
if (objective != 0) { CurrentObjective = objective; ObjectiveChanged?.Invoke(objective); }
|
|
}
|
|
}
|
|
|
|
private void OnTaskAssigned(string name, int value)
|
|
{
|
|
HideCollected();
|
|
}
|
|
|
|
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,
|
|
};
|
|
}
|