Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ------------------------------------------
- -- Vril's program v 1.0 --
- ------------------------------------------
- function addWork(lx, ly, lz)
- size=size+1
- worklist[size]={}
- worklist[size][1]=lx
- worklist[size][2]=ly
- worklist[size][3]=lz
- end
- function removeWork(index)
- local i
- if index < 1 or index > size then
- return
- end
- for i=index,size-1 do
- worklist[i][1]=worklist[i+1][1]
- worklist[i][2]=worklist[i+1][2]
- worklist[i][3]=worklist[i+1][3]
- end
- worklist[size]=nil
- size=size-1
- end
- function removeCoords(rx, ry, rz)
- local i
- i=1
- while i<=size do
- if worklist[i][1] == rx and
- worklist[i][2] == ry and
- worklist[i][3] == rz then
- removeWork(i)
- end
- i=i+1
- end
- end
- function reduceWork()
- local i, j, lx, ly, lz
- i=1
- while i<size do
- lx=worklist[i][1]
- ly=worklist[i][2]
- lz=worklist[i][3]
- j=i+1
- while j<=size do
- if worklist[j][1] == lx and
- worklist[j][2] == ly and
- worklist[j][3] == lz then
- removeWork(j)
- j=j-1
- end
- j=j+1
- end
- i=i+1
- end
- end
- function findeNearestPlot()
- local lx,ly,lz
- local mindist=math.huge
- local dist=0
- local res=0
- local i
- for i=1,size do
- lx=worklist[i][1]
- ly=worklist[i][2]
- lz=worklist[i][3]
- dist=(lx-tX)*(lx-tX) + (ly-tY)*(ly-tY) + (lz-tZ)*(lz-tZ)
- if dist<mindist then
- mindist=dist
- res=i
- end
- end
- return res
- end
- function forceMoveForward()
- local can_move=true
- can_move=turtle.forward()
- while not can_move do
- --print("ERR: There is a something on my way!")
- sleep(0.2)
- --
- turtle.dig()
- turtle.attack()
- --
- can_move=turtle.forward()
- end
- end
- function forceMoveBack()
- local can_move=true
- can_move=turtle.back()
- while not can_move do
- --print("ERR: There is a something on my way!")
- sleep(0.2)
- can_move=turtle.back()
- end
- end
- function forceMoveUp()
- local can_move=true
- can_move=turtle.up()
- while not can_move do
- --print("ERR: There is a something on my way!")
- sleep(0.2)
- --
- turtle.digUp()
- turtle.attackUp()
- --
- can_move=turtle.up()
- end
- end
- function forceMoveDown()
- local can_move=true
- can_move=turtle.down()
- while not can_move do
- --print("ERR: There is a something on my way!")
- sleep(0.2)
- --
- turtle.digDown()
- turtle.attackDown()
- --
- can_move=turtle.down()
- end
- end
- function left()
- if tD[1]==1 and tD[2]==0 then
- tD[1]=0
- tD[2]=-1
- elseif tD[1]==0 and tD[2]==-1 then
- tD[1]=-1
- tD[2]=0
- elseif tD[1]==-1 and tD[2]==0 then
- tD[1]=0
- tD[2]=1
- elseif tD[1]==0 and tD[2]==1 then
- tD[1]=1
- tD[2]=0
- end
- turtle.turnLeft()
- end
- function right()
- if tD[1]==1 and tD[2]==0 then
- tD[1]=0
- tD[2]=1
- elseif tD[1]==0 and tD[2]==1 then
- tD[1]=-1
- tD[2]=0
- elseif tD[1]==-1 and tD[2]==0 then
- tD[1]=0
- tD[2]=-1
- elseif tD[1]==0 and tD[2]==-1 then
- tD[1]=1
- tD[2]=0
- end
- turtle.turnRight()
- end
- function turnL(num1,num2)
- while tD[1]~=num1 or tD[2]~=num2 do
- left()
- end
- end
- function turnR(num1,num2)
- while tD[1]~=num1 or tD[2]~=num2 do
- right()
- end
- end
- function turnB(num1,num2)
- if math.abs(num1-tD[1])==2 then
- turnR(num1,num2)
- elseif math.abs(num2-tD[2])==2 then
- turnL(num1,num2)
- elseif tD[1]==1 then
- if num2==1 then
- turnR(num1,num2)
- elseif num2==-1 then
- turnL(num1,num2)
- end
- elseif tD[1]==-1 then
- if num2==-1 then
- turnR(num1,num2)
- elseif num2==1 then
- turnL(num1,num2)
- end
- elseif tD[2]==1 then
- if num1==-1 then
- turnR(num1,num2)
- elseif num1==1 then
- turnL(num1,num2)
- end
- elseif tD[2]==-1 then
- if num1==1 then
- turnR(num1,num2)
- elseif num1==-1 then
- turnL(num1,num2)
- end
- end
- end
- function moveXYZ(lx,ly,lz)
- local i
- if tZ<lz then
- for i=1,lz-tZ do
- forceMoveUp()
- end
- else
- for i=1,tZ-lz do
- forceMoveDown()
- end
- end
- if tY<ly then
- turnB(0,1)
- for i=1,ly-tY do
- forceMoveForward()
- end
- elseif tY>ly then
- turnB(0,-1)
- for i=1,tY-ly do
- forceMoveForward()
- end
- end
- if tX<lx then
- turnB(1,0)
- for i=1,lx-tX do
- forceMoveForward()
- end
- elseif tX>lx then
- turnB(-1,0)
- for i=1,tX-lx do
- forceMoveForward()
- end
- end
- tX,tY,tZ=lx,ly,lz
- removeCoords(lx, ly, lz)
- end
- function upgradeWork()
- local lx,ly,lz
- lx,ly,lz = tX,tY,tZ
- turtle.dig()
- sum=turtle.getItemCount(1)
- if sum>prevsum then
- lx=lx+tD[1]
- ly=ly+tD[2]
- addWork(lx, ly, lz)
- prevsum=sum
- end
- left()
- lx,ly,lz = tX,tY,tZ
- turtle.dig()
- sum=turtle.getItemCount(1)
- if sum>prevsum then
- lx=lx+tD[1]
- ly=ly+tD[2]
- addWork(lx, ly, lz)
- prevsum=sum
- end
- left()
- lx,ly,lz = tX,tY,tZ
- turtle.dig()
- sum=turtle.getItemCount(1)
- if sum>prevsum then
- lx=lx+tD[1]
- ly=ly+tD[2]
- addWork(lx, ly, lz)
- prevsum=sum
- end
- left()
- lx,ly,lz = tX,tY,tZ
- turtle.dig()
- sum=turtle.getItemCount(1)
- if sum>prevsum then
- lx=lx+tD[1]
- ly=ly+tD[2]
- addWork(lx, ly, lz)
- prevsum=sum
- end
- lx,ly,lz = tX,tY,tZ
- turtle.digUp()
- sum=turtle.getItemCount(1)
- if sum>prevsum then
- lz=lz+1
- addWork(lx, ly, lz)
- prevsum=sum
- end
- lx,ly,lz = tX,tY,tZ
- turtle.digDown()
- sum=turtle.getItemCount(1)
- if sum>prevsum then
- lz=lz-1
- addWork(lx, ly, lz)
- prevsum=sum
- end
- reduceWork()
- end
- ------------------------------------
- --global variables:
- tX,tY,tZ=0,0,0
- tD={1,0}
- worklist={}
- size=0
- sum, prevsum=0,0
- --Locals:
- local indx=0
- local x,y,z=0,0,0
- turtle.select(1)
- turtle.dropUp()
- moveXYZ(0,0,-1)
- upgradeWork()
- indx=findeNearestPlot()
- while indx~=0 do
- x = worklist[indx][1]
- y = worklist[indx][2]
- z = worklist[indx][3]
- removeWork(indx)
- moveXYZ(x,y,z)
- upgradeWork()
- indx=findeNearestPlot()
- end
- moveXYZ(0,0,0)
- turnB(1,0)
- term.setCursorPos(1,1)
- term.clear()
- print(" *Vykopal!")
- print(" *Label: ",os.getComputerLabel())
- print(" *Fuel Level:",turtle.getFuelLevel())
Advertisement
Add Comment
Please, Sign In to add comment