Compare commits

..

No commits in common. "d17e64f33516bbd2f73e484728f84f5dfe3ec0d3" and "45181c9e55641ce130fe0ffc43a0f39f224da71e" have entirely different histories.

3 changed files with 107 additions and 136 deletions

View File

@ -22,6 +22,7 @@ import ru.dbotthepony.mc.otm.capability.matter.MatterStorageImpl
import ru.dbotthepony.mc.otm.capability.matter.ProfiledMatterStorage
import ru.dbotthepony.mc.otm.config.MachinesConfig
import ru.dbotthepony.mc.otm.container.UpgradeContainer
import ru.dbotthepony.mc.otm.container.slotted.ContainerSlot
import ru.dbotthepony.mc.otm.container.slotted.FilteredContainerSlot
import ru.dbotthepony.mc.otm.container.slotted.SlottedContainer
import ru.dbotthepony.mc.otm.menu.matter.MatterBottlerMenu
@ -34,7 +35,7 @@ import java.util.function.BooleanSupplier
class MatterBottlerBlockEntity(blockPos: BlockPos, blockState: BlockState) :
MatteryPoweredBlockEntity(MBlockEntities.MATTER_BOTTLER, blockPos, blockState) {
val upgrades = UpgradeContainer(3, UpgradeType.BASIC_MATTER, BooleanSupplier { this.blockState.getValue(WorkerState.SEMI_WORKER_STATE) !== WorkerState.IDLE }, ::markDirtyFast)
val upgrades = UpgradeContainer(3, UpgradeType.BASIC_MATTER, BooleanSupplier { blockState.getValue(WorkerState.SEMI_WORKER_STATE) !== WorkerState.IDLE }, ::markDirtyFast)
override val energy = ProfiledEnergyStorage(WorkerEnergyStorage(this::markDirtyFast, upgrades.transform(MachinesConfig.MatterBottler.VALUES)))
val energyConfig = ConfigurableEnergy(energy)
@ -140,7 +141,7 @@ class MatterBottlerBlockEntity(blockPos: BlockPos, blockState: BlockState) :
}
if (state !== blockState) {
level.setBlock(blockPos, state, Block.UPDATE_ALL)
level.setBlock(blockPos, state, Block.UPDATE_CLIENTS)
}
}
@ -153,143 +154,18 @@ class MatterBottlerBlockEntity(blockPos: BlockPos, blockState: BlockState) :
}
private val workerState by countingLazy(blockStateChangesCounter) {
this.blockState.getValue(WorkerState.SEMI_WORKER_STATE)
blockState.getValue(WorkerState.SEMI_WORKER_STATE)
}
private fun blockstateToWorking() {
if (workerState !== WorkerState.WORKING) {
level?.setBlock(blockPos, blockState.setValue(WorkerState.SEMI_WORKER_STATE, WorkerState.WORKING), Block.UPDATE_ALL)
level?.setBlock(blockPos, blockState.setValue(WorkerState.SEMI_WORKER_STATE, WorkerState.WORKING), Block.UPDATE_CLIENTS)
}
}
private fun blockstateToIdle() {
if (workerState !== WorkerState.IDLE) {
level?.setBlock(blockPos, blockState.setValue(WorkerState.SEMI_WORKER_STATE, WorkerState.IDLE), Block.UPDATE_ALL)
}
}
private fun tickBottling() {
var isWorking = false
var hasCapacitors = false
for (slot in bottling.slotIterator()) {
val item = slot.item
val capability = item.getCapability(MatteryCapability.MATTER_ITEM) ?: continue
if (!capability.receiveMatterChecked(Decimal.LONG_MAX_VALUE, true).isPositive) {
unbottling.consumeItem(item, false)
slot.setChanged()
continue
}
hasCapacitors = true
initialCapacity = initialCapacity ?: capability.storedMatter
val rate = MachinesConfig.MatterBottler.RATE * (1.0 + upgrades.speedBonus)
val energyRate = MachinesConfig.MatterBottler.VALUES.energyConsumption * (1.0 + upgrades.speedBonus)
if (matter.storedMatter < rate) {
val toExtract = matter.missingMatter
.coerceAtMost(rate * 200)
.coerceAtMost(capability.missingMatter - matter.storedMatter)
matter.receiveMatter(matterNode.graph.extractMatter(toExtract, false), false)
}
if (matter.storedMatter.isPositive) {
val energyRatio = if (energyRate <= Decimal.ZERO) Decimal.ONE else energy.extractEnergy(energyRate, true) / energyRate
val matterRatio = matter.extractMatter(capability.receiveMatter(rate.coerceAtMost(matter.storedMatter), true), true) / rate
val minRatio = minOf(matterRatio, energyRatio)
if (minRatio > Decimal.ZERO) {
isWorking = true
energy.extractEnergy(energyRate * minRatio, false)
matter.extractMatter(capability.receiveMatter(rate * minRatio, false), false)
workProgress = ((capability.storedMatter - initialCapacity!!) / capability.maxStoredMatter).toFloat()
slot.setChanged()
}
} else {
if (spitItemsWhenCantWork) {
unbottling.consumeItem(item, false)
slot.setChanged()
}
}
break
}
if (isWorking) {
blockstateToWorking()
} else {
blockstateToIdle()
}
if (!hasCapacitors) {
matter.extractMatter(matterNode.graph.receiveMatter(matter.storedMatter, false), false)
initialCapacity = null
workProgress = 0f
}
}
private fun tickUnbottling() {
matter.extractMatter(matterNode.graph.receiveMatter(matter.storedMatter, false), false)
if (!matter.missingMatter.isPositive) {
if (spitItemsWhenCantWork) {
for (slot in unbottling.slotIterator()) {
bottling.consumeItem(slot.item, false)
slot.setChanged()
}
}
blockstateToIdle()
return
}
var any = false
var hasCapacitors = false
for (slot in unbottling.slotIterator()) {
val item = slot.item
val it = item.getCapability(MatteryCapability.MATTER_ITEM) ?: continue
if (!it.extractMatterChecked(Decimal.LONG_MAX_VALUE, true).isPositive) {
bottling.consumeItem(item, false)
slot.setChanged()
continue
}
initialCapacity = initialCapacity ?: it.storedMatter
hasCapacitors = true
val rate = MachinesConfig.MatterBottler.RATE * (1.0 + upgrades.speedBonus)
val energyRate = MachinesConfig.MatterBottler.VALUES.energyConsumption * (1.0 + upgrades.speedBonus)
val energyRatio = if (energyRate <= Decimal.ZERO) Decimal.ONE else energy.extractEnergy(energyRate, true) / energyRate
val matterRatio = matter.receiveMatter(it.extractMatterChecked(rate, true), true) / rate
val minRatio = minOf(energyRatio, matterRatio)
if (minRatio > Decimal.ZERO) {
any = true
energy.extractEnergy(energyRate * energyRatio, false)
matter.receiveMatter(it.extractMatterChecked(rate * minRatio, false), false)
workProgress = 1f - (it.storedMatter / initialCapacity!!).toFloat()
}
break
}
if (any) {
blockstateToWorking()
} else {
blockstateToIdle()
}
if (!hasCapacitors) {
initialCapacity = null
workProgress = 0f
level?.setBlock(blockPos, blockState.setValue(WorkerState.SEMI_WORKER_STATE, WorkerState.IDLE), Block.UPDATE_CLIENTS)
}
}
@ -302,9 +178,106 @@ class MatterBottlerBlockEntity(blockPos: BlockPos, blockState: BlockState) :
}
if (isBottling) {
tickBottling()
var any = false
var idle = false
for (slot in bottling.slotIterator()) {
val item = slot.item
item.getCapability(MatteryCapability.MATTER_ITEM)?.let {
if (!it.missingMatter.isPositive) {
unbottling.consumeItem(item, false)
slot.setChanged()
} else {
any = true
initialCapacity = initialCapacity ?: it.storedMatter
val rate = MachinesConfig.MatterBottler.RATE * (1.0 + upgrades.speedBonus)
if (matter.storedMatter < rate) {
matter.receiveMatter(matterNode.graph.extractMatter(matter.missingMatter
.coerceAtMost(rate * 200)
.coerceAtMost(it.missingMatter - matter.storedMatter), false), false)
}
if (matter.storedMatter.isPositive) {
matter.extractMatter(it.receiveMatter(rate.coerceAtMost(matter.storedMatter), false), false)
if (!it.missingMatter.isPositive) {
initialCapacity = null
workProgress = 0f
} else {
workProgress = ((it.storedMatter - initialCapacity!!) / it.maxStoredMatter).toFloat()
}
} else {
idle = true
if (spitItemsWhenCantWork) {
unbottling.consumeItem(item, false)
slot.setChanged()
}
}
}
}
if (any) {
break
}
}
if (any && !idle) {
blockstateToWorking()
} else {
matter.extractMatter(matterNode.graph.receiveMatter(matter.storedMatter, false), false)
blockstateToIdle()
}
} else {
tickUnbottling()
matter.extractMatter(matterNode.graph.receiveMatter(matter.storedMatter, false), false)
if (!matter.missingMatter.isPositive) {
if (spitItemsWhenCantWork) {
for (slot in unbottling.slotIterator()) {
bottling.consumeItem(slot.item, false)
slot.setChanged()
}
}
blockstateToIdle()
return
}
var any = false
for (slot in unbottling.slotIterator()) {
val item = slot.item
item.getCapability(MatteryCapability.MATTER_ITEM)?.let {
if (!it.storedMatter.isPositive) {
bottling.consumeItem(item, false)
slot.setChanged()
} else {
any = true
initialCapacity = initialCapacity ?: it.storedMatter
matter.receiveMatter(it.extractMatter(MachinesConfig.MatterBottler.RATE, false), false)
if (!it.storedMatter.isPositive) {
initialCapacity = null
workProgress = 0f
} else {
workProgress = 1f - (it.storedMatter / initialCapacity!!).toFloat()
}
}
}
if (any) {
break
}
}
if (any) {
blockstateToWorking()
} else {
blockstateToIdle()
}
}
}
}

