Advertisement
Nezn

Rednet Turtle Forester [CC]

Jul 11th, 2015
429
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.38 KB | None | 0 0
  1. args = { ... }
  2. thisType = 0
  3. workerID = 0
  4. helperID = 0
  5. serverID = 0
  6. x, y, d = 0, 0, 0
  7. sapling = false
  8. protocol = "TurtleFProto"
  9. timer = 0
  10. refuel = false
  11. delay = 600
  12. working = false
  13.  
  14. function line(y, backcolor, text)
  15.         term.setCursorPos(1, y)
  16.         term.setBackgroundColor(backcolor)
  17.         term.clearLine()
  18.         term.write(text)
  19. end
  20.  
  21. function findModem()
  22.         sides = {"front","back","left","right","top","bottom"}
  23.         for i=1,6 do
  24.                 if peripheral.getType(sides[i]) == "modem" then
  25.                         rednet.open(sides[i])
  26.                         return
  27.                 end
  28.         end
  29.         print("Need wireless modem!")
  30.         error(r)
  31. end
  32.  
  33. function getName()
  34.     if thisType == 1 then
  35.         workerID = os.getComputerID()
  36.         return "worker"
  37.     elseif thisType == 2 then
  38.         serverID = os.getComputerID()
  39.         return "server"
  40.     elseif thisType == 3 then
  41.         helperID = os.getComputerID()
  42.         return "helper"
  43.     end
  44. end
  45.  
  46. function findFriends()
  47.     rednet.broadcast("zdarova_ya_"..getName(), protocol)
  48.     while true do
  49.             local event, sender, msg, prot = os.pullEvent("rednet_message")
  50.             if prot == protocol then
  51.             if string.find(msg, "zdarova") then
  52.                 rednet.send(sender, "privet_ya_"..getName(), protocol)
  53.             end
  54.             if string.find(msg, "ya_server") and thisType ~= 2 then
  55.                 serverID = sender
  56.                 print("Server ID: "..serverID)
  57.             elseif string.find(msg, "ya_worker") and thisType ~= 1 then
  58.                 workerID = sender
  59.                 print("Worker ID: "..workerID)
  60.             elseif string.find(msg, "ya_helper") and thisType ~= 3 then
  61.                 helperID = sender
  62.                 print("Helper ID: "..helperID)
  63.             end
  64.             if serverID ~= 0 and workerID ~= 0 and helperID ~= 0 then
  65.                 if thisType == 2 then
  66.                     print("Look at the monitor! Program has been started.")
  67.                         term.redirect(monitor)
  68.                         monitor.setTextScale(1)
  69.                     line(1, colors.black, "  RTF")
  70.                         line(2, colors.green, "FUEL")
  71.                         line(3, colors.green, "SAPLING")
  72.                         line(4, colors.gray, "START")
  73.                         line(5, colors.gray, "EXIT")
  74.                 end
  75.                 return
  76.             end
  77.         end
  78.     end
  79. end
  80.  
  81. function checkArgs()
  82.     if args[1] == nil then
  83.         print("Need args!")
  84.         error(r)
  85.     else
  86.         thisType = tonumber(args[1])
  87.     end
  88.     if args[4] == nil and thisType == 1 then
  89.         print("Need args!")
  90.         error(r)
  91.     elseif thisType == 1 then
  92.         x, y, d = tonumber(args[2]), tonumber(args[3]), tonumber(args[4])
  93.         if args[5] ~= nil then
  94.             protocol = args[5]
  95.         end
  96.     elseif thisType == 2 and args[2] ~= nil then
  97.         delay = tonumber(args[2])
  98.         if args[3] ~= nil then
  99.             protocol = args[3]
  100.         end
  101.     elseif thisType == 3 and args[2] ~= nil then
  102.         protocol = args[2]
  103.     end
  104. end
  105.  
  106. function move(w)
  107.     turtle.suck()
  108.     if turtle.getFuelLevel() == 0 then
  109.         turtle.refuel(1)
  110.     end
  111.     if w == "forward" then
  112.         while true do
  113.             local res = turtle.forward()
  114.             if res then
  115.                 break
  116.             else
  117.                 turtle.dig()
  118.                 turtle.select(16)
  119.                 turtle.equipLeft()
  120.                 turtle.attack()
  121.                 turtle.equipLeft()
  122.                 turtle.select(1)
  123.                 findModem()
  124.                 sleep(1)
  125.             end
  126.         end
  127.     elseif w == "up" then
  128.         turtle.up()
  129.     elseif w == "down" then
  130.         turtle.down()
  131.     end
  132. end
  133.  
  134. function findMonitor()
  135.         local e = peripheral.getNames()
  136.         for i=1,#e do
  137.                 if peripheral.getType(e[i]) == "monitor" then
  138.                         local m = peripheral.wrap(e[i])
  139.                         if m.isColor() then
  140.                 monitor = m
  141.                                 return
  142.                         end
  143.                 end
  144.         end
  145.         if monitor == nil then
  146.                 print("Need advanced monitor!")
  147.                 error(r)
  148.         end
  149. end
  150.  
  151. function dropItems()
  152.     checkSapling()
  153.     for i=1,15 do
  154.         if turtle.getItemCount(i) > 0 then
  155.             local d = turtle.getItemDetail(i)
  156.             turtle.select(i)
  157.             if d.name == "minecraft:log" or d.name == "minecraft:apple" then
  158.                 turtle.dropDown()
  159.             elseif d.name ~= "minecraft:coal" and d.name ~= "minecraft:sapling" then
  160.                 turtle.dropUp()
  161.             end
  162.         end
  163.     end
  164.     turtle.select(1)
  165. end
  166.  
  167. function checkSapling()
  168.     for i=1,15 do
  169.         if turtle.getItemCount(i) > 0 then
  170.             local d = turtle.getItemDetail(i)
  171.             if d.name == "minecraft:sapling" then
  172.                 turtle.select(i)
  173.                 if turtle.getItemCount(2) < 64 then
  174.                     turtle.transferTo(2, turtle.getItemSpace(2))
  175.                 end
  176.                 if turtle.getItemCount(i) > 0 then
  177.                     return
  178.                 end
  179.             end
  180.         end
  181.     end
  182. end
  183.  
  184. function work()
  185.     for i=1,x do
  186.         for j=1,y do
  187.             if sapling then
  188.                 for k=1,d-1 do
  189.                     move("forward")
  190.                 end
  191.                 sapling = false
  192.             else
  193.                 for k=1,d+1 do
  194.                     move("forward")
  195.                 end
  196.             end
  197.             local a, b = turtle.inspect()
  198.             while b.name == "minecraft:log" do
  199.                 turtle.dig()
  200.                 if turtle.inspectUp() then
  201.                     turtle.digUp()
  202.                 end
  203.                 move("up")
  204.                 a, b = turtle.inspect()
  205.             end
  206.             while not turtle.inspectDown() do
  207.                                 move("down")
  208.                         end
  209.             if not turtle.inspect() then
  210.                 turtle.select(2)
  211.                 turtle.place()
  212.                 turtle.select(1)
  213.             end
  214.             local a, b = turtle.inspect()
  215.             if b.name == "minecraft:sapling" then
  216.                 turtle.turnLeft()
  217.                 move("forward")
  218.                 turtle.turnRight()
  219.                 move("forward")
  220.                 move("forward")
  221.                 turtle.turnRight()
  222.                 move("forward")
  223.                 turtle.turnLeft()
  224.                 sapling = true
  225.             end
  226.         end
  227.         if sapling then
  228.             for k=1,d+1 do
  229.                 move("forward")
  230.             end
  231.             sapling = false
  232.         else
  233.             for k=1,d+3 do
  234.                 move("forward")
  235.             end
  236.         end
  237.         if i == x then
  238.             if math.fmod(x,2) == 0 then
  239.                 turtle.turnRight()
  240.                 for k=1,(d+1)*(x-1) do
  241.                     move("forward")
  242.                 end
  243.                 turtle.turnRight()
  244.                 dropItems()
  245.                 rednet.send(serverID, "finish", protocol)
  246.             else
  247.                 turtle.turnRight()
  248.                 move("forward")
  249.                 turtle.turnRight()
  250.                 for l=1,(y+1)*(d+1)+2 do
  251.                     move("forward")
  252.                 end
  253.                 turtle.turnRight()
  254.                 for l=1,(d+1)*(x-1)+1 do
  255.                     move("forward")
  256.                 end
  257.                 turtle.turnRight()
  258.                 dropItems()
  259.                 rednet.send(serverID, "finish", protocol)
  260.             end
  261.         else
  262.             if math.fmod(i,2) == 1 then
  263.                 turtle.turnRight()
  264.             else
  265.                 turtle.turnLeft()
  266.             end
  267.             for k=1,d+1 do
  268.                 move("forward")
  269.             end
  270.             if math.fmod(i,2) == 1 then
  271.                 turtle.turnRight()
  272.             else
  273.                 turtle.turnLeft()
  274.             end
  275.         end
  276.     end
  277. end
  278.  
  279. function workerEvents(event, sender, msg, prot)
  280.     if event == "rednet_message" then
  281.         if msg == "start" then
  282.             if turtle.getFuelLevel() < 800 and turtle.getItemCount(1) == 0 then
  283.                 rednet.send(serverID, "needbackup", protocol)
  284.             elseif turtle.getItemCount(2) < x*y then
  285.                 rednet.send(serverID, "needbackup", protocol)
  286.             else
  287.                 work()
  288.             end
  289.         elseif msg == "catch" then
  290.             if turtle.getItemCount(1) < 10 then
  291.                 rednet.send(helperID, "coal", protocol)
  292.                 return
  293.             end
  294.             if turtle.getItemCount(2) < x*y then
  295.                 rednet.send(helperID, "sapling", protocol)
  296.                 return
  297.             end
  298.             rednet.send(helperID, "ok", protocol)
  299.         elseif msg == "disconnect" then
  300.             print("Server has disconnected.")
  301.             error(r)
  302.         end
  303.     end
  304. end
  305.  
  306. function getCount()
  307.     local a, b = 0, 0
  308.     for i=1,16 do
  309.         if turtle.getItemCount(i) > 0 then
  310.             local k = turtle.getItemDetail(i)
  311.             if k.name == "minecraft:sapling" then
  312.                 a = a + turtle.getItemCount(i)
  313.             elseif k.name == "minecraft:coal" then
  314.                 b = b + turtle.getItemCount(i)
  315.             end
  316.         end
  317.     end
  318.     return a, b
  319. end
  320.  
  321. function helperEvents(event, sender, msg, prot)
  322.     if event == "rednet_message" then
  323.         if msg == "refuel" and sender == serverID then
  324.             local a, b = getCount()
  325.             if a < 64 or b < 64 then
  326.                 rednet.send(serverID, "needbackup", protocol)
  327.             else
  328.                 refuel = true
  329.                 rednet.send(workerID, "catch", protocol)
  330.             end
  331.         elseif msg == "coal" or msg == "sapling" then
  332.             if sender == workerID and refuel then
  333.                 for i=1,16 do
  334.                     local k = turtle.getItemDetail(i)
  335.                     if turtle.getItemCount(i) > 0 and k.name == "minecraft:"..msg then
  336.                         turtle.select(i)
  337.                         turtle.drop(1)
  338.                         rednet.send(workerID, "catch", protocol)
  339.                         return
  340.                     end
  341.             end
  342.             end
  343.         elseif msg == "ok" and sender == workerID then
  344.             rednet.send(serverID, "ok", protocol)
  345.             refuel = false
  346.         elseif msg == "disconnect" then
  347.             print("Server has disconnected.")
  348.             error(r)
  349.         end
  350.     end
  351. end
  352.  
  353. function sendStart()
  354.     line(1, colors.yellow, "  RTF")
  355.         line(2, colors.green, "FUEL")
  356.         line(3, colors.green, "SAPLING")
  357.         line(4, colors.blue, "STOP")
  358.         rednet.send(workerID, "start", protocol)
  359. end
  360.  
  361. function serverEvents(event, arg1, arg2, arg3)
  362.     if event == "monitor_touch" then
  363.             if arg3 == 4 then
  364.             if working then
  365.                 working = false
  366.                 os.cancelTimer(timer)
  367.                 line(1, colors.black, "  RTF")
  368.                 line(4, colors.blue, "START")
  369.             elseif not refuel then
  370.                 working = true
  371.                 sendStart()
  372.             end
  373.         elseif arg3 == 5 then
  374.             rednet.send(workerID, "disconnect", protocol)
  375.             rednet.send(helperID, "disconnect", protocol)
  376.             term.setBackgroundColor(colors.black)
  377.             term.clear()
  378.             term.setCursorPos(1,1)
  379.             error(r)
  380.                 end
  381.     elseif event == "rednet_message" and arg3 == protocol then
  382.             if arg2 == "needbackup" and arg1 == workerID then
  383.             working = false
  384.             rednet.send(helperID, "refuel", protocol)
  385.             line(1, colors.black, "  RTF")
  386.                 line(2, colors.blue, "FUEL")
  387.                 line(3, colors.blue, "SAPLING")
  388.                 line(4, colors.gray, "START")
  389.             refuel = true
  390.         elseif arg2 == "needbackup" and arg1 == helperID then
  391.             line(4, colors.gray, "START")
  392.             line(2, colors.red, "REFUEL")
  393.             line(3, colors.red, "SAPLING")
  394.             line(1, colors.black, "  RTF")
  395.             refuel = false
  396.         elseif arg2 == "ok" and arg1 == helperID then
  397.             refuel = false
  398.             working = true
  399.             sendStart()
  400.         elseif arg2 == "finish" then
  401.             if working then
  402.                 timer = os.startTimer(delay)
  403.             else
  404.                 line(1, colors.black, "  RTF")
  405.                 line(4, colors.gray, "START")
  406.             end
  407.         end
  408.     elseif event == "timer" and arg1 == timer then
  409.         sendStart()
  410.         os.cancelTimer(timer)
  411.     end
  412. end
  413.  
  414. function start()
  415.     term.setCursorPos(1,1)
  416.         term.clear()
  417.     checkArgs()
  418.     print("I am "..getName())
  419.     findModem()
  420.     if thisType == 2 then
  421.         findMonitor()
  422.     end
  423.     print("Searching...")
  424.     findFriends()
  425.     while true do
  426.         local event, sender, msg, prot = os.pullEvent()
  427.         if thisType == 1 then
  428.             workerEvents(event, sender, msg, prot)
  429.         elseif thisType == 2 then
  430.             serverEvents(event, sender, msg, prot)
  431.         elseif thisType == 3 then
  432.             helperEvents(event, sender, msg, prot)
  433.         end
  434.     end
  435. end
  436.  
  437. start() --Start application
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement