Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local Event = require('event')
- local GPS = require('gps')
- local Point = require('point')
- local Socket = require('socket')
- local Util = require('util')
- local os = _G.os
- local hut1 = {
- { ' ', ' ', ' ', ' ', ' 6 ' },
- { '33333', '14441', '14 41', '14 41', '11111' },
- { '34443', '4 4', '4 4', '4 4', '11111' },
- { '34443', '4 4', '5 5', '4 4', '11111' },
- { '34443', '4 4', '4 4', '4 4', '11111' },
- { '33333', '14441', '14541', '14441', '11111' },
- }
- local hut2 = {
- { ' ', ' ', ' ', ' ', ' ', ' 6 ' },
- { '77777', '33333', '14441', '14 41', '14 41', '11111' },
- { '7 7', '34443', '4 4', '4 4', '4 4', '11111' },
- { '7 7', '39443', '4 4', '5 5', '4 4', '11111' },
- { '7 7', '38443', '48 4', '48 4', '48 4', '11111' },
- { '77777', '33333', '14441', '14541', '14441', '11111' },
- }
- local blocks = {
- [ '1' ] = { 'minecraft:cobblestone' },
- [ '2' ] = { 'minecraft:oak_stairs', },
- [ '3' ] = { 'minecraft:log', Point.DOWN },
- [ '4' ] = { 'minecraft:planks' },
- [ '5' ] = { 'minecraft:glass_pane' },
- [ '6' ] = { 'minecraft:stone_stairs', Point.DOWN, Point.EAST, },
- [ '7' ] = { 'minecraft:fence', nil, nil, Point.DOWN },
- [ '8' ] = { 'minecraft:ladder', Point.EAST, nil, Point.EAST },
- [ '9' ] = { 'minecraft:planks', nil, nil, Point.EAST },
- }
- local spt = GPS.getPoint() or error('GPS failure')
- local house = { }
- local template = hut2
- -- construct a simple schematic from the template
- for x,plane in pairs(template) do
- for ry,row in pairs(plane) do
- local y = #plane - ry + 1
- for z = 1, #row do
- local b = row:sub(z, z)
- if blocks[b] then
- table.insert(house, {
- x = x + spt.x,
- y = y + spt.y - 1,
- z = z + spt.z,
- name = blocks[b][1],
- direction = blocks[b][2], -- east, down, up, etc
- heading = blocks[b][3], -- n,s,e,w
- dependency = blocks[b][4],
- })
- end
- end
- end
- end
- for _,b in pairs(house) do
- if b.dependency then
- local hi = Point.headings[b.dependency]
- local x,y,z = b.x + hi.xd, b.y + hi.yd, b.z + hi.zd
- for _,b2 in pairs(house) do
- if b2.x == x and b2.y == y and b2.z == z then
- b.dependency = b2
- break
- end
- end
- end
- end
- -- define a box the turtles should not path find out of
- local height, width, length = #template[1], #template[1][1], #template
- local box = {
- x = spt.x - 1,
- y = spt.y,
- z = spt.z - 1,
- ex = spt.x + length + 1,
- ey = spt.y + height + 1,
- ez = spt.z + width + 1,
- }
- local function hijackTurtle(remoteId)
- local socket, msg = Socket.connect(remoteId, 188)
- if not socket then
- error(msg)
- end
- socket:write('turtle')
- local methods = socket:read()
- local hijack = { }
- for _,method in pairs(methods) do
- hijack[method] = function(...)
- socket:write({ fn = method, args = { ... } })
- local resp = socket:read()
- if not resp then
- error('timed out')
- end
- return table.unpack(resp)
- end
- end
- return hijack
- end
- local args = { ... }
- local turtleCount = #args
- local turtles = { }
- for _, id in pairs(args) do -- secondary channel
- table.insert(turtles, hijackTurtle(tonumber(id)))
- end
- for _, id in pairs(args) do
- Event.addRoutine(function()
- --os.sleep(1)
- local turtle = hijackTurtle(tonumber(id))
- os.sleep(1)
- turtle.resetState()
- turtle.enableGPS()
- local lastPoint = turtle.getPoint()
- turtle.setPersistent(true)
- turtle.pathfind(Point.closestPointInBox(lastPoint, box))
- turtle.setPathingBox(box)
- while true do
- -- get nearest point
- local pt
- local depends = { }
- while true do
- pt = Point.closest(turtle.getPoint(), house)
- if not pt then
- break
- end
- Util.removeByValue(house, pt)
- if pt.dependency and not pt.dependency.placed then
- table.insert(depends, pt)
- else
- break
- end
- end
- if not pt then
- break
- end
- for _,b in pairs(depends) do
- table.insert(house, b)
- end
- -- try a couple times to place block in case a turtle is in the way
- for _ = 1, 3 do
- if turtle.placeAt(pt, pt.name) then
- pt.placed = true
- -- notify all turtles about this block
- for _, t in pairs(turtles) do
- t.addWorldBlock(pt)
- end
- break
- end
- os.sleep(.5)
- end
- end
- turtle.setPathingBox()
- turtle.pathfind(lastPoint)
- if turtleCount == 1 then
- Event.exitPullEvents()
- end
- turtleCount = turtleCount - 1
- end)
- end
- print('pulling')
- Event.pullEvents()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement