GonzoPR 1 Posted June 19, 2017 Hi, long time reader, first time poster. I've been getting into scripting a lot lately, I was always in and around it but recently trying to step it up a bit. I'm trying to write an IED script myself (i know there's a million out there) that searches for roads and places IEDs, IF there's x distance between this point and the last IED placed. This script has taken several forms as I try get it working properly, but this is my latest: Spoiler _searchFrom = player; //search from this point _area = 10000; //radius to search _minSpacing = 1000; //minimum space between 2 IEDs _iedPos = objNull; //pre-declare var to anything _placedIeds = []; //array of positions where IED has been placed (see _placedIeds pushBack below) _distArray = []; //array of distances calculated for debug _IED = objNull; //define _IED _prevIedPos = [0,0,0]; //just defining the var _roads = _searchFrom nearRoads _area; // _roads becomes array of all road objects found within params { _prevIedPos = _iedPos; // set this to last _iedPos before the loop updates _iedPos _iedPos = getPos _x; //position of object in _x _dist = _prevIedPos distance2D _iedPos; if ((_iedPos select 2) < 0.01 && (_iedPos select 2) > -0.05) then // select 2 is Z axis - <0.01 and > 0.05 stops mid air and below ground IEDs - not sure why pos of road obj is like this { if (_dist > _minSpacing) then { _IED = createVehicle ["Land_VR_Shape_01_cube_1m_F", _iedPos, [], 0, "NONE"]; _placedIeds pushBackUnique _iedPos; //add each _iedPos to new aray of positions for debug _distArray pushBack _dist; //add each distance calc to new array for debug systemChat str _dist; copyToClipboard str _distArray; }; }; } forEach _roads; Currently it will place IEDs (VR blocks) at very odd distanced from each other, some as little as 20m away. I also made up a script that will place IEDs on markers chosen at random and randomly choose the IED type - ultimately I want to merge these 2 together, but for now it's getting the spacing constraints to work that's my issue. Random marker + IED type script for anyone interested: Spoiler /* ACE IED CLASS NAMES ACE_IEDLandSmall_Range_Ammo ACE_IEDLandBig_Range_Ammo ACE_IEDUrbanSmall_Range_Ammo ACE_IEDUrbanBig_Range_Ammo */ _iedCount = 0; //There are no IEDs yet _iedTypes = ["ACE_IEDLandSmall_Range_Ammo","ACE_IEDUrbanSmall_Range_Ammo","ACE_IEDLandBig_Range_Ammo","ACE_IEDUrbanBig_Range_Ammo"]; //Selection of IED types to randomly place _iedMarkers = ["IED","IED_1","IED_2","IED_3","IED_4","IED_5","IED_6","IED_7","IED_8","IED_9","IED_10"]; //Selection of markers to choose randomly from _maxIed = 4; //Max IEDs to place, must be less than total markers or there is no randomness _iedMrkrChoice = 0; //Makes error message fuck off about undefined var while {_iedCount < _maxIed} do { _iedTypeChoice = selectRandom _iedTypes; //pick IED type at random _iedMrkrChoice = selectRandom _iedMarkers; //pick IED marker at random _IED = createVehicle [_iedTypeChoice, getMarkerPos _iedMrkrChoice, [], 2, "NONE"]; //spawn the IED _iedCount = _iedCount + 1; //add 1 to _iedCount to allow loop to stop once maxIed has been reached _iedSubtract = [_iedMrkrChoice]; _iedMarkers = _iedMarkers - _iedSubtract; //remove chosen marker from marker array for next cycle of loop to avoid multiple IEDs on 1 marker }; Thanks for any input on this, I've been staring at it so long its becoming a blur of words :P Share this post Link to post Share on other sites
Grumpy Old Man 3546 Posted June 19, 2017 Pretty simple actually, select helps to great effect here: {deletemarker _x} foreach allMapMarkers; _IEDs = []; _roads = getposatl player nearroads 10000; //grabbing all roads within 10000m systemchat str count _roads; while {count _roads > 0} do { _rndRoad = selectRandom _roads; //picking a random road segment _roads = _roads select {_x distance2d _rndRoad >= 1000}; //removing any roads closer than 1km to that random segment _IEDs pushback _rndroad; //store the random segment in an array for later usage //some markers to visualize the entire thing _marker = createmarker [str _rndRoad,getposatl _rndRoad]; _marker setmarkertype "hd_dot"; _marker setmarkercolor "ColorRed"; _marker setmarkertext format ["IED %1",_IEDs find _rndRoad]; }; //that's it _IEDs is an array of road segments that are meeting your criteria for placing an IED near it. Working fine and not too hard on performance. Checking all roads within 10000m on altis (24k roads standing in front of athiras church) takes 3 seconds, so perfectly fine for a one time use during mission init. Cheers 3 Share this post Link to post Share on other sites
pedeathtrian 99 Posted June 19, 2017 You only check distance to the last placed IED and not all of the placed IEDs. This way you might end up with IEDs placed on adjacent road segments. So either check for distance to all placed IEDs or better filter out road segments within _minSpacing: // ... ditto _roads = _searchFrom nearRoads _area; while {(count _roads) > 0} do { _iedPos = getPos (_roads select 0); // unconditionally place IED here ... _roads = _roads select {(_iedPos distance2d (getPos _x)) > _minSpacing}; } 2 Share this post Link to post Share on other sites
GonzoPR 1 Posted June 19, 2017 I was definitely going the long way round with my code, hopefully some of what I learned from doing it has sunk in though. Makes sense now, the way mine was setup it wasn't able to check all IED positions. Amazing how short and concise something can be after you've written a wall of crap haha. Grumpy Old Man, your code is ridiculously straight forward, i think I might pinch that for the time being and work in a debug and admin check to activate map markers somehow. Just little side projects for me to potter with and possibly implement. Thanks again. 1 Share this post Link to post Share on other sites