+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)"]