Cities gain tiles in layers

Cities spawned with 1 layer of tiles instead of 2
Tiles have owner parameter
Currently buit improvement is no longer an improvement option
This commit is contained in:
Yair Morgenstern 2017-11-28 23:06:34 +02:00
parent 2b8fefe675
commit 214f64effb
11 changed files with 85 additions and 69 deletions

View File

@ -8,7 +8,6 @@ import com.unciv.models.gamebasics.Building;
import com.unciv.models.gamebasics.GameBasics; import com.unciv.models.gamebasics.GameBasics;
import com.unciv.models.stats.FullStats; import com.unciv.models.stats.FullStats;
import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
@ -83,7 +82,7 @@ public class CityBuildings
if(IsBuilt(building.Name)) return false; if(IsBuilt(building.Name)) return false;
// if (building.Name.equals("Worker") || building.Name.equals("Settler")) return false; // if (building.Name.equals("Worker") || building.Name.equals("Settler")) return false;
if(building.ResourceRequired) { if(building.ResourceRequired) {
boolean containsResourceWithImprovement = GetCity().getCityTiles() boolean containsResourceWithImprovement = GetCity().getTilesInRange()
.any(new Predicate<TileInfo>() { .any(new Predicate<TileInfo>() {
@Override @Override
public boolean evaluate(TileInfo tile) { public boolean evaluate(TileInfo tile) {

View File

@ -2,16 +2,13 @@ package com.unciv.civinfo;
import com.badlogic.gdx.math.Vector2; import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.utils.Predicate; import com.badlogic.gdx.utils.Predicate;
import com.unciv.game.HexMath;
import com.unciv.game.UnCivGame; import com.unciv.game.UnCivGame;
import com.unciv.models.LinqCollection; import com.unciv.models.LinqCollection;
import com.unciv.models.gamebasics.Building;
import com.unciv.models.gamebasics.ResourceType; import com.unciv.models.gamebasics.ResourceType;
import com.unciv.models.gamebasics.TileResource; import com.unciv.models.gamebasics.TileResource;
import com.unciv.models.stats.FullStats; import com.unciv.models.stats.FullStats;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Comparator;
public class CityInfo { public class CityInfo {
public final Vector2 cityLocation; public final Vector2 cityLocation;
@ -22,14 +19,13 @@ public class CityInfo {
public int cultureStored; public int cultureStored;
private int tilesClaimed; private int tilesClaimed;
private TileMap getTileMap(){return UnCivGame.Current.civInfo.tileMap; }
public LinqCollection<Vector2> CityTileLocations = new LinqCollection<Vector2>(); public LinqCollection<TileInfo> getTilesInRange(){
return getTileMap().getTilesInDistance(cityLocation,3).where(new Predicate<TileInfo>() {
public LinqCollection<TileInfo> getCityTiles(){
return CityTileLocations.select(new com.unciv.models.LinqCollection.Func<Vector2, TileInfo>() {
@Override @Override
public TileInfo GetBy(Vector2 arg0) { public boolean evaluate(TileInfo arg0) {
return UnCivGame.Current.civInfo.tileMap.get(arg0); return UnCivGame.Current.civInfo.civName.equals(arg0.Owner);
} }
}); });
} }
@ -56,11 +52,11 @@ public class CityInfo {
cityBuildings = new CityBuildings(this); cityBuildings = new CityBuildings(this);
cityPopulation = new CityPopulation(); cityPopulation = new CityPopulation();
for(Vector2 vector : HexMath.GetVectorsInDistance(cityLocation,2)) for(TileInfo tileInfo : civInfo.tileMap.getTilesInDistance(cityLocation,1)) {
{ tileInfo.Owner = civInfo.civName;
if(civInfo.tileMap.get(vector).GetCity() == null)
CityTileLocations.add(vector);
} }
civInfo.tileMap.get(cityLocation).WorkingCity = this.Name;
autoAssignWorker(); autoAssignWorker();
civInfo.Cities.add(this); civInfo.Cities.add(this);
@ -68,8 +64,8 @@ public class CityInfo {
ArrayList<String> getLuxuryResources() { ArrayList<String> getLuxuryResources() {
ArrayList<String> LuxuryResources = new ArrayList<String>(); ArrayList<String> LuxuryResources = new ArrayList<String>();
for (TileInfo tileInfo : getCityTiles()) { for (TileInfo tileInfo : getTilesInRange()) {
com.unciv.models.gamebasics.TileResource resource = tileInfo.GetTileResource(); TileResource resource = tileInfo.GetTileResource();
if (resource != null && resource.ResourceType == ResourceType.Luxury && resource.Improvement.equals(tileInfo.Improvement)) if (resource != null && resource.ResourceType == ResourceType.Luxury && resource.Improvement.equals(tileInfo.Improvement))
LuxuryResources.add(tileInfo.Resource); LuxuryResources.add(tileInfo.Resource);
} }
@ -78,12 +74,12 @@ public class CityInfo {
private int getWorkingPopulation() { private int getWorkingPopulation() {
return getCityTiles().count(new Predicate<TileInfo>() { return getTilesInRange().count(new Predicate<TileInfo>() {
@Override @Override
public boolean evaluate(TileInfo arg0) { public boolean evaluate(TileInfo arg0) {
return arg0.IsWorked; return Name.equals(arg0.WorkingCity);
} }
}); })-1; // 1 is the city center
} }
public int getFreePopulation() { public int getFreePopulation() {
@ -102,9 +98,10 @@ public class CityInfo {
stats.Science += cityPopulation.Population; stats.Science += cityPopulation.Population;
// Working ppl // Working ppl
for (TileInfo cell : getCityTiles()) { for (TileInfo cell : getTilesInRange())
if (cell.IsWorked || cell.IsCityCenter()) stats.add(cell.GetTileStats()); if (Name.equals(cell.WorkingCity) || cell.IsCityCenter())
} stats.add(cell.GetTileStats());
//idle ppl //idle ppl
stats.Production += getFreePopulation(); stats.Production += getFreePopulation();
stats.Food -= cityPopulation.Population * 2; stats.Food -= cityPopulation.Population * 2;
@ -126,7 +123,7 @@ public class CityInfo {
cityBuildings.NextTurn(stats.Production); cityBuildings.NextTurn(stats.Production);
for (TileInfo tileInfo : getCityTiles()) { for (TileInfo tileInfo : getTilesInRange()) {
tileInfo.NextTurn(); tileInfo.NextTurn();
} }
@ -140,35 +137,36 @@ public class CityInfo {
cultureStored -= getCultureToNextTile(); cultureStored -= getCultureToNextTile();
tilesClaimed++; tilesClaimed++;
LinqCollection<Vector2> possibleNewTileVectors = new LinqCollection<Vector2>(); LinqCollection<Vector2> possibleNewTileVectors = new LinqCollection<Vector2>();
for (TileInfo tile : getCityTiles())
for (Vector2 vector : HexMath.GetAdjacentVectors(tile.Position))
if(!CityTileLocations.contains(vector) && !possibleNewTileVectors.contains(vector))
possibleNewTileVectors.add(vector);
LinqCollection<TileInfo> possibleNewTiles = new LinqCollection<TileInfo>(); for (int i = 2; i <4 ; i++) {
TileMap tileMap = UnCivGame.Current.civInfo.tileMap; LinqCollection<TileInfo> tiles = getTileMap().getTilesInDistance(cityLocation,i);
for (Vector2 vector : possibleNewTileVectors) tiles = tiles.where(new Predicate<TileInfo>() {
if(tileMap.contains(vector) && tileMap.get(vector).GetCity()==null) @Override
possibleNewTiles.add(tileMap.get(vector)); public boolean evaluate(TileInfo arg0) {
return arg0.Owner == null;
}
});
if(tiles.size()==0) continue;
TileInfo TileChosen=null; TileInfo TileChosen=null;
double TileChosenRank=0; double TileChosenRank=0;
for(TileInfo tile : possibleNewTiles){ for(TileInfo tile : tiles){
double rank = rankTile(tile); double rank = rankTile(tile);
if(rank>TileChosenRank){ if(rank>TileChosenRank){
TileChosenRank = rank; TileChosenRank = rank;
TileChosen = tile; TileChosen = tile;
}
} }
TileChosen.Owner = UnCivGame.Current.civInfo.civName;
return;
} }
CityTileLocations.add(TileChosen.Position);
} }
private void autoAssignWorker() { private void autoAssignWorker() {
double maxValue = 0; double maxValue = 0;
TileInfo toWork = null; TileInfo toWork = null;
for (TileInfo tileInfo : getCityTiles()) { for (TileInfo tileInfo : getTilesInRange()) {
if (tileInfo.IsWorked || tileInfo.IsCityCenter()) continue; if (tileInfo.WorkingCity!=null) continue;
FullStats stats = tileInfo.GetTileStats(); FullStats stats = tileInfo.GetTileStats();
double value = stats.Food + stats.Production * 0.5; double value = stats.Food + stats.Production * 0.5;
@ -177,7 +175,7 @@ public class CityInfo {
toWork = tileInfo; toWork = tileInfo;
} }
} }
toWork.IsWorked = true; toWork.WorkingCity = Name;
} }
private double rankTile(TileInfo tile){ private double rankTile(TileInfo tile){

View File

@ -1,6 +1,7 @@
package com.unciv.civinfo; package com.unciv.civinfo;
import com.badlogic.gdx.math.Vector2; import com.badlogic.gdx.math.Vector2;
import com.unciv.game.UnCivGame;
import com.unciv.models.LinqCollection; import com.unciv.models.LinqCollection;
import com.unciv.models.gamebasics.GameBasics; import com.unciv.models.gamebasics.GameBasics;
import com.unciv.models.stats.CivStats; import com.unciv.models.stats.CivStats;
@ -11,9 +12,11 @@ import java.util.HashSet;
* Created by LENOVO on 10/18/2017. * Created by LENOVO on 10/18/2017.
*/ */
public class CivilizationInfo { public class CivilizationInfo {
public static CivilizationInfo current(){return UnCivGame.Current.civInfo; }
public CivStats civStats = new CivStats(); public CivStats civStats = new CivStats();
public int baseHappiness = 15; public int baseHappiness = 15;
public String civName = "Babylon";
public CivilizationTech Tech = new CivilizationTech(); public CivilizationTech Tech = new CivilizationTech();
public int turns = 1; public int turns = 1;

View File

@ -16,16 +16,20 @@ public class TileInfo
public String BaseTerrain; public String BaseTerrain;
public String TerrainFeature; public String TerrainFeature;
public String Resource; public String Resource;
public boolean IsWorked = false; // public boolean IsWorked = false;
public String Improvement; public String Improvement;
public String ImprovementInProgress; public String ImprovementInProgress;
public String Owner; // owning civ name
public String WorkingCity; // Working City Name
public int TurnsToImprovement; public int TurnsToImprovement;
public Terrain GetBaseTerrain(){return GameBasics.Terrains.get(BaseTerrain);} public Terrain GetBaseTerrain(){return GameBasics.Terrains.get(BaseTerrain);}
public CityInfo GetCity(){return UnCivGame.Current.civInfo.Cities.first(new Predicate<CityInfo>() { public CityInfo GetCity(){
if(WorkingCity == null) return null;
return CivilizationInfo.current().Cities.first(new Predicate<CityInfo>() {
@Override @Override
public boolean evaluate(CityInfo arg0) { public boolean evaluate(CityInfo arg0) {
return arg0.CityTileLocations.contains(Position); return arg0.Name.equals(WorkingCity);
} }
});} });}

View File

@ -67,8 +67,6 @@ public class TileMap{
} }
if (resource != null) tileInfo.Resource = resource.Name; if (resource != null) tileInfo.Resource = resource.Name;
// tileInfo.City = this;
// GetCityTiles.put(vector2, tileInfo);
tiles.put(position.toString(),tileInfo); tiles.put(position.toString(),tileInfo);
} }
@ -76,6 +74,16 @@ public class TileMap{
public TileInfo get(Vector2 vector){return tiles.get(vector.toString());} public TileInfo get(Vector2 vector){return tiles.get(vector.toString());}
public LinqCollection<TileInfo> getTilesInDistance(Vector2 origin, int distance){
LinqCollection<TileInfo> tiles = new LinqCollection<TileInfo>();
for(Vector2 vector : HexMath.GetVectorsInDistance(origin, distance))
if(contains(vector))
tiles.add(get(vector));
return tiles;
}
public LinqCollection<TileInfo> values(){return tiles.linqValues();} public LinqCollection<TileInfo> values(){return tiles.linqValues();}
public TileResource GetRandomResource(LinqCollection<TileResource> resources, final ResourceType resourceType) { public TileResource GetRandomResource(LinqCollection<TileResource> resources, final ResourceType resourceType) {

View File

@ -123,8 +123,7 @@ public class CityScreen extends CameraStageBaseScreen {
Group allTiles = new Group(); Group allTiles = new Group();
for(Vector2 vector : HexMath.GetVectorsInDistance(cityInfo.cityLocation,5)){ for(final TileInfo tileInfo : game.civInfo.tileMap.getTilesInDistance(cityInfo.cityLocation,5)){
final TileInfo tileInfo = game.civInfo.tileMap.get(vector);
TileGroup group = new TileGroup(tileInfo); TileGroup group = new TileGroup(tileInfo);
group.addListener(new ClickListener(){ group.addListener(new ClickListener(){
@Override @Override
@ -134,20 +133,20 @@ public class CityScreen extends CameraStageBaseScreen {
} }
}); });
if(!cityInfo.getCityTiles().contains(tileInfo)) group.setColor(0,0,0,0.3f); if(!cityInfo.getTilesInRange().contains(tileInfo)) group.setColor(0,0,0,0.3f);
else if(!tileInfo.IsCityCenter()) { else if(!tileInfo.IsCityCenter()) {
group.addPopulationIcon(); group.addPopulationIcon();
group.populationImage.addListener(new ClickListener() { group.populationImage.addListener(new ClickListener() {
@Override @Override
public void clicked(InputEvent event, float x, float y) { public void clicked(InputEvent event, float x, float y) {
if (cityInfo.getFreePopulation() > 0 || tileInfo.IsWorked) if(tileInfo.WorkingCity==null && cityInfo.getFreePopulation() > 0) tileInfo.WorkingCity = cityInfo.Name;
tileInfo.IsWorked = !tileInfo.IsWorked; else if(cityInfo.Name.equals(tileInfo.WorkingCity)) tileInfo.WorkingCity = null;
updateCityTable(); updateCityTable();
} }
}); });
} }
Vector2 positionalVector = HexMath.Hex2WorldCoords(vector.cpy().sub(cityInfo.cityLocation)); Vector2 positionalVector = HexMath.Hex2WorldCoords(tileInfo.Position.cpy().sub(cityInfo.cityLocation));
int groupSize = 50; int groupSize = 50;
group.setPosition(stage.getWidth()/2 + positionalVector.x*0.8f * groupSize, group.setPosition(stage.getWidth()/2 + positionalVector.x*0.8f * groupSize,
stage.getHeight()/2 + positionalVector.y*0.8f * groupSize); stage.getHeight()/2 + positionalVector.y*0.8f * groupSize);

View File

@ -78,7 +78,7 @@ public class TileGroup extends Group {
} }
if(populationImage!=null){ if(populationImage!=null){
if(tileInfo.IsWorked) populationImage.setColor(Color.WHITE); if(tileInfo.WorkingCity!=null) populationImage.setColor(Color.WHITE);
else populationImage.setColor(Color.GRAY); else populationImage.setColor(Color.GRAY);
} }
} }

View File

@ -25,7 +25,13 @@ public class UnCivGame extends Game {
public void create() { public void create() {
SetupGameBasics(); SetupGameBasics();
Current = this; Current = this;
if(GameSaver.GetSave("Autosave").exists()) GameSaver.LoadGame(this,"Autosave"); if(GameSaver.GetSave("Autosave").exists()) {
try {
GameSaver.LoadGame(this, "Autosave");
} catch(Exception ex){ // silent fail if we can't read the autosave
startNewGame();
}
}
else startNewGame(); else startNewGame();
worldScreen = new WorldScreen(this); worldScreen = new WorldScreen(this);

View File

@ -348,11 +348,10 @@ public class WorldScreen extends CameraStageBaseScreen {
} }
}); });
if(HexMath.GetVectorsInDistance(selectedTile.Position,2).any(new Predicate<Vector2>() { if(game.civInfo.tileMap.getTilesInDistance(selectedTile.Position,2).any(new Predicate<TileInfo>() {
@Override @Override
public boolean evaluate(Vector2 arg0) { public boolean evaluate(TileInfo arg0) {
return tileGroups.containsKey(arg0.toString()) && return arg0.IsCityCenter();
tileGroups.get(arg0.toString()).tileInfo.IsCityCenter();
} }
})){ })){
foundCityButton.setDisabled(true); foundCityButton.setDisabled(true);
@ -414,9 +413,9 @@ public class WorldScreen extends CameraStageBaseScreen {
HashSet<String> ViewableVectorStrings = new HashSet<String>(); HashSet<String> ViewableVectorStrings = new HashSet<String>();
// tiles adjacent to city tiles // tiles adjacent to city tiles
for(CityInfo city : game.civInfo.Cities) for(TileInfo tileInfo : game.civInfo.tileMap.values())
for(Vector2 tileLocation : city.CityTileLocations) if(game.civInfo.civName.equals(tileInfo.Owner))
for(Vector2 adjacentLocation : HexMath.GetAdjacentVectors(tileLocation)) for(Vector2 adjacentLocation : HexMath.GetAdjacentVectors(tileInfo.Position))
ViewableVectorStrings.add(adjacentLocation.toString()); ViewableVectorStrings.add(adjacentLocation.toString());
// Tiles within 2 tiles of units // Tiles within 2 tiles of units

View File

@ -27,11 +27,11 @@ public class WorldTileGroup extends TileGroup {
void update(WorldScreen worldScreen) { void update(WorldScreen worldScreen) {
super.update(); super.update();
if(tileInfo.IsWorked && populationImage==null) addPopulationIcon(); if(tileInfo.WorkingCity != null && populationImage==null) addPopulationIcon();
if(!tileInfo.IsWorked && populationImage!=null) removePopulationIcon(); if(tileInfo.WorkingCity == null && populationImage!=null) removePopulationIcon();
if (tileInfo.GetCity() != null && hexagon == null) { if (tileInfo.Owner != null && hexagon == null) {
hexagon = ImageGetter.getImageByFilename("TerrainIcons/Hexagon.png"); hexagon = ImageGetter.getImageByFilename("TerrainIcons/Hexagon.png");
float imageScale = terrainImage.getWidth() * 1.3f / hexagon.getWidth(); float imageScale = terrainImage.getWidth() * 1.3f / hexagon.getWidth();
hexagon.setScale(imageScale); hexagon.setScale(imageScale);

View File

@ -35,7 +35,7 @@ public class ImprovementPickerScreen extends PickerScreen {
rightSideButton.setColor(Color.GRAY); rightSideButton.setColor(Color.GRAY);
for(final TileImprovement improvement : GameBasics.TileImprovements.values()) { for(final TileImprovement improvement : GameBasics.TileImprovements.values()) {
if(!tileInfo.CanBuildImprovement(improvement)) continue; if(!tileInfo.CanBuildImprovement(improvement) || improvement.Name.equals(tileInfo.Improvement)) continue;
TextButton TB = new TextButton(improvement.Name+"\r\n"+improvement.TurnsToBuild+" turns", skin); TextButton TB = new TextButton(improvement.Name+"\r\n"+improvement.TurnsToBuild+" turns", skin);
TB.addListener(new ClickListener(){ TB.addListener(new ClickListener(){
@Override @Override