mirror of
https://github.com/yairm210/Unciv.git
synced 2025-07-09 23:39:40 +07:00
Added movement costs to tiles and computed tiles that unit can travel to, and the distance (including roads)
This commit is contained in:
BIN
android/assets/TerrainIcons/road.png
Normal file
BIN
android/assets/TerrainIcons/road.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 145 B |
@ -3,6 +3,7 @@
|
|||||||
name:"Grassland",
|
name:"Grassland",
|
||||||
type:"BaseTerrain",
|
type:"BaseTerrain",
|
||||||
food:2,
|
food:2,
|
||||||
|
movementCost:1,
|
||||||
rgb:"0,255,0"
|
rgb:"0,255,0"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -10,17 +11,20 @@
|
|||||||
type:"BaseTerrain",
|
type:"BaseTerrain",
|
||||||
food:1,
|
food:1,
|
||||||
production:1,
|
production:1,
|
||||||
|
movementCost:1,
|
||||||
rgb:"240,180,135"
|
rgb:"240,180,135"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name:"Tundra",
|
name:"Tundra",
|
||||||
type:"BaseTerrain",
|
type:"BaseTerrain",
|
||||||
food:1,
|
food:1,
|
||||||
|
movementCost:1,
|
||||||
rgb:"130,137,121"
|
rgb:"130,137,121"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name:"Desert",
|
name:"Desert",
|
||||||
type:"BaseTerrain",
|
type:"BaseTerrain",
|
||||||
|
movementCost:1,
|
||||||
rgb:"237,201,175"
|
rgb:"237,201,175"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -35,6 +39,7 @@
|
|||||||
name:"Hill",
|
name:"Hill",
|
||||||
type:"BaseTerrain",
|
type:"BaseTerrain",
|
||||||
production:2,
|
production:2,
|
||||||
|
movementCost:2,
|
||||||
rgb:"116,88,62"
|
rgb:"116,88,62"
|
||||||
},
|
},
|
||||||
|
|
||||||
@ -44,6 +49,7 @@
|
|||||||
type:"TerrainFeature",
|
type:"TerrainFeature",
|
||||||
production:1,
|
production:1,
|
||||||
food:1,
|
food:1,
|
||||||
|
movementCost:2,
|
||||||
overrideStats:true,
|
overrideStats:true,
|
||||||
unbuildable:true,
|
unbuildable:true,
|
||||||
occursOn:["Tundra","Plains","Grassland"],
|
occursOn:["Tundra","Plains","Grassland"],
|
||||||
@ -53,6 +59,7 @@
|
|||||||
name:"Jungle",
|
name:"Jungle",
|
||||||
type:"TerrainFeature",
|
type:"TerrainFeature",
|
||||||
food:2,
|
food:2,
|
||||||
|
movementCost:2,
|
||||||
overrideStats:true,
|
overrideStats:true,
|
||||||
unbuildable:true,
|
unbuildable:true,
|
||||||
occursOn:["Plains","Grassland"],
|
occursOn:["Plains","Grassland"],
|
||||||
@ -62,6 +69,7 @@
|
|||||||
name:"Marsh",
|
name:"Marsh",
|
||||||
type:"TerrainFeature",
|
type:"TerrainFeature",
|
||||||
food:-1,
|
food:-1,
|
||||||
|
movementCost:2,
|
||||||
unbuildable:true,
|
unbuildable:true,
|
||||||
occursOn:["Grassland"],
|
occursOn:["Grassland"],
|
||||||
removalTech: "Masonry"
|
removalTech: "Masonry"
|
||||||
@ -70,6 +78,7 @@
|
|||||||
name:"Oasis",
|
name:"Oasis",
|
||||||
type:"TerrainFeature",
|
type:"TerrainFeature",
|
||||||
food:3,
|
food:3,
|
||||||
|
movementCost:1,
|
||||||
unbuildable:true,
|
unbuildable:true,
|
||||||
occursOn:["Desert"]
|
occursOn:["Desert"]
|
||||||
},
|
},
|
||||||
@ -77,6 +86,7 @@
|
|||||||
name:"Flood plains",
|
name:"Flood plains",
|
||||||
type:"TerrainFeature",
|
type:"TerrainFeature",
|
||||||
food:2,
|
food:2,
|
||||||
|
movementCost:1,
|
||||||
occursOn:["Desert"]
|
occursOn:["Desert"]
|
||||||
}
|
}
|
||||||
]
|
]
|
@ -9,6 +9,9 @@ import com.unciv.models.gamebasics.TileImprovement;
|
|||||||
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.text.DecimalFormat;
|
||||||
|
import java.text.DecimalFormatSymbols;
|
||||||
|
|
||||||
public class TileInfo
|
public class TileInfo
|
||||||
{
|
{
|
||||||
public Unit unit;
|
public Unit unit;
|
||||||
@ -138,7 +141,7 @@ public class TileInfo
|
|||||||
if(roadStatus!= RoadStatus.None) SB.append(",\r\n" + roadStatus);
|
if(roadStatus!= RoadStatus.None) SB.append(",\r\n" + roadStatus);
|
||||||
if (improvement != null) SB.append(",\r\n" + improvement);
|
if (improvement != null) SB.append(",\r\n" + improvement);
|
||||||
if (improvementInProgress != null) SB.append(",\r\n" + improvementInProgress +" in "+this.turnsToImprovement +" turns");
|
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();
|
return SB.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -11,8 +11,6 @@ import com.unciv.models.gamebasics.Terrain;
|
|||||||
import com.unciv.models.gamebasics.TerrainType;
|
import com.unciv.models.gamebasics.TerrainType;
|
||||||
import com.unciv.models.gamebasics.TileResource;
|
import com.unciv.models.gamebasics.TileResource;
|
||||||
|
|
||||||
import java.util.function.Function;
|
|
||||||
|
|
||||||
public class TileMap{
|
public class TileMap{
|
||||||
|
|
||||||
private LinqHashMap<String, TileInfo> tiles = new LinqHashMap<String, TileInfo>();
|
private LinqHashMap<String, TileInfo> tiles = new LinqHashMap<String, TileInfo>();
|
||||||
@ -86,6 +84,33 @@ public class TileMap{
|
|||||||
return tiles;
|
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();}
|
public LinqCollection<TileInfo> values(){return tiles.linqValues();}
|
||||||
|
|
||||||
TileResource GetRandomResource(LinqCollection<TileResource> resources, final ResourceType resourceType) {
|
TileResource GetRandomResource(LinqCollection<TileResource> resources, final ResourceType resourceType) {
|
||||||
|
@ -3,7 +3,7 @@ package com.unciv.civinfo;
|
|||||||
public class Unit{
|
public class Unit{
|
||||||
public String Name;
|
public String Name;
|
||||||
public int MaxMovement;
|
public int MaxMovement;
|
||||||
public int CurrentMovement;
|
public float CurrentMovement;
|
||||||
|
|
||||||
public Unit(){} // for json parsing, we need to have a default constructor
|
public Unit(){} // for json parsing, we need to have a default constructor
|
||||||
|
|
||||||
|
@ -212,19 +212,21 @@ public class WorldScreen extends CameraStageBaseScreen {
|
|||||||
group.addListener(new ClickListener() {
|
group.addListener(new ClickListener() {
|
||||||
@Override
|
@Override
|
||||||
public void clicked(InputEvent event, float x, float y) {
|
public void clicked(InputEvent event, float x, float y) {
|
||||||
selectedTile = tileInfo;
|
selectedTile = tileInfo;
|
||||||
if(unitTile != null && group.tileInfo.unit == null ) {
|
if(unitTile != null && group.tileInfo.unit == null ) {
|
||||||
int distance = HexMath.GetDistance(unitTile.position, group.tileInfo.position);
|
LinqHashMap<TileInfo, Float> distanceToTiles = game.civInfo.tileMap.getDistanceToTiles(unitTile.position,unitTile.unit.CurrentMovement);
|
||||||
if (distance <= unitTile.unit.CurrentMovement) {
|
if(distanceToTiles.containsKey(selectedTile)) {
|
||||||
unitTile.unit.CurrentMovement -= distance;
|
unitTile.unit.CurrentMovement -= distanceToTiles.get(selectedTile);
|
||||||
group.tileInfo.unit = unitTile.unit;
|
unitTile.unit.CurrentMovement = round(unitTile.unit.CurrentMovement,3);
|
||||||
unitTile.unit = null;
|
if(unitTile.unit.CurrentMovement < 0.1) unitTile.unit.CurrentMovement=0; // silly floats which are "almost zero"
|
||||||
unitTile = null;
|
group.tileInfo.unit = unitTile.unit;
|
||||||
selectedTile = group.tileInfo;
|
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
|
// Set all tiles transparent except those in unit range
|
||||||
for(TileGroup TG : tileGroups.linqValues()) TG.setColor(0,0,0,0.3f);
|
for(TileGroup TG : tileGroups.linqValues()) TG.setColor(0,0,0,0.3f);
|
||||||
for(Vector2 vector : HexMath.GetVectorsInDistance(unitTile.position, unitTile.unit.CurrentMovement)){
|
for(TileInfo tile : game.civInfo.tileMap.getDistanceToTiles(unitTile.position,unitTile.unit.CurrentMovement).keySet()){
|
||||||
if(tileGroups.containsKey(vector.toString()))
|
tileGroups.get(tile.position.toString()).setColor(Color.WHITE);
|
||||||
tileGroups.get(vector.toString()).setColor(Color.WHITE);
|
}
|
||||||
}
|
|
||||||
|
|
||||||
update();
|
update();
|
||||||
}
|
}
|
||||||
@ -456,6 +457,10 @@ public class WorldScreen extends CameraStageBaseScreen {
|
|||||||
scrollPane.updateVisualScroll();
|
scrollPane.updateVisualScroll();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
float round(float value, int scale){
|
||||||
|
return (float) (Math.round(value * Math.pow(10, scale)) / Math.pow(10, scale));
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -30,6 +30,7 @@ public class Terrain extends NamedStats implements ICivilopedia {
|
|||||||
*For terrain features - which technology alllows removal of this feature
|
*For terrain features - which technology alllows removal of this feature
|
||||||
*/
|
*/
|
||||||
public String removalTech;
|
public String removalTech;
|
||||||
|
public int movementCost=1;
|
||||||
|
|
||||||
public String rgb;
|
public String rgb;
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user