Port and tune 26.3 wood farming
This commit is contained in:
@@ -417,6 +417,7 @@ def start_automation(global_state, initialize=True):
|
||||
g.break_time = 0.0
|
||||
g.item_lock = False
|
||||
g.minimum_cache_slots = g.get('minimum_cache_slots', 27)
|
||||
g.maximum_supply_slots = g.get('maximum_supply_slots', 33)
|
||||
g.draining = False
|
||||
g.dumping = None
|
||||
g.dump_waiting = None
|
||||
@@ -461,6 +462,7 @@ def start_automation(global_state, initialize=True):
|
||||
g.setdefault('drain_error', None)
|
||||
g.setdefault('item_lock', False)
|
||||
g.setdefault('minimum_cache_slots', 27)
|
||||
g.setdefault('maximum_supply_slots', 33)
|
||||
g.setdefault('holding_slot', 0)
|
||||
g.setdefault('holding_hand', 0)
|
||||
g.setdefault('time', 0.0)
|
||||
|
||||
+3
-1
@@ -71,7 +71,9 @@ class Commands:
|
||||
}
|
||||
if ((command == 'gather' and data.lower().strip() != 'wood') or
|
||||
command in unsupported or
|
||||
(command == 'farm' and not data.lower().startswith('crop'))):
|
||||
(command == 'farm' and
|
||||
not data.lower().startswith('crop') and
|
||||
data.lower().strip() != 'wood')):
|
||||
reply = 'not migrated to Minecraft 26.3 yet'
|
||||
print('Reply:', reply)
|
||||
self._send_reply(reply, private, reply_target)
|
||||
|
||||
+29
-2
@@ -2,6 +2,7 @@ import re
|
||||
import time
|
||||
import random
|
||||
from itertools import count
|
||||
from math import floor, isfinite
|
||||
from munch import Munch
|
||||
|
||||
from minecraft.networking.packets import Packet, clientbound, serverbound
|
||||
@@ -721,15 +722,41 @@ class Game(LegacyGame):
|
||||
block_id = self.g.chunks.get_block_at(*location)
|
||||
if block_id is None or block_id == blocks.AIR:
|
||||
return False
|
||||
|
||||
holding = self.g.get('holding', 0)
|
||||
held_slot = (45 if self.g.get('holding_hand', 0) == 1 else
|
||||
36 + int(self.g.get('holding_slot', 0) or 0))
|
||||
held_stack = self.g.get('inv', {}).get(held_slot)
|
||||
enchantments = (getattr(held_stack, 'components', {}) or {}).get(
|
||||
'enchantments', {})
|
||||
in_water = False
|
||||
on_ground = True
|
||||
position = self.g.get('pos')
|
||||
chunks = self.g.get('chunks')
|
||||
if position is not None and chunks is not None:
|
||||
x, y, z = (floor(position.x), floor(position.y),
|
||||
floor(position.z))
|
||||
support = chunks.get_block_at(x, floor(position.y - 0.2), z)
|
||||
if support is not None:
|
||||
on_ground = support not in blocks.NON_SOLID_IDS
|
||||
feet = chunks.get_block_at(x, y, z)
|
||||
eyes = chunks.get_block_at(x, floor(position.y + 1.62), z)
|
||||
in_water = feet in blocks.WATER_IDS or eyes in blocks.WATER_IDS
|
||||
|
||||
duration = utils.break_time(
|
||||
block_id, holding, in_water=in_water, on_ground=on_ground,
|
||||
enchantments=enchantments, effects=self.g.get('effects', {}))
|
||||
if not isfinite(duration):
|
||||
return False
|
||||
|
||||
packet = PlayerActionPacket(
|
||||
action=0, location=location, face=1,
|
||||
sequence=self._next_sequence())
|
||||
self._send_action(packet)
|
||||
self.g.breaking = location
|
||||
self.breaking_state = block_id
|
||||
hardness_time = utils.break_time(block_id, self.g.get('holding', 0))
|
||||
now = time.monotonic()
|
||||
self.break_deadline = now + max(0.05, hardness_time)
|
||||
self.break_deadline = now + duration
|
||||
self.animate()
|
||||
self.next_swing_at = now + self.ARM_SWING_INTERVAL
|
||||
return True
|
||||
|
||||
@@ -55,6 +55,15 @@ def get_id(name):
|
||||
return ITEMS['minecraft:' + name]['protocol_id']
|
||||
|
||||
|
||||
def get_sapling_id(tree_type):
|
||||
"""Return the plantable sapling/propagule item for a tree species."""
|
||||
for suffix in ('_sapling', '_propagule'):
|
||||
item = ITEMS.get('minecraft:' + tree_type + suffix)
|
||||
if item is not None:
|
||||
return item['protocol_id']
|
||||
return None
|
||||
|
||||
|
||||
def get_name(idx):
|
||||
return ITEM_NAMES[idx]
|
||||
|
||||
|
||||
@@ -270,6 +270,11 @@ class JobStates(LegacyJobStates):
|
||||
self.g = global_state
|
||||
self.gather_wood_states = gather_wood.GatherWoodStates(global_state)
|
||||
self.gather_crop_states = gather_crop.GatherCropStates(global_state)
|
||||
self.grab_supplies_states = grab_supplies.GrabSuppliesStates(
|
||||
global_state)
|
||||
self.clear_leaves_states = clear_leaves.ClearLeavesStates(global_state)
|
||||
self.plant_tree_states = plant_tree.PlantTreeStates(global_state)
|
||||
self.grab_sapling_states = grab_sapling.GrabSaplingStates(global_state)
|
||||
self.cache_items_states = cache_items.CacheItemsStates(global_state)
|
||||
self.sleep_with_bed_states = sleep_with_bed.SleepWithBedStates(
|
||||
global_state)
|
||||
@@ -278,6 +283,25 @@ class JobStates(LegacyJobStates):
|
||||
global_state)
|
||||
self.state = self.idle
|
||||
|
||||
def farm_wood(self):
|
||||
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 [
|
||||
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,
|
||||
]
|
||||
|
||||
def farm_crop(self):
|
||||
self.cache_items_states.silent = True
|
||||
self.sleep_with_bed_states.silent = True
|
||||
@@ -314,6 +338,10 @@ class JobStates(LegacyJobStates):
|
||||
self.g.game.leave_bed()
|
||||
self.gather_wood_states.state = self.gather_wood_states.idle
|
||||
self.gather_crop_states.state = self.gather_crop_states.idle
|
||||
self.grab_supplies_states.state = self.grab_supplies_states.idle
|
||||
self.clear_leaves_states.state = self.clear_leaves_states.idle
|
||||
self.plant_tree_states.state = self.plant_tree_states.idle
|
||||
self.grab_sapling_states.state = self.grab_sapling_states.idle
|
||||
self.cache_items_states.state = self.cache_items_states.idle
|
||||
self.sleep_with_bed_states.state = self.sleep_with_bed_states.idle
|
||||
self.sleep_with_bed_states.force_new_bed = False
|
||||
|
||||
@@ -17,6 +17,8 @@ from mosfet.info import mcdata
|
||||
from mosfet.info import mobs
|
||||
|
||||
class ClearLeavesStates:
|
||||
LEAF_BREAK_INTERVAL = 0.6
|
||||
|
||||
def idle(self):
|
||||
return None
|
||||
|
||||
@@ -26,9 +28,13 @@ class ClearLeavesStates:
|
||||
self.state = self.cleanup
|
||||
return
|
||||
|
||||
sapling_type = self.g.chopped_tree + '_sapling'
|
||||
sapling_item = items.get_id(sapling_type)
|
||||
sapling_item = items.get_sapling_id(self.g.chopped_tree)
|
||||
if sapling_item is None:
|
||||
print('No sapling for chopped tree, skipping leaf cleanup')
|
||||
self.state = self.cleanup
|
||||
return
|
||||
num_saplings = self.g.game.count_items([sapling_item])
|
||||
sapling_type = items.get_name(sapling_item)
|
||||
print('Have', num_saplings, sapling_type, 'in inventory')
|
||||
|
||||
if num_saplings > 8:
|
||||
@@ -57,9 +63,13 @@ class ClearLeavesStates:
|
||||
def break_leaves(self):
|
||||
if not self.g.breaking:
|
||||
if self.leaves:
|
||||
now = time.monotonic()
|
||||
if now < self.next_break_at:
|
||||
return
|
||||
leaf = self.leaves.pop(0)
|
||||
self.g.look_at = leaf
|
||||
self.g.game.break_block(leaf)
|
||||
if self.g.game.break_block(leaf):
|
||||
self.next_break_at = now + self.LEAF_BREAK_INTERVAL
|
||||
print('Breaking leaf', leaf)
|
||||
else:
|
||||
self.wait_time = 1
|
||||
@@ -86,6 +96,7 @@ class ClearLeavesStates:
|
||||
|
||||
self.leaves = []
|
||||
self.wait_time = 0
|
||||
self.next_break_at = 0.0
|
||||
|
||||
def run(self):
|
||||
self.state()
|
||||
|
||||
@@ -32,15 +32,21 @@ class GatherWoodStates:
|
||||
|
||||
for tree in w.find_trees(p, 100):
|
||||
print('Found tree:', tree)
|
||||
if tree not in self.bad_trees:
|
||||
break
|
||||
if tree in self.bad_trees:
|
||||
continue
|
||||
tree_type = blocks.BLOCKS[w.block_at(*tree)].replace('_log', '')
|
||||
if items.get_sapling_id(tree_type) is None:
|
||||
print('No plantable sapling for', tree_type, '- skipping')
|
||||
self.bad_trees.append(tree)
|
||||
continue
|
||||
break
|
||||
else: # for
|
||||
print('No good trees left, aborting.')
|
||||
self.state = self.cleanup
|
||||
return
|
||||
|
||||
self.tree = tree
|
||||
self.type = blocks.BLOCKS[w.block_at(*tree)].replace('_log', '')
|
||||
self.type = tree_type
|
||||
print('Type:', self.type)
|
||||
|
||||
self.state = self.find_openings
|
||||
|
||||
@@ -13,7 +13,6 @@ 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
|
||||
|
||||
class GrabSuppliesStates:
|
||||
@@ -160,8 +159,8 @@ class GrabSuppliesStates:
|
||||
print('Didnt get a window, aborting')
|
||||
self.state = self.cleanup
|
||||
return
|
||||
if w.data.window_type != mcdata.SINGLE_CHEST:
|
||||
print('Got wrong window, aborting')
|
||||
if w.get('container_slots', 0) != 27:
|
||||
print('Got wrong container size, aborting')
|
||||
self.state = self.cleanup
|
||||
return
|
||||
used_slots = self.g.game.count_inventory_slots()
|
||||
@@ -173,10 +172,7 @@ class GrabSuppliesStates:
|
||||
self.state = self.cleanup
|
||||
return
|
||||
|
||||
w_info = mcdata.WINDOWS[w.data.window_type]
|
||||
w_container_slots = w_info.container
|
||||
|
||||
for slot_num in w_container_slots:
|
||||
for slot_num in range(w.container_slots):
|
||||
if slot_num not in w.contents:
|
||||
continue
|
||||
|
||||
|
||||
@@ -39,10 +39,14 @@ class PlantTreeStates:
|
||||
|
||||
def select_sapling(self):
|
||||
p = utils.pint(self.g.pos)
|
||||
sapling_type = self.g.chopped_tree + '_sapling'
|
||||
sapling_item = items.get_id(sapling_type)
|
||||
sapling_item = items.get_sapling_id(self.g.chopped_tree)
|
||||
if sapling_item is None:
|
||||
print('Aborting planting, no sapling for', self.g.chopped_tree)
|
||||
self.state = self.cleanup
|
||||
return
|
||||
|
||||
if self.g.game.select_item([sapling_item]):
|
||||
sapling_type = items.get_name(sapling_item)
|
||||
self.g.look_at = utils.padd(p, path.BLOCK_BELOW)
|
||||
self.state = self.wait_select
|
||||
self.wait_time = 1
|
||||
|
||||
@@ -5,7 +5,7 @@ from munch import Munch
|
||||
from minecraft.networking.packets import clientbound, serverbound
|
||||
from minecraft.networking.modern import (
|
||||
AddEntityPacket, ContainerOpenPacket, ContainerSetContentPacket,
|
||||
ContainerSetSlotPacket,
|
||||
ContainerSetSlotPacket, SetEntityDataPacket,
|
||||
PaletteSection,
|
||||
EntityPositionSyncPacket, MoveEntityPositionPacket,
|
||||
MoveEntityPositionRotationPacket, MoveEntityRotationPacket,
|
||||
@@ -347,6 +347,7 @@ class EntityManager:
|
||||
register = self.g.connection.register_packet_listener
|
||||
for packet_type, handler in (
|
||||
(AddEntityPacket, self.handle_add_entity),
|
||||
(SetEntityDataPacket, self.handle_entity_data),
|
||||
(RemoveEntitiesPacket, self.handle_remove_entities),
|
||||
(MoveEntityPositionPacket, self.handle_move),
|
||||
(MoveEntityPositionRotationPacket, self.handle_move),
|
||||
@@ -378,6 +379,14 @@ class EntityManager:
|
||||
type=packet.entity_type_id, yaw=packet.yaw,
|
||||
pitch=packet.pitch)
|
||||
|
||||
def handle_entity_data(self, packet):
|
||||
obj = self.g.objects.get(packet.entity_id)
|
||||
if obj is None or not packet.item_stacks:
|
||||
return
|
||||
stack = packet.item_stacks[-1]
|
||||
obj.item_id = stack.item_id if stack.present else None
|
||||
obj.item_count = stack.item_count if stack.present else 0
|
||||
|
||||
def _entity(self, entity_id):
|
||||
for collection in (self.g.players, self.g.mobs, self.g.objects):
|
||||
entity = collection.get(entity_id)
|
||||
|
||||
+34
-19
@@ -82,33 +82,48 @@ def diffrange(n):
|
||||
sign = 1 if n >= 0 else -1
|
||||
return range(0, n+sign, sign)
|
||||
|
||||
def break_time(block_id, held_item=0, in_water=False, on_ground=True, enchantments=[], effects={}):
|
||||
# from PrismarineJS/prismarine-block
|
||||
def break_time(block_id, held_item=0, in_water=False, on_ground=True,
|
||||
enchantments=None, effects=None):
|
||||
"""Return vanilla block-breaking duration rounded to whole game ticks."""
|
||||
held_item = str(held_item)
|
||||
block_data = blocks.get(block_id)
|
||||
hardness = block_data.get('hardness', 0)
|
||||
if hardness < 0:
|
||||
return float('inf')
|
||||
|
||||
can_harvest = 'harvestTools' not in block_data or str(held_item) in block_data['harvestTools']
|
||||
can_harvest = (
|
||||
'harvestTools' not in block_data or
|
||||
held_item in block_data['harvestTools'])
|
||||
material = block_data.get('material', 'n/a')
|
||||
tool_multipliers = mcdata.mcd.materials.get(material, [])
|
||||
is_best_tool = held_item in tool_multipliers
|
||||
time = block_data['hardness']
|
||||
tool_multipliers = mcdata.mcd.materials.get(material, {})
|
||||
speed = tool_multipliers.get(held_item, 1.0)
|
||||
|
||||
if can_harvest:
|
||||
time *= 1.5
|
||||
else:
|
||||
time *= 5.0
|
||||
enchantments = enchantments or {}
|
||||
if speed > 1:
|
||||
efficiency = int(enchantments.get(
|
||||
'efficiency', enchantments.get('minecraft:efficiency', 0)) or 0)
|
||||
if efficiency > 0:
|
||||
speed += efficiency * efficiency + 1
|
||||
|
||||
if is_best_tool:
|
||||
speed_multiplier = tool_multipliers[held_item]
|
||||
# TODO: calc efficiency, haste, mining fatigue
|
||||
else:
|
||||
speed_multiplier = 1.0
|
||||
effects = effects or {}
|
||||
haste = int(effects.get('haste', effects.get('minecraft:haste', 0)) or 0)
|
||||
if haste >= 0 and ('haste' in effects or 'minecraft:haste' in effects):
|
||||
speed *= 1 + 0.2 * (haste + 1)
|
||||
fatigue = int(effects.get(
|
||||
'mining_fatigue', effects.get('minecraft:mining_fatigue', -1)))
|
||||
if fatigue >= 0:
|
||||
speed *= (0.3, 0.09, 0.0027, 0.00081, 0.000243)[min(fatigue, 4)]
|
||||
|
||||
time /= speed_multiplier
|
||||
if in_water: time *= 5.0
|
||||
if not on_ground: time *= 5.0
|
||||
duration = hardness * (1.5 if can_harvest else 5.0) / speed
|
||||
if in_water:
|
||||
duration *= 5.0
|
||||
if not on_ground:
|
||||
duration *= 5.0
|
||||
|
||||
return time
|
||||
# Vanilla advances breaking progress in 1/20-second ticks; clients cannot
|
||||
# finish between ticks. Round up, while avoiding floating-point over-round.
|
||||
ticks = max(1, ceil(duration / TICK - 1e-9))
|
||||
return ticks * TICK
|
||||
|
||||
def search_2d(distance=0):
|
||||
def get_neighbors(x,y,z):
|
||||
|
||||
Reference in New Issue
Block a user