Need help with math(?) function

averes

New Member
Reaction score
17
I wrote up this function real quick to find what the closest buildable location to another location was:

JASS:
function GetNearestPathableLoc takes location start returns location
    local real x = GetLocationX(start)
    local real y = GetLocationY(start)
    local location checking = null

    local real increment = 1.0
    local boolean pathable = false

    loop
        exitwhen pathable == true
        
        set pathable = IsTerrainPathable(x+increment,y, PATHING_TYPE_BUILDABILITY)
        set checking = Location(x+increment,y)
        
        set pathable = IsTerrainPathable(x-increment,y, PATHING_TYPE_BUILDABILITY)
        set checking = Location(x-increment,y)
        
        set pathable = IsTerrainPathable(x,y+increment, PATHING_TYPE_BUILDABILITY)
        set checking = Location(x,y+increment)
        
        set pathable = IsTerrainPathable(x,y-increment, PATHING_TYPE_BUILDABILITY)
        set checking = Location(x,y-increment)
        
        set pathable = IsTerrainPathable(x+increment,y+increment, PATHING_TYPE_BUILDABILITY)
        set checking = Location(x+increment,y+increment)
        
        set pathable = IsTerrainPathable(x+increment,y-increment, PATHING_TYPE_BUILDABILITY)
        set checking = Location(x+increment,y-increment)
        
        set pathable = IsTerrainPathable(x-increment,y+increment, PATHING_TYPE_BUILDABILITY)
        set checking = Location(x-increment,y+increment)
        
        set pathable = IsTerrainPathable(x-increment,y-increment, PATHING_TYPE_BUILDABILITY)
        set checking = Location(x-increment,y-increment)
        
        set increment = increment + 1.0
    endloop
    return checking
endfunction


The only problem is... when I order a unit to move to the nearest buildable loc, they just seen to move to the given location "start", regardless whether or not its actually buildable. Any help on this?
 

trb92

Throwing science at the wall to see what sticks
Reaction score
142
Try changing the order of your functions. Make it set the location then check it, otherwise it will do this:

JASS:
loop//Begin loop
        exitwhen pathable == true
        
        set pathable = IsTerrainPathable(x+increment,y, PATHING_TYPE_BUILDABILITY) //Check this location
        set checking = Location(x+increment,y) //Set this location, same as was just checked.        

        set pathable = IsTerrainPathable(x-increment,y, PATHING_TYPE_BUILDABILITY) //Check this location. 
        //Let's say it's true, so return this one. 
        //Notice that the location variable hasn't been set to this new location, it'll return the pervious location.


But this entire function might be built wrong. The exitwhen condition isn't checked until all the actions are done(I think), so it will always return the last location you set.
 

averes

New Member
Reaction score
17
Hm, tried that and no change. It must be that the exitwhen isnt checked until its all done then, but that still doesnt explain why it would pick a non-buildable location, that would just cause it to pick one that wasnt nessecarily the closest wouldnt it?
 

Trollvottel

never aging title
Reaction score
262
the increment isnt really big, is it? and IsTerrainPathable is a bit weird. if the terrain IS buildable it will return FALSE. well, mind that this function only checks for the TERRAIN, not for destructables, units, buidlings, etc.
 

averes

New Member
Reaction score
17
the increment isnt really big, is it? and IsTerrainPathable is a bit weird. if the terrain IS buildable it will return FALSE. well, mind that this function only checks for the TERRAIN, not for destructables, units, buidlings, etc.

That did the trick! And yes, I intend on adding that check later. The only problem now is that while the location may be buildable, its still unable to 'build there' I assume this is because its right on the edge of a non-buildable terrain? Do you have any idea how I might be able to return a terrain thats not only buildable, but could actually be used in an Issue Build Order?
 

Trollvottel

never aging title
Reaction score
262
just a quick question, for what do you need this function? because if you create a building somewhere it cant stand its automatically moved to the next possible point.
 

averes

New Member
Reaction score
17
It does? Ive been testing this function for awhile and when I order a peasant to build at the 'NearestBuildableLoc', if that location isn't able to be built on it just sits there and does nothing.... I need it for some building AI in a map I'm making.

Edit: Erm I misread your reply, my mistake, but yeah I need it for some building AI where the buildings need to be built in relatively non-random/smart spots, so I can't just use a campaign AI.
 

Trollvottel

never aging title
Reaction score
262
a possible thing would be this: you create a dummy building with a trigger and then save its location and remove the building. now you know the nearest pathable place.
 

averes

New Member
Reaction score
17
a possible thing would be this: you create a dummy building with a trigger and then save its location and remove the building. now you know the nearest pathable place.

Just tried doing this, works perfectly! And is far simplier than the function I wrote XD. Thanks!
 

N-a-z-g-u-l

New Member
Reaction score
30
might work, but the new location will never be on another cliff level (can be quite annoying if the space on that cliff-level is filled, then the building can be created in some trees... and building order would fail

im not 100% sure, but i had the same problem (only with units)

my solution was: create a second unit of the same type on the same position:
if both units are on the same position later, there is some collision mistake, if they are not, everything should have worked correctly... if it did not work correctly, you would need to try another location somewhere else...
 
General chit-chat
Help Users

      The Helper Discord

      Staff online

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top