Advertisement
asqapro

AI A*

Jan 1st, 2013
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #pragma strict
  2.  
  3. import System.Collections.Generic; //Using unity lists //Syntax highlighting comes from copy/ paste
  4.  
  5. //var structs : GameObject[];
  6. //structs = GameObject.FindGameObjectsWithTag("Static");
  7.  
  8. var player : Transform; //Set in inspector to player
  9. var playGoal : boolean; //A boolean checking if the player's position is the goal
  10.  
  11. var mon : Transform;  //Set in inspector to monster
  12.  
  13. var currentNode : node;  //A dynamic coordinate object, used for whenever I need to access the node the monster is standing on
  14. var nextNode : node;  //A dynamic coordinate object, used for whenever I need to access the next node the monster will be standing on
  15.  
  16. var targetNode : node; //Used when monster is actually moving, the sub-main goal
  17. var target : node; //Used when monster is actually moving, a goal in a series of goals between two sub-main goals
  18.  
  19. var nodeLeft : node;
  20. var nodeRight : node;
  21. var nodeFor : node;
  22. var nodeBack : node;
  23. var nodeForLeft : node;
  24. var nodeForRight : node;
  25. var nodeBackLeft : node;
  26. var nodeBackRight : node;
  27. var bestScore : node;
  28. var tempScore : node;
  29.  
  30. //var currentPoly : Transform; //I was (and still might) navmap it to an extent, this will allow me to access the polygon the monster is standing on
  31.  
  32. var goals : List.<node> = new List.<node>();  //A list of the main position goals of the monster
  33. var nodeList : List.<node> = new List.<node>(); //A list of all the nodes in an area
  34. var neighborNodes : List.<node> = new List.<node>();   //A list later containing nodes surrounding monster's current position
  35.  
  36. var openSet : List.<node> = new List.<node>();   //Open set of nodes (explored, viable)  
  37. var closedSet : List.<node> = new List.<node>(); //Closed set of nodes (explored, not viable)
  38. var lockedSet : List.<node> = new List.<node>(); //Explored, unusable (blocked)
  39.  
  40. var maxEyeRange : int; //How far the monster can see with no obstructions
  41. var rightEye : boolean; //A placeholder in the code to make sure the monster can see out of his right eye
  42. var leftEye : boolean; //A placeholder in the code to make sure the monster can see out of his left eye
  43. var lineOfSight : int;  //How far the monster can currently see
  44.  
  45. var subCounter : int;  //A counter to keep track of sub nodes
  46. var mainCounter : int; //A counter to keep track of overhead nodes
  47.  
  48. function Start(){ //Assign all the previously created variables
  49.     subCounter = 0;
  50.     mainCounter = 0;
  51.     leftEye = true;
  52.     rightEye = true;
  53.     lineOfSight = 5;
  54.     maxEyeRange = 5;
  55.     playGoal = false;
  56.     currentNode = new node(player.position);
  57.     InvokeRepeating("checkSight", 0, 1); //Start checking sightline immediately every second
  58.     yield layerMap();  //And begin to create the main goals
  59. }
  60.  
  61. function checkSight(){
  62.     if(leftEye && rightEye){ //If both of the monster's eyes are functioning
  63.         var sight : Vector3 = Vector3(mon.position.x + maxEyeRange, mon.position.y, mon.position.z); //Check the spot 5 meters directly ahead of him
  64.         if(Physics.Linecast(mon.position, sight)){ //If the spot hits
  65.             lineOfSight = Mathf.Abs(sight.x - mon.position.x); //Reduce his line of sight
  66.         }
  67.     }
  68. }
  69.  
  70. function layerMap() : IEnumerator{
  71.     while(Vector3.Distance(mon.position, currentNode.getPos()) > maxEyeRange){ //If he can't see the end goal
  72.         //print(currentNode.getPos());
  73.         goals.Add(currentNode); //Put that goal in the goal list
  74.         currentNode.setPos(Vector3(((mon.position.x + currentNode.getPos().x) / 2), currentNode.getPos().y, ((mon.position.z + currentNode.getPos().z) / 2))); //Next goal is halfway between them
  75.         //Note: I was using recursion, but according to research on Unity forums, function calls cost more memory than loops - each call must be placed onto the stack
  76.     }
  77.     goals.Reverse(); //Reverse the list's order so to not have to access them backwards (before reverse - last goal--->third goal--->second goal--->first goal)
  78.     refineMap(new node(mon.position), goals[0]);  //After the large goals have been set, fill in the smaller spaces
  79. }
  80.  
  81. function refineMap(currentNode : node, goal : node) : IEnumerator{ //Params are the node the monster is standing on and where he wants to get to next
  82.     //while(Mathf.RoundToInt(currentNode.getPos().x) != Mathf.RoundToInt(goal.getPos().x) && Mathf.RoundToInt(currentNode.getPos().z) != Mathf.RoundToInt(goal.getPos().z)){ //if the mapping has reached the edge of the monster's vision, begin movement along pathways
  83.     while(Vector3.Distance(currentNode.getPos(), goal.getPos()) >= 0.1){
  84.         //print("loop" + "   " + Vector3.Distance(currentNode.getPos(), goal.getPos()));
  85.         //print(new Vector3(Mathf.RoundToInt(currentNode.getPos().x), 0, Mathf.RoundToInt(currentNode.getPos().z)) + "   " + new Vector3(Mathf.RoundToInt(goal.getPos().x), 0, Mathf.RoundToInt(goal.getPos().z)));
  86.         nodeLeft.setPos(Vector3(currentNode.getPos().x, currentNode.getPos().y, currentNode.getPos().z + 0.1));                     //]
  87.         nodeLeft.setCost(Vector3.Distance(goal.getPos(), nodeLeft.getPos()) + Vector3.Distance(nodeLeft.getPos(), currentNode.getPos()));//Heuristic - Distance to goal + distance between nodes
  88.         //print("Left: " + nodeLeft.getPos() + "   " + nodeLeft.getCost());
  89.    
  90.         nodeRight.setPos(Vector3(currentNode.getPos().x, currentNode.getPos().y, currentNode.getPos().z - 0.1));                    //]
  91.         nodeRight.setCost(Vector3.Distance(goal.getPos(), nodeRight.getPos()) + Vector3.Distance(nodeRight.getPos(), currentNode.getPos()));
  92.         //print("Right: " + nodeRight.getPos() + "   " + nodeRight.getCost());
  93.    
  94.         nodeFor.setPos(Vector3(currentNode.getPos().x + 0.1, currentNode.getPos().y, currentNode.getPos().z));                      //]
  95.         nodeFor.setCost(Vector3.Distance(goal.getPos(), nodeLeft.getPos()) + Vector3.Distance(nodeFor.getPos(), currentNode.getPos()));
  96.         //print("For: " + nodeFor.getPos() + "   " + nodeFor.getCost());
  97.    
  98.         nodeBack.setPos(Vector3(currentNode.getPos().x - 0.1, currentNode.getPos().y, currentNode.getPos().z));                     //] Grab the next 8 nodes
  99.         nodeBack.setCost(Vector3.Distance(goal.getPos(), nodeBack.getPos()) + Vector3.Distance(nodeBack.getPos(), currentNode.getPos()));
  100.         //print("Back: " + nodeBack.getPos() + "   " + nodeBack.getCost());
  101.    
  102.         nodeForLeft.setPos(Vector3(currentNode.getPos().x + 0.1, currentNode.getPos().y, currentNode.getPos().z + 0.1));                //] In a decagon of directions
  103.         nodeForLeft.setCost(Vector3.Distance(goal.getPos(), nodeForLeft.getPos()) + Vector3.Distance(nodeForLeft.getPos(), currentNode.getPos()));
  104.         //print("ForLeft: " + nodeForLeft.getPos() + "   " + nodeForLeft.getCost());
  105.    
  106.         nodeForRight.setPos(Vector3(currentNode.getPos().x + 0.1, currentNode.getPos().y, currentNode.getPos().z - 0.1));               //] Around the node just traveled to
  107.         nodeForRight.setCost(Vector3.Distance(goal.getPos(), nodeForRight.getPos()) + Vector3.Distance(nodeForRight.getPos(), currentNode.getPos()));
  108.         //print("ForRight: " + nodeForRight.getPos() + "   " + nodeForRight.getCost());
  109.    
  110.         nodeBackLeft.setPos(Vector3(currentNode.getPos().x - 0.1, currentNode.getPos().y, currentNode.getPos().z + 0.1));               //]
  111.         nodeBackLeft.setCost(Vector3.Distance(goal.getPos(), nodeBackLeft.getPos()) + Vector3.Distance(nodeBackLeft.getPos(), currentNode.getPos()));
  112.         //print("BackLeft: " + nodeBackLeft.getPos() + "   " + nodeBackLeft.getCost());
  113.    
  114.         nodeBackRight.setPos(Vector3(currentNode.getPos().x - 0.1, currentNode.getPos().y, currentNode.getPos().z - 0.1));              //]
  115.         nodeBackRight.setCost(Vector3.Distance(goal.getPos(), nodeBackRight.getPos()) + Vector3.Distance(nodeBackRight.getPos(), currentNode.getPos()));
  116.         //print("BackRight: " + nodeBackRight.getPos() + "   " + nodeBackRight.getCost());
  117.    
  118.         neighborNodes.Add(nodeLeft);
  119.         neighborNodes.Add(nodeRight);
  120.         neighborNodes.Add(nodeFor);                 //]  Put all the possible next nodes
  121.         neighborNodes.Add(nodeBack);
  122.         neighborNodes.Add(nodeForLeft);
  123.         neighborNodes.Add(nodeForRight);            //]  Into the "neighborNodes" list
  124.         neighborNodes.Add(nodeBackLeft);
  125.         neighborNodes.Add(nodeBackRight);
  126.         //for(var iter : int = neighborNodes.Count-1; iter >= 0; iter--){
  127.         //  if(openSet.Contains(neighborNodes[iter]) || closedSet.Contains(neighborNodes[iter])){
  128.         //      neighborNodes[iter].setCost(-1);
  129.         //  }
  130.         //}
  131.         //var neighborCopy : List.<node> = neighborNodes;
  132.         for(var iter = neighborNodes.Count-1; iter >= 0; iter--){
  133.             for(var loop1 = openSet.Count-1; loop1 >= 0; loop1--){
  134.                 if(openSet[loop1].getPos() == neighborNodes[iter].getPos()){
  135.                     //print("REMOVE111: " + neighborNodes[iter].getPos() + "  " + neighborNodes[iter].getCost());
  136.                     neighborNodes.Remove(neighborNodes[iter]);
  137.                     iter -= 0.5;
  138.                 }
  139.             }
  140.             if(iter != 0){
  141.                 if(1 % iter > 1){
  142.                     iter += 0.5;
  143.                 }
  144.             }
  145.             for(var loop2 = closedSet.Count-1; loop2 >= 0; loop2--){
  146.                 if(closedSet[loop2].getPos() == neighborNodes[iter].getPos()){
  147.                     //print("REMOVE222: " + neighborNodes[iter].getPos() + "  " + closedSet[loop2].getPos());
  148.                     neighborNodes.Remove(neighborNodes[iter]);
  149.                     iter -= 0.5;
  150.                 }
  151.             }
  152.             if(iter != 0){
  153.                 if(1 % iter > 1){
  154.                     iter += 0.5;
  155.                 }
  156.             }
  157.             for(var loop3 = lockedSet.Count-1; loop3 >= 0; loop3--){
  158.                 if(lockedSet[loop3].getPos() == neighborNodes[loop3].getPos()){
  159.                     print("REMOVE333: " + neighborNodes[iter].getPos() + "  " + neighborNodes[iter].getCost());
  160.                     neighborNodes.Remove(neighborNodes[iter]);
  161.                 }
  162.             }
  163.         }
  164.         bestScore.setCost(Mathf.Infinity); //Set the compare score to the maximum value
  165.         for(iter = 0; iter < neighborNodes.Count; iter++){  //For every element left in the "neighborNodes" list
  166.             if(neighborNodes[iter].getCost() < bestScore.getCost()){ //Check for the best score
  167.                 bestScore.setPos(neighborNodes[iter].getPos());
  168.                 bestScore.setCost(neighborNodes[iter].getCost());
  169.             }
  170.         }
  171.         //print("LIST CONTENTS111111: ");
  172.         //for(var item : int = 0; item < openSet.Count; item++){
  173.         //  print(openSet[item].getPos());
  174.         //}
  175.         //print("END111111");
  176.         openSet.Add(new node(bestScore)); //Add the best scoring node
  177.         //print("BEST SCORE POS: " + bestScore.getPos() + " BEST SCORE COST: " + bestScore.getCost());
  178.         //print("LIST CONTENTS222222: ");
  179.         //for(var item2 : int = 0; item2 < openSet.Count; item2++){
  180.         //  print(openSet[item2].getPos());
  181.         //}
  182.         //print("END222222");
  183.         neighborNodes.Remove(bestScore);
  184.         for(iter = 0; iter < neighborNodes.Count; iter++){
  185.             closedSet.Add(new node(neighborNodes[iter])); //Add all the non-best score nodes to the closed set
  186.         }
  187.         neighborNodes.Clear(); //Clear the list for the next loop
  188.         currentNode = bestScore;
  189.         //print(currentNode.getPos() + "   " + currentNode.getCost() + "CURRENTNODE");
  190.         if(playGoal){
  191.             goal.setPos(player.position);
  192.         }
  193.         yield;
  194.     }
  195.     travelPath();
  196. }
  197.  
  198. function travelPath(){
  199.     print("CONTENTS: ");
  200.     for(var item : int = 0; item < openSet.Count; item++){
  201.         print(openSet[item].getPos());
  202.     }
  203.     targetNode = goals[mainCounter];
  204.     if(openSet[openSet.Count-1].getPos().x != targetNode.getPos().x){
  205.         openSet[openSet.Count-1].setPos(targetNode.getPos());
  206.     }
  207.     print("TARGETTT: " + targetNode.getPos());
  208.     while(Vector3.Distance(mon.position, targetNode.getPos()) > 0.1){
  209.         target = openSet[subCounter];
  210.         mon.LookAt(targetNode.getPos());
  211.         //mon.Rotate(-90);
  212.         //mon.localRotation.eulerAngles = Vector3(0,rotation,0);
  213.         //while(mon. <= Mathf.Atan((target.getPos().z - mon.position.z) / (target.getPos().x - mon.position.x)) * (180 / Mathf.PI)){
  214.         print(Vector3.Distance(mon.position, target.getPos()));
  215.         while(Vector3.Distance(mon.position, target.getPos()) > 0.1){
  216.             mon.Translate(0, 0, Time.deltaTime * 6);
  217.             print(mon.position + "    " + target.getPos());
  218.             yield;
  219.         }
  220.         print("LOOP MAIN");
  221.         subCounter++;
  222.         yield;
  223.     }
  224.     print("BREAK BREAK BREAK BREAK BREAK");
  225.     mainCounter++;
  226.     refineMap(targetNode, goals[mainCounter]);
  227. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement