Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //FLIPPER AI V1.0
- //By Hobo_Drew
- //This is AI for flippers in RR2 that use pistons to fire.
- //Use toggle switch with 'control as servo' turned on as controls for the flipper. Use button 1.
- //Massive thanks to KupaTec for basically teaching me how to code miniscript.
- // USER-CREATED FUNCTIONS
- 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.
- 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].
- 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.
- 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
- //=======USER INPUTS=======
- //These have to be adjusted as they fluctuate depending on the flipper.
- //Using the same terms as Kupa in hopes that they'll be accepted as universal terms so that RR2 AI becomes easier to understand.
- //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.
- weaponFOV = 0.15
- //amount of time the flipper waits before firing the weapon.
- weaponDelay = 0.1
- //amount of time the robot waits before retracting the weapon.
- weaponRecharge = 0.3
- //size of the zone the enemy has to be in for the flipper to activate. Changes the diameter of aforementioned circle.
- weaponRange = 0.7
- //==========================
- // This 'wait' was an attempt to stop the flipper from firing the second the match started, but it didn't work.
- wait(1)
- //main loop
- while 1
- //locate opponent and self
- e = getNearestEnemy()
- me = getMyRobot()
- //Drive and turn towards opponent
- drive = driveToward(e.position)
- turn = turnToward(e.position)
- //finds the distance between you and the opponent
- distance = getDistance(me.position,e.position)
- //weapon mechanism AI. Instead of Kupa's 'normalize' code, I used abs(). It does the same thing.
- if(distance < weaponRange) and (abs(turn) < weaponFOV) then
- button1 = 1
- aistate = "fire weapon"
- wait(weaponDelay)
- button1 = -1
- aistate = "reset weapon"
- wait(weaponRecharge)
- else
- button1 = false
- aistate = "out of range"
- end if
- //If inverted, self right.
- if me.upValue < 0 then
- button1 = true
- wait(weaponDelay)
- button1 = false
- wait(weaponRecharge)
- aistate = "self-righting"
- end if
- //If immobile, try to get free.
- if me.isImmobile() then
- button1 = true
- wait(weaponDelay)
- button1 = false
- wait(weaponRecharge)
- aistate = "trying to get unstuck"
- end if
- yield
- end while
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement