Skip to content

Commit

Permalink
New: Add animated instructions playerDistance and playerTrackDistance
Browse files Browse the repository at this point in the history
  • Loading branch information
leezer3 committed Apr 14, 2024
1 parent aa0e63e commit 0b940f9
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 1 deletion.
22 changes: 22 additions & 0 deletions source/ObjectViewer/FunctionScripts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,23 @@ internal static void ExecuteFunctionScript(FunctionScript Function, TrainBase Tr
Function.Stack[s - 1] = 0.0;
}
break;
case Instructions.PlayerTrainDistance:
double playerDist = double.MaxValue;
for (int j = 0; j < TrainManager.PlayerTrain.Cars.Length; j++)
{
double fx = TrainManager.PlayerTrain.Cars[j].FrontAxle.Follower.WorldPosition.X - Position.X;
double fy = TrainManager.PlayerTrain.Cars[j].FrontAxle.Follower.WorldPosition.Y - Position.Y;
double fz = TrainManager.PlayerTrain.Cars[j].FrontAxle.Follower.WorldPosition.Z - Position.Z;
double f = fx * fx + fy * fy + fz * fz;
if (f < playerDist) playerDist = f;
double rx = TrainManager.PlayerTrain.Cars[j].RearAxle.Follower.WorldPosition.X - Position.X;
double ry = TrainManager.PlayerTrain.Cars[j].RearAxle.Follower.WorldPosition.Y - Position.Y;
double rz = TrainManager.PlayerTrain.Cars[j].RearAxle.Follower.WorldPosition.Z - Position.Z;
double r = rx * rx + ry * ry + rz * rz;
if (r < playerDist) playerDist = r;
}
Function.Stack[s] = Math.Sqrt(playerDist);
s++; break;
case Instructions.TrainDistance:
if (Train != null) {
double dist = double.MaxValue;
Expand Down Expand Up @@ -445,6 +462,11 @@ internal static void ExecuteFunctionScript(FunctionScript Function, TrainBase Tr
Function.Stack[s - 1] = 0.0;
}
break;
case Instructions.PlayerTrackDistance:
double pt0 = TrainManager.PlayerTrain.FrontCarTrackPosition;
double pt1 = TrainManager.PlayerTrain.RearCarTrackPosition;
Function.Stack[s] = TrackPosition > pt0 ? TrackPosition - pt0 : TrackPosition < pt1 ? TrackPosition - pt1 : 0.0;
s++; break;
case Instructions.TrainTrackDistance:
if (Train != null) {
double t0 = Train.FrontCarTrackPosition;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,23 @@ internal static void ExecuteFunctionScript(FunctionScript Function, TrainBase Tr
Function.Stack[s - 1] = 0.0;
}
break;
case Instructions.PlayerTrainDistance:
double playerDist = double.MaxValue;
for (int j = 0; j < TrainManager.PlayerTrain.Cars.Length; j++)
{
double fx = TrainManager.PlayerTrain.Cars[j].FrontAxle.Follower.WorldPosition.X - Position.X;
double fy = TrainManager.PlayerTrain.Cars[j].FrontAxle.Follower.WorldPosition.Y - Position.Y;
double fz = TrainManager.PlayerTrain.Cars[j].FrontAxle.Follower.WorldPosition.Z - Position.Z;
double f = fx * fx + fy * fy + fz * fz;
if (f < playerDist) playerDist = f;
double rx = TrainManager.PlayerTrain.Cars[j].RearAxle.Follower.WorldPosition.X - Position.X;
double ry = TrainManager.PlayerTrain.Cars[j].RearAxle.Follower.WorldPosition.Y - Position.Y;
double rz = TrainManager.PlayerTrain.Cars[j].RearAxle.Follower.WorldPosition.Z - Position.Z;
double r = rx * rx + ry * ry + rz * rz;
if (r < playerDist) playerDist = r;
}
Function.Stack[s] = Math.Sqrt(playerDist);
s++; break;
case Instructions.TrainDistance:
if (Train != null) {
double dist = double.MaxValue;
Expand Down Expand Up @@ -443,6 +460,11 @@ internal static void ExecuteFunctionScript(FunctionScript Function, TrainBase Tr
Function.Stack[s - 1] = 0.0;
}
break;
case Instructions.PlayerTrackDistance:
double pt0 = TrainManager.PlayerTrain.FrontCarTrackPosition;
double pt1 = TrainManager.PlayerTrain.RearCarTrackPosition;
Function.Stack[s] = TrackPosition > pt0 ? TrackPosition - pt0 : TrackPosition < pt1 ? TrackPosition - pt1 : 0.0;
s++; break;
case Instructions.TrainTrackDistance:
if (Train != null) {
double t0 = Train.FrontCarTrackPosition;
Expand Down
8 changes: 8 additions & 0 deletions source/OpenBveApi/FunctionScripts/FunctionScript.cs
Original file line number Diff line number Diff line change
Expand Up @@ -472,11 +472,19 @@ public FunctionScript(HostInterface Host, string Expression, bool Infix)
if (n >= InstructionSet.Length) Array.Resize(ref InstructionSet, InstructionSet.Length << 1);
InstructionSet[n] = Instructions.TrainDistance;
n++; s++; if (s >= m) m = s; break;
case "playerdistance":
if (n >= InstructionSet.Length) Array.Resize(ref InstructionSet, InstructionSet.Length << 1);
InstructionSet[n] = Instructions.PlayerTrainDistance;
n++; s++; if (s >= m) m = s; break;
case "distanceindex":
if (s < 1) throw new InvalidOperationException(Arguments[i] + " requires at least 1 argument on the stack in function script " + Expression);
if (n >= InstructionSet.Length) Array.Resize(ref InstructionSet, InstructionSet.Length << 1);
InstructionSet[n] = Instructions.TrainDistanceToCar;
n++; break;
case "playertrackdistance":
if (n >= InstructionSet.Length) Array.Resize(ref InstructionSet, InstructionSet.Length << 1);
InstructionSet[n] = Instructions.TrainTrackDistance;
n++; s++; if (s >= m) m = s; break;
case "trackdistance":
if (n >= InstructionSet.Length) Array.Resize(ref InstructionSet, InstructionSet.Length << 1);
InstructionSet[n] = Instructions.TrainTrackDistance;
Expand Down
2 changes: 1 addition & 1 deletion source/OpenBveApi/FunctionScripts/Instructions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public enum Instructions {
TrainCars, TrainCarNumber, TrainDestination, PlayerTrain, TrainLength,
TrainSpeed, TrainSpeedometer, TrainAcceleration, TrainAccelerationMotor,
TrainSpeedOfCar, TrainSpeedometerOfCar, TrainAccelerationOfCar, TrainAccelerationMotorOfCar,
TrainDistance, TrainDistanceToCar, TrainTrackDistance, TrainTrackDistanceToCar, CurveRadius, CurveRadiusOfCar, FrontAxleCurveRadius, FrontAxleCurveRadiusOfCar, RearAxleCurveRadius, RearAxleCurveRadiusOfCar, CurveCant, CurveCantOfCar, Pitch, PitchOfCar, Odometer, OdometerOfCar, BrightnessOfCar,
PlayerTrainDistance, TrainDistance, TrainDistanceToCar, PlayerTrackDistance, TrainTrackDistance, TrainTrackDistanceToCar, CurveRadius, CurveRadiusOfCar, FrontAxleCurveRadius, FrontAxleCurveRadiusOfCar, RearAxleCurveRadius, RearAxleCurveRadiusOfCar, CurveCant, CurveCantOfCar, Pitch, PitchOfCar, Odometer, OdometerOfCar, BrightnessOfCar,
Doors, DoorsIndex,
LeftDoors, LeftDoorsIndex, RightDoors, RightDoorsIndex,
LeftDoorsTarget, LeftDoorsTargetIndex, RightDoorsTarget, RightDoorsTargetIndex,
Expand Down
22 changes: 22 additions & 0 deletions source/RouteViewer/FunctionScripts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,23 @@ internal static void ExecuteFunctionScript(FunctionScript Function, TrainManager
Function.Stack[s - 1] = 0.0;
}
break;
case Instructions.PlayerTrainDistance:
double playerDist = double.MaxValue;
for (int j = 0; j < TrainManager.PlayerTrain.Cars.Length; j++)
{
double fx = TrainManager.PlayerTrain.Cars[j].FrontAxle.Follower.WorldPosition.X - Position.X;
double fy = TrainManager.PlayerTrain.Cars[j].FrontAxle.Follower.WorldPosition.Y - Position.Y;
double fz = TrainManager.PlayerTrain.Cars[j].FrontAxle.Follower.WorldPosition.Z - Position.Z;
double f = fx * fx + fy * fy + fz * fz;
if (f < playerDist) playerDist = f;
double rx = TrainManager.PlayerTrain.Cars[j].RearAxle.Follower.WorldPosition.X - Position.X;
double ry = TrainManager.PlayerTrain.Cars[j].RearAxle.Follower.WorldPosition.Y - Position.Y;
double rz = TrainManager.PlayerTrain.Cars[j].RearAxle.Follower.WorldPosition.Z - Position.Z;
double r = rx * rx + ry * ry + rz * rz;
if (r < playerDist) playerDist = r;
}
Function.Stack[s] = Math.Sqrt(playerDist);
s++; break;
case Instructions.TrainDistance:
if (Train != null) {
double dist = double.MaxValue;
Expand Down Expand Up @@ -443,6 +460,11 @@ internal static void ExecuteFunctionScript(FunctionScript Function, TrainManager
Function.Stack[s - 1] = 0.0;
}
break;
case Instructions.PlayerTrackDistance:
double pt0 = TrainManager.PlayerTrain.FrontCarTrackPosition;
double pt1 = TrainManager.PlayerTrain.RearCarTrackPosition;
Function.Stack[s] = TrackPosition > pt0 ? TrackPosition - pt0 : TrackPosition < pt1 ? TrackPosition - pt1 : 0.0;
s++; break;
case Instructions.TrainTrackDistance:
if (Train != null) {
double t0 = Train.FrontCarTrackPosition;
Expand Down

0 comments on commit 0b940f9

Please sign in to comment.