+def try_worldstate_update():
+ """Write worldstate file if io_db["worldstate_updateable"] is set."""
+ if io_db["worldstate_updateable"]:
+
+ def draw_visible_Things(map, run):
+ for id in world_db["Things"]:
+ type = world_db["Things"][id]["T_TYPE"]
+ consumable = world_db["ThingTypes"][type]["TT_CONSUMABLE"]
+ alive = world_db["ThingTypes"][type]["TT_LIFEPOINTS"]
+ if (0 == run and not consumable and not alive) \
+ or (1 == run and consumable and not alive) \
+ or (2 == run and alive):
+ y = world_db["Things"][id]["T_POSY"]
+ x = world_db["Things"][id]["T_POSX"]
+ fovflag = world_db["Things"][0]["fovmap"][(y * length) + x]
+ if 'v' == chr(fovflag):
+ c = world_db["ThingTypes"][type]["TT_SYMBOL"]
+ map[(y * length) + x] = ord(c)
+
+ def write_map(string, map):
+ for i in range(length):
+ line = map[i * length:(i * length) + length].decode()
+ string = string + line + "\n"
+ return string
+
+ inventory = ""
+ if [] == world_db["Things"][0]["T_CARRIES"]:
+ inventory = "(none)\n"
+ else:
+ for id in world_db["Things"][0]["T_CARRIES"]:
+ type_id = world_db["Things"][id]["T_TYPE"]
+ name = world_db["ThingTypes"][type_id]["TT_NAME"]
+ inventory = inventory + name + "\n"
+ string = str(world_db["TURN"]) + "\n" + \
+ str(world_db["Things"][0]["T_LIFEPOINTS"]) + "\n" + \
+ str(world_db["Things"][0]["T_SATIATION"]) + "\n" + \
+ inventory + "%\n" + \
+ str(world_db["Things"][0]["T_POSY"]) + "\n" + \
+ str(world_db["Things"][0]["T_POSX"]) + "\n" + \
+ str(world_db["MAP_LENGTH"]) + "\n"
+ length = world_db["MAP_LENGTH"]
+ fov = bytearray(b' ' * (length ** 2))
+ for pos in range(length ** 2):
+ fovflag = world_db["Things"][0]["fovmap"][pos]
+ if 'v' == chr(fovflag):
+ fov[pos] = world_db["MAP"][pos]
+ for i in range(3):
+ draw_visible_Things(fov, i)
+ string = write_map(string, fov)
+ mem = world_db["Things"][0]["T_MEMMAP"][:]
+ for i in range(2):
+ for id in world_db["Things"][0]["T_MEMTHING"]:
+ type = world_db["Things"][id]["T_TYPE"]
+ consumable = world_db["ThingTypes"][type]["TT_CONSUMABLE"]
+ if (i == 0 and not consumable) or (i == 1 and consumable):
+ c = world_db["ThingTypes"][type]["TT_SYMBOL"]
+ mem[(y * length) + x] = ord(c)
+ string = write_map(string, mem)
+ atomic_write(io_db["path_worldstate"], string)
+ strong_write(io_db["file_out"], "WORLD_UPDATED\n")
+ io_db["worldstate_updateable"] = False
+
+