Jump to content
Luft08

Can someone explain how to use calculatePath

Recommended Posts

The title says it all. I need to know what direction a vehicle will go when placed on a random road segment with a random road segment as a destination. I think calculatePath might be useful but I don't know how to use the "agent" after I get it.

Share this post


Link to post
Share on other sites

You can use calculatePath to return an array of positions via the PathCalculated Event Handler.

Once you have an array of positions, you can find the direction from array select 0 to array select 1, etc.  "I need to know what direction a vehicle will go..."

I have used this in testing for ambient civilians, driving back/forth from town to town, in conjunction with setDriveOnPath.

 

I can dig up some code from a test mission if you would like.

  • Like 2

Share this post


Link to post
Share on other sites
36 minutes ago, panther42 said:

You can use calculatePath to return an array of positions via the PathCalculated Event Handler.

Once you have an array of positions, you can find the direction from array select 0 to array select 1, etc.  "I need to know what direction a vehicle will go..."

I have used this in testing for ambient civilians, driving back/forth from town to town, in conjunction with setDriveOnPath.

 

I can dig up some code from a test mission if you would like.

Any examples you can provide will be helpful. I modified and used the example provided on the wiki page. To plot the path of the convoy I execute my markPath.sqf code:

if(!isServer) exitWith {};

params["_startPos", "_endPos"];

convoyPath = [];

(calculatePath ["wheeled_APC","safe",_startPos,_endPos]) addEventHandler ["PathCalculated",{
    {
    	private _object = _x;
	if(_forEachIndex mod 15 == 0) then {
    		private _marker = createMarker ["marker" + str _forEachIndex, _x];
    		_marker setMarkerType "mil_dot"; 
    		_marker setMarkerColor "ColorOrange";  	
        	convoyPath pushBack _x;
       };
    } forEach (_this select 1);
}];

Here is the test code that calls markPath.sqf:

// Mark likely convoy path
private _handle = [_posStart, _posEnd] execVM ("LFT_Convoy\markPath.sqf");
waitUntil {scriptDone _handle};

private _point1 = convoyPath select 0;
private _point2 = convoyPath select 1; // <<<<<<<<------ Throws a zero divisor error.

if(isNil "_point1" ) then {
	hint "Nil"; // <<<<<<<<-------------- This hint shows up.
} else {
	hint str _point1;
};

I would be grateful for any examples of working code.  Thanks.

  • Like 1

Share this post


Link to post
Share on other sites

This is part of the code which I used for testing.  I was using this to send cars driving back/forth from distant neighbor locations.  Very similar to what you are working on.  You can tweak to your variables, etc.

Convoys are another animal, depending on if you set vehicles in the convoy along the route start position via your scripts also.  I used to use one of Rube's convoy route scripts for all of it.

 

Don't believe I had any issues with the Problem(s) listed in calculatePath:

For some unknown reason, the "PathCalculated" Event Handler is fired twice, first with calculated path and second with array consisting of 2 elements, which are identical and are equal to the end point. See Example 3 & 4 for a workaround.

 

Spoiler

romTest = [];

isNil {(calculatePath ["car","safe",_startPos,_destPos]) addEventHandler ["PathCalculated", 
{					
	romTest = _this select 1;
}]};
waitUntil {count romTest > 0};

private _romTemp = + romTest;

//diag_log format ["_romTemp: %1",_romTemp];


	[_vehX, _romTemp] spawn
	{
		params ["_vehX", "_rom"];
		private _route = + _rom;
		/*
		private _color = selectRandom ["ColorGrey", "ColorBlue", "ColorYellow", "ColorRed", "ColorWhite", "ColorOrange"];
		{
			private _marker1 = createMarker [format["%1_wp_%2", _vehx,_forEachIndex], _x];
			_marker1 setMarkerShape "ICON";
			_marker1 setMarkerType "mil_dot";
			_marker1 setmarkerbrush "Solid";
			_marker1 setmarkercolor _color;
			_marker1 setmarkersize [0.5,0.5];
			_marker1 setmarkertext format["%1_wp_%2", _vehx, _forEachIndex];
		} forEach _route;
		*/
		{_x pushBack 15} forEach _route;  //set speed
			
		while {canMove _vehX && {alive (driver _vehX)} && {fuel _vehX > 0}} do
		{
			_vehX setDriveOnPath _route;
			private _lastROM = [((_route select ((count _route) - 1)) select 0), ((_route select ((count _route) - 1)) select 1), 0];
							
			waitUntil
			{
				if (_vehX distance2D _lastROM < 100 && {speed _vehX < 5}) exitWith {true};
				false
			};
							
			sleep 1;
			reverse _route;
			sleep 1;
		};
		
		{doGetOut _x} forEach (crew _vehP);						
		(crew _vehP) orderGetIn false;
		(crew _vehP) allowGetIn false;
	};				

 

 

 

  • Like 2

Share this post


Link to post
Share on other sites

There are other methods to path finding available, if interested.  These are based on shortest road routes from point A to point B.

I believe I still have some test missions for code34's OO_Pathfinding, Spiderswine's Navigation System featuring Dijkstra-Algorithm, and Rube's RUBE_findRoute & RUBE_plotRoute

Rube convoy also.  Note, with the Rube stuff, I've updated code, and removed folders/code which is not used.

  • Like 2

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

×