Improve pathfinding

This commit is contained in:
Tanner Collin 2020-10-12 23:25:26 -06:00
parent fa9d597483
commit 83132ab2bb
3 changed files with 7 additions and 29 deletions

View File

@ -224,8 +224,8 @@ class Game:
solution = path.Pathfinder(self.g.chunks).astar(utils.pint(self.g.pos), utils.pint(self.g.goal)) solution = path.Pathfinder(self.g.chunks).astar(utils.pint(self.g.pos), utils.pint(self.g.goal))
if solution: if solution:
solution = list(solution) solution = list(solution)
self.g.path = solution #self.g.path = solution
self.g.job.state = self.g.job.stop #self.g.job.state = self.g.job.stop
print(len(solution)) print(len(solution))
print(solution) print(solution)
print(round(time.time() - start, 3), 'seconds') print(round(time.time() - start, 3), 'seconds')
@ -550,6 +550,7 @@ class Game:
if entry.value.item_id in items.GAPPLE_ID: if entry.value.item_id in items.GAPPLE_ID:
self.g.chat.send('gapple found: ' + str(current_chest)[1:-1]) self.g.chat.send('gapple found: ' + str(current_chest)[1:-1])
print('gapple found:', str(current_chest)[1:-1])
def handle_spawn_living(self, packet): def handle_spawn_living(self, packet):

21
path.py
View File

@ -1,7 +1,7 @@
import importlib import importlib
import functools import functools
import time import time
from math import hypot from math import hypot, sqrt
from astar import AStar from astar import AStar
@ -117,21 +117,6 @@ HALF_PARKOUR = {
(-2, 0, 0): (-1, 0, 0), (-2, 0, 0): (-1, 0, 0),
} }
HYPOT_LUT = {
(0, -1): 1.0,
(0, 1): 1.0,
(1, 0): 1.0,
(-1, 0): 1.0,
(1, -1): 1.414,
(-1, -1): 1.414,
(1, 1): 1.414,
(-1, 1): 1.414,
(0, 2): 2.0,
(-2, 0): 2.0,
(2, 0): 2.0,
(0, -2): 2.0,
}
# larger started being slower # larger started being slower
BLOCK_CACHE_SIZE = 2**14 BLOCK_CACHE_SIZE = 2**14
@ -300,9 +285,9 @@ class Pathfinder(AStar):
def distance_between(self, n1, n2): def distance_between(self, n1, n2):
(x1, y1, z1) = n1 (x1, y1, z1) = n1
(x2, y2, z2) = n2 (x2, y2, z2) = n2
return HYPOT_LUT[x2 - x1, z2 - z1] return hypot(x2-x1, y2-y1, z2-z1)
def heuristic_cost_estimate(self, n1, n2): def heuristic_cost_estimate(self, n1, n2):
(x1, y1, z1) = n1 (x1, y1, z1) = n1
(x2, y2, z2) = n2 (x2, y2, z2) = n2
return hypot(x2 - x1, z2 - z1) return abs(x2-x1) + abs(y2-y1) + abs(z2-z1)

View File

@ -126,22 +126,14 @@ class ChunksManager:
return True return True
def unload_chunks(self, position): def unload_chunks(self, position):
start = time.time()
x, y, z = utils.pint(position) x, y, z = utils.pint(position)
player_chunk = (x//16, 0, z//16) player_chunk = (x//16, 0, z//16)
loaded_chunks = list(self.chunks.keys()) loaded_chunks = list(self.chunks.keys())
count = 0
for chunk in loaded_chunks: for chunk in loaded_chunks:
check = (chunk[0], 0, chunk[2]) check = (chunk[0], 0, chunk[2])
if utils.phyp_king(player_chunk, check) > 16: if utils.phyp_king(player_chunk, check) > 20:
del self.chunks[chunk] del self.chunks[chunk]
count += 1
if count:
print('unloaded', count, 'chunks in', time.time()-start, 's')
class ChunkNotLoadedException(Exception): class ChunkNotLoadedException(Exception):