Tuesday, April 14, 2009

Traffic Script Revisited

Well this following script may be considered flogging a dead horse, but it illustrates a few key points about using the format command together with iteration. Below is the script, and the init.sqf, both of which intended to be saved to your PC.

//init.sqf
sleep 0.1; // wait until the sim is really started (post-brief)
[car1, "M", "NOCYCLE"] execVM "PathA.sqf"; // exec script for car1 on path M (e.g. m1, m2 , m3, m4), no cycle.


// PathA.sqf
// Version 1.01a (31 OCT 08)
// by Jamie Coombs
// Purpose: To facilitate vehicle movement by markers on various terrains, allows ability to cycle.
// Useage: [car, prefix, "NOCYCLE"] execVM "PathA.sqf";
// car - the name of the car as it is in the OME/RTE
// prefix - the marker prefix for the pre-set track, e.g. if your marker series is named A1 thru A20 the prefix is "A". The vehicle will cycle through all 20 markers
// "NOCYCLE" - Optional switch, stops vehicles cycling through markers once the end is reached, by default the vehicles cycle the waypoints.
// TODO: Add further error handling, with hint and warning messages.

_car = _this select 0; // select the first index as the car name.
_track = _this select 1; // Marker track prefix e.g. "A"

_markArr = markers; // All markers in current scenario returned assigned to var as array (_markArr)

// Don't allow damage, just in case, to ensure that civilian cars keep moving to maintain density.
_car allowDammage false;

_i = 1;

// always loop through the following.
while {true} do {
_car setSpeedMode "NORMAL"; // set the speed mode to normal to prevent bad driving.
_wp = format ["%1%2", _track, _i]; // put _track and _i together to create our next WP name
// if the waypoint marker is in the scenario, then go to it!
if (_wp in _markArr) then {

//DEBUG?
if ("DEBUG" in _this) then { hint _wp;};

// Check car distance to waypoint Assign to a variable.
_cond = {(_car distance (getMarkerPos _wp)) <= 80}; // Move to the waypoint.
 _car Move (getMarkerPos _wp); // Wait for the condition to be true/satisfied (car within 80m of _wp) 
waituntil _cond; // Check distance again, reassign the variable 
_cond = {(_car distance (getMarkerPos _wp)) <= 10};
 // Slow down to take the turn! 
// TODO: judge angle between car, _wp and (_wp+1) and determine if slowing down is necessary 
_car setSpeedMode "LIMITED"; 
// Wait for the condition to be true. 
waitUntil _cond; 
// increase _i by one 
_i = _i+1; 
// sleep a bit 
sleep 0.1; 
// cycle again 
} else {
 // _wp is not in the mission as a marker 
if (_i == 1 || "NOCYCLE" in _this) then { exitWith {};}; // Exit, we're done for now. No next _wp OR potential error, but I'm a script, what do I know? 
_i = 1; // if no problems and we're cycling, start the path again! from M1 
};
 }; // EOF

You can get the associated documentation for this here: PDF . To test the script, create a new scenario with a car and four markers, named A1, A2, A3 and A4. Name the car as you wish, put down a separate player unit (to be you viewing the scenario). Alter the supplied init.sqf above as appropriate.

Questions? Please use the comment feature, all responses will be via the comment feature so anyone else having trouble may get their solution there as well.

Jamie.

No comments:

Post a Comment