From: Christian Heller Date: Thu, 28 Feb 2019 01:13:41 +0000 (+0100) Subject: Use tuples for positions; fix inventory saving bug. X-Git-Url: https://plomlompom.com/repos/%7B%7Bdb.prefix%7D%7D/ledger?a=commitdiff_plain;h=65e83c99b95a619afc79e8984e6f5027bc7aac1b;p=plomrogue2-experiments Use tuples for positions; fix inventory saving bug. --- diff --git a/new/plomrogue/commands.py b/new/plomrogue/commands.py index ad53438..3ae5958 100644 --- a/new/plomrogue/commands.py +++ b/new/plomrogue/commands.py @@ -38,13 +38,13 @@ cmd_THING_TYPE.argtypes = 'int:nonneg string:thingtype' def cmd_THING_POS(game, i, yx): t = game.world.get_thing(i) - t.position = list(yx) + t.position = tuple(yx) cmd_THING_POS.argtypes = 'int:nonneg yx_tuple:nonneg' def cmd_THING_INVENTORY(game, id_, ids): t = game.world.get_thing(id_) - t.inventory = [ids] # TODO: test whether valid IDs -cmd_THING_INVENTORY.argtypes = 'int:nonneg, seq:int:nonneg' + t.inventory = ids # TODO: test whether valid IDs +cmd_THING_INVENTORY.argtypes = 'int:nonneg seq:int:nonneg' def cmd_TERRAIN_LINE(game, y, terrain_line): game.world.map_.set_line(y, terrain_line) @@ -84,8 +84,11 @@ def cmd_SAVE(game): write(f, 'THING_TYPE %s %s' % (thing.id_, thing.type_)) write(f, 'THING_POS %s %s' % (thing.id_, stringify_yx(thing.position))) - write(f, 'THING_INVENTORY %s %s' % - (thing.id_,','.join([str(i) for i in thing.inventory]))) + if len(thing.inventory) > 0: + write(f, 'THING_INVENTORY %s %s' % + (thing.id_,','.join([str(i) for i in thing.inventory]))) + else: + write(f, 'THING_INVENTORY %s ,' % thing.id_) if hasattr(thing, 'task'): task = thing.task if task is not None: diff --git a/new/plomrogue/game.py b/new/plomrogue/game.py index bc5b8c3..e24edbb 100755 --- a/new/plomrogue/game.py +++ b/new/plomrogue/game.py @@ -2,6 +2,7 @@ from plomrogue.tasks import Task_WAIT, Task_MOVE, Task_PICKUP, Task_DROP from plomrogue.errors import ArgError from plomrogue.commands import (cmd_GEN_WORLD, cmd_GET_GAMESTATE, cmd_MAP, cmd_MAP, cmd_THING_TYPE, cmd_THING_POS, + cmd_THING_INVENTORY, cmd_TERRAIN_LINE, cmd_PLAYER_ID, cmd_TURN, cmd_SWITCH_PLAYER, cmd_SAVE) from plomrogue.mapping import MapHex @@ -77,8 +78,8 @@ class World(WorldBase): def add_thing(type_): t = self.game.thing_types[type_](self) - t.position = [random.randint(0, yx[0] -1), - random.randint(0, yx[1] - 1)] + t.position = (random.randint(0, yx[0] -1), + random.randint(0, yx[1] - 1)) self.things += [t] return t @@ -116,6 +117,7 @@ class Game: 'MAP': cmd_MAP, 'THING_TYPE': cmd_THING_TYPE, 'THING_POS': cmd_THING_POS, + 'THING_INVENTORY': cmd_THING_INVENTORY, 'TERRAIN_LINE': cmd_TERRAIN_LINE, 'PLAYER_ID': cmd_PLAYER_ID, 'TURN': cmd_TURN, diff --git a/new/plomrogue/mapping.py b/new/plomrogue/mapping.py index e21def8..64dad7c 100644 --- a/new/plomrogue/mapping.py +++ b/new/plomrogue/mapping.py @@ -43,7 +43,7 @@ class Map(MapBase): """Iterate over YX position coordinates.""" for y in range(self.size[0]): for x in range(self.size[1]): - yield [y, x] + yield (y, x) def lines(self): width = self.size[1] @@ -96,20 +96,20 @@ class Map(MapBase): class MapWithLeftRightMoves(Map): def move_LEFT(self, start_pos): - return [start_pos[0], start_pos[1] - 1] + return (start_pos[0], start_pos[1] - 1) def move_RIGHT(self, start_pos): - return [start_pos[0], start_pos[1] + 1] + return (start_pos[0], start_pos[1] + 1) class MapSquare(MapWithLeftRightMoves): def move_UP(self, start_pos): - return [start_pos[0] - 1, start_pos[1]] + return (start_pos[0] - 1, start_pos[1]) def move_DOWN(self, start_pos): - return [start_pos[0] + 1, start_pos[1]] + return (start_pos[0] + 1, start_pos[1]) @@ -121,27 +121,27 @@ class MapHex(MapWithLeftRightMoves): def move_UPLEFT(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_UPRIGHT(self, start_pos): if start_pos[0] % 2 == 1: - return [start_pos[0] - 1, start_pos[1]] + return (start_pos[0] - 1, start_pos[1]) else: - return [start_pos[0] - 1, start_pos[1] + 1] + return (start_pos[0] - 1, start_pos[1] + 1) 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: - return [start_pos[0] + 1, start_pos[1]] + return (start_pos[0] + 1, start_pos[1]) else: - return [start_pos[0] + 1, start_pos[1] + 1] + return (start_pos[0] + 1, start_pos[1] + 1) @@ -225,11 +225,11 @@ class FovMap: def basic_circle_out_move(self, pos, direction): """Move position pos into direction. Return whether still in map.""" mover = getattr(self, 'move_' + direction) - pos[:] = mover(pos) + pos = mover(pos) if pos[0] < 0 or pos[1] < 0 or \ pos[0] >= self.size[0] or pos[1] >= self.size[1]: - return False - return True + return pos, False + return pos, True def circle_out(self, yx, f): # Optimization potential: Precalculate movement positions. (How to check @@ -246,11 +246,12 @@ class FovMap: #print('DEBUG CIRCLE_OUT', yx) while circle_in_map: circle_in_map = False - self.basic_circle_out_move(yx, 'RIGHT') + yx, _ = self.basic_circle_out_move(yx, 'RIGHT') for dir_i in range(len(self.circle_out_directions)): for dir_progress in range(distance): direction = self.circle_out_directions[dir_i] - if self.circle_out_move(yx, direction): + yx, test = self.circle_out_move(yx, direction) + if test: f(yx, distance, dir_i, dir_progress) circle_in_map = True distance += 1 diff --git a/new/plomrogue/tasks.py b/new/plomrogue/tasks.py index 5dc2f82..1be5b6d 100644 --- a/new/plomrogue/tasks.py +++ b/new/plomrogue/tasks.py @@ -48,7 +48,7 @@ class Task_MOVE(Task): self.args[0]) for id_ in self.thing.inventory: t = self.thing.world.get_thing(id_) - t.position[:] = self.thing.position + t.position = self.thing.position diff --git a/new/plomrogue/things.py b/new/plomrogue/things.py index 9bc8490..a631b17 100644 --- a/new/plomrogue/things.py +++ b/new/plomrogue/things.py @@ -5,7 +5,7 @@ from plomrogue.errors import GameError class ThingBase: type_ = '?' - def __init__(self, world, id_=None, position=[0,0]): + def __init__(self, world, id_=None, position=(0,0)): self.world = world self.position = position if id_ is None: