Get rid of local state, move everything to global

This commit is contained in:
2020-09-16 14:16:10 -06:00
parent 43eefcf41a
commit 41133ce343
3 changed files with 57 additions and 66 deletions

89
bot.py
View File

@@ -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')