SHARE
TWEET

CC-CactusFarmBuilder

skypop Jul 16th, 2019 232 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- Builing cactus farm
  2. -- by absolument
  3. --
  4. -- Vertical passive cactus farm 3x3
  5. -- Turtle gradually builds the farm stage by stage,
  6. -- as and when it is provided with the necessary resources.
  7. --
  8. -- When sky limit is reached, program files is renamed
  9. -- as TooHig_filename.lua
  10. -- to prevent unnecessary maneuvers.
  11. --
  12.  
  13. --Blocks chart
  14. local id = {}
  15. id.cover    = "minecraft:glass"
  16. id.platform = "minecraft:cobblestone"
  17. id.farmland = "minecraft:sand"
  18. id.crop     = "minecraft:cactus"
  19. id.harvester= "minecraft:fence"
  20. id.light    = "minecraft:torch"
  21.  
  22. --Display
  23. local w,h = term.getSize()
  24. local _log = window.create(term.current(),1, 1, w, h-4)
  25. local function clear()
  26.     _log.setCursorPos(1,1)
  27.     _log.clear()
  28.     _log.redraw()
  29. end
  30. local function output(...)
  31.     local x,y = term.getCursorPos()
  32.     local old = term.redirect(_log)
  33.     print(...)
  34.     term.redirect(old)
  35.     _log.redraw()
  36.     term.setCursorPos(x,y)
  37. end
  38.  
  39. --Safe ascent
  40. local tooHigh = false
  41. function tryUp()
  42.     local r = {turtle.up()}
  43.     tooHigh = not r[1] and r[2] == "Too high to move"
  44.     return r
  45. end
  46.  
  47. --Check inventory
  48. local function newInv(...)
  49.     local arg = {...}
  50.     return {
  51. --      [id.cover]    = arg[1] or 0, --80 Cover / stage
  52.         [id.platform] = arg[2] or 0, --4 Platform / stage
  53.         [id.farmland] = arg[3] or 0, --4 Farmland / stage
  54.         [id.crop]  = arg[4] or 0, --4 Crop / stage
  55.         [id.harvester]= arg[5] or 0, --2 Harvester / stage
  56.         [id.light]    = arg[6] or 0, --1 Light level / stage
  57.     }
  58. end
  59.  
  60. function checkInventory()
  61.     local count = newInv()
  62.     output("Check inventory")
  63.     for slot = 1, 16 do
  64.         local data = turtle.getItemDetail(slot)
  65.         if data and data.name and count[data.name] then
  66.             count[data.name] = count[data.name] + data.count
  67.             output("slot",slot..":",data.count,data.name)
  68.         elseif  data and data.count > 0 then
  69.             output("slot",slot..": useless", data.name,"-> drop()")
  70.             turtle.select(slot)
  71.             turtle.drop()
  72.         else
  73.             output("slot",slot..": empty")
  74.         end
  75.     end
  76.     return count
  77. end
  78.  
  79. function requiredItems(count)
  80.     local itemPerStage = newInv(80,4,4,4,2,1) --items per stage
  81.     local stages = math.huge
  82.     for name, qty in pairs(count) do
  83.         stages = math.min(stages, math.floor(qty / itemPerStage[name]))
  84.     end
  85.     output("Materials:",stages,"stages")
  86.     return stages
  87. end
  88.  
  89. function selectItem(name)
  90.     output("Select item", name)
  91.     for slot=1,16 do
  92.         local data = turtle.getItemDetail(slot)
  93.         if data and data.name == name then
  94.             output("Found in slot:", slot)
  95.             turtle.select(slot)
  96.             return true
  97.         end
  98.     end
  99.     return false
  100. end
  101.  
  102. --Ascent
  103. -- 4 step up without fence
  104. function ascent()
  105.     output("Ascent phase")
  106.    
  107.     --take place in the center
  108.     output("Align to center")
  109.     for i=1,3 do
  110.         turtle.forward()
  111.     end
  112.  
  113.     --step on first stage top layer
  114.     output("VAlign top layer")
  115.     repeat
  116.         tryUp()
  117.         if tooHigh then
  118.             return false
  119.         end
  120.         local b, data = turtle.inspect()
  121.     until b and data.name == id.harvester
  122.    
  123.     --reach last stage to build top layer
  124.     output("Reach first stage to build")
  125.     repeat
  126.         for i=1,4 do
  127.             tryUp()
  128.             if tooHigh then
  129.                 return false
  130.             end
  131.         end
  132.     until not turtle.detect()
  133.     return true
  134. end
  135.  
  136. --Build
  137. function build(stagesToBuild)
  138.     output("Starting build")
  139.    
  140.     --repositionning
  141.     output("Repositionning - platform stage")
  142.     for i=1,3 do
  143.         turtle.down()
  144.     end
  145.    
  146.     for i=1,stagesToBuild do
  147.         --place platform
  148.         output("Building platform")
  149.         selectItem(id.platform)
  150.         turtle.forward()
  151.         turtle.turnLeft()
  152.         turtle.place()
  153.         turtle.turnLeft()
  154.         turtle.turnLeft()
  155.         turtle.place()
  156.         turtle.turnRight()
  157.         turtle.forward()
  158.         turtle.forward()
  159.         turtle.turnLeft()
  160.         turtle.place()
  161.         turtle.turnLeft()
  162.         turtle.turnLeft()
  163.         turtle.place()
  164.        
  165.         --place farmland
  166.         output("Placing farmland")
  167.         selectItem(id.farmland)
  168.         tryUp()
  169.         if tooHigh then
  170.             return false
  171.         end
  172.         turtle.place()
  173.         turtle.turnLeft()
  174.         turtle.turnLeft()
  175.         turtle.place()
  176.         turtle.turnLeft()
  177.         turtle.forward()
  178.         turtle.forward()
  179.         turtle.turnLeft()
  180.         turtle.place()
  181.         turtle.turnLeft()
  182.         turtle.turnLeft()
  183.         turtle.place()
  184.        
  185.         --place light
  186.         output("Placing light source")
  187.         selectItem(id.light)
  188.         turtle.turnLeft()
  189.         turtle.back()
  190.         turtle.place()
  191.        
  192.         --place crop + harvester
  193.         output("Plant crops and placing harvester")
  194.         for i=1,2 do
  195.             tryUp()
  196.             if tooHigh then
  197.                 return false
  198.             end
  199.         end
  200.         turtle.forward()
  201.         turtle.turnLeft()
  202.         turtle.forward()
  203.         selectItem(id.crop)
  204.         turtle.placeDown()
  205.         turtle.back()
  206.         turtle.back()
  207.         turtle.placeDown()
  208.         selectItem(id.harvester)
  209.         turtle.place()
  210.         turtle.turnLeft()
  211.         turtle.forward()
  212.         turtle.forward()
  213.         selectItem(id.crop)
  214.         turtle.placeDown()
  215.         turtle.turnLeft()
  216.         turtle.back()
  217.         turtle.back()
  218.         turtle.placeDown()
  219.         selectItem(id.harvester)
  220.         turtle.place()
  221.        
  222.         --reposition
  223.         output("Reposition to center")
  224.         turtle.turnLeft()
  225.         turtle.forward()
  226.         turtle.turnRight()
  227.         turtle.forward()
  228.         turtle.turnLeft()
  229.         if stagesToBuild > i then
  230.             output("Repositionning - platform stage")
  231.             tryUp()
  232.             if tooHigh then
  233.                 return false
  234.             end
  235.         end
  236.     end
  237.     return true
  238. end
  239.  
  240. function descent()
  241.     output("Descent")
  242.     repeat
  243.         local move = turtle.down()
  244.     until not move
  245.     output("Repositionning")
  246.     for i=1,3 do
  247.         turtle.back()
  248.     end
  249. end
  250.  
  251. --Main
  252. repeat
  253.     --Fuel level indicator
  254.     local fuelPerCent = 100/turtle.getFuelLimit()
  255.     local fuelIndicator = string.format(
  256.         "Fuel: %s (%s%s)",
  257.         turtle.getFuelLevel(),
  258.         math.floor(turtle.getFuelLevel()*fuelPerCent),
  259.         "%"
  260.     )
  261.     --Wait materials
  262.     local stages
  263.     local goForIt = false
  264.     local itemPerStage = newInv(80,4,4,4,2,1) --items per stage
  265.     repeat
  266.         local inv = checkInventory()
  267.         stages = requiredItems(inv)
  268.         clear()
  269.         output(fuelIndicator)
  270.         for name, qty in pairs(inv) do
  271.             output(qty, "/", itemPerStage[name], name)
  272.         end
  273.         output("Stages:", stages, (stages > 0) and "\nPress [Enter] to start" or "")
  274.         repeat
  275.             local e = {os.pullEvent()}
  276.             if stages > 0 and e[1] == "key" then
  277.                 goForIt = (e[2] == keys.enter)
  278.             end
  279.         until e[1] == "turtle_inventory" or goForIt
  280.     until stages > 0 and goForIt
  281.    
  282.     clear()
  283.     output("start")
  284.     sleep(2)
  285.    
  286.     if ascent() then
  287.         build(stages)
  288.     end
  289.     descent()
  290.    
  291.     if tooHigh then
  292.         output("Building reach highest altitude")
  293.         local path = shell.getRunningProgram()
  294.         shell.run("mv", path, fs.getDir(path).."/".."TooHigh_"..fs.getName(path))
  295.         return
  296.     end
  297.  
  298.     output("stop")
  299.     sleep(2)
  300. until false
  301.  
  302. -- Farm design
  303. -- Chart :
  304. -- c = cactus
  305. -- F = fence
  306. -- S = sand
  307. -- C = cobblestone
  308. --
  309. -- Top view
  310. --     __
  311. -- cFc
  312. --  
  313. -- cFc __
  314. --
  315. -- Side view
  316. --         __
  317. --   F      
  318. --  c c    
  319. --  StS    
  320. --  C C  __
  321. --   F  
  322. --  c c
  323. --  StS
  324. --  C C  __
  325. --
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top