Home   Help Search Login Register  

Author Topic: Compare distances  (Read 1347 times)

0 Members and 3 Guests are viewing this topic.

Offline GRFoxhound

  • OFPEC Patron
  • ****
    • Armaholic.com
Compare distances
« on: 18 Jun 2006, 05:07:30 »
I am stuck again with something.

I am trying to let a civilian go to the closest OPFOR unit.
The problem is, the civilian will check an array of OPFOR units to see how close they are, and if they are within a certain range he will go to that position.

But I have a problem, I will give an example:
If the civilian is closer to  unit Z than the civilian is to unit V he will still go to unit V (if V is within the predefined minimal distance) since unit V is checked first by the scipt.

I call the script like this:
Code: [Select]
[thislist select 0,informant,[v,w,x,y,z]] exec "temp.sqs"

I would like something to compare all disances and than the civilian decides what unit is the closest OPFOR unit and move to it.
I can make a massive script which compares all outcomes with eachoter but that will become massive. I guess I am just overlooking a simple solution  :-[
Tried to search for this aswell but found only one script but that link is dead :(. Other distance script were using the "nearestobject" command, and thats not what I need :)

Maybe one of you can help me on this one?

Offline Trapper

  • Honoured Contributor
  • ***
  • I'm a llama!
Re: Compare distances
« Reply #1 on: 18 Jun 2006, 10:08:44 »
Code: [Select]
_dis = 99999
_nearest = "init"

{if (mycivilian distance _x < _dis) then {_dis = (mycivilian distance _x); _nearest = _x}} foreach [myarray]
if (_dis <= mycertainrange) then {mycivilian domove getpos _nearest}
« Last Edit: 18 Jun 2006, 10:10:57 by Trapper »

Offline GRFoxhound

  • OFPEC Patron
  • ****
    • Armaholic.com
Re: Compare distances
« Reply #2 on: 18 Jun 2006, 13:20:47 »
Thanks for your help, I kind of understand what you are doing there, I am still a script noob. But I have 2 questions.

Code: [Select]
_nearest = "init"
Where is this "init" comming from? Or what does it do? Is it the same as
Code: [Select]
_nearest = []

And question 2
Code: [Select]
if (_dis <= mycertainrange) then {mycivilian domove getpos _nearest}
What is "mycertainrange"? In the script line before it you havent specified it, so where does it get its value?
I gues _dis is the distance I want the civ to check for OPFOR, so "mycertainrange" must be a different distance right?

Sorry for my noob questions but if you could/want please explain.  :-[



Offline Trapper

  • Honoured Contributor
  • ***
  • I'm a llama!
Re: Compare distances
« Reply #3 on: 18 Jun 2006, 14:26:44 »
I like to explain it to you, of course.

1.
Yes, my "init" is almost the same as [].
It's needed because the new variable _nearest can't be initalized in a code line: then {_dis = (mycivilian distance _x); _nearest = _x}. The script wouldn't work. It has to be done before.

[] initalizes the variable as array.
"" initalizes the variable as empty string; "init" just fills it with the nonsense string 'init'. :)
I don't know if it would work with an empty array but the later stored unit id isn't a string type either and it works.

2.
Quote
The problem is, the civilian will check an array of OPFOR units to see how close they are, and if they are within a certain range he will go to that position.
I thought you would need this line also.
Replace mycertainrange with 50 and the civilian will only start walking if the nearest unit is closer than 50m.

_dis starts with an impossible high distance value, so that even a unit on the other side on the island could be detected as the "nearest". Afterwards it stores any lower distance while the OPFOR array is checked.

Offline GRFoxhound

  • OFPEC Patron
  • ****
    • Armaholic.com
Re: Compare distances
« Reply #4 on: 18 Jun 2006, 16:04:29 »
Thank you very much  :)
It all works just perfectly.

And thanks aswell for that extra explanation, learned 2 more things :D

Offline GRFoxhound

  • OFPEC Patron
  • ****
    • Armaholic.com
Re: Compare distances
« Reply #5 on: 19 Jun 2006, 19:08:14 »
Code: [Select]
{if (mycivilian distance _x < _dis) then {_dis = (mycivilian distance _x); _nearest = _x}} foreach [u1,u2,u3,u4,u5,u6,u7]

Sorry to bother again but I am stuck again on this one.
This line is causing an error I cant figure how to pass. In the array I put ALL units that I would like to be checked by the script.
I continue the script with the following.

Code: [Select]
_leader = leader _nearest
blabla
blabla
?!(alive _leader) : exit

But the problem is, the civilian checking the array for the closest unit will ALSO check the dead guys in the array. So, if the leader of the nearest guy (lets say thats u7) is dead the script wont continue. It will keep exiting since the leader of u7 is dead. Now only if other friendly units come closer than those other (dead) units the script will continue again since it found an alive _leader.

How can I make it so that the script wont check distances of dead units?

Any help would be very much appreciated.
« Last Edit: 19 Jun 2006, 19:17:55 by GRFoxhound »

Offline GRFoxhound

  • OFPEC Patron
  • ****
    • Armaholic.com
Re: Compare distances
« Reply #6 on: 20 Jun 2006, 14:44:28 »
ok what about this?

Code: [Select]
_grouparray = [u1,u2,u3,u4,u5,u6,u7]

#start
?!(alive u1) : _grouparray = _grouparray - [u1]
?!(alive u2) : _grouparray = _grouparray - [u2]
?!(alive u3) : _grouparray = _grouparray - [u3]
?!(alive u4) : _grouparray = _grouparray - [u4]
?!(alive u5) : _grouparray = _grouparray - [u5]
?!(alive u6) : _grouparray = _grouparray - [u6]
?!(alive u7) : _grouparray = _grouparray - [u7]

{if (mycivilian distance _x < _dis) then {_dis = (mycivilian distance _x); _nearest = _x}} foreach _grouparray

This means, if I kill a unit which is in the array the script should see that, as that unit is no longer alive and........hopefully remove it from the array. Everytime this happens it should use the new (updated and so smaller) unitarray..............right?  ???

Offline Trapper

  • Honoured Contributor
  • ***
  • I'm a llama!
Re: Compare distances
« Reply #7 on: 20 Jun 2006, 17:23:43 »
Code: [Select]
{if ((mycivilian distance _x < _dis) && (alive _x)) then {_dis = (mycivilian distance _x); _nearest = _x}} foreach [u1,u2,u3,u4,u5,u6,u7]

Your script would work Foxhound. Of course a second foreach line would be able to replace the seven others.

Offline GRFoxhound

  • OFPEC Patron
  • ****
    • Armaholic.com
Re: Compare distances
« Reply #8 on: 21 Jun 2006, 20:06:06 »
Thanks Tapper.

That did the trick :)