MV Can't figure out a Math problem with SAN Tile Toner...

I'm using https://plugin.fungamemake.com/archives/1606 this along with some eventing to basically simulate a strat rpg while on certain maps (I don't need an actual tactical system as what I'm doing, I don't need the battle side of any of those). I'm having an issue with the math on things tho. I have an example of how to place a diamond of sorts under someone and I figured out how to place it under the player, but the thing is...

I don't quite understand the example/demo code enough to know how to get the amount of tiles covered that I need per event/player.

JavaScript:
var cX = $gamePlayer._x;
var cY = $gamePlayer._y;
for(var x = 0; x < 5; x++) {
  for(var y = 0; y < 5; y++) {
    if (Math.abs(x - 2) + Math.abs(y - 2) < 3) {
      $gameMap.setTileTone(
        cX + x - 2,
        cY + y - 2,
        0, 0, 255, 160);
    }
  }
}

this looks like this...

ScreenShot_5_18_2024_8_14_2.png


I'm trying to get different numbers instead of 5 across based on player/event stuff, but even messing around with the numbers in the code above, I can't get what I'm looking for. Crazy thing is I know this is probably a simple solution, but my math skills aren't amazing by ANY means. Anyone with better Javascript/Math skills have a solution, I'm all ears.
var cX = $gamePlayer._x; var cY = $gamePlayer._y; for(var x = 0; x < 5; x++) { for(var y = 0; y < 5; y++) { if (Math.abs(x - 2) + Math.abs(y - 2) < 3) { $gameMap.setTileTone( cX + x - 2, cY + y - 2, 0, 0, 255, 160); } } }
So what you have is a diamond shape grid with the player at the center.

x < 5 and y < 5 represents the max length and height of the diamond grid. (0 through 4 is 5 units total)

Math.abs() functions are checking to see if you are 2 tiles or less from every tile direction where the player is standing and those tiles will be colored blue if that is true. Notice that diagonally from the player only 1 tile is colored because 2 tiles diagonally comes out to be 3 units based on the condition we set in the script.

Here is a quick function that might help you get an idea of what values are variable based on the diamond grid you want:
JavaScript:
function grid_Radius(value) {
    if (isNaN(value)) {
        console.error('Integer is required.');
        return;
    };
    const len = Math.floor(Math.abs(value));
    const cX = $gamePlayer._x;
    const cY = $gamePlayer._y;
    for (let x = 0; x <= len * 2; x++) {
        for (let y = 0; y <= len * 2; y++) {
            if (Math.abs(x - len) + Math.abs(y - len) <= len) {
                $gameMap.setTileTone(cX + x - len, cY + y - len, 0, 0, 255, 160);
            };
        };
    };
};
I haven't tested this function but I believe it should work for you - my apologies in advance if I missed a few things since I'm not at a computer to test this (and it's pretty late here). All you need to do is save the above as a plugin and in your event/common event/etc, run the script call:
JavaScript:
grid_Radius(2)
and it should give you a diamond grid of 2 tiles away from the player all around (effectively 5 tiles height and width). Use whatever integer you want and it should work. Let me know if it doesn't.

Thanks!

Edit: made some changes so it should be better now. Maybe grid_Radius might make more sense being that it is looking at the radius units so I'll rename it that.
the code worked, big thanks! between this and trying to figure out how to remove tiles based on region, this was quite the headache to get working right. This code works best with odd numbers I'm noticing, which is fine. I mostly just needed a way to visually show where the play or event can move per movement phase.

I do have one quick question, how would I use this for events? Or is there a way to do it where I can reference an event without the number. I know
$gameMap.event(this._eventId) won't work in this case because it doesn't know what event is even being called.
the code worked, big thanks! between this and trying to figure out how to remove tiles based on region, this was quite the headache to get working right. This code works best with odd numbers I'm noticing, which is fine. I mostly just needed a way to visually show where the play or event can move per movement phase.

I do have one quick question, how would I use this for events? Or is there a way to do it where I can reference an event without the number. I know
$gameMap.event(this._eventId) won't work in this case because it doesn't know what event is even being called.
I changed some of the code if you haven't checked yet so it should be based on radius and not diameter now.

It depends on which event you want - can you be more specific? There are a few ways to get the events on the map and filter to get the one you need.
basically any event that cam move around in the area, I'd like to get one of these under them. like in the picture I posted, the 2 events with the word Void. they'd basically be enemy npcs running around. I've already got it setup to where if you're close enough to them when it's their turn in movement, you can get attacked. I would just want the player to be able to visually see where they can go.
basically any event that cam move around in the area, I'd like to get one of these under them. like in the picture I posted, the 2 events with the word Void. they'd basically be enemy npcs running around. I've already got it setup to where if you're close enough to them when it's their turn in movement, you can get attacked. I would just want the player to be able to visually see where they can go.
If I understood this correctly, you want the events to also have a grid (possibly a different color), correct?

Here's a change that you would need in the function to look for player (use -1 for id) or event (use 0 for this event, 1+ for the event's id) and you can define the rgba stuff too:
JavaScript:
function grid_Radius(value, id, r, g, b, a) {
    if (isNaN(value)) {
        console.error('Integer is required.');
        return;
    };
    const len = Math.floor(Math.abs(value));
    const cX = $gameMap._interpreter.character(id || 0)._x;
    const cY = $gameMap._interpreter.character(id || 0)._y;
    for (let x = 0; x <= len * 2; x++) {
        for (let y = 0; y <= len * 2; y++) {
            if (Math.abs(x - len) + Math.abs(y - len) <= len) {
                $gameMap.setTileTone(cX + x - len, cY + y - len, r || 0, g || 0, b || 255, a || 160);
            };
        };
    };
};

If we assume you put "Void" in the event's name box, you can filter all events on map and iterate over the array of events with the name "Void" (or whatever name you choose):
JavaScript:
const enemy = $gameMap.events().filter(e => e.event().name === 'Void');
for (const a of enemy) grid_Radius(2, a.eventId(), 255, 0, 0, 160);
(Still not at the computer so please test - hurray for trying to code on the phone lol - thanks!)

Edit: if you want all events, just do this:
JavaScript:
for (const a of $gameMap.events()) grid_Radius(2, a.eventId());
pretty sure I'm doing something wrong on my end, but it keeps saying this.character isn't a function. while I could get the other code working, I can't get this one to. tho again, I'm pretty sure I'm probably doing something wrong on my end. I think half the problem is I'm not sure where I'm supposed to call this from. The first code, I put it in a js file and simply called it at the beginning of an event and it was fine. I can't seem to get that to work in this case.
pretty sure I'm doing something wrong on my end, but it keeps saying this.character isn't a function. while I could get the other code working, I can't get this one to. tho again, I'm pretty sure I'm probably doing something wrong on my end. I think half the problem is I'm not sure where I'm supposed to call this from. The first code, I put it in a js file and simply called it at the beginning of an event and it was fine. I can't seem to get that to work in this case.
Sorry, change it to $gameMap._interpreter.character() instead of this.character(). It's probably not the right scope. I changed it above.
awesome, that fixed it! I'm kinda surprised this.character didn't work tho seeing as it can be used for this in general as the examples I have show. Tho, I'm wondering if that'll only work within the actual event itself. this helps me out a lot, thanks for all the help.

I've basically got everything working outside of making sure you/enemy npcs can't move outside of the tile radius. I think I can get that figured out tho since I was able to figure out how to remove them per region. I can probably look in the console to see if I can find an array with every current tile listed or something.

Original thread: https://forums.rpgmakerweb.com/threads/cant-figure-out-a-math-problem-with-san-tile-toner.168815/