reformat code

- fix color identity
- service pool is working good on android now so use it on generating map
This commit is contained in:
Anthony Calosa
2022-08-09 09:17:19 +08:00
parent 36e7687034
commit c8073a333c
2 changed files with 224 additions and 258 deletions

View File

@@ -46,16 +46,15 @@ public class World implements Disposable, SaveFileContent {
private boolean worldDataLoaded = false; private boolean worldDataLoaded = false;
private Texture globalTexture = null; private Texture globalTexture = null;
public Random getRandom() public Random getRandom() {
{
return random; return random;
} }
static public int highestBiome(long biome) { static public int highestBiome(long biome) {
return (int) (Math.log(Long.highestOneBit(biome)) / Math.log(2)); return (int) (Math.log(Long.highestOneBit(biome)) / Math.log(2));
} }
public boolean collidingTile(Rectangle boundingRect) public boolean collidingTile(Rectangle boundingRect) {
{
int xLeft = (int) boundingRect.getX() / getTileSize(); int xLeft = (int) boundingRect.getX() / getTileSize();
int yTop = (int) boundingRect.getY() / getTileSize(); int yTop = (int) boundingRect.getY() / getTileSize();
@@ -73,6 +72,7 @@ public class World implements Disposable, SaveFileContent {
return false; return false;
} }
public void loadWorldData() { public void loadWorldData() {
if (worldDataLoaded) if (worldDataLoaded)
return; return;
@@ -105,7 +105,6 @@ public class World implements Disposable, SaveFileContent {
terrainMap = (int[][]) saveFileData.readObject("terrainMap"); terrainMap = (int[][]) saveFileData.readObject("terrainMap");
width = saveFileData.readInt("width"); width = saveFileData.readInt("width");
height = saveFileData.readInt("height"); height = saveFileData.readInt("height");
mapObjectIds = new SpritesDataMap(getChunkSize(), this.data.tileSize, this.data.width / getChunkSize()); mapObjectIds = new SpritesDataMap(getChunkSize(), this.data.tileSize, this.data.width / getChunkSize());
@@ -137,6 +136,7 @@ public class World implements Disposable, SaveFileContent {
public BiomeSpriteData getObject(int id) { public BiomeSpriteData getObject(int id) {
return mapObjectIds.get(id); return mapObjectIds.get(id);
} }
private class DrawingInformation { private class DrawingInformation {
private int neighbors; private int neighbors;
@@ -154,6 +154,7 @@ public class World implements Disposable, SaveFileContent {
regions.drawPixmapOn(terrain, neighbors, drawingPixmap); regions.drawPixmapOn(terrain, neighbors, drawingPixmap);
} }
} }
public Pixmap getBiomeSprite(int x, int y) { public Pixmap getBiomeSprite(int x, int y) {
if (x < 0 || y <= 0 || x >= width || y > height) if (x < 0 || y <= 0 || x >= width || y > height)
return new Pixmap(data.tileSize, data.tileSize, Pixmap.Format.RGBA8888); return new Pixmap(data.tileSize, data.tileSize, Pixmap.Format.RGBA8888);
@@ -188,8 +189,7 @@ public class World implements Disposable, SaveFileContent {
bitIndex--; bitIndex--;
} }
} }
if(biomeTerrain!=0&&neighbors!=0b111_111_111) if (biomeTerrain != 0 && neighbors != 0b111_111_111) {
{
bitIndex = 8; bitIndex = 8;
int baseNeighbors = 0; int baseNeighbors = 0;
for (int ny = 1; ny > -2; ny--) { for (int ny = 1; ny > -2; ny--) {
@@ -206,8 +206,7 @@ public class World implements Disposable, SaveFileContent {
} }
int lastFullNeighbour = -1; int lastFullNeighbour = -1;
int counter = 0; int counter = 0;
for(DrawingInformation info:information) for (DrawingInformation info : information) {
{
if (info.neighbors == 0b111_111_111) if (info.neighbors == 0b111_111_111)
lastFullNeighbour = counter; lastFullNeighbour = counter;
counter++; counter++;
@@ -216,10 +215,8 @@ public class World implements Disposable, SaveFileContent {
counter = 0; counter = 0;
if (lastFullNeighbour < 0 && information.size() != 0) if (lastFullNeighbour < 0 && information.size() != 0)
information.get(0).neighbors = 0b111_111_111; information.get(0).neighbors = 0b111_111_111;
for(DrawingInformation info:information) for (DrawingInformation info : information) {
{ if (counter < lastFullNeighbour) {
if(counter<lastFullNeighbour)
{
counter++; counter++;
continue; continue;
} }
@@ -236,6 +233,7 @@ public class World implements Disposable, SaveFileContent {
return 0; return 0;
} }
} }
public boolean isStructure(int x, int y) { public boolean isStructure(int x, int y) {
try { try {
return (terrainMap[x][height - y - 1] & ~isStructureBit) != 0; return (terrainMap[x][height - y - 1] & ~isStructureBit) != 0;
@@ -259,31 +257,30 @@ public class World implements Disposable, SaveFileContent {
return true; return true;
} }
} }
public WorldData getData() { public WorldData getData() {
return data; return data;
} }
private void clearTerrain(int x,int y,int size)
{ private void clearTerrain(int x, int y, int size) {
for (int xclear = -size; xclear < size; xclear++) for (int xclear = -size; xclear < size; xclear++)
for(int yclear=-size;yclear<size;yclear++) for (int yclear = -size; yclear < size; yclear++) {
{
try { try {
terrainMap[x + xclear][height - 1 - (y + yclear)] = 0; terrainMap[x + xclear][height - 1 - (y + yclear)] = 0;
} } catch (ArrayIndexOutOfBoundsException e) {
catch (ArrayIndexOutOfBoundsException e)
{
} }
} }
} }
private long measureGenerationTime(String msg,long lastTime)
{ private long measureGenerationTime(String msg, long lastTime) {
long currentTime = System.currentTimeMillis(); long currentTime = System.currentTimeMillis();
System.out.print("\n" + msg + " :\t\t" + ((currentTime - lastTime) / 1000f) + " s"); System.out.print("\n" + msg + " :\t\t" + ((currentTime - lastTime) / 1000f) + " s");
return currentTime; return currentTime;
} }
public World generateNew(long seed) { public World generateNew(long seed) {
if (GuiBase.isAndroid()) if (GuiBase.isAndroid())
GuiBase.getInterface().preventSystemSleep(true); GuiBase.getInterface().preventSystemSleep(true);
@@ -328,15 +325,6 @@ private long measureGenerationTime(String msg,long lastTime)
int biomeHeight = (int) Math.round(biome.height * (double) height); int biomeHeight = (int) Math.round(biome.height * (double) height);
for (BiomeStructureData data : biome.structures) { for (BiomeStructureData data : biome.structures) {
long localSeed = seed; long localSeed = seed;
if (GuiBase.isAndroid()) {
FThreads.invokeInEdtNowOrLater(() -> {
long threadStartTime = System.currentTimeMillis();
BiomeStructure structure = new BiomeStructure(data, localSeed, biomeWidth, biomeHeight);
structure.initialize();
structureDataMap.put(data, structure);
measureGenerationTime("wavefunctioncollapse " + data.sourcePath, threadStartTime);
});
} else {
ThreadUtil.getServicePool().submit(() -> { ThreadUtil.getServicePool().submit(() -> {
long threadStartTime = System.currentTimeMillis(); long threadStartTime = System.currentTimeMillis();
BiomeStructure structure = new BiomeStructure(data, localSeed, biomeWidth, biomeHeight); BiomeStructure structure = new BiomeStructure(data, localSeed, biomeWidth, biomeHeight);
@@ -347,7 +335,6 @@ private long measureGenerationTime(String msg,long lastTime)
} }
} }
} }
}
FThreads.invokeInEdtNowOrLater(() -> { FThreads.invokeInEdtNowOrLater(() -> {
for (BiomeData biome : data.GetBiomes()) { for (BiomeData biome : data.GetBiomes()) {
@@ -387,13 +374,10 @@ private long measureGenerationTime(String msg,long lastTime)
biomeMap[x][y] |= (1L << biomeIndex[0]); biomeMap[x][y] |= (1L << biomeIndex[0]);
int terrainCounter = 1; int terrainCounter = 1;
terrainMap[x][y] = 0; terrainMap[x][y] = 0;
if(biome.terrain!=null) if (biome.terrain != null) {
{ for (BiomeTerrainData terrain : biome.terrain) {
for(BiomeTerrainData terrain:biome.terrain)
{
float terrainNoise = ((float) noise.eval(x / (float) width * (noiseZoom * terrain.resolution), y / (float) height * (noiseZoom * terrain.resolution)) + 1) / 2; float terrainNoise = ((float) noise.eval(x / (float) width * (noiseZoom * terrain.resolution), y / (float) height * (noiseZoom * terrain.resolution)) + 1) / 2;
if(terrainNoise>=terrain.min&&terrainNoise<=terrain.max) if (terrainNoise >= terrain.min && terrainNoise <= terrain.max) {
{
terrainMap[x][y] = terrainCounter; terrainMap[x][y] = terrainCounter;
} }
terrainCounter++; terrainCounter++;
@@ -401,10 +385,8 @@ private long measureGenerationTime(String msg,long lastTime)
} }
if (biome.collision) if (biome.collision)
terrainMap[x][y] |= collisionBit; terrainMap[x][y] |= collisionBit;
if(biome.structures!=null) if (biome.structures != null) {
{ for (BiomeStructureData data : biome.structures) {
for(BiomeStructureData data:biome.structures)
{
while (!structureDataMap.containsKey(data)) { while (!structureDataMap.containsKey(data)) {
try { try {
Thread.sleep(10); Thread.sleep(10);
@@ -418,8 +400,7 @@ private long measureGenerationTime(String msg,long lastTime)
int structureYStart = y - (biomeYStart - biomeHeight / 2) - (int) ((data.y * biomeHeight) - (data.height * biomeHeight / 2)); int structureYStart = y - (biomeYStart - biomeHeight / 2) - (int) ((data.y * biomeHeight) - (data.height * biomeHeight / 2));
int structureIndex = structure.objectID(structureXStart, structureYStart); int structureIndex = structure.objectID(structureXStart, structureYStart);
if(structureIndex>=0) if (structureIndex >= 0) {
{
pix.setColor(data.mappingInfo[structureIndex].getColor()); pix.setColor(data.mappingInfo[structureIndex].getColor());
pix.drawPixel(x, y); pix.drawPixel(x, y);
terrainMap[x][y] = terrainCounter + structureIndex; terrainMap[x][y] = terrainCounter + structureIndex;
@@ -462,8 +443,7 @@ private long measureGenerationTime(String msg,long lastTime)
y *= (biome.height * height / 2); y *= (biome.height * height / 2);
y += (height - (biome.startPointY * height)); y += (height - (biome.startPointY * height));
if((int)x<0||(int)y<=0||(int)y>=height||(int)x>=width|| biomeIndex2!= highestBiome(getBiome((int)x,(int)y))) if ((int) x < 0 || (int) y <= 0 || (int) y >= height || (int) x >= width || biomeIndex2 != highestBiome(getBiome((int) x, (int) y))) {
{
continue; continue;
} }
@@ -477,36 +457,29 @@ private long measureGenerationTime(String msg,long lastTime)
break; break;
} }
} }
if (breakNextLoop) if (breakNextLoop) {
{
boolean foundSolution = false; boolean foundSolution = false;
boolean noSolution = false; boolean noSolution = false;
breakNextLoop = false; breakNextLoop = false;
for(int xi=-1;xi<2&&!foundSolution;xi++) for (int xi = -1; xi < 2 && !foundSolution; xi++) {
{ for (int yi = -1; yi < 2 && !foundSolution; yi++) {
for(int yi=-1;yi<2&&!foundSolution;yi++)
{
for (Rectangle rect : otherPoints) { for (Rectangle rect : otherPoints) {
if (rect.contains(x + xi * data.tileSize, y + yi * data.tileSize)) { if (rect.contains(x + xi * data.tileSize, y + yi * data.tileSize)) {
noSolution = true; noSolution = true;
break; break;
} }
} }
if(!noSolution) if (!noSolution) {
{
foundSolution = true; foundSolution = true;
x = x + xi * data.tileSize; x = x + xi * data.tileSize;
y = y + yi * data.tileSize; y = y + yi * data.tileSize;
} }
} }
} }
if(!foundSolution) if (!foundSolution) {
{ if (counter == 499) {
if(counter==499)
{
System.err.print("Can not place POI " + poi.name + "\n"); System.err.print("Can not place POI " + poi.name + "\n");
} }
continue; continue;
@@ -525,9 +498,7 @@ private long measureGenerationTime(String msg,long lastTime)
if (poi.type != null && poi.type.equals("town")) { if (poi.type != null && poi.type.equals("town")) {
towns.add(newPoint); towns.add(newPoint);
} } else {
else
{
notTowns.add(newPoint); notTowns.add(newPoint);
} }
break; break;
@@ -610,8 +581,7 @@ private long measureGenerationTime(String msg,long lastTime)
int sy = startY < y1 ? 1 : -1; int sy = startY < y1 ? 1 : -1;
int err = dx - dy; int err = dx - dy;
int e2; int e2;
while (true) while (true) {
{
if (startX < 0 || startY <= 0 || startX >= width || startY > height) continue; if (startX < 0 || startY <= 0 || startX >= width || startY > height) continue;
if ((terrainMap[startX][height - startY] & collisionBit) != 0)//clear terrain if it has collision if ((terrainMap[startX][height - startY] & collisionBit) != 0)//clear terrain if it has collision
terrainMap[startX][height - startY] = 0; terrainMap[startX][height - startY] = 0;
@@ -620,13 +590,10 @@ private long measureGenerationTime(String msg,long lastTime)
if (startX == x1 && startY == y1) if (startX == x1 && startY == y1)
break; break;
e2 = 2 * err; e2 = 2 * err;
if (e2 > -dy) if (e2 > -dy) {
{
err = err - dy; err = err - dy;
startX = startX + sx; startX = startX + sx;
} } else if (e2 < dx) {
else if (e2 < dx)
{
err = err + dx; err = err + dx;
startY = startY + sy; startY = startY + sy;
} }
@@ -655,8 +622,7 @@ private long measureGenerationTime(String msg,long lastTime)
int sy = startY < y1 ? 1 : -1; int sy = startY < y1 ? 1 : -1;
int err = dx - dy; int err = dx - dy;
int e2; int e2;
while (true) while (true) {
{
if (startX < 0 || startY <= 0 || startX >= width || startY > height) continue; if (startX < 0 || startY <= 0 || startX >= width || startY > height) continue;
biomeMap[startX][height - startY] |= (1L << biomeIndex[0]); biomeMap[startX][height - startY] |= (1L << biomeIndex[0]);
terrainMap[startX][height - startY] = 0; terrainMap[startX][height - startY] = 0;
@@ -665,13 +631,10 @@ private long measureGenerationTime(String msg,long lastTime)
if (startX == x1 && startY == y1) if (startX == x1 && startY == y1)
break; break;
e2 = 2 * err; e2 = 2 * err;
if (e2 > -dy) if (e2 > -dy) {
{
err = err - dy; err = err - dy;
startX = startX + sx; startX = startX + sx;
} } else if (e2 < dx) {
else if (e2 < dx)
{
err = err + dx; err = err + dx;
startY = startY + sy; startY = startY + sy;
} }
@@ -768,6 +731,7 @@ private long measureGenerationTime(String msg,long lastTime)
public PointOfInterest findPointsOfInterest(String name) { public PointOfInterest findPointsOfInterest(String name) {
return mapPoiIds.findPointsOfInterest(name); return mapPoiIds.findPointsOfInterest(name);
} }
public int getChunkSize() { public int getChunkSize() {
return (Scene.getIntendedWidth() > Scene.getIntendedHeight() ? Scene.getIntendedWidth() : Scene.getIntendedHeight()) / data.tileSize; return (Scene.getIntendedWidth() > Scene.getIntendedHeight() ? Scene.getIntendedWidth() : Scene.getIntendedHeight()) / data.tileSize;
} }

View File

@@ -35,21 +35,19 @@ public class WorldSave {
private final SignalList onLoadList = new SignalList(); private final SignalList onLoadList = new SignalList();
public final World getWorld() public final World getWorld() {
{
return world; return world;
} }
public AdventurePlayer getPlayer()
{ public AdventurePlayer getPlayer() {
return player; return player;
} }
public void onLoad(Runnable run) public void onLoad(Runnable run) {
{
onLoadList.add(run); onLoadList.add(run);
} }
public PointOfInterestChanges getPointOfInterestChanges(String id)
{ public PointOfInterestChanges getPointOfInterestChanges(String id) {
if (!pointOfInterestChanges.containsKey(id)) if (!pointOfInterestChanges.containsKey(id))
pointOfInterestChanges.put(id, new PointOfInterestChanges()); pointOfInterestChanges.put(id, new PointOfInterestChanges());
return pointOfInterestChanges.get(id); return pointOfInterestChanges.get(id);
@@ -64,8 +62,7 @@ public class WorldSave {
try { try {
try (FileInputStream fos = new FileInputStream(fileName); try (FileInputStream fos = new FileInputStream(fileName);
InflaterInputStream inf = new InflaterInputStream(fos); InflaterInputStream inf = new InflaterInputStream(fos);
ObjectInputStream oos = new ObjectInputStream(inf)) ObjectInputStream oos = new ObjectInputStream(inf)) {
{
currentSave.header = (WorldSaveHeader) oos.readObject(); currentSave.header = (WorldSaveHeader) oos.readObject();
SaveFileData mainData = (SaveFileData) oos.readObject(); SaveFileData mainData = (SaveFileData) oos.readObject();
currentSave.player.load(mainData.readSubData("player")); currentSave.player.load(mainData.readSubData("player"));
@@ -89,9 +86,11 @@ public class WorldSave {
} }
return true; return true;
} }
public static boolean isSafeFile(String name) { public static boolean isSafeFile(String name) {
return filenameToSlot(name) != INVALID_SAVE_SLOT; return filenameToSlot(name) != INVALID_SAVE_SLOT;
} }
static public int filenameToSlot(String name) { static public int filenameToSlot(String name) {
if (name.equals("auto_save.sav")) if (name.equals("auto_save.sav"))
return AUTO_SAVE_SLOT; return AUTO_SAVE_SLOT;
@@ -133,6 +132,7 @@ public class WorldSave {
identity = dp.getColorIdentityforAdventure(); identity = dp.getColorIdentityforAdventure();
} else { } else {
starterDeck = isFantasy ? DeckgenUtil.getRandomOrPreconOrThemeDeck("", false, false, false) : Config.instance().starterDecks()[startingColorIdentity]; starterDeck = isFantasy ? DeckgenUtil.getRandomOrPreconOrThemeDeck("", false, false, false) : Config.instance().starterDecks()[startingColorIdentity];
identity = DeckProxy.getColorIdentityforAdventure(starterDeck);
} }
currentSave.player.create(name, startingColorIdentity, starterDeck, male, race, avatarIndex, isFantasy, diff); currentSave.player.create(name, startingColorIdentity, starterDeck, male, race, avatarIndex, isFantasy, diff);
currentSave.player.setWorldPosY((int) (currentSave.world.getData().playerStartPosY * currentSave.world.getData().height * currentSave.world.getTileSize())); currentSave.player.setWorldPosY((int) (currentSave.world.getData().playerStartPosY * currentSave.world.getData().height * currentSave.world.getTileSize()));
@@ -148,12 +148,15 @@ public class WorldSave {
public boolean autoSave() { public boolean autoSave() {
return save("auto save", AUTO_SAVE_SLOT); return save("auto save", AUTO_SAVE_SLOT);
} }
public boolean quickSave() { public boolean quickSave() {
return save("quick save", QUICK_SAVE_SLOT); return save("quick save", QUICK_SAVE_SLOT);
} }
public boolean quickLoad() { public boolean quickLoad() {
return load(QUICK_SAVE_SLOT); return load(QUICK_SAVE_SLOT);
} }
public boolean save(String text, int currentSlot) { public boolean save(String text, int currentSlot) {
header.name = text; header.name = text;
@@ -163,8 +166,7 @@ public class WorldSave {
try { try {
try (FileOutputStream fos = new FileOutputStream(fileName); try (FileOutputStream fos = new FileOutputStream(fileName);
DeflaterOutputStream def = new DeflaterOutputStream(fos); DeflaterOutputStream def = new DeflaterOutputStream(fos);
ObjectOutputStream oos = new ObjectOutputStream(def)) ObjectOutputStream oos = new ObjectOutputStream(def)) {
{
header.saveDate = new Date(); header.saveDate = new Date();
oos.writeObject(header); oos.writeObject(header);
SaveFileData mainData = new SaveFileData(); SaveFileData mainData = new SaveFileData();