Compare commits

...

3 Commits

Author SHA1 Message Date
25cc638465 Fix bugs 2020-12-19 08:56:17 +00:00
cdd71d2610 Fix bugs and pause on zzz 2020-12-18 04:15:09 +00:00
d108d7c817 Add a job for grabbing supplies from barrels 2020-12-17 00:02:56 +00:00
5 changed files with 200 additions and 19 deletions

View File

@ -241,6 +241,7 @@ INDEXED = [
'chest',
'trapped_chest',
'emerald_block',
'barrel',
]
@ -274,6 +275,11 @@ for block_name in ['chest']:
for state in JSON_BLOCKS['minecraft:' + block_name]['states']:
CHEST_IDS.add(state['id'])
BARREL_IDS = set()
for block_name in ['barrel']:
for state in JSON_BLOCKS['minecraft:' + block_name]['states']:
BARREL_IDS.add(state['id'])
TRAPPED_CHEST_IDS = set()
for block_name in ['trapped_chest']:
for state in JSON_BLOCKS['minecraft:' + block_name]['states']:

6
bot.py
View File

@ -101,6 +101,10 @@ def tick(global_state):
target.x += 0.5
target.z += 0.5
if g.afk_timeout > 0:
target = None
g.afk_timeout -= utils.TICK
if target:
d = p - target
@ -205,7 +209,7 @@ def init(global_state):
g.job = jobs.JobStates(g)
g.chopped_tree = False
g.queue_afk = False
g.afk_timeout = 0
g.filling = False

13
game.py
View File

@ -288,7 +288,7 @@ class MCWorld:
def find_monsters(self, center, distance):
# finds monsters within distance
result = []
for eid, mob in self.g.mobs.items():
for eid, mob in copy(self.g.mobs).items():
if mob.type not in mobs.EVIL_IDS:
continue
pos = utils.pint((mob.x, mob.y, mob.z))
@ -578,12 +578,8 @@ class Game:
if command == 'zzz':
if not self.g.afk and self.g.dimension == 'overworld':
if self.g.path:
travel_time = int(len(self.g.path) * 0.4) + 2
reply = 'gimme ' + str(travel_time) + ' secs, moving'
self.g.queue_afk = True
else:
reply = '/afk'
self.g.afk_timeout = 5.0
if command == 'tree':
pos = utils.pint(self.g.pos)
@ -1101,8 +1097,3 @@ class Game:
if not self.g.path:
self.g.correction_count = 0
if self.g.queue_afk:
self.g.chat.send('/afk')
self.g.queue_afk = False

View File

@ -31,6 +31,15 @@ SHOVELS = [
'netherite_shovel',
]
AXES = [
'wooden_axe',
'stone_axe',
'golden_axe',
'iron_axe',
'diamond_axe',
'netherite_axe',
]
SAPLINGS = [
'oak_sapling',
'spruce_sapling',
@ -48,6 +57,10 @@ SHOVEL_IDS = set()
for item_name in SHOVELS:
SHOVEL_IDS.add(ITEMS['minecraft:'+item_name]['protocol_id'])
AXE_IDS = set()
for item_name in AXES:
AXE_IDS.add(ITEMS['minecraft:'+item_name]['protocol_id'])
SAPLING_IDS = set()
for item_name in SAPLINGS:
SAPLING_IDS.add(ITEMS['minecraft:'+item_name]['protocol_id'])
@ -69,5 +82,5 @@ POTATO_ID = get_id('potato')
WHEAT_SEEDS_ID = get_id('wheat_seeds')
BEETROOT_SEEDS_ID = get_id('beetroot_seeds')
NEEDED_ITEMS = BED_IDS | set([CHEST_ID])
NEEDED_ITEMS = BED_IDS | SHOVEL_IDS | AXE_IDS | set([CHEST_ID])
WANTED_ITEMS = SAPLING_IDS | set([NETHERWART_ID, CARROT_ID, POTATO_ID, WHEAT_SEEDS_ID, BEETROOT_SEEDS_ID])

177
jobs.py
View File

@ -364,6 +364,10 @@ class GatherWoodStates:
def init(self):
self.g.chopped_tree = False
self.state = self.select_axe
def select_axe(self):
self.g.game.select_item(items.AXE_IDS)
self.state = self.find_new_tree
def find_new_tree(self):
@ -647,7 +651,7 @@ class GrabSandStates:
w = self.g.world
p = utils.pint(self.g.pos)
sand = w.find_objects(items.SAND_ID)
sand = w.find_objects([items.SAND_ID])
if not sand:
print('No sand objects found, aborting')
@ -771,7 +775,7 @@ class SleepWithBedStates:
self.state = self.use_bed
def use_bed(self):
if self.g.time >= 12542:
if self.g.time > 12550:
print('Sleeping')
self.g.game.place_block(self.area, BlockFace.TOP)
if not self.silent:
@ -784,7 +788,8 @@ class SleepWithBedStates:
threats = w.find_threats(p, 30)
if threats:
print('Waking up due to threats')
print('Waking up due to threats:')
print(threats)
self.g.game.leave_bed()
self.state = self.break_bed
elif self.g.time < 100:
@ -798,7 +803,7 @@ class SleepWithBedStates:
def collect_bed(self):
if not self.g.breaking:
self.g.path = [utils.padd(self.area, utils.spiral(n)) for n in range(9)]
self.wait_time = 4
self.wait_time = 2
self.state = self.wait
def wait(self):
@ -1034,6 +1039,158 @@ class CacheItemsStates:
self.state()
class GrabSuppliesStates:
def idle(self):
return None
def init(self):
if self.supplies:
self.state = self.check_supplies
else:
print('Aborting getting supplies, none specified')
self.state = self.cleanup
def check_supplies(self):
for items, maximum in self.supplies.items():
print('Checking items:', items)
num_items = self.g.game.count_items(items)
print('Have:', num_items)
if num_items:
print('Skipping')
continue
else:
self.target_items = items
self.maximum_items = maximum
self.count = 0
self.state = self.find_barrels
return
print('Aborting, don\'t need any more supplies')
self.state = self.cleanup
def find_barrels(self):
print('Finding barrels...')
w = self.g.world
p = utils.pint(self.g.pos)
self.barrels = w.find_blocks_indexed(p, blocks.BARREL_IDS, 100)
print('Found:', self.barrels)
self.state = self.choose_barrel
def choose_barrel(self):
print('Choosing a 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.cleanup
return
barrel = self.barrels[0]
tmp = c.get_block_at(*barrel)
c.set_block_at(*barrel, blocks.AIR)
navpath = w.path_to_place(p, barrel)
c.set_block_at(*barrel, tmp)
print('navpath:', navpath)
if navpath:
self.g.path = navpath[:-1]
self.opening = self.g.path[-1]
self.area = barrel
self.state = self.going_to_barrel
return
else:
self.barrels.pop(0)
def going_to_barrel(self):
if utils.pint(self.g.pos) == self.opening:
self.g.look_at = self.area
self.state = self.open_barrel
def open_barrel(self):
print('Opening barrel')
self.g.game.open_container(self.area)
self.wait_time = 1
self.state = self.wait
def wait(self):
# wait for server to send us barrel contents
if self.wait_time > 0:
self.wait_time -= utils.TICK
else:
self.state = self.grab_items
def grab_items(self):
if self.g.item_lock: return
w = self.g.window
w_info = mcdata.WINDOWS[w.data.window_type]
w_container_slots = w_info.container
for slot_num in w_container_slots:
if slot_num not in w.contents:
continue
slot = w.contents[slot_num]
if not slot.present:
continue
if slot.item_id not in self.target_items:
continue
if self.maximum_items and self.count >= self.maximum_items:
break
self.count += 1
print('Moving', slot)
self.g.item_lock = True
self.g.game.click_window(slot_num, 0, 1, slot)
return
print('Nothing left to move')
self.state = self.close_barrel
def close_barrel(self):
print('Closing barrel')
self.g.game.close_window()
self.state = self.cleanup
def cleanup(self):
self.g.look_at = None
self.state = self.done
def done(self):
# never gets ran, placeholder
return None
def __init__(self, global_state):
self.g = global_state
self.state = self.idle
self.supplies = {}
self.target_supplies = []
self.barrels = []
self.target_items = None
self.maximum_items = 0
self.count = 0
self.area = None
self.opening = None
self.wait_time = 0
def run(self):
self.state()
class PlantTreeStates:
def idle(self):
return None
@ -1278,7 +1435,8 @@ class CheckThreatsStates:
threats = w.find_threats(p, 40)
if threats:
print('Found', len(threats), 'threats, fleeing')
print('Found', len(threats), 'threats, fleeing:')
print(threats)
self.state = self.find_safety
else:
print('Aborting, no threats')
@ -1558,6 +1716,7 @@ class JobStates:
self.gather_sand_states = GatherSandStates(self.g)
self.sleep_with_bed_states = SleepWithBedStates(self.g)
self.cache_items_states = CacheItemsStates(self.g)
self.grab_supplies_states = GrabSuppliesStates(self.g)
self.find_gapple_states = FindGappleStates(self.g)
self.plant_tree_states = PlantTreeStates(self.g)
self.clear_leaves_states = ClearLeavesStates(self.g)
@ -1590,6 +1749,7 @@ class JobStates:
def farm_sand(self):
machines = [
self.grab_supplies_states,
self.check_threats_states,
self.gather_sand_states,
self.grab_sand_states,
@ -1598,6 +1758,9 @@ class JobStates:
]
self.sleep_with_bed_states.silent = True
self.cache_items_states.silent = True
self.grab_supplies_states.supplies = {
tuple(items.SHOVEL_IDS): 9
}
return machines
def cache_items(self):
@ -1623,6 +1786,7 @@ class JobStates:
def farm_wood(self):
machines = [
self.grab_supplies_states,
self.gather_wood_states,
self.clear_leaves_states,
self.plant_tree_states,
@ -1632,6 +1796,9 @@ class JobStates:
]
self.sleep_with_bed_states.silent = True
self.cache_items_states.silent = True
self.grab_supplies_states.supplies = {
tuple(items.AXE_IDS): 9,
}
return machines
def farm_wart(self):