Files

74 lines
2.9 KiB
C#

using GamePort.Assets;
namespace GamePort.Game;
/// <summary>
/// The two inventory-state groups serialized by the original TSK writer.
/// Preserves unknown existing values; this is not a complete inventory UI or
/// implementation of the separate 29-item consumable-count group.
/// </summary>
public sealed class SceneInventory
{
private readonly int[] states;
public int SelectedGadget { get; private set; } = -1;
public event Action<int>? GadgetSelected;
public SceneInventory(IEnumerable<int> savedStates)
{
states = savedStates.ToArray();
if (states.Length != OriginalSave.InventoryCount)
throw new ArgumentException("Expected the original 19 inventory states", nameof(savedStates));
}
public int State(int group, int item) => states[Offset(group, item)];
public void SetState(int group, int item, int state) => states[Offset(group, item)] = state;
/// <summary>
/// Jimmy PostLoad: enable eight owned gadgets, disable them in VR01..08,
/// then re-enable that tutorial's gadget in the later level-specific tail.
/// LV1B disables gadgets1/7. Other level-specific tails remain unported.
/// State4 is unavailable ownership, distinct from unowned0.
/// </summary>
public void ApplyLevelAvailability(string levelCode)
{
ArgumentNullException.ThrowIfNull(levelCode);
ReadOnlySpan<int> items = [7, 3, 0, 5, 1, 6, 2, 4];
foreach (var item in items)
if (states[item] == 4) states[item] = 1;
// Exactly the eight original FourCC values in the source numeric range.
if (levelCode.Length == 4 && levelCode.StartsWith("VR0", StringComparison.Ordinal)
&& levelCode[3] is >= '1' and <= '8')
{
foreach (var item in items)
if (states[item] == 1) states[item] = 4;
//4242AF..424400: each practice scene enables its matching gadget.
var practiceItem = levelCode[3] - '1';
if (states[practiceItem] == 4) states[practiceItem] = 1;
}
else if (levelCode == "LV1B")
{
//424102: lab restrictions follow the common enable pass.
foreach (var item in new[] { 1, 7 })
if (states[item] == 1) states[item] = 4;
}
}
public void SelectGadget(int item, bool equip = true)
{
if (item != -1 && (item is < 0 or >= 9 || State(0, item) == 0))
throw new ArgumentOutOfRangeException(nameof(item), "Gadget is outside the recovered group or unavailable");
SelectedGadget = item;
if (equip) GadgetSelected?.Invoke(item);
}
public int[] Snapshot() => states.ToArray();
private static int Offset(int group, int item) => group switch
{
0 when item is >= 0 and < 9 => item,
1 when item is >= 0 and < 10 => 9 + item,
_ => throw new ArgumentOutOfRangeException(nameof(item), "Only the two serialized inventory groups are recovered here"),
};
}