home · contact · privacy
Make add_line hack unnecessary.
[plomrogue2-experiments] / new / example_client.py
index 230b045ac916deead9c79a21e5780664b7c51454..a88c85b3c2d642f2a7a9cce5dc3be95f46b6e010 100755 (executable)
@@ -5,14 +5,14 @@ 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.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)
@@ -33,10 +33,12 @@ class ClientMap(MapHex):
                 cut_end = cut_start + view_width
             map_lines[:] = [line[cut_start:cut_end] for line in map_lines]
 
-    def format_to_view(self, map_cells, center, size):
+    def format_to_view(self, map_cells, center, size, indent_first_line):
 
         def map_cells_to_lines(map_cells):
-            map_view_chars = ['0']
+            map_view_chars = []
+            if indent_first_line:
+                map_view_chars += ['0']
             x = 0
             y = 0
             for cell in map_cells:
@@ -49,22 +51,17 @@ class ClientMap(MapHex):
                     map_view_chars += ['\n']
                     x = 0
                     y += 1
-                    if y % 2 == 0:
+                    if y % 2 == int(not indent_first_line):
                         map_view_chars += ['0']
-            if y % 2 == 0:
+            if y % 2 == int(not indent_first_line):
                 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
 
 
@@ -136,7 +133,7 @@ cmd_THING_TYPE.argtypes = 'int:nonneg string'
 
 def cmd_THING_POS(game, i, yx):
     t = game.world.get_thing(i)
-    t.position = yx
+    t.position = YX(0,0), yx
 cmd_THING_POS.argtypes = 'int:nonneg yx_tuple:nonneg'
 
 
@@ -157,6 +154,7 @@ class Game:
     def __init__(self):
         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,
@@ -203,8 +201,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_):
@@ -338,7 +334,7 @@ class DescriptorWidget(TextLinesWidget):
     def get_text_lines(self):
         lines = []
         pos_i = self.tui.game.world.map_.\
-                get_position_index(self.tui.examiner_position)
+                get_position_index(self.tui.examiner_position[1])
         terrain = self.tui.game.world.map_.terrain[pos_i]
         lines = [terrain]
         for t in self.tui.game.world.things_at_pos(self.tui.examiner_position):
@@ -408,7 +404,7 @@ class MapWidget(Widget):
                 if t.id_ in self.tui.game.world.player_inventory:
                     continue
                 pos_i = self.tui.game.world.map_.\
-                        get_position_index(t.position)
+                        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]
@@ -419,7 +415,7 @@ class MapWidget(Widget):
                     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)
+                        get_position_index(self.tui.examiner_position[1])
                 terrain_as_list[pos_i] = (terrain_as_list[pos_i][0], '?')
             return terrain_as_list
 
@@ -465,8 +461,10 @@ class MapWidget(Widget):
         center = self.tui.game.world.player.position
         if self.tui.examiner_mode:
             center = self.tui.examiner_position
+        indent_first_line = not bool(self.tui.game.world.offset.y % 2)
         lines = self.tui.game.world.map_.\
-                format_to_view(annotated_terrain, center, self.size)
+                format_to_view(annotated_terrain, center, self.size,
+                               indent_first_line)
         pad_or_cut_x(lines)
         pad_y(lines)
         self.safe_write(lines_to_colored_chars(lines))
@@ -570,8 +568,9 @@ class TUI:
 
         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.world.map_.size)
+            if new_examine_pos[0] == (0,0):
                 self.examiner_position = new_examine_pos
             self.to_update['map'] = True