Reimplemented Indian settlement generation after updaing algorithm to match restructuring. Colonies are now displayed with their names and population.

This commit is contained in:
smelenchuk 2004-04-29 02:46:09 +00:00
parent cfae925b8f
commit caab8d3ba0
3 changed files with 240 additions and 30 deletions

View File

@ -676,7 +676,7 @@ public final class GUI {
if (map.isValid(p)) {
Tile borderingTile = map.getTile(p);
if (tile.getType() == borderingTile.getType() ||
if (tile.getType() == borderingTile.getType() ||
!borderingTile.isLand() ||
!borderingTile.isExplored()) {
// Equal tiles, sea tiles and unexplored tiles have no effect
@ -756,6 +756,21 @@ public final class GUI {
// Draw image of colony in center of the tile.
g.drawImage(lib.getColonyImage(type), x + (lib.getTerrainImageWidth(tile.getType()) - lib.getColonyImageWidth(type)) / 2, y + (lib.getTerrainImageHeight(tile.getType()) - lib.getColonyImageHeight(type)) / 2, null);
// The 5 here is arbitrary -sjm
g.drawImage(lib.getColorChip(((Colony)settlement).getOwner().getColor()), x + STATE_OFFSET_X, y + 5, null);
String populationString = Integer.toString(((Colony)settlement).getUnitCount());
if (((Colony)settlement).getOwner().getColor() == Color.BLACK) {
g.setColor(Color.WHITE);
} else {
g.setColor(Color.BLACK);
}
g.drawString(populationString, x + TEXT_OFFSET_X + STATE_OFFSET_X, y + 5 + TEXT_OFFSET_Y);
g.setColor(Color.BLACK);
String nameString = ((Colony)settlement).getName();
g.drawString(nameString, x + (lib.getTerrainImageWidth(tile.getType()) - g.getFontMetrics().stringWidth(nameString))/2, y + (lib.getTerrainImageHeight(tile.getType()) - lib.getColonyImageHeight(type)) / 2);
} else if (settlement instanceof IndianSettlement) {
int type = lib.getSettlementGraphicsType(settlement);

View File

@ -53,6 +53,10 @@ public final class Tile extends FreeColGameObject implements Location {
ADD_HILLS = 3,
ADD_MOUNTAINS = 4;
// Indians' claims on the tile may be one of the following:
public static final int CLAIM_NONE = 0,
CLAIM_VISITED = 1,
CLAIM_CLAIMED = 2;
private boolean road,
plowed,
@ -65,6 +69,8 @@ public final class Tile extends FreeColGameObject implements Location {
private int x,
y;
private int indianClaim;
/** A pointer to the settlement located on this tile or 'null' if there is no settlement on this tile. */
private Settlement settlement;
@ -108,6 +114,7 @@ public final class Tile extends FreeColGameObject implements Location {
unitContainer = new UnitContainer(game, this);
this.type = type;
this.addition_type = ADD_NONE;
this.indianClaim = CLAIM_NONE;
road = false;
plowed = false;
@ -331,7 +338,23 @@ public final class Tile extends FreeColGameObject implements Location {
addition_type = addition;
}
/**
* Returns the claim on this Tile.
*
* @return The claim on this Tile.
*/
public int getClaim() {
return indianClaim;
}
/**
* Sets the claim on this Tile.
* @param claim The claim on this Tile.
*/
public void setClaim(int claim) {
indianClaim = claim;
}
/**
* Puts a <code>Settlement</code> on this <code>Tile</code>.
* A <code>Tile</code> can only have one <code>Settlement</code>
@ -768,7 +791,7 @@ public final class Tile extends FreeColGameObject implements Location {
// Check if there is a settlement on this tile: Do not show enemy units hidden in a settlement:
// Check if the player can see the tile: Do not show enemy units on a tile out-of-sight.
if ((settlement == null || settlement.getOwner().equals(player)) && player.canSee(this)) {
if ((settlement == null || settlement instanceof IndianSettlement || settlement.getOwner().equals(player)) && player.canSee(this)) {
tileElement.appendChild(unitContainer.toXMLElement(player, document));
} else {
UnitContainer emptyUnitContainer = new UnitContainer(getGame(), this);

View File

@ -57,16 +57,188 @@ public class MapGenerator {
TerrainGenerator terrainGenerator = new TerrainGenerator(landMap);
Map map = terrainGenerator.createMap();
createIndianSettlements(map);
createEuropeanUnits(map, width, height, players);
return map;
}
/**
* Create the Indian settlements, at least a capital for every nation and
* random numbers of other settlements.
* @throws FreeColException if thrown by a called method
*/
private void createIndianSettlements(Map map) throws FreeColException {
Iterator incaIterator = map.getFloodFillIterator
(getRandomStartingPos(map));
Iterator aztecIterator = map.getFloodFillIterator
(getRandomStartingPos(map));
Iterator arawakIterator = map.getFloodFillIterator
(getRandomStartingPos(map));
Iterator cherokeeIterator = map.getFloodFillIterator
(getRandomStartingPos(map));
Iterator iroquoisIterator = map.getFloodFillIterator
(getRandomStartingPos(map));
Iterator siouxIterator = map.getFloodFillIterator
(getRandomStartingPos(map));
Iterator apacheIterator = map.getFloodFillIterator
(getRandomStartingPos(map));
Iterator tupiIterator = map.getFloodFillIterator
(getRandomStartingPos(map));
placeIndianSettlement(IndianSettlement.INCA,
IndianSettlement.CITY,
true, incaIterator, map);
placeIndianSettlement(IndianSettlement.AZTEC,
IndianSettlement.CITY,
true, aztecIterator, map);
placeIndianSettlement(IndianSettlement.ARAWAK,
IndianSettlement.VILLAGE,
true, arawakIterator, map);
placeIndianSettlement(IndianSettlement.CHEROKEE,
IndianSettlement.VILLAGE,
true, cherokeeIterator, map);
placeIndianSettlement(IndianSettlement.IROQUOIS,
IndianSettlement.VILLAGE,
true, iroquoisIterator, map);
placeIndianSettlement(IndianSettlement.SIOUX,
IndianSettlement.CAMP,
true, siouxIterator, map);
placeIndianSettlement(IndianSettlement.APACHE,
IndianSettlement.CAMP,
true, apacheIterator, map);
placeIndianSettlement(IndianSettlement.TUPI,
IndianSettlement.CAMP,
true, tupiIterator, map);
while (incaIterator.hasNext() || aztecIterator.hasNext() ||
arawakIterator.hasNext() || cherokeeIterator.hasNext() ||
iroquoisIterator.hasNext() || siouxIterator.hasNext() ||
apacheIterator.hasNext() || tupiIterator.hasNext()) {
if (incaIterator.hasNext() && random.nextInt(5) != 0)
placeIndianSettlement(IndianSettlement.INCA,
IndianSettlement.CITY,
false, incaIterator, map);
if (aztecIterator.hasNext() && random.nextInt(5) != 0)
placeIndianSettlement(IndianSettlement.AZTEC,
IndianSettlement.CITY,
false, aztecIterator, map);
if (arawakIterator.hasNext() && random.nextInt(3) != 0)
placeIndianSettlement(IndianSettlement.ARAWAK,
IndianSettlement.VILLAGE,
false, arawakIterator, map);
if (cherokeeIterator.hasNext() && random.nextInt(4) != 0)
placeIndianSettlement(IndianSettlement.CHEROKEE,
IndianSettlement.VILLAGE,
false, cherokeeIterator, map);
if (iroquoisIterator.hasNext() && random.nextInt(4) != 0)
placeIndianSettlement(IndianSettlement.IROQUOIS,
IndianSettlement.VILLAGE,
false, iroquoisIterator, map);
if (siouxIterator.hasNext() && random.nextInt(4) != 0)
placeIndianSettlement(IndianSettlement.SIOUX,
IndianSettlement.CAMP,
false, siouxIterator, map);
if (apacheIterator.hasNext() && random.nextInt(3) != 0)
placeIndianSettlement(IndianSettlement.APACHE,
IndianSettlement.CAMP,
false, apacheIterator, map);
if (tupiIterator.hasNext() && random.nextInt(2) != 0)
placeIndianSettlement(IndianSettlement.TUPI,
IndianSettlement.CAMP,
false, tupiIterator, map);
}
}
/**
* Finds a suitable location for a settlement and builds it there. If no
* location can be found (the iterator is exhausted) the settlement will
* be discarded.
* @param settlement The settlement to place
* @param iterator The nation's iterator to use
* @throws FreeColException if thrown by a called method
*/
private void placeIndianSettlement(int owner, int type,
boolean capital,
Iterator iterator, Map map)
throws FreeColException {
while (iterator.hasNext()) {
Position position = (Position)iterator.next();
int radius = (type == IndianSettlement.CITY) ? 2 : 1;
if (isIndianSettlementCandidate(position, radius + 1, map) &&
random.nextInt(2) != 0) {
//System.out.println("Setting indian settlement at "
// + position.getX() + "x" + position.getY());
//TODO: Make the player not null!
map.getTile(position).setSettlement(
new IndianSettlement(game, null, map.getTile(position), owner, type, capital));
map.getTile(position).setClaim(Tile.CLAIM_CLAIMED);
map.getTile(position).setOwner(map.getTile(position).getSettlement());
Iterator circleIterator = map.getCircleIterator
(position, true, radius);
while (circleIterator.hasNext()) {
Position adjPos = (Position)circleIterator.next();
map.getTile(adjPos).setClaim(Tile.CLAIM_CLAIMED);
map.getTile(adjPos).setOwner(map.getTile(position).getSettlement());
}
//TODO: Place braves into settlements once player != null
return;
}
}
}
/**
* Check to see if it is possible to build an Indian settlement at a
* given map position. A city (Incas and Aztecs) needs a free radius
* of two tiles, a village or camp needs one tile in every direction.
* There must be at least three productive tiles in the area including
* the settlement tile.
* @param position Candidate position
* @param radius necessary radius
* @return True if position suitable for settlement
* @throws FreeColException if thrown by a called method
*/
private boolean isIndianSettlementCandidate(Position position, int radius, Map map)
throws FreeColException {
if (map.getTile(position).getClaim() == Tile.CLAIM_NONE) {
map.getTile(position).setClaim(Tile.CLAIM_VISITED);
if (map.getTile(position).isSettleable()) {
int numSettleableNeighbors = 0;
Iterator iterator = map.getCircleIterator(position, true,
radius);
while (iterator.hasNext()) {
Position adjPos = (Position)iterator.next();
if (map.getTile(adjPos).getClaim() == Tile.CLAIM_CLAIMED) {
return false;
}
if (map.getTile(adjPos).isSettleable() &&
map.getDistance(position.getX(), position.getY(),
adjPos.getX(), adjPos.getY()) == 1) {
numSettleableNeighbors++;
}
}
return numSettleableNeighbors >= 2;
}
}
return false;
}
/**
* Select a random position on the map to use as a starting position.
* @return Position selected
*/
private Position getRandomStartingPos(Map map) {
int x = 0;
int y = 0;
while (!map.getTile(x, y).isLand()) {
x = random.nextInt(width - 20) + 10;
y = random.nextInt(height - 20) + 10;
}
return new Map.Position(x, y);
}
/**
* Create two ships, one with a colonist, for each player, and
@ -159,8 +331,8 @@ public class MapGenerator {
private static final int MAX_DISTANCE_TO_EDGE = 12;
private boolean[][] landMap;
private Random tileType;
private Random tileType;
TerrainGenerator(boolean[][] landMap) {
@ -175,33 +347,33 @@ public class MapGenerator {
if (max > 99) max = 99;
int thisValue = tileType.nextInt(max - min) + min;
thisValue /= 10; // Gives a number from 0-3.
int minWoD = 0;
int maxWoD = 99;
int dryOrWet = tileType.nextInt(maxWoD - minWoD) + minWoD;
dryOrWet /= 33;
int minWoD = 0;
int maxWoD = 99;
int dryOrWet = tileType.nextInt(maxWoD - minWoD) + minWoD;
dryOrWet /= 33;
switch(thisValue) {
case 0: return Tile.ARCTIC;
case 1: case 2: switch (dryOrWet) {
case 0: return Tile.TUNDRA;
case 1: default: return Tile.TUNDRA;
case 2: return Tile.MARSH;
}
case 3: case 4: case 5: case 6: default: switch (dryOrWet) {
case 0: return Tile.PLAINS;
case 1: default: return Tile.PRAIRIE;
case 2: return Tile.SWAMP;
}
case 7: case 8: case 9: switch (dryOrWet) {
case 0: return Tile.GRASSLANDS;
case 1: default: return Tile.SAVANNAH;
case 2: return Tile.DESERT;
}
case 0: return Tile.ARCTIC;
case 1: case 2: switch (dryOrWet) {
case 0: return Tile.TUNDRA;
case 1: default: return Tile.TUNDRA;
case 2: return Tile.MARSH;
}
case 3: case 4: case 5: case 6: default: switch (dryOrWet) {
case 0: return Tile.PLAINS;
case 1: default: return Tile.PRAIRIE;
case 2: return Tile.SWAMP;
}
case 7: case 8: case 9: switch (dryOrWet) {
case 0: return Tile.GRASSLANDS;
case 1: default: return Tile.SAVANNAH;
case 2: return Tile.DESERT;
}
}
}
public Map createMap() {
Vector columns = new Vector(width);
tileType = new Random();
tileType = new Random();
for (int i = 0; i < width; i++) {
Vector v = new Vector(height);
for (int j = 0; j < height; j++) {