import socket
import threading
from plomrogue.parser import ArgError, Parser
-from plomrogue.commands import cmd_MAP, cmd_THING_POS, cmd_PLAYER_ID
+from plomrogue.commands import (cmd_MAP, cmd_THING_POS, cmd_PLAYER_ID,
+ cmd_THING_HEALTH)
from plomrogue.game import Game, WorldBase
from plomrogue.mapping import MapHex
from plomrogue.io import PlomSocket
game.log(msg)
cmd_LAST_PLAYER_TASK_RESULT.argtypes = 'string'
+
def cmd_TURN_FINISHED(game, n):
"""Do nothing. (This may be extended later.)"""
pass
cmd_TURN_FINISHED.argtypes = 'int:nonneg'
+
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'
+
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_GAME_STATE_COMPLETE(game):
game.tui.to_update['turn'] = True
game.tui.to_update['map'] = True
game.tui.to_update['inventory'] = True
+
def cmd_THING_TYPE(game, i, type_):
t = game.world.get_thing(i)
t.type_ = 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
+ 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'
'MAP': cmd_MAP,
'PICKABLE_ITEMS': cmd_PICKABLE_ITEMS,
'THING_TYPE': cmd_THING_TYPE,
+ 'THING_HEALTH': cmd_THING_HEALTH,
'THING_POS': cmd_THING_POS}
self.log_text = ''
self.do_quit = False
symbol = '@'
elif type_ == 'monster':
symbol = 'm'
- elif type_ == 'item':
- symbol = 'i'
+ elif type_ == 'food':
+ symbol = 'f'
return symbol
get_position_index(self.tui.examiner_position)
terrain = self.tui.game.world.map_.terrain[pos_i]
lines = [terrain]
- for t in self.tui.game.world.things:
- if t.position == self.tui.examiner_position:
- lines += [t.type_]
+ for t in self.tui.game.world.things_at_pos(self.tui.examiner_position):
+ lines += [t.type_]
return lines
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_)]
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):
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
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))]
elif c in {'x', 'X', '#'}:
chars_with_attrs += [(c, curses.color_pair(3))]
+ elif c == '?':
+ chars_with_attrs += [(c, curses.color_pair(5))]
else:
chars_with_attrs += [c]
return chars_with_attrs
self.safe_write((str(self.tui.game.world.turn), curses.color_pair(2)))
+class HealthWidget(Widget):
+
+ def draw(self):
+ if hasattr(self.tui.game.world.player, 'health'):
+ self.safe_write((str(self.tui.game.world.player.health),
+ curses.color_pair(2)))
+
+
class TextLineWidget(Widget):
def __init__(self, text_line, *args, **kwargs):
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)
if new_examine_pos:
self.examiner_position = new_examine_pos
self.to_update['map'] = True
- self.to_update['descriptor'] = True
def switch_to_pick_or_drop(target_widget):
self.item_pointer = 0
curses.init_pair(2, curses.COLOR_BLACK, curses.COLOR_GREEN)
curses.init_pair(3, curses.COLOR_BLACK, curses.COLOR_BLUE)
curses.init_pair(4, curses.COLOR_BLACK, curses.COLOR_YELLOW)
+ curses.init_pair(5, curses.COLOR_BLACK, curses.COLOR_WHITE)
# Basic curses initialization work.
setup_screen(stdscr)
edit_widget.children += [edit_line_widget]
turn_widget = TextLineWidget('TURN:', self, (2, 0), (1, 20))
turn_widget.children += [TurnWidget(self, (2, 6), (1, 14), ['turn'])]
- log_widget = LogWidget(self, (4, 0), (None, 20), ['log'])
- descriptor_widget = DescriptorWidget(self, (4, 0), (None, 20),
- ['descriptor'], False)
+ health_widget = TextLineWidget('HEALTH:', self, (3, 0), (1, 20))
+ health_widget.children += [HealthWidget(self, (3, 8), (1, 12), ['turn'])]
+ log_widget = LogWidget(self, (5, 0), (None, 20), ['log'])
+ 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)
- top_widgets = [edit_widget, turn_widget, log_widget,
+ 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]
popup_widget = PopUpWidget(self, (0, 0), (1, 1), visible=False)
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