docs.html 13.2 KB
Newer Older
nachtgold's avatar
nachtgold committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
{% extends 'core/base.html' %}
{% load static %}

{% block earlycss %}
    <link rel="stylesheet" type="text/css" href="{% static "docs/github-markdown.css" %}" />
{% endblock %}

{% block css %}
    <link rel="stylesheet" type="text/css" href="{% static "core/highlight.js/styles/agate.css" %}" />
    <link rel="stylesheet" type="text/css" href="{% static "docs/docs.css" %}" />
{% endblock %}

{% block js%}
    <script src="{% static "core/highlight.js/highlight.pack.js" %}"></script>
    <script>hljs.initHighlightingOnLoad();</script>
{% endblock %}

{% block content %}

<div class="docs_container markdown-body">


<h1>Bot Documentation</h1>

<div id="docs_content">

<p>This is a brief overview of all usable functions within the bot logic.</p>
<p>
nachtgold's avatar
nachtgold committed
29 30 31 32 33
 Every player can start with a simple demo bot. You get the code in the
 <a href="/snake/edit/latest">editor</a>. A snake hungers, so it constantly
 loses size. Feed it and it will stay alive and grows. Your snake will die,
 when it touches another snake with its head.

nachtgold's avatar
nachtgold committed
34
 <h2>Lua global namespace</h2>
35
 <p>Every snake is allowed to store 10MB within the global namespace and the
nachtgold's avatar
nachtgold committed
36 37
 data persists until the bot dies. So the bot can save data from frame
 to frame.</p>
nachtgold's avatar
nachtgold committed
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
 <p>The following Lua standard functions are allowed:</p>
 <table>
 <tr><td><a href="https://www.lua.org/manual/5.3/manual.html#pdf-assert">assert</a></td>
 <td><a href="https://www.lua.org/manual/5.3/manual.html#pdf-print">print</a></td>
 <td><a href="https://www.lua.org/manual/5.3/manual.html#pdf-ipairs">ipairs</a></td>
 <td><a href="https://www.lua.org/manual/5.3/manual.html#pdf-error">error</a></td>
 <td><a href="https://www.lua.org/manual/5.3/manual.html#pdf-next">next</a></td>
 <td><a href="https://www.lua.org/manual/5.3/manual.html#pdf-pairs">pairs</a></td>
 <td><a href="https://www.lua.org/manual/5.3/manual.html#pdf-pcall">pcall</a></td></tr>
 <tr><td><a href="https://www.lua.org/manual/5.3/manual.html#pdf-select">select</a></td>
 <td><a href="https://www.lua.org/manual/5.3/manual.html#pdf-tonumber">tonumber</a>,
 <td><a href="https://www.lua.org/manual/5.3/manual.html#pdf-tostring">tostring</a></td>
 <td><a href="https://www.lua.org/manual/5.3/manual.html#pdf-type">type</a></td>
 <td><a href="https://www.lua.org/manual/5.3/manual.html#pdf-unpack">unpack</a></td>
 <td><a href="https://www.lua.org/manual/5.3/manual.html#pdf-_VERSION">_VERSION</a></td>
 <td><a href="https://www.lua.org/manual/5.3/manual.html#pdf-xpcall">xpcall</a></td></tr></table>
 <p>From the module <code>math</code> you can use:</p>
 <table><tr><td><a href="https://www.lua.org/manual/5.3/manual.html#pdf-math.abs">abs</a></td>
 <td><a href="https://www.lua.org/manual/5.3/manual.html#pdf-math.acos">acos</a></td>
 <td><a href="https://www.lua.org/manual/5.3/manual.html#pdf-math.asin">asin</a></td>
 <td><a href="https://www.lua.org/manual/5.3/manual.html#pdf-math.atan">atan</a></td>
 <td><a href="https://www.lua.org/manual/5.3/manual.html#pdf-math.ceil">ceil</a></td>
 <td><a href="https://www.lua.org/manual/5.3/manual.html#pdf-math.cos">cos</a></td>
 <td><a href="https://www.lua.org/manual/5.3/manual.html#pdf-math.deg">deg</a></td></tr>
 <tr><td><a href="https://www.lua.org/manual/5.3/manual.html#pdf-math.exp">exp</a></td>
 <td><a href="https://www.lua.org/manual/5.3/manual.html#pdf-math.floor">floor</a></td>
 <td><a href="https://www.lua.org/manual/5.3/manual.html#pdf-math.fmod">fmod</a></td>
 <td><a href="https://www.lua.org/manual/5.3/manual.html#pdf-math.frexp">frexp</a></td>
 <td><a href="https://www.lua.org/manual/5.3/manual.html#pdf-math.huge">huge</a></td>
 <td><a href="https://www.lua.org/manual/5.3/manual.html#pdf-math.log">log</a></td>
 <td><a href="https://www.lua.org/manual/5.3/manual.html#pdf-math.max">max</a></td></tr>
 <tr><td><a href="https://www.lua.org/manual/5.3/manual.html#pdf-math.min">min</a></td>
 <td><a href="https://www.lua.org/manual/5.3/manual.html#pdf-math.modf">modf</a></td>
 <td><a href="https://www.lua.org/manual/5.3/manual.html#pdf-math.pi">pi</a></td>
 <td><a href="https://www.lua.org/manual/5.3/manual.html#pdf-math.pow">pow</a></td>
 <td><a href="https://www.lua.org/manual/5.3/manual.html#pdf-math.rad">rad</a></td>
 <td><a href="https://www.lua.org/manual/5.3/manual.html#pdf-math.random">random</a></td>
 <td><a href="https://www.lua.org/manual/5.3/manual.html#pdf-math.randomseed">randomseed</a></td></tr>
 <tr><td><a href="https://www.lua.org/manual/5.3/manual.html#pdf-math.sin">sin</a></td>
 <td><a href="https://www.lua.org/manual/5.3/manual.html#pdf-math.sqrt">sqrt</a></td>
 <td><a href="https://www.lua.org/manual/5.3/manual.html#pdf-math.tan">tan</a></td></tr></table>
 <p>From the module <code>os</code> you can use:</p>
 <table><tr><td><a href="https://www.lua.org/manual/5.3/manual.html#pdf-os.clock">clock</a></td>
 <td><a href="https://www.lua.org/manual/5.3/manual.html#pdf-os.difftime">difftime</a></td>
 <td><a href="https://www.lua.org/manual/5.3/manual.html#pdf-os.time">time</a></td></tr></table>
 <p>From the module <code>table</code> you can use:</p>
 <table><tr><td><a href="https://www.lua.org/manual/5.3/manual.html#pdf-table.insert">insert</a></td>
 <td><a href="https://www.lua.org/manual/5.3/manual.html#pdf-table.remove">remove</a></td>
 <td><a href="https://www.lua.org/manual/5.3/manual.html#pdf-table.sort">sort</a></td></tr></table>
 <p>Every snake can describe itself with the following attributes of the global variable <code>self</code>:</p>
 <table>
 <thead>
 <tr><td>Variable within self</td><td>Type</td><td>Description</td></tr>
 </thead>
 <tbody>
 <tr><td>id</td><td>number</td><td>internal identifier</td></tr>
 <tr><td>segment_radius</td><td>number</td><td>the size of the snake</td></tr>
 <tr><td>mass</td><td>number</td><td>the weight of the snake</td></tr>
 <tr><td>sight_radius</td><td>number</td><td>the field of view of the snake, which increases with the size of the snake</td></tr>
 <tr><td>consume_radius</td><td>number</td><td>the field of feed of the snake, which increases with the size of the snake</td></tr>
 <tr><td>max_step_angle</td><td>number</td><td>limits the return value of the step function (-max_step_angle to +max_step_angle)</td></tr>
 <tr><td>start_frame</td><td>number</td><td>the game frame, when the snake was born</td></tr>
 <tr><td>current_frame</td><td>number</td><td>the current game frame</td></tr>
 <tr><td>speed</td><td>number</td><td>default speed is 1, but it can change if a snake is using boost</td></tr>
 <tr><td>food_consumed_natural</td><td>number</td><td>consumed food which appeared naturally in the world</td></tr>
 <tr><td>food_consumed_hunted_self</td><td>number</td><td>consumed food from snakes that you killed</td></tr>
 <tr><td>food_consumed_hunted_by_others</td><td>number</td><td>consumed food from snakes that were killed by others</td></tr>
 <tr><td>colors</td><td>{number,..}</td><td>table of colors (default color is 0x0000FF00, maximum count is 100, set is available in <a href="#init">init</a>)</td></tr>
 <tr><td>face</td><td>number</td><td>not implemented yet (returns 0, set is available in <a href="#init">init</a>)</td></tr>
 <tr><td>logo</td><td>number</td><td>not implemented yet (returns 0, set is available in <a href="#init">init</a>)</td></tr></tr>
 </tbody></table></p>


<h2><a href="#Functions">Functions</a></h2>
<table class="function_list">
  <thead>
    <tr>
      <td>
        Function name
      </td>
      <td>
        Description
      </td>
    </tr>
  </thead>
  <tbody>
	<tr>
	<td class="name" nowrap><a href="#init">init ()</a></td>
	<td class="summary">This function is called upon creation of a bot
 Initialialize your environment here.</td>
	</tr>
	<tr>
	<td class="name" nowrap><a href="#findFood">findFood (max_distance, minimum_food_value)</a></td>
	<td class="summary">That function returns all food as list.</td>
	</tr>
	<tr>
	<td class="name" nowrap><a href="#findSegments">findSegments (max_distance, include_own)</a></td>
	<td class="summary">That function returns all segments of all snakes within a certain radius as list.</td>
	</tr>
	<tr>
	<td class="name" nowrap><a href="#log">log (message)</a></td>
	<td class="summary">That function sends messages to the development console.</td>
	</tr>
	<tr>
	<td class="name" nowrap><a href="#step">step ()</a></td>
	<td class="summary">That function (defined by you) is called each frame.</td>
	</tr>
  </tbody>
</table>


    <h2><a name="Functions"></a>Functions</h2>

    <dl class="function">
    <dt>
    <a name = "init"></a>
    <strong>init ()</strong>
    </dt>
    <dd>
    This function is called upon creation of a bot
 Initialialize your environment here.
159
 You can also modify the "self" variables here and, e.g. set your snake's colors.
nachtgold's avatar
nachtgold committed
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179






    <h4>Usage:</h4>
    <ul>
        <pre class="usage"><code class="lua"> <span class="keyword">function</span> init()
     self.colors = { <span class="number">0xFF0000</span>, <span class="number">0xFFBF00</span>, <span class="number">0x80FF00</span>, <span class="number">0x00FF40</span>, <span class="number">0x00FFFF</span>, <span class="number">0x0015FF</span>, <span class="number">0x8000FF</span> }
 <span class="keyword">end</span></code></pre>
    </ul>

</dd>
    <dt>
    <a name = "findFood"></a>
    <strong>findFood (max_distance, minimum_food_value)</strong>
    </dt>
    <dd>
    That function returns all food as list.
nachtgold's avatar
nachtgold committed
180 181 182
 The list is ordered by food value, from largest to lowest. The values of new spawning
 food are calculated based on a mean value of 3.5 and a standard deviation of 2.
 A killed snake drops a part of the consumed food, that is distributed by the same rule.
nachtgold's avatar
nachtgold committed
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307


    <h4>Parameters:</h4>
    <ul>
        <li><code>max_distance</code>
         all food within the distance is included
        </li>
        <li><code>minimum_food_value</code>
         all lower food values are filtered (min: 0, max: unknown)
        </li>
    </ul>




    <h4>Usage:</h4>
    <ul>
        <pre class="usage"><code class="lua"> <span class="keyword">local</span> food = findFood(max_distance, <span class="number">0.8</span>)
 <span class="keyword">for</span> i, item <span class="keyword">in</span> food:<span class="global">pairs</span>() <span class="keyword">do</span>
   item.d    <span class="comment">-- angle in radian (-π to +π)
</span>   item.dist <span class="comment">-- distance
</span>   item.v    <span class="comment">-- food value
</span> <span class="keyword">end</span></code></pre>
    </ul>

</dd>
    <dt>
    <a name = "findSegments"></a>
    <strong>findSegments (max_distance, include_own)</strong>
    </dt>
    <dd>
    That function returns all segments of all snakes within a certain radius as list.
 The list is ordered by distance, from smallest to largest. All attributes of each
 item in the segments list are measured between the center of your head the the center
 of the item center.


    <h4>Parameters:</h4>
    <ul>
        <li><code>max_distance</code>
         all segements within that distance are included
        </li>
        <li><code>include_own</code>
         If true, your snake segments are included. Otherwise you get only enemy segments
        </li>
    </ul>




    <h4>Usage:</h4>
    <ul>
        <pre class="usage"><code class="lua"> <span class="keyword">local</span> segments = findSegments(<span class="number">50.0</span>, <span class="keyword">false</span>)
 <span class="keyword">for</span> i, item <span class="keyword">in</span> segments:<span class="global">pairs</span>() <span class="keyword">do</span>
   item.d        <span class="comment">-- angle in radian (-π to +π)
</span>   item.r        <span class="comment">-- radius of the item
</span>   item.dist     <span class="comment">-- distance
</span>   item.bot      <span class="comment">-- id of the other snake (new one after death)
</span>   item.bot_id   <span class="comment">-- id of the other snake (new one after death)
</span>   item.bot_name <span class="comment">-- name of the other snake (will never change)
</span> <span class="keyword">end</span></code></pre>
    </ul>

</dd>
    <dt>
    <a name = "log"></a>
    <strong>log (message)</strong>
    </dt>
    <dd>
    That function sends messages to the development console.


    <h4>Parameters:</h4>
    <ul>
        <li><code>message</code>
         this string will be transfered to the console
        </li>
    </ul>





</dd>
    <dt>
    <a name = "step"></a>
    <strong>step ()</strong>
    </dt>
    <dd>
    That function (defined by you) is called each frame.
 Here you can implement you bot logic. The return value must be an radiant angle.
 A negative angle means turn left and a positive angle
 means turn right. With 0, the snake keeps its direction.



    <h4>Returns:</h4>
    <ol>
        <li>
        new angle relative to the head direction</li>
        <li>
        if true the snake will use boost, otherwise false or empty (optional)</li>
    </ol>



    <h4>Usage:</h4>
    <ul>
        <li><pre class="usage"><code class="lua"> <span class="keyword">function</span> step()
   <span class="keyword">return</span> <span class="number">0.005</span>
 <span class="keyword">end</span></code></pre></li>
        <li><pre class="usage"><code class="lua"> <span class="keyword">function</span> step()
   <span class="keyword">return</span> <span class="number">0.005</span>, <span class="keyword">true</span>
 <span class="keyword">end</span></code></pre></li>
    </ul>

</dd>
</dl>


</div> <!-- id="content" -->
</div> <!-- id="main" -->
</div> <!-- id="container" -->

{% endblock %}