+ if world_data["inventory_selection"] > win_size[0]/2:
+ if world_data["inventory_selection"] < len(world_data["inventory"]) \
+ - win_size[0]/2:
+ offset[0] = world_data["inventory_selection"] - int(win_size[0]/2)
+ else:
+ offset[0] = len(world_data["inventory"]) - win_size[0]
+ return offset, winmap_size, winmap
+
+
+def win_look():
+ winmap = ""
+ winmap_size = [0, 0]
+ for line in world_data["look"]:
+ winmap_size[1] = winmap_size[1] if len(line) <= winmap_size[1] \
+ else len(line)
+ for line in world_data["look"]:
+ padding_size = winmap_size[1] - len(line)
+ winmap += line + (" " * padding_size)
+ winmap_size[0] = winmap_size[0] + 1
+ offset = [0, 0]
+ return offset, winmap_size, winmap
+
+
+def win_info():
+ winmap = "T: " + str(world_data["turn"]) \
+ + " H: " + str(world_data["lifepoints"]) \
+ + " S: " + str(world_data["satiation"])
+ winmap_size = [1, len(winmap)]
+ offset = [0, 0]
+ return offset, winmap_size, winmap
+
+
+def win_log():
+ win_size = next(win["size"] for win in windows if win["func"] == win_log)
+ offset = [0, 0]
+ winmap = ""
+ number_of_lines = 0
+ for line in world_data["log"]:
+ number_of_lines += math.ceil(len(line) / win_size[1])
+ padding_size = win_size[1] - (len(line) % win_size[1])
+ winmap += line + (padding_size * " ")
+ if number_of_lines < win_size[0]:
+ winmap = (" " * win_size[1] * (win_size[0] - number_of_lines)) + winmap
+ number_of_lines = win_size[0]
+ elif number_of_lines > win_size[0]:
+ offset[0] = number_of_lines - win_size[0]
+ winmap_size = [number_of_lines, win_size[1]]
+ return offset, winmap_size, winmap
+
+
+def command_quit():
+ command_sender("QUIT")()
+ raise SystemExit("Received QUIT command, forwarded to server, leaving.")
+
+
+def command_toggle_look_mode():
+ if not world_data["look_mode"]:
+ world_data["look_mode"] = True
+ else:
+ world_data["look_mode"] = False
+ world_data["map_center"] = world_data["avatar_position"]
+ query_mapcell()
+
+
+def command_sender(string):
+ def command_send():
+ io["file_out"].write(string + "\n")
+ io["file_out"].flush()
+ return command_send
+
+
+def command_looker(string):
+ def command_look():
+ if string == "west" \
+ and world_data["map_center"][1] > 0:
+ world_data["map_center"][1] -= 1
+ elif string == "east" \
+ and world_data["map_center"][1] < world_data["map_size"] - 1:
+ world_data["map_center"][1] += 1
+ else:
+ y_unevenness = world_data["map_center"][0] % 2
+ y_evenness = int(not(y_unevenness))
+ if string[6:] == "west" and \
+ world_data["map_center"][1] > -y_unevenness:
+ if string[:5] == "north" and world_data["map_center"][0] > 0:
+ world_data["map_center"][0] -= 1
+ world_data["map_center"][1] -= y_evenness
+ if string[:5] == "south" and world_data["map_center"][0] \
+ < world_data["map_size"] - 1:
+ world_data["map_center"][0] += 1
+ world_data["map_center"][1] -= y_evenness
+ elif string[6:] == "east" and world_data["map_center"][1] \
+ < world_data["map_size"] - y_unevenness:
+ if string[:5] == "north" and world_data["map_center"][0] > 0:
+ world_data["map_center"][0] -= 1
+ world_data["map_center"][1] += y_unevenness
+ if string[:5] == "south" and world_data["map_center"][0] \
+ < world_data["map_size"] - 1:
+ world_data["map_center"][0] += 1
+ world_data["map_center"][1] += y_unevenness
+ query_mapcell()
+ return command_look
+
+
+def command_inventory_selector(string):
+ def command_inventory_select():
+ logfile = open("logfile", "a")
+ logfile.write(string + "\n")
+ logfile.close()
+ if string == "up" and world_data["inventory_selection"] > 0:
+ world_data["inventory_selection"] -= 1
+ elif string == "down" and world_data["inventory_selection"] \
+ < len(world_data["inventory"]) - 1:
+ world_data["inventory_selection"] += 1
+ return command_inventory_select