|
From: <ste...@us...> - 2011-07-07 04:53:42
|
Revision: 1607
http://rails.svn.sourceforge.net/rails/?rev=1607&view=rev
Author: stefanfrey
Date: 2011-07-07 04:53:35 +0000 (Thu, 07 Jul 2011)
Log Message:
-----------
Added support for runTo and runThrough properties of MapHex to revenue calculation
Modified Paths:
--------------
trunk/18xx/rails/algorithms/NetworkCompanyGraph.java
trunk/18xx/rails/algorithms/NetworkVertex.java
trunk/18xx/rails/algorithms/RevenueAdapter.java
Modified: trunk/18xx/rails/algorithms/NetworkCompanyGraph.java
===================================================================
--- trunk/18xx/rails/algorithms/NetworkCompanyGraph.java 2011-07-06 13:13:50 UTC (rev 1606)
+++ trunk/18xx/rails/algorithms/NetworkCompanyGraph.java 2011-07-07 04:53:35 UTC (rev 1607)
@@ -52,12 +52,24 @@
return new NetworkCompanyGraph(graphBuilder, company);
}
+ public SimpleGraph<NetworkVertex, NetworkEdge> getRouteGraph() {
+ return routeGraph;
+ }
+
+ public SimpleGraph<NetworkVertex, NetworkEdge> getRevenueGraph() {
+ return revenueGraph;
+ }
+
+ public Multigraph<NetworkVertex, NetworkEdge> getPhase2Graph() {
+ return phase2Graph;
+ }
+
public SimpleGraph<NetworkVertex, NetworkEdge> createRouteGraph(boolean addHQ) {
// get mapgraph from builder
SimpleGraph<NetworkVertex, NetworkEdge> mapGraph = graphBuilder.getMapGraph();
// set sinks on mapgraph
- NetworkVertex.initAllRailsVertices(mapGraph.vertexSet(), company, null);
+ NetworkVertex.initAllRailsVertices(mapGraph, company, null);
// initialized simple graph
SimpleGraph<NetworkVertex, NetworkEdge> graph = new SimpleGraph<NetworkVertex, NetworkEdge>(NetworkEdge.class);
@@ -93,7 +105,7 @@
if (!addHQ) graph.removeVertex(hqVertex);
// deactivate sinks on mapgraph
- NetworkVertex.initAllRailsVertices(mapGraph.vertexSet(), null, null);
+ NetworkVertex.initAllRailsVertices(mapGraph, null, null);
// store and return
routeGraph = graph;
Modified: trunk/18xx/rails/algorithms/NetworkVertex.java
===================================================================
--- trunk/18xx/rails/algorithms/NetworkVertex.java 2011-07-06 13:13:50 UTC (rev 1606)
+++ trunk/18xx/rails/algorithms/NetworkVertex.java 2011-07-07 04:53:35 UTC (rev 1607)
@@ -10,6 +10,7 @@
import java.util.Set;
import org.apache.log4j.Logger;
+import org.jgrapht.Graph;
import org.jgrapht.graph.SimpleGraph;
import rails.game.City;
@@ -227,13 +228,22 @@
/**
* Initialize for rails vertexes
+ * @return true = can stay inside the network, false = has to be removed
*/
- public void initRailsVertex(PublicCompanyI company) {
+ public boolean initRailsVertex(PublicCompanyI company) {
// side vertices use the defaults, virtuals cannot use this function
- if (virtual || type == VertexType.SIDE) return;
+ if (virtual || type == VertexType.SIDE) return true;
log.info("Init of vertex " + this);
+ // check if it has to be removed because it is run-to only
+ if (company != null) { // if company == null, then no vertex gets removed
+ if (hex.isRunToAllowed() == MapHex.Run.NO || hex.isRunToAllowed() == MapHex.Run.TOKENONLY && !city.hasTokenOf(company))
+ {
+ return false;
+ }
+ }
+
// check if it is a major or minor
if (station.getType().equals(Station.CITY) || station.getType().equals(Station.OFF_MAP_AREA)) {
setStationType(StationType.MAJOR);
@@ -245,8 +255,14 @@
// check if it is a sink
if (company == null) { // if company == null, then all sinks are deactivated
sink = false;
- } else if (station.getType().equals(Station.OFF_MAP_AREA) ||
- station.getType().equals(Station.CITY) && !city.hasTokenSlotsLeft() && city.getSlots() != 0 && !city.hasTokenOf(company)) {
+ } else if (station.getType().equals(Station.OFF_MAP_AREA) || (
+ // or station is city
+ station.getType().equals(Station.CITY)
+ // and is either fully tokened and has token slots or only tokens allow run through
+ && ( city.getSlots() != 0 && !city.hasTokenSlotsLeft() || hex.isRunThroughAllowed() == MapHex.Run.TOKENONLY)
+ // and city does not have a token
+ && !city.hasTokenOf(company))
+ ) {
sink = true;
}
@@ -262,6 +278,10 @@
cityName = hex.getCityName() + "." + station.getCityName();
}
}
+
+ // no removal
+ return true;
+
}
public void setRailsVertexValue(PhaseI phase) {
@@ -269,7 +289,6 @@
if (virtual || type == VertexType.SIDE) return;
// define value
-// if (station.getType().equals(Station.OFF_MAP_AREA) || station.getValue() == -1) {
if (hex.hasOffBoardValues()) {
value = hex.getCurrentOffBoardValue(phase);
} else {
@@ -308,14 +327,28 @@
}
}
- public static void initAllRailsVertices(Collection<NetworkVertex> vertices,
+ /**
+ *
+ * @param graph network graph
+ * @param company the company (with regard to values, sinks and removals)
+ * @param phase the current phase (with regard to values)
+ */
+ public static void initAllRailsVertices(Graph<NetworkVertex, NetworkEdge> graph,
PublicCompanyI company, PhaseI phase) {
- for (NetworkVertex v:vertices) {
- if (company != null)
- v.initRailsVertex(company);
- if (phase != null)
+
+ // store vertices for removal
+ List<NetworkVertex> verticesToRemove = new ArrayList<NetworkVertex>();
+ for (NetworkVertex v:graph.vertexSet()) {
+ if (company != null) {
+ if (!v.initRailsVertex(company)) {
+ verticesToRemove.add(v);
+ }
+ }
+ if (phase != null) {
v.setRailsVertexValue(phase);
+ }
}
+ graph.removeAllVertices(verticesToRemove);
}
/**
Modified: trunk/18xx/rails/algorithms/RevenueAdapter.java
===================================================================
--- trunk/18xx/rails/algorithms/RevenueAdapter.java 2011-07-06 13:13:50 UTC (rev 1606)
+++ trunk/18xx/rails/algorithms/RevenueAdapter.java 2011-07-07 04:53:35 UTC (rev 1607)
@@ -208,7 +208,7 @@
graph = companyGraph.createRouteGraph(false);
// initialize vertices
- NetworkVertex.initAllRailsVertices(graph.vertexSet(), company, phase);
+ NetworkVertex.initAllRailsVertices(graph, company, phase);
// define startVertexes
addStartVertices(companyGraph.getCompanyBaseTokenVertexes(company));
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|