- def loop(self, stdscr):
- import datetime
-
- def safe_addstr(y, x, line):
- if y < self.size.y - 1 or x + len(line) < self.size.x:
- stdscr.addstr(y, x, line, curses.color_pair(1))
- else: # workaround to <https://stackoverflow.com/q/7063128>
- cut_i = self.size.x - x - 1
- cut = line[:cut_i]
- last_char = line[cut_i]
- stdscr.addstr(y, self.size.x - 2, last_char, curses.color_pair(1))
- stdscr.insstr(y, self.size.x - 2, ' ')
- stdscr.addstr(y, x, cut, curses.color_pair(1))
-
- def handle_input(msg):
- command, args = self.parser.parse(msg)
- command(*args)
-
- def task_action_on(action):
- return action_tasks[action] in self.game.tasks
-
- def msg_into_lines_of_width(msg, width):
- chunk = ''
- lines = []
- x = 0
- for i in range(len(msg)):
- if x >= width or msg[i] == "\n":
- lines += [chunk]
- chunk = ''
- x = 0
- if msg[i] == "\n":
- x -= 1
- if msg[i] != "\n":
- chunk += msg[i]
- x += 1
- lines += [chunk]
- return lines
-
- def reset_screen_size():
- self.size = YX(*stdscr.getmaxyx())
- self.size = self.size - YX(self.size.y % 4, 0)
- self.size = self.size - YX(0, self.size.x % 4)
- self.window_width = int(self.size.x / 2)
-
- def recalc_input_lines():
- if not self.mode.has_input_prompt:
- self.input_lines = []
- else:
- self.input_lines = msg_into_lines_of_width(input_prompt + self.input_,
- self.window_width)
-
- def move_explorer(direction):
- target = self.game.map_geometry.move_yx(self.explorer, direction)
- if target:
- self.explorer = target
- if self.mode.shows_info:
- self.query_info()
- if self.tile_draw:
- self.send_tile_control_command()
- else:
- self.flash = True
-
- def draw_history():
- lines = []
- for line in self.log:
- lines += msg_into_lines_of_width(line, self.window_width)
- lines.reverse()
- height_header = 2
- max_y = self.size.y - len(self.input_lines)
- for i in range(len(lines)):
- if (i >= max_y - height_header):
- break
- safe_addstr(max_y - i - 1, self.window_width, lines[i])
-
- def draw_info():
- if not self.game.turn_complete:
- return
- pos_i = self.explorer.y * self.game.map_geometry.size.x + self.explorer.x
- info = 'MAP VIEW: %s\n' % self.map_mode
- if self.game.fov[pos_i] != '.':
- info += 'outside field of view'
- else:
- terrain_char = self.game.map_content[pos_i]
- terrain_desc = '?'
- if terrain_char in self.game.terrains:
- terrain_desc = self.game.terrains[terrain_char]
- info += 'TERRAIN: "%s" / %s\n' % (terrain_char, terrain_desc)
- protection = self.game.map_control_content[pos_i]
- if protection == '.':
- protection = 'unprotected'
- info += 'PROTECTION: %s\n' % protection
- for t in self.game.things:
- if t.position == self.explorer:
- protection = t.protection
- if protection == '.':
- protection = 'none'
- info += 'THING: %s / %s' % (t.type_,
- self.game.thing_types[t.type_])
- if hasattr(t, 'thing_char'):
- info += t.thing_char
- if hasattr(t, 'name'):
- info += ' (%s)' % t.name
- info += ' / protection: %s\n' % protection
- if self.explorer in self.game.portals:
- info += 'PORTAL: ' + self.game.portals[self.explorer] + '\n'
- else:
- info += 'PORTAL: (none)\n'
- if self.explorer in self.game.info_db:
- info += 'ANNOTATION: ' + self.game.info_db[self.explorer]
- else:
- info += 'ANNOTATION: waiting …'
- lines = msg_into_lines_of_width(info, self.window_width)
- height_header = 2
- for i in range(len(lines)):
- y = height_header + i
- if y >= self.size.y - len(self.input_lines):
- break
- safe_addstr(y, self.window_width, lines[i])
-
- def draw_input():
- y = self.size.y - len(self.input_lines)
- for i in range(len(self.input_lines)):
- safe_addstr(y, self.window_width, self.input_lines[i])
- y += 1
-
- def draw_turn():
- if not self.game.turn_complete:
- return
- safe_addstr(0, self.window_width, 'TURN: ' + str(self.game.turn))
-
- def draw_mode():
- help = "hit [%s] for help" % self.keys['help']
- if self.mode.has_input_prompt:
- help = "enter /help for help"
- safe_addstr(1, self.window_width,
- 'MODE: %s – %s' % (self.mode.short_desc, help))
-
- def draw_map():
- if not self.game.turn_complete:
- return
+ def get_info(self):
+ if self.info_cached:
+ return self.info_cached
+ pos_i = self.explorer.y * self.game.map_geometry.size.x + self.explorer.x
+ info_to_cache = ''
+ if len(self.game.fov) > pos_i and self.game.fov[pos_i] != '.':
+ info_to_cache += 'outside field of view'
+ else:
+ for t in self.game.things:
+ if t.position == self.explorer:
+ info_to_cache += '%s' % self.get_thing_info(t, True)
+ terrain_char = self.game.map_content[pos_i]
+ terrain_desc = '?'
+ if terrain_char in self.game.terrains:
+ terrain_desc = self.game.terrains[terrain_char]
+ info_to_cache += 'TERRAIN: %s (%s' % (terrain_char,
+ terrain_desc)
+ protection = self.game.map_control_content[pos_i]
+ if protection != '.':
+ info_to_cache += '/protection:%s' % protection
+ info_to_cache += ')\n'
+ if self.explorer in self.game.portals:
+ info_to_cache += 'PORTAL: ' +\
+ self.game.portals[self.explorer] + '\n'
+ if self.explorer in self.game.annotations:
+ info_to_cache += 'ANNOTATION: ' +\
+ self.game.annotations[self.explorer]
+ self.info_cached = info_to_cache
+ return self.info_cached
+
+ def get_thing_info(self, t, detailed=False):
+ info = ''
+ if detailed:
+ info += '- '
+ info += self.game.thing_types[t.type_]
+ if hasattr(t, 'thing_char'):
+ info += t.thing_char
+ if hasattr(t, 'name'):
+ info += ': %s' % t.name
+ info += ' (%s' % t.type_
+ if hasattr(t, 'installed'):
+ info += '/installed'
+ if t.type_ == 'Bottle':
+ if t.thing_char == '_':
+ info += '/empty'
+ elif t.thing_char == '~':
+ info += '/full'
+ if detailed:
+ protection = t.protection
+ if protection != '.':
+ info += '/protection:%s' % protection
+ info += ')\n'
+ if hasattr(t, 'hat') or hasattr(t, 'face'):
+ info += '----------\n'
+ if hasattr(t, 'hat'):
+ info += '| %s |\n' % t.hat[0:6]
+ info += '| %s |\n' % t.hat[6:12]
+ info += '| %s |\n' % t.hat[12:18]
+ if hasattr(t, 'face'):
+ info += '| %s |\n' % t.face[0:6]
+ info += '| %s |\n' % t.face[6:12]
+ info += '| %s |\n' % t.face[12:18]
+ info += '----------\n'
+ if hasattr(t, 'design'):
+ line_length = t.design[0].x
+ lines = []
+ for i in range(t.design[0].y):
+ start = i * line_length
+ end = (i + 1) * line_length
+ lines += [t.design[1][start:end]]
+ info += '-' * (line_length + 4) + '\n'
+ for line in lines:
+ info += '| %s |\n' % line
+ info += '-' * (line_length + 4) + '\n'
+ else:
+ info += ')'
+ return info
+
+ def reset_size(self):
+ super().reset_size()
+ self.left_window_width = min(52, int(self.size.x / 2))
+ self.right_window_width = self.size.x - self.left_window_width
+
+ def addstr(self, y, x, line, ignore=None):
+ super().addstr(y, x, line, curses.color_pair(1))
+
+ def init_loop(self):
+ self.switch_mode('waiting_for_server')
+ curses.start_color()
+ self.set_default_colors()
+ curses.init_pair(1, 7, 0)
+ if not curses.can_change_color():
+ self.log('@ unfortunately, your terminal does not seem to '
+ 'support re-definition of colors; you might miss out '
+ 'on some color effects')
+ super().init_loop()
+
+ def recalc_input_lines(self):
+ if not self.mode.has_input_prompt:
+ self.input_lines = []
+ else:
+ self.input_lines = msg_into_lines_of_width(self.input_prompt
+ + self.input_ + '█',
+ self.right_window_width)
+ def draw_history(self):
+ lines = []
+ for line in self._log:
+ lines += msg_into_lines_of_width(line, self.right_window_width)
+ lines.reverse()
+ height_header = 2
+ max_y = self.size.y - len(self.input_lines)
+ for i in range(len(lines)):
+ if (i >= max_y - height_header):
+ break
+ self.addstr(max_y - i - 1, self.left_window_width, lines[i])
+
+ def draw_info(self):
+ info = 'MAP VIEW: %s\n%s' % (self.map_mode, self.get_info())
+ lines = msg_into_lines_of_width(info, self.right_window_width)
+ height_header = 2
+ for i in range(len(lines)):
+ y = height_header + i
+ if y >= self.size.y - len(self.input_lines):
+ break
+ self.addstr(y, self.left_window_width, lines[i])
+
+ def draw_input(self):
+ y = self.size.y - len(self.input_lines)
+ for i in range(len(self.input_lines)):
+ self.addstr(y, self.left_window_width, self.input_lines[i])
+ y += 1
+
+ def draw_stats(self):
+ stats = 'ENERGY: %s BLADDER: %s' % (self.game.energy,
+ self.game.bladder_pressure)
+ self.addstr(0, self.left_window_width, stats)
+
+ def draw_mode(self):
+ help = "hit [%s] for help" % self.keys['help']
+ if self.mode.has_input_prompt:
+ help = "enter /help for help"
+ self.addstr(1, self.left_window_width,
+ 'MODE: %s – %s' % (self.mode.short_desc, help))
+
+ def draw_map(self):
+ if (not self.game.turn_complete) and len(self.map_lines) == 0:
+ return
+ if self.game.turn_complete: