ACE3/addons/common/functions/fnc_getInPosition.sqf

235 lines
8.9 KiB
Plaintext
Raw Normal View History

/*
* Author: commy2
2015-09-20 23:07:49 +00:00
* Move unit into given vehicle position or switch to that position if the unit is already inside the vehicle.
*
* Arguments:
* 0: Unit <OBJECT>
* 1: Vehicle <OBJECT>
* 2: Position ("Driver", "Pilot", "Gunner", "Commander", "Copilot", "Turret", "FFV", "Codriver", "Cargo") <STRING>
* 3: Index (only applies to "Turret", "FFV", "Codriver", "Cargo") (default: next free index) <NUMBER>
*
* Return Value:
* None
*
2015-09-20 23:07:49 +00:00
* Public: Yes
*/
2015-01-13 19:56:02 +00:00
#include "script_component.hpp"
#define CANGETINDRIVER (isNull (driver _vehicle) || {!alive driver _vehicle}) && {!lockedDriver _vehicle} && {getNumber (_config >> "isUav") != 1}
#define CANGETINTURRETINDEX (isNull (_vehicle turretUnit _turret) || {!alive (_vehicle turretUnit _turret)}) && {!(_vehicle lockedTurret _turret)} && {getNumber (_config >> "isUav") != 1}
2015-09-20 23:07:49 +00:00
params ["_unit", "_vehicle", "_position", ["_index", -1]];
2015-09-20 23:07:49 +00:00
_position = toLower _position;
// general
if (!alive _vehicle || {locked _vehicle > 1}) exitWith {false};
private _config = configFile >> "CfgVehicles" >> typeOf _vehicle;
private _turret = [];
private _isInside = vehicle _unit == _vehicle;
private _script = {};
private _enemiesInVehicle = false; //Possible Side Restriction
2015-09-20 23:07:49 +00:00
{
2015-05-14 18:06:06 +00:00
if (side _unit getFriend side _x < 0.6) exitWith {_enemiesInVehicle = true};
2015-09-20 23:07:49 +00:00
false
} count crew _vehicle;
switch (_position) do {
2015-05-14 18:06:06 +00:00
case "driver" : {
if (CANGETINDRIVER) then {
2015-09-20 23:07:49 +00:00
_script = [
{_unit action [["GetInDriver", "MoveToDriver"] select _isInside, _vehicle];},
{if (_isInside) then {moveOut _unit}; _unit moveInDriver _vehicle; call _fnc_getInEH;}
] select _enemiesInVehicle;
2015-05-14 18:06:06 +00:00
};
};
2015-05-14 18:06:06 +00:00
case "pilot" : {
if (CANGETINDRIVER) then {
2015-09-20 23:07:49 +00:00
_script = [
{_unit action [["GetInPilot", "MoveToPilot"] select _isInside, _vehicle];},
{if (_isInside) then {moveOut _unit}; _unit moveInDriver _vehicle; call _fnc_getInEH;}
] select _enemiesInVehicle;
_position = "driver";
2015-05-14 18:06:06 +00:00
};
};
2015-05-14 18:06:06 +00:00
case "gunner" : {
_turret = [_vehicle] call FUNC(getTurretGunner);
2015-05-14 18:06:06 +00:00
if (CANGETINTURRETINDEX) then {
2015-09-20 23:07:49 +00:00
_script = [
{_unit action [["GetInGunner", "MoveToGunner"] select _isInside, _vehicle];},
{if (_isInside) then {moveOut _unit}; _unit moveInGunner _vehicle; call _fnc_getInEH;}
] select _enemiesInVehicle;
2015-05-14 18:06:06 +00:00
};
};
2015-05-14 18:06:06 +00:00
case "commander" : {
_turret = [_vehicle] call FUNC(getTurretCommander);
2015-05-14 18:06:06 +00:00
if (CANGETINTURRETINDEX) then {
2015-09-20 23:07:49 +00:00
_script = [
{_unit action [["GetInCommander", "MoveToCommander"] select _isInside, _vehicle];},
{if (_isInside) then {moveOut _unit}; _unit moveInCommander _vehicle; call _fnc_getInEH;}
] select _enemiesInVehicle;
};
};
2015-05-14 18:06:06 +00:00
case "copilot" : {
_turret = [_vehicle] call FUNC(getTurretCopilot);
2015-05-14 18:06:06 +00:00
if (CANGETINTURRETINDEX) then {
2015-09-20 23:07:49 +00:00
_script = [
{_unit action [["GetInTurret", "moveToTurret"] select _isInside, _vehicle, _turret];},
{if (_isInside) then {moveOut _unit}; _unit moveInTurret [_vehicle, _turret]; call _fnc_getInEH;}
] select _enemiesInVehicle;
2015-09-20 23:07:49 +00:00
_position = "gunner"; // I think. It's a turret after all and turrets supposedly return "gunner"
};
};
2015-05-14 18:06:06 +00:00
case "turret" : {
private _turrets = [_vehicle] call FUNC(getTurretsOther);
2015-05-14 18:06:06 +00:00
if (_index != -1 && {_turret = _turrets select _index; CANGETINTURRETINDEX}) then {
_script = [
2015-09-20 23:07:49 +00:00
{_unit action [["GetInTurret", "moveToTurret"] select _isInside, _vehicle, _turret];},
{if (_isInside) then {moveOut _unit}; _unit moveInTurret [_vehicle, _turret]; call _fnc_getInEH;}
2015-05-14 18:06:06 +00:00
] select _enemiesInVehicle;
_position = "gunner";
} else {
for "_index" from 0 to (count _turrets - 1) do {
_turret = _turrets select _index;
if (CANGETINTURRETINDEX) exitWith {
_script = [
2015-09-20 23:07:49 +00:00
{_unit action [["GetInTurret", "moveToTurret"] select _isInside, _vehicle, _turret];},
{if (_isInside) then {moveOut _unit}; _unit moveInTurret [_vehicle, _turret]; call _fnc_getInEH;}
2015-05-14 18:06:06 +00:00
] select _enemiesInVehicle;
_position = "gunner";
};
2015-09-20 23:07:49 +00:00
};
2015-05-14 18:06:06 +00:00
};
};
2015-05-14 18:06:06 +00:00
case "ffv" : {
private _turrets = [_vehicle] call FUNC(getTurretsFFV);
2015-05-14 18:06:06 +00:00
if (_index != -1 && {_turret = _turrets select _index; CANGETINTURRETINDEX}) then {
_script = [
2015-09-20 23:07:49 +00:00
{_unit action [["GetInTurret", "moveToTurret"] select _isInside, _vehicle, _turret];},
{if (_isInside) then {moveOut _unit}; _unit moveInTurret [_vehicle, _turret]; call _fnc_getInEH;}
2015-05-14 18:06:06 +00:00
] select _enemiesInVehicle;
_position = "gunner"; // I think. It's a turret after all and turrets supposedly return "gunner"
} else {
for "_index" from 0 to (count _turrets - 1) do {
2015-09-20 23:07:49 +00:00
_turret = _turrets select _index;
if (CANGETINTURRETINDEX) exitWith {
_script = [
{_unit action [["GetInTurret", "moveToTurret"] select _isInside, _vehicle, _turret];},
{if (_isInside) then {moveOut _unit}; _unit moveInTurret [_vehicle, _turret]; call _fnc_getInEH;}
] select _enemiesInVehicle;
_position = "gunner"; // I think. It's a turret after all and turrets supposedly return "gunner"
};
2015-05-14 18:06:06 +00:00
};
};
};
2015-05-14 18:06:06 +00:00
case "codriver" : {
private _positions = [typeOf _vehicle] call FUNC(getVehicleCodriver);
2015-05-14 18:06:06 +00:00
{
if (alive _x) then {_positions deleteAt (_positions find (_vehicle getCargoIndex _x))};
} forEach crew _vehicle;
if (_index != -1 && {_index in _positions}) then {
_script = [
2015-09-20 23:07:49 +00:00
{_unit action [["GetInCargo", "MoveToCargo"] select _isInside, _vehicle, _index];},
{if (_isInside) then {moveOut _unit}; _unit moveInCargo [_vehicle, _index]; call _fnc_getInEH;}
2015-05-14 18:06:06 +00:00
] select _enemiesInVehicle;
_position = "cargo";
} else {
_index = _positions select 0;
if (!isNil "_index") then {
_script = [
2015-09-20 23:07:49 +00:00
{_unit action [["GetInCargo", "MoveToCargo"] select _isInside, _vehicle, _index];},
{if (_isInside) then {moveOut _unit}; _unit moveInCargo [_vehicle, _index]; call _fnc_getInEH;}
2015-05-14 18:06:06 +00:00
] select _enemiesInVehicle;
_position = "cargo";
};
};
};
2015-05-14 18:06:06 +00:00
case "cargo" : {
private "_positions";
_positions = [typeOf _vehicle] call FUNC(getVehicleCargo);
{
if (alive _x) then {_positions deleteAt (_positions find (_vehicle getCargoIndex _x))};
} forEach crew _vehicle;
if (_index != -1 && {_index in _positions}) then {
_script = [
2015-09-20 23:07:49 +00:00
{_unit action [["GetInCargo", "MoveToCargo"] select _isInside, _vehicle, _index];},
{if (_isInside) then {moveOut _unit}; _unit moveInCargo [_vehicle, _index]; call _fnc_getInEH;}
2015-05-14 18:06:06 +00:00
] select _enemiesInVehicle;
_position = "cargo";
} else {
_index = _positions select 0;
if (!isNil "_index") then {
_script = [
2015-09-20 23:07:49 +00:00
{_unit action [["GetInCargo", "MoveToCargo"] select _isInside, _vehicle, _index];},
{if (_isInside) then {moveOut _unit}; _unit moveInCargo [_vehicle, _index]; call _fnc_getInEH;}
2015-05-14 18:06:06 +00:00
] select _enemiesInVehicle;
_position = "cargo";
};
};
};
2015-05-14 18:06:06 +00:00
default {};
};
// this will execute all config based event handlers. Not script based ones unfortunately, but atleast we don't use any.
private _fnc_getInEH = {
2015-05-14 18:06:06 +00:00
// config based getIn EHs are assigned to the soldier, not the vehicle. Why Bis? Why?
private _config = configFile >> "CfgVehicles" >> typeOf _unit >> "EventHandlers";
2015-05-14 18:06:06 +00:00
if (isClass _config) then {
//getIn is local effects with global arguments. It doesn't trigger if the unit was already inside and only switched seats
if !(_isInside) then {
[_vehicle, _position, _unit, _turret] call compile getText (_config >> "getIn");
};
};
};
// if you want into the cargo and you can't, then check ffv turrets aswell
if (_position == "cargo") exitWith {
2015-05-14 18:06:06 +00:00
if (_script isEqualTo {}) then {
[_unit, _vehicle, "ffv"] call FUNC(getInPosition);
} else {
call _script;
};
};
call _script;
/*
sleep 0.1;
if ((vehicle _unit) != _vehicle) then {
["fn_getInPosition.sqf - Side Restriction, failed to move _unit into vehicle"] call bis_fnc_error;
_unit moveInAny _vehicle; //attempt to fail gracefully
};
*/