Jump to content
iceman77

Light Vehicle Respawn Script

Recommended Posts

rGv3c6G.png

Official Example Mission:

xYqfb0d.png

Note: ALiVE users need to select "Virtualize synced units only" in the ALiVE Virtual AI system module.

Official fn_vehRespawn.sqf

/* ----------------------------------------------------------------------------------------------------

File: fn_vehRespawn.sqf

Author: Iceman77

Description:
- Monitor a vehicle and "respawn" it if it's destroyed or abandoned 
- Can be used on vehicles
- Set vehicle init upon respawn (optional)
- Store and keep the vehicle's variable name automatically

Parameter(s):
- _this select 0: < OBJECT > - VEHICLE 
- _this select 1: < NUMBER > - ABANDONED DELAY IN SECONDS
- _this select 2: < NUMBER > - DESTROYED DELAY IN SECONDS
- _this select 3: < CODE > - FUNCTION THE VEHICLE CALLS UPON RESPAWN (OPTIONAL)

Usage (Vehicle init Line): 
_nul = [ this, 120, 60, LVR_fnc_hunterInit ] spawn LVR_fnc_vehRespawn << have the vehicle call the custom LVR Function upon respawn 
   _nul = [this, 20, 10, {_this setVehicleAmmo 0;  _this setDammage 0.5;}] spawn LVR_fnc_vehRespawn; << Run code directly on the vehicle when it respawns
_nul = [ this, 120, 60 ] spawn LVR_fnc_vehRespawn; << Default usage

---------------------------------------------------------------------------------------------------- */


// SERVER CODE
if ( ! ( isServer ) ) exitWith {};

// SET SCOPE OF LOCAL VARIABLES
private ["_veh","_abandonDelay","_destroyedDelay","_vehInit","_vehName","_vehDir","_vehPos","_vehtype","_abandoned","_dead"];

// PASSED ARGUMENTS ( OBJECT, ABANDONED DELAY, DESTROYED DELAY )
_veh = 			 [_this, 0, objNull, [objNull]] call BIS_fnc_param; 
_abandonDelay =  	 [_this, 1, 60, [0]] call BIS_fnc_param; 
_destroyedDelay =	 [_this, 2, 60, [0]] call BIS_fnc_param; 
_vehInit =        [_this, 3, {}, [{}] ] call BIS_fnc_param;

// STORE THE VEHICLES NAME, DIRECTION, POSITION AND TYPE
_vehName =        vehicleVarName _veh;
_vehDir = 		 getDir _veh; 
_vehPos = 		 getPos _veh; 
_vehtype = 		 typeOf _veh; 

