Added movement costs to tiles and computed tiles that unit can travel to, and the distance (including roads)

This commit is contained in:
Yair Morgenstern 2017-12-04 16:02:27 +02:00
parent 7a4b0232a7
commit d47d59146f
7 changed files with 63 additions and 19 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 145 B

View file

@ -3,6 +3,7 @@
name:"Grassland",
type:"BaseTerrain",
food:2,
movementCost:1,
rgb:"0,255,0"
},
{
@ -10,17 +11,20 @@
type:"BaseTerrain",
food:1,
production:1,
movementCost:1,
rgb:"240,180,135"
},
{
name:"Tundra",
type:"BaseTerrain",
food:1,
movementCost:1,
rgb:"130,137,121"
},
{
name:"Desert",
type:"BaseTerrain",
movementCost:1,
rgb:"237,201,175"
},
{
@ -35,6 +39,7 @@
name:"Hill",
type:"BaseTerrain",
production:2,
movementCost:2,
rgb:"116,88,62"
},
@ -44,6 +49,7 @@
type:"TerrainFeature",
production:1,
food:1,
movementCost:2,
overrideStats:true,
unbuildable:true,
occursOn:["Tundra","Plains","Grassland"],
@ -53,6 +59,7 @@
name:"Jungle",
type:"TerrainFeature",
food:2,
movementCost:2,
overrideStats:true,
unbuildable:true,
occursOn:["Plains","Grassland"],
@ -62,6 +69,7 @@
name:"Marsh",
type:"TerrainFeature",
food:-1,
movementCost:2,
unbuildable:true,
occursOn:["Grassland"],
removalTech: "Masonry"
@ -70,6 +78,7 @@
name:"Oasis",
type:"TerrainFeature",
food:3,
movementCost:1,
unbuildable:true,
occursOn:["Desert"]
},
@ -77,6 +86,7 @@
name:"Flood plains",
type:"TerrainFeature",
food:2,
movementCost:1,
occursOn:["Desert"]
}
]

View file

@ -9,6 +9,9 @@ import com.unciv.models.gamebasics.TileImprovement;
import com.unciv.models.gamebasics.TileResource;
import com.unciv.models.stats.FullStats;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
public class TileInfo
{
public Unit unit;
@ -138,7 +141,7 @@ public class TileInfo
if(roadStatus!= RoadStatus.None) SB.append(",\r\n" + roadStatus);
if (improvement != null) SB.append(",\r\n" + improvement);
if (improvementInProgress != null) SB.append(",\r\n" + improvementInProgress +" in "+this.turnsToImprovement +" turns");
if(unit !=null) SB.append(",\r\n" + unit.Name+ "("+ unit.CurrentMovement+"/"+ unit.MaxMovement+")");
if(unit !=null) SB.append(",\r\n" + unit.Name+ "("+ new DecimalFormat("0.#").format(unit.CurrentMovement)+"/"+ unit.MaxMovement+")");
return SB.toString();
}

View file

@ -11,8 +11,6 @@ import com.unciv.models.gamebasics.Terrain;
import com.unciv.models.gamebasics.TerrainType;
import com.unciv.models.gamebasics.TileResource;
import java.util.function.Function;
public class TileMap{
private LinqHashMap<String, TileInfo> tiles = new LinqHashMap<String, TileInfo>();
@ -86,6 +84,33 @@ public class TileMap{
return tiles;
}
public LinqHashMap<TileInfo,Float> getDistanceToTiles(Vector2 origin, float maximumMovement){
LinqHashMap<TileInfo,Float> distanceToTiles = new LinqHashMap<TileInfo, Float>();
distanceToTiles.put(get(origin), 0f);
LinqCollection<TileInfo> tilesToCheck = new LinqCollection<TileInfo>();
tilesToCheck.add(get(origin));
while(!tilesToCheck.isEmpty()){
LinqCollection<TileInfo> updatedTiles = new LinqCollection<TileInfo>();
for(TileInfo tileToCheck : tilesToCheck)
for (TileInfo maybeUpdatedTile : getTilesInDistance(tileToCheck.position,1)) {
float distanceBetweenTiles = maybeUpdatedTile.getLastTerrain().movementCost;
if(tileToCheck.roadStatus!=RoadStatus.None && maybeUpdatedTile.roadStatus!=RoadStatus.None) distanceBetweenTiles = 1/3f;
if(tileToCheck.roadStatus==RoadStatus.Railroad && maybeUpdatedTile.roadStatus==RoadStatus.Railroad) distanceBetweenTiles = 1/10f;
float totalDistanceToTile = distanceToTiles.get(tileToCheck)+ distanceBetweenTiles;
if (!distanceToTiles.containsKey(maybeUpdatedTile) || distanceToTiles.get(maybeUpdatedTile) > totalDistanceToTile) {
if(totalDistanceToTile<maximumMovement) updatedTiles.add(maybeUpdatedTile);
else totalDistanceToTile = maximumMovement;
distanceToTiles.put(maybeUpdatedTile,totalDistanceToTile);
}
}
tilesToCheck = updatedTiles;
}
return distanceToTiles;
}
public LinqCollection<TileInfo> values(){return tiles.linqValues();}
TileResource GetRandomResource(LinqCollection<TileResource> resources, final ResourceType resourceType) {

View file

@ -3,7 +3,7 @@ package com.unciv.civinfo;
public class Unit{
public String Name;
public int MaxMovement;
public int CurrentMovement;
public float CurrentMovement;
public Unit(){} // for json parsing, we need to have a default constructor

View file

@ -212,19 +212,21 @@ public class WorldScreen extends CameraStageBaseScreen {
group.addListener(new ClickListener() {
@Override
public void clicked(InputEvent event, float x, float y) {
selectedTile = tileInfo;
if(unitTile != null && group.tileInfo.unit == null ) {
int distance = HexMath.GetDistance(unitTile.position, group.tileInfo.position);
if (distance <= unitTile.unit.CurrentMovement) {
unitTile.unit.CurrentMovement -= distance;
group.tileInfo.unit = unitTile.unit;
unitTile.unit = null;
unitTile = null;
selectedTile = group.tileInfo;
}
selectedTile = tileInfo;
if(unitTile != null && group.tileInfo.unit == null ) {
LinqHashMap<TileInfo, Float> distanceToTiles = game.civInfo.tileMap.getDistanceToTiles(unitTile.position,unitTile.unit.CurrentMovement);
if(distanceToTiles.containsKey(selectedTile)) {
unitTile.unit.CurrentMovement -= distanceToTiles.get(selectedTile);
unitTile.unit.CurrentMovement = round(unitTile.unit.CurrentMovement,3);
if(unitTile.unit.CurrentMovement < 0.1) unitTile.unit.CurrentMovement=0; // silly floats which are "almost zero"
group.tileInfo.unit = unitTile.unit;
unitTile.unit = null;
unitTile = null;
selectedTile = group.tileInfo;
}
}
update();
update();
}
});
@ -327,10 +329,9 @@ 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(Vector2 vector : HexMath.GetVectorsInDistance(unitTile.position, unitTile.unit.CurrentMovement)){
if(tileGroups.containsKey(vector.toString()))
tileGroups.get(vector.toString()).setColor(Color.WHITE);
}
for(TileInfo tile : game.civInfo.tileMap.getDistanceToTiles(unitTile.position,unitTile.unit.CurrentMovement).keySet()){
tileGroups.get(tile.position.toString()).setColor(Color.WHITE);
}
update();
}
@ -456,6 +457,10 @@ public class WorldScreen extends CameraStageBaseScreen {
scrollPane.updateVisualScroll();
}
float round(float value, int scale){
return (float) (Math.round(value * Math.pow(10, scale)) / Math.pow(10, scale));
}
}

View file

@ -30,6 +30,7 @@ public class Terrain extends NamedStats implements ICivilopedia {
*For terrain features - which technology alllows removal of this feature
*/
public String removalTech;
public int movementCost=1;
public String rgb;