Error checking, handle out of bounds grids

This commit is contained in:
PabstMirror 2015-06-30 11:11:53 -05:00
parent 1f7994e10f
commit 1f47ffc5de
3 changed files with 68 additions and 21 deletions

View File

@ -22,23 +22,43 @@
PARAMS_1(_pos);
DEFAULT_PARAM(1,_returnSingleString,false);
private["_count", "_easting", "_nativeGrid", "_northing"];
//Fallback, when map data is weird (letters)
if ((count GVAR(mapGridData)) == 0) exitWith {
_nativeGrid = mapGridPosition _pos;
if (_returnSingleString) then {
mapGridPosition _pos
_nativeGrid
} else {
[(mapGridPosition _pos) select [0,5], (mapGridPosition _pos) select [5,5]]
_count = floor ((count _nativeGrid) / 2);
[(_nativeGrid select [0, _count]), (_nativeGrid select [_count, _count])]
};
};
EXPLODE_4_PVT(GVAR(mapGridData),_offsetX,_realOffsetY,_stepXat5,_stepYat5);
_easting = str floor (((_pos select 0) - _offsetX) / _stepXat5);
_northing = str floor (((_pos select 1) - _realOffsetY) / _stepYat5);
_easting = floor (((_pos select 0) - _offsetX) / _stepXat5);
_northing = floor (((_pos select 1) - _realOffsetY) / _stepYat5);
while {count _easting < 5} do {_easting = "0" + _easting;};
while {count _northing < 5} do {_northing = "0" + _northing;};
//Attempt to handle negative east/north (e.g.: moving west of map bounds)
if (_easting > 0) then {
_easting = str _easting;
while {count _easting < 5} do {_easting = "0" + _easting;};
} else {
_easting = str abs _easting;
while {count _easting < 4} do {_easting = "0" + _easting;};
_easting = "-" + _easting;
};
if (_northing > 0) then {
_northing = str _northing;
while {count _northing < 5} do {_northing = "0" + _northing;};
} else {
_northing = str abs _northing;
while {count _northing < 4} do {_northing = "0" + _northing;};
_northing = "-" + _northing;
};
if (_returnSingleString) then {
_easting+_northing
_easting + _northing
} else {
[_easting, _northing]
};

View File

@ -4,22 +4,28 @@
*
* Argument:
* 0: Grid Cords <STRING>
* 1: Get Center or bottom right <BOOL><OPTIONAL>
*
* Return values:
* 0: Position <ARRAY>
* Position <ARRAY>
*
* Example:
* ["6900080085"] call ace_map_fnc_getMapPosFromGrid
*
* Public: Yes
*/
// #define DEBUG_MODE_FULL
#include "script_component.hpp"
PARAMS_1(_inputString);
DEFAULT_PARAM(1,_getCenterOfGrid,true);
private["_countInput", "_countInputHalf", "_xPart", "_xPos", "_yPart", "_yPos"];
if ((count GVAR(mapGridData)) == 0) exitWith {
ERROR("Map has bad data, falling back to BIS_fnc_gridToPos");
(_this call BIS_fnc_gridToPos) select 0
};
EXPLODE_4_PVT(GVAR(mapGridData),_offsetX,_realOffsetY,_stepXat5,_stepYat5);
_countInput = count _inputString;

View File

@ -1,12 +1,14 @@
#include "script_component.hpp"
private["_cfgGrid", "_formatX", "_formatY", "_heightOffset", "_offsetX", "_offsetY", "_originGrid", "_realOffsetY", "_startGrid", "_stepX", "_stepY", "_zoom", "_zoomMax", "_letterGrid"];
GVAR(mapGridData) = [];
//--- Extract grid values from world config (Borrowed from BIS_fnc_gridToPos)
//
_cfgGrid = configfile >> "CfgWorlds" >> worldname >> "Grid";
_offsetX = getnumber (_cfgGrid >> "offsetX");
_offsetY = getnumber (_cfgGrid >> "offsetY");
_cfgGrid = configFile >> "CfgWorlds" >> worldName >> "Grid";
_offsetX = getNumber (_cfgGrid >> "offsetX");
_offsetY = getNumber (_cfgGrid >> "offsetY");
_zoomMax = 1e99;
_format = "";
_formatX = "";
_formatY = "";
_stepX = 1e10;
@ -15,14 +17,21 @@ _stepY = 1e10;
_zoom = getnumber (_x >> "zoomMax");
if (_zoom < _zoomMax) then {
_zoomMax = _zoom;
_format = gettext (_x >> "format");
_formatX = gettext (_x >> "formatX");
_formatY = gettext (_x >> "formatY");
_stepX = getnumber (_x >> "stepX");
_stepY = getnumber (_x >> "stepY");
_formatX = getText (_x >> "formatX");
_formatY = getText (_x >> "formatY");
_stepX = getNumber (_x >> "stepX");
_stepY = getNumber (_x >> "stepY");
};
} foreach configproperties [_cfgGrid,"isclass _x",false];
} foreach configProperties [_cfgGrid, "isClass _x", false];
_letterGrid = false;
if (((toLower _formatX) find "a") != -1) then {_letterGrid = true};
if (((toLower _formatY) find "a") != -1) then {_letterGrid = true};
if (_letterGrid) exitWith {
diag_log text format ["[ACE] Map Grid Warning (%1) - Map uses letter grids [%2,%3]", worldName, _formatX, _formatY];
};
//Start at [0, 500] and move north until we get a change in grid
_heightOffset = 500;
_startGrid = mapGridPosition [0, _heightOffset];
_originGrid = _startGrid;
@ -31,9 +40,21 @@ while {_startGrid == _originGrid} do {
_originGrid = mapGridPosition [0, _heightOffset];
};
//Calculate the real y offset
_realOffsetY = parseNumber (_originGrid select [(count _formatX), (count _formatY)]) * _stepY + _heightOffset - 1;
//Calculate MGRS 10digit step - they should both be 1 meter:
_stepXat5 = _stepX * 10 ^ ((count _formatX) - 5);
_stepYat5 = -1 * _stepY * 10 ^ ((count _formatY) - 5);
if (_stepYat5 < 0) then {
diag_log text format ["[ACE] Map Grid Warning (%1) - Northing is reversed", worldName];
};
if (_stepXat5 != 1) then {
diag_log text format ["[ACE] Map Grid Warning (%1) - MGRS 10 digit grid does not equal 1 meter: (%2) for x", worldName, _stepXat5];
};
if ((_stepYat5 != 1) && {_stepYat5 != -1}) then {
diag_log text format ["[ACE] Map Grid Warning (%1) - MGRS 10 digit grid does not equal 1 meter: (%2) for y", worldName, _stepXat5];
};
GVAR(mapGridData) = [_offsetX, _realOffsetY, _stepXat5, _stepYat5];