-
Notifications
You must be signed in to change notification settings - Fork 18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Pathfinding improvements #42
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,6 +39,8 @@ | |
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import static org.joml.Math.round; | ||
|
||
@RegisterSystem | ||
@Share(value = NavGraphSystem.class) | ||
public class NavGraphSystem extends BaseComponentSystem implements UpdateSubscriberSystem, WorldChangeListener { | ||
|
@@ -106,14 +108,33 @@ public WalkableBlock getBlock(EntityRef minion) { | |
} | ||
|
||
public WalkableBlock getBlock(Vector3f pos) { | ||
Vector3i blockPos = new Vector3i(pos.x + 0.25f, pos.y, pos.z + 0.25f); | ||
Vector3i blockPos = new Vector3i(round(pos.x), round(pos.y), round(pos.z)); | ||
blockPos.y += 2; //Added in case the height of minion is really low | ||
WalkableBlock block = getBlock(blockPos); | ||
if (block == null) { | ||
while (blockPos.y >= (int) pos.y - 4 && block == null) { | ||
blockPos.y--; | ||
block = getBlock(blockPos); | ||
if (block == null) { | ||
block = getBlock(blockPos); | ||
} | ||
|
||
// Checking Neighbours as minion could be hanging on the edge of a block | ||
|
||
for (int i = 0; i < 8; i++) { | ||
int dx = NavGraphChunk.DIRECTIONS[i][0]; | ||
int dz = NavGraphChunk.DIRECTIONS[i][1]; | ||
Vector3i directionVector = new Vector3i(dx, 0, dz); | ||
directionVector.add(blockPos); | ||
block = getBlock(directionVector); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This 100% of the time overwrites what's assigned in line 117. I think that means two things:
If I'm wrong there just correct me. I think this method should prefer the block directly underneath if it can find one. I also think it should only search the surrounding block closest to the entity. Consider an NPC standing on the very corner A of a 1x1 pillar. If the opposite corner C happens to be checked first then this method will return C, which in the worst case could have a very different navgraph result. As written I think this section adds more bugs than it fixes. The following psuedo code might do better.
|
||
if (block != null) { | ||
break; | ||
} | ||
} | ||
|
||
|
||
} | ||
} | ||
|
||
|
||
return block; | ||
} | ||
|
||
|
@@ -131,13 +152,15 @@ public NavGraphChunk updateChunk(Vector3i chunkPos) { | |
} | ||
NavGraphChunk navGraphChunk = heightMaps.remove(chunkPos); | ||
if (navGraphChunk != null) { | ||
navGraphChunk.disconnectNeighborMaps(getNeighbor(chunkPos, -1, 0), getNeighbor(chunkPos, 0, -1), getNeighbor(chunkPos, 1, 0), getNeighbor(chunkPos, 0, 1)); | ||
navGraphChunk.disconnectNeighborMaps(getNeighbor(chunkPos, -1, 0), getNeighbor(chunkPos, 0, -1), | ||
getNeighbor(chunkPos, 1, 0), getNeighbor(chunkPos, 0, 1)); | ||
navGraphChunk.cells = null; | ||
} | ||
navGraphChunk = new NavGraphChunk(world, chunkPos); | ||
navGraphChunk.update(); | ||
heightMaps.put(chunkPos, navGraphChunk); | ||
navGraphChunk.connectNeighborMaps(getNeighbor(chunkPos, -1, 0), getNeighbor(chunkPos, 0, -1), getNeighbor(chunkPos, 1, 0), getNeighbor(chunkPos, 0, 1)); | ||
navGraphChunk.connectNeighborMaps(getNeighbor(chunkPos, -1, 0), getNeighbor(chunkPos, 0, -1), | ||
getNeighbor(chunkPos, 1, 0), getNeighbor(chunkPos, 0, 1)); | ||
return navGraphChunk; | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change looks correct. The engine internally uses
RoundingMode.HALF_UP
to convert V3f to V3i.