X-Git-Url: https://plomlompom.com/repos/?a=blobdiff_plain;f=new2%2Fplomrogue%2Fgame.py;h=8fb264447d5c2e30f757d2d507c9e62e5a3ee84d;hb=9209d0e0f1bbc79f872ce3b2bd353f1307a7c84d;hp=82e5d181c98d3562f4bcd73b215bd714634b9813;hpb=2595413918ab08fe7aa5b0772abf2420dc89d175;p=plomrogue2-experiments diff --git a/new2/plomrogue/game.py b/new2/plomrogue/game.py index 82e5d18..8fb2644 100755 --- a/new2/plomrogue/game.py +++ b/new2/plomrogue/game.py @@ -1,6 +1,8 @@ -from plomrogue.tasks import Task_WAIT, Task_MOVE, Task_WRITE +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 +from plomrogue.commands import (cmd_ALL, cmd_LOGIN, cmd_QUERY, cmd_PING, + cmd_TURN, cmd_MAP_LINE, cmd_MAP) from plomrogue.io import GameIO from plomrogue.misc import quote from plomrogue.things import Thing, ThingPlayer @@ -31,22 +33,37 @@ class GameBase: class Game(GameBase): - def __init__(self, *args, **kwargs): + def __init__(self, save_file, *args, **kwargs): + import os super().__init__(*args, **kwargs) self.changed = True - self.io = GameIO(self) + self.io = GameIO(self, save_file) self.tasks = {'WAIT': Task_WAIT, 'MOVE': Task_MOVE, - 'WRITE': Task_WRITE} + 'WRITE': Task_WRITE, + 'FLATTEN_SURROUNDINGS': Task_FLATTEN_SURROUNDINGS} self.map_geometry = MapGeometrySquare(YX(24, 40)) self.commands = {'QUERY': cmd_QUERY, 'ALL': cmd_ALL, '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} self.sessions = {} self.map = Map(self.map_geometry.size) + if os.path.exists(self.io.save_file): + if not os.path.isfile(self.io.save_file): + raise GameError('save file path refers to non-file') + else: + with open(self.io.save_file, 'r') as f: + lines = f.readlines() + for i in range(len(lines)): + line = lines[i] + print("FILE INPUT LINE %5s: %s" % (i, line), end='') + self.io.handle_input(line) def get_string_options(self, string_option_type): import string @@ -54,7 +71,7 @@ class Game(GameBase): return self.map_geometry.get_directions() if string_option_type == 'char': return [c for c in - string.digits + string.ascii_letters + string.punctuation] + string.digits + string.ascii_letters + string.punctuation + ' '] return None def send_gamestate(self, connection_id=None): @@ -91,6 +108,7 @@ class Game(GameBase): self.turn += 1 self.send_gamestate() self.changed = False + self.save() def get_command(self, command_name): @@ -134,3 +152,18 @@ class Game(GameBase): # the more obvious indicators that it does – that's why there's # no safeguard here against this. return self.things[-1].id_ + 1 + + def save(self): + + def write(f, msg): + f.write(msg + '\n') + + 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)