home · contact · privacy
Allow item-in-vicinity selection for item pickup.
authorChristian Heller <c.heller@plomlompom.de>
Sat, 9 Mar 2019 00:08:35 +0000 (01:08 +0100)
committerChristian Heller <c.heller@plomlompom.de>
Sat, 9 Mar 2019 00:08:35 +0000 (01:08 +0100)
new/example_client.py
new/plomrogue/commands.py
new/plomrogue/game.py
new/plomrogue/tasks.py
new/plomrogue/things.py

index b561d9ddbdbb380e4ea285b374026b0ecacb0379..1e910704fe5104a767f9cd63c1215b00ef92e865 100755 (executable)
@@ -71,6 +71,7 @@ class World(WorldBase):
         self.map_ = Map()
         self.player_inventory = []
         self.player_id = 0
+        self.pickable_items = []
 
     def new_map(self, yx):
         self.map_ = Map(yx)
@@ -94,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'
@@ -115,6 +117,11 @@ def cmd_PLAYER_INVENTORY(game, 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:
 
@@ -130,6 +137,7 @@ class Game:
                          '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 = ''
@@ -139,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
@@ -153,6 +162,7 @@ class Game:
         return None
 
     def handle_input(self, msg):
+        self.log(msg)
         if msg == 'BYE':
             self.do_quit = True
             return
@@ -319,13 +329,17 @@ class MapWidget(Widget):
         if self.tui.view == 'map':
             self.draw_map()
         elif self.tui.view == 'inventory':
-            self.draw_inventory()
-
-    def draw_inventory(self):
-        lines = ['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 self.tui.game.world.player_inventory:
-            pointer = '*' if counter == self.tui.inventory_pointer else ' '
+        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
@@ -408,7 +422,7 @@ class TUI:
         self.game = game
         self.parser = Parser(self.game)
         self.to_update = {'edit': False}
-        self.inventory_pointer = 0
+        self.item_pointer = 0
         curses.wrapper(self.loop)
 
     def draw_screen(self):
@@ -494,31 +508,45 @@ class TUI:
                             for w in widgets:
                                 w.ensure_freshness(True)
                     elif key == 'p':
-                        for t in self.game.world.things:
-                            if t == self.game.world.player or \
-                               t.id_ in self.game.world.player_inventory:
-                                continue
-                            if t.position == self.game.world.player.position:
-                                self.socket.send('TASK:PICKUP %s' % t.id_)
-                                break
+                        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 == 'i':
+                    if key == 'c':
                         self.view = 'map'
                     elif key == 'j' and \
                          len(self.game.world.player_inventory) > \
-                         self.inventory_pointer + 1:
-                        self.inventory_pointer += 1
-                    elif key == 'k' and self.inventory_pointer > 0:
-                        self.inventory_pointer -= 1
+                         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.inventory_pointer]
+                        id_ = self.game.world.player_inventory[self.item_pointer]
                         self.socket.send('TASK:DROP %s' % id_)
-                        if self.inventory_pointer > 0:
-                            self.inventory_pointer -= 1
+                        if self.item_pointer > 0:
+                            self.item_pointer -= 1
                     else:
                         continue
                     self.game.to_update['map'] = True
index 3ae59583799f4c3283ac5b20b8d0df0f87a5299b..92f583dc4c90df047f0fbe5c036d2ee62d8bbc60 100644 (file)
@@ -46,6 +46,14 @@ def cmd_THING_INVENTORY(game, id_, ids):
     t.inventory = ids  # TODO: test whether valid IDs
 cmd_THING_INVENTORY.argtypes = 'int:nonneg seq:int:nonneg'
 
+def cmd_GET_PICKABLE_ITEMS(game, connection_id):
+    pickable_ids = game.world.player.get_pickable_items()
+    if len(pickable_ids) > 0:
+        game.io.send('PICKABLE_ITEMS %s' %
+                     ','.join([str(id_) for id_ in pickable_ids]))
+    else:
+        game.io.send('PICKABLE_ITEMS ,')
+
 def cmd_TERRAIN_LINE(game, y, terrain_line):
     game.world.map_.set_line(y, terrain_line)
 cmd_TERRAIN_LINE.argtypes = 'int:nonneg string'
index e24edbb5404543e9c10a212556bd92972af152fd..e8e80c0f1e8785f00771a3700fce11e5fb0a194e 100755 (executable)
@@ -2,7 +2,7 @@ from plomrogue.tasks import Task_WAIT, Task_MOVE, Task_PICKUP, Task_DROP
 from plomrogue.errors import ArgError
 from plomrogue.commands import (cmd_GEN_WORLD, cmd_GET_GAMESTATE, cmd_MAP,
                                 cmd_MAP, cmd_THING_TYPE, cmd_THING_POS,
-                                cmd_THING_INVENTORY,
+                                cmd_THING_INVENTORY, cmd_GET_PICKABLE_ITEMS,
                                 cmd_TERRAIN_LINE, cmd_PLAYER_ID, cmd_TURN,
                                 cmd_SWITCH_PLAYER, cmd_SAVE)
 from plomrogue.mapping import MapHex
@@ -119,6 +119,7 @@ class Game:
                          'THING_POS': cmd_THING_POS,
                          'THING_INVENTORY': cmd_THING_INVENTORY,
                          'TERRAIN_LINE': cmd_TERRAIN_LINE,
+                         'GET_PICKABLE_ITEMS': cmd_GET_PICKABLE_ITEMS,
                          'PLAYER_ID': cmd_PLAYER_ID,
                          'TURN': cmd_TURN,
                          'SWITCH_PLAYER': cmd_SWITCH_PLAYER,
index 1be5b6da40b2c757fccd08541fbea6002f6809aa..6f2f1be9ee4397630059c35eece39ebd48bda99f 100644 (file)
@@ -59,9 +59,7 @@ class Task_PICKUP(Task):
         to_pick_up = self.thing.world.get_thing(self.args[0],
                                                 create_unfound=False)
         if to_pick_up is None or \
-           to_pick_up.in_inventory or \
-           to_pick_up == self.thing or \
-           self.thing.position != to_pick_up.position:
+           to_pick_up.id_ not in self.thing.get_pickable_items():
             raise GameError('thing of ID %s not in reach to pick up'
                             % self.args[0])
 
@@ -69,6 +67,7 @@ class Task_PICKUP(Task):
         to_pick_up = self.thing.world.get_thing(self.args[0])
         self.thing.inventory += [self.args[0]]
         to_pick_up.in_inventory = True
+        to_pick_up.position = self.thing.position
 
 
 
index a631b174f9292dde5ab047b98836bd2820939137..20ce4867c2211882b506de309d8881eb16842379 100644 (file)
@@ -149,6 +149,16 @@ class ThingAnimate(Thing):
                 visible_things += [thing]
         return visible_things
 
+    def get_pickable_items(self):
+        pickable_ids = []
+        for t in [t for t in self.get_visible_things() if
+                  isinstance(t, ThingItem) and
+                  (t.position == self.position or
+                   t.position in
+                   self.world.map_.get_neighbors(self.position).values())]:
+            pickable_ids += [t.id_]
+        return pickable_ids
+
 
 
 class ThingHuman(ThingAnimate):