• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • 🏆 Hive's 6th HD Modeling Contest: Mechanical is now open! Design and model a mechanical creature, mechanized animal, a futuristic robotic being, or anything else your imagination can tinker with! 📅 Submissions close on June 30, 2024. Don't miss this opportunity to let your creativity shine! Enter now and show us your mechanical masterpiece! 🔗 Click here to enter!

[SNIPPET] Is Line Walkable

AGD

AGD

Level 16
Joined
Mar 29, 2016
Messages
688
IsLineWalkable v1.0b


This simple snippet allows you to check if there are any unwalkable paths such as cliffs, mapboundary and other obstacles such as some doodads between two points.



Requires:
TerrainPathability

Script
JASS:
library IsLineWalkable uses TerrainPathability/* v1.0b

     ________________
    |                |
    | Written by AGD |
    |________________|


    |=====|
    | API |
    |=====|

        function IsLineWalkable takes real x1, real y1, real x2, real y2 returns boolean
        - Checks if the line between points P1(x1, y1) and P2(x2, y2) does not contain any
          any unwalkable coordinates including some doodads, cliffs, mapboundary, and other similar stuffs

        function GetLastWalkableX takes nothing returns real
        - Returns the value of the x-coordinate of the last walkable terrain when using IsLineWalkable

        function GetLastWalkableY takes nothing returns real
        - Returns the value of the y-coordinate of the last walkable terrain when using IsLineWalkable

*/
    globals
        ///////////////////////////////////////////
        // The path walkability check distance   //
        // interval. Lower value increases       //
        // precision but may cost performance.   //
        // Suggested value limit (10.00 - 25.00) //
        ///////////////////////////////////////////
        private constant real checkInterval = 20.00
    endglobals

    //==============================================================================

    globals
        private real X = 0.00
        private real Y = 0.00
    endglobals

    function IsLineWalkable takes real x1, real y1, real x2, real y2 returns boolean
        local real dx = x2 - x1
        local real dy = y2 - y1
        local real angle
        local real x
        local real y
        if dx*dx + dy*dy < checkInterval*checkInterval then
            if not IsTerrainWalkable(x2, y2) then
                set X = TerrainPathability_X
                set Y = TerrainPathability_Y
                return false
            endif
        else
            set angle = Atan2(dy, dx)
            set x = checkInterval*Cos(angle)
            set y = checkInterval*Sin(angle)
            loop
                set x1 = x1 + x
                set y1 = y1 + y
                exitwhen x1 > x2 or y1 > y2
                if not IsTerrainWalkable(x1, y1) then
                    set X = TerrainPathability_X
                    set Y = TerrainPathability_Y
                    return false
                endif
            endloop
        endif
        set X = TerrainPathability_X
        set Y = TerrainPathability_Y
        return true
    endfunction

    function GetLastWalkableX takes nothing returns real
        return X
    endfunction

    function GetLastWalkableY takes nothing returns real
        return Y
    endfunction


endlibrary


v1.0b
- Replaced the variables IsLineWalkable_X/Y with the functions GetLastWalkableX/Y
- Fixed a bug regarding the values of GetLastWalkableX/Y not being updated if IsLineWalkable returns true
v1.0
- First Release
 
Last edited:
Level 20
Joined
Aug 13, 2013
Messages
1,696
After searching if theres any similar to this and surprisingly theres actually none.

Useful well made system. If only you made it compatible with vanilla ( but the TerrainPathability is strictly required so it would only make it complicated to do so especially coding that pathability ). Its pretty unique and nice idea, ( Hopefully vJass coders gonna use this ^^ )
 

AGD

AGD

Level 16
Joined
Mar 29, 2016
Messages
688
After searching if theres any similar to this and surprisingly theres actually none.
But I thought I've seen something with similar title to this, I'm not sure though, I did not look carefully. I made this because I need this in my spell I couldn't also find one.

If only you made it compatible with vanilla
Do you hardly need a vanilla jass version?
 

AGD

AGD

Level 16
Joined
Mar 29, 2016
Messages
688
Your only checks pathing in a line, if it can be reached through other routes..?
I understand now, perhaps I will include that in my updates if I can.

EDIT:
Updated to v1.0b
- Replaced the variables IsLineWalkable_X/Y with the functions GetLastWalkableX/Y
- Fixed a bug regarding the values of GetLastWalkableX/Y not being updated if IsLineWalkable returns true

feels like functionality to check if a point is reachable would be a logical addition.
I'm not sure if this is possible, take for example a very complex maze. In this case the system has to check every possible routes before it can tell if it's walkable. I'm not sure though.
 
Last edited:

AGD

AGD

Level 16
Joined
Mar 29, 2016
Messages
688
We could add it though to small code snipptets if you would like to. :)
Its perfectly okay with me =). Btw, I'm thinking of having this combined with my BoundCoordinates so that instead of making sure that the unit's coordinates will be set within the map boundary, it will ensure that the unit's position will be set to a walkable terrain.
 
Level 14
Joined
Nov 17, 2010
Messages
1,265
I've been thinking about making a blink-strike ability for my campaign but I didn't want it to be able to be exploited by blinking through blocked areas to reach enemies on the other side before you should be able to. This kind of thing would be perfect for that. Same can be said for a leap-type ability.

That being said it could be annoying if there is something like a barrel between your unit and the enemy that stops you from casting it..
 

AGD

AGD

Level 16
Joined
Mar 29, 2016
Messages
688
Bresenham Pathchecker
But I thought I've seen something with similar title to this, I'm not sure though, I did not look carefully. I made this because I need this in my spell I couldn't also find one.
So this is that I've seen once. Now I remember looking for this kind resource, I couldn't believe it was just in the submission section all along.
Btw, I dont get the difference, isn't the method here also like checking a per cell basis?
 
No, this one checks on a flat per-distance basis, which means you will end up checking the same cell twice or even three times, depending on the angle.

Also, the solution used here has no diagonal safety feature, which means depending on the angle and the pathing blockers, you might end up with a false positive (i.e. for the diagonal fence doodad).
 
Top