Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Welcome to the tutorial!!!
- This file is full of helpful comments to walk you through acheiving your mission.
- Below is your AI function. At the bottom of thie file there is more detail on
- exactly what that means.
- */
- Game.IntroPlayerAI = function(units, waypoints) {
- //First lets define some variables
- var i; //iterator for loops
- var ourShip; //the ship we are controlling
- var nextDestination; //where our ship is moving to next
- var moveInstruction; //the instruction that tells our ship to move there
- var target; //the target for our ship to attack when it is ready
- var attackInstruction; //the instruction telling it to attack
- // Next lets write a few helper functions...
- /* This function finds our ship.
- We only have 1 ship so its pretty simple.
- The units array contains all ships so we just loop through that array and
- find the first ship that isn't an enemy ship. */
- function getOurShip(units) {
- //loop through the array of units
- for ( i = 0; i < units.length; i++) {
- //check if its an enemy ship
- if (units[i].isEnemy === false) {
- return units[i]; //if its not return it
- }
- }
- };
- //This function finds an enemy ship using the exact same approach
- function getNextEnemy(units) {
- //loop through the array of units
- for (i = 0; i < units.length; i++) {
- //check if it is an enemy ship
- if(units[i].isEnemy) {
- return units[i]; //if it is return it.
- }
- }
- return 'none'; // if we don't find an enemy return none;
- };
- //This function loops through the waypoints and finds the first waypoint
- //that we have not reached
- function getNextWayPoint(waypoints) {
- //go through the way points
- for(var i = 0; i < waypoints.length; i++) {
- //if we find one we haven't reached, return it
- if(!waypoints[i].reached) {
- return waypoints[i];
- }
- }
- return 'none'; // if we don't find a waypoint return none;
- };
- //That's most of the hard work :) Now we just have to use our helper
- //functions
- // Now that we've taken care of the way points lets add some code
- // to deal with our enemies... The idea is exactly the same.
- // but you'll have to do some of the work yourself :)
- //Step 1: add code to use your getNextEnemy helper function to get
- // the next enemy (hint nextEnemy = getNextEnemy(units);)
- var nextEnemy = getNextEnemy(units);
- //Step 2: if the next enemy is not equal to 'none' create an
- // attack instruction to attack it and issue the command.
- // example code for those two lines is commented out below.
- if (nextEnemy != 'none') {
- attackInstruction = new AttackInstruction(nextEnemy);
- return [new Command(ourShip, [attackInstruction])];
- }
- /* Code to create an AttackInstruction to attack the nextEnemy and to
- send that instruction as a Command to our ship.
- attackInstruction = new AttackInstruction(nextEnemy);
- return [new Command(ourShip, [attackInstruction])];
- */
- ourShip = getOurShip(units); //get our ship
- nextDestination = getNextWayPoint(waypoints); //get the next waypoint
- //Okay if we have a destination lets go there...
- if(nextDestination !== 'none') {
- //To go to the waypoint we want a move instruction.
- moveInstruction = new MoveInstruction(nextDestination);
- //A command takes a list of instructions and assigns
- //them to a particular ship. To make our ship fly to the first way point
- //We need to issue it a command with our move instruction and return it
- //like so. Just uncomment the line below and then click begin battle
- //and watch your ship go.
- return [new Command(ourShip, [moveInstruction])];
- }
- };
- /*
- DETAILS ON HOW YOUR AI FUNCTION WORKS:
- Each tick the game will call your AI function with a list of the units that are alive in the game and a list of waypoints. Your job is to take that data and return an array of Commands that tell your ship(s) what to do. If you do not return a command for a specific ship it will continue executing whatever instructions it received previously until they are all completed at which point it will just wait for additional commands.
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement