From 8089051c009ca0bdbc8452000896810e9c966911 Mon Sep 17 00:00:00 2001 From: Curle Date: Fri, 30 Jun 2023 17:39:09 +0100 Subject: [PATCH] coolant improvements --- src/main/java/uk/gemwire/engage/Engage.java | 11 ++++++ .../block/coolant/CoolantBaseBlock.java | 39 ++++++++++--------- 2 files changed, 32 insertions(+), 18 deletions(-) diff --git a/src/main/java/uk/gemwire/engage/Engage.java b/src/main/java/uk/gemwire/engage/Engage.java index fddcf68..6041692 100644 --- a/src/main/java/uk/gemwire/engage/Engage.java +++ b/src/main/java/uk/gemwire/engage/Engage.java @@ -2,13 +2,18 @@ package uk.gemwire.engage; import net.minecraftforge.eventbus.api.IEventBus; import net.minecraftforge.fml.common.Mod; +import net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent; import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext; +import uk.gemwire.engage.block.coolant.CoolantBaseBlock; import uk.gemwire.engage.registries.Blocks; import uk.gemwire.engage.registries.Fluids; import uk.gemwire.engage.registries.Items; import uk.gemwire.engage.registries.Worldgen; import uk.gemwire.engage.registries.fluid.FluidDeferredRegister; +import java.util.List; +import java.util.Set; + /** * Engage adds four varieties of Warp Core for power generation, based on: * - The Defiant @@ -62,5 +67,11 @@ public class Engage { Fluids.FLUIDS_REGISTRY.register(bus); Worldgen.CARVER_REGISTRY.register(bus); Worldgen.BIOME_MODIFIER_SERIALIZERS.register(bus); + + bus.addListener(this::commonSetup); + } + + public void commonSetup(FMLCommonSetupEvent e) { + e.enqueueWork(() -> CoolantBaseBlock.VALID_COOLANT_BLOCKS = Set.of(Blocks.COMPRESSOR_BLOCK.block().get(), Blocks.COPPER_TUBE_BLOCK.block().get(), Blocks.COOLANT_METERING_BLOCK.block().get(), Blocks.COOLANT_HEAT_SPREADER_BLOCK.block().get())); } } diff --git a/src/main/java/uk/gemwire/engage/block/coolant/CoolantBaseBlock.java b/src/main/java/uk/gemwire/engage/block/coolant/CoolantBaseBlock.java index ee99b8c..f0cfbf6 100644 --- a/src/main/java/uk/gemwire/engage/block/coolant/CoolantBaseBlock.java +++ b/src/main/java/uk/gemwire/engage/block/coolant/CoolantBaseBlock.java @@ -17,7 +17,7 @@ import java.util.*; public class CoolantBaseBlock extends Block { static Logger LOGGER = LogManager.getLogger(); - static Set VALID_COOLANT_BLOCKS = Set.of(Blocks.COMPRESSOR_BLOCK.block().get(), Blocks.COPPER_TUBE_BLOCK.block().get(), Blocks.COOLANT_HEAT_SPREADER_BLOCK.block().get(), Blocks.COOLANT_METERING_BLOCK.block().get()); + public static Set VALID_COOLANT_BLOCKS; public CoolantBaseBlock(Properties p_49795_) { super(p_49795_); @@ -29,14 +29,14 @@ public class CoolantBaseBlock extends Block { long startTime = System.currentTimeMillis(); - searchForValidNetwork(new BlockPos.MutableBlockPos(pos.getX(), pos.getY(), pos.getZ()), level); + //searchForValidNetwork(new BlockPos.MutableBlockPos(pos.getX(), pos.getY(), pos.getZ()), level); long endTime = System.currentTimeMillis(); LOGGER.info("Discovery took " + (endTime - startTime) + "ms"); } - private Set getValidTransitionsFrom(NetworkState state, Level level) { - Set dirs = new HashSet<>(); + private Queue getValidTransitionsFrom(NetworkState state, Level level) { + Queue dirs = new ArrayDeque<>(); for (Direction dir : Direction.values()) { if (state.lastTransition != null && dir == state.lastTransition) continue; BlockPos newPos = state.currentPos.relative(dir); @@ -75,7 +75,7 @@ public class CoolantBaseBlock extends Block { while(!queue.isEmpty()) { // The search loop - search(queue.element(), level, queue); + search(level, queue); // Try to finalise stuff - we gotta have everything in our network. // First, check that the last remaining direction takes us into a block we've already visited (the closed loop) @@ -93,37 +93,40 @@ public class CoolantBaseBlock extends Block { } } } + queue.poll(); } LOGGER.info("Valid coolant loop!"); return true; } - void search(StateTransitions st, Level level, Queue queue) { - while (st.transitions.size() > 1) { + void search(Level level, Queue queue) { + StateTransitions currentState = queue.peek(); + while (currentState.transitions.size() > 1) { // Traverse the new block - Transition transition = st.transitions.iterator().next(); - st.state.lastTransition = transition.dir(); - st.state.currentPos = transition.pos(); + Transition transition = currentState.transitions.iterator().next(); + currentState.state.lastTransition = transition.dir(); + currentState.state.currentPos = transition.pos(); Block reachedBlock = transition.state().getBlock(); - LOGGER.info("Traversing through " + st.state.lastTransition + " to " + st.state.currentPos + ", which is a " + getBlockName(reachedBlock)); - st.state.visited.add(st.state.currentPos); + LOGGER.info("Traversing through " + currentState.state.lastTransition + " to " + currentState.state.currentPos + ", which is a " + getBlockName(reachedBlock)); + currentState.state.visited.add(currentState.state.currentPos); // If we reached a metering block for the first time if (reachedBlock == Blocks.COOLANT_METERING_BLOCK.block().get()) { - st.state.metering(); + currentState.state.metering(); // The compressor has its own things to check.. } else if (reachedBlock == Blocks.COMPRESSOR_BLOCK.block().get()) { - st.state.compressor(); + currentState.state.compressor(); // Heat spreaders may have many, so we just flip the flag to be true if we're just getting to one now. } else if (reachedBlock == Blocks.COOLANT_HEAT_SPREADER_BLOCK.block().get()) { - if (!st.state.spreader) st.state.spreader = true; + if (!currentState.state.spreader) currentState.state.spreader = true; } - Set newTransitions = getValidTransitionsFrom(st.state, level); - queue.add(new StateTransitions(st.state.copy(), newTransitions)); + Queue newTransitions = getValidTransitionsFrom(currentState.state, level); + queue.add(new StateTransitions(currentState.state.copy(), newTransitions)); + currentState = queue.peek(); } } @@ -187,5 +190,5 @@ public class CoolantBaseBlock extends Block { } } - record StateTransitions(NetworkState state, Set transitions) {} + record StateTransitions(NetworkState state, Queue transitions) {} }