From fee7032748faad3feab219c88c75ec79725ed62c Mon Sep 17 00:00:00 2001 From: Christian Heller Date: Tue, 27 Oct 2020 02:02:19 +0100 Subject: [PATCH] Add MAP command to explicitely set map size. --- new2/plomrogue/commands.py | 4 ++++ new2/plomrogue/game.py | 8 +++++++- new2/plomrogue/parser.py | 29 +++++++++++++++++++++++++++++ 3 files changed, 40 insertions(+), 1 deletion(-) diff --git a/new2/plomrogue/commands.py b/new2/plomrogue/commands.py index 8175b2e..2ca6230 100644 --- a/new2/plomrogue/commands.py +++ b/new2/plomrogue/commands.py @@ -52,3 +52,7 @@ cmd_TURN.argtypes = 'int:nonneg' def cmd_MAP_LINE(game, y, line): game.map.set_line(y, line) cmd_MAP_LINE.argtypes = 'int:nonneg string' + +def cmd_MAP(game, size): + game.new_map(size) +cmd_MAP.argtypes = 'yx_tuple:pos' diff --git a/new2/plomrogue/game.py b/new2/plomrogue/game.py index 55f7c72..52e0d60 100755 --- a/new2/plomrogue/game.py +++ b/new2/plomrogue/game.py @@ -2,7 +2,7 @@ from plomrogue.tasks import (Task_WAIT, Task_MOVE, Task_WRITE, Task_FLATTEN_SURROUNDINGS) from plomrogue.errors import GameError from plomrogue.commands import (cmd_ALL, cmd_LOGIN, cmd_QUERY, cmd_PING, - cmd_TURN, cmd_MAP_LINE) + cmd_TURN, cmd_MAP_LINE, cmd_MAP) from plomrogue.io import GameIO from plomrogue.misc import quote from plomrogue.things import Thing, ThingPlayer @@ -48,6 +48,7 @@ class Game(GameBase): 'LOGIN': cmd_LOGIN, 'TURN': cmd_TURN, 'MAP_LINE': cmd_MAP_LINE, + 'MAP': cmd_MAP, 'PING': cmd_PING} self.thing_type = Thing self.thing_types = {'player': ThingPlayer} @@ -159,5 +160,10 @@ class Game(GameBase): with open(self.io.save_file, 'w') as f: write(f, 'TURN %s' % self.turn) + write(f, 'MAP %s' % (self.map_geometry.size,)) for y, line in self.map.lines(): write(f, 'MAP_LINE %5s %s' % (y, quote(line))) + + def new_map(self, size): + self.map_geometry = MapGeometrySquare(YX(size.y, size.x)) + self.map = Map(self.map_geometry.size) diff --git a/new2/plomrogue/parser.py b/new2/plomrogue/parser.py index f23a7aa..b350ee5 100644 --- a/new2/plomrogue/parser.py +++ b/new2/plomrogue/parser.py @@ -1,5 +1,6 @@ import unittest from plomrogue.errors import ArgError +from plomrogue.mapping import YX class Parser: @@ -40,6 +41,32 @@ class Parser: tokens += [token] return tokens + def parse_yx_tuple(self, yx_string, range_=None): + """Parse yx_string as yx_tuple, return result. + + The range_ argument may be 'nonneg' (non-negative, including + 0) or 'pos' (positive, excluding 0). + + """ + + def get_axis_position_from_argument(axis, token): + if len(token) < 3 or token[:2] != axis + ':' or \ + not (token[2:].isdigit() or token[2] == '-'): + raise ArgError('Non-int arg for ' + axis + ' position.') + n = int(token[2:]) + if n < 1 and range_ == 'pos': + raise ArgError('Arg for ' + axis + ' position < 1.') + elif n < 0 and range_ == 'nonneg': + raise ArgError('Arg for ' + axis + ' position < 0.') + return n + + tokens = yx_string.split(',') + if len(tokens) != 2: + raise ArgError('Wrong number of yx-tuple arguments.') + y = get_axis_position_from_argument('Y', tokens[0]) + x = get_axis_position_from_argument('X', tokens[1]) + return YX(y, x) + def parse(self, msg): """Parse msg as call to function, return function with args tuple. @@ -79,6 +106,8 @@ class Parser: if not arg.isdigit(): raise ArgError('Argument must be non-negative integer.') args += [int(arg)] + elif tmpl == 'yx_tuple:pos': + args += [self.parse_yx_tuple(arg, 'pos')] elif tmpl == string_string: args += [arg] elif tmpl[:len(string_string) + 1] == string_string + ':': -- 2.30.2