Difference between revisions of "Robot gallery"

From Bitfighter
Line 27: Line 27:
 
</source>
 
</source>
  
 +
'''JUST AS IMPORTANT'''
 +
In 011 alpha 2, there is a bug in setThrustToPt() that can cause Bitfighter to hang. It will be fixed in alpha 3. Inserting the line:
 +
  if(bot:getTime() == 0) then return end
 +
as the first line of your getMove() function will fix the problem.
  
 
==OrbitBot==
 
==OrbitBot==

Revision as of 05:24, 19 June 2009

For reference information on how these robots work, see the Programming Robots section.

IMPORTANT: In order for these bots to work with Bitfighter 011 alpha 2, you will need to update the file "robot_helper_functions.lua" in your Bitfighter install folder. Replace the file's contents with the following:

bot = LuaRobot(Robot) -- This is a reference to our bot.
 
 
function getFiringSolution(item)
    if(item == nil) then
        return nil
    end
 
    type = item:getClassID()
    if(type == nil) then
        return nil
    end
 
    return bot:getFiringSolution(type, item)
end
 
 
function logprint(msg)
    bot:logprint(msg)
end

JUST AS IMPORTANT In 011 alpha 2, there is a bug in setThrustToPt() that can cause Bitfighter to hang. It will be fixed in alpha 3. Inserting the line:

  if(bot:getTime() == 0) then return end

as the first line of your getMove() function will fix the problem.

OrbitBot

-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
--
-- OrbitBot, a simple robot that finds the nearest TestItem and orbits it
-- Note that this relies on line-of-sight navigation, so only good for open 
-- levels
--
-- Works with Bitfighter 011 alpha 2
--
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
-- This is called by the robot's idle routine each tick. 
-- This function must be present for the robot to work!
 
function getMove()
 
    local loc = bot:getLoc()
    local items = bot:findGlobalItems(TestItemType) -- Find a TestItem
 
    local minDist = 999999
    local closestItem = nil
    local found = false
 
    for indx, item in ipairs(items) do -- Iterate over our list
        -- Use distSquared because it is less computationally expensive
        -- and works great for comparing distances
        local d = loc:distSquared(item:getLoc()) -- Dist btwn robot and item
 
        if(d < minDist) then -- Is it the closest yet?
            closestItem = item
            minDist = d
        end
    end
 
    -- closestItem will be nil if the bot can't find any.  This may happen
    -- if the level has no TestItems, or if they are beyond the robot's
    -- range to know about them.
    if(closestItem == nil) then
        return
    end
 
    local botLoc = bot:getLoc()
    local itemLoc = closestItem:getLoc()
 
    dist = botLoc:distanceTo(itemLoc)
 
    -- Here we use the getTime() function to make the motion look smooth.
    -- If we just advanced orbitAng by a fixed amount each frame, it would
    -- appear jerky, as each frame is a slightly different length.
 
    -- .0015 determined experimentally
 
    orbitAng = orbitAng + .0015 * bot:getTime() 
 
    local dest
 
    if(dist <= orbitRadius * 1.1) then 
        dest = itemLoc
        dest:setxy(itemLoc:x() + orbitRadius * math.cos(orbitAng),
        itemLoc:y() + orbitRadius * math.sin(orbitAng))
    else
        dest = itemLoc
        orbitAng = botLoc:angleTo(dest)
    end
 
    bot:setThrustToPt(dest)     -- travel towards calculated point
 
    bot:setAngle(botLoc:angleTo(itemLoc))
end
 
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
-- This function is called once and should return the robot's name
 
function getName()
    return("OrbitBot")
end
 
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
-- Setup code here: this is run once when the robot is initialized, and
-- any variables set here will persist throughout the robot's life. These
-- variables can be accessed from various functions defined in this file.
-------------------------------------------------------------------------------
-- Global variables
 
 
-- Other initialization code
logprint("Hello, I'm " .. getName())   -- Print a message to the game log
orbitRadius = 300                      -- How far from object will we orbit?
orbitAng = 0