Vrill

Computercraft: kopay

May 7th, 2014
390
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.88 KB | None | 0 0
  1. ------------------------------------------
  2. -- Vril's program v 1.0                 --
  3. ------------------------------------------
  4.  
  5. function addWork(lx, ly, lz)
  6.  size=size+1
  7.  worklist[size]={}
  8.  worklist[size][1]=lx
  9.  worklist[size][2]=ly
  10.  worklist[size][3]=lz
  11. end
  12.  
  13. function removeWork(index)
  14.  local i
  15.  if index < 1 or index > size then
  16.   return
  17.  end
  18.  for i=index,size-1 do
  19.   worklist[i][1]=worklist[i+1][1]
  20.   worklist[i][2]=worklist[i+1][2]
  21.   worklist[i][3]=worklist[i+1][3]
  22.  end
  23.  worklist[size]=nil
  24.  size=size-1
  25. end
  26.  
  27. function removeCoords(rx, ry, rz)
  28.  local i
  29.  
  30.  i=1
  31.  while i<=size do
  32.   if worklist[i][1] == rx and
  33.      worklist[i][2] == ry and
  34.      worklist[i][3] == rz then
  35.    removeWork(i)
  36.   end
  37.   i=i+1
  38.  end
  39. end
  40.  
  41. function reduceWork()
  42.  local i, j, lx, ly, lz
  43.  
  44.  i=1
  45.  while i<size do
  46.   lx=worklist[i][1]
  47.   ly=worklist[i][2]
  48.   lz=worklist[i][3]
  49.   j=i+1
  50.   while j<=size do
  51.    if worklist[j][1] == lx and
  52.       worklist[j][2] == ly and
  53.       worklist[j][3] == lz then
  54.     removeWork(j)
  55.     j=j-1
  56.    end
  57.    j=j+1
  58.   end
  59.   i=i+1
  60.  end
  61. end
  62.  
  63. function findeNearestPlot()
  64.  local lx,ly,lz
  65.  local mindist=math.huge
  66.  local dist=0
  67.  local res=0
  68.  local i
  69.  
  70.  for i=1,size do
  71.   lx=worklist[i][1]
  72.   ly=worklist[i][2]
  73.   lz=worklist[i][3]
  74.   dist=(lx-tX)*(lx-tX) + (ly-tY)*(ly-tY) + (lz-tZ)*(lz-tZ)
  75.   if dist<mindist then
  76.    mindist=dist
  77.    res=i
  78.   end
  79.  end
  80.  
  81.  return res
  82. end
  83.  
  84. function forceMoveForward()
  85.  local can_move=true
  86.  can_move=turtle.forward()
  87.  while not can_move do
  88.   --print("ERR: There is a something on my way!")
  89.   sleep(0.2)
  90.   --
  91.   turtle.dig()
  92.   turtle.attack()
  93.   --
  94.   can_move=turtle.forward()
  95.  end
  96. end
  97.  
  98. function forceMoveBack()
  99.  local can_move=true
  100.  can_move=turtle.back()
  101.  while not can_move do
  102.   --print("ERR: There is a something on my way!")
  103.   sleep(0.2)
  104.   can_move=turtle.back()
  105.  end
  106. end
  107.  
  108. function forceMoveUp()
  109.  local can_move=true
  110.  can_move=turtle.up()
  111.  while not can_move do
  112.   --print("ERR: There is a something on my way!")
  113.   sleep(0.2)
  114.   --
  115.   turtle.digUp()
  116.   turtle.attackUp()
  117.   --
  118.   can_move=turtle.up()
  119.  end
  120. end
  121.  
  122. function forceMoveDown()
  123.  local can_move=true
  124.  can_move=turtle.down()
  125.  while not can_move do
  126.   --print("ERR: There is a something on my way!")
  127.   sleep(0.2)
  128.   --
  129.   turtle.digDown()
  130.   turtle.attackDown()
  131.   --
  132.   can_move=turtle.down()
  133.  end
  134. end
  135.  
  136. function left()
  137.  if tD[1]==1 and tD[2]==0 then
  138.   tD[1]=0
  139.   tD[2]=-1
  140.  elseif tD[1]==0 and tD[2]==-1 then
  141.   tD[1]=-1
  142.   tD[2]=0
  143.  elseif tD[1]==-1 and tD[2]==0 then
  144.   tD[1]=0
  145.   tD[2]=1
  146.  elseif tD[1]==0 and tD[2]==1 then
  147.   tD[1]=1
  148.   tD[2]=0
  149.  end
  150.  turtle.turnLeft()
  151. end
  152.  
  153. function right()
  154.  if tD[1]==1 and tD[2]==0 then
  155.   tD[1]=0
  156.   tD[2]=1
  157.  elseif tD[1]==0 and tD[2]==1 then
  158.   tD[1]=-1
  159.   tD[2]=0
  160.  elseif tD[1]==-1 and tD[2]==0 then
  161.   tD[1]=0
  162.   tD[2]=-1
  163.  elseif tD[1]==0 and tD[2]==-1 then
  164.   tD[1]=1
  165.   tD[2]=0
  166.  end
  167.  turtle.turnRight()
  168. end
  169.  
  170. function turnL(num1,num2)
  171.  while tD[1]~=num1 or tD[2]~=num2 do
  172.   left()
  173.  end
  174. end
  175.  
  176. function turnR(num1,num2)
  177.  while tD[1]~=num1 or tD[2]~=num2 do
  178.   right()
  179.  end
  180. end
  181.  
  182. function turnB(num1,num2)
  183.  if math.abs(num1-tD[1])==2 then
  184.   turnR(num1,num2)
  185.  
  186.  elseif math.abs(num2-tD[2])==2 then
  187.   turnL(num1,num2)
  188.  
  189.  elseif tD[1]==1 then
  190.   if num2==1 then
  191.    turnR(num1,num2)
  192.   elseif num2==-1 then
  193.    turnL(num1,num2)
  194.   end
  195.  
  196.  elseif tD[1]==-1 then
  197.   if num2==-1 then
  198.    turnR(num1,num2)
  199.   elseif num2==1 then
  200.    turnL(num1,num2)
  201.   end
  202.  
  203.  elseif tD[2]==1 then
  204.   if num1==-1 then
  205.    turnR(num1,num2)
  206.   elseif num1==1 then
  207.    turnL(num1,num2)
  208.   end
  209.  
  210.  elseif tD[2]==-1 then
  211.   if num1==1 then
  212.    turnR(num1,num2)
  213.   elseif num1==-1 then
  214.    turnL(num1,num2)
  215.   end
  216.  
  217.  end
  218. end
  219.  
  220. function moveXYZ(lx,ly,lz)
  221.  local i
  222.  
  223.  if tZ<lz then
  224.   for i=1,lz-tZ do
  225.    forceMoveUp()
  226.   end
  227.  else
  228.   for i=1,tZ-lz do
  229.    forceMoveDown()
  230.   end
  231.  end
  232.  
  233.  if tY<ly then
  234.   turnB(0,1)
  235.   for i=1,ly-tY do
  236.    forceMoveForward()
  237.   end
  238.  elseif tY>ly then
  239.   turnB(0,-1)
  240.   for i=1,tY-ly do
  241.    forceMoveForward()
  242.   end
  243.  end
  244.  
  245.  if tX<lx then
  246.   turnB(1,0)
  247.   for i=1,lx-tX do
  248.    forceMoveForward()
  249.   end
  250.  elseif tX>lx then
  251.   turnB(-1,0)
  252.   for i=1,tX-lx do
  253.    forceMoveForward()
  254.   end
  255.  end
  256.  
  257.  tX,tY,tZ=lx,ly,lz
  258.  removeCoords(lx, ly, lz)
  259. end
  260.  
  261. function upgradeWork()
  262. local lx,ly,lz
  263.  
  264.  lx,ly,lz = tX,tY,tZ
  265.  turtle.dig()
  266.  sum=turtle.getItemCount(1)
  267.  if sum>prevsum then
  268.   lx=lx+tD[1]
  269.   ly=ly+tD[2]
  270.   addWork(lx, ly, lz)
  271.   prevsum=sum
  272.  end
  273.  
  274.  left()
  275.  
  276.  lx,ly,lz = tX,tY,tZ
  277.  turtle.dig()
  278.  sum=turtle.getItemCount(1)
  279.  if sum>prevsum then
  280.   lx=lx+tD[1]
  281.   ly=ly+tD[2]
  282.   addWork(lx, ly, lz)
  283.   prevsum=sum
  284.  end
  285.  
  286.  left()
  287.  
  288.  lx,ly,lz = tX,tY,tZ
  289.  turtle.dig()
  290.  sum=turtle.getItemCount(1)
  291.  if sum>prevsum then
  292.   lx=lx+tD[1]
  293.   ly=ly+tD[2]
  294.   addWork(lx, ly, lz)
  295.   prevsum=sum
  296.  end
  297.  
  298.  left()
  299.  
  300.  lx,ly,lz = tX,tY,tZ
  301.  turtle.dig()
  302.  sum=turtle.getItemCount(1)
  303.  if sum>prevsum then
  304.   lx=lx+tD[1]
  305.   ly=ly+tD[2]
  306.   addWork(lx, ly, lz)
  307.   prevsum=sum
  308.  end
  309.  
  310.  lx,ly,lz = tX,tY,tZ
  311.  turtle.digUp()
  312.  sum=turtle.getItemCount(1)
  313.  if sum>prevsum then
  314.   lz=lz+1
  315.   addWork(lx, ly, lz)
  316.   prevsum=sum
  317.  end
  318.  
  319.  lx,ly,lz = tX,tY,tZ
  320.  turtle.digDown()
  321.  sum=turtle.getItemCount(1)
  322.  if sum>prevsum then
  323.   lz=lz-1
  324.   addWork(lx, ly, lz)
  325.   prevsum=sum
  326.  end
  327.  
  328.  reduceWork()
  329. end
  330.  
  331. ------------------------------------
  332.  
  333. --global variables:
  334. tX,tY,tZ=0,0,0
  335. tD={1,0}
  336. worklist={}
  337. size=0
  338. sum, prevsum=0,0
  339.  
  340. --Locals:
  341. local indx=0
  342. local x,y,z=0,0,0
  343.  
  344.  
  345. turtle.select(1)
  346. turtle.dropUp()
  347. moveXYZ(0,0,-1)
  348.  
  349. upgradeWork()
  350.  
  351. indx=findeNearestPlot()
  352. while indx~=0 do
  353.  x = worklist[indx][1]
  354.  y = worklist[indx][2]
  355.  z = worklist[indx][3]
  356.  removeWork(indx)
  357.  moveXYZ(x,y,z)
  358.  upgradeWork()
  359.  indx=findeNearestPlot()
  360. end
  361.  
  362. moveXYZ(0,0,0)
  363. turnB(1,0)
  364.  
  365. term.setCursorPos(1,1)
  366. term.clear()
  367. print("  *Vykopal!")
  368. print("  *Label: ",os.getComputerLabel())
  369. print("  *Fuel Level:",turtle.getFuelLevel())
Advertisement
Add Comment
Please, Sign In to add comment