From b4e37fbd1f8a473525367b976eea63fb6f21d0b2 Mon Sep 17 00:00:00 2001 From: DBotThePony Date: Wed, 15 Jan 2025 21:27:13 +0700 Subject: [PATCH] Cable segment merging --- .../entity/cable/EnergyCableBlockEntity.kt | 3 +- .../block/entity/cable/EnergyCableGraph.kt | 76 +++++++++++++++---- 2 files changed, 65 insertions(+), 14 deletions(-) diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/cable/EnergyCableBlockEntity.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/cable/EnergyCableBlockEntity.kt index 86a0703c2..f5afde287 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/cable/EnergyCableBlockEntity.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/cable/EnergyCableBlockEntity.kt @@ -99,7 +99,8 @@ abstract class EnergyCableBlockEntity(type: BlockEntityType<*>, blockPos: BlockP } inner class Node : GraphNode(::EnergyCableGraph) { - private var _segment = EnergyCableGraph.Segment(this) + @Deprecated("internal property") + var _segment = EnergyCableGraph.Segment(this) var segment: EnergyCableGraph.Segment get() { return _segment } diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/cable/EnergyCableGraph.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/cable/EnergyCableGraph.kt index bd53852f5..3fa115953 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/cable/EnergyCableGraph.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/cable/EnergyCableGraph.kt @@ -3,6 +3,7 @@ package ru.dbotthepony.mc.otm.block.entity.cable import it.unimi.dsi.fastutil.objects.ObjectLinkedOpenHashSet import it.unimi.dsi.fastutil.objects.ObjectOpenHashSet import it.unimi.dsi.fastutil.objects.Reference2ObjectOpenHashMap +import it.unimi.dsi.fastutil.objects.ReferenceArraySet import it.unimi.dsi.fastutil.objects.ReferenceOpenHashSet import net.minecraft.core.BlockPos import org.apache.logging.log4j.LogManager @@ -118,7 +119,6 @@ class EnergyCableGraph : GraphNodeList= throughput || !amount.isPositive) { @@ -217,6 +220,7 @@ class EnergyCableGraph : GraphNodeList= throughput || !amount.isPositive) { return Decimal.ZERO } @@ -238,33 +242,30 @@ class EnergyCableGraph : GraphNodeList= segment.nodes.size) { + segment.combineInto(this) + } else { + combineInto(segment) + } + + return true + } + + return false + } + + fun tryCombine() { + if (nodes.isEmpty()) return + + // TODO: This is inefficient as fk + for (path in paths) { + for (segment in path.segments) { + if (tryCombineWith(segment)) return + } + } + } + + fun tryCombine(with: Collection) { + if (nodes.isEmpty()) return + + for (segment in with) { + if (tryCombineWith(segment)) return + } + } } fun invalidatePathCache() { @@ -329,11 +377,13 @@ class EnergyCableGraph : GraphNodeList() + val touchedSegments = ReferenceArraySet() solution.forEach { touchedSegments.addAll(it.segment.split()) } val path = SegmentPath(a.blockEntity.blockPos, b.blockEntity.blockPos) touchedSegments.forEach { it.add(path) } + touchedSegments.forEach { it.tryCombine(touchedSegments) } + return path } else { for (neighbour in first.node.neighboursView.values) {