Skip to content

Commit

Permalink
Added towing actions connect and disconnect functions
Browse files Browse the repository at this point in the history
  • Loading branch information
jonpas committed Aug 19, 2015
1 parent 3f155af commit 44e63e8
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 32 deletions.
2 changes: 1 addition & 1 deletion addons/towing/CfgVehicles.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
displayName = CSTRING(TowDisconnect); \
distance = TOWING_ACTION_DISTANCE; \
condition = QUOTE(_this call FUNC(canDisconnectTowBar)); \
statement = QUOTE(_player call FUNC(disconnectTowBar)); \
statement = QUOTE(_this call FUNC(disconnectTowBar)); \
exceptions[] = {"isNotInside"}; \
}; \
}; \
Expand Down
1 change: 1 addition & 0 deletions addons/towing/functions/fnc_canConnectTiltCable.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ params ["_target", "_unit"];

!(_target distance _unit > TOWING_ACTION_DISTANCE) &&
{isNil {_target getVariable QGVAR(tiltUp)}} &&
{isNil {_unit getVariable QGVAR(towConnecting)}} &&
{((vectorUp _target) select 2) < 0.7 || _unit getVariable [QGVAR(isTilting), 0] == 1}
3 changes: 1 addition & 2 deletions addons/towing/functions/fnc_canDisconnectTowBar.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,4 @@
params ["_target", "_unit"];

!(_target distance _unit > TOWING_ACTION_DISTANCE) &&
{!isNil {_target getVariable QGVAR(isTowing)}} &&
{!isNil {_target getVariable QGVAR(isTowed)}}
{!isNil {_target getVariable QGVAR(isTowing)} || !isNil {_target getVariable QGVAR(isTowed)}}
21 changes: 19 additions & 2 deletions addons/towing/functions/fnc_connectTowBar.sqf
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Author:
* Author: Jonpas
* Connects a tow bar to a vehicle.
*
* Arguments:
Expand All @@ -16,6 +16,23 @@
*/
#include "script_component.hpp"

private ["_connectingVehicle"];
params ["_target", "_unit"];

diag_log "connectTowBar";
_connectingVehicle = _unit getVariable QGVAR(towConnecting);

if (isNil "_connectingVehicle") then {
// Set connecting and temporary towing variable
_unit setVariable [QGVAR(towConnecting), _target];
_target setVariable [QGVAR(isTowing), true, true];
} else {
// Remove connecting variables
_unit setVariable [QGVAR(towConnecting), nil];

// Set variables on both vehicles
_target setVariable [QGVAR(isTowed), _connectingVehicle, true];
_connectingVehicle setVariable [QGVAR(isTowing), _target, true];

// Start towing
[_connectingVehicle, _target] call FUNC(towVehicle);
};
31 changes: 27 additions & 4 deletions addons/towing/functions/fnc_disconnectTowBar.sqf
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
/*
* Author:
* Author: Jonpas
* Disconnects the tow bar.
*
* Arguments:
* 0: Unit <OBJECT>
* 0: Target <OBJECT>
* 1: Unit <OBJECT>
*
* Return Value:
* None
Expand All @@ -15,6 +16,28 @@
*/
#include "script_component.hpp"

params ["_unit"];
private ["_connectingVehicle", "_towingVehicle", "_towedVehicle"];
params ["_target", "_unit"];

diag_log "disconnectTowBar";
_connectingVehicle = _unit getVariable QGVAR(towConnecting);
_towingVehicle = _target getVariable QGVAR(isTowed);
_towedVehicle = _target getVariable QGVAR(isTowing);

if (!isNil "_connectingVehicle") exitWith {
_unit setVariable [QGVAR(towConnecting), nil];
_connectingVehicle setVariable [QGVAR(isTowing), nil, true];
};

// Vehicle being towed
if (!isNil "_towingVehicle") exitWith {
_target setVariable [QGVAR(towState), nil, true];
_target setVariable [QGVAR(isTowed), nil, true];
_towingVehicle setVariable [QGVAR(isTowing), nil, true];
};

// Vehicle towing
if (!isNil "_towedVehicle") exitWith {
_towedVehicle setVariable [QGVAR(towState), nil, true];
_target setVariable [QGVAR(isTowing), nil, true];
_towedVehicle setVariable [QGVAR(isTowed), nil, true];
}
52 changes: 29 additions & 23 deletions addons/towing/functions/fnc_towVehicle.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,45 @@
* Tows a vehicle.
*
* Arguments:
* 0: ...
* 0: Towing Vehicle <OBJECT>
* 1: Towed Vehicle <OBJECT>
*
* Return Value:
* None
*
* Example:
* [towTruck, towedVehicle] call ace_towing_fnc_towVehicle
* [towing, towed] call ace_towing_fnc_towVehicle
*
* Public: No
*/
#include "script_component.hpp"

params ["_towTruck", "_towedTruck"];
params ["_towing", "_towed"];

_towed setVariable [QGVAR(towState), 1];

[{
params ["_args", "_idPFH"];
_args params ["_towTruck", "_vehicle"];
_args params ["_towing", "_vehicle"];

_state = _vehicle getVariable QGVAR(towState);

if (isNil "_state") exitWith {
_vehicle setVariable [QGVAR(lastPosVeh), nil];
_vehicle setVariable [QGVAR(towState), nil];
_lastPosVeh = getPosATL _vehicle;
_vehicle setPosATL [_lastPosVeh select 0, _lastPosVeh select 1, (_lastPosVeh select 2) + 1]; // Add 1m height to prevent getting stuck in the floor
_vehicle enableSimulation true;
_towing setHitpointDamage ["HitEngine", 0];
[_idPFH] call CBA_fnc_removePerFrameHandler;
};

_posTowTruck = getPosATL _towTruck;
_posTow = _posTowTruck vectorAdd ((vectorDir _towTruck) vectorMultiply -5);
_posTowTruck = getPosATL _towing;
_posTow = _posTowTruck vectorAdd ((vectorDir _towing) vectorMultiply -5);

_state = _vehicle getVariable [QGVAR(towState), -1];
if (_state == -1) exitWith {};
if (_state == 1) then {
_vehicle enableSimulation false;
_towTruck setHitpointDamage ["HitEngine", 1];
_towing setHitpointDamage ["HitEngine", 1];
_dirV = (getPosATL _vehicle) vectorFromTo _posTow;
_dir = (_dirV select 0) atan2 (_dirV select 1);
_angle = _dir - (getDir _vehicle);
Expand All @@ -39,6 +52,7 @@ params ["_towTruck", "_towedTruck"];
_vehicle setVariable [QGVAR(towState), 2];
_vehicle setVariable [QGVAR(towAngle), _angle];
};

if (_state == 2) then {
_dirV = (getPosATL _vehicle) vectorFromTo _posTow;
_dir = (_dirV select 0) atan2 (_dirV select 1);
Expand All @@ -56,6 +70,7 @@ params ["_towTruck", "_towedTruck"];
_vehicle setDir (_dir - _angle + _dt * 5 * _angle / abs(_angle));
};
};

if (_state == 3) then {
_dirV = (getPosATL _vehicle) vectorFromTo _posTow;
_start = _vehicle getVariable [QGVAR(towStart), time];
Expand All @@ -64,13 +79,14 @@ params ["_towTruck", "_towedTruck"];
_dt = time - _start;
if (_dt > (_distance - 7)) then {
_vehicle setVariable [QGVAR(towState), 4];
_towTruck setHitpointDamage ["HitEngine", 0];
_towing setHitpointDamage ["HitEngine", 0];
} else {
_distance = -7 - (_distance - 7) + _dt;
_posVeh = _posTow vectorAdd (_dirV vectorMultiply _distance);
_vehicle setPosATL _posVeh;
};
};

if (_state == 4) then {
_lastPosVeh = _vehicle getVariable [QGVAR(lastPosVeh), getPosATL _vehicle];
_dirV = _lastPosVeh vectorFromTo _posTow;
Expand All @@ -83,12 +99,12 @@ params ["_towTruck", "_towedTruck"];
hint str _posTowTruck;
_posVeh set [2, 0];

_angle = (velocity _towTruck) vectorCos (vectorDir _towTruck);
_angle = (velocity _towing) vectorCos (vectorDir _towing);
if (_angle < 0) then {
_dir = _dir - 180;
};

if ((_posVeh distance _lastPosVeh) > .1) then {
if ((_posVeh distance _lastPosVeh) > 0.1) then {
_vehicle setDir _dir;
_vehicle setVariable [QGVAR(lastPosVeh), _posVeh];
};
Expand All @@ -99,14 +115,4 @@ params ["_towTruck", "_towedTruck"];
_vehicle setVectorUp _normal;
_vehicle setPosATL _posVeh;
};
if (_state == 5) then {
_vehicle setVariable [QGVAR(lastPosVeh), nil];
_vehicle getVariable [QGVAR(towState), nil];
_vehicle enableSimulation true;
[_idPFH] call cba_fnc_removePerFrameHandler;
};

}, 0, [_towTruck, _towedTruck] ] call cba_fnc_addPerFrameHandler;


_towedTruck setVariable [QGVAR(towState), 1];
}, 0, [_towing, _towed] ] call CBA_fnc_addPerFrameHandler;

0 comments on commit 44e63e8

Please sign in to comment.