"""Functions related to the user interface. """ from . import g import th WHITE = 37, RED = 31, GREEN = 32, YELLOW = 33, BLUE = 34, MAGENTA = 35, CYAN = 36, BRIGHT_WHITE = 1, 37 BRIGHT_RED = 1, 31 BRIGHT_GREEN = 1, 32 BRIGHT_YELLOW = 1, 33 BRIGHT_BLUE = 1, 34 BRIGHT_MAGENTA = 1, 35 BRIGHT_CYAN = 1, 36 MAJOR_WHITE = 7, 37 MAJOR_RED = 7, 31 MAJOR_GREEN = 7, 32 MAJOR_YELLOW = 7, 33 MAJOR_BLUE = 7, 34 MAJOR_MAGENTA = 7, 35 MAJOR_CYAN = 7, 36 BOLD = 1, INVERSE = 7, DEFAULT_COLOR = 0, import sys, tty, termios, math, textwrap def gstring(prompt='Type a string. '): g.lines = 0 return raw_input(prompt).strip() def getch(prompt='> ', chars=1): g.lines = 0 fd = sys.stdin.fileno() old_settings = termios.tcgetattr(fd) try: sys.stdout.write(prompt) tty.setraw(sys.stdin.fileno()) ch = sys.stdin.read(int(chars)).strip() finally: termios.tcsetattr(fd, termios.TCSADRAIN, old_settings) return ch def yn(prompt="Answer y/n (n) ", default=False): inp = getch(prompt) if inp == 'y': print 'yes' return True elif inp == 'n': print 'no' return False else: if default: print 'yes' else: print 'no' return default def pk(prompt="Press any key to continue."): getch(prompt) print def choice_str(prompt, choices, cancel=False): inp = '' l = int(math.log(len(choices), 10)) + 1 while 1: if cancel: print '0' * l + ': Cancel' for e, x in enumerate(choices): print '%%0%dd: %%s' % l % (e + 1, x) inp = getch(prompt, l) try: inp = int(inp) if cancel and inp == 0: print 0 return -1 elif inp > 0 and inp <= len(choices): print inp return inp - 1 else: print inp print 'Invalid selection.\n' continue except ValueError: print '-' continue def show(mons, l=1): _mons = ['%%0%dd: %%s' % l % (e + 1, m.describe(False)) for e, m in enumerate(mons)] return '\n'.join(_mons) show_mons = show_items = show def choice(prompt, l): _l = int(math.log(len(l), 10)) + 1 _mons = show(l, _l) while 1: print '0' * _l + ': Cancel' print _mons inp = getch(prompt, _l) try: inp = int(inp) if inp == 0: print 0 return None elif inp > 0 and inp <= len(l): print inp return l[inp - 1] else: print inp print 'Invalid selection.\n' continue except ValueError: print '-' continue def prenumbered_choice(prompt, l): _l = int(math.log(max(e for e, m in l) + 1, 10)) + 1 _mons = '\n'.join(['%%0%dd: %%s' % _l % (e + 1, m.describe(False)) for e, m in l]) choices = dict(l) while 1: print '0' * _l + ': Cancel' print _mons inp = getch(prompt, _l) try: inp = int(inp) if inp == 0: print 0 return None elif inp - 1 in choices: print inp return choices[inp - 1] else: print inp print 'Invalid selection.\n' continue except ValueError: print '-' continue def gnsew(prompt, here): s = '' if here.north: s += 'n' if here.south: s += 's' if here.east: s += 'e' if here.west: s += 'w' ret = None while ret is None: inp = getch('%s (%s, 0 to cancel) ' % (prompt, s)) if inp == '0': ret = 0 elif inp == 'n' and here.north: ret = here.north elif inp == 's' and here.south: ret = here.south elif inp == 'e' and here.east: ret = here.east elif inp == 'w' and here.west: ret = here.west else: print inp print inp return ret def get_spe(inp): for s in th.data.Species.one_of_everything: if s.name.lower() == inp.lower(): return s return None def color(args=None): if args is not None: return '\x1b[%sm' % ';'.join(map(str, args)) else: return '\x1b[0m' def hpcolor(mon): if mon.hp >= mon.mhp: return color(WHITE) elif mon.hp >= (mon.mhp * 3)/4: return color(GREEN) elif mon.hp > mon.mhp / 2: return color(YELLOW) elif mon.hp > mon.mhp / 4: return color(RED) else: return color(MAJOR_RED) def the_mon(who=None, showallied=True, possessive=False): if not who or who is g.player: return 'you' + ('r' if possessive else '') elif who.name: return who.name + ("'s" if possessive else '') else: return 'the %s%s%s%s' % ('allied ' if (showallied and who.perceived_ai(g.player) == th.Monster.PET) else '', ('insane ' if who.species.smart else 'rabid ') if who.rabid else '', who.species.name, "'s" if possessive else '') def format(s, mon): for k, v in mon.fmt.iteritems(): s = s.replace('|%s|' % k, v) return s def your(s, mon=None, do_pk=True): if g.phase == 0: return if mon is g.player or mon is None: ret = 'Your ' else: if mon.here is not g.player.here: return if mon.name: ret = "%s's " % mon.name else: if mon.rabid: rabid = 'insane ' if mon.species.smart else 'rabid ' else: rabid = '' ret = "The %s%s%s's " % ('allied ' if mon.perceived_ai(g.player) == th.Monster.PET else '', rabid, mon.species.name) ret += format(s, mon or g.player) msg(ret, do_pk=do_pk) def you(s, mon=None, do_pk=True): if g.phase == 0: return if mon is g.player or mon is None: ret = 'You ' else: if mon.here is not g.player.here: return if mon.name: ret = "%s " % mon.name else: if mon.rabid: rabid = 'insane ' if mon.species.smart else 'rabid ' else: rabid = '' ret = "The %s%s%s " % ('allied ' if mon.perceived_ai(g.player) == th.Monster.PET else '', rabid, mon.species.name) ret += format(s, mon or g.player) msg(ret, do_pk=do_pk) def msg(s, do_pk=True): for line in s.split('\n'): for line_ in textwrap.wrap(line, 80, replace_whitespace=0): print line_ g.lines += 1 if g.lines >= 28: pk() if do_pk: g.glidetrigger = True if g.lines > 1: g.unpk_only = False