From 05ce4f824c46b451093285d1ee53d90315847430 Mon Sep 17 00:00:00 2001
From: VKing <kauestad@gmail.com>
Date: Wed, 20 Jan 2016 00:10:27 +0100
Subject: [PATCH] Rewrite fnc_getRange to work with specified minimum and
 maximum ranges Also cleaning up fnc_keyUp a bit

---
 addons/fcs/functions/fnc_getRange.sqf | 25 +++++++++++++++++++++++--
 addons/fcs/functions/fnc_keyDown.sqf  |  5 +----
 addons/fcs/functions/fnc_keyUp.sqf    | 21 ++++++++-------------
 3 files changed, 32 insertions(+), 19 deletions(-)

diff --git a/addons/fcs/functions/fnc_getRange.sqf b/addons/fcs/functions/fnc_getRange.sqf
index 51cdf564df..46a4565352 100644
--- a/addons/fcs/functions/fnc_getRange.sqf
+++ b/addons/fcs/functions/fnc_getRange.sqf
@@ -3,7 +3,9 @@
  * Read laser distance measurement from engine.
  *
  * Argument:
- * None
+ * 0: Measurement Accuracy (default: 1) <NUMBER>
+ * 1: Maximum measure distance (default: 5000) <NUMBER>
+ * 2: Minimum measure distance (default: 0) <NUMBER>
  *
  * Return value:
  * Measured distance <NUMBER>
@@ -12,7 +14,26 @@
  */
 #include "script_component.hpp"
 
+params [["_accuracy",1], ["_maxDistance",5000], ["_minDistance",0]];
+
 disableSerialization;
 private _dlgRangefinder = uiNamespace getVariable ["ACE_dlgRangefinder", displayNull];
 
-parseNumber ctrlText (_dlgRangefinder displayCtrl 151);
+private _distance = parseNumber ctrlText (_dlgRangefinder displayCtrl 151);
+
+if (isNil "_distance" || _distance == 0) then {
+    _distance = _this call EFUNC(common,getTargetDistance);
+} else {
+    if (_distance > _maxDistance) then {
+        _distance = _maxDistance;
+    };
+
+    if (_distance < _minDistance) then {
+        _distance = _minDistance;
+    };
+
+    _accuracy = if(_accuracy < 1) then {1} else {_accuracy};
+    _distance = (round (_distance/_accuracy)) * _accuracy;
+};
+
+_distance
diff --git a/addons/fcs/functions/fnc_keyDown.sqf b/addons/fcs/functions/fnc_keyDown.sqf
index 488611fde1..86b72252d1 100644
--- a/addons/fcs/functions/fnc_keyDown.sqf
+++ b/addons/fcs/functions/fnc_keyDown.sqf
@@ -15,7 +15,7 @@
 
 params ["_vehicle", "_turret"];
 
-private _distance = call FUNC(getRange);
+private _distance = [] call FUNC(getRange);
 call FUNC(updateRangeHUD);
 
 if !(!GVAR(enabled) && FUNC(canUseFCS)) exitWith {};
@@ -23,9 +23,6 @@ if !(!GVAR(enabled) && FUNC(canUseFCS)) exitWith {};
 GVAR(Enabled) = true;
 GVAR(time) = ACE_time;
 
-if (_distance == 0) then {
-    _distance = [5, 5000, 0] call EFUNC(common,getTargetDistance); // maximum distance: 5000m, 5m precision
-};
 
 private _weaponDirection = _vehicle weaponDirection (_vehicle currentWeaponTurret _turret);  // @todo doesn't work for sub turrets
 
diff --git a/addons/fcs/functions/fnc_keyUp.sqf b/addons/fcs/functions/fnc_keyUp.sqf
index e729a48b86..4e26eb8023 100644
--- a/addons/fcs/functions/fnc_keyUp.sqf
+++ b/addons/fcs/functions/fnc_keyUp.sqf
@@ -20,15 +20,11 @@ private _turretConfig = [configFile >> "CfgVehicles" >> typeOf _vehicle, _turret
 call FUNC(updateRangeHUD);
 
 if (isNil "_distance") then {
-    _distance = call FUNC(getRange);
-
-    if (_distance == 0) then {
-        _distance = [
-            getNumber (_turretConfig >> QGVAR(DistanceInterval)),
-            getNumber (_turretConfig >> QGVAR(MaxDistance)),
-            getNumber (_turretConfig >> QGVAR(MinDistance))
-        ] call EFUNC(common,getTargetDistance); // maximum distance: 5000m, 5m precision
-    };
+    _distance = [
+        getNumber (_turretConfig >> QGVAR(DistanceInterval)),
+        getNumber (_turretConfig >> QGVAR(MaxDistance)),
+        getNumber (_turretConfig >> QGVAR(MinDistance))
+    ] call FUNC(getRange);
 };
 
 private _weapon = _vehicle currentWeaponTurret _turret;
@@ -172,10 +168,9 @@ if (_showHint) then {
     [format ["%1: %2", localize LSTRING(ZeroedTo), _distance]] call EFUNC(common,displayTextStructured);
 };
 
-//Update the hud's distance display to the new value or "----" if out of range
-//(10m fudge because of EFUNC(common,getTargetDistance))
-if (_distance + 10 >= getNumber (_turretConfig >> QGVAR(MaxDistance))) then {
-    ((uiNamespace getVariable ["ACE_dlgRangefinder", displayNull]) displayCtrl 1713151) ctrlSetText "----";
+// Display the minimum or maximum distance with an * if out of bounds
+if (_distance >= getNumber (_turretConfig >> QGVAR(MaxDistance)) || _distance <= getNumber (_turretConfig >> QGVAR(MinDistance))) then {
+    ((uiNamespace getVariable ["ACE_dlgRangefinder", displayNull]) displayCtrl 1713151) ctrlSetText ([_distance, 4, 0] call CBA_fnc_formatNumber) + "*";
 } else {
     ((uiNamespace getVariable ["ACE_dlgRangefinder", displayNull]) displayCtrl 1713151) ctrlSetText ([_distance, 4, 0] call CBA_fnc_formatNumber);
 };