From 757436140bd050a1672040e0b2cccc6f9856033d Mon Sep 17 00:00:00 2001 From: VKing Date: Wed, 20 Jan 2016 00:06:06 +0100 Subject: [PATCH] fnc_getTargetDistance rewritten to use LineIntersectsSurfaces. Turns out it wasn't really needed after all, but better is better :p --- .../functions/fnc_getTargetDistance.sqf | 44 ++++++++----------- 1 file changed, 18 insertions(+), 26 deletions(-) diff --git a/addons/common/functions/fnc_getTargetDistance.sqf b/addons/common/functions/fnc_getTargetDistance.sqf index 4bd050a2c5..07a989270d 100644 --- a/addons/common/functions/fnc_getTargetDistance.sqf +++ b/addons/common/functions/fnc_getTargetDistance.sqf @@ -1,11 +1,11 @@ /* - * Author: commy2 + * Author: VKing * Get the distance to the next object the player is looking at. Used for laser distance measurements. * * Arguments: - * 0: Messurement Accuracy - * 1: Maximal messure distance - * 2: Minimal messure distance (default: nil) + * 0: Measurement Accuracy (default: 1) + * 1: Maximum measure distance (default: 5000) + * 2: Minimum measure distance (default: 0) * * Return Value: * Distance in meters @@ -14,33 +14,25 @@ */ #include "script_component.hpp" -params ["_interval", "_maxDistance", "_minDistance"]; +params [["_accuracy",1], ["_maxDistance",5000], ["_minDistance",0]]; -private _position = ATLToASL positionCameraToWorld [0, 0, 0]; -_position set [2, (_position select 2) - (getTerrainHeightASL _position min 0)]; +private _camPosition = AGLToASL positionCameraToWorld [0, 0, 0]; +private _aimLinePos = AGLToASL positionCameraToWorld [0, 0, _maxDistance]; -private _laser = + _position; -private _line = [_position, _laser]; +private _LIS = lineIntersectsSurfaces [_camPosition, _aimLinePos]; -private _distance = _maxDistance; -private _iteration = _distance; - -while { - _iteration > _interval / 2 -} do { - _iteration = _iteration / 2; - - _laser = ATLToASL positionCameraToWorld [0, 0, _distance]; - _laser set [2, (_laser select 2) - (getTerrainHeightASL _laser min 0)]; - _line set [1, _laser]; - - _distance = _distance + (([1, -1] select (lineIntersects (_line + [vehicle ACE_player]) || {terrainIntersectASL _line})) * _iteration); +private _distance = 0; +if (count _LIS > 0) then { + _distance = _camPosition vectorDistance ((_LIS select 0) select 0); +} else { + _distance = _maxDistance; }; -_distance = _interval * round (_distance / _interval); +if (_distance < _minDistance) then { + _distance = _minDistance; +}; -_distance = _distance min _maxDistance; - -if (!isNil "_minDistance") then {_distance = _distance max _minDistance}; +_accuracy = if(_accuracy < 1) then {1} else {_accuracy}; +_distance = (round (_distance/_accuracy)) * _accuracy; _distance