- <P>--挖矿程序核心</P>
- <P>--对行走失败时的处理程序,先判断是否是方块阻塞;然后判断是否是生物阻塞;剑要放在第15物品槽中
- --测试通过。
- function g_obstruct()
- bl1 = turtle.dig()
- if bl1 == false then
- turtle.select(15)
- turtle.equipLeft()
- turtle.attack()
- turtle.attack()
- turtle.attack()
- turtle.equipLeft()
- turtle.select(1)
- end
- end</P>
- <P>--行走函数,可以记录位置(x,y,z)和朝向(direction);direction1,2,3,0分别代表北、东、南、西
- --燃油大于等于最小返回距离时返回true;否则返回false。
- --测试通过。
- function tT_go(x,y,z,direction,remind)
- rhJudge = false
- goSuccess = turtle.forward()
- while goSuccess == false do
- library.g_obstruct()
- goSuccess = turtle.forward()
- library.yljc()
- end
- if remind == true then
- x,y,z = library.azimuthRecorder(x,y,z,direction,"go")
- end
- rhJudge = library.g_rhJudge(x,y,z)
- return rhJudge,x,y,z
- end</P>
- <P>--转向函数,记录朝向变化
- --返回朝向
- --测试通过。
- function tT_LR(mode,direction)
- if mode == 'l' then
- turtle.turnLeft()
- _,_,_,direction = library.azimuthRecorder(0,0,0,direction,"turnLeft")
- elseif mode == 'r' then
- turtle.turnRight()
- _,_,_,direction = library.azimuthRecorder(0,0,0,direction,"turnRight")
- elseif mode == 'b' then
- direction = library.tT_LR('r',direction)
- direction = library.tT_LR('r',direction)
- end
- direction = direction % 4
- return direction
- end</P>
- <P>--方位记录器,在方位(x,y,z)和朝向发生变化时记录变化数据
- --返回方位和朝向
- --测试通过。
- function azimuthRecorder(x,y,z,direction,mode)
- if mode == "turnLeft" then
- direction = direction - 1
- elseif mode == "turnRight" then
- direction = direction + 1
- elseif mode == "go" then
- if direction == 1 then
- y = y+1
- elseif direction == 2 then
- x = x+1
- elseif direction == 3 then
- y = y-1
- elseif direction == 0 then
- x = x-1
- end
- end
- return x,y,z,direction
- end</P>
- <P>--判断返回函数,燃油大于等于最小返回距离时返回true;否则返回false。
- --测试通过。
- function g_rhJudge(x,y,z)
- gH = math.abs(x)+math.abs(y)+math.abs(z)
- if gH >= turtle.getFuelLevel() - 1 then
- return true
- end
- return false
- end</P>
- <P>--最快返回函数,沿最近矿道返回
- --测试通过。
- function tT_goHome(x,y,z,direction,beginDirection)
- if beginDirection == 1 then
- library.confirmDirection(3,0,x,y,z,direction)
- elseif beginDirection == 2 then
- library.confirmDirection(1,0,x,y,z,direction)
- elseif beginDirection == 3 then
- library.confirmDirection(1,2,x,y,z,direction)
- elseif beginDirection == 0 then
- library.confirmDirection(3,2,x,y,z,direction)
- end
- end</P>
- <P>--方向确认函数,确认最快返回的方向并计算线路
- --测试通过。
- function confirmDirection(a,b,x,y,z,direction)
- abl_X = math.abs(x) % 5
- abl_Y = math.abs(y) % 5
- if abl_X >= abl_Y then
- while direction ~= a do
- direction = library.tT_LR('l',direction)
- end
- else
- while direction ~= b do
- direction = library.tT_LR('l',direction)
- end
- end
- if direction == b then
- if abl_X ~= 0 then
- for i = 0,abl_X-1 do
- _,x,y = library.tT_go(x,y,z,direction,false)
- end
- end
- direction = library.tT_LR('r',direction)
- if math.abs(y) ~= 0 then
- for i = 0,math.abs(y)-1 do
- _,x,y = library.tT_go(x,y,z,direction,false)
- end
- end
- direction = library.tT_LR('l',direction)
-
- if math.abs(x)-abl_X ~= 0 then
- for i = 0,math.abs(x)-abl_X-1 do
- _,x,y = library.tT_go(x,y,z,direction,false)
- end
- end
- elseif direction == a then
- if abl_Y ~= 0 then
- for i = 0,abl_Y-1 do
- _,x,y = library.tT_go(x,y,z,direction,false)
- end
- end
- direction = library.tT_LR('l',direction)
-
- if math.abs(x) ~= 0 then
- for i = 0,math.abs(x)-1 do
- _,x,y = library.tT_go(x,y,z,direction,false)
- end
- end
- direction = library.tT_LR('r',direction)
-
- if math.abs(y)-abl_Y ~= 0 then
- for i = 0,math.abs(y)-abl_Y-1 do
- _,x,y = library.tT_go(x,y,z,direction,false)
- end
- end
- direction = library.tT_LR('l',direction)
- end
- direction = library.tT_LR('r',direction)
- direction = library.tT_LR('r',direction)
- end</P>
复制代码 |