minecraft-bot/mosfet/job.py

265 lines
7.3 KiB
Python

import re
import time
import importlib
import random
from itertools import count
from minecraft.networking.types import BlockFace
from mosfet.protocol.managers import ChunkNotLoadedException
from mosfet import utils
from mosfet import path
from mosfet.info import blocks
from mosfet.info import items
from mosfet.info import mcdata
from mosfet.info import mobs
from mosfet.jobs import (
cache_items,
check_threats,
clear_leaves,
eat_food,
fill_blocks,
find_gapple,
gather_crop,
gather_sand,
gather_wart,
gather_wood,
grab_sand,
grab_sapling,
grab_supplies,
plant_tree,
sell_to_villager,
sleep_with_bed,
)
for module in [
cache_items,
check_threats,
clear_leaves,
eat_food,
fill_blocks,
find_gapple,
gather_crop,
gather_sand,
gather_wart,
gather_wood,
grab_sand,
grab_sapling,
grab_supplies,
plant_tree,
sell_to_villager,
sleep_with_bed,
]:
importlib.reload(module)
class JobStates:
def idle(self):
return []
def init_machines(self):
self.gather_wood_states = gather_wood.GatherWoodStates(self.g)
self.gather_sand_states = gather_sand.GatherSandStates(self.g)
self.sleep_with_bed_states = sleep_with_bed.SleepWithBedStates(self.g)
self.cache_items_states = cache_items.CacheItemsStates(self.g)
self.grab_supplies_states = grab_supplies.GrabSuppliesStates(self.g)
self.find_gapple_states = find_gapple.FindGappleStates(self.g)
self.plant_tree_states = plant_tree.PlantTreeStates(self.g)
self.clear_leaves_states = clear_leaves.ClearLeavesStates(self.g)
self.grab_sapling_states = grab_sapling.GrabSaplingStates(self.g)
self.grab_sand_states = grab_sand.GrabSandStates(self.g)
self.fill_blocks_states = fill_blocks.FillBlocksStates(self.g)
self.check_threats_states = check_threats.CheckThreatsStates(self.g)
self.gather_wart_states = gather_wart.GatherWartStates(self.g)
self.gather_crop_states = gather_crop.GatherCropStates(self.g)
self.eat_food_states = eat_food.EatFoodStates(self.g)
self.sell_to_villager_states = sell_to_villager.SellToVillagerStates(self.g)
def run_machines(self, machines):
for m in machines:
if m.state == m.idle:
continue
if m.state != m.done:
m.run()
return
# if we went through them all
for m in machines:
m.state = m.init
def gather_sand(self):
machines = [
self.gather_sand_states,
self.grab_sand_states,
self.cache_items_states,
self.sleep_with_bed_states,
self.eat_food_states,
]
return machines
def farm_sand(self):
machines = [
self.grab_supplies_states,
self.check_threats_states,
self.gather_sand_states,
self.grab_sand_states,
self.cache_items_states,
self.sleep_with_bed_states,
self.eat_food_states,
]
self.sleep_with_bed_states.silent = True
self.cache_items_states.silent = True
self.grab_supplies_states.supplies = {
tuple(items.SHOVEL_IDS): (1, 9),
}
items.set_needed(items.SHOVEL_IDS)
return machines
def cache_items(self):
machines = [
self.cache_items_states,
]
return machines
def find_gapple(self):
machines = [
self.find_gapple_states,
]
return machines
def gather_wood(self):
machines = [
self.gather_wood_states,
self.sleep_with_bed_states,
self.eat_food_states,
self.cache_items_states,
]
return machines
def farm_wood(self):
machines = [
self.grab_supplies_states,
self.gather_wood_states,
self.clear_leaves_states,
self.plant_tree_states,
self.grab_sapling_states,
self.sleep_with_bed_states,
self.eat_food_states,
self.cache_items_states,
]
self.sleep_with_bed_states.silent = True
self.cache_items_states.silent = True
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):
machines = [
self.gather_wart_states,
self.sleep_with_bed_states,
self.eat_food_states,
self.cache_items_states,
]
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):
machines = [
self.gather_crop_states,
self.sleep_with_bed_states,
self.eat_food_states,
self.cache_items_states,
]
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):
machines = [
self.grab_supplies_states,
self.fill_blocks_states,
self.sleep_with_bed_states,
self.eat_food_states,
]
self.sleep_with_bed_states.silent = True
f = self.g.filling
if f:
name = blocks.BLOCKS[f.block]
item = items.ITEMS['minecraft:'+name]['protocol_id']
self.grab_supplies_states.supplies = {
tuple([item]): (1, 0),
}
return machines
def loiter(self):
machines = [
self.check_threats_states,
self.sleep_with_bed_states,
self.eat_food_states,
]
self.sleep_with_bed_states.silent = True
return machines
def trade(self):
machines = [
self.grab_supplies_states,
self.sell_to_villager_states,
self.sleep_with_bed_states,
self.eat_food_states,
self.cache_items_states,
]
self.sleep_with_bed_states.silent = True
self.cache_items_states.silent = True
self.grab_supplies_states.supplies = {
tuple([items.PUMPKIN_ID]): (64, 3),
tuple([items.BERRIES_ID]): (64, 3),
tuple([items.IRON_INGOT_ID]): (64, 3),
tuple([items.WHEAT_ID]): (64, 3),
tuple([items.POTATO_ID]): (64, 3),
tuple([items.CARROT_ID]): (64, 3),
tuple([items.BEETROOT_ID]): (64, 3),
}
items.set_needed(set([
items.PUMPKIN_ID,
items.BERRIES_ID,
items.IRON_INGOT_ID,
items.WHEAT_ID,
items.POTATO_ID,
items.CARROT_ID,
items.BEETROOT_ID,
]))
return machines
def stop(self):
self.init_machines()
self.state = self.idle
def __init__(self, global_state):
self.g = global_state
self.init_machines()
self.state = self.idle
def tick(self):
self.run_machines(self.state())