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

AI function fixes and improvements #440

Merged
merged 3 commits into from
Jul 17, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
32 changes: 15 additions & 17 deletions addons/ai/fnc_taskDefend.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,12 @@ _position = [_position,_group] select (_position isEqualTo []);
_position = _position call CBA_fnc_getPos;

[_group] call CBA_fnc_clearWaypoints;
_group enableAttack false;

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reason behind this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since it's now using _unit disableAI "PATH"; this allows units not assigned to a position to counterattack in the case of contact

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I updated the commit message to more clearly reflect the reasoning

private _statics = _position nearObjects ["StaticWeapon", _radius];
private _buildings = _position nearObjects ["Building", _radius];

// Filter out occupied statics
[_statics,{(_x emptyPositions "Gunner") > 0},true] call CBA_fnc_filter;
_statics = _statics select {(_x emptyPositions "Gunner") > 0};

// Filter out buildings below the size threshold (and store positions for later use)
_buildings = _buildings select {
Expand All @@ -57,15 +56,17 @@ _buildings = _buildings select {
count (_positions) > _threshold
};

// If patrolling is enabled then the leader must be free to lead it
private _units = units _group;
private _assigned = 0;
if (_patrol && {count _units > 1}) then {
_units deleteAt (_units find (leader _group));
};

{
// 31% chance to occupy nearest free static weapon
if ((random 1 < 0.31) && { !(_statics isEqualto []) }) then {
_x assignAsGunner (_statics deleteAt 0);
[_x] orderGetIn true;

_assigned = _assigned + 1;
} else {
// 93% chance to occupy a random nearby building position
if ((random 1 < 0.93) && { !(_buildings isEqualto []) }) then {
Expand All @@ -77,32 +78,29 @@ private _assigned = 0;

// If building positions are all taken remove from possible buildings
if (_array isEqualTo []) then {
_buildings = _buildings - [_building];
_buildings deleteAt (_buildings find _building);
_building setVariable ["CBA_taskDefend_positions",nil];
} else {
_building setVariable ["CBA_taskDefend_positions",_array];
};
_building setVariable ["CBA_taskDefend_positions",_array];

// AI manipulation trickey to keep them in position until commanded to move
// Wait until AI is in position then force them to stay
[_x, _pos] spawn {
params ["_unit","_pos"];
if (surfaceIsWater _pos) exitwith {};

_unit doMove _pos;
sleep 5;
waituntil {unitReady _unit};
_unit disableAI "move";
doStop _unit;
waituntil {!(unitReady _unit)};
_unit enableAI "move";
_unit disableAI "PATH";
_unit setUnitPos "UP";
};

_assigned = _assigned + 1;
};
};
};
} forEach _units;

// If half of the group's units aren't assigned then patrol
if (_patrol && {_assigned < (count _units) * 0.5}) then {
[_group, _position, _radius, 5, "sad", "safe", "red", "limited"] call CBA_fnc_taskpatrol;
// Remaining units will patrol if enabled
if (_patrol) then {
[_group, _position, _radius, 5, "sad", "safe", "red", "limited"] call CBA_fnc_taskPatrol;
};
11 changes: 8 additions & 3 deletions addons/ai/fnc_taskSearchArea.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -64,16 +64,21 @@ private _pos = [_area] call CBA_fnc_randPosArea;
if ((_pos isEqualTo []) || {_area isEqualTo ""} || {isNull _group}) exitWith {};

// Prepare recursive function call statement
private _statement = "[this] call CBA_fnc_taskSearchArea;";
private _statements = ["[this] call CBA_fnc_taskSearchArea"];

// Prepare building search statement
private _building = nearestBuilding _pos;
if ((_building distanceSqr _pos) < 400) then {
_statement = _statement + "[this] call CBA_fnc_searchNearby;";
// Clear waypoint to prevent getting stuck in a search loop
_statements append [
"deleteWaypoint [group this, currentWaypoint (group this)]",
"[group this] call CBA_fnc_searchNearby"
];
};

// Inject the statement in this order to ensure valid syntax
_onComplete = _statement + _onComplete;
_statements pushBack _onComplete;
_onComplete = _statements joinString ";";

// Add the waypoint
[
Expand Down