Skip to content

Commit

Permalink
Add max_complexity network config option
Browse files Browse the repository at this point in the history
  • Loading branch information
NichtStudioCode committed May 19, 2024
1 parent 52a6149 commit 300b851
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class EnergyNetwork(networkData: NetworkData<EnergyNetwork>) : Network<EnergyNet
private val providers = HashSet<EnergyHolder>()
private val consumers = HashSet<EnergyHolder>()
private val buffers = HashSet<EnergyHolder>()
private val complexity: Int

private val transferRate: Long
private val availableProviderEnergy: Long
Expand All @@ -31,6 +32,7 @@ class EnergyNetwork(networkData: NetworkData<EnergyNetwork>) : Network<EnergyNet

init {
var transferRate = DEFAULT_TRANSFER_RATE
var complexity = 0

for ((node, faces) in networkData.nodes.values) {
if (node is NetworkEndPoint) {
Expand All @@ -52,18 +54,24 @@ class EnergyNetwork(networkData: NetworkData<EnergyNetwork>) : Network<EnergyNet
NetworkConnectionType.EXTRACT -> providers += energyHolder
else -> throw IllegalArgumentException("Invalid connection config for $energyHolder")
}

complexity++
} else if (node is EnergyBridge) {
transferRate = min(transferRate, node.energyTransferRate)
}
}

this.transferRate = transferRate
this.complexity = complexity
}

/**
* Called every tick to transfer energy.
*/
fun tick() {
if (MAX_COMPLEXITY != -1 && complexity > MAX_COMPLEXITY)
return

val providerEnergy = min(transferRate, availableProviderEnergy)
val bufferEnergy = min(transferRate - providerEnergy, availableBufferEnergy)
val requestedEnergy = min(transferRate, requestedConsumerEnergy)
Expand Down Expand Up @@ -154,6 +162,7 @@ class EnergyNetwork(networkData: NetworkData<EnergyNetwork>) : Network<EnergyNet
val DEFAULT_TRANSFER_RATE: Long by combinedProvider(ENERGY_NETWORK.entry<Double>("default_transfer_rate"), TICK_DELAY_PROVIDER)
.map { (defaultTransferRate, tickDelay) -> (defaultTransferRate * tickDelay).roundToLong() }
.map { defaultTransferRate -> if (defaultTransferRate < 0) Long.MAX_VALUE else defaultTransferRate }
val MAX_COMPLEXITY: Int by ENERGY_NETWORK.entry<Int>("max_complexity")

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ class FluidNetwork(networkData: NetworkData<FluidNetwork>) : Network<FluidNetwor

private val channels: Array<FluidNetworkChannel?> = arrayOfNulls(CHANNEL_AMOUNT)
private val transferRate: Long
private val complexity: Int

private var nextChannel = 0

init {
var transferRate = DEFAULT_TRANSFER_RATE
var complexity = 0

for ((node, faces) in networkData.nodes.values) {
if (node is NetworkEndPoint) {
Expand All @@ -36,18 +38,24 @@ class FluidNetwork(networkData: NetworkData<FluidNetwork>) : Network<FluidNetwor
channel.addHolder(fluidHolder, face)
}
}

complexity++
} else if (node is FluidBridge) {
transferRate = min(transferRate, node.fluidTransferRate)
}
}

this.transferRate = transferRate
this.complexity = complexity

for (channel in channels)
channel?.createDistributor()
}

