For reference information on how these robots work, see the Programming Robots section.
-- -- 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 -- -- !!! WILL ONLY WORK WITH RELEASE VERSION OF BITFIGHTER 011 AND ABOVE !!! --
-- This is called by the robot's idle routine each tick. -- This function must be present for the robot to work!
if(targetItem == nil) then targetItem = findClosest(bot:findGlobalItems(TestItemType)) end
if(targetItem == nil) then return end
local botLoc = bot:getLoc() local itemLoc = targetItem:getLoc()
local 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()
if( dist <= orbitRadius * 1.1 ) then -- Close enough to enter orbit dest = itemLoc dest:setxy( itemLoc:x() + orbitRadius * math.cos (orbitAng), itemLoc:y() + orbitRadius * math.sin (orbitAng) ) if( not isInOrbit ) then orbitAng = botLoc:angleTo( dest ) - math.pi / 2 end isInOrbit = true else -- Travel directly toward object dest = itemLoc orbitAng = botLoc:angleTo( dest ) isInOrbit = false end
bot:setThrustToPt( dest ) -- Travel towards calculated point bot:setAngle( botLoc:angleTo( itemLoc ) ) -- Aim ship that way too
-- This function is called once and should return the robot's name
return( "OrbitBot" )
-- 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 must be declared here before they can be used elsewhere orbitAng = 0 orbitRadius = 300 isInOrbit = false targetItem = nil </source>
You can use the above bot with the following level file. Copy the robot code into a file called "orbitbot.bot" in your robots folder, then copy the following code into a file called "orbitbotdemo.level" in your levels file. The Robot line in the level file tells Bitfighter to load a robot called orbitbot.bot from the robots folder, and assign it to Team 1.
When you play the level, you should see a functional orbitbot!
<source lang="levelcode"> GameType 10 10 LevelName OrbitBot Home LevelDescription Robot demo level LevelCredits Robot 1 orbitbot.bot GridSize 255 MinPlayers 0 MaxPlayers 0 Team Blue 0 0 1 Team Red 1 0 0 BarrierMaker 50 1 -2 1 6 9 6 9 -2 1 -2 Spawn 0 3 2 Spawn 1 7 2 TestItem 5 2 </source>