Advertisement
Guest User

OC_Robot_Farm_API

a guest
May 30th, 2015
252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.40 KB | None | 0 0
  1.  
  2.  
  3.  
  4. local APIS={}
  5. local inv_slots = 16
  6. local inv_side = require("sides").left
  7. local lc = {x=0, y=0, z=0, dr=3}
  8. local charge_side = require("sides").top
  9. local battery_side = require("sides").bottom
  10.  
  11. function APIS.charge(charge_side)
  12. comp = require("computer")
  13. while comp.energy() < (comp.maxEnergy()/2) do
  14. --local rs = require("component").redstone
  15. --rs.setOutput(charge_side, 15)
  16. require("term").clear()
  17. APIS.use(charge_side)
  18. print("Зарядка...")
  19. os.sleep(60)
  20. --rs.setOutput(charge_side, 0)
  21. APIS.use(charge_side)
  22. end
  23. print("Батарея заряжена")
  24. return true
  25. end
  26. function APIS.charge_tool(s)
  27.     local inv = require("component").inventory_controller
  28.     local r = require("robot") 
  29.     if r.durability() < 0.2 then
  30.         r.select(1)
  31.         inv.equip()
  32.         if s == 1 then
  33.             r.dropUp()
  34.             os.sleep(15)
  35.             r.suckUp()
  36.         else
  37.             if s == 2 then
  38.                 r.turnAround()
  39.                 r.drop()
  40.                 os.sleep(15)
  41.                 r.suck()
  42.                 r.turnAround()
  43.             else
  44.             if s == 3 then
  45.                 r.drop()
  46.                 os.sleep(15)
  47.                 r.suck()
  48.             else
  49.                 if s == 4 then
  50.                     r.turnRight()
  51.                     r.drop()
  52.                     os.sleep(15)
  53.                     r.suck()
  54.                     r.turnLeft()
  55.                 else
  56.                 if s==5 then
  57.                     r.turnLeft()
  58.                     r.drop()
  59.                     os.sleep(15)
  60.                     r.suck()
  61.                     r.turnRight()
  62.                 else
  63.                     r.dropDown()
  64.                     os.sleep(15)
  65.                     r.suckDown()
  66.                 end
  67.                 end
  68.             end
  69.             end
  70.         end
  71.     end
  72. end
  73. function APIS.use(s)
  74. local r = require("robot")
  75. if s == 1 then r.useUp() else
  76.  if s == 2 then
  77.  r.turnAround()
  78.  r.use()
  79.  r.turnAround()
  80.  else
  81.   if s == 3 then r.use() else
  82.    if s == 4 then
  83.    r.turnRight()
  84.    r.use()
  85.    r.turnLeft()
  86.    else
  87.     if s==5 then
  88.   r.turnLeft()
  89.     r.use()
  90.     r.turnRight()
  91.   else
  92.   r.useDown()
  93.   end
  94.    end
  95.   end
  96.  end
  97. end
  98. end
  99. function APIS.drop()
  100.  local r= require("robot")
  101. for i=1,inv_slots do
  102.  r.select(i)
  103.  r.drop()
  104.  end
  105.  r.select(1)
  106. end
  107. function APIS.printState()
  108. print("Статус:")
  109. print(lc.x, lc.y, lc.z, lc.dr)
  110. end
  111. function turnLeft()
  112.   local r = require("robot")
  113.   r.turnLeft()
  114.   if lc.dr == 3 then
  115.    lc.dr = 5
  116.   elseif lc.dr == 4 then
  117.    lc.dr = 3
  118.   elseif lc.dr == 2 then
  119.    lc.dr = 4
  120.   elseif lc.dr == 5 then
  121.    lc.dr = 2
  122.   end
  123. end
  124. function APIS.rot(side)
  125. local sides = require("sides")
  126. local r = require("robot")  
  127.     while lc.dr ~= side do
  128.       turnLeft()
  129.     end
  130. APIS.printState()
  131. end
  132. function APIS.mUp()
  133.  local r = require("robot")
  134.  local try = 1
  135.  while not r.up() == true do
  136.  try = try + 1
  137.  event.pull(0.4)
  138.   if try >= 10 then    
  139.    APIS.printState()
  140.    return false
  141.   end
  142.  end
  143.  if try < 10 then
  144.  lc.y = lc.y + 1
  145.  APIS.printState()
  146.  return true
  147.  end
  148. end
  149. function APIS.mDown()
  150.  local r = require("robot")
  151.  local event = require("event")
  152.  local try = 1
  153.  while not r.down() == true do
  154.  try = try + 1
  155.  event.pull(0.4)
  156.   if try >= 10 then
  157.    APIS.printState()
  158.    return false
  159.   end
  160.  end
  161.  if try < 10 then
  162.  lc.y = lc.y - 1
  163.  return true
  164.  end
  165. end
  166. function APIS.mForw(harv)
  167.  local r = require("robot")
  168.  local event = require("event")
  169.  local try = 1
  170.  local sides = require("sides")
  171.  if harv == true then
  172.  --print("Собираем")
  173.  APIS.use(sides.down)
  174.  APIS.use(sides.down)
  175.  end
  176.  while not r.forward() do
  177.  try = try + 1
  178.  event.pull(0.4)
  179.   if try >= 10 then
  180.    return false  
  181.   end
  182.  end
  183.  if try < 10 then
  184.   if lc.dr==2 then
  185.   lc.x = lc.x - 1
  186.   elseif lc.dr==3 then
  187.    lc.x = lc.x + 1
  188.   elseif lc.dr==4 then
  189.    lc.z = lc.z + 1
  190.   elseif lc.dr==5 then
  191.    lc.z = lc.z - 1
  192.   end  
  193.  APIS.printState()
  194.  return true
  195.  end
  196. end
  197. function APIS.mTo(x, y, z, harv)
  198.  local r = require("robot")
  199.  local sides = require("sides")
  200.  while lc.y < y do
  201.   APIS.mUp()
  202.   --print("едем вверх")
  203.  end
  204.  if lc.x > x then
  205.   APIS.rot(sides.back)
  206.   while lc.x > x do
  207.    APIS.mForw(harv)
  208.   end
  209.  end
  210.  if lc.x < x then
  211.   APIS.rot(sides.forward)
  212.   while lc.x < x do
  213.    APIS.mForw(harv)
  214.   end
  215.  end
  216.  if lc.z > z then
  217.   APIS.rot(sides.left)
  218.   while lc.z > z do
  219.    APIS.mForw(harv)
  220.   end  
  221.  end
  222.  if lc.z < z then
  223.   APIS.rot(sides.right)
  224.   while lc.z < z do
  225.    APIS.mForw(harv)
  226.   end
  227.  end
  228.   while lc.y > y do
  229.   APIS.mDown()
  230.   print("едем вниз")
  231.  end
  232. end
  233. function APIS.plane(x,z,harv)
  234. local dir = "forward"
  235. for j=0,z do
  236.  if dir == "forward" then
  237.   for i=0,x do
  238.    APIS.mTo(i,0,j,harv)
  239.   end
  240.   dir = "back"
  241.  else
  242.   for i=x,0,-1 do
  243.    APIS.mTo(i,0,j,harv)
  244.   end
  245.   dir = "forward"
  246.  end
  247. end
  248. end
  249. function APIS.home(harv)
  250.  APIS.mTo(0,0,0, harv)
  251.  APIS.rot(2)
  252.  APIS.drop()
  253.  APIS.rot(3)
  254. end
  255. function APIS.field(x,z, time)
  256. while true do
  257.     APIS.charge(require("sides").top)
  258.     APIS.plane(x,z, true)
  259.     APIS.use(require("sides").down)
  260.     APIS.use(require("sides").down)
  261.     APIS.home()
  262.     print("Ожидание созревания...", time, "минут.")
  263.     os.sleep(60*time)
  264. end
  265. end
  266. function APIS.chop_single()
  267.     local inv = require("component").inventory_controller
  268.     local beam = require("component").tractor_beam
  269.     local r = require("robot") 
  270.     while true do
  271.         local _, wood = r.detect()
  272.         if wood == "solid" then
  273.             r.swing()
  274.             os.sleep(2)
  275.             for i=1, 3 do
  276.                 beam.suck()
  277.             end
  278.             for i=1, inv_slots do
  279.                 a=inv.getStackInInternalSlot(i)["name"]
  280.                 if a=="minecraft:sapling" then
  281.                     r.select(i)
  282.                     r.place()
  283.                     r.select(1)
  284.                     break
  285.                 end
  286.             end
  287.         APIS.rot(inv_side)
  288.         APIS.drop()
  289.         APIS.rot(3)
  290.         APIS.charge(charge_side)
  291.         APIS.charge_tool(battery_side)
  292.         end
  293.         os.sleep(10)
  294.     end
  295. end
  296.  
  297.  
  298. return APIS
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement