Advertisement
KupaTec

HAMMER AI V1.1

Dec 6th, 2019
619
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.62 KB | None | 0 0
  1. //HAMMER AI V1.1
  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. //PLEASE BACK UP YOUR BOTS BEFORE ADDING/EDITING AI!
  7.  
  8. //Update log
  9. //-V1.1 Released!
  10. //--Massive thanks to Hobo_Drew for pointing out I can't code
  11. //--Removed redundant function
  12. //--Rewrote weapon firing detection to be cleaner and much more accurate
  13. //--Rewrote driving sequence to make the bot a lot more stable and accurate when hitting
  14. //--Rewrote immobilisation handling to fire weapon when attempting to move
  15.  
  16. //TODO
  17. //-Rewrite srimech
  18. //-Create a function to control max drive and turn speed
  19. //-Potentially add in ability to define which buttons to use
  20.  
  21. //====================================TUNE THE AI HERE====================================
  22. //Distance from enemy to arm weapon
  23. weaponRange = 1
  24. //Deviation from center to fire weapon once in range
  25. //Min = 0, Max = 1, Default 0.05 or 13 degrees
  26. weaponFOV = .05
  27. //"Recharge" time for weapon in seconds
  28. //For Hammers this should be set to how long it takes to fully extend or retract
  29. //For Flippers this should be set to the minimum time between attempting to flip
  30. weaponRecharge = .5
  31. //=======================================END TUNING=======================================
  32.  
  33. //// USER-CREATED FUNCTIONS
  34.  
  35. // getDistance is a function that computes the distance betwen two points in meters.
  36. 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
  37.  
  38. // getMyRobot is a function that returns a map of our robot. Sometimes our robot is not robots[0].
  39. getMyRobot = function(); for robot in robots; if robot.tag == myTag then; return robot; end if; end for; end function
  40.  
  41. // getNearestEnemy is a function that returns a map of the closest robot to us that is not us.
  42. 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
  43.  
  44. //// MAIN EVENT LOOP
  45.  
  46. while 1
  47.  
  48. // Get a reference to self and nearest enemy as well as distance between.
  49. e = getNearestEnemy()
  50. me = getMyRobot()
  51. distance = getDistance(me.position,e.position)
  52.  
  53. if(abs(turnToward(e.position)) <= weaponFOV) then
  54. drive = driveToward(e.position)
  55. turn = 0
  56. aistate = "Driving towards target"
  57. else
  58. turn = turnToward(e.position)
  59. drive = 0
  60. aistate = "Turning towards target"
  61. end if
  62.  
  63. if(distance < weaponRange) and (abs(turnToward(e.position)) <= weaponFOV) then
  64. drive = 0
  65. turn = 0
  66. button2 = true
  67. button3 = false
  68. aistate = "weapon fire"
  69. wait(weaponRecharge)
  70. button2 = false
  71. button3 = true
  72. aistate = "weapon reset"
  73. wait(weaponRecharge)
  74. else
  75. button2 = false
  76. button3 = false
  77. end if
  78.  
  79. // If inverted, try to self-right.
  80. if(me.upValue < -0.5) then
  81. button2 = true
  82. button3 = false
  83. aistate = "srimech fire"
  84. wait(weaponRecharge)
  85. button2 = false
  86. button3 = true
  87. aistate = "srimech reset"
  88. wait(weaponRecharge)
  89. end if
  90.  
  91. // If immobile, try to get unstuck.
  92. if me.isImmobile() then
  93. if floor(me.immobileTimerRemaining)%4 < 2 then
  94. drive = -1
  95. button2 = true
  96. button3 = false
  97. wait(weaponRecharge)
  98. drive = 1
  99. button2 = false
  100. button3 = true
  101. wait(weaponRecharge)
  102. end if
  103. aistate = "I want to break free!"
  104. end if
  105.  
  106. yield
  107. end while
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement