home · contact · privacy
Enforce sane create_unfound decisions.
[plomrogue2-experiments] / new / example_client.py
index 230b045ac916deead9c79a21e5780664b7c51454..19538161a5b3d575df3d80fe9cfe77c42f3b2f26 100755 (executable)
@@ -4,15 +4,15 @@ import socket
 import threading
 from plomrogue.parser import ArgError, Parser
 from plomrogue.commands import cmd_PLAYER_ID, cmd_THING_HEALTH
-from plomrogue.game import Game, WorldBase
-from plomrogue.mapping import MapHex, YX
+from plomrogue.game import GameBase
+from plomrogue.mapping import Map, MapGeometryHex, YX
 from plomrogue.io import PlomSocket
 from plomrogue.things import ThingBase
 import types
 import queue
 
 
-class ClientMap(MapHex):
+class ClientMap(Map):
 
     def y_cut(self, map_lines, center_y, view_height):
         map_height = len(map_lines)
@@ -36,7 +36,9 @@ class ClientMap(MapHex):
     def format_to_view(self, map_cells, center, size):
 
         def map_cells_to_lines(map_cells):
-            map_view_chars = ['0']
+            map_view_chars = []
+            if self.start_indented:
+                map_view_chars += ['0']
             x = 0
             y = 0
             for cell in map_cells:
@@ -49,49 +51,20 @@ class ClientMap(MapHex):
                     map_view_chars += ['\n']
                     x = 0
                     y += 1
-                    if y % 2 == 0:
+                    if y % 2 == int(not self.start_indented):
                         map_view_chars += ['0']
-            if y % 2 == 0:
+            if y % 2 == int(not self.start_indented):
                 map_view_chars = map_view_chars[:-1]
             map_view_chars = map_view_chars[:-1]
             return ''.join(map_view_chars).split('\n')
 
         map_lines = map_cells_to_lines(map_cells)
-        if len(map_lines) % 2 == 0:
-            map_lines = map_lines[1:]
-        else:
-            for i in range(len(map_lines)):
-                map_lines[i] = '0' + map_lines[i]
-        self.y_cut(map_lines, center.y, size.y)
+        self.y_cut(map_lines, center[1].y, size.y)
         map_width = self.size.x * 2 + 1
-        self.x_cut(map_lines, center.x * 2, size.x, map_width)
+        self.x_cut(map_lines, center[1].x * 2, size.x, map_width)
         return map_lines
 
 
-class World(WorldBase):
-
-    def __init__(self, *args, **kwargs):
-        """Extend original with local classes and empty default map.
-
-        We need the empty default map because we draw the map widget
-        on any update, even before we actually receive map data.
-        """
-        super().__init__(*args, **kwargs)
-        self.map_ = ClientMap()
-        self.offset = YX(0,0)
-        self.player_inventory = []
-        self.player_id = 0
-        self.pickable_items = []
-
-    def new_map(self, offset, size):
-        self.map_ = ClientMap(size)
-        self.offset = offset
-
-    @property
-    def player(self):
-        return self.get_thing(self.player_id)
-
-
 def cmd_LAST_PLAYER_TASK_RESULT(game, msg):
     if msg != "success":
         game.log(msg)
@@ -106,19 +79,20 @@ cmd_TURN_FINISHED.argtypes = 'int:nonneg'
 
 def cmd_TURN(game, n):
     """Set game.turn to n, empty game.things."""
-    game.world.turn = n
-    game.world.things = []
-    game.world.pickable_items[:] = []
+    game.log_text = ''
+    game.turn = n
+    game.things = []
+    game.pickable_items[:] = []
 cmd_TURN.argtypes = 'int:nonneg'
 
 
-def cmd_VISIBLE_MAP(game, offset, size):
-    game.world.new_map(offset, size)
-cmd_VISIBLE_MAP.argtypes = 'yx_tuple yx_tuple:pos'
+def cmd_VISIBLE_MAP(game, size, indent_first_line):
+    game.new_map(size, indent_first_line)
+cmd_VISIBLE_MAP.argtypes = 'yx_tuple:pos bool'
 
 
 def cmd_VISIBLE_MAP_LINE(game, y, terrain_line):
-    game.world.map_.set_line(y, terrain_line)
+    game.map_.set_line(y, terrain_line)
 cmd_VISIBLE_MAP_LINE.argtypes = 'int:nonneg string'
 
 
@@ -129,34 +103,40 @@ def cmd_GAME_STATE_COMPLETE(game):
 
 
 def cmd_THING_TYPE(game, i, type_):
-    t = game.world.get_thing(i)
+    t = game.get_thing(i, create_unfound=True)
     t.type_ = type_
 cmd_THING_TYPE.argtypes = 'int:nonneg string'
 
 
 def cmd_THING_POS(game, i, yx):
-    t = game.world.get_thing(i)
-    t.position = yx
+    t = game.get_thing(i,create_unfound=True)
+    t.position = YX(0,0), yx
 cmd_THING_POS.argtypes = 'int:nonneg yx_tuple:nonneg'
 
 
 def cmd_PLAYER_INVENTORY(game, ids):
-    game.world.player_inventory[:] = ids  # TODO: test whether valid IDs
+    game.player_inventory[:] = ids  # TODO: test whether valid IDs
     game.tui.to_update['inventory'] = True
 cmd_PLAYER_INVENTORY.argtypes = 'seq:int:nonneg'
 
 
 def cmd_PICKABLE_ITEMS(game, ids):
-    game.world.pickable_items[:] = ids
+    game.pickable_items[:] = ids
     game.tui.to_update['pickable_items'] = True
 cmd_PICKABLE_ITEMS.argtypes = 'seq:int:nonneg'
 
 
-class Game:
+class Game(GameBase):
 
-    def __init__(self):
+    def __init__(self, *args, **kwargs):
+        super().__init__(*args, **kwargs)
+        self.map_ = ClientMap()  # we need an empty default map cause we draw
+        self.offset = YX(0,0)    # the map widget even before we get a real one
+        self.player_inventory = []
+        self.player_id = 0
+        self.pickable_items = []
         self.parser = Parser(self)
-        self.world = World(self)
+        self.map_geometry = MapGeometryHex()
         self.thing_type = ThingBase
         self.commands = {'LAST_PLAYER_TASK_RESULT': cmd_LAST_PLAYER_TASK_RESULT,
                          'TURN_FINISHED': cmd_TURN_FINISHED,
@@ -174,6 +154,13 @@ class Game:
         self.do_quit = False
         self.tui = None
 
+    def new_map(self, size, indent_first_line):
+        self.map_ = ClientMap(size, start_indented=indent_first_line)
+
+    @property
+    def player(self):
+        return self.get_thing(self.player_id, create_unfound=False)
+
     def get_command(self, command_name):
         from functools import partial
         if command_name in self.commands:
@@ -203,8 +190,6 @@ class Game:
     def log(self, msg):
         """Prefix msg plus newline to self.log_text."""
         self.log_text = msg + '\n' + self.log_text
-        with open('log', 'w') as f:
-            f.write(self.log_text)
         self.tui.to_update['log'] = True
 
     def symbol_for_type(self, type_):
@@ -337,11 +322,11 @@ class DescriptorWidget(TextLinesWidget):
 
     def get_text_lines(self):
         lines = []
-        pos_i = self.tui.game.world.map_.\
-                get_position_index(self.tui.examiner_position)
-        terrain = self.tui.game.world.map_.terrain[pos_i]
+        pos_i = self.tui.game.map_.\
+                get_position_index(self.tui.examiner_position[1])
+        terrain = self.tui.game.map_.terrain[pos_i]
         lines = [terrain]
-        for t in self.tui.game.world.things_at_pos(self.tui.examiner_position):
+        for t in self.tui.game.things_at_pos(self.tui.examiner_position):
             lines += [t.type_]
         return lines
 
@@ -385,7 +370,7 @@ class ItemsSelectorWidget(Widget):
         counter = 0
         for id_ in self.selection:
             pointer = '*' if counter == self.tui.item_pointer else ' '
-            t = self.tui.game.world.get_thing(id_)
+            t = self.tui.game.get_thing(id_, create_unfound=False)
             lines += ['%s %s' % (pointer, t.type_)]
             counter += 1
         line_width = self.size.x
@@ -403,12 +388,11 @@ class MapWidget(Widget):
     def draw(self):
 
         def annotated_terrain():
-            terrain_as_list = list(self.tui.game.world.map_.terrain[:])
-            for t in self.tui.game.world.things:
-                if t.id_ in self.tui.game.world.player_inventory:
+            terrain_as_list = list(self.tui.game.map_.terrain[:])
+            for t in self.tui.game.things:
+                if t.id_ in self.tui.game.player_inventory:
                     continue
-                pos_i = self.tui.game.world.map_.\
-                        get_position_index(t.position)
+                pos_i = self.tui.game.map_.get_position_index(t.position[1])
                 symbol = self.tui.game.symbol_for_type(t.type_)
                 if terrain_as_list[pos_i][0] in {'f', '@', 'm'}:
                     old_symbol = terrain_as_list[pos_i][0]
@@ -418,8 +402,8 @@ class MapWidget(Widget):
                 else:
                     terrain_as_list[pos_i] = symbol
             if self.tui.examiner_mode:
-                pos_i = self.tui.game.world.map_.\
-                        get_position_index(self.tui.examiner_position)
+                pos_i = self.tui.game.map_.\
+                        get_position_index(self.tui.examiner_position[1])
                 terrain_as_list[pos_i] = (terrain_as_list[pos_i][0], '?')
             return terrain_as_list
 
@@ -455,17 +439,17 @@ class MapWidget(Widget):
                     chars_with_attrs += [c]
             return chars_with_attrs
 
-        if self.tui.game.world.map_.terrain == '':
+        if self.tui.game.map_.terrain == '':
             lines = []
             pad_y(lines)
             self.safe_write(''.join(lines))
             return
 
         annotated_terrain = annotated_terrain()
-        center = self.tui.game.world.player.position
+        center = self.tui.game.player.position
         if self.tui.examiner_mode:
             center = self.tui.examiner_position
-        lines = self.tui.game.world.map_.\
+        lines = self.tui.game.map_.\
                 format_to_view(annotated_terrain, center, self.size)
         pad_or_cut_x(lines)
         pad_y(lines)
@@ -475,14 +459,14 @@ class MapWidget(Widget):
 class TurnWidget(Widget):
 
     def draw(self):
-        self.safe_write((str(self.tui.game.world.turn), curses.color_pair(2)))
+        self.safe_write((str(self.tui.game.turn), curses.color_pair(2)))
 
 
 class HealthWidget(Widget):
 
     def draw(self):
-        if hasattr(self.tui.game.world.player, 'health'):
-            self.safe_write((str(self.tui.game.world.player.health),
+        if hasattr(self.tui.game.player, 'health'):
+            self.safe_write((str(self.tui.game.player.health),
                              curses.color_pair(2)))
 
 
@@ -550,7 +534,7 @@ class TUI:
                 return True
 
             selectables_menu(key, pickable_items_widget,
-                             self.game.world.pickable_items, f)
+                             self.game.pickable_items, f)
 
         def inventory_menu(key):
 
@@ -566,12 +550,14 @@ class TUI:
                 return True
 
             selectables_menu(key, inventory_widget,
-                             self.game.world.player_inventory, f)
+                             self.game.player_inventory, f)
 
         def move_examiner(direction):
             start_pos = self.examiner_position
-            new_examine_pos = self.game.world.map_.move(start_pos, direction)
-            if new_examine_pos:
+            new_examine_pos = self.game.map_geometry.move(start_pos, direction,
+                                                          self.game.map_.size,
+                                                          self.game.map_.start_indented)
+            if new_examine_pos[0] == (0,0):
                 self.examiner_position = new_examine_pos
             self.to_update['map'] = True
 
@@ -588,7 +574,7 @@ class TUI:
                 switch_widgets(descriptor_widget, log_widget)
             else:
                 self.examiner_mode = True
-                self.examiner_position = self.game.world.player.position
+                self.examiner_position = self.game.player.position
                 switch_widgets(log_widget, descriptor_widget)
             self.to_update['map'] = True
 
@@ -669,11 +655,11 @@ class TUI:
                                              ['map'], False)
         map_widget = MapWidget(self, YX(0, 21), YX(None, None), ['map'])
         inventory_widget = ItemsSelectorWidget('INVENTORY:',
-                                               self.game.world.player_inventory,
+                                               self.game.player_inventory,
                                                self, YX(0, 21), YX(None, None),
                                                ['inventory'], False)
         pickable_items_widget = ItemsSelectorWidget('PICKABLE:',
-                                                    self.game.world.pickable_items,
+                                                    self.game.pickable_items,
                                                     self, YX(0, 21),
                                                     YX(None, None),
                                                     ['pickable_items'],