def split_item(self, item: Item, split_amount: int = 1) -> None: """ De-stack / Split an item from a pile. """ if item.stack_count > split_amount: if split_amount >= 1: # Create new stack spliten_item = item.duplicate_self(quantity=split_amount) spliten_item.stackable = False # Remove item from stack self.remove_item(item, split_amount) # Drop item to the floor if there is no room in the inventory if len(self.items) >= self.capacity: spliten_item.place(self.parent.x, self.parent.y, self.gamemap) else: self.add_item(spliten_item) # Reset the stackable of the copied item. # NOTE: This line of code is important, since it lets items to be stacked again even after they are spliten. spliten_item.stackable = True # set gamemap spliten_item.gamemap = item.gamemap # return the copy return spliten_item else: raise Impossible(f"The minimum amount you can split is 1.") else: raise Impossible( f"The maximum amount you can split is {item.stack_count - 1}.")
def throw(self, item: Item, x: int, y: int, show_msg: bool = True) -> None: """ Remove an item from the inventory and place it on the given location. This process is more of a "teleporting item" rather than throwing, since the actual throwing/collision is handled in throwable component of an item. NOTE: You can only throw one item at a time. Args: show_msg: Whether to show a message to the log or not. """ # Duplicate and place the item spliten_item = item.duplicate_self(quantity=1) self.remove_item(item, remove_count=1) spliten_item.place(x, y, self.gamemap) if show_msg: if self.parent == self.engine.player: self.engine.message_log.add_message( f"You threw the {item.name}.") else: self.engine.message_log.add_message( f"{self.parent.name} threw the {item.name}.", fg=color.gray, target=self.parent) return True