From 1eef063e8c0b0d490b1d8ca0818bd6495edcff11 Mon Sep 17 00:00:00 2001 From: Ammar Askar Date: Mon, 14 Oct 2024 16:10:29 -0400 Subject: [PATCH] Add data to SimsObjectAsset and start simulation ticking --- .../OpenTS2/Content/DBPF/SimsObjectAsset.cs | 35 +++++++++++++++++ .../Engine/Tests/LotObjectSimulationTest.cs | 25 ++++++++++-- .../Files/Formats/DBPF/SimsObjectCodec.cs | 38 ++++++++++++------- 3 files changed, 81 insertions(+), 17 deletions(-) diff --git a/Assets/Scripts/OpenTS2/Content/DBPF/SimsObjectAsset.cs b/Assets/Scripts/OpenTS2/Content/DBPF/SimsObjectAsset.cs index ee876dd..cc52cf5 100644 --- a/Assets/Scripts/OpenTS2/Content/DBPF/SimsObjectAsset.cs +++ b/Assets/Scripts/OpenTS2/Content/DBPF/SimsObjectAsset.cs @@ -2,6 +2,41 @@ namespace OpenTS2.Content.DBPF { public class SimsObjectAsset : AbstractAsset { + public SimsObjectAsset(float tileLocationY, float tileLocationX, int level, float elevation, int objectGroupId, + short[] attrs, short[] semiAttrs, short[] temp, short[] data, SimsObjectStackFrame[] stackFrames) + { + TileLocationY = tileLocationY; + TileLocationX = tileLocationX; + Level = level; + Elevation = elevation; + ObjectGroupId = objectGroupId; + Attrs = attrs; + SemiAttrs = semiAttrs; + Temp = temp; + Data = data; + StackFrames = stackFrames; + } + public float TileLocationY { get; } + public float TileLocationX { get; } + public int Level { get; } + public float Elevation { get; } + public int ObjectGroupId { get; } + + public short[] Attrs { get; } + public short[] SemiAttrs { get; } + public short[] Temp { get; } + public short[] Data { get; } + + public SimsObjectStackFrame[] StackFrames { get; } + } + + public struct SimsObjectStackFrame + { + public ushort ObjectId; + public ushort TreeId; + + public short[] Params; + public short[] Locals; } } \ No newline at end of file diff --git a/Assets/Scripts/OpenTS2/Engine/Tests/LotObjectSimulationTest.cs b/Assets/Scripts/OpenTS2/Engine/Tests/LotObjectSimulationTest.cs index b7ff5ff..dc1adf1 100644 --- a/Assets/Scripts/OpenTS2/Engine/Tests/LotObjectSimulationTest.cs +++ b/Assets/Scripts/OpenTS2/Engine/Tests/LotObjectSimulationTest.cs @@ -6,6 +6,7 @@ using OpenTS2.Content.DBPF; using OpenTS2.Files; using OpenTS2.Files.Formats.DBPF; +using OpenTS2.SimAntics; using UnityEngine; namespace OpenTS2.Engine.Tests @@ -64,10 +65,28 @@ private void LoadLot(string neighborhoodPrefix, int id) Debug.Assert(objectDefinition != null, "Could not find objd."); // Now load the state of the object. - var objectState = - lotPackage.GetAssetByTGI( + var objectState = lotPackage.GetAssetByTGI( new ResourceKey(instanceID: (uint)objectToLoad.saveType, GroupIDs.Local, TypeIDs.XOBJ)); - Debug.Log($"object state: {objectState}"); + + // Create an entity for the object. + var vm = new VM(); + var entity = new VMEntity(objectDefinition) + { + Attributes = objectState.Attrs, + SemiAttributes = objectState.SemiAttrs, + Temps = objectState.Temp, + ObjectData = objectState.Data + }; + + foreach (var frame in objectState.StackFrames) + { + var vmFrame = new VMStackFrame(entity.GetBHAV(frame.TreeId), entity.MainThread); + vmFrame.Arguments = frame.Params; + vmFrame.Locals = frame.Locals; + entity.MainThread.Frames.Push(vmFrame); + } + + vm.Tick(); } } } diff --git a/Assets/Scripts/OpenTS2/Files/Formats/DBPF/SimsObjectCodec.cs b/Assets/Scripts/OpenTS2/Files/Formats/DBPF/SimsObjectCodec.cs index 71b8db8..24eec5e 100644 --- a/Assets/Scripts/OpenTS2/Files/Formats/DBPF/SimsObjectCodec.cs +++ b/Assets/Scripts/OpenTS2/Files/Formats/DBPF/SimsObjectCodec.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.IO; +using System.Linq; using OpenTS2.Common; using OpenTS2.Content; using OpenTS2.Content.DBPF; @@ -10,14 +11,13 @@ namespace OpenTS2.Files.Formats.DBPF { /// - /// Codec for what is known in game as cTSObject. Contains attributes and locations of objects. + /// Codec for what is known in game as cTSObject. Contains state and locations of objects. /// [Codec(TypeIDs.XOBJ)] public class SimsObjectCodec : AbstractCodec { public override AbstractAsset Deserialize(byte[] bytes, ResourceKey tgi, DBPFFile sourceFile) { - var asset = new SimsObjectAsset(); var stream = new MemoryStream(bytes); var reader = IoBuffer.FromStream(stream, ByteOrder.LITTLE_ENDIAN); @@ -28,8 +28,6 @@ public override AbstractAsset Deserialize(byte[] bytes, ResourceKey tgi, DBPFFil private static SimsObjectAsset DeserializeWithVersion(IoBuffer reader, int version) { - var asset = new SimsObjectAsset(); - // Skip first 64 bytes. reader.Seek(SeekOrigin.Begin, 64); @@ -46,10 +44,7 @@ private static SimsObjectAsset DeserializeWithVersion(IoBuffer reader, int versi reader.ReadUInt16(); var elevation = reader.ReadFloat(); var objectGroupId = reader.ReadInt32(); - var unknown = reader.ReadInt16(); - - Debug.Log($"tileLocationY: {tileLocationY}, tileLocationX: {tileLocationX}, level: {level}, " + - $"elevation: {elevation}, objectGroupId: {objectGroupId}, unknown: {unknown}"); + reader.ReadInt16(); // unknown var numAttrs = reader.ReadInt16(); var attrs = new short[numAttrs]; @@ -57,7 +52,6 @@ private static SimsObjectAsset DeserializeWithVersion(IoBuffer reader, int versi { attrs[i] = reader.ReadInt16(); } - Debug.Log($"numAttrs: {numAttrs}, attrs: [{string.Join(", ", attrs)}]"); var numSemiAttrs = reader.ReadInt16(); var semiAttrs = new short[numSemiAttrs]; @@ -65,7 +59,6 @@ private static SimsObjectAsset DeserializeWithVersion(IoBuffer reader, int versi { semiAttrs[i] = reader.ReadInt16(); } - Debug.Log($"numSemiAttrs: {numAttrs}, semiAttrs: [{string.Join(", ", semiAttrs)}]"); Debug.Log($" Data array offset: {reader.Position:X}"); @@ -83,9 +76,16 @@ private static SimsObjectAsset DeserializeWithVersion(IoBuffer reader, int versi 0xAD => 0x58, _ => throw new NotImplementedException($"SimObjectCodec not implemented for version {version:X}"), }; - for (var i = 0; i < numShorts; i++) + + var temp = new short[8]; + for (var i = 0; i < temp.Length; i++) { - reader.ReadInt16(); + temp[i] = reader.ReadInt16(); + } + var data = new short[numShorts - 8]; + for (var i = 0; i < data.Length; i++) + { + data[i] = reader.ReadInt16(); } // Another 8 shorts, called the "tempTokenFields". @@ -145,7 +145,7 @@ private static SimsObjectAsset DeserializeWithVersion(IoBuffer reader, int versi // Read the cTSTreeStack, a set of cTreeStackElems, probably the edith execution stack? var numStackFrames = reader.ReadInt32(); - Debug.Log($" numStackFrames: {numStackFrames}"); + var frames = new SimsObjectStackFrame[numStackFrames]; reader.ReadUInt32(); // unknown for (var i = 0; i < numStackFrames; i++) { @@ -180,6 +180,13 @@ private static SimsObjectAsset DeserializeWithVersion(IoBuffer reader, int versi Debug.Log($"- objectID: {objectID}, bhav: {behavSaveType}, runningObjID: {runningObjId}, runningOnObjID: {runningOnObjId}"); Debug.Log($" locals: {string.Join(", ", locals)}"); + frames[i] = new SimsObjectStackFrame + { + ObjectId = objectID, + TreeId = treeID, + Locals = locals, + Params = frameParams + }; } Debug.Log($"[P] Position before cTSRelationshipTable: 0x{reader.Position:X}"); @@ -284,7 +291,10 @@ private static SimsObjectAsset DeserializeWithVersion(IoBuffer reader, int versi Debug.Log($"{overrideString1} / {overrideString2} / {overrideString3}"); } - return asset; + return new SimsObjectAsset(tileLocationY: tileLocationY, tileLocationX: tileLocationX, level: level, + elevation: elevation, objectGroupId: objectGroupId, attrs: attrs, semiAttrs: semiAttrs, temp: temp, + data: data, + stackFrames: frames); } } } \ No newline at end of file