Jump to content
Sign in to follow this  
zealot111

[Release] Civilian vehicles script

Recommended Posts

Hi,

I am proud to present new civilian vehicles script. It automatically spawns(when players are near the city or town) and despawn civilian vehicles in cities around all Altis (will work with other islands as well).

Vehicles just stand empty near houses in the cities and towns. Useful when you want to give your players additional backup vehicles.

Script tested in MP with ~50 users.

Installation:

Copy to your unpacked mission folder

Include in init.sqf next string:

[] execvm "zlt_civveh.sqf";

zlt_civveh.sqf:

// v1d Civilian Vehicles from [sTELS]Zealot
if (not isserver) exitwith {};

zlt_civveh_round = 0;
zlt_civveh_vehicles = [];
zlt_civveh_spawnedloc = [];

zlt_rnd_seed = 3;

zlt_fnc_random = {
 private ["_a","_c","_m"];
 _a = 75;
 _c = 0;
 _m = 65537;
 zlt_rnd_seed = ( zlt_rnd_seed * _a + _c ) mod (_m);
 (zlt_rnd_seed / _m) ;
};


zlt_fnc_selectrandom = {
_this select floor ( ([] call zlt_fnc_random) * count (_this));
};

zlt_fnc_civvehs = {
_debug = [_this, 1, false] call bis_fnc_param;
_lrange = _this select 2;
_cars = _this select 3;
_t1 = diag_ticktime;


_startpos= _this select 0;
_houses = _startpos nearobjects ["House",_lrange];
//_vehmax =  _cars;
_vehmax = 	(round ((sqrt (count _houses)) * 0.5)) max 1;

_classes = ["C_Offroad_01_F"];

_vehs = 0;
_attemps = 0;

_class = _classes select 0;
zlt_rnd_seed =  ((_startpos select 0) + (_startpos select 1) mod 65537);
while {_vehs < _vehmax and _attemps < 10} do {
	//diag_log ["CV",_startpos, _lrange, _houses];

	_house = _houses call zlt_fnc_selectrandom;

	_housepos = _house modeltoworld [0,0,0];
	_newpos = _housepos findEmptyPosition [ 3 , 15, _class ];
	_houses = _houses - [_house];
	if (count _newpos == 0) then {
		_attemps = _attemps + 1;
	} else {
		_veh = _class createVehicle (_newpos ) ;
		_veh setvariable ["zlt_civveh", true];
		zlt_civveh_vehicles set [count zlt_civveh_vehicles, _veh];

		_direction = (random 360);
		_nearRoads = _newpos nearRoads 10;

		if(count _nearRoads > 0) then
		{
			_road = _nearRoads select 0;
			_roadConnectedTo = roadsConnectedTo _road;
			if (count _roadConnectedTo > 0) then {
				_connectedRoad = _roadConnectedTo select 0;
				_direction = [_road, _connectedRoad] call BIS_fnc_DirTo;
			};
		}; 

		_veh setdir _direction;
		_attemps = 0;
		_vehs = _vehs + 1;
		if (_debug) then {
			["crt"+str(_newpos), _newpos] call zlt_fnc_createmarkerlocal;
		};
	};
};
_t2 = diag_ticktime;
if (_debug) then {
	diag_log str ["civeh.sqf", _t2-_t1];
};
};


