<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[getTerrainOfRoom — fast copy of staticTerrainData[room]]]></title><description><![CDATA[<p>Suggested <code>getTerrainOfRoom()</code> is very minor, but useful function: instead of 2500 calls of <code>getTerrainAt()</code> to acquire static terrain data user now can call <code>getTerrainOfRoom()</code> once to get <strong>copy</strong> of <code>runtimeData.staticTerrainData[roomName]</code> typed array.</p>
<p>There are <code>TERRAIN_MASK_*</code> constants within existing API which can be used for future analysis of this <code>Uint8Array</code>: <strong>distance transform</strong>, <strong>skeletonization</strong>, <strong>convolution</strong> with CostMatrices, transferring to <strong>WASM</strong> etc. IMHO, terrain data acquiring and transferring is pretty expensive now to try some advanced path/map techniques.</p>
<details><summary>Simple test to try (spoiler):</summary>
<pre><code>const room = Game.rooms[Object.keys(Game.rooms)[0&rsqb;&rsqb;;
let t = 0;

t = Game.cpu.getUsed();
const arr = Game.map.getTerrainOfRoom(room);
t = Game.cpu.getUsed() - t;
console.log(`getTerrainOfRoom   = ${t.toFixed(6)} CPU`);

t = Game.cpu.getUsed();
const own = new Uint8Array(2500);
for(let i = 0; i &lt; 50; ++i)
    for(let j = 0; j &lt; 50; ++j)
        own[i + 50*j] = Game.map.getTerrainAt(i, j, room.name).charCodeAt(0);
t = Game.cpu.getUsed() - t;
console.log(`getTerrainAt 2500x = ${t.toFixed(6)} CPU`);
</code></pre>
</details>
<p>Typical performance tests:</p>
<pre><code>[5:42:29 PM]getTerrainAt 2500x = 0.988422 CPU
[5:42:30 PM]getTerrainOfRoom   = 0.015738 CPU
</code></pre>
<p><em>Need reply to ensure there aren't security underwater rocks here (constructor or accessors rewriting?) Now I'm investigating it as well.</em></p>
<p>Pull request: <a href="https://github.com/screeps/engine/pull/83" rel="nofollow">https://github.com/screeps/engine/pull/83</a></p>
<p><em>NOTE: there is a typo inside function (<code>.roomName</code> instead of <code>.name</code> if argument is a <code>Room</code> object), it'll be fixed before merging.</em></p>
]]></description><link>http://screeps.com/forum/topic/2138/getterrainofroom-fast-copy-of-staticterraindata-room</link><generator>RSS for Node</generator><lastBuildDate>Sat, 12 Sep 2026 23:22:23 GMT</lastBuildDate><atom:link href="http://screeps.com/forum/topic/2138.rss" rel="self" type="application/rss+xml"/><pubDate>Sat, 10 Mar 2018 09:25:09 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to getTerrainOfRoom — fast copy of staticTerrainData[room] on Mon, 12 Mar 2018 14:13:19 GMT]]></title><description><![CDATA[<p>Suggested <code>getTerrainOfRoom()</code> is very minor, but useful function: instead of 2500 calls of <code>getTerrainAt()</code> to acquire static terrain data user now can call <code>getTerrainOfRoom()</code> once to get <strong>copy</strong> of <code>runtimeData.staticTerrainData[roomName]</code> typed array.</p>
<p>There are <code>TERRAIN_MASK_*</code> constants within existing API which can be used for future analysis of this <code>Uint8Array</code>: <strong>distance transform</strong>, <strong>skeletonization</strong>, <strong>convolution</strong> with CostMatrices, transferring to <strong>WASM</strong> etc. IMHO, terrain data acquiring and transferring is pretty expensive now to try some advanced path/map techniques.</p>
<details><summary>Simple test to try (spoiler):</summary>
<pre><code>const room = Game.rooms[Object.keys(Game.rooms)[0&rsqb;&rsqb;;
let t = 0;

t = Game.cpu.getUsed();
const arr = Game.map.getTerrainOfRoom(room);
t = Game.cpu.getUsed() - t;
console.log(`getTerrainOfRoom   = ${t.toFixed(6)} CPU`);

t = Game.cpu.getUsed();
const own = new Uint8Array(2500);
for(let i = 0; i &lt; 50; ++i)
    for(let j = 0; j &lt; 50; ++j)
        own[i + 50*j] = Game.map.getTerrainAt(i, j, room.name).charCodeAt(0);
t = Game.cpu.getUsed() - t;
console.log(`getTerrainAt 2500x = ${t.toFixed(6)} CPU`);
</code></pre>
</details>
<p>Typical performance tests:</p>
<pre><code>[5:42:29 PM]getTerrainAt 2500x = 0.988422 CPU
[5:42:30 PM]getTerrainOfRoom   = 0.015738 CPU
</code></pre>
<p><em>Need reply to ensure there aren't security underwater rocks here (constructor or accessors rewriting?) Now I'm investigating it as well.</em></p>
<p>Pull request: <a href="https://github.com/screeps/engine/pull/83" rel="nofollow">https://github.com/screeps/engine/pull/83</a></p>
<p><em>NOTE: there is a typo inside function (<code>.roomName</code> instead of <code>.name</code> if argument is a <code>Room</code> object), it'll be fixed before merging.</em></p>
]]></description><link>http://screeps.com/forum/post/9886</link><guid isPermaLink="true">http://screeps.com/forum/post/9886</guid><dc:creator><![CDATA[Mototroller]]></dc:creator><pubDate>Mon, 12 Mar 2018 14:13:19 GMT</pubDate></item></channel></rss>