Jump to content
Sign in to follow this  
kyfohatl

Finding the shortest distance between an allied base and an enemy base... How to?

Recommended Posts

The scenario is that I have a total of 10 capturable locations on the map, any of which could belong to the enemy or you. I've already written a script to identify which bases are held by which faction, now I need a script that will help me find out the smallest distance between and allied base and an enemy base, so that that enemy base becomes the next target of assault (i.e. which base should be attacked next, so waypoints are generated for AI squads to go there). It's for a dynamic battle scenario.

The bases are: base1, base2, ... base10; and as I said, I know which bases belong to me and which bases belong to the enemy. So, can anyone help me with making this script?

Oh, by the way, the location of a "base" is signified by markers called "base(1-10)".

Cheers

EDIT: Found this good thread on finding the smallest value in an array:

http://forums.bistudio.com/showthread.php?t=64321&highlight=smallest+number

That should make things much easier

Edited by kyfohatl

Share this post


Link to post
Share on other sites

hmm well theres multiple ways to go about this:

you can do something like once a base is taken over, then in the if statement:

if (baseenemy1takenover) then {

_dist = enemybase1 distance enemybase2

// setwaypoint or whatever

}:

but that implies that the ai are to attack one base at a time from 1 to 5, and not dynamically attack anythat are nearby.

if thats not what you want, you can try using a while loop to constantly check for bases using nearest objects.

this is just an outline of how to do that:


while {alive _groupleader} do {
   _nearbase = nearestObjects [_groupleader, ["BaseClassnamehere"], 200]
        if ([_nearbase] == basenamehere) then {
           _groupleader addwaypoint etc etc
        };
};

that might possibly work, but youd have to check for each element in the array and see if one of them matches a base. tho im not sure if nearest objects can give you the name of the base.

i hope that helps, i havnt done anything like that so im not to sure myself.

Share this post


Link to post
Share on other sites

I suggest you use location logics for each of the bases, that way you can use 'setside' to determine who they belong too.

Then using the following function, executed via the following, using the return value for whatever you want.

_closestBase = [west, mybase1] call myfunc

myfunc = call compile preprocessfilelinenumbers "fn_nearestBase.sqf";

allBases = [base1, base2, base3, base4, base5];

fn_nearestBase.sqf

_side = _this select 0;
_base = _this select 1;
_position = position _base;
if (count allBases < 2) exitwith {objNull};
_closest = allBases select 0;
_min = _position distance (position _closest);
{
if (side _x != _side) then {
	_dist = _base distance (position _x);
	if (_dist < _min) then {
		_min = _dist;
		_closest = _x;
	};
};
} foreach (allBases - [_base]);
_closest;

To add bases easily, put the following in initlines:

if (isnil "allBases") then {allBases = [];}; allBases set [count allBases, this];

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  

×