From c1d99422e9dd57e96c5d8fe60a03fb4eac341dda Mon Sep 17 00:00:00 2001 From: Nahuel Rocchetti Date: Mon, 21 Aug 2023 17:56:58 -0300 Subject: [PATCH] Implement data sources --- .../Primitives/VMExpressionPrimitive.cs | 43 ++++++++++++++++++- .../Scripts/OpenTS2/SimAntics/VMDataSource.cs | 2 +- .../OpenTS2/SimAntics/VMPrimitiveContext.cs | 26 +++++++++++ 3 files changed, 69 insertions(+), 2 deletions(-) diff --git a/Assets/Scripts/OpenTS2/SimAntics/Primitives/VMExpressionPrimitive.cs b/Assets/Scripts/OpenTS2/SimAntics/Primitives/VMExpressionPrimitive.cs index 887558f..d7999df 100644 --- a/Assets/Scripts/OpenTS2/SimAntics/Primitives/VMExpressionPrimitive.cs +++ b/Assets/Scripts/OpenTS2/SimAntics/Primitives/VMExpressionPrimitive.cs @@ -8,9 +8,50 @@ namespace OpenTS2.SimAntics.Primitives { public class VMExpressionPrimitive : VMPrimitive { + public enum Operator : byte + { + GreaterThan, + LessThan, + EqualTo, + Add, + Subtract, + Assign, + Multiply, + Divide, + IsFlagSet, + SetFlag, + ClearFlag, + AddThenLessThan, + Modulo, + And, + GreaterThanOrEqualTo, + LessThanOrEqualTo, + NotEqualTo, + SubtractThenGreaterThan, + Or, + Xor, + Abs, + Assign32BitValue + } public override VMReturnValue Execute(VMPrimitiveContext ctx) { - throw new NotImplementedException(); + var lhsData = ctx.Node.GetUInt16(0); + var rhsData = ctx.Node.GetUInt16(2); + var signedFlag = ctx.Node.Operands[4]; + var op = (Operator)ctx.Node.Operands[5]; + var lhsSource = (VMDataSource)ctx.Node.Operands[6]; + var rhsSource = (VMDataSource)ctx.Node.Operands[7]; + + switch(op) + { + case Operator.GreaterThan: + return ctx.GetData(lhsSource, lhsData) > ctx.GetData(rhsSource, rhsData) ? + VMReturnValue.ReturnTrue : VMReturnValue.ReturnFalse; + case Operator.LessThan: + return ctx.GetData(lhsSource, lhsData) < ctx.GetData(rhsSource, rhsData) ? + VMReturnValue.ReturnTrue : VMReturnValue.ReturnFalse; + } + return VMReturnValue.ReturnFalse; } } } diff --git a/Assets/Scripts/OpenTS2/SimAntics/VMDataSource.cs b/Assets/Scripts/OpenTS2/SimAntics/VMDataSource.cs index b5fe935..f8e44c8 100644 --- a/Assets/Scripts/OpenTS2/SimAntics/VMDataSource.cs +++ b/Assets/Scripts/OpenTS2/SimAntics/VMDataSource.cs @@ -6,7 +6,7 @@ using System.Threading.Tasks; namespace OpenTS2.SimAntics { - public enum VMDataSource : ushort + public enum VMDataSource : byte { MyObjectsAttributes, StackObjectsAttributes, diff --git a/Assets/Scripts/OpenTS2/SimAntics/VMPrimitiveContext.cs b/Assets/Scripts/OpenTS2/SimAntics/VMPrimitiveContext.cs index 529cabf..85c5b46 100644 --- a/Assets/Scripts/OpenTS2/SimAntics/VMPrimitiveContext.cs +++ b/Assets/Scripts/OpenTS2/SimAntics/VMPrimitiveContext.cs @@ -13,5 +13,31 @@ namespace OpenTS2.SimAntics public VMStack Stack => StackFrame.Stack; public VMEntity Entity => StackFrame.Stack.Entity; public VM VM => Entity.VM; + + // TODO - still super incomplete, just enough to run basic scripts. + public short GetData(VMDataSource source, ushort dataIndex) + { + return source switch + { + VMDataSource.Literal => (short)dataIndex, + VMDataSource.Temps => Entity.Temps[dataIndex], + VMDataSource.Params => StackFrame.Arguments[dataIndex], + _ => throw new ArgumentOutOfRangeException("SimAntics data source out of range!") + }; + } + + public void SetData(VMDataSource source, ushort dataIndex, short value) + { + switch(source) + { + case VMDataSource.Temps: + Entity.Temps[dataIndex] = value; + return; + case VMDataSource.Params: + StackFrame.Arguments[dataIndex] = value; + return; + } + throw new ArgumentOutOfRangeException("SimAntics data source out of range!"); + } } }