ns09005264

botania-livestuff

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

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×