{% extends "_default.html" %} {% block head %} Sample Code :: RBN-CODE {% endblock %} {% block body %}

SAMPLE CODE front

SPINBOT

// create a persistent frame counter in memory
// runs once per frame of simulation
if (!('counter' in context.memory)) context.memory.counter = 0;
context.memory.counter++;

// move forward towards center of arena for first 23 frames ...
if (context.memory.counter < 23) {
    context.action.desiredAngle = context.state.angleToCenter;
    context.action.desiredForce = 0.001;
} else {
    // ...then start spinning and shooting!
    context.action.desiredAngle = context.state.angle + 0.05;
    context.action.desiredLaunch = true;
}
        

FASTBOT / SLOWBOT

var closest = context.state.proximity[0] || {};

// when detecting a wall very nearby, turn towards center of arena
if (closest.entityType == "wall") {
    // face robot towards center of arena if too close to wall
    context.action.desiredAngle = context.state.angleToCenter;
}

// always move forward - the divisor (/2) determines max speed/force
context.action.desiredForce = context.state.maxForce/2;

// fire at anything within scanner range
if (context.state.scanner.ALL.length > 0)
    context.action.desiredLaunch = true;
        

DEFAULT

// randomly move forward
if (Math.random() < 0.5) {
    context.action.desiredForce = context.state.maxForce;
}

// detect closest entity via (short range) proximity alerts
var closest = context.state.proximity[0] || {};

// detect robot entity in front of robot via (short-medium range) scanner
var front = context.state.scanner.robot[0] || false;

if (closest.entityType == "wall") {
    // face robot towards center of arena if too close to wall
    context.action.desiredAngle = context.state.angleToCenter;
} else if (front) {
    if (front.active && front.team != context.state.team) {
        // follow active enemy robots
        context.action.desiredAngle = (
            context.state.angle - (context.state.angle - front.angleFromRobot));
        context.action.desiredLaunch = true;
    } else {
        // avoid inactive robots
        dir = (front.angleFromRobot - context.state.angle) < 0 ? -1:1;
        context.action.desiredAngle = context.state.angle - (dir * 0.174533);
        context.action.desiredForce = context.state.maxForce / 2;
    }
} else if (Math.random() < 0.005) {
    // face random direction
    var maxvar = Math.PI / 4, // 45 degrees
        random = (Math.random() * maxvar) - maxvar/2; // left or right
        context.action.desiredAngle = context.state.angle + random;
        context.action.desiredForce = 0; // tight turn
}
        
{% endblock %}