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

Update geometry related functions #442

Merged
merged 2 commits into from
Jul 19, 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
2 changes: 1 addition & 1 deletion addons/ai/fnc_taskSearchArea.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ Author:
#include "script_component.hpp"
params [
["_group", objNull, [objNull,grpNull]],
["_area", "", ["",objNull]],
["_area", "", ["",objNull,locationNull,[]], 5],
["_behaviour", "UNCHANGED", [""]],
["_combat", "NO CHANGE", [""]],
["_speed", "UNCHANGED", [""]],
Expand Down
2 changes: 1 addition & 1 deletion addons/common/CfgFunctions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,14 +96,14 @@ class CfgFunctions {
};

class Positions {
F_FILEPATH(getArea);
F_FILEPATH(getDistance);
F_FILEPATH(getPos);
F_FILEPATH(setPos);
F_FILEPATH(realHeight);
F_FILEPATH(setHeight);
F_FILEPATH(randPos);
F_FILEPATH(randPosArea);
F_FILEPATH(inArea);
F_FILEPATH(getNearest);
F_FILEPATH(getNearestBuilding);
};
Expand Down
8 changes: 8 additions & 0 deletions addons/common/backwards_comp.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,11 @@ CBA_fnc_intToString = {
if (isNil "_int") exitWith {""};
str _int
};

CBA_fnc_inArea = {
Copy link
Contributor

@Killswitch00 Killswitch00 Jul 18, 2016

Choose a reason for hiding this comment

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

This won't be compileFinal:ed, which, if I'm not mistaken, CfgFunctions functions are.

Also, there's a deprecation macro.

Copy link
Contributor

Choose a reason for hiding this comment

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

That's a general problem with this file and should probably be fixed in another PR

Copy link
Contributor

Choose a reason for hiding this comment

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

Indeed, so we should not add to the problem.

Instead, change the old function file and keep its CfgFunctions entry.

Copy link
Contributor

@commy2 commy2 Jul 18, 2016

Choose a reason for hiding this comment

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

The problem with that is, that the function will appear in the config functions viewer. Deprecated functions shouldn't appear there in my opinion.

WARNING('Deprecated function used: CBA_fnc_inArea (new: inArea)');
params ["_position", ["_zRef", objNull, ["", objNull, locationNull, []], 5]];

_position = _position call CBA_fnc_getPos;
_position inArea _zRef;
};
61 changes: 61 additions & 0 deletions addons/common/fnc_getArea.sqf
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/* ----------------------------------------------------------------------------
Function: CBA_fnc_getArea

Description:
Returns the array form of any area construct: [center, a, b, angle, isRectangle]

Parameters:
_area - The area construct to process <MARKER, TRIGGER, LOCATION, ARRAY>

Returns:
Area <ARRAY> (Empty array if invalid area was provided)

Examples:
(begin example)
_area = [marker] call CBA_fnc_getArea;

_area = [trigger] call CBA_fnc_getArea;

_area = [location] call CBA_fnc_getArea;

_area = [[center, a, b, angle, isRectangle]] call CBA_fnc_getArea;
(end)

Author:
SilentSpike
---------------------------------------------------------------------------- */

params [ ["_zRef", [], ["",objNull,locationNull,[]], 5] ];

private _area = [];
if (_zRef isEqualType "") then {
// Validate that marker exists and is correct shape
if ((markerShape _zRef) in ["RECTANGLE","ELLIPSE"]) then {
_area pushBack (markerPos _zRef);
_area append (markerSize _zRef);
_area pushBack (markerDir _zRef);
_area pushBack ((markerShape _zRef) == "RECTANGLE");
};
} else {
if (_zRef isEqualType objNull) then {
// Validate that object is a trigger
if !((triggerArea _zRef) isEqualTo []) then {
_area pushBack (getPos _zRef);
_area append (triggerArea _zRef);
};
} else {
if (_zRef isEqualType locationNull) then {
_area pushBack (getPos _zRef);
_area append (size _zRef);
_area pushBack (direction _zRef);
_area pushBack (rectangular _zRef);
} else {
// Validate that area is of correct form
if (_area isEqualTypeArray [[],0,0,0,true]) then {
_area = _zRef;
};
};
};
};

_area
90 changes: 0 additions & 90 deletions addons/common/fnc_inArea.sqf

This file was deleted.

84 changes: 37 additions & 47 deletions addons/common/fnc_randPosArea.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,26 @@
Function: CBA_fnc_randPosArea

Description:
Find a random (uniformly distributed) position within the given area.
Find a random (uniformly distributed) position within the given area without rejection sampling.

* You can <CBA_fnc_randPos> to find a position within a simple radius.
* You can use <CBA_fnc_randPos> to find a position within a simple radius.

Parameters:
_zone - The zone to find a position within <MARKER, TRIGGER>
_area - The area to find a position within <MARKER, TRIGGER, LOCATION, ARRAY>
_perimeter - True to return only positions on the area perimeter (optional, default: false) <BOOLEAN>

Returns:
Position <ARRAY> (Empty array if non-area object/marker was given)
Position <ARRAY> (Empty array if invalid area was provided)

Examples:
(begin example)
_position = [marker, true] call CBA_fnc_randPosArea;

_position = [trigger] call CBA_fnc_randPosArea;

_position = [location] call CBA_fnc_randPosArea;

_position = [[center, a, b, angle, isRectangle]] call CBA_fnc_randPosArea;
(end)

Author:
Expand All @@ -26,58 +30,44 @@ Author:
#include "script_component.hpp"
SCRIPT(randPosArea);

private ["_zRef","_zSize","_zDir","_zRect","_zPos","_perimeter","_posVector"];
_zRef = _this select 0;
_perimeter = if (count _this > 1) then {_this select 1} else {false};

switch (typeName _zRef) do {
case "STRING" : {
if ((markerShape _zRef) in ["RECTANGLE","ELLIPSE"]) then {
_zSize = markerSize _zRef;
_zDir = markerDir _zRef;
_zRect = (markerShape _zRef) == "RECTANGLE";
_zPos = markerPos _zRef;
};
};
case "OBJECT" : {
if !((triggerArea _zRef) isEqualTo []) then {
_zSize = triggerArea _zRef;
_zDir = _zSize select 2;
_zRect = _zSize select 3;
_zPos = getPos _zRef;
};
};
};
params [
["_zRef", [], ["",objNull,locationNull,[]], 5],
["_perimeter", false, [true]]
];
private _area = [_zRef] call CBA_fnc_getArea;

if (_area isEqualTo []) exitWith {[]};

if (isNil "_zSize") exitWith {[]};
_area params ["_center","_a","_b","_angle","_isRect"];

private ["_x","_y","_a","_b","_rho","_phi","_x1","_x2","_y1","_y2"];
if (_zRect) then {
_x = _zSize select 0;
_y = _zSize select 1;
_a = _x*2;
_b = _y*2;
private _posVector = [0,0,0];
if (_isRect) then {
private _2a = _a*2;
private _2b = _b*2;

if (_perimeter) then {
_rho = random (2*(_a + _b));
private _rho = random (4*(_a + _b));

_x1 = (_rho min _a);
_y1 = ((_rho - _x1) min _b) max 0;
_x2 = ((_rho - _x1 - _y1) min _a) max 0;
_y2 = ((_rho - _x1 - _y1 - _x2) min _b) max 0;
_posVector = [(_x1 - _x2) - _x, (_y1 - _y2) - _y, 0];
private _x1 = (_rho min _2a);
private _y1 = ((_rho - _x1) min _2b) max 0;
private _x2 = ((_rho - _x1 - _y1) min _2a) max 0;
private _y2 = ((_rho - _x1 - _y1 - _x2) min _2b) max 0;
_posVector = [(_x1 - _x2) - _a, (_y1 - _y2) - _b, 0];
} else {
_posVector = [random(_a) - _x, random(_b) - _y, 0];
_posVector = [random(_2a) - _a, random(_2b) - _b, 0];
};
} else {
_rho = if (_perimeter) then {1} else {random 1};
_phi = random 360;
_x = sqrt(_rho) * cos(_phi);
_y = sqrt(_rho) * sin(_phi);
// Generate point on circle of R=1
private _rho = [random 1, 1] select _perimeter;
private _phi = random 360;

// Scale circle to dimensions of the ellipse
private _x = sqrt(_rho) * cos(_phi);
private _y = sqrt(_rho) * sin(_phi);

_posVector = [_x * (_zSize select 0), _y * (_zSize select 1), 0];
_posVector = [_x * _a, _y * _b, 0];
};

_posVector = [_posVector, -_zDir] call BIS_fnc_rotateVector2D;
_posVector = [_posVector, -_angle] call BIS_fnc_rotateVector2D;

_zPos vectorAdd _posVector
_center vectorAdd _posVector