Calculate the correct block breaking time
This commit is contained in:
		
							
								
								
									
										12
									
								
								bot.py
									
									
									
									
									
								
							
							
						
						
									
										12
									
								
								bot.py
									
									
									
									
									
								
							@@ -33,7 +33,6 @@ importlib.reload(utils)
 | 
			
		||||
import path
 | 
			
		||||
importlib.reload(path)
 | 
			
		||||
 | 
			
		||||
TICK = 0.05
 | 
			
		||||
last_tick = time.time()
 | 
			
		||||
 | 
			
		||||
PITCH_ANGLE_DIR = LVector3f(x=0, y=1, z=0)
 | 
			
		||||
@@ -87,8 +86,8 @@ def tick(global_state):
 | 
			
		||||
            l.path.pop(0)
 | 
			
		||||
 | 
			
		||||
    if l.y_v or l.y_a:
 | 
			
		||||
        p.y += l.y_v * TICK
 | 
			
		||||
        l.y_v += l.y_a * TICK
 | 
			
		||||
        p.y += l.y_v * utils.TICK
 | 
			
		||||
        l.y_v += l.y_a * utils.TICK
 | 
			
		||||
 | 
			
		||||
    block_below = g.chunks.get_block_at(floor(p.x), ceil(p.y-1), floor(p.z))
 | 
			
		||||
    in_air = block_below in blocks.NON_SOLID_IDS
 | 
			
		||||
@@ -151,6 +150,7 @@ def init(global_state):
 | 
			
		||||
    l.pitch = 0
 | 
			
		||||
 | 
			
		||||
    l.breaking = None
 | 
			
		||||
    l.break_time = 0
 | 
			
		||||
 | 
			
		||||
    #l.jobstate = JobStates(connection, player_info)
 | 
			
		||||
    #l.jobstate.run()
 | 
			
		||||
@@ -184,12 +184,12 @@ def bot(global_state):
 | 
			
		||||
 | 
			
		||||
    try:
 | 
			
		||||
        while not g.pos:
 | 
			
		||||
            time.sleep(TICK)
 | 
			
		||||
            time.sleep(utils.TICK)
 | 
			
		||||
        print('Player loaded.')
 | 
			
		||||
 | 
			
		||||
        x, y, z = utils.pint(g.pos)
 | 
			
		||||
        while (floor(x/16), floor(y/16), floor(z/16)) not in g.chunks.chunks:
 | 
			
		||||
            time.sleep(TICK)
 | 
			
		||||
            time.sleep(utils.TICK)
 | 
			
		||||
        print('Chunks loaded.')
 | 
			
		||||
 | 
			
		||||
        init(g)
 | 
			
		||||
@@ -198,7 +198,7 @@ def bot(global_state):
 | 
			
		||||
            tick(g)
 | 
			
		||||
 | 
			
		||||
            global last_tick
 | 
			
		||||
            sleep_time = TICK + last_tick - time.time()
 | 
			
		||||
            sleep_time = utils.TICK + last_tick - time.time()
 | 
			
		||||
            if sleep_time < 0: sleep_time = 0
 | 
			
		||||
            time.sleep(sleep_time)
 | 
			
		||||
            last_tick = time.time()
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										40
									
								
								game.py
									
									
									
									
									
								
							
							
						
						
									
										40
									
								
								game.py
									
									
									
									
									
								
							@@ -12,6 +12,8 @@ import utils
 | 
			
		||||
importlib.reload(utils)
 | 
			
		||||
import path
 | 
			
		||||
importlib.reload(path)
 | 
			
		||||
import blocks
 | 
			
		||||
importlib.reload(blocks)
 | 
			
		||||
 | 
			
		||||
class Game:
 | 
			
		||||
    def __init__(self, global_state):
 | 
			
		||||
@@ -38,7 +40,7 @@ class Game:
 | 
			
		||||
    def handle_block_change(self, packet):
 | 
			
		||||
        l = self.g.local_state
 | 
			
		||||
 | 
			
		||||
        if packet.block_state_id == 3887:
 | 
			
		||||
        if packet.block_state_id == blocks.SOUL_TORCH:
 | 
			
		||||
            try:
 | 
			
		||||
                l.goal = LPoint3f(x=packet.location[0], y=packet.location[1], z=packet.location[2])
 | 
			
		||||
                print('new waypoint:', l.goal)
 | 
			
		||||
@@ -130,22 +132,9 @@ class Game:
 | 
			
		||||
        if packet.window_id == 0:
 | 
			
		||||
            self.g.inv[packet.slot] = packet.slot_data
 | 
			
		||||
 | 
			
		||||
    def handle_break_animation(self, packet):
 | 
			
		||||
        print(packet)
 | 
			
		||||
        if self.l.breaking and packet.entity_id == self.g.eid:
 | 
			
		||||
            if packet.destroy_stage >= 10:
 | 
			
		||||
                packet = PlayerDiggingPacket()
 | 
			
		||||
                packet.status = 2
 | 
			
		||||
                packet.location = self.l.breaking
 | 
			
		||||
                packet.face = 1
 | 
			
		||||
                self.g.connection.write_packet(packet)
 | 
			
		||||
                self.l.breaking = None
 | 
			
		||||
 | 
			
		||||
    def handle_break_ack(self, packet):
 | 
			
		||||
        print(packet)
 | 
			
		||||
 | 
			
		||||
    def break_block(self, location):
 | 
			
		||||
        if self.g.chunks.get_block_at(*location) != 0:
 | 
			
		||||
        bid = self.g.chunks.get_block_at(*location)
 | 
			
		||||
        if bid != 0:
 | 
			
		||||
            packet = PlayerDiggingPacket()
 | 
			
		||||
            packet.status = 0
 | 
			
		||||
            packet.location = location
 | 
			
		||||
@@ -153,6 +142,22 @@ class Game:
 | 
			
		||||
            self.g.connection.write_packet(packet)
 | 
			
		||||
 | 
			
		||||
            self.l.breaking = location
 | 
			
		||||
            self.l.break_time = time.time() + utils.break_time(bid)
 | 
			
		||||
 | 
			
		||||
    def break_finish(self):
 | 
			
		||||
        packet = PlayerDiggingPacket()
 | 
			
		||||
        packet.status = 2
 | 
			
		||||
        packet.location = self.l.breaking
 | 
			
		||||
        packet.face = 1
 | 
			
		||||
        self.g.connection.write_packet(packet)
 | 
			
		||||
        self.l.breaking = None
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    def handle_break_animation(self, packet):
 | 
			
		||||
        print(packet)
 | 
			
		||||
 | 
			
		||||
    def handle_break_ack(self, packet):
 | 
			
		||||
        print(packet)
 | 
			
		||||
 | 
			
		||||
    def animate(self):
 | 
			
		||||
        packet = serverbound.play.AnimationPacket()
 | 
			
		||||
@@ -162,3 +167,6 @@ class Game:
 | 
			
		||||
    def tick(self):
 | 
			
		||||
        if self.l.breaking:
 | 
			
		||||
            self.animate()
 | 
			
		||||
 | 
			
		||||
            if time.time() >= self.l.break_time - 2*utils.TICK:
 | 
			
		||||
                self.break_finish()
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										32
									
								
								utils.py
									
									
									
									
									
								
							
							
						
						
									
										32
									
								
								utils.py
									
									
									
									
									
								
							@@ -1,5 +1,11 @@
 | 
			
		||||
import importlib
 | 
			
		||||
from math import floor, ceil
 | 
			
		||||
 | 
			
		||||
import blocks
 | 
			
		||||
importlib.reload(blocks)
 | 
			
		||||
 | 
			
		||||
TICK = 0.05
 | 
			
		||||
 | 
			
		||||
def padd(p1, p2):
 | 
			
		||||
    return (p1[0] + p2[0], p1[1] + p2[1], p1[2] + p2[2])
 | 
			
		||||
 | 
			
		||||
@@ -25,3 +31,29 @@ def cap(x, amount):
 | 
			
		||||
    sign = 1 if x >= 0 else -1
 | 
			
		||||
    return sign * min(abs(x), amount)
 | 
			
		||||
 | 
			
		||||
def break_time(block_id, held_item=0, in_water=False, on_ground=True, enchantments=[], effects={}):
 | 
			
		||||
    # from PrismarineJS/prismarine-block
 | 
			
		||||
    data = blocks.get(block_id)
 | 
			
		||||
 | 
			
		||||
    can_harvest = 'harvestTools' not in data or str(held_item) in data['harvestTools']
 | 
			
		||||
    tool_multipliers = blocks.mcd.materials.get(data['material'], [])
 | 
			
		||||
    is_best_tool = held_item in tool_multipliers
 | 
			
		||||
    time = data['hardness']
 | 
			
		||||
 | 
			
		||||
    if can_harvest:
 | 
			
		||||
        time *= 1.5
 | 
			
		||||
    else:
 | 
			
		||||
        time *= 5.0
 | 
			
		||||
 | 
			
		||||
    if is_best_tool:
 | 
			
		||||
        speed_multiplier = tool_multipliers[held_item]
 | 
			
		||||
        # TODO: calc efficiency, haste, mining fatigue
 | 
			
		||||
    else:
 | 
			
		||||
        speed_multiplier = 1.0
 | 
			
		||||
 | 
			
		||||
    time /= speed_multiplier
 | 
			
		||||
    if in_water: time *= 5.0
 | 
			
		||||
    if not on_ground: time *= 5.0
 | 
			
		||||
 | 
			
		||||
    return time
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user