Added unit movement to further tiles

This commit is contained in:
Yair Morgenstern 2017-12-30 22:58:36 +02:00
parent ab2b33318b
commit befb0561b5
7 changed files with 93 additions and 17 deletions

View file

@ -21,7 +21,7 @@ android {
applicationId "com.unciv.game"
minSdkVersion 9
targetSdkVersion 25
versionCode 13
versionCode 14
versionName "0.9"
}
buildTypes {

View file

@ -60,7 +60,7 @@ public class CityInfo {
this.cityLocation = cityLocation;
civInfo.cities.add(this);
cityConstructions = new CityConstructions(this);
if(civInfo.cities.size()==0) {
if(civInfo.cities.size()==1) {
cityConstructions.builtBuildings.add("Palace");
cityConstructions.currentConstruction = "Worker"; // Default for first city only!
}
@ -174,7 +174,8 @@ public class CityInfo {
stats.culture+=population/2;
FullStats statPercentBonuses = cityConstructions.getStatPercentBonuses();
if(isCapital() || isConnectedToCapital(RoadStatus.Railroad)) statPercentBonuses.production += 25;
if( civInfo.tech.isResearched ("Combustion") &&
(isCapital() || isConnectedToCapital(RoadStatus.Railroad))) statPercentBonuses.production += 25;
if(civInfo.isGoldenAge()) statPercentBonuses.production+=20;
IConstruction currentConstruction = cityConstructions.getCurrentConstruction();
if(currentConstruction instanceof Building && ((Building)currentConstruction).isWonder){

View file

@ -90,7 +90,15 @@ public class CivilizationInfo {
// We need to update the stats after ALL the cities are done updating because
// maybe one of them has a wonder that affects the stats of all the rest of the cities
for(TileInfo tile : tileMap.values()) tile.nextTurn();
// Here we need to filter out the tiles that don''t have units, because what happens if a unit in in tile 1,
// gets activated, and then moves to tile 2, which is activated later? Problem!
for(TileInfo tile : tileMap.values().where(new Predicate<TileInfo>() {
@Override
public boolean evaluate(TileInfo arg0) {
return arg0.unit!=null;
}
})) tile.nextTurn();
if(isGoldenAge()) turnsLeftForCurrentGoldenAge--;

View file

@ -1,5 +1,6 @@
package com.unciv.civinfo;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.utils.Predicate;
import com.unciv.models.LinqCollection;
import com.unciv.models.LinqHashMap;
@ -12,10 +13,20 @@ public class MapUnit{
public String action; // work, automation, fortifying, I dunno what.
public void doAction(TileInfo tile){
if(currentMovement==0) return; // We've already done stuff this turn, and can't do any more stuff
if(action!=null && action.startsWith("moveTo")){
String[] destination = action.replace("moveTo ","").split(",");
Vector2 destinationVector = new Vector2(Integer.parseInt(destination[0]), Integer.parseInt(destination[1]));
TileInfo gotTo = headTowards(tile.position,destinationVector);
if(gotTo.position.equals(destinationVector)) action=null;
if(currentMovement!=0) doAction(gotTo);
return;
}
if(name.equals("Worker") && tile.improvementInProgress!=null) workOnImprovement(tile);
if ("automation".equals(action)) doAutomatedAction(tile);
}
private void workOnImprovement(TileInfo tile){
tile.turnsToImprovement -= 1;
if(tile.turnsToImprovement == 0)
@ -56,7 +67,7 @@ public class MapUnit{
// We'll search for a tile that needs our help in the reachable area
LinqHashMap<TileInfo, Float> distanceToTiles =
CivilizationInfo.current().tileMap.getUnitDistanceToTiles(tile.position,currentMovement);
CivilizationInfo.current().tileMap.getDistanceToTilesWithinTurn(tile.position,currentMovement);
TileInfo tileWithinDistance = new LinqCollection<TileInfo>(distanceToTiles.keySet()).first(new Predicate<TileInfo>() {
@Override
public boolean evaluate(TileInfo tile) {
@ -72,7 +83,6 @@ public class MapUnit{
// If not, then we don't know what to do. Oh well.
}
private String chooseImprovement(final TileInfo tile){
if(tile.improvementInProgress!=null) return tile.improvementInProgress;
if("Forest".equals(tile.terrainFeature)) return "Lumber mill";
@ -86,4 +96,14 @@ public class MapUnit{
if(tile.baseTerrain.equals("Tundra")) return "Trading post";
return null;
}
public TileInfo headTowards(Vector2 origin, Vector2 destination){
TileMap tileMap = CivilizationInfo.current().tileMap;
LinqCollection<TileInfo> path = tileMap.getShortestPath(origin,destination,currentMovement,maxMovement);
TileInfo destinationThisTurn = path.get(0);
float distanceToTile = tileMap.getDistanceToTilesWithinTurn(origin,currentMovement).get(destinationThisTurn);
tileMap.get(origin).moveUnitToTile(destinationThisTurn, distanceToTile);
return destinationThisTurn;
}
}

View file

@ -84,7 +84,7 @@ public class TileMap{
return tiles;
}
public LinqHashMap<TileInfo,Float> getUnitDistanceToTiles(Vector2 origin, float maximumMovement){
public LinqHashMap<TileInfo,Float> getDistanceToTilesWithinTurn(Vector2 origin, float currentUnitMovement){
LinqHashMap<TileInfo,Float> distanceToTiles = new LinqHashMap<TileInfo, Float>();
distanceToTiles.put(get(origin), 0f);
LinqCollection<TileInfo> tilesToCheck = new LinqCollection<TileInfo>();
@ -102,8 +102,8 @@ public class TileMap{
float totalDistanceToTile = distanceToTiles.get(tileToCheck)+ distanceBetweenTiles;
if (!distanceToTiles.containsKey(maybeUpdatedTile) || distanceToTiles.get(maybeUpdatedTile) > totalDistanceToTile) {
if(totalDistanceToTile<maximumMovement) updatedTiles.add(maybeUpdatedTile);
else totalDistanceToTile = maximumMovement;
if(totalDistanceToTile<currentUnitMovement) updatedTiles.add(maybeUpdatedTile);
else totalDistanceToTile = currentUnitMovement;
distanceToTiles.put(maybeUpdatedTile,totalDistanceToTile);
}
@ -114,6 +114,44 @@ public class TileMap{
return distanceToTiles;
}
public class BfsInfo{
final TileInfo parent;
final int totalDistance;
public BfsInfo(TileInfo parent, int totalDistance) {
this.parent = parent;
this.totalDistance = totalDistance;
}
}
public LinqCollection<TileInfo> getShortestPath(Vector2 origin, Vector2 destination, float currentMovement, int maxMovement){
LinqCollection<TileInfo> toCheck = new LinqCollection<TileInfo>(get(origin));
LinqHashMap<TileInfo,TileInfo> parents = new LinqHashMap<TileInfo, TileInfo>();
parents.put(get(origin),null);
for (int distance = 1; ; distance++) {
LinqCollection<TileInfo> newToCheck = new LinqCollection<TileInfo>();
for (TileInfo ti : toCheck){
for (TileInfo otherTile : getDistanceToTilesWithinTurn(ti.position, distance == 1 ? currentMovement : maxMovement).keySet()){
if(parents.containsKey(otherTile) || otherTile.unit!=null) continue; // We cannot be faster than anything existing...
parents.put(otherTile,ti);
if(otherTile.position.equals(destination)){
LinqCollection<TileInfo> path = new LinqCollection<TileInfo>();
TileInfo current = otherTile;
while(parents.get(current)!=null){
path.add(current);
current = parents.get(current);
}
return path.reverse();
}
newToCheck.add(otherTile);
}
}
toCheck = newToCheck;
}
}
public LinqCollection<TileInfo> values(){return tiles.linqValues();}
TileResource GetRandomResource(LinqCollection<TileResource> resources, final ResourceType resourceType) {

View file

@ -314,12 +314,17 @@ public class WorldScreen extends CameraStageBaseScreen {
public void clicked(InputEvent event, float x, float y) {
selectedTile = tileInfo;
if(unitTile != null && group.tileInfo.unit == null ) {
LinqHashMap<TileInfo, Float> distanceToTiles = game.civInfo.tileMap.getUnitDistanceToTiles(unitTile.position,unitTile.unit.currentMovement);
LinqHashMap<TileInfo, Float> distanceToTiles = game.civInfo.tileMap.getDistanceToTilesWithinTurn(unitTile.position,unitTile.unit.currentMovement);
if(distanceToTiles.containsKey(selectedTile)) {
unitTile.moveUnitToTile(group.tileInfo,distanceToTiles.get(selectedTile));
unitTile = null;
selectedTile = group.tileInfo;
}
else {
unitTile.unit.action="moveTo "+ ((int) selectedTile.position.x)+","+ ((int) selectedTile.position.y);
unitTile.unit.doAction(unitTile);
}
unitTile = null;
selectedTile = group.tileInfo;
}
update();
@ -425,7 +430,7 @@ public class WorldScreen extends CameraStageBaseScreen {
// Set all tiles transparent except those in unit range
for(TileGroup TG : tileGroups.linqValues()) TG.setColor(0,0,0,0.3f);
for(TileInfo tile : game.civInfo.tileMap.getUnitDistanceToTiles(unitTile.position,unitTile.unit.currentMovement).keySet()){
for(TileInfo tile : game.civInfo.tileMap.getDistanceToTilesWithinTurn(unitTile.position,unitTile.unit.currentMovement).keySet()){
tileGroups.get(tile.position.toString()).setColor(Color.WHITE);
}

View file

@ -5,7 +5,9 @@ import com.badlogic.gdx.utils.Predicate;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Stack;
/**
* Created by LENOVO on 10/20/2017.
@ -74,7 +76,9 @@ public class LinqCollection <T> extends ArrayList<T> {
return newCollection;
}
}
public LinqCollection<T> reverse(){
LinqCollection<T> newCol = new LinqCollection<T>(this);
Collections.reverse(newCol);
return newCol;
}
}