Guest User

QuickBot

a guest
Apr 23rd, 2010
379
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.86 KB | None | 0 0
  1. game = nil
  2. difficulty = nil
  3. speed = nil
  4. directionThreshold = nil
  5. averageIndex = nil
  6. averageMax = nil
  7. myOrbitalDirection = nil
  8. agression = nil
  9. defense = nil
  10. myNum = nil
  11. gameType = nil
  12. goalPt = vec.new(0,0)
  13. dirToGo = nil
  14.  
  15. pathTimer = nil
  16. pathTimerMax = nil
  17.  
  18. o = vec.new(0,0)
  19.  
  20. botLoc = nil        -- Bot's location, will be updated when onTick() is run
  21.  
  22. botRadius = nil
  23.  
  24. function main()
  25.     botRadius = bot:getRad()
  26.     pathTimerMax = 250
  27.     pathTimer = pathTimerMax
  28.     dirToGo = 0
  29.     game = GameInfo()
  30.     difficulty = tonumber(arg[1]) or .5
  31.     agression = tonumber(arg[2]) or .5
  32.     defense = tonumber(arg[3]) or .5
  33.     speed = tonumber(arg[4]) or .75
  34.     directionThreshold = tonumber(arg[5]) or .25
  35.  
  36.     gameType = game:getGameType()
  37.    
  38.     averageIndex = 1
  39.     averageMax = 20
  40.     averageArray = {}
  41.     for i = 1, averageMax do
  42.         averageArray[i] = false
  43.     end
  44.     myOrbitalDirection = 1
  45.     myNum = math.random(0,10)
  46. end
  47.  
  48. function vectorX(dir, dist)
  49.     return(dist * math.cos(dir))
  50. end
  51. function vectorY(dir, dist)
  52.     return(-dist * math.sin(dir))  
  53. end
  54.  
  55. function shieldSelf()
  56.     local bullets = bot:findItems(BulletType,AsteroidType,MineType)
  57.     local distToShieldAt = bot:getRad()*2 + (1-difficulty)*100
  58.     if (bullets ~= nil) then
  59.         for i,bullet in ipairs(bullets) do
  60.             local bulletLoc = bullet:getLoc()
  61.             local bulletVel = bullet:getVel()
  62.             local angleDiff = math.abs(angleDifference(vec.angleTo(o, bulletVel), vec.angleTo(bulletLoc, botLoc)))
  63.             --logprint(angleDiff)
  64.             if (vec.distanceTo(bulletLoc, botLoc) < distToShieldAt + bullet:getRad() and angleDiff < math.pi/4) then
  65.                 bot:activateModule(ModuleShield)
  66.                 return(true)
  67.             end
  68.         end
  69.     end
  70. end
  71.  
  72. function angleDifference(angleA,angleB)
  73.     return (math.mod(math.mod(angleA - angleB, math.pi*2) + math.pi*3, math.pi*2) - math.pi)
  74. end
  75.  
  76. function fireAtObjects()
  77.     local enemies = bot:findItems(RobotType, TurretType, ShipType, AsteroidType, ForceFieldProjectorType, SpyBugType)
  78.     for index,enemy in ipairs(enemies) do
  79.         if(fireAtObject(enemy, WeaponPhaser)) then
  80.             break
  81.         end
  82.     end
  83. end
  84.  
  85.  
  86. --Fires at the specified object with the specified weapon if the obj is a good target.
  87. --Does not fire if object is on the same team or if there is something in the way.
  88. --Returns whether it fired or not.
  89. function fireAtObject(obj, weapon)
  90.     if(obj:getClassID() == TurretType or obj:getClassID() == ForceFieldProjectorType) then
  91.         if(obj:getHealth() < .1) then
  92.             --logprint("It's dead.")
  93.             return(false)
  94.         end
  95.     end
  96.    
  97.     if(obj:getClassID() == ShipType or
  98.         obj:getClassID() == RobotType or
  99.         obj:getClassID() == TurretType or
  100.         obj:getClassID() == ForceFieldProjectorType) then
  101.             if obj:getTeamIndx() == bot:getTeamIndx() and game:isTeamGame() then
  102.                 --logprint("It's an ally.")
  103.                 return(false)
  104.             end
  105.     end
  106.    
  107.     local angle = getFiringSolution(obj)
  108.     if angle ~= nil and bot:hasWeapon(weapon) then
  109.         bot:setAngle(angle + math.rad((math.random()-1)*20*(1-difficulty)))
  110.         bot:setWeapon(weapon)
  111.         bot:fire()
  112.         --logprint("bot:fire() called!");
  113.         return(true)
  114.     end
  115.     --logprint("Firing solution not found.");
  116.     return(false)
  117. end
  118.  
  119.  
  120. function getName()
  121.      return("QuickBot")
  122. end
  123.  
  124. function findEnemy()
  125.     local enemies = bot:findItems(ShipType, RobotType)
  126.     local target = nil
  127.     local smallestDist = 999999
  128.    
  129.     for index,enemy in ipairs(enemies) do
  130.         if (enemy:getTeamIndx() ~= bot:getTeamIndx() and game:isTeamGame()) then        -- Put cheaper test first
  131.             local d = vec.distSquared(botLoc, enemy:getLoc())  
  132.             if( d < smallestDist ) then
  133.                 target = enemy
  134.                 smallestDist = d
  135.             end
  136.         end  
  137.     end
  138.    
  139.     if(target ~= nil and bot:hasLosPt(target:getLoc())) then
  140.         return(target)
  141.     elseif(enemies[0] ~= nil) then
  142.         return(enemies[0])
  143.     end
  144.    
  145.     return(nil)
  146. end
  147.  
  148. function shield()
  149.     averageArray[averageIndex] = shieldSelf()
  150.     averageIndex = math.mod(averageIndex,averageMax) + 1
  151.     local shieldPercent = 0
  152.     for i = 1, averageMax do
  153.         if(averageArray[averageIndex]) then
  154.             shieldPercent = shieldPercent + 1
  155.         end
  156.     end
  157.     shieldPercent = shieldPercent / averageMax
  158.     if(shieldPercent > directionThreshold) then
  159.         myOrbitalDirection = -myOrbitalDirection
  160.         for i = 1, averageMax do
  161.             averageArray[i] = false
  162.         end
  163.     end
  164. end
  165.  
  166. function orbitPoint(pt, dir, inputDist, inputStrictness)
  167.     local distAway = bot:getRad()*7
  168.     local strictness = 2
  169.     local direction = 1
  170.     if(dir ~= nil) then direction = dir end
  171.     if(inputDist ~= nil) then distAway = inputDist end
  172.     if(inputStrictness ~= nil) then strictness = inputStrictness end
  173.     if(pt ~= nil) then
  174.         local dist = vec.distanceTo(pt, botLoc)
  175.         local deltaDistance = (dist - distAway) * strictness / distAway
  176.         local sign = 1
  177.         if(deltaDistance > 0) then
  178.             sign = 1
  179.         elseif(deltaDistance < 0) then
  180.             sign = -1
  181.         end
  182.         local changeInAngle = (math.abs(deltaDistance)/(deltaDistance + sign))*math.pi/2
  183.         local angleToPoint = vec.angleTo(pt, bot:getLoc())
  184.         dirToGo = angleToPoint + (math.pi/2 + changeInAngle)*direction
  185.         --bot:setThrust(speed,dirToGo)
  186.     end
  187. end
  188.  
  189. function gotoPosition(pt)
  190.     if(pathTimer<.01)then
  191.         goalPt=bot:getWaypoint(pt)
  192.         dirToGo = vec.angleTo(botLoc,goalPt)
  193.     end
  194. end
  195.  
  196. function gotoAndOrbitPosition(pt)
  197.     if not bot:hasLosPt(pt) then
  198.         gotoPosition(pt)
  199.     else
  200.         orbitPoint(pt, myOrbitalDirection, botRadius * 5, 2)
  201.     end
  202. end
  203.  
  204. --returns if it found an enemy to fight
  205. function attackNearbyEnemies(agressionLevel)
  206.     local target = findEnemy()
  207.     if target ~= nil then
  208.         local targetLoc = target:getLoc()
  209.      
  210.         local dist = vec.distanceTo(botLoc, targetLoc)
  211.         local myPow = (bot:getEnergy() + bot:getHealth() )
  212.         local enemies = bot:findItems(ShipType, RobotType)
  213.      
  214.         --for index, otherShip in ipairs(enemies) do
  215.         -- otherPow = otherPow + target:getEnergy() + target:getHealth()
  216.         --end
  217.         -- Means this, right?
  218.         local otherPow = target:getEnergy() + target:getHealth() * #enemies
  219.      
  220.         --advantage is between -1 and 1, -1 meaning an extreme disadvantage and 1 meaning an extreme advantage
  221.         local advantage = (myPow-otherPow) / math.max(myPow,otherPow)
  222.         if(advantage/2 + .5>agressionLevel) then
  223.             orbitPoint(targetLoc, myOrbitalDirection, botRadius * 9, 2)
  224.             return(true)
  225.         else
  226.         end
  227.     end
  228.     return(false)
  229. end
  230.  
  231. --Returns the objective for the bot based on myNum.  This makes bots choose different defending locations.
  232. function getObjective(objType, team, onTeam)
  233.     local items = bot:findGlobalItems(objType)
  234.     local itemsOnMyTeam = {}
  235.     local currentIndex = 1
  236.     for ind, it in ipairs(items) do
  237.         local itTeamIndex = it:getTeamIndx()
  238.         if(objType == ShipType or objType == RobotType or objType == TurretType or objType == ForceFieldProjectorType or objType == FlagType or objType == GoalZoneType or objType == NexusType or objType == SpyBugType) then
  239.             if (itTeamIndex == team and onTeam) or (itTeamIndex ~= team and not onTeam) then
  240.                 itemsOnMyTeam[currentIndex] = it
  241.                 currentIndex = currentIndex + 1
  242.             end
  243.         else
  244.             itemsOnMyTeam[currentIndex] = it
  245.             currentIndex = currentIndex + 1
  246.         end
  247.     end
  248.     local listMax = 0
  249.     --find max
  250.     if itemsOnMyTeam[1] ~= nil then
  251.         for index,item in ipairs(itemsOnMyTeam) do
  252.             if(item ~= nil) then
  253.                 listMax = listMax + 1
  254.             end
  255.         end
  256.         local targetNum = math.mod(myNum,listMax) + 1
  257.         return(itemsOnMyTeam[targetNum])
  258.     else
  259.         return(nil)
  260.     end
  261. end
  262.  
  263. function doObjective()
  264.     if(gameType == BitmatchGame or gameType == NexusGame or gameType == RabbitGame) then
  265.         --Nothing to defend!  Wander aimlessly
  266.        
  267.     elseif(gameType == HTFGame) then
  268.         --defend the goals
  269.     elseif(gameType == CTFGame) then
  270.         --defend the flag
  271.         local myFlag = getObjective(FlagType,bot:getTeamIndx(),true)
  272.         local otherFlag = getObjective(FlagType,bot:getTeamIndx(),false)
  273.         if(defense<.5) then
  274.             if bot:hasFlag() then
  275.                 if not myFlag:isOnShip() then
  276.                     gotoPosition(myFlag:getLoc())
  277.                 else
  278.                     gotoAndOrbitPosition(myFlag:getLoc())
  279.                 end
  280.                 --gotoPosition(myFlag:getLoc())
  281.             elseif(otherFlag ~= nil) then
  282.                 if not otherFlag:isOnShip() then
  283.                     gotoPosition(otherFlag:getLoc())
  284.                 else
  285.                     gotoAndOrbitPosition(otherFlag:getLoc())
  286.                 end
  287.             end
  288.         else
  289.             if bot:hasFlag() then
  290.                 gotoPosition(myFlag:getLoc())
  291.             elseif myFlag:isInInitLoc() then
  292.                 gotoAndOrbitPosition(myFlag:getLoc())
  293.             else
  294.                 if myFlag:isOnShip() then
  295.                     gotoAndOrbitPosition(myFlag:getLoc())
  296.                 else
  297.                     gotoPosition(myFlag:getLoc())
  298.                 end
  299.             end
  300.         end
  301.     elseif(gameType == SoccerGame) then
  302.         --defend the goal
  303.     elseif(gameType == RetrieveGame) then
  304.         --defend the goals
  305.     elseif(gameType == ZoneControlGame) then
  306.         --defend the zones owned by bot's team
  307.     end
  308. end
  309.  
  310. function goInDirection()
  311.     bot:setThrust(speed,dirToGo)
  312. end
  313.  
  314. function onTick()
  315.     botLoc = bot:getLoc()
  316.     pathTimer=pathTimer-bot:getTime()
  317.     assert(bot:getLoc() ~= nil)
  318.    
  319.     if not attackNearbyEnemies(1-agression) then
  320.         doObjective()
  321.     end
  322.     goInDirection()
  323.     fireAtObjects()
  324.     shield()
  325.     if(pathTimer<0)then
  326.         pathTimer=pathTimerMax+math.random(0,pathTimerMax)
  327.     end
  328. end
Advertisement
Add Comment
Please, Sign In to add comment