Jump to content
Sign in to follow this  
1para{god-father}

Script Convoy and towns

Recommended Posts

OK need some help please on the following:-

1) Is there a way I can get a town location as below but also then the second town location (_RandomTownPosition2) must be x amount away from town 1 i.e radius of 1000 must be outside this but find the closest then outside that radius.

2) My way-points do not seem to work is the below WP bit correct ? -- also as this might be some way away do i need to add more way points , if so how would i know where to put them as it is random ?

/// get town locations
_towns = nearestLocations [getPosATL player, ["NameVillage","NameCity","NameCityCapital"], 25000];

///start location spawn point
_RandomTownPosition1 = position (_towns select (floor (random (count _towns))));

/// convoy end destination
_RandomTownPosition2 = position (_towns select (floor (random (count _towns))));

///spawn vehicle   // need to work out how to put them on a road/////

_v1 = [_RandomTownPosition1, 001, "Ural_INS", east] call bis_fnc_spawnvehicle;


////add waypoints
_wp1 = _v1 addWaypoint [_RandomTownPosition2, 1];
_wp1 setWaypointType "MOVE";
_wp1 setWaypointSpeed "FULL";

Share this post


Link to post
Share on other sites

Hi mate... Here is the answer to the first part of your problem. Demo mission is here.

Hope it helps you man.

script.sqf:-

// get town locations
_towns = nearestLocations [getPosATL player, ["NameVillage","NameCity","NameCityCapital"], 25000];

//get a random index to use to select the a random town
_randindx = floor (random (count _towns));

///start location spawn point....town1
_town1 = _towns select _randindx;

//delete first town from list...so we can't get the same town again
_towns set [_randindx,"del"];
_towns = _towns - ["del"];

//get list of towns greater than 1000m away
_newlist = [];
for "_i" from 0 to (count _towns)-1 do {
_town = _towns select _i;
_dis = (position _town1) distance (position _town);
if (_dis > 1000) then {
	_newlist set [count _newlist, _town];
};
sleep 0.01;
};

//sort the new list by distance...nearest to furthest. May not be needed!
_slist = [];
_total = count _newlist;
for "_i" from 0 to (_total-1) do {
_nearest = objnull;
_nearestdist = 99999;
for "_j" from ((count _newlist)-1) to 0 step -1 do {
	_current = _newlist select _j;
	_distance = _current distance (position _town1);
	if (_distance < _nearestdist) then {
		_nearest = _current;
		_nearestdist = _distance;
	};
};
_slist set [count _slist,_nearest];
_newlist = _newlist - [_nearest];
};

//select the nearest one.. this will be further than 1000m
_town2 = _slist select 0;

//create some markers so we can see what's going on

//marker for radius
_m = createMarkerLocal ["Radius", position _town1];
_m setMarkerShapeLocal "ELLIPSE";
_m setMarkerBrushLocal "SOLID";
_m setMarkerColorLocal "colorYellow";
_m setMarkerSizeLocal [1000, 1000];		//radius
_m setMarkerTextLocal "Radius";

//marker for Town 1
_m = createMarkerLocal ["Town 1", position _town1];
_m setMarkerTypeLocal "mil_dot";
_m setMarkerColorLocal "colorRED";
_m setMarkerSizeLocal [2, 2];
_m setMarkerTextLocal "Town 1";

//marker for Town 2
_m = createMarkerLocal ["Town 2", position _town2];
_m setMarkerTypeLocal "mil_dot";
_m setMarkerColorLocal "colorRED";
_m setMarkerSizeLocal [2, 2];
_m setMarkerTextLocal "Town 2";

Edited by twirly
Clarity

Share this post


Link to post
Share on other sites

Also prepared something (script tested - works fine. Both points placed on nearby road, truck is spawned at first point and going to second town):

/// get town locations
_towns = nearestLocations [getPosATL player, ["NameVillage","NameCity","NameCityCapital"], 25000];

///start location spawn point
_pos1 = position (_towns select (floor (random (count _towns))));

_RandomTownPosition1 = [_pos1 select 0,_pos1 select 1,0];

_nRoads = _RandomTownPosition1 nearRoads 10;

_rng = 10;

while {((count _nRoads) == 0)} do
{
_rng = _rng * 2;
_nRoads = _RandomTownPosition1 nearRoads _rng;
};

_RandomTownPosition1 = position (_nRoads select (floor (random (count _nRoads))));

/// convoy end destination

_towns = nearestLocations [_RandomTownPosition1, ["NameVillage","NameCity","NameCityCapital"], 25000];

_RandomTownPosition2 = _RandomTownPosition1;

{
if (((position _x) distance _RandomTownPosition2) > 1000) exitwith  {_RandomTownPosition2 = [(position _x) select 0,(position _x) select 1,0]}
}
foreach _towns;

_nRoads = _RandomTownPosition2 nearRoads 10;

_rng = 10;

while {((count _nRoads) == 0)} do
{
_rng = _rng * 2;
_nRoads = _RandomTownPosition2 nearRoads _rng;
};

_RandomTownPosition2 = position (_nRoads select (floor (random (count _nRoads))));

///spawn vehicle   // need to work out how to put them on a road/////

_v1 = [_RandomTownPosition1, 001, "Ural_INS", east] call bis_fnc_spawnvehicle;

_grp = _v1 select 2;

////add waypoints
_wp1 = _grp addWaypoint [_RandomTownPosition2, 1];
_wp1 setWaypointType "MOVE";
_wp1 setWaypointSpeed "FULL";

//un-comment below for testing markers
/*
_i = "mark1" + (str (random 1000));
_i = createMarker [_i,_RandomTownPosition1];
_i setMarkerColor "ColorRed";
_i setMarkerShape "ICON";
_i setMarkerType "DOT";
_i setMarkerText "StartPoint";

_i2 = "mark2" + (str (random 1000));
_i2 = createMarker [_i2,_RandomTownPosition2];
_i2 setMarkerColor "ColorRed";
_i2 setMarkerShape "ICON";
_i2 setMarkerType "DOT";
_i2 setMarkerText "EndPoint";

Sorting by distance not neccessary indeed, because array from nearestLocations is already sorted such way, so is sufficient to get again list of towns, this time around first town position.

You do not need more waypoints (BTW pity, because script responding to last question may be very interesting - it is about finding best route between two points), only if end point will be far away, then will take some seconds, before truck finish to calculate route (exactly, interesting, how this is calculated by game engine?). Waypoint wasn't working, because _v1 is here an array, and name of group needed for addWaypoint is third element of this array. Corrected this.

Edited by Rydygier

Share this post


Link to post
Share on other sites
....Sorting by distance not neccessary indeed, because array from nearestLocations is already sorted such way

Thought so.... just went the extra mile in case!

Share this post


Link to post
Share on other sites

Nice scripts there gents. I have something similar that creates vehicles with waypoints on random road positions around a town nearest the player, sort of an ambient traffic system.

One thing I've noticed is that sometimes the engine decides that the best way to get from road waypoint a to road waypoint b is via cross country. Seeing a car driving through a paddock is a bit immersion breaking. Is there a way to prevent this?

Share this post


Link to post
Share on other sites

Thanks mate, I'll put this setbehaviour 'CARELESS' in the spawned drivers' init fields and see how I go.

I think it has a lot to do with how you set their behaviour. http://community.bistudio.com/wiki/setBehaviour

I should have also said... and Combat Mode.

Share this post


Link to post
Share on other sites

Hey guys nice script but I seem to be having a problem with it. I have adapted it ever so slightly so that it does not pick the town where my FOB is (Feruz Abad) as one of the start or end points.

Here is the code:

_locations = nearestLocations [[0,0], ["NameVillage","NameCity","NameCityCapital"], 30000];

if (DEBUG) then {
server globalChat "SEARCHING FOR USABLE POSITIONS";
};

{	
if(position _x distance getMarkerPos "fob" > 2000) exitWith {
	_startpos = _x;
};
} foreach _locations;

if (DEBUG) then {
server globalChat "FOUND USABLE POSITIONS";
};

_convoystart = [(position _startpos) select 0,(position _startpos) select 1, 0];

_nRoads = _convoystart nearRoads 10;
_rng = 10;

while {((count _nRoads) == 0)} do {
_rng = _rng * 2;
   _nRoads = _convoystart nearRoads _rng;
};

_convoystart = position (_nRoads select (floor (random (count _nRoads))));

{	
if(position _x distance _startpos > 2500) exitWith {
	_endpos = _x;
};
} foreach _locations;

_convoyend = [(position _endpos) select 0,(position _endpos) select 1, 0];

_nRoads = _convoyend nearRoads 10;
_rng = 10;

while {((count _nRoads) == 0)} do {
   _rng = _rng * 2;
   _nRoads = _convoyend nearRoads _rng;
};

_convoyend = position (_nRoads select (floor (random (count _nRoads))));

[_convoystart,_convoyend,7] call createConvoy;

Here is the .RPT where the error is occurring:

Error in expression <_rng = _rng * 2;
_nRoads = _convoystart nearRoads _rng;
};

_convoystart = posit>
 Error position: <nearRoads _rng;
};

_convoystart = posit>
 Error Type Any, expected Number
File C:\Users\*******\Documents\ArmA 2 Other Profiles\*******\mpmissions\*******\server\missions\enemy_convoy.sqf, line 24

I can't seem to find the error, can anyone lend a hand?

Edited by bigshotking

Share this post


Link to post
Share on other sites

There might be other stuff that is not right... but I did notice this.... _startpos can end up being an array of coordinates and not one set of coordinates.... and the same with _endpos further down.

{
   if(position _x distance getMarkerPos "FOB" > 2000) then {
       [b][i]_startpos[/i][/b] = position _x;
   };

} foreach _locations;

Share this post


Link to post
Share on other sites

Yeah I updated the code above with an if() exitWith {}; instead of a then, i see what you mean by that but it still gives me the error with the nearRoads.

Share this post


Link to post
Share on other sites

_startpos ...even though it is only one set of coordinates... is still an array.... so initialize it before using it inside the if.

_startpos = [];

{
   if (position _x distance getMarkerPos "FOB" > 2000) exitWith {
       _startpos = position _x;
   };

} foreach _locations;

EDIT: I found the exitWith quite clever!!

---------- Post added at 11:45 AM ---------- Previous post was at 11:41 AM ----------

Of course.... same for _endpos

---------- Post added at 11:54 AM ---------- Previous post was at 11:45 AM ----------

MORE:

DEBUG = true;

_locations = nearestLocations [[0,0], ["NameVillage","NameCity","NameCityCapital"], 30000];

if (DEBUG) then {
   player globalChat "SEARCHING FOR USABLE POSITIONS";
};

_startpos = [];

{
   if(position _x distance getMarkerPos "mkr_FOB" > 2000) exitWith {
       _startpos = position _x;
   };

} foreach _locations;

if (DEBUG) then {
   player globalChat "FOUND USABLE POSITIONS";
};

[s]_convoystart = _startpos) select 0,(position _startpos) select 1, 0];[/s]

_nRoads = [b][i]_startpos[/i][/b] nearRoads 10;

_rng = 10;

while {((count _nRoads) == 0)} do {

   _rng = _rng * 2;
   _nRoads = [b][i]_startpos[/i][/b] nearRoads _rng;

};

_convoystart = position (_nRoads select (floor (random (count _nRoads))));

_endpos = [];

{
   if(position _x distance [b][i]_startpos[/i][/b] > 2500) exitWith {
       _endpos = position _x;
   };

} foreach _locations;

[s]_convoyend = [(position _endpos) select 0,(position _endpos) select 1, 0];[/s]

_nRoads = [b][i]_endpos[/i][/b] nearRoads 10;
_rng = 10;

while {((count _nRoads) == 0)} do {
   _rng = _rng * 2;
   _nRoads = [b][i]_endpos[/i][/b] nearRoads _rng;
};

_convoyend = position (_nRoads select (floor (random (count _nRoads))));

[_convoystart,_convoyend,7] call createConvoy;

Edited by twirly
Clarity

Share this post


Link to post
Share on other sites

Got it working, thanks for the help.

loc_start = [];

_locations = nearestLocations [[8000,0], ["NameVillage","NameCity","NameCityCapital"], 30000];

if (DEBUG) then {
server globalChat "SEARCHING FOR USABLE POSITIONS";
};

{	
if(position _x distance getMarkerPos "fob" > 4500) exitWith {
	loc_start = loc_start + [_x];
};
} foreach _locations;

if (DEBUG) then {
server globalChat "FOUND USABLE POSITIONS";
};

_startpos = loc_start select ceil(random (count loc_start - 1));
_convoystart = [(position _startpos) select 0,(position _startpos) select 1, 0];

_nRoads = _convoystart nearRoads 10;
_rng = 10;

while {((count _nRoads) == 0)} do {
_rng = _rng * 2;
   _nRoads = _convoystart nearRoads _rng;
};

_convoystart = position (_nRoads select (floor (random (count _nRoads))));

loc_end = [];

{	
if(position _x distance _startpos > 5000 && position _x distance getMarkerPos "fob" > 5000) exitWith {
	loc_end = loc_end + [_x];
};
} foreach _locations;

_endpos = loc_end select round(random (count loc_end - 1));
_convoyend = [(position _endpos) select 0,(position _endpos) select 1, 0];

_nRoads = _convoyend nearRoads 10;
_rng = 10;

while {((count _nRoads) == 0)} do {
   _rng = _rng * 2;
   _nRoads = _convoyend nearRoads _rng;
};

_convoyend = position (_nRoads select (floor (random (count _nRoads))));

[_convoystart,_convoyend,7] call createConvoy;

Share this post


Link to post
Share on other sites

I see you are using the following to spawn in the convoy:-

[_convoystart,_convoyend,7] call createConvoy;  

how do you make your own function like that, as i am sure that will save me a lot of time on each mission re writting code?

Thanks

Share this post


Link to post
Share on other sites

just create a functions.sqf and in your init put this:

[] call compile preprocessFile "functions.sqf";

That stores the functions so that they are ready to call later on in the mission.

Now in the functions.sqf put this:

createConvoy = {
       // code goes here
};

and thats it.

Edited by bigshotking

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  

×