From 8be01002eb4379db518eea3d264d1fd1b54769cc Mon Sep 17 00:00:00 2001 From: Oskar Wiksten Date: Mon, 30 Jul 2012 23:25:43 +0000 Subject: [PATCH] Added named areas (towns, taverns) to worldmap. --- AndorsTrail/res/values/worldmap_template.xml | 8 +- AndorsTrail/res/xml/worldmap.xml | 43 +++++---- .../AndorsTrail/AndorsTrailApplication.java | 2 +- .../controller/WorldMapController.java | 89 +++++++++++++++---- .../model/map/WorldMapSegment.java | 15 ++++ .../resource/parsers/WorldMapParser.java | 16 ++++ 6 files changed, 139 insertions(+), 34 deletions(-) diff --git a/AndorsTrail/res/values/worldmap_template.xml b/AndorsTrail/res/values/worldmap_template.xml index d4bdbd7b3..4ea5c1fa9 100644 --- a/AndorsTrail/res/values/worldmap_template.xml +++ b/AndorsTrail/res/values/worldmap_template.xml @@ -6,7 +6,12 @@ @@ -14,6 +19,7 @@
{{maps}} +{{areas}}
x
diff --git a/AndorsTrail/res/xml/worldmap.xml b/AndorsTrail/res/xml/worldmap.xml index f43aedee5..58e40de15 100644 --- a/AndorsTrail/res/xml/worldmap.xml +++ b/AndorsTrail/res/xml/worldmap.xml @@ -63,7 +63,7 @@ - + @@ -72,7 +72,7 @@ - + @@ -97,7 +97,7 @@ - + @@ -122,7 +122,7 @@ - + @@ -142,17 +142,17 @@ - + - + - + @@ -162,20 +162,28 @@ - + - - - - - - - - + + + + + + + + + + + + + + + + @@ -183,10 +191,11 @@ - + + diff --git a/AndorsTrail/src/com/gpl/rpg/AndorsTrail/AndorsTrailApplication.java b/AndorsTrail/src/com/gpl/rpg/AndorsTrail/AndorsTrailApplication.java index 6486784bd..761d80a39 100644 --- a/AndorsTrail/src/com/gpl/rpg/AndorsTrail/AndorsTrailApplication.java +++ b/AndorsTrail/src/com/gpl/rpg/AndorsTrail/AndorsTrailApplication.java @@ -16,7 +16,7 @@ public final class AndorsTrailApplication extends Application { public static final boolean DEVELOPMENT_DEBUGRESOURCES = false; public static final boolean DEVELOPMENT_FORCE_STARTNEWGAME = false; public static final boolean DEVELOPMENT_FORCE_CONTINUEGAME = false; - public static final boolean DEVELOPMENT_DEBUGBUTTONS = false; + public static final boolean DEVELOPMENT_DEBUGBUTTONS = true; public static final boolean DEVELOPMENT_VALIDATEDATA = false; public static final boolean DEVELOPMENT_DEBUGMESSAGES = false; public static final boolean DEVELOPMENT_INCOMPATIBLE_SAVEGAMES = DEVELOPMENT_DEBUGRESOURCES; diff --git a/AndorsTrail/src/com/gpl/rpg/AndorsTrail/controller/WorldMapController.java b/AndorsTrail/src/com/gpl/rpg/AndorsTrail/controller/WorldMapController.java index 5a378b491..d71277ac8 100644 --- a/AndorsTrail/src/com/gpl/rpg/AndorsTrail/controller/WorldMapController.java +++ b/AndorsTrail/src/com/gpl/rpg/AndorsTrail/controller/WorldMapController.java @@ -4,6 +4,7 @@ import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.PrintWriter; +import java.util.HashSet; import android.content.Context; import android.content.Intent; @@ -24,10 +25,13 @@ import com.gpl.rpg.AndorsTrail.model.map.LayeredTileMap; import com.gpl.rpg.AndorsTrail.model.map.MapLayer; import com.gpl.rpg.AndorsTrail.model.map.PredefinedMap; import com.gpl.rpg.AndorsTrail.model.map.WorldMapSegment; +import com.gpl.rpg.AndorsTrail.model.map.WorldMapSegment.NamedWorldMapArea; import com.gpl.rpg.AndorsTrail.model.map.WorldMapSegment.WorldMapSegmentMap; import com.gpl.rpg.AndorsTrail.resource.tiles.TileCollection; import com.gpl.rpg.AndorsTrail.util.Coord; +import com.gpl.rpg.AndorsTrail.util.CoordRect; import com.gpl.rpg.AndorsTrail.util.L; +import com.gpl.rpg.AndorsTrail.util.Size; public final class WorldMapController { @@ -152,48 +156,103 @@ public final class WorldMapController { return new File(getWorldmapDirectory(), Constants.FILENAME_WORLDMAP_HTMLFILE_PREFIX + segmentName + Constants.FILENAME_WORLDMAP_HTMLFILE_SUFFIX); } + private static boolean shouldDisplayMapOnWorldmap(String mapName) { + File f = WorldMapController.getFileForMap(mapName); + return f.exists(); + } private static String getWorldMapSegmentAsHtml(Resources res, WorldContext world, String segmentName) throws IOException { WorldMapSegment segment = world.maps.worldMapSegments.get(segmentName); - StringBuffer sb = new StringBuffer(); - + HashSet displayedMapNames = new HashSet(); Coord offsetWorldmapTo = new Coord(999999, 999999); for (WorldMapSegmentMap map : segment.maps.values()) { - File f = WorldMapController.getFileForMap(map.mapName); - if (!f.exists()) continue; + if (!shouldDisplayMapOnWorldmap(map.mapName)) continue; + displayedMapNames.add(map.mapName); offsetWorldmapTo.x = Math.min(offsetWorldmapTo.x, map.worldPosition.x); offsetWorldmapTo.y = Math.min(offsetWorldmapTo.y, map.worldPosition.y); } + StringBuffer mapsAsHtml = new StringBuffer(); for (WorldMapSegmentMap segmentMap : segment.maps.values()) { File f = WorldMapController.getFileForMap(segmentMap.mapName); if (!f.exists()) continue; - PredefinedMap map = world.maps.findPredefinedMap(segmentMap.mapName); - - sb + Size size = getMapSize(segmentMap, world); + mapsAsHtml .append(""); + if (AndorsTrailApplication.DEVELOPMENT_DEBUGMESSAGES) mapsAsHtml.append("\n"); + } + + StringBuffer namedAreasAsHtml = new StringBuffer(); + for (NamedWorldMapArea area : segment.namedAreas.values()) { + CoordRect r = determineNamedAreaBoundary(area, segment, world, displayedMapNames); + if (r == null) continue; + namedAreasAsHtml + .append("
") + .append(area.name) + .append("
"); + if (AndorsTrailApplication.DEVELOPMENT_DEBUGMESSAGES) namedAreasAsHtml.append("\n"); } return res.getString(R.string.worldmap_template) - .replace("{{maps}}", sb.toString()) + .replace("{{maps}}", mapsAsHtml.toString()) + .replace("{{areas}}", namedAreasAsHtml.toString()) .replace("{{offsetx}}", Integer.toString(offsetWorldmapTo.x * WorldMapController.WORLDMAP_DISPLAY_TILESIZE)) .replace("{{offsety}}", Integer.toString(offsetWorldmapTo.y * WorldMapController.WORLDMAP_DISPLAY_TILESIZE)); } + private static Size getMapSize(WorldMapSegmentMap map, WorldContext world) { + return world.maps.findPredefinedMap(map.mapName).size; + } + + private static CoordRect determineNamedAreaBoundary(NamedWorldMapArea area, WorldMapSegment segment, WorldContext world, HashSet displayedMapNames) { + Coord topLeft = null; + Coord bottomRight = null; + + for (String mapName : area.mapNames) { + if (!displayedMapNames.contains(mapName)) continue; + WorldMapSegmentMap map = segment.maps.get(mapName); + Size size = getMapSize(map, world); + if (topLeft == null) { + topLeft = new Coord(map.worldPosition); + } else { + topLeft.x = Math.min(topLeft.x, map.worldPosition.x); + topLeft.y = Math.min(topLeft.y, map.worldPosition.y); + } + if (bottomRight == null) { + bottomRight = new Coord(map.worldPosition.x + size.width, map.worldPosition.y + size.height); + } else { + bottomRight.x = Math.max(bottomRight.x, map.worldPosition.x + size.width); + bottomRight.y = Math.max(bottomRight.y, map.worldPosition.y + size.height); + } + } + if (topLeft == null) return null; + return new CoordRect(topLeft, new Size(bottomRight.x - topLeft.x, bottomRight.y - topLeft.y)); + } + private static void updateWorldMapSegment(Resources res, WorldContext world, String segmentName) throws IOException { String mapAsHtml = getWorldMapSegmentAsHtml(res, world, segmentName); File outputFile = getCombinedWorldMapFile(segmentName); diff --git a/AndorsTrail/src/com/gpl/rpg/AndorsTrail/model/map/WorldMapSegment.java b/AndorsTrail/src/com/gpl/rpg/AndorsTrail/model/map/WorldMapSegment.java index 1c529af47..8b1a084d6 100644 --- a/AndorsTrail/src/com/gpl/rpg/AndorsTrail/model/map/WorldMapSegment.java +++ b/AndorsTrail/src/com/gpl/rpg/AndorsTrail/model/map/WorldMapSegment.java @@ -1,12 +1,14 @@ package com.gpl.rpg.AndorsTrail.model.map; import java.util.HashMap; +import java.util.HashSet; import com.gpl.rpg.AndorsTrail.util.Coord; public final class WorldMapSegment { public final String name; public final HashMap maps = new HashMap(); + public final HashMap namedAreas = new HashMap(); public WorldMapSegment(String name) { this.name = name; @@ -22,4 +24,17 @@ public final class WorldMapSegment { this.worldPosition = worldPosition; } } + + // Towns, cities, villages, taverns, named dungeons + public static final class NamedWorldMapArea { + public final String id; + public final String name; + public final String type; // "settlement" or "other" + public final HashSet mapNames = new HashSet(); + public NamedWorldMapArea(String id, String name, String type) { + this.id = id; + this.name = name; + this.type = type; + } + } } diff --git a/AndorsTrail/src/com/gpl/rpg/AndorsTrail/resource/parsers/WorldMapParser.java b/AndorsTrail/src/com/gpl/rpg/AndorsTrail/resource/parsers/WorldMapParser.java index 20c8c801c..16513d291 100644 --- a/AndorsTrail/src/com/gpl/rpg/AndorsTrail/resource/parsers/WorldMapParser.java +++ b/AndorsTrail/src/com/gpl/rpg/AndorsTrail/resource/parsers/WorldMapParser.java @@ -1,6 +1,7 @@ package com.gpl.rpg.AndorsTrail.resource.parsers; import java.io.IOException; +import java.util.ArrayList; import org.xmlpull.v1.XmlPullParserException; @@ -9,9 +10,11 @@ import android.content.res.XmlResourceParser; import com.gpl.rpg.AndorsTrail.model.map.MapCollection; import com.gpl.rpg.AndorsTrail.model.map.WorldMapSegment; +import com.gpl.rpg.AndorsTrail.model.map.WorldMapSegment.NamedWorldMapArea; import com.gpl.rpg.AndorsTrail.model.map.WorldMapSegment.WorldMapSegmentMap; import com.gpl.rpg.AndorsTrail.util.Coord; import com.gpl.rpg.AndorsTrail.util.L; +import com.gpl.rpg.AndorsTrail.util.Pair; import com.gpl.rpg.AndorsTrail.util.XmlResourceParserUtils; public final class WorldMapParser { @@ -41,6 +44,7 @@ public final class WorldMapParser { String segmentName = xrp.getAttributeValue(null, "id"); final WorldMapSegment segment = new WorldMapSegment(segmentName); + final ArrayList> mapsInNamedAreas = new ArrayList>(); XmlResourceParserUtils.readCurrentTagUntilEnd(xrp, new XmlResourceParserUtils.TagHandler() { @Override public void handleTag(XmlResourceParser xrp, String tagName) throws XmlPullParserException, IOException { @@ -52,10 +56,22 @@ public final class WorldMapParser { ); WorldMapSegmentMap map = new WorldMapSegmentMap(mapName, mapPosition); segment.maps.put(mapName, map); + + String namedArea = xrp.getAttributeValue(null, "area"); + if (namedArea != null) mapsInNamedAreas.add(new Pair(mapName, namedArea)); + } else if (tagName.equals("namedarea")) { + String id = xrp.getAttributeValue(null, "id"); + String name = xrp.getAttributeValue(null, "name"); + String type = xrp.getAttributeValue(null, "type"); + segment.namedAreas.put(id, new NamedWorldMapArea(id, name, type)); } } }); + for (Pair m : mapsInNamedAreas) { + segment.namedAreas.get(m.second).mapNames.add(m.first); + } + return segment; }