|
6 | 6 | import com.legobmw99.allomancy.modules.powers.PowersConfig;
|
7 | 7 | import com.legobmw99.allomancy.modules.powers.data.AllomancerAttachment;
|
8 | 8 | import it.unimi.dsi.fastutil.longs.LongOpenHashSet;
|
| 9 | +import net.minecraft.Util; |
9 | 10 | import net.minecraft.client.Minecraft;
|
10 | 11 | import net.minecraft.core.BlockPos;
|
11 | 12 | import net.minecraft.world.entity.Entity;
|
|
16 | 17 | import net.minecraft.world.phys.Vec3;
|
17 | 18 |
|
18 | 19 | import java.util.*;
|
| 20 | +import java.util.concurrent.ExecutorService; |
| 21 | +import java.util.concurrent.Future; |
| 22 | +import java.util.concurrent.atomic.AtomicInteger; |
| 23 | +import java.util.concurrent.locks.Lock; |
| 24 | +import java.util.concurrent.locks.ReentrantLock; |
19 | 25 | import java.util.function.Consumer;
|
20 | 26 |
|
21 | 27 | public class SensoryTracking {
|
22 | 28 |
|
| 29 | + |
23 | 30 | private final List<Entity> metal_entities = new ArrayList<>();
|
24 |
| - private final List<MetalBlockBlob> metal_blobs = new ArrayList<>(); |
| 31 | + private final SyncList<MetalBlockBlob> metal_blobs = new SyncList<>(); |
25 | 32 | private final List<Player> nearby_allomancers = new ArrayList<>();
|
26 |
| - |
27 |
| - private int tickOffset = 0; |
28 | 33 | private final Deque<BlockPos> to_consider = new ArrayDeque<>(20 * 20);
|
29 | 34 | // trick taken from BlockPos#breadthFirstTraversal used in SpongeBlock
|
30 | 35 | private final Set<Long> seen = new LongOpenHashSet(20 * 20);
|
31 | 36 |
|
32 |
| - public void tick() { |
33 |
| - this.tickOffset = (this.tickOffset + 1) % 2; |
34 |
| - if (this.tickOffset == 0) { |
35 |
| - populateSensoryLists(); |
36 |
| - } |
37 |
| - } |
| 37 | + private Future<?> blobFuture = null; |
38 | 38 |
|
39 | 39 | public void forEachSeeked(Consumer<Player> f) {
|
40 | 40 | this.nearby_allomancers.forEach(f);
|
41 | 41 | }
|
42 | 42 |
|
43 |
| - public void forEachMetalicEntity(Consumer<Entity> f) { |
| 43 | + public void forEachMetallicEntity(Consumer<Entity> f) { |
44 | 44 | this.metal_entities.forEach(f);
|
45 | 45 | }
|
46 | 46 |
|
47 | 47 | public void forEachMetalBlob(Consumer<MetalBlockBlob> f) {
|
48 | 48 | this.metal_blobs.forEach(f);
|
49 | 49 | }
|
50 | 50 |
|
51 |
| - private void populateSensoryLists() { |
| 51 | + public void tick() { |
52 | 52 | Player player = Minecraft.getInstance().player;
|
53 | 53 | IAllomancerData data = player.getData(AllomancerAttachment.ALLOMANCY_DATA);
|
54 | 54 |
|
55 |
| - this.metal_blobs.clear(); |
56 |
| - this.metal_entities.clear(); |
57 | 55 | if (data.isBurning(Metal.IRON) || data.isBurning(Metal.STEEL)) {
|
58 | 56 | int max = PowersConfig.max_metal_detection.get();
|
59 | 57 | var negative = player.blockPosition().offset(-max, -max, -max);
|
60 | 58 | var positive = player.blockPosition().offset(max, max, max);
|
61 | 59 |
|
62 | 60 | // Add metal entities to metal list
|
| 61 | + this.metal_entities.clear(); |
63 | 62 | this.metal_entities.addAll(
|
64 | 63 | player.level().getEntitiesOfClass(Entity.class, AABB.encapsulatingFullBlocks(negative, positive), e -> PowerUtils.isEntityMetal(e) && !e.equals(player)));
|
65 | 64 |
|
66 | 65 | // Add metal blobs to metal list
|
67 |
| - this.seen.clear(); |
68 |
| - BlockPos |
69 |
| - .betweenClosed(negative.getX(), negative.getY(), negative.getZ(), positive.getX(), positive.getY(), positive.getZ()) |
70 |
| - .forEach(starter -> searchNearbyMetalBlocks(player.blockPosition(), max, starter, player.level())); |
| 66 | + if (this.blobFuture == null || this.blobFuture.isDone()) { |
| 67 | + this.blobFuture = Util.backgroundExecutor().submit(() -> { |
| 68 | + this.seen.clear(); |
| 69 | + BlockPos |
| 70 | + .betweenClosed(negative.getX(), negative.getY(), negative.getZ(), positive.getX(), positive.getY(), positive.getZ()) |
| 71 | + .forEach(starter -> searchNearbyMetalBlocks(player.blockPosition(), max, starter, player.level())); |
| 72 | + this.metal_blobs.swapAndClearOld(); |
| 73 | + }); |
| 74 | + } |
| 75 | + |
| 76 | + } else if (this.blobFuture != null) { // previously we were burning |
| 77 | + this.blobFuture = null; |
| 78 | + this.metal_blobs.clearBothAsync(Util.backgroundExecutor()); |
| 79 | + this.metal_entities.clear(); |
71 | 80 | }
|
72 | 81 |
|
73 | 82 | // Populate our list of nearby allomancy users
|
@@ -135,6 +144,71 @@ private boolean addSeeked(IAllomancerData data, Player otherPlayer) {
|
135 | 144 | }
|
136 | 145 |
|
137 | 146 |
|
| 147 | + private static class SyncList<T> { |
| 148 | + private final List<T> list_a = new ArrayList<>(); |
| 149 | + private final List<T> list_b = new ArrayList<>(); |
| 150 | + |
| 151 | + private final Lock swapLock = new ReentrantLock(); |
| 152 | + |
| 153 | + /** |
| 154 | + * When this is even, we are reading A and writing B |
| 155 | + */ |
| 156 | + private final AtomicInteger AorB = new AtomicInteger(0); |
| 157 | + |
| 158 | + |
| 159 | + /** |
| 160 | + * Intended to be invoked from the main thread |
| 161 | + */ |
| 162 | + public void forEach(Consumer<T> f) { |
| 163 | + this.swapLock.lock(); |
| 164 | + try { |
| 165 | + if (this.AorB.get() % 2 == 0) { |
| 166 | + this.list_a.forEach(f); |
| 167 | + } else { |
| 168 | + this.list_b.forEach(f); |
| 169 | + } |
| 170 | + } finally { |
| 171 | + this.swapLock.unlock(); |
| 172 | + } |
| 173 | + } |
| 174 | + |
| 175 | + public void add(T t) { |
| 176 | + if (this.AorB.get() % 2 == 1) { |
| 177 | + this.list_a.add(t); |
| 178 | + } else { |
| 179 | + this.list_b.add(t); |
| 180 | + } |
| 181 | + } |
| 182 | + |
| 183 | + |
| 184 | + /** |
| 185 | + * Intended to be invoked from a thread other than main |
| 186 | + */ |
| 187 | + public void swapAndClearOld() { |
| 188 | + this.swapLock.lock(); |
| 189 | + int newAB = this.AorB.incrementAndGet(); |
| 190 | + this.swapLock.unlock(); |
| 191 | + if (newAB % 2 == 1) { |
| 192 | + this.list_a.clear(); |
| 193 | + } else { |
| 194 | + this.list_b.clear(); |
| 195 | + } |
| 196 | + } |
| 197 | + |
| 198 | + public void clearBothAsync(ExecutorService ex) { |
| 199 | + ex.submit(() -> { |
| 200 | + this.swapLock.lock(); |
| 201 | + try { |
| 202 | + this.list_a.clear(); |
| 203 | + this.list_b.clear(); |
| 204 | + } finally { |
| 205 | + this.swapLock.unlock(); |
| 206 | + } |
| 207 | + }); |
| 208 | + } |
| 209 | + } |
| 210 | + |
| 211 | + |
138 | 212 | public static class MetalBlockBlob {
|
139 | 213 |
|
140 | 214 | private static final Level level = Minecraft.getInstance().level;
|
|
0 commit comments