mirror of
https://github.com/0x7c13/Pal3.Unity.git
synced 2026-11-26 17:38:10 +08:00
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.
203 lines
7.9 KiB
C#
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
|
|
}
|
|
}
|
|
} |