DijkstraMagic.java 14 KB

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