Skip to content

Commit

Permalink
fix #104 and also probably last big refactor - split "legacy" module …
Browse files Browse the repository at this point in the history
…into "lifecycle" and "activities" (#106)

* add vanilla military buildings to exclusion lists, put lists into GVARs
* addNestedStateMachine to nest another machine within a compound state
* split legacy -> activities,lifecycle
* move % rename lots of settings
* move civClasses setting, localCivs array & getter, civ_added|removed events into lifecycle module
* unify & fix dismount conditions, add (!not driver)
  • Loading branch information
Fusselwurm authored Dec 13, 2020
1 parent b9552b8 commit 00b5bd8
Show file tree
Hide file tree
Showing 160 changed files with 381 additions and 276 deletions.
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ common events:

["grad_civs_vehicleTheft", { params ["_vehicle", "_thief"]; }] call CBA_fnc_addEventHandler;

### grad_civs_legacy_fnc_doCustomActivity
### grad_civs_activities_fnc_doCustomActivity

To let civilians break from their usual activity and do something else for a limited time.

Expand All @@ -108,7 +108,7 @@ Example:
[],
"hiding",
"pooped my pants, hiding for ten minutes"
] call grad_civs_legacy_fnc_doCustomActivity;
] call grad_civs_activities_fnc_doCustomActivity;
```

**NOTE**: this whole thing will *NOT* work while they are panicking.
Expand Down Expand Up @@ -173,7 +173,7 @@ headgear | Array - All classnames of clothes that civilians may wear.
Sets all backpacks that civilians may wear and sets probability. Overwrites value from CBA settings. Execute globally

#### Syntax
`[backpacks,probability] call grad_civs_legacy_fnc_setHeadgear`
`[backpacks,probability] call grad_civs_loadout_fnc_setHeadgear`

Parameter | Explanation
------------|-----------------------------------------------------------------------
Expand Down Expand Up @@ -227,9 +227,9 @@ Let's have a very simple example:
```sqf
MY_CIV_LIST = ["C_Offroad_01_F" createVehicle position player];
_machine = [{MY_CIV_LIST}] call CBA_statemachine_fnc_create;
_state_init = [_machine, { diag_log "init"; }, { diag_log "onEnter_init" }, { diag_log "onExit_init" }] call grad_civs_legacy_fnc_addState;
_state_stuff = [_machine, {diag_log "wörk" }, {diag_log "onEnter_wörk"}, {}] call grad_civs_legacy_fnc_addState;
_transition = [_machine, _state_init, _state_stuff, {CBA_missionTime > 30}, {diag_log "changing state" }] call grad_civs_legacy_fnc_addTransition;
_state_init = [_machine, { diag_log "init"; }, { diag_log "onEnter_init" }, { diag_log "onExit_init" }] call grad_civs_cba_statemachine_fnc_addState;
_state_stuff = [_machine, {diag_log "wörk" }, {diag_log "onEnter_wörk"}, {}] call grad_civs_cba_statemachine_fnc_addState;
_transition = [_machine, _state_init, _state_stuff, {CBA_missionTime > 30}, {diag_log "changing state" }] call grad_civs_cba_statemachine_fnc_addTransition;
```

this will print something like this to RPT:
Expand All @@ -252,12 +252,12 @@ wörk
In our case, and with CBA state machines, that means:

* we have a bunch of state machines, chief of which is the *activities* state machine. It is implemented in `/functions/sm_activities/fn_activities.sqf`
* states are added to it using [grad_civs_legacy_fnc_addState](https://cbateam.github.io/CBA_A3/docs/files/statemachine/fnc_addState-sqf.html) .
* states are added to it using [grad_civs_cba_statemachine_fnc_addState](https://cbateam.github.io/CBA_A3/docs/files/statemachine/fnc_addState-sqf.html) .
* every state has a bunch of callbacks that are called with a civilian as parameter
* one is called periodically as long as the civ is in the state
* one is called when the civ enters the state
* one is called when the civ leaves the state
* transitions are being added by using [grad_civs_legacy_fnc_addTransition](https://cbateam.github.io/CBA_A3/docs/files/statemachine/fnc_addTransition-sqf.html) (or fnc_addEventTransition for transitions triggered by CBA events)
* transitions are being added by using [grad_civs_cba_statemachine_fnc_addTransition](https://cbateam.github.io/CBA_A3/docs/files/statemachine/fnc_addTransition-sqf.html) (or fnc_addEventTransition for transitions triggered by CBA events)
* every transition is defined as a one-way connection between two states
* every transition gets two callbacks
* one is called periodically to check whether a civ can move along the transition
Expand Down
1 change: 1 addition & 0 deletions addons/activities/$PBOPREFIX$
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
z\grad_civs\addons\activities
File renamed without changes.
24 changes: 0 additions & 24 deletions addons/legacy/XEH_PREP.hpp → addons/activities/XEH_PREP.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
PREP(adoptAbandonedCivs);
PREP(compare);
PREP(doReverse);
PREP(deleteIfDamaged);
PREP(doCustomActivity);
Expand All @@ -10,23 +9,12 @@ PREP(isStopped);
PREP(doCarryOn);

PREP(endCustomActivity);

PREP(findBuildings);
PREP(findPositionOfInterest);
PREP(findRandomPosArea);
PREP(findRandomPos);
// PREP(findSpawnPosition.spec);
PREP(findSpawnPosition);
PREP(findUnclaimedHouse);
PREP(forceEmotionSpeed);
PREP(forcePanicSpeed);
PREP(handleAceUnconscious);
PREP(getGlobalCivs);

PREP(initCommonEventhandlers);
PREP(initConfig);
PREP(initHCs);
PREP(isInDistanceFromOtherPlayers);
PREP(isInHouse);
PREP(overclockStateMachines);
PREP(reverse_abort);
Expand All @@ -44,23 +32,11 @@ PREP(sm_activities_trans_business_panic_condition);
PREP(sm_business);
PREP(sm_business_state_rally_enter);
PREP(sm_emotions);
PREP(sm_lifecycle);
PREP(sm_lifecycle_state_death_enter);
PREP(sm_lifecycle_state_despawn_enter);
PREP(sm_lifecycle_state_life_enter);
PREP(sm_lifecycle_state_life_exit);
PREP(sm_lifecycle_state_spawn_enter);
PREP(sm_lifecycle_trans_life_despawn_condition);
PREP(sm_panic);
PREP(sm_panic_state_flight_enter);
PREP(sm_panic_state_flight_loop);
PREP(sm_panic_state_hidden_enter);
PREP(sm_panic_state_hide_enter);
PREP(sm_panic_trans_hide_hidden_condition);
PREP(sm_panic_trans_hide_hidden_handler);
PREP(spawnCivilianGroup);
PREP(spawnCivilian);
PREP(localSpawnPass);
PREP(globalSpawnPass);
PREP(uid);
PREP(cleanupLocalCivs);
11 changes: 11 additions & 0 deletions addons/activities/XEH_postInit.sqf
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include "script_component.hpp"

if (!(EGVAR(main,enabled))) exitWith {};

[] call FUNC(initCommonEventhandlers);

if (isServer || CBA_isHeadlessClient) then {
["lifecycle", ["lfc_life"], FUNC(sm_emotions)] call EFUNC(common,augmentStateMachine);
["lifecycle", ["lfc_life"], FUNC(sm_activities)] call EFUNC(common,augmentStateMachine);
[QEGVAR(lifecycle,civ_added), FUNC(onCivAdded)] call CBA_fnc_addEventHandler;
};
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* this *should not happen, as we're setting the acex exlusion var, but you never know...
*/

private _civClasses = call EFUNC(common,config_getCivClasses);
private _civClasses = call EFUNC(lifecycle,config_getCivClasses);
private _allCivs = entities [_civClasses, [], true, true];
private _myCivs = _allCivs select { local _x && (_x getVariable ["grad_civs_primaryTask", ""] != "")};
private _orphanedCivs = _myCivs - GVAR(localCivs);
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
16 changes: 16 additions & 0 deletions addons/activities/functions/fnc_initConfig.sqf
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include "..\script_component.hpp"

INFO("initConfig running...");

private _settingsGroup = ["GRAD Civs", "4) basic activities - panicking"];

[
QGVAR(panicCooldown),
"EDITBOX",
"Time for panic to wear off [low, med, high]",
_settingsGroup,
QUOTE([ARR_3(15,120,240)]),
false,
{},
false
] call CBA_fnc_addSetting;
File renamed without changes.
File renamed without changes.
7 changes: 7 additions & 0 deletions addons/activities/functions/fnc_onCivAdded.sqf
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
params [
["_civ", objNull, [objNull]]
];
if (!local _civ) exitWith {};

private _panicCooldown = [GVAR(panicCooldown)] call EFUNC(common,parseCsv);
_civ setVariable["GRAD_CIVS_PANICCOOLDOWN" , random _panicCooldown, true];
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ if (isClass (configFile >> "CfgVehicles" >> "LOP_AFR_Civ_Ural_open")) then {
[
["WHEN I let it reverse",
{
[_this, [0, -50, 0]] call grad_civs_legacy_fnc_reverse;
[_this, [0, -50, 0]] call grad_civs_activities_fnc_reverse;
_this
},
[
Expand Down Expand Up @@ -54,7 +54,7 @@ if (isClass (configFile >> "CfgVehicles" >> "LOP_AFR_Civ_Ural_open")) then {
[
["WHEN I let it reverse",
{
[_this, [0, -50, 0]] call grad_civs_legacy_fnc_reverse;
[_this, [0, -50, 0]] call grad_civs_activities_fnc_reverse;
_this
},
[
Expand Down Expand Up @@ -92,7 +92,7 @@ if (isClass (configFile >> "CfgVehicles" >> "LOP_AFR_Civ_Ural_open")) then {
[
["WHEN I let it reverse",
{
[_this, [0, -20, 0]] call grad_civs_legacy_fnc_reverse;
[_this, [0, -20, 0]] call grad_civs_activities_fnc_reverse;
_this
},
[
Expand Down Expand Up @@ -126,9 +126,9 @@ if (isClass (configFile >> "CfgVehicles" >> "LOP_AFR_Civ_Ural_open")) then {
],
["WHEN I let it reverse 100m AND abort after 2 seconds",
{
[_this, [0, -100, 0]] call grad_civs_legacy_fnc_reverse;
[_this, [0, -100, 0]] call grad_civs_activities_fnc_reverse;
sleep 2;
[_this] call grad_civs_legacy_fnc_reverse_abort;
[_this] call grad_civs_activities_fnc_reverse_abort;
_this
},
[
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
#include "..\script_component.hpp"

params [
["_sm_lifecycle", locationNull, [locationNull]]
];


private _activities = [[], true, "activities"] call EFUNC(cba_statemachine,create);
private _business = [] call FUNC(sm_business);
private _panic = [] call FUNC(sm_panic);

// STATES

assert(_activities isEqualType locationNull);
assert(_business isEqualType locationNull);
assert(_panic isEqualType locationNull);
Expand Down Expand Up @@ -75,7 +81,6 @@ assert ([
_act_asOrdered + _act_business + "_event"
] call CBA_statemachine_fnc_addEventTransition);

EGVAR(common,stateMachines) setVariable ["activities", _activities];

[_sm_lifecycle, "lfc_life", _activities] call EFUNC(cba_statemachine,addNestedStateMachine);

_activities
EGVAR(common,stateMachines) setVariable ["activities", _activities];
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
#include "..\script_component.hpp"

params [
["_sm_lifecycle", locationNull, [locationNull]]
];


private _emotions = [[], true, "emotions"] call EFUNC(cba_statemachine,create);

// STATES
// STATES

private _emo_relaxed = [
_emotions,
Expand Down Expand Up @@ -48,9 +53,7 @@ private _emo_panic = [
"emo_panic"
] call EFUNC(cba_statemachine,addState);


// TRANSITIONS

// TRANSITIONS

assert ([
_emotions,
Expand Down Expand Up @@ -96,7 +99,8 @@ assert ([
_emo_relaxed + _emo_wary
] call CBA_statemachine_fnc_addEventTransition);

EGVAR(common,stateMachines) setVariable ["emotions", _emotions];
[_sm_lifecycle, "lfc_life", _emotions] call EFUNC(cba_statemachine,addNestedStateMachine);

EGVAR(common,stateMachines) setVariable ["emotions", _emotions];

_emotions
File renamed without changes.
File renamed without changes.
14 changes: 14 additions & 0 deletions addons/activities/script_component.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#define COMPONENT activities
#include "\z\grad_civs\addons\main\script_mod.hpp"

// #define DEBUG_MODE_FULL
// #define DISABLE_COMPILE_CACHE

#ifdef DEBUG_ENABLED_MAIN
#define DEBUG_MODE_FULL
#endif
#ifdef DEBUG_SETTINGS_MAIN
#define DEBUG_SETTINGS DEBUG_SETTINGS_MAIN
#endif

#include "\z\grad_civs\addons\main\script_macros.hpp"
1 change: 1 addition & 0 deletions addons/cars/XEH_PREP.hpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
PREP(dismountCondition);
PREP(getGlobalVehicles);
PREP(getGroupVehicle);
PREP(initConfig);
Expand Down
1 change: 1 addition & 0 deletions addons/cars/XEH_postInit.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ if (!(EGVAR(main,enabled))) exitWith {};

if (isServer || CBA_isHeadlessClient) then {
["business", ["bus_rally"], FUNC(sm_business)] call EFUNC(common,augmentStateMachine);
[QEGVAR(lifecycle,civ_added), FUNC(onCivAdded)] call CBA_fnc_addEventHandler;
};
2 changes: 1 addition & 1 deletion addons/cars/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class CfgPatches {
units[] = {};
weapons[] = {};
requiredVersion = REQUIRED_VERSION;
requiredAddons[] = {"grad_civs_common", "grad_civs_legacy"};
requiredAddons[] = {"grad_civs_common", "grad_civs_activities"};
author = "AUTHOR";
VERSION_CONFIG;
};
Expand Down
9 changes: 9 additions & 0 deletions addons/cars/functions/fnc_dismountCondition.sqf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include "..\script_component.hpp"

if (leader _this != _this) exitWith {true};

if (driver vehicle _this != _this) exitWith {true};

if (!(canMove vehicle _this)) exitWith {true};

(([_this, "emotions"] call EFUNC(common,civGetState)) == "emo_panic") || (_this call EFUNC(interact,sm_activities_helper_surrenderCondition))
2 changes: 1 addition & 1 deletion addons/cars/functions/fnc_getGlobalVehicles.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ params [
["_primaryTask", "", [""]] /* filter civs by primary task */
];

private _civs = _this call EFUNC(legacy,getGlobalCivs);
private _civs = _this call EFUNC(lifecycle,getGlobalCivs);
private _vehicles = [];
{
private _veh = vehicle _x;
Expand Down
2 changes: 1 addition & 1 deletion addons/cars/functions/fnc_initConfig.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

INFO("initConfig running...");

private _settingsGroup = ["GRAD Civs", "5) cars"];
private _settingsGroup = ["GRAD Civs", "7) cars - basic settings for civilian drivers"];

[
QGVAR(animalTransportChance),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#include "..\script_component.hpp"

if (leader _this != _this) exitWith {true};

(([_this, "emotions"] call EFUNC(common,civGetState)) == "emo_panic") || (_this call EFUNC(interact,sm_activities_helper_surrenderCondition))
_this call FUNC(dismountCondition);
2 changes: 1 addition & 1 deletion addons/cars/functions/fnc_spawnCarAndCrew.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ if (_automtaicVehicleGroupSize) then {
_groupSize = (floor random [0, 1, _maxCount]) + 1
};

private _group = [_pos, _groupSize, _house, _primaryTask] call EFUNC(legacy,spawnCivilianGroup);
private _group = [_pos, _groupSize, _house, _primaryTask] call EFUNC(lifecycle,spawnCivilianGroup);

{
// for convenience & speed: shortcut so units dont have to lengthily embark on their own
Expand Down
2 changes: 1 addition & 1 deletion addons/cars/functions/fnc_spawnVehicle.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ _veh addEventHandler [
{
params ["_vec", "_firer", "_distance", "_weapon", "_muzzle", "_mode", "_ammo", "_gunner"];
{
[QEGVAR(legacy,firedNear), [_x], _x] call CBA_fnc_targetEvent;
[QEGVAR(activities,firedNear), [_x], _x] call CBA_fnc_targetEvent;
} forEach (crew _vec);
}
];
Expand Down
1 change: 1 addition & 0 deletions addons/cba_statemachine/XEH_PREP.hpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
PREP(addCompoundState);
PREP(addNestedStateMachine);
PREP(addState);
PREP(addToStateMachine);
PREP(addTransition);
Expand Down
10 changes: 5 additions & 5 deletions addons/cba_statemachine/functions/fnc_addCompoundState.sqf
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
#include "..\script_component.hpp"

#define NESTED(var) (var + "_nested")
#define COMPOUNDONSTATEENTERED(var) (var + "_onCompoundStateEntered")
#define COMPOUNDONSTATELEAVING(var) (var + "_onCompoundStateLeaving")

params [
["_outerStateMachine", locationNull, [locationNull]],
["_nestedStateMachines", [], [locationNull, []]],
Expand All @@ -14,7 +10,11 @@ params [
];

if (!(_nestedStateMachines isEqualType [])) then { _nestedStateMachines = [_nestedStateMachines]; };
if (!(_nestedStateMachines isEqualTypeAll locationNull)) exitWith {ERROR_2("when trying to create state %1: not all _nestedStateMachines %2 are CBA state machines!", _name, _nestedStateMachines); ""};
if (!((_nestedStateMachines isEqualTo []) || (_nestedStateMachines isEqualTypeAll locationNull))) exitWith {
// NOTE bug: isEqualTypeAll returns false for empty arrays
ERROR_2("when trying to create state %1: not all _nestedStateMachines %2 are CBA state machines!", _name, _nestedStateMachines); ""
};

{
if (!((_x getVariable ["cba_statemachine_list", []]) isEqualType [])) exitWith {ERROR("nested state machines must have empty items array!"); ""};
} forEach _nestedStateMachines;
Expand Down
16 changes: 16 additions & 0 deletions addons/cba_statemachine/functions/fnc_addNestedStateMachine.sqf
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include "..\script_component.hpp"

params [
["_outerStateMachine", locationNull, [locationNull]],
["_outerState", "", [""]],
["_nestedStateMachine", locationNull, [locationNull]]
];

if (!(_nestedStateMachine isEqualType locationNull)) exitWith {ERROR_1("when trying to adding state machine %1: is not CBA state machine!", _nestedStateMachine);};

private _nestedStateMachines = _outerStateMachine getVariable [NESTED(_outerState), false];
if (!(_nestedStateMachines isEqualType [])) exitWith {ERROR_2("state %1 of %2 is not a compound state!", _outerState, _outerStateMachine)};

_nestedStateMachines pushBackUnique _nestedStateMachine;

_outerStateMachine setVariable [NESTED(_outerState), _nestedStateMachines];
Loading

0 comments on commit 00b5bd8

Please sign in to comment.