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'
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
'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}
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)
import unittest
from plomrogue.errors import ArgError
+from plomrogue.mapping import YX
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.
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 + ':':