RasmusStagsted

Farm Lane

Jun 25th, 2018
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.08 KB | None | 0 0
  1. os.loadAPI("move")
  2. os.loadAPI("inv")
  3.  
  4. function split(inputstr, sep)
  5.         if sep == nil then
  6.                 sep = "%s"
  7.         end
  8.         local table = {}
  9.         i=1
  10.         for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
  11.                 table [i] = str
  12.                 i = i + 1
  13.         end
  14.         return table
  15. end
  16.  
  17. function plantTile()
  18.     if (turtle.placeDown()) then
  19.         rednet.open("right")
  20.         local t, d = turtle.inspectDown()
  21.         local mes = textutils.serialize({move.getLocalCoord(), d.name})
  22.         rednet.broadcast(mes, "farm")
  23.         rednet.close("right")
  24.         return true
  25.     else
  26.         return false
  27.     end
  28. end
  29.  
  30. function harvestAndPlant(plant)
  31.     local t, d = turtle.inspectDown()
  32.     if (t) then
  33.         local p = split(split(d.name, ":")[2], "_")
  34.         if (table.getn(p) == 2) then
  35.             p = p[1]
  36.         elseif (table.getn(p) == 3) then
  37.             p = p[1] .. "_" .. p[2]
  38.         elseif (table.getn(p) == 4) then
  39.             p = p[1] .. "_" .. p[2] .. "_" .. p[3]
  40.         end
  41.         d.name = split(d.name, ":")[1] .. ":" .. p .. "_seeds"
  42.         if (d.metadata == 7) then
  43.             inv.selectItem(d.name)
  44.             turtle.digDown()
  45.             if (type(plant) == "table") then
  46.                 for i = 1, table.getn(plant), 1 do
  47.                     if (inv.selectItem(plant[i])) then
  48.                         plantTile()
  49.                         return
  50.                     end
  51.                 end
  52.             else
  53.                 if (inv.selectItem(plant)) then
  54.                     plantTile()
  55.                     return
  56.                 end
  57.             end
  58.         end
  59.     else
  60.         if (type(plant) == "table") then
  61.             for i = 1, table.getn(plant), 1 do
  62.                 if (inv.selectItem(plant[i])) then
  63.                     while not plantTile() do
  64.                         inv.selectItem("minecraft:diamond_hoe")
  65.                         turtle.equipLeft()
  66.                         turtle.digDown()
  67.                         inv.selectItem("minecraft:diamond_pickaxe")
  68.                         turtle.equipLeft()
  69.                         inv.selectItem(plant[i])
  70.                     end
  71.                     return
  72.                 end
  73.             end
  74.         else
  75.             if (inv.selectItem(plant)) then
  76.                 print(21)
  77.                 while not plantTile() do
  78.                     inv.selectItem("minecraft:diamond_hoe")
  79.                     turtle.equipLeft()
  80.                     turtle.digDown()
  81.                     inv.selectItem("minecraft:diamond_pickaxe")
  82.                     turtle.equipLeft()
  83.                     inv.selectItem(plant[i])
  84.                 end
  85.                 return
  86.             end
  87.         end
  88.     end
  89. end
  90.  
  91. function farm(z, plant)
  92.     for i = 1, 2, 1 do
  93.         move.right()
  94.         move.forward()
  95.         move.right()
  96.         for j = 1, z - 1, 1 do
  97.             if i % 9 ~= 5 or j % 9 ~= 5 then
  98.                 print("h & p")
  99.                 harvestAndPlant(plant)
  100.             end
  101.             move.forward(1)
  102.         end
  103.         harvestAndPlant(plant)
  104.     end
  105. end
  106.  
  107. white_list = {
  108.     "minecraft:diamond_pickaxe",
  109.     "minecraft:diamond_hoe",
  110.     "mysticalagriculture:tier6_inferium_seeds",
  111.     "mysticalagriculture:tier5_inferium_seeds",
  112.     "mysticalagriculture:tier4_inferium_seeds",
  113.     "mysticalagriculture:tier3_inferium_seeds",
  114.     "mysticalagriculture:tier2_inferium_seeds",
  115.     "mysticalagriculture:tier1_inferium_seeds",
  116.     "mysticalagriculture:coal_seeds",
  117.     "mysticalagriculture:iron_seeds",
  118.     "mysticalagriculture:glowstone_seeds",
  119.     "mysticalagriculture:blaze_seeds"
  120. }
  121.  
  122. move.left()
  123. move.left()
  124. move.updatePosition()
  125. move.updatePosition()
  126. move.updatePosition()
  127. local coord = move.getGlobalCoord()
  128. move.globalGoto(180, 3, coord.z)
  129. move.look("west")
  130. if (not turtle.detect()) then
  131.     move.left()
  132.     move.forward()
  133.     move.right()
  134. end
  135. while true do
  136.     for i = 1, 16, 1 do
  137.         local in_list = false
  138.         for j = 1, table.getn(white_list), 1 do
  139.             if (turtle.getItemDetail(i)) then
  140.                 if (white_list[j] == turtle.getItemDetail(i).name) then
  141.                     in_list = true
  142.                 end
  143.             end
  144.         end
  145.         if in_list == false then
  146.             turtle.select(i)
  147.             turtle.drop()
  148.         end
  149.     end
  150.     rednet.open("right")
  151.     rednet.broadcast("idle", "idle farm")
  152.     print("idle")
  153.     senderId, message, protocol = rednet.receive("start farm")
  154.     rednet.broadcast("running", "farm")
  155.     rednet.close("right")
  156.     local data = textutils.unserialize(message)
  157.     print("Starting farming:")
  158.     print("\tLength: " .. data.length)
  159.     print("\tWidth: " .. data.width)
  160.     print("\tPriority:")
  161.     for i = 1, table.getn(data.priority), 1 do
  162.         print("\t\t" .. data.priority[i])
  163.     end
  164.     if (turtle.getFuelLevel() < (data.length * data.width + data.width - 2)) then
  165.         while (turtle.getFuelLevel() < (data.length * data.width + data.width - 2)) do
  166.             turtle.suckUp()
  167.             turtle.refuel()
  168.         end
  169.     end
  170.     farm(data.length, data.priority)
  171. end
Add Comment
Please, Sign In to add comment