BerryBots forums

It is currently Mon Sep 28, 2020 2:05 pm

All times are UTC

Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: My BerryBot
PostPosted: Wed Oct 23, 2013 11:55 pm 

Joined: Sat Oct 19, 2013 11:06 pm
Posts: 15
Here is version 1 of my bot, it beats BasicBattler and rarely NightShade

The choice of ship to shoot at is taken directly from Shooter which is just a function of closeness and health.

The laser firing angle is chosen so that if the enemy continues with the same heading and speed it's a direct hit.

The thruster fires with full power in a direction perpendicular to the laser firing angle to avoid running over own laser. If there is no enemy it fires in the direction of current heading. There is some random direction added to both cases.

Not sure where to upload code so here it is:
-- Cortez the killer.
thrustAngleRange = 3.0
ship = nil
world = nil
constants = nil

function init(shipArg, worldArg)
  ship = shipArg
  world = worldArg 
  constants = world:constants()

function run(enemyShips)
  local thrusterAngle
  local laserAngle = fireTheLaser(enemyShips)
  if laserAngle then
    local angleDiff = (laserAngle - ship:heading()) % (2 * math.pi)
    if angleDiff > math.pi then
      thrusterAngle = laserAngle + math.pi / 2
      thrusterAngle = laserAngle - math.pi / 2
    thrusterAngle = ship:heading()

function square(x)
  return x * x

function fireTheThruster(thrusterAngle)
  local thrusterAngleRandom = (math.random() - 0.5) * thrustAngleRange + thrusterAngle
  ship:fireThruster(thrusterAngleRandom, 1)

function fireTheLaser(enemyShips)
  local laserAngle
  if (# enemyShips > 0) then
    targetShip = getTargetShip(enemyShips)   
    local xRel = targetShip.x - ship:x()
    local yRel = targetShip.y - ship:y()   
    laserAngle = getAngle(xRel, yRel, targetShip.speed, targetShip.heading)
    if laserAngle then
  return laserAngle

function getTargetShip(enemyShips)
  local bestRating = math.huge
  local targetShip = nil
  for i, enemyShip in pairs(enemyShips) do
    local rating =
        * square(ship:x() - enemyShip.x) + square(ship:y() - enemyShip.y)
    if (rating < bestRating) then
      targetShip = enemyShip
      bestRating = rating
  return targetShip

function getAngle(xRel, yRel, speed, heading)
  local angle
  local a = square(constants.LASER_SPEED) - square(speed)
  local b = speed * (xRel * math.cos(heading) + yRel * math.sin(heading))
  local c = square(xRel) + square(yRel)
  local d = square(b) + a * c
  if d >= 0 then
    local ToF = (b + math.sqrt(d)) / a 
    local xHitRel = xRel + ToF * speed * math.cos(heading)
    local yHitRel = yRel + ToF * speed * math.sin(heading)
    angle = math.atan2(yHitRel, xHitRel)
  return angle

 Post subject: Re: My BerryBot
PostPosted: Thu Oct 24, 2013 3:34 am 
Site Admin
User avatar

Joined: Sat Nov 17, 2012 5:03 am
Posts: 88
Nice! It crushes BasicBattler a lot harder than I expected.

I noticed that your linear targeting doesn't iterate and thought it was flawed at first. A common pitfall (well, in Robocode, but it's the same problem) is to forget that the enemy distance is different now than when your laser will get there. And the solution I normally use and see for that is to iterate: recalculate the laser flight time based on the updated distance, recalculate, repeat until it doesn't change (or is alternating between two).

But I guess you're doing something constructing a triangle whose sides are the same ratio as laser speed to enemy speed? I haven't worked through the trig, but it's certainly much less code than the iteration. Though you might lose a tiny bit of precision if it doesn't exactly model the discrete time. (E.g., if you shoot at the spot your laser will intersect the enemy 13.5 ticks from now, it might actually not intersect at 13 or at 14.)

I also like seeing your bot bouncing around with no fear of the walls. :-) It's almost instinctual to me to try not to hit the wall, but it's no problem in BerryBots.

Dropbox is a pretty nice and easy place to upload bots. A lot of Robocoders use it.

 Post subject: Re: My BerryBot
PostPosted: Thu Oct 24, 2013 5:23 am 

Joined: Sat Oct 19, 2013 11:06 pm
Posts: 15
Yeah that's how it's done, I had to bust out a bit of trig to figure it out. Wouldn't the finite size of the ships allow for that though?

BerryBots is my first my first programming game so I haven't got any such preconceptions haha.

Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC

Who is online

Users browsing this forum: No registered users and 2 guests

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group