DijkstraMagic.java 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. package pathgame.algorithm;
  2. import pathgame.gameplay.PlayerAbilities;
  3. import pathgame.tilemap.TileMap;
  4. import java.util.ArrayList;
  5. public class DijkstraMagic {
  6. private int townID = -1;
  7. private ArrayList<Coord> towns = new ArrayList<>();
  8. private TileMap map;
  9. private Node2D[][] weightMap;
  10. private ArrayList<ArrayList<SaleRoute>> salesPitch = new ArrayList<>();
  11. public DijkstraMagic(TileMap map) {
  12. this.map = map;
  13. //weightMap = new Node2D[map.getWidth()][map.getHeight()];
  14. weightMap = new Node2D[5][5];
  15. setup();
  16. for(int i = 0; i < towns.size(); i++) {
  17. doMagicforTown(i);
  18. }
  19. printDijkstraResult();
  20. }
  21. private void doMagicforTown(int fromIndex) {
  22. if(fromIndex != 0) {
  23. resetWeightMap();
  24. }
  25. Coord origin = towns.get(fromIndex);
  26. int posX = origin.getX(), posY = origin.getY();
  27. weightMap[posX][posY].setCostSoFar(0);
  28. weightMap[posX][posY].setQAdded(true);
  29. ArrayList<Coord> tileQ = new ArrayList<>();
  30. tileQ.add(new Coord(posX, posY));
  31. while(tileQ.size() > 0) {
  32. int leastCost = Integer.MAX_VALUE;
  33. int leastIndex = -1;
  34. for(int i = 0; i < tileQ.size(); i++) {
  35. int iX = tileQ.get(i).getX();
  36. int iY = tileQ.get(i).getY();
  37. if(weightMap[iX][iY].getCostSoFar() < leastCost) {
  38. leastCost = weightMap[iX][iY].getCostSoFar();
  39. leastIndex = i;
  40. }
  41. }
  42. dijkstraOnTile(tileQ.get(leastIndex).getX(), tileQ.get(leastIndex).getY(), tileQ, leastIndex);
  43. }
  44. makeListForPitch(fromIndex);
  45. printDijkstraMap();
  46. }
  47. private void makeListForPitch(int fromIndex) {
  48. ArrayList<SaleRoute> listForPitch = new ArrayList<>();
  49. for(int i = fromIndex+1; i < towns.size(); i++) {
  50. ArrayList<Coord> listForRoute = new ArrayList<>();
  51. Coord curPos = new Coord(towns.get(i).getX(), towns.get(i).getY());
  52. int totalCost = weightMap[curPos.getX()][curPos.getY()].getCostSoFar();
  53. while(true) {
  54. char dir = weightMap[curPos.getX()][curPos.getY()].getPrevOfPath();
  55. listForRoute.add(new Coord(curPos.getX(), curPos.getY()));
  56. if(dir == 'n') {
  57. curPos.changeY(-1);
  58. }
  59. else if(dir == 'e') {
  60. curPos.changeX(1);
  61. }
  62. else if(dir == 's') {
  63. curPos.changeY(1);
  64. }
  65. else if(dir == 'w') {
  66. curPos.changeX(-1);
  67. }
  68. else {
  69. break;
  70. }
  71. }
  72. listForPitch.add(new SaleRoute(listForRoute, totalCost));
  73. }
  74. salesPitch.add(listForPitch);
  75. }
  76. private void dijkstraOnTile(int posX, int posY, ArrayList<Coord> tileQ, int qIndex) {
  77. int prevCost = weightMap[posX][posY].getCostSoFar();
  78. if(posX < weightMap.length-1) {
  79. checkNeighbor(posX + 1, posY, prevCost, 'w');
  80. }
  81. if(posX > 0) {
  82. checkNeighbor(posX - 1, posY, prevCost, 'e');
  83. }
  84. if(posY < weightMap[0].length-1) {
  85. checkNeighbor(posX, posY + 1, prevCost, 'n');
  86. }
  87. if(posY > 0) {
  88. checkNeighbor(posX, posY - 1, prevCost, 's');
  89. }
  90. if(weightMap[posX][posY].hasExtraPaths()) {
  91. //TODO: implement extra path stuff
  92. }
  93. tileQ.remove(qIndex);
  94. if(posX < weightMap.length-1) {
  95. if(!weightMap[posX+1][posY].isQAdded()) {
  96. weightMap[posX+1][posY].setQAdded(true);
  97. tileQ.add(new Coord(posX+1, posY));
  98. }
  99. }
  100. if(posX > 0) {
  101. if(!weightMap[posX-1][posY].isQAdded()) {
  102. weightMap[posX-1][posY].setQAdded(true);
  103. tileQ.add(new Coord(posX-1, posY));
  104. }
  105. }
  106. if(posY < weightMap[0].length-1) {
  107. if(!weightMap[posX][posY+1].isQAdded()) {
  108. weightMap[posX][posY+1].setQAdded(true);
  109. tileQ.add(new Coord(posX, posY+1));
  110. }
  111. }
  112. if(posY > 0) {
  113. if(!weightMap[posX][posY-1].isQAdded()) {
  114. weightMap[posX][posY-1].setQAdded(true);
  115. tileQ.add(new Coord(posX, posY-1));
  116. }
  117. }
  118. if(weightMap[posX][posY].hasExtraPaths()) {
  119. //TODO: implement extra path stuff
  120. }
  121. }
  122. private void checkNeighbor(int posX, int posY, int prevCost, char origin) {
  123. int newCost = prevCost + weightMap[posX][posY].getWeight();
  124. if(newCost < weightMap[posX][posY].getCostSoFar()) {
  125. weightMap[posX][posY].setCostSoFar(newCost);
  126. weightMap[posX][posY].setPrevOfPath(origin);
  127. }
  128. }
  129. private void resetWeightMap() {
  130. for(int x = 0; x < weightMap.length; x++) {
  131. for (int y = 0; y < weightMap[0].length; y++) {
  132. weightMap[x][y].setCostSoFar(Integer.MAX_VALUE);
  133. weightMap[x][y].setQAdded(false);
  134. weightMap[x][y].setPrevOfPath('0');
  135. }
  136. }
  137. }
  138. private void setup() {
  139. //find towns
  140. //mock implementation
  141. //test start
  142. towns.add(new Coord(0, 0));
  143. towns.add(new Coord(2, 3));
  144. towns.add(new Coord(4, 1));
  145. weightMap[0][0] = new Node2D(1);
  146. weightMap[1][0] = new Node2D(5);
  147. weightMap[2][0] = new Node2D(1);
  148. weightMap[3][0] = new Node2D(5);
  149. weightMap[4][0] = new Node2D(5);
  150. weightMap[0][1] = new Node2D(1);
  151. weightMap[1][1] = new Node2D(10);
  152. weightMap[2][1] = new Node2D(1);
  153. weightMap[3][1] = new Node2D(5);
  154. weightMap[4][1] = new Node2D(1);
  155. weightMap[0][2] = new Node2D(1);
  156. weightMap[1][2] = new Node2D(10);
  157. weightMap[2][2] = new Node2D(1);
  158. weightMap[3][2] = new Node2D(1);
  159. weightMap[4][2] = new Node2D(1);
  160. weightMap[0][3] = new Node2D(1);
  161. weightMap[1][3] = new Node2D(10);
  162. weightMap[2][3] = new Node2D(1);
  163. weightMap[3][3] = new Node2D(1);
  164. weightMap[4][3] = new Node2D(5);
  165. weightMap[0][4] = new Node2D(1);
  166. weightMap[1][4] = new Node2D(1);
  167. weightMap[2][4] = new Node2D(1);
  168. weightMap[3][4] = new Node2D(5);
  169. weightMap[4][4] = new Node2D(5);
  170. //test end
  171. /*towns.add(new Coord(6, 10));
  172. towns.add(new Coord(30, 29));
  173. towns.add(new Coord(41, 20));
  174. towns.add(new Coord(42, 24));
  175. towns.add(new Coord(26, 41));
  176. towns.add(new Coord(39, 26));
  177. towns.add(new Coord(16, 8));
  178. towns.add(new Coord(6, 27));
  179. towns.add(new Coord(26, 7));
  180. towns.add(new Coord(28, 13));
  181. towns.add(new Coord(49, 4));
  182. towns.add(new Coord(34, 47));
  183. towns.add(new Coord(20, 20));
  184. towns.add(new Coord(43, 32));
  185. towns.add(new Coord(43, 21));
  186. towns.add(new Coord(3, 43));
  187. towns.add(new Coord(34, 8));
  188. towns.add(new Coord(30, 17));
  189. towns.add(new Coord(35, 49));
  190. towns.add(new Coord(28, 10));*/
  191. /*for(int x = 0; x < map.getWidth(); x++) {
  192. for(int y = 0; y < map.getHeight(); y++) {
  193. //actual implementation - find towns; townID needs to be set to correct value
  194. /*if(map.getTile(x, y).getId() == townID) {
  195. towns.add(new Coord(x, y));
  196. }*/
  197. //translate map to weightMap
  198. //TODO: boats
  199. /*PlayerAbilities test = new PlayerAbilities(0, 0, 0, 0, 0, 0, 0);
  200. weightMap[x][y] = new Node2D(map.getTile(x, y).getEnergyCost(test));
  201. }
  202. }*/
  203. for(int y = 0; y < weightMap[0].length; y++) {
  204. for(int x = 0; x < weightMap.length; x++) {
  205. System.out.print(weightMap[x][y].getWeight() + " ");
  206. }
  207. System.out.println();
  208. }
  209. System.out.println();
  210. }
  211. private void printDijkstraMap() {
  212. for(int y = 0; y < weightMap[0].length; y++) {
  213. for(int x = 0; x < weightMap.length; x++) {
  214. int cost = weightMap[x][y].getCostSoFar();
  215. if(cost == Integer.MAX_VALUE) {
  216. System.out.print("- ");
  217. }
  218. else {
  219. System.out.print(cost + " ");
  220. }
  221. }
  222. System.out.println();
  223. }
  224. System.out.println();
  225. }
  226. private void printDijkstraResult() {
  227. System.out.println();
  228. for(int origNum = 0; origNum < salesPitch.size(); origNum++) {
  229. for (int lNum = 0; lNum < salesPitch.get(origNum).size(); lNum++) {
  230. System.out.print("Total Cost: " + salesPitch.get(origNum).get(lNum).getTotalCost() + " ");
  231. for (int lInd = 0; lInd < salesPitch.get(origNum).get(lNum).getPath().size(); lInd++) {
  232. System.out.print(salesPitch.get(origNum).get(lNum).getPath().get(lInd).getX() + "/" + salesPitch.get(origNum).get(lNum).getPath().get(lInd).getY() + " ");
  233. }
  234. System.out.println();
  235. }
  236. System.out.println();
  237. }
  238. System.out.println();
  239. }
  240. }