Home   Help Search Login Register  

Author Topic: Selecting a random target (SOLVED)  (Read 2336 times)

0 Members and 1 Guest are viewing this topic.

Offline nominesine

  • Former Staff
  • ****
  • I'm NOT back!
    • The IKB Forum
Selecting a random target (SOLVED)
« on: 21 May 2008, 17:45:37 »
I'm trying to make a script that selects a random target for a likewise random AI unit. I need it to be very general in nature, so I can attach it to any unit after mission start. The script is run from a trigger and starts by identifying a shooter:

Code: [Select]
_shooter = _this select 0
The problem starts when I want to find a random victim for my random shooter. I tried with this, but obviously it didn't work:

Code: [Select]
_victim = nearestObject [_shooter, "man"]
Im not familiar with arrays or the nearestObject-familly of commands. Can someone who is help me. I need to return the name of a nearby unit (anyone will do), close to the shooter-unit, as my intended victim. I need an object that can be doTargeted and doFired upon.



EDIT: Maybe I was a bit unclear. This is what I've been testing (ad hoc code):

Code: [Select]
_shooter = this select 0

_targetList = [_shooter nearTargets 100]
~1
_shooter doTarget (_targetList select 0)
~1
_shooter doFire (_targetList select 0)

The problem is that I don't know how to use arrays. The nearTargets command returns a list of possible targets within 100 meters. But how do I select one of them, and covert it into an object that can be doTargeted by _shooter?
« Last Edit: 23 May 2008, 23:11:45 by nominesine »
OFPEC | Intel Depot
RETARDED Ooops... Retired!

Offline Rommel92

  • Members
  • *
Re: Selecting a random target
« Reply #1 on: 22 May 2008, 21:57:06 »
Ok, to understand the idea first, you want a simulated madman in a city killing randoms closest to him or something similar? ( :whistle: )

Code: [Select]
_shooter = this select 0

_targetList = [_shooter nearTargets 100]
~1
_shooter doTarget (_targetList select 0)
~1
_shooter doFire (_targetList select 0)

What error does this return?

Offline nominesine

  • Former Staff
  • ****
  • I'm NOT back!
    • The IKB Forum
Re: Selecting a random target
« Reply #2 on: 22 May 2008, 22:10:31 »
Ok, to understand the idea first, you want a simulated madman in a city killing randoms closest to him or something similar? ( :whistle: )


Yes. Am I that predictable  :-[

The error message reads as follows:

doTarget: Any; expected array, Object
doFire: Any; expected array, Object


Bear in mind though: I haven't got a clue on how to script this. I'm far from sure that the nearTarget command is the right way to go. That's why I refered to my little script as an ad hoc code.
OFPEC | Intel Depot
RETARDED Ooops... Retired!

Offline Rommel92

  • Members
  • *
Re: Selecting a random target
« Reply #3 on: 22 May 2008, 22:25:06 »
Code: [Select]
[[[2534.75,2551.16,1.34027],"SoldierEB",EAST,440000,EAST1-1-A:1,0.0340452]]]Returned from a simple hint format ["%1",player nearTargets 100];

So it would have to be:

Code: [Select]
_shooter = _this select 0;
_i = 0;

While {alive _shooter} do
{
_targetList = [_shooter nearTargets 100];
_target = _targetList select 0;
_shooter doTarget (_target select 4);
_shooter doFire (_target select 4);
WaitUntil {!alive _target select 4};
sleep 2.0;
_i = _i + 1;
};

However this does not work in game... I got to go, will try and help you with this later mate...
« Last Edit: 22 May 2008, 22:28:59 by Rommel92 »

Offline Wolfrug

  • Addons Depot
  • Former Staff
  • ****
  • Official OFPEC Old Timer
Re: Selecting a random target
« Reply #4 on: 22 May 2008, 22:36:11 »
Now...here's the easiest way I can think of off the cuff, since I haven't got ArmA with me to verify.

