Jump to content
Sign in to follow this  
hogmason

spawn vehicles at random distance from marker

Recommended Posts

ive got this code that spawn my AA but im stuck on trying to get them to spawn around the map and not on the center

of my spawn marker.

Ive tried implementing in hundreds of ways something like this

_aadis = [200,500,1000,2000,3000,4000,5000] call BIS_fnc_selectRandom;

but i cant seem to get that to work in the code

this is my code any ideas how i can get the above code to work in the following code to let the aa spawn at random distances


       _grp = createGroup east;
           _aapod = createVehicle ["Igla_AA_pod_East", getmarkerpos "center", [], 0, "NONE"]; 
           _aapod setDir 290;
           _dir =  290;			
           _aapod addEventHandler["killed", { (_this select 0) spawn AALoop; }];

	    _gunner = _grp createUnit ["RU_Soldier_Crew", [0,0,1], [], 0, "CAN_COLLIDE"];
           _gunner assignAsGunner _aapod;
           _gunner moveInGunner _aapod; 

---------- Post added at 10:33 ---------- Previous post was at 10:05 ----------

i just tried this still no luck


       _grp = createGroup east;
           //_aapod = createVehicle ["Igla_AA_pod_East", getmarkerpos "center", [], 0, "NONE"]; 
	    _distances = [5,10,35,50,65,80,90] call BIS_fnc_selectRandom;
        _ang = random 360;  
        _dis = _distances; 
        _dx = sin(_ang)*_dis; 
        _dy = cos(_ang)*_dis; 
		_aapod = createVehicle ["Igla_AA_pod_East", [((getmarkerpos "center") select 0) + _dx, ((getmarkerpos "center") select 1) + _dy, 0], [], 0, "NONE"];

---------- Post added at 11:40 ---------- Previous post was at 10:33 ----------

any ideas guys

Share this post


Link to post
Share on other sites

I'll take a stab, even though I can't test right now.

The createVehicle array you are using allows for radius from the center of the given position, in which case you are using 0.

What would happen if you did this:

_aadis = [200,500,1000,2000,3000,4000,5000] call BIS_fnc_selectRandom;

_aapod = createVehicle ["Igla_AA_pod_East", getmarkerpos "center", [], _aadis, "NONE"];

See this post for reason why second example you gave does not work:

Tajin's explanation

Are you using the -showScriptErrors start up parameter?

Edited by panther42

Share this post


Link to post
Share on other sites

cheers guys,

So i done kylania's tip with shk_pos but still not working i think i done it wrong this is what i done highlighted in red

           [color="#FF0000"] 
             _aadis = [200,500,1000,2000,3000,4000,5000] call BIS_fnc_selectRandom;[/color]
      _aapod = createVehicle ["Igla_AA_pod_East", getmarkerpos "center", [], 0, "NONE"];
             _aapod setDir random 290;
             _dir = 290;	
		[color="#FF0000"]
             _aapod setVehicleInit "_pos = [getMarkerPos "center", _aadis, random 359, false, [0, 0]] call SHK_pos;"
             processInitCommands;			 [/color]
             _aapod addEventHandler["killed", { (_this select 0) spawn AALoop; }];

           _gunner = _grp createUnit ["RU_Soldier_Crew", [0,0,1], [], 0, "CAN_COLLIDE"];
           _gunner assignAsGunner _aapod;
           _gunner moveInGunner _aapod; 
    hint "aa spawned";


Share this post


Link to post
Share on other sites

shk_pos simply returns a position, so you'd use it before and within the createVehicle command.

_aadis = [200,500,1000,2000,3000,4000,5000] call BIS_fnc_selectRandom;
_pos = [getMarkerPos "center", _aadis, random 359, false, [0, 0]] call SHK_pos;	      
_aapod = createVehicle ["Igla_AA_pod_East", _pos, [], 0, "NONE"];
_aapod setDir random 290;
_aapod addEventHandler["killed", { (_this select 0) spawn AALoop; }];

Though as panther said you can just do this as well:

_aadis = [200,500,1000,2000,3000,4000,5000] call BIS_fnc_selectRandom;
_aapod = createVehicle ["Igla_AA_pod_East", getMarkerPos "center", [], _aadis, "NONE"];
_aapod setDir random 290;
_aapod addEventHandler["killed", { (_this select 0) spawn AALoop; }];

Not sure you need the setDir since it's a turret though. :)

