Advertisement
joebodo

hijackpoc.lua

Jan 12th, 2019
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.52 KB | None | 0 0
  1. local Event  = require('event')
  2. local GPS    = require('gps')
  3. local Point  = require('point')
  4. local Socket = require('socket')
  5. local Util   = require('util')
  6.  
  7. local os = _G.os
  8.  
  9. local hut1 = {
  10.     { '     ', '     ', '     ', '     ', '  6  ' },
  11.     { '33333', '14441', '14 41', '14 41', '11111' },
  12.     { '34443', '4   4', '4   4', '4   4', '11111' },
  13.     { '34443', '4   4', '5   5', '4   4', '11111' },
  14.     { '34443', '4   4', '4   4', '4   4', '11111' },
  15.     { '33333', '14441', '14541', '14441', '11111' },
  16. }
  17.  
  18. local hut2 = {
  19.     { '     ', '     ', '     ', '     ', '     ', '  6  ' },
  20.     { '77777', '33333', '14441', '14 41', '14 41', '11111' },
  21.     { '7   7', '34443', '4   4', '4   4', '4   4', '11111' },
  22.     { '7   7', '39443', '4   4', '5   5', '4   4', '11111' },
  23.     { '7   7', '38443', '48  4', '48  4', '48  4', '11111' },
  24.     { '77777', '33333', '14441', '14541', '14441', '11111' },
  25. }
  26.  
  27. local blocks = {
  28.     [ '1' ] = { 'minecraft:cobblestone'   },
  29.     [ '2' ] = { 'minecraft:oak_stairs',   },
  30.     [ '3' ] = { 'minecraft:log',          Point.DOWN },
  31.     [ '4' ] = { 'minecraft:planks'        },
  32.     [ '5' ] = { 'minecraft:glass_pane'    },
  33.     [ '6' ] = { 'minecraft:stone_stairs', Point.DOWN, Point.EAST, },
  34.     [ '7' ] = { 'minecraft:fence',        nil,        nil,        Point.DOWN },
  35.     [ '8' ] = { 'minecraft:ladder',       Point.EAST, nil,        Point.EAST },
  36.     [ '9' ] = { 'minecraft:planks',       nil,        nil,        Point.EAST },
  37. }
  38.  
  39. local spt = GPS.getPoint() or error('GPS failure')
  40. local house = { }
  41.  
  42. local template = hut2
  43. -- construct a simple schematic from the template
  44. for x,plane in pairs(template) do
  45.     for ry,row in pairs(plane) do
  46.         local y = #plane - ry + 1
  47.         for z = 1, #row do
  48.             local b = row:sub(z, z)
  49.             if blocks[b] then
  50.                 table.insert(house, {
  51.                     x = x + spt.x,
  52.                     y = y + spt.y - 1,
  53.                     z = z + spt.z,
  54.                     name       = blocks[b][1],
  55.                     direction  = blocks[b][2], -- east, down, up, etc
  56.                     heading    = blocks[b][3], -- n,s,e,w
  57.                     dependency = blocks[b][4],
  58.                 })
  59.             end
  60.         end
  61.     end
  62. end
  63.  
  64. for _,b in pairs(house) do
  65.     if b.dependency then
  66.         local hi = Point.headings[b.dependency]
  67.         local x,y,z = b.x + hi.xd, b.y + hi.yd, b.z + hi.zd
  68.         for _,b2 in pairs(house) do
  69.             if b2.x == x and b2.y == y and b2.z == z then
  70.                 b.dependency = b2
  71.                 break
  72.             end
  73.         end
  74.     end
  75. end
  76.  
  77. -- define a box the turtles should not path find out of
  78. local height, width, length = #template[1], #template[1][1], #template
  79. local box = {
  80.     x = spt.x - 1,
  81.     y = spt.y,
  82.     z = spt.z - 1,
  83.     ex = spt.x + length + 1,
  84.     ey = spt.y + height + 1,
  85.     ez = spt.z + width + 1,
  86. }
  87.  
  88. local function hijackTurtle(remoteId)
  89.     local socket, msg = Socket.connect(remoteId, 188)
  90.  
  91.     if not socket then
  92.         error(msg)
  93.     end
  94.  
  95.     socket:write('turtle')
  96.     local methods = socket:read()
  97.  
  98.     local hijack = { }
  99.     for _,method in pairs(methods) do
  100.         hijack[method] = function(...)
  101.             socket:write({ fn = method, args = { ... } })
  102.             local resp = socket:read()
  103.             if not resp then
  104.                 error('timed out')
  105.             end
  106.             return table.unpack(resp)
  107.         end
  108.     end
  109.  
  110.     return hijack
  111. end
  112.  
  113. local args = { ... }
  114. local turtleCount = #args
  115. local turtles = { }
  116.  
  117. for _, id in pairs(args) do -- secondary channel
  118.     table.insert(turtles, hijackTurtle(tonumber(id)))
  119. end
  120.  
  121. for _, id in pairs(args) do
  122.     Event.addRoutine(function()
  123.         --os.sleep(1)
  124.         local turtle = hijackTurtle(tonumber(id))
  125.         os.sleep(1)
  126.         turtle.resetState()
  127.         turtle.enableGPS()
  128.         local lastPoint = turtle.getPoint()
  129.         turtle.setPersistent(true)
  130.         turtle.pathfind(Point.closestPointInBox(lastPoint, box))
  131.         turtle.setPathingBox(box)
  132.  
  133.         while true do
  134.             -- get nearest point
  135.             local pt
  136.             local depends = { }
  137.             while true do
  138.                 pt = Point.closest(turtle.getPoint(), house)
  139.                 if not pt then
  140.                     break
  141.                 end
  142.                 Util.removeByValue(house, pt)
  143.  
  144.                 if pt.dependency and not pt.dependency.placed then
  145.                     table.insert(depends, pt)
  146.                 else
  147.                     break
  148.                 end
  149.             end
  150.             if not pt then
  151.                 break
  152.             end
  153.             for _,b in pairs(depends) do
  154.                 table.insert(house, b)
  155.             end
  156.  
  157.             -- try a couple times to place block in case a turtle is in the way
  158.             for _ = 1, 3 do
  159.                 if turtle.placeAt(pt, pt.name) then
  160.                     pt.placed = true
  161.                     -- notify all turtles about this block
  162.                     for _, t in pairs(turtles) do
  163.                         t.addWorldBlock(pt)
  164.                     end
  165.                     break
  166.                 end
  167.                 os.sleep(.5)
  168.             end
  169.         end
  170.  
  171.         turtle.setPathingBox()
  172.         turtle.pathfind(lastPoint)
  173.         if turtleCount == 1 then
  174.             Event.exitPullEvents()
  175.         end
  176.         turtleCount = turtleCount - 1
  177.     end)
  178. end
  179.  
  180. print('pulling')
  181. Event.pullEvents()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement