Skip to content

Commit f1e8de4

Browse files
committed
Heavily updated delivery drone pathfinding logic
1 parent 944d40b commit f1e8de4

File tree

3 files changed

+35
-10
lines changed

3 files changed

+35
-10
lines changed

src/main/java/com/workert/robotics/content/robotics/drone_delivery/GlobalDroneNetworkManager.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ public void portAdded(LevelAccessor levelAccessor, GlobalPos pos, String filter)
5959
if (!pathsInThisDimension.containsKey(Couple.create(trackedPortPos, blockPos))) {
6060
this.recalculatePath((Level) levelAccessor, blockPos, trackedPortPos);
6161
pathsInThisDimension.put(Couple.create(trackedPortPos, blockPos), null);
62+
pathsInThisDimension.put(Couple.create(blockPos, trackedPortPos), null);
6263
}
6364

6465
});
@@ -75,7 +76,7 @@ public void recalculatePath(Level level, BlockPos blockPos, BlockPos trackedPort
7576
System.out.println("Calculating path from " + blockPos + " to " + trackedPortPos);
7677
this.savedPaths.computeIfAbsent(level.dimension().toString(),
7778
key -> new HashMap<>()).remove(Couple.create(blockPos, trackedPortPos));
78-
new Thread(() -> {
79+
Thread thread = new Thread(() -> {
7980
final SimpleWorldProvider worldProvider = new SimpleWorldProvider();
8081

8182
addCellsFromWorld(worldProvider, trackedPortPos, blockPos, 16, level); // TODO Make configurable
@@ -100,13 +101,18 @@ public void recalculatePath(Level level, BlockPos blockPos, BlockPos trackedPort
100101
.collect(Collectors.toCollection(ArrayList::new));
101102

102103
ArrayList<BlockPos> reversePathList = new ArrayList<>(pathList);
103-
Collections.reverse(pathList);
104+
Collections.reverse(reversePathList);
104105

106+
this.savedPaths.computeIfAbsent(level.dimension().toString(), key -> new HashMap<>())
107+
.put(Couple.create(blockPos, trackedPortPos), pathList);
105108
this.savedPaths.computeIfAbsent(level.dimension().toString(), key -> new HashMap<>())
106109
.put(Couple.create(trackedPortPos, blockPos), reversePathList);
107110
this.markDirty();
108111
System.out.println("Finished path from " + blockPos + " to " + trackedPortPos);
109-
}).start();
112+
});
113+
thread.setDaemon(true);
114+
thread.setPriority(2);
115+
thread.start();
110116
}
111117

112118
private static void addCellsFromWorld(SimpleWorldProvider blockManager, BlockPos trackedPortPos, BlockPos blockPos, int margin, Level level) {

src/main/java/com/workert/robotics/content/robotics/drone_delivery/delivery_drone/DeliveryDroneEntity.java

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
import java.util.List;
2828

2929
public class DeliveryDroneEntity extends LivingEntity {
30-
private ItemStack box;
30+
private ItemStack box = ItemStack.EMPTY;
3131

3232
private boolean isReturning = false;
3333
private BlockPos startBlockPos;
@@ -38,7 +38,6 @@ public class DeliveryDroneEntity extends LivingEntity {
3838

3939
public DeliveryDroneEntity(EntityType<? extends LivingEntity> entityType, Level level) {
4040
super(entityType, level);
41-
this.box = ItemStack.EMPTY;
4241
this.setNoGravity(true);
4342
}
4443

@@ -72,6 +71,9 @@ public void tick() {
7271
}
7372
}
7473

74+
System.out.println(this.path);
75+
System.out.println(this.pathProgress);
76+
7577
if (this.path == null || this.path.isEmpty()) {
7678
this.path = Robotics.DRONE_NETWORK.savedPaths.computeIfAbsent(this.level().dimension().toString(), key -> new HashMap<>())
7779
.get(this.isReturning
@@ -94,7 +96,6 @@ public void tick() {
9496
}
9597
}
9698

97-
9899
if (this.path != null && !this.path.isEmpty()) {
99100
if (!this.level()
100101
.isEmptyBlock(this.path.get(this.pathProgress)) || (this.pathProgress != (this.path.size() - 1) && !this.level()
@@ -114,6 +115,7 @@ public void tick() {
114115
this.pathProgress++;
115116
} else {
116117
if (!this.box.isEmpty() && this.destinationBlockPos != null
118+
&& this.position().distanceTo(this.destinationBlockPos.getCenter()) < 5
117119
&& this.level().getBlockEntity(this.destinationBlockPos) != null
118120
&& this.level().getBlockEntity(this.destinationBlockPos) instanceof DronePortBlockEntity dronePortBlockEntity) {
119121
if (this.isReturning) {
@@ -206,6 +208,24 @@ protected void dropAllDeathLoot(DamageSource pDamageSource) {
206208
this.dropBox();
207209
}
208210

211+
@Override
212+
public void kill() {
213+
super.kill();
214+
for (int x = -3; x <= 3; x++) {
215+
for (int z = -3; z <= 3; z++) {
216+
ForgeChunkManager.forceChunk(
217+
(ServerLevel) this.level(),
218+
Robotics.MOD_ID,
219+
this,
220+
this.chunkPosition().x + x,
221+
this.chunkPosition().z + z,
222+
false,
223+
false
224+
);
225+
}
226+
}
227+
}
228+
209229
@Override
210230
public void addAdditionalSaveData(CompoundTag compound) {
211231
super.addAdditionalSaveData(compound);

src/main/java/com/workert/robotics/content/robotics/drone_delivery/drone_port/DronePortBlockEntity.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ protected void onOpenChange(boolean open) {
4747
public void lazyTick() {
4848
if (this.level != null && !this.inventory.isEmpty()) {
4949
ItemStack box = ItemHelper.extract(this.inventory,
50-
itemStack -> !itemStack.isEmpty() && PackageItem.isPackage(itemStack) && !PackageItem.matchAddress(itemStack,
51-
this.addressFilter),
52-
1, true);
50+
itemStack -> !itemStack.isEmpty() && PackageItem.isPackage(itemStack) && !PackageItem.getAddress(itemStack)
51+
.isEmpty() && !PackageItem.matchAddress(itemStack,
52+
this.addressFilter), 1, true);
5353
if (!box.isEmpty() && this.target != null && this.level.getEntitiesOfClass(DeliveryDroneEntity.class,
5454
new AABB(this.getBlockPos()).inflate(0, 1, 0)).isEmpty())
5555
if (this.target.export(this.level, this.worldPosition, box, false))
@@ -122,7 +122,6 @@ private static BlockPos getNearestBlockPos(BlockPos target, List<BlockPos> desti
122122
nearest = pos;
123123
}
124124
}
125-
126125
return nearest;
127126
}
128127

0 commit comments

Comments
 (0)