package org.freerealm.tile;
import org.freerealm.unit.Unit;
import org.freerealm.settlement.Settlement;
import java.util.TreeMap;
import java.util.Iterator;
import java.util.Vector;
import org.freerealm.resource.Resource;
import org.freerealm.resource.bonus.BonusResource;
import org.freerealm.tile.improvement.TileImprovementType;
import org.freerealm.vegetation.Vegetation;
/**
*
* @author Deniz ARIKAN
*/
public class Tile {
private TileType type;
private Vegetation vegetation;
private Vector<TileImprovementType> improvements;
private TreeMap<Integer, Unit> units;
private Settlement settlement;
private BonusResource bonusResource;
public Tile() {
units = new TreeMap<Integer, Unit>();
improvements = new Vector<TileImprovementType>();
}
public Tile(TileType type) {
this();
this.type = type;
}
public int getProduction(Resource resource) {
return getType().getProduction(resource) + getResourceModifier(resource);
}
private int getResourceModifier(Resource resource) {
int resourceModifier = 0;
boolean tileHasResource = (getType().getProduction(resource) > 0);
Iterator<TileImprovementType> improvementsIterator = getImprovementsIterator();
while (improvementsIterator.hasNext()) {
TileImprovementType improvement = improvementsIterator.next();
resourceModifier = resourceModifier + improvement.getResourceModifier(resource, tileHasResource);
}
if (getVegetation() != null) {
resourceModifier = resourceModifier + getVegetation().getResourceModifier(resource, tileHasResource);
}
if (getBonusResource() != null) {
resourceModifier = resourceModifier + getBonusResource().getResourceModifier(resource, tileHasResource);
}
return resourceModifier;
}
public void putUnit(Unit unit) {
units.put(unit.getId(), unit);
}
public void removeUnit(Unit unit) {
units.remove(unit.getId());
}
public int getNumberOfUnits() {
return getUnits().size();
}
public TreeMap<Integer, Unit> getUnits() {
return units;
}
public Settlement getSettlement() {
return settlement;
}
public void setSettlement(Settlement settlement) {
this.settlement = settlement;
}
public TileType getType() {
return type;
}
public void setType(TileType type) {
this.type = type;
}
public Iterator<Unit> getUnitsIterator() {
return getUnits().values().iterator();
}
public float getMovementCost() {
float typeMovementCost = getType().getMovementCost();
if (getVegetation() != null) {
if (getVegetation().getMovementCost() > typeMovementCost) {
typeMovementCost = getVegetation().getMovementCost();
}
}
float improvementMovementCost = getImprovementMovementCost();
if (typeMovementCost > improvementMovementCost) {
typeMovementCost = improvementMovementCost;
}
return typeMovementCost;
}
private float getImprovementMovementCost() {
float improvementMovementCost = Float.MAX_VALUE;
Iterator<TileImprovementType> improvemenIterator = getImprovementsIterator();
while (improvemenIterator.hasNext()) {
TileImprovementType improvement = improvemenIterator.next();
if (improvement.getMovementCost() != 0 && improvement.getMovementCost() < improvementMovementCost) {
improvementMovementCost = improvement.getMovementCost();
}
}
return improvementMovementCost;
}
public boolean hasImprovement(TileImprovementType improvement) {
return getImprovements().contains(improvement);
}
public Vegetation getVegetation() {
return vegetation;
}
public void setVegetation(Vegetation vegetation) {
this.vegetation = vegetation;
}
public Iterator<TileImprovementType> getImprovementsIterator() {
return getImprovements().iterator();
}
public void addImprovement(TileImprovementType improvement) {
getImprovements().add(improvement);
}
private Vector<TileImprovementType> getImprovements() {
return improvements;
}
public int getImprovementCount() {
return getImprovements().size();
}
public void clearImprovements() {
getImprovements().clear();
}
public BonusResource getBonusResource() {
return bonusResource;
}
public void setBonusResource(BonusResource bonusResource) {
this.bonusResource = bonusResource;
}
}