X-Git-Url: https://plomlompom.com/repos/?p=plomrogue2-experiments;a=blobdiff_plain;f=new%2Fexample_client.py;h=c9fb4b1f638cce13f4b638cf18829ba6b506a66c;hp=7a9902708a60cf36140ae2024fd1e9c21cbb00d6;hb=6c37a16df7e55754ca89a9de0aaf49c3c778155e;hpb=d84b39d4b7265d1ce6597023aedf6dca89836eea diff --git a/new/example_client.py b/new/example_client.py index 7a99027..c9fb4b1 100755 --- a/new/example_client.py +++ b/new/example_client.py @@ -3,14 +3,17 @@ import curses import socket import threading from plomrogue.parser import ArgError, Parser -from plomrogue.commands import cmd_MAP, cmd_THING_TYPE, cmd_THING_POS -from plomrogue.game import Game, WorldBase, ThingBase -from plomrogue.mapping import MapBase +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 +from plomrogue.things import ThingBase import types +import queue -class Map(MapBase): +class ClientMap(MapHex): def y_cut(self, map_lines, center_y, view_height): map_height = len(map_lines) @@ -31,14 +34,17 @@ class Map(MapBase): cut_end = cut_start + view_width map_lines[:] = [line[cut_start:cut_end] for line in map_lines] - def format_to_view(self, map_string, center, size): + def format_to_view(self, map_cells, center, size): - def map_string_to_lines(map_string): + def map_cells_to_lines(map_cells): map_view_chars = ['0'] x = 0 y = 0 - for c in map_string: - map_view_chars += [c, ' '] + for cell in map_cells: + if type(cell) == str: + map_view_chars += [cell, ' '] + else: + map_view_chars += [cell[0], cell[1]] x += 1 if x == self.size[1]: map_view_chars += ['\n'] @@ -51,7 +57,7 @@ class Map(MapBase): map_view_chars = map_view_chars[:-1] return ''.join(map_view_chars).split('\n') - map_lines = map_string_to_lines(map_string) + 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) @@ -67,42 +73,65 @@ class World(WorldBase): on any update, even before we actually receive map data. """ super().__init__(*args, **kwargs) - self.map_ = Map() - self.player_position = (0, 0) + self.map_ = ClientMap() + self.player_inventory = [] + self.player_id = 0 + self.pickable_items = [] def new_map(self, yx): - self.map_ = Map(yx) + self.map_ = ClientMap(yx) + + @property + def player(self): + return self.get_thing(self.player_id) -def cmd_LAST_PLAYER_TASK_RESULT(self, msg): +def cmd_LAST_PLAYER_TASK_RESULT(game, msg): if msg != "success": - self.log(msg) + game.log(msg) cmd_LAST_PLAYER_TASK_RESULT.argtypes = 'string' -def cmd_TURN_FINISHED(self, n): + +def cmd_TURN_FINISHED(game, n): """Do nothing. (This may be extended later.)""" pass cmd_TURN_FINISHED.argtypes = 'int:nonneg' -def cmd_TURN(self, n): - """Set self.turn to n, empty self.things.""" - self.world.turn = n - self.world.things = [] - self.to_update['turn'] = False - self.to_update['map'] = False + +def cmd_TURN(game, n): + """Set game.turn to n, empty game.things.""" + game.world.turn = n + game.world.things = [] + game.world.pickable_items = [] cmd_TURN.argtypes = 'int:nonneg' -def cmd_VISIBLE_MAP_LINE(self, y, terrain_line): - self.world.map_.set_line(y, terrain_line) + +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_PLAYER_POS(self, yx): - self.world.player_position = yx -cmd_PLAYER_POS.argtypes = 'yx_tuple:pos' -def cmd_GAME_STATE_COMPLETE(self): - self.to_update['turn'] = True - self.to_update['map'] = True +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 +cmd_PLAYER_INVENTORY.argtypes = 'seq:int:nonneg' + + +def cmd_PICKABLE_ITEMS(game, ids): + game.world.pickable_items = ids + game.tui.to_update['pickable_items'] = True +cmd_PICKABLE_ITEMS.argtypes = 'seq:int:nonneg' class Game: @@ -115,18 +144,17 @@ class Game: 'TURN_FINISHED': cmd_TURN_FINISHED, 'TURN': cmd_TURN, 'VISIBLE_MAP_LINE': cmd_VISIBLE_MAP_LINE, - 'PLAYER_POS': cmd_PLAYER_POS, + 'PLAYER_ID': cmd_PLAYER_ID, + '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_HEALTH': cmd_THING_HEALTH, 'THING_POS': cmd_THING_POS} self.log_text = '' - self.to_update = { - 'log': True, - 'map': True, - 'turn': True, - } self.do_quit = False + self.tui = None def get_command(self, command_name): from functools import partial @@ -141,6 +169,7 @@ class Game: return None def handle_input(self, msg): + self.log(msg) if msg == 'BYE': self.do_quit = True return @@ -148,17 +177,15 @@ class Game: command, args = self.parser.parse(msg) if command is None: self.log('UNHANDLED INPUT: ' + msg) - self.to_update['log'] = True else: command(*args) except ArgError as e: self.log('ARGUMENT ERROR: ' + msg + '\n' + str(e)) - self.to_update['log'] = True def log(self, msg): """Prefix msg plus newline to self.log_text.""" self.log_text = msg + '\n' + self.log_text - self.to_update['log'] = True + self.tui.to_update['log'] = True def symbol_for_type(self, type_): symbol = '?' @@ -166,6 +193,8 @@ class Game: symbol = '@' elif type_ == 'monster': symbol = 'm' + elif type_ == 'item': + symbol = 'i' return symbol @@ -173,22 +202,23 @@ ASCII_printable = ' !"#$%&\'\(\)*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWX' 'YZ[\\]^_\`abcdefghijklmnopqrstuvwxyz{|}~' -def recv_loop(plom_socket, game): +def recv_loop(plom_socket, game, q): for msg in plom_socket.recv(): - game.handle_input(msg) + q.put(msg) class Widget: - def __init__(self, tui, start, size, check_game=[], check_tui=[]): - self.check_game = check_game - self.check_tui = check_tui + def __init__(self, tui, start, size, check_updates=[], visible=True): + self.check_updates = check_updates self.tui = tui self.start = start self.win = curses.newwin(1, 1, self.start[0], self.start[1]) self.size_def = size # store for re-calling .size on SIGWINCH self.size = size self.do_update = True + self.visible = visible + self.children = [] @property def size(self): @@ -215,14 +245,10 @@ class Widget: if not type(part) == str: part_string = part[0] attr = part[1] - if len(part_string) > 0: - return [(char, attr) for char in part_string] - elif len(part_string) == 1: - return [part] - return [] + return [(char, attr) for char in part_string] chars_with_attrs = [] - if type(foo) == str or len(foo) == 2 and type(foo[1]) == int: + if type(foo) == str or (len(foo) == 2 and type(foo[1]) == int): chars_with_attrs += to_chars_with_attrs(foo) else: for part in foo: @@ -242,20 +268,21 @@ class Widget: self.win.addstr(char_with_attr[0], char_with_attr[1]) def ensure_freshness(self, do_refresh=False): - if not do_refresh: - for key in self.check_game: - if self.tui.game.to_update[key]: - do_refresh = True - break - if not do_refresh: - for key in self.check_tui: - if self.tui.to_update[key]: - do_refresh = True - break - if do_refresh: - self.win.erase() - self.draw() - self.win.refresh() + did_refresh = False + if self.visible: + if not do_refresh: + for key in self.check_updates: + if key in self.tui.to_update and self.tui.to_update[key]: + do_refresh = True + break + if do_refresh: + self.win.erase() + self.draw() + self.win.refresh() + did_refresh = True + for child in self.children: + did_refresh = child.ensure_freshness(do_refresh) | did_refresh + return did_refresh class EditWidget(Widget): @@ -264,13 +291,13 @@ class EditWidget(Widget): self.safe_write((''.join(self.tui.to_send), curses.color_pair(1))) -class LogWidget(Widget): +class TextLinesWidget(Widget): def draw(self): + lines = self.get_text_lines() line_width = self.size[1] - log_lines = self.tui.game.log_text.split('\n') to_join = [] - for line in log_lines: + for line in lines: to_pad = line_width - (len(line) % line_width) if to_pad == line_width: to_pad = 0 @@ -278,16 +305,95 @@ class LogWidget(Widget): self.safe_write((''.join(to_join), curses.color_pair(3))) +class LogWidget(TextLinesWidget): + + def get_text_lines(self): + return self.tui.game.log_text.split('\n') + + +class DescriptorWidget(TextLinesWidget): + + def get_text_lines(self): + lines = [] + pos_i = self.tui.game.world.map_.\ + 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_] + return lines + + +class PopUpWidget(Widget): + + def draw(self): + self.safe_write(self.tui.popup_text) + + def reconfigure(self): + 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]) + + +class ItemsSelectorWidget(Widget): + + def draw_item_selector(self, title, selection): + lines = [title] + counter = 0 + 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 + line_width = self.size[1] + to_join = [] + for line in lines: + to_pad = line_width - (len(line) % line_width) + if to_pad == line_width: + to_pad = 0 + to_join += [line + ' '*to_pad] + 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): - def terrain_with_objects(): + 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) - terrain_as_list[pos_i] = self.tui.game.symbol_for_type(t.type_) - return ''.join(terrain_as_list) + symbol = self.tui.game.symbol_for_type(t.type_) + if terrain_as_list[pos_i][0] in {'i', '@', 'm'}: + old_symbol = terrain_as_list[pos_i][0] + if old_symbol in {'@', 'm'}: + symbol = old_symbol + terrain_as_list[pos_i] = (symbol, '+') + else: + 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) + 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] @@ -309,10 +415,14 @@ class MapWidget(Widget): for c in ''.join(lines): if c in {'@', 'm'}: chars_with_attrs += [(c, curses.color_pair(1))] + elif c == 'i': + 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 @@ -323,9 +433,11 @@ class MapWidget(Widget): self.safe_write(''.join(lines)) return - terrain_with_objects = terrain_with_objects() - center = self.tui.game.world.player_position - lines = self.tui.game.world.map_.format_to_view(terrain_with_objects, + annotated_terrain = annotated_terrain() + 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) pad_or_cut_x(lines) pad_y(lines) @@ -338,79 +450,250 @@ 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): + self.text_line = text_line + super().__init__(*args, **kwargs) + + def draw(self): + self.safe_write(self.text_line) + + class TUI: - def __init__(self, plom_socket, game): + def __init__(self, plom_socket, game, q): self.socket = plom_socket self.game = game + self.game.tui = self + self.queue = q self.parser = Parser(self.game) - self.to_update = {'edit': False} + self.to_update = {} + self.item_pointer = 0 + self.examiner_position = (0, 0) + self.examiner_mode = False + self.popup_text = 'Hi bob' + self.to_send = [] + self.draw_popup_if_visible = True curses.wrapper(self.loop) - def setup_screen(self, stdscr): - self.stdscr = stdscr - self.stdscr.refresh() # will be called by getkey else, clearing screen - self.stdscr.timeout(10) - self.stdscr.addstr(0, 0, 'SEND:') - self.stdscr.addstr(2, 0, 'TURN:') - def loop(self, stdscr): - self.setup_screen(stdscr) - curses.init_pair(1, curses.COLOR_BLACK, curses.COLOR_RED) - curses.init_pair(2, curses.COLOR_BLACK, curses.COLOR_GREEN) - curses.init_pair(3, curses.COLOR_BLACK, curses.COLOR_BLUE) + + def setup_screen(stdscr): + self.stdscr = stdscr + self.stdscr.refresh() # will be called by getkey else, clearing screen + self.stdscr.timeout(10) + + def switch_widgets(widget_1, widget_2): + widget_1.visible = False + widget_2.visible = 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 + 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: + return + trigger = widget.check_updates[0] + self.to_update[trigger] = True + + 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 + + def switch_to_pick_or_drop(target_widget): + self.item_pointer = 0 + switch_widgets(map_widget, target_widget) + if self.examiner_mode: + self.examiner_mode = False + switch_widgets(descriptor_widget, log_widget) + + def toggle_examiner_mode(): + if self.examiner_mode: + self.examiner_mode = False + switch_widgets(descriptor_widget, log_widget) + else: + self.examiner_mode = True + self.examiner_position = self.game.world.player.position + switch_widgets(log_widget, descriptor_widget) + self.to_update['map'] = True + + def toggle_popup(): + if popup_widget.visible: + popup_widget.visible = False + for w in top_widgets: + w.ensure_freshness(True) + else: + self.to_update['popup'] = True + popup_widget.visible = True + popup_widget.reconfigure() + self.draw_popup_if_visible = True + + def try_write_keys(): + if len(key) == 1 and key in ASCII_printable and \ + len(self.to_send) < len(edit_line_widget): + self.to_send += [key] + self.to_update['edit'] = True + elif key == 'KEY_BACKSPACE': + self.to_send[:] = self.to_send[:-1] + self.to_update['edit'] = True + elif key == '\n': # Return key + self.socket.send(''.join(self.to_send)) + self.to_send[:] = [] + self.to_update['edit'] = True + + def try_examiner_keys(): + if key == 'w': + move_examiner('UPLEFT') + elif key == 'e': + move_examiner('UPRIGHT') + elif key == 's': + move_examiner('LEFT') + elif key == 'd': + move_examiner('RIGHT') + elif key == 'x': + move_examiner('DOWNLEFT') + elif key == 'c': + move_examiner('DOWNRIGHT') + + def try_player_move_keys(): + if key == 'w': + self.socket.send('TASK:MOVE UPLEFT') + elif key == 'e': + self.socket.send('TASK:MOVE UPRIGHT') + elif key == 's': + self.socket.send('TASK:MOVE LEFT') + elif key == 'd': + self.socket.send('TASK:MOVE RIGHT') + elif key == 'x': + self.socket.send('TASK:MOVE DOWNLEFT') + elif key == 'c': + self.socket.send('TASK:MOVE DOWNRIGHT') + + def init_colors(): + curses.init_pair(1, curses.COLOR_BLACK, curses.COLOR_RED) + 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) curses.curs_set(False) # hide cursor - self.to_send = [] - self.edit = EditWidget(self, (0, 6), (1, 14), check_tui = ['edit']) - self.turn = TurnWidget(self, (2, 6), (1, 14), ['turn']) - self.log = LogWidget(self, (4, 0), (None, 20), ['log']) - self.map_ = MapWidget(self, (0, 21), (None, None), ['map']) - widgets = (self.edit, self.turn, self.log, self.map_) - map_mode = False + 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.children += [edit_line_widget] + turn_widget = TextLineWidget('TURN:', self, (2, 0), (1, 20)) + turn_widget.children += [TurnWidget(self, (2, 6), (1, 14), ['turn'])] + 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, health_widget, log_widget, + descriptor_widget, map_widget, inventory_widget, + pickable_items_widget] + popup_widget = PopUpWidget(self, (0, 0), (1, 1), visible=False) + + # Ensure initial window state before loop starts. + for w in top_widgets: + w.ensure_freshness(True) + self.socket.send('GET_GAMESTATE') + write_mode = False while True: - for w in widgets: - w.ensure_freshness() - for key in self.game.to_update: - self.game.to_update[key] = False - for key in self.to_update: - self.to_update[key] = False + + # Draw screen. + for w in top_widgets: + if w.ensure_freshness(): + self.draw_popup_if_visible = True + if popup_widget.visible and self.draw_popup_if_visible: + popup_widget.ensure_freshness(True) + self.draw_popup_if_visible = False + for k in self.to_update.keys(): + self.to_update[k] = False + + # Handle input from server. + while True: + try: + command = self.queue.get(block=False) + except queue.Empty: + break + self.game.handle_input(command) + + # Handle keys (and resize event read as key). try: key = self.stdscr.getkey() if key == 'KEY_RESIZE': curses.endwin() - self.setup_screen(curses.initscr()) - for w in widgets: + setup_screen(curses.initscr()) + for w in top_widgets: w.size = w.size_def w.ensure_freshness(True) elif key == '\t': # Tabulator key. - map_mode = False if map_mode else True - elif map_mode: - if key == 'w': - self.socket.send('TASK:MOVE UPLEFT') - elif key == 'e': - self.socket.send('TASK:MOVE UPRIGHT') - if key == 's': - self.socket.send('TASK:MOVE LEFT') - elif key == 'd': - self.socket.send('TASK:MOVE RIGHT') - if key == 'x': - self.socket.send('TASK:MOVE DOWNLEFT') - elif key == 'c': - self.socket.send('TASK:MOVE DOWNRIGHT') - else: - if len(key) == 1 and key in ASCII_printable and \ - len(self.to_send) < len(self.edit): - self.to_send += [key] - self.to_update['edit'] = True - elif key == 'KEY_BACKSPACE': - self.to_send[:] = self.to_send[:-1] - self.to_update['edit'] = True - elif key == '\n': # Return key - self.socket.send(''.join(self.to_send)) - self.to_send[:] = [] - self.to_update['edit'] = True + write_mode = False if write_mode else True + elif write_mode: + try_write_keys() + elif key == 't': + toggle_popup() + elif map_widget.visible: + if key == '?': + toggle_examiner_mode() + elif key == 'p': + self.socket.send('GET_PICKABLE_ITEMS') + switch_to_pick_or_drop(pickable_items_widget) + elif key == 'i': + switch_to_pick_or_drop(inventory_widget) + elif self.examiner_mode: + try_examiner_keys() + 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') + elif inventory_widget.visible: + pick_or_drop_menu('d', inventory_widget, + self.game.world.player_inventory, + 'DROP') except curses.error: pass + + # Quit when server recommends it. if self.game.do_quit: break @@ -418,6 +701,7 @@ class TUI: s = socket.create_connection(('127.0.0.1', 5000)) plom_socket = PlomSocket(s) game = Game() -t = threading.Thread(target=recv_loop, args=(plom_socket, game)) +q = queue.Queue() +t = threading.Thread(target=recv_loop, args=(plom_socket, game, q)) t.start() -TUI(plom_socket, game) +TUI(plom_socket, game, q)