Computercraft Lua同时运行的多个进程
我想做一个简单的隧道挖掘海龟。我试图在挖掘时显示一些关于海龟的信息。例如,进度和燃油消耗 挖掘实际隧道和显示信息的过程/功能应该同时运行,但目前还不能做到这一点 我尝试使用并行API,但它不能按我希望的方式工作 以下是我目前掌握的代码:Computercraft Lua同时运行的多个进程,lua,minecraft,computercraft,Lua,Minecraft,Computercraft,我想做一个简单的隧道挖掘海龟。我试图在挖掘时显示一些关于海龟的信息。例如,进度和燃油消耗 挖掘实际隧道和显示信息的过程/功能应该同时运行,但目前还不能做到这一点 我尝试使用并行API,但它不能按我希望的方式工作 以下是我目前掌握的代码: --Starting Conditions-- HeightQuestion = true WidthQuestion = true LengthQuestion = true BlocksToMine =0 EstBlocksDone =0 BlocksDon
--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()
——启动条件--
高度问题=正确
问题=true
长度问题=true
布洛克斯多明=0
EstBlocksDone=0
BlocksOne=0
BlocksToDo=0
屏幕=0
函数FuncFuel()
如果turtle.getFuelLevel()小于10,则
乌龟加油
打印(“燃油油位:…turtle.getfuelevel()…/”.turtle.getfuelimit())
其他的
打印(“燃油油位:…turtle.getfuelevel()…/”.turtle.getfuelimit())
终止
终止
函数FuncHeight()
而(HeightQuestion==true)do--Height问题
打印(“隧道高度?”)
IniHeight=tonNumber(读取())
如果IniHeight==nil,则
打印(“请用数字回答”)
否则,高度>=2,则
高度问题=错误
elseif IniHeight==1,则
打印(“通道高度必须大于1。”)
elseif IniHeight==0,则
打印(“隧道高度不能是无限的。”)
其他的
打印(“隧道高度必须为正。”)
终止
终止
高度=高度
终止
函数FuncWidth()
而(WidthQuestion==true)do--WidthQuestion
打印(“隧道宽度?”)
IniWidth=tonNumber(读取())
如果IniWidth==nil,则
打印(“请用数字回答”)
elseif IniWidth>0则
问题=false
elseif IniWidth==0,则
打印(“隧道宽度不能是无限的。”)
其他的
打印(“通道宽度必须为正。”)
终止
终止
宽度=宽度
终止
函数FuncLength()
而(LengthQuestion==true)do--Length问题
打印(“隧道长度?”)
IniLength=tonNumber(读取())
如果IniLength==nil,则
打印(“请用数字回答”)
elseif IniLength>0则
长度问题=错误
elseif IniLength==0,则
长度问题=错误
无穷长=真
TorchesQuestion=false
TorchspackingQuestion=错误
其他的
打印(“通道长度必须为正。”)
终止
终止
长度=长度
终止
函数FuncDig()
while.detect()==true do
乌龟
BlocksDone=BlocksDone+1
睡眠(0.5)
终止
终止
函数FuncDigUp()
而turtle.detectUp()==true do
乌龟
BlocksDone=BlocksDone+1
睡眠(0.5)
终止
终止
函数FuncDigDown()
而turtle.detectDown()==true do
海龟
BlocksDone=BlocksDone+1
睡眠(0.5)
终止
终止
函数FuncRight()
海龟右转
FuncDig()
乌龟前进
乌龟左转弯
终止
函数FuncLeft()
乌龟左转弯
FuncDig()
乌龟前进
海龟右转
终止
函数FuncUp()
FuncDigUp()
乌龟
终止
函数FuncDown()
FuncDigDown()
乌龟
终止
函数FuncForward()
FuncDig()
乌龟前进
终止
函数SetMineScreen()
BlocksToMine=IniHeight*IniWidth*IniLength
BlocksToDo=BlocksToMine blockstone
PercentageDone=EstBlocksOne*100/BlocksToMine
如果turtle.getFuelLevel()小于10,则
乌龟。加油(1)
终止
术语.clear()
打印(“------------MineBot------\n”)
打印(“-----------已激活----------------\n”)
打印(“要执行的总金额:“…BlocksToMine…””\n)
打印(“完成的总金额:“…BlocksDone…”\n)
打印(“预计完成:…EstBlocksDone..\n”)
打印(“进度:…百分比完成…”\n)
打印(“燃油油位:…turtle.getfuelevel()…/“.turtle.getfuelimit()…”\n)
打印(“”)
终止
函数进程()
尽管如此
SetMineScreen()
睡眠(0.5)
终止
终止
函数FuncTunnel()
燃料()
宽度=宽度-1
高度=高度-1
对于i=1,长度do
对于i=1,高度do
FuncUp()
终止
对于i=1,高度/2 do
对于i=1,宽度do
FuncDig()
FuncRight()
EstBlocksDone=EstBlocksDone+1
终止
FuncDig()
EstBlocksDone=EstBlocksDone+1
FuncDown()
对于i=1,宽度do
FuncDig()
EstBlocksDone=EstBlocksDone+1
FuncLeft()
终止
FuncDig()
EstBlocksDone=EstBlocksDone+1
FuncDown()
终止
如果高度%2==0,则
对于i=1,宽度do
FuncDig()
EstBlocksDone=EstBlocksDone+1
FuncRight()
终止
FuncDig()
EstBlocksDone=EstBlocksDone+1
对于i=1,宽度do
FuncDig()
FuncLeft()
终止
FuncDig()
EstBlocksDone=EstBlocksDone+1
elseif高度%2==1,则
对于i=1,宽度do
FuncDig()
EstBlocksDone=EstBlocksDone+1
FuncRight()
终止
FuncDig()
EstBlocksDone=EstBlocksDone+1
FuncDown()
对于i=1,宽度do
FuncDig()
FuncLeft()
EstBlocksDone=EstBlocksDone+1
终止
FuncDig()
EstBlocksDone=EstBlocksDone+1
终止
前进(
parallel.waitForAll(MineScreenProcess(), FuncTunnel())
parallel.waitForAll(MineScreenProcess, FuncTunnel)
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 FuncFuel()
if turtle.getFuelLevel() < 10 then
turtle.refuel()
end
print("fuel level: " .. turtle.getFuelLevel().. "/".. turtle.getFuelLimit())
end
elseif IniHeight == 0 then
print( "The tunnel Height can't be infinite." )
while true do
parallel.waitForAny(MineScreenProcess(), FuncTunnel())
end