Compare commits

..

3 Commits

4 changed files with 174 additions and 49 deletions

3
bot.py
View File

@ -226,6 +226,9 @@ def init(global_state):
g.filling = False
g.minimum_cache_slots = 33
g.maximum_supply_slots = 27
def bot(global_state):
g = global_state

25
game.py
View File

@ -75,6 +75,7 @@ class MCWorld:
continue
if distance and utils.phyp(center, block) > distance:
continue
if block not in result:
result.append(block)
result.sort(key=lambda x: utils.phyp(center, x))
@ -847,7 +848,7 @@ class Game:
if authed:
if command == 'print':
data = data.replace('^', '.')
data = data.replace('`', '.')
reply = str(eval(data))
if command == 'exit':
@ -963,6 +964,28 @@ class Game:
count += item.item_count
return count
def count_inventory_slots(self):
# count how many inventory slots are filled
# excludes armour, crafting slots, off-hand
count = 0
for slot, item in self.g.inv.items():
if item.present and slot >= 9 and slot <= 45:
count += 1
return count
def count_window_slots(self):
# count how many window slots are filled
# excludes player inventory
w = self.g.window
w_info = mcdata.WINDOWS[w.data.window_type]
w_container_slots = w_info.container
count = 0
for slot, item in w.contents.items():
if item.present and slot in w_container_slots:
count += 1
return count
def get_window_slot(self, item_id):
# get the first slot that matches item of a window
window_items = list(self.g.window.contents.items())

View File

@ -105,6 +105,7 @@ NETHERWART_ID = get_id('nether_wart')
CARROT_ID = get_id('carrot')
POTATO_ID = get_id('potato')
WHEAT_ID = get_id('wheat')
WHEAT_SEEDS_ID = get_id('wheat_seeds')
BEETROOT_SEEDS_ID = get_id('beetroot_seeds')
PUMPKIN_ID = get_id('pumpkin')
@ -113,5 +114,16 @@ EMERALD_ID = get_id('emerald')
BERRIES_ID = get_id('sweet_berries')
IRON_INGOT_ID = get_id('iron_ingot')
NEEDED_ITEMS = BED_IDS | SHOVEL_IDS | AXE_IDS | FOOD_IDS | set([CHEST_ID, PUMPKIN_ID, BERRIES_ID, IRON_INGOT_ID])
WANTED_ITEMS = SAPLING_IDS | set([NETHERWART_ID, CARROT_ID, POTATO_ID, WHEAT_SEEDS_ID, BEETROOT_SEEDS_ID])
INIT_NEEDED_ITEMS = BED_IDS | FOOD_IDS | set([CHEST_ID])
NEEDED_ITEMS = INIT_NEEDED_ITEMS
INIT_WANTED_ITEMS = set()
WANTED_ITEMS = INIT_WANTED_ITEMS
def set_needed(items):
NEEDED_ITEMS = INIT_WANTED_ITEMS | items
def set_wanted(items):
WANTED_ITEMS = INIT_WANTED_ITEMS | items

177
jobs.py
View File

@ -739,7 +739,13 @@ class SleepWithBedStates:
w = self.g.world
p = utils.pint(self.g.pos)
self.beds = w.find_blocks_indexed(p, blocks.BED_IDS)
result = w.find_blocks_indexed(p, blocks.BED_IDS)
self.beds = []
for bed in result:
if bed not in self.bad_beds:
self.beds.append(bed)
print('Found:', self.beds)
self.state = self.choose_bed
@ -755,6 +761,7 @@ class SleepWithBedStates:
return
bed = self.beds[0]
print('Chose:', bed)
tmp = c.get_block_at(*bed)
c.set_block_at(*bed, blocks.AIR)
@ -766,16 +773,20 @@ class SleepWithBedStates:
if navpath:
self.g.path = navpath[:-1]
self.opening = self.g.path[-1]
self.g.look_at = self.opening
self.area = bed
self.state = self.going_to_bed
return
else:
self.beds.pop(0)
self.bad_beds.append(bed)
print('Cant get to bed, blacklisting')
self.state = self.select_bed
def going_to_bed(self):
if utils.pint(self.g.pos) == self.opening:
self.my_bed = False
self.g.look_at = self.area
self.g.look_at = utils.padd(self.area, path.BLOCK_BELOW)
self.state = self.use_bed
def select_bed(self):
@ -813,6 +824,7 @@ class SleepWithBedStates:
return
self.g.path = navpath
self.g.look_at = self.opening
self.state = self.going_to_area
def going_to_area(self):
@ -884,6 +896,7 @@ class SleepWithBedStates:
self.my_bed = False
self.beds = []
self.bad_beds = []
self.area = None
self.opening = None
self.bad_areas = []
@ -901,9 +914,9 @@ class CacheItemsStates:
self.skip_slots = []
self.skip_items = []
num_stacks = len([x for x in self.g.inv.values() if x.present])
num_stacks = self.g.game.count_inventory_slots()
print('Inventory amount:', num_stacks)
if num_stacks >= self.minimum:
if num_stacks >= self.g.minimum_cache_slots:
self.state = self.find_trapped_chests
else:
print('Aborting caching, not full')
@ -1026,6 +1039,16 @@ class CacheItemsStates:
w_info = mcdata.WINDOWS[w.data.window_type]
w_inventory_slots = w_info.inventory
w_container_slots = w_info.container
used_slots = self.g.game.count_window_slots()
print('used:', used_slots, 'total:', len(w_container_slots))
if used_slots >= len(w_container_slots):
print('Container is too full, aborting')
self.g.game.close_window()
self.g.look_at = None
self.state = self.cleanup
return
slot_list = []
@ -1038,7 +1061,7 @@ class CacheItemsStates:
if not slot.present:
continue
if slot.item_id in self.needed_items:
if slot.item_id in items.NEEDED_ITEMS:
continue
if slot_num in self.skip_slots:
@ -1049,7 +1072,7 @@ class CacheItemsStates:
slot_list.sort(key=lambda x: x[1].item_count, reverse=True)
for slot_num, slot in slot_list:
if slot.item_id in self.wanted_items and slot.item_id not in self.skip_items:
if slot.item_id in items.WANTED_ITEMS and slot.item_id not in self.skip_items:
print('skipping wanted item', slot)
self.skip_slots.append(slot_num)
self.skip_items.append(slot.item_id)
@ -1083,14 +1106,8 @@ class CacheItemsStates:
self.g = global_state
self.state = self.idle
self.minimum = 27
self.silent = False
# keep all needed items
self.needed_items = items.NEEDED_ITEMS
# keep one stack of wanted items
self.wanted_items = items.WANTED_ITEMS
self.skip_slots = []
self.skip_items = []
@ -1110,6 +1127,16 @@ class GrabSuppliesStates:
return None
def init(self):
print('Started grab supplies states')
self.checked_barrels = []
used_slots = self.g.game.count_inventory_slots()
print('used:', used_slots, 'total:', self.g.maximum_supply_slots)
if used_slots >= self.g.maximum_supply_slots:
print('Inventory is too full, aborting')
self.state = self.cleanup
return
if self.supplies:
self.state = self.check_supplies
else:
@ -1117,33 +1144,22 @@ class GrabSuppliesStates:
self.state = self.cleanup
def check_supplies(self):
# TODO: only visit each barrel once
# switch the supplies and barrels steps
# check if we need to grab anything
for items, limits in self.supplies.items():
minimum, maximum = limits
print('Checking items:', items)
num_items = self.g.game.count_items(items)
print('Have:', num_items)
if items in self.checked_supplies:
print('Already checked, skipping')
continue
if num_items >= minimum:
print('Have enough, skipping')
continue
self.target_items = items
self.checked_supplies.append(items)
self.maximum_items = maximum
self.count = 0
print('Need at least one item')
self.state = self.find_barrels
return
self.checked_supplies = []
print('Aborting, don\'t need any more supplies')
print('Aborting, dont need any supplies')
self.state = self.cleanup
def find_barrels(self):
@ -1157,16 +1173,26 @@ class GrabSuppliesStates:
def choose_barrel(self):
print('Choosing a barrel...')
for barrel in self.barrels:
if barrel in self.checked_barrels:
continue
if barrel in self.bad_barrels:
continue
self.barrel = barrel
self.state = self.path_to_barrel
return
print('No barrels')
self.state = self.cleanup
def path_to_barrel(self):
print('Finding path to barrel')
w = self.g.world
p = utils.pint(self.g.pos)
c = self.g.chunks
if not len(self.barrels):
print('No barrels')
self.state = self.no_more_barrels
return
barrel = self.barrels[0]
barrel = self.barrel
tmp = c.get_block_at(*barrel)
c.set_block_at(*barrel, blocks.AIR)
@ -1178,11 +1204,15 @@ class GrabSuppliesStates:
if navpath:
self.g.path = navpath[:-1]
self.opening = self.g.path[-1]
self.checked_barrels.append(barrel)
self.area = barrel
self.state = self.going_to_barrel
self.checked_supplies = []
return
else:
self.barrels.pop(0)
print('No path, blacklisting barrel')
self.bad_barrels.append(barrel)
self.state = self.choose_barrel
def going_to_barrel(self):
if utils.pint(self.g.pos) == self.opening:
@ -1196,11 +1226,38 @@ class GrabSuppliesStates:
self.state = self.wait
def wait(self):
# wait for server to send us barrel contents
# wait for server to send us inventory contents
if self.wait_time > 0:
self.wait_time -= utils.TICK
else:
self.state = self.choose_items
def choose_items(self):
print('Selecting next item')
for items, limits in self.supplies.items():
minimum_items, maximum_stacks = limits
print('Checking items:', items)
num_items = self.g.game.count_items(items)
print('Have:', num_items)
if num_items >= minimum_items:
print('Have enough, skipping')
continue
if items in self.checked_supplies:
print('Already checked, skipping')
continue
print('Need some')
self.checked_supplies.append(items)
self.target_items = items
self.maximum_stacks = maximum_stacks
self.count = 0
self.state = self.grab_items
return
print('Aborting, dont need any more supplies')
self.state = self.close_barrel
def grab_items(self):
if self.g.item_lock: return
@ -1214,6 +1271,14 @@ class GrabSuppliesStates:
print('Got wrong window, aborting')
self.state = self.cleanup
return
used_slots = self.g.game.count_inventory_slots()
print('used:', used_slots, 'total:', self.g.maximum_supply_slots)
if used_slots >= self.g.maximum_supply_slots:
print('Inventory is too full, aborting')
self.g.game.close_window()
self.g.look_at = None
self.state = self.cleanup
return
w_info = mcdata.WINDOWS[w.data.window_type]
w_container_slots = w_info.container
@ -1230,7 +1295,7 @@ class GrabSuppliesStates:
if slot.item_id not in self.target_items:
continue
if self.maximum_items and self.count >= self.maximum_items:
if self.maximum_stacks and self.count >= self.maximum_stacks:
break
self.count += 1
@ -1240,19 +1305,16 @@ class GrabSuppliesStates:
self.g.game.click_window(slot_num, 0, 1, slot)
return
print('Nothing left to move')
self.state = self.close_barrel
print('None left to move')
self.wait_time = 0.25
self.state = self.wait
def close_barrel(self):
print('Closing barrel')
self.g.game.close_window()
self.g.look_at = None
self.barrels.pop(0)
self.state = self.choose_barrel
def no_more_barrels(self):
self.state = self.check_supplies
def cleanup(self):
self.state = self.done
@ -1264,14 +1326,15 @@ class GrabSuppliesStates:
self.g = global_state
self.state = self.idle
self.checked_supplies = []
self.supplies = {}
self.barrels = []
self.checked_barrels = []
self.bad_barrels = []
self.barrel = None
self.checked_supplies = []
self.target_items = None
self.maximum_items = 0
self.maximum_stacks = 0
self.count = 0
self.area = None
self.opening = None
self.wait_time = 0
@ -2118,6 +2181,8 @@ class JobStates:
self.grab_supplies_states.supplies = {
tuple(items.SHOVEL_IDS): (1, 9),
}
items.set_needed(items.SHOVEL_IDS)
return machines
def cache_items(self):
@ -2158,6 +2223,9 @@ class JobStates:
self.grab_supplies_states.supplies = {
tuple(items.AXE_IDS): (1, 9),
}
items.set_needed(items.AXE_IDS)
items.set_wanted(items.SAPLING_IDS)
return machines
def farm_wart(self):
@ -2169,6 +2237,8 @@ class JobStates:
]
self.sleep_with_bed_states.silent = True
self.cache_items_states.silent = True
items.set_wanted(set([items.NETHERWART_ID]))
return machines
def farm_crop(self):
@ -2180,6 +2250,13 @@ class JobStates:
]
self.sleep_with_bed_states.silent = True
self.cache_items_states.silent = True
items.set_wanted(set([
items.CARROT_ID,
items.POTATO_ID,
items.WHEAT_SEEDS_ID,
items.BEETROOT_SEEDS_ID,
]))
return machines
def fill_blocks(self):
@ -2224,7 +2301,17 @@ class JobStates:
tuple([items.PUMPKIN_ID]): (64, 9),
tuple([items.BERRIES_ID]): (64, 9),
tuple([items.IRON_INGOT_ID]): (64, 9),
tuple([items.WHEAT_ID]): (64, 9),
tuple([items.POTATO_ID]): (64, 9),
}
items.set_needed(set([
items.PUMPKIN_ID,
items.BERRIES_ID,
items.IRON_INGOT_ID,
items.WHEAT_ID,
items.POTATO_ID,
]))
return machines
def stop(self):