Computer Craft のメモ
[PR]上記の広告は3ヶ月以上新規記事投稿のないブログに表示されています。新しい記事を書く事で広告が消えます。
ただいまコメントを受けつけておりません。
-- turtle bucket filler
-- slot1 : sample backet (oil, lava, etc)
rs.setOutput("back", true);
while true do
print("turtle bucket filler");
for i=2, 16 do
turtle.suck();
end
local foundIdx = -1;
for i=2, 16 do
turtle.select(i);
if -1 == foundIdx then
cnt = turtle.getItemCount(i);
if 0 < cnt then
if true == turtle.compareTo(1) then
turtle.drop();
else
foundIdx = i;
turtle.drop(cnt - 1);
end
end
else
turtle.drop();
end
end
if -1 ~= foundIdx then
rs.setOutput("back", false);
while true do
turtle.select(foundIdx);
turtle.placeDown();
if true == turtle.compareTo(1) then
rs.setOutput("back", true);
turtle.drop();
print("filled one bucket.");
break;
end
sleep(1);
print("no liquid. retry.");
end
else
print("no empty bucket.");
end
sleep(1);
end
local function dropAllExceptSample(sampleSlot, dropFunc)
local keepCnt;
for i = 1, 16 do
turtle.select(i);
if true == turtle.compareTo(sampleSlot) then
if i == sampleSlot then
keepCnt = 1;
else
keepCnt = 0;
end
local tmpRslt = dropFunc(math.max(0, turtle.getItemCount(i) - keepCnt));
if not tmpRslt then
return false;
end
end
end
return true;
end
local function dropItems(sampleSlot, dropCnt, dropFunc)
turtle.select(sampleSlot);
local norm = dropCnt;
local keepCnt;
for i = 1, 16 do
if norm <= 0 then
break;
end
local tgtCnt = 0;
if i == sampleSlot then
tgtCnt = math.max(0, turtle.getItemCount(i) - 1);
else
if turtle.compareTo(i) then
tgtCnt = turtle.getItemCount(i);
end
end
if 0 < tgtCnt then
local feed = math.min(norm, tgtCnt);
turtle.select(i);
dropFunc(feed);
turtle.select(sampleSlot);
norm = norm - feed;
end
end
end
local function calcSpecificItemCount(sampleSlot)
local count = 0;
turtle.select(sampleSlot);
for i = 1, 16 do
if i == sampleSlot then
count = count + turtle.getItemCount(i);
elseif turtle.compareTo(i) then
count = count + turtle.getItemCount(i);
end
end
return count;
end
local function calcCntParBooth(sampleSlot, boothCount)
local count = calcSpecificItemCount(sampleSlot);
count = count - 1;
local cntParBooth = math.floor(count / boothCount);
if 0 < count % boothCount then
cntParBooth = cntParBooth + 1;
end
return cntParBooth;
end
local function retryLoop(func, sleepSec, waitMsg)
while true do
if true == func() then
break;
end
print(waitMsg);
sleep(sleepSec);
end
end
-- turtle bucket filler manager
-- slot1 : sample backet (filled : oil, lava, etc.)
-- slot2 : sample backet (empty)
local BOOTH_CNT = 4;
local FILLED_BKT_SLOT = 1;
local EMPTY_BKT_SLOT = 2;
local OTHER_SLOTS = { 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
local filledBucketsReleaseFunc = turtle.dropUp;
local emptyBucketsSupplementFunc = turtle.suck;
local filledBucketsSuckFunc = turtle.suckUp;
local function returnFilledBuckets()
return dropAllExceptSample(FILLED_BKT_SLOT, filledBucketsReleaseFunc);
end
local function refuelAction(requiredFuelLv, filledBucketsSuckFunc)
while true do
for i = 1, 16 do
filledBucketsSuckFunc();
end
local cmpl = false;
for _, i in ipairs(OTHER_SLOTS) do
turtle.select(i);
turtle.refuel();
local tmp = turtle.getFuelLevel();
print(" fuel level -> " .. tmp);
if requiredFuelLv <= tmp then
print(" refueling was completed.");
cmpl = true;
break;
end
end
retryLoop(returnFilledBuckets, 5, "Inventory full? sleep(5)");
if cmpl then
break;
else
term.write("(sleep)");
for i = 1, 20 do
sleep(1);
term.write(".");
end
end
end
end
print("-- status check");
while true do
if 0 == turtle.getItemCount(FILLED_BKT_SLOT) or
0 == turtle.getItemCount(EMPTY_BKT_SLOT) then
print("Set items as follows.");
print("slot1 : filled bucket x 1");
print(" (lava, oil, etc.)");
print("slot2 : empty bucket x 1");
print("Hit enter key when ready.");
read();
else
break;
end
end
print("-- reset position");
for i=1, BOOTH_CNT * 2 do
turtle.forward();
end
local earnedFlg = true;
local distributedFlg = true;
local turn = 0;
while true do
term.clear();
term.setCursorPos(1, 1);
print("---------------------------------");
print("-- turtle bucket filler manager");
print("---------------------------------");
print("[turn] = " .. tostring(turn));
local fuelLevel = turtle.getFuelLevel();
print("[fuel level] = " .. fuelLevel);
local tmp = BOOTH_CNT - 1;
local requiredFuelLv = (math.min(tmp, 1) * 4 + tmp * 4) * (tmp / 2);
print("[required ] = " .. requiredFuelLv);
if fuelLevel < requiredFuelLv then
print("<< out of fuel. >>");
refuelAction(requiredFuelLv, filledBucketsSuckFunc);
end
if not earnedFlg and not distributedFlg then
print("[skip] capture filled buckets");
else
earnedFlg = false;
print("capture filled buckets");
for i = 1, BOOTH_CNT do
print(" booth No. " .. tostring(i));
for j = 2, i do
turtle.back();
turtle.back();
end
for j = 1, 16 do
turtle.suckDown();
end
dropAllExceptSample(EMPTY_BKT_SLOT, turtle.dropDown);
for j = 2, i do
turtle.forward();
turtle.forward();
end
if 1 < calcSpecificItemCount(FILLED_BKT_SLOT) then
earnedFlg = true;
end
retryLoop(returnFilledBuckets, 5, "Inventory full? sleep(5)");
end
if earnedFlg then
print("got new filled buckets.");
else
print("no new filled buckets.");
end
end
for j = 1, 16 do
emptyBucketsSupplementFunc();
end
if 1 < calcSpecificItemCount(EMPTY_BKT_SLOT) then
distributedFlg = true;
end
if not earnedFlg and not distributedFlg then
print("[skip] distribute empty buckets. (no empty buckets.)");
else
print("distribute empty buckets");
dropAllExceptSample(FILLED_BKT_SLOT, filledBucketsReleaseFunc);
local cntParBooth = calcCntParBooth(EMPTY_BKT_SLOT, BOOTH_CNT);
if 0 < cntParBooth then
distributedFlg = true;
else
distributedFlg = false;
end
if 0 < cntParBooth then
local currentSlot = 2;
for i = 1, BOOTH_CNT do
print(" booth No. " .. tostring(i));
if 1 < i then
turtle.back();
turtle.back();
end
currentSlot = dropItems(EMPTY_BKT_SLOT, cntParBooth, turtle.dropDown);
end
for i = 1, BOOTH_CNT do
if 1 < i then
turtle.forward();
turtle.forward();
end
end
end
end
if not earnedFlg and not distributedFlg then
term.write("(sleep)");
for i = 1, 20 do
sleep(0.4);
term.write(".");
end
end
turn = turn + 1;
end