Advertisement
KupaTec

Hammer AI v1.0

Dec 4th, 2019
417
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.45 KB | None | 0 0
  1. //HAMMER AI V1.0
  2. //By KupaTec
  3.  
  4. //AI script useful for limited rotation motor weapons such as hammers or flippers
  5. //To use set up your motor as a DOUBLE THROW MOMENTARY SWITCH using button2 as + and button3 as -
  6.  
  7. //Update log
  8. //-V1 Released!
  9. //--Easier to read and tune
  10. //--No more constantly spinning motors!
  11. //--Refined srimech detection
  12.  
  13. //TODO
  14. //-Rewrite srimech and immobile handling
  15. //-Potentially add in ability to define which buttons to use
  16.  
  17. //====================================TUNE THE AI HERE====================================
  18. //Distance from enemy to arm weapon in meters(?)
  19. weaponRange = 2
  20. //Deviation from center to fire weapon once in range
  21. //Min = 0, Max = 1
  22. weaponFOV = .1
  23. //"Recharge" time for weapon in seconds
  24. //For Hammers this should be set to how long it takes to fully extend or retract
  25. //For Flippers this should be set to the minimum time between attempting to flip
  26. weaponRecharge = .75
  27. //=======================================END TUNING=======================================
  28.  
  29. //// USER-CREATED FUNCTIONS
  30.  
  31. // getDistance is a function that computes the distance betwen two points in meters.
  32. getDistance = function(v1,v2); return sqrt((v2.x-v1.x)*(v2.x-v1.x) + (v2.y-v1.y)*(v2.y-v1.y) + (v2.z-v1.z)*(v2.z-v1.z)); end function
  33.  
  34. // getMyRobot is a function that returns a map of our robot. Sometimes our robot is not robots[0].
  35. getMyRobot = function(); for robot in robots; if robot.tag == myTag then; return robot; end if; end for; end function
  36.  
  37. // getNearestEnemy is a function that returns a map of the closest robot to us that is not us.
  38. getNearestEnemy = function(); pos = getMyRobot().position;nearestEnemyDistance = 1000000;nearestEnemy = {};for robot in robots;if robot.tag != myTag then;testDistance = getDistance(pos,robot.position);if testDistance < nearestEnemyDistance then;nearestEnemyDistance = testDistance;nearestEnemy = robot;end if;end if;end for;return nearestEnemy;end function
  39.  
  40. // Normalize
  41. // Normalizes a number to always produce a positive integer
  42. // for example normalize(-0.2) should output 0.02
  43. normalize = function(n); if n < 0 then nn = n*-1 else nn = n; return nn; end if; end function
  44.  
  45. //// MAIN EVENT LOOP
  46.  
  47. while 1
  48.  
  49. // Get a reference to self and nearest enemy as well as distance between.
  50. e = getNearestEnemy()
  51. me = getMyRobot()
  52. distance = getDistance(me.position,e.position)
  53.  
  54. // Drive towards opponent
  55. drive = driveToward(e.position)
  56. // Turn towards opponent
  57. turn = turnToward(e.position)
  58.  
  59. if(distance < weaponRange) then
  60. if(normalize(turn) <= weaponFOV) then
  61. button2 = true
  62. button3 = false
  63. aistate = "weapon fire"
  64. wait(weaponRecharge)
  65. button2 = false
  66. button3 = true
  67. aistate = "weapon reset"
  68. wait(weaponRecharge)
  69. else
  70. button2 = false
  71. button3 = false
  72. aistate = "in range"
  73. end if
  74. else
  75. button2 = false
  76. button3 = false
  77. aistate = "out of range"
  78. end if
  79.  
  80. // If inverted, try to self-right.
  81. if(me.upValue < -0.5) then
  82. button2 = true
  83. button3 = false
  84. aistate = "srimech fire"
  85. wait(weaponRecharge)
  86. button2 = false
  87. button3 = true
  88. aistate = "srimech reset"
  89. wait(weaponRecharge)
  90. end if
  91.  
  92. // If immobile, try to get unstuck.
  93. // Do this by driving forward, then backward, toggling button1 on and off
  94. // every 2 seconds.
  95. if me.isImmobile() then
  96. if floor(me.immobileTimerRemaining)%4 < 2 then
  97. drive = -1
  98. else
  99. drive = 1
  100. end if
  101. aistate = "Wiggling"
  102. end if
  103.  
  104. yield
  105. end while
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement