DijkstraMagic.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. package pathgame.algorithm;
  2. import pathgame.tilemap.TileMap;
  3. import pathgame.tilemap.TileType;
  4. import pathgame.tilemap.Tiles;
  5. import java.util.ArrayList;
  6. import pathgame.gameplay.Player;
  7. /** Class that takes a TileMap, finds all towns in it and generates a table of shortest paths between each town using the Dijkstra algorithm
  8. *
  9. */
  10. public class DijkstraMagic {
  11. private int TOWN_ID = Tiles.TOWN.getId();
  12. private int START_ID = Tiles.HOME_TOWN.getId();
  13. private int PORT_ID = Tiles.PORT.getId();
  14. private int WATER_WEIGHT;
  15. private ArrayList<Coord> towns = new ArrayList<>();
  16. private ArrayList<Coord> ports = new ArrayList<>();
  17. private TileMap map;
  18. private Node2D[][] weightMap;
  19. private ArrayList<ArrayList<SaleRoute>> salesPitch = new ArrayList<>();
  20. /** Generates a table of shortest paths between each town in the given TileMap
  21. *
  22. * @param map TileMap that the algorithm should use
  23. * @param player that is traversing the map
  24. */
  25. public DijkstraMagic(TileMap map, Player player) {
  26. this.map = map;
  27. weightMap = new Node2D[map.getWidth()][map.getHeight()];
  28. setup(player);
  29. //printWeightMap();
  30. for(int i = 0; i < ports.size(); i++) {
  31. doMagic(i, false);
  32. }
  33. for(int i = 0; i < towns.size(); i++) {
  34. doMagic(i, true);
  35. }
  36. //printDijkstraResult();
  37. }
  38. /** Returns the generated table of Dijkstra results
  39. *
  40. * @return generated table of Dijkstra results
  41. */
  42. public ArrayList<ArrayList<SaleRoute>> getSalesPitch() {
  43. return salesPitch;
  44. }
  45. private void doMagic(int fromIndex, boolean forTown) {
  46. resetWeightMap();
  47. Coord origin;
  48. if(forTown) {
  49. origin = towns.get(fromIndex);
  50. }
  51. else {
  52. origin = ports.get(fromIndex);
  53. }
  54. int posX = origin.getX(), posY = origin.getY();
  55. weightMap[posX][posY].setCostSoFar(0);
  56. weightMap[posX][posY].setQAdded(true);
  57. ArrayList<Coord> tileQ = new ArrayList<>();
  58. tileQ.add(new Coord(posX, posY));
  59. while(tileQ.size() > 0) {
  60. int leastCost = Integer.MAX_VALUE;
  61. int leastIndex = -1;
  62. for(int i = 0; i < tileQ.size(); i++) {
  63. int iX = tileQ.get(i).getX();
  64. int iY = tileQ.get(i).getY();
  65. if(weightMap[iX][iY].getCostSoFar() < leastCost) {
  66. leastCost = weightMap[iX][iY].getCostSoFar();
  67. leastIndex = i;
  68. }
  69. }
  70. Coord c = tileQ.get(leastIndex);
  71. if(weightMap[c.getX()][c.getY()].isBlocked() && forTown) {
  72. tileQ.remove(leastIndex);
  73. }
  74. else {
  75. dijkstraForTownOrPort(tileQ.get(leastIndex).getX(), tileQ.get(leastIndex).getY(), tileQ, leastIndex, forTown);
  76. }
  77. }
  78. if(forTown) {
  79. //printPathMap();
  80. //printPorts();
  81. makeListForPitch(fromIndex);
  82. }
  83. else {
  84. addPortsPaths(fromIndex);
  85. }
  86. //printDijkstraMap();
  87. }
  88. private void dijkstraForTownOrPort(int posX, int posY, ArrayList<Coord> tileQ, int qIndex, boolean forTown) {
  89. int prevCost = weightMap[posX][posY].getCostSoFar();
  90. if(posX < weightMap.length-1) {
  91. adjacent(posX+1, posY, prevCost, 'w', null, -1, tileQ, forTown);
  92. }
  93. if(posX > 0) {
  94. adjacent(posX-1, posY, prevCost, 'e', null, -1, tileQ, forTown);
  95. }
  96. if(posY < weightMap[0].length-1) {
  97. adjacent(posX, posY+1, prevCost, 'n', null, -1, tileQ, forTown);
  98. }
  99. if(posY > 0) {
  100. adjacent(posX, posY-1, prevCost, 's', null, -1, tileQ, forTown);
  101. }
  102. if(forTown) {
  103. if(weightMap[posX][posY].hasExtraPaths()) {
  104. ArrayList<ExtraPath> extraPaths = weightMap[posX][posY].getExtraPaths();
  105. for(int p = 0; p < extraPaths.size(); p++) {
  106. ExtraPath path = extraPaths.get(p);
  107. ArrayList<ExtraPath> foreingPaths = weightMap[path.getDestX()][path.getDestY()].getExtraPaths();
  108. int extraInd = -1;
  109. for(int forP = 0; forP < foreingPaths.size(); forP++) {
  110. ExtraPath fPath = foreingPaths.get(forP);
  111. if(fPath.getDestX() == posX && fPath.getDestY() == posY) {
  112. extraInd = forP;
  113. break;
  114. }
  115. }
  116. adjacent(path.getDestX(), path.getDestY(), prevCost, '\0', path, extraInd, tileQ, forTown);
  117. }
  118. }
  119. }
  120. tileQ.remove(qIndex);
  121. }
  122. private void adjacent(int posX, int posY, int prevCost, char dir, ExtraPath boatPath, int extraInd, ArrayList<Coord> tileQ, boolean forTown) {
  123. if(forTown) {
  124. if(!weightMap[posX][posY].isBlocked()) {
  125. adjacentCalc(posX, posY, prevCost, dir, boatPath, extraInd, tileQ, forTown);
  126. }
  127. }
  128. else {
  129. TileType type = weightMap[posX][posY].getType();
  130. if(type == TileType.SHALLOW_WATER || type == TileType.DEEP_WATER || type == TileType.PORT) {
  131. adjacentCalc(posX, posY, prevCost, dir, null, -1, tileQ, forTown);
  132. }
  133. }
  134. }
  135. private void adjacentCalc(int posX, int posY, int prevCost, char dir, ExtraPath extraPath, int extraInd, ArrayList<Coord> tileQ, boolean forTown) {
  136. int newCost = prevCost;
  137. if(forTown) {
  138. if(extraPath == null) {
  139. newCost += weightMap[posX][posY].getWeight();
  140. }
  141. else {
  142. newCost += extraPath.getPathWeight();
  143. }
  144. }
  145. else {
  146. newCost += WATER_WEIGHT;
  147. }
  148. if(newCost < weightMap[posX][posY].getCostSoFar()) {
  149. weightMap[posX][posY].setCostSoFar(newCost);
  150. if(extraPath == null) {
  151. weightMap[posX][posY].setPrevOfPath(dir);
  152. }
  153. else {
  154. weightMap[posX][posY].setPrevBoatPath(extraInd);
  155. }
  156. }
  157. if(!weightMap[posX][posY].isQAdded()) {
  158. weightMap[posX][posY].setQAdded(true);
  159. tileQ.add(new Coord(posX, posY));
  160. }
  161. }
  162. private void addPortsPaths(int fromIndex) {
  163. Coord origin = ports.get(fromIndex);
  164. for(int i = fromIndex+1; i < ports.size(); i++) {
  165. Coord nextPort = ports.get(i);
  166. if(weightMap[nextPort.getX()][nextPort.getY()].getCostSoFar() < Integer.MAX_VALUE) {
  167. //System.out.println("connecting ports " + fromIndex + " and " + i);
  168. int pathWeight = weightMap[nextPort.getX()][nextPort.getY()].getCostSoFar();
  169. ArrayList<Coord> pathCoords = new ArrayList<>();
  170. makePathCoords(false, new Coord(nextPort.getX(), nextPort.getY()), pathCoords);
  171. weightMap[origin.getX()][origin.getY()].addExtraPath(nextPort, pathWeight, pathCoords);
  172. weightMap[nextPort.getX()][nextPort.getY()].addExtraPath(origin, pathWeight, pathCoords);
  173. }
  174. }
  175. }
  176. private void makePathCoords(boolean forTown, Coord curPos, ArrayList<Coord> coordList) {
  177. while(true) {
  178. char dir = weightMap[curPos.getX()][curPos.getY()].getPrevOfPath();
  179. int extraInd = weightMap[curPos.getX()][curPos.getY()].getPrevBoatPath();
  180. if(forTown) {
  181. coordList.add(new Coord(curPos.getX(), curPos.getY()));
  182. }
  183. else {
  184. coordList.add(new Coord(curPos.getX(), curPos.getY(), true));
  185. }
  186. if(dir == 'n') {
  187. curPos.changeY(-1);
  188. }
  189. else if(dir == 'e') {
  190. curPos.changeX(1);
  191. }
  192. else if(dir == 's') {
  193. curPos.changeY(1);
  194. }
  195. else if(dir == 'w') {
  196. curPos.changeX(-1);
  197. }
  198. else if(extraInd != -1 && forTown) {
  199. ExtraPath extraPath = weightMap[curPos.getX()][curPos.getY()].getExtraPaths().get(extraInd);
  200. curPos.setCoord(extraPath.getDestX(), extraPath.getDestY());
  201. coordList.addAll(extraPath.getPathCoords());
  202. }
  203. else {
  204. break;
  205. }
  206. }
  207. if(!forTown) {
  208. coordList.remove(0);
  209. coordList.remove(coordList.size()-1);
  210. }
  211. }
  212. private void makeListForPitch(int fromIndex) {
  213. ArrayList<SaleRoute> listForPitch = new ArrayList<>();
  214. for(int i = fromIndex+1; i < towns.size(); i++) {
  215. ArrayList<Coord> listForRoute = new ArrayList<>();
  216. Coord curPos = new Coord(towns.get(i).getX(), towns.get(i).getY());
  217. int totalCost = weightMap[curPos.getX()][curPos.getY()].getCostSoFar();
  218. makePathCoords(true, curPos, listForRoute);
  219. listForPitch.add(new SaleRoute(listForRoute, totalCost));
  220. }
  221. salesPitch.add(listForPitch);
  222. }
  223. private void resetWeightMap() {
  224. for(int x = 0; x < weightMap.length; x++) {
  225. for (int y = 0; y < weightMap[0].length; y++) {
  226. weightMap[x][y].setCostSoFar(Integer.MAX_VALUE);
  227. weightMap[x][y].setQAdded(false);
  228. weightMap[x][y].setPrevOfPath('\0');
  229. weightMap[x][y].setPrevBoatPath(-1);
  230. }
  231. }
  232. }
  233. private void setup(Player player) {
  234. //find towns
  235. for(int x = 0; x < map.getWidth(); x++) {
  236. for(int y = 0; y < map.getHeight(); y++) {
  237. weightMap[x][y] = new Node2D(map.getTile(x, y).getEnergyCost(player));
  238. weightMap[x][y].setType(map.getTile(x, y).getType());
  239. if(map.getTile(x, y).getId() == TOWN_ID || map.getTile(x, y).getId() == START_ID) {
  240. towns.add(new Coord(x, y));
  241. weightMap[x][y].setTown(true);
  242. }
  243. else if(map.getTile(x, y).getId() == PORT_ID) {
  244. ports.add(new Coord(x, y));
  245. }
  246. if(map.getTile(x, y).isBlockingMovement(player)) {
  247. weightMap[x][y].setBlocked(true);
  248. }
  249. }
  250. }
  251. player.switchSailing();
  252. WATER_WEIGHT = Tiles.SHALLOW_WATER.getEnergyCost(player);
  253. player.switchSailing();
  254. }
  255. private void printDijkstraMap() {
  256. String ANSI_RESET = "\u001B[0m";
  257. String ANSI_BLUE = "\u001B[34m";
  258. String ANSI_PURPLE = "\u001B[35m";
  259. String ANSI_RED = "\u001B[31m";
  260. for(int y = 0; y < weightMap[0].length; y++) {
  261. for(int x = 0; x < weightMap.length; x++) {
  262. int cost = weightMap[x][y].getCostSoFar();
  263. if(cost == Integer.MAX_VALUE) {
  264. System.out.print("- ");
  265. }
  266. else {
  267. if(weightMap[x][y].getType() == TileType.PORT) {
  268. System.out.print(ANSI_RED + cost + " " + ANSI_RESET);
  269. }
  270. else if (weightMap[x][y].getType() == TileType.SHALLOW_WATER) {
  271. System.out.print(ANSI_BLUE + cost + " " + ANSI_RESET);
  272. }
  273. else if (weightMap[x][y].isTown()) {
  274. System.out.print(ANSI_PURPLE + cost + " " + ANSI_RESET);
  275. }
  276. else {
  277. System.out.print(cost + " ");
  278. }
  279. }
  280. }
  281. System.out.println();
  282. }
  283. System.out.println();
  284. }
  285. private void printDijkstraResult() {
  286. System.out.println();
  287. String ANSI_RESET = "\u001B[0m";
  288. String ANSI_BLUE = "\u001B[34m";
  289. for(int origNum = 0; origNum < salesPitch.size(); origNum++) {
  290. for (int lNum = 0; lNum < salesPitch.get(origNum).size(); lNum++) {
  291. System.out.print("Total Cost: " + salesPitch.get(origNum).get(lNum).getTotalCost() + " ");
  292. for (int lInd = 0; lInd < salesPitch.get(origNum).get(lNum).getPath().size(); lInd++) {
  293. Coord c = salesPitch.get(origNum).get(lNum).getPath().get(lInd);
  294. if(c.isBoatTile()) {
  295. System.out.print(ANSI_BLUE + c.getX() + "/" + c.getY() + " " + ANSI_RESET);
  296. }
  297. else {
  298. System.out.print(c.getX() + "/" + c.getY() + " ");
  299. }
  300. }
  301. System.out.println();
  302. }
  303. System.out.println();
  304. }
  305. System.out.println();
  306. }
  307. private void printPorts() {
  308. for(int y = 0; y < weightMap[0].length; y++) {
  309. for(int x = 0; x < weightMap.length; x++) {
  310. if(weightMap[x][y].hasExtraPaths()) {
  311. ArrayList<ExtraPath> extraPaths = weightMap[x][y].getExtraPaths();
  312. System.out.println(x + "/" + y);
  313. for (int i = 0; i < extraPaths.size(); i++) {
  314. System.out.println("port id: " + i + ", " + extraPaths.get(i).getDestX() + "/" + extraPaths.get(i).getDestY());
  315. }
  316. System.out.println();
  317. }
  318. }
  319. }
  320. }
  321. private void printPathMap() {
  322. for(int y = 0; y < weightMap[0].length; y++) {
  323. for(int x = 0; x < weightMap.length; x++) {
  324. if(weightMap[x][y].hasExtraPaths()) {
  325. System.out.print(weightMap[x][y].getPrevBoatPath() + " ");
  326. }
  327. else {
  328. System.out.print(weightMap[x][y].getPrevOfPath() + " ");
  329. }
  330. }
  331. System.out.println();
  332. }
  333. System.out.println();
  334. }
  335. private void printWeightMap() {
  336. String ANSI_RESET = "\u001B[0m";
  337. String ANSI_BLUE = "\u001B[34m";
  338. String ANSI_CYAN = "\u001B[36m";
  339. String ANSI_RED = "\u001B[31m";
  340. for(int y = 0; y < weightMap[0].length; y++) {
  341. for(int x = 0; x < weightMap.length; x++) {
  342. int cost = weightMap[x][y].getWeight();
  343. TileType type = weightMap[x][y].getType();
  344. if(weightMap[x][y].isTown()) {
  345. System.out.print(ANSI_RED + "T " + ANSI_RESET);
  346. }
  347. else if(type == TileType.PORT) {
  348. System.out.print(ANSI_CYAN + "P " + ANSI_RESET);
  349. }
  350. else if(type == TileType.SHALLOW_WATER) {
  351. System.out.print(ANSI_BLUE + "s " + ANSI_RESET);
  352. }
  353. else if(type == TileType.DEEP_WATER) {
  354. System.out.print(ANSI_BLUE + "d " + ANSI_RESET);
  355. }
  356. else {
  357. System.out.print(cost + " ");
  358. }
  359. }
  360. System.out.println();
  361. }
  362. System.out.println();
  363. }
  364. }