kLeeNex24

dig old

Jul 28th, 2013
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.05 KB | None | 0 0
  1. pars = {}
  2. parsNames = {"position", "width", "height", "depth", "fuelType" }
  3. fuelArray = { 6, 18, 96, 1000 }
  4. fuelNames = { "sticks", "wood/Planks", "coal", "lava" }
  5.  
  6. mM = { {true, true},{false, true}, {true, false}, {false, false} }
  7. -- {1( isToRight, isToDown ),2,3,4} movingModificators
  8.  
  9. function hM(isFromPoint, count) --heightMove
  10.  if isFromPoint == mM[pars[1]][2] then
  11.   turtlex.down(count, "dig")
  12.  else
  13.   turtlex.up(count, "dig")
  14.  end  
  15. end
  16.  
  17. function hD(isFromPoint) --heightDig
  18.  if isFromPoint == mM[pars[1]][2] then
  19.   turtlex.digDown()
  20.  else
  21.   turtlex.digUp()
  22.  end  
  23. end
  24.  
  25. function forward(count, onBlocked, isDigFrom, isDigTo)
  26.  for i=1,count do
  27.   turtlex.forward(1, onBlocked)
  28.   if isDigFrom then hD(true) end
  29.   if isDigTo then hD(false) end
  30.  end
  31. end
  32.  
  33. function turn(isRight)
  34. if isRight == mM[pars[1]][1] then
  35.   turtlex.turnRight()
  36.  else
  37.   turtlex.turnLeft()
  38.  end  
  39. end
  40.  
  41. function fuelNeeded(width, height, depth)
  42. aLevels = height % 3 --additional levels
  43. mLevels = height - aLevels --main levels
  44. Levels = mLevels / 3
  45. count = Levels * ( width * depth + 2 ) + height + 1
  46.  
  47. if aLevels ~= 0 then
  48.  count = count + width * depth + aLevels - 1
  49.  Levels = Levels + 1
  50. end
  51.  
  52. if depth % 2 ~= 0 then
  53.  count = count + width
  54. end
  55.  
  56. if Levels % 2 ~= 0 then
  57.  count = count + depth
  58. end
  59.  
  60. return count
  61. end
  62.  
  63. function dig1e(w, RorL, isDigFrom, isDigTo) --dig one horizontal element of the level
  64. forward(w - 1, "dig", isDigFrom, isDigTo)
  65. turn(RorL)
  66. end
  67.  
  68. function dig1l(w, d, isRight, isDigFrom, isDigTo)
  69.  for i = 1, d do
  70.   dig1e(w, not isRight, isDigFrom, isDigTo)
  71.  
  72.   if i ~= d then
  73.    forward(1, "dig", isDigFrom, isDigTo)
  74.   end
  75.  
  76.   turn(not isRight)
  77.  
  78.   isRight = not isRight
  79.  end
  80.  
  81. end
  82.  
  83. function dig(w, h, d)
  84.  turtlex.pushPos()
  85.  isRight = true
  86.  isDigFrom = false
  87.  isDigTo = false
  88.  aLevels = h % 3 --additional levels
  89.  mLevels = (h - aLevels)/3 --main levels
  90.  
  91.  turtlex.forward(1, "dig")
  92.  turn(true)
  93.  
  94.  if aLevels ~= 0 then --need to correct
  95.    if aLevels == 2 then
  96.     hD(true)
  97.     hM(true, 1)
  98.     isDigTo = true
  99.    end
  100.  
  101.   dig1l(w, d, isRight, isDigFrom, isDigTo)
  102.   if (d % 2 == 0) then
  103.    isRight = not isRight
  104.   end
  105.  
  106.   if mLevels ~= 0 then
  107.    hM(true, 1)
  108.   end
  109.  end
  110.  
  111.  isDigFrom = true
  112.  isDigTo = true
  113.  
  114.  for i = 1,mLevels do
  115.   hM(true, 1)
  116.   hD(true)
  117.   dig1l(w, d, isRight, isDigFrom, isDigTo)
  118.   if (d % 2 == 0) then
  119.    isRight = not isRight
  120.   end  
  121.    if i ~= mLevels then
  122.     hM(true, 2)
  123.    end
  124.  end
  125. turtlex.popPos()
  126. end
  127.  
  128.  
  129. -- main code --
  130. term.clear()
  131. term.setCursorPos(1,1)
  132. print[[Enter the 1)position:
  133. 1--2
  134. |  |
  135. 3--4
  136. 2)width, 3)height, 4)depth, 5)fuelType:
  137. 1-stick,2-wood/Planks,3-coal,4-lavaBucket
  138. e.g. "1 2 3 4 1"]]
  139.  
  140. repeat
  141. iHasPars = true
  142. k = 0
  143.  errorStr = ""
  144.  input = read()
  145.  pars = {}
  146.   for v in string.gmatch(input, '%w+') do
  147.   k = k + 1  
  148.     if k <= table.getn(parsNames) then --check it    
  149.       pars[k] = tonumber(v)
  150.       if pars[k] == nil then
  151.         iHasPars = false
  152.         errorStr = parsNames[k] .. " is sets to \"" .. v .. "\", it must be a number"
  153.         break
  154.    
  155.       elseif pars[1] > 4 or pars[1] < 1 then
  156.         iHasPars = false
  157.         errorStr = parsNames[1] .. " must be 0<pos<5"
  158.         break
  159.        
  160.        elseif k == 5 then
  161.         if pars[5] > table.getn(fuelArray) or pars[5] < 1 then
  162.          iHasPars = false
  163.          errorStr = parsNames[5] .. " must be 0<fuelType<" .. table.getn(fuelArray) + 1
  164.          break
  165.         end
  166.       end  
  167.  
  168.     else
  169.      break
  170.     end
  171.  
  172.  
  173. --print("k=" .. k .. ", iHasPars = " .. tostring(iHasPars) .. " " .. errorStr)
  174.  end
  175.  
  176. if iHasPars == true and table.getn(pars) < table.getn(parsNames) then
  177.  iHasPars = false
  178.  errorStr = parsNames[table.getn(pars) + 1] .. " is missing"
  179. end  
  180.  
  181.  
  182. if iHasPars == false then
  183. print(errorStr)
  184. end
  185. until iHasPars == true
  186.  
  187. fuelN = fuelNeeded(pars[2], pars[3], pars[4])
  188. if fuelN > turtle.getFuelLevel() then
  189.  print("put " .. math.ceil(( fuelN - turtle.getFuelLevel() ) / fuelArray[pars[5]]) .. " " .. fuelNames[pars[5]] .. " into my 1st slot plz" )
  190.  print("press Enter to refuel")
  191.  read()
  192.  turtle.refuel()
  193.  
  194.    while fuelN > turtle.getFuelLevel() do
  195.     print("error, i hasnt enough fuel. Type the number of new fuelType or press Enter if you want'nt change it")
  196.  
  197.     repeat
  198.      iHasPars = true
  199.      input = read()
  200.       if input ~= "" then
  201.         pars[5] = tonumber(input)
  202.          if pars[5] ~= nil then    
  203.          if pars[5] > table.getn(fuelArray) or pars[5] < 1 then
  204.            iHasPars = false
  205.            print("fuelType must be 0<fuelType<" .. table.getn(fuelArray) + 1)        
  206.          end
  207.          else
  208.          print("fuelType must be the number")
  209.          iHasPars = false
  210.          end    
  211.        end
  212.       until iHasPars == true
  213.  
  214.       print("put " .. math.ceil(( fuelN - turtle.getFuelLevel() ) / fuelArray[pars[5]]) .. " " .. fuelNames[pars[5]] .. " into my 1st slot plz" )
  215.       print("press Enter to refuel")
  216.       read()
  217.       turtle.refuel()
  218.    end
  219. end
  220.  
  221.  
  222.  
  223.  
  224. dig(pars[2], pars[3], pars[4])
  225. print(turtle.getFuelLevel() .. " fuel remaining")
Add Comment
Please, Sign In to add comment