From d9f7afe199056dbb6b44869442e60d36207465df Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Mon, 12 Aug 2019 18:05:35 +0200 Subject: [PATCH] Parse NPC data --- lihzahrd/fileutils/filereader.py | 3 +++ lihzahrd/npcs/__init__.py | 3 +++ lihzahrd/npcs/npc.py | 18 ++++++++++++++++++ lihzahrd/world.py | 19 +++++++++++++++++++ 4 files changed, 43 insertions(+) create mode 100644 lihzahrd/npcs/__init__.py create mode 100644 lihzahrd/npcs/npc.py diff --git a/lihzahrd/fileutils/filereader.py b/lihzahrd/fileutils/filereader.py index 37d3f9a..d15a37f 100644 --- a/lihzahrd/fileutils/filereader.py +++ b/lihzahrd/fileutils/filereader.py @@ -80,5 +80,8 @@ class FileReader: data += self.file.read(1) return data + def skip_until(self, address: int) -> None: + self.file.seek(address) + def __repr__(self): return f"" diff --git a/lihzahrd/npcs/__init__.py b/lihzahrd/npcs/__init__.py new file mode 100644 index 0000000..0747258 --- /dev/null +++ b/lihzahrd/npcs/__init__.py @@ -0,0 +1,3 @@ +from .npc import NPC + +__all__ = ["NPC"] diff --git a/lihzahrd/npcs/npc.py b/lihzahrd/npcs/npc.py new file mode 100644 index 0000000..a88e38c --- /dev/null +++ b/lihzahrd/npcs/npc.py @@ -0,0 +1,18 @@ +import typing +from ..fileutils import Coordinates + + +class NPC: + """A NPC somewhere in the world.""" + def __init__(self, + sprite_id: int, + name: str, + position: Coordinates, + home: typing.Optional[Coordinates] = None): + self.sprite_id: int = sprite_id + self.name: str = name + self.position: Coordinates = position + self.home: typing.Optional[Coordinates] = home + + def __repr__(self): + return f"" diff --git a/lihzahrd/world.py b/lihzahrd/world.py index 9e5ac41..4575319 100644 --- a/lihzahrd/world.py +++ b/lihzahrd/world.py @@ -6,6 +6,7 @@ from .header import * from .tiles import * from .chests import * from .signs import * +from .npcs import * from .timer import Timer @@ -527,6 +528,24 @@ class World: unknown_signs_data = f.read_until(pointers.npcs) + with Timer("NPCs", display=True): + npcs = [] + + while f.bool(): + npc_sprite_id = f.int4() + npc_name = f.string() + npc_position = Coordinates(f.single(), f.single()) + is_homeless = f.bool() + npc_home = Coordinates(f.int4(), f.int4()) + if is_homeless: + npc_home = None + + npc = NPC(sprite_id=npc_sprite_id, + name=npc_name, + position=npc_position, + home=npc_home) + npcs.append(npc) + world = World(version=version, savefile_type=savefile_type, revision=revision, is_favorite=is_favorite, name=name, generator=generator, uuid_=uuid_, id_=id_, bounds=bounds, size=world_size, is_expert=is_expert, created_on=created_on, styles=world_styles, backgrounds=backgrounds,