Example Code: Convert Position To Map Grid: Difference between revisions

From Bohemia Interactive Community
Jump to navigation Jump to search
m (added category)
(Format to Example Code)
Line 1: Line 1:
= Problem =
__NOTOC__
{{ArgTitle|{{arma3}}|2|{{GVI|arma3|1.00}} {{GVI|arma2|1.04}}}}
{{Informative |
* [[mapGridPosition]] has been introduced in {{GVI|arma2|1.04}}
}}


How to get map's grid from object's position? For example: how to get "Fe 26" from [10987.8, 15092.8, 0.0017395].
[[private]] _pos = [[getPosATL]] [[player]];
[[private]] _mapGridPosition = [[mapGridPosition]] _pos;


= Solution =


'''[[mapGridPosition]]'''
{{ArgTitle|{{arma2}}|2|{{GVI|arma2|1.00}}}}
{{Informative |
* [[BIS_fnc_PosToGrid]] has been introduced in {{GVI|arma2|1.00}}
}}


and before this command appeared, here is how it was found:
[[private]] _mapGridPosition = [[player]] [[call]] [[BIS_fnc_PosToGrid]];




Simgle small grid has size of 200*200. Moreover position [0,0,0] is somewhere in the middle of second big grid on Y axis. Fraction parts of coordinates and Z axis are also useless. So at the begining we have to adjust our position.
{{ArgTitle|{{arma}}|2|{{GVI|arma|1.00}}}}
{{Informative|
* [[floor]] has been introduced in {{GVI|arma|1.00}}
}}


<pre>_this = [floor ((_this select 0) / 200), floor (((_this select 1) + 520) / 200)]</pre>
[[private]] _gridSize = 200; {{cc|given grid squares are 200&nbsp;&times;&nbsp;200}}
[[private]] _yOffset = -280; {{cc|position [0,0,0] is somewhere in the middle of second big grid on Y axis}}
[[private]] _xPos = [[floor]] (([[_this]] [[select]] 0) / _gridSize);
[[private]] _yPos = [[floor]] ((([[_this]] [[select]] 1) + _yOffset) / _gridSize);
[[private]] _letter1Index = [[floor]] (_xPos / 10);
[[private]] _letter2Index = [[floor]] (_xPos % 10);
[[private]] _letter1Choice = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J"];
[[private]] _letter2Choice = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"];
[[private]] _letter1 = _letter1Choice [[select]] _letter1Index;
[[private]] _letter2 = _letter2Choice [[select]] _letter2Index;
[[private]] _numbers = 100 - _yPos; {{cc|coordinate numbers start from top-left of the map}}
[[private]] _mapGridPosition = [[format]] ["%1%2%3", _letter1, _letter2, _numbers];


The result is id of X and Y grids.


* Getting X axis grid
{{ArgTitle|{{ofp}}|2|{{GVI|ofp|1.00}}}}
{{Informative | This is an [[SQS syntax]] example.}}


Notice that for marking X grids engine uses letters from A to J (ten symbols). So to get string id from numeric id we can simply exchange 0 on A, 1 on B etc.
{{codecomment|; given grid squares are 200&nbsp;&times;&nbsp;200}}
[[private]] _gridSize = 200
{{codecomment|; position [0,0,0] is somewhere in the middle of second big grid on Y axis}}
[[private]] _yOffset = -280
[[private]] _xPos = ([[_this]] [[select]] 0) / _gridSize
[[private]] _yPos = (([[_this]] [[select]] 1) + _yOffset) / _gridSize
_xPos = _xPos - (_xPos [[mod]] 1)
_yPos = _yPos - (_yPos [[mod]] 1)
[[private]] _letter1Index = _xPos / 10
[[private]] _letter2Index = _xPos % 10
_letter1Index = _letter1Index - (_letter1Index [[mod]] 1)
_letter2Index = _letter2Index - (_letter2Index [[mod]] 1)
[[private]] _letter1Choice = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J"]
[[private]] _letter2Choice = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"]
[[private]] _letter1 = _letter1Choice [[select]] _letter1Index
[[private]] _letter2 = _letter2Choice [[select]] _letter2Index
{{codecomment|coordinate numbers start from top-left of the map}}
[[private]] _numbers = 100 - _yPos
[[private]] _mapGridPosition = [[format]] ["%1%2%3", _letter1, _letter2, _numbers]


<pre>?_t == 0: _str = _str + "A"
?_t == 1: _str = _str + "B"
?_t == 2: _str = _str + "C"
?_t == 3: _str = _str + "D"
?_t == 4: _str = _str + "E"
?_t == 5: _str = _str + "F"
?_t == 6: _str = _str + "G"
?_t == 7: _str = _str + "H"
?_t == 8: _str = _str + "I"
?_t == 9: _str = _str + "J"</pre>


First number is exact division of X grid id by ten:
[[Category:Example Code]]
 
<pre>_t = floor ((this select 0) / 10)</pre>
 
Second number is then remainder of the division of X grid id by ten
 
<pre>_t = (this select 0) mod 10</pre>
 
* Getting Y axis grind
 
Notice that the begining of coordinate system is in SE corner of map but counting Y grid begins from NE corner. So proper Y grid is substraction beetwen grinds count (100) and actual grid id.
 
<pre>_y = 100 - (_this select 1)</pre>
 
= Implementations =
 
* SQS Script
 
Arguments: [[Position]]
 
<pre>_this = [floor ((_this select 0) / 200), floor (((_this select 1) - 280) / 200)]
 
_str = str _this + "\n"
 
;OX
_x = _this select 0
_t = floor (_x / 10)
?_t == 0: _str = _str + "A"
?_t == 1: _str = _str + "B"
?_t == 2: _str = _str + "C"
?_t == 3: _str = _str + "D"
?_t == 4: _str = _str + "E"
?_t == 5: _str = _str + "F"
?_t == 6: _str = _str + "G"
?_t == 7: _str = _str + "H"
?_t == 8: _str = _str + "I"
?_t == 9: _str = _str + "J"
_t = _x mod 10
?_t == 0: _str = _str + "a"
?_t == 1: _str = _str + "b"
?_t == 2: _str = _str + "c"
?_t == 3: _str = _str + "d"
?_t == 4: _str = _str + "e"
?_t == 5: _str = _str + "f"
?_t == 6: _str = _str + "g"
?_t == 7: _str = _str + "h"
?_t == 8: _str = _str + "i"
?_t == 9: _str = _str + "j"
 
_str = _str + " "
 
;OY
_str = _str + str (100 - (_this select 1))
 
Hint _str</pre>
 
 
[[Category:Scripting Topics]]

Revision as of 12:38, 5 September 2019

2

private _pos = getPosATL player;
private _mapGridPosition = mapGridPosition _pos;


2

private _mapGridPosition = player call BIS_fnc_PosToGrid;


-wrong parameter ("Arma") defined!-1.00

2

private _gridSize = 200; // given grid squares are 200 × 200
private _yOffset = -280; // position [0,0,0] is somewhere in the middle of second big grid on Y axis

private _xPos = floor ((_this select 0) / _gridSize);
private _yPos = floor (((_this select 1) + _yOffset) / _gridSize);

private _letter1Index = floor (_xPos / 10);
private _letter2Index = floor (_xPos % 10);
private _letter1Choice = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J"];
private _letter2Choice = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"];
private _letter1 = _letter1Choice select _letter1Index;
private _letter2 = _letter2Choice select _letter2Index;

private _numbers = 100 - _yPos; // coordinate numbers start from top-left of the map

private _mapGridPosition = format ["%1%2%3", _letter1, _letter2, _numbers];


2

This is an SQS syntax example.
; given grid squares are 200 × 200
private _gridSize = 200

; position [0,0,0] is somewhere in the middle of second big grid on Y axis
private _yOffset = -280

private _xPos = (_this select 0) / _gridSize
private _yPos = ((_this select 1) + _yOffset) / _gridSize
_xPos = _xPos - (_xPos mod 1)
_yPos = _yPos - (_yPos mod 1)

private _letter1Index = _xPos / 10
private _letter2Index = _xPos % 10
_letter1Index = _letter1Index - (_letter1Index mod 1)
_letter2Index = _letter2Index - (_letter2Index mod 1)
private _letter1Choice = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J"]
private _letter2Choice = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"]
private _letter1 = _letter1Choice select _letter1Index
private _letter2 = _letter2Choice select _letter2Index

coordinate numbers start from top-left of the map
private _numbers = 100 - _yPos

private _mapGridPosition = format ["%1%2%3", _letter1, _letter2, _numbers]