View File

@ -60,7 +60,7 @@ object MachinesConfig : AbstractConfig("machines") {
private val MATTER_BOTTLER = workerValues(
MNames.MATTER_BOTTLER,
energyStorage = Decimal(40_000),
energyConsumption = Decimal(40),
energyConsumption = Decimal(10),
energyThroughput = Decimal(200),
matterCapacity = Decimal(400),
workTimeMultiplier = null
@ -85,7 +85,7 @@ object MachinesConfig : AbstractConfig("machines") {
object MatterBottler {
val VALUES by ::MATTER_BOTTLER
val RATE by builder.comment("Matter transferred per tick").defineDecimal("RATE", Decimal("10.0"), Decimal.ONE_TENTH)
val RATE by builder.comment("Matter transferred per tick").defineDecimal("RATE", Decimal("2.0"), Decimal.ONE_TENTH)
}
object MatterRecycler {

View File

@ -274,7 +274,6 @@ class SlottedContainer(
for ((item, slot) in items) {
if (slot in 0 until containerSize) {
slots[slot].item = item
bitmap[slot] = item.isNotEmpty
} else if (item.isNotEmpty) {
ItemStack.CODEC.encodeStart(provider.createSerializationContext(NbtOps.INSTANCE), item)
.ifError { LOGGER.warn("Unable to serialize 'lost' item: ${it.message()}") }
@ -308,7 +307,6 @@ class SlottedContainer(
if (i in 0 until containerSize) {
slots[i].deserializeNBT(provider, element)
bitmap[i] = slots[i].isNotEmpty
} else {
lostItems.add(element)
this.provider = provider