Advertisement
Guest User

startup.lua

a guest
Jul 19th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.11 KB | None | 0 0
  1. --rednet.open("right")
  2.  
  3. local tArgs = {...}
  4. local sendChannel = 12456
  5. if #tArgs>0 then
  6.   if tonumber(tArgs[1]) then
  7.     sendChannel = tonumber(tArgs[1])
  8.   end
  9. end
  10.  
  11. local blacklist = {
  12.   "minecraft:cobblestone"
  13. }
  14.  
  15. local blacklistLookup = {}
  16. for k,v in pairs(blacklist) do
  17.   blacklistLookup[v] = true
  18. end
  19.  
  20. shell.run("refuel all")
  21.  
  22. local facings = {}
  23. facings.north = 0
  24. facings.east = 1
  25. facings.south = 2
  26. facings.west = 3
  27. for k,v in pairs(facings) do
  28.     facings[v] = k
  29. end
  30.  
  31. local x,y,z = gps.locate()
  32. local assigned = nil
  33. local facing = 0
  34. local modem = peripheral.find("modem")
  35. modem.open(os.getComputerID())
  36.  
  37. local up
  38. local down
  39. local dig
  40. local tryup
  41. local trydown
  42. local digDown
  43. local digUp
  44.  
  45. local function receive(timeout)
  46.   local id = os.startTimer(timeout and timeout or 10)
  47.   while true do
  48.     local event = {os.pullEvent()}
  49.     if event[1] == "modem_message" and event[6] < 200 then
  50.       return event
  51.     elseif event[1] == "timer" then
  52.       return nil      
  53.     end
  54.   end
  55. end
  56.  
  57. local function send(message)
  58.   modem.transmit(sendChannel,os.getComputerID(),message)
  59. end
  60.  
  61. local function left()
  62.     turtle.turnLeft()
  63.     facing = facing+3
  64.     facing=facing%4
  65. end
  66.  
  67. local function right()
  68.     turtle.turnRight()
  69.     facing = facing+1
  70.     facing = facing%4
  71. end
  72.  
  73. up = function()
  74.     while (os.clock()*100)%10~=0 do
  75.         os.queueEvent("hi")
  76.         coroutine.yield()
  77.     end
  78.     while not turtle.up() do
  79.         digUp()
  80.         while (os.clock()*100)%10~=0 do
  81.             os.queueEvent("hi")
  82.             coroutine.yield()
  83.         end
  84.     end
  85.     y=y+1
  86. end
  87.  
  88. tryup = function()
  89.     while (os.clock()*100)%10~=0 do
  90.         os.queueEvent("hi")
  91.         coroutine.yield()
  92.     end
  93.     for i =1 ,3 do
  94.         if turtle.up() then
  95.             y=y+1
  96.             break
  97.         end
  98.         digUp()
  99.         while (os.clock()*100)%10~=0 do
  100.             os.queueEvent("hi")
  101.             coroutine.yield()
  102.         end
  103.     end
  104. end
  105.  
  106. down = function()
  107.     while (os.clock()*100)%10~=0 do
  108.         os.queueEvent("hi")
  109.         coroutine.yield()
  110.     end
  111.     while not turtle.down() do
  112.         digDown()
  113.         while (os.clock()*100)%10~=0 do
  114.             os.queueEvent("hi")
  115.             coroutine.yield()
  116.         end
  117.     end
  118.     y=y-1
  119. end
  120.  
  121. trydown = function()
  122.     while (os.clock()*100)%10~=0 do
  123.         os.queueEvent("hi")
  124.         coroutine.yield()
  125.     end
  126.     for i = 1 , 3 do
  127.         if turtle.down() then
  128.             y=y-1
  129.             break
  130.         end
  131.         digDown()
  132.         while (os.clock()*100)%10~=0 do
  133.             os.queueEvent("hi")
  134.             coroutine.yield()
  135.         end
  136.     end
  137. end
  138.  
  139. dig = function()
  140.     while (os.clock()*100)%10 == 0 do
  141.         os.queueEvent("hi")
  142.         coroutine.yield()
  143.     end
  144.     local ok,data = turtle.inspect()
  145.     if ok then
  146.         if data.name:lower():match("computercraft") then
  147.             if data.state and data.state.facing then
  148.                 if facings[(facing+2)%4] == data.state.facing then
  149.                     if math.random(1,2) == 2 then
  150.                         right()
  151.                     else
  152.                         left()
  153.                     end
  154.                 end
  155.             end
  156.             return false
  157.         else
  158.             return turtle.dig()
  159.         end
  160.     end
  161. end
  162.  
  163. digDown = function()
  164.     while (os.clock()*100)%10 == 0 do
  165.         os.queueEvent("hi")
  166.         coroutine.yield()
  167.     end
  168.     local ok,data = turtle.inspectDown()
  169.     if ok then
  170.         if data.name:lower():match("computercraft") then
  171.             return false
  172.         else
  173.             return turtle.digDown()
  174.         end
  175.     end
  176. end
  177.  
  178. digUp = function()
  179.     while (os.clock()*100)%10 == 0 do
  180.         os.queueEvent("hi")
  181.         coroutine.yield()
  182.     end
  183.     local ok,data = turtle.inspectUp()
  184.     if ok then
  185.         if data.name:lower():match("computercraft") then
  186.             return false
  187.         else
  188.             return turtle.digUp()
  189.         end
  190.     end
  191. end
  192.  
  193. while not turtle.forward() do
  194.     if math.random(1,2) == 2 then
  195.         up()
  196.     else
  197.         down()
  198.     end
  199.     dig()
  200.     x,y,z = gps.locate()
  201. end
  202. do
  203.     local x2,y2,z2 = gps.locate()
  204.     if z2<z then
  205.         facing = 0
  206.     elseif z2>z then
  207.         facing = 2
  208.     elseif x<x2 then
  209.         facing = 1
  210.     else
  211.         facing = 3
  212.     end
  213. end
  214. x,y,z = gps.locate()
  215. print(x,y,z,facing)
  216. local function forward()
  217.     while (os.clock()*100)%10~=0 do
  218.         os.queueEvent("hi")
  219.         coroutine.yield()
  220.     end
  221.     while not turtle.forward() do
  222.         dig()
  223.         while (os.clock()*100)%10~=0 do
  224.             os.queueEvent("hi")
  225.             coroutine.yield()
  226.         end
  227.     end
  228.     if facing == 0 then
  229.         z=z-1
  230.     elseif facing == 2 then
  231.         z=z+1
  232.     elseif facing == 1 then
  233.         x=x+1
  234.     else
  235.         x=x-1
  236.     end
  237. end
  238.  
  239.  
  240. local function tryforward()
  241.     while (os.clock()*100)%10~=0 do
  242.         os.queueEvent("hi")
  243.         coroutine.yield()
  244.     end
  245.     for i = 1, 3 do
  246.         if turtle.forward() then
  247.             if facing == 0 then
  248.                 z=z-1
  249.             elseif facing == 2 then
  250.                 z=z+1
  251.             elseif facing == 1 then
  252.                 x=x+1
  253.             else
  254.                 x=x-1
  255.             end
  256.             break
  257.         end
  258.         dig()
  259.         while (os.clock()*100)%10~=0 do
  260.             os.queueEvent("hi")
  261.             coroutine.yield()
  262.         end
  263.     end
  264.    
  265. end
  266.  
  267.  
  268. local function goto(coords)
  269.     local x3,y3,z3 = coords:match("(%-?%d+),(%-?%d+),(%-?%d+)")
  270.     x3 = tonumber(x3)
  271.     y3 = tonumber(y3)
  272.     z3 = tonumber(z3)
  273.     local px,py,pz
  274.     while y~=y3 or x3~=x or z3~=z do
  275.         px = x
  276.         py = y
  277.         pz = z
  278.         for i = 1, 20 do
  279.             if y~=y3 then
  280.                 if y3>y then
  281.                     tryup()
  282.                 else
  283.                     trydown()
  284.                 end
  285.             end
  286.         end
  287.         if x3~=x then
  288.             for i = 1, 10 do
  289.                 if x3>x then
  290.                     while facing~=1 do
  291.                         left()
  292.                     end
  293.                     tryforward()
  294.                 elseif x3<x then
  295.                     while facing~=3 do
  296.                         right()
  297.                     end
  298.                     tryforward()
  299.                 end
  300.             end
  301.         end
  302.        
  303.         if z3~=z then
  304.             for i = 1, 10 do
  305.                 if z3>z then
  306.                     while facing~= 2 do
  307.                         right()
  308.                     end
  309.                     tryforward()
  310.                 elseif z3<z then
  311.                     while facing~=0 do
  312.                         right()
  313.                     end
  314.                     tryforward()
  315.                 end
  316.             end
  317.         end
  318.         if x == px and y == py and z==pz then
  319.             print("gaveup"..coords)
  320.             send("gaveup"..coords)
  321.             assigned = nil
  322.             break
  323.         end
  324.     end
  325.     for i = 1, 16 do
  326.       if turtle.getItemCount(i) > 0 then
  327.         local item = turtle.getItemDetail(i)
  328.         if item and blacklistLookup[item.name] then
  329.           turtle.select(i)
  330.           turtle.drop()
  331.         end
  332.       end
  333.     end
  334.     turtle.select(1)
  335. end
  336.  
  337. while true do
  338.     if not assigned then
  339.         send("request"..x..","..y..","..z)
  340.         local message = receive(1)
  341.         local counter = 0
  342.         while not message or type(message[5]) ~="string" or message[5]:match("request") or message[5]:match("gaveup") do
  343.             message = receive(.05)
  344.             counter = counter+1
  345.             if counter == 30 then
  346.                 send("request"..x..","..y..","..z)
  347.                 counter = 0
  348.             end
  349.         end
  350.         if message then
  351.           print(message[5])
  352.         end
  353.         if type(message[5]) == "string" then
  354.             if not message[5]:match("request") and message[5]:match("a%-?%d+,%-?%d+,%-?%d+") then
  355.                 assigned = message[5]
  356.             end
  357.         end
  358.         print(assigned)
  359.     else
  360.         print("test1")
  361.         goto(assigned)
  362.         print("test2")
  363.         assigned = nil
  364.     end
  365.     sleep(1)
  366. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement