Home   Help Search Login Register  

Author Topic: detect nearest east soldier  (Read 1779 times)

0 Members and 1 Guest are viewing this topic.

g

  • Guest
detect nearest east soldier
« on: 05 Jan 2003, 19:52:35 »
Hi all,

Just a quick question cos my scripting skill are lame  ;D

I want to detect the nearest east soldier of any class from my player and then if the distance is under say 50 meters a certain action takes place

at the moment my script looks like this

_enemy = NearestObject [player,"SoldierEB"]

_dis = player distance _enemy

? _dis > 200 : goto "noone"

where I check the nearest basic soldier but its pretty crude and I would like to detect any class of soldier

Thanks

G

Offline Ranger

  • Members
  • *
  • Hoo-ah!
Re:detect nearest east soldier
« Reply #1 on: 06 Jan 2003, 20:37:50 »
Try the following ideas.  Note that I didn't test it, so it might need some fixing.

First, execute your script with the following code:

player exec "DetectNearest.sqs"

Then, create the DetectNearest.sqs script with the following code:

Code: [Select]
; Give the local variable _player the value of _this, which is the passed
; unit.

_player = _this

; Initialize an array with the object names of each East soldier type.
; Note that in my example code here, I didn't include *EVERY* existing
; East soldier, as I don't recall the names of all of them.  Also note that
; this code is universal in that you can use it to detect the proximity of
; *ANY* unit type (east or west, infantry or vehicle), not just east soldiers.

_unitTypes = ["OfficerE","SoldierEMG","SoldierEB","SoldierEG","SoldierELAW"]

; Get the size of the _unitTypes array.

_i = count _unitTypes

#Check

; Decrement _i.

_i = _i - 1

; Get the nearest unit of a particular type.

_nearObj = NearestObject [_player,(_unitTypes select _i)]

; Check if _i < 1, and if so, reset its value.

? (_i < 1) : _i = count _unitTypes

; Check if the nearest unit of the indicated type is less than 50m from
; _player.  If so, then take some kind of action.

? _player distance _nearObj < 50 : goto "TakeAction"

; If we got here, then loop to check the next unit type.

goto "Check"

#TakeAction

; Put your code to make them respond here.

; When done, loop to "Check" to check for the next unit type.

goto "Check"
Ranger

g

  • Guest
Re:detect nearest east soldier
« Reply #2 on: 06 Jan 2003, 22:13:04 »
Hi Ranger

Thanks for the reply no time to test it yet but will let you know

G

Offline toadlife

  • OFPEC Old Skool
  • Former Staff
  • ****
  • Official OFP Editing Center Toad
    • toadlife.net
Re:detect nearest east soldier
« Reply #3 on: 07 Jan 2003, 20:27:49 »
Nearestobject only works within 25 meters or so, so it wouldn't work in this situation. I would use a trigger and a script to accomplish your goal.

Start by making a circular trigger with a width of 100 meters. Set it to "East Present" and "repeat". Leave the on activation and condition alone. Give the trigger a name, such as "eastlist".

Then use this script to find your nearest east dude.

SCRIPT REMOVED
« Last Edit: 07 Jan 2003, 23:12:20 by toadlife »
"Whenever you want information on the 'net, don't ask a question; just post a wrong answer." -- Cancer Omega.

Offline Ranger

  • Members
  • *
  • Hoo-ah!
Re:detect nearest east soldier
« Reply #4 on: 07 Jan 2003, 21:24:16 »
_c = (count list _trigger) - 1
_dis = 5000
while "(_c >= 0) && ((_trigger select _c) distance _player) < _dis" do {_nearest == _trigger select _c; _c = _c - 1}
?(count list _trigger) > 0:goto "action"
goto "movetrigger"

Just wondering, why did you set _dis to 5000?  Is that a typo and you meant to say 50, since he wants to detect an enemy within 50 meters.

Also, g wants to detect only infantry, according to his post.

@g: So, in order to check only for infantry, you'll have to combine Toadlife's idea with mine.  If you think you can figure out how to do that, great.  Otherwise, let me know and I'll help you again.
Ranger

Offline toadlife

  • OFPEC Old Skool
  • Former Staff
  • ****
  • Official OFP Editing Center Toad
    • toadlife.net
Re:detect nearest east soldier
« Reply #5 on: 07 Jan 2003, 21:55:56 »
Just wondering, why did you set _dis to 5000?  Is that a typo and you meant to say 50, since he wants to detect an enemy within 50 meters.

THe _dis variable is set to a number to begin with so that the distance to the first enemy checked in the list can be compared to something. THe first enemy checked allways becomes the closest enemy, and _dis is set to the distance to that enemy. After that, the next enemy is checked and if he is closer that the first, then _dis is set to the distance to him. The variable "_nearest" is the closest enemy, and is reset every time a closer enemy is found.
The width of the trigger dictates which soldiers are checked in the first place. With the trigger being 100m wide, only soldiers that are within 50m will be checked. To increase the "scan" range, all you would have to do is make the trigger bigger.

Also, g wants to detect only infantry, according to his post.
This updated version of the script should do the job. This script is not tested at all so there might be syntax errors in it (or might not work period!).

Code: [Select]
;Execution: [player,trigger] exec "thisscript.sqs"

_player = _this select 0
_trigger = _this select 1

#movetrigger
;moves trigger over _player
;
~2
_trigger setpos getpos player

#checklist
;checks the list of east units in the trigger
;if trigger contains no east units script returns to the "movetrigger" section
;if trigger does contain an east unit, the script finds the nearest one and moves to the "doaction" section
;
_c = (count list _trigger) - 1
_dis = 5000
while "(_c >= 0) && ((_trigger select _c) distance _player) < _dis && (({man} countype [_trigger select _c]) > 0)" do {_nearest = _trigger select _c;_dis = (_trigger select _c) distance _player;_c = _c - 1}
?((count list _trigger) > 0) && ("man" countype list _trigger > 0):goto "doaction"
goto "movetrigger"


#doaction

;insert code here - nearest enemy is "_nearest"

exit
« Last Edit: 07 Jan 2003, 23:07:42 by toadlife »
"Whenever you want information on the 'net, don't ask a question; just post a wrong answer." -- Cancer Omega.

Offline toadlife

  • OFPEC Old Skool
  • Former Staff
  • ****
  • Official OFP Editing Center Toad
    • toadlife.net
Re:detect nearest east soldier
« Reply #6 on: 07 Jan 2003, 21:59:49 »
NOte that I found a huge bug in the script when updating it to find only infantry. I updated both the old and the new script with the fix.
"Whenever you want information on the 'net, don't ask a question; just post a wrong answer." -- Cancer Omega.

g

  • Guest
Re:detect nearest east soldier
« Reply #7 on: 07 Jan 2003, 22:10:58 »
 :o Wow this is why i love this forum I go away for a day come back and loads of help.

Thanks Guys,

Just in case you want to know the script is for a surrender action, but I dont want people to surrender until there is someone to surrender to.
So I check for the enemy being close enough so you can stand up and wave the white flag!

Didnt know that nearest object only worked within 25 meters that explains why my original script didnt work until the guys were really close I just thought it was slow AI  ;D

G
« Last Edit: 07 Jan 2003, 22:28:07 by g »

Offline Ranger

  • Members
  • *
  • Hoo-ah!
Re:detect nearest east soldier
« Reply #8 on: 07 Jan 2003, 22:19:14 »
THe _dis variable is set to a number to begin with so that the distance to the first enemy checked in the list can be compared to something. THe first enemy checked allways becomes the closest enemy, and _dis is set to the distance to that enemy. After that, the next enemy is checked and if he is closer that the first, then _dis is set to the distance to him. The variable "_nearest" is the closest enemy, and is reset every time a closer enemy is found.

I must be blind, because I don't see anywhere in the code where _dis is set to anything *except* for a static 5000.  ???


Quote
while "(_c >= 0) && ((_trigger select _c) distance _player) < _dis && (({man} countype [_trigger select _c]) > 0)" do {_nearest == _trigger select _c; _c = _c - 1}

Aha, the countType command is exactly what I needed in my attempt at fixing the problem, but I didn't think of using it.

Shouldn't the == be =?

Quote
{_nearest == _trigger select _c; _c = _c - 1}
changed to
Quote
{_nearest = _trigger select _c; _c = _c - 1}
Ranger

Offline toadlife

  • OFPEC Old Skool
  • Former Staff
  • ****
  • Official OFP Editing Center Toad
    • toadlife.net
Re:detect nearest east soldier
« Reply #9 on: 07 Jan 2003, 23:10:41 »
Yeah Ranger, those are bugs you found. I fixed them in the second script.
"Whenever you want information on the 'net, don't ask a question; just post a wrong answer." -- Cancer Omega.

Offline toadlife

  • OFPEC Old Skool
  • Former Staff
  • ****
  • Official OFP Editing Center Toad
    • toadlife.net
Re:detect nearest east soldier
« Reply #10 on: 08 Jan 2003, 20:36:49 »
I did some testing last night and can't seem to kill a strange bug in the script. I'll give it another go tonight and post the exmaple mission here.
"Whenever you want information on the 'net, don't ask a question; just post a wrong answer." -- Cancer Omega.