// START LOOP TO MONITOR THE VEHICLE
while { true } Do {

sleep 1;

// IF THE VEHICLE IS ALIVE AND CAN MOVE AND IS EMPTY THEN THE VEHICLE IS ABANDONED
if ( ( alive _veh ) && { ( canMove _veh ) && { { ( alive _x ) } count ( crew _veh ) == 0 } } ) then {

	_abandoned = true;

		// COUNTDOWN THE ABANDONED DELAY - STALL SCRIPT HERE
		for "_i" from 0 to _abandonDelay do {  

			// IF THE VEHICLE ISN'T EMPTY, OR IS DESTROYED, OR IF IT CAN'T MOVE THEN IT'S NOT ABANDONED SO EXIT THE COUNTDOWN EARLY - CONTINUE THE SCRIPT
			if ( { ( alive _x ) } count (crew _veh) > 0 || { !( alive _veh ) || { !( canMove _veh ) } } ) exitWith { _abandoned = false; };
			sleep 1;  

		};

	// IF THE VEHICLE IS ABANDONED AND ISN'T CLOSE TO IT'S STARTING POSITION THEN DELETE IT AND CREATE A NEW ONE AT THE STARTING POSITION
	if ( ( _abandoned ) && { _veh distance _vehPos > 10 } ) then {

		deleteVehicle _veh;
		sleep 1;
		_veh = createVehicle [ _vehtype, _vehPos, [], 0, "CAN_COLLIDE" ];
		_veh setPos [ ( _vehPos select 0 ), ( _vehPos select 1 ), 0 ];
		_veh setDir _vehDir;
		_veh call _vehInit;
		if (_vehName != "") then {
			missionNamespace setVariable [_vehName, _veh];
			publicVariable _vehName;
		};	
	};
};

// IF THE VEHICLE IS DESTROYED OR IF IT CAN'T MOVE THEN ITS DEAD
if ( !( alive _veh ) || { !( canMove _veh ) } ) then {

	_dead = true;

		// COUNTDOWN THE DEAD DELAY - STALL SCRIPT HERE
		for "_i" from 0 to _destroyedDelay do {  

			// IF THE VEHICLE ISN'T EMPTY, OR IF IT CAN MOVE ( HAS BEEN REPAIRED ) THEN IT'S NOT DEAD SO EXIT THE COUNTDOWN EARLY - CONTINUE THE SCRIPT
			if ( { ( alive _x ) } count ( crew _veh ) > 0 || { ( canMove _veh ) } ) exitWith { _dead = false; };
			sleep 1;  

		};

	// IF THE VEHICLE IS DEAD THEN DELETE IT AND CREATE A NEW ONE AT THE STARTING POSITION
	if ( _dead ) then {

		deleteVehicle _veh;
		sleep 1;
		_veh = createVehicle [ _vehtype, _vehPos, [], 0, "CAN_COLLIDE" ];
		_veh setPos [ ( _vehPos select 0 ), (_vehPos select 1 ), 0 ];
		_veh setDir _vehDir;
		_veh call _vehInit;
		if (_vehName != "") then {
			missionNamespace setVariable [_vehName, _veh];
			publicVariable _vehName;
		};				
	};
};
};

-------------------------------------------------------------- ALL CONTENT BELOW IS OPTIONAL ----------------------------------------------------------

Bonus LVR script (Not required)

For Dynamic Sector Control Vehicle Respawns

- Assign each vehicle to a sector

- Vehicles respawn into the appropriate vehicle type based on which side owns the sector that the vehicle belongs to.

- Think BF2 / BF3 vehicle respawn in conquest mode.

- I can provide an example mission if needed.

Installation & suggested usage:

-Installation is essentially the same as normal LVR. Place a vehicle, and the usage code into it's init line.

- Requires one empty marker per sector with the location's name as custom marker text. This is used to notify the players at which sector the vehicle has respawned,

via notification message. So.. if I've a sector deemed power plant, I would place one empty marker near that sector's center, with any variable name,

but with the text power plant.

Personal Suggestions:

-To be fair to all participants, the initial vehicle should be a civ vehicle. It can then respawn into a NATO/CSAT/INDEP vehicle (if applicable).

- Follow the comment's instructions carefully and see how to assign a vehicle to a sector and more.

- Keep it fair. Don't have an MRAP spawning for one side and an MBT for another. Take a look at the vehicle type array and the usage example in the comment.

Options:

- If you would like the vehicles to "refresh" even if they are at their initial spawn position (at the sector), remove && {(_veh distance _vehPos > 10)} from the _abandoned check. Otherwise, vehicle's that aren't damaged, and sitting at their initial spawn point, will not resapwn. Player's will need to A) Move the vehicle away or B) destroy the vehicle before a new one will respawn. It's really up to you. I personally like the distance check over the "refresh" option. Though the "refresh" option is nice too.

Notes:

-This version of LVR doesn't retain the vehicle's variable name. It doesn't support vehicle init respawn. It's essentially "old" style LVR, but converted for sectors.

description.ext

class CfgNotifications 
{
class BCM_GENERIC_DEFAULT_MSG 
{
	color[] = {1,1,1,0.75};
	colorIconPicture[] = {1,1,1,0.75};
	colorIconText[] = {1,1,1,0.75};
	title = "%1";
	description = "%2";
	iconPicture = "%3";
	iconText = "%4";
	duration = 7;		
};
};

sector_fn_vehrespawn.sqf

/* ----------------------------------------------------------------------------------------------------

File: fn_vehRespawn.sqf

Author: Iceman77

Description:
	- MODIFIED version of my LVR to accomodate dynamic vehicle spawning at sectors based on current owner's side
	- Monitor a vehicle and "respawn" it if it's destroyed or abandoned 
	- Can be used on multiple vehicles

Parameter(s):
	- _this select 0: < OBJECT > 	- VEHICLE TO MONITOR.
	- _this select 1: < NUMBER > 	- ABANDONED DELAY IN SECONDS.
	- _this select 2: < NUMBER > 	- DESTROYED DELAY IN SECONDS.
	- _this select 3: < OBJECT > 	- SECTOR THE VEHICLE BELONGS TO.
	- _this select 4: < ARRAY >  	- ARRAY OF STRINGS. VEHICLE CLASSNAMES. UPON REFRESH, ONE WILL BE CREATED BASED ON WHO OWNS THE SECTOR.
									SELECT 0 - DEFAULT VEHICLE TYPE
									SELECT 1 - OPFOR VEHICLE TYPE
									SELECT 2 - BLUFOR VEHICLE TYPE
									SELECT 3 - INDEPENDENT VEHICLE TYPE
									e.g.:
									["defaultVehicleClassName","bluforVehicleclassName","opforVehicleClassName","independentVehicleClassName"]

Usage Example (Vehicle init Line): _nul = [ this, 60, 60, sector1, [ typeOf this,"B_MRAP_01_hmg_F","O_MRAP_02_hmg_F","I_MRAP_03_hmg_F" ] ] spawn ICE_fnc_vehRespawn 

---------------------------------------------------------------------------------------------------- */

if ( ! ( isServer ) ) exitWith {};

private ["_veh","_abandonDelay","_destroyedDelay","_sector","_vehArray","_defaultVehicle","_bluforVehicle","_opforVehicle","_independentVehicle","_vehDir","_vehPos","_vehType","_abandoned","_dead","_markerText","_displayName","_picture"];

_veh 			= [ _this, 0, objNull, [ objNull ] ] call BIS_fnc_param; 
_abandonDelay 	= [ _this, 1, 60, [ 0 ] ] call BIS_fnc_param; 
_destroyedDelay 	= [ _this, 2, 60, [ 0 ] ] call BIS_fnc_param; 
_sector 			= [ _this, 3, objNull, [ objNull ] ] call BIS_fnc_param;
_vehArray 		= [ _this, 4, [ ], [ [ ] ] ] call BIS_fnc_param; 
_defaultVehicle 	= ( _vehArray select 0 );
_bluforVehicle 	= ( _vehArray select 1 );
_opforVehicle 	= ( _vehArray select 2 );
_indepVehicle 	= ( _vehArray select 3 );
_vehDir 			= getDir _veh; 
_vehPos 			= getPos _veh; 
_abandoned 		= false;
_dead 			= false;

