Advertisement
artem211

api

Jun 9th, 2015
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.87 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.   for j=0,z do
  294.    if dir == "forward" then
  295.     for i=0,x do
  296.      APIS.mTo(i,y,j,action,arg)
  297.     end
  298.     APIS.check_state()
  299.     dir = "back"
  300.    else
  301.     for i=x,0,-1 do
  302.      APIS.mTo(i,y,j,action,arg)
  303.     end
  304.     APIS.check_state()
  305.     dir = "forward"
  306.    end
  307.   end
  308.  else
  309.   for j=z,0,-1 do
  310.    if dir == "forward" then
  311.     for i=x,0,-1 do
  312.      APIS.mTo(i,y,j,action,arg)
  313.     end
  314.     APIS.check_state()
  315.     dir = "back"
  316.    else
  317.     for i=x,0,-1 do
  318.      APIS.mTo(i,y,j,action,arg)
  319.     end
  320.     APIS.check_state()
  321.     dir = "forward"
  322.    end
  323.   end
  324.  end
  325. end
  326. function APIS.home(action,arg)
  327.  APIS.mTo(0,0,0, action,arg)
  328.  APIS.rot(2)
  329.  APIS.drop()
  330.  APIS.rot(3)
  331. end
  332. function APIS.back_to_the_future()
  333.  print("Возврат робота.")
  334.  APIS.mTo(lc.x, 0, lc.z)
  335.  APIS.home()
  336.  print("Робот на исходной. Завершение программы.")
  337.  os.exit()
  338. end
  339. function APIS.check_inv()
  340.  local r = require("robot")
  341.  local i = 16
  342.  local count = 16
  343.  while pcall(r, i) do
  344.   count = i
  345.   i=i+1
  346.  end
  347.  return count
  348. end
  349. function APIS.check_state()
  350.  local r = require("robot")
  351.  local inv = require("component").inventory_controller
  352.  local need_fuel = false
  353.  if comp.energy() < (comp.maxEnergy()/4) then
  354.   need_fuel = true
  355.  end
  356.  if (r.count(inv_size-2) > 0) or need_fuel then
  357.   for i=1, inv_size do  
  358.    local slot = inv.getStackInInternalSlot(i)
  359.    for j, name in pairs(scrap) do  
  360.     if slot.name == name then
  361.      r.select(i)
  362.      r.drop()
  363.      --print("Блок - мусор.")
  364.     elseif need_fuel then
  365.      if slot.name == "minecraft:coal" then
  366.       local gen = require("component").generator
  367.        if gen.count() ~= 0 then
  368.         r.select(i)
  369.         gen.insert()
  370.        end
  371.      end
  372.     end  
  373.    end
  374.   end
  375.   r.select(1)
  376.   return true
  377.  else
  378.   return false
  379.  end
  380. end
  381. function APIS.quarry(x, y, z)
  382.  inv_size = APIS.check_inv()
  383.  for i=0, -y, -3 do
  384.   APIS.mTo(0, i, 0)
  385.   APIS.plane((x - 1),i,(z - 1), APIS.dig_ore)  
  386.   APIS.mTo(0, i, 0)
  387.  end
  388.  APIS.home()
  389. end
  390. function APIS.field(x, y, z, time)
  391.  while true do
  392.   APIS.charge(require("sides").top)
  393.   APIS.plane(x-1, y, z-1, harvest, 2)
  394.   APIS.use(require("sides").down)
  395.   APIS.use(require("sides").down)
  396.   APIS.home()
  397.   print("Ожидание созревания...", time, "минут.")
  398.   os.sleep(60*time)
  399.  end
  400. end
  401. function APIS.chop_single()
  402.  local inv = require("component").inventory_controller
  403.  local beam = require("component").tractor_beam
  404.  local r = require("robot")
  405.  local function start()
  406.   APIS.rot(inv_side)
  407.   APIS.drop()
  408.   inv.suckFromSlot(3, APIS.inv_scaner("minecraft:sapling"), 1)     
  409.   APIS.rot(3)
  410.   r.place()
  411.  end
  412.  start()
  413.  while true do
  414.   require("term").clear()      
  415.   print("Лесоруб, одиночное дерево. Ожидание роста саженца.")
  416.   local _, wood = r.detect()
  417.   if wood == "solid" then
  418.    print("Рубка дерева.")           
  419.    r.swing()
  420.    os.sleep(5)
  421.    for i=1, 8 do
  422.     beam.suck()
  423.    end
  424.    start()     
  425.    require("term").clear()
  426.    APIS.charge(charge_side)
  427.    APIS.charge_tool(battery_side)
  428.   end
  429.   os.sleep(10)
  430.  end
  431. end
  432. function APIS.inv_scaner(filter)
  433.  local inv = require("component").inventory_controller
  434.  ins = inv.getInventorySize(3)
  435.  if ins ~= nil then
  436.   for i=1, ins do
  437.    if inv.getStackInSlot(3, i).name == filter then
  438.     print(filter.."найден!")
  439.     return i
  440.    end
  441.   end
  442.  print("Найти "..filter.."не удалось.")
  443.  return 0
  444.  end
  445. end
  446.  
  447.  
  448. return APIS
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement