From c66b1cf4ab62fdfff2d222a83bef51c1790a87f8 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Sun, 7 Jun 2020 17:59:19 +0200 Subject: [PATCH] ...I kinda forgot to do separate commits... --- lihzahrd/__init__.py | 6 ++-- lihzahrd/bestiary/bestiary.py | 15 +++++----- lihzahrd/chests/chest.py | 7 +++-- lihzahrd/enums/__init__.py | 13 ++++++++ lihzahrd/{tiles => enums}/blocktype.py | 0 .../{npcs/npctype.py => enums/entitytype.py} | 0 lihzahrd/{items => enums}/itemtype.py | 0 lihzahrd/{tiles => enums}/liquidtype.py | 0 lihzahrd/{tiles => enums}/walltype.py | 0 lihzahrd/fileutils/filereader.py | 14 ++++++--- lihzahrd/header/__init__.py | 4 +-- lihzahrd/header/events.py | 6 ++-- .../{lanternevent.py => lanternnight.py} | 6 ++-- lihzahrd/header/pets.py | 16 +++++----- lihzahrd/header/savedoretiers.py | 2 +- lihzahrd/items/__init__.py | 3 +- lihzahrd/items/itemstack.py | 2 +- lihzahrd/npcs/__init__.py | 3 +- lihzahrd/npcs/mob.py | 8 ++--- lihzahrd/npcs/npc.py | 30 +++++++++---------- lihzahrd/tileentities/targetdummy.py | 1 + lihzahrd/tiles/__init__.py | 5 +--- lihzahrd/tiles/block.py | 2 +- lihzahrd/tiles/liquid.py | 2 +- lihzahrd/tiles/tilematrix.py | 2 +- lihzahrd/tiles/wall.py | 2 +- lihzahrd/townmanager/room.py | 2 +- lihzahrd/world.py | 3 +- 28 files changed, 87 insertions(+), 67 deletions(-) create mode 100644 lihzahrd/enums/__init__.py rename lihzahrd/{tiles => enums}/blocktype.py (100%) rename lihzahrd/{npcs/npctype.py => enums/entitytype.py} (100%) rename lihzahrd/{items => enums}/itemtype.py (100%) rename lihzahrd/{tiles => enums}/liquidtype.py (100%) rename lihzahrd/{tiles => enums}/walltype.py (100%) rename lihzahrd/header/{lanternevent.py => lanternnight.py} (83%) mode change 100755 => 100644 diff --git a/lihzahrd/__init__.py b/lihzahrd/__init__.py index f57f539..bbb17f7 100644 --- a/lihzahrd/__init__.py +++ b/lihzahrd/__init__.py @@ -1,5 +1,5 @@ from .world import World -from . import bestiary, chests, fileutils, header, journeypowers, npcs, pressureplates, signs, tileentities, tiles, townmanager -__all__ = ["World", "bestiary", "chests", "fileutils", "header", "journeypowers", "npcs", "pressureplates", "signs", "tileentities", "tiles", - "townmanager"] +__all__ = [ + "World", +] diff --git a/lihzahrd/bestiary/bestiary.py b/lihzahrd/bestiary/bestiary.py index 266825d..0959908 100755 --- a/lihzahrd/bestiary/bestiary.py +++ b/lihzahrd/bestiary/bestiary.py @@ -1,17 +1,18 @@ import typing + class Bestiary: """A bestiary entry.""" - __slots__ = "npc_chats_count", "npc_chats_data", "npc_kill_count","npc_kill_data", "npc_sighting_count", "npc_sighting_data" + __slots__ = "npc_chats_count", "npc_chats_data", "npc_kill_count", "npc_kill_data", "npc_sighting_count", "npc_sighting_data" def __init__(self, - npc_chats_count: int, - npc_chats_data: typing.Dict[str, int], - npc_kill_count: int, - npc_kill_data: typing.List[str], - npc_sighting_count: int, - npc_sighting_data: typing.List[str],): + npc_chats_count: int, + npc_chats_data: typing.Dict[str, int], + npc_kill_count: int, + npc_kill_data: typing.List[str], + npc_sighting_count: int, + npc_sighting_data: typing.List[str], ): self.npc_chats_count: int = npc_chats_count self.npc_chats_data: typing.Dict[str, int] = npc_chats_data self.npc_kill_count: int = npc_kill_count diff --git a/lihzahrd/chests/chest.py b/lihzahrd/chests/chest.py index a8827bd..423c66d 100644 --- a/lihzahrd/chests/chest.py +++ b/lihzahrd/chests/chest.py @@ -1,14 +1,17 @@ import typing -from ..items.itemstack import ItemStack +from ..items import ItemStack from ..fileutils import Coordinates class Chest: """A chest with its contents.""" + def __init__(self, position: Coordinates, name: str, contents: typing.List[ItemStack]): self.position: Coordinates = position self.name: str = name self.contents: typing.List[ItemStack] = contents def __repr__(self): - return f"" + if self.name: + return f'' + return f"" diff --git a/lihzahrd/enums/__init__.py b/lihzahrd/enums/__init__.py new file mode 100644 index 0000000..6a1b971 --- /dev/null +++ b/lihzahrd/enums/__init__.py @@ -0,0 +1,13 @@ +from .blocktype import BlockType +from .entitytype import EntityType +from .itemtype import ItemType +from .liquidtype import LiquidType +from .walltype import WallType + +__all__ = [ + "BlockType", + "EntityType", + "ItemType", + "LiquidType", + "WallType", +] diff --git a/lihzahrd/tiles/blocktype.py b/lihzahrd/enums/blocktype.py similarity index 100% rename from lihzahrd/tiles/blocktype.py rename to lihzahrd/enums/blocktype.py diff --git a/lihzahrd/npcs/npctype.py b/lihzahrd/enums/entitytype.py similarity index 100% rename from lihzahrd/npcs/npctype.py rename to lihzahrd/enums/entitytype.py diff --git a/lihzahrd/items/itemtype.py b/lihzahrd/enums/itemtype.py similarity index 100% rename from lihzahrd/items/itemtype.py rename to lihzahrd/enums/itemtype.py diff --git a/lihzahrd/tiles/liquidtype.py b/lihzahrd/enums/liquidtype.py similarity index 100% rename from lihzahrd/tiles/liquidtype.py rename to lihzahrd/enums/liquidtype.py diff --git a/lihzahrd/tiles/walltype.py b/lihzahrd/enums/walltype.py similarity index 100% rename from lihzahrd/tiles/walltype.py rename to lihzahrd/enums/walltype.py diff --git a/lihzahrd/fileutils/filereader.py b/lihzahrd/fileutils/filereader.py index d907c80..95883df 100644 --- a/lihzahrd/fileutils/filereader.py +++ b/lihzahrd/fileutils/filereader.py @@ -2,12 +2,13 @@ import typing import struct import uuid import datetime +import functools from .rect import Rect class FileReader: - def __init__(self, file): - self.file = file + def __init__(self, file: typing.IO): + self.file: typing.IO = file def bool(self) -> bool: return struct.unpack("?", self.file.read(1))[0] @@ -42,8 +43,9 @@ class FileReader: def double(self) -> float: return struct.unpack("d", self.file.read(8))[0] - def bits(self) -> typing.Tuple[bool, bool, bool, bool, bool, bool, bool, bool]: - data = struct.unpack("B", self.file.read(1))[0] + @staticmethod + @functools.lru_cache(256) + def _bitify(data) -> typing.Tuple[bool, bool, bool, bool, bool, bool, bool, bool]: return (bool(data & 0b0000_0001), bool(data & 0b0000_0010), bool(data & 0b0000_0100), @@ -53,6 +55,10 @@ class FileReader: bool(data & 0b0100_0000), bool(data & 0b1000_0000)) + def bits(self) -> typing.Tuple[bool, bool, bool, bool, bool, bool, bool, bool]: + data = struct.unpack("B", self.file.read(1))[0] + return self._bitify(data) + def rect(self) -> Rect: left, right, top, bottom = struct.unpack("iiii", self.file.read(16)) return Rect(left, right, top, bottom) diff --git a/lihzahrd/header/__init__.py b/lihzahrd/header/__init__.py index fd8316a..8ae7578 100644 --- a/lihzahrd/header/__init__.py +++ b/lihzahrd/header/__init__.py @@ -9,7 +9,7 @@ from .fourpartsplit import FourPartSplit from .generatorinfo import GeneratorInfo from .invasion import Invasion from .invasiontype import InvasionType -from .lanternevent import LanternEvent +from .lanternnight import LanternNight from .lunarevents import LunarEvents from .moonphase import MoonPhase from .moonstyle import MoonStyle @@ -40,7 +40,7 @@ __all__ = [ "GeneratorInfo", "Invasion", "InvasionType", - "LanternEvent", + "LanternNight", "LunarEvents", "MoonPhase", "MoonStyle", diff --git a/lihzahrd/header/events.py b/lihzahrd/header/events.py index f610734..a1fd289 100644 --- a/lihzahrd/header/events.py +++ b/lihzahrd/header/events.py @@ -3,7 +3,7 @@ from .rain import Rain from .party import Party from .sandstorm import Sandstorm from .lunarevents import LunarEvents -from .lanternevent import LanternEvent +from .lanternnight import LanternNight class Events: """Information about the ongoing world events.""" @@ -16,7 +16,7 @@ class Events: party: Party, sandstorm: Sandstorm, lunar_events: LunarEvents, - lantern_night: LanternEvent): + lantern_night: LanternNight): self.blood_moon: bool = blood_moon """If the current moon is a Blood Moon.""" @@ -41,7 +41,7 @@ class Events: self.lunar_events: LunarEvents = lunar_events """Information about the currently ongoing Lunar Events.""" - self.lantern_night: LanternEvent = lantern_night + self.lantern_night: LanternNight = lantern_night """Information about the currently ongoing lantern night.""" def __repr__(self): diff --git a/lihzahrd/header/lanternevent.py b/lihzahrd/header/lanternnight.py old mode 100755 new mode 100644 similarity index 83% rename from lihzahrd/header/lanternevent.py rename to lihzahrd/header/lanternnight.py index c50c9c2..beea50f --- a/lihzahrd/header/lanternevent.py +++ b/lihzahrd/header/lanternnight.py @@ -1,7 +1,7 @@ import typing -class LanternEvent: +class LanternNight: """Lantern Night event related information.""" def __init__(self, nights_on_cooldown: int, @@ -17,8 +17,8 @@ class LanternEvent: self.manual: bool = manual """Was this night started by the player?""" - self.next_night_is_lantern_night:bool = next_night_is_lantern_night - """Is the next night a lantern night?""" + self.next_night_is_lantern_night: bool = next_night_is_lantern_night + """Was a boss just defeated, making the next night a Lantern Night?""" def __repr__(self): return f"WorldLanternNight(nights_on_cooldown={self.nights_on_cooldown}," \ diff --git a/lihzahrd/header/pets.py b/lihzahrd/header/pets.py index fe84956..162392f 100755 --- a/lihzahrd/header/pets.py +++ b/lihzahrd/header/pets.py @@ -1,19 +1,21 @@ import typing + class Pets: """Pet related information""" def __init__(self, - cat: bool, - dog: bool, - bunny: bool): + cat: bool, + dog: bool, + bunny: bool): + self.cat: bool = cat - """Has the cat been bought.""" + """Was the `Cat License `_ ever activated?""" self.dog: int = dog - """Has the cat been bought.""" + """Was the `Dog License `_ ever activated?""" self.bunny: float = bunny - """Has the bunny been bought.""" + """Was the `Bunny License `_ ever activated?""" def __repr__(self): - return f"WorldPets(cat={self.cat}, dog={self.dog}, bunny={self.bunny})" + return f"{self.__class__.__qualname__}(cat={self.cat}, dog={self.dog}, bunny={self.bunny})" diff --git a/lihzahrd/header/savedoretiers.py b/lihzahrd/header/savedoretiers.py index 3d58f13..00443cb 100755 --- a/lihzahrd/header/savedoretiers.py +++ b/lihzahrd/header/savedoretiers.py @@ -1,5 +1,5 @@ from typing import * -from ..tiles import BlockType +from ..enums import BlockType class SavedOreTiers: diff --git a/lihzahrd/items/__init__.py b/lihzahrd/items/__init__.py index ec54bb8..18fbbbc 100644 --- a/lihzahrd/items/__init__.py +++ b/lihzahrd/items/__init__.py @@ -1,4 +1,3 @@ from .itemstack import ItemStack -from .itemtype import ItemType -__all__ = ["ItemStack", "ItemType"] +__all__ = ["ItemStack"] diff --git a/lihzahrd/items/itemstack.py b/lihzahrd/items/itemstack.py index 7494857..231292a 100644 --- a/lihzahrd/items/itemstack.py +++ b/lihzahrd/items/itemstack.py @@ -1,4 +1,4 @@ -from .itemtype import ItemType +from ..enums import ItemType class ItemStack: diff --git a/lihzahrd/npcs/__init__.py b/lihzahrd/npcs/__init__.py index 15f4695..28fed6b 100644 --- a/lihzahrd/npcs/__init__.py +++ b/lihzahrd/npcs/__init__.py @@ -1,5 +1,4 @@ from .npc import NPC from .mob import Mob -from .npctype import EntityType -__all__ = ["NPC", "Mob", "EntityType"] +__all__ = ["NPC", "Mob"] diff --git a/lihzahrd/npcs/mob.py b/lihzahrd/npcs/mob.py index 4651d9f..b56b693 100644 --- a/lihzahrd/npcs/mob.py +++ b/lihzahrd/npcs/mob.py @@ -1,5 +1,5 @@ import typing -from .npctype import EntityType +from ..enums import EntityType from ..fileutils import Coordinates @@ -9,10 +9,10 @@ class Mob: __slots__ = "type", "position" def __init__(self, - type_: int, + type_: EntityType, position: Coordinates, ): - self.type: int = type_ - """The type of mob this object represents.""" + self.type: EntityType = type_ + """The type of entity this object represents.""" self.position: Coordinates = position """The position of the mob in the game world.""" diff --git a/lihzahrd/npcs/npc.py b/lihzahrd/npcs/npc.py index 1e88a8f..f03f97c 100644 --- a/lihzahrd/npcs/npc.py +++ b/lihzahrd/npcs/npc.py @@ -1,33 +1,31 @@ -import typing -from .npctype import EntityType +from typing import Optional +from ..enums import EntityType from ..fileutils import Coordinates +from .mob import Mob -class NPC: +class NPC(Mob): """A NPC somewhere in the world.""" __slots__ = "type", "name", "position", "home", "variation_index" def __init__(self, - type_: EntityType, - name: str, - position: Coordinates, - variation_index: int, - home: typing.Optional[Coordinates] = None,): - self.type: EntityType = type_ - """The NPC this object represents.""" + type_: EntityType, + position: Coordinates, + name: str, + variation_index: int, + home: Optional[Coordinates] = None): + + super().__init__(type_, position) self.name: str = name """The name of this NPC.""" - self.position: Coordinates = position - """The position of the mob in the game world.""" - - self.home: typing.Optional[Coordinates] = home - """The coordinates of the home of this NPC, or None if the NPC is homeless.""" + self.home: Optional[Coordinates] = home + """The coordinates of the home of this NPC, or ``None`` if the NPC is homeless.""" self.variation_index: int = variation_index - """Unsure, but seems to be an index to different possible NPC variations.""" + """(Unknown) Possibly the current `Zoologist `_ form?""" def __repr__(self): return f"" diff --git a/lihzahrd/tileentities/targetdummy.py b/lihzahrd/tileentities/targetdummy.py index 8d2a748..5db7b15 100644 --- a/lihzahrd/tileentities/targetdummy.py +++ b/lihzahrd/tileentities/targetdummy.py @@ -2,6 +2,7 @@ class TargetDummy: """Data pertaining to a Target Dummy (https://terraria.gamepedia.com/Target_Dummy)""" def __init__(self, npc: int): + # TODO: what's this? self.npc: int = npc def __repr__(self): diff --git a/lihzahrd/tiles/__init__.py b/lihzahrd/tiles/__init__.py index 2ca8f29..73dc13e 100644 --- a/lihzahrd/tiles/__init__.py +++ b/lihzahrd/tiles/__init__.py @@ -1,15 +1,12 @@ -from .liquidtype import LiquidType from .rleencoding import RLEEncoding from .shape import Shape from .wiring import Wiring -from .blocktype import BlockType from .frameimportantdata import FrameImportantData -from .walltype import WallType from .block import Block from .wall import Wall from .liquid import Liquid from .tile import Tile from .tilematrix import TileMatrix -__all__ = ["LiquidType", "RLEEncoding", "Shape", "Wiring", "BlockType", "FrameImportantData", "WallType", "Block", +__all__ = ["RLEEncoding", "Shape", "Wiring", "FrameImportantData", "Block", "Wall", "Liquid", "Tile", "TileMatrix"] diff --git a/lihzahrd/tiles/block.py b/lihzahrd/tiles/block.py index 2ec5ddb..72c24b5 100644 --- a/lihzahrd/tiles/block.py +++ b/lihzahrd/tiles/block.py @@ -1,5 +1,5 @@ import typing -from .blocktype import BlockType +from ..enums import BlockType from .frameimportantdata import FrameImportantData from .shape import Shape diff --git a/lihzahrd/tiles/liquid.py b/lihzahrd/tiles/liquid.py index a14f6b5..d75485d 100644 --- a/lihzahrd/tiles/liquid.py +++ b/lihzahrd/tiles/liquid.py @@ -1,4 +1,4 @@ -from .liquidtype import LiquidType +from ..enums import LiquidType class Liquid: diff --git a/lihzahrd/tiles/tilematrix.py b/lihzahrd/tiles/tilematrix.py index 4493949..bc57e8f 100644 --- a/lihzahrd/tiles/tilematrix.py +++ b/lihzahrd/tiles/tilematrix.py @@ -1,6 +1,6 @@ import typing from .tile import Tile -from ..fileutils import FileReader, Coordinates +from ..fileutils import Coordinates class TileMatrix: diff --git a/lihzahrd/tiles/wall.py b/lihzahrd/tiles/wall.py index b385161..7b6fd47 100644 --- a/lihzahrd/tiles/wall.py +++ b/lihzahrd/tiles/wall.py @@ -1,5 +1,5 @@ import typing -from .walltype import WallType +from ..enums import WallType class Wall: diff --git a/lihzahrd/townmanager/room.py b/lihzahrd/townmanager/room.py index d5185ba..c0f589a 100644 --- a/lihzahrd/townmanager/room.py +++ b/lihzahrd/townmanager/room.py @@ -1,5 +1,5 @@ from ..fileutils import Coordinates -from ..npcs import EntityType +from ..enums import EntityType class Room: diff --git a/lihzahrd/world.py b/lihzahrd/world.py index 3ba4e4b..43a9ce5 100644 --- a/lihzahrd/world.py +++ b/lihzahrd/world.py @@ -2,6 +2,7 @@ import uuid import math from typing import * from .fileutils import * +from .enums import * from .items import * from .header import * from .tiles import * @@ -610,7 +611,7 @@ class World: advanced_combat=combat_book_used ) - lantern_night = LanternEvent(f.int4(), f.bool(), f.bool(), f.bool()) + lantern_night = LanternNight(f.int4(), f.bool(), f.bool(), f.bool()) events = Events(blood_moon=blood_moon, solar_eclipse=eclipse,