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.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<TileInfo>() {
@Override
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.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<Vector2> CityTileLocations = new LinqCollection<Vector2>();
public LinqCollection<TileInfo> getCityTiles(){
return CityTileLocations.select(new com.unciv.models.LinqCollection.Func<Vector2, TileInfo>() {
public LinqCollection<TileInfo> getTilesInRange(){
return getTileMap().getTilesInDistance(cityLocation,3).where(new Predicate<TileInfo>() {
@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<String> getLuxuryResources() {
ArrayList<String> LuxuryResources = new ArrayList<String>();
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<TileInfo>() {
return getTilesInRange().count(new Predicate<TileInfo>() {
@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<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>();
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<TileInfo> tiles = getTileMap().getTilesInDistance(cityLocation,i);
tiles = tiles.where(new Predicate<TileInfo>() {
@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){

View file

@ -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;

View file

@ -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<CityInfo>() {
public CityInfo GetCity(){
if(WorkingCity == null) return null;
return CivilizationInfo.current().Cities.first(new Predicate<CityInfo>() {
@Override
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;
// 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<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 TileResource GetRandomResource(LinqCollection<TileResource> resources, final ResourceType resourceType) {

View file

@ -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);

View file

@ -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);
}
}

View file

@ -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);

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
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<String> ViewableVectorStrings = new HashSet<String>();
// 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

View file

@ -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);

View file

@ -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