From 711d7ea8ca60c4d84142df3a3bfcc31cf12034a8 Mon Sep 17 00:00:00 2001 From: raeleus Date: Thu, 3 Oct 2024 00:16:14 -0700 Subject: [PATCH] Allow #goto to travel partially toward a target. --- Input.js | 20 +++++++++++++++++--- Library.js | 13 +++++++++++++ Output.js | 2 ++ README.md | 1 + 4 files changed, 33 insertions(+), 3 deletions(-) diff --git a/Input.js b/Input.js index 3f7b7ee..9d5393a 100644 --- a/Input.js +++ b/Input.js @@ -1307,13 +1307,13 @@ function doGoToLocation(command) { var arg1 = getArgument(command, 1) if (arg0 == null || isNaN(arg0)) { - arg0 = state.x + arg0 = state.x arg1 = state.y locationArgIndex = 0 } if (arg0 != null && (arg1 == null || isNaN(arg1))) { - arg1 = state.y + arg1 = null locationArgIndex = 1 } @@ -1325,7 +1325,21 @@ function doGoToLocation(command) { return "\n[Error: Not enough parameters. See #help]\n" } - if (locationName == null) { + if (arg1 == null && locationName != null) { + var index = state.locations.findIndex(x => x.name.toLowerCase() == locationName.toLowerCase()) + if (index != -1) { + location = state.locations[index] + var direction = pointDirection(state.x, state.y, location.x, location.y) + log(`direction:${direction}`) + var args = rotate(state.x, state.y, state.x + parseInt(arg0), state.y, direction) + arg0 = Math.round(args[0]) + arg1 = Math.round(args[1]) + location = null + } else { + arg1 = state.y + location = null + } + } else if (locationName == null) { var index = state.locations.findIndex(x => x.x == arg0 && x.y == arg1) if (index != -1) location = state.locations[index] } else { diff --git a/Library.js b/Library.js index b679150..cb195d5 100644 --- a/Library.js +++ b/Library.js @@ -25,6 +25,19 @@ function pointDistance(x1, y1, x2, y2) { return Math.sqrt(Math.pow(x1 - x2, 2) + Math.pow(y1 - y2, 2)) } +function pointDirection(x1, y1, x2, y2) { + Math.atn2 + var a = Math.atan2(y2 - y1, x2 - x1); + if (a < 0) a += 2 * Math.PI; + + if (a < 0) a += 2 * Math.PI; + if (a < 0) a += 2 * Math.PI; + a = Math.abs((Math.PI * 2) - a); + a *= 180 / Math.PI; + + return a; +} + function rotate(cx, cy, x, y, angle) { var radians = (Math.PI / 180) * angle var cos = Math.cos(radians) diff --git a/Output.js b/Output.js index 5b6a754..adf1a75 100644 --- a/Output.js +++ b/Output.js @@ -333,6 +333,8 @@ const modifier = (text) => { text += "\n Creates a location at the given coordinates. The coordinates must be integers. If the coordinates are not provided, they are randomized within a range of 10 units from the party's current location. You can also use \"here\" to indicate that the location is at party's coordinates. \"far\" indicates that the coordinates will be randomly generated 50-100 units away. You may also just specify a distance. Multiple locations may exist at the same coordinates. A story card is created for the location. Quotes are not necessary." text += "\n#goto (x) (y) or (location_name)" text += "\n Makes the party travel to the location specified by the coordinates (as integers) or location_name. You must provide at least one or the other. If the location does not exist, it is created at your current coordinates. If you only specify coordinates, you will go to the first location at those coordinates. Quotes are not necessary." + text += "\n#goto distance location_name" + text += "\n An alternative of the above. Travels the specified distance towards the location indicated by location name. Quotes are not necessary" text += "\n#gonorth (distance)" text += "\n The party travels north the given distance (an integer). If distance is not specified, it is assumed to be 1." text += "\n#gosouth (distance)" diff --git a/README.md b/README.md index 51f836d..3f4234e 100644 --- a/README.md +++ b/README.md @@ -18,6 +18,7 @@ Watch the [tutorial video](https://youtu.be/E5TYU7rDaBQ). v. 0.1.1 * #note without parameters will store the last action text into the notes +* #goto specified with a distance and location name will take you partially toward the target. * Minor bug fixes and improvements v. 0.1.0