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.filling = False
g.minimum_cache_slots = 33
g.maximum_supply_slots = 27
def bot(global_state): def bot(global_state):
g = global_state g = global_state

27
game.py
View File

@ -75,7 +75,8 @@ class MCWorld:
continue continue
if distance and utils.phyp(center, block) > distance: if distance and utils.phyp(center, block) > distance:
continue continue
result.append(block) if block not in result:
result.append(block)
result.sort(key=lambda x: utils.phyp(center, x)) result.sort(key=lambda x: utils.phyp(center, x))
return result return result
@ -847,7 +848,7 @@ class Game:
if authed: if authed:
if command == 'print': if command == 'print':
data = data.replace('^', '.') data = data.replace('`', '.')
reply = str(eval(data)) reply = str(eval(data))
if command == 'exit': if command == 'exit':
@ -963,6 +964,28 @@ class Game:
count += item.item_count count += item.item_count
return 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): def get_window_slot(self, item_id):
# get the first slot that matches item of a window # get the first slot that matches item of a window
window_items = list(self.g.window.contents.items()) 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') CARROT_ID = get_id('carrot')
POTATO_ID = get_id('potato') POTATO_ID = get_id('potato')
WHEAT_ID = get_id('wheat')
WHEAT_SEEDS_ID = get_id('wheat_seeds') WHEAT_SEEDS_ID = get_id('wheat_seeds')
BEETROOT_SEEDS_ID = get_id('beetroot_seeds') BEETROOT_SEEDS_ID = get_id('beetroot_seeds')
PUMPKIN_ID = get_id('pumpkin') PUMPKIN_ID = get_id('pumpkin')
@ -113,5 +114,16 @@ EMERALD_ID = get_id('emerald')
BERRIES_ID = get_id('sweet_berries') BERRIES_ID = get_id('sweet_berries')
IRON_INGOT_ID = get_id('iron_ingot') 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 w = self.g.world
p = utils.pint(self.g.pos) 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) print('Found:', self.beds)
self.state = self.choose_bed self.state = self.choose_bed
@ -755,6 +761,7 @@ class SleepWithBedStates:
return return
bed = self.beds[0] bed = self.beds[0]
print('Chose:', bed)
tmp = c.get_block_at(*bed) tmp = c.get_block_at(*bed)
c.set_block_at(*bed, blocks.AIR) c.set_block_at(*bed, blocks.AIR)
@ -766,16 +773,20 @@ class SleepWithBedStates:
if navpath: if navpath:
self.g.path = navpath[:-1] self.g.path = navpath[:-1]
self.opening = self.g.path[-1] self.opening = self.g.path[-1]
self.g.look_at = self.opening
self.area = bed self.area = bed
self.state = self.going_to_bed self.state = self.going_to_bed
return return
else: else:
self.beds.pop(0) 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): def going_to_bed(self):
if utils.pint(self.g.pos) == self.opening: if utils.pint(self.g.pos) == self.opening:
self.my_bed = False 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 self.state = self.use_bed
def select_bed(self): def select_bed(self):
@ -813,6 +824,7 @@ class SleepWithBedStates:
return return
self.g.path = navpath self.g.path = navpath
self.g.look_at = self.opening
self.state = self.going_to_area self.state = self.going_to_area
def going_to_area(self): def going_to_area(self):
@ -884,6 +896,7 @@ class SleepWithBedStates:
self.my_bed = False self.my_bed = False
self.beds = [] self.beds = []
self.bad_beds = []
self.area = None self.area = None
self.opening = None self.opening = None
self.bad_areas = [] self.bad_areas = []
@ -901,9 +914,9 @@ class CacheItemsStates:
self.skip_slots = [] self.skip_slots = []
self.skip_items = [] 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) print('Inventory amount:', num_stacks)
if num_stacks >= self.minimum: if num_stacks >= self.g.minimum_cache_slots:
self.state = self.find_trapped_chests self.state = self.find_trapped_chests
else: else:
print('Aborting caching, not full') print('Aborting caching, not full')
@ -1026,6 +1039,16 @@ class CacheItemsStates:
w_info = mcdata.WINDOWS[w.data.window_type] w_info = mcdata.WINDOWS[w.data.window_type]
w_inventory_slots = w_info.inventory 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 = [] slot_list = []
@ -1038,7 +1061,7 @@ class CacheItemsStates:
if not slot.present: if not slot.present:
continue continue
if slot.item_id in self.needed_items: if slot.item_id in items.NEEDED_ITEMS:
continue continue
if slot_num in self.skip_slots: 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) slot_list.sort(key=lambda x: x[1].item_count, reverse=True)
for slot_num, slot in slot_list: 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) print('skipping wanted item', slot)
self.skip_slots.append(slot_num) self.skip_slots.append(slot_num)
self.skip_items.append(slot.item_id) self.skip_items.append(slot.item_id)
@ -1083,14 +1106,8 @@ class CacheItemsStates:
self.g = global_state self.g = global_state
self.state = self.idle self.state = self.idle
self.minimum = 27
self.silent = False 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_slots = []
self.skip_items = [] self.skip_items = []
@ -1110,6 +1127,16 @@ class GrabSuppliesStates:
return None return None
def init(self): 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: if self.supplies:
self.state = self.check_supplies self.state = self.check_supplies
else: else:
@ -1117,33 +1144,22 @@ class GrabSuppliesStates:
self.state = self.cleanup self.state = self.cleanup
def check_supplies(self): def check_supplies(self):
# check if we need to grab anything
# TODO: only visit each barrel once
# switch the supplies and barrels steps
for items, limits in self.supplies.items(): for items, limits in self.supplies.items():
minimum, maximum = limits minimum, maximum = limits
print('Checking items:', items) print('Checking items:', items)
num_items = self.g.game.count_items(items) num_items = self.g.game.count_items(items)
print('Have:', num_items) print('Have:', num_items)
if items in self.checked_supplies:
print('Already checked, skipping')
continue
if num_items >= minimum: if num_items >= minimum:
print('Have enough, skipping') print('Have enough, skipping')
continue continue
self.target_items = items print('Need at least one item')
self.checked_supplies.append(items)
self.maximum_items = maximum
self.count = 0
self.state = self.find_barrels self.state = self.find_barrels
return return
self.checked_supplies = [] print('Aborting, dont need any supplies')
print('Aborting, don\'t need any more supplies')
self.state = self.cleanup self.state = self.cleanup
def find_barrels(self): def find_barrels(self):
@ -1157,16 +1173,26 @@ class GrabSuppliesStates:
def choose_barrel(self): def choose_barrel(self):
print('Choosing a barrel...') 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 w = self.g.world
p = utils.pint(self.g.pos) p = utils.pint(self.g.pos)
c = self.g.chunks c = self.g.chunks
if not len(self.barrels): barrel = self.barrel
print('No barrels')
self.state = self.no_more_barrels
return
barrel = self.barrels[0]
tmp = c.get_block_at(*barrel) tmp = c.get_block_at(*barrel)
c.set_block_at(*barrel, blocks.AIR) c.set_block_at(*barrel, blocks.AIR)
@ -1178,11 +1204,15 @@ class GrabSuppliesStates:
if navpath: if navpath:
self.g.path = navpath[:-1] self.g.path = navpath[:-1]
self.opening = self.g.path[-1] self.opening = self.g.path[-1]
self.checked_barrels.append(barrel)
self.area = barrel self.area = barrel
self.state = self.going_to_barrel self.state = self.going_to_barrel
self.checked_supplies = []
return return
else: 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): def going_to_barrel(self):
if utils.pint(self.g.pos) == self.opening: if utils.pint(self.g.pos) == self.opening:
@ -1196,11 +1226,38 @@ class GrabSuppliesStates:
self.state = self.wait self.state = self.wait
def wait(self): def wait(self):
# wait for server to send us barrel contents # wait for server to send us inventory contents
if self.wait_time > 0: if self.wait_time > 0:
self.wait_time -= utils.TICK self.wait_time -= utils.TICK
else: 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 self.state = self.grab_items
return
print('Aborting, dont need any more supplies')
self.state = self.close_barrel
def grab_items(self): def grab_items(self):
if self.g.item_lock: return if self.g.item_lock: return
@ -1214,6 +1271,14 @@ class GrabSuppliesStates:
print('Got wrong window, aborting') print('Got wrong window, aborting')
self.state = self.cleanup self.state = self.cleanup
return 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_info = mcdata.WINDOWS[w.data.window_type]
w_container_slots = w_info.container w_container_slots = w_info.container
@ -1230,7 +1295,7 @@ class GrabSuppliesStates:
if slot.item_id not in self.target_items: if slot.item_id not in self.target_items:
continue continue
if self.maximum_items and self.count >= self.maximum_items: if self.maximum_stacks and self.count >= self.maximum_stacks:
break break
self.count += 1 self.count += 1
@ -1240,19 +1305,16 @@ class GrabSuppliesStates:
self.g.game.click_window(slot_num, 0, 1, slot) self.g.game.click_window(slot_num, 0, 1, slot)
return return
print('Nothing left to move') print('None left to move')
self.state = self.close_barrel self.wait_time = 0.25
self.state = self.wait
def close_barrel(self): def close_barrel(self):
print('Closing barrel') print('Closing barrel')
self.g.game.close_window() self.g.game.close_window()
self.g.look_at = None self.g.look_at = None
self.barrels.pop(0)
self.state = self.choose_barrel self.state = self.choose_barrel
def no_more_barrels(self):
self.state = self.check_supplies
def cleanup(self): def cleanup(self):
self.state = self.done self.state = self.done
@ -1264,14 +1326,15 @@ class GrabSuppliesStates:
self.g = global_state self.g = global_state
self.state = self.idle self.state = self.idle
self.checked_supplies = []
self.supplies = {} self.supplies = {}
self.barrels = [] self.barrels = []
self.checked_barrels = []
self.bad_barrels = []
self.barrel = None
self.checked_supplies = []
self.target_items = None self.target_items = None
self.maximum_items = 0 self.maximum_stacks = 0
self.count = 0 self.count = 0
self.area = None self.area = None
self.opening = None self.opening = None
self.wait_time = 0 self.wait_time = 0
@ -2118,6 +2181,8 @@ class JobStates:
self.grab_supplies_states.supplies = { self.grab_supplies_states.supplies = {
tuple(items.SHOVEL_IDS): (1, 9), tuple(items.SHOVEL_IDS): (1, 9),
} }
items.set_needed(items.SHOVEL_IDS)
return machines return machines
def cache_items(self): def cache_items(self):
@ -2158,6 +2223,9 @@ class JobStates:
self.grab_supplies_states.supplies = { self.grab_supplies_states.supplies = {
tuple(items.AXE_IDS): (1, 9), tuple(items.AXE_IDS): (1, 9),
} }
items.set_needed(items.AXE_IDS)
items.set_wanted(items.SAPLING_IDS)
return machines return machines
def farm_wart(self): def farm_wart(self):
@ -2169,6 +2237,8 @@ class JobStates:
] ]
self.sleep_with_bed_states.silent = True self.sleep_with_bed_states.silent = True
self.cache_items_states.silent = True self.cache_items_states.silent = True
items.set_wanted(set([items.NETHERWART_ID]))
return machines return machines
def farm_crop(self): def farm_crop(self):
@ -2180,6 +2250,13 @@ class JobStates:
] ]
self.sleep_with_bed_states.silent = True self.sleep_with_bed_states.silent = True
self.cache_items_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 return machines
def fill_blocks(self): def fill_blocks(self):
@ -2224,7 +2301,17 @@ class JobStates:
tuple([items.PUMPKIN_ID]): (64, 9), tuple([items.PUMPKIN_ID]): (64, 9),
tuple([items.BERRIES_ID]): (64, 9), tuple([items.BERRIES_ID]): (64, 9),
tuple([items.IRON_INGOT_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 return machines
def stop(self): def stop(self):