Ajudantes: o Drone
Um drone que voa com o seu robô, atira, protege ou conserta.
Passe ajudantes ao construtor com super({ helpers: [new Drone()] }). O drone voa ao redor do seu robô, tem 15 de vida, é derrubado por qualquer tiro que cruze o caminho dele e cai junto com você.
Ajudantes e itens dividem os 3 espaços. O primeiro drone custa 3 pontos, e cada um a mais custa 1 a mais (2 drones = 7 pontos).
Modos
O new Drone() escolhe o que fazer pelo mode, que você troca na instância (this.helpers[0].mode = "guard"). A troca vale na hora:
follow(padrão): acompanha você e atira no inimigo mais próximo (2 de dano a cada 2 s, alcance 18), mirando na frente dele.attack: persegue o inimigo emtargete atira nele; enquanto não o vê, faz o mesmo quefollow.guard: fica entre você e o inimigo mais próximo, com escudo, sem atirar.repair: não atira; devolve 0,4 de vida por segundo, gastando 3 de energia sua por ponto.
Como o Drone funciona
Este é o código que o new Drone() executa, exatamente. Para mudar, crie uma classe que estende Drone e reescreva só o método que quiser (followOwner, attackTarget, guardOwner, repairOwner ou aim; em Python, Ruby e Lua, follow_owner…). Por exemplo, para mirar sem antecipar o movimento:
class MeuDrone extends Drone {
aim(state, enemy) { this.fireAt(enemy.x, enemy.z); }
}
Para mudar tudo, copie a classe inteira e troque o nome.
// 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)
endUm drone com o seu próprio código
O ajudante segue o mesmo padrão do robô: um run() chamado uma vez, com o loop dele, e scan() a cada turno.
state.meé o drone (posição, vida elimits:speed,range,bulletSpeed…),state.owneré o seu robô estate.enemiestraz os inimigos que ele enxerga.- Comandos:
goTo(x, z),follow(),fireAt(x, z),shield()erepair()(Python, Ruby e Lua:go_to,fire_at…). - Um turno sem nenhum comando deixa o drone parado.
super.run()(Pythonsuper().run(), Rubysuper, LuaDrone.run(self)) usa o programa pronto. - Cada drone roda separado do robô: os dois conversam pelo
modee pelotarget. O jogo aplica os limites do drone (velocidade, alcance, cadência) a qualquer código, e cada drone tem o próprio limite de processamento por turno.
Na batalha
this.helpers guarda as instâncias na ordem declarada; um drone destruído fica na lista com alive falso. Os drones inimigos que o radar vê ficam em state.helpers (com owner, mode, distance e bearing); eles não entram em state.scanned. Eventos: hitHelper (seu tiro acertou um drone) e helperDestroyed (um drone seu caiu).