Jump to content
FederalRazer89

any help with function for checking distance

Recommended Posts

Hello

I dont really like to ask for help this often but i was about to test a way to approch another problem, that people of this forum have suggested some options that i will try.
The problem is that i am trying to create a function that will check the distance to serveral positions, and if to close spawn a new position. I have done this kind of things more in a manual style with another script, but the amount of positions that i will create will need a lot writing of code that will be a bit cumbersome to work with. So a function that would handle that process would be nice.

the script:


fn_CheckDistanceToSites = {                                                <----------------------------------------// function for checking distance to other positions

    _MainPos      = _this select 0;
    _CompareArray = _this select 1;
    
    for "_i" from 0 to ((count _CompareArray) - 1) do 
    {
    
    if ((_MainPos distance (_CompareArray select _i)) <= 500) exitWith {[_MainPos] call fn_findsettlement};
    
    };

    _MainPos = _this select 0;
};
 

fn_findsettlement = {                                                      <----------------------------------------// function for creating a position near some buildings

    _housepos  = _this select 0;
    _houselist = _this select 1;

    //Parameters
    _houseAmountCheck = 20;  
    _checkradius      = 100;

    
    _house        = _houselist call BIS_fnc_selectRandom;
    _houseposList = nearestTerrainObjects [(getpos _house) ,["HOUSE","CHURCH","CHAPEL","FUELSTATION","HOSPITAL"], _checkradius,false];
    _housepostest = _houseposList call BIS_fnc_selectRandom;

    
 if ((count _houseposList) <= _houseAmountCheck) then {
    _house        = nil;
    _housepostest = nil;
    _house        = _houselist call BIS_fnc_selectRandom;
    _houseposList = nearestTerrainObjects [(getpos _house) ,["HOUSE","CHURCH","CHAPEL","FUELSTATION","HOSPITAL"], _checkradius,false];
    _housepostest = _houseposList call BIS_fnc_selectRandom;
};
 if ((count _houseposList) <= _houseAmountCheck) then {                                                          <--------------------------- // This command repeats for about 500 lines.
    _house        = nil;
    _housepostest = nil;
    _house        = _houselist call BIS_fnc_selectRandom;
    _houseposList = nearestTerrainObjects [(getpos _house) ,["HOUSE","CHURCH","CHAPEL","FUELSTATION","HOSPITAL"], _checkradius,false];
    _housepostest = _houseposList call BIS_fnc_selectRandom;
};


//The main code

_houseposList = nearestTerrainObjects [_Center ,["HOUSE","CHURCH","CHAPEL","FUELSTATION","HOSPITAL"], _mapsize*2,false];
_Mat_pos1     = _houseposList call BIS_fnc_selectRandom;
[_Mat_pos1,_houseposList] call fn_findsettlement;  


_Mat_pos2 = _houseposList call BIS_fnc_selectRandom;
[_Mat_pos2,_houseposList] call fn_findsettlement;
_matZoneArray1 = [_Mat_pos1];
[_Mat_pos2,_matZoneArray1] call fn_CheckDistanceToSites;


_Mat_pos3 = _houseposList call BIS_fnc_selectRandom;
[_Mat_pos3,_houseposList] call fn_findsettlement;
_matZoneArray2 = [_Mat_pos1,_Mat_pos2];
[_Mat_pos3,_matZoneArray2] call fn_CheckDistanceToSites;


The lines at the bottom are only 3 positions of 16 and when they are generated the script will create markers on those positions. I get the error undefined varible in _housepostest, but i have another mission that works fine with that function.
I have been trying to solve this problem since i woke up so any help would be appreciated.

Share this post


Link to post
Share on other sites

This one is created on altis but i intend to make it work on any map so manuall placement of markers is something i want to avoid. And i would like to learn how to create a function that would be able to handle this kind of problem.

Share this post


Link to post
Share on other sites

It's a bit unclear what you want.

Maybe something like this:

_positions = [pos1,pos2,pos3,etc];
_referencePos = getposatl player;//or whatever you want
_tooClose = count (_positions select {_x distance2d _referencePos < 500) > 0;//will return true if any position is closer than 500

Cheers

Share this post


Link to post
Share on other sites

Will try and explain it in more detail.

fn_findsettlement : Will try to find positions that have a certain amount of houses, and it will try about 40 locations (will rewrite this function later).
fn_CheckDistanceToSites  : This function is intended to check the distance of one position to an array of positions. If the position that is compared to the array is too close to any of the positions in the array it will use exitwith and start a new search for a new location with fn_findsettlement.

The purpose of the script is to create randomly created positions and place markers on them. These markers will be used as a place to get resources. The amount of marker will be 16 at the moment and manually compare each resources site with each position would be cumbersome and easy to create unnecessary error when tweaking.
 

Share this post


Link to post
Share on other sites

In this case the example Grumpy Old Man gave you will work like a charm, just use _tooclose as the condition and execute fn_findsettlement, if the condition is true. 

Share this post


Link to post
Share on other sites

I tried to use it this way, but because i am not really not sure of how the use "count" and "select" function work in the way you described Grumpy Old Man. I get an error that it wants a number but i produces a bool typ, any posibilty that you can exlain what i did wrong in my implementation?


_Mat_pos3 = _houseposList call BIS_fnc_selectRandom;
[_Mat_pos3,_houseposList] call fn_findsettlement;
_matZoneArray2 = [_Mat_pos1,_Mat_pos2];
_tooClose1     = count (_matZoneArray1 select {_x distance2d _Mat_pos3 < 500} > 0);    <----------------------------  Error here
 if (_tooclose ) then {[_Mat_pos3,_houseposList] call fn_findsettlement;};

 

Share this post


Link to post
Share on other sites

Thanks Grumpy Old Man that solved that problem.

Now got another thing to solve, but i can fix on my own.

I appreciat the help that i got thanks.

  • Like 1

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

×