Skip to content

Commit

Permalink
Merge branch 'dev-1.18' into dev-1.19
Browse files Browse the repository at this point in the history
  • Loading branch information
Trinsdar committed Jan 23, 2024
2 parents 407913a + 74bf349 commit 8080fc6
Show file tree
Hide file tree
Showing 17 changed files with 127 additions and 257 deletions.
2 changes: 1 addition & 1 deletion common/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ dependencies{
compileOnly("com.electronwill.night-config:toml:${rootProject.nightconfig_version}")
compileOnly("com.electronwill.night-config:core:${rootProject.nightconfig_version}")
modImplementation("earth.terrarium:botarium-common-${project.minecraft_version}:${project.botarium_version}")
modImplementation "maven.modrinth:carbon-config:yIKfSms3"
modImplementation "maven.modrinth:carbon-config:${project.carbon_config_fabric_version}"
testImplementation('junit:junit:4.11')
testImplementation(sourceSets.main.output)
}
Expand Down
2 changes: 1 addition & 1 deletion common/gradle.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
mod_version=0.2.2
mod_version=0.2.3-pre1
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@
import net.minecraft.world.level.block.entity.BlockEntity;
import tesseract.TesseractCapUtils;
import tesseract.TesseractGraphWrappers;
import tesseract.api.gt.GTConsumer;
import tesseract.api.gt.GTTransaction;
import tesseract.api.gt.IEnergyHandler;
import tesseract.api.gt.IGTCable;
import tesseract.api.gt.*;
import tesseract.graph.Graph;
import tesseract.util.Pos;

Expand All @@ -24,15 +21,14 @@ public TesseractGTCapability(T tile, Direction dir, boolean isNode, ITransaction
}

