{% extends 'core/base.html' %} {% load static %} {% block earlycss %} {% endblock %} {% block css %} {% endblock %} {% block js%} {% endblock %} {% block content %}

Bot Documentation

This is a brief overview of all usable functions within the bot logic.

Every player can start with a simple demo bot. You get the code in the editor. 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.

Lua global namespace

Every snake is allowed to store 10MB within the global namespace and the data persists until the bot dies. So the bot can save data from frame to frame.

The following Lua standard functions are allowed:

assert print ipairs error next pairs pcall
select tonumber, tostring type unpack _VERSION xpcall

From the module math you can use:

abs acos asin atan ceil cos deg
exp floor fmod frexp huge log max
min modf pi pow rad random randomseed
sin sqrt tan

From the module os you can use:

clock difftime time

From the module table you can use:

insert remove sort

Every snake can describe itself with the following attributes of the global variable self:

Variable within selfTypeDescription
idnumberinternal identifier
segment_radiusnumberthe size of the snake
massnumberthe weight of the snake
sight_radiusnumberthe field of view of the snake, which increases with the size of the snake
consume_radiusnumberthe field of feed of the snake, which increases with the size of the snake
max_step_anglenumberlimits the return value of the step function (-max_step_angle to +max_step_angle)
start_framenumberthe game frame, when the snake was born
current_framenumberthe current game frame
speednumberdefault speed is 1, but it can change if a snake is using boost
food_consumed_naturalnumberconsumed food which appeared naturally in the world
food_consumed_hunted_selfnumberconsumed food from snakes that you killed
food_consumed_hunted_by_othersnumberconsumed food from snakes that were killed by others
colors{number,..}table of colors (default color is 0x0000FF00, maximum count is 100, set is available in init)
facenumbernot implemented yet (returns 0, set is available in init)
logonumbernot implemented yet (returns 0, set is available in init)

Functions

Function name Description
init () This function is called upon creation of a bot Initialialize your environment here.
findFood (max_distance, minimum_food_value) That function returns all food as list.
findSegments (max_distance, include_own) That function returns all segments of all snakes within a certain radius as list.
log (message) That function sends messages to the development console.
step () That function (defined by you) is called each frame.

Functions

init ()
This function is called upon creation of a bot Initialialize your environment here. You can also modify the "self" variables here and, e.g. set your snake's colors.

Usage:

     function init()
         self.colors = { 0xFF0000, 0xFFBF00, 0x80FF00, 0x00FF40, 0x00FFFF, 0x0015FF, 0x8000FF }
     end
findFood (max_distance, minimum_food_value)
That function returns all food as list. 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.

Parameters:

  • max_distance all food within the distance is included
  • minimum_food_value all lower food values are filtered (min: 0, max: unknown)

Usage:

     local food = findFood(max_distance, 0.8)
     for i, item in food:pairs() do
       item.d    -- angle in radian (-π to +π)
       item.dist -- distance
       item.v    -- food value
     end
findSegments (max_distance, include_own)
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.

Parameters:

  • max_distance all segements within that distance are included
  • include_own If true, your snake segments are included. Otherwise you get only enemy segments

Usage:

     local segments = findSegments(50.0, false)
     for i, item in segments:pairs() do
       item.d        -- angle in radian (-π to +π)
       item.r        -- radius of the item
       item.dist     -- distance
       item.bot      -- id of the other snake (new one after death)
       item.bot_id   -- id of the other snake (new one after death)
       item.bot_name -- name of the other snake (will never change)
     end
log (message)
That function sends messages to the development console.

Parameters:

  • message this string will be transfered to the console
step ()
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.

Returns:

  1. new angle relative to the head direction
  2. if true the snake will use boost, otherwise false or empty (optional)

Usage:

  •  function step()
       return 0.005
     end
  •  function step()
       return 0.005, true
     end
{% endblock %}