Files
Pal3.Unity/Assets/Scripts/Pal3.Game/Actor/Controllers/ActorController.cs
Jiaqi Liu d1b2c05eb4 Optimize resource loading and fix PAL3A script execution (#92)
* Optimize DDS texture creation and static POL meshes

Separate file decoding from Unity upload through format-neutral texture data
and explicit world, sprite and skybox policies. Validate DDS layouts, preserve
alpha coverage and use native BC uploads with software RGBA fallback.

Compact referenced POL vertices and batch nearby static opaque groups sharing
materials. Keep water and dynamic geometry separate, with source ranges retained
only in the Editor. Add DDS layout and POL index regression tests.

Validation: PAL3 and PAL3A each passed 24 Editor tests, eight combined Game and
ResourceViewer style regressions, 936 paired skybox comparisons, and both macOS
Player builds on Metal. Editor provenance and Core tests are excluded from Players.

* Add core protocol regression tests and copyright checks

Cover SCE command contracts, binary readers, resource layouts and full local CPK decompression for PAL3 and PAL3A. Fix CVD time rebasing, binary read bounds and short reads, and CPK/NAV validation.

Standardize first-party copyright headers, preserve correct creation years, and update console source generation. Validate both variants in Unity and verify all live CPK entries.

* 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.

* Run CI only for pull requests and upgrade build actions
2026-11-13 09:28:31 +08:00

277 lines
10 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.Actor.Controllers
{
using System;
using System.Collections;
using Command;
using Command.Extensions;
using Core.Command;
using Core.Command.SceCommands;
using Core.Contract.Constants;
using Core.Contract.Enums;
using Engine.Animation;
using Engine.Core.Abstraction;
using Engine.Core.Implementation;
using Engine.Extensions;
using Engine.Navigation;
using Engine.Services;
using Input;
using GamePlay;
using Script.Waiter;
using Quaternion = UnityEngine.Quaternion;
using Vector3 = UnityEngine.Vector3;
public sealed class ActorController : GameEntityScript,
ICommandExecutor<ActorSetFacingCommand>,
ICommandExecutor<ActorSetFacingDirectionCommand>,
ICommandExecutor<ActorRotateFacingCommand>,
ICommandExecutor<ActorRotateFacingDirectionCommand>,
#if PAL3A
ICommandExecutor<ActorSetNoTurnCommand>,
#endif
ICommandExecutor<ActorSetScriptCommand>,
ICommandExecutor<ActorSetYPositionCommand>,
ICommandExecutor<ActorChangeScaleCommand>
{
private GameActor _actor;
private ActorActionController _actionController;
private ActorMovementController _movementController;
private PlayerInputActions _inputActions;
public bool IsActive
{
get => _actor.IsActive;
set
{
if (value) Activate();
else DeActivate();
_actor.IsActive = value;
}
}
private ActorBehaviourType _currentBehaviour;
protected override void OnEnableGameEntity()
{
CommandExecutorRegistry<ICommand>.Instance.Register(this);
}
protected override void OnDisableGameEntity()
{
CommandExecutorRegistry<ICommand>.Instance.UnRegister(this);
}
public void Init(GameActor actor,
ActorActionController actionController,
ActorMovementController movementController)
{
_actor = actor;
_actionController = actionController;
_movementController = movementController;
// Init facing direction
Transform.Rotation = Quaternion.Euler(0, -_actor.Info.FacingDirection, 0);
// Activate if InitActive == 1
if (_actor.Info.InitActive == 1) IsActive = true;
}
public ActorBehaviourType GetCurrentBehaviour()
{
return _currentBehaviour;
}
#if PAL3A
public void Execute(ActorSetNoTurnCommand command)
{
if (_actor.Id == command.ActorId) _actor.SetNoTurn(command.NoTurn);
}
#endif
public bool IsDirectlyInteractable(float distance)
{
if (distance > _actor.GetInteractionMaxDistance()) return false;
return _actor.GetScriptId() != ScriptConstants.InvalidScriptId;
}
public GameActor GetActor()
{
return _actor;
}
private void Activate()
{
#if PAL3A
// Reset NanGongHuang actor name
if (_actor.Id == (int)PlayerActorId.NanGongHuang &&
!string.Equals(_actor.Name, ActorConstants.NanGongHuangHumanModeActorName))
{
_actor.ChangeName(ActorConstants.NanGongHuangHumanModeActorName);
}
#endif
if (_actor.Info.Type == ActorType.MainActor)
{
if (string.IsNullOrEmpty(_actionController.GetCurrentAction()))
{
_actionController.PerformAction(_actor.GetIdleAction());
}
return;
}
// Perform init action
if (!_actor.IsActive)
{
switch (_actor.Info.InitBehaviour)
{
case ActorBehaviourType.None:
_currentBehaviour = ActorBehaviourType.None;
_actionController.PerformAction(_actor.GetIdleAction());
break;
case ActorBehaviourType.Hold:
_currentBehaviour = ActorBehaviourType.Hold;
_actionController.PerformAction(_actor.GetInitAction());
if (_actor.Info.LoopAction == 0)
{
_actionController.PauseAnimation(); // Hold at the first frame
}
break;
case ActorBehaviourType.Wander:
_currentBehaviour = ActorBehaviourType.Wander;
_actionController.PerformAction(_actor.GetIdleAction());
break;
case ActorBehaviourType.PathFollow:
_currentBehaviour = ActorBehaviourType.PathFollow;
_actionController.PerformAction(_actor.GetIdleAction());
break;
}
}
if (_currentBehaviour == ActorBehaviourType.PathFollow &&
_actor.Info.Path.NumberOfWaypoints > 0)
{
Vector3[] waypoints = new Vector3[_actor.Info.Path.NumberOfWaypoints];
for (int i = 0; i < _actor.Info.Path.NumberOfWaypoints; i++)
{
waypoints[i] = _actor.Info.Path.GameBoxWaypoints[i].ToUnityPosition();
}
_movementController.SetupPath(waypoints, 0, EndOfPathActionType.WaitAndReverse, ignoreObstacle: true);
}
}
private void DeActivate()
{
_actionController.DeActivate();
}
protected override void OnCollisionEnterGameEntity(IGameEntity otherGameEntity)
{
if (_actor.Info.Type == ActorType.CombatNpc &&
(_actor.Info.MonsterIds[0] != 0 || _actor.Info.MonsterIds[1] != 0 || _actor.Info.MonsterIds[2] != 0) &&
otherGameEntity.GetComponent<ActorController>() is {} actorController &&
ServiceLocator.Instance.Get<PlayerActorManager>()
.GetPlayerActorId() == actorController.GetActor().Id)
{
Pal3.Instance.Execute(new CombatActorCollideWithPlayerActorNotification(
_actor.Id, actorController.GetActor().Id));
}
}
private IEnumerator AnimateScaleAsync(float toScale, float duration, Action onFinished = null)
{
yield return CoreAnimation.EnumerateValueAsync(Transform.LocalScale.x,
toScale, duration, AnimationCurveType.Linear, value =>
{
Transform.LocalScale = new Vector3(value, value, value);
});
onFinished?.Invoke();
}
public void Execute(ActorSetFacingCommand command)
{
if (_actor.Id != command.ActorId) return;
Transform.Rotation = Quaternion.Euler(0, command.Degrees, 0);
}
public void Execute(ActorRotateFacingCommand command)
{
if (_actor.Id != command.ActorId) return;
#if PAL3
float currentYAngles = Transform.EulerAngles.y;
Transform.Rotation = Quaternion.Euler(0, currentYAngles - command.Degrees, 0);
#elif PAL3A
Transform.Rotation = Quaternion.Euler(0, - command.Degrees, 0);
#endif
}
public void Execute(ActorSetFacingDirectionCommand command)
{
if (_actor.Id != command.ActorId) return;
if (command.Direction is >= 0 and < 8)
{
Transform.Rotation = Quaternion.Euler(0, -command.Direction * 45, 0);
}
else
{
// Note: in the original game scripts, this command is sometimes misused to set the facing direction.
// Instead of calling ActorRotateFacingCommand, there are places where the script calls
// ActorSetFacingDirectionCommand. In this case, the direction parameter is actually the degrees
// to rotate. Thus we need to handle this case here.
int degrees = command.Direction;
Execute(new ActorRotateFacingCommand(command.ActorId, degrees));
}
}
public void Execute(ActorRotateFacingDirectionCommand command)
{
if (_actor.Id != command.ActorId) return;
if (command.Direction is >= 0 and < 8)
{
Transform.Rotation = Quaternion.Euler(0, -command.Direction * 45, 0);
}
else
{
// Note: in the original game scripts, this command is sometimes misused to set the facing direction.
// Instead of calling ActorRotateFacingCommand, there are places where the script calls
// ActorRotateFacingDirectionCommand. In this case, the direction parameter is actually the degrees
// to rotate. Thus we need to handle this case here.
int degrees = command.Direction;
Execute(new ActorRotateFacingCommand(command.ActorId, degrees));
}
}
public void Execute(ActorSetScriptCommand command)
{
if (command.ActorId != _actor.Id) return;
if (_actor.GetScriptId() != (uint)command.ScriptId)
{
_actor.ChangeScriptId((uint)command.ScriptId);
}
}
public void Execute(ActorChangeScaleCommand command)
{
if (command.ActorId != _actor.Id) return;
WaitUntilCanceled waiter = new();
Pal3.Instance.Execute(new ScriptRunnerAddWaiterRequest(waiter));
StartCoroutine(AnimateScaleAsync(command.Scale, 2f, () => waiter.CancelWait()));
}
public void Execute(ActorSetYPositionCommand command)
{
if (command.ActorId != _actor.Id) return;
Vector3 oldPosition = Transform.Position;
Transform.Position = new Vector3(oldPosition.x,
command.GameBoxYPosition.ToUnityYPosition(),
oldPosition.z);
}
}
}