Advertisement
Semior001

tt

Jan 12th, 2020
399
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.21 KB | None | 0 0
  1. revision = "0.0.2b"
  2.  
  3. -- logging
  4. function dump(o)
  5.     if type(o) == 'table' then
  6.         local s = '{ \n';
  7.         for k,v in pairs(o) do
  8.             s = s .. "    " .. k ..': ' .. dump(v) .. ',\n';
  9.         end
  10.         return s .. '}';
  11.     else
  12.         return tostring(o);
  13.     end
  14. end
  15.  
  16. logToFile = false -- indicates the neccessarity to log to file
  17.  
  18. function err(s)
  19.     if logToFile then
  20.         file.write(os.date() .. " [ERR] " .. s)
  21.     end
  22.     print(os.date() .. " [ERR] " .. s)
  23. end
  24. function info(s)
  25.     if logToFile then
  26.         file.write(os.date() .. " [INFO] " .. s)
  27.     end
  28.     print(os.date() .. " [INFO] " .. s)
  29. end
  30. function warn(s)
  31.     if logToFile then
  32.         file.write(os.date() .. " [WARN] " .. s)
  33.     end
  34.     print(os.date() .. " [WARN] " .. s)
  35. end
  36. function debug(s)
  37.     if logToFile then
  38.         file.write(os.date() .. " [DEBUG] " .. s)
  39.     end
  40.     print(os.date() .. " [DEBUG] " .. s)
  41. end
  42.  
  43. -- common functions
  44. function try(--[[required]]fn, --[[optional]]fnArgs, --[[optional]]fnName, --[[optional]]args, --[[optional]]maySkip, --[[optional]]errFn)
  45.     if fnName == nil then
  46.         fnName = "unknown()"
  47.     end
  48.     fnName = fnName .. ": "
  49.  
  50.     if maySkip == nil then
  51.         maySkip = false
  52.     end
  53.    
  54.     if errFn == nil then
  55.         errFn = err
  56.     end
  57.  
  58.     local scs = false
  59.  
  60.     repeat
  61.         scs = fn(fnArgs)
  62.         if not scs then
  63.             errFn(fnName .. ", fnArgs: " .. dump(fnArgs) .. ", args: " .. dump(args))
  64.         end
  65.     until(scs or maySkip)
  66.  
  67. end
  68.  
  69. -- program itself
  70. function digRoom(length, width, height)
  71.     for h=1, height do
  72.         digPlane(length, width)
  73.         returnToStartPoint(length, width)
  74.         if needToDrop() then
  75.             for hh = 1, h do
  76.                 turtle.up()
  77.             end
  78.             try(dropUpLoot, nil, "dropUpLoot()", nil, true, warn)
  79.             for hh = 1, h do
  80.                 turtle.down()
  81.             end
  82.         end
  83.         if h < height then
  84.             turtle.digDown()
  85.             turtle.down()
  86.         end
  87.     end
  88. end
  89.  
  90. function digPlane(length, width)
  91.     for w = 1, width do
  92.         for l = 1, length-1 do
  93.             -- turtle.dig()
  94.             try(turtle.dig, nil, "turtle.dig()", {currL = l, currW = w}, true, warn)
  95.             try(turtle.forward, nil, "turtle.forward()", {currL = l, currW = w})
  96.         end
  97.        
  98.         if (w % 2 ~= 0) and (w < width) then
  99.             turtle.turnLeft()
  100.             -- turtle.dig()
  101.             try(turtle.dig, nil, "turtle.dig(turning)", {currW = w}, true, warn)
  102.             try(turtle.forward, nil, "turtle.forward(turning)", {currW = w})
  103.             turtle.turnLeft()
  104.         elseif (w < width) then
  105.             turtle.turnRight()
  106.             -- turtle.dig()
  107.             try(turtle.dig, nil, "turtle.dig(turning)", {currW = w}, true, warn)
  108.             try(turtle.forward, nil, "turtle.forward(turning)", {currW = w})
  109.             turtle.turnRight()
  110.         end
  111.  
  112.     end
  113. end
  114.  
  115. function dropUpLoot()
  116.     if not turtle.detectUp() then
  117.         return false
  118.     end
  119.  
  120.     for i = 1, 16 do
  121.         turtle.select(i)
  122.         turtle.dropUp()
  123.     end
  124.     return true
  125. end
  126.  
  127. function needToDrop()
  128.     sum = 0
  129.     for i = 1, 16 do
  130.         turtle.select(i)
  131.         if turtle.getItemCount() <= 0 then
  132.             sum = sum + 1
  133.         end
  134.     end
  135.     return sum <= 3
  136. end
  137.  
  138. -- todo: rewrite
  139. function returnToStartPoint(length, width)
  140.     if width % 2 == 0 then
  141.         turtle.turnLeft()
  142.         for w = 1, width-1 do
  143.             try(turtle.forward, nil, "turtle.forward(returning)", {currW = w})
  144.         end
  145.         turtle.turnLeft()
  146.         return
  147.     end
  148.     turtle.turnRight()
  149.     for w = 1, width-1 do
  150.         try(turtle.forward, nil, "turtle.forward(returning)", {currW = w})
  151.     end
  152.     turtle.turnRight()
  153.     for l = 1, length-1 do
  154.         try(turtle.forward, nil, "turtle.forward(returning)", {currL = l})
  155.     end
  156.     turtle.turnRight()
  157.     turtle.turnRight()
  158. end
  159.  
  160. function usage()
  161.     print("roomdigger by @semior001 https://github.com/semior001")
  162.     print("usage: rd length width height [logToFile]")
  163. end
  164.  
  165. function main(args)
  166.     if #args < 3 then
  167.         usage()
  168.         return
  169.     end
  170.    
  171.     if #args == 4 then
  172.         logToFile = args[4] == "true"
  173.     end
  174.    
  175.     logToFile = true
  176.     if logToFile then
  177.         file = fs.open("roomdigger.log","w")
  178.     end
  179.    
  180.     -- procedures call
  181.     info("roomdigger, revision " .. revision .. " - started program execution")
  182.     local length = tonumber(args[1])
  183.     local width = tonumber(args[2])
  184.     local height = tonumber(args[3])
  185.  
  186.     -- checking fuel
  187.  
  188.     initFuel = turtle.getFuelLevel()
  189.     requiredFuelAmount = length * width * height + (width-1 + length-1) * height
  190.     if initFuel < requiredFuelAmount then
  191.         neededFuel = requiredFuelAmount - initFuel
  192.         print("not enough fuel, needed " .. neededFuel .. " more fuel")
  193.         return
  194.     end
  195.  
  196.     digRoom(length, width, height)
  197.    
  198.     currFuel = turtle.getFuelLevel()
  199.     spentFuel = initFuel - currFuel
  200.  
  201.     print("spent " .. spentFuel)
  202.     print(currFuel .. " fuel left")
  203.  
  204.     if logToFile then
  205.         file.close()
  206.     end
  207. end
  208.  
  209. local args = { ... }
  210. main(args)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement