我正在尝试制作一个简单的隧道采矿龟。我试图在挖掘时显示有关海龟的一些信息。例如,进度和油耗。
挖掘实际隧道和显示信息的过程/功能应该同时运行,但此时它并没有真正做到这一点。
我尝试使用并行 API,但它并没有按照我想要的方式工作。
这是我到目前为止的代码:
--Starting Conditions--
HeightQuestion = true
WidthQuestion = true
LengthQuestion = true
BlocksToMine =0
EstBlocksDone =0
BlocksDone =0
BlocksToDo =0
screen = 0
function FuncFuel()
if turtle.getFuelLevel() < 10 then
turtle.refuel()
print("fuel level: " .. turtle.getFuelLevel() .. "/".. turtle.getFuelLimit())
else
print("fuel level: " .. turtle.getFuelLevel().. "/".. turtle.getFuelLimit())
end
end
function FuncHeight()
while (HeightQuestion == true) do -- Height Question
print("Height of tunnel?")
IniHeight = tonumber(read())
if IniHeight == nil then
print( "Please answer with a number." )
elseif IniHeight >= 2 then
HeightQuestion = false
elseif IniHeight == 1 then
print( "The tunnel Height must be larger than one." )
elseif IniHeight == 0 then
print( "The tunnel Height can't be infinite." )
else
print( "The tunnel Height must be positive." )
end
end
Height=IniHeight
end
function FuncWidth()
while (WidthQuestion == true) do -- Width Question
print("Width of tunnel?")
IniWidth = tonumber(read())
if IniWidth == nil then
print( "Please answer with a number." )
elseif IniWidth > 0 then
WidthQuestion = false
elseif IniWidth == 0 then
print( "The tunnel Width can't be infinite." )
else
print( "The tunnel Width must be positive." )
end
end
Width=IniWidth
end
function FuncLength()
while (LengthQuestion == true) do -- Length Question
print("Length of tunnel?")
IniLength = tonumber(read())
if IniLength == nil then
print( "Please answer with a number." )
elseif IniLength > 0 then
LengthQuestion = false
elseif IniLength == 0 then
LengthQuestion = false
InfiniteLength = true
TorchesQuestion = false
TorchSpacingQuestion = false
else
print( "The tunnel Length must be positive." )
end
end
Length=IniLength
end
function FuncDig()
while turtle.detect()==true do
turtle.dig()
BlocksDone = BlocksDone+1
sleep(0.5)
end
end
function FuncDigUp()
while turtle.detectUp()==true do
turtle.digUp()
BlocksDone = BlocksDone+1
sleep(0.5)
end
end
function FuncDigDown()
while turtle.detectDown()==true do
turtle.digDown()
BlocksDone = BlocksDone+1
sleep(0.5)
end
end
function FuncRight()
turtle.turnRight()
FuncDig()
turtle.forward()
turtle.turnLeft()
end
function FuncLeft()
turtle.turnLeft()
FuncDig()
turtle.forward()
turtle.turnRight()
end
function FuncUp()
FuncDigUp()
turtle.up()
end
function FuncDown()
FuncDigDown()
turtle.down()
end
function FuncForward()
FuncDig()
turtle.forward()
end
function SetMineScreen()
BlocksToMine = IniHeight*IniWidth*IniLength
BlocksToDo = BlocksToMine-BlocksDone
PercentageDone= EstBlocksDone*100/BlocksToMine
if turtle.getFuelLevel() < 10 then
turtle.refuel(1)
end
term.clear()
print(" ----------MineBot---------- \n")
print(" ---------ACTIVATED--------- \n")
print(" Total Amount To Do: " .. BlocksToMine .. "\n")
print(" Total Amount Done: " .. BlocksDone .. "\n")
print(" Estimated Done: " .. EstBlocksDone .. "\n")
print(" Progress : " .. PercentageDone .. "\n")
print(" Fuel Level: " .. turtle.getFuelLevel() .. "/" .. turtle.getFuelLimit() .. "\n")
print(" <-- previous Next-->")
end
function MineScreenProcess()
while true do
SetMineScreen()
sleep(0.5)
end
end
function FuncTunnel()
FuncFuel()
Width = Width - 1
Height = Height - 1
for i=1, Length do
for i=1,Height do
FuncUp()
end
for i=1, Height / 2 do
for i=1,Width do
FuncDig()
FuncRight()
EstBlocksDone = EstBlocksDone+1
end
FuncDig()
EstBlocksDone = EstBlocksDone+1
FuncDown()
for i=1, Width do
FuncDig()
EstBlocksDone = EstBlocksDone+1
FuncLeft()
end
FuncDig()
EstBlocksDone = EstBlocksDone+1
FuncDown()
end
if Height % 2 == 0 then
for i=1,Width do
FuncDig()
EstBlocksDone = EstBlocksDone+1
FuncRight()
end
FuncDig()
EstBlocksDone = EstBlocksDone+1
for i=1, Width do
FuncDig()
FuncLeft()
end
FuncDig()
EstBlocksDone = EstBlocksDone+1
elseif Height % 2 == 1 then
for i=1,Width do
FuncDig()
EstBlocksDone = EstBlocksDone+1
FuncRight()
end
FuncDig()
EstBlocksDone = EstBlocksDone+1
FuncDown()
for i=1, Width do
FuncDig()
FuncLeft()
EstBlocksDone = EstBlocksDone+1
end
FuncDig()
EstBlocksDone = EstBlocksDone+1
end
FuncForward()
end
turtle.turnRight()
turtle.turnRight()
for i=1, Length do
turtle.forward()
end
turtle.turnRight()
turtle.turnRight()
end
FuncHeight()
FuncWidth()
FuncLength()
parallel.waitForAll(MineScreenProcess(), FuncTunnel())
--FuncDig()