From 214f64effbf8c8fc3c3d76a2d9e11b0de6a88d88 Mon Sep 17 00:00:00 2001 From: Yair Morgenstern Date: Tue, 28 Nov 2017 23:06:34 +0200 Subject: [PATCH] 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 --- core/src/com/unciv/civinfo/CityBuildings.java | 3 +- core/src/com/unciv/civinfo/CityInfo.java | 84 +++++++++---------- .../com/unciv/civinfo/CivilizationInfo.java | 3 + core/src/com/unciv/civinfo/TileInfo.java | 10 ++- core/src/com/unciv/civinfo/TileMap.java | 12 ++- core/src/com/unciv/game/CityScreen.java | 11 ++- core/src/com/unciv/game/TileGroup.java | 2 +- core/src/com/unciv/game/UnCivGame.java | 8 +- core/src/com/unciv/game/WorldScreen.java | 13 ++- core/src/com/unciv/game/WorldTileGroup.java | 6 +- .../ImprovementPickerScreen.java | 2 +- 11 files changed, 85 insertions(+), 69 deletions(-) diff --git a/core/src/com/unciv/civinfo/CityBuildings.java b/core/src/com/unciv/civinfo/CityBuildings.java index ee75e3a6..ba4b9890 100644 --- a/core/src/com/unciv/civinfo/CityBuildings.java +++ b/core/src/com/unciv/civinfo/CityBuildings.java @@ -8,7 +8,6 @@ import com.unciv.models.gamebasics.Building; import com.unciv.models.gamebasics.GameBasics; import com.unciv.models.stats.FullStats; -import java.util.ArrayList; import java.util.HashMap; @@ -83,7 +82,7 @@ public class CityBuildings if(IsBuilt(building.Name)) return false; // if (building.Name.equals("Worker") || building.Name.equals("Settler")) return false; if(building.ResourceRequired) { - boolean containsResourceWithImprovement = GetCity().getCityTiles() + boolean containsResourceWithImprovement = GetCity().getTilesInRange() .any(new Predicate() { @Override public boolean evaluate(TileInfo tile) { diff --git a/core/src/com/unciv/civinfo/CityInfo.java b/core/src/com/unciv/civinfo/CityInfo.java index 466f2c71..8fa575bc 100644 --- a/core/src/com/unciv/civinfo/CityInfo.java +++ b/core/src/com/unciv/civinfo/CityInfo.java @@ -2,16 +2,13 @@ package com.unciv.civinfo; import com.badlogic.gdx.math.Vector2; import com.badlogic.gdx.utils.Predicate; -import com.unciv.game.HexMath; import com.unciv.game.UnCivGame; import com.unciv.models.LinqCollection; -import com.unciv.models.gamebasics.Building; import com.unciv.models.gamebasics.ResourceType; import com.unciv.models.gamebasics.TileResource; import com.unciv.models.stats.FullStats; import java.util.ArrayList; -import java.util.Comparator; public class CityInfo { public final Vector2 cityLocation; @@ -22,14 +19,13 @@ public class CityInfo { public int cultureStored; private int tilesClaimed; + private TileMap getTileMap(){return UnCivGame.Current.civInfo.tileMap; } - public LinqCollection CityTileLocations = new LinqCollection(); - - public LinqCollection getCityTiles(){ - return CityTileLocations.select(new com.unciv.models.LinqCollection.Func() { + public LinqCollection getTilesInRange(){ + return getTileMap().getTilesInDistance(cityLocation,3).where(new Predicate() { @Override - public TileInfo GetBy(Vector2 arg0) { - return UnCivGame.Current.civInfo.tileMap.get(arg0); + public boolean evaluate(TileInfo arg0) { + return UnCivGame.Current.civInfo.civName.equals(arg0.Owner); } }); } @@ -56,11 +52,11 @@ public class CityInfo { cityBuildings = new CityBuildings(this); cityPopulation = new CityPopulation(); - for(Vector2 vector : HexMath.GetVectorsInDistance(cityLocation,2)) - { - if(civInfo.tileMap.get(vector).GetCity() == null) - CityTileLocations.add(vector); + for(TileInfo tileInfo : civInfo.tileMap.getTilesInDistance(cityLocation,1)) { + tileInfo.Owner = civInfo.civName; } + civInfo.tileMap.get(cityLocation).WorkingCity = this.Name; + autoAssignWorker(); civInfo.Cities.add(this); @@ -68,8 +64,8 @@ public class CityInfo { ArrayList getLuxuryResources() { ArrayList LuxuryResources = new ArrayList(); - for (TileInfo tileInfo : getCityTiles()) { - com.unciv.models.gamebasics.TileResource resource = tileInfo.GetTileResource(); + for (TileInfo tileInfo : getTilesInRange()) { + TileResource resource = tileInfo.GetTileResource(); if (resource != null && resource.ResourceType == ResourceType.Luxury && resource.Improvement.equals(tileInfo.Improvement)) LuxuryResources.add(tileInfo.Resource); } @@ -78,12 +74,12 @@ public class CityInfo { private int getWorkingPopulation() { - return getCityTiles().count(new Predicate() { + return getTilesInRange().count(new Predicate() { @Override public boolean evaluate(TileInfo arg0) { - return arg0.IsWorked; + return Name.equals(arg0.WorkingCity); } - }); + })-1; // 1 is the city center } public int getFreePopulation() { @@ -102,9 +98,10 @@ public class CityInfo { stats.Science += cityPopulation.Population; // Working ppl - for (TileInfo cell : getCityTiles()) { - if (cell.IsWorked || cell.IsCityCenter()) stats.add(cell.GetTileStats()); - } + for (TileInfo cell : getTilesInRange()) + if (Name.equals(cell.WorkingCity) || cell.IsCityCenter()) + stats.add(cell.GetTileStats()); + //idle ppl stats.Production += getFreePopulation(); stats.Food -= cityPopulation.Population * 2; @@ -126,7 +123,7 @@ public class CityInfo { cityBuildings.NextTurn(stats.Production); - for (TileInfo tileInfo : getCityTiles()) { + for (TileInfo tileInfo : getTilesInRange()) { tileInfo.NextTurn(); } @@ -140,35 +137,36 @@ public class CityInfo { cultureStored -= getCultureToNextTile(); tilesClaimed++; LinqCollection possibleNewTileVectors = new LinqCollection(); - for (TileInfo tile : getCityTiles()) - for (Vector2 vector : HexMath.GetAdjacentVectors(tile.Position)) - if(!CityTileLocations.contains(vector) && !possibleNewTileVectors.contains(vector)) - possibleNewTileVectors.add(vector); - LinqCollection possibleNewTiles = new LinqCollection(); - TileMap tileMap = UnCivGame.Current.civInfo.tileMap; - for (Vector2 vector : possibleNewTileVectors) - if(tileMap.contains(vector) && tileMap.get(vector).GetCity()==null) - possibleNewTiles.add(tileMap.get(vector)); + for (int i = 2; i <4 ; i++) { + LinqCollection tiles = getTileMap().getTilesInDistance(cityLocation,i); + tiles = tiles.where(new Predicate() { + @Override + public boolean evaluate(TileInfo arg0) { + return arg0.Owner == null; + } + }); + if(tiles.size()==0) continue; - TileInfo TileChosen=null; - double TileChosenRank=0; - for(TileInfo tile : possibleNewTiles){ - double rank = rankTile(tile); - if(rank>TileChosenRank){ - TileChosenRank = rank; - TileChosen = tile; + TileInfo TileChosen=null; + double TileChosenRank=0; + for(TileInfo tile : tiles){ + double rank = rankTile(tile); + if(rank>TileChosenRank){ + TileChosenRank = rank; + TileChosen = tile; + } } + TileChosen.Owner = UnCivGame.Current.civInfo.civName; + return; } - - CityTileLocations.add(TileChosen.Position); } private void autoAssignWorker() { double maxValue = 0; TileInfo toWork = null; - for (TileInfo tileInfo : getCityTiles()) { - if (tileInfo.IsWorked || tileInfo.IsCityCenter()) continue; + for (TileInfo tileInfo : getTilesInRange()) { + if (tileInfo.WorkingCity!=null) continue; FullStats stats = tileInfo.GetTileStats(); double value = stats.Food + stats.Production * 0.5; @@ -177,7 +175,7 @@ public class CityInfo { toWork = tileInfo; } } - toWork.IsWorked = true; + toWork.WorkingCity = Name; } private double rankTile(TileInfo tile){ diff --git a/core/src/com/unciv/civinfo/CivilizationInfo.java b/core/src/com/unciv/civinfo/CivilizationInfo.java index 5e57df3a..0015a070 100644 --- a/core/src/com/unciv/civinfo/CivilizationInfo.java +++ b/core/src/com/unciv/civinfo/CivilizationInfo.java @@ -1,6 +1,7 @@ package com.unciv.civinfo; import com.badlogic.gdx.math.Vector2; +import com.unciv.game.UnCivGame; import com.unciv.models.LinqCollection; import com.unciv.models.gamebasics.GameBasics; import com.unciv.models.stats.CivStats; @@ -11,9 +12,11 @@ import java.util.HashSet; * Created by LENOVO on 10/18/2017. */ public class CivilizationInfo { + public static CivilizationInfo current(){return UnCivGame.Current.civInfo; } public CivStats civStats = new CivStats(); public int baseHappiness = 15; + public String civName = "Babylon"; public CivilizationTech Tech = new CivilizationTech(); public int turns = 1; diff --git a/core/src/com/unciv/civinfo/TileInfo.java b/core/src/com/unciv/civinfo/TileInfo.java index f9b2454b..cea01132 100644 --- a/core/src/com/unciv/civinfo/TileInfo.java +++ b/core/src/com/unciv/civinfo/TileInfo.java @@ -16,16 +16,20 @@ public class TileInfo public String BaseTerrain; public String TerrainFeature; public String Resource; - public boolean IsWorked = false; +// public boolean IsWorked = false; public String Improvement; public String ImprovementInProgress; + public String Owner; // owning civ name + public String WorkingCity; // Working City Name public int TurnsToImprovement; public Terrain GetBaseTerrain(){return GameBasics.Terrains.get(BaseTerrain);} - public CityInfo GetCity(){return UnCivGame.Current.civInfo.Cities.first(new Predicate() { + public CityInfo GetCity(){ + if(WorkingCity == null) return null; + return CivilizationInfo.current().Cities.first(new Predicate() { @Override public boolean evaluate(CityInfo arg0) { - return arg0.CityTileLocations.contains(Position); + return arg0.Name.equals(WorkingCity); } });} diff --git a/core/src/com/unciv/civinfo/TileMap.java b/core/src/com/unciv/civinfo/TileMap.java index 662dce04..f505abad 100644 --- a/core/src/com/unciv/civinfo/TileMap.java +++ b/core/src/com/unciv/civinfo/TileMap.java @@ -67,8 +67,6 @@ public class TileMap{ } if (resource != null) tileInfo.Resource = resource.Name; -// tileInfo.City = this; -// GetCityTiles.put(vector2, tileInfo); tiles.put(position.toString(),tileInfo); } @@ -76,6 +74,16 @@ public class TileMap{ public TileInfo get(Vector2 vector){return tiles.get(vector.toString());} + public LinqCollection getTilesInDistance(Vector2 origin, int distance){ + LinqCollection tiles = new LinqCollection(); + + for(Vector2 vector : HexMath.GetVectorsInDistance(origin, distance)) + if(contains(vector)) + tiles.add(get(vector)); + + return tiles; + } + public LinqCollection values(){return tiles.linqValues();} public TileResource GetRandomResource(LinqCollection resources, final ResourceType resourceType) { diff --git a/core/src/com/unciv/game/CityScreen.java b/core/src/com/unciv/game/CityScreen.java index abcc4d3d..24f7583d 100644 --- a/core/src/com/unciv/game/CityScreen.java +++ b/core/src/com/unciv/game/CityScreen.java @@ -123,8 +123,7 @@ public class CityScreen extends CameraStageBaseScreen { Group allTiles = new Group(); - for(Vector2 vector : HexMath.GetVectorsInDistance(cityInfo.cityLocation,5)){ - final TileInfo tileInfo = game.civInfo.tileMap.get(vector); + for(final TileInfo tileInfo : game.civInfo.tileMap.getTilesInDistance(cityInfo.cityLocation,5)){ TileGroup group = new TileGroup(tileInfo); group.addListener(new ClickListener(){ @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()) { group.addPopulationIcon(); group.populationImage.addListener(new ClickListener() { @Override public void clicked(InputEvent event, float x, float y) { - if (cityInfo.getFreePopulation() > 0 || tileInfo.IsWorked) - tileInfo.IsWorked = !tileInfo.IsWorked; + if(tileInfo.WorkingCity==null && cityInfo.getFreePopulation() > 0) tileInfo.WorkingCity = cityInfo.Name; + else if(cityInfo.Name.equals(tileInfo.WorkingCity)) tileInfo.WorkingCity = null; updateCityTable(); } }); } - Vector2 positionalVector = HexMath.Hex2WorldCoords(vector.cpy().sub(cityInfo.cityLocation)); + Vector2 positionalVector = HexMath.Hex2WorldCoords(tileInfo.Position.cpy().sub(cityInfo.cityLocation)); int groupSize = 50; group.setPosition(stage.getWidth()/2 + positionalVector.x*0.8f * groupSize, stage.getHeight()/2 + positionalVector.y*0.8f * groupSize); diff --git a/core/src/com/unciv/game/TileGroup.java b/core/src/com/unciv/game/TileGroup.java index 5182f3bc..88c01622 100644 --- a/core/src/com/unciv/game/TileGroup.java +++ b/core/src/com/unciv/game/TileGroup.java @@ -78,7 +78,7 @@ public class TileGroup extends Group { } if(populationImage!=null){ - if(tileInfo.IsWorked) populationImage.setColor(Color.WHITE); + if(tileInfo.WorkingCity!=null) populationImage.setColor(Color.WHITE); else populationImage.setColor(Color.GRAY); } } diff --git a/core/src/com/unciv/game/UnCivGame.java b/core/src/com/unciv/game/UnCivGame.java index 5715a616..bb7a330e 100644 --- a/core/src/com/unciv/game/UnCivGame.java +++ b/core/src/com/unciv/game/UnCivGame.java @@ -25,7 +25,13 @@ public class UnCivGame extends Game { public void create() { SetupGameBasics(); 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(); worldScreen = new WorldScreen(this); diff --git a/core/src/com/unciv/game/WorldScreen.java b/core/src/com/unciv/game/WorldScreen.java index 8d18f5f0..5a92620c 100644 --- a/core/src/com/unciv/game/WorldScreen.java +++ b/core/src/com/unciv/game/WorldScreen.java @@ -348,11 +348,10 @@ public class WorldScreen extends CameraStageBaseScreen { } }); - if(HexMath.GetVectorsInDistance(selectedTile.Position,2).any(new Predicate() { + if(game.civInfo.tileMap.getTilesInDistance(selectedTile.Position,2).any(new Predicate() { @Override - public boolean evaluate(Vector2 arg0) { - return tileGroups.containsKey(arg0.toString()) && - tileGroups.get(arg0.toString()).tileInfo.IsCityCenter(); + public boolean evaluate(TileInfo arg0) { + return arg0.IsCityCenter(); } })){ foundCityButton.setDisabled(true); @@ -414,9 +413,9 @@ public class WorldScreen extends CameraStageBaseScreen { HashSet ViewableVectorStrings = new HashSet(); // tiles adjacent to city tiles - for(CityInfo city : game.civInfo.Cities) - for(Vector2 tileLocation : city.CityTileLocations) - for(Vector2 adjacentLocation : HexMath.GetAdjacentVectors(tileLocation)) + for(TileInfo tileInfo : game.civInfo.tileMap.values()) + if(game.civInfo.civName.equals(tileInfo.Owner)) + for(Vector2 adjacentLocation : HexMath.GetAdjacentVectors(tileInfo.Position)) ViewableVectorStrings.add(adjacentLocation.toString()); // Tiles within 2 tiles of units diff --git a/core/src/com/unciv/game/WorldTileGroup.java b/core/src/com/unciv/game/WorldTileGroup.java index 9e21826b..73edd9ff 100644 --- a/core/src/com/unciv/game/WorldTileGroup.java +++ b/core/src/com/unciv/game/WorldTileGroup.java @@ -27,11 +27,11 @@ public class WorldTileGroup extends TileGroup { void update(WorldScreen worldScreen) { super.update(); - if(tileInfo.IsWorked && populationImage==null) addPopulationIcon(); - if(!tileInfo.IsWorked && populationImage!=null) removePopulationIcon(); + if(tileInfo.WorkingCity != null && populationImage==null) addPopulationIcon(); + 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"); float imageScale = terrainImage.getWidth() * 1.3f / hexagon.getWidth(); hexagon.setScale(imageScale); diff --git a/core/src/com/unciv/game/pickerscreens/ImprovementPickerScreen.java b/core/src/com/unciv/game/pickerscreens/ImprovementPickerScreen.java index 01b46d8a..588a2ec3 100644 --- a/core/src/com/unciv/game/pickerscreens/ImprovementPickerScreen.java +++ b/core/src/com/unciv/game/pickerscreens/ImprovementPickerScreen.java @@ -35,7 +35,7 @@ public class ImprovementPickerScreen extends PickerScreen { rightSideButton.setColor(Color.GRAY); 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); TB.addListener(new ClickListener(){ @Override