Simple math alteration to proximity script. (radius instead of square)

Hello. I love this simple little script by James Westbrook:

Spoiler: open for code
Code:
//-----------------------------------------------------------------------------
// James Westbrook's Distance Conditional
// Version 1.0
var Imported = Imported || {};
Imported.JMW_DistanceConditional = true;

var JMW = JMW || {};                // Main object
JMW.DC = JMW.DC || {};  // Plugin object

/*:
 * @plugindesc Version 1.0 Checks how close the event is to the player/ another event.
 *
 * @author James Westbrook 1.0
 *
 * @param
 * @desc
 * @default
 *
 * @help
 *Credits: James Westbrook for writing plugin, and Galv for
 *assisting James in writing his first plugin.
 *(find Galv at galvs-scripts.com)
 *Allowed for any use. Credit not necessary since this
 *is one of the easiest plugins to make.
 *
 * Normally when determining the distance between the player and an event(or an
 *event with another event), you need four lines of scripting that you will have
 *to insert into a conditional command. This plugin allows you to need only
 *one line!
 *
 *Uses for this plugin:
 *You can use this plugin to determine if the player is close enough to an
 *event to meet the distance requirement. You are able to modify this distance
 *number to suit your needs. You can also use it with two different events,
 *instead of with a player.
 *
 *Script Calls:
 *You can put either of the scripts in a conditional event.
 *
 *JMW.checkDist(eventID, X distance)
 *
 *JMW.checkEventsDist(eventNumber1, eventNumber2, distance)
 *
 *(read later in help for further instructions
 * on how to use these calls)
 *
 *The first is used when you want the condition to be if
 *the player is X close to the event. In "eventID"
 *you fill in what the event's ID is.
 *In "X distance" you fill in the distance you
 *want. For example if I put "JMW.checkDist(4, 5)"
 *, it would make the conditional would only
 *be met if the player was within 5 spaces
 *of event 4!
 *
 *The second script can be used to determine
 *if two events were within X range. For
 *example, if I put "JMW.checkEventsDist(7, 8, 5)"
 * if events 7 and 8 were within 5 spaces, the
 condition is met.
 *The exact place you will be using these SCRIPT
 *commands is found in the first tab when making
 * an event. Under "Flow Control", select
 *"Conditional Branch". Go to the fourth
 *tab. At the bottom is the script option.
 *Click onto the word "Script" and then
 fill in the box with your script call!
 *For a visual aid, refer to my Distance
 *Conditional Demo!
 *
 *
 */
    

(function() {
 

JMW.checkDist = function(eventNumber, playerDistance) {
return $gameMap.event(eventNumber)._x > $gamePlayer._x - playerDistance && $gameMap.event(eventNumber)._x < $gamePlayer._x + playerDistance && $gameMap.event(eventNumber)._y > $gamePlayer._y - playerDistance && $gameMap.event(eventNumber)._y < $gamePlayer._y + playerDistance
};

JMW.checkEventsDist = function(eventFirst, eventSecond, distanceSecond) {
    return $gameMap.event(eventFirst)._x > $gameMap.event(eventSecond)._x - distanceSecond && $gameMap.event(eventFirst)._x < $gameMap.event(eventSecond)._x + distanceSecond && $gameMap.event(eventFirst)._y > $gameMap.event(eventSecond)._y - distanceSecond && $gameMap.event(eventFirst)._y < $gameMap.event(eventSecond)._y + distanceSecond
};
    
    


})();

However the problem is, it calculates proximity of the player and other events with math that results in a square for triggering. What I want is actual radius. Please see the image below:

Spoiler: open for image example
2bv2giS.png

If the green square is event A and the player or event B is in a red square... I want it to trigger only like in the left hand example depending on the distance, not the right hand example like the code does now. So distance=1 would be on top of eachother. Distance=2 would be anything just 1 step away. Distance=3 would be anything 2 steps away. And so on.

Also, it would be helpful if entering "0" for the first argument would indicate "this event" but I understand that's more work and just a nice thing to have too. 🙂
https://forums.rpgmakerweb.com/index.php?threads/galvs-event-detectors.67977/
Have you tried QSight?
https://quxios.github.io/plugins/QSight
It can check if an event sees the player by using a circular/conical/rectangular area of sight
I mentioned a few different distance-measurement formulae in an old post over on the Steam discussions. It's not a plugin, but here are a couple of code blocks that can be used in Conditional Branch > Script:
  • Taxicab (default distance measurement, produces diamond AoE):
    Code:
    var radius = 5, ev = $gameMap.event(this.eventId()), pl = $gamePlayer; $gameMap.distance(ev.x, ev.y, pl.x, pl.y) <= radius
  • Cartesian (produces circular AoE fit to square tile-grid, diamond-shaped at low radii):
    Code:
    var radius = 5, ev = $gameMap.event(this.eventId()), pl = $gamePlayer; Math.round(Math.hypot($gameMap.deltaX(ev.x, pl.x), $gameMap.deltaY(ev.y, pl.y))) <= radius
In either case, simply adjust the radius variable to whatever value you like. I've used <= for comparing radius and distance here; given what you said you may want to change that to < instead. 🙂

I feel Yanfly's Event Proximity Activate plugin may be worth mentioning, too. 🙏
Thanks for the replies. The problem with most of these is they only function between an an event and the player--not event to event.

SO...
Have you tried QSight?
https://quxios.github.io/plugins/QSight
It can check if an event sees the player by using a circular/conical/rectangular area of sight

THIS looks actually REALLY useful for me but for some reason it doesn't work... nor does the QPlus.js

Just in case, I opened a brand new project and put in just QPlus and QSight, made sure they're turned on, but neither works at all. Not quite sure what I'm missing here.
Oh, here's event-event versions of the script calls I posted, in case they're helpful~
Spoiler: Event-event distance script checks
  • Taxicab:
    Code:
    var radius = 5, e1 = $gameMap.event(1), e2 = $gameMap.event(2); $gameMap.distance(e1.x, e1.y, e2.x, e2.y) <= radius
  • Cartesian:
    Code:
    var radius = 5, e1 = $gameMap.event(1), e2 = $gameMap.event(2); Math.round(Math.hypot($gameMap.deltaX(e1.x, e2.x), $gameMap.deltaY(e1.y, e2.y))) <= radius
Just need to replace 1 and 2 with the appropriate event IDs! 🙏

So, let's say you wanted to use the "taxicab"/"diamond" version (same distance measurement as the rest of the default game code), you could edit the plugin code to this (edits should be OK since the plugin cites it's "Allowed for any use"):
Spoiler: Edited plugin
Code:
(function() {  // edited to change distance metric

  JMW.checkDist = function(eventNumber, playerDistance) {
    var ev = $gameMap.event(eventNumber);
    var pl = $gamePlayer;
    return $gameMap.distance(ev.x, ev.y, pl.x, pl.y) <= playerDistance;
  };

  JMW.checkEventsDist = function(eventFirst, eventSecond, distanceSecond) {
    var e1 = $gameMap.event(eventFirst);
    var e2 = $gameMap.event(eventSecond);
    return $gameMap.distance(e1.x, e1.y, e2.x, e2.y) <= distanceSecond;
  };
    
})();
Even though this is basically a total rewrite, I've retained the JMW prefixes because I assume you've already got some script calls using that. You can edit the code in anything you like (e.g. Notepad) just be sure to keep the result as a .js file (Save As > File Type: All Files, Filename: whateverYouLike.js).

Note that if you'd like to use the ID of "this event", you can try something like this in a script call:
Code:
JMW.checkEventsDist(this.eventId(), 1, 5)
This says "is the distance between this event and event ID 1 less than or equal to 5?" 🙂

Original thread: https://forums.rpgmakerweb.com/threads/simple-math-alteration-to-proximity-script-radius-instead-of-square.114027/