Advertisement
Guest User

Untitled

a guest
Feb 21st, 2018
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.52 KB | None | 0 0
  1.  
  2. int moveCostG(int x, int y, int dx, int dy) {
  3. int g = (abs(dx - x) + abs(dy - y));
  4. return g;
  5. }
  6.  
  7. int moveCostH(int dx, int dy, int nx, int ny){
  8. int h = (abs(nx - dx) + abs(ny - dy));
  9. return h;
  10. }
  11.  
  12. int moveCostF(int x, int y, int dx, int dy, int nx, int ny) {
  13. return (moveCostG(x, y, dx, dy) + moveCostH(dx, dy, nx, ny));
  14. }
  15.  
  16. bool searchList(std::vector<coordList> n_list, int search_term_x, int search_term_y) {
  17. int size = sizeof(n_list)/sizeof(coordList);
  18. for (i = 0; i < size; i++) {
  19. if ((n_list[i].x == search_term_x) && (n_list[i].y == search_term_y)){
  20. return true;
  21. }
  22. }
  23. return false;
  24. }
  25.  
  26. void pathfinding(int x, int y, int nx, int ny) {
  27. int size;
  28. int dx = x;
  29. int dy = y;
  30. bool found = false;
  31. int boolMap[MAX_X][MAX_Y];
  32. for (i = 0; i < MAX_X; i++) {
  33. for (j = 0; j < MAX_Y; j++) {
  34. if (!map[i][j].layer1.passable || !map[i][j].layer2.passable) {
  35. boolMap[i][j] = 1;
  36. }
  37. else {
  38. boolMap[i][j] = 0;
  39. }
  40. }
  41. }
  42. /*
  43. for (i = 0; i < MAX_X; i++) {
  44. for (j = 0; j < MAX_Y; j++) {
  45. printf("%d", boolMap[i][j]);
  46. }
  47. printf("\n");
  48. }
  49. */
  50.  
  51.  
  52.  
  53.  
  54. //Creating lists
  55. std::vector<coordList> openList;
  56. std::vector<coordList> closedList;
  57.  
  58. //Adding starting location to closed list
  59. coordList current;
  60. current.x = x;
  61. current.y = y;
  62. current.f = moveCostF(x, y, dx, dy, nx, ny);
  63. openList.push_back(current);
  64. closedList.push_back(current);
  65. map[current.x][current.y].layer1.colour = TCODColor::purple;
  66.  
  67. //Adding next doors
  68. current.x = dx + 1;
  69. current.y = dy;
  70. current.f = moveCostF(x, y, dx + 1, dy, nx, ny);
  71. if (boolMap[current.x][current.y] == 0){
  72. if (searchList(closedList, current.x, current.y) == true) {
  73. //found in closed -- ignore it
  74. }
  75. else if (searchList(openList, current.x, current.y) == false){
  76. openList.push_back(current);
  77. map[current.x][current.y].layer1.colour = TCODColor::red;
  78. }
  79. else {
  80. //If T is already in the open list: Check if the F score
  81. //is lower when we use the current generated path to get there.
  82. //If it is, update its score and update its parent as well.
  83. //????
  84. //openList.push_back(current);
  85. }
  86. }
  87.  
  88. current.x = dx - 1;
  89. current.y = dy;
  90. current.f = moveCostF(x, y, dx - 1, dy, nx, ny);
  91.  
  92. if (boolMap[current.x][current.y] == 0){
  93. if (searchList(closedList, current.x, current.y) == true) {
  94. //found in closed -- ignore it
  95. }
  96. else if (searchList(openList, current.x, current.y) == false){
  97. openList.push_back(current);
  98. map[current.x][current.y].layer1.colour = TCODColor::red;
  99. }
  100. else {
  101. //If T is already in the open list: Check if the F score
  102. //is lower when we use the current generated path to get there.
  103. //If it is, update its score and update its parent as well.
  104. //????
  105. //openList.push_back(current);
  106. }
  107. }
  108.  
  109. current.x = dx;
  110. current.y = dy + 1;
  111. current.f = moveCostF(x, y, dx, dy + 1, nx, ny);
  112.  
  113. if (boolMap[current.x][current.y] == 0){
  114. if (searchList(closedList, current.x, current.y) == true) {
  115. //found in closed -- ignore it
  116. }
  117. else if (searchList(openList, current.x, current.y) == false){
  118. openList.push_back(current);
  119. map[current.x][current.y].layer1.colour = TCODColor::red;
  120. }
  121. else {
  122. //If T is already in the open list: Check if the F score
  123. //is lower when we use the current generated path to get there.
  124. //If it is, update its score and update its parent as well.
  125. //????
  126. //openList.push_back(current);
  127. }
  128. }
  129.  
  130. current.x = dx;
  131. current.y = dy - 1;
  132. current.f = moveCostF(x, y, dx, dy - 1, nx, ny);
  133.  
  134. if (boolMap[current.x][current.y] == 0){
  135. if (searchList(closedList, current.x, current.y) == true) {
  136. //found in closed -- ignore it
  137. }
  138. else if (searchList(openList, current.x, current.y) == false){
  139. openList.push_back(current);
  140. map[current.x][current.y].layer1.colour = TCODColor::red;
  141. }
  142. else {
  143. //If T is already in the open list: Check if the F score
  144. //is lower when we use the current generated path to get there.
  145. //If it is, update its score and update its parent as well.
  146. //????
  147. //openList.push_back(current);
  148. }
  149. }
  150. printf("Assigning initial variables");
  151. getchar();
  152. DisplayMapTest();
  153. openList.erase(openList.begin());
  154.  
  155. //--- NOW ALL INITIAL SQUARES HAVE BEEN CALCULATED
  156. int counter = 0;
  157. bool first_time = true;
  158. //While?
  159. while (found == false) {
  160. if (first_time == false) {
  161.  
  162. //Add new walkable tiles
  163. current.x = dx + 1;
  164. current.y = dy;
  165. current.f = moveCostF(x, y, dx + 1, dy, nx, ny);
  166. if (boolMap[current.x][current.y] == 0){
  167. if (searchList(closedList, current.x, current.y) == true) {
  168. //found in closed -- ignore it
  169. }
  170. else if (searchList(openList, current.x, current.y) == false){
  171. openList.push_back(current);
  172. map[current.x][current.y].layer1.colour = TCODColor::red;
  173. }
  174. else {
  175. //If T is already in the open list: Check if the F score
  176. //is lower when we use the current generated path to get there.
  177. //If it is, update its score and update its parent as well.
  178. //????
  179. //openList.push_back(current);
  180. }
  181. }
  182.  
  183. current.x = dx - 1;
  184. current.y = dy;
  185. current.f = moveCostF(x, y, dx - 1, dy, nx, ny);
  186.  
  187. if (boolMap[current.x][current.y] == 0){
  188. if (searchList(closedList, current.x, current.y) == true) {
  189. //found in closed -- ignore it
  190. }
  191. else if (searchList(openList, current.x, current.y) == false){
  192. openList.push_back(current);
  193. map[current.x][current.y].layer1.colour = TCODColor::red;
  194. }
  195. else {
  196. //If T is already in the open list: Check if the F score
  197. //is lower when we use the current generated path to get there.
  198. //If it is, update its score and update its parent as well.
  199. //????
  200. //openList.push_back(current);
  201. }
  202. }
  203.  
  204. current.x = dx;
  205. current.y = dy + 1;
  206. current.f = moveCostF(x, y, dx, dy + 1, nx, ny);
  207.  
  208. if (boolMap[current.x][current.y] == 0){
  209. if (searchList(closedList, current.x, current.y) == true) {
  210. //found in closed -- ignore it
  211. }
  212. else if (searchList(openList, current.x, current.y) == false){
  213. openList.push_back(current);
  214. map[current.x][current.y].layer1.colour = TCODColor::red;
  215. }
  216. else {
  217. //If T is already in the open list: Check if the F score
  218. //is lower when we use the current generated path to get there.
  219. //If it is, update its score and update its parent as well.
  220. //????
  221. //openList.push_back(current);
  222. }
  223. }
  224.  
  225. current.x = dx;
  226. current.y = dy - 1;
  227. current.f = moveCostF(x, y, dx, dy - 1, nx, ny);
  228.  
  229. if (boolMap[current.x][current.y] == 0){
  230. if (searchList(closedList, current.x, current.y) == true) {
  231. //found in closed -- ignore it
  232. }
  233. else if (searchList(openList, current.x, current.y) == false){
  234. openList.push_back(current);
  235. map[current.x][current.y].layer1.colour = TCODColor::red;
  236. }
  237. else {
  238. //If T is already in the open list: Check if the F score
  239. //is lower when we use the current generated path to get there.
  240. //If it is, update its score and update its parent as well.
  241. //????
  242. //openList.push_back(current);
  243. }
  244. }
  245.  
  246. }
  247. printf("Adding new to open list");
  248. getchar();
  249. DisplayMapTest();
  250. first_time = false;
  251. size = sizeof(openList)/sizeof(coordList);
  252. printf("Current X, D, N\n");
  253. printf("X: %d, Y: %d\n", x, y);
  254. printf("DX: %d, DY: %d\n", dx, dy);
  255. printf("NX: %d, NY: %d\n\n", nx, ny);
  256. int lowest = 999999999;
  257. int lowest_place;
  258. printf("Open list tiles and scores (size: %d)\n", size);
  259. for (i = 0; i < size + 1; i++) {
  260. if ((openList[i].f < lowest) && ((abs(openList[i].x - dx) == 1) || (abs(openList[i].y - dy) == 1))){
  261. lowest = openList[i].f;
  262. lowest_place = i;
  263. }
  264. printf("X:%d Y:%d: F:%d\n", openList[i].x, openList[i].y, openList[i].f);
  265. }
  266. printf("\nLowest score\n");
  267. printf("X:%d Y:%d: F:%d\n", openList[lowest_place].x, openList[lowest_place].y, openList[lowest_place].f);
  268. closedList.push_back(openList[lowest_place]);
  269. map[openList[lowest_place].x][openList[lowest_place].y].layer1.colour = TCODColor::purple;
  270. dx = openList[lowest_place].x;
  271. dy = openList[lowest_place].y;
  272. if ((dx == nx) && (dy == ny)) {
  273. printf("FOUND IT!");
  274. found = true;
  275. break;
  276. }
  277. openList.erase(openList.begin() + lowest_place);
  278. printf("Removed lowest one and added to closed list ");
  279. getchar();
  280. DisplayMapTest();
  281.  
  282. printf("END");
  283. getchar();
  284. DisplayMapTest();
  285.  
  286. counter++;
  287. if (counter == 10) {
  288. break;
  289. }
  290. }
  291.  
  292. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement