Compare commits
3 Commits
13770fab24
...
c30ac5aefc
Author | SHA1 | Date | |
---|---|---|---|
c30ac5aefc | |||
41133ce343 | |||
43eefcf41a |
90
bot.py
90
bot.py
|
@ -44,7 +44,6 @@ YAW_LOOK_AHEAD = 4
|
|||
|
||||
def tick(global_state):
|
||||
g = global_state
|
||||
l = g.local_state
|
||||
p = g.pos
|
||||
|
||||
target = None
|
||||
|
@ -54,10 +53,10 @@ def tick(global_state):
|
|||
except ChunkNotLoadedException:
|
||||
return
|
||||
|
||||
#l.jobstate.run()
|
||||
#g.jobstate.run()
|
||||
|
||||
if l.path and len(l.path):
|
||||
target = LPoint3f(l.path[0])
|
||||
if g.path and len(g.path):
|
||||
target = LPoint3f(g.path[0])
|
||||
target.x += 0.5
|
||||
target.z += 0.5
|
||||
|
||||
|
@ -65,46 +64,46 @@ def tick(global_state):
|
|||
d = p - target
|
||||
|
||||
# jump up block
|
||||
if d.y < -0.9 and not l.y_v:
|
||||
l.y_v = 8.5
|
||||
l.y_a = -36.0
|
||||
if d.y < -0.9 and not g.y_v:
|
||||
g.y_v = 8.5
|
||||
g.y_a = -36.0
|
||||
|
||||
# jump gap
|
||||
if d.xz.length() > 1.6 and not l.y_v:
|
||||
l.y_v = 8.5
|
||||
l.y_a = -36.0
|
||||
if d.xz.length() > 1.6 and not g.y_v:
|
||||
g.y_v = 8.5
|
||||
g.y_a = -36.0
|
||||
|
||||
if d.length() > 0:
|
||||
if l.y_v < 5:
|
||||
if g.y_v < 5:
|
||||
p.x -= utils.cap(d.x, 0.2)
|
||||
p.z -= utils.cap(d.z, 0.2)
|
||||
|
||||
if len(l.path) > 1 and d.length() < 0.2:
|
||||
if len(g.path) > 1 and d.length() < 0.2:
|
||||
# removes some jitter in walking
|
||||
l.path.pop(0)
|
||||
g.path.pop(0)
|
||||
elif d.length() == 0:
|
||||
l.path.pop(0)
|
||||
g.path.pop(0)
|
||||
|
||||
if l.y_v or l.y_a:
|
||||
p.y += l.y_v * utils.TICK
|
||||
l.y_v += l.y_a * utils.TICK
|
||||
if g.y_v or g.y_a:
|
||||
p.y += g.y_v * utils.TICK
|
||||
g.y_v += g.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
|
||||
|
||||
if in_air:
|
||||
l.y_a = -36.0
|
||||
g.y_a = -36.0
|
||||
else:
|
||||
p.y = ceil(p.y)
|
||||
l.y_v = 0
|
||||
l.y_a = 0
|
||||
g.y_v = 0
|
||||
g.y_a = 0
|
||||
|
||||
if l.look_at:
|
||||
look_at = LPoint3f(l.look_at)
|
||||
elif l.path and len(l.path) > YAW_LOOK_AHEAD:
|
||||
look_at = LPoint3f(l.path[YAW_LOOK_AHEAD])
|
||||
elif l.path and len(l.path):
|
||||
look_at = LPoint3f(l.path[-1])
|
||||
if g.look_at:
|
||||
look_at = LPoint3f(g.look_at)
|
||||
elif g.path and len(g.path) > YAW_LOOK_AHEAD:
|
||||
look_at = LPoint3f(g.path[YAW_LOOK_AHEAD])
|
||||
elif g.path and len(g.path):
|
||||
look_at = LPoint3f(g.path[-1])
|
||||
else:
|
||||
look_at = None
|
||||
|
||||
|
@ -116,21 +115,21 @@ def tick(global_state):
|
|||
if look_at_d.length() > 0.6:
|
||||
target_pitch = look_at_d.normalized().angleDeg(PITCH_ANGLE_DIR)
|
||||
target_pitch = (target_pitch - 90) * -1
|
||||
target_pitch_d = target_pitch - l.pitch
|
||||
l.pitch += utils.cap(target_pitch_d, 10)
|
||||
target_pitch_d = target_pitch - g.pitch
|
||||
g.pitch += utils.cap(target_pitch_d, 10)
|
||||
|
||||
# remove vertical component for yaw calculation
|
||||
look_at_d.y = 0
|
||||
|
||||
target_yaw = look_at_d.normalized().signedAngleDeg(other=YAW_ANGLE_DIR, ref=YAW_ANGLE_REF)
|
||||
target_yaw_d = target_yaw - l.yaw
|
||||
target_yaw_d = target_yaw - g.yaw
|
||||
target_yaw_d = (target_yaw_d + 180) % 360 - 180
|
||||
l.yaw += utils.cap(target_yaw_d, 30)
|
||||
g.yaw += utils.cap(target_yaw_d, 30)
|
||||
else:
|
||||
target_pitch_d = 0 - l.pitch
|
||||
l.pitch += utils.cap(target_pitch_d, 10)
|
||||
target_pitch_d = 0 - g.pitch
|
||||
g.pitch += utils.cap(target_pitch_d, 10)
|
||||
|
||||
packet = serverbound.play.PositionAndLookPacket(x=p.x, feet_y=p.y, z=p.z, pitch=l.pitch, yaw=l.yaw, on_ground=(not in_air))
|
||||
packet = serverbound.play.PositionAndLookPacket(x=p.x, feet_y=p.y, z=p.z, pitch=g.pitch, yaw=g.yaw, on_ground=(not in_air))
|
||||
g.connection.write_packet(packet, force=True)
|
||||
|
||||
g.game.tick()
|
||||
|
@ -138,26 +137,24 @@ def tick(global_state):
|
|||
|
||||
def init(global_state):
|
||||
g = global_state
|
||||
l = g.local_state
|
||||
|
||||
l.time = 0
|
||||
g.time = 0
|
||||
|
||||
l.path = []
|
||||
l.look_at = None
|
||||
l.y_v = 0
|
||||
l.y_a = 0
|
||||
l.yaw = 360
|
||||
l.pitch = 0
|
||||
g.path = []
|
||||
g.look_at = None
|
||||
g.y_v = 0
|
||||
g.y_a = 0
|
||||
g.yaw = 360
|
||||
g.pitch = 0
|
||||
|
||||
l.breaking = None
|
||||
l.break_time = 0
|
||||
g.breaking = None
|
||||
g.break_time = 0
|
||||
|
||||
#l.jobstate = JobStates(connection, player_info)
|
||||
#l.jobstate.run()
|
||||
#g.jobstate = JobStates(connection, player_info)
|
||||
#g.jobstate.run()
|
||||
|
||||
def bot(global_state):
|
||||
g = global_state
|
||||
g.local_state = Bunch()
|
||||
|
||||
if not g.mcdata:
|
||||
g.mcdata = DataManager('./mcdata')
|
||||
|
@ -181,6 +178,7 @@ def bot(global_state):
|
|||
g.chat = ChatManager(g)
|
||||
|
||||
g.game = game.Game(g)
|
||||
g.world = game.MCWorld(g)
|
||||
|
||||
try:
|
||||
while not g.pos:
|
||||
|
|
188
game.py
188
game.py
|
@ -1,6 +1,7 @@
|
|||
import re
|
||||
import time
|
||||
import importlib
|
||||
from math import hypot
|
||||
|
||||
from panda3d.core import LPoint3f
|
||||
|
||||
|
@ -15,10 +16,153 @@ importlib.reload(path)
|
|||
import blocks
|
||||
importlib.reload(blocks)
|
||||
|
||||
class MCWorld:
|
||||
def __init__(self, global_state):
|
||||
self.g = global_state
|
||||
|
||||
def block_at(self, x, y, z):
|
||||
return self.g.chunks.get_block_at(x, y, z)
|
||||
|
||||
def find_blocks(self, center, distance, block_ids, limit=0):
|
||||
# search in a spiral from center to all blocks with ID
|
||||
result = []
|
||||
for n in count():
|
||||
offset = utils.spiral(n)
|
||||
check = utils.padd(center, offset)
|
||||
if self.block_at(*check) in block_ids:
|
||||
if hypot(*offset) < distance:
|
||||
result.append(check)
|
||||
if limit and len(result) == limit:
|
||||
return result
|
||||
if offset[0] > distance:
|
||||
return result
|
||||
|
||||
def find_trees(self, center, distance):
|
||||
logs = []
|
||||
for i in range(5):
|
||||
check = utils.padd(center, alternate(i, 3))
|
||||
logs.extend(self.find_blocks(check, distance, blocks.LOG_IDS, 50))
|
||||
|
||||
trees = []
|
||||
for log in logs:
|
||||
# crawl to the bottom log
|
||||
while self.block_at(*utils.padd(log, path.BLOCK_BELOW)) in blocks.LOG_IDS:
|
||||
log = utils.padd(log, path.BLOCK_BELOW)
|
||||
|
||||
# make sure we are on the ground
|
||||
if self.block_at(*utils.padd(log, path.BLOCK_BELOW)) in blocks.NON_SOLID_IDS:
|
||||
continue
|
||||
|
||||
# crawl to the top log to count
|
||||
log_count = 1
|
||||
while self.block_at(*utils.padd(log, path.BLOCK_ABOVE)) in blocks.LOG_IDS:
|
||||
log = utils.padd(log, path.BLOCK_ABOVE)
|
||||
log_count += 1
|
||||
|
||||
# make sure it's a good tree
|
||||
if self.block_at(*utils.padd(log, path.BLOCK_ABOVE)) in blocks.LEAF_IDS and log_count > 2:
|
||||
# crawl back to the bottom log
|
||||
while self.block_at(*utils.padd(log, path.BLOCK_BELOW)) in blocks.LOG_IDS:
|
||||
log = utils.padd(log, path.BLOCK_BELOW)
|
||||
trees.append(log)
|
||||
|
||||
trees.sort(key=lambda x: 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)
|
||||
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)
|
||||
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)
|
||||
|
||||
try:
|
||||
s = maze_solver.astar(start, place)
|
||||
return list(s) if s else None
|
||||
except AStarTimeout:
|
||||
return None
|
||||
|
||||
def find_bed_areas(self, center, distance):
|
||||
air = []
|
||||
for i in range(5):
|
||||
check = utils.padd(center, alternate(i, 1))
|
||||
air.extend(self.find_blocks(check, distance, [0], 200))
|
||||
|
||||
areas = []
|
||||
for a in air:
|
||||
# check for ground around the area
|
||||
if len(self.find_blocks(utils.padd(a, path.BLOCK_BELOW), 2, blocks.NON_SOLID_IDS, 9)):
|
||||
continue
|
||||
|
||||
# check for air around the area
|
||||
if len(self.find_blocks(a, 2, [0], 9)) < 9:
|
||||
continue
|
||||
|
||||
# check for air above the area
|
||||
if len(self.find_blocks(utils.padd(a, path.BLOCK_ABOVE), 2, [0], 9)) < 9:
|
||||
continue
|
||||
|
||||
areas.append(a)
|
||||
|
||||
areas.sort(key=lambda x: phyp(center, x))
|
||||
return areas
|
||||
|
||||
def sand_adjacent_safe(self, sand):
|
||||
for direction in CHECK_DIRECTIONS:
|
||||
if self.block_at(*utils.padd(sand, direction)) in blocks.AVOID_IDS:
|
||||
return False
|
||||
return True
|
||||
|
||||
def find_sand(self, center, distance, origin):
|
||||
sand = []
|
||||
for i in range(10):
|
||||
check = utils.padd(center, alternate(i, 1))
|
||||
sand.extend(self.find_blocks(check, distance, [66], 20))
|
||||
|
||||
safe_sand = []
|
||||
for s in sand:
|
||||
# make sure it has solid below
|
||||
if self.block_at(*utils.padd(s, path.BLOCK_BELOW)) in blocks.NON_SOLID_IDS:
|
||||
continue
|
||||
# make sure it has solid two below - prevent hanging sand
|
||||
if self.block_at(*utils.padd(s, path.BLOCK_BELOW2)) in blocks.NON_SOLID_IDS:
|
||||
continue
|
||||
|
||||
# and walkable air above
|
||||
if self.block_at(*utils.padd(s, path.BLOCK_ABOVE)) not in blocks.NON_SOLID_IDS:
|
||||
continue
|
||||
|
||||
if not self.sand_adjacent_safe(s):
|
||||
continue
|
||||
|
||||
safe_sand.append(s)
|
||||
|
||||
safe_sand.sort(key=lambda x: utils.phyp_bias(center, x, origin))
|
||||
return safe_sand
|
||||
|
||||
def find_bed_openings(self, area):
|
||||
# returns coords in a cardinal direction where we can stand by bed
|
||||
result = []
|
||||
|
||||
for direction in CHECK_DIRECTIONS:
|
||||
result.append(utils.padd(area, direction))
|
||||
return result
|
||||
|
||||
|
||||
class Game:
|
||||
def __init__(self, global_state):
|
||||
self.g = global_state
|
||||
self.l = self.g.local_state
|
||||
|
||||
register = self.g.connection.register_packet_listener
|
||||
register(self.handle_block_change, clientbound.play.BlockChangePacket)
|
||||
|
@ -38,19 +182,17 @@ class Game:
|
|||
self.g.eid = packet.entity_id
|
||||
|
||||
def handle_block_change(self, packet):
|
||||
l = self.g.local_state
|
||||
|
||||
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)
|
||||
self.g.goal = LPoint3f(x=packet.location[0], y=packet.location[1], z=packet.location[2])
|
||||
print('new waypoint:', self.g.goal)
|
||||
|
||||
start = time.time()
|
||||
solution = path.Pathfinder(self.g.chunks).astar(utils.pint(self.g.pos), utils.pint(l.goal))
|
||||
solution = path.Pathfinder(self.g.chunks).astar(utils.pint(self.g.pos), utils.pint(g.goal))
|
||||
if solution:
|
||||
solution = list(solution)
|
||||
l.path = solution
|
||||
#l.jobstate.state = l.jobstate.stop
|
||||
self.g.path = solution
|
||||
#g.jobstate.state = self.g.jobstate.stop
|
||||
print(len(solution))
|
||||
print(solution)
|
||||
print(round(time.time() - start, 3), 'seconds')
|
||||
|
@ -58,8 +200,8 @@ class Game:
|
|||
print('No path found')
|
||||
#say(connection, 'No path found')
|
||||
|
||||
#l.y_v = 10.0
|
||||
#l.y_a = -36.0
|
||||
#g.y_v = 10.0
|
||||
#g.y_a = -36.0
|
||||
except BaseException as e:
|
||||
import traceback
|
||||
print(traceback.format_exc())
|
||||
|
@ -124,8 +266,7 @@ class Game:
|
|||
self.g.chat.send(reply)
|
||||
|
||||
def handle_time_update(self, packet):
|
||||
l = self.g.local_state
|
||||
l.time = packet.time_of_day % 24000
|
||||
self.g.time = packet.time_of_day % 24000
|
||||
|
||||
def handle_set_slot(self, packet):
|
||||
print(packet)
|
||||
|
@ -141,16 +282,16 @@ class Game:
|
|||
packet.face = 1
|
||||
self.g.connection.write_packet(packet)
|
||||
|
||||
self.l.breaking = location
|
||||
self.l.break_time = time.time() + utils.break_time(bid)
|
||||
self.g.breaking = location
|
||||
self.g.break_time = time.time() + utils.break_time(bid)
|
||||
|
||||
def break_finish(self):
|
||||
packet = PlayerDiggingPacket()
|
||||
packet.status = 2
|
||||
packet.location = self.l.breaking
|
||||
packet.location = self.g.breaking
|
||||
packet.face = 1
|
||||
self.g.connection.write_packet(packet)
|
||||
self.l.breaking = None
|
||||
self.g.breaking = None
|
||||
|
||||
|
||||
def handle_break_animation(self, packet):
|
||||
|
@ -164,9 +305,20 @@ class Game:
|
|||
packet.hand = packet.HAND_MAIN
|
||||
self.g.connection.write_packet(packet)
|
||||
|
||||
def place_block(self, location, face):
|
||||
packet = serverbound.play.PlayerBlockPlacementPacket()
|
||||
packet.hand = 0
|
||||
packet.location = pos
|
||||
packet.face = face
|
||||
packet.x = 0.5
|
||||
packet.y = 0.5
|
||||
packet.z = 0.5
|
||||
packet.inside_block = False
|
||||
self.g.connection.write_packet(packet)
|
||||
|
||||
def tick(self):
|
||||
if self.l.breaking:
|
||||
if self.g.breaking:
|
||||
self.animate()
|
||||
|
||||
if time.time() >= self.l.break_time - 2*utils.TICK:
|
||||
if time.time() >= self.g.break_time - 2*utils.TICK:
|
||||
self.break_finish()
|
||||
|
|
445
jobs.py
Normal file
445
jobs.py
Normal file
|
@ -0,0 +1,445 @@
|
|||
import re
|
||||
import time
|
||||
import importlib
|
||||
from math import hypot
|
||||
|
||||
from panda3d.core import LPoint3f
|
||||
|
||||
from minecraft.networking.types import BlockFace
|
||||
|
||||
import utils
|
||||
importlib.reload(utils)
|
||||
import path
|
||||
importlib.reload(path)
|
||||
import blocks
|
||||
importlib.reload(blocks)
|
||||
|
||||
|
||||
class LumberjackStates:
|
||||
def bair(self, p):
|
||||
return self.g.chunks.get_block_at(*p) in blocks.NON_SOLID_IDS
|
||||
|
||||
def blog(self, p):
|
||||
return self.g.chunks.get_block_at(*p) in blocks.LOG_IDS
|
||||
|
||||
def idle(self):
|
||||
return None
|
||||
|
||||
def init(self):
|
||||
self.state = self.find_new_tree
|
||||
|
||||
def find_new_tree(self):
|
||||
print('Finding new tree...')
|
||||
w = self.g.world
|
||||
p = utils.pint(self.g.pos)
|
||||
|
||||
trees = w.find_trees(p, 100)
|
||||
print('Found trees:', trees)
|
||||
|
||||
while trees[0] in self.bad_trees:
|
||||
trees.pop(0)
|
||||
self.tree = trees[0]
|
||||
|
||||
self.openings = w.find_tree_openings(self.tree)
|
||||
self.state = self.choose_opening
|
||||
|
||||
def choose_opening(self):
|
||||
w = self.g.world
|
||||
p = utils.pint(self.g.pos)
|
||||
|
||||
print('openings:', self.openings)
|
||||
|
||||
if not len(self.openings):
|
||||
print('Unable to get to tree', self.tree)
|
||||
self.bad_trees.append(self.tree)
|
||||
self.state = self.cleanup
|
||||
return
|
||||
|
||||
path = w.path_to_place(p, self.openings[0])
|
||||
|
||||
if path:
|
||||
self.g.path = path
|
||||
self.state = self.going_to_tree
|
||||
else:
|
||||
self.openings.pop(0)
|
||||
|
||||
def going_to_tree(self):
|
||||
if utils.pint(self.g.pos) == self.openings[0]:
|
||||
self.g.look_at = self.tree
|
||||
self.state = self.clear_leaves
|
||||
|
||||
def clear_leaves(self):
|
||||
if not self.g.breaking:
|
||||
p = utils.pint(self.g.pos)
|
||||
diff = psub(self.tree, p)
|
||||
|
||||
for x in diffrange(diff[0]):
|
||||
for z in diffrange(diff[2]):
|
||||
for y in range(2):
|
||||
check = utils.padd(p, (x, y, z))
|
||||
if check == self.tree:
|
||||
break
|
||||
if not self.bair(check):
|
||||
print('Breaking leaf')
|
||||
self.g.game.break_block(check)
|
||||
return
|
||||
|
||||
self.state = self.clear_trunk_base
|
||||
|
||||
def clear_trunk_base(self):
|
||||
if not self.g.breaking:
|
||||
base = self.tree
|
||||
above = utils.padd(self.tree, path.BLOCK_ABOVE)
|
||||
|
||||
if self.blog(base):
|
||||
self.g.game.break_block(base)
|
||||
print('breaking base')
|
||||
elif self.blog(above):
|
||||
self.g.game.break_block(above)
|
||||
print('breaking above')
|
||||
else:
|
||||
w = self.g.world
|
||||
p = utils.pint(self.g.pos)
|
||||
path = w.path_to_place(p, self.tree)
|
||||
|
||||
if path:
|
||||
self.g.path = path
|
||||
self.state = self.going_to_trunk_base
|
||||
else:
|
||||
self.openings.pop(0)
|
||||
self.state = self.choose_opening
|
||||
|
||||
def going_to_trunk_base(self):
|
||||
if utils.pint(self.g.pos) == self.tree:
|
||||
self.g.look_at = utils.padd(self.tree, path.BLOCK_ABOVE2)
|
||||
self.state = self.clear_trunk
|
||||
|
||||
def clear_trunk(self):
|
||||
if not self.g.breaking:
|
||||
check = self.tree
|
||||
|
||||
count = 0
|
||||
while self.bair(check) and count < 6:
|
||||
check = utils.padd(check, path.BLOCK_ABOVE)
|
||||
count += 1
|
||||
|
||||
if self.blog(check):
|
||||
print('breaking log', check)
|
||||
self.g.game.break_block(check)
|
||||
else:
|
||||
print('Finished clearing tree')
|
||||
self.wait_time = 0.5
|
||||
self.state = self.wait
|
||||
|
||||
def wait(self):
|
||||
# wait for the last log to fall
|
||||
if self.wait_time > 0:
|
||||
self.wait_time -= TICK
|
||||
else:
|
||||
self.state = self.cleanup
|
||||
|
||||
def cleanup(self):
|
||||
self.g.look_at = None
|
||||
self.state = self.done
|
||||
|
||||
def done(self):
|
||||
# never gets ran, placeholder
|
||||
return None
|
||||
|
||||
|
||||
def __init__(self, global_state):
|
||||
self.g = global_state
|
||||
self.l = self.g.local_state
|
||||
self.state = self.idle
|
||||
|
||||
self.tree = None
|
||||
self.openings = []
|
||||
self.bad_trees = []
|
||||
self.wait_time = 0
|
||||
|
||||
def run(self):
|
||||
self.state()
|
||||
|
||||
|
||||
class GatherSandStates:
|
||||
def bair(self, p):
|
||||
return self.g.chunks.get_block_at(*p) in blocks.NON_SOLID_IDS
|
||||
|
||||
def bsand(self, p):
|
||||
return self.g.chunks.get_block_at(*p) == 66
|
||||
|
||||
def idle(self):
|
||||
return None
|
||||
|
||||
def init(self):
|
||||
self.state = self.find_new_sand
|
||||
|
||||
def find_new_sand(self):
|
||||
print('Finding new sand...')
|
||||
w = self.g.world
|
||||
p = utils.pint(self.g.pos)
|
||||
|
||||
sand = w.find_sand(p, 150, self.origin)
|
||||
print('Found sand:', sand)
|
||||
|
||||
while sand[0] in self.bad_sand:
|
||||
sand.pop(0)
|
||||
self.sand = sand[0]
|
||||
|
||||
self.state = self.nav_to_sand
|
||||
|
||||
def nav_to_sand(self):
|
||||
w = self.g.world
|
||||
p = utils.pint(self.g.pos)
|
||||
|
||||
w.chunks.set_block_at(*self.sand, 0)
|
||||
path = w.path_to_place(p, self.sand)
|
||||
w.chunks.set_block_at(*self.sand, 66)
|
||||
|
||||
if path:
|
||||
self.g.path = path[:-1]
|
||||
self.state = self.going_to_sand
|
||||
else:
|
||||
self.bad_sand.append(self.sand)
|
||||
self.state = self.find_new_sand
|
||||
|
||||
def going_to_sand(self):
|
||||
if not len(self.g.path):
|
||||
self.g.look_at = self.sand
|
||||
self.state = self.dig_sand
|
||||
|
||||
def dig_sand(self):
|
||||
if not self.g.breaking:
|
||||
if self.bsand(self.sand):
|
||||
self.g.game.break_block(self.sand)
|
||||
print('digging sand')
|
||||
else:
|
||||
self.state = self.get_sand
|
||||
|
||||
def get_sand(self):
|
||||
w = self.g.world
|
||||
p = utils.pint(self.g.pos)
|
||||
path = w.path_to_place(p, self.sand)
|
||||
|
||||
if path:
|
||||
self.g.path = path
|
||||
self.state = self.going_to_item
|
||||
else:
|
||||
self.bad_sand.append(self.sand)
|
||||
self.state = self.find_new_sand
|
||||
|
||||
def going_to_item(self):
|
||||
if utils.pint(self.g.pos) == self.sand:
|
||||
self.g.look_at = self.sand
|
||||
self.state = self.cleanup
|
||||
|
||||
def cleanup(self):
|
||||
self.g.look_at = None
|
||||
self.state = self.done
|
||||
|
||||
def done(self):
|
||||
# never gets ran, placeholder
|
||||
return None
|
||||
|
||||
|
||||
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)
|
||||
self.sand = None
|
||||
self.bad_sand = []
|
||||
self.wait_time = 0
|
||||
|
||||
def run(self):
|
||||
self.state()
|
||||
|
||||
|
||||
class SleepWithBedStates:
|
||||
def idle(self):
|
||||
return None
|
||||
|
||||
def init(self):
|
||||
if self.g.time >= 12000:
|
||||
self.state = self.find_bed_spot
|
||||
else:
|
||||
print('Aborting sleep, not night')
|
||||
self.state = self.cleanup
|
||||
|
||||
def find_bed_spot(self):
|
||||
print('Finding a bed spot...')
|
||||
w = self.g.world
|
||||
p = utils.pint(self.g.pos)
|
||||
|
||||
areas = w.find_bed_areas(p, 100)
|
||||
print('Found areas:', areas)
|
||||
|
||||
if len(areas):
|
||||
while areas[0] in self.bad_areas:
|
||||
areas.pop(0)
|
||||
self.area = areas[0]
|
||||
elif self.last_area:
|
||||
self.area = self.last_area
|
||||
else:
|
||||
print('Unable to find area, and no last area')
|
||||
self.state = self.cleanup
|
||||
return
|
||||
|
||||
openings = w.find_bed_openings(self.area)
|
||||
|
||||
for o in openings:
|
||||
path = w.path_to_place(p, o)
|
||||
self.opening = o
|
||||
if path: 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.state = self.going_to_area
|
||||
self.last_area = self.area
|
||||
|
||||
def going_to_area(self):
|
||||
if utils.pint(self.g.pos) == self.opening:
|
||||
self.g.look_at = self.area
|
||||
self.state = self.select_bed
|
||||
|
||||
def select_bed(self):
|
||||
for slot, item in self.g.inv.items():
|
||||
if item.item_id in items.BED_IDS:
|
||||
print('Found bed in slot', slot)
|
||||
self.g.look_at = utils.padd(self.area, path.BLOCK_BELOW)
|
||||
choose_slot(self.connection, slot)
|
||||
self.state = self.place_bed
|
||||
break
|
||||
else: # for
|
||||
say(self.connection, 'I need a bed')
|
||||
self.state = self.cleanup
|
||||
|
||||
def place_bed(self):
|
||||
place_block(self.connection, self.area, BlockFace.TOP)
|
||||
self.state = self.use_bed
|
||||
|
||||
def use_bed(self):
|
||||
if self.g.time >= 12542:
|
||||
print('Sleeping')
|
||||
place_block(self.connection, self.area, BlockFace.TOP)
|
||||
say(self.connection, 'zzz')
|
||||
self.state = self.sleep_bed
|
||||
|
||||
def sleep_bed(self):
|
||||
if self.g.time < 100:
|
||||
print('Woke up')
|
||||
self.state = self.break_bed
|
||||
|
||||
def break_bed(self):
|
||||
self.g.game.break_block(self.area)
|
||||
self.state = self.collect_bed
|
||||
|
||||
def collect_bed(self):
|
||||
if not self.g.breaking:
|
||||
self.g.path = [utils.padd(self.area, utils.spiral(n)) for n in range(9)]
|
||||
self.wait_time = 4
|
||||
self.state = self.wait
|
||||
|
||||
def wait(self):
|
||||
# wait to pick up bed
|
||||
if self.wait_time > 0:
|
||||
self.wait_time -= TICK
|
||||
else:
|
||||
self.state = self.cleanup
|
||||
|
||||
def cleanup(self):
|
||||
self.g.look_at = None
|
||||
self.state = self.done
|
||||
|
||||
def done(self):
|
||||
# never gets ran, placeholder
|
||||
return None
|
||||
|
||||
def __init__(self, global_state):
|
||||
self.g = global_state
|
||||
self.l = self.g.local_state
|
||||
self.state = self.idle
|
||||
|
||||
self.area = None
|
||||
self.opening = None
|
||||
self.bad_areas = []
|
||||
self.last_area = None
|
||||
self.wait_time = 0
|
||||
|
||||
def run(self):
|
||||
self.state()
|
||||
|
||||
|
||||
class JobStates:
|
||||
def idle(self):
|
||||
return None
|
||||
|
||||
def sleep_with_bed(self):
|
||||
s = self.sleep_with_bed_states
|
||||
if s.state == s.idle:
|
||||
s.state = s.init
|
||||
elif s.state == s.done:
|
||||
s.state = s.init
|
||||
# check time, etc
|
||||
|
||||
if self.prev_state:
|
||||
print('Reverting to prev state')
|
||||
self.state = self.prev_state
|
||||
return
|
||||
|
||||
s.run()
|
||||
|
||||
def gather_sand(self):
|
||||
s = self.gather_sand_states
|
||||
if s.state == s.idle:
|
||||
s.state = s.init
|
||||
elif s.state == s.done:
|
||||
s.state = s.init
|
||||
# check time, etc
|
||||
|
||||
if self.survive:
|
||||
self.prev_state = self.gather_sand
|
||||
self.state = self.sleep_with_bed
|
||||
return
|
||||
|
||||
s.run()
|
||||
|
||||
def lumberjack(self):
|
||||
s = self.lumberjack_states
|
||||
if s.state == s.idle:
|
||||
s.state = s.init
|
||||
elif s.state == s.done:
|
||||
s.state = s.init
|
||||
# check time, etc
|
||||
|
||||
if self.survive:
|
||||
self.prev_state = self.lumberjack
|
||||
self.state = self.sleep_with_bed
|
||||
return
|
||||
|
||||
s.run()
|
||||
|
||||
def stop(self):
|
||||
self.lumberjack_states = LumberjackStates(self.g)
|
||||
self.gather_sand_states = GatherSandStates(self.g)
|
||||
self.sleep_with_bed_states = SleepWithBedStates(self.g)
|
||||
self.state = self.idle
|
||||
|
||||
def __init__(self, global_state):
|
||||
self.g = global_state
|
||||
self.l = self.g.local_state
|
||||
|
||||
self.state = self.idle
|
||||
self.prev_state = None
|
||||
self.lumberjack_states = LumberjackStates(self.g)
|
||||
self.gather_sand_states = GatherSandStates(self.g)
|
||||
self.sleep_with_bed_states = SleepWithBedStates(self.g)
|
||||
self.survive = False
|
||||
|
||||
def run(self):
|
||||
self.state()
|
1
main.py
1
main.py
|
@ -14,7 +14,6 @@ import bot
|
|||
|
||||
global_state = Bunch()
|
||||
g = global_state
|
||||
g.local_state = False
|
||||
g.connection = False
|
||||
g.mcdata = False
|
||||
g.pos = False
|
||||
|
|
12
path.py
12
path.py
|
@ -49,6 +49,18 @@ PARKOUR_SOUTH = (0, 0, +2)
|
|||
PARKOUR_EAST = (+2, 0, 0)
|
||||
PARKOUR_WEST = (-2, 0, 0)
|
||||
|
||||
CHECK_NORTH = (0, 0, -1)
|
||||
CHECK_SOUTH = (0, 0, +1)
|
||||
CHECK_EAST = (+1, 0, 0)
|
||||
CHECK_WEST = (-1, 0, 0)
|
||||
|
||||
CHECK_DIRECTIONS = [
|
||||
CHECK_NORTH,
|
||||
CHECK_SOUTH,
|
||||
CHECK_EAST,
|
||||
CHECK_WEST,
|
||||
]
|
||||
|
||||
TRAVERSE = [
|
||||
TRAVERSE_NORTH,
|
||||
TRAVERSE_SOUTH,
|
||||
|
|
Loading…
Reference in New Issue
Block a user