diff --git a/src/micropolisj/engine/TileSpec.java b/src/micropolisj/engine/TileSpec.java index ec44328..3d40c59 100644 --- a/src/micropolisj/engine/TileSpec.java +++ b/src/micropolisj/engine/TileSpec.java @@ -284,4 +284,65 @@ public class TileSpec { return "{tile#"+tileNumber+"}"; } + + void resolveReferences(Map tileMap) + { + String tmp = this.getAttribute("becomes"); + if (tmp != null) { + this.animNext = tileMap.get(tmp); + } + tmp = this.getAttribute("onpower"); + if (tmp != null) { + this.onPower = tileMap.get(tmp); + } + tmp = this.getAttribute("onshutdown"); + if (tmp != null) { + this.onShutdown = tileMap.get(tmp); + } + tmp = this.getAttribute("building-part"); + if (tmp != null) { + this.handleBuildingPart(tmp, tileMap); + } + } + + private void handleBuildingPart(String text, Map tileMap) + { + String [] parts = text.split(","); + if (parts.length != 3) { + throw new Error("Invalid building-part specification"); + } + + this.owner = tileMap.get(parts[0]); + this.ownerOffsetX = Integer.parseInt(parts[1]); + this.ownerOffsetY = Integer.parseInt(parts[2]); + + assert this.owner != null; + assert this.ownerOffsetX != 0 || this.ownerOffsetY != 0; + } + + public static String [] generateTileNames(Properties recipe) + { + int ntiles = recipe.size(); + String [] tileNames = new String[ntiles]; + ntiles = 0; + for (int i = 0; recipe.containsKey(Integer.toString(i)); i++) { + tileNames[ntiles++] = Integer.toString(i); + } + int naturalNumberTiles = ntiles; + + for (Object n_obj : recipe.keySet()) { + String n = (String)n_obj; + if (n.matches("^\\d+$")) { + int x = Integer.parseInt(n); + if (x >= 0 && x < naturalNumberTiles) { + assert tileNames[x].equals(n); + continue; + } + } + assert ntiles < tileNames.length; + tileNames[ntiles++] = n; + } + assert ntiles == tileNames.length; + return tileNames; + } } diff --git a/src/micropolisj/engine/Tiles.java b/src/micropolisj/engine/Tiles.java index b769bbb..5d607ea 100644 --- a/src/micropolisj/engine/Tiles.java +++ b/src/micropolisj/engine/Tiles.java @@ -54,22 +54,8 @@ public class Tiles tiles = tilesList.toArray(new TileSpec[0]); for (int i = 0; i < tiles.length; i++) { - String tmp = tiles[i].getAttribute("becomes"); - if (tmp != null) { - tiles[i].animNext = get(Integer.parseInt(tmp)); - } - tmp = tiles[i].getAttribute("onpower"); - if (tmp != null) { - tiles[i].onPower = get(Integer.parseInt(tmp)); - } - tmp = tiles[i].getAttribute("onshutdown"); - if (tmp != null) { - tiles[i].onShutdown = get(Integer.parseInt(tmp)); - } - tmp = tiles[i].getAttribute("building-part"); - if (tmp != null) { - handleBuildingPart(tiles[i], tmp); - } + tiles[i].resolveReferences(tilesByName); + TileSpec.BuildingInfo bi = tiles[i].getBuildingInfo(); if (bi != null) { for (int j = 0; j < bi.members.length; j++) { @@ -90,20 +76,6 @@ public class Tiles } } - private static void handleBuildingPart(TileSpec partTile, String tmp) - { - String [] parts = tmp.split(","); - if (parts.length != 3) { - throw new Error("Invalid building-part specification"); - } - - partTile.owner = get(Integer.parseInt(parts[0])); - partTile.ownerOffsetX = Integer.parseInt(parts[1]); - partTile.ownerOffsetY = Integer.parseInt(parts[2]); - - assert partTile.ownerOffsetX != 0 || partTile.ownerOffsetY != 0; - } - public static TileSpec get(int tileNumber) { if (tileNumber >= 0 && tileNumber < tiles.length) {