[Wurst] Pathable

Chaosy

Tutorial Reviewer
Level 39
Joined
Jun 9, 2011
Messages
13,070
Since I did not find something to check for pathability in the standard lib I adapted the isTerrainWalkable by Rising Dusk to wurst.

Code:
package pathable

import LinkedList

constant real    MAX_RANGE     = 10.
constant integer DUMMY_ITEM_ID = 'wolg'

LinkedList<item> list          = new LinkedList<item>()

item pathChecker
rect detect

public function vec2.isTerrainWalkable() returns boolean
    return isTerrainWalkable(this.x, this.y)

public function isTerrainWalkable(vec2 pos) returns boolean
    return isTerrainWalkable(pos.x, pos.y)

function showItems()
    for item i in list
        i.setVisible(true)
        list.remove(i)

function hideItem()
    item i = GetEnumItem()
    if IsItemVisible(i)
        i.setVisible(false)
        list.push(i)

function hideNearbyItems(real x, real y)
    detect.moveTo(x, y)
    EnumItemsInRect(detect ,null, function hideItem)

function isTerrainWalkable(real x, real y) returns boolean
    hideNearbyItems(x, y)
    pathChecker.setPos(x, y)
    pathChecker.setVisible(false)
    real itemPosX = pathChecker.getX()
    real itemPosY = pathChecker.getY()

    showItems()
    return (itemPosX-x)*(itemPosX-x)+(itemPosY-y)*(itemPosY-y) <= MAX_RANGE*MAX_RANGE and not IsTerrainPathable(x, y, PATHING_TYPE_WALKABILITY)

init  
    detect = Rect(0, 0, 128, 128)
    pathChecker = CreateItem(DUMMY_ITEM_ID, 0, 0)
    ..setVisible(false)

Simple Code Example



Code:
import pathable
import HashMap
import TimerUtils
import LinkedListModule

HashMap<integer, Charge> hash = new HashMap<integer, Charge>()
unit test

class Charge
    use LinkedListModule

    unit charger
    vec2 destination

    construct(real x, real y, unit u)
        getTimer()
        ..startPeriodic(0.03, function onLoop)
        charger = u
        destination = vec2(x, y)

    function update()
        vec2 pos = charger.getPos()
        angle a = pos.angleTo(destination)
        vec2 moveTo = pos.polarOffset(a, 5)
        if(moveTo.isTerrainWalkable())
            charger.setPos(moveTo)
            if charger.getPos().distanceTo(destination) < 10
                destroy this
        else
            destroy this

    static function onLoop()
        thistype current = Charge.first
        while(current != null)
            current.update()
            current = current.next

function onClick()
    new Charge(BlzGetTriggerPlayerMouseX(), BlzGetTriggerPlayerMouseY(), test)

init
    test = createUnit(players[0], 'hpea', vec2(0, 0), angle(0))

    CreateTrigger()
    ..registerPlayerEvent(players[0], EVENT_PLAYER_MOUSE_DOWN)
    ..addAction(function onClick)

    getTimer()
    ..startPeriodic(0.03, function Charge.onLoop)

 
Last edited:
Top