<?xml version="1.0" encoding="utf-8"?>
<s1 title="MapFunctions"><p><p>Contents</p>
<ol><li>
<link anchor="A">AllCastles</link></li>
<li>
<link anchor="F">FieldIdToCoords</link></li>
<li>
<link anchor="F-1">FindField</link></li>
<li>
<link anchor="F-2">FormatDistance</link></li>
<li>
<link anchor="G">GetDetailInfo</link></li>
<li>
<link anchor="G-1">GetFieldId</link></li>
<li>
<link anchor="G-2">GetFieldName</link></li>
<li>
<link anchor="G-3">GetLevel</link></li>
<li>
<link anchor="G-4">GetZoneName</link></li>
<li>
<link anchor="M">MapDistance</link></li>
<li>
<link anchor="S">StateCoords</link></li>
</ol>
</li>
</ol>
<p> </p>
<p>See also: <jump href="/wiki/FieldTypes">FieldTypes</jump><br/>
 </p>
<p></p>
<s2 id="AllCastles" title="AllCastles">
<table><strong class="highlight"><![CDATA[]]></strong><tr><td><strong class="highlight"><![CDATA[]]></strong><p>Usage: </p>
</td><td><strong class="highlight"><![CDATA[]]></strong><p>AllCastles(fieldId1,fieldId2) </p>
</td></tr><tr><td><strong class="highlight"><![CDATA[]]></strong><p>Example: </p>
</td><td><strong class="highlight"><![CDATA[]]></strong><p>AllCastles(0,159399) </p>
</td></tr></table><p>This function returns an array of MapCastleBean objects within an area. The first fieldId will be the northwest (top left) most corner of the area. The second fieldId will be the southeast (bottom right) most corner of the area. Both values must be field id not coordinates.  </p>
<p>This example returns an array of all MapCastleBean objects within the state of Friesland: </p>
<ul><li><source><![CDATA[scanrec 0,0 199,199
castles = AllCastles(GetFieldId(0,0),GetFieldId(199,199))

Result:
Too long to post, just try it ;)]]></source></li>
</ul>
<p>This example will return an array of all MapCastleBean objects within a specified distance. Like a radius but in a rectangle... </p>
<ul><li><source><![CDATA[distance = 5
execute "rescanrec {city.x - distance},{city.y - distance} {city.x + distance},{city.y + distance}"
castles = AllCastles(GetFieldId(city.x - distance,city.y - distance),GetFieldId(city.x + distance , city.y + distance))

echo "There are {castles.length} cities within {distance} miles of {city.name}({city.coords})." 

label next
info = castles.shift()
if info == null end
echo  "Coord: {FieldIdToCoords(info.id)}, Username: {info.userName}, Alliance: {info.allianceName}, Prestige: {info.prestige}"
goto next

Result (debug off to reduce confusion/spam):
12:58:08 SCAN COMPLETED: 455,350 465,360
12:58:09 There are 6 cities within 5 miles of MyCity(460,355).
12:58:12 Coord: 455,353, Username: Bob, Alliance: null, Prestige: 4991
12:58:16 Coord: 457,353, Username: Fred, Alliance: null, Prestige: 8060
12:58:20 Coord: 459,357, Username: George, Alliance: null, Prestige: 10847
12:58:24 Coord: 457,358, Username: George, Alliance: null, Prestige: 10847
12:58:28 Coord: 464,360, Username: Harry, Alliance: null, Prestige: 0
12:58:32 Coord: 460,355, Username: YayMe, Alliance: 123456, Prestige: 14517826
12:58:35 Script stopped]]></source></li>
</ul>
<p></p>
</s2>
<s2 id="FieldIdToCoords" title="FieldIdToCoords">
<table><strong class="highlight"><![CDATA[]]></strong><tr><td><strong class="highlight"><![CDATA[]]></strong><p>Usage: </p>
</td><td><strong class="highlight"><![CDATA[]]></strong><p>FieldIdToCoords(fieldId) </p>
</td></tr><tr><td><strong class="highlight"><![CDATA[]]></strong><p>Example: </p>
</td><td><strong class="highlight"><![CDATA[]]></strong><p>fid = 284460<br/>
echo FieldIdToCoords(fid) </p>
</td></tr></table><p>This function converts a field id into coordinates. </p>
<p>This example finds the coordinates of a field id: </p>
<ul><li><source><![CDATA[fid = 284460
echo FieldIdToCoords(fid)

Result:
14:58:01 Starting script
14:58:01 Running line 1
14:58:01 fid = 284460
14:58:02 Running line 2
14:58:02 460,355
14:58:03 Script stopped]]></source></li>
</ul>
<p></p>
</s2>
<s2 id="FindField" title="FindField">
<table><strong class="highlight"><![CDATA[]]></strong><tr><td><strong class="highlight"><![CDATA[]]></strong><p>Usage: </p>
</td><td><strong class="highlight"><![CDATA[]]></strong><p>FindField(x,y,radius,FieldTypes[,level]) </p>
</td></tr><tr><td><strong class="highlight"><![CDATA[]]></strong><p>Example: </p>
</td><td><strong class="highlight"><![CDATA[]]></strong><p>FindField(city.x,city.y,20,12,10) </p>
</td></tr></table><p>See also: <jump href="/wiki/FieldTypes">FieldTypes</jump> </p>
<p>This function can find objects on the map within a specified radius of the searching city. You can for example use FindField to generate a list of all players near you, or all npcs level 10 within 20 miles, or whatever. The FieldTypes usable with FindField are linked above. The level is optional and can be omitted, or you may use &quot;0&quot; for any level. You can echo the results directly, or find and manipulate them from $result. </p>
<p>This example finds all the flats within a specified radius around your city, then echos the level of each: </p>
<ul><li><source><![CDATA[radius = 20
execute "rescanmap {city.coords} {radius}"
flats = FindField(city.x,city.y,radius,10)
echo "There are {flats.length} flats around {city.name} ({city.coords})."

label checkFlat
thisFlat = flats.shift()
if thisFlat == null end
echo  "Coord: {FieldIdToCoords(thisFlat)} is a level {GetLevel(thisFlat)} flat."
if flats.length goto checkFlat

Result (debug off to reduce confusion/spam):
17:02:45 SCAN COMPLETED: 422,319 radius 20
17:02:46 There are 12 flats around MyCity (422,319).
17:02:49 Coord: 410,321 is a level 3 flat.
17:02:53 Coord: 414,320 is a level 5 flat.
17:02:57 Coord: 414,327 is a level 4 flat.
17:03:01 Coord: 419,331 is a level 9 flat.
17:03:05 Coord: 421,306 is a level 1 flat.
17:03:09 Coord: 422,309 is a level 3 flat.
17:03:13 Coord: 423,301 is a level 2 flat.
17:03:17 Coord: 423,308 is a level 1 flat.
17:03:21 Coord: 424,301 is a level 10 flat.
17:03:25 Coord: 424,311 is a level 3 flat.
17:03:29 Coord: 425,302 is a level 9 flat.
17:03:33 Coord: 425,306 is a level 1 flat.
17:03:35 Script stopped]]></source></li>
</ul>
<p>This example gets a list of all npcs level 10 within 20 miles of your city, and then sorts them into an array of nearest to furthest from your city. Note - these are fieldIds not coordinates. You can turn the fieldIds into coordinates using <jump href="/wiki/MapFunctions#F">FieldIdToCoords</jump> </p>
<ul><li><source><![CDATA[list = FindField(city.x,city.y,20,12,10).sort(city.compareByDistanceToCastle)
echo list

Result:
17:09:36 251614,250815,250014,250813,251613,250015,252412,254818,246020,250823,243621,247602,237221,244396
17:09:37 Script stopped]]></source></li>
</ul>
<p></p>
</s2>
<s2 id="FormatDistance" title="FormatDistance">
<table><strong class="highlight"><![CDATA[]]></strong><tr><td><strong class="highlight"><![CDATA[]]></strong><p>Usage: </p>
</td><td><strong class="highlight"><![CDATA[]]></strong><p>FormatDistance(fieldId1,fieldId2) </p>
</td></tr><tr><td><strong class="highlight"><![CDATA[]]></strong><p>Example: </p>
</td><td><strong class="highlight"><![CDATA[]]></strong><p>FormatDistance(12345,23456) </p>
</td></tr></table><p>This function finds the distance between two field ids and displays it in a nice readable format. </p>
<p>Basic example: </p>
<ul><li><source><![CDATA[echo FormatDistance(12345,23456)

Result:
15:27:46 Starting script
15:27:46 Running line 1
15:27:46 90.09 miles
15:27:47 Script stopped]]></source></li>
</ul>
<p>This example finds the distance between your own city and a target coordinates: </p>
<ul><li><source><![CDATA[targ = "123,456"
targetId = GetFieldId(targ)
distance = FormatDistance(city.fieldId,targetId)
echo "Distance from {city.coords} to {targ} is {distance}.

Result:
15:29:56 Starting script
15:29:56 Running line 1
15:29:56 targ = 123,456
15:29:57 Running line 2
15:29:57 targetId = 364923
15:29:58 Running line 3
15:29:58 distance = 351.80 miles
15:29:59 Running line 4
15:29:59 Distance from 460,355 to 123,456 is 351.80 miles.
15:30:00 Script stopped]]></source></li>
</ul>
<p></p>
</s2>
<s2 id="GetDetailInfo" title="GetDetailInfo">
<table><strong class="highlight"><![CDATA[]]></strong><tr><td><strong class="highlight"><![CDATA[]]></strong><p>Usage: </p>
</td><td><strong class="highlight"><![CDATA[]]></strong><p>GetDetailInfo(FieldId,[priority],[expireSeconds],[compareSeconds]) </p>
</td></tr><tr><td><strong class="highlight"><![CDATA[]]></strong><p>Example: </p>
</td><td><strong class="highlight"><![CDATA[]]></strong><p>echo GetDetailInfo(GetFieldId(123,456)).name </p>
</td></tr></table><p>This function is used to get detailed information about a field id. Field id is required as a parameter, priority, expire seconds, and compare seconds are optional.  </p>
<p><em><strong>Priority</strong></em> is a true/false value to determine whether to place a request to the top of the queue for faster processing. It is only used if we have to queue update request. The default is false. </p>
<p><em><strong>ExpireSeconds</strong></em> is how long since map data was last updated until the bot considers data &quot;expired&quot;. This uses current time or value of &quot;now&quot; parameters as described below. The default is 0. If data is considered expired, the bot will queue an update request as needed. </p>
<p><em><strong>CompareSeconds</strong></em> is the timestamp used to compare with for ExpireSeconds. The bot uses the current time if this is set to 0. The default is 0. </p>
<p>This example returns previously cached map data if we have it cached less than 30 seconds ago (then it returns null, and auto-requests map update) </p>
<ul><li><source><![CDATA[id = GetFieldId(123,234)
data = GetDetailInfo(id, false, 30) 
data = GetDetailInfo(id, false, 0, date().getTime() - 30000) // same as the line above]]></source></li>
</ul>
<p>The above would make &quot;data&quot; into a variable containing the MapCastleBean object for that field id, which you could then reference, for example: </p>
<ul><li><source><![CDATA[id = GetFieldId(123,234)
data = GetDetailInfo(id, false, 30) 
if data == null repeat // repeat above if the map detail info is not available yet
echo data.name
echo data.id

Result:
16:17:39 Grassland
16:17:40 187323
16:17:41 Script stopped]]></source></li>
</ul>
<p>Or you can even echo the entire object with all it&apos;s properties like so: </p>
<ul><li><source><![CDATA[id = GetFieldId(123,234)
data = GetDetailInfo(id, false, 30) 
if data == null repeat // repeat above if the map detail info is not available yet
echo json_encode(data)

Result:
16:16:54 {"id":187323,"prestige":0,"furlough":false,"name":"Grassland","lastUpdated":1419714992381,"changeface":0,"canLoot":false,"honor":0,"playerLogoUrl":null,"canScout":true,"zoneName":"LOWER LORRAINE","canOccupy":true,"state":1,"npc":false,"userName":null,"canTrans":false,"flag":null,"allianceName":null,"relation":6,"canSend":false}
16:16:55 Script stopped]]></source></li>
</ul>
<p></p>
</s2>
<s2 id="GetFieldId" title="GetFieldId">
<table><strong class="highlight"><![CDATA[]]></strong><tr><td><strong class="highlight"><![CDATA[]]></strong><p>Usage: </p>
</td><td><strong class="highlight"><![CDATA[]]></strong><p>GetFieldId(coords) </p>
</td></tr><tr><td><strong class="highlight"><![CDATA[]]></strong><p>Example: </p>
</td><td><strong class="highlight"><![CDATA[]]></strong><p>coords = &quot;460,355&quot;<br/>
echo GetFieldId(coords) </p>
</td></tr></table><p>This function converts coordinates into a field id.  </p>
<p>The field id is a number between 0-63999. Each number directly corresponds to a square on the 800x800 grid map. The game server uses field ids to reference all the squares on the map... 0,0 has field id of 0.... 1,0 has field id of 1... 799,0 has field id of 799... 1,1 is field id 800... and so on. Many of the advanced scripting functions require you to convert your coordinates into fieldIds to use them. </p>
<p>This example finds the coordinates of a field id: </p>
<ul><li><source><![CDATA[coords = "460,355"
echo GetFieldId(coords)

Result:
15:07:40 Starting script
15:07:40 Running line 1
15:07:40 coords = 460,355
15:07:42 Running line 2
15:07:42 284460
15:07:43 Script stopped]]></source></li>
</ul>
<p></p>
</s2>
<s2 id="GetFieldName" title="GetFieldName">
<table><strong class="highlight"><![CDATA[]]></strong><tr><td><strong class="highlight"><![CDATA[]]></strong><p>Usage: </p>
</td><td><strong class="highlight"><![CDATA[]]></strong><p>GetFieldName(#) </p>
</td></tr><tr><td><strong class="highlight"><![CDATA[]]></strong><p>Example: </p>
</td><td><strong class="highlight"><![CDATA[]]></strong><p>GetFieldName(10) </p>
</td></tr></table><p>This function converts a numeric field value into a named string, for example: </p>
<ul><li><source><![CDATA[// get the name of field type 3
echo GetFieldName(3)

Result:
18:42:42 Hill
18:42:43 Script stopped]]></source></li>
<li><source><![CDATA[// first get the type of the 1st valley held in the city, then get the name for that type
echo city.fields[0].type
echo GetFieldName(city.fields[0].type)

Result:
18:47:30 5
18:47:31 Grassland
18:47:32 Script stopped]]></source></li>
</ul>
<p>The possible field numerics -&gt; strings are as follows: </p>
<ul><li><source><![CDATA[1  Forest    
2  Desert    
3  Hill      
4  Swamp     
5  Grassland 
6  Lake      
10 Flat      ]]></source></li>
</ul>
<p></p>
</s2>
<s2 id="GetLevel" title="GetLevel">
<table><strong class="highlight"><![CDATA[]]></strong><tr><td><strong class="highlight"><![CDATA[]]></strong><p>Usage: </p>
</td><td><strong class="highlight"><![CDATA[]]></strong><p>GetLevel(fieldId) </p>
</td></tr><tr><td><strong class="highlight"><![CDATA[]]></strong><p>Example: </p>
</td><td><strong class="highlight"><![CDATA[]]></strong><p>GetLevel(city.fieldId) </p>
</td></tr></table><p>This function returns the level of whatever is located at a certain field id. </p>
<ul><li><source><![CDATA[echo "The object on the map at {FieldIdToCoords(1234)} is level {GetLevel(1234)}." //This is a level 1 desert at coords 434,1 on the map

Results: 
16:28:35 Running line 1
16:28:35 The object on the map at 434,1 is level 1.
16:28:36 Script stopped]]></source></li>
</ul>
<p></p>
</s2>
<s2 id="GetZoneName" title="GetZoneName">
<table><strong class="highlight"><![CDATA[]]></strong><tr><td><strong class="highlight"><![CDATA[]]></strong><p>Usage: </p>
</td><td><strong class="highlight"><![CDATA[]]></strong><p>GetZoneName(fieldId) </p>
</td></tr><tr><td><strong class="highlight"><![CDATA[]]></strong><p>Example: </p>
</td><td><strong class="highlight"><![CDATA[]]></strong><p>GetZoneName(city.fieldId) </p>
</td></tr></table><p>This function returns the name of the state a field id is located in. </p>
<ul><li><source><![CDATA[echo "My city is located in {GetZoneName(city.fieldId)}."

Results: 
16:34:28 Running line 1
16:34:28 My city is located in thuringia.
16:34:29 Script stopped]]></source></li>
</ul>
<p></p>
</s2>
<s2 id="MapDistance" title="MapDistance">
<table><strong class="highlight"><![CDATA[]]></strong><tr><td><strong class="highlight"><![CDATA[]]></strong><p>Usage: </p>
</td><td><strong class="highlight"><![CDATA[]]></strong><p>MapDistance(x1,y1,x2,y2) </p>
</td></tr><tr><td><strong class="highlight"><![CDATA[]]></strong><p>Example: </p>
</td><td><strong class="highlight"><![CDATA[]]></strong><p>MapDistance(123,456,111,222) </p>
</td></tr></table><p>This function finds the distance between two sets of coordinates on the map. </p>
<p>This example finds the distance between your own city and target coordinates, rounded to 2 decimal places: </p>
<ul><li><source><![CDATA[targX = 123
targY = 456
distance = round(MapDistance(city.x,city.y,targX,targY),2)
echo "Distance from {city.coords} to {targX},{targY} is {distance} miles."

Result:
15:36:13 Starting script
15:36:13 Running line 1
15:36:13 targX = 123
15:36:14 Running line 2
15:36:14 targY = 456
15:36:15 Running line 3
15:36:15 distance = 351.81
15:36:16 Running line 4
15:36:16 Distance from 460,355 to 123,456 is 351.81 miles.
15:36:17 Script stopped]]></source></li>
</ul>
<p></p>
</s2>
<s2 id="StateCoords" title="StateCoords">
<table><strong class="highlight"><![CDATA[]]></strong><tr><td><strong class="highlight"><![CDATA[]]></strong><p>Usage: </p>
</td><td><strong class="highlight"><![CDATA[]]></strong><p>StateCoords(zone) </p>
</td></tr><tr><td><strong class="highlight"><![CDATA[]]></strong><p>Example: </p>
</td><td><strong class="highlight"><![CDATA[]]></strong><p>StateCoords(&quot;bohemia&quot;) </p>
</td></tr></table><p>The function returns the state coordinates range, or null if invalid. You may use the zone name or number, for example &quot;Friesland&quot; or 0.  </p>
<p>Some examples: </p>
<ul><li><source><![CDATA[echo StateCoords("upper lorraine")  // outputs "0,400 199,599"
echo StateCoords(8)                 // outputs "0,400 199,599"

echo StateCoords("all")             // outputs "0,0 799,799"

state = "Lombardy"
coords = StateCoords(state)         // coords = "200,600 399,799"
if !coords die "Unknown state name '{state}'"
execute "scanmap {coords}"          // scanmap 200,600 399,799]]></source></li>
</ul>
<p></p>

<br/>------------------------------------------------------------------------------<br/>
<p> <jump href="/wiki/CategoryFunctions">CategoryFunctions</jump> </p>
</s2></s1>