11package com .workert .robotics .content .robotics .drone_delivery ;
22
3+ import com .workert .robotics .content .robotics .drone_delivery .drone_port .DronePortBlockEntity ;
34import com .workert .robotics .content .robotics .drone_delivery .pathing .Cell ;
45import com .workert .robotics .content .robotics .drone_delivery .pathing .Pathfinder ;
56import com .workert .robotics .content .robotics .drone_delivery .pathing .SimpleWorldProvider ;
@@ -34,45 +35,50 @@ public void levelLoaded(LevelAccessor level) {
3435 }
3536
3637 public void portAdded (LevelAccessor levelAccessor , GlobalPos pos , String filter ) {
37- if (this .dronePorts .computeIfAbsent (pos .dimension ().toString (), key -> new HashMap <>()).containsKey (pos .pos ()))
38- return ;
39-
4038 BlockPos blockPos = pos .pos ();
4139
4240 Map <BlockPos , String > portsInThisDimension = this .dronePorts .computeIfAbsent (pos .dimension ().toString (), key -> new HashMap <>());
4341
44- List <BlockPos > trackedPorts = new ArrayList <>();
42+ if (((DronePortBlockEntity ) Objects .requireNonNull (levelAccessor .getBlockEntity (blockPos ))).acceptsPackages ) {
43+ List <BlockPos > trackedPorts = new ArrayList <>();
4544
46- portsInThisDimension .forEach ((portBlockPos , portFilter ) -> {
47- if (!portBlockPos .equals (blockPos )) {
48- double distance = blockPos .distToCenterSqr (portBlockPos .getX (), portBlockPos .getY (), portBlockPos .getZ ());
49- if (distance < 256 ) { // TODO Make configurable
50- trackedPorts .add (portBlockPos );
45+ portsInThisDimension .forEach ((portBlockPos , portFilter ) -> {
46+ if (!portBlockPos .equals (blockPos )) {
47+ double distance = blockPos .distToCenterSqr (portBlockPos .getX (), portBlockPos .getY (), portBlockPos .getZ ());
48+ if (distance < 16384 ) { // TODO Make configurable
49+ trackedPorts .add (portBlockPos );
50+ }
5151 }
52- }
53- });
52+ });
5453
55- Map <Couple <BlockPos >, List <BlockPos >> pathsInThisDimension = this .savedPaths .computeIfAbsent (
56- ((Level ) levelAccessor ).dimension ().toString (),
57- key -> new HashMap <>());
54+ Map <Couple <BlockPos >, List <BlockPos >> pathsInThisDimension = this .savedPaths .computeIfAbsent (
55+ ((Level ) levelAccessor ).dimension ().toString (),
56+ key -> new HashMap <>());
5857
59- trackedPorts .forEach (trackedPortPos -> {
60- if (!pathsInThisDimension .containsKey (Couple .create (blockPos , trackedPortPos )))
61- this .recalculatePath ((Level ) levelAccessor , blockPos , trackedPortPos );
58+ trackedPorts .forEach (trackedPortPos -> {
59+ if (!pathsInThisDimension .containsKey (Couple .create (trackedPortPos , blockPos ))) {
60+ this .recalculatePath ((Level ) levelAccessor , blockPos , trackedPortPos );
61+ pathsInThisDimension .put (Couple .create (trackedPortPos , blockPos ), null );
62+ }
6263
63- });
64+ });
65+ }
6466
6567 portsInThisDimension .put (blockPos , filter );
6668 this .markDirty ();
6769 }
6870
6971 public void recalculatePath (Level level , BlockPos blockPos , BlockPos trackedPortPos ) {
72+ if (level .isClientSide ())
73+ return ;
74+
75+ System .out .println ("Calculating path from " + blockPos + " to " + trackedPortPos );
7076 this .savedPaths .computeIfAbsent (level .dimension ().toString (),
7177 key -> new HashMap <>()).remove (Couple .create (blockPos , trackedPortPos ));
7278 new Thread (() -> {
7379 final SimpleWorldProvider worldProvider = new SimpleWorldProvider ();
7480
75- addCellsFromWorld (worldProvider , trackedPortPos , blockPos , 10 , level ); // TODO Make configurable
81+ addCellsFromWorld (worldProvider , trackedPortPos , blockPos , 16 , level ); // TODO Make configurable
7682
7783 Pathfinder pathfinder = new Pathfinder (
7884 new Cell (blockPos .getX (), blockPos .getY () + 1 , blockPos .getZ ()),
@@ -92,15 +98,14 @@ public void recalculatePath(Level level, BlockPos blockPos, BlockPos trackedPort
9298
9399 ArrayList <BlockPos > pathList = path .stream ().map (cell -> new BlockPos (cell .x , cell .y , cell .z ))
94100 .collect (Collectors .toCollection (ArrayList ::new ));
95- this .savedPaths .computeIfAbsent (level .dimension ().toString (), key -> new HashMap <>())
96- .put (Couple .create (blockPos , trackedPortPos ), pathList );
97101
98102 ArrayList <BlockPos > reversePathList = new ArrayList <>(pathList );
99- Collections .reverse (reversePathList );
103+ Collections .reverse (pathList );
100104
101105 this .savedPaths .computeIfAbsent (level .dimension ().toString (), key -> new HashMap <>())
102106 .put (Couple .create (trackedPortPos , blockPos ), reversePathList );
103107 this .markDirty ();
108+ System .out .println ("Finished path from " + blockPos + " to " + trackedPortPos );
104109 }).start ();
105110 }
106111
@@ -111,7 +116,12 @@ private static void addCellsFromWorld(SimpleWorldProvider blockManager, BlockPos
111116 trackedPortPos .getX ()) + margin ; x ++) {
112117 for (int z = Math .min (blockPos .getZ (), trackedPortPos .getZ ()) - margin ; z <= Math .max (blockPos .getZ (),
113118 trackedPortPos .getZ ()) + margin ; z ++) {
114- if (!level .isEmptyBlock (new BlockPos (x , y , z )))
119+ if (!level .isEmptyBlock (new BlockPos (x , y , z )) ||
120+ (
121+ !(level .getBlockEntity (new BlockPos (x , y - 1 , z )) instanceof DronePortBlockEntity ) &&
122+ !level .isEmptyBlock (new BlockPos (x , y - 1 , z ))
123+ )
124+ )
115125 blockManager .addWall (new Cell (x , y , z ));
116126 }
117127 }
@@ -121,13 +131,11 @@ private static void addCellsFromWorld(SimpleWorldProvider blockManager, BlockPos
121131 public void portRemoved (GlobalPos pos ) {
122132 this .dronePorts .computeIfAbsent (pos .dimension ().toString (), key -> new HashMap <>()).remove (pos .pos ());
123133 List <Couple <BlockPos >> toRemove = new ArrayList <>();
124- /* Also remove corresponding savedPaths
125134 this .savedPaths .computeIfAbsent (pos .dimension ().toString (), key -> new HashMap <>()).forEach ((couple , path ) -> {
126135 if (couple .either (blockPos -> blockPos .equals (pos .pos ())))
127136 toRemove .add (couple );
128137 });
129138 toRemove .forEach (couple -> this .savedPaths .get (pos .dimension ().toString ()).remove (couple ));
130- */
131139 this .markDirty ();
132140 }
133141
0 commit comments