SHARE
TWEET

turtle bucket filler manager

kssr3951 Jun 14th, 2014 191 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local function dropAllExceptSample(sampleSlot, dropFunc)
  2.   local keepCnt;
  3.   for i = 1, 16 do
  4.     turtle.select(i);
  5.     if true == turtle.compareTo(sampleSlot) then
  6.       if i == sampleSlot then
  7.         keepCnt = 1;
  8.       else
  9.         keepCnt = 0;
  10.       end
  11.  
  12.       local tmpRslt = dropFunc(math.max(0, turtle.getItemCount(i) - keepCnt));
  13.       if not tmpRslt then
  14.         return false;
  15.       end
  16.     end
  17.   end
  18.   return true;
  19. end
  20.  
  21. local function dropItems(sampleSlot, dropCnt, dropFunc)
  22.  
  23.   turtle.select(sampleSlot);
  24.   local norm = dropCnt;
  25.   local keepCnt;
  26.   for i = 1, 16 do
  27.     if norm <= 0 then
  28.       break;
  29.     end
  30.     local tgtCnt = 0;
  31.     if i == sampleSlot then
  32.       tgtCnt = math.max(0, turtle.getItemCount(i) - 1);
  33.     else
  34.       if turtle.compareTo(i) then
  35.         tgtCnt = turtle.getItemCount(i);
  36.       end
  37.     end
  38.     if 0 < tgtCnt then
  39.       local feed = math.min(norm, tgtCnt);
  40.       turtle.select(i);
  41.       dropFunc(feed);
  42.       turtle.select(sampleSlot);
  43.       norm = norm - feed;
  44.     end
  45.   end
  46. end
  47.  
  48. local function calcSpecificItemCount(sampleSlot)
  49.   local count = 0;
  50.   turtle.select(sampleSlot);
  51.   for i = 1, 16 do
  52.     if i == sampleSlot then
  53.       count = count + turtle.getItemCount(i);
  54.     elseif turtle.compareTo(i) then
  55.       count = count + turtle.getItemCount(i);
  56.     end
  57.   end
  58.   return count;
  59. end
  60.  
  61. local function calcCntParBooth(sampleSlot, boothCount)
  62.  
  63.   local count = calcSpecificItemCount(sampleSlot);
  64.   count = count - 1;
  65.  
  66.   local cntParBooth = math.floor(count / boothCount);
  67.   if 0 < count % boothCount then
  68.     cntParBooth = cntParBooth + 1;
  69.   end
  70.   return cntParBooth;
  71. end
  72.  
  73. local function retryLoop(func, sleepSec, waitMsg)
  74.   while true do
  75.     if true == func() then
  76.       break;
  77.     end
  78.     print(waitMsg);
  79.     sleep(sleepSec);
  80.   end
  81. end
  82.  
  83. -- turtle bucket filler manager
  84. -- slot1 : sample backet (filled : oil, lava, etc.)
  85. -- slot2 : sample backet (empty)
  86. local BOOTH_CNT = 4;
  87. local FILLED_BKT_SLOT = 1;
  88. local EMPTY_BKT_SLOT  = 2;
  89. local OTHER_SLOTS = { 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
  90.  
  91. local filledBucketsReleaseFunc   = turtle.dropUp;
  92. local emptyBucketsSupplementFunc = turtle.suck;
  93. local filledBucketsSuckFunc      = turtle.suckUp;
  94.  
  95. local function returnFilledBuckets()
  96.   return dropAllExceptSample(FILLED_BKT_SLOT, filledBucketsReleaseFunc);
  97. end
  98.  
  99. local function refuelAction(requiredFuelLv, filledBucketsSuckFunc)
  100.   while true do
  101.     for i = 1, 16 do
  102.       filledBucketsSuckFunc();
  103.     end
  104.     local cmpl = false;
  105.     for _, i in ipairs(OTHER_SLOTS) do
  106.       turtle.select(i);
  107.       turtle.refuel();
  108.       local tmp = turtle.getFuelLevel();
  109.       print("  fuel level -> " .. tmp);
  110.       if requiredFuelLv <= tmp then
  111.         print("  refueling was completed.");
  112.         cmpl = true;
  113.         break;
  114.       end
  115.     end
  116.     retryLoop(returnFilledBuckets, 5, "Inventory full? sleep(5)");
  117.     if cmpl then
  118.       break;
  119.     else
  120.       term.write("(sleep)");
  121.       for i = 1, 20 do
  122.         sleep(1);
  123.         term.write(".");
  124.       end
  125.     end
  126.   end
  127. end
  128.  
  129. print("-- status check");
  130. while true do
  131.   if  0 == turtle.getItemCount(FILLED_BKT_SLOT) or
  132.       0 == turtle.getItemCount(EMPTY_BKT_SLOT) then
  133.     print("Set items as follows.");
  134.     print("slot1 : filled bucket x 1");
  135.     print("        (lava, oil, etc.)");
  136.     print("slot2 : empty bucket  x 1");
  137.     print("Hit enter key when ready.");
  138.     read();
  139.   else
  140.     break;
  141.   end
  142. end
  143.  
  144. print("-- reset position");
  145. for i=1, BOOTH_CNT * 2 do
  146.   turtle.forward();
  147. end
  148.  
  149. local earnedFlg      = true;
  150. local distributedFlg = true;
  151. local turn = 0;
  152. while true do
  153.   term.clear();
  154.   term.setCursorPos(1, 1);
  155.   print("---------------------------------");
  156.   print("-- turtle bucket filler manager");
  157.   print("---------------------------------");
  158.   print("[turn] = " .. tostring(turn));
  159.   local fuelLevel = turtle.getFuelLevel();
  160.   print("[fuel level] = " .. fuelLevel);
  161.   local tmp = BOOTH_CNT - 1;
  162.   local requiredFuelLv = (math.min(tmp, 1) * 4 + tmp * 4) * (tmp / 2);
  163.   print("[required  ] = " .. requiredFuelLv);
  164.   if fuelLevel < requiredFuelLv then
  165.     print("<< out of fuel. >>");
  166.     refuelAction(requiredFuelLv, filledBucketsSuckFunc);
  167.   end
  168.   if not earnedFlg and not distributedFlg then
  169.     print("[skip] capture filled buckets");
  170.   else
  171.     earnedFlg = false;
  172.     print("capture filled buckets");
  173.     for i = 1, BOOTH_CNT do
  174.       print("  booth No. " .. tostring(i));
  175.       for j = 2, i do
  176.         turtle.back();
  177.         turtle.back();
  178.       end
  179.       for j = 1, 16 do
  180.         turtle.suckDown();
  181.       end
  182.       dropAllExceptSample(EMPTY_BKT_SLOT, turtle.dropDown);
  183.       for j = 2, i do
  184.         turtle.forward();
  185.         turtle.forward();
  186.       end
  187.       if 1 < calcSpecificItemCount(FILLED_BKT_SLOT) then
  188.         earnedFlg = true;
  189.       end
  190.       retryLoop(returnFilledBuckets, 5, "Inventory full? sleep(5)");
  191.     end
  192.     if earnedFlg then
  193.       print("got new filled buckets.");
  194.     else
  195.       print("no new filled buckets.");
  196.     end
  197.   end
  198.  
  199.   for j = 1, 16 do
  200.     emptyBucketsSupplementFunc();
  201.   end
  202.   if 1 < calcSpecificItemCount(EMPTY_BKT_SLOT) then
  203.     distributedFlg = true;
  204.   end
  205.   if not earnedFlg and not distributedFlg then
  206.     print("[skip] distribute empty buckets. (no empty buckets.)");
  207.   else
  208.     print("distribute empty buckets");
  209.     dropAllExceptSample(FILLED_BKT_SLOT, filledBucketsReleaseFunc);
  210.     local cntParBooth = calcCntParBooth(EMPTY_BKT_SLOT, BOOTH_CNT);
  211.     if 0 < cntParBooth then
  212.       distributedFlg = true;
  213.     else
  214.       distributedFlg = false;
  215.     end
  216.  
  217.     if 0 < cntParBooth then
  218.       local currentSlot = 2;
  219.       for i = 1, BOOTH_CNT do
  220.         print("  booth No. " .. tostring(i));
  221.         if 1 < i then
  222.           turtle.back();
  223.           turtle.back();
  224.         end
  225.         currentSlot = dropItems(EMPTY_BKT_SLOT, cntParBooth, turtle.dropDown);
  226.       end
  227.       for i = 1, BOOTH_CNT do
  228.         if 1 < i then
  229.           turtle.forward();
  230.           turtle.forward();
  231.         end
  232.       end
  233.     end
  234.   end
  235.   if not earnedFlg and not distributedFlg then
  236.     term.write("(sleep)");
  237.     for i = 1, 20 do
  238.       sleep(0.4);
  239.       term.write(".");
  240.     end
  241.   end
  242.   turn = turn + 1;
  243. end
RAW Paste Data
Challenge yourself this year...
Learn something new in 2017
Top