home · contact · privacy
Transform items into food to replenish player energy.
authorChristian Heller <c.heller@plomlompom.de>
Tue, 16 Apr 2019 18:57:15 +0000 (20:57 +0200)
committerChristian Heller <c.heller@plomlompom.de>
Tue, 16 Apr 2019 18:57:15 +0000 (20:57 +0200)
new/example_client.py
new/plomrogue/game.py
new/plomrogue/tasks.py
new/plomrogue/things.py

index c9fb4b1f638cce13f4b638cf18829ba6b506a66c..45f5be0861239a68d9287ff547a323af5d83a20a 100755 (executable)
@@ -102,7 +102,7 @@ def cmd_TURN(game, n):
     """Set game.turn to n, empty game.things."""
     game.world.turn = n
     game.world.things = []
     """Set game.turn to n, empty game.things."""
     game.world.turn = n
     game.world.things = []
-    game.world.pickable_items = []
+    game.world.pickable_items[:] = []
 cmd_TURN.argtypes = 'int:nonneg'
 
 
 cmd_TURN.argtypes = 'int:nonneg'
 
 
@@ -124,12 +124,13 @@ cmd_THING_TYPE.argtypes = 'int:nonneg string'
 
 
 def cmd_PLAYER_INVENTORY(game, ids):
 
 
 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
+    game.tui.to_update['inventory'] = True
 cmd_PLAYER_INVENTORY.argtypes = 'seq:int:nonneg'
 
 
 def cmd_PICKABLE_ITEMS(game, ids):
 cmd_PLAYER_INVENTORY.argtypes = 'seq:int:nonneg'
 
 
 def cmd_PICKABLE_ITEMS(game, ids):
-    game.world.pickable_items = ids
+    game.world.pickable_items[:] = ids
     game.tui.to_update['pickable_items'] = True
 cmd_PICKABLE_ITEMS.argtypes = 'seq:int:nonneg'
 
     game.tui.to_update['pickable_items'] = True
 cmd_PICKABLE_ITEMS.argtypes = 'seq:int:nonneg'
 
@@ -193,8 +194,8 @@ class Game:
             symbol = '@'
         elif type_ == 'monster':
             symbol = 'm'
             symbol = '@'
         elif type_ == 'monster':
             symbol = 'm'
-        elif type_ == 'item':
-            symbol = 'i'
+        elif type_ == 'food':
+            symbol = 'f'
         return symbol
 
 
         return symbol
 
 
@@ -342,10 +343,26 @@ class PopUpWidget(Widget):
 
 class ItemsSelectorWidget(Widget):
 
 
 class ItemsSelectorWidget(Widget):
 
-    def draw_item_selector(self, title, selection):
-        lines = [title]
+    def __init__(self, headline, selection, *args, **kwargs):
+        super().__init__(*args, **kwargs)
+        self.headline = headline
+        self.selection = selection
+
+    def ensure_freshness(self, *args, **kwargs):
+        # We only update pointer on non-empty selection so that the zero-ing
+        # of the selection at TURN_FINISHED etc. before pulling in a new
+        # state does not destroy any memory of previous item pointer positions.
+        if len(self.selection) > 0 and\
+           len(self.selection) < self.tui.item_pointer + 1 and\
+           self.tui.item_pointer > 0:
+            self.tui.item_pointer = max(0, len(self.selection) - 1)
+            self.tui.to_update[self.check_updates[0]] = True
+        super().ensure_freshness(*args, **kwargs)
+
+    def draw(self):
+        lines = [self.headline]
         counter = 0
         counter = 0
-        for id_ in selection:
+        for id_ in self.selection:
             pointer = '*' if counter == self.tui.item_pointer else ' '
             t = self.tui.game.world.get_thing(id_)
             lines += ['%s %s' % (pointer, t.type_)]
             pointer = '*' if counter == self.tui.item_pointer else ' '
             t = self.tui.game.world.get_thing(id_)
             lines += ['%s %s' % (pointer, t.type_)]
@@ -360,19 +377,6 @@ class ItemsSelectorWidget(Widget):
         self.safe_write((''.join(to_join), curses.color_pair(3)))
 
 
         self.safe_write((''.join(to_join), curses.color_pair(3)))
 
 
-class InventoryWidget(ItemsSelectorWidget):
-
-    def draw(self):
-        self.draw_item_selector('INVENTORY:',
-                                self.tui.game.world.player_inventory)
-
-class PickableItemsWidget(ItemsSelectorWidget):
-
-    def draw(self):
-        self.draw_item_selector('PICKABLE:',
-                                self.tui.game.world.pickable_items)
-
-
 class MapWidget(Widget):
 
     def draw(self):
 class MapWidget(Widget):
 
     def draw(self):
@@ -382,7 +386,7 @@ class MapWidget(Widget):
             for t in self.tui.game.world.things:
                 pos_i = self.tui.game.world.map_.get_position_index(t.position)
                 symbol = self.tui.game.symbol_for_type(t.type_)
             for t in self.tui.game.world.things:
                 pos_i = self.tui.game.world.map_.get_position_index(t.position)
                 symbol = self.tui.game.symbol_for_type(t.type_)
-                if terrain_as_list[pos_i][0] in {'i', '@', 'm'}:
+                if terrain_as_list[pos_i][0] in {'f', '@', 'm'}:
                     old_symbol = terrain_as_list[pos_i][0]
                     if old_symbol in {'@', 'm'}:
                         symbol = old_symbol
                     old_symbol = terrain_as_list[pos_i][0]
                     if old_symbol in {'@', 'm'}:
                         symbol = old_symbol
@@ -415,7 +419,7 @@ class MapWidget(Widget):
             for c in ''.join(lines):
                 if c in {'@', 'm'}:
                     chars_with_attrs += [(c, curses.color_pair(1))]
             for c in ''.join(lines):
                 if c in {'@', 'm'}:
                     chars_with_attrs += [(c, curses.color_pair(1))]
-                elif c == 'i':
+                elif c == 'f':
                     chars_with_attrs += [(c, curses.color_pair(4))]
                 elif c == '.':
                     chars_with_attrs += [(c, curses.color_pair(2))]
                     chars_with_attrs += [(c, curses.color_pair(4))]
                 elif c == '.':
                     chars_with_attrs += [(c, curses.color_pair(2))]
@@ -498,29 +502,48 @@ class TUI:
             trigger = widget_2.check_updates[0]
             self.to_update[trigger] = True
 
             trigger = widget_2.check_updates[0]
             self.to_update[trigger] = True
 
-        def pick_or_drop_menu(action_key, widget, selectables, task,
-                              bonus_command=None):
-            if len(selectables) < self.item_pointer + 1 and\
-               self.item_pointer > 0:
-                self.item_pointer = len(selectables) - 1
+        def selectables_menu(key, widget, selectables, f):
             if key == 'c':
                 switch_widgets(widget, map_widget)
             elif key == 'j':
                 self.item_pointer += 1
             elif key == 'k' and self.item_pointer > 0:
                 self.item_pointer -= 1
             if key == 'c':
                 switch_widgets(widget, map_widget)
             elif key == 'j':
                 self.item_pointer += 1
             elif key == 'k' and self.item_pointer > 0:
                 self.item_pointer -= 1
-            elif key == action_key and len(selectables) > 0:
-                id_ = selectables[self.item_pointer]
-                self.socket.send('TASK:%s %s' % (task, id_))
-                if bonus_command:
-                    self.socket.send(bonus_command)
-                if self.item_pointer > 0:
-                    self.item_pointer -= 1
-            else:
+            elif not f(key, selectables):
                 return
             trigger = widget.check_updates[0]
             self.to_update[trigger] = True
 
                 return
             trigger = widget.check_updates[0]
             self.to_update[trigger] = True
 
+        def pickup_menu(key):
+
+            def f(key, selectables):
+                if key == 'p' and len(selectables) > 0:
+                    id_ = selectables[self.item_pointer]
+                    self.socket.send('TASK:PICKUP %s' % id_)
+                    self.socket.send('GET_PICKABLE_ITEMS')
+                else:
+                    return False
+                return True
+
+            selectables_menu(key, pickable_items_widget,
+                             self.game.world.pickable_items, f)
+
+        def inventory_menu(key):
+
+            def f(key, selectables):
+                if key == 'd' and len(selectables) > 0:
+                    id_ = selectables[self.item_pointer]
+                    self.socket.send('TASK:DROP %s' % id_)
+                elif key == 'e' and len(selectables) > 0:
+                    id_ = selectables[self.item_pointer]
+                    self.socket.send('TASK:EAT %s' % id_)
+                else:
+                    return False
+                return True
+
+            selectables_menu(key, inventory_widget,
+                             self.game.world.player_inventory, f)
+
         def move_examiner(direction):
             start_pos = self.examiner_position
             new_examine_pos = self.game.world.map_.move(start_pos, direction)
         def move_examiner(direction):
             start_pos = self.examiner_position
             new_examine_pos = self.game.world.map_.move(start_pos, direction)
@@ -621,10 +644,17 @@ class TUI:
         descriptor_widget = DescriptorWidget(self, (5, 0), (None, 20),
                                              ['map'], False)
         map_widget = MapWidget(self, (0, 21), (None, None), ['map'])
         descriptor_widget = DescriptorWidget(self, (5, 0), (None, 20),
                                              ['map'], False)
         map_widget = MapWidget(self, (0, 21), (None, None), ['map'])
-        inventory_widget = InventoryWidget(self, (0, 21), (None, None),
-                                           ['inventory'], False)
-        pickable_items_widget = PickableItemsWidget(self, (0, 21), (None, None),
-                                                    ['pickable_items'], False)
+        inventory_widget = ItemsSelectorWidget('INVENTORY:',
+                                               self.game.world.player_inventory,
+                                               self, (0, 21), (None,
+                                                               None), ['inventory'],
+                                               False)
+        pickable_items_widget = ItemsSelectorWidget('PICKABLE:',
+                                                    self.game.world.pickable_items,
+                                                    self, (0, 21),
+                                                    (None, None),
+                                                    ['pickable_items'],
+                                                    False)
         top_widgets = [edit_widget, turn_widget, health_widget, log_widget,
                        descriptor_widget, map_widget, inventory_widget,
                        pickable_items_widget]
         top_widgets = [edit_widget, turn_widget, health_widget, log_widget,
                        descriptor_widget, map_widget, inventory_widget,
                        pickable_items_widget]
@@ -683,13 +713,9 @@ class TUI:
                     else:
                         try_player_move_keys()
                 elif pickable_items_widget.visible:
                     else:
                         try_player_move_keys()
                 elif pickable_items_widget.visible:
-                    pick_or_drop_menu('p', pickable_items_widget,
-                                      self.game.world.pickable_items,
-                                      'PICKUP', 'GET_PICKABLE_ITEMS')
+                    pickup_menu(key)
                 elif inventory_widget.visible:
                 elif inventory_widget.visible:
-                    pick_or_drop_menu('d', inventory_widget,
-                                      self.game.world.player_inventory,
-                                      'DROP')
+                    inventory_menu(key)
             except curses.error:
                 pass
 
             except curses.error:
                 pass
 
index 2e780928cd073d6e23d2248ff30307abfcc2b40e..25cb3e74fa72b5693c167af0fcee31a14eab2fcf 100755 (executable)
@@ -1,4 +1,5 @@
-from plomrogue.tasks import Task_WAIT, Task_MOVE, Task_PICKUP, Task_DROP
+from plomrogue.tasks import (Task_WAIT, Task_MOVE, Task_PICKUP,
+                             Task_DROP, Task_EAT)
 from plomrogue.errors import ArgError, GameError
 from plomrogue.commands import (cmd_GEN_WORLD, cmd_GET_GAMESTATE,
                                 cmd_MAP, cmd_MAP, cmd_THING_TYPE,
 from plomrogue.errors import ArgError, GameError
 from plomrogue.commands import (cmd_GEN_WORLD, cmd_GET_GAMESTATE,
                                 cmd_MAP, cmd_MAP, cmd_THING_TYPE,
@@ -11,7 +12,7 @@ from plomrogue.mapping import MapHex
 from plomrogue.parser import Parser
 from plomrogue.io import GameIO
 from plomrogue.misc import quote, stringify_yx
 from plomrogue.parser import Parser
 from plomrogue.io import GameIO
 from plomrogue.misc import quote, stringify_yx
-from plomrogue.things import Thing, ThingMonster, ThingHuman, ThingItem
+from plomrogue.things import Thing, ThingMonster, ThingHuman, ThingFood
 
 
 
 
 
 
@@ -102,8 +103,10 @@ class World(WorldBase):
         self.player_id = player.id_
         add_thing('monster')
         add_thing('monster')
         self.player_id = player.id_
         add_thing('monster')
         add_thing('monster')
-        add_thing('item')
-        add_thing('item')
+        add_thing('food')
+        add_thing('food')
+        add_thing('food')
+        add_thing('food')
         return 'success'
 
 
         return 'success'
 
 
@@ -116,6 +119,7 @@ class Game:
         self.tasks = {'WAIT': Task_WAIT,
                       'MOVE': Task_MOVE,
                       'PICKUP': Task_PICKUP,
         self.tasks = {'WAIT': Task_WAIT,
                       'MOVE': Task_MOVE,
                       'PICKUP': Task_PICKUP,
+                      'EAT': Task_EAT,
                       'DROP': Task_DROP}
         self.commands = {'GEN_WORLD': cmd_GEN_WORLD,
                          'GET_GAMESTATE': cmd_GET_GAMESTATE,
                       'DROP': Task_DROP}
         self.commands = {'GEN_WORLD': cmd_GEN_WORLD,
                          'GET_GAMESTATE': cmd_GET_GAMESTATE,
@@ -135,7 +139,7 @@ class Game:
         self.thing_type = Thing
         self.thing_types = {'human': ThingHuman,
                             'monster': ThingMonster,
         self.thing_type = Thing
         self.thing_types = {'human': ThingHuman,
                             'monster': ThingMonster,
-                            'item': ThingItem}
+                            'food': ThingFood}
 
     def get_string_options(self, string_option_type):
         if string_option_type == 'direction':
 
     def get_string_options(self, string_option_type):
         if string_option_type == 'direction':
index c54d0899b98ecb56ec2c4fe422ec357086951df2..c9ce45ed654f1aff64848c65f6b43167db3037c7 100644 (file)
@@ -73,18 +73,45 @@ class Task_PICKUP(Task):
 
 
 
 
 
 
-class Task_DROP(Task):
+class TaskOnInventoryItem(Task):
+    argtypes = 'int:nonneg'
+
+    def _basic_inventory_item_check(self):
+        item = self.thing.world.get_thing(self.args[0], create_unfound=False)
+        if item is None:
+            raise GameError('no thing of ID %s' % self.args[0])
+        if item.id_ not in self.thing.inventory:
+            raise GameError('no thing of ID %s in inventory' % self.args[0])
+        return item
+
+    def _eliminate_from_inventory(self):
+        item = self.thing.world.get_thing(self.args[0])
+        del self.thing.inventory[self.thing.inventory.index(item.id_)]
+        item.in_inventory = False
+        return item
+
+
+
+class Task_DROP(TaskOnInventoryItem):
     argtypes = 'int:nonneg'
 
     def check(self):
     argtypes = 'int:nonneg'
 
     def check(self):
-        to_drop = self.thing.world.get_thing(self.args[0], create_unfound=False)
-        if to_drop is None:
-            raise GameError('no thing of ID %s to drop' % self.args[0])
-        if to_drop.id_ not in self.thing.inventory:
-            raise GameError('no thing of ID %s to drop in inventory'
-                            % self.args[0])
+        self._basic_inventory_item_check()
+
+    def do(self):
+        self._eliminate_from_inventory()
+
+
+
+class Task_EAT(TaskOnInventoryItem):
+    argtypes = 'int:nonneg'
+
+    def check(self):
+        to_eat = self._basic_inventory_item_check()
+        if to_eat.type_ != 'food':
+            raise GameError('thing of ID %s s not food' % self.args[0])
 
     def do(self):
 
     def do(self):
-        to_drop = self.thing.world.get_thing(self.args[0])
-        del self.thing.inventory[self.thing.inventory.index(to_drop.id_)]
-        to_drop.in_inventory = False
+        to_eat = self._eliminate_from_inventory()
+        del self.thing.world.things[self.thing.world.things.index(to_eat)]
+        self.thing.health += 50
index 112a1ce9449399700afe28e33b4f4c1a52990c8e..f4cc2aeeae689d788223d648407bb9c2215985d0 100644 (file)
@@ -29,7 +29,12 @@ class Thing(ThingBase):
 
 
 class ThingItem(Thing):
 
 
 class ThingItem(Thing):
-    type_ = 'item'
+    pass
+
+
+
+class ThingFood(ThingItem):
+    type_ = 'food'