Browse Source

1000x more efficient cables, energy logic

master
Martmists 2 years ago
parent
commit
e8329d344c
  1. 61
      src/main/java/com/martmists/ynet/YNetMod.java
  2. 29
      src/main/java/com/martmists/ynet/blocks/CableBlock.java
  3. 28
      src/main/java/com/martmists/ynet/blocks/ConnectorBlock.java
  4. 2
      src/main/java/com/martmists/ynet/network/ConnectorConfiguration.java
  5. 90
      src/main/java/com/martmists/ynet/network/Network.java

61
src/main/java/com/martmists/ynet/YNetMod.java

@ -12,6 +12,7 @@ import com.martmists.ynet.event.ProviderTickCallback; @@ -12,6 +12,7 @@ import com.martmists.ynet.event.ProviderTickCallback;
import com.martmists.ynet.network.ConnectorConfiguration;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.client.itemgroup.FabricItemGroupBuilder;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.block.Block;
import net.minecraft.block.Material;
import net.minecraft.block.entity.BlockEntityType;
@ -24,8 +25,10 @@ import net.minecraft.util.Identifier; @@ -24,8 +25,10 @@ import net.minecraft.util.Identifier;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.registry.Registry;
import net.minecraft.util.registry.SimpleRegistry;
import net.minecraft.world.World;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
@ -65,14 +68,58 @@ public class YNetMod implements ModInitializer { @@ -65,14 +68,58 @@ public class YNetMod implements ModInitializer {
// - Find a way to route all items (+ filters?)
});
register("ynet:energy", EnergyProvider.class, (listeners, be) -> {
// TODO:
// - Get matching channels from ControllerBlockEntity
// - Find a way to route all energy
Set<ConnectorConfiguration> takeEnergy = listeners.stream().filter(config -> config.state == ConnectorConfiguration.State.INPUT).collect(Collectors.toSet());
Set<ConnectorConfiguration> giveEnergy = listeners.stream().filter(config -> config.state == ConnectorConfiguration.State.OUTPUT).collect(Collectors.toSet());
});
if (FabricLoader.getInstance().isModLoaded("techreborn")){
// Enable energy
register("ynet:energy", EnergyProvider.class, (listeners, be) -> {
// TODO:
// - Get matching channels from ControllerBlockEntity
// - Find a way to route all energy
World world = be.getWorld();
List<ConnectorConfiguration> takeEnergy = listeners.stream()
.filter(config -> config.state == ConnectorConfiguration.State.INPUT)
.sorted((a, b) -> a.priority > b.priority ? 0 : 1)
.collect(Collectors.toList());
List<ConnectorConfiguration> giveEnergy = listeners.stream()
.filter(config -> config.state == ConnectorConfiguration.State.OUTPUT)
.sorted((a, b) -> a.priority > b.priority ? 0 : 1)
.collect(Collectors.toList());
double lastStored = 0.0;
double lastTaken = 0.0;
while (!takeEnergy.isEmpty() && !giveEnergy.isEmpty()) {
ConnectorConfiguration receiverConfig = takeEnergy.get(0);
ConnectorConfiguration providerConfig = giveEnergy.get(0);
EnergyProvider receiver = (EnergyProvider) world.getBlockState(receiverConfig.providerPos);
EnergyProvider provider = (EnergyProvider) world.getBlockState(providerConfig.providerPos);
if (lastStored >= receiver.getEnergyInputLimit(world, receiverConfig.providerPos)) {
lastStored = 0.0;
receiverConfig = takeEnergy.get(1);
receiver = (EnergyProvider) world.getBlockState(receiverConfig.providerPos);
takeEnergy.remove(0);
}
if (lastTaken >= provider.getEnergyOutputLimit(world, providerConfig.providerPos)) {
lastTaken = 0.0;
providerConfig = giveEnergy.get(1);
provider = (EnergyProvider) world.getBlockState(providerConfig.providerPos);
giveEnergy.remove(0);
}
double toTransfer = Math.min(
receiver.getEnergyInputLimit(world, receiverConfig.providerPos),
provider.getEnergyOutputLimit(world, providerConfig.providerPos));
provider.outputEnergy(world, providerConfig.providerPos, toTransfer);
receiver.inputEnergy(world, receiverConfig.providerPos, toTransfer);
lastStored += toTransfer;
lastTaken += toTransfer;
}
});
}
// TODO:
// - Add support for configuring redstone signals on connectors
}

29
src/main/java/com/martmists/ynet/blocks/CableBlock.java

@ -58,36 +58,13 @@ public class CableBlock extends ConnectingBlock { @@ -58,36 +58,13 @@ public class CableBlock extends ConnectingBlock {
}
@Override
public void onBreak(World world, BlockPos pos, BlockState state, PlayerEntity player) {
super.onBreak(world, pos, state, player);
Set<BlockPos> controllers = new HashSet<>();
// No longer connected, check all neighbors
Network.getConnectedControllers(world, pos, controllers);
System.out.println("Controllers: " + controllers); // Empty?
for (BlockPos p : controllers){
ControllerBlockEntity be = (ControllerBlockEntity)world.getBlockEntity(p);
be.updateNetwork();
}
public void onBroken(IWorld world, BlockPos pos, BlockState state) {
Network.removeCable(world, pos);
}
@Override
public void onPlaced(World world, BlockPos pos, BlockState state, LivingEntity placer, ItemStack itemStack) {
Set<BlockPos> controllers = new HashSet<>();
// TODO: Find a better way to do this instead of a BFS through the world
Network.getConnectedControllers(world, pos, controllers);
System.out.println("Controllers: " + controllers);
for (BlockPos p : controllers){
ControllerBlockEntity be = (ControllerBlockEntity)world.getBlockEntity(p);
// be.network.cables.add(p);
Set<BlockPos> known = new HashSet<>();
known.addAll(be.network.cables);
known.addAll(be.network.connectors);
Network.getConnectedBlocks(world, pos, known, be.network.cables, be.network.connectors);
}
Network.addCable(world, pos);
}
public BlockState withConnectionProperties(BlockView world, BlockPos pos) {

28
src/main/java/com/martmists/ynet/blocks/ConnectorBlock.java

@ -118,35 +118,13 @@ public class ConnectorBlock extends ConnectingBlock implements BlockEntityProvid @@ -118,35 +118,13 @@ public class ConnectorBlock extends ConnectingBlock implements BlockEntityProvid
}
@Override
public void onBreak(World world, BlockPos pos, BlockState state, PlayerEntity player) {
super.onBreak(world, pos, state, player);
Set<BlockPos> controllers = new HashSet<>();
Network.getConnectedControllers(world, pos, controllers);
System.out.println("Controllers: " + controllers); // Empty?
for (BlockPos p : controllers){
ControllerBlockEntity be = (ControllerBlockEntity)world.getBlockEntity(p);
be.updateNetwork();
}
public void onBroken(IWorld world, BlockPos pos, BlockState state) {
Network.removeConnector(world, pos);
}
@Override
public void onPlaced(World world, BlockPos pos, BlockState state, LivingEntity placer, ItemStack itemStack) {
Set<BlockPos> controllers = new HashSet<>();
// TODO: Find a better way to do this instead of a BFS through the world
Network.getConnectedControllers(world, pos, controllers);
System.out.println("Controllers: " + controllers);
for (BlockPos p : controllers){
ControllerBlockEntity be = (ControllerBlockEntity)world.getBlockEntity(p);
// be.network.connectors.add(p);
Set<BlockPos> known = new HashSet<>();
known.addAll(be.network.cables);
known.addAll(be.network.connectors);
Network.getConnectedBlocks(world, pos, known, be.network.cables, be.network.connectors);
}
Network.addConnector(world, pos);
}
public int getRedstoneOutput(Direction facing, BlockView world, BlockPos pos){

2
src/main/java/com/martmists/ynet/network/ConnectorConfiguration.java

@ -7,6 +7,8 @@ public class ConnectorConfiguration { @@ -7,6 +7,8 @@ public class ConnectorConfiguration {
public State state = State.DISABLED;
public BlockPos providerPos;
public int priority;
// For items
public Item[] filter;
public static enum State {

90
src/main/java/com/martmists/ynet/network/Network.java

@ -10,11 +10,77 @@ import net.minecraft.world.BlockView; @@ -10,11 +10,77 @@ import net.minecraft.world.BlockView;
import java.util.*;
public class Network {
private static Map<BlockPos, Network> networks = new HashMap<>();
private BlockPos controller;
public Set<BlockPos> cables;
public Set<BlockPos> connectors;
public void reloadAllNodes(BlockView world) {
public synchronized static void removeCable(BlockView world, BlockPos p) {
for (Map.Entry<BlockPos, Network> e : networks.entrySet()) {
Network n = e.getValue();
if (n.cables.contains(p)) {
System.out.println("Reloading nodes for network " + n);
n.reloadAllNodes(world);
System.out.println("Done reloading nodes");
}
}
}
public synchronized static void removeConnector(BlockView world, BlockPos p) {
for (Map.Entry<BlockPos, Network> e : networks.entrySet()) {
Network n = e.getValue();
if (n.connectors.contains(p)) {
System.out.println("Reloading nodes for network " + n);
n.reloadAllNodes(world);
System.out.println("Done reloading nodes");
}
}
}
public synchronized static void addCable(BlockView world, BlockPos p) {
Set<Network> connectedNetworks = new HashSet<>();
for (BlockPos pos : new BlockPos[]{ p.up(), p.down(), p.north(), p.east(), p.south(), p.west() }){
for (Map.Entry<BlockPos, Network> e : networks.entrySet()){
Network n = e.getValue();
if (n.connectors.contains(pos) || n.cables.contains(pos)){
connectedNetworks.add(n);
}
}
}
for (Network n : connectedNetworks) {
n.cables.add(p);
Set<BlockPos> known = new HashSet<>();
known.addAll(n.cables);
known.addAll(n.connectors);
System.out.println("Loading new nodes for network " + n);
getConnectedBlocks(world, p, known, n.cables, n.connectors);
System.out.println("Done reloading nodes");
}
}
public synchronized static void addConnector(BlockView world, BlockPos p) {
Set<Network> connectedNetworks = new HashSet<>();
for (BlockPos pos : new BlockPos[]{ p.up(), p.down(), p.north(), p.east(), p.south(), p.west() }){
for (Map.Entry<BlockPos, Network> e : networks.entrySet()){
Network n = e.getValue();
if (n.connectors.contains(pos) || n.cables.contains(pos)){
connectedNetworks.add(n);
}
}
}
for (Network n : connectedNetworks) {
n.connectors.add(p);
Set<BlockPos> known = new HashSet<>();
known.addAll(n.cables);
known.addAll(n.connectors);
System.out.println("Loading new nodes for network " + n);
getConnectedBlocks(world, p, known, n.cables, n.connectors);
System.out.println("Done reloading nodes");
}
}
public synchronized void reloadAllNodes(BlockView world) {
cables = new HashSet<>();
connectors = new HashSet<>();
ControllerBlockEntity be = ((ControllerBlockEntity)world.getBlockEntity(controller));
@ -57,29 +123,9 @@ public class Network { @@ -57,29 +123,9 @@ public class Network {
}
}
public static void getConnectedControllers(BlockView world, BlockPos origin, Set<BlockPos> controllers) {
ArrayDeque<BlockPos> toSearch = new ArrayDeque<>();
toSearch.push(origin);
List<BlockPos> searched = new ArrayList<>();
while (!toSearch.isEmpty()){
BlockPos p = toSearch.removeFirst();
for (BlockPos p2 : Arrays.asList(p.up(), p.down(), p.north(), p.south(), p.east(), p.west())) {
if (searched.contains(p2)) {
continue;
}
searched.add(p2);
Block b = world.getBlockState(p2).getBlock();
if (b == YNetMod.CABLE || b == YNetMod.CONNECTOR) {
toSearch.add(p2);
} else if (b == YNetMod.CONTROLLER) {
controllers.add(p2);
}
}
}
}
public void setController(BlockPos pos) {
controller = pos;
networks.put(pos, this);
}
public Set<BlockPos> getProviders(BlockView world) {

Loading…
Cancel
Save