Skip to content

Commit

Permalink
rewrite common entities functions
Browse files Browse the repository at this point in the history
  • Loading branch information
commy2 committed Mar 4, 2016
1 parent 7b0c174 commit 5ba5885
Show file tree
Hide file tree
Showing 8 changed files with 125 additions and 117 deletions.
35 changes: 19 additions & 16 deletions addons/common/fnc_deleteEntity.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Description:
A function used to delete entities
Parameters:
Array, Object, Group or Marker
_entity to delete. Can be array of entites. <ARRAY, OBJECT, GROUP, LOCATION, MARKER>
Example:
(begin example)
Expand All @@ -17,36 +17,39 @@ Returns:
Author:
Rommel
---------------------------------------------------------------------------- */
#include "script_component.hpp"
SCRIPT(deleteEntity);

[_this] params [["_entity", objNull, [objNull, grpNull, locationNull, "", []]]];

switch (typeName _this) do {
switch (typeName _entity) do {
case "ARRAY" : {
{
_x call CBA_fnc_deleteEntity;
} foreach _this;
} foreach _entity;
};
case "OBJECT" : {
if (vehicle _this != _this) then {
unassignVehicle _this;
_this setPosASL [0,0,0];
if (vehicle _entity != _entity) then {
unassignVehicle _entity;
_entity setPosASL [0,0,0];
} else {
if (count ((crew _this) - [_this]) > 0) then {
(crew _this) call CBA_fnc_deleteEntity;
if ({_x != _entity} count (crew _entity) > 0) then {
(crew _entity) call CBA_fnc_deleteEntity;
};
};
deleteVehicle _this;
deleteVehicle _entity;
};
case "GROUP" : {
(units _this) call CBA_fnc_deleteEntity;
{deleteWaypoint _x} foreach (wayPoints _this);
deleteGroup _this;
(units _entity) call CBA_fnc_deleteEntity;
{deleteWaypoint _x} foreach (wayPoints _entity);
deleteGroup _entity;
};
case "LOCATION" : {
deleteLocation _this;
deleteLocation _entity;
};
case "STRING" : {
deleteMarker _this
deleteMarker _entity
};
default {deleteVehicle _this};
default {};
};
23 changes: 11 additions & 12 deletions addons/common/fnc_findEntity.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,32 @@ Description:
A function used to find out the first entity of parsed type in a nearEntitys call
Parameters:
- Type (Classname, Object)
- Position (XYZ, Object, Location or Group)
- Type (Classname or Object) <STRING, OBJECT>
- Position <OBJECT, LOCATION, GROUP, TASK, MARKER, POSITION>
Optional:
- Radius (Scalar)
- Radius <NUMBER>
Example:
(begin example)
_veh = ["LaserTarget", player] call CBA_fnc_findEntity
_veh = ["LaserTarget", player] call CBA_fnc_findEntity
(end)
Returns:
First entity; nil return if nothing
Author:
Rommel
---------------------------------------------------------------------------- */

#include "script_component.hpp"

SCRIPT(findEntity);

params ["_type","_position", ["_radius",50]];
params [["_type", "", [objNull, ""]], ["_position", objNull], ["_radius", 50, [0]]];

if (_type isEqualType objNull) then {
_type = typeOf _type;
};

// this function doesn't return the nearest entity, neither did the previous version
private _return = (_position call CBA_fnc_getPos) nearEntities [[_type], _radius];

_ret = (_position call CBA_fnc_getpos) nearEntities [[_type], _radius];
if (count _ret > 0) exitWith {_ret select 0};
nil
_return param [0]
38 changes: 13 additions & 25 deletions addons/common/fnc_getAlive.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -5,42 +5,30 @@ Description:
A function used to find out who is alive in an array or a group.
Parameters:
Array, Group or Unit
_entities - Array or Group <GROUP, ARRAY>
Example:
(begin example)
_alive = (Units player) call CBA_fnc_getAlive
_alive = (units player) call CBA_fnc_getAlive
(end)
Returns:
Array
<ARRAY>
Author:
Rommel
---------------------------------------------------------------------------- */

#include "script_component.hpp"
SCRIPT(getAlive);

[_this] params [["_entities", [], [objNull, grpNull, []]]];

if (_entities isEqualType objNull) exitwith {
alive _entities;
};

private "_typename";
_typename = typename _this;
if (_typename == "OBJECT") exitwith {alive _this};

private ["_return","_array"];
_array = [];
switch (_typename) do {
case "GROUP" : {
_array = units _this;
};
case "ARRAY" :{
_array =+ _this;
};
if (_entities isEqualType grpNull) then {
_entities = units _entities;
};

_return = [];
{
if (alive _x) then {
_return pushBack _x;
}
} foreach _array;
_return
_entities select {alive _x} // return
16 changes: 10 additions & 6 deletions addons/common/fnc_getGroup.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,24 @@ Description:
A function used to find out the group of an object.
Parameters:
Group or Unit
_entity - <OBJECT, GROUP>
Example:
(begin example)
_group = player call CBA_fnc_getGroup
_group = player call CBA_fnc_getGroup
(end)
Returns:
Group
Author:
Rommel
---------------------------------------------------------------------------- */
params [["_group", grpNull, [grpNull, objNull]]];
if ((typeName _group) isEqualTo "GROUP") exitWith {_group};
group _group
#include "script_component.hpp"
SCRIPT(getGroup);

params [["_entity", grpNull, [grpNull, objNull]]];

if (_entity isEqualType grpNull) exitWith {_entity};

group _entity
41 changes: 20 additions & 21 deletions addons/common/fnc_getSharedGroup.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -5,39 +5,38 @@ Description:
Returns existing group on side, or newly created group when not existent.
Parameters:
_side - Side to get group for [Side]
_side - Side to get group for <SIDE>
Returns:
Group [Group]
Group <GROUP>
Examples:
(begin example)
_sharedAlliedGroup = west call CBA_fnc_getSharedGroup
(end)
Author:
Sickboy
Sickboy, commy2
---------------------------------------------------------------------------- */
#include "script_component.hpp"
SCRIPT(getSharedGroup);

#define SIDES [east, west, resistance, civilian, sideLogic]

params ["_side"];

private ["_group", "_idx", "_center"];
_group = grpNull;
_idx = SIDES find _side;
if (_idx > - 1) then {
if (isNull (GVAR(groups) select _idx)) then {
_center = [_side] call CBA_fnc_createCenter;
_group = createGroup _center;
GVAR(groups) set [_idx, _group];
// TODO: Evaluate if this doesn't mess things up when multiple clients are requesting groups
publicVariable QGVAR(groups);
} else {
_group = GVAR(groups) select _idx;
};
} else {
WARNING("Illegal side parameter!");
params [["_side", sideFriendly, [west]]]; // sideFriendly as pseudo null side, replace with sideEmpty

private _id = SIDES find _side;

if (_id < 0) exitWith {grpNull};

private _group = GVAR(groups) select _id;

if (isNull _group) then {
_group = createGroup _side;
GVAR(groups) set [_id, _group];

// TODO: Evaluate if this doesn't mess things up when multiple clients are requesting groups
publicVariable QGVAR(groups);
};

_group;
_group
34 changes: 21 additions & 13 deletions addons/common/fnc_isAlive.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -5,37 +5,45 @@ Description:
A function used to find out if the group or object is alive.
Parameters:
Array, Group or Unit
_entity - Array, Group or Unit <OBJECT, GROUP, ARRAY>
Example:
(begin example)
_alive = (Units player) call CBA_fnc_getAlive
_alive = (units player) call CBA_fnc_isAlive
(end)
Returns:
Boolean
<BOOLEAN>
Author:
Rommel
---------------------------------------------------------------------------- */
#include "script_component.hpp"
SCRIPT(isAlive);

[_this] params [["_entity", objNull, [objNull, grpNull, []]]];

switch (typename _this) do {
switch (typename _entity) do {
case "ARRAY" : {
private _return = false;

{
if (_x call CBA_fnc_isalive) exitwith {true};
false;
} foreach _this;
if (_x call CBA_fnc_isAlive) exitwith {
_return = true;
};
} foreach _entity;

_return
};
case "OBJECT" : {
alive _this;
alive _entity;
};
case "GROUP" : {
if (isnull (leader _this)) then {
false;
if (isnull (leader _entity)) then {
false
} else {
(units _this) call CBA_fnc_isalive;
(units _entity) call CBA_fnc_isAlive
};
};
default {alive _this};
default {};
};
27 changes: 14 additions & 13 deletions addons/common/fnc_nearPlayer.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,32 @@ Description:
Check whether these are any players within a certain distance of a unit.
Parameters:
_unit: the unit to check the distance from. [Object]
_distance: the desired distance. [Number]
_unit - the entity to check the distance from. <OBJECT>
_distance - the desired distance. <NUMBER>
Returns:
Boolean - true if there are any players within the given
distance of the unit, false if there aren't.
true if there are any players within the given distance of the unit, false if there aren't. <BOOLEAN>
Examples:
(begin example)
[unit, distance] call CBA_fnc_nearPlayer
[unit, distance] call CBA_fnc_nearPlayer
(end)
Author:
---------------------------------------------------------------------------- */

#include "script_component.hpp"
SCRIPT(nearPlayer);

params ["_unit","_distance"];
params [["_entity", objNull], ["_distance", 0, [0]]];

private _position = _entity call CBA_fnc_getPos;
private _return = false;

private "_position";
_position = _unit call CBA_fnc_getpos;
{
if ((_position distance _x) < _distance) exitWith { true };
false;
} forEach ([] call CBA_fnc_players);
if (_position distance _x < _distance) exitWith {
_return = true;
};
} forEach allPlayers;

_return
Loading

0 comments on commit 5ba5885

Please sign in to comment.