Advertisement
hobo_drew

RR2 Default AI

Dec 1st, 2019
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.54 KB | None | 0 0
  1. // Welcome to Robot AI Programming 101!
  2. // This code is written in a language called MiniScript,
  3. // which is designed to be simple and easy to learn.
  4. // For more details, go to: http://miniscript.org
  5. //
  6. // <--- This is a comment (2 forward slashes). It is completely ignored by the compiler.
  7.  
  8.  
  9. //// Outputs:
  10. // drive - the forward/backward drive signal
  11. // turn - the left/right turning signal
  12. // lever1, lever2 - controller analog sticks #1 and #2
  13. // button1, button2, button3, and button4 - controller buttons
  14.  
  15.  
  16. //// Inputs:
  17. // See complete list of AI input variables shown to the left.
  18.  
  19.  
  20. //// FUNCTIONS AND THEIR VARIABLES
  21. // In this block, we are declaring all of the functions and any of the variables they might need.
  22. // An example function declaration:
  23. // add = function(x,y)
  24. // return x+y
  25. // end function
  26.  
  27.  
  28. //// BUILT-IN FUNCTIONS
  29.  
  30.  
  31. // getWaypoints(p1)
  32. //
  33. // getWaypoints is a built-in function that computes the navmesh waypoints to travel
  34. // to a point in the arena.
  35. // It takes one arguments:
  36. // p1 = position to travel to (a map of x, y, z values)
  37. // getWaypoints returns a list of position maps of the form [{x0,y0,z0},{x1,y1,z1},{x2,y2,z2}]
  38. // Example usage: nextWaypoint = getWaypoints(enemy.position)[0]
  39.  
  40.  
  41. // getClosestPointOnHazard(p1)
  42. //
  43. // getClosestPointOnHazard is a built-in function that finds the closest point on the
  44. // surface of an arena hazard.
  45. // It takes 1 argument:
  46. // p1 = position #1 (a map of x, y, z values)
  47. // getWaypoints returns a position map of the form {x0,y0,z0
  48. // Example usage: hazardPoint = getClosestPointOnHazard(myRobot.position)
  49.  
  50.  
  51. // driveToward(p1)
  52. //
  53. // driveToward is a built-in function that computes the drive (forward/backward) signal
  54. // required to drive toward a point.
  55. // It takes 1 argument:
  56. // p1 = position #1 (a map of x, y, z values)
  57. // driveToward returns a number: +1 = forward, 0 = stop, -1 = backward
  58. // Example usage: drive = driveToward(nextWaypoint)
  59.  
  60.  
  61. // turnToward(p1)
  62. //
  63. // turnToward is a built-in function that computes the turn (left/right) signal
  64. // required to drive toward a point.
  65. // It takes 1 argument:
  66. // p1 = position #1 (a map of x, y, z values)
  67. // turnToward returns a number: +1 = right, 0 = stop turning, -1 = left
  68. // Example usage: turn = turnToward(nextWaypoint)
  69.  
  70.  
  71.  
  72. //// USER-CREATED FUNCTIONS
  73.  
  74. // isVector is a fuction that tests if the given map has x, y, and z components.
  75. // It takes 1 arguments:
  76. // v = some variable
  77. // example usage: if isVector(myVariable) == true then
  78. 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
  79.  
  80. // getDistance is a function that computes the distance betwen two points in meters.
  81. // It takes 2 arguments:
  82. // v1 = position #1 (a map of x, y, z values)
  83. // v2 = position #2 (a map of x, y, z values)
  84. // example usage: distance = getDistance(p1,p2)
  85. 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
  86.  
  87. // getMyRobot is a function that returns a map of our robot. Sometimes our robot is not robots[0].
  88. // It takes no arguments.
  89. // example usage: myRobot = getMyRobot()
  90. getMyRobot = function(); for robot in robots; if robot.tag == myTag then; return robot; end if; end for; end function
  91.  
  92. // getNearestEnemy is a function that returns a map of the closest robot to us that is not us.
  93. // It takes no arguments.
  94. // example usage: enemy = getNearestEnemy()
  95. 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
  96.  
  97.  
  98. //// MAIN EVENT LOOP
  99. ///This is our main event loop. It runs continuously.
  100. // In order to keep running, it needs the following:
  101. // An opening statement: while 1
  102. // A bunch of events to execute
  103. // A brief wait statement: wait(0.01)
  104. // A closing statement: end while
  105.  
  106. while 1
  107.  
  108. // Get a reference to self and nearest enemy.
  109. e = getNearestEnemy()
  110. me = getMyRobot()
  111.  
  112. // Always try to attack.
  113. drive = driveToward(e.position)
  114. turn = turnToward(e.position)
  115.  
  116. distance = getDistance(me.position,e.position)
  117. if(distance < 2) then
  118. button1 = true
  119. else if distance >= 2 then
  120. button1 = false
  121. end if
  122. aistate = "attacking"
  123.  
  124. // If inverted, try to self-right.
  125. // Do this by driving backward and turning button1 on.
  126. // Depending on the location of the self righting mechanism,
  127. // this might need to be assigned to a different button.
  128. if me.upValue < 0 then
  129. if floor(time)%4 < 2 then
  130. drive = -1
  131. button1 = true
  132. else
  133. drive = 1
  134. buttonn1 = false
  135. end if
  136. aistate = "self-righting"
  137. end if
  138.  
  139. // If immobile, try to get unstuck.
  140. // Do this by driving forward, then backward, toggling button1 on and off
  141. // every 2 seconds.
  142. if me.isImmobile() then
  143. if floor(me.immobileTimerRemaining)%4 < 2 then
  144. drive = -1
  145. button1 = false
  146. else
  147. drive = 1
  148. button1 = true
  149. end if
  150. aistate = "trying to get unstuck"
  151. end if
  152.  
  153.  
  154. yield
  155. end while
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement