Cache Torus.generate result for Origin (#2845)

* Cache Torus.generate result for Origin

Fusion Coils are laggy, not by ticking block entities, just purely by torus.generate(BlockPos.origin, size)

* Assign TorusListCache

* use cached result
This commit is contained in:
AngelBottomless 2022-03-05 23:17:16 +09:00 committed by GitHub
parent 40f4785816
commit 7f415d3ec9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 4 deletions

View file

@ -24,8 +24,10 @@
package reborncore.common.util;
import com.google.common.collect.ImmutableList;
import it.unimi.dsi.fastutil.ints.Int2IntMap;
import it.unimi.dsi.fastutil.ints.Int2IntOpenHashMap;
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
import net.minecraft.util.math.BlockPos;
import java.util.ArrayList;
@ -37,7 +39,7 @@ import java.util.concurrent.TimeUnit;
public class Torus {
private static final ExecutorService GEN_EXECUTOR = Executors.newSingleThreadExecutor();
private static Int2IntMap torusSizeCache;
private static Int2ObjectOpenHashMap<ImmutableList<BlockPos>> torusListCache;
public static List<BlockPos> generate(BlockPos origin, int radius) {
List<BlockPos> posLists = new ArrayList<>();
for (int x = -radius; x < radius; x++) {
@ -49,9 +51,12 @@ public class Torus {
}
}
}
return posLists;
}
public static List<BlockPos> getOriginPositions(int radius){
return torusListCache.get(radius);
}
public static void genSizeMap(int maxRadius) {
if (torusSizeCache != null) {
// Let's not do this again
@ -61,21 +66,24 @@ public class Torus {
final int sizeToCompute = maxRadius + 10;
torusSizeCache = new Int2IntOpenHashMap(sizeToCompute);
torusListCache = new Int2ObjectOpenHashMap<>(sizeToCompute);
for (int i = 0; i < sizeToCompute; i++) {
final int radius = i;
GEN_EXECUTOR.submit(() -> {
int size = 0;
ArrayList<BlockPos> list = new ArrayList<>();
for (int x = -radius; x < radius; x++) {
for (int y = -radius; y < radius; y++) {
for (int z = -radius; z < radius; z++) {
if (Math.pow(radius / 2 - Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2)), 2) + Math.pow(z, 2) < Math.pow(radius * 0.05, 2)) {
size++;
list.add(new BlockPos(x,z,y));
}
}
}
}
torusSizeCache.put(radius, size);
torusListCache.put(radius, ImmutableList.copyOf(list));
});
}

View file

@ -391,7 +391,7 @@ public class FusionControlComputerBlockEntity extends GenericMachineBlockEntity
@Override
public void writeMultiblock(MultiblockWriter writer) {
BlockState coil = TRContent.Machine.FUSION_COIL.block.getDefaultState();
Torus.generate(BlockPos.ORIGIN, size).forEach(pos -> writer.add(pos.getX(), pos.getY(), pos.getZ(), coil));
Torus.getOriginPositions(size).forEach(pos -> writer.add(pos.getX(), pos.getY(), pos.getZ(), coil));
}
@Override