Difference between revisions of "Robot gallery"

From Bitfighter
(Added more instructions)
 
(9 intermediate revisions by 4 users not shown)
Line 1: Line 1:
 
For reference information on how these robots work, see the [[Programming_robots|Programming Robots]] section.
 
For reference information on how these robots work, see the [[Programming_robots|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:
 
 
<source lang="lua">
 
 
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
 
</source>
 
 
 
==OrbitBot==
 
  
 
<source lang="lua">
 
<source lang="lua">
Line 35: Line 7:
 
--
 
--
 
-- OrbitBot, a simple robot that finds the nearest TestItem and orbits it
 
-- 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  
+
-- Note that this relies on line-of-sight navigation, so only good for open levels
-- levels
+
 
--
 
--
-- Works with Bitfighter 011 alpha 2
+
-- !!! WILL ONLY WORK WITH RELEASE VERSION OF BITFIGHTER 011 AND ABOVE !!!
 
--
 
--
 
-------------------------------------------------------------------------------
 
-------------------------------------------------------------------------------
Line 45: Line 16:
 
-------------------------------------------------------------------------------
 
-------------------------------------------------------------------------------
 
-------------------------------------------------------------------------------
 
-------------------------------------------------------------------------------
-- This is called by the robot's idle routine each tick.  
+
-- This is called by the robot's idle routine each tick.
 
-- This function must be present for the robot to work!
 
-- This function must be present for the robot to work!
  
 
function getMove()
 
function getMove()
  
     local loc = bot:getLoc()
+
     if(targetItem == nil) then
    local items = bot:findGlobalItems(TestItemType) -- Find a TestItem
+
        targetItem = findClosest(bot:findGlobalItems(TestItemType))
 +
    end
  
     local minDist = 999999
+
     if(targetItem == nil) then return end
    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 botLoc = bot:getLoc()
     local itemLoc = closestItem:getLoc()
+
     local itemLoc = targetItem:getLoc()
  
     dist = botLoc:distanceTo(itemLoc)
+
     local dist = botLoc:distanceTo( itemLoc )
  
 
     -- 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 85: Line 38:
  
 
     -- .0015 determined experimentally
 
     -- .0015 determined experimentally
 +
    orbitAng = orbitAng + .0015 * bot:getTime()
  
    orbitAng = orbitAng + .0015 * bot:getTime()
 
  
 
     local dest
 
     local dest
   
+
 
     if(dist <= orbitRadius * 1.1) then  
+
     if( dist <= orbitRadius * 1.1 ) then   -- Close enough to enter orbit
 
         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
+
        if( not isInOrbit ) then
 +
            orbitAng = botLoc:angleTo( dest ) - math.pi / 2
 +
        end   
 +
       
 +
        isInOrbit = true
 +
                   
 +
     else                                   -- Travel directly toward object
 
         dest = itemLoc
 
         dest = itemLoc
         orbitAng = botLoc:angleTo(dest)
+
         orbitAng = botLoc:angleTo( dest )
 +
        isInOrbit = false
 
     end
 
     end
  
     bot:setThrustToPt(dest)     -- travel towards calculated point
+
     bot:setThrustToPt( dest )                 -- Travel towards calculated point
 
+
     bot:setAngle( botLoc:angleTo( itemLoc ) ) -- Aim ship that way too
     bot:setAngle(botLoc:angleTo(itemLoc))
+
 
end
 
end
  
Line 113: Line 72:
  
 
function getName()
 
function getName()
     return("OrbitBot")
+
     return( "OrbitBot" )
 
end
 
end
  
Line 122: Line 81:
 
-------------------------------------------------------------------------------
 
-------------------------------------------------------------------------------
 
-- Setup code here: this is run once when the robot is initialized, and
 
-- 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
+
-- any variables set here will persist throughout the robot's life. These
 
-- variables can be accessed from various functions defined in this file.
 
-- variables can be accessed from various functions defined in this file.
 
-------------------------------------------------------------------------------
 
-------------------------------------------------------------------------------
-- Global variables
+
-- 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.
  
-- Other initialization code
+
When you play the level, you should see a functional orbitbot!
logprint("Hello, I'm " .. getName())  -- Print a message to the game log
+
orbitRadius = 300                      -- How far from object will we orbit?
+
orbitAng = 0
+
  
 +
<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>
 
</source>

Latest revision as of 18:38, 12 February 2010

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!
 
function getMove()
 
    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()
 
 
    local dest
 
    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
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 must be declared here before they can be used elsewhere
orbitAng = 0
orbitRadius = 300
isInOrbit = false
targetItem = nil

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!

Invalid language.

You need to specify a language like this: <source lang="html4strict">...</source>

Supported languages for syntax highlighting:

4cs, 6502acme, 6502kickass, 6502tasm, 68000devpac, abap, actionscript, actionscript3, ada, algol68, apache, applescript, apt_sources, arm, asm, asp, asymptote, autoconf, autohotkey, autoit, avisynth, awk, bascomavr, bash, basic4gl, bf, bibtex, blitzbasic, bnf, boo, c, c_loadrunner, c_mac, caddcl, cadlisp, cfdg, cfm, chaiscript, cil, clojure, cmake, cobol, coffeescript, cpp, cpp-qt, csharp, css, cuesheet, d, dcl, dcpu16, dcs, delphi, diff, div, dos, dot, e, ecmascript, eiffel, email, epc, erlang, euphoria, f1, falcon, fo, fortran, freebasic, freeswitch, fsharp, gambas, gdb, genero, genie, gettext, glsl, gml, gnuplot, go, groovy, gwbasic, haskell, haxe, hicest, hq9plus, html4strict, html5, icon, idl, ini, inno, intercal, io, j, java, java5, javascript, jquery, kixtart, klonec, klonecpp, latex, lb, ldif, lisp, llvm, locobasic, logtalk, lolcode, lotusformulas, lotusscript, lscript, lsl2, lua, m68k, magiksf, make, mapbasic, matlab, mirc, mmix, modula2, modula3, mpasm, mxml, mysql, nagios, netrexx, newlisp, nsis, oberon2, objc, objeck, ocaml, ocaml-brief, octave, oobas, oorexx, oracle11, oracle8, oxygene, oz, parasail, parigp, pascal, pcre, per, perl, perl6, pf, php, php-brief, pic16, pike, pixelbender, pli, plsql, postgresql, povray, powerbuilder, powershell, proftpd, progress, prolog, properties, providex, purebasic, pycon, pys60, python, q, qbasic, rails, rebol, reg, rexx, robots, rpmspec, rsplus, ruby, sas, scala, scheme, scilab, sdlbasic, smalltalk, smarty, spark, sparql, sql, stonescript, systemverilog, tcl, teraterm, text, thinbasic, tsql, typoscript, unicon, upc, urbi, uscript, vala, vb, vbnet, vedit, verilog, vhdl, vim, visualfoxpro, visualprolog, whitespace, whois, winbatch, xbasic, xml, xorg_conf, xpp, yaml, z80, zxbasic


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