Files
Pal3.Unity/Assets/Scripts/Pal3.Game/Scene/SceneBase.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

203 lines
7.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.Scene
{
using System;
using System.Collections.Generic;
using Actor;
using Core.Contract.Constants;
using Core.DataReader.Cpk;
using Core.DataReader.Cvd;
using Core.DataReader.Lgt;
using Core.DataReader.Nav;
using Core.DataReader.Pol;
using Core.DataReader.Scn;
using Core.Utilities;
using Data;
using Engine.Core.Abstraction;
using Engine.Core.Implementation;
using Engine.Logging;
using SceneObjects;
using State;
/// <summary>
/// Scene base class used to init and hold scene data models.
/// </summary>
public abstract class SceneBase : GameEntityScript
{
private const float ACTOR_MOVEMENT_MAX_Y_DIFFERENTIAL_DEFAULT = 2.2f;
private const float ACTOR_MOVEMENT_MAX_Y_DIFFERENTIAL_CROSS_LAYER_DEFAULT = 2f;
private const float ACTOR_MOVEMENT_MAX_Y_DIFFERENTIAL_CROSS_PLATFORM_DEFAULT = 2f;
protected float ActorMovementMaxYDifferential;
protected float ActorMovementMaxYDifferentialCrossLayer;
protected float ActorMovementMaxYDifferentialCrossPlatform;
protected Tilemap Tilemap;
protected ScnFile ScnFile;
protected NavFile NavFile;
//protected LgtFile LgtFile;
protected (PolFile PolFile, ITextureResourceProvider TextureProvider) ScenePolyMesh;
protected (CvdFile CvdFile, ITextureResourceProvider TextureProvider)? SceneCvdMesh;
protected readonly Dictionary<int, SceneObject> SceneObjects = new ();
protected readonly Dictionary<int, GameActor> Actors = new ();
private HashSet<int> _sceneObjectIdsToNotLoadFromSaveState;
private GameResourceProvider _resourceProvider;
private ISceneObjectFactory _sceneObjectFactory;
private SceneStateManager _sceneStateManager;
protected void Init(GameResourceProvider resourceProvider,
ScnFile scnFile,
ISceneObjectFactory sceneObjectFactory,
SceneStateManager sceneStateManager,
HashSet<int> sceneObjectIdsToNotLoadFromSaveState)
{
_resourceProvider = resourceProvider;
_sceneObjectFactory = sceneObjectFactory;
_sceneStateManager = sceneStateManager;
_sceneObjectIdsToNotLoadFromSaveState = sceneObjectIdsToNotLoadFromSaveState;
ScnFile = scnFile;
InitActorMovementMaxYDifferentialValues();
InitMeshData();
InitNavData();
InitSceneObjectData();
InitActorData();
}
private void InitActorMovementMaxYDifferentialValues()
{
ActorMovementMaxYDifferential = ACTOR_MOVEMENT_MAX_Y_DIFFERENTIAL_DEFAULT;
ActorMovementMaxYDifferentialCrossLayer = ACTOR_MOVEMENT_MAX_Y_DIFFERENTIAL_CROSS_LAYER_DEFAULT;
ActorMovementMaxYDifferentialCrossPlatform = ACTOR_MOVEMENT_MAX_Y_DIFFERENTIAL_CROSS_PLATFORM_DEFAULT;
#if PAL3 // Make sure actor cannot walk out of the trap holes in m10_2
if (ScnFile.SceneInfo.Is("m10", "2"))
{
ActorMovementMaxYDifferentialCrossLayer = 0.2f;
}
#endif
}
protected bool IsNightScene()
{
#if PAL3
if (ScnFile.SceneInfo.LightMap == 1)
#elif PAL3A
if (ScnFile.SceneInfo.LightMap == 1 ||
ScnFile.SceneInfo.SceneName.EndsWith("y", StringComparison.OrdinalIgnoreCase) ||
ScnFile.SceneInfo.IsCity("q11")) // 蜀山深夜场景
#endif
{
return true;
}
else
{
return false;
}
}
private void InitMeshData()
{
char separator = CpkConstants.DirectorySeparatorChar;
string meshFileRelativePath = $"{ScnFile.SceneInfo.CityName}{CpkConstants.FileExtension}{separator}" +
$"{ScnFile.SceneInfo.Model}{separator}";
// Switch to night version of the mesh model when LightMap flag is set to 1
if (ScnFile.SceneInfo.LightMap == 1)
{
meshFileRelativePath += $"1{separator}";
}
string sceneMetadataFilePrefix = meshFileRelativePath + ScnFile.SceneInfo.Model;
ITextureResourceProvider sceneTextureProvider = _resourceProvider.CreateTextureResourceProvider(
CoreUtility.GetDirectoryName(sceneMetadataFilePrefix, separator));
PolFile polFile = _resourceProvider.GetGameResourceFile<PolFile>(sceneMetadataFilePrefix + ".pol");
ScenePolyMesh = (polFile, sceneTextureProvider);
// Only few of the scenes use CVD models, so we need to check first
if (_resourceProvider.FileExists(sceneMetadataFilePrefix + ".cvd"))
{
CvdFile cvdFile = _resourceProvider.GetGameResourceFile<CvdFile>(sceneMetadataFilePrefix + ".cvd");
SceneCvdMesh = (cvdFile, sceneTextureProvider);
}
// The light file in the original game data does not have good enough
// information to be used for lighting, so we are not using it for now.
// if (_resourceProvider.FileExists(sceneMetadataFilePrefix + ".lgt"))
// {
// LgtFile = _resourceProvider.GetGameResourceFile<LgtFile>(sceneMetadataFilePrefix + ".lgt");
// }
}
private void InitNavData()
{
NavFile = _resourceProvider.GetGameResourceFile<NavFile>(
FileConstants.GetNavFileVirtualPath(ScnFile.SceneInfo.CityName, ScnFile.SceneInfo.Model));
Tilemap = new Tilemap(NavFile);
}
private void InitSceneObjectData()
{
foreach (ScnObjectInfo originalObjectInfo in ScnFile.ObjectInfos)
{
ScnObjectInfo objectInfo = originalObjectInfo;
// Load scene object state from state manager if it exists
if (!_sceneObjectIdsToNotLoadFromSaveState.Contains(objectInfo.Id) &&
_sceneStateManager.TryGetSceneObjectStateOverride(ScnFile.SceneInfo.CityName,
ScnFile.SceneInfo.SceneName, objectInfo.Id, out SceneObjectStateOverride state))
{
objectInfo = state.ApplyOverrides(objectInfo);
}
if (_sceneObjectFactory.Create(objectInfo, ScnFile.SceneInfo) is { } sceneObject)
{
SceneObjects[objectInfo.Id] = sceneObject;
}
else
{
EngineLogger.LogError($"Scene object type: {objectInfo.Type} is not " +
$"supported or implemented.");
}
}
}
private void InitActorData()
{
foreach (ScnNpcInfo npcInfo in ScnFile.NpcInfos)
{
Actors[npcInfo.Id] = new GameActor(_resourceProvider, npcInfo);
}
foreach (ScnNpcInfo playerInfo in NpcInfoFactory.CreateAllPlayerActorNpcInfos())
{
if (!Actors.ContainsKey(playerInfo.Id))
{
Actors[playerInfo.Id] = new GameActor(_resourceProvider, playerInfo);
}
}
#if PAL3A
foreach (ScnNpcInfo fengYaSongInfo in NpcInfoFactory.CreateAllFengYaSongNpcInfos())
{
if (!Actors.ContainsKey(fengYaSongInfo.Id))
{
Actors[fengYaSongInfo.Id] = new GameActor(_resourceProvider, fengYaSongInfo);
}
}
#endif
}
}
}