while { true } Do {
sleep 1;
if ( ( alive _veh ) && { ( canMove _veh ) && { { ( alive _x ) } count ( crew _veh ) == 0 } } ) then {
	_abandoned = true;
	for "_i" from 0 to _abandonDelay do {  
		if ( { ( alive _x ) } count (crew _veh) > 0 || { !( alive _veh ) || { !( canMove _veh ) } } ) exitWith { 
			_abandoned = false; 
		};
		sleep 1;  		
	};

	if ( _abandoned && {(_veh distance _vehPos > 10)}) then {
		deleteVehicle _veh;

		_vehType = switch ( _sector getVariable "owner" ) do {
			case West:{ _bluforVehicle; };
			case East:{ _opforVehicle; };
			case Independent:{ _indepVehicle; };
			default { _defaultVehicle; };
		};

		sleep 1;
		_veh = createVehicle [ _vehtype, _vehPos, [], 0, "CAN_COLLIDE" ];
		_veh addEventHandler ["getIn","_null=call BCM_fnc_crewRestrictions"];
		_veh setDir _vehDir;
		_veh setPos [ ( _vehPos select 0 ), (_vehPos select 1 ), 0 ];
		_displayName = getText (configFile >> "CfgVehicles" >> _vehType >> "displayName");
		_picture = getText (configFile >> "CfgVehicles" >> _vehType >> "picture");

		{
			if ((markerPos _x) distance _sector < 25 && (markerType _x == "EMPTY")) then {
				_markerText = markerText _x; 
			};
		} forEach allMapMarkers;

		[
			[
				"BCM_GENERIC_DEFAULT_MSG",
				[
					"Vehicle Respawn", 
					format ["A %1 has spawned at the %2", _displayName, _markertext],
					_picture,
					""
				]
			],
			"BIS_fnc_showNotification", 
			_sector getVariable "owner" 
		] call BIS_fnc_MP;
	};
};

if ( !( alive _veh ) || { !( canMove _veh ) } ) then {
	_dead = true;
	for "_i" from 0 to _destroyedDelay do {  
		if ( { ( alive _x ) } count ( crew _veh ) > 0 || { ( canMove _veh ) } ) exitWith { _dead = false; };
		sleep 1;  
	};

	if ( _dead ) then {
		deleteVehicle _veh;

		_vehType = switch ( _sector getVariable "owner" ) do {
			case West:{ _bluforVehicle; };
			case East:{ _opforVehicle; };
			case Independent:{ _indepVehicle; };
			default { _defaultVehicle; };
		};

		sleep 1;
		_veh = createVehicle [ _vehtype, _vehPos, [], 0, "CAN_COLLIDE" ];
		_veh addEventHandler ["getIn","_null=call BCM_fnc_crewRestrictions"];
		_veh setDir _vehDir;
		_veh setPos [ ( _vehPos select 0 ), (_vehPos select 1 ), 0 ];
		_displayName = getText (configFile >> "CfgVehicles" >> _vehType >> "displayName");
		_picture = getText (configFile >> "CfgVehicles" >> _vehType >> "picture");

		{
			if ((markerPos _x) distance _sector < 25 && (markerType _x == "EMPTY")) then {
				_markerText = markerText _x;
			};
		} forEach allMapMarkers;

		[
			[
				"BCM_GENERIC_DEFAULT_MSG",
				[
					"Vehicle Respawn", 
					format ["A %1 has spawned at the %2", _displayName, _markertext],
					_picture,
					""
				]
			],
			"BIS_fnc_showNotification", 
			_sector getVariable "owner" 
		] call BIS_fnc_MP;
	};		
};	
};

nil

-------------------------------------------------------------- Other projects related to LVR ----------------------------------------------------------

Not So Light Vehicle Respawn Framework ( BY LARROW )

- Tons of features

Edited by Iceman77

Share this post


Link to post
Share on other sites

Updated:

- Refined some conditions to use lazy evaluation.

- Cleaned up un-needed checks

Edited by Iceman77
Vehicle Respawn Module still broken

Share this post


Link to post
Share on other sites
You my friend are a genius, thank you so much for your help and time :)

Hardly. But , Yeah np. I was hoping the vehicle respawn module would be fixed. But it's not 100%. You should see Larrow's version of LVR aka NSLVR. Now that on the other hand is genius. Maybe I'll post it here if he allows it.

Edited by Iceman77

Share this post


Link to post
Share on other sites
Maybe I'll post it here if he allows it.
Sure chap post away, still a few things i want to tweak but seem solid enough, would be good to get some feedback on what is is not working properly, only so many variations i can test on my own :) .

Share this post


Link to post
Share on other sites

Updated the original post to contain Larrow's version.

Share this post


Link to post
Share on other sites
Guest

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!

** Larrow's version not (yet) mirrored.

Share this post


Link to post
Share on other sites

Thanks Foxhound. Would it be possible to include the spoiler on the download page? As there's two different version (not counting larrows). The "light version" is a simple copy & paste from the spoiler, the "advanced version" works differently and actually contains extra files and vehRespawn.sqf is actually quite a bit different for advancced version.

Share this post


Link to post
Share on other sites
Guest

Light version added to the page as copy/paste, page renamed to "Advanced Vehicle Respawn Script".

