Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Common - Add FUNC(doArtilleryFire) #9260

Draft
wants to merge 56 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
56 commits
Select commit Hold shift + click to select a range
39f200b
fix belt linking issues
LinkIsGrim Jun 30, 2023
78800be
fix unloading to units with full inventory
LinkIsGrim Jun 30, 2023
01dffca
progress bar text changes
LinkIsGrim Jun 30, 2023
aca90fb
this took way too long
LinkIsGrim Jul 1, 2023
f3c393f
fix TRACE
LinkIsGrim Jul 1, 2023
76a2076
cache nearby ammo sources
LinkIsGrim Jul 1, 2023
5d62ab4
fix undefined variable
LinkIsGrim Jul 2, 2023
3b42998
Merge remote-tracking branch 'upstream' into csw-fix-belt-linking
LinkIsGrim Jul 12, 2023
d00c9ca
all the things
LinkIsGrim Jul 13, 2023
c96a9da
remove unnecessary change
LinkIsGrim Jul 13, 2023
c46dc33
_staticWeapon > _vehicle, StaticWeapon > CSW
LinkIsGrim Jul 13, 2023
6c2a8a2
add container property
LinkIsGrim Jul 13, 2023
ed44c0d
more headers
LinkIsGrim Jul 13, 2023
aa8fff3
fix case sensitivy, move container creation
LinkIsGrim Jul 13, 2023
4b92ef2
remove systemChat
LinkIsGrim Jul 13, 2023
e5a9fad
add check for invalid proxyWeapon
LinkIsGrim Jul 13, 2023
17156d5
missing semicolon
LinkIsGrim Jul 13, 2023
078857b
add getAvailableAmmo
LinkIsGrim Jul 13, 2023
73bc331
function header
LinkIsGrim Jul 13, 2023
cafe1f4
compile cache
LinkIsGrim Jul 13, 2023
7647123
add forcedMag and ai_unloadMagazines
LinkIsGrim Jul 13, 2023
4e8c69e
make public
LinkIsGrim Jul 13, 2023
76518c3
i hate git
LinkIsGrim Jul 13, 2023
99fbfdf
improve mag switch
LinkIsGrim Jul 13, 2023
8758645
fix stupid, add turretPath bool
LinkIsGrim Jul 13, 2023
abd766d
Merge branch 'csw-fix-belt-linking' into artillerytables-doArtilleryF…
LinkIsGrim Jul 13, 2023
6dfebb9
fix derp
LinkIsGrim Jul 13, 2023
ef93a61
fix derp
LinkIsGrim Jul 13, 2023
9328b00
Merge branch 'csw-fix-belt-linking' into artillerytables-doArtilleryF…
LinkIsGrim Jul 13, 2023
5108394
add check for gunner staying in vehicle
LinkIsGrim Jul 13, 2023
bb9ac71
fix derp part 2
LinkIsGrim Jul 13, 2023
6527851
Merge branch 'csw-fix-belt-linking' into artillerytables-doArtilleryF…
LinkIsGrim Jul 13, 2023
19315e4
thisd be easier if I was awake
LinkIsGrim Jul 13, 2023
702edf5
Merge remote-tracking branch 'upstream' into artillerytables-doArtill…
LinkIsGrim Jul 13, 2023
1818762
i should squash these
LinkIsGrim Jul 13, 2023
b5f1767
Merge branch 'csw-fix-belt-linking' into artillerytables-doArtilleryF…
LinkIsGrim Jul 13, 2023
bd841e8
add failsafes
LinkIsGrim Jul 13, 2023
22d45be
function header
LinkIsGrim Jul 14, 2023
b7abfea
add ai_switchMagazine
LinkIsGrim Jul 14, 2023
ae22373
Merge branch 'csw-fix-belt-linking' into artillerytables-doArtilleryF…
LinkIsGrim Jul 14, 2023
c192cf9
switch to ai_switchMagazine
LinkIsGrim Jul 14, 2023
b2d0b73
condition
LinkIsGrim Jul 14, 2023
fac2e61
add example to header
LinkIsGrim Jul 14, 2023
8f2e952
Merge branch 'csw-fix-belt-linking' into artillerytables-doArtilleryF…
LinkIsGrim Jul 14, 2023
2b52fee
fix container creation
LinkIsGrim Jul 14, 2023
23199a3
Merge branch 'csw-fix-belt-linking' into artillerytables-doArtilleryF…
LinkIsGrim Jul 14, 2023
1cf254a
fix condition
LinkIsGrim Jul 14, 2023
3b94e4d
Merge branch 'csw-fix-belt-linking' into artillerytables-doArtilleryF…
LinkIsGrim Jul 14, 2023
ac947cd
move CSW handling to CSW
LinkIsGrim Jul 14, 2023
17d7cd1
remove else, function header
LinkIsGrim Jul 14, 2023
3c767c6
skip magazines with no ammo
LinkIsGrim Jul 16, 2023
19db283
solve passing carry mags to disabled CSW
LinkIsGrim Jul 16, 2023
0d12e89
Merge branch 'csw-fix-belt-linking' into artillerytables-doArtilleryF…
LinkIsGrim Jul 16, 2023
5207870
move to common
LinkIsGrim Jul 16, 2023
f11b0f1
Update addons/common/functions/fnc_doArtilleryFire.sqf
LinkIsGrim Jul 17, 2023
bcb0719
fix condition for disabled assembly
LinkIsGrim Jul 22, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions addons/common/XEH_PREP.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ PREP(displayText);
PREP(displayTextPicture);
PREP(displayTextStructured);
PREP(doAnimation);
PREP(doArtilleryFire);
PREP(doGesture);
PREP(dummy);
PREP(dropBackpack);
Expand Down
74 changes: 74 additions & 0 deletions addons/common/functions/fnc_doArtilleryFire.sqf
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#include "script_component.hpp"
/*
* Author: LinkIsGrim, mharris001
* Wrapper for engine doArtilleryFire, fires barrage one round at a time.
* Handles and accepts CSW carry magazines.
*
* Arguments:
* 0: Vehicle, gunner must be local <OBJECT>
* 1: Target <OBJECT, STRING or POSITION AGL>
* 2: Magazine Type <STRING>
* 3: Rounds to fire <NUMBER>
* 4: Spread around target position, in meters <NUMBER> (default: 0)
*
* Return Value:
* Barrage Started <BOOL>
*
* Example:
* [cursorObject, [0, 0, 0], "8Rnd_82mm_Mo_shells", 2, 25] call ace_common_fnc_doArtilleryFire
*
* Public: Yes
*/
params [["_vehicle", objNull, [objNull]], ["_position", [0, 0, 0], [[], objNull, ""], 3], ["_magazine", "", [""]], ["_rounds", 0, [0]], ["_spread", 0, [0]]];