fun tick() {
if (MAX_COMPLEXITY != -1 && complexity > MAX_COMPLEXITY)
return

val startingChannel = nextChannel
var amountLeft = transferRate
do {
Expand All @@ -66,6 +74,7 @@ class FluidNetwork(networkData: NetworkData<FluidNetwork>) : Network<FluidNetwor
.map { (defaultTransferRate, tickDelay) -> (defaultTransferRate * tickDelay).roundToLong() }
.map { defaultTransferRate -> if (defaultTransferRate < 0) Long.MAX_VALUE else defaultTransferRate }
val CHANNEL_AMOUNT: Int by FLUID_NETWORK.entry<Int>("channel_amount")
val MAX_COMPLEXITY: Int by FLUID_NETWORK.entry<Int>("max_complexity")

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,35 @@ class ItemNetwork(networkData: NetworkData<ItemNetwork>) : Network<ItemNetwork>,

internal val channels: Array<ItemDistributor?>
private val transferRate: Int
val complexity: Int

private var nextChannel = 0

init {
var transferRate = DEFAULT_TRANSFER_RATE
var complexity = 0
val channelsBuilder = ItemChannelsBuilder()
for ((node, faces) in nodes.values) {
if (node is NetworkEndPoint) {
val itemHolder = node.holders.firstInstanceOfOrNull<ItemHolder>()
?: continue

channelsBuilder.addHolder(itemHolder, faces)
complexity++
} else if (node is ItemBridge) {
transferRate = min(transferRate, node.itemTransferRate)
}
}

this.transferRate = transferRate
this.complexity = complexity
channels = channelsBuilder.build()
}

internal fun tick() {
if (MAX_COMPLEXITY != -1 && complexity > MAX_COMPLEXITY)
return

val startingChannel = nextChannel
var transfersLeft = transferRate
do {
Expand All @@ -60,6 +68,7 @@ class ItemNetwork(networkData: NetworkData<ItemNetwork>) : Network<ItemNetwork>,
.map { (defaultTransferRate, tickDelay) -> (defaultTransferRate * tickDelay).roundToInt() }
.map { defaultTransferRate -> if (defaultTransferRate < 0) Int.MAX_VALUE else defaultTransferRate }
val CHANNEL_AMOUNT: Int by ITEM_NETWORK.entry<Int>("channel_amount")
val MAX_COMPLEXITY: Int by ITEM_NETWORK.entry<Int>("max_complexity")

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package xyz.xenondevs.nova.tileentity.network.type.item
import net.minecraft.world.item.ItemStack
import xyz.xenondevs.nova.tileentity.network.NetworkGroup
import xyz.xenondevs.nova.tileentity.network.NetworkGroupData
import xyz.xenondevs.nova.tileentity.network.type.item.ItemNetwork.Companion.MAX_COMPLEXITY
import xyz.xenondevs.nova.tileentity.network.type.item.channel.FilteredNetworkedInventory
import xyz.xenondevs.nova.tileentity.network.type.item.inventory.NetworkedInventory
import xyz.xenondevs.nova.util.RoundRobinCounter
Expand All @@ -13,6 +14,7 @@ internal class ItemNetworkGroup(data: NetworkGroupData<ItemNetwork>) : NetworkGr

private val providerSnapshots = HashMap<NetworkedInventory, Array<ItemStack>>()
private val filteredProviderSnapshots = HashMap<FilteredNetworkedInventory, Array<ItemStack>>()
private var hasSnapshot = false

private val roundRobin = RoundRobinCounter(networks.size)

Expand Down Expand Up @@ -54,10 +56,19 @@ internal class ItemNetworkGroup(data: NetworkGroupData<ItemNetwork>) : NetworkGr
}

override fun preTick() {
if (MAX_COMPLEXITY != -1 && networks.all { it.complexity > MAX_COMPLEXITY }) {
hasSnapshot = false
return
}

takeSnapshot()
hasSnapshot = true
}

override fun tick() {
if (!hasSnapshot)
return

val startIdx = roundRobin.next()
for (i in startIdx..<networks.size) {
networks[i].tick()
Expand Down
6 changes: 6 additions & 0 deletions nova/src/main/resources/configs/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -123,13 +123,17 @@ network:
energy:
# The delay in-between energy network ticks, in game ticks.
tick_delay: 1
# The maximum amount of end points in energy networks. Networks with more end points will not be ticked. (-1 for unlimited)
max_complexity: -1
# The default transfer rate for energy networks, in energy units per game tick. (-1 for unlimited)
# This is scaled with the network tick delay, so that the actual amount of energy transferred per second stays the same.
default_transfer_rate: -1
# Settings affecting nova:item networks.
item:
# The delay in-between item network ticks, in game ticks.
tick_delay: 20
# The maximum amount of end points in item networks. Networks with more end points will not be ticked. (-1 for unlimited)
max_complexity: -1
# The default transfer rate for item networks, in items per game tick. (-1 for unlimited)
# This is scaled with the network tick delay, so that the actual amount of items transferred per second stays the same.
default_transfer_rate: -1
Expand All @@ -139,6 +143,8 @@ network:
fluid:
# The delay in-between fluid network ticks, in game ticks.
tick_delay: 1
# The maximum amount of end points in fluid networks. Networks with more end points will not be ticked. (-1 for unlimited)
max_complexity: -1
# The default transfer rate for fluid networks, in fluid units per game tick. (-1 for unlimited)
# This is scaled with the network tick delay, so that the actual amount of fluid transferred per second stays the same.
default_transfer_rate: -1
Expand Down

0 comments on commit 300b851

Please sign in to comment.