X-Git-Url: https://plomlompom.com/repos/foo.html?a=blobdiff_plain;ds=inline;f=new%2Fexample_client.py;h=19538161a5b3d575df3d80fe9cfe77c42f3b2f26;hb=d2324a2fcb0d4562ff2da13bb7f92f291589d6b4;hp=66c2bf651ea777d77f1ed48dd9f155c836287dde;hpb=e08cc5e251e260b969e374b610b09d3b8fdc4f9f;p=plomrogue2-experiments
diff --git a/new/example_client.py b/new/example_client.py
index 66c2bf6..1953816 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.game import Game, WorldBase
-from plomrogue.mapping import MapBase
+from plomrogue.commands import cmd_PLAYER_ID, cmd_THING_HEALTH
+from plomrogue.game import GameBase
+from plomrogue.mapping import Map, MapGeometryHex, YX
from plomrogue.io import PlomSocket
from plomrogue.things import ThingBase
import types
import queue
-class Map(MapBase):
+class ClientMap(Map):
def y_cut(self, map_lines, center_y, view_height):
map_height = len(map_lines)
@@ -33,100 +33,110 @@ 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):
- map_view_chars = ['0']
+ def map_cells_to_lines(map_cells):
+ map_view_chars = []
+ if self.start_indented:
+ 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]:
+ if x == self.size.x:
map_view_chars += ['\n']
x = 0
y += 1
- if y % 2 == 0:
+ if y % 2 == int(not self.start_indented):
map_view_chars += ['0']
- if y % 2 == 0:
+ if y % 2 == int(not self.start_indented):
map_view_chars = map_view_chars[:-1]
map_view_chars = map_view_chars[:-1]
return ''.join(map_view_chars).split('\n')
- map_lines = map_string_to_lines(map_string)
- 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)
+ map_lines = map_cells_to_lines(map_cells)
+ 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
-class World(WorldBase):
-
- def __init__(self, *args, **kwargs):
- """Extend original with local classes and empty default map.
-
- We need the empty default map because we draw the map widget
- on any update, even before we actually receive map data.
- """
- super().__init__(*args, **kwargs)
- self.map_ = Map()
- self.player_inventory = []
- self.player_id = 0
- self.pickable_items = []
-
- def new_map(self, yx):
- self.map_ = Map(yx)
-
- @property
- def player(self):
- return self.get_thing(self.player_id)
-
-
def cmd_LAST_PLAYER_TASK_RESULT(game, msg):
if msg != "success":
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.log_text = ''
+ game.turn = n
+ game.things = []
+ game.pickable_items[:] = []
cmd_TURN.argtypes = 'int:nonneg'
+
+def cmd_VISIBLE_MAP(game, size, indent_first_line):
+ game.new_map(size, indent_first_line)
+cmd_VISIBLE_MAP.argtypes = 'yx_tuple:pos bool'
+
+
def cmd_VISIBLE_MAP_LINE(game, y, terrain_line):
- game.world.map_.set_line(y, terrain_line)
+ game.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 = game.get_thing(i, create_unfound=True)
t.type_ = type_
cmd_THING_TYPE.argtypes = 'int:nonneg string'
+
+def cmd_THING_POS(game, i, yx):
+ t = game.get_thing(i,create_unfound=True)
+ 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.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.tui.to_update['map'] = True
+ game.pickable_items[:] = ids
+ game.tui.to_update['pickable_items'] = True
cmd_PICKABLE_ITEMS.argtypes = 'seq:int:nonneg'
-class Game:
+class Game(GameBase):
- def __init__(self):
+ def __init__(self, *args, **kwargs):
+ super().__init__(*args, **kwargs)
+ self.map_ = ClientMap() # we need an empty default map cause we draw
+ self.offset = YX(0,0) # the map widget even before we get a real one
+ self.player_inventory = []
+ self.player_id = 0
+ self.pickable_items = []
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,
@@ -135,14 +145,22 @@ 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
self.tui = None
+ def new_map(self, size, indent_first_line):
+ self.map_ = ClientMap(size, start_indented=indent_first_line)
+
+ @property
+ def player(self):
+ return self.get_thing(self.player_id, create_unfound=False)
+
def get_command(self, command_name):
from functools import partial
if command_name in self.commands:
@@ -180,8 +198,8 @@ class Game:
symbol = '@'
elif type_ == 'monster':
symbol = 'm'
- elif type_ == 'item':
- symbol = 'i'
+ elif type_ == 'food':
+ symbol = 'f'
return symbol
@@ -196,33 +214,35 @@ def recv_loop(plom_socket, game, q):
class Widget:
- def __init__(self, tui, start, size, check_updates=[]):
+ 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.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
- self.visible = True
+ self.visible = visible
self.children = []
@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):
@@ -232,14 +252,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:
@@ -251,27 +267,29 @@ 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])
def ensure_freshness(self, do_refresh=False):
- if not self.visible:
- return
- 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()
- for child in self.children:
- child.ensure_freshness(do_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):
@@ -280,13 +298,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):
- line_width = self.size[1]
- log_lines = self.tui.game.log_text.split('\n')
+ lines = self.get_text_lines()
+ line_width = self.size.x
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
@@ -294,44 +312,68 @@ 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.map_.\
+ get_position_index(self.tui.examiner_position[1])
+ terrain = self.tui.game.map_.terrain[pos_i]
+ lines = [terrain]
+ for t in self.tui.game.things_at_pos(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):
- self.visible = True
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])
- self.ensure_freshness(True)
+ 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 MapWidget(Widget):
+class ItemsSelectorWidget(Widget):
+
+ 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):
- if self.tui.view == 'map':
- self.draw_map()
- elif self.tui.view == 'inventory':
- self.draw_item_selector('INVENTORY:',
- self.tui.game.world.player_inventory)
- elif self.tui.view == 'pickable_items':
- self.draw_item_selector('PICKABLE:',
- self.tui.game.world.pickable_items)
-
- def draw_item_selector(self, title, selection):
- lines = [title]
+ 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_)
+ t = self.tui.game.get_thing(id_, create_unfound=False)
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)
@@ -340,20 +382,33 @@ class MapWidget(Widget):
to_join += [line + ' '*to_pad]
self.safe_write((''.join(to_join), curses.color_pair(3)))
- def draw_map(self):
- def terrain_with_objects():
- 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)
- symbol = self.tui.game.symbol_for_type(t.type_)
- if symbol in {'i'} and terrain_as_list[pos_i] in {'@', 'm'}:
+class MapWidget(Widget):
+
+ def draw(self):
+
+ def annotated_terrain():
+ terrain_as_list = list(self.tui.game.map_.terrain[:])
+ for t in self.tui.game.things:
+ if t.id_ in self.tui.game.player_inventory:
continue
- terrain_as_list[pos_i] = symbol
- return ''.join(terrain_as_list)
+ pos_i = self.tui.game.map_.get_position_index(t.position[1])
+ symbol = self.tui.game.symbol_for_type(t.type_)
+ 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
+ terrain_as_list[pos_i] = (symbol, '+')
+ else:
+ terrain_as_list[pos_i] = symbol
+ if self.tui.examiner_mode:
+ pos_i = self.tui.game.map_.\
+ 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):
@@ -363,35 +418,39 @@ 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))]
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
- if self.tui.game.world.map_.terrain == '':
+ if self.tui.game.map_.terrain == '':
lines = []
pad_y(lines)
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,
- center, self.size)
+ annotated_terrain = annotated_terrain()
+ center = self.tui.game.player.position
+ if self.tui.examiner_mode:
+ center = self.tui.examiner_position
+ lines = self.tui.game.map_.\
+ format_to_view(annotated_terrain, center, self.size)
pad_or_cut_x(lines)
pad_y(lines)
self.safe_write(lines_to_colored_chars(lines))
@@ -400,7 +459,15 @@ class MapWidget(Widget):
class TurnWidget(Widget):
def draw(self):
- self.safe_write((str(self.tui.game.world.turn), curses.color_pair(2)))
+ self.safe_write((str(self.tui.game.turn), curses.color_pair(2)))
+
+
+class HealthWidget(Widget):
+
+ def draw(self):
+ if hasattr(self.tui.game.player, 'health'):
+ self.safe_write((str(self.tui.game.player.health),
+ curses.color_pair(2)))
class TextLineWidget(Widget):
@@ -423,140 +490,245 @@ class TUI:
self.parser = Parser(self.game)
self.to_update = {}
self.item_pointer = 0
- self.top_widgets = []
+ self.examiner_position = (YX(0,0), YX(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)
-
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)
- curses.init_pair(4, curses.COLOR_BLACK, curses.COLOR_YELLOW)
+
+ 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 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 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.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.player_inventory, f)
+
+ def move_examiner(direction):
+ start_pos = self.examiner_position
+ new_examine_pos = self.game.map_geometry.move(start_pos, direction,
+ self.game.map_.size,
+ self.game.map_.start_indented)
+ if new_examine_pos[0] == (0,0):
+ 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.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 = []
- edit_widget = TextLineWidget('SEND:', self, (0, 0), (1, 20))
- edit_line_widget = EditWidget(self, (0, 6), (1, 14), ['edit'])
+ init_colors()
+
+ # With screen initialized, set up widgets with their curses windows.
+ 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'])
- map_widget = MapWidget(self, (0, 21), (None, None), ['map'])
- popup_widget = PopUpWidget(self, (0, 0), (1, 1), ['popup'])
- popup_widget.visible = False
- self.popup_text = 'Hi bob'
- self.top_widgets = [edit_widget, turn_widget, log_widget, map_widget,
- popup_widget]
- write_mode = True
- self.view = 'map'
- for w in self.top_widgets:
+ 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.player_inventory,
+ self, YX(0, 21), YX(None, None),
+ ['inventory'], False)
+ pickable_items_widget = ItemsSelectorWidget('PICKABLE:',
+ self.game.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, YX(0, 0), YX(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 self.top_widgets:
- w.ensure_freshness()
+
+ # 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 self.top_widgets:
+ setup_screen(curses.initscr())
+ for w in top_widgets:
w.size = w.size_def
w.ensure_freshness(True)
elif key == '\t': # Tabulator key.
write_mode = False if write_mode else True
elif write_mode:
- 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
- elif self.view == 'map':
- 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')
- elif key == 't':
- if not popup_widget.visible:
- self.to_update['popup'] = True
- popup_widget.visible = True
- popup_widget.reconfigure()
- else:
- popup_widget.visible = False
- for w in self.top_widgets:
- w.ensure_freshness(True)
+ 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')
- self.item_pointer = 0
- self.view = 'pickable_items'
+ switch_to_pick_or_drop(pickable_items_widget)
elif key == 'i':
- self.item_pointer = 0
- self.view = 'inventory'
- self.to_update['map'] = True
- elif self.view == 'pickable_items':
- if key == 'c':
- self.view = 'map'
- elif key == 'j' and \
- len(self.game.world.pickable_items) > \
- self.item_pointer + 1:
- self.item_pointer += 1
- elif key == 'k' and self.item_pointer > 0:
- self.item_pointer -= 1
- elif key == 'p' and \
- len(self.game.world.pickable_items) > 0:
- id_ = self.game.world.pickable_items[self.item_pointer]
- self.socket.send('TASK:PICKUP %s' % id_)
- self.socket.send('GET_PICKABLE_ITEMS')
- if self.item_pointer > 0:
- self.item_pointer -= 1
+ switch_to_pick_or_drop(inventory_widget)
+ elif self.examiner_mode:
+ try_examiner_keys()
else:
- continue
- self.to_update['map'] = True
- elif self.view == 'inventory':
- if key == 'c':
- self.view = 'map'
- elif key == 'j' and \
- len(self.game.world.player_inventory) > \
- self.item_pointer + 1:
- self.item_pointer += 1
- elif key == 'k' and self.item_pointer > 0:
- self.item_pointer -= 1
- elif key == 'd' and \
- len(self.game.world.player_inventory) > 0:
- id_ = self.game.world.player_inventory[self.item_pointer]
- self.socket.send('TASK:DROP %s' % id_)
- if self.item_pointer > 0:
- self.item_pointer -= 1
- else:
- continue
- self.to_update['map'] = True
+ try_player_move_keys()
+ elif pickable_items_widget.visible:
+ pickup_menu(key)
+ elif inventory_widget.visible:
+ inventory_menu(key)
except curses.error:
pass
+
+ # Quit when server recommends it.
if self.game.do_quit:
break