Skip to content

Commit

Permalink
Merge pull request #343 from erik-vos/master
Browse files Browse the repository at this point in the history
1837: Improvements and bug fixes, part III
  • Loading branch information
neutronc authored Feb 7, 2021
2 parents 3c68668 + fdb8d0d commit 1b06743
Show file tree
Hide file tree
Showing 11 changed files with 476 additions and 354 deletions.
30 changes: 18 additions & 12 deletions src/main/java/net/sf/rails/common/parser/Tag.java
Original file line number Diff line number Diff line change
Expand Up @@ -276,18 +276,24 @@ private void parseSubTags(Element element) throws ConfigurationException {
value = attribute.getNodeValue();
attributes.put(name, value);
}
} else if ( "IfOption".equalsIgnoreCase(childTagName)) {
Node nameAttr = nnp.getNamedItem("name");
if (nameAttr == null)
throw new ConfigurationException(
"IfOption has no optionName attribute");
name = nameAttr.getNodeValue();

Node parmAttr = nnp.getNamedItem("parm");
if (parmAttr != null) {
value = parmAttr.getNodeValue();
Iterable<String> parameters = Splitter.on(XMLTags.VALUES_DELIM).split(value);
name = GameOption.constructParameterisedName(name, ImmutableList.copyOf(parameters));
} else if ("IfOption".equalsIgnoreCase(childTagName)
|| "IfVariant".equalsIgnoreCase(childTagName)) {

if ("IfOption".equalsIgnoreCase(childTagName)) {
Node nameAttr = nnp.getNamedItem("name");
if (nameAttr == null)
throw new ConfigurationException(
"IfOption has no optionName attribute");
name = nameAttr.getNodeValue();

Node parmAttr = nnp.getNamedItem("parm");
if (parmAttr != null) {
value = parmAttr.getNodeValue();
Iterable<String> parameters = Splitter.on(XMLTags.VALUES_DELIM).split(value);
name = GameOption.constructParameterisedName(name, ImmutableList.copyOf(parameters));
}
} else { // IfVariant
name = "Variant";
}

Node valueAttr = nnp.getNamedItem("value");
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/net/sf/rails/game/StartItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ public class StartItem extends RailsAbstractItem {
protected int column = 0;
protected int index;

// To allow the displayed name to include both primary and secondary.
// Default is the primary name (id) only.
protected String displayName = null;

// Bids
protected final GenericState<Player> lastBidder = new GenericState<>(this, "lastBidder");
protected final Map<Player, CountingMoneyModel> bids = Maps.newHashMap();
Expand Down Expand Up @@ -271,6 +275,14 @@ public Certificate getSecondary() {
return secondary;
}

public String getDisplayName() {
return (displayName != null ? displayName : getId());
}

public void setDisplayName(String displayName) {
this.displayName = displayName;
}

/**
* Get the start item base price.
*
Expand Down
44 changes: 41 additions & 3 deletions src/main/java/net/sf/rails/game/StartPacket.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ public class StartPacket extends RailsAbstractItem {
protected int minimumIncrement = 5;
/** The modulus of all bids (i.e. of which value the bid must be a multiple) */
protected int modulus = 5;
/** Is multiple-column display enabled?
* If so, row and col attributes become mandatory for all start items
* (if the <MultipleColumn> tag precedes all start items).*/
protected boolean multipleColumns = false;
/** The number of columns. Will be derived from the column attributes. */
protected int numberOfColumns = 1;
/** The number of rows. Will be derived from the row attributes, if multipleColumns is true. */
protected int numberOfRows;

/** Default name */
public static final String DEFAULT_ID = "Initial";
Expand All @@ -56,6 +64,12 @@ public static StartPacket create(RailsItem parent, String id, String roundClassN
* @throws ConfigurationException if anything goes wrong.
*/
public void configureFromXML(Tag tag) throws ConfigurationException {

// Multiple column display?
Tag columnsTag = tag.getChild("MultipleColumns");
multipleColumns = columnsTag != null;

// Bidding parameters, if applicable
Tag biddingTag = tag.getChild("Bidding");
if (biddingTag != null) {
minimumInitialIncrement =
Expand Down Expand Up @@ -83,14 +97,27 @@ public void configureFromXML(Tag tag) throws ConfigurationException {

int basePrice = itemTag.getAttributeAsInteger("basePrice", 0);
boolean reduceable = itemTag.getAttributeAsBoolean("reduceable", false);
StartItem item = StartItem.create(this, itemName, itemType, basePrice, reduceable, index++, president);
StartItem item = StartItem.create(this, itemName, itemType,
basePrice, reduceable, index++, president);
items.add(item);

// Optional attributes
int row = itemTag.getAttributeAsInteger("row", 0);
if (row > 0) item.setRow(row);
item.setRow(row);
int column = itemTag.getAttributeAsInteger("column", 0);
if (column > 0) item.setColumn(column);
if (multipleColumns) {
if (!(row > 0 && column > 0)) {
throw new ConfigurationException(
"With multiple columns, both row and column attributes are required");
}
item.setColumn(column);
numberOfRows = Math.max (numberOfRows, row);
numberOfColumns = Math.max (numberOfColumns, column);
}

// Displayed name
String displayName = itemTag.getAttributeAsString("displayName", null);
if (displayName != null) item.setDisplayName(displayName);

// Check if there is another certificate
List<Tag> subItemTags = itemTag.getChildren("SubItem");
Expand Down Expand Up @@ -241,4 +268,15 @@ public int getModulus() {
return modulus;
}

public boolean isMultipleColumns() {
return multipleColumns;
}

public int getNumberOfColumns() {
return numberOfColumns;
}

public int getNumberOfRows() {
return numberOfRows;
}
}
10 changes: 7 additions & 3 deletions src/main/java/net/sf/rails/game/TileUpgrade.java
Original file line number Diff line number Diff line change
Expand Up @@ -383,12 +383,13 @@ private Rotation processRotations(HexSide side) {
log.debug("base={} target={}", base, target);
}
// check if there are stations to map
Map<Station, Station> stationMapping = assignStations(base, target);
Map<Station, Station> stationMapping = assignStations(base, target);

// Try something else: match sides with old and new Stations in a simple way
// Each pair of stations that matches with the same side is connected.
// This finally appears to work for the 1837 green Vienna upgrade.
// Though it may only work where both base and target tiles have a fixed orientation.
// (may be obsolete, now that automatic relay works better (see issue #341).
if (stationMapping == null) {
stationMapping = new HashMap<>(6);
Station b, t;
Expand Down Expand Up @@ -506,8 +507,11 @@ private Map<Station, Station> assignStations(TrackConfig base, TrackConfig targe
}
}
// check if all base and target stations are assigned
if (stationMap.keySet().size() != baseNb ||
Sets.newHashSet(stationMap.values()).size() != targetNb) {
if (stationMap.keySet().size() != baseNb
/* Unclear why the all-stations-mapped check was applied to the new tile.
It inhibited upgrading 1837 Vienna to green (4 -> 6 stations).
|| Sets.newHashSet(stationMap.values()).size() != targetNb*/
) {
stationMap = null;
log.debug("Mapping: Not all stations assigned, set stationMap to null");
}
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/net/sf/rails/ui/swing/ORUIManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -788,8 +788,9 @@ private void relayBaseTokens (LayTile action) {
Tile newTile = action.getLaidTile();
Tile oldTile = hex.getCurrentTile();

// Why does that need to be configured?
// Shouldn't tokens always be relaid??
// Check if manual token relay is required.
// This was an emergency measure in cases where automatic relay
// did not work (e.g. 1837 tile 427). Now probably obsolete.
if (!action.isRelayBaseTokens()
&& !oldTile.relayBaseTokensOnUpgrade()) return; // is deprecated

Expand All @@ -798,7 +799,6 @@ private void relayBaseTokens (LayTile action) {
/* Check which tokens must be relaid, and in which sequence.
* Ideally, the game engine should instruct the UI what to do
* if there is more than one stop and more than one token.
* TODO LayTile does not yet allow that.
*
* For now, the only case that needs special handling is the 1835 BA home hex L6,
* where it it possible to have two tokens laid before even one tile.
Expand Down
Loading

0 comments on commit 1b06743

Please sign in to comment.