[ebd3538] | 1 | package main;
|
---|
| 2 |
|
---|
| 3 | import java.io.BufferedReader;
|
---|
| 4 | import java.io.FileNotFoundException;
|
---|
| 5 | import utils.Utils;
|
---|
| 6 | import java.util.HashMap;
|
---|
| 7 | import java.io.IOException;
|
---|
| 8 | import java.io.Writer;
|
---|
| 9 | import java.io.PrintWriter;
|
---|
| 10 | import java.io.FileWriter;
|
---|
| 11 | import java.util.Iterator;
|
---|
| 12 | import java.awt.Graphics;
|
---|
| 13 | import java.util.Collection;
|
---|
| 14 | import java.awt.Point;
|
---|
| 15 | import java.awt.Color;
|
---|
| 16 | import java.util.TreeSet;
|
---|
| 17 | import java.util.LinkedList;
|
---|
| 18 | import astar.AStarMap;
|
---|
| 19 |
|
---|
| 20 | public class Map
|
---|
| 21 | {
|
---|
| 22 | private Location[][] grid;
|
---|
| 23 | public AStarMap asMap;
|
---|
| 24 | public LinkedList<Creature> creatureList;
|
---|
| 25 | TreeSet<Creature> creatures;
|
---|
| 26 | TreeSet<Tile> tiles;
|
---|
| 27 | TreeSet<Item> items;
|
---|
| 28 | int xLow;
|
---|
| 29 | int xHigh;
|
---|
| 30 | int yLow;
|
---|
| 31 | int yHigh;
|
---|
| 32 | static int count;
|
---|
| 33 | public String message;
|
---|
| 34 | public boolean valid;
|
---|
| 35 | public int startX;
|
---|
| 36 | public int startY;
|
---|
| 37 | private static Color redHaze;
|
---|
| 38 |
|
---|
| 39 | static {
|
---|
| 40 | Map.count = 0;
|
---|
| 41 | Map.redHaze = new Color(255, 0, 0, 100);
|
---|
| 42 | }
|
---|
| 43 |
|
---|
| 44 | public Map(final int x, final int y) {
|
---|
| 45 | this.grid = new Location[x][y];
|
---|
| 46 | this.asMap = new AStarMap(x, y);
|
---|
| 47 | this.creatureList = new LinkedList<Creature>();
|
---|
| 48 | this.creatures = new TreeSet<Creature>();
|
---|
| 49 | this.tiles = new TreeSet<Tile>();
|
---|
| 50 | this.items = new TreeSet<Item>();
|
---|
| 51 | this.xLow = 0;
|
---|
| 52 | this.xHigh = 0;
|
---|
| 53 | this.yLow = 0;
|
---|
| 54 | this.yHigh = 0;
|
---|
| 55 | this.startX = 400;
|
---|
| 56 | this.startY = 300;
|
---|
| 57 | this.initBounds();
|
---|
| 58 | for (int i = 0; i < this.getWidth(); ++i) {
|
---|
| 59 | for (int j = 0; j < this.getHeight(); ++j) {
|
---|
| 60 | this.grid[i][j] = new Location(null);
|
---|
| 61 | }
|
---|
| 62 | }
|
---|
| 63 | this.valid = true;
|
---|
| 64 | }
|
---|
| 65 |
|
---|
| 66 | public Map(final int x, final int y, final Tile defaultTile) {
|
---|
| 67 | this(x, y);
|
---|
| 68 | for (int i = 0; i < this.getWidth(); ++i) {
|
---|
| 69 | for (int j = 0; j < this.getHeight(); ++j) {
|
---|
| 70 | this.setGround(i, j, new Tile(defaultTile, i * 40, j * 40, 0));
|
---|
| 71 | }
|
---|
| 72 | }
|
---|
| 73 | this.message = "New map created successfully";
|
---|
| 74 | }
|
---|
| 75 |
|
---|
| 76 | public Map(final int x, final int y, final Tile defaultTile, final Tile guidelineTile) {
|
---|
| 77 | this(x, y);
|
---|
| 78 | for (int i = 0; i < this.getWidth(); ++i) {
|
---|
| 79 | for (int j = 0; j < this.getHeight(); ++j) {
|
---|
| 80 | this.setGround(i, j, new Tile(defaultTile, i * 40, j * 40, 0));
|
---|
| 81 | }
|
---|
| 82 | }
|
---|
| 83 | for (int i = 19; i < this.getWidth(); i += 20) {
|
---|
| 84 | for (int j = 14; j < this.getHeight(); j += 15) {
|
---|
| 85 | for (int a = 0; a < 20; ++a) {
|
---|
| 86 | this.setGround(i - a, j - 14, new Tile(guidelineTile, (i - a) * 40, (j - 14) * 40, 0));
|
---|
| 87 | this.setGround(i - a, j, new Tile(guidelineTile, (i - a) * 40, j * 40, 0));
|
---|
| 88 | }
|
---|
| 89 | for (int b = 1; b < 14; ++b) {
|
---|
| 90 | this.setGround(i - 19, j - b, new Tile(guidelineTile, (i - 19) * 40, (j - b) * 40, 0));
|
---|
| 91 | this.setGround(i, j - b, new Tile(guidelineTile, i * 40, (j - b) * 40, 0));
|
---|
| 92 | }
|
---|
| 93 | }
|
---|
| 94 | }
|
---|
| 95 | this.message = "New map created successfully";
|
---|
| 96 | }
|
---|
| 97 |
|
---|
| 98 | public void initBounds() {
|
---|
| 99 | this.xLow = this.startX / 40 - 12;
|
---|
| 100 | this.xHigh = this.xLow + 24;
|
---|
| 101 | this.yLow = this.startY / 40 - 10;
|
---|
| 102 | this.yHigh = this.yLow + 20;
|
---|
| 103 | if (this.xLow < 0) {
|
---|
| 104 | this.xLow = 0;
|
---|
| 105 | }
|
---|
| 106 | if (this.xHigh > this.getWidth() - 1) {
|
---|
| 107 | this.xHigh = this.getWidth() - 1;
|
---|
| 108 | }
|
---|
| 109 | if (this.yLow < 0) {
|
---|
| 110 | this.yLow = 0;
|
---|
| 111 | }
|
---|
| 112 | if (this.yHigh > this.getHeight() - 1) {
|
---|
| 113 | this.yHigh = this.getHeight() - 1;
|
---|
| 114 | }
|
---|
| 115 | }
|
---|
| 116 |
|
---|
| 117 | public Location getLoc(final int x, final int y) {
|
---|
| 118 | return this.grid[x][y];
|
---|
| 119 | }
|
---|
| 120 |
|
---|
| 121 | public void setBounds(final int x1, final int x2, final int y1, final int y2) {
|
---|
| 122 | this.xLow = x1;
|
---|
| 123 | this.xHigh = x2;
|
---|
| 124 | this.yLow = y1;
|
---|
| 125 | this.yHigh = y2;
|
---|
| 126 | }
|
---|
| 127 |
|
---|
| 128 | public int getWidth() {
|
---|
| 129 | return this.grid.length;
|
---|
| 130 | }
|
---|
| 131 |
|
---|
| 132 | public int getHeight() {
|
---|
| 133 | if (this.grid.length > 0) {
|
---|
| 134 | return this.grid[0].length;
|
---|
| 135 | }
|
---|
| 136 | return 0;
|
---|
| 137 | }
|
---|
| 138 |
|
---|
| 139 | public LinkedList<MapObject> getObjectsInVicinity(final Point p, final int xDist, final int yDist) {
|
---|
| 140 | int lowX = p.x / 40 - xDist;
|
---|
| 141 | int highX = p.x / 40 + xDist;
|
---|
| 142 | int lowY = p.y / 40 - yDist;
|
---|
| 143 | int highY = p.y / 40 + yDist;
|
---|
| 144 | final LinkedList<MapObject> list = new LinkedList<MapObject>();
|
---|
| 145 | if (lowX < 0) {
|
---|
| 146 | lowX = 0;
|
---|
| 147 | }
|
---|
| 148 | if (highX > this.getWidth() - 1) {
|
---|
| 149 | highX = this.getWidth() - 1;
|
---|
| 150 | }
|
---|
| 151 | if (lowY < 0) {
|
---|
| 152 | lowY = 0;
|
---|
| 153 | }
|
---|
| 154 | if (highY > this.getHeight() - 1) {
|
---|
| 155 | highY = this.getHeight() - 1;
|
---|
| 156 | }
|
---|
| 157 | for (int x = lowX; x <= highX; ++x) {
|
---|
| 158 | for (int y = lowY; y <= highY; ++y) {
|
---|
| 159 | list.addAll(this.getLoc(x, y).getCreatures());
|
---|
| 160 | list.addAll(this.getLoc(x, y).getObjects());
|
---|
| 161 | list.add(this.getLoc(x, y).getGround());
|
---|
| 162 | }
|
---|
| 163 | }
|
---|
| 164 | return list;
|
---|
| 165 | }
|
---|
| 166 |
|
---|
| 167 | public LinkedList<Creature> getCreaturesInVicinity(final Point p, final int xDist, final int yDist) {
|
---|
| 168 | int lowX = p.x / 40 - xDist;
|
---|
| 169 | int highX = p.x / 40 + xDist;
|
---|
| 170 | int lowY = p.y / 40 - yDist;
|
---|
| 171 | int highY = p.y / 40 + yDist;
|
---|
| 172 | final LinkedList<Creature> list = new LinkedList<Creature>();
|
---|
| 173 | if (lowX < 0) {
|
---|
| 174 | lowX = 0;
|
---|
| 175 | }
|
---|
| 176 | if (highX > this.getWidth() - 1) {
|
---|
| 177 | highX = this.getWidth() - 1;
|
---|
| 178 | }
|
---|
| 179 | if (lowY < 0) {
|
---|
| 180 | lowY = 0;
|
---|
| 181 | }
|
---|
| 182 | if (highY > this.getHeight() - 1) {
|
---|
| 183 | highY = this.getHeight() - 1;
|
---|
| 184 | }
|
---|
| 185 | for (int x = lowX; x <= highX; ++x) {
|
---|
| 186 | for (int y = lowY; y <= highY; ++y) {
|
---|
| 187 | list.addAll(this.getLoc(x, y).getCreatures());
|
---|
| 188 | }
|
---|
| 189 | }
|
---|
| 190 | return list;
|
---|
| 191 | }
|
---|
| 192 |
|
---|
| 193 | public void setGround(final int x, final int y, final Tile o) {
|
---|
| 194 | if (this.xLow <= x && x <= this.xHigh && this.yLow <= y && y <= this.yHigh) {
|
---|
| 195 | if (this.getLoc(x, y).getGround() != null) {
|
---|
| 196 | this.tiles.remove(this.getLoc(x, y).getGround());
|
---|
| 197 | }
|
---|
| 198 | if (this.getLoc(x, y).getBase() != null) {
|
---|
| 199 | this.tiles.remove(this.getLoc(x, y).getBase());
|
---|
| 200 | }
|
---|
| 201 | this.getLoc(x, y).setGround(o);
|
---|
| 202 | this.tiles.add(this.getLoc(x, y).getGround());
|
---|
| 203 | if (this.getLoc(x, y).getBase() != null) {
|
---|
| 204 | this.tiles.add(this.getLoc(x, y).getBase());
|
---|
| 205 | }
|
---|
| 206 | }
|
---|
| 207 | else {
|
---|
| 208 | this.getLoc(x, y).setGround(o);
|
---|
| 209 | }
|
---|
| 210 | this.asMap.addObstacle(o);
|
---|
| 211 | }
|
---|
| 212 |
|
---|
| 213 | public void addCreature(final int x, final int y, final Creature o) {
|
---|
| 214 | if (this.xLow <= x && x <= this.xHigh && this.yLow <= y && y <= this.yHigh) {
|
---|
| 215 | this.creatures.add(o);
|
---|
| 216 | }
|
---|
| 217 | this.creatureList.add(o);
|
---|
| 218 | this.getLoc(x, y).addCreature(o);
|
---|
| 219 | }
|
---|
| 220 |
|
---|
| 221 | public void removeCreature(final int x, final int y, final Creature o) {
|
---|
| 222 | if (this.xLow <= x && x <= this.xHigh && this.yLow <= y && y <= this.yHigh) {
|
---|
| 223 | this.creatures.remove(o);
|
---|
| 224 | }
|
---|
| 225 | this.creatureList.remove(o);
|
---|
| 226 | this.getLoc(x, y).getCreatures().remove(o);
|
---|
| 227 | }
|
---|
| 228 |
|
---|
| 229 | public void addObject(final int x, final int y, final Tile o) {
|
---|
| 230 | if (this.xLow <= x && x <= this.xHigh && this.yLow <= y && y <= this.yHigh) {
|
---|
| 231 | this.tiles.add(o);
|
---|
| 232 | }
|
---|
| 233 | this.getLoc(x, y).addObject(o);
|
---|
| 234 | this.asMap.addObstacle(o);
|
---|
| 235 | }
|
---|
| 236 |
|
---|
| 237 | public void removeObject(final int x, final int y, final Tile o) {
|
---|
| 238 | if (this.xLow <= x && x <= this.xHigh && this.yLow <= y && y <= this.yHigh) {
|
---|
| 239 | this.tiles.remove(o);
|
---|
| 240 | }
|
---|
| 241 | this.getLoc(x, y).getObjects().remove(o);
|
---|
| 242 | }
|
---|
| 243 |
|
---|
| 244 | public void addItem(final int x, final int y, final Item o) {
|
---|
| 245 | if (this.xLow <= x && x <= this.xHigh && this.yLow <= y && y <= this.yHigh) {
|
---|
| 246 | this.items.add(o);
|
---|
| 247 | }
|
---|
| 248 | this.getLoc(x, y).addItem(o);
|
---|
| 249 | }
|
---|
| 250 |
|
---|
| 251 | public void removeItem(final int x, final int y, final Item o) {
|
---|
| 252 | if (this.xLow <= x && x <= this.xHigh && this.yLow <= y && y <= this.yHigh) {
|
---|
| 253 | this.items.remove(o);
|
---|
| 254 | }
|
---|
| 255 | this.getLoc(x, y).getItems().remove(o);
|
---|
| 256 | }
|
---|
| 257 |
|
---|
| 258 | private void addObjectsAt(final int x, final int y) {
|
---|
| 259 | this.creatures.addAll(this.getLoc(x, y).getCreatures());
|
---|
| 260 | this.items.addAll(this.getLoc(x, y).getItems());
|
---|
| 261 | this.tiles.addAll(this.getLoc(x, y).getObjects());
|
---|
| 262 | this.tiles.add(this.getLoc(x, y).getGround());
|
---|
| 263 | if (this.getLoc(x, y).getBase() != null) {
|
---|
| 264 | this.tiles.add(this.getLoc(x, y).getBase());
|
---|
| 265 | }
|
---|
| 266 | }
|
---|
| 267 |
|
---|
| 268 | private void removeObjectsAt(final int x, final int y) {
|
---|
| 269 | this.creatures.removeAll(this.getLoc(x, y).getCreatures());
|
---|
| 270 | this.items.removeAll(this.getLoc(x, y).getItems());
|
---|
| 271 | this.tiles.removeAll(this.getLoc(x, y).getObjects());
|
---|
| 272 | this.tiles.remove(this.getLoc(x, y).getGround());
|
---|
| 273 | if (this.getLoc(x, y).getBase() != null) {
|
---|
| 274 | this.tiles.remove(this.getLoc(x, y).getBase());
|
---|
| 275 | }
|
---|
| 276 | }
|
---|
| 277 |
|
---|
| 278 | private void addColAt(final int x, final int yLower, final int yUpper) {
|
---|
| 279 | for (int i = yLower; i <= yUpper; ++i) {
|
---|
| 280 | this.addObjectsAt(x, i);
|
---|
| 281 | }
|
---|
| 282 | }
|
---|
| 283 |
|
---|
| 284 | private void removeColAt(final int x, final int yLower, final int yUpper) {
|
---|
| 285 | for (int i = yLower; i <= yUpper; ++i) {
|
---|
| 286 | this.removeObjectsAt(x, i);
|
---|
| 287 | }
|
---|
| 288 | }
|
---|
| 289 |
|
---|
| 290 | private void addRowAt(final int y, final int xLower, final int xUpper) {
|
---|
| 291 | for (int i = xLower; i <= xUpper; ++i) {
|
---|
| 292 | this.addObjectsAt(i, y);
|
---|
| 293 | }
|
---|
| 294 | }
|
---|
| 295 |
|
---|
| 296 | private void removeRowAt(final int y, final int xLower, final int xUpper) {
|
---|
| 297 | for (int i = xLower; i <= xUpper; ++i) {
|
---|
| 298 | this.removeObjectsAt(i, y);
|
---|
| 299 | }
|
---|
| 300 | }
|
---|
| 301 |
|
---|
| 302 | private void updateBounds(final int xLow_new, final int xHigh_new, final int yLow_new, final int yHigh_new) {
|
---|
| 303 | if (xLow_new < this.xLow) {
|
---|
| 304 | for (int x = xLow_new; x < this.xLow; ++x) {
|
---|
| 305 | this.addColAt(x, yLow_new, yHigh_new);
|
---|
| 306 | }
|
---|
| 307 | }
|
---|
| 308 | else {
|
---|
| 309 | for (int x = this.xLow; x < xLow_new; ++x) {
|
---|
| 310 | this.removeColAt(x, this.yLow, this.yHigh);
|
---|
| 311 | }
|
---|
| 312 | }
|
---|
| 313 | if (xHigh_new > this.xHigh) {
|
---|
| 314 | for (int x = xHigh_new; x > this.xHigh; --x) {
|
---|
| 315 | this.addColAt(x, yLow_new, yHigh_new);
|
---|
| 316 | }
|
---|
| 317 | }
|
---|
| 318 | else {
|
---|
| 319 | for (int x = this.xHigh; x > xHigh_new; --x) {
|
---|
| 320 | this.removeColAt(x, this.yLow, this.yHigh);
|
---|
| 321 | }
|
---|
| 322 | }
|
---|
| 323 | if (yLow_new < this.yLow) {
|
---|
| 324 | for (int y = yLow_new; y < this.yLow; ++y) {
|
---|
| 325 | this.addRowAt(y, xLow_new, xHigh_new);
|
---|
| 326 | }
|
---|
| 327 | }
|
---|
| 328 | else {
|
---|
| 329 | for (int y = this.yLow; y < yLow_new; ++y) {
|
---|
| 330 | this.removeRowAt(y, this.xLow, this.xHigh);
|
---|
| 331 | }
|
---|
| 332 | }
|
---|
| 333 | if (yHigh_new > this.yHigh) {
|
---|
| 334 | for (int y = yHigh_new; y > this.yHigh; --y) {
|
---|
| 335 | this.addRowAt(y, xLow_new, xHigh_new);
|
---|
| 336 | }
|
---|
| 337 | }
|
---|
| 338 | else {
|
---|
| 339 | for (int y = this.yHigh; y > yHigh_new; --y) {
|
---|
| 340 | this.removeRowAt(y, this.xLow, this.xHigh);
|
---|
| 341 | }
|
---|
| 342 | }
|
---|
| 343 | this.xLow = xLow_new;
|
---|
| 344 | this.xHigh = xHigh_new;
|
---|
| 345 | this.yLow = yLow_new;
|
---|
| 346 | this.yHigh = yHigh_new;
|
---|
| 347 | }
|
---|
| 348 |
|
---|
| 349 | public void draw(final Graphics g, final Point p, final boolean bounds, final boolean passable) {
|
---|
| 350 | int xLow_new = p.x / 40 - 12;
|
---|
| 351 | int xHigh_new = xLow_new + 24;
|
---|
| 352 | int yLow_new = p.y / 40 - 10;
|
---|
| 353 | int yHigh_new = yLow_new + 20;
|
---|
| 354 | if (xLow_new < 0) {
|
---|
| 355 | xLow_new = 0;
|
---|
| 356 | }
|
---|
| 357 | if (xHigh_new > this.getWidth() - 1) {
|
---|
| 358 | xHigh_new = this.getWidth() - 1;
|
---|
| 359 | }
|
---|
| 360 | if (yLow_new < 0) {
|
---|
| 361 | yLow_new = 0;
|
---|
| 362 | }
|
---|
| 363 | if (yHigh_new > this.getHeight() - 1) {
|
---|
| 364 | yHigh_new = this.getHeight() - 1;
|
---|
| 365 | }
|
---|
| 366 | this.updateBounds(xLow_new, xHigh_new, yLow_new, yHigh_new);
|
---|
| 367 | final TreeSet<MapObject> tmp = new TreeSet<MapObject>(this.tiles);
|
---|
| 368 | tmp.addAll(this.creatures);
|
---|
| 369 | tmp.addAll(this.items);
|
---|
| 370 | for (final MapObject cur : tmp) {
|
---|
| 371 | if (cur.z == 0 || !passable) {
|
---|
| 372 | cur.draw(g, 400 - p.x, 300 - p.y);
|
---|
| 373 | }
|
---|
| 374 | }
|
---|
| 375 | if (bounds) {
|
---|
| 376 | final Iterator<MapObject> iter = tmp.iterator();
|
---|
| 377 | g.setColor(Color.red);
|
---|
| 378 | while (iter.hasNext()) {
|
---|
| 379 | final MapObject obj = iter.next();
|
---|
| 380 | if (obj.getBound() != null) {
|
---|
| 381 | obj.getBound().draw(g, 400 - p.x, 300 - p.y, obj);
|
---|
| 382 | }
|
---|
| 383 | if (obj.getSelectionBound() != null) {
|
---|
| 384 | obj.getSelectionBound().draw(g, 400 - p.x, 300 - p.y, obj);
|
---|
| 385 | }
|
---|
| 386 | }
|
---|
| 387 | }
|
---|
| 388 | if (passable) {
|
---|
| 389 | g.setColor(Map.redHaze);
|
---|
| 390 | for (int x = this.xLow; x <= this.xHigh; ++x) {
|
---|
| 391 | for (int y = this.yLow; y <= this.yHigh; ++y) {
|
---|
| 392 | this.asMap.map[x][y].drawBlocked(g, 400 - p.x, 300 - p.y);
|
---|
| 393 | this.asMap.map[x][y].draw(g, 400 - p.x, 300 - p.y, false);
|
---|
| 394 | }
|
---|
| 395 | }
|
---|
| 396 | }
|
---|
| 397 | }
|
---|
| 398 |
|
---|
| 399 | public boolean save(final String fileName) {
|
---|
| 400 | String strObjects = "";
|
---|
| 401 | final TreeSet<Tile> objSet = new TreeSet<Tile>();
|
---|
| 402 | try {
|
---|
| 403 | final PrintWriter out = new PrintWriter(new FileWriter("maps/" + fileName));
|
---|
| 404 | out.println(String.valueOf(this.getWidth()) + "x" + this.getHeight());
|
---|
| 405 | for (int x = 0; x < this.getWidth(); ++x) {
|
---|
| 406 | String strTiles = "";
|
---|
| 407 | for (int y = 0; y < this.getHeight(); ++y) {
|
---|
| 408 | final Location loc = this.getLoc(x, y);
|
---|
| 409 | if (loc.getBase() != null) {
|
---|
| 410 | strTiles = String.valueOf(strTiles) + loc.getBase().getImg().getKey() + loc.getBase().z + "|";
|
---|
| 411 | }
|
---|
| 412 | strTiles = String.valueOf(strTiles) + loc.getGround().getImg().getKey() + loc.getGround().z + ",";
|
---|
| 413 | objSet.addAll(loc.getObjects());
|
---|
| 414 | }
|
---|
| 415 | out.println(strTiles.substring(0, strTiles.length() - 1));
|
---|
| 416 | }
|
---|
| 417 | for (final Tile curObj : objSet) {
|
---|
| 418 | strObjects = String.valueOf(strObjects) + curObj.loc.x + "," + curObj.loc.y + "," + curObj.getImg().getKey() + "," + curObj.z + ";";
|
---|
| 419 | }
|
---|
| 420 | if (strObjects.length() == 0) {
|
---|
| 421 | out.println();
|
---|
| 422 | }
|
---|
| 423 | else {
|
---|
| 424 | out.println(strObjects.substring(0, strObjects.length() - 1));
|
---|
| 425 | }
|
---|
| 426 | final Iterator<Creature> iter2 = this.creatureList.iterator();
|
---|
| 427 | strObjects = "";
|
---|
| 428 | while (iter2.hasNext()) {
|
---|
| 429 | final Creature curCr = iter2.next();
|
---|
| 430 | strObjects = String.valueOf(strObjects) + curCr.loc.x + "," + curCr.loc.y + "," + curCr.id + ";";
|
---|
| 431 | }
|
---|
| 432 | if (strObjects.length() == 0) {
|
---|
| 433 | out.println();
|
---|
| 434 | }
|
---|
| 435 | else {
|
---|
| 436 | out.println(strObjects.substring(0, strObjects.length() - 1));
|
---|
| 437 | }
|
---|
| 438 | out.close();
|
---|
| 439 | }
|
---|
| 440 | catch (IOException ioe) {
|
---|
| 441 | this.message = "There was a problem saving the map";
|
---|
| 442 | ioe.printStackTrace();
|
---|
| 443 | return false;
|
---|
| 444 | }
|
---|
| 445 | this.message = "The map has been saved successfully";
|
---|
| 446 | return true;
|
---|
| 447 | }
|
---|
| 448 |
|
---|
| 449 | public static Map load(final String fileName, final Map baseMap, final HashMap<Integer, Tile> imageMap, final HashMap<Integer, Creature> crMap) {
|
---|
| 450 | Map map = null;
|
---|
| 451 | try {
|
---|
| 452 | final BufferedReader in = Utils.loadTextFile("maps/" + fileName);
|
---|
| 453 | final String sizeStr = in.readLine();
|
---|
| 454 | final String xSize = sizeStr.substring(0, sizeStr.indexOf("x"));
|
---|
| 455 | final String ySize = sizeStr.substring(sizeStr.indexOf("x") + 1);
|
---|
| 456 | map = new Map(Integer.parseInt(xSize), Integer.parseInt(ySize));
|
---|
| 457 | if (baseMap == null) {
|
---|
| 458 | map.initBounds();
|
---|
| 459 | }
|
---|
| 460 | else {
|
---|
| 461 | map.setBounds(baseMap.xLow, baseMap.xHigh, baseMap.yLow, baseMap.yHigh);
|
---|
| 462 | }
|
---|
| 463 | for (int x = 0; x < map.getWidth(); ++x) {
|
---|
| 464 | String row = in.readLine();
|
---|
| 465 | for (int y = 0; y < map.getHeight(); ++y) {
|
---|
| 466 | String loc;
|
---|
| 467 | if (row.indexOf(",") == -1) {
|
---|
| 468 | loc = row;
|
---|
| 469 | }
|
---|
| 470 | else {
|
---|
| 471 | loc = row.substring(0, row.indexOf(","));
|
---|
| 472 | row = row.substring(row.indexOf(",") + 1);
|
---|
| 473 | }
|
---|
| 474 | int ground;
|
---|
| 475 | int groundZ;
|
---|
| 476 | if (loc.indexOf("|") == -1) {
|
---|
| 477 | ground = Integer.parseInt(loc) / 10;
|
---|
| 478 | groundZ = Integer.parseInt(loc) % 10;
|
---|
| 479 | }
|
---|
| 480 | else {
|
---|
| 481 | final int base = Integer.parseInt(loc.substring(0, loc.indexOf("|"))) / 10;
|
---|
| 482 | final int baseZ = Integer.parseInt(loc.substring(0, loc.indexOf("|"))) % 10;
|
---|
| 483 | ground = Integer.parseInt(loc.substring(loc.indexOf("|") + 1)) / 10;
|
---|
| 484 | groundZ = Integer.parseInt(loc.substring(loc.indexOf("|") + 1)) % 10;
|
---|
| 485 | map.setGround(x, y, new Tile(imageMap.get(base), x * 40, y * 40, baseZ));
|
---|
| 486 | }
|
---|
| 487 | map.setGround(x, y, new Tile(imageMap.get(ground), x * 40, y * 40, groundZ));
|
---|
| 488 | }
|
---|
| 489 | }
|
---|
| 490 | String objects = in.readLine();
|
---|
| 491 | while (!objects.equals("")) {
|
---|
| 492 | String obj;
|
---|
| 493 | if (objects.indexOf(";") == -1) {
|
---|
| 494 | obj = objects;
|
---|
| 495 | objects = "";
|
---|
| 496 | }
|
---|
| 497 | else {
|
---|
| 498 | obj = objects.substring(0, objects.indexOf(";"));
|
---|
| 499 | objects = objects.substring(objects.indexOf(";") + 1);
|
---|
| 500 | }
|
---|
| 501 | final int x2 = Integer.parseInt(obj.substring(0, obj.indexOf(",")));
|
---|
| 502 | obj = obj.substring(obj.indexOf(",") + 1);
|
---|
| 503 | final int y2 = Integer.parseInt(obj.substring(0, obj.indexOf(",")));
|
---|
| 504 | obj = obj.substring(obj.indexOf(",") + 1);
|
---|
| 505 | final int key = Integer.parseInt(obj.substring(0, obj.indexOf(",")));
|
---|
| 506 | final int z = Integer.parseInt(obj.substring(obj.indexOf(",") + 1));
|
---|
| 507 | map.addObject(x2 / 40, y2 / 40, new Tile(imageMap.get(key), x2, y2, z));
|
---|
| 508 | }
|
---|
| 509 | String creatures = in.readLine();
|
---|
| 510 | while (!creatures.equals("")) {
|
---|
| 511 | String cr;
|
---|
| 512 | if (creatures.indexOf(";") == -1) {
|
---|
| 513 | cr = creatures;
|
---|
| 514 | creatures = "";
|
---|
| 515 | }
|
---|
| 516 | else {
|
---|
| 517 | cr = creatures.substring(0, creatures.indexOf(";"));
|
---|
| 518 | creatures = creatures.substring(creatures.indexOf(";") + 1);
|
---|
| 519 | }
|
---|
| 520 | final int x3 = Integer.parseInt(cr.substring(0, cr.indexOf(",")));
|
---|
| 521 | cr = cr.substring(cr.indexOf(",") + 1);
|
---|
| 522 | final int y3 = Integer.parseInt(cr.substring(0, cr.indexOf(",")));
|
---|
| 523 | final int id = Integer.parseInt(cr.substring(cr.indexOf(",") + 1));
|
---|
| 524 | map.addCreature(x3 / 40, y3 / 40, crMap.get(id).copy(new Point(x3, y3)));
|
---|
| 525 | }
|
---|
| 526 | in.close();
|
---|
| 527 | map.asMap.coalesceQuadTrees();
|
---|
| 528 | map.asMap.generateAllNeighbors();
|
---|
| 529 | }
|
---|
| 530 | catch (FileNotFoundException fnfe) {
|
---|
| 531 | if (map == null) {
|
---|
| 532 | map = new Map(0, 0);
|
---|
| 533 | }
|
---|
| 534 | map.message = "That saved game file does not exists";
|
---|
| 535 | map.valid = false;
|
---|
| 536 | return map;
|
---|
| 537 | }
|
---|
| 538 | catch (IOException ioe) {
|
---|
| 539 | if (map == null) {
|
---|
| 540 | map = new Map(0, 0);
|
---|
| 541 | }
|
---|
| 542 | map.message = "There was a problem loading the map";
|
---|
| 543 | ioe.printStackTrace();
|
---|
| 544 | map.valid = false;
|
---|
| 545 | return map;
|
---|
| 546 | }
|
---|
| 547 | map.message = "The map has been loaded successfully";
|
---|
| 548 | map.valid = true;
|
---|
| 549 | return map;
|
---|
| 550 | }
|
---|
| 551 | }
|
---|