Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //HAMMER AI V1.0
- //By KupaTec
- //AI script useful for limited rotation motor weapons such as hammers or flippers
- //To use set up your motor as a DOUBLE THROW MOMENTARY SWITCH using button2 as + and button3 as -
- //Update log
- //-V1 Released!
- //--Easier to read and tune
- //--No more constantly spinning motors!
- //--Refined srimech detection
- //TODO
- //-Rewrite srimech and immobile handling
- //-Potentially add in ability to define which buttons to use
- //====================================TUNE THE AI HERE====================================
- //Distance from enemy to arm weapon in meters(?)
- weaponRange = 2
- //Deviation from center to fire weapon once in range
- //Min = 0, Max = 1
- weaponFOV = .1
- //"Recharge" time for weapon in seconds
- //For Hammers this should be set to how long it takes to fully extend or retract
- //For Flippers this should be set to the minimum time between attempting to flip
- weaponRecharge = .75
- //=======================================END TUNING=======================================
- //// USER-CREATED FUNCTIONS
- // 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
- // Normalize
- // Normalizes a number to always produce a positive integer
- // for example normalize(-0.2) should output 0.02
- normalize = function(n); if n < 0 then nn = n*-1 else nn = n; return nn; end if; end function
- //// MAIN EVENT LOOP
- while 1
- // Get a reference to self and nearest enemy as well as distance between.
- e = getNearestEnemy()
- me = getMyRobot()
- distance = getDistance(me.position,e.position)
- // Drive towards opponent
- drive = driveToward(e.position)
- // Turn towards opponent
- turn = turnToward(e.position)
- if(distance < weaponRange) then
- if(normalize(turn) <= weaponFOV) then
- button2 = true
- button3 = false
- aistate = "weapon fire"
- wait(weaponRecharge)
- button2 = false
- button3 = true
- aistate = "weapon reset"
- wait(weaponRecharge)
- else
- button2 = false
- button3 = false
- aistate = "in range"
- end if
- else
- button2 = false
- button3 = false
- aistate = "out of range"
- end if
- // If inverted, try to self-right.
- if(me.upValue < -0.5) then
- button2 = true
- button3 = false
- aistate = "srimech fire"
- wait(weaponRecharge)
- button2 = false
- button3 = true
- aistate = "srimech reset"
- wait(weaponRecharge)
- 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
- else
- drive = 1
- end if
- aistate = "Wiggling"
- end if
- yield
- end while
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement