From f51dba79a687082b85f5b4620dedd5c8b782ef47 Mon Sep 17 00:00:00 2001 From: Forwolk Date: Wed, 8 Aug 2018 00:17:07 +0300 Subject: [PATCH] Region managed moved to CubicWorld --- .../generated_world/world/CubicWorld.java | 148 +++++++++++++++--- .../generated_world/world/RegionManager.java | 143 ----------------- .../SeedRandomGeneratorTest.java | 2 + 3 files changed, 124 insertions(+), 169 deletions(-) delete mode 100644 generated_world/src/main/java/mc/world/generated_world/world/RegionManager.java diff --git a/generated_world/src/main/java/mc/world/generated_world/world/CubicWorld.java b/generated_world/src/main/java/mc/world/generated_world/world/CubicWorld.java index d7f1724..85c3290 100644 --- a/generated_world/src/main/java/mc/world/generated_world/world/CubicWorld.java +++ b/generated_world/src/main/java/mc/world/generated_world/world/CubicWorld.java @@ -4,34 +4,65 @@ import com.flowpowered.nbt.Tag; import lombok.Getter; import lombok.Setter; import lombok.extern.slf4j.Slf4j; +import mc.core.Direction; import mc.core.Location; import mc.core.WarpPosition; -import mc.core.world.block.BlockType; import mc.core.player.Look; -import mc.core.world.chunk.Chunk; import mc.core.world.IWorldType; import mc.core.world.Region; import mc.core.world.World; +import mc.core.world.WorldGenerator; +import mc.core.world.block.BlockType; +import mc.core.world.chunk.Chunk; +import mc.world.generated_world.serialization.RegionReaderWriter; import org.springframework.beans.factory.annotation.Autowired; +import java.io.File; +import java.io.IOException; +import java.text.MessageFormat; import java.util.HashMap; import java.util.Map; import java.util.UUID; +import java.util.concurrent.locks.Lock; +import java.util.concurrent.locks.ReentrantLock; import java.util.stream.Stream; -import static mc.world.generated_world.WorldConstants.WORLD_CHUNK_SIZE; -import static mc.world.generated_world.WorldConstants.WORLD_MAX_HEIGHT; +import static mc.world.generated_world.WorldConstants.*; + +/* + * NORTH + * + * EAST WEST + * + * SOUTH + * + * + ----> X + * | + * | + * | + * V Z + */ @Slf4j public class CubicWorld implements World { + private int pointX = -1; + private int pointZ = -1; + private int sizeX = 2; + private int sizeZ = 2; + private Region[][] regions = new Region[sizeX][sizeZ]; + private final Lock regionSaveLock = new ReentrantLock(); + @Autowired + private RegionReaderWriter regionReaderWriter; + @Autowired + private WorldGenerator worldGenerator; + @Setter + private boolean autoSaveRegionAfterGenerating = true; @Getter private final UUID worldId; private final int seed; private volatile WarpPosition warpPosition; private final transient Object spawnLocationLock = new Object(); private final Map> nbtTagMap = new HashMap<>(); - @Autowired - private RegionManager regionManager; @Getter@Setter private String name; @@ -62,23 +93,8 @@ public class CubicWorld implements World { @Override public WarpPosition getSpawn() { - if (warpPosition == null) { - synchronized (spawnLocationLock) { - if (warpPosition == null) { - log.warn("Spawn location is not defined. Trying to select best location"); - warpPosition = new WarpPosition(Location.startPointLocation(), new Look(0, 0)); - for (int y = WORLD_MAX_HEIGHT; y > 0; y --) { - Chunk chunk = getChunk(0,y / WORLD_CHUNK_SIZE, 0); - if (chunk.getBlock(0, y, 0).getBlockType() != BlockType.AIR) { - warpPosition = new WarpPosition(new Location(0, y + 1, 0), new Look(0, 0)); - break; - } - } - warpPosition = new WarpPosition(Location.startPointLocation(), new Look(0,0)); - } - } - } - return warpPosition; + /* FIXME */ + return new WarpPosition(new Location(0, 100, 0), new Look(0, 0)); } @Override @@ -90,7 +106,8 @@ public class CubicWorld implements World { @Override public Chunk getChunk(int x, int y, int z) { - return null; + Region region = getRegion(x / 16, z / 16); + return region.getChunkAt(x % 16, y % 16, z % 16); } @Override @@ -100,12 +117,42 @@ public class CubicWorld implements World { @Override public Region getRegion(int x, int z) { - return null; + checkCoordsInCache(x, z); + Region region; + if (regions[x - pointX][z - pointZ] == null) { + File file = new File(new File("worlds", this.getWorldId().toString()), MessageFormat.format(REGION_FILE_NAME_TEMPLATE, x, z)); + if (!file.exists()) { + region = worldGenerator.generateRegion(x, z, this); + if (autoSaveRegionAfterGenerating) { + try { + regionReaderWriter.write(region); + } catch (IOException e) { + log.error("Error occurred while saving region data"); + } + } + } else { + try { + region = regionReaderWriter.read(x, z, this); + } catch (IOException e) { + log.error("Error occurred while loading region"); + region = null; + } + } + setRegion(region.getX(), region.getZ(), region); + } else { + region = regions[x - pointX][z - pointZ]; + } + return region; } @Override public void setRegion(int x, int z, Region region) { - + try { + regionSaveLock.lock(); + regions[x - pointX][z - pointZ] = region; + } finally { + regionSaveLock.unlock(); + } } @Override @@ -127,4 +174,53 @@ public class CubicWorld implements World { public Stream> tagStream() { return nbtTagMap.values().stream(); } + + private void checkCoordsInCache (int x, int z) { + if (x < pointX) { + addLines(Direction.EAST, pointX - x); + } else if (x > pointX + sizeX) { + addLines(Direction.WEST, x - (pointX + sizeX)); + } else if (z < pointZ) { + addLines(Direction.NORTH, pointZ - z); + } else if (z > pointZ + sizeZ) { + addLines(Direction.SOUTH, z - (pointZ + sizeZ)); + } + } + + private void addLines (Direction direction, int amount) { + int addBeforeX = 0; + int addAfterX = 0; + int addBeforeZ = 0; + int addAfterZ = 0; + switch (direction) { + case NORTH: + addBeforeZ = amount; + break; + case EAST: + addBeforeX = amount; + break; + case WEST: + addAfterX = amount; + break; + case SOUTH: + addAfterZ = amount; + break; + } + try { + regionSaveLock.lock(); + int tempSizeX = sizeX + addAfterX + addBeforeX; + int tempSizeZ = sizeZ + addAfterZ + addBeforeZ; + Region[][] temp = new Region[tempSizeX][tempSizeZ]; + for (int x = 0; x < sizeX; x ++) { + System.arraycopy(regions[x], 0, temp[x + addBeforeX], addBeforeZ, sizeZ); + } + + this.sizeX = tempSizeX; + this.sizeZ = tempSizeZ; + this.pointX = pointX - addBeforeX; + this.pointZ = pointZ - addBeforeZ; + } finally { + regionSaveLock.unlock(); + } + } } diff --git a/generated_world/src/main/java/mc/world/generated_world/world/RegionManager.java b/generated_world/src/main/java/mc/world/generated_world/world/RegionManager.java deleted file mode 100644 index 0cc0ef4..0000000 --- a/generated_world/src/main/java/mc/world/generated_world/world/RegionManager.java +++ /dev/null @@ -1,143 +0,0 @@ -package mc.world.generated_world.world; - -import lombok.Setter; -import lombok.extern.slf4j.Slf4j; -import mc.core.Direction; -import mc.core.world.Region; -import mc.core.world.World; -import mc.core.world.WorldGenerator; -import mc.world.generated_world.serialization.RegionReaderWriter; -import org.springframework.beans.factory.annotation.Autowired; - -import java.io.File; -import java.io.IOException; -import java.text.MessageFormat; -import java.util.concurrent.locks.Lock; -import java.util.concurrent.locks.ReentrantLock; - -import static mc.world.generated_world.WorldConstants.REGION_FILE_NAME_TEMPLATE; - -/* -* NORTH -* -* EAST WEST -* -* SOUTH -* -* + ----> X -* | -* | -* | -* V Z -*/ -@Slf4j -public class RegionManager { - private final World world; - private int pointX = -1; - private int pointZ = -1; - private int sizeX = 2; - private int sizeZ = 2; - private Region[][] regions = new Region[sizeX][sizeZ]; - private final Lock regionSaveLock = new ReentrantLock(); - @Autowired - private RegionReaderWriter regionReaderWriter; - @Autowired - private WorldGenerator worldGenerator; - @Setter - private boolean autoSaveRegionAfterGenerating = true; - - - public RegionManager(World world) { - this.world = world; - } - - public void setRegion (Region region) { - int x = region.getX(); - int z = region.getZ(); - - try { - regionSaveLock.lock(); - regions[x - pointX][z - pointZ] = region; - } finally { - regionSaveLock.unlock(); - } - } - - private void checkCoordsInCache (int x, int z) { - if (x < pointX) { - addLines(Direction.EAST, pointX - x); - } else if (x > pointX + sizeX) { - addLines(Direction.WEST, x - (pointX + sizeX)); - } else if (z < pointZ) { - addLines(Direction.NORTH, pointZ - z); - } else if (z > pointZ + sizeZ) { - addLines(Direction.SOUTH, z - (pointZ + sizeZ)); - } - } - - public Region getRegion (int x, int z) { - checkCoordsInCache(x, z); - Region region; - if (regions[x - pointX][z - pointZ] == null) { - File file = new File(new File("worlds", world.getWorldId().toString()), MessageFormat.format(REGION_FILE_NAME_TEMPLATE, x, z)); - if (!file.exists()) { - region = worldGenerator.generateRegion(x, z, world); - if (autoSaveRegionAfterGenerating) { - try { - regionReaderWriter.write(region); - } catch (IOException e) { - log.error("Error occurred while saving region data"); - } - } - } else { - try { - region = regionReaderWriter.read(x, z, world); - } catch (IOException e) { - log.error("Error occurred while loading region"); - region = null; - } - } - setRegion(region); - } else { - region = regions[x - pointX][z - pointZ]; - } - return region; - } - - private void addLines (Direction direction, int amount) { - int addBeforeX = 0; - int addAfterX = 0; - int addBeforeZ = 0; - int addAfterZ = 0; - switch (direction) { - case NORTH: - addBeforeZ = amount; - break; - case EAST: - addBeforeX = amount; - break; - case WEST: - addAfterX = amount; - break; - case SOUTH: - addAfterZ = amount; - break; - } - try { - int tempSizeX = sizeX + addAfterX + addBeforeX; - int tempSizeZ = sizeZ + addAfterZ + addBeforeZ; - Region[][] temp = new Region[tempSizeX][tempSizeZ]; - for (int x = 0; x < sizeX; x ++) { - System.arraycopy(regions[x], 0, temp[x + addBeforeX], addBeforeZ, sizeZ); - } - - this.sizeX = tempSizeX; - this.sizeZ = tempSizeZ; - this.pointX = pointX - addBeforeX; - this.pointZ = pointZ - addBeforeZ; - } finally { - regionSaveLock.unlock(); - } - } - -} diff --git a/generated_world/src/test/java/mc/world/generated_world/SeedRandomGeneratorTest.java b/generated_world/src/test/java/mc/world/generated_world/SeedRandomGeneratorTest.java index a99a251..6c5fbe4 100644 --- a/generated_world/src/test/java/mc/world/generated_world/SeedRandomGeneratorTest.java +++ b/generated_world/src/test/java/mc/world/generated_world/SeedRandomGeneratorTest.java @@ -1,6 +1,7 @@ package mc.world.generated_world; import mc.world.generated_world.generator.SeedRandomGenerator; +import org.junit.Ignore; import org.junit.Test; import javax.imageio.ImageIO; @@ -9,6 +10,7 @@ import java.io.File; import static org.junit.Assert.*; +@Ignore public class SeedRandomGeneratorTest { @Test