The command findNearestEnemy is one of those excellent little things that came with ArmA, and should be able to serve you here. Basically what it does is find the closest enemy to the unit that the unit KNOWS about. So no madman going crazy shooting at people behind a building or out of sight. Now, it only finds enemies, which might be a bit of a problem - unless you turn the guy to sideEnemy, upon which EVERYONE will be his enemies. Turning anyone to sideEnemy is as simple as addrating -10000.

Now the thing is, as sideEnemy there's a good chance he'll go crazy all on his own, so there's really no need to do the extra dofire/dotarget thing, but if not, you could try doing something like:

Code: [Select]
_victim = madMan findNearestEnemy (position madMan);
madMan dotarget _victim;
madMan dofire _victim;

Repeat at will.

You could also try messing with the nearTargets command, since it returns every kind of unit from all sides that your guy knows about, but you'd have to create a special parsing portion of the script that removes all the extra information and only returns the actual unit :)

Here's the link to the nearTargets command: nearTargets

Basically what it returns is an array of arrays, the enemy of all sane ArmA scripters. So it looks sort of like this:

[Known guy 1 array, known guy 2 array, known guy 3 array....]

And each known guy array looks something like:

[Position, Type, Side, Subjective Cost, Object]

So to access for instance the first actual UNIT in a neartargets array, you'd have to do something like:

Code: [Select]
_mainarray = madMan nearTargets 100;
_mainArrayDude1 = ((_mainArray select 0) select 4);
madMan dotarget _mainArrayDude1;
madMan dofire _mainarraydude1;

Of course, the nearTargets array isn't constructed based on distance to target (so you could in fact be targeting the person the furthest away by select the first (0) element in the array). So what you'd have to do is first parse through the whole nearTargets array, compare distances, and then select one.

From that point of view, the easiest is simply to mess with the sides and stuff :) (make all civilians RACS by grouping them with a non-existent RACS leader, then make the madman OPFOR...or something).

Good luck anyway! I do hope your next ouevre will not require Queens Gambit! Would love to play some ol' nominesine again! :D

Wolfrug out.
« Last Edit: 23 May 2008, 01:23:46 by Wolfrug »
"When 900 years YOU reach, look as good you will not!"

Offline nominesine

  • Former Staff
  • ****
  • I'm NOT back!
    • The IKB Forum
Re: Selecting a random target
« Reply #5 on: 22 May 2008, 23:35:29 »
Thanks you, both of you. I start to understand how nearTargets work now. It returns an array where each individual target is included as a sub-array. These sub-arays cannot be converted into objects, as far as I can see. A this select 4 directed at the sub-arrays within the main array only returns object type, not the object itself. Crap!

Looking forward to your advice. Maybe I'll need a different approach. Or can you really solve this Rommel???

EDIT: I solved the problem. I was playing with the wrong command, as I suspected. This is my current script (tested and working)

Code: [Select]
? !(local server): exit

_shooter = _this select 0

_shooter addMagazine "8Rnd_9x18_Makarov"
_shooter addWeapon "Makarov"


#killingSpree

_targetList = nearestObjects [_shooter, ["man"], 150]
_target = _targetList select 1

_shooter doTarget _target
_shooter doFire _target
~1

? !(alive _target): _target = nil
~1
goto "killingSpree"
« Last Edit: 22 May 2008, 23:49:29 by nominesine »
OFPEC | Intel Depot
RETARDED Ooops... Retired!

Offline Mr.Peanut

  • Former Staff
  • ****
  • urp!
Re: Selecting a random target
« Reply #6 on: 23 May 2008, 16:45:22 »
AFAIK, nearTargets is a command intended for use in FSMs. One other note. You no longer need a logic named "server". You can now use the ArmA command isServer instead.
urp!

Offline nominesine

  • Former Staff
  • ****
  • I'm NOT back!
    • The IKB Forum
Re: Selecting a random target
« Reply #7 on: 23 May 2008, 16:58:14 »
You no longer need a logic named "server". You can now use the ArmA command isServer instead.

I suspected that, but wasn't sure. I think I'll stick to my old method though. Makes copying/pasting from my old OFP archive easier.
OFPEC | Intel Depot
RETARDED Ooops... Retired!