From: Christian Heller Date: Fri, 25 Jan 2019 19:23:50 +0000 (+0100) Subject: Add very basic pathfinding AI. X-Git-Url: https://plomlompom.com/repos/%7B%7Bdb.prefix%7D%7D/static/templates?a=commitdiff_plain;h=4542a12bf12101e60aedea9bb9df098573bf0d25;p=plomrogue2-experiments Add very basic pathfinding AI. --- diff --git a/server_/game.py b/server_/game.py index df6a9f2..968d301 100644 --- a/server_/game.py +++ b/server_/game.py @@ -105,16 +105,70 @@ class Thing(game_common.Thing): return 'success' def decide_task(self): - #if self.position[1] > 1: - # self.set_task('move', 'LEFT') - #elif self.position[1] < 3: - # self.set_task('move', 'RIGHT') - #else: - self.set_task('wait') + visible_things = self.get_visible_things() + target = None + for t in visible_things: + if t.type_ == 'human': + target = t.position + break + if target is None: + self.set_task('wait') + return + dijkstra_map = type(self.world.map_)(self.world.map_.size) + n_max = 256 + dijkstra_map.terrain = [n_max for i in range(dijkstra_map.size_i)] + dijkstra_map[target] = 0 + shrunk = True + while shrunk: + shrunk = False + for pos in dijkstra_map: + if self.world.map_[pos] != '.': + continue + neighbors = dijkstra_map.get_neighbors(pos) + for yx in neighbors: + if yx is not None and dijkstra_map[yx] < dijkstra_map[pos] - 1: + dijkstra_map[pos] = dijkstra_map[yx] + 1 + shrunk = True + #with open('log', 'a') as f: + # f.write('---------------------------------\n') + # for y, line in dijkstra_map.lines(): + # for val in line: + # if val < 10: + # f.write(str(val)) + # elif val == 256: + # f.write('x') + # else: + # f.write('~') + # f.write('\n') + neighbors = dijkstra_map.get_neighbors(self.position) + n = n_max + dirs = dijkstra_map.get_directions() + #print('DEBUG dirs', dirs) + #print('DEBUG neighbors', neighbors) + #debug_scores = [] + #for pos in neighbors: + # if pos is None: + # debug_scores += [9000] + # else: + # debug_scores += [dijkstra_map[pos]] + #print('DEBUG debug_scores', debug_scores) + direction = None + for i_dir in range(len(neighbors)): + pos = neighbors[i_dir] + if pos is not None and dijkstra_map[pos] < n: + n = dijkstra_map[pos] + direction = dirs[i_dir] + #print('DEBUG result', direction) + if direction: + self.set_task('move', direction=direction) + #self.world.game.io.send('would move ' + direction) + else: + self.set_task('wait') + def set_task(self, task_name, *args, **kwargs): self.task = Task(self, task_name, args, kwargs) - self.task.check() + self.task.check() # will throw GameError if necessary def proceed(self, is_AI=True): """Further the thing in its tasks. diff --git a/server_/map_.py b/server_/map_.py index 96021be..2e87fc5 100644 --- a/server_/map_.py +++ b/server_/map_.py @@ -12,7 +12,10 @@ class Map(game_common.Map): def __setitem__(self, yx, c): pos_i = self.get_position_index(yx) - self.terrain = self.terrain[:pos_i] + c + self.terrain[pos_i + 1:] + if type(c) == str: + self.terrain = self.terrain[:pos_i] + c + self.terrain[pos_i + 1:] + else: + self.terrain[pos_i] = c def __iter__(self): """Iterate over YX position coordinates.""" @@ -97,9 +100,9 @@ class MapHex(Map): def move_DOWNLEFT(self, start_pos): if start_pos[0] % 2 == 1: - return [start_pos[0] + 1, start_pos[1] - 1] + return [start_pos[0] + 1, start_pos[1] - 1] else: - return [start_pos[0] + 1, start_pos[1]] + return [start_pos[0] + 1, start_pos[1]] def move_DOWNRIGHT(self, start_pos): if start_pos[0] % 2 == 1: @@ -107,6 +110,34 @@ class MapHex(Map): else: return [start_pos[0] + 1, start_pos[1] + 1] + def get_neighbors(self, pos): + # DOWNLEFT, DOWNRIGHT, LEFT, RIGHT, UPLEFT, UPRIGHT (alphabetically) + neighbors = [None, None, None, None, None, None] # e, d, c, x, s, w + if pos[1] > 0: + neighbors[2] = [pos[0], pos[1] - 1] + if pos[1] < self.size[1] - 1: + neighbors[3] = [pos[0], pos[1] + 1] + # x, c, s, d, w, e # 3->0, 2->1, 5->4, 0->5 + if pos[0] % 2 == 1: + if pos[0] > 0 and pos[1] > 0: + neighbors[4] = [pos[0] - 1, pos[1] - 1] + if pos[0] < self.size[0] - 1 and pos[1] > 0: + neighbors[0] = [pos[0] + 1, pos[1] - 1] + if pos[0] > 0: + neighbors[5] = [pos[0] - 1, pos[1]] + if pos[0] < self.size[0] - 1: + neighbors[1] = [pos[0] + 1, pos[1]] + else: + if pos[0] > 0 and pos[1] < self.size[1] - 1: + neighbors[5] = [pos[0] - 1, pos[1] + 1] + if pos[0] < self.size[0] - 1 and pos[1] < self.size[1] - 1: + neighbors[1] = [pos[0] + 1, pos[1] + 1] + if pos[0] > 0: + neighbors[4] = [pos[0] - 1, pos[1]] + if pos[0] < self.size[0] - 1: + neighbors[0] = [pos[0] + 1, pos[1]] + return neighbors + class MapFovHex(MapHex): @@ -231,6 +262,19 @@ class MapSquare(Map): def move_DOWN(self, start_pos): return [start_pos[0] + 1, start_pos[1]] + def get_neighbors(self, pos): + # DOWN, LEFT, RIGHT, UP (alphabetically) + neighbors = [None, None, None, None] + if pos[0] > 0: + neighbors[3] = [pos[0] - 1, pos[1]] + if pos[1] > 0: + neighbors[1] = [pos[0], pos[1] - 1] + if pos[0] < self.size[0] - 1: + neighbors[0] = [pos[0] + 1, pos[1]] + if pos[1] < self.size[1] - 1: + neighbors[2] = [pos[0], pos[1] + 1] + return neighbors + class MapFovSquare(MapSquare): """Just a marginally and unsatisfyingly adapted variant of MapFovHex."""