Skip to content

Commit

Permalink
implement simulator loop
Browse files Browse the repository at this point in the history
  • Loading branch information
LazyDuchess committed Jun 28, 2024
1 parent 11b4521 commit aa50501
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 5 deletions.
38 changes: 38 additions & 0 deletions Assets/Editor/OpenTS2/SimulatorEditor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using OpenTS2.Game;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEditor;
using UnityEngine;

namespace OpenTS2
{
[CustomEditor(typeof(Simulator))]
public class SimulatorEditor : Editor
{
public override void OnInspectorGUI()
{
DrawDefaultInspector();
var simulator = target as Simulator;
var vm = simulator.VirtualMachine;
if (vm == null) return;
var entities = vm.Entities;
if (entities == null) return;
GUILayout.Label("Entities");
GUILayout.BeginVertical("box");
foreach(var entity in entities)
{
GUILayout.BeginVertical("box");
GUILayout.Label($"{entity.ID} - {entity.ObjectDefinition.FileName}");
GUILayout.EndVertical();
if (GUILayout.Button("Kill"))
{
entity.Delete();
}
}
GUILayout.EndVertical();
}
}
}
11 changes: 11 additions & 0 deletions Assets/Editor/OpenTS2/SimulatorEditor.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 36 additions & 4 deletions Assets/Scripts/OpenTS2/Game/Simulator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public class Simulator : MonoBehaviour
public VM VirtualMachine => _virtualMachine;
private VM _virtualMachine;
public Context SimulationContext = Context.Neighborhood;
public bool DeleteEntityOnError = true;
public enum Context
{
Lot = 1,
Expand All @@ -25,18 +26,29 @@ public enum Context
/// Number of ticks to run per second.
/// </summary>
public int TickRate = 20;
private float _timer = 0f;

private void Awake()
{
Instance = this;
_virtualMachine = new VM();
_virtualMachine.ExceptionHandler += HandleException;
}

private void Start()
{
CreateGlobalObjects();
}

private void Update()
{
var rateSecs = (float)1 / TickRate;
_timer += Time.deltaTime;
var timesToTick = Mathf.FloorToInt(_timer / rateSecs);
_virtualMachine.Tick();
_timer -= timesToTick * rateSecs;
}

private void CreateGlobalObjects()
{
var objects = ObjectManager.Instance.Objects;
Expand All @@ -53,20 +65,26 @@ private void CreateGlobalObjects()
public VMEntity CreateObject(ObjectDefinitionAsset objectDefinition)
{
var entity = new VMEntity(objectDefinition);
_virtualMachine.AddEntity(entity);

try
{
_virtualMachine.AddEntity(entity);

UpdateObjectData(entity);

var initFunction = entity.ObjectDefinition.Functions.GetFunction(ObjectFunctionsAsset.FunctionNames.Init);

if (initFunction.ActionTree != 0)
entity.RunTreeImmediately(initFunction.ActionTree);

var mainFunction = entity.ObjectDefinition.Functions.GetFunction(ObjectFunctionsAsset.FunctionNames.Main);

if (mainFunction.ActionTree != 0) {
entity.PushTreeToThread(entity.MainThread, mainFunction.ActionTree);
}
}
catch(SimAnticsException e)
catch(Exception e)
{
HandleSimAnticsException(e);
HandleException(e, entity);
}
return entity;
}
Expand All @@ -77,9 +95,23 @@ void UpdateObjectData(VMEntity entity)
entity.SetObjectData(VMObjectData.ObjectID, entity.ID);
}

public void HandleException(Exception exception, VMEntity entity)
{
if (exception is SimAnticsException)
{
HandleSimAnticsException(exception as SimAnticsException);
return;
}
Debug.LogError($"Non-SimAntics exception caused by entity {entity.ID} - {entity.ObjectDefinition.FileName}\n{exception}");
if (DeleteEntityOnError)
entity.Delete();
}

public void HandleSimAnticsException(SimAnticsException exception)
{
Debug.LogError(exception.ToString());
if (DeleteEntityOnError)
exception.StackFrame.Thread.Entity.Delete();
}

public void Kill()
Expand Down
11 changes: 10 additions & 1 deletion Assets/Scripts/OpenTS2/SimAntics/VM.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using OpenTS2.Common;
using OpenTS2.Content;
using OpenTS2.Files.Formats.DBPF;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
Expand All @@ -18,6 +19,7 @@ public class VM
public VMScheduler Scheduler = new VMScheduler();
public List<VMEntity> Entities = new List<VMEntity>();
public uint CurrentTick = 0;
public Action<Exception, VMEntity> ExceptionHandler;

private Dictionary<short, VMEntity> _entitiesByID = new Dictionary<short, VMEntity>();

Expand Down Expand Up @@ -66,7 +68,14 @@ public void Tick()
Scheduler.OnBeginTick(this);
foreach(var entity in Entities)
{
entity.Tick();
try
{
entity.Tick();
}
catch(Exception e)
{
ExceptionHandler?.Invoke(e, entity);
}
}
Scheduler.OnEndTick(this);
CurrentTick++;
Expand Down
7 changes: 7 additions & 0 deletions Assets/Scripts/OpenTS2/SimAntics/VMEntity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,13 @@ public BHAVAsset GetBHAV(ushort treeID)
return VM.GetBHAV(treeID, groupid);
}

public void PushTreeToThread(VMThread thread, ushort treeId)
{
var bhav = GetBHAV(treeId);
var stackFrame = new VMStackFrame(bhav, thread);
thread.Frames.Push(stackFrame);
}

public VMExitCode RunTreeImmediately(ushort treeID)
{
var thread = new VMThread(this);
Expand Down

0 comments on commit aa50501

Please sign in to comment.