Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Welcome to Robot AI Programming 101!
- // This code is written in a language called MiniScript,
- // which is designed to be simple and easy to learn.
- // For more details, go to: http://miniscript.org
- //
- // <--- This is a comment (2 forward slashes). It is completely ignored by the compiler.
- //// Outputs:
- // drive - the forward/backward drive signal
- // turn - the left/right turning signal
- // lever1, lever2 - controller analog sticks #1 and #2
- // button1, button2, button3, and button4 - controller buttons
- //// Inputs:
- // See complete list of AI input variables shown to the left.
- //// FUNCTIONS AND THEIR VARIABLES
- // In this block, we are declaring all of the functions and any of the variables they might need.
- // An example function declaration:
- // add = function(x,y)
- // return x+y
- // end function
- //// BUILT-IN FUNCTIONS
- // getWaypoints(p1)
- //
- // getWaypoints is a built-in function that computes the navmesh waypoints to travel
- // to a point in the arena.
- // It takes one arguments:
- // p1 = position to travel to (a map of x, y, z values)
- // getWaypoints returns a list of position maps of the form [{x0,y0,z0},{x1,y1,z1},{x2,y2,z2}]
- // Example usage: nextWaypoint = getWaypoints(enemy.position)[0]
- // getClosestPointOnHazard(p1)
- //
- // getClosestPointOnHazard is a built-in function that finds the closest point on the
- // surface of an arena hazard.
- // It takes 1 argument:
- // p1 = position #1 (a map of x, y, z values)
- // getWaypoints returns a position map of the form {x0,y0,z0
- // Example usage: hazardPoint = getClosestPointOnHazard(myRobot.position)
- // driveToward(p1)
- //
- // driveToward is a built-in function that computes the drive (forward/backward) signal
- // required to drive toward a point.
- // It takes 1 argument:
- // p1 = position #1 (a map of x, y, z values)
- // driveToward returns a number: +1 = forward, 0 = stop, -1 = backward
- // Example usage: drive = driveToward(nextWaypoint)
- // turnToward(p1)
- //
- // turnToward is a built-in function that computes the turn (left/right) signal
- // required to drive toward a point.
- // It takes 1 argument:
- // p1 = position #1 (a map of x, y, z values)
- // turnToward returns a number: +1 = right, 0 = stop turning, -1 = left
- // Example usage: turn = turnToward(nextWaypoint)
- //// USER-CREATED FUNCTIONS
- // isVector is a fuction that tests if the given map has x, y, and z components.
- // It takes 1 arguments:
- // v = some variable
- // example usage: if isVector(myVariable) == true then
- 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
- // getDistance is a function that computes the distance betwen two points in meters.
- // It takes 2 arguments:
- // v1 = position #1 (a map of x, y, z values)
- // v2 = position #2 (a map of x, y, z values)
- // example usage: distance = getDistance(p1,p2)
- 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
- // getMyRobot is a function that returns a map of our robot. Sometimes our robot is not robots[0].
- // It takes no arguments.
- // example usage: myRobot = getMyRobot()
- getMyRobot = function(); for robot in robots; if robot.tag == myTag then; return robot; end if; end for; end function
- // getNearestEnemy is a function that returns a map of the closest robot to us that is not us.
- // It takes no arguments.
- // example usage: enemy = getNearestEnemy()
- 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
- //// MAIN EVENT LOOP
- ///This is our main event loop. It runs continuously.
- // In order to keep running, it needs the following:
- // An opening statement: while 1
- // A bunch of events to execute
- // A brief wait statement: wait(0.01)
- // A closing statement: end while
- while 1
- // Get a reference to self and nearest enemy.
- e = getNearestEnemy()
- me = getMyRobot()
- // Always try to attack.
- drive = driveToward(e.position)
- turn = turnToward(e.position)
- distance = getDistance(me.position,e.position)
- if(distance < 2) then
- button1 = true
- else if distance >= 2 then
- button1 = false
- end if
- aistate = "attacking"
- // If inverted, try to self-right.
- // Do this by driving backward and turning button1 on.
- // Depending on the location of the self righting mechanism,
- // this might need to be assigned to a different button.
- if me.upValue < 0 then
- if floor(time)%4 < 2 then
- drive = -1
- button1 = true
- else
- drive = 1
- buttonn1 = false
- end if
- aistate = "self-righting"
- end if
- // If immobile, try to get unstuck.
- // Do this by driving forward, then backward, toggling button1 on and off
- // every 2 seconds.
- if me.isImmobile() then
- if floor(me.immobileTimerRemaining)%4 < 2 then
- drive = -1
- button1 = false
- else
- drive = 1
- button1 = true
- end if
- aistate = "trying to get unstuck"
- end if
- yield
- end while
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement