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.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)
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']
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)
on any update, even before we actually receive map data.
"""
super().__init__(*args, **kwargs)
- self.map_ = Map()
+ 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):
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:
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
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):
- self.visible = True
size = (1, len(self.tui.popup_text))
self.size = size
self.size_def = size
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)
symbol = self.tui.game.symbol_for_type(t.type_)
- if symbol in {'i'} and terrain_as_list[pos_i] in {'@', 'm'}:
- continue
- terrain_as_list[pos_i] = symbol
- return ''.join(terrain_as_list)
+ 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]
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(''.join(lines))
return
- terrain_with_objects = terrain_with_objects()
+ annotated_terrain = annotated_terrain()
center = self.tui.game.world.player.position
- lines = self.tui.game.world.map_.format_to_view(terrain_with_objects,
+ 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)
self.parser = Parser(self.game)
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 loop(self, stdscr):
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
+ self.to_update['descriptor'] = 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.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.curs_set(False) # hide cursor
- self.to_send = []
+ 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'])]
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, map_widget,
- inventory_widget, pickable_items_widget]
+ top_widgets = [edit_widget, turn_widget, log_widget,
+ descriptor_widget, map_widget, inventory_widget,
+ pickable_items_widget]
popup_widget = PopUpWidget(self, (0, 0), (1, 1), visible=False)
- self.popup_text = 'Hi bob'
- write_mode = True
+
+ # Ensure initial window state before loop starts.
for w in top_widgets:
w.ensure_freshness(True)
- draw_popup_if_visible = True
+ self.socket.send('GET_GAMESTATE')
+ write_mode = False
while True:
+
+ # Draw screen.
for w in top_widgets:
- did_refresh = w.ensure_freshness()
- draw_popup_if_visible = did_refresh | draw_popup_if_visible
- if popup_widget.visible and draw_popup_if_visible:
+ 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)
- draw_popup_if_visible = False
+ 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':
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
+ try_write_keys()
elif key == 't':
- if not popup_widget.visible:
- self.to_update['popup'] = True
- popup_widget.visible = True
- popup_widget.reconfigure()
- draw_popup_if_visible = True
- else:
- popup_widget.visible = False
- for w in top_widgets:
- w.ensure_freshness(True)
+ toggle_popup()
elif map_widget.visible:
- 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')
+ if key == '?':
+ toggle_examiner_mode()
elif key == 'p':
self.socket.send('GET_PICKABLE_ITEMS')
- self.item_pointer = 0
- switch_widgets(map_widget, pickable_items_widget)
+ switch_to_pick_or_drop(pickable_items_widget)
elif key == 'i':
- self.item_pointer = 0
- switch_widgets(map_widget, inventory_widget)
+ 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,
'DROP')
except curses.error:
pass
+
+ # Quit when server recommends it.
if self.game.do_quit:
break