206 lines
7.5 KiB
Python
206 lines
7.5 KiB
Python
import unittest
|
|
from munch import Munch
|
|
|
|
from mosfet.info import items
|
|
from mosfet.jobs.grab_supplies import GrabSuppliesStates
|
|
from mosfet.protocol.types import Slot
|
|
|
|
|
|
class InventorySupplyRecoveryTests(unittest.TestCase):
|
|
def make_inventory(self):
|
|
return {
|
|
36: Slot(True, next(iter(items.BED_IDS)), 1),
|
|
37: Slot(True, items.CHEST_ID, 32),
|
|
38: Slot(True, next(iter(items.FOOD_IDS)), 64),
|
|
39: Slot(True, next(iter(items.AXE_IDS)), 1),
|
|
40: Slot(True, items.SAND_ID, 16),
|
|
41: Slot(True, items.get_id('stone'), 64),
|
|
42: Slot(True, items.get_id('dirt'), 4),
|
|
43: Slot(True, items.get_id('stick'), 1),
|
|
44: Slot(True, items.get_id('cobblestone'), 2),
|
|
}
|
|
|
|
def with_needed_axes(self, callback):
|
|
previous_needed = items.NEEDED_ITEMS
|
|
try:
|
|
items.set_needed(items.AXE_IDS)
|
|
callback()
|
|
finally:
|
|
items.NEEDED_ITEMS = previous_needed
|
|
|
|
def test_preserves_essential_stacks_and_selects_largest_junk_stack(self):
|
|
def run():
|
|
g = Munch(inv=self.make_inventory(), drop_waiting=None)
|
|
state = GrabSuppliesStates(g)
|
|
self.assertEqual(state._protected_inventory_slots(),
|
|
{36, 37, 38, 39})
|
|
self.assertEqual(state._least_important_inventory_slot(
|
|
hotbar_only=True), 41)
|
|
self.assertEqual(state._least_important_inventory_slot(), 41)
|
|
|
|
self.with_needed_axes(run)
|
|
|
|
def test_full_container_transfer_swaps_instead_of_aborting(self):
|
|
def run():
|
|
axe_id = next(iter(items.AXE_IDS))
|
|
swaps = []
|
|
|
|
class FakeGame:
|
|
def count_inventory_slots(self):
|
|
return 33
|
|
|
|
def swap_container_slot_with_hotbar(self, container_slot,
|
|
inventory_slot):
|
|
swaps.append((container_slot, inventory_slot))
|
|
return True
|
|
|
|
g = Munch(
|
|
inv=self.make_inventory(), item_lock=False,
|
|
window=Munch(container_slots=27, contents={
|
|
0: Slot(True, axe_id, 1),
|
|
}),
|
|
maximum_supply_slots=33, game=FakeGame(),
|
|
)
|
|
state = GrabSuppliesStates(g)
|
|
state.target_items = {axe_id}
|
|
state.maximum_stacks = 1
|
|
state.grab_items()
|
|
|
|
self.assertEqual(swaps, [(0, 41)])
|
|
self.assertTrue(g.item_lock)
|
|
self.assertEqual(state.count, 1)
|
|
|
|
self.with_needed_axes(run)
|
|
|
|
def test_full_ground_pickup_frees_room_and_waits_for_target(self):
|
|
def run():
|
|
class FakeGame:
|
|
def __init__(self):
|
|
self.inventory_slots = 33
|
|
self.target_count = 0
|
|
self.dropped = []
|
|
|
|
def count_inventory_slots(self):
|
|
return self.inventory_slots
|
|
|
|
def count_items(self, _item_ids):
|
|
return self.target_count
|
|
|
|
def drop_inventory_slot(self, slot, whole_stack=True):
|
|
self.dropped.append((slot, whole_stack))
|
|
return True
|
|
|
|
target_id = items.NETHERWART_ID
|
|
obj = Munch(entity_id=99, item_id=target_id,
|
|
x=0.5, y=64.0, z=0.5)
|
|
game = FakeGame()
|
|
g = Munch(
|
|
inv=self.make_inventory(), drop_waiting=None,
|
|
objects={99: obj}, path=[], pos=(0.5, 64, 0.5),
|
|
maximum_supply_slots=33, game=game,
|
|
)
|
|
state = GrabSuppliesStates(g)
|
|
state.checked_ground_items = set()
|
|
state.checked_ground_groups = set()
|
|
state.ground_stack_counts = {}
|
|
state.target_items = (target_id,)
|
|
state.target_minimum = 1
|
|
state.target_maximum_stacks = 1
|
|
state.ground_entity_id = 99
|
|
state.ground_position = (0, 64, 0)
|
|
state.ground_required_count = 1
|
|
|
|
state.going_to_ground_item()
|
|
self.assertEqual(game.dropped, [(41, True)])
|
|
self.assertIs(state.state.__func__,
|
|
state.wait_for_inventory_space.__func__)
|
|
|
|
g.drop_waiting = None
|
|
game.inventory_slots = 32
|
|
state.wait_for_inventory_space()
|
|
self.assertIs(state.state.__func__,
|
|
state.wait_for_ground_item.__func__)
|
|
|
|
game.target_count = 1
|
|
state.wait_for_ground_item()
|
|
self.assertEqual(state.ground_stack_counts[(target_id,)], 1)
|
|
self.assertIs(state.state.__func__, state.check_supplies.__func__)
|
|
|
|
self.with_needed_axes(run)
|
|
|
|
def test_container_swap_can_stage_a_main_inventory_stack_through_hotbar(self):
|
|
previous_needed = items.NEEDED_ITEMS
|
|
previous_wanted = items.WANTED_ITEMS
|
|
try:
|
|
items.set_needed(items.AXE_IDS | items.SHOVEL_IDS)
|
|
saplings = list(items.SAPLING_IDS)[:4]
|
|
items.set_wanted(set(saplings))
|
|
axes = next(iter(items.AXE_IDS))
|
|
inventory = {
|
|
36: Slot(True, next(iter(items.BED_IDS)), 1),
|
|
37: Slot(True, items.CHEST_ID, 1),
|
|
38: Slot(True, next(iter(items.FOOD_IDS)), 64),
|
|
39: Slot(True, axes, 1),
|
|
40: Slot(True, next(iter(items.SHOVEL_IDS)), 1),
|
|
41: Slot(True, saplings[0], 1),
|
|
42: Slot(True, saplings[1], 1),
|
|
43: Slot(True, saplings[2], 1),
|
|
44: Slot(True, saplings[3], 1),
|
|
9: Slot(True, items.get_id('stone'), 64),
|
|
}
|
|
swaps = []
|
|
|
|
class FakeGame:
|
|
def count_inventory_slots(self):
|
|
return 33
|
|
|
|
def swap_container_slot_with_hotbar(self, slot, hotbar):
|
|
swaps.append((slot, hotbar))
|
|
return True
|
|
|
|
target_id = items.PUMPKIN_ID
|
|
g = Munch(
|
|
inv=inventory, item_lock=False,
|
|
window=Munch(container_slots=27, contents={
|
|
0: Slot(True, target_id, 1),
|
|
}),
|
|
maximum_supply_slots=33, game=FakeGame(),
|
|
)
|
|
state = GrabSuppliesStates(g)
|
|
state.target_items = {target_id}
|
|
state.maximum_stacks = 1
|
|
state.grab_items()
|
|
|
|
self.assertEqual(swaps, [(27, 36)])
|
|
self.assertTrue(g.item_lock)
|
|
self.assertEqual(state.count, 0)
|
|
finally:
|
|
items.NEEDED_ITEMS = previous_needed
|
|
items.WANTED_ITEMS = previous_wanted
|
|
|
|
def test_full_ground_pickup_drops_largest_nonessential_stack(self):
|
|
def run():
|
|
dropped = []
|
|
|
|
class FakeGame:
|
|
def drop_inventory_slot(self, slot, whole_stack=True):
|
|
dropped.append((slot, whole_stack))
|
|
return True
|
|
|
|
g = Munch(
|
|
inv=self.make_inventory(), drop_waiting=None, path=[],
|
|
game=FakeGame(),
|
|
)
|
|
state = GrabSuppliesStates(g)
|
|
state.target_items = {items.NETHERWART_ID}
|
|
self.assertTrue(state._drop_stack_for_ground_pickup())
|
|
self.assertEqual(dropped, [(41, True)])
|
|
self.assertIs(state.state.__func__,
|
|
state.wait_for_inventory_space.__func__)
|
|
|
|
self.with_needed_axes(run)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
unittest.main()
|