Advertisement
Guest User

robot.lua

a guest
Aug 26th, 2016
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.96 KB | None | 0 0
  1. local component = require "component"
  2. local computer = require "computer"
  3. local generator = component.generator
  4. local term = require "term"
  5. local robot = require "robot"
  6. local sides = require "sides"
  7.  
  8. local ISGENERATOR = component.isAvailable("generator")
  9. local TORCHSLOT = 1
  10. local SECTIONENERGY
  11. local SUBTUNNELLENGTH, SECTIONLENGTH --TODO
  12. local energy = computer.energy()
  13.  
  14. local function detect(type, direction)  --Обнаружение определенноо типа [направления] роботом. Возвращает boolean.
  15.   local detection
  16.   if direction == "up" then detection = select(2, robot.detectUp())
  17.    elseif direction == "down" then detection = select(2, robot.detectDown())
  18.    else
  19.     detection =  select(2, robot.detect())
  20.   end
  21.   if detection  == type then
  22.     return true
  23.   end
  24. end
  25.  
  26. local function unloading() --TODO
  27.   robot.turnAround()
  28.   for i = 4, 16 do
  29.     robot.select(i)
  30.     robot.drop()
  31.   end
  32.   robot.turnAround()
  33. end
  34.  
  35. local function gen(slot) --TODO
  36.   if math.floor(computer.energy()) <= 200000 and generator.count() == 0 then
  37.     robot.select(slot)
  38.     generator.insert()
  39.   end
  40.   --while generator.count() ~= 0 do os.sleep(1) end
  41. end
  42.  
  43. local function checkEnergy(sectCount)
  44.   if  math.floor(computer.energy()) <= 1000*sectCount + SECTIONENERGY then
  45.     while math.floor(computer.energy()) <= 1000*sectCount + SECTIONENERGY + 30000 do
  46.       print(math.floor(computer.energy()))
  47.       os.sleep(20)
  48.     end
  49.   end
  50. end
  51.  
  52.  
  53. local function placeBlock(types, slot, direction)
  54.   robot.select(slot)
  55.   for _, type in pairs(types) do
  56.     if detect(type,direction) then
  57.       if direction == "up"  then
  58.         robot.placeUp()
  59.        elseif direction == "down"then
  60.         robot.placeDown()
  61.        else robot.place()
  62.       end
  63.     end
  64.   end
  65. end
  66.  
  67.  
  68. local function killLiquid(slot)
  69.   types = {"liquid", "replaceable"}
  70.  
  71.   placeBlock(types, slot, "up")
  72.   placeBlock(types, slot,"down")
  73.   placeBlock(types, slot)
  74.   robot.turnRight()
  75.   placeBlock(types, slot)
  76.   robot.turnAround()
  77.   placeBlock(types, slot)
  78.   robot.turnRight()
  79.  
  80. end
  81.  
  82. function placeTorch()
  83.   robot.turnAround()
  84.   robot.select(TORCHSLOT)
  85.   robot.place()
  86.   robot.turnAround()
  87. end
  88.  
  89. local function tunnel(numSteps, height, isBack)  --REFACTOR!!
  90.   local types = {"air", "liquid", "replaceable"}
  91.  
  92.   for i = 1, numSteps  do
  93.     if robot.durability() == nil then
  94.       print("Place pickaxe and input something")
  95.       term.read()
  96.     end
  97.     while robot.detect() do robot.swing()  end
  98.     robot.forward()
  99.     if not isBack then placeBlock(types, 2, "down") end
  100.     if (i%13 == 0 or i == 2) and height > 1  then placeTorch() end
  101.     for i = 1, height-1 do
  102.       while robot.detectUp() do robot.swingUp() end
  103.       robot.up()
  104.     end
  105.     for i = 1, height-1 do
  106.       while robot.detectDown() do robot.swingDown() end
  107.       robot.down()
  108.     end
  109.   end
  110. end
  111.  
  112. local function section(length)
  113.   local function subTunnels(length)
  114.     robot.turnLeft()
  115.     tunnel(length,1)
  116.     robot.turnAround()
  117.     tunnel(length,1,true)
  118.     tunnel(length,1)
  119.     robot.turnAround()
  120.     tunnel(length,1,true)
  121.     robot.turnRight()
  122.   end
  123.  
  124.   tunnel(length, 2)
  125.   placeTorch()
  126.   robot.up()
  127.   robot.turnAround()
  128.   local count = math.floor(length/4)
  129.   repeat
  130.     subTunnels(5)
  131.     tunnel(4, 1, true)
  132.     count = count - 1
  133.   until count == 0
  134.   robot.turnAround()
  135.   robot.down()
  136. end
  137.  
  138. local function miner(sectionCount,sectionLength)
  139.   SECTIONENERGY = computer.energy()
  140.   gen(3)
  141.   section(sectionLength)
  142.   unloading()
  143.   SECTIONENERGY = SECTIONENERGY - computer.energy() + 1000
  144.   for i = 1, sectionCount do
  145.     robot.turnLeft()
  146.     tunnel(11*i, 2)
  147.     robot.turnRight()
  148.     section(sectionLength)
  149.     robot.up()
  150.     robot.turnRight()
  151.     tunnel(11*i, 1, true)
  152.     robot.turnLeft()
  153.     robot.down()
  154.     unloading()
  155.     checkEnergy(i)
  156.   end
  157.  
  158. end
  159.  
  160. term.clear()
  161. print("Input section count: ")
  162. local secCount = term.read()
  163. print("Input section length: ")
  164. local secLength = term.read()
  165. print('Start energy: '..energy)
  166. miner(secCount,secLength)
  167. print('Energy used  : ' .. math.floor(energy - computer.energy()))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement