Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- 植物魔法活石活木
- local args = {...};
- if args[1] == nil or args[1] == 0 or args[2] == nil or args[2] == 0 then
- print("invalid arg expect program range range");
- return;
- end
- local mData = {
- wood = {
- solt = 4,
- layer = 2,
- productSolt = 2,
- radio = 0.3
- },
- stone = {
- solt = 5,
- layer = 3,
- productSolt = 3,
- radio = 0.7
- },
- currLayer = 1;
- }
- local range = {x = tonumber(args[1]), z = tonumber(args[2])};
- -- 传输活石或活木到箱子中
- function reap(solt)
- print("current solt = ", solt);
- local itemData = turtle.getItemDetail(solt);
- print("item name = ", itemData.name);
- for i = 1, 16, 1 do
- print("try get itemData now");
- local currItemData = turtle.getItemDetail(i);
- -- 如果物品存在并且与指定位置的物品相同
- if (currItemData ~= nil) then
- print("cant get itemData at ", i, "solt");
- print("this item name = ", currItemData.name);
- if (itemData.name == currItemData.name) then
- local itemCount = turtle.getItemCount(i);
- print("this item count = ", itemCount);
- turtle.select(i);
- if i == solt then
- turtle.drop(itemCount - 1);
- end
- if i ~= solt then
- turtle.drop(itemCount);
- end
- end
- end
- if currItemData == nil then
- print("cant get itemData at ", i, "solt");
- end
- end
- end
- function fetch(solt, need, layer)
- turtle.select(solt);
- local itemCount = turtle.getItemCount();
- if itemCount == nil then
- return 0;
- end
- if itemCount + 1 >= need then
- return itemCount + 1;
- else
- goLayer(layer);
- turtle.suck(need - itemCount + 1);
- print("layer suck item count = ", need - itemCount + 1, " then itemCount = ", turtle.getItemCount());
- return turtle.getItemCount();
- end
- end
- function goLayer(layer)
- print("go layer = ", layer, "currLayer = ", mData.currLayer);
- local step = layer - mData.currLayer;
- while (mData.currLayer < 10) and (mData.currLayer > 0) do
- if layer == mData.currLayer then
- return true;
- end
- if step < 0 then
- turtle.down();
- mData.currLayer = mData.currLayer - 1;
- end
- if step > 0 then
- turtle.up();
- mData.currLayer = mData.currLayer + 1;
- end
- end
- return false;
- end
- function tryStart(totalStep)
- local totalCount = totalStep - (totalStep / 9); -- 32
- local woodNeed = math.floor(totalCount * mData.wood.radio); -- 9
- local stoneNeed = totalCount - woodNeed; -- 23
- local fetchCount = fetch(mData.wood.solt, woodNeed, mData.wood.layer) + fetch(mData.stone.solt, stoneNeed, mData.stone.layer);
- goLayer(1);
- return fetchCount >= totalCount;
- end
- function patrolling(range)
- local totalStep = range.x * range.z; -- 18
- local postion = {x = 1, z = 1};
- local totalCount = totalStep - (totalStep / 9);
- local woodStep = {math.floor(totalCount * mData.wood.radio)};
- turtle.turnRight();
- turtle.turnRight();
- for i = 1, totalStep, 1 do
- action(woodStep, i);
- -- print("i=", i, ",postion.z=", postion.z, ",postion.x=", postion.x);
- if math.fmod(i, range.x) == 0 then -- 如果达到x坐标的变界
- print("reach x border, postion.z=", postion.z, ",range.z=", range.z);
- if postion.z == range.z then -- 如果当前z坐标等于范围z坐标,说明已经走到最大的范围
- print("start back currx=", postion.x, " currz=", postion.z);
- if math.fmod(postion.z, 2) == 1 then
- back(range.x, range.z);
- end
- if math.fmod(postion.z, 2) == 0 then
- back(1, range.z);
- end
- break;
- end
- if math.fmod(postion.z, 2) == 1 then -- 如果是奇数列
- turtle.turnRight();
- turtle.forward();
- turtle.turnRight();
- end
- if math.fmod(postion.z, 2) == 0 then -- 如果是偶数列
- turtle.turnLeft();
- turtle.forward();
- turtle.turnLeft();
- end
- postion.z = postion.z + 1;
- else -- 如果没有达到x坐标的边界
- turtle.forward();
- end
- end
- end
- function back(x, z) -- x == 6 z == 6
- local home = {x = 1, z = 1};
- if x > home.x then
- turtle.turnLeft();
- turtle.turnLeft();
- end
- while true do
- if x == home.x then
- break;
- else
- turtle.forward();
- end
- x = x - 1;
- end
- if z > home.z then
- turtle.turnRight();
- while true do
- if z == home.z then
- turtle.turnLeft();
- break;
- else
- turtle.forward();
- end
- z = z - 1;
- end
- end
- reap(mData.wood.productSolt);
- reap(mData.stone.productSolt);
- end
- function compareAndDig(woodStep, step)
- turtle.select(mData.wood.productSolt);
- if turtle.compareDown() then
- turtle.digDown();
- placeBlock(woodStep, step);
- return true;
- end
- turtle.select(mData.stone.productSolt);
- if turtle.compareDown() then
- turtle.digDown();
- placeBlock(woodStep, step);
- return true;
- end
- return false;
- end
- function action(woodStep, step)
- -- 如果检测到下方有方块,则挖掘并放置
- if turtle.detectDown() and not compareAndDig(woodStep, step) then
- woodStep[1] = woodStep[1] + 1;
- -- 如果没有检测到方块则直接放置
- else
- placeBlock(woodStep, step);
- end
- end
- function placeBlock(woodStep, step)
- if step <= woodStep[1] then
- turtle.select(mData.wood.solt);
- else
- turtle.select(mData.stone.solt);
- end
- if turtle.getItemCount() > 1 then
- turtle.placeDown();
- end
- end
- while true do
- if turtle.getFuelLevel() ~= "unlimited" and turtle.getFuelLevel() < 1 then
- turtle.select(1);
- turtle.refuel(1);
- end
- if turtle.getFuelLevel() < 1 then
- print("need fuel!!");
- break;
- end
- if redstone.getInput("right") then break end
- if tryStart(range.x * range.z) then
- if turtle.detectDown() then
- turtle.select(mData.wood.productSolt);
- if turtle.compareDown() then
- patrolling(range);
- end
- turtle.select(mData.stone.productSolt);
- if turtle.compareDown() then
- patrolling(range);
- end
- else
- patrolling(range);
- end
- end
- os.sleep(2);
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement