def __init__(self, address="localhost", port=4711, parallelism=200): self.address = address self.port = port self.parallelism = parallelism self.connections = [ Connection(address, port) for i in range(parallelism) ]
def get_blocks_in_parallel(c1, c2, degree=35): """get a cuboid of block data parms: c1, c2: the corners of the cuboid degree: the degree of parallelism (number of sockets) returns: map from mcpi.vec3.Vec3 to mcpi.block.Block """ # Set up the work queue c1.x, c2.x = sorted((c1.x, c2.x)) c1.y, c2.y = sorted((c1.y, c2.y)) c1.z, c2.z = sorted((c1.z, c2.z)) workq = Queue.Queue() for x in range(c1.x, c2.x + 1): for y in range(c1.y, c2.y + 1): for z in range(c1.z, c2.z + 1): workq.put((x, y, z)) print "Getting data for %d blocks" % workq.qsize() # Create socket connections, if needed # TO DO: Bad! Assumes degree is a constant # To do: close the socket global connections if not connections: connections = [Connection("localhost", 4711) for i in range(0, degree)] # Create worker threads def worker_fn(connection, workq, outq): try: while True: pos = workq.get(False) # print "working", pos[0], pos[1], pos[2] connection.send("world.getBlockWithData", pos[0], pos[1], pos[2]) ans = connection.receive() blockid, blockdata = map(int, ans.split(",")) outq.put((pos, (blockid, blockdata))) except Queue.Empty: pass outq = Queue.Queue() workers = [] for w in range(degree): t = threading.Thread(target=worker_fn, args=(connections[w], workq, outq)) t.start() workers.append(t) # Wait for workers to finish for w in workers: # print "waiting for", w.name w.join() # Collect results answer = {} while not outq.empty(): pos, block = outq.get() answer[pos] = block return answer
def get_blocks_using_multiple_threads(c1, c2, thread_count=100): """get a cuboid of Minecraft block data Purpose: Use multiple threads to get Minecraft block data faster than by calling Minecraft.getBlockWithData() in a loop. parms: c1, c2: the corners of the cuboid, where each corner is a mcpi.vec3.Vec3 thread_count: the number of threads (also number of connections) returns: dict from mcpi.vec3.Vec3 to mcpi.block.Block """ # Set up the work queue c1.x, c2.x = sorted((c1.x, c2.x)) c1.y, c2.y = sorted((c1.y, c2.y)) c1.z, c2.z = sorted((c1.z, c2.z)) workq = Queue.Queue() for x in range(c1.x, c2.x): for y in range(c1.y, c2.y): for z in range(c1.z, c2.z): workq.put((x,y,z)) print("Getting data for %d blocks" % workq.qsize()) # Create connections (with one socket for each) for each thread # To do: close the socket connections = [Connection("localhost", 4711) for i in range(0,thread_count)] # The worker threads def worker_fn(connection, workq, outq): try: while True: pos = workq.get(False) #print("working " + str(pos[0]) + str(pos[1]) + str(pos[2])) connection.send("world.getBlockWithData", pos[0], pos[1], pos[2]) ans = connection.receive() #print("Got "+ans) blockid, blockdata = map(int, ans.split(",")) outq.put((pos, (blockid, blockdata))) except Queue.Empty: pass # Create worker threads outq = Queue.Queue() workers = [] for w in range(thread_count): t = threading.Thread(target = worker_fn, args = (connections[w], workq, outq)) t.start() workers.append(t) # Wait for workers to finish for w in workers: # print("waiting for", w.name) w.join() # Collect results answer = {} while not outq.empty(): pos, block = outq.get() answer[pos] = block[0] # only collect blockId return answer
def create(address="localhost", port=4711): return Minecraft(Connection(address, port))
def conn(): with mock.patch('socket.socket', spec=socket.socket): return Connection('localhost', 8000)
z = ppos.z xv = 0.005 yv = 0.1 zv = 0.02 while yv > 0: mc.player.setPos(x, y, z) x += xv y += yv z += zv yv -= 0.0001 time.sleep(0.001) # Try stacking up multiple getBlocks: if True: """This code is weird. Delete it!""" connection = Connection("localhost", 4711) for x in range(-20, 20): for z in range(-20, 20): connection.send("world.getBlockWithData", x, 2, z) print connection.receive() print connection.receive() # How big is the world? if False: corner1 = Vec3(-200, 0, 0) corner2 = Vec3(200, 0, 0) degree = 150 xaxis = get_blocks_in_parallel(corner1, corner2, degree) xmin = xmax = 0 for x in range(200):