Optimized hex math - GetVectorsInDistance is now much more efficient!

This commit is contained in:
Yair Morgenstern 2017-12-09 23:12:08 +02:00
parent 45869c5a0b
commit c579215743
8 changed files with 61 additions and 53 deletions

View file

@ -145,7 +145,7 @@ public class CityBuildings
stats.science += getCity().getBuildingUniques().count(new Predicate<String>() {
@Override
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)
return stats;

View file

@ -281,6 +281,11 @@ public class CityInfo {
public String GetBy(Building arg0) {
return arg0.unique;
}
}).where(new Predicate<String>() {
@Override
public boolean evaluate(String arg0) {
return arg0!=null;
}
});
}

View file

@ -5,11 +5,12 @@ import com.unciv.models.gamebasics.Technology;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
public class CivilizationTech{
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. */
public ArrayList<String> techsToResearch = new ArrayList<String>();
public HashMap<String, Integer> techsInProgress = new HashMap<String, Integer>();

View file

@ -46,22 +46,31 @@ public class HexMath
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> oldHexes;
HashSet<Vector2> newHexes = new HashSet<Vector2>();
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);
}
}
for (int i = 0; i < distance + 1; i++) {
hexesToReturn.addAll(GetVectorsAtDistance(origin, i));
}
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.
int distance = 0;
while(true){
if(GetVectorsInDistance(origin,distance).contains(destination)) return distance;
if(GetVectorsAtDistance(origin,distance).contains(destination)) return 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;
// }
}

View file

@ -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.Image;
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.unciv.civinfo.CivilizationInfo;
import com.unciv.civinfo.RoadStatus;
import com.unciv.civinfo.TileInfo;
import com.unciv.models.LinqCollection;
import com.unciv.models.LinqHashMap;
public class TileGroup extends Group {
Image terrainImage;
String terrainType;
Image resourceImage;
Image unitImage;
Image improvementImage;
@ -29,7 +28,8 @@ public class TileGroup extends Group {
TileGroup(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.setSize(50,50);
addActor(terrainImage);
@ -50,8 +50,12 @@ public class TileGroup extends Group {
void update() {
String terrainFileName ="TerrainIcons/" + tileInfo.getLastTerrain().name.replace(' ','_') + "_(Civ5).png";
terrainImage.setDrawable(new TextureRegionDrawable(ImageGetter.textureRegionByFileName.get(terrainFileName))); // In case we e.g. removed a jungle
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
}
if (tileInfo.hasViewableResource() && resourceImage == null) { // Need to add the resource image!
String fileName = "ResourceIcons/" + tileInfo.resource + "_(Civ5).png";

View file

@ -94,8 +94,10 @@ public class WorldScreen extends CameraStageBaseScreen {
}
public void update(){
if(game.civInfo.tech.freeTechs!=0)
game.setScreen(new TechPickerScreen(game,true));
if(game.civInfo.tech.freeTechs!=0) {
game.setScreen(new TechPickerScreen(game, true));
return;
}
updateTechButton();
updateTileTable();
updateTiles();
@ -416,16 +418,12 @@ public class WorldScreen extends CameraStageBaseScreen {
TileTable.pack();
// TileTable.setBackground(getTableBackground(TileTable.getWidth(),TileTable.getHeight()));
TileTable.setPosition(stage.getWidth()-10- TileTable.getWidth(), 10);
}
private void updateTiles() {
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"
// YES A TRIPLE FOR, GOT PROBLEMS WITH THAT?

View file

@ -31,10 +31,7 @@ public class TechPickerScreen extends PickerScreen {
public TechPickerScreen(final UnCivGame game) {
super(game);
Technology[][] techMatrix = new Technology[10][10]; // Divided into columns, then rows
for (int i = 0; i < techMatrix.length; i++) {
techMatrix[i] = new Technology[10];
}
Technology[][] techMatrix = new Technology[12][10]; // Divided into columns, then rows
for (Technology technology : GameBasics.Technologies.linqValues()) {
techMatrix[technology.column.columnNumber-1][technology.row - 1] = technology;
@ -58,9 +55,10 @@ public class TechPickerScreen extends PickerScreen {
topTable.add(TB);
}
}
SetButtonsInfo();
}
setButtonsInfo();
rightSideButton.setText("Pick a tech");
rightSideButton.setTouchable(Touchable.disabled);
rightSideButton.setColor(Color.GRAY);
@ -76,12 +74,13 @@ public class TechPickerScreen extends PickerScreen {
}
else civTech.techsToResearch = techsToResearch;
game.setWorldScreen();
game.worldScreen.update();
dispose();
}
});
}
public void SetButtonsInfo() {
public void setButtonsInfo() {
for (String techName : techNameToButton.keySet()) {
TextButton TB = techNameToButton.get(techName);
TB.getStyle().checkedFontColor = Color.BLACK;
@ -91,7 +90,7 @@ public class TechPickerScreen extends PickerScreen {
else TB.setColor(Color.GRAY);
TB.setChecked(false);
TB.setText(techName);
StringBuilder text = new StringBuilder(techName);
if (selectedTech != null) {
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));
}
if (thisTech.prerequisites.contains(selectedTech.name)) TB.setText("*" + techName);
else if (selectedTech.prerequisites.contains(techName)) TB.setText(techName + "*");
if (thisTech.prerequisites.contains(selectedTech.name)) text.insert(0, "*");
else if (selectedTech.prerequisites.contains(techName)) text.append("*");
}
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.setTouchable(Touchable.disabled);
rightSideButton.setColor(Color.GRAY);
SetButtonsInfo();
setButtonsInfo();
return;
}
@ -148,7 +148,7 @@ public class TechPickerScreen extends PickerScreen {
}
rightSideButton.setText("Research \r\n" + techsToResearch.get(0));
SetButtonsInfo();
setButtonsInfo();
}
private void selectTechnologyForFreeTech(Technology tech){

View file

@ -1,6 +1,7 @@
package com.unciv.models.gamebasics;
import java.util.ArrayList;
import java.util.HashSet;
public class Technology
{
@ -8,7 +9,7 @@ public class Technology
public String description;
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 int row;