Home   Help Search Login Register  

Author Topic: Distance Trig and Array combo  (Read 6395 times)

0 Members and 1 Guest are viewing this topic.

Offline eegore

  • Members
  • *
Distance Trig and Array combo
« on: 09 Feb 2014, 20:06:22 »

  I have a mission where I am having a player run up to goats and they get chased back to the barn.

player distance goat1 < 3

goat1 domove getposATL barn


  Now if I have 30 goats I need 30 separate triggers.  Is there a way to put these goats into an array so I can have just one trigger?

goats = [goat1,goat2,goat3] would create an array right? But then what?

  Thank you and welcome back!

Offline h-

  • OFPEC Site
  • Administrator
  • *****
  • Formerly HateR_Kint
    • OFPEC
Re: Distance Trig and Array combo
« Reply #1 on: 10 Feb 2014, 08:01:53 »
Do you want it to work so that each goat is herded separately or as a group?

In any case, you have to create them by scripting (createUnit), otherwise they do not respond to doMove commands.

And the herding should be done via scripting.

EDIT:
Ok, so, create your goats with createUnit and put them into an array.
Tested this by creating the goats into player's group and then unjoining them (goats join grpNull).

Then:
Code: [Select]
[goats,3,barn] execVM "herder.sqf"
The script:
Code: (herder.sqf) [Select]
private["_goats","_dist","_goat","_barn"];

_goats = _this select 0; //goats
_dist = _this select 1; //distance how close player must be
_barn = _this select 2; // barn, can be object or position
if (typeName _barn == "OBJECT") then {_barn = position _barn};

//variable init
_goat = nil;
_goatsHome = [];

while {(count _goats) > 0} do {
sleep 1;
for [{_i=0},{_i<(count _goats)},{_i=_i+1}] do {
_goat = _goats select _i;

if (((player distance _goat)<_dist)) exitWith {
_rX = random 6;
if ((random 100) <= 50) then {_rX = random (-6)};
_rY = random 6;
if ((random 100) <= 50) then {_rY = random (-6)};
_rD = random 360;

_barn = [(_barn select 0)+_rX*sin(_rD),(_barn select 1)+_rY*cos(_rD)];

_goat doMove _barn;
_goatsHome + [_goat];
_goats = _goats - [_goat];
};
};
};
« Last Edit: 10 Feb 2014, 09:28:05 by h- »
Project MCAR   ---   Northern Fronts   ---   Emitter 3Ditor
INFORMATIVE THREAD TITLES PLEASE. "PLEASE HELP" IS NOT ONE..
Chuck Norris can divide by zero.

Offline eegore

  • Members
  • *
Re: Distance Trig and Array combo
« Reply #2 on: 10 Feb 2014, 10:24:05 »

  Thank you, I will try this out.

  The goats are spread out and the player just has to run up to them individually and they will get scared back to the barn.  That's pretty much all I know how to do. 

  I originally tried creating them and using the Urban Patrol Script so they aren't all just standing facing the same direction but they won't respond to further commands after UPS is activated.

  At this point I'd be happy if I could put multiple goats/sheep/cows around a farm and just have them run back to the barn without needing a separate trigger for each animal.

Offline eegore

  • Members
  • *
Re: Distance Trig and Array combo
« Reply #3 on: 12 Feb 2014, 10:45:42 »

  Ive been working on this but I can't figure some parts out.

  I have a working spawn trigger that spawns goats at various markers.  They are boring no-move goats but they are there.

  I have a trigger on a delay to fire after the goats spawn with:

Con: goats = [goat1,goat2,goat3,goat4,goat5]

  I think this makes the array.

  I am trying to get the third trigger to call the "goatherder.sqf" script but I can't figure that part out. 

  If I put:  [goats,3,barn] execVM "herder.sqf I of course get the "Type script expected Nothing error.  So I don't know where the above code goes. I've tried in mission init, in various locations in the other scripts etc. but nothing makes the goats move or join the player etc.

  Any help is appreciated.


 

Offline h-

  • OFPEC Site
  • Administrator
  • *****
  • Formerly HateR_Kint
    • OFPEC
Re: Distance Trig and Array combo
« Reply #4 on: 12 Feb 2014, 19:26:55 »
I'll post you a test mission as soon as I get back to my PC..
It should make things clearer.

EDIT:
Actually you're doing everything right I guess, just that if you use triggers for executing the script you need to use something like:
Code: [Select]
void=[goats,3,barn] execVM "herder.sqf"in scripts you don't have to do that.

Anyways, I attached my test mission for this script. It's made in A2-OA combo so if you're using vanilla A2 I'm not sure if it works or not.
Everything is done in init.sqf, not in scripts.
« Last Edit: 12 Feb 2014, 22:06:13 by h- »
Project MCAR   ---   Northern Fronts   ---   Emitter 3Ditor
INFORMATIVE THREAD TITLES PLEASE. "PLEASE HELP" IS NOT ONE..
Chuck Norris can divide by zero.

Offline eegore

  • Members
  • *
Re: Distance Trig and Array combo
« Reply #5 on: 14 Feb 2014, 07:40:03 »

  This test mission helps.  The only difference between it and the way I have spawned a cow from a borrowed script is that the cow will move about like a normal cow.

Code: [Select]
//Create a cow called cow1
_cow1 = _group createUnit ["cow04", (getMarkerPos "cow_marker"), [], 0, "FORM"];
_VarName = "cow1";
_cow1 SetVehicleVarName _VarName;
_cow1 Call Compile Format ["%1=_This ; PublicVariable ""%1""",_VarName];
_cow1 setVehicleInit "cow1 = this; this allowdamage false; this disableAI ""FSM""; this disableAI ""MOVE"";";

  I believe its the last line where disable AI is written that makes it start walking around.  Since I didn't write it up myself I don't know all the commands but if I could get these goats to mill about feeding then the mission would be more realistic.

Offline h-

  • OFPEC Site
  • Administrator
  • *****
  • Formerly HateR_Kint
    • OFPEC
Re: Distance Trig and Array combo
« Reply #6 on: 14 Feb 2014, 09:51:53 »
I don't understand why that piece of code would make the cows move about as it completely disables all movement abilities from them, they will not respond even to scripted moveto commands if disableAI "move" is used..

Although that whole bunch of code is written for MP so I'm not sure it even has any functionality in SP.
If you are doing this for MP then this whole thread is in the wrong board  :whistle:
What group does the _group point to in that code?



Anyways, fixed the herder.sqf a bit:
Code: [Select]
private["_animals","_dist","_animal","_barn","_animalsHome","_count"];

_animals = _this select 0; //animals
_dist = _this select 1; // distance how close player must be from gievn animal
_barn = _this select 2; // barn, can be object or position
if (typeName _barn == "OBJECT") then {_barn = position _barn};

//variable init
_animal = nil;
_animalsHome = [];
_count = count _animals;

while {(count _animals) > 0} do {
sleep 1;
for [{_i=0},{_i<(count _animals)},{_i=_i+1}] do {
_animal = _animals select _i;

if (((player distance _animal)<_dist)) exitWith {
//very bad maths to try to randomize the moveto position
_rX = random 6;
if ((random 100) <= 50) then {_rX = random (-6)};
_rY = random 6;
if ((random 100) <= 50) then {_rY = random (-6)};
_rD = random 360;

_barn = [(_barn select 0)+_rX*sin(_rD),(_barn select 1)+_rY*cos(_rD)];

//move animal, if it's alive..
_animal doMove _barn;
// add animal to an array of animals that have been herded
_animalsHome + [_animal];
//remove herded animal from the original non-herded animals
_animals = _animals - [_animal];
};
};
};
Project MCAR   ---   Northern Fronts   ---   Emitter 3Ditor
INFORMATIVE THREAD TITLES PLEASE. "PLEASE HELP" IS NOT ONE..
Chuck Norris can divide by zero.

