Helpers: the Drone
A drone that flies with your robot and shoots, guards or repairs.
Pass helpers to the constructor with super({ helpers: [new Drone()] }). The drone flies around your robot, has 15 health, is downed by any shot that crosses its path and falls along with you.
Helpers and items share the 3 slots. The first drone costs 3 points, and each extra one costs 1 more (2 drones = 7 points).
Modes
new Drone() decides what to do by its mode, which you change on the instance (this.helpers[0].mode = "guard"). The change applies right away:
follow(default): stays with you and shoots the nearest enemy (2 damage every 2 s, range 18), aiming ahead of it.attack: chases the enemy intargetand shoots it; while it can’t see it, it does the same asfollow.guard: stays between you and the nearest enemy, with a shield, without shooting.repair: doesn’t shoot; gives back 0.4 health per second, using 3 of your energy per point.
How the Drone works
This is the code new Drone() runs, exactly. To change it, create a class that extends Drone and override only the method you want (followOwner, attackTarget, guardOwner, repairOwner or aim; in Python, Ruby and Lua, follow_owner…). For example, to aim without leading the target:
class MyDrone extends Drone {
aim(state, enemy) { this.fireAt(enemy.x, enemy.z); }
}
To change everything, copy the whole class and rename it.
// Como o Drone funciona: este é o código que new Drone() executa.
// Para mudar, crie class MeuDrone extends Drone e reescreva só o método
// que quiser (followOwner, attackTarget, guardOwner, repairOwner ou aim).
class Drone extends Helper {
get kind() { return "drone"; }
// O loop do drone: a cada turno, o programa do modo que o robô escolheu.
run() {
while (true) {
const state = this.scan();
if (this.mode === "attack") this.attackTarget(state);
else if (this.mode === "guard") this.guardOwner(state);
else if (this.mode === "repair") this.repairOwner(state);
else this.followOwner(state);
}
}
// Orbita o dono e atira no inimigo mais próximo que estiver no alcance.
followOwner(state) {
this.follow(4);
const enemy = state.enemies.find(e => e.distance <= state.me.limits.range);
if (enemy) this.aim(state, enemy);
}
// Fica a 8 do alvo (this.target) e atira nele; sem vê-lo, segue o dono.
attackTarget(state) {
const target = state.enemies.find(e => e.id === this.target);
if (!target) return this.followOwner(state);
const dx = state.me.x - target.x, dz = state.me.z - target.z;
const distance = Math.max(Math.sqrt(dx * dx + dz * dz), 1e-9);
this.goTo(target.x + dx / distance * 8, target.z + dz / distance * 8);
if (target.distance <= state.me.limits.range) this.aim(state, target);
}
// Fica entre o dono e o inimigo mais perto dele, com escudo (não atira).
guardOwner(state) {
let threat = null;
for (const e of state.enemies) {
if (!threat || e.ownerDistance < threat.ownerDistance || (e.ownerDistance === threat.ownerDistance && e.id < threat.id)) threat = e;
}
if (threat) {
// Vários drones protegendo se abrem em leque na linha de tiro.
const spread = (state.me.index - (state.me.count - 1) / 2) * 0.55;
const angle = Math.atan2(threat.x - state.owner.x, threat.z - state.owner.z) + spread;
this.goTo(state.owner.x + Math.sin(angle) * 2.9, state.owner.z + Math.cos(angle) * 2.9);
} else {
this.follow(2.9);
}
this.shield();
}
// Fica perto do dono e devolve vida a ele (gasta a energia do dono).
repairOwner(state) {
this.follow(2.6);
if (state.owner.health < state.owner.maxHealth) this.repair();
}
// Atira onde o inimigo vai estar quando o tiro chegar.
aim(state, enemy) {
let x = enemy.x, z = enemy.z;
for (let i = 0; i < 2; i++) {
const dx = x - state.me.x, dz = z - state.me.z;
const flight = Math.sqrt(dx * dx + dz * dz) / state.me.limits.bulletSpeed;
x = enemy.x + Math.sin(enemy.heading) * enemy.velocity * flight;
z = enemy.z + Math.cos(enemy.heading) * enemy.velocity * flight;
}
this.fireAt(x, z);
}
}// Como o Drone funciona: este é o código que new Drone() executa.
// Para mudar, crie class MeuDrone extends Drone e reescreva só o método
// que quiser (followOwner, attackTarget, guardOwner, repairOwner ou aim).
class Drone extends Helper {
get kind(): string { return "drone"; }
// O loop do drone: a cada turno, o programa do modo que o robô escolheu.
run(): void {
while (true) {
const state: HelperState = this.scan();
if (this.mode === "attack") this.attackTarget(state);
else if (this.mode === "guard") this.guardOwner(state);
else if (this.mode === "repair") this.repairOwner(state);
else this.followOwner(state);
}
}
// Orbita o dono e atira no inimigo mais próximo que estiver no alcance.
followOwner(state: HelperState): void {
this.follow(4);
const enemy = state.enemies.find(e => e.distance <= state.me.limits.range);
if (enemy) this.aim(state, enemy);
}
// Fica a 8 do alvo (this.target) e atira nele; sem vê-lo, segue o dono.
attackTarget(state: HelperState): void {
const target = state.enemies.find(e => e.id === this.target);
if (!target) return this.followOwner(state);
const dx = state.me.x - target.x, dz = state.me.z - target.z;
const distance = Math.max(Math.sqrt(dx * dx + dz * dz), 1e-9);
this.goTo(target.x + dx / distance * 8, target.z + dz / distance * 8);
if (target.distance <= state.me.limits.range) this.aim(state, target);
}
// Fica entre o dono e o inimigo mais perto dele, com escudo (não atira).
guardOwner(state: HelperState): void {
let threat: HelperContact | null = null;
for (const e of state.enemies) {
if (!threat || e.ownerDistance < threat.ownerDistance || (e.ownerDistance === threat.ownerDistance && e.id < threat.id)) threat = e;
}
if (threat) {
// Vários drones protegendo se abrem em leque na linha de tiro.
const spread = (state.me.index - (state.me.count - 1) / 2) * 0.55;
const angle = Math.atan2(threat.x - state.owner.x, threat.z - state.owner.z) + spread;
this.goTo(state.owner.x + Math.sin(angle) * 2.9, state.owner.z + Math.cos(angle) * 2.9);
} else {
this.follow(2.9);
}
this.shield();
}
// Fica perto do dono e devolve vida a ele (gasta a energia do dono).
repairOwner(state: HelperState): void {
this.follow(2.6);
if (state.owner.health < state.owner.maxHealth) this.repair();
}
// Atira onde o inimigo vai estar quando o tiro chegar.
aim(state: HelperState, enemy: HelperContact): void {
let x = enemy.x, z = enemy.z;
for (let i = 0; i < 2; i++) {
const dx = x - state.me.x, dz = z - state.me.z;
const flight = Math.sqrt(dx * dx + dz * dz) / state.me.limits.bulletSpeed;
x = enemy.x + Math.sin(enemy.heading) * enemy.velocity * flight;
z = enemy.z + Math.cos(enemy.heading) * enemy.velocity * flight;
}
this.fireAt(x, z);
}
}# Como o Drone funciona: este é o código que Drone() executa.
# Para mudar, crie class MeuDrone(Drone) e reescreva só o método
# que quiser (follow_owner, attack_target, guard_owner, repair_owner ou aim).
class Drone(Helper):
kind = "drone"
# O loop do drone: a cada turno, o programa do modo que o robô escolheu.
def run(self):
while True:
state = self.scan()
if self.mode == "attack":
self.attack_target(state)
elif self.mode == "guard":
self.guard_owner(state)
elif self.mode == "repair":
self.repair_owner(state)
else:
self.follow_owner(state)
# Orbita o dono e atira no inimigo mais próximo que estiver no alcance.
def follow_owner(self, state):
self.follow(4)
for e in state.enemies:
if e.distance <= state.me.limits.range:
return self.aim(state, e)
# Fica a 8 do alvo (self.target) e atira nele; sem vê-lo, segue o dono.
def attack_target(self, state):
enemy = None
for e in state.enemies:
if e.id == self.target:
enemy = e
break
if enemy is None:
return self.follow_owner(state)
dx = state.me.x - enemy.x
dz = state.me.z - enemy.z
distance = max(math.sqrt(dx * dx + dz * dz), 1e-9)
self.go_to(enemy.x + dx / distance * 8, enemy.z + dz / distance * 8)
if enemy.distance <= state.me.limits.range:
self.aim(state, enemy)
# Fica entre o dono e o inimigo mais perto dele, com escudo (não atira).
def guard_owner(self, state):
threat = None
for e in state.enemies:
if threat is None or e.owner_distance < threat.owner_distance or (e.owner_distance == threat.owner_distance and e.id < threat.id):
threat = e
if threat is not None:
# Vários drones protegendo se abrem em leque na linha de tiro.
spread = (state.me.index - (state.me.count - 1) / 2) * 0.55
angle = math.atan2(threat.x - state.owner.x, threat.z - state.owner.z) + spread
self.go_to(state.owner.x + math.sin(angle) * 2.9, state.owner.z + math.cos(angle) * 2.9)
else:
self.follow(2.9)
self.shield()
# Fica perto do dono e devolve vida a ele (gasta a energia do dono).
def repair_owner(self, state):
self.follow(2.6)
if state.owner.health < state.owner.max_health:
self.repair()
# Atira onde o inimigo vai estar quando o tiro chegar.
def aim(self, state, enemy):
x, z = enemy.x, enemy.z
for _ in range(2):
dx = x - state.me.x
dz = z - state.me.z
flight = math.sqrt(dx * dx + dz * dz) / state.me.limits.bullet_speed
x = enemy.x + math.sin(enemy.heading) * enemy.velocity * flight
z = enemy.z + math.cos(enemy.heading) * enemy.velocity * flight
self.fire_at(x, z)# Como o Drone funciona: este é o código que Drone.new executa.
# Para mudar, crie class MeuDrone < Drone e reescreva só o método
# que quiser (follow_owner, attack_target, guard_owner, repair_owner ou aim).
class Drone < Helper
def kind = "drone"
# O loop do drone: a cada turno, o programa do modo que o robô escolheu.
def run
loop do
state = scan
case mode.to_s
when "attack" then attack_target(state)
when "guard" then guard_owner(state)
when "repair" then repair_owner(state)
else follow_owner(state)
end
end
end
# Orbita o dono e atira no inimigo mais próximo que estiver no alcance.
def follow_owner(state)
follow 4
enemy = state.enemies.find { |e| e.distance <= state.me.limits.range }
aim(state, enemy) if enemy
end
# Fica a 8 do alvo (target) e atira nele; sem vê-lo, segue o dono.
def attack_target(state)
enemy = state.enemies.find { |e| e.id == target.to_s }
return follow_owner(state) if enemy.nil?
dx = state.me.x - enemy.x
dz = state.me.z - enemy.z
distance = [Math.sqrt(dx * dx + dz * dz), 1e-9].max
go_to enemy.x + dx / distance * 8, enemy.z + dz / distance * 8
aim(state, enemy) if enemy.distance <= state.me.limits.range
end
# Fica entre o dono e o inimigo mais perto dele, com escudo (não atira).
def guard_owner(state)
threat = nil
state.enemies.each do |e|
if threat.nil? || e.owner_distance < threat.owner_distance || (e.owner_distance == threat.owner_distance && e.id < threat.id)
threat = e
end
end
if threat
# Vários drones protegendo se abrem em leque na linha de tiro.
spread = (state.me.index - (state.me.count - 1) / 2.0) * 0.55
angle = Math.atan2(threat.x - state.owner.x, threat.z - state.owner.z) + spread
go_to state.owner.x + Math.sin(angle) * 2.9, state.owner.z + Math.cos(angle) * 2.9
else
follow 2.9
end
shield
end
# Fica perto do dono e devolve vida a ele (gasta a energia do dono).
def repair_owner(state)
follow 2.6
repair if state.owner.health < state.owner.max_health
end
# Atira onde o inimigo vai estar quando o tiro chegar.
def aim(state, enemy)
x = enemy.x
z = enemy.z
2.times do
dx = x - state.me.x
dz = z - state.me.z
flight = Math.sqrt(dx * dx + dz * dz) / state.me.limits.bullet_speed
x = enemy.x + Math.sin(enemy.heading) * enemy.velocity * flight
z = enemy.z + Math.cos(enemy.heading) * enemy.velocity * flight
end
fire_at x, z
end
end-- Como o Drone funciona: este é o código que Drone:new() executa.
-- Para mudar, crie MeuDrone = Drone:extend() e reescreva só o método
-- que quiser (follow_owner, attack_target, guard_owner, repair_owner ou aim).
Drone = Helper:extend()
Drone.kind = "drone"
-- O loop do drone: a cada turno, o programa do modo que o robô escolheu.
function Drone:run()
while true do
local state = self:scan()
if self.mode == "attack" then self:attack_target(state)
elseif self.mode == "guard" then self:guard_owner(state)
elseif self.mode == "repair" then self:repair_owner(state)
else self:follow_owner(state) end
end
end
-- Orbita o dono e atira no inimigo mais próximo que estiver no alcance.
function Drone:follow_owner(state)
self:follow(4)
for _, e in ipairs(state.enemies) do
if e.distance <= state.me.limits.range then return self:aim(state, e) end
end
end
-- Fica a 8 do alvo (self.target) e atira nele; sem vê-lo, segue o dono.
function Drone:attack_target(state)
local enemy = nil
for _, e in ipairs(state.enemies) do
if e.id == self.target then enemy = e break end
end
if enemy == nil then return self:follow_owner(state) end
local dx, dz = state.me.x - enemy.x, state.me.z - enemy.z
local distance = math.max(math.sqrt(dx * dx + dz * dz), 1e-9)
self:go_to(enemy.x + dx / distance * 8, enemy.z + dz / distance * 8)
if enemy.distance <= state.me.limits.range then self:aim(state, enemy) end
end
-- Fica entre o dono e o inimigo mais perto dele, com escudo (não atira).
function Drone:guard_owner(state)
local threat = nil
for _, e in ipairs(state.enemies) do
if threat == nil or e.owner_distance < threat.owner_distance or (e.owner_distance == threat.owner_distance and e.id < threat.id) then
threat = e
end
end
if threat then
-- Vários drones protegendo se abrem em leque na linha de tiro.
local spread = (state.me.index - (state.me.count - 1) / 2) * 0.55
local angle = math.atan(threat.x - state.owner.x, threat.z - state.owner.z) + spread
self:go_to(state.owner.x + math.sin(angle) * 2.9, state.owner.z + math.cos(angle) * 2.9)
else
self:follow(2.9)
end
self:shield()
end
-- Fica perto do dono e devolve vida a ele (gasta a energia do dono).
function Drone:repair_owner(state)
self:follow(2.6)
if state.owner.health < state.owner.max_health then self:repair() end
end
-- Atira onde o inimigo vai estar quando o tiro chegar.
function Drone:aim(state, enemy)
local x, z = enemy.x, enemy.z
for _ = 1, 2 do
local dx, dz = x - state.me.x, z - state.me.z
local flight = math.sqrt(dx * dx + dz * dz) / state.me.limits.bullet_speed
x = enemy.x + math.sin(enemy.heading) * enemy.velocity * flight
z = enemy.z + math.cos(enemy.heading) * enemy.velocity * flight
end
self:fire_at(x, z)
endA drone with your own code
The helper follows the robot’s pattern: a run() called once, with its own loop, and scan() every turn.
state.meis the drone (position, health andlimits:speed,range,bulletSpeed…),state.owneris your robot andstate.enemieshas the enemies it can see.- Commands:
goTo(x, z),follow(),fireAt(x, z),shield()andrepair()(Python, Ruby and Lua:go_to,fire_at…). - A turn with no command leaves the drone still.
super.run()(Pythonsuper().run(), Rubysuper, LuaDrone.run(self)) uses the ready-made program. - Each drone runs apart from the robot: the two talk through
modeandtarget. The game applies the drone’s limits (speed, range, rate of fire) to any code, and each drone has its own processing limit per turn.
In battle
this.helpers keeps the instances in the declared order; a destroyed drone stays in the list with alive false. Enemy drones the radar sees are in state.helpers (with owner, mode, distance and bearing); they don’t go into state.scanned. Events: hitHelper (your shot hit a drone) and helperDestroyed (one of your drones went down).