From: <ev...@us...> - 2010-07-17 22:45:34
|
Revision: 1347 http://rails.svn.sourceforge.net/rails/?rev=1347&view=rev Author: evos Date: 2010-07-17 22:45:27 +0000 (Sat, 17 Jul 2010) Log Message: ----------- Allow the old object positions in more than one list to be saved in ObjectMove. This should restore ALL lists affected by ObjectMove into original order after an Undo. Modified Paths: -------------- trunk/18xx/rails/game/Bank.java trunk/18xx/rails/game/City.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/TrainType.java trunk/18xx/rails/game/move/Moveable.java trunk/18xx/rails/game/move/MoveableHolder.java trunk/18xx/rails/game/move/ObjectMove.java trunk/18xx/rails/game/move/RemoveFromList.java Modified: trunk/18xx/rails/game/Bank.java =================================================================== --- trunk/18xx/rails/game/Bank.java 2010-07-17 22:34:52 UTC (rev 1346) +++ trunk/18xx/rails/game/Bank.java 2010-07-17 22:45:27 UTC (rev 1347) @@ -107,9 +107,9 @@ for (PublicCompanyI comp : companies) { for (PublicCertificateI cert : comp.getCertificates()) { if (cert.isInitiallyAvailable()) { - ipo.addCertificate(cert, -1); + ipo.addCertificate(cert); } else { - unavailable.addCertificate(cert, -1); + unavailable.addCertificate(cert); } } } Modified: trunk/18xx/rails/game/City.java =================================================================== --- trunk/18xx/rails/game/City.java 2010-07-17 22:34:52 UTC (rev 1346) +++ trunk/18xx/rails/game/City.java 2010-07-17 22:45:27 UTC (rev 1347) @@ -23,7 +23,7 @@ * tile. However, the City numbers will not change, unless cities are merged * during upgrades; but even then it is attempted to retain the old city numbers * as much as possible. - * + * * @author Erik Vos */ public class City implements TokenHolder { @@ -100,9 +100,9 @@ return result; } - public boolean addObject(Moveable object, int position) { + public boolean addObject(Moveable object, int[] position) { if (object instanceof TokenI) { - return addToken((TokenI) object, position); + return addToken((TokenI) object, position == null ? -1 : position[0]); } else { return false; } @@ -163,11 +163,11 @@ return false; } - public int getListIndex (Moveable object) { + public int[] getListIndex (Moveable object) { if (object instanceof BaseToken) { - return tokens.indexOf(object); + return new int[] {tokens.indexOf(object)}; } else { - return -1; + return Moveable.AT_END; } } Modified: trunk/18xx/rails/game/GameManager.java =================================================================== --- trunk/18xx/rails/game/GameManager.java 2010-07-17 22:34:52 UTC (rev 1346) +++ trunk/18xx/rails/game/GameManager.java 2010-07-17 22:45:27 UTC (rev 1347) @@ -1500,21 +1500,21 @@ * @param object The object to add. * @return True if successful. */ - public boolean addObject(Moveable object, int position) { + public boolean addObject(Moveable object, int[] position) { if (object instanceof SpecialPropertyI) { SpecialPropertyI sp = (SpecialPropertyI) object; sp.setHolder(null); - return addSpecialProperty(sp, position); + return addSpecialProperty(sp, position == null ? -1 : position[0]); } else { return false; } } - public int getListIndex (Moveable object) { + public int[] getListIndex (Moveable object) { if (object instanceof SpecialPropertyI) { - return commonSpecialProperties.indexOf(object); + return new int[] {commonSpecialProperties.indexOf(object)}; } else { - return -1; + return Moveable.AT_END; } } Modified: trunk/18xx/rails/game/MapHex.java =================================================================== --- trunk/18xx/rails/game/MapHex.java 2010-07-17 22:34:52 UTC (rev 1346) +++ trunk/18xx/rails/game/MapHex.java 2010-07-17 22:45:27 UTC (rev 1347) @@ -827,9 +827,9 @@ return offStationTokens.remove(token); } - public boolean addObject(Moveable object, int position) { + public boolean addObject(Moveable object, int[] position) { if (object instanceof TokenI) { - return addToken((TokenI) object, position); + return addToken((TokenI) object, position == null ? -1 : position[0]); } else { return false; } @@ -843,11 +843,11 @@ } } - public int getListIndex (Moveable object) { + public int[] getListIndex (Moveable object) { if (object instanceof TokenI) { - return offStationTokens.indexOf(object); + return new int[] {offStationTokens.indexOf(object)}; } else { - return -1; + return Moveable.AT_END; } } @@ -1015,7 +1015,7 @@ * NOTE: this method currently only checks for prohibitions caused * by the presence of unlaid home base tokens. * It does NOT (yet) check for free space. - * + * * Remark: There are the following cases to check * A) isBlockedForTokenLays is active for the MapHex => return the state of this * (Example: Erie home base in 1830) Modified: trunk/18xx/rails/game/OperatingRound.java =================================================================== --- trunk/18xx/rails/game/OperatingRound.java 2010-07-17 22:34:52 UTC (rev 1346) +++ trunk/18xx/rails/game/OperatingRound.java 2010-07-17 22:45:27 UTC (rev 1347) @@ -1716,7 +1716,7 @@ train.getType().addToBoughtFromIPO(); // Clone the train if infinitely available if (train.getType().hasInfiniteAmount()) { - ipo.addTrain(train.getType().cloneTrain(), -1); + ipo.addTrain(train.getType().cloneTrain()); } } @@ -2670,7 +2670,7 @@ * This method can be extended to perform other generic checks, * such as if a route exists, * and possibly in subclasses for game-specific checks. - * + * * @param company The company laying a tile. * @param hex The hex on which a tile is laid. * @param station The number of the station/city on which the token Modified: trunk/18xx/rails/game/Portfolio.java =================================================================== --- trunk/18xx/rails/game/Portfolio.java 2010-07-17 22:34:52 UTC (rev 1346) +++ trunk/18xx/rails/game/Portfolio.java 2010-07-17 22:45:27 UTC (rev 1347) @@ -121,23 +121,28 @@ privatesOwnedModel.update(); } - public void addCertificate(PublicCertificateI certificate, int position) { + public void addCertificate(PublicCertificateI certificate){ + addCertificate (certificate, new int[] {-1,-1,-1}); + } + + public void addCertificate(PublicCertificateI certificate, int[] position) { // When undoing a company start, put the President back at the top. - if (certificate.isPresidentShare()) position = 0; + if (certificate.isPresidentShare()) position = new int[] {0,0,0}; + Util.addToList(certificates, certificate, position[0]); + String companyName = certificate.getCompany().getName(); if (!certPerCompany.containsKey(companyName)) { certPerCompany.put(companyName, new ArrayList<PublicCertificateI>()); } - Util.addToList(certificates, certificate, position); - (certPerCompany.get(companyName)).add(certificate); + Util.addToList(certPerCompany.get(companyName), certificate, position[1]); String certTypeId = certificate.getTypeId(); if (!certsPerType.containsKey(certTypeId)) { certsPerType.put(certTypeId, new ArrayList<PublicCertificateI>()); } - certsPerType.get(certTypeId).add(certificate); + Util.addToList(certsPerType.get(certTypeId), certificate, position[2]); certificate.setPortfolio(this); @@ -373,14 +378,18 @@ return swapped; } - public void addTrain(TrainI train, int position) { + public void addTrain (TrainI train) { + addTrain (train, new int[] {-1,-1}); + } - Util.addToList(trains, train, position); + public void addTrain(TrainI train, int[] position) { + + Util.addToList(trains, train, position[0]); TrainTypeI type = train.getType(); if (!trainsPerType.containsKey(type)) { trainsPerType.put(type, new ArrayList<TrainI>()); } - trainsPerType.get(train.getType()).add(train); + Util.addToList(trainsPerType.get(train.getType()), train, position[1]); train.setHolder(this); trainsModel.update(); } @@ -574,20 +583,22 @@ * @param object The object to add. * @return True if successful. */ - public boolean addObject(Moveable object, int position) { + public boolean addObject(Moveable object, int[] position) { if (object instanceof PublicCertificateI) { + if (position == null) position = new int[] {-1, -1, -1}; addCertificate((PublicCertificateI) object, position); return true; } else if (object instanceof PrivateCompanyI) { - addPrivate((PrivateCompanyI) object, position); + addPrivate((PrivateCompanyI) object, position == null ? -1 : position[0]); return true; } else if (object instanceof TrainI) { + if (position == null) position = new int[] {-1, -1}; addTrain((TrainI) object, position); return true; } else if (object instanceof SpecialPropertyI) { - return addSpecialProperty((SpecialPropertyI) object, position); + return addSpecialProperty((SpecialPropertyI) object, position == null ? -1 : position[0]); } else if (object instanceof TokenI) { - return addToken((TokenI) object, position); + return addToken((TokenI) object, position == null ? -1 : position[0]); } else { return false; } @@ -618,19 +629,28 @@ } } - public int getListIndex (Moveable object) { + public int[] getListIndex (Moveable object) { if (object instanceof PublicCertificateI) { - return certificates.indexOf(object); + PublicCertificateI cert = (PublicCertificateI) object; + return new int[] { + certificates.indexOf(object), + certPerCompany.get(cert.getCompany().getName()).indexOf(cert), + certsPerType.get(cert.getTypeId()).indexOf(cert) + }; } else if (object instanceof PrivateCompanyI) { - return privateCompanies.indexOf(object); + return new int[] {privateCompanies.indexOf(object)}; } else if (object instanceof TrainI) { - return trains.indexOf(object); + TrainI train = (TrainI) object; + return new int[] { + trains.indexOf(train), + trainsPerType.get(train.getType()).indexOf(train) + }; } else if (object instanceof SpecialPropertyI) { - return specialProperties.indexOf(object); + return new int[] {specialProperties.indexOf(object)}; } else if (object instanceof TokenI) { - return tokens.indexOf(object); + return new int[] {tokens.indexOf(object)}; } else { - return -1; + return Moveable.AT_END; } } Modified: trunk/18xx/rails/game/PrivateCompany.java =================================================================== --- trunk/18xx/rails/game/PrivateCompany.java 2010-07-17 22:34:52 UTC (rev 1346) +++ trunk/18xx/rails/game/PrivateCompany.java 2010-07-17 22:45:27 UTC (rev 1347) @@ -2,7 +2,6 @@ package rails.game; import java.util.ArrayList; -import java.util.Iterator; import java.util.List; import rails.game.move.*; @@ -55,14 +54,14 @@ // sfy 1889 changed to IntegerArray revenue = tag.getAttributeAsIntegerArray("revenue", new int[0]); - + // pld: adding revenue to info text infoText += "<br>Revenue: "; for (int i = 0; i < revenue.length;i++) { infoText += (Bank.format(revenue[i])); if (i < revenue.length-1) {infoText += ", ";}; } - + // Blocked hexes (until bought by a company) Tag blockedTag = tag.getChild("Blocking"); if (blockedTag != null) { @@ -333,9 +332,10 @@ * Stub to satisfy MoveableHolderI. Special properties are never added after * completing the initial setup. */ - public boolean addObject(Moveable object, int position) { + public boolean addObject(Moveable object, int[] position) { if (object instanceof SpecialPropertyI) { - return Util.addToList(specialProperties, (SpecialPropertyI)object, position); + return Util.addToList(specialProperties, (SpecialPropertyI)object, + position == null ? -1 : position[0]); } else { return false; } @@ -356,11 +356,11 @@ } } - public int getListIndex (Moveable object) { + public int[] getListIndex (Moveable object) { if (object instanceof SpecialPropertyI) { - return specialProperties.indexOf(object); + return new int[] {specialProperties.indexOf(object)}; } else { - return -1; + return Moveable.AT_END; } } Modified: trunk/18xx/rails/game/PublicCompany.java =================================================================== --- trunk/18xx/rails/game/PublicCompany.java 2010-07-17 22:34:52 UTC (rev 1346) +++ trunk/18xx/rails/game/PublicCompany.java 2010-07-17 22:45:27 UTC (rev 1347) @@ -1809,18 +1809,10 @@ public boolean addToken(TokenI token, int position) { boolean result = false; - if (token instanceof BaseToken && laidBaseTokens.remove(token)) { + if (token instanceof BaseToken + && laidBaseTokens.remove(token) + && Util.addToList(freeBaseTokens, token, position)) { token.setHolder(this); - if (position == -1) { - result = freeBaseTokens.add(token); - } else { - try { - freeBaseTokens.add(position, token); - result = true; - } catch (IndexOutOfBoundsException e) { - result = false; - } - } this.baseTokensModel.update(); } return result; @@ -1864,9 +1856,9 @@ } - public boolean addObject(Moveable object, int position) { + public boolean addObject(Moveable object, int[] position) { if (object instanceof TokenI) { - return addToken((TokenI) object, position); + return addToken((TokenI) object, position == null ? -1 : position[0]); } else { return false; } @@ -1880,11 +1872,11 @@ } } - public int getListIndex (Moveable object) { + public int[] getListIndex (Moveable object) { if (object instanceof BaseToken) { - return freeBaseTokens.indexOf(object); + return new int[] {freeBaseTokens.indexOf(object)}; } else { - return -1; + return Moveable.AT_END; } } Modified: trunk/18xx/rails/game/TrainType.java =================================================================== --- trunk/18xx/rails/game/TrainType.java 2010-07-17 22:34:52 UTC (rev 1346) +++ trunk/18xx/rails/game/TrainType.java 2010-07-17 22:45:27 UTC (rev 1347) @@ -230,7 +230,7 @@ for (TrainI train : trains) { train.init(this, lastIndex++); - unavailable.addTrain(train, -1); + unavailable.addTrain(train); } } Modified: trunk/18xx/rails/game/move/Moveable.java =================================================================== --- trunk/18xx/rails/game/move/Moveable.java 2010-07-17 22:34:52 UTC (rev 1346) +++ trunk/18xx/rails/game/move/Moveable.java 2010-07-17 22:45:27 UTC (rev 1347) @@ -2,6 +2,8 @@ public interface Moveable { + public static final int[] AT_END = new int[] {-1}; + public void moveTo(MoveableHolder newHolder); public String getName(); Modified: trunk/18xx/rails/game/move/MoveableHolder.java =================================================================== --- trunk/18xx/rails/game/move/MoveableHolder.java 2010-07-17 22:34:52 UTC (rev 1346) +++ trunk/18xx/rails/game/move/MoveableHolder.java 2010-07-17 22:45:27 UTC (rev 1347) @@ -8,12 +8,12 @@ * @param position Position to insert object at. O: at front, -1, at end, >0: at that position. * @return True if successful. */ - public boolean addObject(Moveable object, int position); + public boolean addObject(Moveable object, int[] position); public boolean removeObject(Moveable object); public String getName(); - public int getListIndex (Moveable object); + public int[] getListIndex (Moveable object); } Modified: trunk/18xx/rails/game/move/ObjectMove.java =================================================================== --- trunk/18xx/rails/game/move/ObjectMove.java 2010-07-17 22:34:52 UTC (rev 1346) +++ trunk/18xx/rails/game/move/ObjectMove.java 2010-07-17 22:45:27 UTC (rev 1347) @@ -14,8 +14,8 @@ MoveableHolder from; MoveableHolder to; String objectClassName; - int fromPosition; - int toPosition = -1; // Default: at end + int[] fromPosition; + int[] toPosition = new int[] {-1}; // Default: at end /** * Create a generic ObjectMove object. Any specific side effects must be @@ -30,7 +30,7 @@ public ObjectMove(Moveable moveableObject, MoveableHolder from, MoveableHolder to) { - this (moveableObject, from, to, -1); + this (moveableObject, from, to, null); } /** @@ -46,13 +46,13 @@ */ public ObjectMove(Moveable moveableObject, MoveableHolder from, - MoveableHolder to, int toPosition) { + MoveableHolder to, int[] toPosition) { this.moveableObject = moveableObject; this.from = from; this.to = to; objectClassName = moveableObject.getClass().getSimpleName(); - this.fromPosition = from.getListIndex(moveableObject); + this.fromPosition = from != null ? from.getListIndex(moveableObject) : null; this.toPosition = toPosition; MoveSet.add(this); @@ -78,8 +78,8 @@ if (from == null) log.warn("From is null"); if (to == null) log.error("To is null"); return "Move " + objectClassName + ": " + moveableObject.getName() - + " from " + (from == null ? from : from.getName()) + "["+fromPosition - + "] to " + to.getName() + "["+toPosition+"]"; + + " from " + (from == null ? from : from.getName()) + "["+fromPosition + + "] to " + to.getName() + "["+toPosition+"]"; } } Modified: trunk/18xx/rails/game/move/RemoveFromList.java =================================================================== --- trunk/18xx/rails/game/move/RemoveFromList.java 2010-07-17 22:34:52 UTC (rev 1346) +++ trunk/18xx/rails/game/move/RemoveFromList.java 2010-07-17 22:45:27 UTC (rev 1347) @@ -1,5 +1,5 @@ /* $Header: /Users/blentz/rails_rcs/cvs/18xx/rails/game/move/RemoveFromList.java,v 1.3 2009/09/23 21:38:57 evos Exp $ - * + * * Created on 18-Jul-2006 * Change Log: */ @@ -32,18 +32,21 @@ public RemoveFromList(List<E> list, E object, String listName) { this (list, object, listName, null); - } + } - public boolean execute() { + @Override + public boolean execute() { list.remove(object); return true; } + @Override public boolean undo() { list.add(index, object); return true; } + @Override public String toString() { return "RemoveFromList " + listName + ": " + object.toString(); } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |