From: Stefan F. <ste...@us...> - 2010-03-28 17:06:04
|
Update of /cvsroot/rails/18xx/rails/game In directory sfp-cvsdas-1.v30.ch3.sourceforge.com:/tmp/cvs-serv17978/rails/game Modified Files: TileI.java MapHex.java TileManager.java GameManagerI.java GameManager.java Tile.java Log Message: Added MapCorrection Mode and some refactoring on the CorrectionClasses Index: Tile.java =================================================================== RCS file: /cvsroot/rails/18xx/rails/game/Tile.java,v retrieving revision 1.36 retrieving revision 1.37 diff -C2 -d -r1.36 -r1.37 *** Tile.java 31 Jan 2010 22:22:28 -0000 1.36 --- Tile.java 28 Mar 2010 17:05:55 -0000 1.37 *************** *** 10,14 **** import rails.util.Tag; ! public class Tile extends ModelObject implements TileI, StationHolder { /** The 'internal id', identifying the tile in the XML files */ --- 10,14 ---- import rails.util.Tag; ! public class Tile extends ModelObject implements TileI, StationHolder, Comparable<TileI> { /** The 'internal id', identifying the tile in the XML files */ *************** *** 373,376 **** --- 373,390 ---- } + /** + * Get all possible upgrades for a specific tile + * + * @return A List of valid upgrade TileI objects. + */ + + public List<TileI> getAllUpgrades() { + List<TileI> upgr = new ArrayList<TileI>(); + for (Upgrade upgrade : upgrades) { + upgr.add(upgrade.getTile()); + } + return upgr; + } + /** Get a delimited list of all possible upgrades, regardless current phase */ public String getUpgradesString(MapHex hex) { *************** *** 461,465 **** --- 475,495 ---- return quantity; } + + @Override + public String toString() { + return "Tile Id=" + externalId; + } + /** ordering of tiles based first on colour, then on external id */ + public int compareTo(TileI anotherTile) { + Integer colour = this.getColourNumber(); + int result = colour.compareTo(anotherTile.getColourNumber()); + if (result == 0) { + Integer externalId = this.getExternalId(); + result = externalId.compareTo(anotherTile.getExternalId()); + } + return result; + } + protected class Upgrade { Index: TileManager.java =================================================================== RCS file: /cvsroot/rails/18xx/rails/game/TileManager.java,v retrieving revision 1.15 retrieving revision 1.16 diff -C2 -d -r1.15 -r1.16 *** TileManager.java 23 Mar 2010 18:44:38 -0000 1.15 --- TileManager.java 28 Mar 2010 17:05:55 -0000 1.16 *************** *** 1 **** ! /* $Header$ */ package rails.game; import java.util.*; import rails.util.LocalText; import rails.util.Tag; public class TileManager implements ConfigurableComponentI { protected Map<Integer, TileI> tileMap = new HashMap<Integer, TileI>(); protected List<Integer> tileIds = new ArrayList<Integer>(); // private static List<String> directories = new ArrayList<String>(); private List<String> directories = new ArrayList<String>(); /** * No-args constructor. */ public TileManager() { } /** * @see rails.game.ConfigurableComponentI#configureFromXML(org.w3c.dom.Element) */ public void configureFromXML(Tag tileSetTop) throws ConfigurationException { /* * Note: prefix se is used for elements from TileSet.xml, prefix te for * elements from Tiles.xml. */ String tileDefFileName = tileSetTop.getAttributeAsString("tiles"); if (tileDefFileName == null) throw new ConfigurationException(LocalText.getText("NoTilesXML")); //directories.add("data/" + ComponentManager.getGameName()); directories.add("data/" + GameManager.getInstance().getGameName()); Tag tileDefTop = Tag.findTopTagInFile(tileDefFileName, directories, "Tiles"); if (tileDefTop == null) throw new ConfigurationException(LocalText.getText("NoTilesTag")); List<Tag> tileSetList = tileSetTop.getChildren("Tile"); List<Tag> tileDefList = tileDefTop.getChildren("Tile"); /* * The XML files TileSet.xml and Tiles.xml are read side by side, as * each one configures different tile aspects. The reason for having two * XML files is, that Tiles.xml defines per-tile aspects that are the * same for all games (such as the colour, tracks and stations; this * file is an automatically generated subset of the generic file * tiles/Tiles.xml), whereas TileSet.xml specifies the aspects that are * (or can be) specific to each rails.game (such as the possible * upgrades). <p>TileSet.xml is leading. */ int tileId; TileI tile; // Creates maps to the tile definitions in both files. Map<Integer, Tag> tileSetMap = new HashMap<Integer, Tag>(); Map<Integer, Tag> tileDefMap = new HashMap<Integer, Tag>(); for (Tag tileSetTag : tileSetList) { tileId = tileSetTag.getAttributeAsInteger("id"); /* * Check for duplicates (this also covers missing tile ids, as this * returns 0, and we always have a tile numbered 0! */ if (tileSetMap.containsKey(tileId)) { throw new ConfigurationException(LocalText.getText( "DuplicateTilesetID", String.valueOf(tileId))); } tileSetMap.put(tileId, tileSetTag); tileIds.add(tileId); } for (Tag tileDefTag : tileDefList) { tileId = tileDefTag.getAttributeAsInteger("id"); /* * Check for duplicates (this also covers missing tile ids, as this * returns 0, and we always have a tile numbered 0! */ if (tileDefMap.containsKey(tileId)) { throw new ConfigurationException(LocalText.getText( "DuplicateTileID", String.valueOf(tileId))); } else if (!tileSetMap.containsKey(tileId)) { throw new ConfigurationException(LocalText.getText( "TileMissingInTileSet", String.valueOf(tileId))); } tileDefMap.put(tileId, tileDefTag); } // Create the Tile objects (must be done before further parsing) for (Integer id : tileSetMap.keySet()) { tile = new Tile(id); tileMap.put(id, tile); } // Finally, parse the <Tile> subtags for (Integer id : tileMap.keySet()) { tile = tileMap.get(id); tile.configureFromXML(tileSetMap.get(id), tileDefMap.get(id)); } } public void finishConfiguration (GameManagerI gameManager) throws ConfigurationException { for (TileI tile : tileMap.values()) { tile.finishConfiguration(this); } } public TileI getTile(int id) { return tileMap.get(id); } /** Get the tile IDs in the XML definition sequence */ public List<Integer> getTileIds() { return tileIds; } } \ No newline at end of file --- 1 ---- ! /* $Header$ */ package rails.game; import java.util.*; import rails.util.LocalText; import rails.util.Tag; public class TileManager implements ConfigurableComponentI { protected Map<Integer, TileI> tileMap = new HashMap<Integer, TileI>(); protected List<Integer> tileIds = new ArrayList<Integer>(); // private static List<String> directories = new ArrayList<String>(); private List<String> directories = new ArrayList<String>(); /** * No-args constructor. */ public TileManager() { } /** * @see rails.game.ConfigurableComponentI#configureFromXML(org.w3c.dom.Element) */ public void configureFromXML(Tag tileSetTop) throws ConfigurationException { /* * Note: prefix se is used for elements from TileSet.xml, prefix te for * elements from Tiles.xml. */ String tileDefFileName = tileSetTop.getAttributeAsString("tiles"); if (tileDefFileName == null) throw new ConfigurationException(LocalText.getText("NoTilesXML")); //directories.add("data/" + ComponentManager.getGameName()); directories.add("data/" + GameManager.getInstance().getGameName()); Tag tileDefTop = Tag.findTopTagInFile(tileDefFileName, directories, "Tiles"); if (tileDefTop == null) throw new ConfigurationException(LocalText.getText("NoTilesTag")); List<Tag> tileSetList = tileSetTop.getChildren("Tile"); List<Tag> tileDefList = tileDefTop.getChildren("Tile"); /* * The XML files TileSet.xml and Tiles.xml are read side by side, as * each one configures different tile aspects. The reason for having two * XML files is, that Tiles.xml defines per-tile aspects that are the * same for all games (such as the colour, tracks and stations; this * file is an automatically generated subset of the generic file * tiles/Tiles.xml), whereas TileSet.xml specifies the aspects that are * (or can be) specific to each rails.game (such as the possible * upgrades). <p>TileSet.xml is leading. */ int tileId; TileI tile; // Creates maps to the tile definitions in both files. Map<Integer, Tag> tileSetMap = new HashMap<Integer, Tag>(); Map<Integer, Tag> tileDefMap = new HashMap<Integer, Tag>(); for (Tag tileSetTag : tileSetList) { tileId = tileSetTag.getAttributeAsInteger("id"); /* * Check for duplicates (this also covers missing tile ids, as this * returns 0, and we always have a tile numbered 0! */ if (tileSetMap.containsKey(tileId)) { throw new ConfigurationException(LocalText.getText( "DuplicateTilesetID", String.valueOf(tileId))); } tileSetMap.put(tileId, tileSetTag); tileIds.add(tileId); } for (Tag tileDefTag : tileDefList) { tileId = tileDefTag.getAttributeAsInteger("id"); /* * Check for duplicates (this also covers missing tile ids, as this * returns 0, and we always have a tile numbered 0! */ if (tileDefMap.containsKey(tileId)) { throw new ConfigurationException(LocalText.getText( "DuplicateTileID", String.valueOf(tileId))); } else if (!tileSetMap.containsKey(tileId)) { throw new ConfigurationException(LocalText.getText( "TileMissingInTileSet", String.valueOf(tileId))); } tileDefMap.put(tileId, tileDefTag); } // Create the Tile objects (must be done before further parsing) for (Integer id : tileSetMap.keySet()) { tile = new Tile(id); tileMap.put(id, tile); } // Finally, parse the <Tile> subtags for (Integer id : tileMap.keySet()) { tile = tileMap.get(id); tile.configureFromXML(tileSetMap.get(id), tileDefMap.get(id)); } } public void finishConfiguration (GameManagerI gameManager) throws ConfigurationException { for (TileI tile : tileMap.values()) { tile.finishConfiguration(this); } } public TileI getTile(int id) { return tileMap.get(id); } /** Get the tile IDs in the XML definition sequence */ public List<Integer> getTileIds() { return tileIds; } /** returns the set of all possible upgrade tiles */ public List<TileI> getAllUpgrades(TileI tile) { TreeSet<TileI> tileSet = new TreeSet<TileI>(); return new ArrayList<TileI>(recursiveUpgrades(tile, tileSet)); } private TreeSet<TileI> recursiveUpgrades(TileI tile, TreeSet<TileI> tileSet) { tileSet.add(tile); List<TileI> directUpgrades = tile.getAllUpgrades(); for (TileI upgrade:directUpgrades) if (!tileSet.contains(upgrade)) tileSet = recursiveUpgrades(upgrade, tileSet); return tileSet; } } \ No newline at end of file Index: GameManager.java =================================================================== RCS file: /cvsroot/rails/18xx/rails/game/GameManager.java,v retrieving revision 1.95 retrieving revision 1.96 diff -C2 -d -r1.95 -r1.96 *** GameManager.java 27 Mar 2010 18:44:24 -0000 1.95 --- GameManager.java 28 Mar 2010 17:05:55 -0000 1.96 *************** *** 55,58 **** --- 55,62 ---- protected TileManager tileManager; protected Bank bank; + + // map of correctionManagers + protected Map<CorrectionType, CorrectionManagerI> correctionManagers = + new HashMap<CorrectionType, CorrectionManagerI>(); protected String gameName; *************** *** 779,783 **** } ! if (result && !(action instanceof GameAction)) { new AddToList<PossibleAction>(executedActions, action, "ExecutedActions"); --- 783,787 ---- } ! if (result && !(action instanceof GameAction) && action.hasActed()) { new AddToList<PossibleAction>(executedActions, action, "ExecutedActions"); *************** *** 792,796 **** // when setting possible actions if (action != null) { ! if (result && !(action instanceof GameAction)) { if (moveStack.isOpen()) moveStack.finish(); } else { --- 796,800 ---- // when setting possible actions if (action != null) { ! if (result && !(action instanceof GameAction) && action.hasActed()) { if (moveStack.isOpen()) moveStack.finish(); } else { *************** *** 831,835 **** // If any Correction is active for (CorrectionType ct:EnumSet.allOf(CorrectionType.class)) { ! CorrectionManager cm = ct.getManager(this); if (cm.isActive()) { possibleActions.clear(); --- 835,839 ---- // If any Correction is active for (CorrectionType ct:EnumSet.allOf(CorrectionType.class)) { ! CorrectionManagerI cm = getCorrectionManager(ct); if (cm.isActive()) { possibleActions.clear(); *************** *** 837,855 **** } - // if (!activeCorrections.isEmpty()) { - // // remove all other actions - // possibleActions.clear(); - // // and set GuiHints for corrections - removed - // } - - // CorrectionMode Actions - // EnumSet<CorrectionType> possibleCorrections = EnumSet.allOf(CorrectionType.class); - // for (CorrectionType ct:possibleCorrections) - // possibleActions.add( - // new CorrectionModeAction(ct, activeCorrections.contains(ct))); // Correction Actions for (CorrectionType ct:EnumSet.allOf(CorrectionType.class)) { ! CorrectionManager cm = ct.getManager(this); possibleActions.addAll(cm.createCorrections()); } --- 841,848 ---- } // Correction Actions for (CorrectionType ct:EnumSet.allOf(CorrectionType.class)) { ! CorrectionManagerI cm = getCorrectionManager(ct); possibleActions.addAll(cm.createCorrections()); } *************** *** 860,890 **** boolean result = false; - // if (a instanceof CorrectionModeAction) { - // CorrectionModeAction cma = (CorrectionModeAction)a; - // CorrectionType ct = cma.getCorrection(); - // moveStack.start(false); - // new SetChange<CorrectionType> - // (activeCorrections, ct, !cma.isActive()); - // if (!cma.isActive()) { - // String text = LocalText.getText("CorrectionModeActivate", - // getCurrentPlayer().getName(), - // LocalText.getText(ct.name()) - // ); - // ReportBuffer.add(text); - // DisplayBuffer.add(text); - // } - // else { - // ReportBuffer.add(LocalText.getText("CorrectionModeDeactivate", - // getCurrentPlayer().getName(), - // LocalText.getText(ct.name()) - // )); - // } - // result = true; - // } - if (a instanceof CorrectionAction) { CorrectionAction ca= (CorrectionAction)a; CorrectionType ct = ca.getCorrectionType(); ! CorrectionManager cm = ct.getManager(this); result = cm.executeCorrection(ca); } --- 853,860 ---- boolean result = false; if (a instanceof CorrectionAction) { CorrectionAction ca= (CorrectionAction)a; CorrectionType ct = ca.getCorrectionType(); ! CorrectionManagerI cm = getCorrectionManager(ct); result = cm.executeCorrection(ca); } *************** *** 1457,1459 **** --- 1427,1441 ---- return guiHints; } + + public CorrectionManagerI getCorrectionManager(CorrectionType ct) { + CorrectionManagerI cm = correctionManagers.get(ct); + if (cm == null) { + cm=ct.newCorrectionManager(this); + correctionManagers.put(ct, cm); + log.debug("Added CorrectionManager for " + ct); + } + return cm; + } + } + Index: MapHex.java =================================================================== RCS file: /cvsroot/rails/18xx/rails/game/MapHex.java,v retrieving revision 1.39 retrieving revision 1.40 diff -C2 -d -r1.39 -r1.40 *** MapHex.java 24 Feb 2010 22:10:45 -0000 1.39 --- MapHex.java 28 Mar 2010 17:05:55 -0000 1.40 *************** *** 62,65 **** --- 62,66 ---- protected TileI currentTile; protected int currentTileRotation; + protected int preprintedTileRotation; protected int[] tileCost; protected String cityName; *************** *** 163,167 **** preprintedTileId = tag.getAttributeAsInteger("tile", -999); ! currentTileRotation = tag.getAttributeAsInteger("orientation", 0); impassable = tag.getAttributeAsString("impassable"); --- 164,169 ---- preprintedTileId = tag.getAttributeAsInteger("tile", -999); ! preprintedTileRotation = tag.getAttributeAsInteger("orientation", 0); ! currentTileRotation = preprintedTileRotation; impassable = tag.getAttributeAsString("impassable"); *************** *** 289,292 **** --- 291,298 ---- return preprintedTileId; } + + public int getPreprintedTileRotation() { + return preprintedTileRotation; + } /** *************** *** 346,355 **** /** ! * Prepare a tile upgrade. The actual tile replacement is done in ! * replaceTile(), via a TileMove object. */ public void upgrade(LayTile action) { TileI newTile = action.getLaidTile(); int newRotation = action.getOrientation(); City newCity; --- 352,372 ---- /** ! * new wrapper function for the LayTile action that calls the actual ! * upgrade mehod ! * @param action executed LayTile action */ public void upgrade(LayTile action) { TileI newTile = action.getLaidTile(); int newRotation = action.getOrientation(); + Map<String, Integer> relaidTokens = action.getRelaidBaseTokens(); + + upgrade(newTile, newRotation, relaidTokens); + } + + /** + * Prepare a tile upgrade. The actual tile replacement is done in + * replaceTile(), via a TileMove object. + */ + public void upgrade(TileI newTile, int newRotation, Map<String, Integer> relaidTokens) { City newCity; *************** *** 357,361 **** List<City> newCities; - Map<String, Integer> relaidTokens = action.getRelaidBaseTokens(); if (relaidTokens == null) relaidTokens = new HashMap<String, Integer>(); --- 374,377 ---- Index: TileI.java =================================================================== RCS file: /cvsroot/rails/18xx/rails/game/TileI.java,v retrieving revision 1.16 retrieving revision 1.17 diff -C2 -d -r1.16 -r1.17 *** TileI.java 30 Oct 2009 21:53:03 -0000 1.16 --- TileI.java 28 Mar 2010 17:05:55 -0000 1.17 *************** *** 46,49 **** --- 46,51 ---- public List<TileI> getUpgrades(MapHex hex, PhaseI phase); + public List<TileI> getAllUpgrades(); + public List<TileI> getValidUpgrades(MapHex hex, PhaseI phase); Index: GameManagerI.java =================================================================== RCS file: /cvsroot/rails/18xx/rails/game/GameManagerI.java,v retrieving revision 1.30 retrieving revision 1.31 diff -C2 -d -r1.30 -r1.31 *** GameManagerI.java 27 Mar 2010 18:44:24 -0000 1.30 --- GameManagerI.java 28 Mar 2010 17:05:55 -0000 1.31 *************** *** 7,10 **** --- 7,12 ---- import rails.common.GuiHints; import rails.game.action.PossibleAction; + import rails.game.correct.CorrectionManagerI; + import rails.game.correct.CorrectionType; import rails.game.model.ModelObject; import rails.game.move.AddToList; *************** *** 195,198 **** public ReportBuffer getReportBuffer(); public GuiHints getUIHints(); ! } \ No newline at end of file --- 197,201 ---- public ReportBuffer getReportBuffer(); public GuiHints getUIHints(); ! ! public CorrectionManagerI getCorrectionManager(CorrectionType ct); } \ No newline at end of file |