//HAMMER AI V1.1 //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 - //PLEASE BACK UP YOUR BOTS BEFORE ADDING/EDITING AI! //Update log //-V1.1 Released! //--Massive thanks to Hobo_Drew for pointing out I can't code //--Removed redundant function //--Rewrote weapon firing detection to be cleaner and much more accurate //--Rewrote driving sequence to make the bot a lot more stable and accurate when hitting //--Rewrote immobilisation handling to fire weapon when attempting to move //TODO //-Rewrite srimech //-Create a function to control max drive and turn speed //-Potentially add in ability to define which buttons to use //====================================TUNE THE AI HERE==================================== //Distance from enemy to arm weapon weaponRange = 1 //Deviation from center to fire weapon once in range //Min = 0, Max = 1, Default 0.05 or 13 degrees weaponFOV = .05 //"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 = .5 //=======================================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 //// 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) if(abs(turnToward(e.position)) <= weaponFOV) then drive = driveToward(e.position) turn = 0 aistate = "Driving towards target" else turn = turnToward(e.position) drive = 0 aistate = "Turning towards target" end if if(distance < weaponRange) and (abs(turnToward(e.position)) <= weaponFOV) then drive = 0 turn = 0 button2 = true button3 = false aistate = "weapon fire" wait(weaponRecharge) button2 = false button3 = true aistate = "weapon reset" wait(weaponRecharge) else button2 = false button3 = false 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. if me.isImmobile() then if floor(me.immobileTimerRemaining)%4 < 2 then drive = -1 button2 = true button3 = false wait(weaponRecharge) drive = 1 button2 = false button3 = true wait(weaponRecharge) end if aistate = "I want to break free!" end if yield end while