Skip to content

Commit

Permalink
Add data to SimsObjectAsset and start simulation ticking
Browse files Browse the repository at this point in the history
  • Loading branch information
ammaraskar committed Oct 14, 2024
1 parent 6c5f6d2 commit 1eef063
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 17 deletions.
35 changes: 35 additions & 0 deletions Assets/Scripts/OpenTS2/Content/DBPF/SimsObjectAsset.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
25 changes: 22 additions & 3 deletions Assets/Scripts/OpenTS2/Engine/Tests/LotObjectSimulationTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<SimsObjectAsset>(
var objectState = lotPackage.GetAssetByTGI<SimsObjectAsset>(
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();
}
}
}
38 changes: 24 additions & 14 deletions Assets/Scripts/OpenTS2/Files/Formats/DBPF/SimsObjectCodec.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -10,14 +11,13 @@
namespace OpenTS2.Files.Formats.DBPF
{
/// <summary>
/// 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.
/// </summary>
[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);

Expand All @@ -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);

Expand All @@ -46,26 +44,21 @@ 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];
for (var i = 0; i < numAttrs; i++)
{
attrs[i] = reader.ReadInt16();
}
Debug.Log($"numAttrs: {numAttrs}, attrs: [{string.Join(", ", attrs)}]");

var numSemiAttrs = reader.ReadInt16();
var semiAttrs = new short[numSemiAttrs];
for (var i = 0; i < numSemiAttrs; i++)
{
semiAttrs[i] = reader.ReadInt16();
}
Debug.Log($"numSemiAttrs: {numAttrs}, semiAttrs: [{string.Join(", ", semiAttrs)}]");

Debug.Log($" Data array offset: {reader.Position:X}");

Expand All @@ -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".
Expand Down Expand Up @@ -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++)
{
Expand Down Expand Up @@ -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}");
Expand Down Expand Up @@ -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);
}
}
}

0 comments on commit 1eef063

Please sign in to comment.