Home   Help Search Login Register  

Author Topic: Ai moving to random positions in building?  (Read 1527 times)

0 Members and 1 Guest are viewing this topic.

Offline subroc

  • Members
  • *
Ai moving to random positions in building?
« on: 29 May 2006, 14:47:54 »
Can somebody help me with this?

Im making a "Kill the V.I.P" mission and the target will be in a building (the big town hall with lots of rooms..)
is there any script to get him to move randomly between different positions in the building.. like:
go to pos(rand#)
wait 5 sec
go to pos(rand#)
wait 13 sec
etc..

Offline 999chris

  • Members
  • *
  • I'm a llama!
Re: Ai moving to random positions in building?
« Reply #1 on: 29 May 2006, 14:57:30 »
I would do something like

Code: [Select]
#Loop
_num = random 5   // Ive set 5 but you can use this number for the different amount of positions you want avaliabe in the building
_wait = random 30 // If you want a random waiting time
// Below you set a position for each possible number that is by "_num = random 5"
?_num == 1: UnitName SetPos ####
?_num == 2: UnitName SetPos ####
?_num == 3: UnitName SetPos ####
?_num == 4: UnitName SetPos ####
?_num == 5: UnitName SetPos ####
~_wait
goto "Loop"

Probably totally long winded but Im still a begginner,..

Offline subroc

  • Members
  • *
Re: Ai moving to random positions in building?
« Reply #2 on: 29 May 2006, 16:39:53 »
thx
but the setPos command would teleport him between the locations i was thinking of using doMove instead.
what i really was looking for was if there is anyway to command a unit to a certain position in a building without using the menu in the waypoint window.

ive tried to put markers in the different locations, but the "lack of" collision detection makes the AI running in and out of walls it also seem unwilling to go up and down the stairs.

Offline bedges

  • Administrator
  • *****
    • OFPEC The Editing Center
Re: Ai moving to random positions in building?
« Reply #3 on: 29 May 2006, 17:11:55 »
there is indeed a command which will do what you want, but be prepared for some frustration and perhaps disappointment, because it doesn't always work.

from the COMREF:

building buildingPos index
Operand types:
    building: Object
    index: Number
Type of returned value:
    Array
Description:
    Returns given indexed position in building, returned value is in format Position

Example:
    buildingPos [building, 1]

so. switch on object ID, find the building, note its number and use

Code: [Select]
unit_name domove buildingpos [(object xyz), 1]
only experimentation will show how many building positions there are and which work. the flats in petrovice on nogova for example boast around 12 positions. 5 of them work, the rest don't.


Offline Baddo

  • Former Staff
  • ****
  • Reservist Jaeger
Re: Ai moving to random positions in building?
« Reply #4 on: 29 May 2006, 17:50:46 »
The buildingPos function does not work like that (official comref is wrong). Let me dig out an example for you...

Edit #1: Here:

Code: [Select]
_unit move (_building buildingPos _rpos)
Taken out of a script I once wrote for handling a bit similar situation. It's pretty easy for you to figure out how to use it in your own script, just make sure you understand that AI units are not good inside buildings, they will probably go to the "basement" very often, and go through walls every now and then.

Edit #2: OK I give you also this little function which I wrote to find out how many indexed positions there are inside a building:

Code: [Select]
// ******************************************************************************************

// buildingPosCount.sqf

// Returns number of indexed positions in a building.
// These positions can be used with the buildingPos function.
// When using indexed positions from a script,
// take in notice that 1st position has index number 0,
// 2nd has index number 1 and so on. So if a building
// has 8 positions, you can use them as 0...7 from a script
// with the buildingPos function.

// USAGE:

// init: buildingPosCount = preprocessFile "buildingPosCount.sqf"

// calling: <building> call buildingPosCount

// returns: <integer>

// example: _posses = nearestBuilding player call buildingPosCount

// Baddo 2005
// You can contact me through www.ofpec.com

// ******************************************************************************************

private ["_i"];

_i = 0;

while { format [{%1}, _this buildingPos _i] != "[0,0,0]" } do
{
_i = _i + 1;
};
_i

Read the comments in the start of the function carefully.

If I recall correctly, the nearestBuilding function only returns buildings that have these indexed positions inside them, so if you want to write a truly dynamic script which looks for buildings with indexed positions and order AI units into them, use nearestBuilding and buildingPosCount for the job to first figure out a building and then how many indexed positions there are in it.
« Last Edit: 29 May 2006, 19:19:13 by Baddo »

Komuna

  • Guest
Re: Ai moving to random positions in building?
« Reply #5 on: 29 May 2006, 18:33:17 »
Hi, Kristoffer!

Consider:
_unit::Object -- the soldier you're handling
_building::Object -- the building you want your soldier to enter
_idx::Integer -- the index of a position in a building, starting on 0
_pos::Array -- the map position representing a building position, which is retrieved from the index number.

Detect the closest enterable building to the unit you are handling:
Code: [Select]
_building = nearestBuilding _unit

Choose a building from the Mission Editor, and handle it by resorting to its object ID:
Code: [Select]
_building = object 123456
Get the coordinates of a position from an enterable building:
Code: [Select]
_pos = _building buildingPos _idxNote: whenever _pos is the map's origin -- [0,0,0] -- _idx doesn't exist in the referenced building. Also, if _idx is 0 and returns the origin, then that building has no house-positions defined (usualy means that it's not enterable).

Make the unit move to a defined position in a building:
Code: [Select]
_unit doMove _pos
There you go, now you have all the tools you need to make your unit move inside buildings. ;)

Examples

Force a certain unit to move inside the nearest building:
Code: [Select]
;Consider the argument to this script is a Man class AI vehicle
_unit = _this
_pos = (nearestBuilding _unit) buildingPos 0
;Check if position exists
? format["%1",_pos] == "[0,0,0]" : exit
_unit doMove _pos

Force a certain unit to randomly move inside a building every 10~20 seconds:
Code: [Select]
;Consider the arguments to this script are a Man class AI vehicle and an enterable House class vehicle
_unit = _this select 0
_building = _this select 1

;Count positions existant in the building
_cnt = 0
while {format["%1",_building buildingPos _cnt] != "[0,0,0]"} do {_cnt = _cnt + 1}

;Fails if building is invalid
? _cnt == 0 : exit

;A function to return a random index, given the count
_rnd = {private "_r"; _r = random _this; _r - (_r % 1)}

;Make the unit move around
#move_unit
_unit doMove (_building buildingPos (_cnt call _rnd))
~10+(random 10)
? alive _unit : goto "move_unit"
exit

Note: the examples were not tested

#Edit
Sorry, Baddo; didn't see your post. :-[

Vincent8099

  • Guest
Re: Ai moving to random positions in building?
« Reply #6 on: 29 May 2006, 18:57:30 »
Try it out with the empty marker or a  trigger, call it hide1, hide2, hide3.....etc.  Place the maker above the building and go look into your mission where the unit is standing (adjust). If you use the trigger, you could use the setpos getpos settings in the trigger(above a building, go look, and adjust your trigger placement or unit). Don't forget to sinchronise those markers with your unit!!

Vincent8099

  • Guest
Re: Ai moving to random positions in building?
« Reply #7 on: 29 May 2006, 19:09:28 »
Sorry you want to make the unit WALK between those locations, well,  now you have a random startpos, tataaaaaa

Offline Baddo

  • Former Staff
  • ****
  • Reservist Jaeger
Re: Ai moving to random positions in building?
« Reply #8 on: 29 May 2006, 19:24:22 »
Komuna, your reply supplements and confirms my reply so  :)

I also made a small optimization to my function from what I saw you wrote. For some odd reason I originally had put the [0,0,0] behind a format call - I recall I had some troubles with no format for the nullpos but that must've been something else wrong back then when I played with this.

Offline subroc

  • Members
  • *
Re: Ai moving to random positions in building?
« Reply #9 on: 30 May 2006, 08:55:24 »
thanks!

i will try this when i get home from work.
i know that the collision detection sucks but as the target will be put out his misery by longrange sniping
i hope that the player wonÂ't notice that heÂ's walking through walls inside the house