@Override
public long insertAmps(long voltage, long amps, boolean simulate) {
if (this.isSending) return 0;
public long insertEu(long voltage, boolean simulate) {
if (this.isSending || (!simulate && old == null)) return 0;
this.isSending = true;
if (!simulate) {
if (old == null) return 0;
old.commit();
} else {
long pos = tile.getBlockPos().asLong();
GTTransaction transaction = new GTTransaction(amps, voltage, t -> {});
GTTransaction transaction = new GTTransaction(voltage, t -> {});
if (!this.isNode) {
TesseractGraphWrappers.GT_ENERGY.getController(tile.getLevel(), pos).insert(pos, side, transaction, callback);
} else {
Expand All @@ -41,17 +37,7 @@ public long insertAmps(long voltage, long amps, boolean simulate) {
this.old = transaction;
}
this.isSending = false;
return amps - old.getAvailableAmps();
}

@Override
public long insertEu(long voltage, boolean simulate) {
return insertAmps(voltage, 1, simulate) == 1 ? voltage : 0;
}

@Override
public long extractAmps(long voltage, long amps, boolean simulate) {
return 0;
return voltage - old.eu;
}

@Override
Expand All @@ -60,37 +46,45 @@ public long extractEu(long voltage, boolean simulate) {
}

private void transferAroundPipe(GTTransaction transaction, long pos) {
boolean flag = false;

boolean hasInserted = false;
boolean lossAdded = false;
for (Direction dir : Graph.DIRECTIONS) {
if (dir == this.side || !this.tile.connects(dir)) continue;
//First, perform cover modifications.
BlockEntity otherTile = tile.getLevel().getBlockEntity(BlockPos.of(Pos.offset(pos, dir)));
if (otherTile != null) {
//Check the handler.
var cap = TesseractCapUtils.getEnergyHandler(otherTile, dir.getOpposite());
if (!cap.isPresent()) continue;
if (cap.isEmpty()) continue;
//Perform insertion, and add to the transaction.
var handler = cap.get();
long voltage = transaction.voltageOut - Math.round(cable.getLoss());
long ampsToInsert = handler.availableAmpsInput(voltage);
GTTransaction.TransferData data = new GTTransaction.TransferData(transaction,voltage, ampsToInsert);
long loss = Math.round(cable.getLoss());
if (hasInserted && !lossAdded){
transaction.addData(0, loss, 0, d -> {});
lossAdded = true;
}

long remainingEu = lossAdded ? transaction.eu : transaction.eu - loss;
GTTransaction.TransferData data = new GTTransaction.TransferData(transaction, remainingEu, transaction.voltage).setLoss(cable.getLoss());
if (this.callback.modify(data, dir, false, true) || this.callback.modify(data, side, true, true)){
continue;
}
if (data.getAmps(true) < ampsToInsert) ampsToInsert = data.getAmps(true);
if (data.getLoss() > 0) voltage -= Math.round(data.getLoss());
long amps = handler.insertAmps(voltage, ampsToInsert, true);
if (amps > 0){
transaction.addData(amps, cable.getLoss(), t -> {
if (data.getEu() < remainingEu) remainingEu = data.getEu();
if (data.getLoss() > 0) remainingEu -= Math.round(data.getLoss());
if (remainingEu <= 0) return;
long inserted = handler.insertEu(remainingEu, true);
if (inserted > 0){
transaction.addData(inserted, inserted, cable.getLoss(), t -> {
if (this.callback.modify(t, dir, false, false) || this.callback.modify(data, side, true, false)){
return;
}
handler.insertAmps(t.getVoltage(), t.getAmps(true), false);
handler.insertEu(t.getEu(), false);
});
if (transaction.voltage > this.cable.getVoltage()){
((IGTEvent)TesseractGraphWrappers.GT_ENERGY.getController(tile.getLevel(), pos)).onCableOverVoltage(tile.getLevel(), pos, transaction.voltage);
}
}
if (transaction.getAvailableAmps() == 0) break;

if (transaction.eu == 0) break;
}
}
}
Expand Down
95 changes: 73 additions & 22 deletions common/src/main/java/tesseract/api/gt/GTController.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package tesseract.api.gt;

import it.unimi.dsi.fastutil.Pair;
import it.unimi.dsi.fastutil.longs.*;
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
import net.minecraft.core.Direction;
Expand All @@ -18,9 +19,14 @@
import tesseract.util.Node;
import tesseract.util.Pos;

import java.util.ArrayList;
import java.util.EnumMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.Function;

/**
* Class acts as a controller in the group of an electrical components.
Expand Down Expand Up @@ -167,6 +173,7 @@ public void insert(long pipePos, Direction side, GTTransaction stack, ITransacti
IGTNode producer = node.value(side.getOpposite());

long voltage_out = producer.getOutputVoltage();
if (stack.voltage > voltage_out) return;

/*
* if (amperage_in <= 0) { // just for sending the last piece of energy
Expand All @@ -175,29 +182,77 @@ public void insert(long pipePos, Direction side, GTTransaction stack, ITransacti
* }
*/
inserted++;
List<Consumer<Long2ObjectMap<IGTCable>>> transferList = new ArrayList<>();

double previousLoss = 0;
for (GTConsumer consumer : list) {
long amperage_in = stack.getAvailableAmps();
long remainingEu = stack.eu;

if (amperage_in <= 0) {
if (remainingEu <= 0) {
break;
}
long loss = Math.round(consumer.getLoss());
if (loss < 0 || loss > voltage_out) {
double loss = consumer.getLoss();
double appliedLoss = loss == 0 ? 0 : loss > previousLoss ? loss - previousLoss : previousLoss - loss;
previousLoss = loss;
long roundedAppliedLoss = Math.round(appliedLoss);
if (roundedAppliedLoss < 0 || roundedAppliedLoss > remainingEu) {
continue;
}

long amperage = consumer.getRequiredAmperage(voltage_out - loss);
if (amperage <= 0) { // if this consumer received all the energy from the other producers
long lossyEu = remainingEu - roundedAppliedLoss;
long euInserted = consumer.getNode().insertEu(lossyEu, true);
if (euInserted <= 0) { // if this consumer received all the energy from the other producers
continue;
}

// Remember amperes stored in this consumer
amperage = Math.min(amperage_in, amperage);
// If we are here, then path had some invalid cables which not suits the limits
// of amps/voltage
stack.addData(amperage, loss, a -> dataCommit(consumer, a));

GTTransaction.TransferData data1 = stack.addData(euInserted, euInserted + roundedAppliedLoss, appliedLoss, a -> {});
transferList.add((l) -> dataCommit(l, consumer, data1));
}
if (!transferList.isEmpty()){
stack.addData(0, 0, 0, d-> dataCommit(transferList));
}
}

public void dataCommit(Long2ObjectMap<IGTCable> cableList, GTConsumer consumer, GTTransaction.TransferData data){
if (!consumer.canHandle(data.getVoltage())) {
for (Long2ObjectMap.Entry<IGTCable> c : consumer.getFull().long2ObjectEntrySet()) {
long pos = c.getLongKey();
IGTCable cable = c.getValue();
if (Objects.requireNonNull(cable.getHandler(data.getVoltage(), 0)) == GTStatus.FAIL_VOLTAGE) {
onCableOverVoltage(getWorld(), pos, data.getVoltage());
return;
}
}
} else {
for (Long2ObjectMap.Entry<IGTCable> c : consumer.getFull().long2ObjectEntrySet()) {
IGTCable cable = c.getValue();
if (!cableList.containsKey(c.getLongKey())) cableList.put(c.getLongKey(), cable);
}
}
cableIsActive.addAll(consumer.uninsulatedCables);

this.totalLoss += data.getLoss();
this.totalVoltage += data.getEu();
consumer.getNode().insertEu(data.getEu(), false);
}

public void dataCommit(List<Consumer<Long2ObjectMap<IGTCable>>> list){
Long2ObjectMap<IGTCable> cableList = new Long2ObjectOpenHashMap<>();
for (var pair : list) {
pair.accept(cableList);
}
for (Long2ObjectMap.Entry<IGTCable> c : cableList.long2ObjectEntrySet()) {
long pos = c.getLongKey();
IGTCable cable = c.getValue();
cable.setHolder(GTHolder.add(cable.getHolder(), 1));
if (GTHolder.isOverAmperage(cable.getHolder())) {
onCableOverAmperage(getWorld(), pos, GTHolder.getAmperage(cable.getHolder()));
return;
}
}
totalAmperage++;
}

/**
Expand All @@ -208,18 +263,18 @@ public void insert(long pipePos, Direction side, GTTransaction stack, ITransacti
* @param data the transfer data.
*/
public void dataCommit(GTConsumer consumer, GTTransaction.TransferData data) {
if (!consumer.canHandle(data.getVoltage()) || !consumer.canHandleAmp(data.getTotalAmperage()) || (consumer.getConnection() == ConnectionType.SINGLE
&& !(consumer.canHandleAmp(data.getTotalAmperage())))) {
if (!consumer.canHandle(data.getVoltage()) || !consumer.canHandleAmp(1) || (consumer.getConnection() == ConnectionType.SINGLE
&& !(consumer.canHandleAmp(1)))) {
for (Long2ObjectMap.Entry<IGTCable> c : consumer.getFull().long2ObjectEntrySet()) {
long pos = c.getLongKey();
IGTCable cable = c.getValue();
switch (cable.getHandler(data.getVoltage(), data.getTotalAmperage())) {
switch (cable.getHandler(data.getVoltage(), 1)) {
case FAIL_VOLTAGE -> {
onCableOverVoltage(getWorld(), pos, data.getVoltage());
return;
}
case FAIL_AMPERAGE -> {
onCableOverAmperage(getWorld(), pos, data.getTotalAmperage());
onCableOverAmperage(getWorld(), pos, 1);
return;
}
default -> {
Expand All @@ -231,11 +286,7 @@ public void dataCommit(GTConsumer consumer, GTTransaction.TransferData data) {
for (Long2ObjectMap.Entry<IGTCable> c : consumer.getCross().long2ObjectEntrySet()) {
long pos = c.getLongKey();
IGTCable cable = c.getValue();
/*pipeMap.compute(c.getLongKey(), (l, i) ->{
if (i == null) return Math.toIntExact(data.getTotalAmperage());
return Math.toIntExact(i + data.getTotalAmperage());
});*/
cable.setHolder(GTHolder.add(cable.getHolder(), data.getTotalAmperage()));
cable.setHolder(GTHolder.add(cable.getHolder(), 1));
if (GTHolder.isOverAmperage(cable.getHolder())) {
onCableOverAmperage(getWorld(), pos, GTHolder.getAmperage(cable.getHolder()));
return;
Expand All @@ -246,9 +297,9 @@ public void dataCommit(GTConsumer consumer, GTTransaction.TransferData data) {
cableIsActive.addAll(consumer.uninsulatedCables);

this.totalLoss += data.getLoss();
this.totalAmperage += data.getTotalAmperage();
this.totalVoltage += data.getTotalAmperage() * data.getVoltage();
consumer.getNode().insertAmps(data.getEnergy(1, true), data.getAmps(true), false);
this.totalAmperage++;
this.totalVoltage += data.getEu();
consumer.getNode().insertEu(data.getEu(), false);
}

@Override
Expand Down
Loading

0 comments on commit 8080fc6

Please sign in to comment.