run() and scan()
How your robot's code runs: your own loop, one turn per scan().
A robot is a class that extends Robot. The game calls run() only once, when the battle starts: the loop is yours. Inside it, scan() ends the turn (there are 30 per second) and returns the state with everything the robot knows at that moment.
class MyRobot extends Robot {
run() {
while (true) {
const state = this.scan();
if (state.scanned.length > 0) {
const enemy = state.scanned[0];
this.turn(enemy.bearing);
this.fire(1);
} else {
this.turnRadar(1);
}
}
}
}// @language typescript
class MyRobot extends Robot {
run(): void {
while (true) {
const state: State = this.scan();
if (state.scanned.length > 0) {
this.turn(state.scanned[0].bearing);
this.fire(1);
} else {
this.turnRadar(1);
}
}
}
}class MyRobot(Robot):
def run(self):
while True:
state = self.scan()
if state.scanned:
self.turn(state.scanned[0].bearing)
self.fire(1)
else:
self.turn_radar(1)class MyRobot < Robot
def run
loop do
state = scan
if state.scanned.any?
turn state.scanned[0].bearing
fire 1
else
turn_radar 1
end
end
end
endMyRobot = Robot:extend()
function MyRobot:run()
while true do
local state = self:scan()
if #state.scanned > 0 then
self:turn(state.scanned[1].bearing)
self:fire(1)
else
self:turn_radar(1)
end
end
endCommands ask, the turn does
Commands (move, turn, turnRadar, fire…) ask for an action. It happens when the turn ends, at scan(). A command holds until it’s replaced: if you ask for move(10), the robot keeps moving what’s left over the next turns.
Some commands wait until they’re done and only return when the action is over, passing the turns by themselves:
walk(distance): moves and waits to arrive.rotate(angle): turns the body and waits until it’s done.walkTo(x, z): navigates to the point and waits to arrive (or give up).wait(n): letsnturns go by.
Use turn + scan() when you want to react every turn, and rotate when you want to finish turning before you shoot.
Angles
Angles are in radians; positive values turn counterclockwise. The enemies the radar sees come with the angle ready for aiming: this.turn(enemy.bearing) points the gun at them, because the gun is on the arm and shoots where the body points.
Watch out
- A turn that takes more than 1 second until the next
scan()disqualifies the robot (for example, an endless loop with noscan()). - To skip the rest of a turn, use
continue(Ruby:next).returnendsrun(), and the robot stands still for the rest of the battle.