From 58458a561f469da5c535d34dab41fd4876bb44c8 Mon Sep 17 00:00:00 2001 From: Tanner Collin Date: Wed, 16 Sep 2020 19:12:01 -0600 Subject: [PATCH] Fix jobstate bugs --- bot.py | 7 +++++-- game.py | 30 +++++++++++++++++++----------- jobs.py | 46 +++++++++++++++++++++------------------------- utils.py | 34 +++++++++++++++++++++++++++++++++- 4 files changed, 78 insertions(+), 39 deletions(-) diff --git a/bot.py b/bot.py index 24cd72e..bf4678f 100644 --- a/bot.py +++ b/bot.py @@ -32,6 +32,8 @@ import utils importlib.reload(utils) import path importlib.reload(path) +import jobs +importlib.reload(jobs) last_tick = time.time() @@ -133,6 +135,7 @@ def tick(global_state): g.connection.write_packet(packet, force=True) g.game.tick() + g.job.tick() def init(global_state): @@ -150,8 +153,7 @@ def init(global_state): g.breaking = None g.break_time = 0 - #g.jobstate = JobStates(connection, player_info) - #g.jobstate.run() + g.job = jobs.JobStates(g) def bot(global_state): g = global_state @@ -191,6 +193,7 @@ def bot(global_state): print('Chunks loaded.') init(g) + print('Initialized.') while g.running: tick(g) diff --git a/game.py b/game.py index 55cb50e..0db403e 100644 --- a/game.py +++ b/game.py @@ -2,6 +2,7 @@ import re import time import importlib from math import hypot +from itertools import count from panda3d.core import LPoint3f @@ -40,7 +41,7 @@ class MCWorld: def find_trees(self, center, distance): logs = [] for i in range(5): - check = utils.padd(center, alternate(i, 3)) + check = utils.padd(center, utils.alternate(i, 3)) logs.extend(self.find_blocks(check, distance, blocks.LOG_IDS, 50)) trees = [] @@ -66,26 +67,26 @@ class MCWorld: log = utils.padd(log, path.BLOCK_BELOW) trees.append(log) - trees.sort(key=lambda x: phyp(center, x)) + trees.sort(key=lambda x: utils.phyp(center, x)) return trees def find_tree_openings(self, tree): # returns coords in a cardinal direction where we can stand by tree - maze_solver = MazeSolver(self.g.chunks) + maze_solver = path.Pathfinder(self.g.chunks) result = [] # TODO: make sure only non-solid and leaves between # make sure traversable too for distance in range(5): - for direction in CHECK_DIRECTIONS: - offset = pmul(direction, distance+1) + for direction in path.CHECK_DIRECTIONS: + offset = utils.pmul(direction, distance+1) if maze_solver.check_traverse(tree, offset): result.append(utils.padd(tree, offset)) return result def path_to_place(self, start, place): - maze_solver = MazeSolver(self.g.chunks) + maze_solver = path.Pathfinder(self.g.chunks) try: s = maze_solver.astar(start, place) @@ -115,11 +116,11 @@ class MCWorld: areas.append(a) - areas.sort(key=lambda x: phyp(center, x)) + areas.sort(key=lambda x: utils.phyp(center, x)) return areas def sand_adjacent_safe(self, sand): - for direction in CHECK_DIRECTIONS: + for direction in path.CHECK_DIRECTIONS: if self.block_at(*utils.padd(sand, direction)) in blocks.AVOID_IDS: return False return True @@ -155,7 +156,7 @@ class MCWorld: # returns coords in a cardinal direction where we can stand by bed result = [] - for direction in CHECK_DIRECTIONS: + for direction in path.CHECK_DIRECTIONS: result.append(utils.padd(area, direction)) return result @@ -188,7 +189,7 @@ class Game: print('new waypoint:', self.g.goal) start = time.time() - solution = path.Pathfinder(self.g.chunks).astar(utils.pint(self.g.pos), utils.pint(g.goal)) + solution = path.Pathfinder(self.g.chunks).astar(utils.pint(self.g.pos), utils.pint(self.g.goal)) if solution: solution = list(solution) self.g.path = solution @@ -261,6 +262,11 @@ class Game: self.break_block((616, 78, 496)) reply = 'ok' + if command == 'gather' and data: + if data == 'wood': + self.g.job.state = self.g.job.lumberjack + reply = 'ok' + if reply: print(reply) self.g.chat.send(reply) @@ -291,6 +297,7 @@ class Game: packet.location = self.g.breaking packet.face = 1 self.g.connection.write_packet(packet) + self.g.chunks.set_block_at(*self.g.breaking, 0) self.g.breaking = None @@ -298,7 +305,8 @@ class Game: print(packet) def handle_break_ack(self, packet): - print(packet) + #print(packet) + return def animate(self): packet = serverbound.play.AnimationPacket() diff --git a/jobs.py b/jobs.py index 86319db..fb6be01 100644 --- a/jobs.py +++ b/jobs.py @@ -55,10 +55,10 @@ class LumberjackStates: self.state = self.cleanup return - path = w.path_to_place(p, self.openings[0]) + navpath = w.path_to_place(p, self.openings[0]) - if path: - self.g.path = path + if navpath: + self.g.path = navpath self.state = self.going_to_tree else: self.openings.pop(0) @@ -71,10 +71,10 @@ class LumberjackStates: def clear_leaves(self): if not self.g.breaking: p = utils.pint(self.g.pos) - diff = psub(self.tree, p) + diff = utils.psub(self.tree, p) - for x in diffrange(diff[0]): - for z in diffrange(diff[2]): + for x in utils.diffrange(diff[0]): + for z in utils.diffrange(diff[2]): for y in range(2): check = utils.padd(p, (x, y, z)) if check == self.tree: @@ -100,10 +100,10 @@ class LumberjackStates: else: w = self.g.world p = utils.pint(self.g.pos) - path = w.path_to_place(p, self.tree) + navpath = w.path_to_place(p, self.tree) - if path: - self.g.path = path + if navpath: + self.g.path = navpath self.state = self.going_to_trunk_base else: self.openings.pop(0) @@ -134,7 +134,7 @@ class LumberjackStates: def wait(self): # wait for the last log to fall if self.wait_time > 0: - self.wait_time -= TICK + self.wait_time -= utils.TICK else: self.state = self.cleanup @@ -149,7 +149,6 @@ class LumberjackStates: def __init__(self, global_state): self.g = global_state - self.l = self.g.local_state self.state = self.idle self.tree = None @@ -193,11 +192,11 @@ class GatherSandStates: p = utils.pint(self.g.pos) w.chunks.set_block_at(*self.sand, 0) - path = w.path_to_place(p, self.sand) + navpath = w.path_to_place(p, self.sand) w.chunks.set_block_at(*self.sand, 66) - if path: - self.g.path = path[:-1] + if navpath: + self.g.path = navpath[:-1] self.state = self.going_to_sand else: self.bad_sand.append(self.sand) @@ -219,10 +218,10 @@ class GatherSandStates: def get_sand(self): w = self.g.world p = utils.pint(self.g.pos) - path = w.path_to_place(p, self.sand) + navpath = w.path_to_place(p, self.sand) - if path: - self.g.path = path + if navpath: + self.g.path = navpath self.state = self.going_to_item else: self.bad_sand.append(self.sand) @@ -244,7 +243,6 @@ class GatherSandStates: def __init__(self, global_state): self.g = global_state - self.l = self.g.local_state self.state = self.idle self.origin = utils.pint(self.g.pos) @@ -289,16 +287,16 @@ class SleepWithBedStates: openings = w.find_bed_openings(self.area) for o in openings: - path = w.path_to_place(p, o) + navpath = w.path_to_place(p, o) self.opening = o - if path: break + if navpath: break else: # for print('Unable to get to bed area', self.area) self.bad_areas.append(self.area) self.state = self.cleanup return - self.g.path = path + self.g.path = navpath self.state = self.going_to_area self.last_area = self.area @@ -348,7 +346,7 @@ class SleepWithBedStates: def wait(self): # wait to pick up bed if self.wait_time > 0: - self.wait_time -= TICK + self.wait_time -= utils.TICK else: self.state = self.cleanup @@ -362,7 +360,6 @@ class SleepWithBedStates: def __init__(self, global_state): self.g = global_state - self.l = self.g.local_state self.state = self.idle self.area = None @@ -432,7 +429,6 @@ class JobStates: def __init__(self, global_state): self.g = global_state - self.l = self.g.local_state self.state = self.idle self.prev_state = None @@ -441,5 +437,5 @@ class JobStates: self.sleep_with_bed_states = SleepWithBedStates(self.g) self.survive = False - def run(self): + def tick(self): self.state() diff --git a/utils.py b/utils.py index cdf1f3e..89b5351 100644 --- a/utils.py +++ b/utils.py @@ -1,5 +1,5 @@ import importlib -from math import floor, ceil +from math import floor, ceil, sqrt, hypot import blocks importlib.reload(blocks) @@ -31,6 +31,38 @@ def cap(x, amount): sign = 1 if x >= 0 else -1 return sign * min(abs(x), amount) +def spiral(n): + # return x, 0, z coords along a spiral at step n + # I forget where I found this + n += 1 + k = ceil((sqrt(n)-1)/2) + t = 2 * k + 1 + m = t**2 + t = t - 1 + if n >= m-t: + return k-(m-n), 0, -k + else: + m = m-t + if n >= m-t: + return -k, 0, -k+(m-n) + else: + m = m-t + if n >= m-t: + return -k+(m-n), 0, k + else: + return k, 0, k-(m-n-t) + +def alternate(n, amount): + # return 0, y, 0 where y alternates +/- by amount + # example: 0, 2, -2, 4, -4, 6, -6 for amount = 2 + sign = 1 if n % 2 else -1 + return (0, ceil(n/2) * sign * amount, 0) + +def diffrange(n): + # same as range(n+1) but can go negative + 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 data = blocks.get(block_id)