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