• 🏆 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!

[JASS] Check if point is in rect

Status
Not open for further replies.
Level 1
Joined
Jul 6, 2012
Messages
5
I want to create n units randomly on the playable map, but they should only spawn on a walkable location.

  • Init
    • Events
      • Map initialization
    • Conditions
    • Actions
      • Visibility - Disable fog of war
      • Visibility - Disable black mask
      • Set tmp_reg = (Playable map area)
      • For each (Integer A) from 1 to 200, do (Actions)
        • Loop - Actions
          • Set tmp_point = (Random point in tmp_reg)
          • Custom script: call walkable(udg_tmp_point)
          • Unit - Create 1 Peasant for Player 1 (Red) at tmp_point facing Default building facing degrees
          • Custom script: call RemoveLocation(udg_tmp_point)
      • Custom script: call RemoveRect(udg_tmp_reg)
This init trigger calls the function walkable which I'd like to redefine udg_tmp_point, if it should be created at a unwalkable location.

JASS:
function walkable takes location l returns nothing

local boolean b
local real x = GetLocationX(l)
local real y = GetLocationY(l)

loop

set b = IsTerrainWalkable(x, y)

if b == true then


set udg_tmp_point = Location(x, y)
exitwhen true

else

set l = Location(GetRandomReal(GetRectMinX(udg_tmp_reg), GetRectMaxX(udg_tmp_reg)), GetRandomReal(GetRectMinY(udg_tmp_reg), GetRectMaxY(udg_tmp_reg)))
set x = GetLocationX(l)
set y = GetLocationY(l)
endif




endloop



endfunction


The problem is that it does not seem to work. Anyone got see what is wrong?
 
Level 5
Joined
Jun 16, 2004
Messages
108
Does not work how? Units get created on areas you cannot walk on, perhaps? Nothing happens?

Here are a couple of suggestions and comments.
- You should clean up your location right before setting udg_tmp_point to a new one, like so:
JASS:
call RemoveLocation(udg_temp_point)
set udg_tmp_point = Location(x, y)
since the way you currently do it will leak a location. You should also call "RemoveLocation" on variable l before setting l to a new location.

- You should not destroy the rect like you did here
  • Custom script: call RemoveRect(udg_tmp_reg)
That would render "(Playable map area)" unusable to future triggers

- You do not show the "IsTerrainWalkable" function, but I imagine it wraps around IsTerrainPathable. Are you aware that IsTerrainPathable returns false when the area is pathable, and true when it is not pathable? Assuming that your IsTerrainWalkable function is the same, then you would want to do this instead:
JASS:
set b = not IsTerrainWalkable(x, y)
 
Level 5
Joined
Sep 28, 2010
Messages
75
You are continuously looping to find a walkable terrain. Why would you do that?
SetUnitPosition(unit, x, y) will move the unit AND if the terrain is unwalkable the unit will be moved to a nearby walkable terrain.

Also, Unit - Create N Unit-Type for Player at Location facing X degrees automatically creates units and moves them to a walkable location.

edit:
- You do not show the "IsTerrainWalkable" function, but I imagine it wraps around IsTerrainPathable. Are you aware that IsTerrainPathable returns false when the area is pathable, and true when it is not pathable?

I think IsTerrainWalkable is a wrapper from the library TerrainPathability. I am quite familiar with it, and TerrainPathability always wraps IsTerrainPathable so that it returns the correct value.
 
Level 1
Joined
Jul 6, 2012
Messages
5
@Halo7568, what happens is on the only walkable area on the map only 3-5 units are created, where there should have been 200. Thanks for the heads up on the coding errors. I'll fix that.

@F1ashB0nd "Also, Unit - Create N Unit-Type for Player at Location facing X degrees automatically creates units and moves them to a walkable location." When I don't call the script, I get a fatal error. Why would that be?

(It is indeed the TerrainPathability library I am using.)
 
Level 5
Joined
Jun 16, 2004
Messages
108
You probably do not have to change how you set the boolean, if you are using someone else's library then IsTerrainWalkable probably returns true for walkable terrain as you would expect.

I suppose there is the possibility of your thread hitting the op limit, do you happen to have mostly terrain that is not walkable?
 
Status
Not open for further replies.
Top