Advertisement
BombBloke

Feeder (OpenPeripheral)

Jan 5th, 2014
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.80 KB | None | 0 0
  1. -- Feeder
  2. -- ----------------------------------------------------------
  3.  
  4. -- Goes around and dumps various forms of animal food into
  5. -- automated feeder units.
  6.  
  7. -- ----------------------------------------------------------
  8.  
  9. -- Initialise important values:
  10.  
  11. -- ----------------------------------------------------------
  12.  
  13. -- Locations of interest:
  14. local node = {{255,447,66,"south"}, --   1, feed chest (turtle will face south when it gets here, make sure that's where the ME system is)
  15.     {255,441,65,"west",true},   --   2, chickens. (turtle will face west here, but all feed will be placed downwards)
  16.     {255,443,65,"west"},        --   3, a way point where no feeding will be performed.
  17.     {255,442,65,"west"},        --   4, another waypoint.
  18.     {255,442,65,"west",true}}   --   5, another feeding point.
  19.  
  20. local facing = {"north","east","south","west"}
  21. local direction,x,y,z,counter
  22. local modem = peripheral.wrap("right")
  23.  
  24. -- ----------------------------------------------------------
  25.  
  26. -- Functions and stuff:
  27.  
  28. -- ----------------------------------------------------------
  29.  
  30. -- Returns true if the turtle is carrying anything.
  31. local function carrying()
  32.     for i=1,16 do if turtle.getItemCount(i) ~= 0 then return true end end
  33.     return false
  34. end
  35.  
  36. -- Accepts strings representing compass-facings to turn the turtle.
  37. local function faceDirection(targetdirection)
  38.     local tardir = 1
  39.     for i=1,4 do if targetdirection == facing[i] then tardir = i end end
  40.    
  41.     if tardir < direction then
  42.         if tardir == 1 and direction == 4 then
  43.             turtle.turnRight()
  44.         else
  45.             for i=1,direction-tardir do turtle.turnLeft() end
  46.         end
  47.     elseif tardir > direction then
  48.         if tardir == 4 and direction == 1 then
  49.             turtle.turnLeft()
  50.         else
  51.             for i=1,tardir-direction do turtle.turnRight() end
  52.         end
  53.     end
  54.    
  55.     direction = tardir
  56. end
  57.  
  58. -- Travel to a co-ordinate.
  59. local function goToPos(target,digger)
  60.     while (x ~= node[target][1]) or (y ~= node[target][2]) or (z ~= node[target][3]) do
  61.         if z > node[target][3] then
  62.             if turtle.down() then z = z - 1 elseif digger then turtle.digDown() end
  63.         elseif z < node[target][3] then
  64.             if turtle.up() then z = z + 1 elseif digger then turtle.digUp() end
  65.         end
  66.        
  67.         if x > node[target][1] then
  68.             if direction ~= 4 then faceDirection("west") end
  69.             if turtle.forward() then x = x - 1 elseif digger then turtle.dig() end
  70.         elseif x < node[target][1] then
  71.             if direction ~= 2 then faceDirection("east") end
  72.             if turtle.forward() then x = x + 1 elseif digger then turtle.dig() end
  73.         end
  74.        
  75.         if y > node[target][2] then
  76.             if direction ~= 1 then faceDirection("north") end
  77.             if turtle.forward() then y = y - 1 elseif digger then turtle.dig() end
  78.         elseif y < node[target][2] then
  79.             if direction ~= 3 then faceDirection("south") end
  80.             if turtle.forward() then y = y + 1 elseif digger then turtle.dig() end
  81.         end
  82.     end
  83.    
  84.     faceDirection(node[target][4])
  85. end
  86.  
  87. -- ----------------------------------------------------------
  88.  
  89. -- I've just booted up. Where am I? Who am I? etc...
  90.  
  91. -- ----------------------------------------------------------
  92.  
  93. x, y, z = gps.locate(5)
  94.  
  95. -- Ping the GPS servers until I get a valid reading.
  96. while x == nil or y == nil or z == nil do
  97.     print("GPS servers aren't answering; trying again...")
  98.     x, y, z = gps.locate(5)
  99.     sleep(5)
  100. end
  101.  
  102. -- Just a way of isolating some throw-away local variables.
  103. do
  104.     while not turtle.forward() do
  105.       turtle.down()
  106.       turtle.turnLeft()
  107.     end
  108.    
  109.     local tempx, tempy, tempz = gps.locate(5)
  110.  
  111.     if x > tempx then
  112.         direction = 4
  113.     elseif x < tempx then
  114.         direction = 2
  115.     elseif y > tempy then
  116.         direction = 1
  117.     else direction = 3 end
  118. end
  119.  
  120. x, y, z = gps.locate(5)
  121. print("I'm at "..x..","..y..","..z..", I have "..turtle.getFuelLevel().." fuel and I'm facing "..facing[direction]..".")
  122. print("")
  123.  
  124. while true do
  125.     print("Heading to stock...")
  126.     print("")
  127.    
  128.     goToPos(1)
  129.    
  130.     stock = peripheral.wrap("front")
  131.    
  132.     for i=1,16 do
  133.         if turtle.getItemCount(i) > 0 then
  134.             turtle.select(i)
  135.             turtle.drop(i)
  136.         end
  137.     end
  138.    
  139.     if turtle.getFuelLevel() < 1000 then
  140.         turtle.select(1)
  141.         stock.pushItem("south",6,64)  -- Assuming the turtle is south of the ME system, and you have charcoal or whatever in slot 6 of the ME system.
  142.         if turtle.getItemCount(1) == 0 then print("Warning: ME system out of turtle fuel! Will continue until out of power...")
  143.         turtle.refuel()
  144.     end
  145.    
  146.     counter = 1
  147.    
  148.     for i=2,#node do
  149.         if node[i][5] then
  150.             turtle.select(counter)
  151.             stock.pushItem("south",counter,64)
  152.             if turtle.getItemCount(counter) == 0 then error("Error: ME system out of food!!! Exiting...") end
  153.             counter = counter + 1
  154.         end
  155.     end
  156.    
  157.     counter = 1
  158.  
  159.     print("Feeding critters...")
  160.     print("")
  161.  
  162.     for i=2,#node do
  163.         goToPos(i)
  164.        
  165.         if node[i][5] then
  166.             turtle.select(counter)
  167.             turtle.dropDown()
  168.             counter = counter + 1
  169.         end
  170.     end    
  171. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement