X-Git-Url: https://plomlompom.com/repos/?a=blobdiff_plain;f=new%2Fexample_client.py;h=a88c85b3c2d642f2a7a9cce5dc3be95f46b6e010;hb=2634704d62ea6ef488f4eb42d266e147c8e9facd;hp=5fc3063da146b2a4be1a26e47bc7f61169de903d;hpb=1ed87907733a1d3f3646c4b1a94fb17e3cff500f;p=plomrogue2-experiments diff --git a/new/example_client.py b/new/example_client.py index 5fc3063..a88c85b 100755 --- a/new/example_client.py +++ b/new/example_client.py @@ -3,16 +3,16 @@ import curses 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_PLAYER_ID, cmd_THING_HEALTH from plomrogue.game import Game, WorldBase -from plomrogue.mapping import MapHex +from plomrogue.mapping import Map, MapGeometryHex, YX from plomrogue.io import PlomSocket from plomrogue.things import ThingBase import types import queue -class ClientMap(MapHex): +class ClientMap(Map): def y_cut(self, map_lines, center_y, view_height): map_height = len(map_lines) @@ -33,10 +33,12 @@ class ClientMap(MapHex): cut_end = cut_start + view_width map_lines[:] = [line[cut_start:cut_end] for line in map_lines] - def format_to_view(self, map_cells, center, size): + def format_to_view(self, map_cells, center, size, indent_first_line): def map_cells_to_lines(map_cells): - map_view_chars = ['0'] + map_view_chars = [] + if indent_first_line: + map_view_chars += ['0'] x = 0 y = 0 for cell in map_cells: @@ -45,21 +47,21 @@ class ClientMap(MapHex): else: map_view_chars += [cell[0], cell[1]] x += 1 - if x == self.size[1]: + if x == self.size.x: map_view_chars += ['\n'] x = 0 y += 1 - if y % 2 == 0: + if y % 2 == int(not indent_first_line): map_view_chars += ['0'] - if y % 2 == 0: + if y % 2 == int(not indent_first_line): map_view_chars = map_view_chars[:-1] map_view_chars = map_view_chars[:-1] return ''.join(map_view_chars).split('\n') map_lines = map_cells_to_lines(map_cells) - self.y_cut(map_lines, center[0], size[0]) - map_width = self.size[1] * 2 + 1 - self.x_cut(map_lines, center[1] * 2, size[1], map_width) + self.y_cut(map_lines, center[1].y, size.y) + map_width = self.size.x * 2 + 1 + self.x_cut(map_lines, center[1].x * 2, size.x, map_width) return map_lines @@ -73,12 +75,14 @@ class World(WorldBase): """ super().__init__(*args, **kwargs) self.map_ = ClientMap() + self.offset = YX(0,0) self.player_inventory = [] self.player_id = 0 self.pickable_items = [] - def new_map(self, yx): - self.map_ = ClientMap(yx) + def new_map(self, offset, size): + self.map_ = ClientMap(size) + self.offset = offset @property def player(self): @@ -90,38 +94,57 @@ def cmd_LAST_PLAYER_TASK_RESULT(game, msg): 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(game, offset, size): + game.world.new_map(offset, size) +cmd_VISIBLE_MAP.argtypes = 'yx_tuple yx_tuple:pos' + + 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_THING_POS(game, i, yx): + t = game.world.get_thing(i) + t.position = YX(0,0), yx +cmd_THING_POS.argtypes = 'int:nonneg yx_tuple:nonneg' + + 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' @@ -131,6 +154,7 @@ class Game: def __init__(self): self.parser = Parser(self) self.world = World(self) + self.map_geometry = MapGeometryHex() self.thing_type = ThingBase self.commands = {'LAST_PLAYER_TASK_RESULT': cmd_LAST_PLAYER_TASK_RESULT, 'TURN_FINISHED': cmd_TURN_FINISHED, @@ -139,9 +163,10 @@ class Game: 'PLAYER_ID': cmd_PLAYER_ID, 'PLAYER_INVENTORY': cmd_PLAYER_INVENTORY, 'GAME_STATE_COMPLETE': cmd_GAME_STATE_COMPLETE, - 'MAP': cmd_MAP, + 'VISIBLE_MAP': cmd_VISIBLE_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 @@ -184,8 +209,8 @@ class Game: symbol = '@' elif type_ == 'monster': symbol = 'm' - elif type_ == 'item': - symbol = 'i' + elif type_ == 'food': + symbol = 'f' return symbol @@ -204,7 +229,7 @@ class Widget: self.check_updates = check_updates self.tui = tui self.start = start - self.win = curses.newwin(1, 1, self.start[0], self.start[1]) + self.win = curses.newwin(1, 1, self.start.y, self.start.x) self.size_def = size # store for re-calling .size on SIGWINCH self.size = size self.do_update = True @@ -213,20 +238,22 @@ class Widget: @property def size(self): - return self.win.getmaxyx() + return YX(*self.win.getmaxyx()) @size.setter def size(self, size): """Set window size. Size be y,x tuple. If y or x None, use legal max.""" n_lines, n_cols = size + getmaxyx = YX(*self.tui.stdscr.getmaxyx()) if n_lines is None: - n_lines = self.tui.stdscr.getmaxyx()[0] - self.start[0] + n_lines = getmaxyx.y - self.start.y if n_cols is None: - n_cols = self.tui.stdscr.getmaxyx()[1] - self.start[1] + n_cols = getmaxyx.x - self.start.x self.win.resize(n_lines, n_cols) def __len__(self): - return self.win.getmaxyx()[0] * self.win.getmaxyx()[1] + getmaxyx = YX(*self.win.getmaxyx()) + return getmaxyx.y * getmaxyx.x def safe_write(self, foo): @@ -251,9 +278,9 @@ class Widget: else: # workaround to cut = chars_with_attrs[:len(self) - 1] last_char_with_attr = chars_with_attrs[len(self) - 1] - self.win.addstr(self.size[0] - 1, self.size[1] - 2, + self.win.addstr(self.size.y - 1, self.size.x - 2, last_char_with_attr[0], last_char_with_attr[1]) - self.win.insstr(self.size[0] - 1, self.size[1] - 2, ' ') + self.win.insstr(self.size.y - 1, self.size.x - 2, ' ') self.win.move(0, 0) for char_with_attr in cut: self.win.addstr(char_with_attr[0], char_with_attr[1]) @@ -286,7 +313,7 @@ class TextLinesWidget(Widget): def draw(self): lines = self.get_text_lines() - line_width = self.size[1] + line_width = self.size.x to_join = [] for line in lines: to_pad = line_width - (len(line) % line_width) @@ -307,12 +334,11 @@ class DescriptorWidget(TextLinesWidget): def get_text_lines(self): lines = [] pos_i = self.tui.game.world.map_.\ - get_position_index(self.tui.examiner_position) + get_position_index(self.tui.examiner_position[1]) 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 @@ -325,23 +351,40 @@ class PopUpWidget(Widget): size = (1, len(self.tui.popup_text)) self.size = size self.size_def = size - offset_y = int((self.tui.stdscr.getmaxyx()[0] / 2) - (size[0] / 2)) - offset_x = int((self.tui.stdscr.getmaxyx()[1] / 2) - (size[1] / 2)) - self.start = (offset_y, offset_x) - self.win.mvwin(self.start[0], self.start[1]) + getmaxyx = YX(*self.tui.stdscr.getmaxyx()) + offset_y = int(getmaxyx.y / 2 - size.y / 2) + offset_x = int(getmaxyx.x / 2 - size.x / 2) + self.start = YX(offset_y, offset_x) + self.win.mvwin(self.start.y, self.start.x) 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_)] counter += 1 - line_width = self.size[1] + line_width = self.size.x to_join = [] for line in lines: to_pad = line_width - (len(line) % line_width) @@ -351,19 +394,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): @@ -371,9 +401,12 @@ class MapWidget(Widget): def annotated_terrain(): terrain_as_list = list(self.tui.game.world.map_.terrain[:]) for t in self.tui.game.world.things: - pos_i = self.tui.game.world.map_.get_position_index(t.position) + if t.id_ in self.tui.game.world.player_inventory: + continue + pos_i = self.tui.game.world.map_.\ + get_position_index(t.position[1]) 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 @@ -382,12 +415,12 @@ class MapWidget(Widget): terrain_as_list[pos_i] = symbol if self.tui.examiner_mode: pos_i = self.tui.game.world.map_.\ - get_position_index(self.tui.examiner_position) + get_position_index(self.tui.examiner_position[1]) terrain_as_list[pos_i] = (terrain_as_list[pos_i][0], '?') return terrain_as_list def pad_or_cut_x(lines): - line_width = self.size[1] + line_width = self.size.x for y in range(len(lines)): line = lines[y] if line_width > len(line): @@ -397,16 +430,16 @@ class MapWidget(Widget): lines[y] = line[:line_width] def pad_y(lines): - if len(lines) < self.size[0]: - to_pad = self.size[0] - len(lines) - lines += to_pad * ['0' * self.size[1]] + if len(lines) < self.size.y: + to_pad = self.size.y - len(lines) + lines += to_pad * ['0' * self.size.x] def lines_to_colored_chars(lines): chars_with_attrs = [] 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))] @@ -428,8 +461,10 @@ class MapWidget(Widget): center = self.tui.game.world.player.position if self.tui.examiner_mode: center = self.tui.examiner_position - lines = self.tui.game.world.map_.format_to_view(annotated_terrain, - center, self.size) + indent_first_line = not bool(self.tui.game.world.offset.y % 2) + lines = self.tui.game.world.map_.\ + format_to_view(annotated_terrain, center, self.size, + indent_first_line) pad_or_cut_x(lines) pad_y(lines) self.safe_write(lines_to_colored_chars(lines)) @@ -441,6 +476,14 @@ class TurnWidget(Widget): 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): @@ -461,7 +504,7 @@ class TUI: self.parser = Parser(self.game) self.to_update = {} self.item_pointer = 0 - self.examiner_position = (0, 0) + self.examiner_position = (YX(0,0), YX(0, 0)) self.examiner_mode = False self.popup_text = 'Hi bob' self.to_send = [] @@ -481,36 +524,55 @@ 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) - if new_examine_pos: + new_examine_pos = self.game.map_geometry.move(start_pos, direction, + self.game.world.map_.size) + if new_examine_pos[0] == (0,0): 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 @@ -594,23 +656,31 @@ class TUI: init_colors() # With screen initialized, set up widgets with their curses windows. - edit_widget = TextLineWidget('SEND:', self, (0, 0), (1, 20)) - edit_line_widget = EditWidget(self, (0, 6), (1, 14), ['edit']) + edit_widget = TextLineWidget('SEND:', self, YX(0, 0), YX(1, 20)) + edit_line_widget = EditWidget(self, YX(0, 6), YX(1, 14), ['edit']) 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) - 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, + turn_widget = TextLineWidget('TURN:', self, YX(2, 0), YX(1, 20)) + turn_widget.children += [TurnWidget(self, YX(2, 6), YX(1, 14), ['turn'])] + health_widget = TextLineWidget('HEALTH:', self, YX(3, 0), YX(1, 20)) + health_widget.children += [HealthWidget(self, YX(3, 8), YX(1, 12), ['turn'])] + log_widget = LogWidget(self, YX(5, 0), YX(None, 20), ['log']) + descriptor_widget = DescriptorWidget(self, YX(5, 0), YX(None, 20), + ['map'], False) + map_widget = MapWidget(self, YX(0, 21), YX(None, None), ['map']) + inventory_widget = ItemsSelectorWidget('INVENTORY:', + self.game.world.player_inventory, + self, YX(0, 21), YX(None, None), + ['inventory'], False) + pickable_items_widget = ItemsSelectorWidget('PICKABLE:', + self.game.world.pickable_items, + self, YX(0, 21), + YX(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) + popup_widget = PopUpWidget(self, YX(0, 0), YX(1, 1), visible=False) # Ensure initial window state before loop starts. for w in top_widgets: @@ -665,13 +735,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