DijkstraMagic.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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 = 18;
  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][15];
  15. setup();
  16. printWeightMap();
  17. for(int i = 0; i < towns.size(); i++) {
  18. doMagicforTown(i);
  19. }
  20. printDijkstraResult();
  21. }
  22. public ArrayList<ArrayList<SaleRoute>> getSalesPitch() {
  23. return salesPitch;
  24. }
  25. private void doMagicforTown(int fromIndex) {
  26. if(fromIndex != 0) {
  27. resetWeightMap();
  28. }
  29. Coord origin = towns.get(fromIndex);
  30. int posX = origin.getX(), posY = origin.getY();
  31. weightMap[posX][posY].setCostSoFar(0);
  32. weightMap[posX][posY].setQAdded(true);
  33. ArrayList<Coord> tileQ = new ArrayList<>();
  34. tileQ.add(new Coord(posX, posY));
  35. while(tileQ.size() > 0) {
  36. int leastCost = Integer.MAX_VALUE;
  37. int leastIndex = -1;
  38. for(int i = 0; i < tileQ.size(); i++) {
  39. int iX = tileQ.get(i).getX();
  40. int iY = tileQ.get(i).getY();
  41. if(weightMap[iX][iY].getCostSoFar() < leastCost) {
  42. leastCost = weightMap[iX][iY].getCostSoFar();
  43. leastIndex = i;
  44. }
  45. }
  46. dijkstraOnTile(tileQ.get(leastIndex).getX(), tileQ.get(leastIndex).getY(), tileQ, leastIndex);
  47. }
  48. makeListForPitch(fromIndex);
  49. //printDijkstraMap();
  50. }
  51. private void makeListForPitch(int fromIndex) {
  52. ArrayList<SaleRoute> listForPitch = new ArrayList<>();
  53. for(int i = fromIndex+1; i < towns.size(); i++) {
  54. ArrayList<Coord> listForRoute = new ArrayList<>();
  55. Coord curPos = new Coord(towns.get(i).getX(), towns.get(i).getY());
  56. int totalCost = weightMap[curPos.getX()][curPos.getY()].getCostSoFar();
  57. while(true) {
  58. char dir = weightMap[curPos.getX()][curPos.getY()].getPrevOfPath();
  59. listForRoute.add(new Coord(curPos.getX(), curPos.getY()));
  60. if(dir == 'n') {
  61. curPos.changeY(-1);
  62. }
  63. else if(dir == 'e') {
  64. curPos.changeX(1);
  65. }
  66. else if(dir == 's') {
  67. curPos.changeY(1);
  68. }
  69. else if(dir == 'w') {
  70. curPos.changeX(-1);
  71. }
  72. else {
  73. break;
  74. }
  75. }
  76. listForPitch.add(new SaleRoute(listForRoute, totalCost));
  77. }
  78. salesPitch.add(listForPitch);
  79. }
  80. private void dijkstraOnTile(int posX, int posY, ArrayList<Coord> tileQ, int qIndex) {
  81. int prevCost = weightMap[posX][posY].getCostSoFar();
  82. if(posX < weightMap.length-1) {
  83. checkNeighbor(posX + 1, posY, prevCost, 'w');
  84. }
  85. if(posX > 0) {
  86. checkNeighbor(posX - 1, posY, prevCost, 'e');
  87. }
  88. if(posY < weightMap[0].length-1) {
  89. checkNeighbor(posX, posY + 1, prevCost, 'n');
  90. }
  91. if(posY > 0) {
  92. checkNeighbor(posX, posY - 1, prevCost, 's');
  93. }
  94. if(weightMap[posX][posY].hasExtraPaths()) {
  95. //TODO: implement extra path stuff
  96. }
  97. tileQ.remove(qIndex);
  98. if(posX < weightMap.length-1) {
  99. if(!weightMap[posX+1][posY].isQAdded()) {
  100. weightMap[posX+1][posY].setQAdded(true);
  101. tileQ.add(new Coord(posX+1, posY));
  102. }
  103. }
  104. if(posX > 0) {
  105. if(!weightMap[posX-1][posY].isQAdded()) {
  106. weightMap[posX-1][posY].setQAdded(true);
  107. tileQ.add(new Coord(posX-1, posY));
  108. }
  109. }
  110. if(posY < weightMap[0].length-1) {
  111. if(!weightMap[posX][posY+1].isQAdded()) {
  112. weightMap[posX][posY+1].setQAdded(true);
  113. tileQ.add(new Coord(posX, posY+1));
  114. }
  115. }
  116. if(posY > 0) {
  117. if(!weightMap[posX][posY-1].isQAdded()) {
  118. weightMap[posX][posY-1].setQAdded(true);
  119. tileQ.add(new Coord(posX, posY-1));
  120. }
  121. }
  122. if(weightMap[posX][posY].hasExtraPaths()) {
  123. //TODO: implement extra path stuff
  124. }
  125. }
  126. private void checkNeighbor(int posX, int posY, int prevCost, char origin) {
  127. int newCost = prevCost + weightMap[posX][posY].getWeight();
  128. if(newCost < weightMap[posX][posY].getCostSoFar()) {
  129. weightMap[posX][posY].setCostSoFar(newCost);
  130. weightMap[posX][posY].setPrevOfPath(origin);
  131. }
  132. }
  133. private void resetWeightMap() {
  134. for(int x = 0; x < weightMap.length; x++) {
  135. for (int y = 0; y < weightMap[0].length; y++) {
  136. weightMap[x][y].setCostSoFar(Integer.MAX_VALUE);
  137. weightMap[x][y].setQAdded(false);
  138. weightMap[x][y].setPrevOfPath('0');
  139. }
  140. }
  141. }
  142. private void setup() {
  143. //find towns
  144. //mock implementation
  145. /*
  146. //test start
  147. towns.add(new Coord(0, 0));
  148. towns.add(new Coord(2, 3));
  149. towns.add(new Coord(4, 1));
  150. towns.add(new Coord(0, 4));
  151. towns.add(new Coord(4, 7));
  152. towns.add(new Coord(2, 5));
  153. towns.add(new Coord(3, 9));
  154. towns.add(new Coord(1, 8));
  155. //new test
  156. towns.add(new Coord(1, 3));
  157. towns.add(new Coord(1, 5));
  158. towns.add(new Coord(1, 14));
  159. towns.add(new Coord(0, 10));
  160. //towns.add(new Coord(2, 8));
  161. //towns.add(new Coord(0, 2));
  162. //towns.add(new Coord(0, 5));
  163. //towns.add(new Coord(0, 14));
  164. //towns.add(new Coord(1, 0));
  165. //towns.add(new Coord(1, 7));
  166. //towns.add(new Coord(1, 9));
  167. //towns.add(new Coord(1, 11));*/
  168. /*weightMap[0][0] = new Node2D(1);
  169. weightMap[1][0] = new Node2D(1);
  170. weightMap[2][0] = new Node2D(1);
  171. weightMap[3][0] = new Node2D(5);
  172. weightMap[4][0] = new Node2D(5);
  173. weightMap[0][1] = new Node2D(1);
  174. weightMap[1][1] = new Node2D(10);
  175. weightMap[2][1] = new Node2D(1);
  176. weightMap[3][1] = new Node2D(5);
  177. weightMap[4][1] = new Node2D(1);
  178. weightMap[0][2] = new Node2D(1);
  179. weightMap[1][2] = new Node2D(10);
  180. weightMap[2][2] = new Node2D(1);
  181. weightMap[3][2] = new Node2D(1);
  182. weightMap[4][2] = new Node2D(1);
  183. weightMap[0][3] = new Node2D(1);
  184. weightMap[1][3] = new Node2D(1);
  185. weightMap[2][3] = new Node2D(1);
  186. weightMap[3][3] = new Node2D(1);
  187. weightMap[4][3] = new Node2D(5);
  188. weightMap[0][4] = new Node2D(1);
  189. weightMap[1][4] = new Node2D(1);
  190. weightMap[2][4] = new Node2D(1);
  191. weightMap[3][4] = new Node2D(5);
  192. weightMap[4][4] = new Node2D(5);
  193. weightMap[0][5] = new Node2D(1);
  194. weightMap[1][5] = new Node2D(1);
  195. weightMap[2][5] = new Node2D(1);
  196. weightMap[3][5] = new Node2D(5);
  197. weightMap[4][5] = new Node2D(5);
  198. weightMap[0][6] = new Node2D(1);
  199. weightMap[1][6] = new Node2D(10);
  200. weightMap[2][6] = new Node2D(1);
  201. weightMap[3][6] = new Node2D(5);
  202. weightMap[4][6] = new Node2D(1);
  203. weightMap[0][7] = new Node2D(1);
  204. weightMap[1][7] = new Node2D(1);
  205. weightMap[2][7] = new Node2D(1);
  206. weightMap[3][7] = new Node2D(1);
  207. weightMap[4][7] = new Node2D(1);
  208. weightMap[0][8] = new Node2D(1);
  209. weightMap[1][8] = new Node2D(1);
  210. weightMap[2][8] = new Node2D(1);
  211. weightMap[3][8] = new Node2D(1);
  212. weightMap[4][8] = new Node2D(5);
  213. weightMap[0][9] = new Node2D(1);
  214. weightMap[1][9] = new Node2D(1);
  215. weightMap[2][9] = new Node2D(1);
  216. weightMap[3][9] = new Node2D(1);
  217. weightMap[4][9] = new Node2D(5);
  218. weightMap[0][10] = new Node2D(1);
  219. weightMap[1][10] = new Node2D(5);
  220. weightMap[2][10] = new Node2D(1);
  221. weightMap[3][10] = new Node2D(5);
  222. weightMap[4][10] = new Node2D(5);
  223. weightMap[0][11] = new Node2D(1);
  224. weightMap[1][11] = new Node2D(1);
  225. weightMap[2][11] = new Node2D(1);
  226. weightMap[3][11] = new Node2D(5);
  227. weightMap[4][11] = new Node2D(1);
  228. weightMap[0][12] = new Node2D(1);
  229. weightMap[1][12] = new Node2D(10);
  230. weightMap[2][12] = new Node2D(1);
  231. weightMap[3][12] = new Node2D(1);
  232. weightMap[4][12] = new Node2D(1);
  233. weightMap[0][13] = new Node2D(1);
  234. weightMap[1][13] = new Node2D(10);
  235. weightMap[2][13] = new Node2D(1);
  236. weightMap[3][13] = new Node2D(1);
  237. weightMap[4][13] = new Node2D(5);
  238. weightMap[0][14] = new Node2D(1);
  239. weightMap[1][14] = new Node2D(1);
  240. weightMap[2][14] = new Node2D(1);
  241. weightMap[3][14] = new Node2D(5);
  242. weightMap[4][14] = new Node2D(5);*/
  243. //test end
  244. /*towns.add(new Coord(6, 10));
  245. towns.add(new Coord(30, 29));
  246. towns.add(new Coord(41, 20));
  247. towns.add(new Coord(42, 24));
  248. towns.add(new Coord(26, 41));
  249. towns.add(new Coord(39, 26));
  250. towns.add(new Coord(16, 8));
  251. towns.add(new Coord(6, 27));
  252. towns.add(new Coord(26, 7));
  253. towns.add(new Coord(28, 13));
  254. towns.add(new Coord(49, 4));
  255. towns.add(new Coord(34, 47));
  256. towns.add(new Coord(20, 20));
  257. towns.add(new Coord(43, 32));
  258. towns.add(new Coord(43, 21));
  259. towns.add(new Coord(3, 43));
  260. towns.add(new Coord(34, 8));
  261. towns.add(new Coord(30, 17));
  262. towns.add(new Coord(35, 49));
  263. towns.add(new Coord(28, 10));*/
  264. for(int x = 0; x < map.getWidth(); x++) {
  265. for(int y = 0; y < map.getHeight(); y++) {
  266. //actual implementation - find towns; townID needs to be set to correct value
  267. if(map.getTile(x, y).getId() == townID) {
  268. towns.add(new Coord(x, y));
  269. }
  270. //translate map to weightMap
  271. //TODO: boats
  272. PlayerAbilities test = new PlayerAbilities("", 0, 0, 0, 0, 0, 0, 0);
  273. weightMap[x][y] = new Node2D(map.getTile(x, y).getEnergyCost(test));
  274. }
  275. }
  276. }
  277. private void printDijkstraMap() {
  278. for(int y = 0; y < weightMap[0].length; y++) {
  279. for(int x = 0; x < weightMap.length; x++) {
  280. int cost = weightMap[x][y].getCostSoFar();
  281. if(cost == Integer.MAX_VALUE) {
  282. System.out.print("- ");
  283. }
  284. else {
  285. System.out.print(cost + " ");
  286. }
  287. }
  288. System.out.println();
  289. }
  290. System.out.println();
  291. }
  292. private void printDijkstraResult() {
  293. System.out.println();
  294. for(int origNum = 0; origNum < salesPitch.size(); origNum++) {
  295. for (int lNum = 0; lNum < salesPitch.get(origNum).size(); lNum++) {
  296. System.out.print("Total Cost: " + salesPitch.get(origNum).get(lNum).getTotalCost() + " ");
  297. for (int lInd = 0; lInd < salesPitch.get(origNum).get(lNum).getPath().size(); lInd++) {
  298. System.out.print(salesPitch.get(origNum).get(lNum).getPath().get(lInd).getX() + "/" + salesPitch.get(origNum).get(lNum).getPath().get(lInd).getY() + " ");
  299. }
  300. System.out.println();
  301. }
  302. System.out.println();
  303. }
  304. System.out.println();
  305. }
  306. private void printWeightMap() {
  307. for(int y = 0; y < weightMap[0].length; y++) {
  308. for(int x = 0; x < weightMap.length; x++) {
  309. System.out.print(weightMap[x][y].getWeight() + " ");
  310. }
  311. System.out.println();
  312. }
  313. System.out.println();
  314. }
  315. }