+def read_worldstate():
+ if not os.access(io["path_worldstate"], os.F_OK):
+ msg = "No world state file found at " + io["path_worldstate"] + "."
+ raise SystemExit(msg)
+ read_anew = False
+ worldstate_file = open(io["path_worldstate"], "r")
+ turn_string = worldstate_file.readline()
+ if int(turn_string) != world_data["turn"]:
+ read_anew = True
+ if not read_anew: # In rare cases, world may change, but not turn number.
+ mtime = os.stat(io["path_worldstate"])
+ if mtime != read_worldstate.last_checked_mtime:
+ read_worldstate.last_checked_mtime = mtime
+ read_anew = True
+ if read_anew:
+ cursed_main.redraw = True
+ world_data["turn"] = int(turn_string)
+ world_data["lifepoints"] = int(worldstate_file.readline())
+ world_data["satiation"] = int(worldstate_file.readline())
+ world_data["inventory"] = []
+ while True:
+ line = worldstate_file.readline().replace("\n", "")
+ if line == '%':
+ break
+ world_data["inventory"] += [line]
+ world_data["avatar_position"][0] = int(worldstate_file.readline())
+ world_data["avatar_position"][1] = int(worldstate_file.readline())
+ if not world_data["look_mode"]:
+ world_data["map_center"][0] = world_data["avatar_position"][0]
+ world_data["map_center"][1] = world_data["avatar_position"][1]
+ world_data["map_size"] = int(worldstate_file.readline())
+ world_data["fov_map"] = ""
+ for i in range(world_data["map_size"]):
+ line = worldstate_file.readline().replace("\n", "")
+ world_data["fov_map"] += line
+ world_data["mem_map"] = ""
+ for i in range(world_data["map_size"]):
+ line = worldstate_file.readline().replace("\n", "")
+ world_data["mem_map"] += line
+ worldstate_file.close()
+read_worldstate.last_checked_mtime = -1
+
+
+def read_message_queue():
+ while (len(message_queue["messages"]) > 1
+ or (len(message_queue["messages"]) == 1
+ and not message_queue["open_end"])):
+ message = message_queue["messages"].pop(0)
+ if message == "THINGS_HERE START":
+ read_message_queue.parse_thingshere = True
+ world_data["look"] = []
+ elif message == "THINGS_HERE END":
+ read_message_queue.parse_thingshere = False
+ if world_data["look"] == []:
+ world_data["look"] = ["(none known)"]
+ cursed_main.redraw = True
+ elif read_message_queue.parse_thingshere:
+ world_data["look"] += [message]
+ elif message[0:4] == "LOG ":
+ world_data["log"] += [message[4:]]
+ cursed_main.redraw = True
+ elif message == "WORLD_UPDATED":
+ query_mapcell()
+read_message_queue.parse_thingshere = False
+
+
+def query_mapcell():
+ command_sender("THINGS_HERE " + str(world_data["map_center"][0]) + " "
+ + str(world_data["map_center"][1]))()
+ world_data["look"] = ["(polling)"]
+
+
+def cursed_main(stdscr):
+
+ def ping_test():
+ half_wait_time = 5
+ if len(new_data_from_server) > 0:
+ ping_test.sent = False
+ elif ping_test.wait_start + half_wait_time < time.time():
+ if not ping_test.sent:
+ io["file_out"].write("PING\n")
+ io["file_out"].flush()
+ ping_test.sent = True
+ ping_test.wait_start = time.time()
+ elif ping_test.sent:
+ raise SystemExit("Server not answering anymore.")
+ ping_test.wait_start = 0
+
+ def read_into_message_queue():
+ if new_data_from_server == "":
+ return
+ new_open_end = False
+ if new_data_from_server[-1] is not "\n":
+ new_open_end = True
+ new_messages = new_data_from_server.splitlines()
+ if message_queue["open_end"]:
+ message_queue["messages"][-1] += new_messages[0]
+ del new_messages[0]
+ message_queue["messages"] += new_messages
+ if new_open_end:
+ message_queue["open_end"] = True
+