Skip to content

Commit

Permalink
Improves essentia network performance and fixes essentia network-rela…
Browse files Browse the repository at this point in the history
…ted bugs
  • Loading branch information
SynthRose committed Apr 2, 2020
1 parent fa01406 commit 0f82e71
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 37 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ yarn_mappings = 20w13b+build.4
loader_version = 0.7.8+build.189

#Mod properties
mod_version = 0.3.1
mod_version = 0.3.1.1
maven_group = io.github.synthrose.artofalchemy
archives_base_name = artofalchemy

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,9 @@ public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEnt
break;
}
if (!world.isClient()) {
EssentiaNetworker.get((ServerWorld) world).update(pos, getConnections(state, pos));
EssentiaNetworker networker = EssentiaNetworker.get((ServerWorld) world);
networker.remove(pos, getConnections(state, pos));
networker.add(pos);
}
return ActionResult.SUCCESS;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,14 @@ public void rebuildNodes() {
}
}

public void rebuildNodes(BlockPos pos) {
nodes.removeIf((node) -> node.getPos() == pos);
pullers.removeIf((node) -> node.getPos() == pos);
pushers.removeIf((node) -> node.getPos() == pos);
passives.removeIf((node) -> node.getPos() == pos);
public void removeNodes(BlockPos pos) {
nodes.removeIf((node) -> node.getPos().equals(pos));
pullers.removeIf((node) -> node.getPos().equals(pos));
pushers.removeIf((node) -> node.getPos().equals(pos));
passives.removeIf((node) -> node.getPos().equals(pos));
}

public void addNodes(BlockPos pos) {
Block block = world.getBlockState(pos).getBlock();
if (block instanceof NetworkElement) {
Set<NetworkNode> newNodes = ((NetworkElement) block).getNodes(world, pos);
Expand Down Expand Up @@ -188,13 +190,21 @@ public void transfer(NetworkNode from, NetworkNode to) {
}

public boolean add(BlockPos pos) {
rebuildNodes(pos);
return positions.add(pos.toImmutable());
if (!positions.contains(pos)) {
addNodes(pos);
return positions.add(pos);
} else {
return false;
}
}

public boolean remove(BlockPos pos) {
rebuildNodes(pos);
return positions.remove(pos);
if (positions.contains(pos)) {
removeNodes(pos);
return positions.remove(pos);
} else {
return false;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,10 @@ public EssentiaNetworker(ServerWorld world) {

@Override
public void fromTag(CompoundTag tag) {
System.out.println(tag.toString());
ListTag networkList = tag.getList("networks", NbtType.LIST);
for (Tag networkTag : networkList) {
if (networkTag instanceof ListTag) {
if (networkTag instanceof ListTag && ((ListTag) networkTag).size() > 0) {
networks.add(new EssentiaNetwork(world, (ListTag) networkTag));
}
}
Expand All @@ -44,24 +45,27 @@ public void fromTag(CompoundTag tag) {
if (orphanTag instanceof ListTag) {
ListTag posTag = (ListTag) orphanTag;
BlockPos pos = new BlockPos(posTag.getInt(0), posTag.getInt(1), posTag.getInt(2));
orphans.add(pos);
orphans.add(pos.toImmutable());
}
}
ListTag legacyList = tag.getList("network_positions", NbtType.LIST);
for (Tag orphanTag : legacyList) {
if (orphanTag instanceof ListTag) {
ListTag posTag = (ListTag) orphanTag;
BlockPos pos = new BlockPos(posTag.getInt(0), posTag.getInt(1), posTag.getInt(2));
legacyOrphans.add(pos);
legacyOrphans.add(pos.toImmutable());
}
}
rebuildCache();
}

@Override
public CompoundTag toTag(CompoundTag tag) {
ListTag networkList = new ListTag();
for (EssentiaNetwork network : networks) {
networkList.add(network.toTag());
if (network.getSize() > 0) {
networkList.add(network.toTag());
}
}
tag.put("networks", networkList);
ListTag orphanList = new ListTag();
Expand All @@ -73,9 +77,20 @@ public CompoundTag toTag(CompoundTag tag) {
orphanList.add(posTag);
}
tag.put("orphans", orphanList);
System.out.println(tag.toString());
System.out.println(cache.toString());
return tag;
}

public void rebuildCache() {
cache.clear();
for (EssentiaNetwork network : networks) {
for (BlockPos pos : network.getPositions()) {
cache.put(pos.toImmutable(), network);
}
}
}

public static EssentiaNetworker get(ServerWorld world) {
return world.getPersistentStateManager().getOrCreate(() -> new EssentiaNetworker(world), getName(world.dimension));
}
Expand All @@ -88,14 +103,14 @@ public void tick() {
processed = 0;
for (BlockPos pos : new HashSet<>(orphans)) {
if (processed < PER_TICK_LIMIT) {
add(pos);
add(pos.toImmutable());
} else {
break;
}
}
for (BlockPos pos : new HashSet<>(legacyOrphans)) {
if (processed < PER_TICK_LIMIT) {
recursiveAdd(pos);
recursiveAdd(pos.toImmutable());
} else {
break;
}
Expand All @@ -116,7 +131,7 @@ public Optional<EssentiaNetwork> getNetwork(BlockPos pos) {
}
for (EssentiaNetwork network : networks) {
if (network.contains(pos)) {
cache.put(pos, network);
cache.put(pos.toImmutable(), network);
return Optional.of(network);
}
}
Expand Down Expand Up @@ -147,8 +162,8 @@ public void add(BlockPos pos) {
if (!getNetwork(pos).isPresent()) {
// Otherwise, add it to any connected networks, creating a new one or merging if necessary
EssentiaNetwork network = merge(getConnectedNetworks(pos).toArray(new EssentiaNetwork[0]));
network.add(pos);
cache.put(pos, network);
network.add(pos.toImmutable());
cache.put(pos.toImmutable(), network);
markDirty();
}
}
Expand All @@ -165,6 +180,9 @@ public EssentiaNetwork merge(EssentiaNetwork... networks) {
this.networks.add(mergedNetwork);
for (EssentiaNetwork network : networks) {
mergedNetwork.getPositions().addAll(network.getPositions());
for (BlockPos pos : mergedNetwork.getPositions()) {
cache.put(pos, mergedNetwork);
}
this.networks.remove(network);
}
mergedNetwork.rebuildNodes();
Expand All @@ -179,36 +197,27 @@ public void remove(BlockPos pos, Set<BlockPos> connections) {
cache.remove(pos);
network.remove(pos);
if (network.getSize() == 0 || connections.size() > 1) {
orphans.addAll(network.getPositions());
for (BlockPos netPos : network.getPositions()) {
orphans.add(netPos.toImmutable());
cache.remove(netPos);
}
networks.remove(network);
}
markDirty();
});
}

// Possibly rebuilds the networks associated with a specific position after connections are made or broken.
public void update(BlockPos pos, Set<BlockPos> connections) {
remove(pos, connections);
add(pos);
}

// Updates the nodes at a specific position. Not sufficient if connections have been made or broken.
public void updateNodes(BlockPos pos) {
getNetwork(pos).ifPresent((network) -> network.rebuildNodes(pos));
markDirty();
}

@Deprecated
public void recursiveAdd(BlockPos pos) {
if (processed < PER_TICK_LIMIT) {
if (!getNetwork(pos).isPresent()) {
add(pos);
orphans.remove(pos);
add(pos.toImmutable());
legacyOrphans.remove(pos);
Set<BlockPos> connections = getConnections(pos);
connections.forEach(this::recursiveAdd);
}
} else {
orphans.add(pos);
legacyOrphans.add(pos.toImmutable());
ArtOfAlchemy.log(Level.WARN, "Reached essentia network processing limit at [" + pos.getX() +
", " + pos.getY() + ", " + pos.getZ()+ "] in " + Registry.DIMENSION_TYPE.getId(world.getDimension().getType()));
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/fabric.mod.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"schemaVersion": 1,
"id": "artofalchemy",
"version": "0.3.1+20w13b",
"version": "0.3.1.1+20w13b",
"name": "Art of Alchemy",
"description": "An industrial magic mod that allows you to unleash the power of transmutation!",
"authors": [
Expand Down

0 comments on commit 0f82e71

Please sign in to comment.