Files
Pal3.Unity/Assets/Scripts/Pal3.Game/Script/UserVariableManager.cs
Jiaqi (0x7c13) Liu 7e5df726cc Implement PAL3A command handlers and isolate script execution
Correct PAL3A parameter signatures and add handlers for fortune selection,
skybox sequences, actor facing and attacks, team and progression state,
camera constraints, and battle preparation flags.

Give script instances separate locals, advance grouped waits concurrently,
and pause PAL3A parents only for their directly decoded child calls. Cancel
stale interactions and save replay on reset, preserve finite action semantics,
and restore camera orientation after releasing horizontal constraints.

Persist supported state in v2 saves and add protocol, runtime, and progression
regressions. Full combat gameplay remains outside this change.

Validation: PAL3 274/274 and PAL3A 291/291 EditMode tests; 18 PAL3 and 22 PAL3A
runtime checks. All 77,889 PAL3A instruction boundaries match EXE signatures.
2026-11-13 00:49:36 +08:00

229 lines
8.9 KiB
C#

// ------------------------------------------------------------------------
// Copyright (c) 2021-2026, Jiaqi (0x7c13) Liu. All rights reserved.
// See LICENSE file in the project root for license information.
// ------------------------------------------------------------------------
namespace Pal3.Game.Script
{
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Command;
using Command.Extensions;
using Core.Command;
using Core.Command.SceCommands;
using Core.Contract.Constants;
using Core.DataReader.Scn;
using Core.Utilities;
using Engine.Logging;
using Engine.Services;
using GameSystems.Dialogue;
using GameSystems.Favor;
using GameSystems.MiniGames;
using Scene;
using State;
public sealed class UserVariableManager : IDisposable, IUserVariableStore<ushort, int>,
ICommandExecutor<ScriptVarSetValueCommand>,
ICommandExecutor<ScriptVarSetRandomValueCommand>,
ICommandExecutor<ScriptVarAddValueCommand>,
ICommandExecutor<ScriptVarDistractAnotherVarCommand>,
ICommandExecutor<ScriptVarSetDialogueSelectionResultCommand>,
ICommandExecutor<ScriptVarSetLimitTimeDialogueSelectionResultCommand>,
ICommandExecutor<ScriptVarSetMazeSwitchStatusCommand>,
ICommandExecutor<ScriptVarSetMoneyCommand>,
ICommandExecutor<ScriptVarSetActorFavorCommand>,
ICommandExecutor<ScriptVarSetMostFavorableActorIdCommand>,
ICommandExecutor<ScriptVarSetLeastFavorableActorIdCommand>,
ICommandExecutor<ScriptVarSetCombatResultCommand>,
#if PAL3
ICommandExecutor<ScriptVarSetAppraisalsResultCommand>,
#elif PAL3A
ICommandExecutor<ScriptVarSetWheelOfTheFiveElementsUsageCountCommand>,
ICommandExecutor<ScriptVarSetObjectUsageStatusCommand>,
#endif
ICommandExecutor<ResetGameStateCommand>
{
private const int DEFAULT_VARIABLE_VALUE = 0;
private readonly Dictionary<ushort, int> _variables = new ();
private IUserVariableStore<ushort, int> _scriptVariables;
public UserVariableManager()
{
CommandExecutorRegistry<ICommand>.Instance.Register(this);
}
public void Dispose()
{
CommandExecutorRegistry<ICommand>.Instance.UnRegister(this);
}
public void Set(ushort variable, int value)
{
if ((variable & ScriptConstants.GlobalVariableFlag) != 0) _variables[variable] = value;
else if (_scriptVariables != null) _scriptVariables.Set(variable, value);
else throw new InvalidOperationException("Local variables require an executing script instance");
}
public int Get(ushort variable)
{
if ((variable & ScriptConstants.GlobalVariableFlag) != 0)
return _variables.GetValueOrDefault(variable, DEFAULT_VARIABLE_VALUE);
return _scriptVariables?.Get(variable) ??
throw new InvalidOperationException("Local variables require an executing script instance");
}
internal void ExecuteInScope(IUserVariableStore<ushort, int> variables,
ICommand command, Action<ICommand> execute)
{
var previous = _scriptVariables;
_scriptVariables = variables;
try { execute(command); }
finally { _scriptVariables = previous; }
}
public IEnumerator<KeyValuePair<ushort, int>> GetEnumerator()
{
return _variables.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return _variables.GetEnumerator();
}
public void Execute(ScriptVarSetValueCommand command)
{
EngineLogger.LogWarning($"Set var {command.Variable} with value: {command.Value}");
Set(command.Variable, command.Value);
}
public void Execute(ScriptVarSetRandomValueCommand command)
{
Set(command.Variable, RandomGenerator.Range(0, command.MaxExclusiveValue));
}
public void Execute(ScriptVarAddValueCommand command)
{
Set(command.Variable, Get(command.Variable) + command.Value);
}
public void Execute(ScriptVarDistractAnotherVarCommand command)
{
int result = Get(command.VariableA) - Get(command.VariableB);
Set(command.VariableA, result >= 0 ? result : -result);
}
#if PAL3
// TODO: Impl
public void Execute(ScriptVarSetAppraisalsResultCommand command)
{
bool result = ServiceLocator.Instance.Get<AppraisalsMiniGame>().GetResult();
Set(command.Variable, result ? 1: 0);
}
#endif
public void Execute(ScriptVarSetDialogueSelectionResultCommand command)
{
int selection = ServiceLocator.Instance.Get<DialogueManager>().GetDialogueSelectionButtonIndex();
Set(command.Variable, selection);
}
public void Execute(ScriptVarSetLimitTimeDialogueSelectionResultCommand command)
{
DialogueManager dialogueManager = ServiceLocator.Instance.Get<DialogueManager>();
bool playerReactedInTime = dialogueManager.PlayerReactedInTimeForLimitTimeDialogue();
Set(command.Variable, playerReactedInTime ? 1 : 0);
}
public void Execute(ScriptVarSetMazeSwitchStatusCommand command)
{
string currentCity = ServiceLocator.Instance.Get<SceneManager>().GetCurrentScene().GetSceneInfo().CityName;
if (ServiceLocator.Instance.Get<SceneStateManager>()
.TryGetSceneObjectStateOverride(currentCity,
command.SceneName,
command.ObjectId,
out SceneObjectStateOverride state) && state.SwitchState.HasValue)
{
Set(command.Variable, state.SwitchState.Value == 1 ? 1 : 0);
}
else Set(command.Variable, 0); // Default to off
}
public void Execute(ScriptVarSetMoneyCommand command)
{
// TODO: Remove this and uncomment the following line
int totalMoney = 777777;
// var totalMoney = ServiceLocator.Instance.Get<InventoryManager>().GetTotalMoney();
Set(command.Variable, totalMoney);
}
public void Execute(ScriptVarSetActorFavorCommand command)
{
int favor = ServiceLocator.Instance.Get<FavorManager>().GetFavorByActor(command.ActorId);
Set(command.Variable, favor);
}
public void Execute(ScriptVarSetMostFavorableActorIdCommand command)
{
int mostFavorableActorId = ServiceLocator.Instance.Get<FavorManager>().GetMostFavorableActorId();
Set(command.Variable, mostFavorableActorId);
}
public void Execute(ScriptVarSetLeastFavorableActorIdCommand command)
{
int leastFavorableActorId = ServiceLocator.Instance.Get<FavorManager>().GetLeastFavorableActorId();
Set(command.Variable, leastFavorableActorId);
}
#if PAL3A
// TODO: Impl
public void Execute(ScriptVarSetWheelOfTheFiveElementsUsageCountCommand command)
{
float rand = RandomGenerator.Range(0f, 1f);
int usageCount = rand > 0.35f ? 360 : 0;
Set(command.Variable, usageCount);
}
public void Execute(ScriptVarSetObjectUsageStatusCommand command)
{
GameScene scene = ServiceLocator.Instance.Get<SceneManager>().GetCurrentScene();
ScnSceneInfo sceneInfo = scene.GetSceneInfo();
SceneStateManager sceneStateManager = ServiceLocator.Instance.Get<SceneStateManager>();
if (sceneStateManager.TryGetSceneObjectStateOverride(sceneInfo.CityName,
command.SceneName,
command.ObjectId,
out SceneObjectStateOverride state) && state.TimesCount.HasValue)
{
// Set variable to 1 if it's fully used, otherwise 0
Set(command.Variable, state.TimesCount.Value == 0 ? 1 : 0);
}
else Set(command.Variable, 0); // Default to not used
}
#endif
// TODO: Impl
public void Execute(ScriptVarSetCombatResultCommand command)
{
bool won = RandomGenerator.Range(0f, 1f) > 0.35f;
#if PAL3
Pal3.Instance.Execute(won
? new UIDisplayNoteCommand("你战胜了重楼")
: new UIDisplayNoteCommand("你输给了重楼"));
#elif PAL3A
Pal3.Instance.Execute(won
? new UIDisplayNoteCommand("你战胜了景小楼")
: new UIDisplayNoteCommand("你输给了景小楼"));
#endif
Set(command.Variable, won ? 1 : 0);
}
public void Execute(ResetGameStateCommand command)
{
_variables.Clear();
}
}
}