Advertisement
kssr3951

farmer

Apr 13th, 2015
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.48 KB | None | 0 0
  1. -- --------
  2. -- farmer
  3. -- --------
  4. local function showUsage()
  5.   print("slot1 : melon   x 1")
  6.   print("slot2 : pumpkin x 1")
  7.   print("slot3 : potato  x 1")
  8.   print("slot4 : carrot  x 1")
  9.   print("slot5 : wheat   x 1")
  10.   print("slot6 : seeds   x 1")
  11.   print("slot7 : poisonous potato x 1")
  12. end
  13. -- --------
  14. --
  15. -- --------
  16. local f = function() while not turtle.forward() do end end
  17. local b = function() while not turtle.back() do end end
  18. local l = turtle.turnLeft
  19. local r = turtle.turnRight
  20. local u = function() while not turtle.up() do end end
  21. local d = function() while not turtle.down() do end end
  22. local e2 = turtle.digDown -- excavate
  23. local p2 = turtle.placeDown
  24. local sel = turtle.select
  25. function vacuum(slotNo)
  26.   local result = false
  27.   turtle.select(slotNo)
  28.   for i = 1, 16 do
  29.     if slotNo ~= i and turtle.compareTo(i) and 1 < turtle.getItemCount(i) then
  30.       turtle.select(i)
  31.       turtle.transferTo(slotNo, turtle.getItemSpace(slotNo))
  32.       result = true
  33.       if 0 == turtle.getItemSpace(slotNo) then
  34.         break
  35.       end
  36.       turtle.select(slotNo)
  37.     end
  38.   end
  39.   turtle.select(slotNo)
  40.   return result
  41. end
  42. local function makeVacuumFunc(slotNo)
  43.   return function()
  44.       if 1 == turtle.getItemCount(slotNo) then
  45.         vacuum(slotNo)
  46.       end
  47.     end
  48. end
  49. local v3 = makeVacuumFunc(3)
  50. local v4 = makeVacuumFunc(4)
  51. local v6 = makeVacuumFunc(6)
  52. local function getTotal(slotNo)
  53.   local cnt = turtle.getItemCount(slotNo)
  54.   turtle.select(slotNo)
  55.   for i=1,16 do
  56.     if i ~= slotNo and turtle.compareTo(i) then
  57.       cnt = cnt + turtle.getItemCount(i)
  58.     end
  59.   end
  60.   return cnt
  61. end
  62. local function dropDownExceptK(slotNo, dropCnt, keepCnt)
  63.   local function slotAction(i, mustKeep, mustDrop, slotNo)
  64.     local cnt = turtle.getItemCount(i)
  65.     if mustKeep < cnt then
  66.       local dropAble = cnt - mustKeep
  67.       local nowDrop = math.min(dropAble, mustDrop)
  68.       mustKeep = 0
  69.       mustDrop = mustDrop - nowDrop
  70.       turtle.select(i)
  71.       turtle.dropDown(nowDrop)
  72.       turtle.select(slotNo)
  73.     else
  74.       mustKeep = mustKeep - cnt
  75.     end
  76.   end
  77.   turtle.select(slotNo)
  78.   local mustKeep = keepCnt
  79.   local mustDrop = dropCnt
  80.   for i=1,16 do
  81.     if i ~= slotNo and turtle.compareTo(i) then
  82.       slotAction(i, mustKeep, mustDrop, slotNo)
  83.     end
  84.   end
  85.   vacuum(slotNo)
  86.   slotAction(slotNo, mustKeep, mustDrop, slotNo)
  87. end
  88. local function dropDownExcept1(slotNo)
  89.   turtle.select(slotNo)
  90.   turtle.dropDown(turtle.getItemCount()-1)
  91.   for i=1,16 do
  92.     if i ~= slotNo and turtle.compareTo(i) then
  93.       turtle.select(i)
  94.       turtle.dropDown()
  95.       turtle.select(slotNo)
  96.     end
  97.   end
  98.   vacuum(slotNo)
  99. end
  100. local function dropDownHalf(slotNo)
  101.   turtle.select(slotNo)
  102.   local c = turtle.getItemCount(slotNo)
  103.   turtle.dropDown((c-1)/2)
  104.   for i=1,16 do
  105.     if i~=slotNo and turtle.compareTo(i) then
  106.       turtle.select(i)
  107.       c = turtle.getItemCount(i)
  108.       turtle.dropDown(c/2)
  109.       turtle.select(slotNo)
  110.     end
  111.   end
  112. end
  113. local function exec(...)
  114.   for i = 1, select("#", ...) do
  115.     local sbj = select(i, ...)
  116.     if "function" == type(sbj) then
  117.       sbj()
  118.     elseif "table" == type(sbj) then
  119.       for j, f in ipairs(sbj) do
  120.         f()
  121.       end
  122.     end
  123.   end
  124. end
  125. local function rep(num, ...)
  126.   local ret = {}
  127.   for i = 1, num do
  128.     for j = 1, select("#", ...) do
  129.       local sbj = select(j, ...)
  130.       if "function" == type(sbj) then
  131.         table.insert(ret, sbj)
  132.       elseif "table" == type(sbj) then
  133.         for j, f in ipairs(sbj) do
  134.           table.insert(ret, f)
  135.         end
  136.       end
  137.     end
  138.   end
  139.   return ret
  140. end
  141. -- --------
  142. -- main
  143. -- --------
  144. while true do
  145.   -- refuel
  146.   exec(u,f,f)
  147.   sel(16)
  148.   turtle.suckUp(3)
  149.   turtle.refuel()
  150.   exec(b)
  151.   -- meron
  152.   sel(1)
  153.   exec(rep(11,e2,f),e2)
  154.   sel(2) -- pumpkin
  155.   exec(r,f,f,f,r)
  156.   exec(rep(11,e2,f))
  157.   exec(e2)
  158.   -- potato
  159.   sel(3)
  160.   exec(r,r,rep(4,f),r,f)
  161.   local potato = {e2,v3,p2}
  162.   exec(
  163.     rep(2,
  164.       rep(7,potato,f),potato,l,f,l,
  165.       rep(7,potato,f),potato,r,f,r
  166.     )
  167.   )
  168.   sel(4)
  169.   local carrot = {e2,v4,p2}
  170.   exec(l)
  171.   exec(
  172.     rep(4,
  173.       rep(3,carrot,f),carrot,r,f,r,
  174.       rep(3,carrot,f),carrot,l,f,l
  175.     )
  176.   )
  177.   sel(6)
  178.   local wheat = {e2,v6,p2}
  179.   exec(f,f,f,r)
  180.   exec(
  181.     rep(4,
  182.       rep(3,wheat,f),wheat,r,f,r,
  183.       rep(3,wheat,f),wheat,l,f,l
  184.     )
  185.   )
  186.   exec(f,f,f,l,l)
  187.   exec(
  188.     rep(2,
  189.       rep(11,wheat,f),wheat,l,f,l,
  190.       rep(11,wheat,f),wheat,r,f,r
  191.     )
  192.   )
  193.   exec(l)
  194.   exec(
  195.     rep(4,
  196.       rep(2,wheat,f),wheat,r,f,r,
  197.       rep(2,wheat,f),wheat,l,f,l
  198.     )
  199.   )
  200.   exec(d,f,f,f,l)
  201.   -- melon
  202.   dropDownExcept1(1)
  203.   exec(f)
  204.   -- pumpkin
  205.   dropDownExcept1(2)
  206.   exec(f)
  207.   -- potato
  208.   dropDownExcept1(3)
  209.   exec(f)
  210.   -- carrot
  211.   dropDownHalf(4)
  212.   exec(f)
  213.   -- seeds
  214.   local tc = getTotal(6)
  215.   dropDownExceptK(6, math.max(0, (tc-16)/2), 32)
  216.   --dropDownHalf(6)
  217.   exec(f)
  218.   -- wheat
  219.   dropDownHalf(5)
  220.   exec(f)
  221.   -- seeds (animal food)
  222.   tc = getTotal(6)
  223.   dropDownExceptK(6, math.max(0, (tc-16)/2), 32)
  224.   --dropDownExcept1(6)
  225.   exec(f)
  226.   -- carrot (animal food)
  227.   dropDownExcept1(4)
  228.   exec(f)
  229.   -- wheat (animal food)
  230.   dropDownExcept1(5)
  231.   exec(l,l,rep(8,f),r,f,f)
  232.   -- poisonous potato
  233.   dropDownExcept1(7)
  234.   exec(f,l,rep(7,f),r)
  235.   print("fuelLv : " .. tostring(turtle.getFuelLevel()))
  236.   for i = 0,9 do
  237.     for j = 0,9 do
  238.       local cnt = 99 - (i * 10 + j)
  239.       term.write(tostring(cnt) .. " ")
  240.       os.sleep(12)
  241.     end
  242.     print()
  243.   end
  244.   print("go!!!")
  245. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement