mirror of
https://github.com/yairm210/Unciv.git
synced 2025-07-21 05:09:25 +07:00
Optimized hex math - GetVectorsInDistance is now much more efficient!
This commit is contained in:
@ -145,7 +145,7 @@ public class CityBuildings
|
|||||||
stats.science += getCity().getBuildingUniques().count(new Predicate<String>() {
|
stats.science += getCity().getBuildingUniques().count(new Predicate<String>() {
|
||||||
@Override
|
@Override
|
||||||
public boolean evaluate(String arg0) {
|
public boolean evaluate(String arg0) {
|
||||||
return arg0.equals("SciencePer2Pop");
|
return "SciencePer2Pop".equals(arg0);
|
||||||
}
|
}
|
||||||
}) * getCity().population/2; // Library and public school unique (not actualy unique, though...hmm)
|
}) * getCity().population/2; // Library and public school unique (not actualy unique, though...hmm)
|
||||||
return stats;
|
return stats;
|
||||||
|
@ -281,6 +281,11 @@ public class CityInfo {
|
|||||||
public String GetBy(Building arg0) {
|
public String GetBy(Building arg0) {
|
||||||
return arg0.unique;
|
return arg0.unique;
|
||||||
}
|
}
|
||||||
|
}).where(new Predicate<String>() {
|
||||||
|
@Override
|
||||||
|
public boolean evaluate(String arg0) {
|
||||||
|
return arg0!=null;
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -5,11 +5,12 @@ import com.unciv.models.gamebasics.Technology;
|
|||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
import java.util.HashSet;
|
||||||
|
|
||||||
public class CivilizationTech{
|
public class CivilizationTech{
|
||||||
public int freeTechs = 0;
|
public int freeTechs = 0;
|
||||||
|
|
||||||
public ArrayList<String> techsResearched = new ArrayList<String>();
|
public HashSet<String> techsResearched = new HashSet<String>();
|
||||||
/* When moving towards a certain tech, the user doesn't have to manually pick every one. */
|
/* When moving towards a certain tech, the user doesn't have to manually pick every one. */
|
||||||
public ArrayList<String> techsToResearch = new ArrayList<String>();
|
public ArrayList<String> techsToResearch = new ArrayList<String>();
|
||||||
public HashMap<String, Integer> techsInProgress = new HashMap<String, Integer>();
|
public HashMap<String, Integer> techsInProgress = new HashMap<String, Integer>();
|
||||||
|
@ -46,22 +46,31 @@ public class HexMath
|
|||||||
return vectors;
|
return vectors;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static LinqCollection<Vector2> GetVectorsInDistance(Vector2 origin, int distance){
|
public static ArrayList<Vector2> GetVectorsAtDistance(Vector2 origin, int distance){
|
||||||
|
ArrayList<Vector2> vectors = new ArrayList<Vector2>();
|
||||||
|
Vector2 Current = origin.cpy().sub(distance,distance); // start at 6 o clock
|
||||||
|
for (int i = 0; i < distance; i++) { // From 6 to 8
|
||||||
|
vectors.add(Current.cpy());
|
||||||
|
vectors.add(origin.cpy().scl(2).sub(Current)); // Get vector on other side of cloick
|
||||||
|
Current.add(1,0);
|
||||||
|
}
|
||||||
|
for (int i = 0; i < distance; i++) { // 8 to 10
|
||||||
|
vectors.add(Current.cpy());
|
||||||
|
vectors.add(origin.cpy().scl(2).sub(Current)); // Get vector on other side of cloick
|
||||||
|
Current.add(1,1);
|
||||||
|
}
|
||||||
|
for (int i = 0; i < distance; i++) { // 10 to 12
|
||||||
|
vectors.add(Current.cpy());
|
||||||
|
vectors.add(origin.cpy().scl(2).sub(Current)); // Get vector on other side of cloick
|
||||||
|
Current.add(1,1);
|
||||||
|
};
|
||||||
|
return vectors;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static LinqCollection<Vector2> GetVectorsInDistance(Vector2 origin, int distance) {
|
||||||
HashSet<Vector2> hexesToReturn = new HashSet<Vector2>();
|
HashSet<Vector2> hexesToReturn = new HashSet<Vector2>();
|
||||||
HashSet<Vector2> oldHexes;
|
for (int i = 0; i < distance + 1; i++) {
|
||||||
HashSet<Vector2> newHexes = new HashSet<Vector2>();
|
hexesToReturn.addAll(GetVectorsAtDistance(origin, i));
|
||||||
hexesToReturn.add(origin);
|
|
||||||
newHexes.add(origin);
|
|
||||||
for (int i = 0; i < distance; i++) {
|
|
||||||
oldHexes = newHexes;
|
|
||||||
newHexes = new HashSet<Vector2>();
|
|
||||||
for (Vector2 vector : oldHexes) {
|
|
||||||
for (Vector2 adjacentVector : GetAdjacentVectors(vector)){
|
|
||||||
if(hexesToReturn.contains(adjacentVector)) continue;
|
|
||||||
hexesToReturn.add(adjacentVector);
|
|
||||||
newHexes.add(adjacentVector);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return new LinqCollection<Vector2>(hexesToReturn);
|
return new LinqCollection<Vector2>(hexesToReturn);
|
||||||
}
|
}
|
||||||
@ -69,19 +78,9 @@ public class HexMath
|
|||||||
public static int GetDistance(Vector2 origin, Vector2 destination){ // Yes, this is a dumb implementation. But I can't be arsed to think of a better one right now, other stuff to do.
|
public static int GetDistance(Vector2 origin, Vector2 destination){ // Yes, this is a dumb implementation. But I can't be arsed to think of a better one right now, other stuff to do.
|
||||||
int distance = 0;
|
int distance = 0;
|
||||||
while(true){
|
while(true){
|
||||||
if(GetVectorsInDistance(origin,distance).contains(destination)) return distance;
|
if(GetVectorsAtDistance(origin,distance).contains(destination)) return distance;
|
||||||
distance++;
|
distance++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// public static boolean IsWithinDistance(Vector2 a, Vector2 b, int distance){
|
|
||||||
// return GetVectorsInDistance(a,distance).contains(b);
|
|
||||||
// Vector2 distanceVector = a.sub(b);
|
|
||||||
// if(distanceVector.x<0) distanceVector = new Vector2(-distanceVector.x,-distanceVector.y);
|
|
||||||
//
|
|
||||||
// int distance = (int) Math.abs(distanceVector.x);
|
|
||||||
// distanceVector = distanceVector.sub(distanceVector.x,distanceVector.x); // Zero X f distance, then we'll calculate Y
|
|
||||||
// distance += Math.abs(distanceVector.y);
|
|
||||||
// return distance;
|
|
||||||
// }
|
|
||||||
}
|
}
|
||||||
|
@ -6,16 +6,15 @@ import com.badlogic.gdx.scenes.scene2d.Group;
|
|||||||
import com.badlogic.gdx.scenes.scene2d.ui.Container;
|
import com.badlogic.gdx.scenes.scene2d.ui.Container;
|
||||||
import com.badlogic.gdx.scenes.scene2d.ui.Image;
|
import com.badlogic.gdx.scenes.scene2d.ui.Image;
|
||||||
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
|
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
|
||||||
import com.badlogic.gdx.scenes.scene2d.utils.Drawable;
|
|
||||||
import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable;
|
import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable;
|
||||||
import com.unciv.civinfo.CivilizationInfo;
|
import com.unciv.civinfo.CivilizationInfo;
|
||||||
import com.unciv.civinfo.RoadStatus;
|
import com.unciv.civinfo.RoadStatus;
|
||||||
import com.unciv.civinfo.TileInfo;
|
import com.unciv.civinfo.TileInfo;
|
||||||
import com.unciv.models.LinqCollection;
|
|
||||||
import com.unciv.models.LinqHashMap;
|
import com.unciv.models.LinqHashMap;
|
||||||
|
|
||||||
public class TileGroup extends Group {
|
public class TileGroup extends Group {
|
||||||
Image terrainImage;
|
Image terrainImage;
|
||||||
|
String terrainType;
|
||||||
Image resourceImage;
|
Image resourceImage;
|
||||||
Image unitImage;
|
Image unitImage;
|
||||||
Image improvementImage;
|
Image improvementImage;
|
||||||
@ -29,7 +28,8 @@ public class TileGroup extends Group {
|
|||||||
TileGroup(TileInfo tileInfo){
|
TileGroup(TileInfo tileInfo){
|
||||||
this.tileInfo = tileInfo;
|
this.tileInfo = tileInfo;
|
||||||
|
|
||||||
String terrainFileName = "TerrainIcons/" + tileInfo.getLastTerrain().name.replace(' ','_') + "_(Civ5).png";
|
terrainType = tileInfo.getLastTerrain().name;
|
||||||
|
String terrainFileName = "TerrainIcons/" + terrainType.replace(' ','_') + "_(Civ5).png";
|
||||||
terrainImage = ImageGetter.getImageByFilename(terrainFileName);
|
terrainImage = ImageGetter.getImageByFilename(terrainFileName);
|
||||||
terrainImage.setSize(50,50);
|
terrainImage.setSize(50,50);
|
||||||
addActor(terrainImage);
|
addActor(terrainImage);
|
||||||
@ -50,8 +50,12 @@ public class TileGroup extends Group {
|
|||||||
|
|
||||||
void update() {
|
void update() {
|
||||||
|
|
||||||
String terrainFileName ="TerrainIcons/" + tileInfo.getLastTerrain().name.replace(' ','_') + "_(Civ5).png";
|
|
||||||
|
if(!terrainType.equals(tileInfo.getLastTerrain().name)) {
|
||||||
|
terrainType = tileInfo.getLastTerrain().name;
|
||||||
|
String terrainFileName = "TerrainIcons/" + terrainType.replace(' ', '_') + "_(Civ5).png";
|
||||||
terrainImage.setDrawable(new TextureRegionDrawable(ImageGetter.textureRegionByFileName.get(terrainFileName))); // In case we e.g. removed a jungle
|
terrainImage.setDrawable(new TextureRegionDrawable(ImageGetter.textureRegionByFileName.get(terrainFileName))); // In case we e.g. removed a jungle
|
||||||
|
}
|
||||||
|
|
||||||
if (tileInfo.hasViewableResource() && resourceImage == null) { // Need to add the resource image!
|
if (tileInfo.hasViewableResource() && resourceImage == null) { // Need to add the resource image!
|
||||||
String fileName = "ResourceIcons/" + tileInfo.resource + "_(Civ5).png";
|
String fileName = "ResourceIcons/" + tileInfo.resource + "_(Civ5).png";
|
||||||
|
@ -94,8 +94,10 @@ public class WorldScreen extends CameraStageBaseScreen {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void update(){
|
public void update(){
|
||||||
if(game.civInfo.tech.freeTechs!=0)
|
if(game.civInfo.tech.freeTechs!=0) {
|
||||||
game.setScreen(new TechPickerScreen(game,true));
|
game.setScreen(new TechPickerScreen(game, true));
|
||||||
|
return;
|
||||||
|
}
|
||||||
updateTechButton();
|
updateTechButton();
|
||||||
updateTileTable();
|
updateTileTable();
|
||||||
updateTiles();
|
updateTiles();
|
||||||
@ -416,16 +418,12 @@ public class WorldScreen extends CameraStageBaseScreen {
|
|||||||
|
|
||||||
TileTable.pack();
|
TileTable.pack();
|
||||||
|
|
||||||
// TileTable.setBackground(getTableBackground(TileTable.getWidth(),TileTable.getHeight()));
|
|
||||||
|
|
||||||
TileTable.setPosition(stage.getWidth()-10- TileTable.getWidth(), 10);
|
TileTable.setPosition(stage.getWidth()-10- TileTable.getWidth(), 10);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void updateTiles() {
|
private void updateTiles() {
|
||||||
for (WorldTileGroup WG : tileGroups.linqValues()) WG.update(this);
|
for (WorldTileGroup WG : tileGroups.linqValues()) WG.update(this);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if(unitTile!=null) return; // While we're in "unit move" mode, no tiles but the tiles the unit can move to will be "visible"
|
if(unitTile!=null) return; // While we're in "unit move" mode, no tiles but the tiles the unit can move to will be "visible"
|
||||||
|
|
||||||
// YES A TRIPLE FOR, GOT PROBLEMS WITH THAT?
|
// YES A TRIPLE FOR, GOT PROBLEMS WITH THAT?
|
||||||
|
@ -31,10 +31,7 @@ public class TechPickerScreen extends PickerScreen {
|
|||||||
public TechPickerScreen(final UnCivGame game) {
|
public TechPickerScreen(final UnCivGame game) {
|
||||||
super(game);
|
super(game);
|
||||||
|
|
||||||
Technology[][] techMatrix = new Technology[10][10]; // Divided into columns, then rows
|
Technology[][] techMatrix = new Technology[12][10]; // Divided into columns, then rows
|
||||||
for (int i = 0; i < techMatrix.length; i++) {
|
|
||||||
techMatrix[i] = new Technology[10];
|
|
||||||
}
|
|
||||||
|
|
||||||
for (Technology technology : GameBasics.Technologies.linqValues()) {
|
for (Technology technology : GameBasics.Technologies.linqValues()) {
|
||||||
techMatrix[technology.column.columnNumber-1][technology.row - 1] = technology;
|
techMatrix[technology.column.columnNumber-1][technology.row - 1] = technology;
|
||||||
@ -58,9 +55,10 @@ public class TechPickerScreen extends PickerScreen {
|
|||||||
topTable.add(TB);
|
topTable.add(TB);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
SetButtonsInfo();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
setButtonsInfo();
|
||||||
|
|
||||||
rightSideButton.setText("Pick a tech");
|
rightSideButton.setText("Pick a tech");
|
||||||
rightSideButton.setTouchable(Touchable.disabled);
|
rightSideButton.setTouchable(Touchable.disabled);
|
||||||
rightSideButton.setColor(Color.GRAY);
|
rightSideButton.setColor(Color.GRAY);
|
||||||
@ -76,12 +74,13 @@ public class TechPickerScreen extends PickerScreen {
|
|||||||
}
|
}
|
||||||
else civTech.techsToResearch = techsToResearch;
|
else civTech.techsToResearch = techsToResearch;
|
||||||
game.setWorldScreen();
|
game.setWorldScreen();
|
||||||
|
game.worldScreen.update();
|
||||||
dispose();
|
dispose();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public void SetButtonsInfo() {
|
public void setButtonsInfo() {
|
||||||
for (String techName : techNameToButton.keySet()) {
|
for (String techName : techNameToButton.keySet()) {
|
||||||
TextButton TB = techNameToButton.get(techName);
|
TextButton TB = techNameToButton.get(techName);
|
||||||
TB.getStyle().checkedFontColor = Color.BLACK;
|
TB.getStyle().checkedFontColor = Color.BLACK;
|
||||||
@ -91,7 +90,7 @@ public class TechPickerScreen extends PickerScreen {
|
|||||||
else TB.setColor(Color.GRAY);
|
else TB.setColor(Color.GRAY);
|
||||||
|
|
||||||
TB.setChecked(false);
|
TB.setChecked(false);
|
||||||
TB.setText(techName);
|
StringBuilder text = new StringBuilder(techName);
|
||||||
|
|
||||||
if (selectedTech != null) {
|
if (selectedTech != null) {
|
||||||
Technology thisTech = GameBasics.Technologies.get(techName);
|
Technology thisTech = GameBasics.Technologies.get(techName);
|
||||||
@ -100,14 +99,15 @@ public class TechPickerScreen extends PickerScreen {
|
|||||||
TB.setColor(TB.getColor().lerp(Color.LIGHT_GRAY, 0.5f));
|
TB.setColor(TB.getColor().lerp(Color.LIGHT_GRAY, 0.5f));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (thisTech.prerequisites.contains(selectedTech.name)) TB.setText("*" + techName);
|
if (thisTech.prerequisites.contains(selectedTech.name)) text.insert(0, "*");
|
||||||
else if (selectedTech.prerequisites.contains(techName)) TB.setText(techName + "*");
|
else if (selectedTech.prerequisites.contains(techName)) text.append("*");
|
||||||
}
|
}
|
||||||
if (techsToResearch.contains(techName)) {
|
if (techsToResearch.contains(techName)) {
|
||||||
TB.setText(TB.getText() + " (" + techsToResearch.indexOf(techName) + ")");
|
text.append(" (").append(techsToResearch.indexOf(techName)).append(")");
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!civTech.isResearched(techName)) TB.setText(TB.getText() + "\r\n" + game.civInfo.turnsToTech(techName) + " turns");
|
if(!civTech.isResearched(techName)) text.append("\r\n"+game.civInfo.turnsToTech(techName) + " turns");
|
||||||
|
TB.setText(text.toString());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -120,7 +120,7 @@ public class TechPickerScreen extends PickerScreen {
|
|||||||
rightSideButton.setText("Research");
|
rightSideButton.setText("Research");
|
||||||
rightSideButton.setTouchable(Touchable.disabled);
|
rightSideButton.setTouchable(Touchable.disabled);
|
||||||
rightSideButton.setColor(Color.GRAY);
|
rightSideButton.setColor(Color.GRAY);
|
||||||
SetButtonsInfo();
|
setButtonsInfo();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -148,7 +148,7 @@ public class TechPickerScreen extends PickerScreen {
|
|||||||
}
|
}
|
||||||
|
|
||||||
rightSideButton.setText("Research \r\n" + techsToResearch.get(0));
|
rightSideButton.setText("Research \r\n" + techsToResearch.get(0));
|
||||||
SetButtonsInfo();
|
setButtonsInfo();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void selectTechnologyForFreeTech(Technology tech){
|
private void selectTechnologyForFreeTech(Technology tech){
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package com.unciv.models.gamebasics;
|
package com.unciv.models.gamebasics;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashSet;
|
||||||
|
|
||||||
public class Technology
|
public class Technology
|
||||||
{
|
{
|
||||||
@ -8,7 +9,7 @@ public class Technology
|
|||||||
|
|
||||||
public String description;
|
public String description;
|
||||||
public int cost;
|
public int cost;
|
||||||
public ArrayList<String> prerequisites = new ArrayList<String>();
|
public HashSet<String> prerequisites = new HashSet<String>();
|
||||||
|
|
||||||
public TechColumn column; // The column that this tech is in the tech tree
|
public TechColumn column; // The column that this tech is in the tech tree
|
||||||
public int row;
|
public int row;
|
||||||
|
Reference in New Issue
Block a user