Jump to content
draoth

How to keep executing a line of code until a parameter is correct

Recommended Posts

I'm trying to create a random unit spawn script but when i'm using my script the other player can see the units spawning if he's at the right distance.

_Randomlocation = [[selectrandom [player1,player2], 1000, 1500, 3, 0, 20, 0] call BIS_fnc_findSafePos] ;

I'd like to know a way i can keep executing this script until the distance with every player is >200. Is there a efficient way to do this?

Share this post


Link to post
Share on other sites
58 minutes ago, draoth said:

I'm trying to create a random unit spawn script but when i'm using my script the other player can see the units spawning if he's at the right distance.


_Randomlocation = [[selectrandom [player1,player2], 1000, 1500, 3, 0, 20, 0] call BIS_fnc_findSafePos] ;

I'd like to know a way i can keep executing this script until the distance with every player is >200. Is there a efficient way to do this?

something along the lines of this. check every player and see if any of them is closer than 200, if one unit is closer than 200 it exits the forEach command and sets _playerToClose to true, if no unit is closer than 200 then it leaves the boolean as false and runs your random location thingy

_minDistance = 200;
_playerToClose = false;
{
	if (player distance _x < _minDistance) exitWith {_playerToClose = true;};
}forEach allPlayers;

if (!_playerToClose) then
{
	_Randomlocation = [[selectrandom [player1,player2], 1000, 1500, 3, 0, 20, 0] call BIS_fnc_findSafePos];
};

 

Share this post


Link to post
Share on other sites
10 hours ago, gokitty1199 said:

something along the lines of this. check every player and see if any of them is closer than 200, if one unit is closer than 200 it exits the forEach command and sets _playerToClose to true, if no unit is closer than 200 then it leaves the boolean as false and runs your random location thingy


_minDistance = 200;
_playerToClose = false;
{
	if (player distance _x < _minDistance) exitWith {_playerToClose = true;};
}forEach allPlayers;

if (!_playerToClose) then
{
	_Randomlocation = [[selectrandom [player1,player2], 1000, 1500, 3, 0, 20, 0] call BIS_fnc_findSafePos];
};

 

The problem is, i want it to check if the random location is close to tot the player and if that's the case of should generate a new location until it's out of range of the players.

Share this post


Link to post
Share on other sites

Here is my offering:

MY_fnc_EnemySpawnLocation = {
    params ["_closeToOneOf", "_minDist", "_maxDist", ["_objDist", 3]];
    _maxDist = _maxDist max (_minDist + 200); // Avoid impossible / hard to find location
    private _badPos = [0, 0, 0];
    private _pos = _badPos;
    // Construct blacklist circles
    private _blacklist = _closeToOneOf apply {
        [getPos _x, [_minDist, _minDist, 0, false]]
    };
    // Sigh...
    private _blacklist2 = _blacklist apply {
        [_x select 0] + (_x select 1)
    };
    // Keep trying
    while {_pos distance _badPos < 1} do {
        _pos = [
            selectRandom _closeToOneOf,
            _minDist,
            _maxDist,
            _objDist,
            0, // No water
            1, // Max Grad
            0, // Don't have to be shore
            _blacklist,
            [_badPos, _badPos]
        ] call BIS_fnc_findSafePos;
        // BIS_fnc_findSafePos only applies blackList in initial phases might still find a bad location
        if (_blacklist2 findIf {_pos inArea _x} >= 0) then {
            _pos = _badPos;
        };
    };
    _pos
};

Here is example of usage:

private _spawnPos = [[player1, player2], 1000, 1500] call MY_fnc_EnemySpawnLocation;

Here is an image of me creating 1000 markers on the position returned with two "players". The red area is the minimum distance, and the blue marks the maximum distance.

gEcI823.jpg

 

Note that in the overlap there are many more points. This is because regardless of which player it picks, the overlapped area has a chance. If it is intended to spawn AI near players, this might in fact be an advantage since it is more likely they will spawn, still out of range, but near more players. Though if this is not desired, it is possible to workaround by using another approach.

 

Depending on conditions, this ran take some time, e.g. if little land available for spawning, say on a small island like Utes above, so you might want to run this in a scheduled environment (eg. using spawn).

  • Like 5
  • Thanks 1

Share this post


Link to post
Share on other sites
6 hours ago, Muzzleflash said:

Here is my offering:


MY_fnc_EnemySpawnLocation = {
    params ["_closeToOneOf", "_minDist", "_maxDist", ["_objDist", 3]];
    _maxDist = _maxDist max (_minDist + 200); // Avoid impossible / hard to find location
    private _badPos = [0, 0, 0];
    private _pos = _badPos;
    // Construct blacklist circles
    private _blacklist = _closeToOneOf apply {
        [getPos _x, [_minDist, _minDist, 0, false]]
    };
    // Sigh...
    private _blacklist2 = _blacklist apply {
        [_x select 0] + (_x select 1)
    };
    // Keep trying
    while {_pos distance _badPos < 1} do {
        _pos = [
            selectRandom _closeToOneOf,
            _minDist,
            _maxDist,
            _objDist,
            0, // No water
            1, // Max Grad
            0, // Don't have to be shore
            _blacklist,
            [_badPos, _badPos]
        ] call BIS_fnc_findSafePos;
        // BIS_fnc_findSafePos only applies blackList in initial phases might still find a bad location
        if (_blacklist2 findIf {_pos inArea _x} >= 0) then {
            _pos = _badPos;
        };
    };
    _pos
};

Here is example of usage:


private _spawnPos = [[player1, player2], 1000, 1500] call MY_fnc_EnemySpawnLocation;

Here is an image of me creating 1000 markers on the position returned with two "players". The red area is the minimum distance, and the blue marks the maximum distance.

gEcI823.jpg

 

Note that in the overlap there are many more points. This is because regardless of which player it picks, the overlapped area has a chance. If it is intended to spawn AI near players, this might in fact be an advantage since it is more likely they will spawn, still out of range, but near more players. Though if this is not desired, it is possible to workaround by using another approach.

 

Depending on conditions, this ran take some time, e.g. if little land available for spawning, say on a small island like Utes above, so you might want to run this in a scheduled environment (eg. using spawn).

Fantastic, Thank you!

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

×