Offline eegore

  • Members
  • *
Re: Distance Trig and Array combo
« Reply #7 on: 14 Feb 2014, 17:30:58 »

  This is a SP mission.  Just one for my friend to have fun with. 

  The beginning of the script looks like this:

Code: [Select]
//Hide variables from other scripts.
private
["_sideHQ", "_group", "_cow1", "_cow2", "_cow3", "_cow4", "_trigger1", "_trigger2", "_trigger3", "_trigger4"];

//Create a group to spawn cows from
_SideHQ = createCenter Civilian;
sleep 0.1;
_group = createGroup Civilian;

sleep 0.1;

  There is no gap between "private" and [_sideHQ" but for some reason the text is forcing a new line on here. 

  Movement is re-enabled on a trigger:
Code: [Select]
//Create a trigger to get the cow to move to the Barn when player is close.
_trigger1 = createTrigger ["EmptyDetector", getpos cow1];
_trigger1 setTriggerArea [0, 0, 0, false];
_trigger1 setTriggerTimeout [0, 0, 0, true];
_trigger1 setTriggerActivation ["NONE", "PRESENT", false];
_trigger1 setTriggerStatements ["((player distance cow1) <=5)", "cow1 enableAI ""FSM""; cow1 enableAI ""MOVE""; cow1 doMove (getMarkerPos ""barn"")", ""];

  Even though the cow AI is disabled the cow still moves a little bit, eating etc. instead of standing like a stone cow motionless like the goats do.  Could just be the built in animations.

  The less efficient method with using the triggers is that I need to have multiple "barn" markers so they don't all stack up on top of each other.  Your herder script has the randomized final destination so one "barn" will be good enough.
 

Offline h-

  • OFPEC Site
  • Administrator
  • *****
  • Formerly HateR_Kint
    • OFPEC
Re: Distance Trig and Array combo
« Reply #8 on: 14 Feb 2014, 18:42:26 »
Quote
The beginning of the script looks like this:
Ah, right, completely forgot that you could create groups out of thin air via scripting.  :-[

So instead of spawning the animals in the player's group you could do what's done there and create the group in the init.sqf.

Quote
Could just be the built in animations.
They are, dunno if there's something broken with the goats as they don't do much of anything. Sometimes they turn their heads but that's about it..

You could script them to walk about though.
Project MCAR   ---   Northern Fronts   ---   Emitter 3Ditor
INFORMATIVE THREAD TITLES PLEASE. "PLEASE HELP" IS NOT ONE..
Chuck Norris can divide by zero.

Offline h-

  • OFPEC Site
  • Administrator
  • *****
  • Formerly HateR_Kint
    • OFPEC
Re: Distance Trig and Array combo
« Reply #9 on: 17 Feb 2014, 19:48:46 »
Quote
Your herder script has the randomized final destination so one "barn" will be good enough.
About this, I updated the herder script a little, the random destination maths had a bit of unnecessary code in it..

Code: (herder.sqf) [Select]
private["_animals","_dist","_animal","_barn","_animalsHome","_count"];

_animals = _this select 0; //animals
_dist = _this select 1; // distance how close player must be from gievn animal
_barn = _this select 2; // barn, can be object or position
if (typeName _barn == "OBJECT") then {_barn = position _barn};

//variable init
_animal = nil;
_animalsHome = [];
_count = count _animals;

while {(count _animals) > 0} do {
sleep 1;
for [{_i=0},{_i<(count _animals)},{_i=_i+1}] do {
_animal = _animals select _i;

if (((player distance _animal)<_dist)) exitWith {
//randomize the moveto position
_rX = random 6;
_rY = random 6;
_rD = random 360;

_barn = [(_barn select 0)+_rX*sin(_rD),(_barn select 1)+_rY*cos(_rD)];

//move animal, if it's alive..
_animal doMove _barn;
// add animal to an array of animals that have been herded
_animalsHome + [_animal];
//remove herded animal from the original non-herded animals
_animals = _animals - [_animal];
};
};
};
Project MCAR   ---   Northern Fronts   ---   Emitter 3Ditor
INFORMATIVE THREAD TITLES PLEASE. "PLEASE HELP" IS NOT ONE..
Chuck Norris can divide by zero.

Offline eegore

  • Members
  • *
Re: Distance Trig and Array combo
« Reply #10 on: 22 Feb 2014, 02:36:49 »

  Ok so I attempted to modify the previous "herder" script (which works great) so they would go to a Game logic object called "Shed" instead of barn.  This is so I can send cows to the barn.

  I altered all  _barn to _shed in the script but they still haul ass to the barn.  I can' figure out what I am missing.

  Init has no mention of the game logic:

Code: [Select]
// create goats. Using player's group
goat1 = (group player) createUnit ["Goat",position gpos1, [], 0, "NONE"];
goat2 = (group player) createUnit ["Goat",position gpos2, [], 0, "NONE"];
goat3 = (group player) createUnit ["Goat",position gpos3, [], 0, "NONE"];
goat4 = (group player) createUnit ["Goat",position gpos4, [], 0, "NONE"];
goat5 = (group player) createUnit ["Goat",position gpos5, [], 0, "NONE"];

//un-join goats from player's group
{[_x] join grpNull} forEach [goat1,goat2,goat3,goat4,goat5];
sleep 1;

//execute herding
[[goat1,goat2,goat3,goat4,goat5],3,barn] execVM "herder2.sqf";


  Here is the script with _shed replacing _barn:

Code: [Select]
// goat to shed script
private["_animals","_dist","_animal","_shed","_animalsHome","_count"];

_animals = _this select 0; //animals
_dist = _this select 1; // distance how close player must be from given animal
_shed = _this select 2; // shed, can be object or position
if (typeName _shed == "OBJECT") then {_shed = position _shed};

//variable init
_animal = nil;
_animalsHome = [];
_count = count _animals;

while {(count _animals) > 0} do {
sleep 1;
for [{_i=0},{_i<(count _animals)},{_i=_i+1}] do {
_animal = _animals select _i;

if (((player distance _animal)<_dist)) exitWith {
//very bad maths to try to randomize the moveto position
_rX = random 6;
if ((random 100) <= 50) then {_rX = random (-6)};
_rY = random 6;
if ((random 100) <= 50) then {_rY = random (-6)};
_rD = random 360;

_shed = [(_shed select 0)+_rX*sin(_rD),(_shed select 1)+_rY*cos(_rD)];

//move animal, if it's alive..
_animal doMove _shed;
// add animal to an array of animals that have been herded
_animalsHome + [_animal];
//remove herded animal from the original non-herded animals
_animals = _animals - [_animal];
};
};

Offline h-

  • OFPEC Site
  • Administrator
  • *****
  • Formerly HateR_Kint
    • OFPEC
Re: Distance Trig and Array combo
« Reply #11 on: 22 Feb 2014, 06:45:05 »
You don't have to change the script in any way.
The variables, like _barn, in the script are local to the script and have nothing to do with the object in the game named 'barn'.


So you can call the destination anything you like, you can feed just a position into it instead of a object and you don't need to change a thing in the script itself. You just execute the script by passing the desired destination, for example:
Code: [Select]
[[goat1,goat2,goat3,goat4,goat5],3,barn] execVM "herder.sqf"; //object called barn
[[goat1,goat2,goat3,goat4,goat5],3,shed] execVM "herder.sqf"; //object called shed
[[goat1,goat2,goat3,goat4,goat5],3,moon] execVM "herder.sqf"; //object called moon
[[goat1,goat2,goat3,goat4,goat5],3,[3613.83,3638.12,0]] execVM "herder.sqf"; //some position
[[goat1,goat2,goat3,goat4,goat5],3,position ((position player) nearestobject 5146)] execVM "herder.sqf"; //building with ID 5146

Also, if you want to have different kind of animals to go to a different locations you can execute several instances of the scripts, like:
Code: [Select]
[[goat1,goat2,goat3,goat4,goat5],3,barn] execVM "herder.sqf";
[[cow1,cow2,cow3],3,shed] execVM "herder.sqf";
Project MCAR   ---   Northern Fronts   ---   Emitter 3Ditor
INFORMATIVE THREAD TITLES PLEASE. "PLEASE HELP" IS NOT ONE..
Chuck Norris can divide by zero.

Offline eegore

  • Members
  • *
Re: Distance Trig and Array combo
« Reply #12 on: 22 Feb 2014, 10:24:50 »

  Ahh I see where I was screwing up.  I didn't notice the location dictated in the init. 

  So if I toss in three separate Game Logics I can use the same script to send them to the shed even if it says _barn?

  I will test this out.

Offline h-

  • OFPEC Site
  • Administrator
  • *****
  • Formerly HateR_Kint
    • OFPEC
Re: Distance Trig and Array combo
« Reply #13 on: 22 Feb 2014, 14:50:40 »
Quote
So if I toss in three separate Game Logics I can use the same script to send them to the shed even if it says _barn?
Yes.
Project MCAR   ---   Northern Fronts   ---   Emitter 3Ditor
INFORMATIVE THREAD TITLES PLEASE. "PLEASE HELP" IS NOT ONE..
Chuck Norris can divide by zero.

Offline eegore

  • Members
  • *
Re: Distance Trig and Array combo
« Reply #14 on: 22 Feb 2014, 19:22:08 »

  Ok it looks good then.

  The scripts in the posts above work perfectly.

  Thanks for the help.

Offline eegore

  • Members
  • *
Re: Distance Trig and Array combo
« Reply #15 on: 24 Feb 2014, 06:03:53 »

  The last portion of this mission is to have a trigger activate when all 5 goats are found or near the barn.

  I think I can have one trigger contain a condition that all 5 goats are near one Game Logic right?

Offline h-

  • OFPEC Site
  • Administrator
  • *****
  • Formerly HateR_Kint
    • OFPEC
Re: Distance Trig and Array combo
« Reply #16 on: 24 Feb 2014, 06:58:16 »
Yes.

Set the trigger to activate from civilian side (animals are civilians), set the correct size for it (something like 10x10 is probably enough), set to activate repeatedly and in the condition field
Code: [Select]
this && {_x in thisList} count goats == 5
Should trigger only when the 5 goats are in.
Note that I use array stored in variable goats (goats = [goat1, goat2, etc ]) instead of writing the array manually, just to make it a bit more tidy.
« Last Edit: 24 Feb 2014, 07:00:04 by h- »
Project MCAR   ---   Northern Fronts   ---   Emitter 3Ditor
INFORMATIVE THREAD TITLES PLEASE. "PLEASE HELP" IS NOT ONE..
Chuck Norris can divide by zero.

Offline eegore

  • Members
  • *
Re: Distance Trig and Array combo
« Reply #17 on: 25 Feb 2014, 01:50:55 »

  Ok I have the trigger set for Civilian present Repeated but it never goes off.

  I also tried it reverse as Not Present.

  Not sure whats up, the only alteration I did to the script was the goat classnames.

Offline h-

  • OFPEC Site
  • Administrator
  • *****
  • Formerly HateR_Kint
    • OFPEC
Re: Distance Trig and Array combo
« Reply #18 on: 25 Feb 2014, 07:16:24 »
Here's the test mission I've used.
It also includes yet again updated herder.sqf (removed more useless code from it).
Project MCAR   ---   Northern Fronts   ---   Emitter 3Ditor
INFORMATIVE THREAD TITLES PLEASE. "PLEASE HELP" IS NOT ONE..
Chuck Norris can divide by zero.

Offline eegore

  • Members
  • *
Re: Distance Trig and Array combo
« Reply #19 on: 26 Feb 2014, 03:48:08 »

  I realize my init had no "civilian" side for the goats like the current init.

  The trigger works fine now.