diff --git a/src/main/java/ru/windcorp/progressia/client/world/ChunkRender.java b/src/main/java/ru/windcorp/progressia/client/world/ChunkRender.java index ca58bf7..c69fcf3 100644 --- a/src/main/java/ru/windcorp/progressia/client/world/ChunkRender.java +++ b/src/main/java/ru/windcorp/progressia/client/world/ChunkRender.java @@ -27,6 +27,7 @@ import ru.windcorp.progressia.client.graphics.model.ShapeRenderHelper; import ru.windcorp.progressia.client.world.block.BlockRender; import ru.windcorp.progressia.client.world.block.BlockRenderRegistry; import ru.windcorp.progressia.client.world.tile.TileRender; +import ru.windcorp.progressia.client.world.tile.TileRenderReference; import ru.windcorp.progressia.client.world.tile.TileRenderRegistry; import ru.windcorp.progressia.client.world.tile.TileRenderStack; import ru.windcorp.progressia.common.world.ChunkData; @@ -34,10 +35,11 @@ import ru.windcorp.progressia.common.world.generic.GenericChunk; import ru.windcorp.progressia.common.world.rels.AbsFace; import ru.windcorp.progressia.common.world.rels.BlockFace; import ru.windcorp.progressia.common.world.rels.RelFace; +import ru.windcorp.progressia.common.world.tile.TileDataReference; import ru.windcorp.progressia.common.world.tile.TileDataStack; public class ChunkRender - implements GenericChunk { + implements GenericChunk { private final WorldRender world; private final ChunkData data; @@ -108,6 +110,28 @@ public class ChunkRender } private class TileRenderStackImpl extends TileRenderStack { + private class TileRenderReferenceImpl implements TileRenderReference { + private final TileDataReference parent; + + public TileRenderReferenceImpl(TileDataReference parent) { + this.parent = parent; + } + + @Override + public TileRender get() { + return TileRenderRegistry.getInstance().get(parent.get().getId()); + } + + @Override + public int getIndex() { + return parent.getIndex(); + } + + @Override + public TileRenderStack getStack() { + return TileRenderStackImpl.this; + } + } private final TileDataStack parent; @@ -129,6 +153,21 @@ public class ChunkRender public RelFace getFace() { return parent.getFace(); } + + @Override + public TileRenderReference getReference(int index) { + return new TileRenderReferenceImpl(parent.getReference(index)); + } + + @Override + public int getIndexByTag(int tag) { + return parent.getIndexByTag(tag); + } + + @Override + public int getTagByIndex(int index) { + return parent.getTagByIndex(index); + } @Override public TileRender get(int index) { diff --git a/src/main/java/ru/windcorp/progressia/client/world/WorldRender.java b/src/main/java/ru/windcorp/progressia/client/world/WorldRender.java index 8be08c2..2e674f0 100644 --- a/src/main/java/ru/windcorp/progressia/client/world/WorldRender.java +++ b/src/main/java/ru/windcorp/progressia/client/world/WorldRender.java @@ -34,6 +34,7 @@ import ru.windcorp.progressia.client.world.block.BlockRender; import ru.windcorp.progressia.client.world.entity.EntityRenderRegistry; import ru.windcorp.progressia.client.world.entity.EntityRenderable; import ru.windcorp.progressia.client.world.tile.TileRender; +import ru.windcorp.progressia.client.world.tile.TileRenderReference; import ru.windcorp.progressia.client.world.tile.TileRenderStack; import ru.windcorp.progressia.common.util.VectorUtil; import ru.windcorp.progressia.common.util.Vectors; @@ -47,7 +48,7 @@ import ru.windcorp.progressia.common.world.generic.ChunkSets; import ru.windcorp.progressia.common.world.generic.GenericWorld; public class WorldRender - implements GenericWorld { + implements GenericWorld { private final WorldData data; private final Client client; diff --git a/src/main/java/ru/windcorp/progressia/client/world/tile/TileRenderReference.java b/src/main/java/ru/windcorp/progressia/client/world/tile/TileRenderReference.java new file mode 100644 index 0000000..3def556 --- /dev/null +++ b/src/main/java/ru/windcorp/progressia/client/world/tile/TileRenderReference.java @@ -0,0 +1,27 @@ +/* + * Progressia + * Copyright (C) 2020-2021 Wind Corporation and contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package ru.windcorp.progressia.client.world.tile; + +import ru.windcorp.progressia.client.world.ChunkRender; +import ru.windcorp.progressia.client.world.block.BlockRender; +import ru.windcorp.progressia.common.world.generic.GenericTileReference; + +public interface TileRenderReference + extends GenericTileReference { + +} diff --git a/src/main/java/ru/windcorp/progressia/client/world/tile/TileRenderStack.java b/src/main/java/ru/windcorp/progressia/client/world/tile/TileRenderStack.java index 6bb8fb0..ca95cd2 100644 --- a/src/main/java/ru/windcorp/progressia/client/world/tile/TileRenderStack.java +++ b/src/main/java/ru/windcorp/progressia/client/world/tile/TileRenderStack.java @@ -19,11 +19,12 @@ package ru.windcorp.progressia.client.world.tile; import ru.windcorp.progressia.client.world.ChunkRender; +import ru.windcorp.progressia.client.world.block.BlockRender; import ru.windcorp.progressia.common.world.generic.GenericTileStack; import ru.windcorp.progressia.common.world.tile.TileDataStack; public abstract class TileRenderStack - extends GenericTileStack { + extends GenericTileStack { public abstract TileDataStack getData(); diff --git a/src/main/java/ru/windcorp/progressia/common/world/ChunkData.java b/src/main/java/ru/windcorp/progressia/common/world/ChunkData.java index 54b922d..9a00e25 100644 --- a/src/main/java/ru/windcorp/progressia/common/world/ChunkData.java +++ b/src/main/java/ru/windcorp/progressia/common/world/ChunkData.java @@ -36,12 +36,12 @@ import ru.windcorp.progressia.common.world.rels.AbsFace; import ru.windcorp.progressia.common.world.rels.BlockFace; import ru.windcorp.progressia.common.world.rels.RelFace; import ru.windcorp.progressia.common.world.tile.TileData; +import ru.windcorp.progressia.common.world.tile.TileDataReference; import ru.windcorp.progressia.common.world.tile.TileDataStack; -import ru.windcorp.progressia.common.world.tile.TileReference; import ru.windcorp.progressia.common.world.tile.TileStackIsFullException; public class ChunkData - implements GenericWritableChunk { + implements GenericWritableChunk { public static final int BLOCKS_PER_CHUNK = Coordinates.CHUNK_SIZE; public static final int CHUNK_RADIUS = BLOCKS_PER_CHUNK / 2; @@ -219,10 +219,10 @@ public class ChunkData * @author javapony */ private class TileDataStackImpl extends TileDataStack { - private class TileReferenceImpl implements TileReference { + private class TileDataReferenceImpl implements TileDataReference { private int index; - public TileReferenceImpl(int index) { + public TileDataReferenceImpl(int index) { this.index = index; } @@ -264,7 +264,7 @@ public class ChunkData private final TileData[] tiles = new TileData[TILES_PER_FACE]; private int size = 0; - private final TileReferenceImpl[] references = new TileReferenceImpl[tiles.length]; + private final TileDataReferenceImpl[] references = new TileDataReferenceImpl[tiles.length]; private final int[] indicesByTag = new int[tiles.length]; private final int[] tagsByIndex = new int[tiles.length]; @@ -437,11 +437,11 @@ public class ChunkData } @Override - public TileReference getReference(int index) { + public TileDataReference getReference(int index) { checkIndex(index, false); if (references[index] == null) { - references[index] = new TileReferenceImpl(index); + references[index] = new TileDataReferenceImpl(index); } return references[index]; @@ -500,7 +500,7 @@ public class ChunkData throw new AssertionError("get(index) is null"); if (references[index] != null) { - TileReference ref = getReference(index); + TileDataReference ref = getReference(index); if (ref == null) throw new AssertionError("references[index] is not null but getReference(index) is"); if (!ref.isValid()) diff --git a/src/main/java/ru/windcorp/progressia/common/world/WorldData.java b/src/main/java/ru/windcorp/progressia/common/world/WorldData.java index 625b8c2..7fec468 100644 --- a/src/main/java/ru/windcorp/progressia/common/world/WorldData.java +++ b/src/main/java/ru/windcorp/progressia/common/world/WorldData.java @@ -38,9 +38,10 @@ import ru.windcorp.progressia.common.world.generic.GenericWorld; import ru.windcorp.progressia.common.world.generic.LongBasedChunkMap; import ru.windcorp.progressia.common.world.tile.TileData; import ru.windcorp.progressia.common.world.tile.TileDataStack; +import ru.windcorp.progressia.common.world.tile.TileDataReference; public class WorldData - implements GenericWorld { + implements GenericWorld { private final ChunkMap chunksByPos = new LongBasedChunkMap<>( TCollections.synchronizedMap(new TLongObjectHashMap<>()) diff --git a/src/main/java/ru/windcorp/progressia/common/world/generic/ChunkMap.java b/src/main/java/ru/windcorp/progressia/common/world/generic/ChunkMap.java index 6c35781..5b4f6f6 100644 --- a/src/main/java/ru/windcorp/progressia/common/world/generic/ChunkMap.java +++ b/src/main/java/ru/windcorp/progressia/common/world/generic/ChunkMap.java @@ -77,27 +77,27 @@ public interface ChunkMap { // TODO implement (int, int, int) and GenericChunk versions of all of the // above - default boolean containsChunk(GenericChunk chunk) { + default boolean containsChunk(GenericChunk chunk) { return containsKey(chunk.getPosition()); } - default V get(GenericChunk chunk) { + default V get(GenericChunk chunk) { return get(chunk.getPosition()); } - default V put(GenericChunk chunk, V obj) { + default V put(GenericChunk chunk, V obj) { return put(chunk.getPosition(), obj); } - default V remove(GenericChunk chunk) { + default V remove(GenericChunk chunk) { return remove(chunk.getPosition()); } - default V getOrDefault(GenericChunk chunk, V def) { + default V getOrDefault(GenericChunk chunk, V def) { return containsChunk(chunk) ? def : get(chunk); } - default > V compute( + default > V compute( C chunk, BiFunction remappingFunction ) { @@ -128,8 +128,8 @@ public interface ChunkMap { void forEach(BiConsumer action); - default > void forEachIn( - GenericWorld world, + default > void forEachIn( + GenericWorld world, BiConsumer action ) { forEach((pos, value) -> { diff --git a/src/main/java/ru/windcorp/progressia/common/world/generic/ChunkMaps.java b/src/main/java/ru/windcorp/progressia/common/world/generic/ChunkMaps.java index e67f67e..c194351 100644 --- a/src/main/java/ru/windcorp/progressia/common/world/generic/ChunkMaps.java +++ b/src/main/java/ru/windcorp/progressia/common/world/generic/ChunkMaps.java @@ -174,42 +174,42 @@ public class ChunkMaps { } @Override - public boolean containsChunk(GenericChunk chunk) { + public boolean containsChunk(GenericChunk chunk) { synchronized (mutex) { return parent.containsChunk(chunk); } } @Override - public V get(GenericChunk chunk) { + public V get(GenericChunk chunk) { synchronized (mutex) { return parent.get(chunk); } } @Override - public V put(GenericChunk chunk, V obj) { + public V put(GenericChunk chunk, V obj) { synchronized (mutex) { return parent.put(chunk, obj); } } @Override - public V remove(GenericChunk chunk) { + public V remove(GenericChunk chunk) { synchronized (mutex) { return parent.remove(chunk); } } @Override - public V getOrDefault(GenericChunk chunk, V def) { + public V getOrDefault(GenericChunk chunk, V def) { synchronized (mutex) { return parent.getOrDefault(chunk, def); } } @Override - public > V compute( + public > V compute( C chunk, BiFunction remappingFunction ) { @@ -247,8 +247,8 @@ public class ChunkMaps { } @Override - public > void forEachIn( - GenericWorld world, + public > void forEachIn( + GenericWorld world, BiConsumer action ) { synchronized (mutex) { diff --git a/src/main/java/ru/windcorp/progressia/common/world/generic/ChunkSet.java b/src/main/java/ru/windcorp/progressia/common/world/generic/ChunkSet.java index 73c2267..6f26ef0 100644 --- a/src/main/java/ru/windcorp/progressia/common/world/generic/ChunkSet.java +++ b/src/main/java/ru/windcorp/progressia/common/world/generic/ChunkSet.java @@ -75,20 +75,20 @@ public interface ChunkSet extends Iterable { return result; } - default boolean contains(GenericChunk chunk) { + default boolean contains(GenericChunk chunk) { return contains(chunk.getPosition()); } - default boolean add(GenericChunk chunk) { + default boolean add(GenericChunk chunk) { return add(chunk.getPosition()); } - default boolean remove(GenericChunk chunk) { + default boolean remove(GenericChunk chunk) { return remove(chunk.getPosition()); } - default > void forEachIn( - GenericWorld world, + default > void forEachIn( + GenericWorld world, Consumer action ) { forEach(position -> { @@ -207,7 +207,7 @@ public interface ChunkSet extends Iterable { } } - default boolean containsAllChunks(Iterable> chunks) { + default boolean containsAllChunks(Iterable> chunks) { boolean[] hasMissing = new boolean[] { false }; chunks.forEach(c -> { @@ -219,7 +219,7 @@ public interface ChunkSet extends Iterable { return hasMissing[0]; } - default boolean containsAnyChunks(Iterable> chunks) { + default boolean containsAnyChunks(Iterable> chunks) { boolean[] hasPresent = new boolean[] { false }; chunks.forEach(c -> { @@ -231,11 +231,11 @@ public interface ChunkSet extends Iterable { return hasPresent[0]; } - default void addAllChunks(Iterable> chunks) { + default void addAllChunks(Iterable> chunks) { chunks.forEach(this::add); } - default void removeAllChunks(Iterable> chunks) { + default void removeAllChunks(Iterable> chunks) { chunks.forEach(this::remove); } diff --git a/src/main/java/ru/windcorp/progressia/common/world/generic/ChunkSets.java b/src/main/java/ru/windcorp/progressia/common/world/generic/ChunkSets.java index 93413e4..4ee643b 100644 --- a/src/main/java/ru/windcorp/progressia/common/world/generic/ChunkSets.java +++ b/src/main/java/ru/windcorp/progressia/common/world/generic/ChunkSets.java @@ -198,29 +198,29 @@ public class ChunkSets { } @Override - public boolean contains(GenericChunk chunk) { + public boolean contains(GenericChunk chunk) { synchronized (mutex) { return parent.contains(chunk); } } @Override - public boolean add(GenericChunk chunk) { + public boolean add(GenericChunk chunk) { synchronized (mutex) { return parent.add(chunk); } } @Override - public boolean remove(GenericChunk chunk) { + public boolean remove(GenericChunk chunk) { synchronized (mutex) { return parent.remove(chunk); } } @Override - public > void forEachIn( - GenericWorld world, + public > void forEachIn( + GenericWorld world, Consumer action ) { synchronized (mutex) { @@ -320,28 +320,28 @@ public class ChunkSets { } @Override - public boolean containsAllChunks(Iterable> chunks) { + public boolean containsAllChunks(Iterable> chunks) { synchronized (mutex) { return parent.containsAllChunks(chunks); } } @Override - public boolean containsAnyChunks(Iterable> chunks) { + public boolean containsAnyChunks(Iterable> chunks) { synchronized (mutex) { return parent.containsAnyChunks(chunks); } } @Override - public void addAllChunks(Iterable> chunks) { + public void addAllChunks(Iterable> chunks) { synchronized (mutex) { parent.addAllChunks(chunks); } } @Override - public void removeAllChunks(Iterable> chunks) { + public void removeAllChunks(Iterable> chunks) { synchronized (mutex) { parent.removeAllChunks(chunks); } diff --git a/src/main/java/ru/windcorp/progressia/common/world/generic/GenericChunk.java b/src/main/java/ru/windcorp/progressia/common/world/generic/GenericChunk.java index e51ac02..281b697 100644 --- a/src/main/java/ru/windcorp/progressia/common/world/generic/GenericChunk.java +++ b/src/main/java/ru/windcorp/progressia/common/world/generic/GenericChunk.java @@ -56,7 +56,15 @@ import ru.windcorp.progressia.common.world.rels.BlockFace; * @param tile stack type * @author javapony */ -public interface GenericChunk, B extends GenericBlock, T extends GenericTile, TS extends GenericTileStack> { +// @formatter:off +public interface GenericChunk< + B extends GenericBlock, + T extends GenericTile, + TS extends GenericTileStack , + TR extends GenericTileReference , + C extends GenericChunk +> { +// @formatter:on /** * The count of blocks in a side of a chunk. This is guaranteed to be a diff --git a/src/main/java/ru/windcorp/progressia/common/world/generic/GenericTileReference.java b/src/main/java/ru/windcorp/progressia/common/world/generic/GenericTileReference.java new file mode 100644 index 0000000..b3e7289 --- /dev/null +++ b/src/main/java/ru/windcorp/progressia/common/world/generic/GenericTileReference.java @@ -0,0 +1,41 @@ +/* + * Progressia + * Copyright (C) 2020-2021 Wind Corporation and contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package ru.windcorp.progressia.common.world.generic; + +// @formatter:off +public interface GenericTileReference< + B extends GenericBlock, + T extends GenericTile, + TS extends GenericTileStack , + TR extends GenericTileReference , + C extends GenericChunk +> { +// @formatter:on + + T get(); + + int getIndex(); + + TS getStack(); + + default boolean isValid() { + return get() != null; + } + +} diff --git a/src/main/java/ru/windcorp/progressia/common/world/generic/GenericTileStack.java b/src/main/java/ru/windcorp/progressia/common/world/generic/GenericTileStack.java index a2e8a4d..ddf7e00 100644 --- a/src/main/java/ru/windcorp/progressia/common/world/generic/GenericTileStack.java +++ b/src/main/java/ru/windcorp/progressia/common/world/generic/GenericTileStack.java @@ -27,9 +27,15 @@ import glm.vec._3.i.Vec3i; import ru.windcorp.progressia.common.world.Coordinates; import ru.windcorp.progressia.common.world.rels.RelFace; -public abstract class GenericTileStack, T extends GenericTile, C extends GenericChunk> - extends AbstractList - implements RandomAccess { +// @formatter:off +public abstract class GenericTileStack< + B extends GenericBlock, + T extends GenericTile, + TS extends GenericTileStack , + TR extends GenericTileReference , + C extends GenericChunk +> extends AbstractList implements RandomAccess { +// @formatter:on public static interface TSConsumer { void accept(int layer, T tile); @@ -43,6 +49,12 @@ public abstract class GenericTileStack public abstract RelFace getFace(); + public abstract TR getReference(int index); + + public abstract int getIndexByTag(int tag); + + public abstract int getTagByIndex(int index); + public Vec3i getBlockInWorld(Vec3i output) { // This is safe return Coordinates.getInWorld(getChunk().getPosition(), getBlockInChunk(output), output); @@ -105,4 +117,8 @@ public abstract class GenericTileStack return findClosest(id) != null; } + public B getHost() { + return getChunk().getBlock(getBlockInChunk(null)); + } + } diff --git a/src/main/java/ru/windcorp/progressia/common/world/generic/GenericWorld.java b/src/main/java/ru/windcorp/progressia/common/world/generic/GenericWorld.java index c71ef54..c945461 100644 --- a/src/main/java/ru/windcorp/progressia/common/world/generic/GenericWorld.java +++ b/src/main/java/ru/windcorp/progressia/common/world/generic/GenericWorld.java @@ -29,7 +29,16 @@ import ru.windcorp.progressia.common.world.Coordinates; import ru.windcorp.progressia.common.world.rels.AbsFace; import ru.windcorp.progressia.common.world.rels.BlockFace; -public interface GenericWorld, C extends GenericChunk, E extends GenericEntity> { +// @formatter:off +public interface GenericWorld< + B extends GenericBlock, + T extends GenericTile, + TS extends GenericTileStack , + TR extends GenericTileReference , + C extends GenericChunk , + E extends GenericEntity +> { +// @formatter:on Collection getChunks(); diff --git a/src/main/java/ru/windcorp/progressia/common/world/generic/GenericWritableChunk.java b/src/main/java/ru/windcorp/progressia/common/world/generic/GenericWritableChunk.java index 9dc1557..baf6cc2 100644 --- a/src/main/java/ru/windcorp/progressia/common/world/generic/GenericWritableChunk.java +++ b/src/main/java/ru/windcorp/progressia/common/world/generic/GenericWritableChunk.java @@ -19,12 +19,18 @@ package ru.windcorp.progressia.common.world.generic; import glm.vec._3.i.Vec3i; -import ru.windcorp.progressia.common.world.block.BlockData; +// @formatter:off +public interface GenericWritableChunk< + B extends GenericBlock, + T extends GenericTile, + TS extends GenericWritableTileStack , + TR extends GenericTileReference , + C extends GenericWritableChunk +> + extends GenericChunk { +// @formatter:on -public interface GenericWritableChunk, B extends GenericBlock, T extends GenericTile, TS extends GenericTileStack> - extends GenericChunk { - - void setBlock(Vec3i posInChunk, BlockData block, boolean notify); + void setBlock(Vec3i posInChunk, B block, boolean notify); void setBlockRel(Vec3i relativeBlockInChunk, B block, boolean notify); diff --git a/src/main/java/ru/windcorp/progressia/common/world/generic/GenericWritableTileStack.java b/src/main/java/ru/windcorp/progressia/common/world/generic/GenericWritableTileStack.java new file mode 100644 index 0000000..b2363e3 --- /dev/null +++ b/src/main/java/ru/windcorp/progressia/common/world/generic/GenericWritableTileStack.java @@ -0,0 +1,160 @@ +/* + * Progressia + * Copyright (C) 2020-2021 Wind Corporation and contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package ru.windcorp.progressia.common.world.generic; + +// @formatter:off +public abstract class GenericWritableTileStack< + B extends GenericBlock, + T extends GenericTile, + TS extends GenericWritableTileStack, + TR extends GenericTileReference, + C extends GenericWritableChunk +> + extends GenericTileStack { +// @formatter:on + + /** + * Inserts the specified tile at the specified position in this stack. + * Shifts the tile currently at that position (if any) and any tiles above + * to + * the top (adds one to their indices). + * + * @param index index at which the specified tile is to be inserted + * @param tile tile to be inserted + * @throws TileStackIsFullException if this stack is {@linkplain #isFull() + * full} + */ + /* + * Impl note: AbstractList provides a useless implementation of this method, + * make sure to override it in subclass + */ + @Override + public abstract void add(int index, T tile); + + /** + * Adds the specified tile at the end of this stack assigning it the + * provided tag. + * This method is useful for copying stacks when preserving tags is + * necessary. + * + * @param tile the tile to add + * @param tag the tag to assign the new tile + * @throws IllegalArgumentException if this stack already contains a tile + * with the + * provided tag + */ + public abstract void load(T tile, int tag); + + /** + * Replaces the tile at the specified position in this stack with the + * specified tile. + * + * @param index index of the tile to replace + * @param tile tile to be stored at the specified position + * @return the tile previously at the specified position + */ + /* + * Impl note: AbstractList provides a useless implementation of this method, + * make sure to override it in subclass + */ + @Override + public abstract T set(int index, T tile); + + /** + * Removes the tile at the specified position in this list. Shifts any + * subsequent tiles + * to the left (subtracts one from their indices). Returns the tile that was + * removed + * from the list. + * + * @param index the index of the tile to be removed + * @return the tile previously at the specified position + */ + /* + * Impl note: AbstractList provides a useless implementation of this method, + * make sure to override it in subclass + */ + @Override + public abstract T remove(int index); + + /* + * Aliases and overloads + */ + + public void addClosest(T tile) { + add(0, tile); + } + + public void addFarthest(T tile) { + add(size(), tile); + } + + /** + * Attempts to {@link #add(int, TileData) add} the provided {@code tile} + * at {@code index}. If the stack is {@linkplain #isFull() full}, does + * nothing. + * + * @param index the index to insert the tile at + * @param tile the tile to try to add + * @return {@code true} iff this stack has changed + */ + public boolean offer(int index, T tile) { + if (isFull()) + return false; + add(index, tile); + return true; + } + + public boolean offerClosest(T tile) { + return offer(0, tile); + } + + public boolean offerFarthest(T tile) { + return offer(size(), tile); + } + + public T removeClosest() { + return remove(0); + } + + public T removeFarthest() { + return remove(size() - 1); + } + + public T poll(int index) { + if (size() <= index) + return null; + return remove(index); + } + + public T pollClosest() { + return poll(0); + } + + public T pollFarthest() { + return poll(size() - 1); + } + + @Override + public boolean add(T tile) { + addFarthest(tile); + return true; + } + +} diff --git a/src/main/java/ru/windcorp/progressia/common/world/generic/LongBasedChunkSet.java b/src/main/java/ru/windcorp/progressia/common/world/generic/LongBasedChunkSet.java index cd5a755..11559ca 100644 --- a/src/main/java/ru/windcorp/progressia/common/world/generic/LongBasedChunkSet.java +++ b/src/main/java/ru/windcorp/progressia/common/world/generic/LongBasedChunkSet.java @@ -45,7 +45,7 @@ public class LongBasedChunkSet implements ChunkSet { addAll(copyFrom); } - public LongBasedChunkSet(TLongSet impl, GenericWorld copyFrom) { + public LongBasedChunkSet(TLongSet impl, GenericWorld copyFrom) { this(impl); addAllChunks(copyFrom.getChunks()); } diff --git a/src/main/java/ru/windcorp/progressia/common/world/generic/Util.java b/src/main/java/ru/windcorp/progressia/common/world/generic/Util.java index 8f6f79f..f3765d7 100644 --- a/src/main/java/ru/windcorp/progressia/common/world/generic/Util.java +++ b/src/main/java/ru/windcorp/progressia/common/world/generic/Util.java @@ -35,7 +35,7 @@ class Util { return hits; } - public static boolean testBiC(Vec3i blockInWorld, GenericChunk chunk, Predicate test) { + public static boolean testBiC(Vec3i blockInWorld, GenericChunk chunk, Predicate test) { Vec3i v = Vectors.grab3i(); v = Coordinates.getInWorld(chunk.getPosition(), Vectors.ZERO_3i, v); diff --git a/src/main/java/ru/windcorp/progressia/common/world/tile/TileReference.java b/src/main/java/ru/windcorp/progressia/common/world/tile/TileDataReference.java similarity index 71% rename from src/main/java/ru/windcorp/progressia/common/world/tile/TileReference.java rename to src/main/java/ru/windcorp/progressia/common/world/tile/TileDataReference.java index 75fde58..b9699c2 100644 --- a/src/main/java/ru/windcorp/progressia/common/world/tile/TileReference.java +++ b/src/main/java/ru/windcorp/progressia/common/world/tile/TileDataReference.java @@ -18,16 +18,10 @@ package ru.windcorp.progressia.common.world.tile; -public interface TileReference { +import ru.windcorp.progressia.common.world.ChunkData; +import ru.windcorp.progressia.common.world.block.BlockData; +import ru.windcorp.progressia.common.world.generic.GenericTileReference; - TileData get(); - - int getIndex(); - - TileDataStack getStack(); - - default boolean isValid() { - return get() != null; - } +public interface TileDataReference extends GenericTileReference { } diff --git a/src/main/java/ru/windcorp/progressia/common/world/tile/TileDataStack.java b/src/main/java/ru/windcorp/progressia/common/world/tile/TileDataStack.java index 80f0ba4..7e88402 100644 --- a/src/main/java/ru/windcorp/progressia/common/world/tile/TileDataStack.java +++ b/src/main/java/ru/windcorp/progressia/common/world/tile/TileDataStack.java @@ -20,147 +20,9 @@ package ru.windcorp.progressia.common.world.tile; import ru.windcorp.progressia.common.world.ChunkData; import ru.windcorp.progressia.common.world.block.BlockData; -import ru.windcorp.progressia.common.world.generic.GenericTileStack; +import ru.windcorp.progressia.common.world.generic.GenericWritableTileStack; public abstract class TileDataStack - extends GenericTileStack { - - /** - * Inserts the specified tile at the specified position in this stack. - * Shifts the tile currently at that position (if any) and any tiles above - * to - * the top (adds one to their indices). - * - * @param index index at which the specified tile is to be inserted - * @param tile tile to be inserted - * @throws TileStackIsFullException if this stack is {@linkplain #isFull() - * full} - */ - /* - * Impl note: AbstractList provides a useless implementation of this method, - * make sure to override it in subclass - */ - @Override - public abstract void add(int index, TileData tile); - - /** - * Adds the specified tile at the end of this stack assigning it the - * provided tag. - * This method is useful for copying stacks when preserving tags is - * necessary. - * - * @param tile the tile to add - * @param tag the tag to assign the new tile - * @throws IllegalArgumentException if this stack already contains a tile - * with the - * provided tag - */ - public abstract void load(TileData tile, int tag); - - /** - * Replaces the tile at the specified position in this stack with the - * specified tile. - * - * @param index index of the tile to replace - * @param tile tile to be stored at the specified position - * @return the tile previously at the specified position - */ - /* - * Impl note: AbstractList provides a useless implementation of this method, - * make sure to override it in subclass - */ - @Override - public abstract TileData set(int index, TileData tile); - - /** - * Removes the tile at the specified position in this list. Shifts any - * subsequent tiles - * to the left (subtracts one from their indices). Returns the tile that was - * removed - * from the list. - * - * @param index the index of the tile to be removed - * @return the tile previously at the specified position - */ - /* - * Impl note: AbstractList provides a useless implementation of this method, - * make sure to override it in subclass - */ - @Override - public abstract TileData remove(int index); - - public abstract TileReference getReference(int index); - - public abstract int getIndexByTag(int tag); - - public abstract int getTagByIndex(int index); - - /* - * Aliases and overloads - */ - - public void addClosest(TileData tile) { - add(0, tile); - } - - public void addFarthest(TileData tile) { - add(size(), tile); - } - - /** - * Attempts to {@link #add(int, TileData) add} the provided {@code tile} - * at {@code index}. If the stack is {@linkplain #isFull() full}, does - * nothing. - * - * @param index the index to insert the tile at - * @param tile the tile to try to add - * @return {@code true} iff this stack has changed - */ - public boolean offer(int index, TileData tile) { - if (isFull()) - return false; - add(index, tile); - return true; - } - - public boolean offerClosest(TileData tile) { - return offer(0, tile); - } - - public boolean offerFarthest(TileData tile) { - return offer(size(), tile); - } - - public TileData removeClosest() { - return remove(0); - } - - public TileData removeFarthest() { - return remove(size() - 1); - } - - public TileData poll(int index) { - if (size() <= index) - return null; - return remove(index); - } - - public TileData pollClosest() { - return poll(0); - } - - public TileData pollFarthest() { - return poll(size() - 1); - } - - @Override - public boolean add(TileData tile) { - addFarthest(tile); - return true; - } - - public BlockData getHost() { - return getChunk().getBlock(getBlockInChunk(null)); - } + extends GenericWritableTileStack { } diff --git a/src/main/java/ru/windcorp/progressia/server/world/ChunkLogic.java b/src/main/java/ru/windcorp/progressia/server/world/ChunkLogic.java index 2ee89d6..a5177fe 100644 --- a/src/main/java/ru/windcorp/progressia/server/world/ChunkLogic.java +++ b/src/main/java/ru/windcorp/progressia/server/world/ChunkLogic.java @@ -33,7 +33,7 @@ import ru.windcorp.progressia.common.world.rels.AbsFace; import ru.windcorp.progressia.common.world.rels.BlockFace; import ru.windcorp.progressia.common.world.rels.RelFace; import ru.windcorp.progressia.common.world.tile.TileDataStack; -import ru.windcorp.progressia.common.world.tile.TileReference; +import ru.windcorp.progressia.common.world.tile.TileDataReference; import ru.windcorp.progressia.server.world.block.BlockLogic; import ru.windcorp.progressia.server.world.block.BlockLogicRegistry; import ru.windcorp.progressia.server.world.block.TickableBlock; @@ -41,16 +41,17 @@ import ru.windcorp.progressia.server.world.tasks.TickChunk; import ru.windcorp.progressia.server.world.ticking.TickingPolicy; import ru.windcorp.progressia.server.world.tile.TickableTile; import ru.windcorp.progressia.server.world.tile.TileLogic; +import ru.windcorp.progressia.server.world.tile.TileLogicReference; import ru.windcorp.progressia.server.world.tile.TileLogicRegistry; import ru.windcorp.progressia.server.world.tile.TileLogicStack; -public class ChunkLogic implements GenericChunk { +public class ChunkLogic implements GenericChunk { private final WorldLogic world; private final ChunkData data; private final Collection tickingBlocks = new ArrayList<>(); - private final Collection tickingTiles = new ArrayList<>(); + private final Collection tickingTiles = new ArrayList<>(); private final TickChunk tickTask = new TickChunk(this); @@ -124,7 +125,7 @@ public class ChunkLogic implements GenericChunk action) { + public void forEachTickingTile(BiConsumer action) { tickingTiles.forEach(ref -> { action.accept( ref, @@ -138,7 +139,29 @@ public class ChunkLogic implements GenericChunk tileStack); + TileStack withTS(GenericTileStack tileStack); default Builder.Chunk withChunk(ChunkData chunk) { Objects.requireNonNull(chunk, "chunk"); return withChunk(chunk.getPosition()); } - default TileTickContext withTile(TileReference ref) { + default TileTickContext withTile(TileDataReference ref) { Objects.requireNonNull(ref, "ref"); return withTS(ref.getStack()).withLayer(ref.getIndex()); } @@ -259,7 +259,7 @@ public abstract class TickContextMutable implements BlockTickContext, TSTickCont } @Override - public TileStack withTS(GenericTileStack tileStack) { + public TileStack withTS(GenericTileStack tileStack) { Objects.requireNonNull(tileStack, "tileStack"); return withBlock( diff --git a/src/main/java/ru/windcorp/progressia/server/world/WorldLogic.java b/src/main/java/ru/windcorp/progressia/server/world/WorldLogic.java index 4111646..0b80b89 100644 --- a/src/main/java/ru/windcorp/progressia/server/world/WorldLogic.java +++ b/src/main/java/ru/windcorp/progressia/server/world/WorldLogic.java @@ -37,10 +37,11 @@ import ru.windcorp.progressia.server.world.generation.WorldGenerator; import ru.windcorp.progressia.server.world.tasks.TickEntitiesTask; import ru.windcorp.progressia.server.world.ticking.Evaluation; import ru.windcorp.progressia.server.world.tile.TileLogic; +import ru.windcorp.progressia.server.world.tile.TileLogicReference; import ru.windcorp.progressia.server.world.tile.TileLogicStack; public class WorldLogic - implements GenericWorld { diff --git a/src/main/java/ru/windcorp/progressia/server/world/tile/TileLogicReference.java b/src/main/java/ru/windcorp/progressia/server/world/tile/TileLogicReference.java new file mode 100644 index 0000000..97bf35c --- /dev/null +++ b/src/main/java/ru/windcorp/progressia/server/world/tile/TileLogicReference.java @@ -0,0 +1,27 @@ +/* + * Progressia + * Copyright (C) 2020-2021 Wind Corporation and contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package ru.windcorp.progressia.server.world.tile; + +import ru.windcorp.progressia.common.world.generic.GenericTileReference; +import ru.windcorp.progressia.server.world.ChunkLogic; +import ru.windcorp.progressia.server.world.block.BlockLogic; + +public interface TileLogicReference + extends GenericTileReference { + +} diff --git a/src/main/java/ru/windcorp/progressia/server/world/tile/TileLogicStack.java b/src/main/java/ru/windcorp/progressia/server/world/tile/TileLogicStack.java index 5e91963..b38a6f6 100644 --- a/src/main/java/ru/windcorp/progressia/server/world/tile/TileLogicStack.java +++ b/src/main/java/ru/windcorp/progressia/server/world/tile/TileLogicStack.java @@ -21,9 +21,10 @@ package ru.windcorp.progressia.server.world.tile; import ru.windcorp.progressia.common.world.generic.GenericTileStack; import ru.windcorp.progressia.common.world.tile.TileDataStack; import ru.windcorp.progressia.server.world.ChunkLogic; +import ru.windcorp.progressia.server.world.block.BlockLogic; public abstract class TileLogicStack - extends GenericTileStack { + extends GenericTileStack { public abstract TileDataStack getData(); diff --git a/src/main/java/ru/windcorp/progressia/server/world/tile/TileTickContext.java b/src/main/java/ru/windcorp/progressia/server/world/tile/TileTickContext.java index 45f41ec..7e7ae88 100644 --- a/src/main/java/ru/windcorp/progressia/server/world/tile/TileTickContext.java +++ b/src/main/java/ru/windcorp/progressia/server/world/tile/TileTickContext.java @@ -20,7 +20,7 @@ package ru.windcorp.progressia.server.world.tile; import ru.windcorp.progressia.common.world.tile.TileData; import ru.windcorp.progressia.common.world.tile.TileDataStack; -import ru.windcorp.progressia.common.world.tile.TileReference; +import ru.windcorp.progressia.common.world.tile.TileDataReference; public interface TileTickContext extends TSTickContext { @@ -53,7 +53,7 @@ public interface TileTickContext extends TSTickContext { return stack.get(getLayer()); } - default TileReference getReference() { + default TileDataReference getReference() { return getTDS().getReference(getLayer()); }