Advertisement
kssr3951

1ckFarmer2

Feb 28th, 2016
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.81 KB | None | 0 0
  1. dofile("turtleMoveApi")
  2. function randomPlaceDown()
  3.   local slots = { 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16 }
  4.   for i = 1, 16 do
  5.     local j = math.random(i, 16)
  6.     slots[i], slots[j] = slots[j], slots[i]
  7.   end
  8.   for i = 1, 16 do
  9.     if 0 < turtle.getItemCount(slots[i]) then
  10.       turtle.select(slots[i])
  11.       local rslt = turtle.placeDown()
  12.       if true == rslt then
  13.         break
  14.       end
  15.     end
  16.   end
  17. end
  18. function harvestAndRandomPlaceDown()
  19.   turtle.select(1)
  20.   e2()
  21.   e2()
  22.   randomPlaceDown()
  23. end
  24. function test()
  25.   if false == turtle.detectDown() then
  26.     e2()
  27.     randomPlaceDown()
  28.   end
  29.   -- https://github.com/kssr3951/NichePeripherals
  30.   -- metascanner
  31.   local w = peripheral.wrap("right")
  32.   local ret, code = w.scanDown()
  33.   if true == ret then
  34.     if code == "0Q7WXFt!R6Kgeo45WciNOw00" then
  35.       harvestAndRandomPlaceDown() -- rice
  36.     elseif code == "v6hCMTVVntvJvQOK8hr59M00" then
  37.       harvestAndRandomPlaceDown() -- bean
  38.     elseif code  == "tbBElJYibHzxRE5vJ1XJnM00" then
  39.       harvestAndRandomPlaceDown() -- wheat
  40.     elseif code == "IWUgHfKk7HrzAxaySVFdv000" then
  41.       harvestAndRandomPlaceDown() -- soybeans
  42.     elseif code == "SYzSqeOm3QkJhJEWx8cfy000" then
  43.       harvestAndRandomPlaceDown() -- tomato
  44.     elseif code == "pMn3UATm9wmDPwlrnH!V8000" then
  45.       harvestAndRandomPlaceDown() -- carrot
  46.     elseif code == "k3APXY8iRQ5UCAwhhnPYs000" then
  47.       harvestAndRandomPlaceDown() -- potato
  48.     end
  49.   end
  50. end
  51. local function getSameItemCount(slot)
  52.   local cnt = turtle.getItemCount(slot)
  53.   if 0 == cnt then
  54.     return 0
  55.   end
  56.   turtle.select(slot)
  57.   cnt = 0
  58.   local slotList = { }
  59.   for i = 1, 16 do
  60.     if true == turtle.compareTo(i) then
  61.       cnt = cnt + turtle.getItemCount(i)
  62.       table.insert(slotList, 1, i)
  63.     end
  64.   end
  65.   return cnt, slotList
  66. end
  67. function dumpHalf()
  68.   local  doneList = { }
  69.   for i = 1, 16 do
  70.     local cnt, slotList = getSameItemCount(i)
  71.     if 0 < cnt and nil == doneList[i] then
  72.       local remain = math.floor(cnt / 2)
  73.       for _, j in ipairs(slotList) do
  74.         doneList[j] = true
  75.       end
  76.       for _, j in ipairs(slotList) do
  77.         turtle.select(j)
  78.         local dropCnt = math.min(remain, turtle.getItemCount(j))
  79.         turtle.dropDown(dropCnt)
  80.         remain = remain - dropCnt
  81.         if remain <= 0 then
  82.           break
  83.         end
  84.       end
  85.     end
  86.   end
  87. end
  88. function refuel()
  89.   local refuelDone = false
  90.   for i = 1, 16 do
  91.     if 0 == turtle.getItemCount(i) then
  92.       turtle.select(i)
  93.       turtle.suckDown(15)
  94.       turtle.refuel(15)
  95.       refuelDone = true
  96.       break
  97.     end
  98.   end
  99.   if not refuelDone then
  100.     for i = 1, 16 do
  101.       vacuum(i)
  102.       for j = 1, 16 do
  103.         if 0 == turtle.getItemCount(j) then
  104.           turtle.select(j)
  105.           turtle.suckDown(15)
  106.           turtle.refuel(15)
  107.           return
  108.         end
  109.       end
  110.     end
  111.   end
  112. end
  113. function displayFuelLevel()
  114.   print("fuelLevel = " .. tostring(turtle.getFuelLevel()))
  115.   debug("fuelLevel = " .. tostring(turtle.getFuelLevel()))
  116. end
  117. registFunction("test", test)
  118. registFunction("dumpHalf", dumpHalf)
  119. registFunction("refuel", refuel)
  120. registFunction("displayFuelLevel", displayFuelLevel)
  121. -- ------------------------------------
  122. -- main
  123. -- ------------------------------------
  124. debug("----------------------------------------")
  125. debug("-- 1ckFarmer2")
  126. debug("----------------------------------------")
  127. registScript(
  128.   rep(256,
  129.     displayFuelLevel,
  130.     rep(4,
  131.       rep(16,
  132.         rep(15,test,f),
  133.         test,
  134.         breakLast,
  135.         swOddEven({r,f,r},{l,f,l})
  136.       ),
  137.       r,rep(7,f),r,rep(7,f),
  138.       anotherLast(
  139.         {d,dumpHalf,u},
  140.         {d,f,dumpHalf,b,refuel,u}
  141.       ),
  142.       r,rep(7,f),r,rep(7,f),
  143.       r,
  144.       rep(4,u),
  145.       rep(4,rep(40,l),d)
  146.     )
  147.   )
  148. )
  149. loadData()
  150. executeScript()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement