* river goes from one border to another. Nor Params, no results.
*/
public static void addRiver(IBoard board, HashMap<IHex, Point> reverseHex) {
int minElevation = Integer.MAX_VALUE;
HashSet<IHex> riverHexes = new HashSet<IHex>();
IHex field;
Point p = null;
int direction = 0;
int nextLeft = 0;
int nextRight = 0;
int width = board.getWidth();
int height = board.getHeight();
/* if map is smaller than 5x5 no real space for an river */
if ((width < 5) || (height < 5)) {
return;
}
/* First select start and the direction */
switch (Compute.randomInt(4)) {
case 0:
p = new Point(0, Compute.randomInt(5) - 2 + height / 2);
direction = Compute.randomInt(2) + 1;
nextLeft = direction - 1;
nextRight = direction + 1;
break;
case 1:
p = new Point(width - 1, Compute.randomInt(5) - 2 + height / 2);
direction = Compute.randomInt(2) + 4;
nextLeft = direction - 1;
nextRight = (direction + 1) % 6;
break;
case 2:
case 3:
p = new Point(Compute.randomInt(5) - 2 + width / 2, 0);
direction = 2;
nextRight = 3;
nextLeft = 4;
break;
} // switch
/* place the river */
field = board.getHex(p.x, p.y);
ITerrainFactory f = Terrains.getTerrainFactory();
do {
/* first the hex itself */
field.removeAllTerrains();
field.addTerrain(f.createTerrain(Terrains.WATER, 1));
riverHexes.add(field);
p = reverseHex.get(field);
/* then maybe the left and right neighbours */
riverHexes.addAll(extendRiverToSide(board, p, Compute.randomInt(3),
nextLeft, reverseHex));
riverHexes.addAll(extendRiverToSide(board, p, Compute.randomInt(3),
nextRight, reverseHex));
switch (Compute.randomInt(4)) {
case 0:
field = board.getHexInDir(p.x, p.y, (direction + 5) % 6);
break;
case 1:
field = board.getHexInDir(p.x, p.y, (direction + 1) % 6);
break;
default:
field = board.getHexInDir(p.x, p.y, direction);
break;
}
} while (field != null);
/* search the elevation for the river */
HashSet<IHex> tmpRiverHexes = new HashSet<IHex>(riverHexes);
while (!tmpRiverHexes.isEmpty()) {
Iterator<IHex> iter = tmpRiverHexes.iterator();
field = iter.next();
if (field.getElevation() < minElevation) {
minElevation = field.getElevation();
}
tmpRiverHexes.remove(field);
Point thisHex = reverseHex.get(field);
/* and now the six neighbours */
for (int i = 0; i < 6; i++) {
field = board.getHexInDir(thisHex.x, thisHex.y, i);
if ((field != null) && (field.getElevation() < minElevation)) {
minElevation = field.getElevation();
}
tmpRiverHexes.remove(field);
}
}
/* now adjust the elevation to same height */
Iterator<IHex> iter = riverHexes.iterator();
while (iter.hasNext()) {
field = iter.next();
field.setElevation(minElevation);
}
return;
}