Skip to content

Commit

Permalink
1.1.13 - toggleScripts : added accuracy argument
Browse files Browse the repository at this point in the history
  • Loading branch information
BlazingTwist committed Oct 20, 2021
1 parent 65b3b9b commit ed063a9
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,31 +48,32 @@ public string Serialize() {
return result;
}

public bool ObjectMatches(GameObject gameObject, bool verbose) {
public bool ObjectMatches(GameObject gameObject, bool verbose, float? accuracy) {
if (!gameObject.name.Equals(name, StringComparison.InvariantCulture)) {
return false;
}
float maxDelta = accuracy ?? epsilon;
Vector3 objectPosition = gameObject.transform.position;
if (x != null) {
if (Math.Abs(x.Value - objectPosition.x) > epsilon) {
if (Math.Abs(x.Value - objectPosition.x) > maxDelta) {
if (verbose) {
BTConsole.WriteLine($"object passed name check: {gameObject.name} | {objectPosition} - but failed x check: expected={x.Value} != actual={objectPosition.x}");
BTConsole.WriteLine($"object passed name check: {gameObject.name} | {objectPosition} - but failed x check: expected={x.Value} != actual={objectPosition.x} | maxDelta={maxDelta}");
}
return false;
}
}
if (y != null) {
if (Math.Abs(y.Value - objectPosition.y) > epsilon) {
if (Math.Abs(y.Value - objectPosition.y) > maxDelta) {
if (verbose) {
BTConsole.WriteLine($"object passed name check: {gameObject.name} | {objectPosition} - but failed y check: expected={y.Value} != actual={objectPosition.y}");
BTConsole.WriteLine($"object passed name check: {gameObject.name} | {objectPosition} - but failed y check: expected={y.Value} != actual={objectPosition.y} | maxDelta={maxDelta}");
}
return false;
}
}
if (z != null) {
if (Math.Abs(z.Value - objectPosition.z) > epsilon) {
if (Math.Abs(z.Value - objectPosition.z) > maxDelta) {
if (verbose) {
BTConsole.WriteLine($"object passed name check: {gameObject.name} | {objectPosition} - but failed z check: expected={z.Value} != actual={objectPosition.z}");
BTConsole.WriteLine($"object passed name check: {gameObject.name} | {objectPosition} - but failed z check: expected={z.Value} != actual={objectPosition.z} | maxDelta={maxDelta}");
}
return false;
}
Expand Down
14 changes: 13 additions & 1 deletion SoD_BaseMod/src/basemod/console/commands/BTLevelCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ private static void OnExecuteRunToggleScript(BTConsoleCommand.BTCommandInput inp
Dictionary<BTToggleScriptEntry, int> matches = toggleScript.ToDictionary(script => script, script => 0);
int total = 0;
foreach (GameObject gameObject in Resources.FindObjectsOfTypeAll<GameObject>()) {
BTToggleScriptEntry matchingEntry = toggleScript.FirstOrDefault(entry => entry.ObjectMatches(gameObject, cmdInput.verbose));
BTToggleScriptEntry matchingEntry = toggleScript.FirstOrDefault(entry => entry.ObjectMatches(gameObject, cmdInput.verbose, cmdInput.accuracy));
if (matchingEntry != null) {
switch (cmdInput.mode) {
case BTLevelRunToggleScriptInput.ToggleScriptMode.ENABLE:
Expand Down Expand Up @@ -247,6 +247,7 @@ public enum ToggleScriptMode {
public string scriptName;
public ToggleScriptMode mode;
public bool verbose;
public float? accuracy;

private void SetScriptName(object scriptName, bool isPresent) {
this.scriptName = (string) scriptName;
Expand All @@ -260,6 +261,10 @@ private void SetVerbose(object verbose, bool isPresent) {
this.verbose = isPresent && (bool) verbose;
}

private void SetAccuracy(object accuracy, bool isPresent) {
this.accuracy = isPresent ? new float?((float) accuracy) : null;
}

protected override IEnumerable<BTConsoleCommand.BTConsoleArgument> BuildConsoleArguments() {
return new List<BTConsoleCommand.BTConsoleArgument> {
new BTConsoleCommand.BTConsoleArgument(
Expand All @@ -282,6 +287,13 @@ private void SetVerbose(object verbose, bool isPresent) {
"if true, provides debug information to the console - defaults to 'false'",
SetVerbose,
typeof(bool)
),
new BTConsoleCommand.BTConsoleArgument(
"accuracy",
true,
"provide a custom accuracy for float comparisons, this is the maximum difference between two equal floats - e.g. '2' means that '1.0' and '3.0' are considered equal",
SetAccuracy,
typeof(float)
)
};
}
Expand Down

0 comments on commit ed063a9

Please sign in to comment.