Implement data sources

This commit is contained in:
Nahuel Rocchetti 2023-08-21 17:56:58 -03:00
parent 4daccae598
commit c1d99422e9
3 changed files with 69 additions and 2 deletions

View file

@ -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;
}
}
}

View file

@ -6,7 +6,7 @@ using System.Threading.Tasks;
namespace OpenTS2.SimAntics
{
public enum VMDataSource : ushort
public enum VMDataSource : byte
{
MyObjectsAttributes,
StackObjectsAttributes,

View file

@ -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!");
}
}
}