Skip to content

Commit 617aa2c

Browse files
committed
Add support for "full" and "semi" rate of fire
1 parent 68b799c commit 617aa2c

File tree

2 files changed

+91
-27
lines changed

2 files changed

+91
-27
lines changed

addons/sup_artillery/ArtilleryStateMachine.hpp

+7-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ class ArtilleryStateMachine {
8080

8181
class Fired {
8282
targetState = "FireDelay";
83-
condition = "true";
83+
condition = "[_this, 'isFireMissionDelayed'] call ALIVE_fnc_artillery";
8484
onTransition = "";
8585
};
8686
};
@@ -90,6 +90,12 @@ class ArtilleryStateMachine {
9090
onStateEntered = "";
9191
onStateLeaving = "";
9292

93+
class FireMissionComplete {
94+
targetState = "ReturnToBase";
95+
condition = "[_this, 'isFireMissionComplete'] call ALIVE_fnc_artillery";
96+
onTransition = "";
97+
};
98+
9399
class Continue {
94100
targetState = "Fire";
95101
condition = "[_this, 'fireNextRound'] call ALIVE_fnc_artillery";

addons/sup_artillery/fnc_artillery.sqf

+84-26
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ switch (_operation) do {
5656
****************/
5757
case "fireMission": {
5858
if (count _args == 0) then {
59-
_result = _logic getVariable ["fireMission", objNull];
59+
_result = _logic getVariable ["fireMission", []];
6060
} else {
6161
private _position = _args param [0, [0,0,0], [[]], 3];
6262
private _roundType = _args param [1, "", [""]];
@@ -90,64 +90,112 @@ switch (_operation) do {
9090
case "execute": {
9191
private _group = _logic getVariable ["group", grpNull];
9292
private _units = (units _group) select {vehicle _x != _x && {gunner (vehicle _x) == _x}};
93-
private _fireMission = _logic getVariable ["fireMission", objNull];
93+
private _fireMission = _logic getVariable ["fireMission", []];
9494
private _fireMissionPos = [_fireMission, "position"] call ALIVE_fnc_hashGet;
9595
[_fireMission, "units", _units] call ALIVE_fnc_hashSet;
9696
[_fireMission, "unitIndex", 0] call ALIVE_fnc_hashSet;
9797
[_fireMission, "roundsShot", 0] call ALIVE_fnc_hashSet;
98+
[_fireMission, "nextRoundTime", time] call ALIVE_fnc_hashSet;
99+
/* _units doWatch [_fireMissionPos select 0, _fireMissionPos select 1, 9999]; */
98100
_units doWatch _fireMissionPos;
99101

100102
_logic setVariable ["fireMission", _fireMission];
103+
104+
// Attach Fired EH to all vehicles in group
105+
{
106+
private _vehicle = vehicle _x;
107+
private _firedEH = _vehicle addEventHandler ["Fired", {
108+
private _unit = param [0, objNull];
109+
private _magazine = param [5, ""];
110+
private _logic = (group _unit) getVariable ["logic", objNull];
111+
private _fireMission = _logic getVariable ["fireMission", []];
112+
private _roundType = [_fireMission, "roundType"] call ALIVE_fnc_hashGet;
113+
private _roundsShot = [_fireMission, "roundsShot"] call ALIVE_fnc_hashGet;
114+
115+
if (_magazine == _roundType) then {
116+
private _delay = [_fireMission, "delay"] call ALIVE_fnc_hashGet;
117+
118+
if (_delay > 0) then {
119+
private _units = [_fireMission, "units"] call ALIVE_fnc_hashGet;
120+
private _unitIndex = [_fireMission, "unitIndex"] call ALIVE_fnc_hashGet;
121+
122+
if ((_unitIndex + 1) >= count _units) then {
123+
_unitIndex = 0;
124+
} else {
125+
_unitIndex = _unitIndex + 1;
126+
};
127+
128+
[_fireMission, "unitIndex", _unitIndex] call ALIVE_fnc_hashSet;
129+
[_fireMission, "nextRoundTime", time + _delay] call ALIVE_fnc_hashSet;
130+
};
131+
132+
[_fireMission, "roundsShot", _roundsShot + 1] call ALIVE_fnc_hashSet;
133+
_logic setVariable ["fireMission", _fireMission];
134+
};
135+
}];
136+
_vehicle setVariable ["sup_artillery_firedEH", _firedEH];
137+
} forEach _units;
101138
};
139+
// TODO: Check if unit is alive, otherwise skip
102140
case "fire": {
103-
private _fireMission = _logic getVariable ["fireMission", objNull];
104-
private _roundsShot = [_fireMission, "roundsShot"] call ALIVE_fnc_hashGet;
141+
private _fireMission = _logic getVariable ["fireMission", []];
142+
private _delay = [_fireMission, "delay"] call ALIVE_fnc_hashGet;
105143
private _units = [_fireMission, "units"] call ALIVE_fnc_hashGet;
106-
private _unitIndex = [_fireMission, "unitIndex"] call ALIVE_fnc_hashGet;
107-
private _unit = _units select _unitIndex;
108144
private _position = [_fireMission, "position"] call ALIVE_fnc_hashGet;
109145
private _roundType = [_fireMission, "roundType"] call ALIVE_fnc_hashGet;
110-
private _delay = [_fireMission, "delay"] call ALIVE_fnc_hashGet;
111146

112-
_unit doArtilleryFire [
113-
_position,
114-
_roundType,
115-
1
116-
];
147+
if (_delay > 0) then {
148+
private _unitIndex = [_fireMission, "unitIndex"] call ALIVE_fnc_hashGet;
149+
private _unit = _units select _unitIndex;
150+
151+
_unit doArtilleryFire [
152+
_position,
153+
_roundType,
154+
1
155+
];
117156

118-
if ((_unitIndex + 1) > ((count _units) - 1)) then {
119-
_unitIndex = 0;
157+
hint format ["%3: _unit: %1 firing 1 %2", _unit, _roundType, time];
120158
} else {
121-
_unitIndex = _unitIndex + 1;
159+
{
160+
private _roundCount = [_fireMission, "roundCount"] call ALIVE_fnc_hashGet;
161+
162+
_x doArtilleryFire [
163+
_position,
164+
_roundType,
165+
floor (_roundCount / (count _units)) // TODO: Better distribution
166+
];
167+
} forEach _units;
122168
};
123169

124-
[_fireMission, "nextRoundTime", time + _delay] call ALIVE_fnc_hashSet;
125-
[_fireMission, "unitIndex", _unitIndex] call ALIVE_fnc_hashSet;
126-
[_fireMission, "roundsShot", _roundsShot + 1] call ALIVE_fnc_hashSet;
127-
170+
[_fireMission, "nextRoundTime", -1] call ALIVE_fnc_hashSet;
128171
_logic setVariable ["fireMission", _fireMission];
129172
};
130173
case "fireNextRound": {
131-
private _fireMission = _logic getVariable ["fireMission", objNull];
174+
private _fireMission = _logic getVariable ["fireMission", []];
132175
private _nextRoundTime = [_fireMission, "nextRoundTime"] call ALIVE_fnc_hashGet;
133-
_result = (time >= _nextRoundTime);
176+
_result = (_nextRoundTime != -1 && {time >= _nextRoundTime});
134177
};
135178
case "hasFireMission": {
136-
private _fireMission = _logic getVariable ["fireMission", objNull];
179+
private _fireMission = _logic getVariable ["fireMission", []];
137180
_result = (count _fireMission > 0);
138181
};
139182
case "isFireMissionComplete": {
140-
private _fireMission = _logic getVariable ["fireMission", objNull];
183+
private _fireMission = _logic getVariable ["fireMission", []];
141184
private _roundCount = [_fireMission, "roundCount"] call ALIVE_fnc_hashGet;
142185
private _roundsShot = [_fireMission, "roundsShot"] call ALIVE_fnc_hashGet;
143186
_result = (_roundsShot >= _roundCount);
144187
};
188+
case "isFireMissionDelayed": {
189+
private _fireMission = _logic getVariable ["fireMission", []];
190+
private _delay = [_fireMission, "delay"] call ALIVE_fnc_hashGet;
191+
_result = (_delay > 0);
192+
};
145193
case "inPosition": {
146194
private _group = _logic getVariable ["group", grpNull];
147195
_result = _group getVariable ["sup_artillery_inPosition", false];
148196
};
149197
case "inRange": {
150-
private _fireMission = _logic getVariable ["fireMission", objNull];
198+
private _fireMission = _logic getVariable ["fireMission", []];
151199
private _position = [_fireMission, "position"] call ALIVE_fnc_hashGet;
152200
private _roundType = [_fireMission, "roundType"] call ALIVE_fnc_hashGet;
153201
private _group = _logic getVariable ["group", grpNull];
@@ -177,7 +225,6 @@ switch (_operation) do {
177225
"true",
178226
"(group this) setVariable ['sup_artillery_inPosition', true]"
179227
];
180-
diag_log format["_group: %1, _position: %2, _waypoint: %3", _group, _position, _waypoint];
181228
};
182229
case "spawn": {
183230
private _position = position _logic;
@@ -222,7 +269,7 @@ switch (_operation) do {
222269
};
223270
case "onActive": {
224271
if (!([_logic, "inRange"] call MAINCLASS)) then {
225-
_logic setVariable ["moveToPos", [3451.45,5379.89,0]]; // TODO: Figure out best firing position
272+
_logic setVariable ["moveToPos", [3744.56,4757.54,0]]; // TODO: Figure out best firing position
226273
};
227274
};
228275
case "onFire": {
@@ -239,6 +286,17 @@ switch (_operation) do {
239286
private _units = (units _group) select {vehicle _x != _x && {gunner (vehicle _x) == _x}};
240287
_units doWatch objNull;
241288

289+
// Cleanup event handlers
290+
{
291+
private _vehicle = vehicle _x;
292+
private _firedEH = _vehicle getVariable ["sup_artillery_firedEH", nil];
293+
294+
if (!isNil "_firedEH") then {
295+
_vehicle removeEventHandler ["Fired", _firedEH];
296+
_vehicle setVariable ["sup_artillery_firedEH", nil];
297+
};
298+
} forEach _units;
299+
242300
_logic setVariable ["fireMission", []];
243301
[_logic, "move", [position _logic]] call MAINCLASS; // TODO: Find (best) RTB position
244302
};

0 commit comments

Comments
 (0)