HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

Two questions: Finding the closest unit and stopping orders.

02-29-2004, 08:56 AM#1
35263526
I've got a few questions which I'd assume would need JASS, but if there is a simple GUI solution I'd gladly take it.

1. I need a way to get the closest unit to a unit, and find out how far away, preferably without causing too much lag.

2. I've got a system set up in which my peasants have abilities that can be used by the player to give them certain orders via triggers. I'm trying to stop any other orders but these certain abilities being done. I can stop the orders being carried out, but I was wondering if there is a way to stop the Stop, Attack, Patrol etc buttons being show. Take into account the fact that I have to be able to give them those orders via triggers.

Thanks to anyone who can help...
02-29-2004, 01:22 PM#2
johnfn
cool! i get to help a mod, instead of the other way around!


Here is the link i found:
here


That shows how to remove the attack button and i assume that is also how to remove the other buttons.

I also have a trigger idea for your other question.
here is some pseudo code(the ideas instead of the real code) for what i have in mind, i also chanced upon a post that asked the same question.


make a region centered on the unit casting a spell seeking out the closest unit
for a = 1 to a very large number do
make that region a little bigger
Pick every unit in reigion and add 1 to int_count_units and if int_count_units > 1 then do (whatever you need to do with the unit thats closest)

hope it helps.
02-29-2004, 06:54 PM#3
Mr. Euthanasia
Hmmm, off the top of my head I can't think of anything incredibly efficient to get the closest unit but:

Select all the units within whatever of your guy, then loop through them, each time you get the distance between your unit and the unit your currently looping and store it into a distance "pointer", each new unit you check compare the distance against your "pointer" and see if it is a smaller number.
02-29-2004, 07:03 PM#4
ObsidianTitan
To get the closest unit there is this here which basicly does what the other people are saying. Its jass so if you dont want to use it then dont.
02-29-2004, 07:13 PM#5
35263526
Thanks, I'm sorted now.
02-29-2004, 08:42 PM#6
Grater
You might also consider using my function here, the main difference being it takes a group and a location (or x,y co-ordinates) and returns the closest unit in the group to the location.
02-29-2004, 08:58 PM#7
Narwanza
Both Grater's and Mike's functions have one flaw. They both will return corpses. Here is mine, and it doesn't return corpses.

Code:
function GetClosestUnit takes unit who, group pool returns unit 
    local location temploc = GetUnitLoc(who) 
    local real distance = 9999999.0 
    local location temploc01 = null 
    local unit u = null 
    local unit actualunit = null 
    local real distance01 = 0 
    local group pool01 = null 
    if (CountUnitsInGroup(pool) > 0) then 
        set pool01 = pool 
    else 
        return null 
    endif 
    call GroupRemoveUnitSimple(who, pool) 
    loop 
        set u = FirstOfGroup(pool01) 
        exitwhen u == null 
        set temploc01 = GetUnitLoc(u) 
        call GroupRemoveUnitSimple(u, pool01) 
        set distance01 = DistanceBetweenPoints(temploc01,temploc) 
        if (distance01 < distance) [color=red]and (GetUnitLifePercent(u)>0) [/color] then 
            set distance = DistanceBetweenPoints(temploc01,temploc) 
            set actualunit = u 
        endif 
        call RemoveLocation(temploc01) 
        set temploc01 = null 
        set u = null 
    endloop 
    call DestroyGroup(pool01) 
    call RemoveLocation(temploc) 
    set temploc = null 
    set pool01 = null 
    return actualunit 
endfunction 

The red part is the one the other two are missing, theirs will return a corpse as the closest unit which is not what I think you want. However if you do want that I submitted a func into the vault a couple days ago that found closest corpse (it is still being verified).
02-29-2004, 10:20 PM#8
Grater
Corpses are units too you corpsist bastard. Anyway in nearly all cases you need a condition (like to filter out buildings) and you just put the (IsAlive = True/False) condition in there.
02-29-2004, 10:28 PM#9
Narwanza
I know corpses are units, but if you try to explode the closest unit from your function, when it returns a corpse you are going to be wondering why it isn't exploding the closest unit. Now if the person passing the group argument is smart they are going to realize that they are going to have to have a ((matching unit) is alive = true) when they gather the group, but not every person out there is bright enough to realize that. So it does no good to call me a bastard since I was just trying to help the general public. If you want to find the closest corpse just change the > to an = red spot and there you have it. If you want to find the closest corpse/unit just remove the and condition alltogether.
02-29-2004, 11:01 PM#10
Grater
It was a joke you know? :////

And "pick every unit in range/region" returning corpses is something that every triggerer learns early in their triggering days, usually the hard way.
02-29-2004, 11:09 PM#11
linkmaster23
I was also wondering this... is there a way to do it in GUI? Custom Text is ok, just walk me through it.
03-01-2004, 01:16 AM#12
Narwanza
@Grater
Sry if I overreacted. I didn't learn that unitl I was actually writing that function and couldnt figure out why it wouldn't return the closest unit. THen I figured out it was returning closest corpse.

Oh, I saw your function at the JASS vault but I have no idea why you did the bj_wantdestroygroup if part. Besides that our functions are nearly identical.
03-01-2004, 02:20 AM#13
Grater
Well my function works more effecientely if it can consume the group given to it, however often triggerers want to use that group again. For blizzard.j functions that take a unit group, setting bj_wantDestroyGroup = true means "yes, destroy the group". From outside appearences it's the same for my function.

Your function always destroys the group given to it, altough I'm not sure if that if that is your intention or if you don't fully understand creating, destroying & copying groups.