Bones51

[RELEASE] Vehicle Ammo Save Script (Updated 12 Aug 18)

100 posts in this topic

#### PUBLIC SERVICE ANNOUNCEMENT - Due to a change in the way data is stored in the database, If you are upgrading from v2 to the latest version you will need to follow the upgrade instructions below or you will crash your server!####

As requested by a few people, my stand-alone vehicle ammo save script.

https://github.com/Bones50/ammoSave

Reverse Engineered from Rod Serlings awesome AVS package.

All Credit to Rod for his original epic work.

Change Log -

v2 - Updated to support exile 1.04 and in particular Virtual Garage, and also to support tank ammo saving as requested. (Note tank ammo saving is a bit of a hack due to BIS bug, so let me know if you see any issues).

v3 (5/8) - Updated to support full pylon configurations for aircraft and UAV's so all aircraft should now work correctly. Note: Ensure your server is using the new version/classnames of aircraft/UAV's (will have dynamicLoadout in the classname) where appropriate, otherwise this script will break. If a vehicle is having problems, check there is not a newer version of the vehicle using dynamic loadout as the first step.

v3.1 (12/8) - Minor bugfixes and adjusted the upgrade instructions to avoid rpt spam.

UPGRADE INSTRUCTIONS FROM V2 to V3.x

If upgrading from the older version please follow the below instructions:

Spoiler

1. Update your server PBO files as per normal.

2. Find and Open the file "ExileServer_object_vehicle_database_load.sqf"

3. Replace the contents with the below:


/**
 * ExileServer_object_vehicle_database_load
 *
 * Exile Mod
 * exile.majormittens.co.uk
 * © 2015 Exile Mod Team
 *
 * This work is licensed under the Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International License. 
 * To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-nd/4.0/.
 */

private["_savedMagazines", "_vehicleID", "_data", "_position", "_vectorDirection", "_vectorUp", "_pinCode", "_texture", "_vehicleObject", "_lock", "_unlockInSafeZonesAfterRestart", "_isLocked", "_hitpoints", "_cargoContainers", "_query", "_oldTest1"];
_vehicleID = _this;
_data = format ["loadVehicle:%1", _vehicleID] call ExileServer_system_database_query_selectSingle;
_position = [_data select 8, _data select 9, _data select 10];
_vectorDirection = [_data select 11, _data select 12, _data select 13];
_vectorUp = [_data select 14, _data select 15, _data select 16];
_pinCode = _data select 20;
_texture = _data select 21;
try 
{
	if !((vectorMagnitude _vectorUp) isEqualTo 1) then 
	{
		throw true;
	};
	if ((_vectorUp select 0) > 0.95) then 
	{
		throw true;
	};
	if ((_vectorUp select 1) > 0.95) then 
	{
		throw true;
	};
	if ((_vectorUp select 2) isEqualTo 0) then 
	{
		throw true;
	};
}
catch
{
	_vectorUp = [0, 0, 1];
};
_vehicleObject = [(_data select 1), _position, [_vectorDirection, _vectorUp], true,_pinCode] call ExileServer_object_vehicle_createPersistentVehicle;
_vehicleObject setVariable ["ExileDatabaseID", _vehicleID];
_vehicleObject setVariable ["ExileOwnerUID", (_data select 3)];
_vehicleObject setVariable ["ExileMoney", (_data select 23), true];
_lock = (_data select 4);
_unlockInSafeZonesAfterRestart = (getNumber (configFile >> "CfgSettings" >> "VehicleSpawn" >> "unlockInSafeZonesAfterRestart")) isEqualTo 1;
_isLocked = (_lock isEqualTo -1);
if (_isLocked) then 
{
	if (_unlockInSafeZonesAfterRestart) then 
	{
		if (_position call ExileClient_util_world_isInTraderZone) then 
		{
			_isLocked = false;
		};
	};
};
if (_isLocked) then
{
	_vehicleObject setVariable ["ExileIsLocked", -1];
	_vehicleObject lock 2;
	_vehicleObject enableRopeAttach false;
}
else
{
	_vehicleObject setVariable ["ExileIsLocked", 0];
	_vehicleObject lock 0;
	_vehicleObject enableRopeAttach true;
};
_vehicleObject setFuel (_data select 5);
_vehicleObject setDamage (_data select 6);
_hitpoints = _data select 7;
if ((typeName _hitpoints) isEqualTo "ARRAY") then 
{
	{
		_vehicleObject setHitPointDamage [_x select 0, _x select 1];
	}
	forEach _hitpoints;
};
[_vehicleObject, (_data select 17)] call ExileServer_util_fill_fillItems;
[_vehicleObject, (_data select 18)] call ExileServer_util_fill_fillMagazines;
[_vehicleObject, (_data select 19)] call ExileServer_util_fill_fillWeapons;
_cargoContainers = format ["loadVehicleContainer:%1", _vehicleID] call ExileServer_system_database_query_selectSingle;
if ((typeName _cargoContainers) isEqualTo "ARRAY") then 
{
	if !(_cargoContainers isEqualTo []) then
	{
		[_vehicleObject, (_cargoContainers select 0)] call ExileServer_util_fill_fillContainers;
	};
};
if !(_texture isEqualTo "") then
{
	{
		_vehicleObject setObjectTextureGlobal [_forEachIndex, _texture select _forEachIndex];
	}
	forEach _texture;
};
_vehicleObject enableSimulationGlobal false;
if (getNumber(missionConfigFile >> "CfgSimulation" >> "enableDynamicSimulation") isEqualTo 1) then 
{
	_vehicleObject enableDynamicSimulation true;
}
else
{
	_vehicleObject call ExileServer_system_simulationMonitor_addVehicle;
};
if (_vehicleObject call ExileClient_util_world_isInTraderZone) then 
{
	_vehicleObject allowDamage false;
};

//Bones Ammo Save Addition
_query = format["getVehicleAmmo:%1", _vehicleID] call ExileServer_system_database_query_selectSingle;
_savedMagazines = _query select 0;
if(count _savedMagazines == 0) exitwith {};
_oldTest1 = (_savedMagazines select 0) select 1;
if (typename _oldTest1 == "ARRAY") then 
{
	_vehicleobject setVehicleAmmo 0;

	if (typeName _savedMagazines isEqualTo "ARRAY") then
	{
		if (typeName (_savedMagazines select 0) isEqualTo "ARRAY") then
		{
			{
				_turretPath = _x select 0;
				for "_i" from (count _x - 1) to 1 step -1 do
				{
					_magData = _x select _i;
					_magClass = _magData select 0;
					_ammoCount = _magData select 1;
					if (["120mm",_magClass] call BIS_fnc_inString || ["125mm",_magClass] call BIS_fnc_inString || ["105mm",_magClass] call BIS_fnc_inString || ["L30A1_Cannon",_magClass] call BIS_fnc_inString || ["2A46",_magClass] call BIS_fnc_inString || ["100mm",_magClass] call BIS_fnc_inString) then
					{
						_vehicleobject addMagazineTurret [_magClass,_turretPath,_ammoCount];
					}
					else
					{
						_maxMagAmmo = (configFile >> "CfgMagazines" >> _magClass >> "count") call BIS_fnc_getCfgData;
						_numMags = ceil (_ammoCount / _maxMagAmmo);
			
						while {_numMags > 1} do
						{
							_vehicleobject addMagazineTurret [_magClass, _turretPath];
							_numMags = _numMags - 1;
							_ammoCount = _ammoCount - _maxMagAmmo;
						};
						_vehicleobject setMagazineTurretAmmo [_magClass, _ammoCount, _turretPath];			
					};
				};
			} forEach _savedMagazines;
		};
	}
	else
	{
		diag_log format["AMMO ERROR - INVALID MAGAZINE SAVED IN DATABASE: %1", _savedMagazines];
		diag_log format["AMMO ERROR - Magazines should be saved an array. Blank entries should be: [] Verify your database."];
	};
} else
{
	_vehicleObject setVehicleAmmo 0;

	{
		private ["_turretPath", "_pylonIndex", "_magClass","_bulletAmount", "_magClass", "_maxMagAmmo", "_numMags"];
		_turretPath = _x select 0;
		_pylonIndex = _x select 1;
		_magClass = _x select 2;
		_bulletAmount = _x select 3;
		
		if (["120mm",_magClass] call BIS_fnc_inString || ["125mm",_magClass] call BIS_fnc_inString || ["105mm",_magClass] call BIS_fnc_inString || ["L30A1_Cannon",_magClass] call BIS_fnc_inString || ["2A46",_magClass] call BIS_fnc_inString || ["100mm",_magClass] call BIS_fnc_inString) then
		{
			_vehicleObject removeMagazinesTurret [_magClass, _turretPath];
			_vehicleObject addMagazineTurret [_magClass,_turretPath,_bulletAmount];
		} else
		{
			//DO THIS IF SMOKE
			if (["smoke",_magclass] call BIS_fnc_inString) then
			{
				_vehicleObject removeMagazinesTurret [_magClass, _turretPath];
				_vehicleObject removeWeaponTurret ["SmokeLauncher", _turretPath];
				_vehicleObject addMagazineTurret [_magClass,_turretPath,_bulletAmount];
				_vehicleObject addWeaponTurret ["SmokeLauncher", _turretPath];
			} else
			{
				//DO THIS IF CHAFF
				if (["chaff",_magclass] call BIS_fnc_inString) then
				{
					_vehicleObject removeMagazinesTurret [_magClass, _turretPath];
					_vehicleObject removeWeaponTurret ["CMFlareLauncher", _turretPath];
					_vehicleObject addMagazineTurret [_magClass,_turretPath,_bulletAmount];
					_vehicleObject addWeaponTurret ["CMFlareLauncher", _turretPath];
				} else
				{
					//DO THIS IF PYLON
					if (["pylon",_magclass] call BIS_fnc_inString) then
					{
						_vehicleObject setAmmoOnPylon [_pylonIndex,_bulletAmount];
					} else
					{
						//FOR EVERYTHING ELSE
						_maxMagAmmo = (configFile >> "CfgMagazines" >> _magClass >> "count") call BIS_fnc_getCfgData;
						_numMags = ceil (_bulletAmount / _maxMagAmmo);
			
						while {_numMags > 1} do
						{
							_vehicleobject addMagazineTurret [_magClass, _turretPath];
							_numMags = _numMags - 1;
							_bulletAmount = _bulletAmount - _maxMagAmmo;
						};
						_vehicleobject setMagazineTurretAmmo [_magClass, _bulletAmount, _turretPath];	
					};
				};
			};
		};
	}forEach _savedMagazines;
};

//End Custom Ammo Save
_vehicleObject

4. Repack PBO and upload.

5. Eventually the server will update all database entries, at which point you can switch back to the original file in GITHUB, or just leave it if unsure.

KNOWN ISSUE

1. If upgrading you will get errors and/or server freeze unless you clear all ammo out of vehicles in the database. Status - FIXED, check the update instructions above

2. Currently a problem with scalpel missiles being removed. Status - FIXED

Edited by Bones51
  • Like 6

Share this post


Link to post
Share on other sites
Advertisement

Sorry, me again. Just opened up another issue about an undefined variable in server object vehicle db load.

 

EDIT: Found a few more errors. Have added them as comments to the new issue on your git.

Edited by BaroN

Share this post


Link to post
Share on other sites

Apologies, somehow an older version of a couple of files got in there. Repo all updated, and this time, tested on a clean server :)

If you do see any more probs just let me know.

Share this post


Link to post
Share on other sites

For those that already installed it, just replace the following files in your mission file from the github repo.

Custom\ammoSave\ExileServer_object_vehicle_database_update.sqf

Custom\ammoSave\ExileServer_object_vehicle_database_load.sqf

Custom\ammoSave\ExileServer_object_vehicle_database_insert.sqf 

Edited by Bones51

Share this post


Link to post
Share on other sites

Still getting

Spoiler

19:35:27 Error in expression <ield = _fields select _i;
if ((typeName _field) isEqualTo "SCALAR") then
{
_fie>
19:35:27   Error position: <_field) isEqualTo "SCALAR") then
{
_fie>
19:35:27   Error Undefined variable in expression: _field
19:35:27 File exile_server\code\ExileServer_util_extDB2_createMessage.sqf, line 19
19:35:27 Error in expression <

_currentLoadout = _vehicleObject call Bones_fnc_getVehicleLoadout;

_extDB2Mes>
19:35:27   Error position: <Bones_fnc_getVehicleLoadout;

_extDB2Mes>
19:35:27   Error Undefined variable in expression: bones_fnc_getvehicleloadout

And

Spoiler

19:35:46 Error in expression <ject setVehicleAmmoDef 0;

if (typeName _savedMagazines isEqualTo "ARRAY") then
>
19:35:46   Error position: <_savedMagazines isEqualTo "ARRAY") then
>
19:35:46   Error Undefined variable in expression: _savedmagazines
19:35:46 File mpmissions\__cur_mp.Chernarus\Custom\ammoSave\ExileServer_object_vehicle_database_load.sqf, line 112

p.s. running it on extdb3

Edited by dima054

Share this post


Link to post
Share on other sites

Ahh extdb3 will be your problem, runs fine on ExtDb2. If i get time i'll have a look at compatibility with extdb3. Both errors are on loading vehicle data from the database so almost definitely that.

Share this post


Link to post
Share on other sites

Init.sqf

add ";" to end

Bones_fnc_getVehicleLoadout = compileFinal (preprocessFileLineNumbers "Custom\ammoSave\Bones_fnc_getVehicleLoadout.sqf");

 

Edited by xDaVIRUS
  • Like 1

Share this post


Link to post
Share on other sites
Advertisement

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.