Advertisement
Guest User

Untitled

a guest
Apr 12th, 2013
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.29 KB | None | 0 0
  1. /*
  2. Welcome to the tutorial!!!
  3.  
  4. This file is full of helpful comments to walk you through acheiving your mission.
  5.  
  6. Below is your AI function. At the bottom of thie file there is more detail on
  7. exactly what that means.
  8. */
  9.  
  10. Game.IntroPlayerAI = function(units, waypoints) {
  11.  
  12. //First lets define some variables
  13.  
  14. var i; //iterator for loops
  15. var ourShip; //the ship we are controlling
  16. var nextDestination; //where our ship is moving to next
  17. var moveInstruction; //the instruction that tells our ship to move there
  18. var target; //the target for our ship to attack when it is ready
  19. var attackInstruction; //the instruction telling it to attack
  20.  
  21. // Next lets write a few helper functions...
  22.  
  23. /* This function finds our ship.
  24. We only have 1 ship so its pretty simple.
  25. The units array contains all ships so we just loop through that array and
  26. find the first ship that isn't an enemy ship. */
  27. function getOurShip(units) {
  28. //loop through the array of units
  29. for ( i = 0; i < units.length; i++) {
  30. //check if its an enemy ship
  31. if (units[i].isEnemy === false) {
  32. return units[i]; //if its not return it
  33. }
  34. }
  35. };
  36.  
  37. //This function finds an enemy ship using the exact same approach
  38. function getNextEnemy(units) {
  39. //loop through the array of units
  40. for (i = 0; i < units.length; i++) {
  41. //check if it is an enemy ship
  42. if(units[i].isEnemy) {
  43. return units[i]; //if it is return it.
  44. }
  45. }
  46. return 'none'; // if we don't find an enemy return none;
  47. };
  48.  
  49. //This function loops through the waypoints and finds the first waypoint
  50. //that we have not reached
  51. function getNextWayPoint(waypoints) {
  52. //go through the way points
  53. for(var i = 0; i < waypoints.length; i++) {
  54. //if we find one we haven't reached, return it
  55. if(!waypoints[i].reached) {
  56. return waypoints[i];
  57. }
  58. }
  59. return 'none'; // if we don't find a waypoint return none;
  60. };
  61.  
  62. //That's most of the hard work :) Now we just have to use our helper
  63. //functions
  64.  
  65. // Now that we've taken care of the way points lets add some code
  66. // to deal with our enemies... The idea is exactly the same.
  67. // but you'll have to do some of the work yourself :)
  68.  
  69. //Step 1: add code to use your getNextEnemy helper function to get
  70. // the next enemy (hint nextEnemy = getNextEnemy(units);)
  71. var nextEnemy = getNextEnemy(units);
  72.  
  73.  
  74. //Step 2: if the next enemy is not equal to 'none' create an
  75. // attack instruction to attack it and issue the command.
  76. // example code for those two lines is commented out below.
  77. if (nextEnemy != 'none') {
  78. attackInstruction = new AttackInstruction(nextEnemy);
  79. return [new Command(ourShip, [attackInstruction])];
  80. }
  81.  
  82. /* Code to create an AttackInstruction to attack the nextEnemy and to
  83. send that instruction as a Command to our ship.
  84.  
  85. attackInstruction = new AttackInstruction(nextEnemy);
  86. return [new Command(ourShip, [attackInstruction])];
  87. */
  88.  
  89. ourShip = getOurShip(units); //get our ship
  90. nextDestination = getNextWayPoint(waypoints); //get the next waypoint
  91.  
  92.  
  93. //Okay if we have a destination lets go there...
  94. if(nextDestination !== 'none') {
  95.  
  96. //To go to the waypoint we want a move instruction.
  97. moveInstruction = new MoveInstruction(nextDestination);
  98.  
  99. //A command takes a list of instructions and assigns
  100. //them to a particular ship. To make our ship fly to the first way point
  101. //We need to issue it a command with our move instruction and return it
  102. //like so. Just uncomment the line below and then click begin battle
  103. //and watch your ship go.
  104.  
  105. return [new Command(ourShip, [moveInstruction])];
  106. }
  107.  
  108.  
  109. };
  110.  
  111. /*
  112.  
  113. DETAILS ON HOW YOUR AI FUNCTION WORKS:
  114.  
  115. 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.
  116. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement