Skip to content

Instantly share code, notes, and snippets.

@BrettMayson
Created May 17, 2019 04:48
Show Gist options
  • Save BrettMayson/97c1a02048607568579d17aab00cb5c6 to your computer and use it in GitHub Desktop.
Save BrettMayson/97c1a02048607568579d17aab00cb5c6 to your computer and use it in GitHub Desktop.
#define PREFIX PABST
#define COMPONENT PABOMIES
#include "\x\cba\addons\main\script_macros_mission.hpp"
["pabst_addDamPass", {_this call ace_medical_fnc_addDamageToUnit}] call CBA_fnc_addEventHandler;
["pabst_setUnitSpeed", {(_this select 0) setAnimSpeedCoef (_this select 1)}] call CBA_fnc_addEventHandler;
if (!isServer) exitWith {};
MISSION_ROOT = str missionConfigFile select [0, count str missionConfigFile - 15];
GVAR(zombiesA) = [];
GVAR(zombiesB) = [];
GVAR(zombiesC) = [];
FUNC(createZombie) = {
params ["_pos", ["_patrolRadius", 0]];
_unit = createAgent ["I_Soldier_F", _pos, [], 10, "NONE"];
_unit setVariable [QGVAR(origin), _pos];
_unit setVariable [QGVAR(patrolRadius), _patrolRadius];
_unit setDir (random 360);
_unit spawn {
sleep 1;
removeallweapons _this;
removeallassigneditems _this;
removeallcontainers _this;
removevest _this;
removebackpack _this;
removeUniform _this;
removeHeadgear _this;
if (0.1 > random 1) then {
_this addVest "V_TacVest_blk_POLICE";
_this addMagazine "CUP_15Rnd_9x19_M9";
};
};
_unit addEventHandler ["FiredNear", {call FUNC(onFiredNear)}];
_unit setBehaviour "CARELESS";
_unit addRating -100000;
_unit allowFleeing 0;
_unit disableAI "FSM";
_unit disableAI "TARGET";
_unit disableAI "AUTOTARGET";
if ((random 1) > 0.6) then {
_unit setUnitPos "DOWN";
} else {
_unit setUnitPos "UP";
};
_unit setCombatMode "BLUE";
_unit setSkill 0;
(selectRandom [GVAR(zombiesA), GVAR(zombiesB), GVAR(zombiesC)]) pushBack _unit;
// _markerName = format ["%1-%2", random 1, random 2];
// createMarker [_markerName, [0,0,0]];
// _markerName setMarkerShape "ICON";
// _markerName setMarkerType "mil_dot";
// _markerName setMarkerPos (getpos _unit);
// _markerName setMarkerColor "ColorGreen";
};
FUNC(onFiredNear) = {
params ["_unit", "_firer", "_dist", "_weapon"];
if (_weapon == "Throw") exitWith {};
private _silencer = switch (_weapon) do {
case (primaryWeapon _firer) : {(primaryWeaponItems _firer) select 0};
case (secondaryWeapon _firer) : {(secondaryWeaponItems _firer) select 0};
case (handgunWeapon _firer) : {(handgunItems _firer) select 0};
default {""};
};
if (_silencer != "") then {_dist = _dist * 2;};
if ((random (_dist * _dist)) > 2000) exitWith {};
_unit setVariable [QGVAR(gunShotLocation), getPosATL _firer];
};
FUNC(visualSearch) = {
params ["_unit", "_add"];
private _return = objNull;
private _nearMen = (_unit nearEntities ["Man", 150]) select {isPlayer _x};
{
private _relDir = _unit getRelDir _x;
if (((_relDir < 135) || {_relDir > 225}) && {
private _vis = [_unit, _x, _add] call FUNC(getTargetVis);
_vis = _vis + (-0.00666 * (_unit distance _x)) + _add;
(random _vis) > 1
}) exitWith {
_return = _x;
};
} forEach _nearMen;
_return
};
FUNC(getTargetVis) = {
params ["_unit", "_target"];
private _eye = eyepos _unit;
private _vis = ([_unit, "VIEW", _target] checkvisibility [_eye, eyePos _target]) + ([_unit, "VIEW", _target] checkvisibility [_eye, AGLtoASL (_target modelToWorld (_target selectionPosition "pelvis"))]);
_vis
};
_fnc_createStateMachine = {
params ["_selectCode"];
private _stateMachine = [_selectCode, true] call CBA_statemachine_fnc_create;
// State: Passive
[_stateMachine, {
private _target = [_this, -0.1] call FUNC(visualSearch);
if (!isNull _target) then {
_this setVariable [QGVAR(target), _target];
} else {
private _patrolRadius = _this getVariable [QGVAR(patrolRadius), 0];
if (((speed _this) < 0.01) && {_patrolRadius > 0} && {(random 1) > 0.9}) then {
private _origin = _this getVariable [QGVAR(origin), 0];
private _location = [_origin, _patrolRadius] call CBA_fnc_randPos;
_this moveTo _location;
};
};
}, {}, {}, "Passive"] call CBA_statemachine_fnc_addState;
// State: Search
[_stateMachine, {
private _target = [_this, 0.25] call FUNC(visualSearch);
_this setVariable [QGVAR(target), _target];
if (CBA_missionTime > (_this getVariable [QGVAR(nextMove), 0])) then {
private _location = _this getVariable [QGVAR(location), getPosATL _this];
_this setVariable [QGVAR(nextMove), (CBA_missionTime + 2)];
if ((_this distance2d _location) > 5) then {
_this moveTo _location;
} else {
private _gunShotLocation = _this getVariable QGVAR(gunShotLocation);
if (!isNil "_gunShotLocation") then {
_this setVariable [QGVAR(location), _gunShotLocation];
_this setVariable [QGVAR(gunShotLocation), nil];
} else {
_this moveTo (_location getPos [15, random 360]);
};
};
};
}, {}, {}, "Search"] call CBA_statemachine_fnc_addState;
// State: Attack
[_stateMachine, {
if (CBA_missionTime > (_this getVariable [QGVAR(nextSound), 0])) then {
playSound3D [(MISSION_ROOT + "fx\beer.ogg"), _this, false, getPosASL _this, 12, 1, 70];
_this setVariable [QGVAR(nextSound), (CBA_missionTime + 3 + random 4)];
};
private _target = _this getVariable [QGVAR(target), objNull];
if ((!alive _target) || {([_this, _target] call FUNC(getTargetVis)) < 0.75}) then {
_this setVariable [QGVAR(location), (getPosATL _target)];
_this setVariable [QGVAR(target), objNull];
} else {
if (CBA_missionTime > (_this getVariable [QGVAR(nextMove), 0])) then {
private _distance = _this distance2d _target;
_this setVariable [QGVAR(nextMove), (CBA_missionTime + 1 + (_distance / 200))];
private _targetEffectiveLocation = (getPosATL _target) vectorAdd ((velocity _target) vectorMultiply (_distance / 10));
_this moveTo _targetEffectiveLocation;
if ((alive _this) && {_distance < 3}) then {
private _spot = selectRandom ["hand_l", "hand_r", "leg_l", "leg_r", "leg_l", "leg_r"];
playSound3D [(MISSION_ROOT + "fx\attack.ogg"), _this, false, getPosASL _this, 2, 1, 30];
_this playActionNow "Gear";
["pabst_addDamPass", [_target, 0.8, _spot, "stab"], _target] call CBA_fnc_targetEvent;
};
};
};
}, {
private _speed = 1 + random 0.5;
if ((stance _this) == "PRONE") then {_speed = 3 * _speed;};
["pabst_setUnitSpeed", [_this, _speed]] call CBA_fnc_globalEvent;
}, {
private _speed = 1;
if ((stance _this) == "PRONE") then {_speed = 2 * _speed;};
["pabst_setUnitSpeed", [_this, _speed]] call CBA_fnc_globalEvent;
}, "Attack"] call CBA_statemachine_fnc_addState;
[_stateMachine, "Passive", "Search", {!isNil {_this getVariable QGVAR(gunShotLocation)}}, {}] call CBA_statemachine_fnc_addTransition;
[_stateMachine, "Passive", "Attack", {alive (_this getVariable [QGVAR(target), objNull])}, {}] call CBA_statemachine_fnc_addTransition;
[_stateMachine, "Search", "Attack", {alive (_this getVariable [QGVAR(target), objNull])}, {}] call CBA_statemachine_fnc_addTransition;
[_stateMachine, "Attack", "Search", {!alive (_this getVariable [QGVAR(target), objNull])}, {}] call CBA_statemachine_fnc_addTransition;
_stateMachine
};
[{GVAR(zombiesA) select {alive _x}}] call _fnc_createStateMachine;
[{GVAR(zombiesB) select {alive _x}}] call _fnc_createStateMachine;
[{GVAR(zombiesC) select {alive _x}}] call _fnc_createStateMachine;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment