minecraft-bot/bot.py

217 lines
5.3 KiB
Python
Raw Normal View History

if __name__ == '__main__':
print('Run main.py instead.')
exit(1)
2020-09-07 22:32:19 +00:00
import os
import time
import importlib
2020-09-08 21:33:42 +00:00
from math import floor, ceil
2020-09-07 22:32:19 +00:00
USERNAME = os.environ['USERNAME']
PASSWORD = os.environ['PASSWORD']
SERVER = os.environ['SERVER']
import monkey_patch # must be before any possible pyCraft imports
from minecraft import authentication
from minecraft.exceptions import YggdrasilError
from minecraft.networking.connection import Connection
from minecraft.networking.packets import Packet, clientbound, serverbound
2020-09-09 02:13:15 +00:00
from protocol.managers import DataManager, ChunksManager, ChatManager, ChunkNotLoadedException
2020-09-08 01:28:56 +00:00
2020-09-08 21:33:42 +00:00
from bunch import Bunch
from panda3d.core import LPoint3f, LVector3f
2020-09-08 20:07:09 +00:00
2020-09-16 06:02:36 +00:00
import game
importlib.reload(game)
2020-09-08 21:33:42 +00:00
import blocks
importlib.reload(blocks)
import utils
importlib.reload(utils)
import path
importlib.reload(path)
2020-09-17 01:12:01 +00:00
import jobs
importlib.reload(jobs)
2020-09-08 01:28:56 +00:00
last_tick = time.time()
2020-09-08 21:33:42 +00:00
PITCH_ANGLE_DIR = LVector3f(x=0, y=1, z=0)
YAW_ANGLE_DIR = LVector3f(x=0, y=0, z=-1)
YAW_ANGLE_REF = LVector3f(x=0, y=1, z=0)
YAW_LOOK_AHEAD = 4
def tick(global_state):
g = global_state
p = g.pos
target = None
try:
g.chunks.get_block_at(*utils.pint(p))
2020-09-09 01:52:50 +00:00
except ChunkNotLoadedException:
2020-09-08 21:33:42 +00:00
return
#g.jobstate.run()
2020-09-08 21:33:42 +00:00
if g.path and len(g.path):
target = LPoint3f(g.path[0])
2020-09-08 21:33:42 +00:00
target.x += 0.5
target.z += 0.5
if target:
d = p - target
# jump up block
if d.y < -0.9 and not g.y_v:
g.y_v = 8.5
g.y_a = -36.0
2020-09-08 21:33:42 +00:00
# jump gap
if d.xz.length() > 1.6 and not g.y_v:
g.y_v = 8.5
g.y_a = -36.0
2020-09-08 21:33:42 +00:00
if d.length() > 0:
if g.y_v < 5:
2020-09-08 21:33:42 +00:00
p.x -= utils.cap(d.x, 0.2)
p.z -= utils.cap(d.z, 0.2)
if len(g.path) > 1 and d.length() < 0.2:
2020-09-08 21:33:42 +00:00
# removes some jitter in walking
g.path.pop(0)
2020-09-08 21:33:42 +00:00
elif d.length() == 0:
g.path.pop(0)
2020-09-08 21:33:42 +00:00
if g.y_v or g.y_a:
p.y += g.y_v * utils.TICK
g.y_v += g.y_a * utils.TICK
2020-09-08 21:33:42 +00:00
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:
g.y_a = -36.0
2020-09-08 21:33:42 +00:00
else:
p.y = ceil(p.y)
g.y_v = 0
g.y_a = 0
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])
2020-09-08 21:33:42 +00:00
else:
look_at = None
if look_at:
look_at.x += 0.5
look_at.z += 0.5
look_at_d = p - look_at
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 - g.pitch
g.pitch += utils.cap(target_pitch_d, 10)
2020-09-08 21:33:42 +00:00
# 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 - g.yaw
2020-09-08 21:33:42 +00:00
target_yaw_d = (target_yaw_d + 180) % 360 - 180
g.yaw += utils.cap(target_yaw_d, 30)
2020-09-08 21:33:42 +00:00
else:
target_pitch_d = 0 - g.pitch
g.pitch += utils.cap(target_pitch_d, 10)
2020-09-08 21:33:42 +00:00
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))
2020-09-08 21:33:42 +00:00
g.connection.write_packet(packet, force=True)
2020-09-16 06:02:36 +00:00
g.game.tick()
2020-09-17 01:12:01 +00:00
g.job.tick()
2020-09-16 06:02:36 +00:00
2020-09-08 21:33:42 +00:00
def init(global_state):
g = global_state
g.time = 0
2020-09-08 21:33:42 +00:00
g.path = []
g.look_at = None
g.y_v = 0
g.y_a = 0
g.yaw = 360
g.pitch = 0
2020-09-08 21:33:42 +00:00
g.breaking = None
g.break_time = 0
2020-09-08 21:33:42 +00:00
g.dumping = None
g.dump_lock = False
2020-09-17 01:12:01 +00:00
g.job = jobs.JobStates(g)
2020-09-08 01:28:56 +00:00
def bot(global_state):
g = global_state
2020-09-09 01:52:50 +00:00
if not g.mcdata:
g.mcdata = DataManager('./mcdata')
2020-09-08 01:28:56 +00:00
if not g.connection:
auth_token = authentication.AuthenticationToken()
try:
auth_token.authenticate(USERNAME, PASSWORD)
except YggdrasilError as e:
print(e)
sys.exit()
print("Logged in as %s..." % auth_token.username)
2020-09-08 20:07:09 +00:00
g.connection = Connection(SERVER, 25565, auth_token=auth_token)
g.chunks = ChunksManager(g.mcdata)
g.connection.connect()
2020-09-09 01:52:50 +00:00
g.chunks.register(g.connection)
2020-09-08 23:51:27 +00:00
g.chat = ChatManager(g)
2020-09-16 06:02:36 +00:00
g.game = game.Game(g)
2020-09-16 20:09:14 +00:00
g.world = game.MCWorld(g)
2020-09-08 23:51:27 +00:00
2020-09-08 01:28:56 +00:00
try:
2020-09-08 21:33:42 +00:00
while not g.pos:
time.sleep(utils.TICK)
2020-09-08 21:33:42 +00:00
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(utils.TICK)
2020-09-08 21:33:42 +00:00
print('Chunks loaded.')
2020-09-08 01:28:56 +00:00
2020-09-08 21:33:42 +00:00
init(g)
2020-09-17 01:12:01 +00:00
print('Initialized.')
2020-09-08 01:28:56 +00:00
while g.running:
2020-09-08 21:33:42 +00:00
tick(g)
2020-09-08 01:28:56 +00:00
global last_tick
sleep_time = utils.TICK + last_tick - time.time()
2020-09-08 01:28:56 +00:00
if sleep_time < 0: sleep_time = 0
time.sleep(sleep_time)
last_tick = time.time()
finally:
print('Removing listeners...')
g.connection.packet_listeners = []
g.connection.early_packet_listeners = []
g.connection.outgoing_packet_listeners = []
g.connection.early_outgoing_packet_listeners = []
print('Bot module loaded.')