Difference between revisions of "Robot gallery"

From Bitfighter
(OrbitBot)
Line 24: Line 24:
 
function getMove()
 
function getMove()
  
     loc = bot:getLoc()
+
     local loc = bot:getLoc()
     items = bot:findGlobalItems( TestItemType ) -- Find a TestItem
+
     local items = bot:findGlobalItems(TestItemType) -- Find a TestItem
  
     minDist = 999999
+
     local minDist = 999999
     closestItem = nil
+
     local closestItem = nil
     found = false
+
     local found = false
  
     for indx, item in ipairs( items ) do -- Iterate over our list
+
     for indx, item in ipairs(items) do -- Iterate over our list
 
         -- Use distSquared because it is less computationally expensive
 
         -- Use distSquared because it is less computationally expensive
 
         -- and works great for comparing distances
 
         -- and works great for comparing distances
         d = loc:distSquared( item:getLoc() ) -- Dist btwn robot and TestItem
+
         local d = loc:distSquared(item:getLoc()) -- Dist btwn robot and TestItem
  
         if( d < minDist ) then -- Is it the closest yet?
+
         if(d < minDist) then -- Is it the closest yet?
 
             closestItem = item
 
             closestItem = item
 
             minDist = d
 
             minDist = d
Line 45: Line 45:
 
     -- if the level has no TestItems, or if they are beyond the robot's
 
     -- if the level has no TestItems, or if they are beyond the robot's
 
     -- range to know about them.
 
     -- range to know about them.
     if( closestItem == nil) then
+
     if(closestItem == nil) then
 
         return
 
         return
 
     end
 
     end
  
 +
    local botLoc = bot:getLoc()
 +
    local itemLoc = closestItem:getLoc()
  
    botLoc = bot:getLoc()
+
     dist = botLoc:distanceTo(itemLoc)
    itemLoc = closestItem:getLoc()
+
 
+
     dist = botLoc:distanceTo( itemLoc )
+
    orbitRadius = 300
+
  
 
     -- Here we use the getTime() function to make the motion look smooth.
 
     -- Here we use the getTime() function to make the motion look smooth.
Line 64: Line 62:
 
     orbitAng = orbitAng + .0015 * bot:getTime()  
 
     orbitAng = orbitAng + .0015 * bot:getTime()  
  
     if( dist <= orbitRadius * 1.1 ) then  
+
    local dest
 +
   
 +
     if(dist <= orbitRadius * 1.1) then  
 
         dest = itemLoc
 
         dest = itemLoc
         dest:setxy( itemLoc:x() + orbitRadius * math.cos (orbitAng),
+
         dest:setxy(itemLoc:x() + orbitRadius * math.cos(orbitAng),
         itemLoc:y() + orbitRadius * math.sin (orbitAng) )
+
         itemLoc:y() + orbitRadius * math.sin(orbitAng))
 
     else
 
     else
 
         dest = itemLoc
 
         dest = itemLoc
         orbitAng = botLoc:angleTo( dest )
+
         orbitAng = botLoc:angleTo(dest)
 
     end
 
     end
  
     bot:setThrustToPt( dest ) -- travel towards calculated point
+
     bot:setThrustToPt(dest)     -- travel towards calculated point
  
     bot:setAngle( botLoc:angleTo( itemLoc) )
+
     bot:setAngle(botLoc:angleTo(itemLoc))
 
end
 
end
  
Line 87: Line 87:
  
 
function getName()
 
function getName()
     return( "OrbitBot")
+
     return("OrbitBot")
 
end
 
end
  
Line 103: Line 103:
  
 
-- Other initialization code
 
-- Other initialization code
bot:logprint( "Hello, I'm "..getName() ) -- Print a message to the game log
+
bot:logprint("Hello, I'm " .. getName()) -- Print a message to the game log
 +
orbitRadius = 300    -- How far from object do we want to orbit?
 
orbitAng = 0
 
orbitAng = 0
 +
 
</source>
 
</source>

Revision as of 19:28, 11 June 2009

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


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 TestItem
 
        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
bot:logprint("Hello, I'm " .. getName()) -- Print a message to the game log
orbitRadius = 300    -- How far from object do we want to orbit?
orbitAng = 0