X-Git-Url: https://plomlompom.com/repos/berlin_corona.txt?a=blobdiff_plain;f=server_%2Fgame.py;h=07dde9d5011144f155758a0304479b1c046a94df;hb=795a476f5d4ba157f3287d7e2f5893cc91b6732e;hp=c881f8e2ad98c2aa62b62cd742e5e45226aae18f;hpb=52a7b4e9b1ef3e38701409d963f49033c23d4eaf;p=plomrogue2-experiments diff --git a/server_/game.py b/server_/game.py index c881f8e..07dde9d 100644 --- a/server_/game.py +++ b/server_/game.py @@ -1,37 +1,21 @@ import sys sys.path.append('../') import game_common +import server_.map_ class GameError(Exception): pass -def move_pos(direction, pos_yx): - if direction == 'UP': - pos_yx[0] -= 1 - elif direction == 'DOWN': - pos_yx[0] += 1 - elif direction == 'RIGHT': - pos_yx[1] += 1 - elif direction == 'LEFT': - pos_yx[1] -= 1 - - -class Map(game_common.Map): - - def get_line(self, y): - width = self.size[1] - return self.terrain[y * width:(y + 1) * width] - - class World(game_common.World): - def __init__(self): + def __init__(self, game): super().__init__() - self.Thing = Thing # use local Thing class instead of game_common's - self.map_ = Map() # use extended child class + self.game = game self.player_id = 0 + # use extended local classes + self.Thing = Thing def proceed_to_next_player_turn(self): """Run game world turns until player can decide their next step. @@ -76,15 +60,8 @@ class Task: direction = self.args[0] else: direction = self.kwargs['direction'] - test_pos = self.thing.position[:] - move_pos(direction, test_pos) - if test_pos[0] < 0 or test_pos[1] < 0 or \ - test_pos[0] >= self.thing.world.map_.size[0] or \ - test_pos[1] >= self.thing.world.map_.size[1]: - raise GameError('would move outside map bounds') - pos_i = test_pos[0] * self.thing.world.map_.size[1] + test_pos[1] - map_tile = self.thing.world.map_.terrain[pos_i] - if map_tile != '.': + test_pos = self.thing.world.map_.move(self.thing.position, direction) + if self.thing.world.map_[test_pos] != '.': raise GameError('would move into illegal terrain') for t in self.thing.world.things: if t.position == test_pos: @@ -103,7 +80,7 @@ class Thing(game_common.Thing): return 'success' def task_move(self, direction): - move_pos(direction, self.position) + self.position = self.world.map_.move(self.position, direction) return 'success' def decide_task(self): @@ -150,37 +127,26 @@ class Thing(game_common.Thing): def get_stencil(self): if self._stencil is not None: return self._stencil - size = self.world.map_.size - m = Map(self.world.map_.size, '?'*size[0]*size[1]) - y_me = self.position[0] - x_me = self.position[1] - for y in range(m.size[0]): - if y in (y_me - 1, y_me, y_me + 1): - for x in range(m.size[1]): - if x in (x_me - 1, x_me, x_me + 1): - pos = y * size[1] + x - m.terrain = m.terrain[:pos] + '.' + m.terrain[pos+1:] + m = self.world.map_.new_from_shape('?') + for pos in m: + if pos == self.position or m.are_neighbors(pos, self.position): + m[pos] = '.' self._stencil = m return self._stencil def get_visible_map(self): stencil = self.get_stencil() - size = self.world.map_.size - size_i = self.world.map_.size[0] * self.world.map_.size[1] - m = Map(size, ' '*size_i) - for i in range(size_i): - if stencil.terrain[i] == '.': - c = self.world.map_.terrain[i] - m.terrain = m.terrain[:i] + c + m.terrain[i+1:] + m = self.world.map_.new_from_shape(' ') + for pos in m: + if stencil[pos] == '.': + m[pos] = self.world.map_[pos] return m def get_visible_things(self): stencil = self.get_stencil() visible_things = [] for thing in self.world.things: - width = self.world.map_.size[1] - pos_i = thing.position[0] * width + thing.position[1] - if stencil.terrain[pos_i] == '.': + if stencil[thing.position] == '.': visible_things += [thing] return visible_things @@ -193,11 +159,12 @@ def fib(n): return fib(n-1) + fib(n-2) -class Game(game_common.Commander): +class Game(game_common.CommonCommandsMixin): def __init__(self, game_file_name): import server_.io - self.world = World() + self.map_manager = server_.map_.map_manager + self.world = World(self) self.io = server_.io.GameIO(game_file_name, self) # self.pool and self.pool_result are currently only needed by the FIB # command and the demo of a parallelized game loop in cmd_inc_p. @@ -213,11 +180,11 @@ class Game(game_common.Commander): return 'Y:' + str(tuple_[0]) + ',X:' + str(tuple_[1]) self.io.send('NEW_TURN ' + str(self.world.turn)) - self.io.send('MAP_SIZE ' + stringify_yx(self.world.map_.size)) + self.io.send('MAP ' + self.world.map_.geometry +\ + ' ' + stringify_yx(self.world.map_.size)) visible_map = self.world.get_player().get_visible_map() - for y in range(self.world.map_.size[0]): - self.io.send('VISIBLE_MAP_LINE %5s %s' % - (y, self.io.quote(visible_map.get_line(y)))) + for y, line in visible_map.lines(): + self.io.send('VISIBLE_MAP_LINE %5s %s' % (y, self.io.quote(line))) visible_things = self.world.get_player().get_visible_things() for thing in visible_things: self.io.send('THING_TYPE %s %s' % (thing.id_, thing.type_)) @@ -274,9 +241,10 @@ class Game(game_common.Commander): def cmd_MOVE(self, direction): """Set player task to 'move' with direction arg, finish player turn.""" import parser - if direction not in {'UP', 'DOWN', 'RIGHT', 'LEFT'}: - raise parser.ArgError('Move argument must be one of: ' - 'UP, DOWN, RIGHT, LEFT') + legal_directions = self.world.map_.get_directions() + if direction not in legal_directions: + raise parser.ArgError('Move argument must be one of: ' + + ', '.join(legal_directions)) self.world.get_player().set_task('move', direction=direction) self.proceed() cmd_MOVE.argtypes = 'string'