Skip to content

Commit

Permalink
Mission - Add Degraded Vehicle function (#702)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mike-MF authored Jul 23, 2024
1 parent d9332a4 commit fda963a
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 0 deletions.
1 change: 1 addition & 0 deletions addons/mission/XEH_PREP.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ PREP(carAlarm);
PREP(connectBatteryToDefusable);
PREP(contaminationGas);
PREP(countAlive);
PREP(degradedVehicle);
PREP(dialogue);
PREP(dialogueLocal);
PREP(disableAI);
Expand Down
88 changes: 88 additions & 0 deletions addons/mission/functions/fnc_degradedVehicle.sqf
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
#include "..\script_component.hpp"
/*
* Author: Mike
* Simulates the vehicle being degraded by failing to start or breaking down at certain speeds.
* Must use a vehicle classname, not a vehicle variable name.
* Damage values should range from 0.1 to 1
* This should not be stacked for multiple types of vehicles.
*
* Call from init.sqf
*
* Arguments
* 0: Vehicle Classname <CLASSNAME/STRING>
* 1: Speed to break <NUMBER> (default: 50)
* 2: Damage from Speed <NUMBER> (default: 0.1)
*
* Return Value:
* None
*
* Example:
* ["C_Truck_02_covered_F", 40, 0.15] call MFUNC(degradedVehicle)
*/

params ["_vehicleClassname", ["_speed", 50], ["_damage", 0.1]];

// Debug
if (is3DENPreview) then {
private _classCheck = isClass (configFile >> "CfgVehicles" >> _vehicleClassname);
if (!_classCheck) then {
ERROR_MSG_1("%1 is not a valid vehicle classname.",_vehicleClassname);
};
if (_damage > 1 || _damage <= 0) then {
ERROR_MSG_1("Damage value (%1) should be between 0.1 - 1",_damage)
};
};

// Handle engine breaking by going too fast.
if (hasInterface) then {
[QGVAR(engineFloodedNotification), {["Engine Flooded", 2, [1, 0, 0]] call CBA_fnc_notify}] call CBA_fnc_addEventHandler;

[{
params ["_args", "_handle"];
_args params ["_vehicleClassname", "_speed", "_damage", ["_engineCheck", false]];

private _playerVehicle = vehicle ace_player;
if !(_playerVehicle isKindOf _vehicleClassname) exitWith {};

// Check Speed and Damage
private _playerSpeed = speed _playerVehicle;
private _vehicleDamage = _playerVehicle getHitPointDamage "hitEngine";

// If going too fast, gradually damage engine until it breaks.
if (_playerSpeed >= _speed) then {
_vehicleDamage = (_vehicleDamage + _damage) min 1;
if (_vehicleDamage <= 1) then {
_playerVehicle setHitPointDamage ["hitEngine", _vehicleDamage]
};
};

}, 2, [_vehicleClassname, _speed, _damage]] call CBA_fnc_addPerFrameHandler;
};

// Will stall by setting fuel to 0, then setting it back to original fuel. If vehicle is too damaged it wont start.
if (isServer) then {
[_vehicleClassname, "Engine", {
params ["_vehicle", "_engineState"];

if (!_engineState) exitWith {};

private _vehicleDamage = _vehicle getHitPointDamage "hitEngine";
private _currentFuel = fuel _vehicle;
if (_currentFuel == 0) exitWith {}; // Avoid Engine flooded spam.

private _willStall = 0.4 < (random 1);
if (_vehicleDamage >= 0.75) then {_willStall = true};
if (_willStall) then {
[{
params ["_vehicle"];
["ace_common_setFuel", [_vehicle, 0], _vehicle] call CBA_fnc_targetEvent;
[QGVAR(engineFloodedNotification), [], driver _vehicle] call CBA_fnc_targetEvent;
}, [_vehicle], 0.5] call CBA_fnc_waitAndExecute;

[{
params ["_vehicle", "_currentFuel"];
["ace_common_setFuel", [_vehicle, _currentFuel], _vehicle] call CBA_fnc_targetEvent;
}, [_vehicle, _currentFuel], 5] call CBA_fnc_waitAndExecute;
};
}, true, [], true] call CBA_fnc_addClassEventHandler;
};

0 comments on commit fda963a

Please sign in to comment.