zlt_civ_checkloop = {


zlt_civveh_notemptyloc = [];

{
	_veh = vehicle _x;
	_isInVeh = (vehicle _x != _x);


	// or _veh getvariable ["zlt_civ_round", -1] != zlt_civveh_round
	if (!_isInVeh || {_veh isKindOf "LandVehicle"}) then {
		_dist = 1000;
		_locations = nearestLocations [(getpos vehicle _x), ["NameCityCapital","NameCity","NameVillage"], _dist];
		_locations2 = nearestLocations [(getpos vehicle _x), ["NameCityCapital","NameCity","NameVillage"], 2500];
		{
			if ( not (_x in zlt_civveh_spawnedloc) ) then {
				_lrange = 50; _cars = 1;
				switch (type _x) do {
					case ("NameCityCapital") : {_lrange = 250; _cars = 7;};
					case ("NameCity") : {_lrange = 150; _cars = 3;};
				};
				_pos = position _x;
				[ [_pos select 0, _pos select 1, 0], false, _lrange, _cars] call zlt_fnc_civvehs;
				zlt_civveh_spawnedloc pushBack _x; 
//					_x setside east;
			};
		} foreach _locations;
		{
			zlt_civveh_notemptyloc pushBack _x;
		} foreach _locations2;
		if (_isInVeh) then { _veh setvariable ["zlt_civ_round", zlt_civveh_round];};
	};
} foreach (if (player in playableunits) then {playableunits} else {[player]});


{
	_pos = [(position _x) select 0, (position _x) select 1, 0];
	_ent = ( (_pos) nearEntities ["C_Offroad_01_F", 300] );
	{
		if ( _x getvariable ["zlt_civveh", false] and {count crew _x == 0 and fuel _x == 1}) then {
			deletevehicle _x;
		};
	} foreach _ent;
	zlt_civveh_spawnedloc = zlt_civveh_spawnedloc - [_x];
} foreach (zlt_civveh_spawnedloc-zlt_civveh_notemptyloc);

zlt_civveh_round = zlt_civveh_round + 1;
};

while {true} do {
//scan loop

sleep 3.4; //magic number
[] call zlt_civ_checkloop;

};

Edited by zealot111

Share this post


Link to post
Share on other sites

Release frontpaged on the Armaholic homepage.

================================================

We have also "connected" these pages to your account on Armaholic.

This means in the future you will be able to maintain these pages yourself if you wish to do so. Once this new feature is ready we will contact you about it and explain how things work and what options you have.

When you have any questions already feel free to PM or email me!

Share this post


Link to post
Share on other sites

This looks awesome! I'll definitely give it a shot, but have you looked at the load it puts on the server? As in, is this better than just randomly placing a bunch of empty vehicles yourself in the editor?

Share this post


Link to post
Share on other sites

Thanks for this .. .do the civs spawn on clients or server?

Share this post


Link to post
Share on other sites

Appears to be the server. First line of the script exits if the script is run on anything other than the server.

---------- Post added at 19:33 ---------- Previous post was at 18:49 ----------

Did a quick test and looks good so far, but a couple of comments/questions.

1) Looks like it only spawns pickup trucks. Think it's just a matter of adding other civilian options to the _classes array and then selecting one at random from that. Something like:

_classes = ["C_Offroad_01_F","C_Quadbike_01_F","C_Hatchback_01_F","C_Hatchback_01_sport_F","C_SUV_01_F","C_Van_01_transport_F","C_Van_01_box_F","C_Van_01_fuel_F"];
...
_class = (_classes call BIS_fnc_selectRandom);

2) Be nice to be able to feed the script parameters for the range that spawn/despawn occurs.

3) Do you 'reset' the variables on these vehicles so if players take them, they don't get cleaned up by despawn?

Edited by Meatball

Share this post


Link to post
Share on other sites

2) Be nice to be able to feed the script parameters for the range that spawn/despawn occurs.

3) Do you 'reset' the variables on these vehicles so if players take them, they don't get cleaned up by despawn?

Excellent question, #3!

I've been using TPW's excellent "park" script for this in single player missions but he doesn't really optimize anything for MP/DED use so this will be a nice addition! One thing I do like about his park script though is that vehicles spawn with a random amount of fuel in them. I haven't been able to test this new script yet to see if it's similar or?

Share this post


Link to post
Share on other sites

Changing this script to have random fuel should be extremely easy (and a great idea!). Where Zealot creates the vehicle on line 55, just add this after that line.

_veh setFuel (random 1);

Should set the fuel to random percent between 0 and 1, 0 being empty, 1 being full.

Share this post


Link to post
Share on other sites
Changing this script to have random fuel should be extremely easy (and a great idea!). Where Zealot creates the vehicle on line 55, just add this after that line.

_veh setFuel (random 1);

Should set the fuel to random percent between 0 and 1, 0 being empty, 1 being full.

Right now it is not very good idea, because fuel value now used by script to determine whether vehicle were used by players or not.

---------- Post added at 21:21 ---------- Previous post was at 21:09 ----------

