Skip to content

Commit

Permalink
Core: optimize away Item.world (#840)
Browse files Browse the repository at this point in the history
* Core: optimize away Item.world

* Update test/general/TestFill.py

* Test: undo unnecessary changes

* lttp: remove two more Item.world writes

Co-authored-by: black-sliver <[email protected]>
  • Loading branch information
Berserker66 and black-sliver authored Aug 5, 2022
1 parent d15c30f commit 21f7c6c
Show file tree
Hide file tree
Showing 6 changed files with 9 additions and 18 deletions.
18 changes: 8 additions & 10 deletions BaseClasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -384,15 +384,13 @@ def create_item(self, item_name: str, player: int) -> Item:
return self.worlds[player].create_item(item_name)

def push_precollected(self, item: Item):
item.world = self
self.precollected_items[item.player].append(item)
self.state.collect(item, True)

def push_item(self, location: Location, item: Item, collect: bool = True):
assert location.can_fill(self.state, item, False), f"Cannot place {item} into {location}."
location.item = item
item.location = location
item.world = self # try to not have this here anymore and create it with item?
if collect:
self.state.collect(item, location.event, location)

Expand Down Expand Up @@ -1102,7 +1100,6 @@ def place_locked_item(self, item: Item):
self.item = item
item.location = self
self.event = item.advancement
self.item.world = self.parent_region.world
self.locked = True

def __repr__(self):
Expand Down Expand Up @@ -1148,7 +1145,6 @@ def as_flag(self) -> int:

class Item:
location: Optional[Location] = None
world: Optional[MultiWorld] = None
code: Optional[int] = None # an item with ID None is called an Event, and does not get written to multidata
name: str
game: str = "Generic"
Expand All @@ -1175,11 +1171,11 @@ def __init__(self, name: str, classification: ItemClassification, code: Optional
self.code = code

@property
def hint_text(self):
def hint_text(self) -> str:
return getattr(self, "_hint_text", self.name.replace("_", " ").replace("-", " "))

@property
def pedestal_hint_text(self):
def pedestal_hint_text(self) -> str:
return getattr(self, "_pedestal_hint_text", self.name.replace("_", " ").replace("-", " "))

@property
Expand All @@ -1205,19 +1201,21 @@ def flags(self) -> int:
def __eq__(self, other):
return self.name == other.name and self.player == other.player

def __lt__(self, other: Item):
def __lt__(self, other: Item) -> bool:
if other.player != self.player:
return other.player < self.player
return self.name < other.name

def __hash__(self):
return hash((self.name, self.player))

def __repr__(self):
def __repr__(self) -> str:
return self.__str__()

def __str__(self):
return self.world.get_name_string_for_object(self) if self.world else f'{self.name} (Player {self.player})'
def __str__(self) -> str:
if self.location:
return self.location.parent_region.world.get_name_string_for_object(self)
return f"{self.name} (Player {self.player})"


class Spoiler():
Expand Down
3 changes: 0 additions & 3 deletions Main.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,9 +217,6 @@ def find_common_pool(players: Set[int], shared_pool: Set[str]):

logger.info("Running Item Plando")

for item in world.itempool:
item.world = world

distribute_planned(world)

logger.info('Running Pre Main Fill.')
Expand Down
3 changes: 1 addition & 2 deletions test/general/TestFill.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ def generate_region(self, parent: Region, size: int, access_rule: CollectionRule
region_name = "player" + str(self.id) + region_tag
region = Region("player" + str(self.id) + region_tag, RegionType.Generic,
"Region Hint", self.id, self.world)
self.locations += generate_locations(size,
self.id, None, region, region_tag)
self.locations += generate_locations(size, self.id, None, region, region_tag)

entrance = Entrance(self.id, region_name + "_entrance", parent)
parent.exits.append(entrance)
Expand Down
1 change: 0 additions & 1 deletion worlds/alttp/Dungeons.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ def make_dungeon(name, default_boss, dungeon_regions, big_key, small_keys, dunge
dungeon_items, player)
for item in dungeon.all_items:
item.dungeon = dungeon
item.world = world
dungeon.boss = BossFactory(default_boss, player) if default_boss else None
for region in dungeon.regions:
world.get_region(region, player).dungeon = dungeon
Expand Down
1 change: 0 additions & 1 deletion worlds/alttp/Rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -935,7 +935,6 @@ def tr_big_key_chest_keys_needed(state):
else:
# A key is required in the Big Key Chest to prevent a possible softlock. Place an extra key to ensure 100% locations still works
item = ItemFactory('Small Key (Turtle Rock)', player)
item.world = world
location = world.get_location('Turtle Rock - Big Key Chest', player)
location.place_locked_item(item)
location.event = True
Expand Down
1 change: 0 additions & 1 deletion worlds/alttp/Shops.py
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,6 @@ def create_shops(world, player: int):
else:
loc.item = ItemFactory(GetBeemizerItem(world, player, 'Nothing'), player)
loc.shop_slot_disabled = True
loc.item.world = world
shop.region.locations.append(loc)
world.clear_location_cache()

Expand Down

0 comments on commit 21f7c6c

Please sign in to comment.