Advertisement
ns09005264

starmetal.lua

Nov 14th, 2020 (edited)
680
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.53 KB | None | 0 0
  1. -- 植物魔法活石活木
  2. local args = {...};
  3. if args[1] == nil or args[1] == 0 or args[2] == nil or args[2] == 0 then
  4.     print("invalid arg expect program range range");
  5.     return;
  6. end
  7. local mData = {
  8.     ore = {
  9.         solt = 1,
  10.         layer = 2,
  11.         productSolt = 2,
  12.         radio = 1
  13.     },
  14.     currLayer = 1;
  15. }
  16. local range = {x = tonumber(args[1]), z = tonumber(args[2])};
  17.  
  18. -- 传输活石或活木到箱子中
  19. function reap(solt)
  20.     print("current solt = ", solt);
  21.     local itemData = turtle.getItemDetail(solt);
  22.     print("item name = ", itemData.name);
  23.     for i = 1, 16, 1 do
  24.         print("try get itemData now");
  25.         local currItemData = turtle.getItemDetail(i);
  26.         -- 如果物品存在并且与指定位置的物品相同
  27.         if (currItemData ~= nil) then
  28.             print("cant get itemData at ", i, "solt");
  29.             print("this item name =  ", currItemData.name);
  30.             if (itemData.name == currItemData.name) then
  31.                 local itemCount = turtle.getItemCount(i);
  32.                 print("this item count = ", itemCount);
  33.                 turtle.select(i);
  34.                 if i == solt then
  35.                     turtle.drop(itemCount - 1);
  36.                 end
  37.                 if i ~= solt then
  38.                     turtle.drop(itemCount);
  39.                 end
  40.             end
  41.         end
  42.  
  43.         if currItemData == nil then
  44.             print("cant get itemData at ", i, "solt");
  45.         end
  46.     end
  47. end
  48.  
  49. function fetch(solt, need, layer)
  50.     turtle.select(solt);
  51.     local itemCount = turtle.getItemCount();
  52.     if itemCount == nil then
  53.         return 0;
  54.     end
  55.     if itemCount + 1 >= need then
  56.         return itemCount + 1;
  57.     else
  58.         goLayer(layer);
  59.         turtle.suck(need - itemCount + 1);
  60.         print("layer suck item count = ", need - itemCount + 1, " then itemCount = ", turtle.getItemCount());
  61.         return turtle.getItemCount();
  62.     end
  63. end
  64.  
  65. function goLayer(layer)
  66.     print("go layer = ", layer, "currLayer = ", mData.currLayer);
  67.     local step = layer - mData.currLayer;
  68.     while (mData.currLayer < 10) and (mData.currLayer > 0) do
  69.         if layer == mData.currLayer then
  70.             return true;
  71.         end
  72.         if step < 0 then
  73.             turtle.down();
  74.             mData.currLayer = mData.currLayer - 1;
  75.         end
  76.         if step > 0 then
  77.             turtle.up();
  78.             mData.currLayer = mData.currLayer + 1;
  79.         end
  80.     end
  81.     return false;
  82. end
  83.  
  84. function tryStart(totalStep)
  85.     local totalCount = totalStep; -- 32
  86.     local oreNeed = math.floor(totalCount * mData.ore.radio); -- 9
  87.     local fetchCount = fetch(mData.ore.solt, oreNeed, mData.ore.layer);
  88.     goLayer(1);
  89.     return fetchCount >= totalCount;
  90. end
  91.  
  92.  
  93. function patrolling(range)
  94.     local totalStep = range.x * range.z; -- 18
  95.     local postion = {x = 1, z = 1};
  96.  
  97.     local totalCount = totalStep;
  98.     local woodStep = {math.floor(totalCount * mData.ore.radio)};
  99.  
  100.     turtle.turnRight();
  101.     turtle.turnRight();
  102.  
  103.     for i = 1, totalStep, 1 do
  104.         action(woodStep, i);
  105.         -- print("i=", i, ",postion.z=", postion.z, ",postion.x=", postion.x);
  106.         if math.fmod(i, range.x) == 0 then -- 如果达到x坐标的变界
  107.             print("reach x border, postion.z=", postion.z, ",range.z=", range.z);
  108.             if postion.z == range.z then -- 如果当前z坐标等于范围z坐标,说明已经走到最大的范围
  109.                 print("start back currx=", postion.x, " currz=", postion.z);
  110.                 if math.fmod(postion.z, 2) == 1 then
  111.                     back(range.x, range.z);
  112.                 end
  113.                 if math.fmod(postion.z, 2) == 0 then
  114.                     back(1, range.z);
  115.                 end
  116.                 break;
  117.             end
  118.             if math.fmod(postion.z, 2) == 1 then -- 如果是奇数列
  119.                 turtle.turnRight();
  120.                 turtle.forward();
  121.                 turtle.turnRight();
  122.             end
  123.             if math.fmod(postion.z, 2) == 0 then -- 如果是偶数列
  124.                 turtle.turnLeft();
  125.                 turtle.forward();
  126.                 turtle.turnLeft();
  127.             end
  128.             postion.z = postion.z + 1;
  129.         else -- 如果没有达到x坐标的边界
  130.             turtle.forward();
  131.         end
  132.     end
  133. end
  134.  
  135. function back(x, z) -- x == 6 z == 6
  136.     local home = {x = 1, z = 1};
  137.     if x > home.x then
  138.         turtle.turnLeft();
  139.         turtle.turnLeft();
  140.     end
  141.     while true do
  142.         if x == home.x then
  143.             break;
  144.         else
  145.             turtle.forward();
  146.         end
  147.         x = x - 1;
  148.     end
  149.     if z > home.z then
  150.         turtle.turnRight();
  151.         while true do
  152.             if z == home.z then
  153.                 turtle.turnLeft();
  154.                 break;
  155.             else
  156.                 turtle.forward();
  157.             end
  158.             z = z - 1;
  159.         end
  160.     end
  161.     reap(mData.ore.productSolt);
  162. end
  163.  
  164. function compareAndDig(woodStep, step)
  165.     turtle.select(mData.ore.productSolt);
  166.     local tryTimes = 0;
  167.     while tryTimes < 3 do
  168.         if turtle.compareDown() then
  169.             turtle.digDown();
  170.             placeBlock(woodStep, step);
  171.             return true;
  172.         end
  173.         os.sleep(1);
  174.         tryTimes = tryTimes + 1;
  175.     end
  176.    
  177.     return false;
  178. end
  179.  
  180. function action(woodStep, step)
  181.     -- 如果检测到下方有方块,则挖掘并放置
  182.     if turtle.detectDown() and not compareAndDig(woodStep, step) then
  183.         woodStep[1] = woodStep[1] + 1;
  184.     -- 如果没有检测到方块则直接放置
  185.     else
  186.         placeBlock(woodStep, step);
  187.     end
  188. end
  189.  
  190. function placeBlock(woodStep, step)
  191.     if step <= woodStep[1] then
  192.         turtle.select(mData.ore.solt);
  193.     else
  194.         turtle.select(mData.stone.solt);
  195.     end
  196.     if turtle.getItemCount() > 1 then
  197.         turtle.placeDown();
  198.     end
  199. end
  200.  
  201. function checkFule()
  202.     if turtle.getFuelLevel() ~= "unlimited" and turtle.getFuelLevel() < 1 then
  203.         turtle.select(1);
  204.         turtle.refuel(1);
  205.     end
  206.     if turtle.getFuelLevel() < 1 then
  207.         print("need fuel!!");
  208.         return false;
  209.     end
  210.     return true;
  211. end
  212.  
  213. while true do
  214.     if not checkFule() or redstone.getInput("right") then break end
  215.     if tryStart(range.x * range.z) then
  216.         if turtle.detectDown() then
  217.             turtle.select(mData.ore.productSolt);
  218.             if turtle.compareDown() then
  219.                 patrolling(range);
  220.             end
  221.         else
  222.             patrolling(range);
  223.         end
  224.     end
  225.     os.sleep(2);
  226. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement