Skip to content

Commit

Permalink
Redo fnc_packMortar
Browse files Browse the repository at this point in the history
  • Loading branch information
marceldev89 committed Jul 17, 2016
1 parent 4661c80 commit f1f844f
Showing 1 changed file with 57 additions and 83 deletions.
140 changes: 57 additions & 83 deletions addons/sup_combatsupport/fnc_packMortar.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -14,108 +14,82 @@ SCRIPT(packMortar);
_this select 0: the support team group (group)
_this select 1: the weapon (option if weapon registered as "supportWeaponSetup" variable)
*/
private["_group","_weapon","_position","_leader","_units","_gunner","_assistant","_type","_wait"];
private _group = _this param [0, grpNull];
private _weapon = _this param [0, objNull];

_group = [_this, 0, grpNull] call bis_fnc_param;
_weapon = [_this, 1, grpNull] call bis_fnc_param;
_type = typeOf _weapon;
_position = position _weapon;
_leader = leader _group;
_gunner = gunner _weapon;
_units = (units _group) - [_leader];
_units = _units - [_gunner];
if (isNull _group || isNull _weapon) exitWith {};

if (_weapon == objNull || isNil "_weapon" || _group == grpNull || _leader == objNull) exitWith {};
private _disassembleToCfg = (configFile >> "CfgVehicles" >> typeOf _weapon >> "assembleInfo" >> "dissasembleTo");
private _disassembleTo = _disassembleToCfg call BIS_fnc_getCfgData;

{
// find a group member that is not in a vehicle or staticweapon and is free
if !(_x getVariable ["packAssistant",false] || (vehicle _x != _x)) exitWith {
_assistant = _x;
_assistant setVariable ["packAssistant",true];
};
} foreach _units;
if (isNil "_disassembleTo" || {count _disassembleTo == 0}) exitWith {};

private _leader = leader _group;
private _gunner = gunner _weapon;
private _units = (units _group) - [_leader] - [_gunner];
private _assistant = _unit select 0;

// diag_log format ["%1, %2, %3, %4, %5, %6, %7", _group, _weapon, _position, _leader, _gunner, _assistant, _type];
private _primaryBag = objNull;
private _secondaryBag = objNull;

private _timeout = -1;

_gunner leaveVehicle _weapon;

_gunner addEventHandler ["WeaponDisassembled", {
_this spawn {
private ["_unit","_bag1","_bag2"];
_unit = _this select 0;
_bag1 = _this select 1;
_bag2 = _this select 2;
private _disassembledEH = _gunner addEventHandler ["WeaponDisassembled", {
private _unit = _this param [0, objNull];
private _primaryBag = _this param [1, objNull];
private _secondaryBag = _this param [2, objNull];

_unit setVariable ["supportWeaponBag1", _bag1];
_unit setVariable ["supportWeaponBag2", _bag2];
};
_unit setVariable ["primaryBag", _primaryBag];
_unit setVariable ["secondaryBag", _secondaryBag];
}];

_gunner action ["Disassemble",_weapon];
_gunner action ["Disassemble", _weapon];

_timeout = time + 5;
while {isNull _primaryBag || isNull _secondaryBag} do {
if (time >= _timeout) exitWith {
_primaryBag = createVehicle [_disassembleTo select 0, position _weapon, [], 0, "NONE"];
_secondaryBag = createVehicle [_disassembleTo select 1, position _weapon, [], 0, "NONE"];

deleteVehicle _weapon;
};

_primaryBag = _gunner getVariable ["primaryBag", objNull];
_secondaryBag = _gunner getVariable ["secondaryBag", objNull];
sleep 0.1;
};

{
_x enableAI "MOVE";
_x enableAI "ANIM";
_x setUnitPos "AUTO";
} forEach [_gunner, _assistant];

{
[_x,position _weapon] call ALiVE_fnc_doMoveRemote;
} foreach [_gunner, _assistant];

[_weapon, _gunner, _assistant] spawn {
private ["_weapon","_gunner","_assistant","_position","_wait","_bag2","_bag1","_timeout","_packs"];
_weapon = _this select 0;
_gunner = _this select 1;
_assistant = _this select 2;
_position = position _weapon;

_timer = time;
waitUntil {sleep 0.3; _gunner call ALiVE_fnc_unitReadyRemote || (time-_timer > 30)};

_gunner action ["Disassemble",_weapon];

_wait = true;
_timeout = false;
_timer = time;
while {_wait} do {
_packs = nearestObjects [_position, ["GroundWeaponHolder"], 3];
if (count _packs > 1) then {_wait = false};
if ((time-_timer) > 30) exitWith {_timeout = true;};
sleep 1;
};
_gunner action ["takeBag", _primaryBag];
_assistant action ["takeBag", _secondaryBag];

if (_timeout) then {
_bag1 = format ["%1_Mortar_01_weapon_F", _weapon select [0,1]];
_bag2 = format ["%1_Mortar_01_support_F",_weapon select [0,1]];
deleteVehicle _weapon;
_gunner addBackpackGlobal _bag1;
_assistant addBackpackGlobal _bag2;
} else {
_bag1 = _gunner getVariable ["supportWeaponBag1", objNull];
_bag2 = _gunner getVariable ["supportWeaponBag2", objNull];
_gunner action ["takeBag", _bag1];
_assistant action ["takeBag", _bag2];
_timeout = time + 5;
while {unitBackpack _gunner != _primaryBag || unitBackpack _assistant != _secondaryBag} do {
if (time >= _timeout) exitWith {
_gunner addBackpackGlobal (typeOf _primaryBag);
_assistant addBackpackGlobal (typeOf _secondaryBag);
};
sleep 0.1;
};

_timer = time;
waitUntil {sleep 1; (unitBackpack _gunner == _bag1 && unitBackpack _assistant == _bag2) || (time-_timer) > 30};
// Cleanup possible remaining backpacks
private _weaponHolders = nearestObjects [position _gunner, ["GroundWeaponHolder"], 25];

if (unitBackpack _gunner != _bag1) then {
_gunner addBackpackGlobal (typeOf _bag1);
};

if (unitBackpack _assistant != _bag2) then {
_assistant addBackpackGlobal (typeOf _bag2);
{
deleteVehicle _x;
}foreach _packs;
};

_gunner setVariable ["supportWeaponGunner", _weapon];
_assistant setVariable ["supportWeaponAsst", _weapon];
_weapon setVariable ["packed",true];
{
private _weaponHolder = _x param [0, objNull];
private _weaponHolderBackpacks = backpackCargo _weaponHolder;

{
private _backpack = _x param [0, objNull];

// diag_log format ["%1 packed up!",_weapon];
};
if (_backpack in _weaponHolderBackpacks) exitWith {
deleteVehicle _weaponHolder;
};
} forEach _disassembleTo;
} forEach _weaponHolders;

0 comments on commit f1f844f

Please sign in to comment.