Advertisement
Guest User

Untitled

a guest
Apr 24th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.83 KB | None | 0 0
  1. package game;
  2.  
  3. import java.util.Arrays;
  4.  
  5. import game.maps.GameMap;
  6. import game.units.Unit;
  7. import game.util.Direction;
  8. import game.vectors.Vector2f;
  9.  
  10. public class TileWorld {
  11.  
  12. private Tile[][] tiles;
  13.  
  14. private GameMap map;
  15. private int tilesX;
  16. private int tilesY;
  17. private float tileSizeX;
  18. private float tileSizeY;
  19. private float worldWidth;
  20. private float worldHeight;
  21.  
  22. protected TileWorld(GameMap map, int tilesX, int tilesY, float tileSizeX, float tileSizeY, float worldWidth, float worldHeight) {
  23. this.map = map;
  24. this.tilesX = tilesX;
  25. this.tilesY = tilesY;
  26. this.tileSizeX = tileSizeX;
  27. this.tileSizeY = tileSizeY;
  28. this.worldWidth = worldWidth;
  29. this.worldHeight = worldHeight;
  30.  
  31. tiles = new Tile[tilesX][];
  32. for (int i = 0; i < tiles.length; i++) {
  33. tiles[i] = new Tile[tilesY];
  34. for (int j = 0; j < tiles[i].length; j++) {
  35. tiles[i][j] = new Tile();
  36. }
  37. }
  38. }
  39.  
  40. public Tile[][] getTiles() {
  41. return tiles;
  42. }
  43.  
  44. public GameMap getMap() {
  45. return map;
  46. }
  47.  
  48. public void addUnitToTile(TileLocation location, Unit unit) {
  49. System.out.println(tileSizeX * location.getX());
  50. unit.setLocation(new Vector2f(tileSizeX * location.getX() - (worldWidth / 2F) + (worldWidth/unit.getFrameSize()),
  51. tileSizeY * (tilesY - location.getY() - 1) - (worldHeight / 2F) + tileSizeY - (worldHeight/unit.getFrameSize())));
  52. tiles[location.getX()][location.getY()].getUnits().add(unit);
  53. System.out.println(unit.getLocation().x + " " + unit.getLocation().y);
  54. }
  55.  
  56. public TileLocation getTileLocationAtPosition(Vector2f pos) {
  57. int x = (int) (tilesX * ((pos.x + (worldWidth / 2)) / worldWidth));
  58. int y = tilesY - 1 - (int) (tilesY * ((pos.y + (worldHeight / 2)) / worldHeight));
  59. if (x >= tilesX) {
  60. x = tilesX - 1;
  61. }
  62. if (y >= tilesY) {
  63. y = tilesY - 1;
  64. }
  65. return new TileLocation(x, y);
  66. }
  67.  
  68. public void valueIteration() {
  69. double prevDelta = 0;
  70. do {
  71. double delta = dostep();
  72. if (Math.abs(delta - prevDelta) <= 0) {
  73. break;
  74. }
  75. prevDelta = delta;
  76. } while (true);
  77. }
  78.  
  79. private double discount = 0.9;
  80.  
  81. public double dostep() {
  82. double delta = 0;
  83. double newvalues[][] = new double[tilesX][tilesY];
  84. for (int x = 0; x < tilesX; x++) {
  85. for (int y = 0; y < tilesY; y++) {
  86. double d = calculateValues(x, y, Direction.values()[0]);
  87. if (d > delta) {
  88. delta = d;
  89. }
  90. tiles[x][y].Q.put(Direction.values()[0], d);
  91. newvalues[x][y] = tiles[x][y].Q.get(Direction.values()[0]);
  92. // for (Direction action : Direction.values()) {
  93. for (int i = 0; i < Direction.values().length; i++) {
  94. Direction action = Direction.values()[i];
  95. d = calculateValues(x, y, action);
  96. if (d > delta) {
  97. delta = d;
  98. }
  99. tiles[x][y].Q.put(action, d);
  100. if (tiles[x][y].Q.get(action) > newvalues[x][y]) {
  101. newvalues[x][y] = tiles[x][y].Q.get(action);
  102. }
  103. }
  104. }
  105. }
  106. // Apply new values
  107. for (int x = 0; x < tilesX; x++) {
  108. for (int y = 0; y < tilesY; y++) {
  109. tiles[x][y].value = newvalues[x][y];
  110. }
  111. }
  112. return delta;
  113. }
  114.  
  115. public double calculateValues(int x, int y, Direction action) {
  116. if (x == 28 && (y == 6 || y == 7)) {
  117. return 100;
  118. }
  119. if (tiles[x][y].hasMonster()) {
  120. return 20;
  121. }
  122. double newQ = 0;
  123. for (Direction dir : Direction.values()) {
  124. double contrib = contribution(x, y, dir);
  125. if (dir == action) {
  126. newQ += contrib;
  127. }
  128. }
  129.  
  130. if (!getMap().isTileLocationValid(new TileLocation(x, y))) {
  131. newQ += -100;
  132. }
  133. return newQ;
  134. }
  135.  
  136. public double contribution(int x, int y, Direction dir) {
  137. switch (dir) {
  138. case DOWN:
  139. if (isOutOfBounds(x, y + 1)) {
  140. return /*-1 + */discount * tiles[x][y].value;
  141. }
  142. return discount * tiles[x][y + 1].value;
  143. case LEFT:
  144. if (isOutOfBounds(x - 1, y)) {
  145. return /*-1 + */discount * tiles[x][y].value;
  146. }
  147. return discount * tiles[x - 1][y].value;
  148. case RIGHT:
  149. if (isOutOfBounds(x + 1, y)) {
  150. return /*-1 + */discount * tiles[x][y].value;
  151. }
  152. return discount * tiles[x + 1][y].value;
  153. case UP:
  154. if (isOutOfBounds(x, y - 1)) {
  155. return /*-1 + */discount * tiles[x][y].value;
  156. }
  157. return discount * tiles[x][y - 1].value;
  158. }
  159. return 0;
  160. }
  161.  
  162. // public double calculateValues(boolean applyBest, int k) {
  163. // double delta = 0;
  164. // for (int i = 0; i < k; i++) {
  165. // for (int y = 0; y < tilesY; y++) {
  166. // for (int x = tilesX - 1; x >= 0; x--) {
  167. // if ((y == 6 || y == 7) && x == 28) { // Goal
  168. // continue;
  169. // }
  170. // double d = calculateValue(x, y, applyBest);
  171. // if (d > delta) {
  172. // delta = d;
  173. // }
  174. // }
  175. // }
  176. // }
  177. // return delta;
  178. // }
  179. //
  180. // public double calculateValue(int x, int y, boolean applyBest) {
  181. // double maxValue = Double.NEGATIVE_INFINITY;
  182. // Direction bestDir = null;
  183. // for (Direction primaryDir : Direction.values()) {
  184. // double value = 0;
  185. // if (!isOutOfBounds(x + primaryDir.getColDif(), y + primaryDir.getRowDif())) {
  186. // value += tiles[x + primaryDir.getColDif()][y + primaryDir.getRowDif()].U;
  187. // } else {
  188. // // Apply reward function for staying in place
  189. // value += tiles[x][y].U;
  190. // }
  191. // if (value > maxValue) {
  192. // maxValue = value;
  193. // bestDir = primaryDir;
  194. // }
  195. // }
  196. // double oldValue = tiles[x][y].U;
  197. // if (getMap().isTileLocationValid(new TileLocation(x, y))) {
  198. // if (tiles[x][y].hasMonster()) {
  199. // tiles[x][y].U = maxValue + 1000;
  200. // } else {
  201. // tiles[x][y].U = maxValue - 400;
  202. // }
  203. // } else {
  204. // tiles[x][y].U = maxValue - 1000;
  205. // }
  206. // if (applyBest) {
  207. // tiles[x][y].D = bestDir;
  208. // }
  209. // return Math.abs(oldValue - tiles[x][y].U);
  210. // }
  211.  
  212. public boolean isOutOfBounds(int x, int y) {
  213. if (y < 0 || x < 0) {
  214. return true;
  215. }
  216. if (x > tilesX -1 || y > tilesY - 1) {
  217. return true;
  218. }
  219. return false;
  220. }
  221.  
  222. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement