Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

E-net recalculation should not break on external errors #39

Open
wants to merge 1 commit into
base: feature/fixes
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 14 additions & 7 deletions src/main/java/tesseract/api/gt/GTController.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import tesseract.util.Node;
import tesseract.util.Pos;

import java.util.Comparator;
import java.util.List;

/**
Expand Down Expand Up @@ -46,8 +45,11 @@ public GTController(int dim) {
*/
@Override
public void change() {
//noinspection StatementWithEmptyBody
while(!changeInternal()); // not sure how many times we may break the network while changing it
}
private boolean changeInternal(){
data.clear();

for (Long2ObjectMap.Entry<Cache<IGTNode>> e : group.getNodes().long2ObjectEntrySet()) {
long pos = e.getLongKey();
IGTNode producer = e.getValue().value();
Expand All @@ -60,15 +62,17 @@ public void change() {
long side = position.offset(direction).asLong();

if (group.getNodes().containsKey(side)) {
onCheck(producer, consumers, null, pos,side);
if (!onCheck(producer, consumers, null, pos,side))
return false;
} else {
Grid<IGTCable> grid = group.getGridAt(side, direction);
if (grid != null) {
for (Path<IGTCable> path : grid.getPaths(pos)) {
if (!path.isEmpty()) {
Node target = path.target();
assert target != null;
onCheck(producer, consumers, path,pos, target.asLong());
if (!onCheck(producer, consumers, path,pos, target.asLong()))
return false;
}
}
}
Expand All @@ -89,6 +93,7 @@ public void change() {
for (List<GTConsumer> consumers : data.values()) {
consumers.sort(GTConsumer.COMPARATOR);
}
return true;
}

/**
Expand Down Expand Up @@ -122,11 +127,11 @@ private void onMerge(IGTNode producer, List<GTConsumer> consumers) {
* @param consumerPos The position of the consumer.
* @param producerPos The position of the producer.
*/
private void onCheck(IGTNode producer, List<GTConsumer> consumers, Path<IGTCable> path, long producerPos, long consumerPos) {
private boolean onCheck(IGTNode producer, List<GTConsumer> consumers, Path<IGTCable> path, long producerPos, long consumerPos) {
Cache<IGTNode> nodee = group.getNodes().get(consumerPos);
if (nodee == null) {
System.out.println("Error in onCheck, null cache.");
return;
return false;
}
IGTNode node = nodee.value();
Pos pos = new Pos(consumerPos).sub(new Pos(producerPos));
Expand All @@ -136,15 +141,17 @@ private void onCheck(IGTNode producer, List<GTConsumer> consumers, Path<IGTCable
GTConsumer consumer = new GTConsumer(node, path);
int voltage = producer.getOutputVoltage() - consumer.getLoss();
if (voltage <= 0) {
return;
return false;
}

if (voltage <= node.getInputVoltage()) {
consumers.add(consumer);
return true;
} else {
onNodeOverVoltage(dim, consumerPos, voltage);
}
}
return false;
}

/**
Expand Down