if (isNull _vehicle || {_rounds isEqualTo 0} || {_magazine isEqualTo ""} || {!(_vehicle turretLocal [0])} || {isNull (gunner _vehicle)}) exitWith {false};

if (_position isEqualType objNull) then {
_position = ASLtoAGL getPosASL _position;
};

if (_position isEqualType "") then {
_position = [_position, true] call CBA_fnc_mapGridToPos;
};

// Magazine must be configCase
_magazine = configName (configFile >> "CfgMagazines" >> _magazine);
if (_magazine isEqualTo "") exitWith {false};

if (["ace_csw"] call EFUNC(common,isModLoaded)) then {
[_vehicle, _magazine] call EFUNC(csw,handleDoArtilleryFire) params ["_cswHandled", "_newMagazine"];
if !(_cswHandled) exitWith {false};
_magazine = _newMagazine;
};

if !(_position inRangeOfArtillery [[_vehicle], _magazine]) exitWith {false};

_vehicle doWatch _position;

[{
params ["_vehicle", "_position", "_magazine", "_roundsLeft", "_spread", "_lastFired"];
if (CBA_missionTime - _lastFired > 30) exitWith {true};

(weaponState [_vehicle, [0]]) params ["", "", "", "_loadedMag", "_ammoCount", "_roundReloadPhase", "_magazineReloadPhase"];
if (
_loadedMag isEqualTo _magazine &&
{_ammoCount > 0} &&
{_roundReloadPhase isEqualTo 0} &&
{_magazineReloadPhase isEqualTo 0} &&
{unitReady _vehicle}
) then {
_vehicle doArtilleryFire [[_position, _spread] call CBA_fnc_randPos, _magazine, 1];
_roundsLeft = _roundsLeft - 1;
_this set [3, _roundsLeft];
_this set [5, CBA_missionTime];
};

if (_roundsLeft <= 0 || {!alive _vehicle} || {!alive (gunner _vehicle)} || {objectParent (gunner _vehicle) isNotEqualTo _vehicle}) exitWith {
[QGVAR(doArtilleryFireComplete), [_vehicle, _magazine, _roundsLeft]] call CBA_fnc_globalEvent;
[{_this doWatch objNull}, _vehicle, 5] call CBA_fnc_waitAndExecute;
true
};
false
}, {}, [_vehicle, _position, _magazine, _rounds, _spread, CBA_missionTime]] call CBA_fnc_waitUntilAndExecute;

true
7 changes: 7 additions & 0 deletions addons/csw/XEH_PREP.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ PREP(aceRearmGetCarryMagazines);

PREP(ai_handleFired);
PREP(ai_handleGetIn);
PREP(ai_switchMagazine);
PREP(ai_reload);

PREP(assemble_canDeployTripod);
Expand All @@ -19,7 +20,12 @@ PREP(assemble_pickupWeapon);
PREP(canGetIn);
PREP(getIn);

PREP(compatibleMagazines);
PREP(getAvailableAmmo);
PREP(getCarryMagazine);
PREP(getNearbySources);
PREP(getSourceCompatibleMagazines);
PREP(handleDoArtilleryFire);
PREP(proxyWeapon);

PREP(reload_actionsLoad);
Expand All @@ -32,6 +38,7 @@ PREP(reload_handleAddTurretMag);
PREP(reload_handleRemoveTurretMag);
PREP(reload_handleReturnAmmo);
PREP(reload_loadMagazine);
PREP(unloadMagazines);

PREP(staticWeaponInit);
PREP(staticWeaponInit_unloadExtraMags);
8 changes: 6 additions & 2 deletions addons/csw/XEH_postInit.sqf
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#include "script_component.hpp"

GVAR(vehicleMagCache) = createHashMap;

["CBA_settingsInitialized", {
TRACE_3("settingsInit",GVAR(defaultAssemblyMode),GVAR(handleExtraMagazines),GVAR(ammoHandling));
["StaticWeapon", "init", LINKFUNC(staticWeaponInit), true, [], true] call CBA_fnc_addClassEventHandler;
Expand All @@ -18,8 +16,14 @@ GVAR(vehicleMagCache) = createHashMap;
[QGVAR(addTurretMag), LINKFUNC(reload_handleAddTurretMag)] call CBA_fnc_addEventHandler;
[QGVAR(removeTurretMag), LINKFUNC(reload_handleRemoveTurretMag)] call CBA_fnc_addEventHandler;
[QGVAR(returnAmmo), LINKFUNC(reload_handleReturnAmmo)] call CBA_fnc_addEventHandler;
[QGVAR(ai_reload), LINKFUNC(ai_reload)] call CBA_fnc_addEventHandler;

[QEGVAR(common,doArtilleryFireComplete), {
params ["_vehicle"];
if !(local _vehicle) exitWith {};

_vehicle setVariable [QGVAR(forcedMag), nil, true];
}] call CBA_fnc_addEventHandler;

#ifdef DEBUG_MODE_FULL
call compile preprocessFileLineNumbers QPATHTOF(dev\checkStaticWeapons.sqf);
Expand Down
4 changes: 3 additions & 1 deletion addons/csw/XEH_preInit.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ PREP_RECOMPILE_END;
#include "initSettings.sqf"

GVAR(initializedStaticTypes) = [];
GVAR(vehicleMagCache) = createHashMap;
GVAR(compatibleMagsCache) = createHashMap;
GVAR(compatibleVehicleMagsCache) = createHashMap;

ADDON = true;

2 changes: 1 addition & 1 deletion addons/csw/functions/fnc_aceRearmGetCarryMagazines.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Helper function for ace_rearm; Gets magazines that should be loaded by csw
*
* Arguments:
* 0: Vehicle <OBJECT>
* 0: CSW <OBJECT>
* 1: Specific Turret or pass bool to check all turrets <ARRAY><BOOL>(default: true)
*
* Return Value:
Expand Down
10 changes: 5 additions & 5 deletions addons/csw/functions/fnc_ai_handleFired.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
* Public: No
*/

params ["_staticWeapon", "_weapon", "_muzzle", "_mode", "_ammo", "_magazine", "_projectile", "_gunner"];
TRACE_8("firedEH:",_staticWeapon,_weapon,_muzzle,_mode,_ammo,_magazine,_projectile,_gunner);
params ["_vehicle", "_weapon", "_muzzle", "_mode", "_ammo", "_magazine", "_projectile", "_gunner"];
TRACE_8("firedEH:",_vehicle,_weapon,_muzzle,_mode,_ammo,_magazine,_projectile,_gunner);

if ((!local _gunner) || {[_gunner] call EFUNC(common,isPlayer)}) exitWith {};
if (someAmmo _staticWeapon) exitWith {};
if (someAmmo _vehicle) exitWith {};

TRACE_2("need ammo",someAmmo _staticWeapon,magazinesAllTurrets _staticWeapon);
TRACE_2("need ammo",someAmmo _vehicle,magazinesAllTurrets _vehicle);

[_staticWeapon, _gunner, _weapon, _magazine] call FUNC(ai_reload);
[_vehicle, _gunner] call FUNC(ai_reload);
14 changes: 6 additions & 8 deletions addons/csw/functions/fnc_ai_handleGetIn.sqf
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "script_component.hpp"
/*
* Author: Grim
* Handles AI GetIn on an empty weapon
* Author: LinkIsGrim
* Handles AI GetIn on an empty CSW
*
* Arguments:
* GetIn EH
Expand All @@ -11,12 +11,10 @@
*
* Public: No
*/
params ["_staticWeapon", "_role", "_gunner"];
TRACE_3("getInEH:",_staticWeapon,_role,_gunner);
params ["_vehicle", "_role", "_gunner", "_turret"];
TRACE_3("getInEH:",_vehicle,_role,_gunner);

if ((!local _gunner) || {[_gunner] call EFUNC(common,isPlayer)}) exitWith {};
if (someAmmo _staticWeapon) exitWith {};
if (someAmmo _vehicle) exitWith {};

TRACE_2("need ammo",someAmmo _staticWeapon,magazinesAllTurrets _staticWeapon);

[_staticWeapon, _gunner, currentWeapon _staticWeapon] call FUNC(ai_reload);
[_vehicle, _gunner] call FUNC(ai_reload);
107 changes: 42 additions & 65 deletions addons/csw/functions/fnc_ai_reload.sqf
Original file line number Diff line number Diff line change
@@ -1,95 +1,72 @@
#include "script_component.hpp"
/*
* Author: PabstMirror, modified by Grim
* Author: PabstMirror, LinkIsGrim
* Handles AI reloading
*
* Arguments:
* 0: Static Weapon <OBJECT>
* 0: CSW <OBJECT>
* 1: Gunner <OBJECT>
* 2: Weapon <STRING>
* 3: Magazine <STRING> (default: "")
* 2: Skip reload time <BOOL> (default: false)
* 3: Clear forced magazine after reloading (default: true)
*
* Return Value:
* None
*
* Public: No
*/
params ["_staticWeapon", "_gunner", "_weapon", ["_magazine", ""]];
params ["_vehicle", "_gunner", ["_instantReload", false], ["_clearForcedMag", false]];
TRACE_3("AI reload",_vehicle,_gunner,_instantReload);

private _turretPath = [_gunner] call EFUNC(common,getTurretIndex);
private _reloadSource = objNull;
private _reloadMag = "";
private _reloadNeededAmmo = -1;
// API, used for ai_switchMagazine
private _forcedMag = _vehicle getVariable [QGVAR(forcedMag), ""];
private _turretIndex = [_gunner] call EFUNC(common,getTurretIndex);
if (_turretIndex isEqualTo []) then {
_turretIndex = [0];
};

private _cfgMagGroups = configFile >> QGVAR(groups);
// If this is called while CSW has ammo, unload mags in gunner's turret
if (someAmmo _vehicle) then {[_vehicle, _turretIndex] call FUNC(unloadMagazines)};

private _nearSupplies = [_gunner] + ((_staticWeapon nearSupplies 10) select {
isNull (group _x) ||
{!([_x] call EFUNC(common,isPlayer)) && {[side group _gunner, side group _x] call BIS_fnc_sideIsFriendly}}
});
private _loadableMagazines = [_vehicle, _gunner, true] call FUNC(reload_getLoadableMagazines);
if (_loadableMagazines isEqualTo []) exitWith {TRACE_1("could not find reloadable mag",_vehicle)};

// Find if there is anything we can reload with
private _bestAmmo = 0;
private _magazineInfo = [];
{
scopeName "findSource";
private _xSource = _x;

private _cswMagazines = [];
{
_cswMagazines pushBackUnique _x;
} forEach ((magazineCargo _xSource) select {isClass (_cfgMagGroups >> _x)});
TRACE_2("",_xSource,_cswMagazines);

private _compatibleMags = [_weapon] call CBA_fnc_compatibleMagazines;
if (_magazine != "") then {
_compatibleMags insert [0, [_magazine]];
_x params ["_xMag", "", "", "", "", "_ammo"];
if (_forcedMag isNotEqualTo "" && {_xMag != _forcedMag}) then {continue};
if (_ammo > _bestAmmo) then {
_bestAmmo = _ammo;
_magazineInfo = _x;
};
} forEach _loadableMagazines;

{
private _xWeaponMag = _x;
{
if ((getNumber (_cfgMagGroups >> _x >> _xWeaponMag)) == 1) then {
private _loadInfo = [_staticWeapon, _turretPath, _x, _xSource] call FUNC(reload_canLoadMagazine);
if (_loadInfo select 0) then {
_reloadMag = _x;
_reloadSource = _xSource;
_reloadNeededAmmo = _loadInfo select 2;
TRACE_3("found mag",_reloadMag,_reloadSource,_x);
breakOut "findSource";
};
};
} forEach _cswMagazines;
} forEach _compatibleMags;
} forEach _nearSupplies;
if (_reloadMag == "") exitWith {TRACE_1("could not find mag",_reloadMag);};
if (_clearForcedMag) then {
_vehicle setVariable [QGVAR(forcedMag), nil, true];
};

// Figure out what we can add from the magazines we have
private _bestAmmoToSend = -1;
{
_x params ["_xMag", "_xAmmo"];
TRACE_2("",_xMag,_xAmmo);
if (_xMag == _reloadMag) then {
if ((_bestAmmoToSend == -1) || {(_xAmmo > _bestAmmoToSend) && {_xAmmo <= _reloadNeededAmmo}}) then {
_bestAmmoToSend = _xAmmo;
};
};
} forEach (if (_reloadSource isKindOf "CAManBase") then {magazinesAmmo _reloadSource} else {magazinesAmmoCargo _reloadSource});
TRACE_4("",_reloadSource,_reloadMag,_reloadNeededAmmo,_bestAmmoToSend);
if (_bestAmmoToSend == -1) exitWith {ERROR("No ammo");};
if (_magazineInfo isEqualTo []) exitWith {};
_magazineInfo params ["_carryMag", "_turretPath", "_loadInfo", "_magSource", "", "_ammo"];

// Remove the mag from the source
[_reloadSource, _reloadMag, _bestAmmoToSend] call EFUNC(common,removeSpecificMagazine);
[_magSource, _carryMag, _ammo] call EFUNC(common,removeSpecificMagazine);

private _timeToLoad = 1;
if (!isNull(configOf _staticWeapon >> QUOTE(ADDON) >> "ammoLoadTime")) then {
_timeToLoad = getNumber(configOf _staticWeapon >> QUOTE(ADDON) >> "ammoLoadTime");
// AI never returns ammo and removes the magazine before reloading, so we can skip distance and weaponHolder checks
private _eventParams = [_vehicle, _turretPath, objNull, _carryMag, _ammo, _gunner];

if (_instantReload) exitWith {
TRACE_1("calling addTurretMag event: instant AI reload",_this);
[QGVAR(addTurretMag), _eventParams] call CBA_fnc_globalEvent;
};

private _timeToLoad = GET_NUMBER(configOf _vehicle >> QUOTE(ADDON) >> "ammoLoadTime", 1);

TRACE_1("Reloading in progress",_timeToLoad);
[{
params ["_staticWeapon", "_turretPath", "_gunner", "_reloadMag", "_bestAmmoToSend"];
if ((!alive _staticWeapon) || {!alive _gunner} || {(_staticWeapon distance _gunner) > 10}) exitWith {TRACE_1("invalid state",_this);};
params ["_vehicle", "", "", "", "", "_gunner"];
if !(alive _vehicle && {alive _gunner}) exitWith {TRACE_2("invalid state",alive _vehicle,alive _gunner);};

// Reload the static weapon
TRACE_5("calling addTurretMag event",_staticWeapon,_turretPath,_gunner,_reloadMag,_bestAmmoToSend);
TRACE_1("calling addTurretMag event: AI reload",_this);
[QGVAR(addTurretMag), _this] call CBA_fnc_globalEvent;
}, [_staticWeapon, _turretPath, _gunner, _reloadMag, _bestAmmoToSend], _timeToLoad] call CBA_fnc_waitAndExecute;
}, _eventParams, _timeToLoad] call CBA_fnc_waitAndExecute;
39 changes: 39 additions & 0 deletions addons/csw/functions/fnc_ai_switchMagazine.sqf
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include "script_component.hpp"
/*
* Author: LinkIsGrim
* Handles AI magazine switching
* Magazine must be compatible and available, use ace_csw_fnc_getAvailableAmmo
*
* Arguments:
* 0: CSW <OBJECT>
* 1: Carry Magazine <STRING>
* 2: Turret Path <ARRAY> (default: [0])
* 3: Skip reload time <BOOL> (default: false)
* 4: Clear forced magazine after reloading <BOOL> (default: true)
*
* Return Value:
* Successful <BOOL>
*
* Example:
* [cursorTarget, "ACE_csw_100Rnd_127x99_mag_red", [0]] call ace_csw_fnc_ai_switchMagazine
*
* Public: Yes
*/
params [["_vehicle", objNull, [objNull]], ["_carryMag", "", [""]], ["_turretPath", [0], [[0]]], ["_instantReload", false, [false]], ["_clearForcedMag", true, [true]]];

if !(alive _vehicle && {!isNull (_vehicle turretUnit _turretPath)} && {!(isNull _vehicle)}) exitWith {false};

// must be config case
_carryMag = configName (configFile >> "CfgMagazines" >> _carryMag);
if (_carryMag isEqualTo "") exitWith {false};

private _availableMags = [_vehicle] call FUNC(getAvailableAmmo);
if !(_carryMag in _availableMags) exitWith {false};

_vehicle setVariable [QGVAR(forcedMag), _carryMag, true];

private _gunner = _vehicle turretUnit _turretPath;

[QGVAR(ai_reload), [_vehicle, _gunner, _instantReload, _clearForcedMag], _gunner] call CBA_fnc_targetEvent;

true
Loading