From 13770fab24bd64c652ed3a265679d3d98c2c7b64 Mon Sep 17 00:00:00 2001 From: Tanner Collin Date: Wed, 16 Sep 2020 00:49:15 -0600 Subject: [PATCH] Calculate the correct block breaking time --- bot.py | 12 ++++++------ game.py | 40 ++++++++++++++++++++++++---------------- utils.py | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 62 insertions(+), 22 deletions(-) diff --git a/bot.py b/bot.py index 8e9d8f5..684e306 100644 --- a/bot.py +++ b/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() diff --git a/game.py b/game.py index bf34796..99c1028 100644 --- a/game.py +++ b/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() diff --git a/utils.py b/utils.py index a4cbc19..cdf1f3e 100644 --- a/utils.py +++ b/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 +