Share this post


Link to post
Share on other sites

thanks mate.

i tried this but it diddnt work

_aadis = [200,500,1000,2000,3000,4000,5000] call BIS_fnc_selectRandom;
_aapod = createVehicle ["Igla_AA_pod_East", getMarkerPos "center", [], _aadis, "NONE"];
_aapod setDir random 290;
_aapod addEventHandler["killed", { (_this select 0) spawn AALoop; }];


Share this post


Link to post
Share on other sites

Try this mate... tested and working.

_centerpos = getmarkerpos "center";
_aadis = [200,500,1000,2000,3000,4000,5000] call BIS_fnc_selectRandom;
_aadir = random 360;
_aapos = [(_centerpos select 0) + sin(_aadir)*_aadis,(_centerpos select 1) + cos(_aadir)*_aadis];
_aapod = createVehicle ["Igla_AA_pod_East", _aapos, [], 0, "NONE"];
_aapod setdir (random 290);

...then add your eventhandler.

EDIT: To test. Do this.... replace...

_aadis = [200,500,1000,2000,3000,4000,5000] call BIS_fnc_selectRandom;

with

_aadis = [30] call BIS_fnc_selectRandom;

That way you should see the Igla spawn 30m from the "center" and know if it is working or not.

**Note** you are going to have problems with stuff spawning in water, on or in buildings etc.

Edited by twirly
Clarity

Share this post


Link to post
Share on other sites

thanks guys all those work perfectly after twirly's comment on issues with spawning in things i.s buildings / water i have decided maybe shk_pos be the best option.

But i have one last question

is there away to set a distance between each aa gun so they dont spawn next to each other i.e maybe check to see no other gun is within 1000m.

This is the codes that work from u guys

_aadis = [200,500,1000,2000,3000,4000,5000] call BIS_fnc_selectRandom;
_pos = [getMarkerPos "center", _aadis, random 359, false, [0, 0]] call SHK_pos;	      
_aapod = createVehicle ["Igla_AA_pod_East", _pos, [], 0, "NONE"];
_aapod setDir random 290;
_aapod addEventHandler["killed", { (_this select 0) spawn AALoop; }];

_centerpos = getmarkerpos "center";
_aadis = [200,500,1000,2000,3000,4000,5000] call BIS_fnc_selectRandom;
_aadir = random 360;
_aapos = [(_centerpos select 0) + sin(_aadir)*_aadis,(_centerpos select 1) + cos(_aadir)*_aadis];
_aapod = createVehicle ["Igla_AA_pod_East", _aapos, [], 0, "NONE"];
_aapod setdir (random 290); 

Share this post


Link to post
Share on other sites

Also why is the below code aa.sqf working but when i modify it for another code it doesnt

so i use this code


///////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////                                                           ////////////////////////////////////
///////////////////////    Enemy AA Script For Operation HOG BY =Mason=           /////////////////////////////////////
//////////////////////                                                           //////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//_aapod = createVehicle ["Igla_AA_pod_East", getmarkerpos "center", [], 0, "NONE"]; <=== this works if all fails
AALoop ={	 		
		 sleep 5;
 		[] spawn createAA;
	   };


[color="#FF0000"]createAA ={ 

	for "_i" from 0 to 10 do 
	{	
       _grp = createGroup east;

      _aadis = [1000,2000,3000,4000] call BIS_fnc_selectRandom;
      _pos = [getMarkerPos "center", _aadis, random 359, false, [0, 0]] call SHK_pos;	      
      _aapod = createVehicle ["Igla_AA_pod_East", _pos, [], 0, "NONE"];
      _aapod setDir random 290;
      _aapod addEventHandler["killed", { (_this select 0) spawn AALoop; }];

   _gunner = _grp createUnit ["RU_Soldier_Crew", [0,0,1], [], 0, "CAN_COLLIDE"];
      _gunner assignAsGunner _aapod;
      _gunner moveInGunner _aapod; 		

{_x setSkill ["aimingAccuracy",0.65]} forEach units _grp;
{_x setSkill ["aimingShake",0.65]} forEach units _grp;
{_x setSkill ["aimingSpeed",0.65]} forEach units _grp;
{_x setSkill ["endurance",0.85]} forEach units _grp;
{_x setSkill ["spotDistance",0.99]} forEach units _grp;
{_x setSkill ["spotTime",0.85]} forEach units _grp;
{_x setSkill ["courage",0.85]} forEach units _grp;
{_x setSkill ["reloadSpeed",0.85]} forEach units _grp;
{_x setSkill ["commanding",0.85]} forEach units _grp;
{_x setSkill ["general",0.85]} forEach units _grp;


      if (debug) then {
               [sgt,nil,rgroupChat,"Spawning Island AA Defence Debug ON."] call RE;                
               markerfollow = {

                   _aagun = _this select 0; 
				_aaman = _this select 1;

				 hint format["%1",_aaman];
				 [sgt,nil,rgroupChat,"Creating Island AA Defence Debug Markers."] call RE;

                    _m = createMarker[str(_aaman),position _aaman]; str(_aaman) setMarkerType "AntiAir";

				sleep 0.2;
				[sgt,nil,rgroupChat,"Island AA Defence Debug Finished."] call RE; 
                   };

                   _handle = [_aapod,_gunner] spawn markerfollow;

      };

 };  
};[/color]
/////////////////////////////////////////////////////////////////////////////////////////
if (isserver) then
{
    [] spawn createAA; 	 
    //sleep 50000;    

};


