X-Git-Url: https://plomlompom.com/repos/?a=blobdiff_plain;f=server_%2Fmap_.py;h=4f686edb32831fc50f79a02ec0855b0ac7502d1f;hb=52463b31fbb7f121a52f72e863a779560a6de531;hp=35d29c6b56da95fd2f25c4e35f4e85a3ff419da8;hpb=77b74e4142ae2e8a3a5637af9856e26d86f8ec76;p=plomrogue2-experiments diff --git a/server_/map_.py b/server_/map_.py index 35d29c6..4f686ed 100644 --- a/server_/map_.py +++ b/server_/map_.py @@ -3,6 +3,7 @@ sys.path.append('../') import game_common import server_.game import math +from server_.game_error import GameError class Map(game_common.Map): @@ -50,6 +51,21 @@ class Map(game_common.Map): directions += [name[5:]] return directions + def get_neighbors(self, pos): + neighbors = {} + if not hasattr(self, 'neighbors_to'): + self.neighbors_to = {} + if pos in self.neighbors_to: + return self.neighbors_to[pos] + for direction in self.get_directions(): + neighbors[direction] = None + try: + neighbors[direction] = self.move(pos, direction) + except GameError: + pass + self.neighbors_to[pos] = neighbors + return neighbors + def new_from_shape(self, init_char): import copy new_map = copy.deepcopy(self) @@ -62,7 +78,7 @@ class Map(game_common.Map): new_pos = mover(start_pos) if new_pos[0] < 0 or new_pos[1] < 0 or \ new_pos[0] >= self.size[0] or new_pos[1] >= self.size[1]: - raise server_.game.GameError('would move outside map bounds') + raise GameError('would move outside map bounds') return new_pos def move_LEFT(self, start_pos): @@ -110,34 +126,6 @@ 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 MapSquare(Map): @@ -151,19 +139,6 @@ 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 FovMap: