Advertisement
Guest User

Untitled

a guest
Aug 27th, 2015
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.02 KB | None | 0 0
  1. AStarSearch(startWp, goalWp)
  2. {
  3. pQOpen = [];
  4. pQSize = 0;
  5. closedList = [];
  6. listSize = 0;
  7. s = spawnstruct();
  8. s.g = 0; //start node
  9. s.h = distance(level.waypoints[startWp].origin, level.waypoints[goalWp].origin);
  10. s.f = s.g + s.h;
  11. s.wpIdx = startWp;
  12. s.parent = spawnstruct();
  13. s.parent.wpIdx = -1;
  14.  
  15. PQPush(pQOpen, s, pQSize); //push s on Open
  16. pQSize++;
  17.  
  18. //while Open is not empty
  19. while(!PQIsEmpty(pQOpen, pQSize))
  20. {
  21. //pop node n from Open // n has the lowest f
  22. n = PQPop(pQOpen, pQSize);
  23. pQSize--;
  24.  
  25. //if n is a goal node; construct path, return success
  26. if(n.wpIdx == goalWp)
  27. {
  28.  
  29. x = n;
  30. for(z = 0; z < 100; z++)
  31. {
  32. parent = x.parent;
  33. if(parent.wpIdx == -1)
  34. {
  35. return;
  36. }
  37. line(level.waypoints[x.wpIdx].origin, level.waypoints[parent.wpIdx].origin, (0,1,0));
  38. x = parent;
  39. }
  40.  
  41. return;
  42. }
  43.  
  44. //for each successor nc of n
  45. for(i = 0; i < level.waypoints[n.wpIdx].childCount; i++)
  46. {
  47. //newg = n.g + cost(n,nc)
  48. newg = n.g + distance(level.waypoints[n.wpIdx].origin, level.waypoints[level.waypoints[n.wpIdx].children[i]].origin);
  49.  
  50. //if nc is in Open or Closed, and nc.g <= newg then skip
  51. if(PQExists(pQOpen, level.waypoints[n.wpIdx].children[i], pQSize))
  52. {
  53. nc = PQGet(pQOpen, level.waypoints[n.wpIdx].children[i], pQSize);
  54.  
  55. if(nc.g <= newg)
  56. {
  57. continue;
  58. }
  59. }
  60. else
  61. if(ListExists(closedList, level.waypoints[n.wpIdx].children[i], listSize))
  62. {
  63. nc = ListGet(closedList, level.waypoints[n.wpIdx].children[i], listSize);
  64.  
  65. if(nc.g <= newg)
  66. {
  67. continue;
  68. }
  69. }
  70.  
  71. // nc.parent = n
  72. // nc.g = newg
  73. // nc.h = GoalDistEstimate( nc )
  74. // nc.f = nc.g + nc.h
  75.  
  76. nc = spawnstruct();
  77. nc.parent = spawnstruct();
  78. nc.parent = n;
  79. Rendflex: ... yeah
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement