From: Christian Heller Date: Tue, 16 Apr 2019 18:57:15 +0000 (+0200) Subject: Transform items into food to replenish player energy. X-Git-Url: https://plomlompom.com/repos/%7B%7Bdb.prefix%7D%7D/static/%7B%7Bprefix%7D%7D/blog?a=commitdiff_plain;h=073618f45f297b21e90390be06cafcd430cd4d62;p=plomrogue2-experiments Transform items into food to replenish player energy. --- diff --git a/new/example_client.py b/new/example_client.py index c9fb4b1..45f5be0 100755 --- a/new/example_client.py +++ b/new/example_client.py @@ -102,7 +102,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.world.pickable_items[:] = [] cmd_TURN.argtypes = 'int:nonneg' @@ -124,12 +124,13 @@ 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 + 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.world.pickable_items[:] = ids 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' - elif type_ == 'item': - symbol = 'i' + elif type_ == 'food': + symbol = 'f' return symbol @@ -342,10 +343,26 @@ class PopUpWidget(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 - 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_)] @@ -360,19 +377,6 @@ class ItemsSelectorWidget(Widget): 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): @@ -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_) - 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 @@ -415,7 +419,7 @@ class MapWidget(Widget): 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))] @@ -498,29 +502,48 @@ class TUI: 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 - 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 + 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) @@ -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']) - 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] @@ -683,13 +713,9 @@ class TUI: 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: - pick_or_drop_menu('d', inventory_widget, - self.game.world.player_inventory, - 'DROP') + inventory_menu(key) except curses.error: pass diff --git a/new/plomrogue/game.py b/new/plomrogue/game.py index 2e78092..25cb3e7 100755 --- a/new/plomrogue/game.py +++ b/new/plomrogue/game.py @@ -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, @@ -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.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') - add_thing('item') - add_thing('item') + add_thing('food') + add_thing('food') + add_thing('food') + add_thing('food') return 'success' @@ -116,6 +119,7 @@ class Game: 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, @@ -135,7 +139,7 @@ class Game: 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': diff --git a/new/plomrogue/tasks.py b/new/plomrogue/tasks.py index c54d089..c9ce45e 100644 --- a/new/plomrogue/tasks.py +++ b/new/plomrogue/tasks.py @@ -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): - 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): - 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 diff --git a/new/plomrogue/things.py b/new/plomrogue/things.py index 112a1ce..f4cc2ae 100644 --- a/new/plomrogue/things.py +++ b/new/plomrogue/things.py @@ -29,7 +29,12 @@ class Thing(ThingBase): class ThingItem(Thing): - type_ = 'item' + pass + + + +class ThingFood(ThingItem): + type_ = 'food'