Advertisement
artem211

Untitled

Jun 9th, 2015
308
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.28 KB | None | 0 0
  1.  
  2.  
  3.  
  4. local APIS={}
  5. local inv_size = 16
  6. local inv_side = require("sides").left
  7. local charge_side = require("sides").right
  8. local battery_side = require("sides").bottom
  9. local scrap = {
  10.     "minecraft:stone",
  11.     "minecraft:cobblestone",
  12.     "minecraft:dirt",
  13.     "minecraft:gravel",
  14.     "minecraft:sand",
  15.     "minecraft:grass"}
  16. local lc = {x=0, y=0, z=0, dr=3}
  17.  
  18.  
  19. function APIS.charge(charge_side)
  20. comp = require("computer")
  21. while comp.energy() < (comp.maxEnergy()/2) do
  22. --local rs = require("component").redstone
  23. --rs.setOutput(charge_side, 15)
  24. require("term").clear()
  25. APIS.use(charge_side)
  26. print("Зарядка...")
  27. os.sleep(60)
  28. --rs.setOutput(charge_side, 0)
  29. APIS.use(charge_side)
  30. end
  31. print("Батарея заряжена")
  32. return true
  33. end
  34. function APIS.charge_tool(s)
  35. local inv = require("component").inventory_controller
  36. local r = require("robot") 
  37. if r.durability() < 0.2 then
  38. r.select(1)
  39. inv.equip()
  40. if s == 1 then
  41. r.dropUp()
  42. os.sleep(15)
  43. r.suckUp()
  44. elseif s == 2 then
  45. r.turnAround()
  46. r.drop()
  47. os.sleep(15)
  48. r.suck()
  49. r.turnAround()
  50. elseif s == 3 then
  51. r.drop()
  52. os.sleep(15)
  53. r.suck()
  54. elseif s == 4 then
  55. r.turnRight()
  56. r.drop()
  57. os.sleep(15)
  58. r.suck()
  59. r.turnLeft()
  60. elseif s==5 then
  61. r.turnLeft()
  62. r.drop()
  63. os.sleep(15)
  64. r.suck()
  65. r.turnRight()
  66. else
  67. r.dropDown()
  68. os.sleep(15)
  69. r.suckDown()
  70. end
  71. end
  72. end
  73. function APIS.use(s)
  74. local r = require("robot")
  75. if s == 1 then r.useUp()
  76. elseif s == 2 then
  77. r.turnAround()
  78. r.use()
  79. r.turnAround()
  80. elseif s == 3 then r.use()
  81. elseif s == 4 then
  82. r.turnRight()
  83. r.use()
  84. r.turnLeft()
  85. elseif s==5 then
  86. r.turnLeft()
  87. r.use()
  88. r.turnRight()
  89. else
  90. r.useDown()
  91. end
  92. end
  93. function APIS.drop()
  94.  local inv = require("component").inventory_controller
  95.  local r= require("robot")
  96.  if r.detect() then
  97.   if inv.getInventorySize(3) ~= nil then
  98.    for i=1,inv_size do
  99.     r.select(i)
  100.     r.drop()
  101.    end
  102.   else
  103.    print("Блок не является сундуком.")
  104.   end
  105.  else
  106.   print("Нет сундука.")
  107.  end
  108.  r.select(1)
  109. end
  110. function APIS.printState()
  111. print("Статус:")
  112. print(lc.x, lc.y, lc.z, lc.dr)
  113. end
  114. function APIS.harvest(count)
  115.  local r=require("robot")
  116.  for i=1,count do
  117.   r.useDown()
  118.  end
  119. end
  120. function APIS.dig(arg)
  121.  r = require("robot")
  122.  event = require("event")
  123.  if arg == 3 then
  124.   r.swing()
  125.  elseif arg == 1 then
  126.   while r.detectUp() do
  127.    r.swingUp()
  128.    event.pull(0.6)
  129.   end
  130.  elseif arg == 0 then
  131.   r.swingDown()
  132.  end
  133. end
  134. function APIS.ore_analyze(arg)
  135.  if arg ~= nil then
  136.   if require("component").isAvailable("geolyzer") then
  137.    local lyz = require("component").geolyzer
  138.    for i, name in pairs(scrap) do
  139.     if lyz.analyze(arg).name == name then
  140.      --print("Блок - мусор.")
  141.      return false
  142.     end  
  143.    end
  144.    --print("Блок - Руда.")
  145.    return true
  146.   else
  147.    --print("Геолайзер не обнаружен в системе.")
  148.    return false
  149.   end
  150.  end
  151. end
  152. function APIS.dig_ore()
  153.  local r=require("robot")
  154.  if r.detectUp() then
  155.   if APIS.ore_analyze(1) then
  156.    APIS.dig(1)
  157.   end
  158.  end
  159.  if r.detectDown() then
  160.   if APIS.ore_analyze(0) then
  161.    APIS.dig(0)
  162.   end
  163.  end
  164. end
  165. function APIS.turnLeft()
  166. local r = require("robot")
  167. r.turnLeft()
  168. if lc.dr == 3 then
  169. lc.dr = 5
  170. elseif lc.dr == 4 then
  171. lc.dr = 3
  172. elseif lc.dr == 2 then
  173. lc.dr = 4
  174. elseif lc.dr == 5 then
  175. lc.dr = 2
  176. end
  177. end
  178. function APIS.rot(side)
  179.  if (side ~= 1) and (side ~= 0) then
  180.   local sides = require("sides")
  181.   local r = require("robot")  
  182.   while lc.dr ~= side do
  183.    APIS.turnLeft()
  184.   end
  185.  end
  186.  APIS.printState()
  187. end
  188. function APIS.mUp()
  189.  local r = require("robot")
  190.  local try = 1
  191.  while not r.up() == true do
  192.   r.swingUp()
  193.   try = try + 1
  194.   if try >= 10 then
  195.    APIS.back_to_the_future()  
  196.    APIS.printState()
  197.    return false
  198.   end
  199.  end
  200.  if try < 10 then
  201.   lc.y = lc.y + 1
  202.   APIS.printState()
  203.   return true
  204.  end
  205. end
  206. function APIS.mDown()
  207. local r = require("robot")
  208. local event = require("event")
  209. local try = 1
  210. while not r.down() == true do
  211.  r.swingDown()
  212.  try = try + 1
  213.  if try >= 10 then
  214.   APIS.printState()
  215.   APIS.back_to_the_future()
  216.   return false
  217.  end
  218. end
  219.  if try < 10 then
  220.   lc.y = lc.y - 1
  221.   return true
  222.  end
  223. end
  224. function APIS.mForw(action,arg)
  225. local r = require("robot")
  226. local event = require("event")
  227. local try = 1
  228. local sides = require("sides")
  229. if action ~= nil then
  230.  action(arg)
  231. end
  232. while not r.forward() do
  233.  r.swing()
  234.  try = try + 1
  235. if try >= 10 then
  236.  APIS.back_to_the_future()
  237.  return false  
  238. end
  239. end
  240. if try < 10 then
  241. if lc.dr==2 then
  242.  lc.x = lc.x - 1
  243. elseif lc.dr==3 then
  244.  lc.x = lc.x + 1
  245. elseif lc.dr==4 then
  246.  lc.z = lc.z + 1
  247. elseif lc.dr==5 then
  248.  lc.z = lc.z - 1
  249. end  
  250.  APIS.printState()
  251.  return true
  252. end
  253. end
  254. function APIS.mTo(x, y, z, action, arg)
  255. local r = require("robot")
  256. local sides = require("sides")
  257.  if lc.x > x then
  258.   APIS.rot(sides.back)
  259.   while lc.x > x do
  260.    APIS.mForw(action,arg)
  261.   end
  262.  end
  263.  if lc.x < x then
  264.   APIS.rot(sides.forward)
  265.   while lc.x < x do
  266.    APIS.mForw(action,arg)
  267.   end
  268.  end
  269.  if lc.z > z then
  270.   APIS.rot(sides.left)
  271.   while lc.z > z do
  272.    APIS.mForw(action,arg)
  273.   end  
  274.  end
  275.  if lc.z < z then
  276.   APIS.rot(sides.right)
  277.   while lc.z < z do
  278.    APIS.mForw(action,arg)
  279.   end
  280.  end
  281.  while lc.y > y do
  282.   APIS.mDown()
  283.   --print("едем вниз")
  284.  end
  285.  while lc.y < y do
  286.   APIS.mUp()
  287.   --print("едем вверх")
  288.  end
  289. end
  290. function APIS.plane(x, y, z, action, arg, forward)
  291.  local dir = "forward"
  292.  if forward then
  293.   print("Плоскость вперед")
  294.   for j=0,z do
  295.    if dir == "forward" then
  296.     print("Движение вперед")
  297.     for i=0,x do
  298.      print("шаг вперед")
  299.      APIS.mTo(i,y,j,action,arg)
  300.     end
  301.     APIS.check_state()
  302.     dir = "back"
  303.     print("Поворот назад")
  304.    else
  305.     print("Движение назад")
  306.     for i=x,0,-1 do
  307.      print("шаг назад")
  308.      APIS.mTo(i,y,j,action,arg)
  309.     end
  310.     APIS.check_state()
  311.     dir = "forward"
  312.     print("Поворот вперед")
  313.    end
  314.   end
  315.  else
  316.   print("Плоскость обратная")
  317.   for j=z,0,-1 do
  318.    if dir == "forward" then
  319.     for i=x,0,-1 do
  320.      APIS.mTo(i,y,j,action,arg)
  321.     end
  322.     APIS.check_state()
  323.     dir = "back"
  324.    else
  325.     for i=x,0,-1 do
  326.      APIS.mTo(i,y,j,action,arg)
  327.     end
  328.     APIS.check_state()
  329.     dir = "forward"
  330.    end
  331.   end
  332.  end
  333. end
  334. function APIS.home(action,arg)
  335.  APIS.mTo(0,0,0, action,arg)
  336.  APIS.rot(2)
  337.  APIS.drop()
  338.  APIS.rot(3)
  339. end
  340. function APIS.back_to_the_future()
  341.  print("Возврат робота.")
  342.  APIS.mTo(lc.x, 0, lc.z)
  343.  APIS.home()
  344.  print("Робот на исходной. Завершение программы.")
  345.  os.exit()
  346. end
  347. function APIS.check_inv()
  348.  local r = require("robot")
  349.  local i = 16
  350.  local count = 16
  351.  while pcall(r, i) do
  352.   count = i
  353.   i=i+1
  354.  end
  355.  return count
  356. end
  357. function APIS.check_state()
  358.  local r = require("robot")
  359.  local inv = require("component").inventory_controller
  360.  local comp = require("computer")
  361.  local need_fuel = false
  362.  if comp.energy() < (comp.maxEnergy()/4) then
  363.   need_fuel = true
  364.  end
  365.  if (r.count(inv_size-2) > 0) or need_fuel then
  366.   for i=1, inv_size do  
  367.    local slot = inv.getStackInInternalSlot(i)
  368.    for j, name in pairs(scrap) do  
  369.     if slot.name == name then
  370.      r.select(i)
  371.      r.drop()
  372.      --print("Блок - мусор.")
  373.     elseif need_fuel then
  374.      if slot.name == "minecraft:coal" then
  375.       local gen = require("component").generator
  376.        if gen.count() ~= 0 then
  377.         r.select(i)
  378.         gen.insert()
  379.        end
  380.      end
  381.     end  
  382.    end
  383.   end
  384.   r.select(1)
  385.   return true
  386.  else
  387.   return false
  388.  end
  389. end
  390. function APIS.quarry(x, z)
  391.  inv_size = APIS.check_inv()
  392.  for i=0, -256, -3 do  
  393.   if i%2 == 0 then
  394.    
  395.    APIS.plane((x - 1),i,(z - 1), APIS.dig_ore, true)  
  396.   else
  397.    
  398.    APIS.plane((x - 1),i,(z - 1), APIS.dig_ore, false)  
  399.   end  
  400.  end
  401.  APIS.home()
  402. end
  403. function APIS.field(x, y, z, time)
  404.  while true do
  405.   APIS.charge(require("sides").top)
  406.   APIS.plane(x-1, y, z-1, harvest, 2)
  407.   APIS.use(require("sides").down)
  408.   APIS.use(require("sides").down)
  409.   APIS.home()
  410.   print("Ожидание созревания...", time, "минут.")
  411.   os.sleep(60*time)
  412.  end
  413. end
  414. function APIS.chop_single()
  415.  local inv = require("component").inventory_controller
  416.  local beam = require("component").tractor_beam
  417.  local r = require("robot")
  418.  local function start()
  419.   APIS.rot(inv_side)
  420.   APIS.drop()
  421.   inv.suckFromSlot(3, APIS.inv_scaner("minecraft:sapling"), 1)     
  422.   APIS.rot(3)
  423.   r.place()
  424.  end
  425.  start()
  426.  while true do
  427.   require("term").clear()      
  428.   print("Лесоруб, одиночное дерево. Ожидание роста саженца.")
  429.   local _, wood = r.detect()
  430.   if wood == "solid" then
  431.    print("Рубка дерева.")           
  432.    r.swing()
  433.    os.sleep(5)
  434.    for i=1, 8 do
  435.     beam.suck()
  436.    end
  437.    start()     
  438.    require("term").clear()
  439.    APIS.charge(charge_side)
  440.    APIS.charge_tool(battery_side)
  441.   end
  442.   os.sleep(10)
  443.  end
  444. end
  445. function APIS.inv_scaner(filter)
  446.  local inv = require("component").inventory_controller
  447.  ins = inv.getInventorySize(3)
  448.  if ins ~= nil then
  449.   for i=1, ins do
  450.    if inv.getStackInSlot(3, i).name == filter then
  451.     print(filter.."найден!")
  452.     return i
  453.    end
  454.   end
  455.  print("Найти "..filter.."не удалось.")
  456.  return 0
  457.  end
  458. end
  459.  
  460.  
  461. return APIS
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement