From: <ev...@us...> - 2010-07-17 11:08:45
|
Revision: 1342 http://rails.svn.sourceforge.net/rails/?rev=1342&view=rev Author: evos Date: 2010-07-17 11:08:38 +0000 (Sat, 17 Jul 2010) Log Message: ----------- All addObject() and related methods now take a position argument. ObjectMove has an additional fromPosition attribute. Also toPosition, but that isn't used yet. This all is used to ensure that Undo always restores the original List order for moves executed via ObjectMove. Also fixed a bug that allowed price tokens to vanish into the empty upper left corner of the 1835 stock chart. Modified Paths: -------------- trunk/18xx/rails/game/Bank.java trunk/18xx/rails/game/City.java trunk/18xx/rails/game/Company.java trunk/18xx/rails/game/GameManager.java trunk/18xx/rails/game/MapHex.java trunk/18xx/rails/game/OperatingRound.java trunk/18xx/rails/game/Portfolio.java trunk/18xx/rails/game/PrivateCompany.java trunk/18xx/rails/game/PublicCompany.java trunk/18xx/rails/game/PublicCompanyI.java trunk/18xx/rails/game/StockMarket.java trunk/18xx/rails/game/Token.java trunk/18xx/rails/game/TokenHolder.java trunk/18xx/rails/game/TrainType.java trunk/18xx/rails/game/move/MoveableHolder.java trunk/18xx/rails/game/move/ObjectMove.java Property Changed: ---------------- trunk/18xx/ Property changes on: trunk/18xx ___________________________________________________________________ Modified: svn:ignore - *.bat *.txt .cvsignore .classpath .externalToolBuilders 18xx.zip deploy.xml .project zip.xml NewUIstuff.zip COMP.WPS rails.jar rails.jardesc Rails-1.0.1.jar my_my.properties log rails-1.0.1.jar rails*.zip rails-*.jar tools rails-?.*.* jar/* classes/* + *.bat *.txt .cvsignore .classpath .externalToolBuilders 18xx.zip deploy.xml .project zip.xml NewUIstuff.zip COMP.WPS rails.jar rails.jardesc Rails-1.0.1.jar my_my.properties log rails-1.0.1.jar rails*.zip rails-*.jar tools rails-?.*.* jar/* classes/* 18xx_autosave.rails 18xx_autosave.rails.tmp Modified: trunk/18xx/rails/game/Bank.java =================================================================== --- trunk/18xx/rails/game/Bank.java 2010-07-16 21:31:11 UTC (rev 1341) +++ trunk/18xx/rails/game/Bank.java 2010-07-17 11:08:38 UTC (rev 1342) @@ -31,8 +31,8 @@ /** Is the bank broken (remains true once set) */ private BooleanState broken = new BooleanState("Bank.broken", false); -// /** Is the bank just broken (returns true exactly once) */ -// private BooleanState brokenReported = new BooleanState("Bank.brokenReported", false); + // /** Is the bank just broken (returns true exactly once) */ + // private BooleanState brokenReported = new BooleanState("Bank.brokenReported", false); /** * The money format template. '@' is replaced by the numeric amount, the @@ -41,7 +41,7 @@ private String moneyFormat = null; protected static Logger log = - Logger.getLogger(Bank.class.getPackage().getName()); + Logger.getLogger(Bank.class.getPackage().getName()); public Bank() { @@ -98,7 +98,7 @@ List<PrivateCompanyI> privates = gameManager.getCompanyManager().getAllPrivateCompanies(); for (PrivateCompanyI priv : privates) { - ipo.addPrivate(priv); + ipo.addPrivate(priv, -1); } // Add public companies @@ -107,13 +107,13 @@ for (PublicCompanyI comp : companies) { for (PublicCertificateI cert : comp.getCertificates()) { if (cert.isInitiallyAvailable()) { - ipo.addCertificate(cert); - } else { - unavailable.addCertificate(cert); - } + ipo.addCertificate(cert, -1); + } else { + unavailable.addCertificate(cert, -1); + } } } -} + } /** * @return IPO Portfolio @@ -154,11 +154,11 @@ return broken.booleanValue(); } -// public boolean isJustBroken() { -// boolean result = broken.booleanValue() && !brokenReported.booleanValue(); -// brokenReported.set(true); -// return result; -// } + // public boolean isJustBroken() { + // boolean result = broken.booleanValue() && !brokenReported.booleanValue(); + // brokenReported.set(true); + // return result; + // } /** * @return Portfolio of stock in Bank Pool Modified: trunk/18xx/rails/game/City.java =================================================================== --- trunk/18xx/rails/game/City.java 2010-07-16 21:31:11 UTC (rev 1341) +++ trunk/18xx/rails/game/City.java 2010-07-17 11:08:38 UTC (rev 1342) @@ -37,12 +37,12 @@ private String trackEdges; protected static Logger log = - Logger.getLogger(City.class.getPackage().getName()); + Logger.getLogger(City.class.getPackage().getName()); public City(MapHex mapHex, int number, Station station) { this.mapHex = mapHex; this.number = number; - + uniqueId = mapHex.getName() + "_" + number; relatedStation = new GenericState<Station>("City_"+uniqueId+"_station", station); setRelatedStation(station); @@ -53,7 +53,7 @@ public String getName() { return mapHex.getName() + "/" + number; - + } /** @@ -75,9 +75,9 @@ this.relatedStation.set(relatedStation); slots = relatedStation.getBaseSlots(); trackEdges = - mapHex.getConnectionString(mapHex.getCurrentTile(), - mapHex.getCurrentTileRotation(), - relatedStation.getNumber()); + mapHex.getConnectionString(mapHex.getCurrentTile(), + mapHex.getCurrentTileRotation(), + relatedStation.getNumber()); } public void setSlots(int slots) { @@ -91,20 +91,28 @@ return uniqueId; } - public boolean addToken(TokenI token) { + public boolean addToken(TokenI token, int position) { if (tokens.contains(token)) { return false; } else { token.setHolder(this); - boolean result = tokens.add(token); - return result; + if (position == -1) { + return tokens.add(token); + } else { + try { + tokens.add(position, token); + return true; + } catch (IndexOutOfBoundsException e) { + return false; + } + } } } - public boolean addObject(Moveable object) { + public boolean addObject(Moveable object, int position) { if (object instanceof TokenI) { - return addToken((TokenI) object); + return addToken((TokenI) object, position); } else { return false; } @@ -133,7 +141,7 @@ public boolean hasTokenSlotsLeft() { return tokens.size() < slots; } - + public int getTokenSlotsLeft () { return slots - tokens.size(); } @@ -148,13 +156,13 @@ * @param company * @return true if this City already contains an instance of the specified * company's token. Do this by calling the hasTokenOf with Company Name. - * Using a tokens.contains(company) fails since the tokens are a ArrayList + * Using a tokens.contains(company) fails since the tokens are a ArrayList * of TokenI not a ArrayList of PublicCompanyI. */ public boolean hasTokenOf(PublicCompanyI company) { return hasTokenOf (company.getName()); } - + public boolean hasTokenOf (String companyName) { for (TokenI token : tokens) { if (token instanceof BaseToken @@ -165,6 +173,14 @@ return false; } + public int getListIndex (Moveable object) { + if (object instanceof BaseToken) { + return tokens.indexOf(object); + } else { + return -1; + } + } + public void setTokens(ArrayList<TokenI> tokens) { this.tokens = tokens; } Modified: trunk/18xx/rails/game/Company.java =================================================================== --- trunk/18xx/rails/game/Company.java 2010-07-16 21:31:11 UTC (rev 1341) +++ trunk/18xx/rails/game/Company.java 2010-07-17 11:08:38 UTC (rev 1342) @@ -13,13 +13,13 @@ import rails.util.Util; public abstract class Company implements CompanyI, ConfigurableComponentI, - Cloneable, Comparable<Company> { +Cloneable, Comparable<Company> { protected String name; protected String longName; protected CompanyTypeI type; protected int companyNumber; // For internal use - + /* Note: portfolio is used in two ways: * In private companies, it is primarily the portfolio that holds this private. * In public companies, it is the portfolio of this company. @@ -42,12 +42,12 @@ /** Closed state */ protected BooleanState closedObject; - + // Moved here from PrivayeCOmpany on behalf of 1835 protected List<SpecialPropertyI> specialProperties = null; - + protected static Logger log = - Logger.getLogger(Company.class.getPackage().getName()); + Logger.getLogger(Company.class.getPackage().getName()); public Company() { } @@ -60,7 +60,7 @@ /** Only to be called from subclasses */ public void configureFromXML(Tag tag) throws ConfigurationException { - + // Special properties Tag spsTag = tag.getChild("SpecialProperties"); if (spsTag != null) { @@ -71,7 +71,7 @@ className = spTag.getAttributeAsString("class"); if (!Util.hasValue(className)) throw new ConfigurationException( - "Missing class in private special property"); + "Missing class in private special property"); SpecialPropertyI sp = null; try { sp = (SpecialPropertyI) Class.forName(className).newInstance(); @@ -87,7 +87,7 @@ } } } - + /** * @return ArrayList of all special properties we have. */ @@ -214,30 +214,30 @@ * * Use addToken(MapHex hex) method instead. */ - public boolean addToken(CompanyI company) { + public boolean addToken(CompanyI company, int position) { return false; } @Override public String toString() { return getTypeName() + ": " + getCompanyNumber() + ". " + getName() - + " $" + this.getValue(); + + " $" + this.getValue(); } public boolean equals(CompanyI company) { if (this.companyNumber == company.getCompanyNumber() - && this.name.equals(company.getName()) - && this.type.equals(company.getType())) return true; + && this.name.equals(company.getName()) + && this.type.equals(company.getType())) return true; return false; } - + public int compareTo(Company otherCompany){ int result; // compare typeNames first result = this.getTypeName().compareTo(otherCompany.getTypeName()); // if same typeName then name - if (result == 0) + if (result == 0) result = this.getName().compareTo(otherCompany.getName()); return result; Modified: trunk/18xx/rails/game/GameManager.java =================================================================== --- trunk/18xx/rails/game/GameManager.java 2010-07-16 21:31:11 UTC (rev 1341) +++ trunk/18xx/rails/game/GameManager.java 2010-07-17 11:08:38 UTC (rev 1342) @@ -187,7 +187,7 @@ /** A List of available game options */ protected List<GameOption> availableGameOptions = new ArrayList<GameOption>(); - + /** indicates that the recoverySave already issued a warning, avoids displaying several warnings */ protected boolean recoverySaveWarning = true; @@ -812,7 +812,7 @@ if (!isGameOver() && possibleActions.containsOnlyPass()) { result = process(possibleActions.getList().get(0)); } - + // moveStack closing is done here to allow state changes to occur // when setting possible actions if (action != null) { @@ -943,7 +943,7 @@ * */ protected void recoverySave() { if (Config.get("save.recovery.active", "yes").equalsIgnoreCase("no")) return; - + String filePath = Config.get("save.recovery.filepath", "18xx_autosave.rails"); // create temporary new save file File tempFile = null; @@ -978,7 +978,7 @@ recoverySaveWarning = false; return; } - + if (result) { log.debug("Renamed to recovery file, path = " + recoveryFile.getAbsolutePath()); if (!recoverySaveWarning) { @@ -992,12 +992,12 @@ } } } - + protected boolean save(GameAction saveAction) { File file = new File(saveAction.getFilepath()); return save(file, true, "SaveFailed"); } - + protected boolean save(File file, boolean displayErrorMessage, String errorMessageKey) { boolean result = false; @@ -1500,16 +1500,24 @@ * @param object The object to add. * @return True if successful. */ - public boolean addObject(Moveable object) { + public boolean addObject(Moveable object, int position) { if (object instanceof SpecialPropertyI) { SpecialPropertyI sp = (SpecialPropertyI) object; sp.setHolder(null); - return addSpecialProperty(sp); + return addSpecialProperty(sp, position); } else { return false; } } + public int getListIndex (Moveable object) { + if (object instanceof SpecialPropertyI) { + return commonSpecialProperties.indexOf(object); + } else { + return -1; + } + } + /** * Remove an object. * @@ -1524,12 +1532,21 @@ } } - public boolean addSpecialProperty(SpecialPropertyI property) { + public boolean addSpecialProperty(SpecialPropertyI property, int position) { if (commonSpecialProperties == null) { commonSpecialProperties = new ArrayList<SpecialPropertyI>(2); } - return commonSpecialProperties.add(property); + if (position == -1) { + return commonSpecialProperties.add(property); + } else { + try { + commonSpecialProperties.add(position, property); + return true; + } catch (IndexOutOfBoundsException e) { + return false; + } + } } /** Modified: trunk/18xx/rails/game/MapHex.java =================================================================== --- trunk/18xx/rails/game/MapHex.java 2010-07-16 21:31:11 UTC (rev 1341) +++ trunk/18xx/rails/game/MapHex.java 2010-07-17 11:08:38 UTC (rev 1342) @@ -38,15 +38,15 @@ * tiles the above picture should be rotated 30 degrees clockwise. */ public class MapHex extends ModelObject implements ConfigurableComponentI, - StationHolder, TokenHolder { +StationHolder, TokenHolder { public static final int EW = 0; public static final int NS = 1; private static final String[] ewOrNames = - { "SW", "W", "NW", "NE", "E", "SE" }; + { "SW", "W", "NW", "NE", "E", "SE" }; private static final String[] nsOrNames = - { "S", "SW", "NW", "N", "NE", "SE" }; + { "S", "SW", "NW", "N", "NE", "SE" }; // Coordinates as used in the rails.ui.swing.hexmap package protected int x; @@ -87,19 +87,19 @@ protected Map<Integer, City> mCities; /* - * changed to state variable to fix undo bug #2954645 + * changed to state variable to fix undo bug #2954645 * null as default implies false - see isBlocked() */ private BooleanState isBlockedForTileLays = null; - - /** + + /** * Is the hex initially blocked for token lays (e.g. when a home base * must first be laid)? <p> * NOTE:<br>null means: blocked unless there is more free space than unlaid home bases,<br> * false means: blocked unless there is any free space.<br> - * This makes a difference for 1835 Berlin, which is home to PR, but - * the absence of a PR token does not block the third slot - * when the green tile is laid. + * This makes a difference for 1835 Berlin, which is home to PR, but + * the absence of a PR token does not block the third slot + * when the green tile is laid. */ private BooleanState isBlockedForTokenLays = null; @@ -108,14 +108,14 @@ /** Tokens that are not bound to a Station (City), such as Bonus tokens */ protected List<TokenI> offStationTokens; - + /** Storage of revenueBonus that are bound to the hex */ protected List<RevenueBonusTemplate> revenueBonuses = null; protected MapManager mapManager = null; protected static Logger log = - Logger.getLogger(MapHex.class.getPackage().getName()); + Logger.getLogger(MapHex.class.getPackage().getName()); public MapHex(MapManager mapManager) { this.mapManager = mapManager; @@ -158,7 +158,7 @@ y = (row + 1) / 2; } } else - // letters go vertical (normal case) + // letters go vertical (normal case) { row = letter - '@'; column = number; @@ -190,13 +190,13 @@ // City name cityName = tag.getAttributeAsString("city", ""); if (Util.hasValue(cityName)) { - infoText += " " + cityName; + infoText += " " + cityName; } - + if (tag.getAttributeAsString("unlaidHomeBlocksTokens") != null) { setBlockedForTokenLays(tag.getAttributeAsBoolean("unlaidHomeBlocksTokens", false)); } - + // revenue bonus List<Tag> bonusTags = tag.getChildren("RevenueBonus"); if (bonusTags != null) { @@ -216,7 +216,7 @@ // stations. cities = new ArrayList<City>(4); mCities = new HashMap<Integer, City>(4); - for (Station s : currentTile.getStations()) { + for (Station s : currentTile.getStations()) { // sid, type, value, slots City c = new City(this, s.getNumber(), s); cities.add(c); @@ -251,8 +251,8 @@ */ TileI tile = neighbour.getCurrentTile(); if (!tile.isUpgradeable() - && !tile.hasTracks(3 + direction - - neighbour.getCurrentTileRotation())) + && !tile.hasTracks(3 + direction + - neighbour.getCurrentTileRotation())) return false; return true; @@ -319,7 +319,7 @@ public int getPreprintedTileId() { return preprintedTileId; } - + public int getPreprintedTileRotation() { return preprintedTileRotation; } @@ -374,7 +374,7 @@ return 0; } } - + public int[] getTileCostAsArray(){ return tileCost; } @@ -388,10 +388,10 @@ 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. @@ -427,12 +427,12 @@ if (citiesToStations.containsKey(city)) continue; Station oldStation = city.getRelatedStation(); int[] oldTrackEnds = - getTrackEndPoints(currentTile, currentTileRotation, - oldStation); + getTrackEndPoints(currentTile, currentTileRotation, + oldStation); if (oldTrackEnds.length == 0) continue; station: for (Station newStation : newTile.getStations()) { int[] newTrackEnds = - getTrackEndPoints(newTile, newRotation, newStation); + getTrackEndPoints(newTile, newRotation, newStation); for (int i = 0; i < oldTrackEnds.length; i++) { for (int j = 0; j < newTrackEnds.length; j++) { if (oldTrackEnds[i] == newTrackEnds[j]) { @@ -456,27 +456,27 @@ } - // Assign the new Stations to the existing cities - for (City city : citiesToStations.keySet()) { - Station newStation = citiesToStations.get(city); - Station oldStation = city.getRelatedStation(); - city.setRelatedStation(newStation); - city.setSlots(newStation.getBaseSlots()); - newTracks = - getConnectionString(newTile, - newRotation, - newStation.getNumber()); - city.setTrackEdges(newTracks); - log.debug("Assigned " - + city.getUniqueId() - + " from " - + oldStation.getId() - + " " - + getConnectionString(currentTile, - currentTileRotation, - oldStation.getNumber()) - + " to " + newStation.getId() + " " - + newTracks); + // Assign the new Stations to the existing cities + for (City city : citiesToStations.keySet()) { + Station newStation = citiesToStations.get(city); + Station oldStation = city.getRelatedStation(); + city.setRelatedStation(newStation); + city.setSlots(newStation.getBaseSlots()); + newTracks = + getConnectionString(newTile, + newRotation, + newStation.getNumber()); + city.setTrackEdges(newTracks); + log.debug("Assigned " + + city.getUniqueId() + + " from " + + oldStation.getId() + + " " + + getConnectionString(currentTile, + currentTileRotation, + oldStation.getNumber()) + + " to " + newStation.getId() + " " + + newTracks); } newCities = cities; @@ -490,7 +490,7 @@ Map<Integer, City> mNewCities = new HashMap<Integer, City>(4); Map<City, City> oldToNewCities = new HashMap<City, City>(); Map<Station, City> newStationsToCities = - new HashMap<Station, City>(); + new HashMap<Station, City>(); // Scan the old cities/stations, // and assign new stations where tracks correspond @@ -499,64 +499,64 @@ int cityNumber = oldCity.getNumber(); Station oldStation = oldCity.getRelatedStation(); int[] oldTrackEnds = - getTrackEndPoints(currentTile, currentTileRotation, - oldStation); + getTrackEndPoints(currentTile, currentTileRotation, + oldStation); log.debug("Old city #" - + currentTile.getId() - + " city " - + oldCity.getNumber() - + ": " - + getConnectionString(currentTile, - currentTileRotation, oldStation.getNumber())); + + currentTile.getId() + + " city " + + oldCity.getNumber() + + ": " + + getConnectionString(currentTile, + currentTileRotation, oldStation.getNumber())); station: for (Station newStation : newTile.getStations()) { int[] newTrackEnds = - getTrackEndPoints(newTile, newRotation, newStation); + getTrackEndPoints(newTile, newRotation, newStation); log.debug("New station #" - + newTile.getId() - + " station " - + newStation.getNumber() - + ": " - + getConnectionString(newTile, newRotation, - newStation.getNumber())); + + newTile.getId() + + " station " + + newStation.getNumber() + + ": " + + getConnectionString(newTile, newRotation, + newStation.getNumber())); for (int i = 0; i < oldTrackEnds.length; i++) { for (int j = 0; j < newTrackEnds.length; j++) { if (oldTrackEnds[i] == newTrackEnds[j]) { // Match found! if (!newStationsToCities.containsKey(newStation)) { newCity = - new City(this, ++newCityNumber, - newStation); + new City(this, ++newCityNumber, + newStation); newCities.add(newCity); mNewCities.put(cityNumber, newCity); newStationsToCities.put(newStation, newCity); newCity.setSlots(newStation.getBaseSlots()); } else { newCity = - newStationsToCities.get(newStation); + newStationsToCities.get(newStation); } oldToNewCities.put(oldCity, newCity); newTracks = - getConnectionString(newTile, - newRotation, - newStation.getNumber()); + getConnectionString(newTile, + newRotation, + newStation.getNumber()); newCity.setTrackEdges(newTracks); log.debug("Assigned from " - + oldCity.getUniqueId() - + " #" - + currentTile.getId() - + "/" - + currentTileRotation - + " " - + oldStation.getId() - + " " - + getConnectionString(currentTile, - currentTileRotation, - oldStation.getNumber()) - + " to " + newCity.getUniqueId() - + " #" + newTile.getId() + "/" - + newRotation + " " - + newStation.getId() + " " - + newTracks); + + oldCity.getUniqueId() + + " #" + + currentTile.getId() + + "/" + + currentTileRotation + + " " + + oldStation.getId() + + " " + + getConnectionString(currentTile, + currentTileRotation, + oldStation.getNumber()) + + " to " + newCity.getUniqueId() + + " #" + newTile.getId() + "/" + + newRotation + " " + + newStation.getId() + " " + + newTracks); break station; } } @@ -599,12 +599,12 @@ + getConnectionString(currentTile, currentTileRotation, oldStation.getNumber()) - + " to " + newCity.getUniqueId() - + " #" + newTile.getId() + "/" - + newRotation + " " - + newCity.getRelatedStation().getId() + " " - + newCity.getTrackEdges()); - break station; + + " to " + newCity.getUniqueId() + + " #" + newTile.getId() + "/" + + newRotation + " " + + newCity.getRelatedStation().getId() + " " + + newCity.getTrackEdges()); + break station; } @@ -629,17 +629,17 @@ newStationsToCities.put(newStation, newCity); newCity.setSlots(newStation.getBaseSlots()); newTracks = - getConnectionString(newTile, newRotation, - newStation.getNumber()); + getConnectionString(newTile, newRotation, + newStation.getNumber()); newCity.setTrackEdges(newTracks); log.debug("New city added " + newCity.getUniqueId() + " #" - + newTile.getId() + "/" + newRotation + " " - + newStation.getId() + " " + newTracks); + + newTile.getId() + "/" + newRotation + " " + + newStation.getId() + " " + newTracks); } // Move the tokens Map<TokenI, TokenHolder> tokenDestinations = - new HashMap<TokenI, TokenHolder>(); + new HashMap<TokenI, TokenHolder>(); for (City oldCity : cities) { newCity = oldToNewCities.get(oldCity); @@ -648,35 +648,35 @@ if (token instanceof BaseToken) { // Check if the new city already has such a token PublicCompanyI company = - ((BaseToken) token).getCompany(); + ((BaseToken) token).getCompany(); for (TokenI token2 : newCity.getTokens()) { if (token2 instanceof BaseToken - && company == ((BaseToken) token2).getCompany()) { + && company == ((BaseToken) token2).getCompany()) { // No duplicate tokens in one city! tokenDestinations.put(token, company); log.debug("Duplicate token " - + token.getUniqueId() - + " moved from " - + oldCity.getName() + " to " - + company.getName()); + + token.getUniqueId() + + " moved from " + + oldCity.getName() + " to " + + company.getName()); ReportBuffer.add(LocalText.getText( "DuplicateTokenRemoved", - company.getName(), - getName() )); + company.getName(), + getName() )); continue oldtoken; } } } tokenDestinations.put(token, newCity); log.debug("Token " + token.getUniqueId() - + " moved from " + oldCity.getName() + " to " - + newCity.getName()); + + " moved from " + oldCity.getName() + " to " + + newCity.getName()); } - if (!tokenDestinations.isEmpty()) { - for (TokenI token : tokenDestinations.keySet()) { - token.moveTo(tokenDestinations.get(token)); - } + if (!tokenDestinations.isEmpty()) { + for (TokenI token : tokenDestinations.keySet()) { + token.moveTo(tokenDestinations.get(token)); } + } } else { log.debug("No new city!?"); } @@ -705,16 +705,16 @@ if (oldTile != currentTile) { new Exception("ERROR! Hex " + name + " wants to replace tile #" - + oldTile.getId() + " but has tile #" - + currentTile.getId() + "!").printStackTrace(); + + oldTile.getId() + " but has tile #" + + currentTile.getId() + "!").printStackTrace(); } if (currentTile != null) { currentTile.remove(this); } log.debug("On hex " + name + " replacing tile " + currentTile.getId() - + "/" + currentTileRotation + " by " + newTile.getId() + "/" - + newTileOrientation); + + "/" + currentTileRotation + " by " + newTile.getId() + "/" + + newTileOrientation); newTile.lay(this); @@ -727,12 +727,12 @@ for (City city : cities) { mCities.put(city.getNumber(), city); log.debug("Tile #" - + newTile.getId() - + " station " - + city.getNumber() - + " has tracks to " - + getConnectionString(newTile, newTileOrientation, - city.getRelatedStation().getNumber())); + + newTile.getId() + + " station " + + city.getNumber() + + " has tracks to " + + getConnectionString(newTile, newTileOrientation, + city.getRelatedStation().getNumber())); } } /* TODO: Further consequences to be processed here, e.g. new routes etc. */ @@ -744,8 +744,8 @@ public boolean layBaseToken(PublicCompanyI company, int station) { if (cities == null || cities.isEmpty()) { log.error("Tile " + getName() - + " has no station for home token of company " - + company.getName()); + + " has no station for home token of company " + + company.getName()); return false; } City city = mCities.get(station); @@ -757,15 +757,15 @@ } else { token.moveTo(city); update(); - - if (isHomeFor(company) - && isBlockedForTokenLays != null + + if (isHomeFor(company) + && isBlockedForTokenLays != null && isBlockedForTokenLays.booleanValue()) { // Assume that there is only one home base on such a tile, // so we don't need to check for other ones isBlockedForTokenLays.set(false); } - + return true; } } @@ -788,7 +788,7 @@ } } - public boolean addToken(TokenI token) { + public boolean addToken(TokenI token, int position) { if (offStationTokens == null) offStationTokens = new ArrayList<TokenI>(); @@ -796,7 +796,16 @@ return false; } else { token.setHolder(this); - return offStationTokens.add(token); + if (position == -1) { + return offStationTokens.add(token); + } else { + try { + offStationTokens.add(position, token); + return true; + } catch (IndexOutOfBoundsException e) { + return false; + } + } } } @@ -826,9 +835,9 @@ return offStationTokens.remove(token); } - public boolean addObject(Moveable object) { + public boolean addObject(Moveable object, int position) { if (object instanceof TokenI) { - return addToken((TokenI) object); + return addToken((TokenI) object, position); } else { return false; } @@ -842,6 +851,16 @@ } } + public int getListIndex (Moveable object) { + if (object instanceof TokenI) { + return offStationTokens.indexOf(object); + } else { + return -1; + } + } + + + public boolean hasTokenSlotsLeft(int station) { if (station == 0) station = 1; // Temp. fix for old save files City city = mCities.get(station); @@ -849,7 +868,7 @@ return city.hasTokenSlotsLeft(); } else { log.error("Invalid station " + station + ", max is " - + (cities.size() - 1)); + + (cities.size() - 1)); return false; } } @@ -890,7 +909,7 @@ for (City city : cities) { for (TokenI token : city.getTokens()) { if (token instanceof BaseToken - && ((BaseToken) token).getCompany() == company) { + && ((BaseToken) token).getCompany() == company) { return city.getNumber(); } } @@ -905,7 +924,7 @@ public City getCity(int cityNumber) { return mCities.get(cityNumber); } - + public City getRelatedCity(Station station) { City foundCity = null; for (City city:mCities.values()) { @@ -986,7 +1005,7 @@ return true; } else { log.debug("Hex " + name + " tile #" + currentTile.getId() - + " is not upgradable now"); + + " is not upgradable now"); return false; } } @@ -1013,7 +1032,7 @@ * C) City is undecided => check all cities if there is a slot left */ public boolean isBlockedForTokenLays(PublicCompanyI company, int cityNumber) { - + if (isHomeFor(company)) // Company can always lay a home base return false; @@ -1079,16 +1098,16 @@ } public String getInfo () { - return infoText; + return infoText; } public List<RevenueBonusTemplate> getRevenueBonuses() { return revenueBonuses; } - + public boolean equals(MapHex hex) { if (hex.getName().equals(getName()) && hex.row == row - && hex.column == column) return true; + && hex.column == column) return true; return false; } @@ -1138,7 +1157,7 @@ public String getConnectionString(int cityNumber) { int stationNumber = - mCities.get(cityNumber).getRelatedStation().getNumber(); + mCities.get(cityNumber).getRelatedStation().getNumber(); return getConnectionString(currentTile, currentTileRotation, stationNumber); } Modified: trunk/18xx/rails/game/OperatingRound.java =================================================================== --- trunk/18xx/rails/game/OperatingRound.java 2010-07-16 21:31:11 UTC (rev 1341) +++ trunk/18xx/rails/game/OperatingRound.java 2010-07-17 11:08:38 UTC (rev 1342) @@ -5,7 +5,8 @@ import rails.common.GuiDef; import rails.game.action.*; -import rails.game.correct.*; +import rails.game.correct.ClosePrivate; +import rails.game.correct.OperatingCost; import rails.game.move.CashMove; import rails.game.move.MapChange; import rails.game.special.*; @@ -28,9 +29,9 @@ /* sfy: using rails without map support */ protected boolean noMapMode = false; - + protected List<PublicCompanyI> companiesOperatedThisRound - = new ArrayList<PublicCompanyI> (); + = new ArrayList<PublicCompanyI> (); protected List<PublicCompanyI> operatingCompanies; @@ -47,19 +48,19 @@ protected List<LayTile> currentNormalTileLays = new ArrayList<LayTile>(); protected Map<String, Integer> tileLaysPerColour = - new HashMap<String, Integer>(); + new HashMap<String, Integer>(); protected List<LayBaseToken> currentNormalTokenLays = - new ArrayList<LayBaseToken>(); + new ArrayList<LayBaseToken>(); protected List<LayBaseToken> currentSpecialTokenLays = - new ArrayList<LayBaseToken>(); + new ArrayList<LayBaseToken>(); /** A List per player with owned companies that have excess trains */ protected Map<Player, List<PublicCompanyI>> excessTrainCompanies = null; protected List<TrainTypeI> trainsBoughtThisTurn = - new ArrayList<TrainTypeI>(4); + new ArrayList<TrainTypeI>(4); protected Map<PublicCompanyI, Integer> loansThisRound = null; @@ -71,17 +72,17 @@ public static final int SPLIT_ROUND_DOWN = 2; // More to the treasury -// protected static GameDef.OrStep[] steps = - protected GameDef.OrStep[] steps = - new GameDef.OrStep[] { - GameDef.OrStep.INITIAL, - GameDef.OrStep.LAY_TRACK, - GameDef.OrStep.LAY_TOKEN, - GameDef.OrStep.CALC_REVENUE, - GameDef.OrStep.PAYOUT, - GameDef.OrStep.BUY_TRAIN, - GameDef.OrStep.TRADE_SHARES, - GameDef.OrStep.FINAL }; + // protected static GameDef.OrStep[] steps = + protected GameDef.OrStep[] steps = + new GameDef.OrStep[] { + GameDef.OrStep.INITIAL, + GameDef.OrStep.LAY_TRACK, + GameDef.OrStep.LAY_TOKEN, + GameDef.OrStep.CALC_REVENUE, + GameDef.OrStep.PAYOUT, + GameDef.OrStep.BUY_TRAIN, + GameDef.OrStep.TRADE_SHARES, + GameDef.OrStep.FINAL }; protected boolean doneAllowed = false; @@ -95,10 +96,10 @@ super (gameManager); operatingCompanies = setOperatingCompanies(); - + // sfy NoMapMode noMapMode = GameOption.convertValueToBoolean(getGameOption("NoMapMode")); - + guiHints.setVisibilityHint(GuiDef.Panel.STOCK_MARKET, false); guiHints.setVisibilityHint(GuiDef.Panel.STATUS, true); guiHints.setActivePanel(GuiDef.Panel.MAP); @@ -111,7 +112,7 @@ ReportBuffer.add(LocalText.getText("START_OR", thisOrNumber)); privatesPayOut(); - + if (operatingCompanies.size() > 0) { StringBuilder msg = new StringBuilder(); @@ -170,7 +171,7 @@ /*--- Common OR checks ---*/ /* Check operating company */ if (action instanceof PossibleORAction - && !(action instanceof DiscardTrain)) { + && !(action instanceof DiscardTrain)) { PublicCompanyI company = ((PossibleORAction) action).getCompany(); if (company != operatingCompany) { DisplayBuffer.add(LocalText.getText("WrongCompany", @@ -290,9 +291,9 @@ // Must be correct company. if (!companyName.equals(operatingCompany.getName())) { errMsg = - LocalText.getText("WrongCompany", - companyName, - operatingCompany.getName() ); + LocalText.getText("WrongCompany", + companyName, + operatingCompany.getName() ); break; } // Must be correct step @@ -305,14 +306,14 @@ if (!getCurrentPhase().isTileColourAllowed(tile.getColourName())) { errMsg = - LocalText.getText("TileNotYetAvailable", - tile.getExternalId()); + LocalText.getText("TileNotYetAvailable", + tile.getExternalId()); break; } if (tile.countFreeTiles() == 0) { errMsg = - LocalText.getText("TileNotAvailable", - tile.getExternalId()); + LocalText.getText("TileNotAvailable", + tile.getExternalId()); break; } @@ -325,10 +326,10 @@ List<TileI> tiles = action.getTiles(); if (tiles != null && !tiles.isEmpty() && !tiles.contains(tile)) { errMsg = - LocalText.getText( - "TileMayNotBeLaidInHex", - tile.getExternalId(), - hex.getName() ); + LocalText.getText( + "TileMayNotBeLaidInHex", + tile.getExternalId(), + hex.getName() ); break; } stl = action.getSpecialProperty(); @@ -341,8 +342,8 @@ */ if (!extra && !validateNormalTileLay(tile)) { errMsg = - LocalText.getText("NumberOfNormalTileLaysExceeded", - tile.getColourName()); + LocalText.getText("NumberOfNormalTileLaysExceeded", + tile.getColourName()); break; } @@ -356,23 +357,23 @@ // Amount must be non-negative multiple of 10 if (cost < 0) { errMsg = - LocalText.getText("NegativeAmountNotAllowed", - Bank.format(cost)); + LocalText.getText("NegativeAmountNotAllowed", + Bank.format(cost)); break; } if (cost % 10 != 0) { errMsg = - LocalText.getText("AmountMustBeMultipleOf10", - Bank.format(cost)); + LocalText.getText("AmountMustBeMultipleOf10", + Bank.format(cost)); break; } // Does the company have the money? if (cost > operatingCompany.getCash()) { errMsg = - LocalText.getText("NotEnoughMoney", - companyName, - Bank.format(operatingCompany.getCash()), - Bank.format(cost) ); + LocalText.getText("NotEnoughMoney", + companyName, + Bank.format(operatingCompany.getCash()), + Bank.format(cost) ); break; } break; @@ -416,7 +417,7 @@ stl.setExercised(); currentSpecialTileLays.remove(action); log.debug("This was a special tile lay, " - + (extra ? "" : " not") + " extra"); + + (extra ? "" : " not") + " extra"); } if (!extra) { @@ -426,11 +427,11 @@ setSpecialTileLays(); log.debug("There are now " + currentSpecialTileLays.size() - + " special tile lay objects"); + + " special tile lay objects"); } if (tile == null || currentNormalTileLays.isEmpty() - && currentSpecialTileLays.isEmpty()) { + && currentSpecialTileLays.isEmpty()) { nextStep(); } @@ -446,16 +447,16 @@ } protected boolean checkNormalTileLay(TileI tile, boolean update) { - -// Map<String,Integer> tileLaysPerColour = tileLaysPerColourState.getObject(); - -// if (tileLaysPerColour.isEmpty()) return false; + // Map<String,Integer> tileLaysPerColour = tileLaysPerColourState.getObject(); + + // if (tileLaysPerColour.isEmpty()) return false; + String colour = tile.getColourName(); Integer oldAllowedNumberObject = tileLaysPerColour.get(colour); if (oldAllowedNumberObject == null) return false; - + int oldAllowedNumber = oldAllowedNumberObject.intValue(); if (oldAllowedNumber <= 0) return false; @@ -467,29 +468,29 @@ * all normal tile lays have been consumed. 2. If any colour is laid, no * different colours may be laid. THIS MAY NOT BE TRUE FOR ALL GAMES! */ - -// Map<String,Integer> tileLaysPerColourUpdated = new HashMap<String, Integer>(); // new (empty) map - + + // Map<String,Integer> tileLaysPerColourUpdated = new HashMap<String, Integer>(); // new (empty) map + if (oldAllowedNumber <= 1) { - for (String key:tileLaysPerColour.keySet()) + for (String key:tileLaysPerColour.keySet()) new MapChange<String,Integer>(tileLaysPerColour, key, new Integer(0)); log.debug("No more normal tile lays allowed"); currentNormalTileLays.clear(); } else { -// tileLaysPerColourUpdated.put(colour, new Integer(oldAllowedNumber - 1)); - for (String key:tileLaysPerColour.keySet()) + // tileLaysPerColourUpdated.put(colour, new Integer(oldAllowedNumber - 1)); + for (String key:tileLaysPerColour.keySet()) if (colour.equals(key)) new MapChange<String,Integer> - (tileLaysPerColour, colour, new Integer(oldAllowedNumber-1)); + (tileLaysPerColour, colour, new Integer(oldAllowedNumber-1)); else new MapChange<String,Integer>(tileLaysPerColour, key, new Integer(0)); - log.debug((oldAllowedNumber - 1) + " more " + colour - + " tile lays allowed"); + log.debug((oldAllowedNumber - 1) + " more " + colour + + " tile lays allowed"); } - -// tileLaysPerColourState.set(tileLaysPerColourUpdated); + // tileLaysPerColourState.set(tileLaysPerColourUpdated); + return true; } @@ -503,7 +504,7 @@ MapHex hex = action.getChosenHex(); int station = action.getChosenStation(); String companyName = operatingCompany.getName(); - + // TEMPORARY FIX to enable fixing invalidated saved files //if ("N11".equals(hex.getName()) && station == 2) { // station = 1; @@ -515,7 +516,7 @@ // Checks // Must be correct step (exception: home base lay & some special token lay) - if (getStep() != GameDef.OrStep.LAY_TOKEN + if (getStep() != GameDef.OrStep.LAY_TOKEN && action.getType() != LayBaseToken.HOME_CITY && action.getType() != LayBaseToken.SPECIAL_PROPERTY) { errMsg = LocalText.getText("WrongActionNoTokenLay"); @@ -526,7 +527,7 @@ errMsg = LocalText.getText("HasNoTokensLeft", companyName); break; } - + if (!isTokenLayAllowed (operatingCompany, hex, station)) { errMsg = LocalText.getText("BaseTokenSlotIsReserved"); break; @@ -544,20 +545,20 @@ */ if (hex.hasTokenOfCompany(operatingCompany)) { errMsg = - LocalText.getText("TileAlreadyHasToken", - hex.getName(), - companyName ); + LocalText.getText("TileAlreadyHasToken", + hex.getName(), + companyName ); break; } - + if (action != null) { List<MapHex> locations = action.getLocations(); if (locations != null && locations.size() > 0 - && !locations.contains(hex) && !locations.contains(null)) { + && !locations.contains(hex) && !locations.contains(null)) { errMsg = - LocalText.getText("TokenLayingHexMismatch", - hex.getName(), - action.getLocationNameString() ); + LocalText.getText("TokenLayingHexMismatch", + hex.getName(), + action.getLocationNameString() ); break; } stl = action.getSpecialProperty(); @@ -573,7 +574,7 @@ companyName, Bank.format(operatingCompany.getCash()), Bank.format(cost)); - break; + break; } break; } @@ -616,18 +617,18 @@ stl.setExercised(); currentSpecialTokenLays.remove(action); log.debug("This was a special token lay, " - + (extra ? "" : " not") + " extra"); + + (extra ? "" : " not") + " extra"); } - + // Jump out if we aren't in the token laying step if (getStep() != GameDef.OrStep.LAY_TOKEN) return true; - + if (!extra) { currentNormalTokenLays.clear(); log.debug("This was a normal token lay"); } - + if (currentNormalTokenLays.isEmpty()) { log.debug("No more normal token lays are allowed"); } else if (operatingCompany.getNumberOfFreeBaseTokens() == 0) { @@ -638,9 +639,9 @@ } setSpecialTokenLays(); log.debug("There are now " + currentSpecialTokenLays.size() - + " special token lay objects"); + + " special token lay objects"); if (currentNormalTokenLays.isEmpty() - && currentSpecialTokenLays.isEmpty()) { + && currentSpecialTokenLays.isEmpty()) { nextStep(); } @@ -666,9 +667,9 @@ MapHex location = action.getChosenHex(); if (location != hex) { errMsg = - LocalText.getText("TokenLayingHexMismatch", - hex.getName(), - location.getName() ); + LocalText.getText("TokenLayingHexMismatch", + hex.getName(), + location.getName() ); break; } stl = action.getSpecialProperty(); @@ -681,8 +682,8 @@ // Does the company have the money? if (cost > operatingCompany.getCash()) { errMsg = - LocalText.getText("NotEnoughMoney", - operatingCompany.getName()); + LocalText.getText("NotEnoughMoney", + operatingCompany.getName()); break; } break; @@ -702,9 +703,9 @@ if (hex.layBonusToken(token, gameManager.getPhaseManager())) { /* TODO: the false return value must be impossible. */ - operatingCompany.addBonus(new Bonus(operatingCompany, - token.getName(), - token.getValue(), Collections.singletonList(hex))); + operatingCompany.addBonus(new Bonus(operatingCompany, + token.getName(), + token.getValue(), Collections.singletonList(hex))); token.setUser(operatingCompany); ReportBuffer.add(LocalText.getText("LaysBonusTokenOn", @@ -718,7 +719,7 @@ stl.setExercised(); currentSpecialTokenLays.remove(action); log.debug("This was a special token lay, " - + (extra ? "" : " not") + " extra"); + + (extra ? "" : " not") + " extra"); } @@ -745,10 +746,10 @@ // Does the company have the money? if (cost > operatingCompany.getCash()) { errMsg = - LocalText.getText("NotEnoughMoney", - operatingCompany.getName(), - Bank.format(operatingCompany.getCash()), - Bank.format(cost)); + LocalText.getText("NotEnoughMoney", + operatingCompany.getName(), + Bank.format(operatingCompany.getCash()), + Bank.format(cost)); break; } break; @@ -767,10 +768,10 @@ moveStack.start(true); new CashMove (operatingCompany, seller, cost); - operatingCompany.addBonus(new Bonus(operatingCompany, - sbt.getName(), - sbt.getValue(), - sbt.getLocations())); + operatingCompany.addBonus(new Bonus(operatingCompany, + sbt.getName(), + sbt.getValue(), + sbt.getLocations())); ReportBuffer.add(LocalText.getText("BuysBonusTokenFrom", operatingCompany.getName(), @@ -783,9 +784,9 @@ return true; } - - - + + + public boolean setRevenueAndDividend(SetDividend action) { String errMsg = validateSetRevenueAndDividend (action); @@ -831,9 +832,9 @@ companyName = company.getName(); if (company != operatingCompany) { errMsg = - LocalText.getText("WrongCompany", - companyName, - operatingCompa... [truncated message content] |