Ledger Nano X - The secure hardware wallet
SHARE
TWEET

autoRefuel

kssr3951 Jun 11th, 2014 228 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local ARMSG__IM_NOT_LABELED          = 1;
  2. local ARMSG__GIVE_ME_FUEL_MORE       = 2;
  3. local ARMSG__REFUELING_WAS_COMPLETED = 3;
  4.  
  5. local floppySource =
  6. [[
  7. local function setMsg(armsg)
  8.   rs.setAnalogOutput("bottom", armsg);
  9. end
  10.  
  11. local function freeze()
  12.   while true do
  13.     sleep(60);
  14.   end
  15. end
  16.  
  17. turtle.turnLeft();
  18. turtle.turnLeft();
  19.  
  20. if nil == os.getComputerLabel() then
  21.   print("I'm not labeled turtle.");
  22.   setMsg(ARMSG__IM_NOT_LABELED);
  23.   freeze();
  24. end
  25.  
  26. local lastSlot = -1;
  27. local selSlot;
  28. while true do
  29.   sleep(0);
  30.   if ARG_FUEL_LEVEL <= turtle.getFuelLevel() then
  31.     print("Refueling was completed.");
  32.     setMsg(ARMSG__REFUELING_WAS_COMPLETED);
  33.     freeze();
  34.   end
  35.  
  36.   print("Give me fuel more.");
  37.   setMsg(ARMSG__GIVE_ME_FUEL_MORE);
  38.  
  39.   selSlot = 1;
  40.   if -1 ~= lastSlot and 1 <= turtle.getItemCount(lastSlot) then
  41.     selSlot = lastSlot;
  42.   else
  43.     for i = 1, 16 do
  44.       if 1 <= turtle.getItemCount(i) then
  45.         selSlot = i;
  46.         break;
  47.       end
  48.     end
  49.   end
  50.  
  51.   local itemCnt = turtle.getItemCount(selSlot);
  52.   if 1 <= itemCnt then
  53.     turtle.select(selSlot);
  54.     local beforeLv = turtle.getFuelLevel();
  55.     local rslt     = turtle.refuel(1);
  56.     local afterLv  = turtle.getFuelLevel();
  57.     local unitFLv  = afterLv - beforeLv;
  58.  
  59.     print("[status] : ARG_LV  = " .. ARG_FUEL_LEVEL);
  60.     print("           afterLv = " .. afterLv);
  61.  
  62.     if not rslt then
  63.       print("This is not fuel.");
  64.       turtle.drop();
  65.     else
  66.       if 1 == turtle.getItemCount(selSlot) and 1 == itemCnt then
  67.         -- Probably empty bucket.
  68.         turtle.drop();
  69.       else
  70.         itemCnt = turtle.getItemCount(selSlot)
  71.         if afterLv + unitFlv * itemCnt < ARG_FUEL_LEVEL then
  72.           turtle.refuel(itemCnt);
  73.         else
  74.           local useCnt = (ARG_FUEL_LEVEL - afterLv) / unitFlv + 1;
  75.           turtle.refuel(useCnt);
  76.         end
  77.       end
  78.     end
  79.   end
  80. end
  81. ]];
  82.  
  83. local SLOT_CHEST  = 1;
  84. local SLOT_FLOPPY = 2;
  85. local SLOT_DRIVE  = 3;
  86.  
  87. print("slot1 : chest       x 2");
  88. print("slot2 : floppy disk x 1");
  89. print("slot3 : disk drive  x 1");
  90. print("left  side : chest for fuels");
  91. print("right side : chest for turtles");
  92.  
  93. local fuelLV;
  94. while true do
  95.   print("Input fuel level to start the script.");
  96.   term.write("fuel level >");
  97.   local input = read();
  98.   fuelLV = tonumber(input);
  99.   if nil == fuelLV then
  100.     print("Wrong input. Please retry.");
  101.   else
  102.     break;
  103.   end
  104. end
  105.  
  106. turtle.digUp();
  107. turtle.up();
  108.  
  109. turtle.turnRight();
  110. turtle.select(SLOT_CHEST);
  111.  
  112. turtle.place();
  113. turtle.turnLeft();
  114. turtle.turnLeft();
  115. turtle.place();
  116.  
  117. turtle.turnRight();
  118. turtle.select(SLOT_DRIVE);
  119. turtle.placeUp();
  120. turtle.select(SLOT_FLOPPY);
  121. turtle.dropUp();
  122.  
  123. local diskPath = disk.getMountPath("top");
  124.  
  125. -- backup original startup.
  126. local stPath = "/" .. diskPath .. "/startup";
  127. local bkPath = "/" .. diskPath .. "/startup_bk_by_autoRefuel";
  128. fs.delete(bkPath);
  129. if fs.exists(stPath) then
  130.   fs.move(stPath, bkPath)
  131. end
  132.  
  133. local hFile = fs.open(stPath, "w");
  134. hFile.writeLine("local ARMSG__IM_NOT_LABELED          = " .. ARMSG__IM_NOT_LABELED          .. ";");
  135. hFile.writeLine("local ARMSG__GIVE_ME_FUEL_MORE       = " .. ARMSG__GIVE_ME_FUEL_MORE       .. ";");
  136. hFile.writeLine("local ARMSG__REFUELING_WAS_COMPLETED = " .. ARMSG__REFUELING_WAS_COMPLETED .. ";");
  137. hFile.writeLine("local ARG_FUEL_LEVEL = " .. fuelLV .. ";");
  138. hFile.writeLine(floppySource);
  139. hFile.close();
  140.  
  141. turtle.turnRight();
  142. turtle.down();
  143.  
  144. local unlabeledFlg = false;
  145.  
  146. while true do
  147.   turtle.down();
  148.   turtle.select(1);
  149.   turtle.suck();
  150.  
  151.   if 0 == turtle.getItemCount(1) then
  152.     break;
  153.   end
  154.  
  155.   turtle.placeUp();
  156.  
  157.   if "turtle" ~= peripheral.getType("top") then
  158.     print("not turtle.");
  159.     turtle.digUp();
  160.     turtle.up();
  161.     for i = 1, 16 do
  162.       if 1 <= turtle.getItemCount(i) then
  163.         turtle.select(i);
  164.         turtle.drop();
  165.       end
  166.     end
  167.     turtle.down();
  168.   else
  169.     print("turnOn.");
  170.     peripheral.call("top", "turnOn");
  171.  
  172.     for i = 1, 16 do
  173.       if 1 <= turtle.getItemCount(i) then
  174.         turtle.select(i);
  175.         turtle.drop();
  176.       end
  177.     end
  178.    
  179.     turtle.turnLeft();
  180.     turtle.turnLeft();
  181.  
  182.     local lastSlot = -1;
  183.  
  184.     while true do
  185.       sleep(0);
  186.       local rcvdMsg = rs.getAnalogInput("top");
  187.       if rcvdMsg == ARMSG__IM_NOT_LABELED then
  188.         unlabeledFlg = true;
  189.         print("Unlabeled turtle.");
  190.         break;
  191.  
  192.       elseif rcvdMsg == ARMSG__GIVE_ME_FUEL_MORE then
  193.         print("Fueling mode.");
  194.        
  195.         if not turtle.suck() then
  196.           print("<< Chest for fuels is empty. >>");
  197.           sleep(3);
  198.         end
  199.        
  200.         if -1 ~= lastSlot and 1 <= turtle.getItemCount(lastSlot) then
  201.           turtle.dropUp();
  202.         else
  203.           for i = 1, 16 do
  204.             if 1 <= turtle.getItemCount(i) then
  205.               turtle.select(i);
  206.               lastSlot = i;
  207.               turtle.dropUp();
  208.               break;
  209.             end
  210.           end
  211.         end
  212.  
  213.       elseif rcvdMsg == ARMSG__REFUELING_WAS_COMPLETED then
  214.         print("Surplus fuel capture mode.");
  215.         for i = 1, 16 do
  216.           turtle.suckUp();
  217.         end
  218.         for i = 1, 16 do
  219.           if 1 <= turtle.getItemCount(i) then
  220.             turtle.select(i);
  221.             turtle.drop();
  222.           end
  223.         end
  224.         break;
  225.       end
  226.     end
  227.  
  228.     turtle.turnRight();
  229.     turtle.turnRight();
  230.  
  231.     turtle.digUp();
  232.     turtle.up();
  233.     turtle.drop();
  234.   end
  235. end
  236.  
  237. -- restore original startup.
  238. fs.delete(stPath);
  239. if fs.exists(bkPath) then
  240.   fs.move(bkPath, stPath)
  241. end
  242. fs.delete(bkPath);
  243.  
  244. turtle.down();
  245.  
  246. turtle.turnLeft();
  247. print("autoRefuel was completed.");
  248. if unlabeledFlg then
  249.   print("* unlabeled turtle was not refueled.");
  250. end
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top