and it works in that whole script but then i change a few names and add it to this script and it doesnt work nothing spawns and no markers or hints appear.

p.s the men and vehicles do spawn but no the static (in red)

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////                                                           ////////////////////////////////////////////////////////////////////////
///////////////////////  Spawn Enemy Script For Operation HOG SOCOMD Command      /////////////////////////////////////////////   EDITED  28/5/2012  //////
//////////////////////                                                           //////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
if isserver then {

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//                                                                                                                   //////////////////////////////////////
//                      ----------      Spawn Infantry    ----------                                                 //////////////////////////////////////
//                                                                                                                   //////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
for "_i" from 0 to 4 do 
	{
	_upsgrp1 = [1,getmarkerpos "taskarea",1,["taskarea","spawned","random","fortify","delete:",120]] execVM "SCRIPTS\UPSMON\MON_SPAWN.SQF";
	_upsgrp2 = [1,getmarkerpos "taskarea",1,["taskarea","spawned","random","delete:",120]] execVM "SCRIPTS\UPSMON\MON_SPAWN.SQF";
	sleep 0.3;
	};

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//                                                                                                                   //////////////////////////////////////
//                       ----------      Spawn Armour    ----------                                                  //////////////////////////////////////
//                                                                                                                   //////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

///////////////////////////////
//FOCKERS Vehicle Spaced Spawn
//[FOCK] Mikie J
////////////////////////
	for "_i" from 0 to 10 do 
	{
	_grp2 = [];
	_spawn2 = getMarkerPos "taskarea";
	_offset2 = [30,50,70,80,120,200,250] call BIS_fnc_selectRandom;
	_offset3 = random 100;
	_prec2 = random 100;
	_exp2 = "(1 - houses) * (1 - houses)";
	_bestplace2 = selectBestPlaces [_spawn2,_offset2,_exp2,_prec2,1];
	_spot4 = _bestplace2 select 0;
	_spot3 = _spot4 select 0;
	_safespawn2 = _spot3 findEmptyPosition [10, 75, "tank"];
	_randomvehicle2 = ["T34_TK_EP1","BRDM2_ATGM_TK_EP1","LandRover_MG_TK_EP1","T72_TK_EP1","ZSU_TK_EP1"] call BIS_fnc_selectRandom;
	_direction2 = random 359;
	_vehicleCreated =[_safespawn2, _direction2, _randomvehicle2, EAST] call BIS_fnc_spawnvehicle;
	_vehiclegroup = _vehicleCreated select 2;//group
	[(units _vehiclegroup) select 0, "taskarea","spawned","nofollow","random","nowait","delete:",120] execVM "scripts\upsmon.sqf";
	//[_vehiclegroup, "terrorHelpers"] spawn FOCK_fnc_VehiclePatrolMkr;
	};
       sleep 0.3;
[color="#FF0000"]///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//                                                                                                                   //////////////////////////////////////
//                       ----------      Spawn Static    ----------                                                  //////////////////////////////////////
//                                                                                                                   //////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////		
	for "_i" from 0 to 5 do 
	{	
       _grp1 = createGroup east;

_staticpos = getmarkerpos "center";
_staticdis = [50,100,200,300,400,500] call BIS_fnc_selectRandom;
_staticdir = random 360;
_staticpos = [(_centerpos select 0) + sin(_staticdir)*_staticdis,(_staticpos select 1) + cos(_staticdir)*_staticdis];
_staticpod = createVehicle ["Igla_AA_pod_East", _staticpos, [], 0, "NONE"];
_staticpod setdir (random 290);

	    _staticgunner = _grp1 createUnit ["RU_Soldier_Crew", [0,0,1], [], 0, "CAN_COLLIDE"];
           _staticgunner assignAsGunner _staticpod;
           _staticgunner moveInGunner _staticpod; 		

{_x setSkill ["aimingAccuracy",0.65]} forEach units _grp1;
{_x setSkill ["aimingShake",0.65]} forEach units _grp1;
{_x setSkill ["aimingSpeed",0.65]} forEach units _grp1;
{_x setSkill ["endurance",0.85]} forEach units _grp1;
{_x setSkill ["spotDistance",0.99]} forEach units _grp1;
{_x setSkill ["spotTime",0.85]} forEach units _grp1;
{_x setSkill ["courage",0.85]} forEach units _grp1;
{_x setSkill ["reloadSpeed",0.85]} forEach units _grp1;
{_x setSkill ["commanding",0.85]} forEach units _grp1;
{_x setSkill ["general",0.85]} forEach units _grp1;


      if (debug) then {
               [sgt,nil,rgroupChat,"Spawning Task Area AA Defence + Debug ON."] call RE;                
               staticmarkerfollow = {
                   _staticgun = _this select 0; 
				_staticman = _this select 1;

				 [sgt,nil,rgroupChat,"Spawning Task Area AA Defence Debug Markers."] call RE; 
                    _m1 = createMarker[str(_staticman),position _staticman]; str(_staticman) setMarkerType "Dot";

				sleep 0.2;
				[sgt,nil,rgroupChat,"Spawning Task Area AA Defence Finished."] call RE; 
                   };

                   _handle = [_staticpod,_staticgunner] spawn staticmarkerfollow;

      };

 };[/color]




	sleep 0.2;
	[] spawn E_reinforce;


};


Share this post


Link to post
Share on other sites

nevermind, double checked myself

damn it...squint won't start up:mad:

Ok, just a quick question.

When you spawn AALoop, it in turn spawns createAA.

createAA creates 11 AA with gunners. Each one has a "killed" eventHandler which spawns AALoop and in turn creates 11 more AA.

So if 1 of the 11 gets destroyed, you are left with 10, but spawn 11 more giving you 21, and so on...

Is this intended, or am I reading this incorrectly?

Edited by panther42

Share this post


Link to post
Share on other sites

no thats not intended lol ive got to // that out this is all still testing

Share this post


Link to post
Share on other sites

Just taking a quick look it seems there's one "}" too many.

Not sure how you intended it to work and haven't tested. But the last few lines need checking.

Otherwise the code looks good.

Share this post


Link to post
Share on other sites

Thanks twirly ill check it out mate. Dont supose u know how i can space the aa guns out so they dont spawn near each other mate or even a command to get me underway. Thanks again mate ur always helpfull.

Share this post


Link to post
Share on other sites

checked everything even retyped it all everything seems to be fine identical to the working version but this just isnt working

here is the code.

///////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////                                                           ////////////////////////////////////
///////////////////////    Enemy static Script For Operation HOG BY =Mason=           /////////////////////////////////////
//////////////////////                                                           //////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
createstatics ={ 

	for "_i" from 0 to 10 do 
	{	
       _grp1 = createGroup east;

      _aadis1 = [1000,2000,3000,4000] call BIS_fnc_selectRandom;
      _pos1 = [getMarkerPos "center", _aadis1, random 359, false, [0, 0]] call SHK_pos;	      
      _aapod1 = createVehicle ["Igla_AA_pod_East", _pos1, [], 0, "NONE"];
      _aapod1 setDir random 290;
      //_aapod1 addEventHandler["killed", { (_this select 0) spawn AALoop; }];

   _gunner1 = _grp1 createUnit ["RU_Soldier_Crew", [0,0,1], [], 0, "CAN_COLLIDE"];
      _gunner1 assignAsGunner _aapod1;
      _gunner1 moveInGunner _aapod1; 		

{_x setSkill ["aimingAccuracy",0.65]} forEach units _grp1;
{_x setSkill ["aimingShake",0.65]} forEach units _grp1;
{_x setSkill ["aimingSpeed",0.65]} forEach units _grp1;
{_x setSkill ["endurance",0.85]} forEach units _grp1;
{_x setSkill ["spotDistance",0.99]} forEach units _grp1;
{_x setSkill ["spotTime",0.85]} forEach units _grp1;
{_x setSkill ["courage",0.85]} forEach units _grp1;
{_x setSkill ["reloadSpeed",0.85]} forEach units _grp1;
{_x setSkill ["commanding",0.85]} forEach units _grp1;
{_x setSkill ["general",0.85]} forEach units _grp1;


      if (debug) then {

               [sgt,nil,rgroupChat,"Spawning Island AA Defence Debug ON."] call RE;

               markerfollow1 = {

                   _aagun1 = _this select 0; 
				_aaman1 = _this select 1;

				 hint format["%1",_aaman1];
				 [sgt,nil,rgroupChat,"Creating Island AA Defence Debug Markers."] call RE;

                    _m1 = createMarker[str(_aaman1),position _aaman1]; str(_aaman1) setMarkerType "Dot";

				sleep 0.2;
				[sgt,nil,rgroupChat,"Island AA Defence Debug Finished."] call RE; 
                   };

                   _handle1 = [_aapod1,_gunner1] spawn markerfollow1;

      };

 };  
};
/////////////////////////////////////////////////////////////////////////////////////////
if (isserver) then
{
    hint "spawning statics";
    [] spawn createstatics; 	 
    //sleep 50000;    

};



Share this post


Link to post
Share on other sites
Dont supose u know how i can space the aa guns out so they dont spawn near each other mate or even a command to get me underway.

Ruebe has a function which will do this. You can see it in this post:

RUBE_randomCirclePositions

In which you can define:

             - "objDistance" (scalar)
              - minimal distance to other/nearest objects
              - default = 1

            - "posDistance" (scalar)
              - minimal distance between returned positions
              - default = "objDistance" * 2

See here for more explanation:

Explanation

I just checked my own ruebe library collection, his latest RUBE_randomCirclePositions has several added optional parameters.

You can find it here:

rube-wip

Or I can post if you need.

Edited by panther42
cleaned up my post...it was a mess

Share this post


Link to post
Share on other sites

thanks panther i think i might be able to filter throught that to find how he sets the distance to spawn away from another object.. very helpfull mate.

Now i got the code working turns out my issue was this

if ([color="#FF0000"]debug[/color]) then {

now that works in my other code that gets loaded from the main init

but with my new code it gets loaded from inside another script and for some reason doesnt remember this from my main init.sqf

debug = paramsarray select 2;

so i had to change it to


if(_debug) then {

///==== and place this at the top of my code

_debug = true;

I know this is fixed but i really need to have my new code read the debug from if (debug) then { so i can have the debug option in my params

would any 1 know why this happens and how to fix the issue.

Share this post


Link to post
Share on other sites

_debug is just a local variable for that script, only that specific script can actually see the variable unless you used the call command. While "debug" is a global variable, accessible from all scripts. Which means that all scripts can change the value of the variable debug.

It's possible that your scripts, or some addon changes the value of "debug" and thus it's not true anymore. It's really hard for us to know why the value is changed.

That's why you should always try to have a TAG in front of your global variables, e.g : HGM_debug, to avoid such conflicts.

Share this post


Link to post
Share on other sites

perfect cuel thanks mate something i diddnt think off i changed it to HOG_debug now everything works great maybe a third party script i used was using debug ;)

---------- Post added at 22:20 ---------- Previous post was at 22:16 ----------

so while we are on the subject of code 1 question

when u use

private [_staic, _aa, _whatever, _whatever2]

this just makes those names only matter on the said script yeah,

so i can use those names again on another script and it want effect them.

is that how it works.

Share this post


Link to post
Share on other sites

Short answer, yes. Except you need to place the variables in quotation marks

private ["_static","_aa"];

This is not needed, but it's recommended.

Every _localVariable that you create only exist within the scope of its control structure. A short example is:

if (true) then {
 _myVar = "hello";
};
hint format ["%1",isNil "_myVar"];

_myVar will not exist outside the scope of that if-statement, unless it has been created earlier, or by using private ["_myVar"] at the top of the script.

This will also work:

_myVar = "";
if (true) then {
 _myVar = "hello";
};
hint format ["%1",isNil "_myVar"];

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
Sign in to follow this  

×