Skip to content

Commit

Permalink
Load BHAV from the actual entity that pushed it onto the stack
Browse files Browse the repository at this point in the history
  • Loading branch information
ammaraskar committed Oct 18, 2024
1 parent e91e799 commit 3086b58
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 8 deletions.
1 change: 1 addition & 0 deletions Assets/Scripts/OpenTS2/Content/DBPF/SimsObjectAsset.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public struct SimsObjectStackFrame
{
public ushort ObjectId;
public ushort TreeId;
public ushort BhavSaveType;

public short[] Params;
public short[] Locals;
Expand Down
35 changes: 32 additions & 3 deletions Assets/Scripts/OpenTS2/Engine/Tests/LotObjectSimulationTest.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using OpenTS2.Common;
using OpenTS2.Content;
using OpenTS2.Content.DBPF;
Expand Down Expand Up @@ -58,6 +59,21 @@ private void LoadLot(string neighborhoodPrefix, int id)
lotPackage.GetAssetByTGI<ObjectSaveTypeTableAsset>(new ResourceKey(instanceID: 0, GroupIDs.Local,
TypeIDs.OBJ_SAVE_TYPE_TABLE));

var saveTypeToGuid = new Dictionary<int, uint>();
for (var index = 0; index < saveTable.Selectors.Count; index++)
{
var selector = saveTable.Selectors[index];
var def = ObjectManager.Instance.GetObjectByGUID(selector.objectGuid);
if (def == null)
{
continue;
}

saveTypeToGuid[selector.saveType] = selector.objectGuid;

Debug.Log($"{index}: saveType: {selector.saveType} resource name: {selector.catalogResourceName}, Obj name: {def.FileName}");
}

var objectToLoad = saveTable.Selectors[ItemIndex];
Debug.Log($"Loading object {objectToLoad.catalogResourceName} with guid {objectToLoad.objectGuid:X}");

Expand All @@ -80,10 +96,23 @@ private void LoadLot(string neighborhoodPrefix, int id)

foreach (var frame in objectState.StackFrames)
{
var vmFrame = new VMStackFrame(entity.GetBHAV(frame.TreeId), entity.MainThread);
vmFrame.Arguments = frame.Params;
vmFrame.Locals = frame.Locals;
Debug.Log("Frame -----");
Debug.Log($" TreeId: 0x{frame.TreeId:X}, bhavSaveType: {frame.BhavSaveType}");

var bhavObjDef = ObjectManager.Instance.GetObjectByGUID(saveTypeToGuid[frame.BhavSaveType]);
// TODO: add a static method to do this or something. We don't want to make a VMEntity instance just
// to get the BHAV.
var bhav = new VMEntity(bhavObjDef).GetBHAV(frame.TreeId);

var vmFrame = new VMStackFrame(bhav, entity.MainThread)
{
Arguments = frame.Params,
Locals = frame.Locals
};
entity.MainThread.Frames.Push(vmFrame);

Debug.Log($" BHAV TGI: {entity.MainThread.Frames.Peek().BHAV.GlobalTGI}");
Debug.Log($" params: ({string.Join(", ", vmFrame.Arguments)})");
}

vm.Tick();
Expand Down
5 changes: 1 addition & 4 deletions Assets/Scripts/OpenTS2/Files/Formats/DBPF/SimsObjectCodec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,12 @@ private static SimsObjectAsset DeserializeWithVersion(IoBuffer reader, int versi
semiAttrs[i] = reader.ReadInt16();
}

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

// 8 unknown shorts called "data".
var dataArray = new short[8];
for (var i = 0; i < dataArray.Length; i++)
{
dataArray[i] = reader.ReadInt16();
}
Debug.Log($"dataArray: [{string.Join(", ", dataArray)}]");

// Next is a number of shorts that depends on the exact version of the file.
uint numShorts = version switch
Expand Down Expand Up @@ -104,7 +101,6 @@ private static SimsObjectAsset DeserializeWithVersion(IoBuffer reader, int versi
}
Debug.Log($"InventoryToken(tokenGUID={tokenGUID}, tokenFlags={tokenFlags}, numTokenProps={numTokenProperties})");


// Next is the number of object arrays. Each being a short array itself.
var numObjectArrays = reader.ReadInt16();
var shortArrays = new List<short[]>(numObjectArrays);
Expand Down Expand Up @@ -184,6 +180,7 @@ private static SimsObjectAsset DeserializeWithVersion(IoBuffer reader, int versi
{
ObjectId = objectID,
TreeId = treeID,
BhavSaveType = behavSaveType,
Locals = locals,
Params = frameParams
};
Expand Down
3 changes: 2 additions & 1 deletion Assets/Scripts/OpenTS2/SimAntics/VMEntity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using OpenTS2.Lua.Disassembly.OpCodes;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
Expand Down Expand Up @@ -33,7 +34,7 @@ public uint SemiGlobalGroupID
{
var semiGlobal = ObjectDefinition.SemiGlobal;
if (semiGlobal == null)
return 0;
throw new FileNotFoundException($"Object {ObjectDefinition.FileName} ({ObjectDefinition.GlobalTGI}) has no semi-global file");
return semiGlobal.SemiGlobalGroupID;
}
}
Expand Down

0 comments on commit 3086b58

Please sign in to comment.