home · contact · privacy
Allow item-in-vicinity selection for item pickup.
[plomrogue2-experiments] / new / example_client.py
index aae66cf9367ccfabbacabdc6aeb390c1a17b7c71..1e910704fe5104a767f9cd63c1215b00ef92e865 100755 (executable)
@@ -3,7 +3,7 @@ import curses
 import socket
 import threading
 from plomrogue.parser import ArgError, Parser
-from plomrogue.commands import cmd_MAP, cmd_THING_POS
+from plomrogue.commands import cmd_MAP, cmd_THING_POS, cmd_PLAYER_ID
 from plomrogue.game import Game, WorldBase
 from plomrogue.mapping import MapBase
 from plomrogue.io import PlomSocket
@@ -69,12 +69,17 @@ class World(WorldBase):
         """
         super().__init__(*args, **kwargs)
         self.map_ = Map()
-        self.player_position = (0, 0)
         self.player_inventory = []
+        self.player_id = 0
+        self.pickable_items = []
 
     def new_map(self, yx):
         self.map_ = Map(yx)
 
+    @property
+    def player(self):
+        return self.get_thing(self.player_id)
+
 
 def cmd_LAST_PLAYER_TASK_RESULT(game, msg):
     if msg != "success":
@@ -90,6 +95,7 @@ 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.to_update['turn'] = False
     game.to_update['map'] = False
 cmd_TURN.argtypes = 'int:nonneg'
@@ -98,10 +104,6 @@ def cmd_VISIBLE_MAP_LINE(game, y, terrain_line):
     game.world.map_.set_line(y, terrain_line)
 cmd_VISIBLE_MAP_LINE.argtypes = 'int:nonneg string'
 
-def cmd_PLAYER_POS(game, yx):
-    game.world.player_position = yx
-cmd_PLAYER_POS.argtypes = 'yx_tuple:pos'
-
 def cmd_GAME_STATE_COMPLETE(game):
     game.to_update['turn'] = True
     game.to_update['map'] = True
@@ -112,9 +114,14 @@ def cmd_THING_TYPE(game, i, type_):
 cmd_THING_TYPE.argtypes = 'int:nonneg string'
 
 def cmd_PLAYER_INVENTORY(game, ids):
-    game.world.player_inventory = [ids]  # TODO: test whether valid IDs
+    game.world.player_inventory = ids  # TODO: test whether valid IDs
 cmd_PLAYER_INVENTORY.argtypes = 'seq:int:nonneg'
 
+def cmd_PICKABLE_ITEMS(game, ids):
+    game.world.pickable_items = ids
+    game.to_update['map'] = True
+cmd_PICKABLE_ITEMS.argtypes = 'seq:int:nonneg'
+
 
 class Game:
 
@@ -126,9 +133,11 @@ class Game:
                          'TURN_FINISHED': cmd_TURN_FINISHED,
                          'TURN': cmd_TURN,
                          'VISIBLE_MAP_LINE': cmd_VISIBLE_MAP_LINE,
-                         'PLAYER_POS': cmd_PLAYER_POS,
+                         'PLAYER_ID': cmd_PLAYER_ID,
+                         'PLAYER_INVENTORY': cmd_PLAYER_INVENTORY,
                          'GAME_STATE_COMPLETE': cmd_GAME_STATE_COMPLETE,
                          'MAP': cmd_MAP,
+                         'PICKABLE_ITEMS': cmd_PICKABLE_ITEMS,
                          'THING_TYPE': cmd_THING_TYPE,
                          'THING_POS': cmd_THING_POS}
         self.log_text = ''
@@ -138,6 +147,7 @@ class Game:
             'turn': True,
             }
         self.do_quit = False
+        self.to_update_lock = False
 
     def get_command(self, command_name):
         from functools import partial
@@ -152,6 +162,7 @@ class Game:
         return None
 
     def handle_input(self, msg):
+        self.log(msg)
         if msg == 'BYE':
             self.do_quit = True
             return
@@ -315,6 +326,33 @@ class PopUpWidget(Widget):
 class MapWidget(Widget):
 
     def draw(self):
+        if self.tui.view == 'map':
+            self.draw_map()
+        elif self.tui.view == 'inventory':
+            self.draw_item_selector('INVENTORY:',
+                                    self.tui.game.world.player_inventory)
+        elif self.tui.view == 'pickable_items':
+            self.draw_item_selector('PICKABLE:',
+                                    self.tui.game.world.pickable_items)
+
+    def draw_item_selector(self, title, selection):
+        lines = [title]
+        counter = 0
+        for id_ in selection:
+            pointer = '*' if counter == self.tui.item_pointer else ' '
+            t = self.tui.game.world.get_thing(id_)
+            lines += ['%s %s' % (pointer, t.type_)]
+            counter += 1
+        line_width = self.size[1]
+        to_join = []
+        for line in lines:
+            to_pad = line_width - (len(line) % line_width)
+            if to_pad == line_width:
+                to_pad = 0
+            to_join += [line + ' '*to_pad]
+        self.safe_write((''.join(to_join), curses.color_pair(3)))
+
+    def draw_map(self):
 
         def terrain_with_objects():
             terrain_as_list = list(self.tui.game.world.map_.terrain[:])
@@ -363,7 +401,7 @@ class MapWidget(Widget):
             return
 
         terrain_with_objects = terrain_with_objects()
-        center = self.tui.game.world.player_position
+        center = self.tui.game.world.player.position
         lines = self.tui.game.world.map_.format_to_view(terrain_with_objects,
                                                         center, self.size)
         pad_or_cut_x(lines)
@@ -384,6 +422,7 @@ class TUI:
         self.game = game
         self.parser = Parser(self.game)
         self.to_update = {'edit': False}
+        self.item_pointer = 0
         curses.wrapper(self.loop)
 
     def draw_screen(self):
@@ -412,7 +451,8 @@ class TUI:
         self.popup.visible = False
         self.popup_text = 'Hi bob'
         widgets = (self.edit, self.turn, self.log, self.map_, self.popup)
-        map_mode = False
+        write_mode = True
+        self.view = 'map'
         while True:
             for w in widgets:
                 w.ensure_freshness()
@@ -429,8 +469,20 @@ class TUI:
                         w.size = w.size_def
                         w.ensure_freshness(True)
                 elif key == '\t':  # Tabulator key.
-                    map_mode = False if map_mode else True
-                elif map_mode:
+                    write_mode = False if write_mode else True
+                elif write_mode:
+                    if len(key) == 1 and key in ASCII_printable and \
+                            len(self.to_send) < len(self.edit):
+                        self.to_send += [key]
+                        self.to_update['edit'] = True
+                    elif key == 'KEY_BACKSPACE':
+                        self.to_send[:] = self.to_send[:-1]
+                        self.to_update['edit'] = True
+                    elif key == '\n':  # Return key
+                        self.socket.send(''.join(self.to_send))
+                        self.to_send[:] = []
+                        self.to_update['edit'] = True
+                elif self.view == 'map':
                     if key == 'w':
                         self.socket.send('TASK:MOVE UPLEFT')
                     elif key == 'e':
@@ -455,18 +507,49 @@ class TUI:
                             self.draw_screen()
                             for w in widgets:
                                 w.ensure_freshness(True)
-                else:
-                    if len(key) == 1 and key in ASCII_printable and \
-                            len(self.to_send) < len(self.edit):
-                        self.to_send += [key]
-                        self.to_update['edit'] = True
-                    elif key == 'KEY_BACKSPACE':
-                        self.to_send[:] = self.to_send[:-1]
-                        self.to_update['edit'] = True
-                    elif key == '\n':  # Return key
-                        self.socket.send(''.join(self.to_send))
-                        self.to_send[:] = []
-                        self.to_update['edit'] = True
+                    elif key == 'p':
+                        self.socket.send('GET_PICKABLE_ITEMS')
+                        self.item_pointer = 0
+                        self.view = 'pickable_items'
+                    elif key == 'i':
+                        self.item_pointer = 0
+                        self.view = 'inventory'
+                        self.game.to_update['map'] = True
+                elif self.view == 'pickable_items':
+                    if key == 'c':
+                        self.view = 'map'
+                    elif key == 'j' and \
+                         len(self.game.world.pickable_items) > \
+                         self.item_pointer + 1:
+                        self.item_pointer += 1
+                    elif key == 'k' and self.item_pointer > 0:
+                        self.item_pointer -= 1
+                    elif key == 'p' and \
+                         len(self.game.world.pickable_items) > 0:
+                        id_ = self.game.world.pickable_items[self.item_pointer]
+                        self.socket.send('TASK:PICKUP %s' % id_)
+                        self.view = 'map'
+                    else:
+                        continue
+                    self.game.to_update['map'] = True
+                elif self.view == 'inventory':
+                    if key == 'c':
+                        self.view = 'map'
+                    elif key == 'j' and \
+                         len(self.game.world.player_inventory) > \
+                         self.item_pointer + 1:
+                        self.item_pointer += 1
+                    elif key == 'k' and self.item_pointer > 0:
+                        self.item_pointer -= 1
+                    elif key == 'd' and \
+                         len(self.game.world.player_inventory) > 0:
+                        id_ = self.game.world.player_inventory[self.item_pointer]
+                        self.socket.send('TASK:DROP %s' % id_)
+                        if self.item_pointer > 0:
+                            self.item_pointer -= 1
+                    else:
+                        continue
+                    self.game.to_update['map'] = True
             except curses.error:
                 pass
             if self.game.do_quit: