Advertisement
DustinLuhmann

farming

Mar 5th, 2015
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.64 KB | None | 0 0
  1. --Farmimg
  2. --Dustin Luhmann
  3. --Version 0.1.1
  4. --3/5/2015
  5. --http://pastebin.com/NqH36dT5
  6.  
  7. --[[TO RUN: pastebin get NqH36dT5 <filename>; <filename>]]
  8. --Example > pastebin get NqH36dT5 test
  9. --        > test
  10.  
  11. -- USER NOTES (**IMPORTANT**)
  12. -- 1) Must be placed facing north
  13. -- 2) Seeds should be placed in slots 1 - 7
  14. -- 3) Bonemeal should be placed in slot 8
  15. -- 4) Nothing should be in slots 9 - 15
  16. -- 5) A crop sensor card should be placed in slot 16
  17.  
  18. os.loadAPI("ocs/apis/sensor")
  19. mySensor = sensor.wrap("left")
  20.  
  21. HEADINGS = {"north","east","south","west"}
  22.  
  23. WHEAT_IDX = 1
  24. CROP_TWO_IDX = 2
  25. CROP_THREE_IDX = 3
  26. BONE_MEAL_IDX = 8
  27. SENSOR_CARD_SLOT = 16
  28.  
  29. isUp = false
  30. heading = 1
  31.  
  32. currentPosition = vector.new(0,0,0)
  33.  
  34. function tablelength(T)
  35.   local count = 0
  36.   for _ in pairs(T) do count = count + 1 end
  37.   return count
  38. end
  39.  
  40. function forward()
  41.     if HEADINGS[heading] == "north" then
  42.         currentPosition["z"] = currentPosition["z"] - 1
  43.     elseif HEADINGS[heading] == "south" then
  44.         currentPosition["z"] = currentPosition["z"] + 1
  45.     elseif HEADINGS[heading] == "east" then
  46.         currentPosition["x"] = currentPosition["x"] + 1
  47.     elseif HEADINGS[heading] == "west" then
  48.         currentPosition["x"] = currentPosition["x"] - 1
  49.     end
  50.     turtle.forward()
  51. end
  52.  
  53. function turnLeft()
  54.     if HEADINGS[heading] == "north" then
  55.         heading = 4
  56.     else
  57.         heading = heading - 1
  58.     end
  59.     turtle.turnLeft()
  60. end
  61.  
  62. function turnRight()
  63.     if HEADINGS[heading] == "west" then
  64.         heading = 1
  65.     else
  66.         heading = heading + 1
  67.     end
  68.     turtle.turnRight()
  69. end
  70.  
  71. function pickUpNormalCrop()
  72.     turtle.digDown()
  73. end
  74.  
  75. function placeSeed(idx)
  76.     turtle.select(idx)
  77.     turtle.placeDown()
  78.     if turtle.getItemCount(idx) == 0 then
  79.     end
  80. end
  81.  
  82. function tillLand()
  83.     turtle.digDown()
  84. end
  85.  
  86. function plantCrop(idx)
  87.     tillLand()
  88.     placeSeed(idx)
  89.     useBoneMeal()
  90.  
  91. end
  92.  
  93. function useBoneMeal()
  94.     turtle.select(BONE_MEAL_IDX)
  95.     count = turtle.getItemCount(BONE_MEAL_IDX)
  96.     turtle.placeDown()
  97.     while turtle.getItemCount(BONE_MEAL_IDX) ~= count do
  98.         count = turtle.getItemCount(BONE_MEAL_IDX)
  99.         turtle.placeDown()
  100.     end
  101. end
  102.  
  103. function scan()
  104.     targets = mySensor.getTargets()
  105.     numberOfPlantsToCollect = 0
  106.     plantLocations = {}
  107.  
  108.     for target, key in pairs(targets) do
  109.      
  110.       for attribute, attributeKey in pairs(mySensor.getTargetDetails(target)) do
  111.         if attribute == "Position" then
  112.             for direction, value in pairs(attributeKey) do
  113.                 if direction == "Y" then
  114.                     y = value
  115.                 end
  116.                 if direction == "X" then
  117.                     x = value
  118.                 end
  119.                 if direction == "Z" then
  120.                     z = value
  121.                 end
  122.             end
  123.             location = vector.new(x,y,z)
  124.         end
  125.         if attribute == "Status" then
  126.             if attributeKey == "Grown" then
  127.                 numberOfPlantsToCollect = numberOfPlantsToCollect + 1
  128.                 plantLocations[numberOfPlantsToCollect] = location
  129.             end
  130.         end
  131.       end
  132.     end
  133.     return plantLocations
  134. end
  135.  
  136. function move(location)
  137.     if not isUp then
  138.         moveInZ()
  139.     end
  140.     if location["x"] > 0 then
  141.         while HEADINGS[heading] ~= "east" do
  142.             turnLeft()
  143.         end
  144.     end
  145.     if location["x"] < 0 then
  146.         while HEADINGS[heading] ~= "west" do
  147.             turnLeft()
  148.         end
  149.         location["x"] = -1 * location["x"]
  150.     end
  151.  
  152.     for i = 1, location["x"] do
  153.         forward()
  154.     end
  155.  
  156.     if location["z"] > 0 then
  157.         while HEADINGS[heading] ~= "south" do
  158.             turnLeft()
  159.         end
  160.     end
  161.  
  162.     if location["z"] < 0 then
  163.         while HEADINGS[heading] ~= "north" do
  164.             turnLeft()
  165.         end
  166.         location["z"] = -1 * location["z"]
  167.     end
  168.  
  169.     for i = 1, location["z"] do
  170.         forward()
  171.     end
  172.  
  173. end
  174.  
  175. function calculateHome()
  176.     return home - currentPosition
  177. end
  178.  
  179. function goHome()
  180.     if currentPosition["x"] ~= home["x"] or currentPosition["y"] ~= home["y"] or currentPosition["z"] ~= home["z"] then
  181.         move(calculateHome())
  182.         while HEADINGS[heading] ~= "north" do
  183.             turnLeft()
  184.         end
  185.         if isUp then
  186.             moveInZ()
  187.         end
  188.     end
  189. end
  190.  
  191. function moveInZ()
  192.     if isUp then
  193.         isUp = false
  194.         turtle.down()
  195.     else
  196.         isUp = true
  197.         turtle.up()
  198.     end
  199. end
  200.  
  201. function goToWork()
  202.     while true do
  203.         plantLocations = scan()
  204.         if (tablelength(plantLocations) == 0) then
  205.             goHome()
  206.         else
  207.             move(plantLocations[1])
  208.             pickUpNormalCrop()
  209.             plantCrop(WHEAT_IDX)
  210.         end
  211.     end
  212.     return
  213. end
  214.  
  215. function exitListen()
  216.     while true do
  217.       print( "Press Space exit program." )
  218.       print("Farming...")
  219.       local event, key = os.pullEvent( "key" ) -- limit os.pullEvent to the 'key' event
  220.      
  221.       if key == keys.space then -- if the key pressed was 'e'
  222.         print( "You pressed [Space]. Exiting program..." )
  223.         break
  224.       end
  225.     end
  226.     return
  227. end
  228.  
  229. function run()
  230.     home = vector.new(0,0,0)
  231.     parallel.waitForAny(goToWork, exitListen)
  232.     goHome()
  233. end
  234.  
  235. run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement