Get rid of local state, move everything to global
This commit is contained in:
parent
43eefcf41a
commit
41133ce343
89
bot.py
89
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')
|
||||
|
|
33
game.py
33
game.py
|
@ -19,7 +19,6 @@ importlib.reload(blocks)
|
|||
class MCWorld:
|
||||
def __init__(self, global_state):
|
||||
self.g = global_state
|
||||
self.l = self.g.local_state
|
||||
|
||||
def block_at(self, x, y, z):
|
||||
return self.g.chunks.get_block_at(x, y, z)
|
||||
|
@ -164,7 +163,6 @@ class MCWorld:
|
|||
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)
|
||||
|
@ -184,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')
|
||||
|
@ -204,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())
|
||||
|
@ -270,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)
|
||||
|
@ -287,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):
|
||||
|
@ -322,8 +317,8 @@ class Game:
|
|||
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()
|
||||
|
|
Loading…
Reference in New Issue
Block a user