MZ Check if a line drawn between 2 events passes through a specific Region

Hello RPG Makerers
I hope you're having a lovely time.
I am making a navigation puzzle where the player plots a course by placing Xs on the gridded map. I would like to check if a virtual line connecting these Xs goes over a specific region at any point, like over land for example. If yes, I want to give a the player a message that says something like,"You can't plot a course over land!".
There are a number of different conditions that trigger a failure for the player. But to break it down to the most basic part of the question:
Spoiler: screen shot
Screenshot 2025-02-15 at 15.54.59C.png
I want to check if the line between Event 1 and Event 2, is passing through Region 2 (see image above in spoiler). Is it possible to do this using script or anything else?

Thanks in advance for any help that anyone can provide
You could use a (sharp) line-raster algorithm to convert start & end points to a series of tile coordinates, then check each one of those tiles to see if one is marked with that region ID. Bresenham's Line Algorithm is probably the best solution, and it's a popular algorithm so the details are readily available online if you want to implement it yourself. 👋

In case it helps, I've implemented that algorithm, along with some helper functions, in feature D02 of my plugin Cae_Tweaks. With that enabled, you could try Conditional Branch -> Script (untested):

const c1 = $gameMap.event(1), c2 = $gameMap.event(2); CAE.Tweaks.D02.raster.lineTo(c1.x, c1.y, c2.x, c2.y, ({x, y}) => $gameMap.regionId(x, y) === 2).complete
I.e. "plot a line from the map XY coordinates of event 1 to those of event 2, blocked if the region of any tile on the line is equal to 2; then branch based on whether the line completed without being blocked".

(Note: my plugin does not include any functionality for visibly drawing lines or highlighting map tiles.)
Thank you @caethyril !! I use some of your plugins in my games and they are super helpful <3

I'll try it out first using your plugin since I have no skills as a programmer. If that doesn't work, I will look into the algorithm you mentioned.

I am not sure if this factors in, in a positive or negative way, but for actually drawing the line in-game, I found this plugin. Would that help the programming in anyway since the line is already being created?
Spoiler: Using CTB_LineBetweenCharaters plugin
Screenshot 2025-02-15 at 15.46.49.png
The script used for of the the lines in the screen capture above:
$gameScreen.createLineBetween($gameMap.event(5), $gameMap.event(2),{id:1, color:'0xff0000', width:1})
So the line already exits and has an id of 1. Would this make it easier to check if this line goes through a region?

Also, I just found out that the creator of this plugin, ct_bolt, is no longer with us. My deepest condolences to their friends on this forum; they seemed like an amazing human.

(edit)
I tried it with your plugin and suggested script @caethyril but it doesn't work :_(
you might do similar, if the condition is true if Caethyril code works, you place
a line, otherwise, show text that it is not possible.
I just tested and it seemed to work OK for me. Ideas:
  1. Cae_Tweaks has all its features disabled by default. In this case, you need to go to its parameters, scroll down, and enable feature D02:

    PluginManager.png
  2. My example checks a line between event ID 1 and event ID 2. Double-check that those IDs are correct for your map.
I don't think CT_Bolt's plugin would help here. It just uses existing functions to draw a line on an overlay: it doesn't directly deal with which pixels the line covers between its endpoints.

Technically, since Bresenham's algorithm is completely integer-based, it should be possible to implement it with no plugins or scripting. You'd need...8 variables, I think? (Current X & Y, target X & Y, step size X & Y, the error tally, and somewhere to put the region ID.) A switch would probably also be useful, to track whether the line was blocked or not. That would still be challenging though, especially if you're not strong in logic and/or maths.
It worked!
Originally it didn't work for 2 reasons:
1- I hadn't turned on the D02 feature 😅
2- The code you gave me:
const c1 = $gameMap.event(1), c2 = $gameMap.event(2); CAE.Tweaks.D02.raster.lineTo(c1.x, c1.y, c2.x, c2.y, ({x, y}) => $gameMap.regionId(x, y) === 2).complete
returns true if the line does NOT pass through region 2. I originally understood the reverse.
So I just put the warning message in the "else" branch of the condition, and left result blank if the above condition is true, like so:
Spoiler: conditional branching
Screenshot 2025-02-18 at 20.59.15.png
Screenshot 2025-02-18 at 21.00.03.png

Anyway, thank you so much for your help! You're amazing @caethyril <3<3<3
I would put the script inside the if branch while true, not above.
or it always draw a line (which you might not want)?

but it does what you need, good luck with your project
I would put the script inside the if branch while true, not above.
or it always draw a line (which you might not want)?

but it does what you need, good luck with your project
Yes, your suggestion is great and I will use it for the actual game, I think.
But i wanted to leave it like that for the moment for the clarity of the screenshot.
Thank you!
Two other line algorithms to try if Bresenham's is inadequate for whatever reason. (I had mixed results for it b/c sometimes it can in rare cases skip a corner, which if you're trying to detect walls can cause a miss.)

DDA Line Algorithm
Wu's Line Algorithm

There's also orthogonally stepping which is guaranteed to not miss a corner but I've lost the real name of that one
Great!
returns true if the line does NOT pass through region 2. I originally understood the reverse.
Oh, oops. Yes, that wasn't very intuitive. 😅

What you've done, using the Else branch, obviously works. Alternatively you can invert the result (true <-> false) by adding a ! ("not") in front of the final statement, i.e.

const c1 = $gameMap.event(1), c2 = $gameMap.event(2); !CAE.Tweaks.D02.raster.lineTo(c1.x, c1.y, c2.x, c2.y, ({x, y}) => $gameMap.regionId(x, y) === 2).complete
Technically my plugin also tracks, for each tile on the path, the numpad direction representing the side of the tile from which the line entered, e.g. 8 if it entered from the top edge. So I think you could extend it to check 2 tiles each step: the current one and the "previous" one (which, like @FlawlessGamedev mentions, might be a skipped corner). Untested:

const c1 = $gameMap.event(1), c2 = $gameMap.event(2); !CAE.Tweaks.D02.raster.lineTo(c1.x, c1.y, c2.x, c2.y, ({ x, y, d }) => $gameMap.regionId(x, y) === 2 || $gameMap.regionId($gameMap.roundXWithDirection(x, d), $gameMap.roundYWithDirection(y, d)) === 2).complete

Edit: actually, I don't think this would work, sorry. The direction my plugin uses is diagonal when taking a corner, which is both lossy in information and incompatible with the roundXWithDirection etc methods because they only handle cardinal directions. Should be possible to work around that by making the script call even longer, but I'm not focused enough to figure it out right now. 😴
You can also try an event approach. It is a bit complicated but maybe it will be easier to modify to add visual effect.
Here's a mockup of the solution:
Spoiler
1739929634326.png


◆Control Variables:#0001 x1 = Map X of dest
◆Control Variables:#0002 y1 = Map Y of dest
◆Control Variables:#0005 x3 = Map X of This Event
◆Control Variables:#0006 y3 = Map Y of This Event
◆Get Location Info:id 0007, Event ID, dest
◆Loop
◆Control Variables:#0003 x2 = Map X of This Event
◆Control Variables:#0004 y2 = Map Y of This Event
◆Get Location Info:region 0008, Region ID, This Event
◆If:region 0008 = 2
◆Text:None, None, Window, Bottom
:Text:You can't plot a course over land!
◆Break Loop
◆
:End
◆If:x1 = x2
◆If:y1 = y2
◆Text:None, None, Window, Bottom
:Text:You have found a course!
◆Break Loop
◆
:End
◆
:End
◆Script:var dest = $gameMap.event($gameVariables.value(7));
:Script:var start = $gameMap.event(this._eventId);
:Script:start.moveTowardCharacter(dest);
:Script:start.setPosition(start._x, start._y);
◆
:Repeat Above
◆Set Event Location:This Event, ({x3},{y3})

You can also call the script from the destination event like this. And maybe let the start event move toward it. You can also use the player itself to do the movement. But I don't know your setup. So:

Spoiler
1739930469534.png


◆Control Variables:#0001 x1 = Map X of dest
◆Control Variables:#0002 y1 = Map Y of dest
◆Control Variables:#0005 x3 = Map X of This Event
◆Control Variables:#0006 y3 = Map Y of This Event
◆Get Location Info:id 0007, Event ID, dest
◆Loop
◆Control Variables:#0003 x2 = Map X of This Event
◆Control Variables:#0004 y2 = Map Y of This Event
◆Get Location Info:region 0008, Region ID, This Event
◆If:region 0008 = 2
◆Text:None, None, Window, Bottom
:Text:You can't plot a course over land!
◆Break Loop
◆
:End
◆If:x1 = x2
◆If:y1 = y2
◆Text:None, None, Window, Bottom
:Text:You have found a course!
◆Break Loop
◆
:End
◆
:End
◆Script:var dest = $gameMap.event($gameVariables.value(7));
:Script:var start = $gameMap.event(this._eventId);
:Script:start.turnTowardCharacter(dest);
◆Set Movement Route:This Event (Wait)
:Set Movement Route:◇1 Step Forward
◆
:Repeat Above
◆Set Event Location:This Event, ({x3},{y3})
ezgif-5a94b9f04254dd.gif
Great!

Oh, oops. Yes, that wasn't very intuitive. 😅

What you've done, using the Else branch, obviously works. Alternatively you can invert the result (true <-> false) by adding a ! ("not") in front of the final statement, i.e.

const c1 = $gameMap.event(1), c2 = $gameMap.event(2); !CAE.Tweaks.D02.raster.lineTo(c1.x, c1.y, c2.x, c2.y, ({x, y}) => $gameMap.regionId(x, y) === 2).complete

Technically my plugin also tracks, for each tile on the path, the numpad direction representing the side of the tile from which the line entered, e.g. 8 if it entered from the top edge. So I think you could extend it to check 2 tiles each step: the current one and the "previous" one (which, like @FlawlessGamedev mentions, might be a skipped corner). Untested:

const c1 = $gameMap.event(1), c2 = $gameMap.event(2); !CAE.Tweaks.D02.raster.lineTo(c1.x, c1.y, c2.x, c2.y, ({ x, y, d }) => $gameMap.regionId(x, y) === 2 || $gameMap.regionId($gameMap.roundXWithDirection(x, d), $gameMap.roundYWithDirection(y, d)) === 2).complete

Thanks! I'll try these out.
Like the programming absolute noob that I am, I tried putting the ! in front if the === before I read this 😅

And thank you @utunnels I'll try your solution too and see if that works better for me.
Ok still haven't found the orthogonal stepping algorithm (or its proper name) but an alternative way to handle missing corners is to just repeat any other algorithm twice: one at the "true" origin, then another time from a tiny offset. That'll reliably hit walls.

Original thread: https://forums.rpgmakerweb.com/threads/check-if-a-line-drawn-between-2-events-passes-through-a-specific-region.175559/