Appears to be the server. First line of the script exits if the script is run on anything other than the server.

---------- Post added at 19:33 ---------- Previous post was at 18:49 ----------

Did a quick test and looks good so far, but a couple of comments/questions.

1) Looks like it only spawns pickup trucks. Think it's just a matter of adding other civilian options to the _classes array and then selecting one at random from that. Something like:

_classes = ["C_Offroad_01_F","C_Quadbike_01_F","C_Hatchback_01_F","C_Hatchback_01_sport_F","C_SUV_01_F","C_Van_01_transport_F","C_Van_01_box_F","C_Van_01_fuel_F"];
...
_class = (_classes call BIS_fnc_selectRandom);

2) Be nice to be able to feed the script parameters for the range that spawn/despawn occurs.

3) Do you 'reset' the variables on these vehicles so if players take them, they don't get cleaned up by despawn?

1) and 2) will update in the next version.

3) Script checks fuel state of the vehicles to determine if they were touched by player. If fuel value is not 1, then vehicle was touched and will not be cleaned on despawn.

Share this post


Link to post
Share on other sites

Ah... I see. That's actually a pretty smart way to see if the players have it :)

Share this post


Link to post
Share on other sites
This looks awesome! I'll definitely give it a shot, but have you looked at the load it puts on the server? As in, is this better than just randomly placing a bunch of empty vehicles yourself in the editor?

Script itself doesnt demand much from the server. Sure, if your players scatters on the Altis trying to visit as many cities as possible it could create many vehicles (usually 4-5, 8-9 in the big cities) which can cause syncronization lags, but I tested this script in MP games with 50 players and there were no problems with it.

Share this post


Link to post
Share on other sites
3) Script checks fuel state of the vehicles to determine if they were touched by player. If fuel value is not 1, then vehicle was touched and will not be cleaned on despawn.

WHat an excellent idea. Great thinking out of the box mate.

One suggestion, perhaps an option to lock/unlock the vehicles

Great script!

Share this post


Link to post
Share on other sites

Another thought too. Maybe once one of the vehicles is 'taken' by the players (fuel goes below 1), just wipe out any of the variables related to the script on the vehicle. No reason to have your cleanup constantly pulling those vehicles back through it's loop when normal cleanup scripts can grab it.

Share this post


Link to post
Share on other sites

Is it me? I see a call to the function zlt_fnc_createmarkerlocal but I do not see the function in the script. I can't get this working.

Share this post


Link to post
Share on other sites

I'm trying to get this to work, or something similar anyway. I just want some vehicles to spawn randomly around the map without me having to set the markers for where they spawn.

Followed the OP's instructions but no vehicles spawn. Tested in Kavala.

Share this post


Link to post
Share on other sites
I'm trying to get this to work, or something similar anyway. I just want some vehicles to spawn randomly around the map without me having to set the markers for where they spawn.

Followed the OP's instructions but no vehicles spawn. Tested in Kavala.

Try this script variant

// v1d Civilian Vehicles from [sTELS]Zealot
if (not isserver) exitwith {};

zlt_civveh_round = 0;
zlt_civveh_vehicles = [];
zlt_civveh_spawnedloc = [];

zlt_rnd_seed = 3;

zlt_fnc_random = {
 private ["_a","_c","_m"];
 _a = 75;
 _c = 0;
 _m = 65537;
 zlt_rnd_seed = ( zlt_rnd_seed * _a + _c ) mod (_m);
 (zlt_rnd_seed / _m) ;
};


zlt_fnc_selectrandom = {
_this select floor ( ([] call zlt_fnc_random) * count (_this));
};

zlt_fnc_civvehs = {
_debug = [_this, 1, false] call bis_fnc_param;
_lrange = _this select 2;
_cars = _this select 3;
_t1 = diag_ticktime;


_startpos= _this select 0;
_houses = _startpos nearobjects ["House",_lrange];
//_vehmax =  _cars;
_vehmax = 	(round ((sqrt (count _houses)) * 0.5)) max 1;

_classes = ["C_Offroad_01_F"];

_vehs = 0;
_attemps = 0;

_class = _classes select 0;
zlt_rnd_seed =  ((_startpos select 0) + (_startpos select 1) mod 65537);
while {_vehs < _vehmax and _attemps < 10} do {
	//diag_log ["CV",_startpos, _lrange, _houses];

	_house = _houses call zlt_fnc_selectrandom;

	_housepos = _house modeltoworld [0,0,0];
	_newpos = _housepos findEmptyPosition [ 3 , 15, _class ];
	_houses = _houses - [_house];
	if (count _newpos == 0) then {
		_attemps = _attemps + 1;
	} else {
		_veh = _class createVehicle (_newpos ) ;
		_veh setvariable ["zlt_civveh", true];
		zlt_civveh_vehicles set [count zlt_civveh_vehicles, _veh];

		_direction = (random 360);
		_nearRoads = _newpos nearRoads 10;

		if(count _nearRoads > 0) then
		{
			_road = _nearRoads select 0;
			_roadConnectedTo = roadsConnectedTo _road;
			if (count _roadConnectedTo > 0) then {
				_connectedRoad = _roadConnectedTo select 0;
				_direction = [_road, _connectedRoad] call BIS_fnc_DirTo;
			};
		}; 

		_veh setdir _direction;
		_attemps = 0;
		_vehs = _vehs + 1;
		if (_debug) then {
			["crt"+str(_newpos), _newpos] call zlt_fnc_createmarkerlocal;
		};
	};
};
_t2 = diag_ticktime;
if (_debug) then {
	diag_log str ["civeh.sqf", _t2-_t1];
};
};


zlt_civ_checkloop = {


zlt_civveh_notemptyloc = [];

{
	_veh = vehicle _x;
	_isInVeh = (vehicle _x != _x);


	// or _veh getvariable ["zlt_civ_round", -1] != zlt_civveh_round
	if (!_isInVeh || {_veh isKindOf "LandVehicle"}) then {
		_dist = 1000;
		_locations = nearestLocations [(getpos vehicle _x), ["NameCityCapital","NameCity","NameVillage"], _dist];
		_locations2 = nearestLocations [(getpos vehicle _x), ["NameCityCapital","NameCity","NameVillage"], 2500];
		{
			if ( not (_x in zlt_civveh_spawnedloc) ) then {
				_lrange = 50; _cars = 1;
				switch (type _x) do {
					case ("NameCityCapital") : {_lrange = 250; _cars = 7;};
					case ("NameCity") : {_lrange = 150; _cars = 3;};
				};
				_pos = position _x;
				[ [_pos select 0, _pos select 1, 0], false, _lrange, _cars] call zlt_fnc_civvehs;
				zlt_civveh_spawnedloc pushBack _x; 
//					_x setside east;
			};
		} foreach _locations;
		{
			zlt_civveh_notemptyloc pushBack _x;
		} foreach _locations2;
		if (_isInVeh) then { _veh setvariable ["zlt_civ_round", zlt_civveh_round];};
	};
} foreach (if (player in playableunits) then {playableunits} else {[player]});


{
	_pos = [(position _x) select 0, (position _x) select 1, 0];
	_ent = ( (_pos) nearEntities ["C_Offroad_01_F", 300] );
	{
		if ( _x getvariable ["zlt_civveh", false] and {count crew _x == 0 and fuel _x == 1}) then {
			deletevehicle _x;
		};
	} foreach _ent;
	zlt_civveh_spawnedloc = zlt_civveh_spawnedloc - [_x];
} foreach (zlt_civveh_spawnedloc-zlt_civveh_notemptyloc);

zlt_civveh_round = zlt_civveh_round + 1;
};

while {true} do {
//scan loop

sleep 3.4; //magic number
[] call zlt_civ_checkloop;

};

Share this post


Link to post
Share on other sites

How do you add different vehicles? All it spawns for me is the Offroad, I've modified the classes and all but it doesn't seem to work.

Share this post


Link to post
Share on other sites

hi zealot!

 

Just a suggestion you may see working in Antistasi. I use more or less the same method to spawn the vehicles, but I create them slightly (1-2 meters) on one side of the road, so they visually seem parked.

 

1/20 times they may explode because the road hasn't room enough and you spawn them in a house... but I think it worth the risk.

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  

×