I hope this is all set up properly now, if not feel free to PM me! ;)

Share this post


Link to post
Share on other sites

Thanks for your awesome script! I used it successfully on Altis but now I made a mission on the ported Takistan map and every time I use set the initialization it spawns another copy of the vehicle where the original one was and they explode. I was wondering how I can prevent this? I'm using the same init command as Altis

_nul = [this, 99, 1, {}] execVM "vehrespawn.sqf";

Edited by Opendome

Share this post


Link to post
Share on other sites
Thanks for your awesome script! I used it successfully on Altis but now I made a mission on the ported Takistan map and every time I use set the initialization it spawns another copy of the vehicle where the original one was and they explode. I was wondering how I can prevent this? I'm using the same init command as Altis

_nul = [this, 99, 1, {}] execVM "vehrespawn.sqf";

same problem here, mine is on takistan as well.

Share this post


Link to post
Share on other sites

Oh weird! I wonder what thats all about and I wonder if theres a respawn script that will work with Takistan

Share this post


Link to post
Share on other sites
Oh weird! I wonder what thats all about and I wonder if theres a respawn script that will work with Takistan

I've tried like 6 now and none work. did you try on other arma 2 maps?

Share this post


Link to post
Share on other sites

No I haven't tried other arma 2 maps yet but itd def be interesting to see!

Share this post


Link to post
Share on other sites
No I haven't tried other arma 2 maps yet but itd def be interesting to see!

just tried Utes, same thing.

Share this post


Link to post
Share on other sites
just tried Utes, same thing.

Oh weird! I wonder how we can fix/get around this

Also so far Ive been unable to get ALIVEs support units to spawn on the map as well. I place the Support Module and sync transport and cas to it but they do not spawn/appear on map like they should

Edited by Opendome

Share this post


Link to post
Share on other sites
this is one of the ones I tried. just tried it again, still not doing anything.

Now that I think about it, I have a mission on Chernarus with that script working on two Hunters. It works in the editor and on my dedi. Might be something you are doing. How are you calling the script?

Share this post


Link to post
Share on other sites
Hardly. But , Yeah np. I was hoping the vehicle respawn module would be fixed.

I'm not aware of it being broken. Could you please send me a link to feedback tracker issue where the problem is described?

Share this post


Link to post
Share on other sites
I'm not aware of it being broken. Could you please send me a link to feedback tracker issue where the problem is described?

At the time I'd posted that, vehicles that were destroyed at their starting points wouldn't spawn at the exact location. It had appeared to me that the newly spawned vehicle was being created at the same time the wreck was being deleted. Causing the newly spawned vehicle to spawn off of the intended position to avoid collision. Maybe a small sleep between deleting the wreck and creating the new vehicle could help?

http://feedback.arma3.com/view.php?id=16186

Share this post


Link to post
Share on other sites
Now that I think about it, I have a mission on Chernarus with that script working on two Hunters. It works in the editor and on my dedi. Might be something you are doing. How are you calling the script?

just in the init line of the vehicle. I followed the guides of every script precisely. you're using the Arma 3 Map Pack mod right? not All in Arma?

I noticed that both me and opendome are using ALiVE, maybe that could be a cause? opendome, what other mods do you have?

Share this post


Link to post
Share on other sites
just in the init line of the vehicle. I followed the guides of every script precisely. you're using the Arma 3 Map Pack mod right? not All in Arma?

I noticed that both me and opendome are using ALiVE, maybe that could be a cause? opendome, what other mods do you have?

The Taskistan one was with AiA and the Chernarus was with A3MP. Both worked as intended. I have not tried with ALiVE.

Share this post


Link to post
Share on other sites
The Taskistan one was with AiA and the Chernarus was with A3MP. Both worked as intended. I have not tried with ALiVE.

I just tried it on altis with ALiVE disabled and uninstalled and it's still not doing anything.

Share this post


Link to post
Share on other sites

You guys should discuss the Victor's respawn script in it's own thread? lol

Share this post


Link to post
Share on other sites
Guest
This topic is now closed to further replies.

×