Advertisement
hobo_drew

RR2 Flipper AI

Dec 4th, 2019
367
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.44 KB | None | 0 0
  1. //FLIPPER AI V1.0
  2. //By Hobo_Drew
  3. //This is AI for flippers in RR2 that use pistons to fire.
  4. //Use toggle switch with 'control as servo' turned on as controls for the flipper. Use button 1.
  5. //Massive thanks to KupaTec for basically teaching me how to code miniscript.
  6.  
  7.  
  8. // USER-CREATED FUNCTIONS
  9.  
  10. isVector = function(v); vx = v.hasIndex("x"); vy = v.hasIndex("y"); vz = v.hasIndex("z"); if vx*vy*vz == 1 then; return true; else; return false; end if;end function
  11.  
  12. // getDistance is a function that computes the distance betwen two points in meters.
  13. 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
  14.  
  15. // getMyRobot is a function that returns a map of our robot. Sometimes our robot is not robots[0].
  16. getMyRobot = function(); for robot in robots; if robot.tag == myTag then; return robot; end if; end for; end function
  17.  
  18. // getNearestEnemy is a function that returns a map of the closest robot to us that is not us.
  19. 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
  20.  
  21.  
  22.  
  23.  
  24. //=======USER INPUTS=======
  25. //These have to be adjusted as they fluctuate depending on the flipper.
  26. //Using the same terms as Kupa in hopes that they'll be accepted as universal terms so that RR2 AI becomes easier to understand.
  27.  
  28. //width of the zone the enemy has to be in for the flipper to activate. Minimum=0, maximum=1. Goes in the shape of a circle.
  29. weaponFOV = 0.15
  30.  
  31. //amount of time the flipper waits before firing the weapon.
  32. weaponDelay = 0.1
  33.  
  34. //amount of time the robot waits before retracting the weapon.
  35. weaponRecharge = 0.3
  36.  
  37. //size of the zone the enemy has to be in for the flipper to activate. Changes the diameter of aforementioned circle.
  38. weaponRange = 0.7
  39. //==========================
  40.  
  41.  
  42.  
  43.  
  44. // This 'wait' was an attempt to stop the flipper from firing the second the match started, but it didn't work.
  45. wait(1)
  46.  
  47.  
  48.  
  49.  
  50. //main loop
  51. while 1
  52.  
  53. //locate opponent and self
  54. e = getNearestEnemy()
  55. me = getMyRobot()
  56.  
  57. //Drive and turn towards opponent
  58. drive = driveToward(e.position)
  59. turn = turnToward(e.position)
  60.  
  61. //finds the distance between you and the opponent
  62. distance = getDistance(me.position,e.position)
  63.  
  64.  
  65. //weapon mechanism AI. Instead of Kupa's 'normalize' code, I used abs(). It does the same thing.
  66. if(distance < weaponRange) and (abs(turn) < weaponFOV) then
  67. button1 = 1
  68. aistate = "fire weapon"
  69. wait(weaponDelay)
  70. button1 = -1
  71. aistate = "reset weapon"
  72. wait(weaponRecharge)
  73. else
  74. button1 = false
  75. aistate = "out of range"
  76. end if
  77.  
  78.  
  79. //If inverted, self right.
  80. if me.upValue < 0 then
  81. button1 = true
  82. wait(weaponDelay)
  83. button1 = false
  84. wait(weaponRecharge)
  85. aistate = "self-righting"
  86. end if
  87.  
  88.  
  89. //If immobile, try to get free.
  90. if me.isImmobile() then
  91. button1 = true
  92. wait(weaponDelay)
  93. button1 = false
  94. wait(weaponRecharge)
  95. aistate = "trying to get unstuck"
  96. end if
  97.  
  98.  
  99. yield
  100. end while
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement