Lupus590

[CC] broken cobble gen builder (v1)

Feb 14th, 2018
312
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 24.56 KB | None | 0 0
  1. local liquidGap = 0 -- set to 1 if placing water and lava needs a gap
  2. local fuelMin = 100 -- minimum amount of fuel to keep at
  3.  
  4. local itemID = {
  5.   buildingMaterial = { name = "minecraft:cobblestone", niceName = "cobblestone", requirementDetails = "", }, -- feel free to change this, make sure it's not flammable
  6.   fuel = {name = "minecraft:coal", niceName = "coal or charcoal", requirementDetails = "", }, -- includes charcoal -- if you want to you can change this
  7.   waterBucket = { name = "minecraft:lapis_block"--[["minecraft:water_bucket"]], niceName = "water bucket", requirementDetails = "", },
  8.   lavaBucket = { name = "minecraft:netherrack"--[["minecraft:lava_bucket"]], niceName = "lavaBucket", requirementDetails = "", },
  9.   miningTurtle = { name = "computercraft:CC-Turtle", damage = 1, niceName = "mining turtle", requirementDetails = "", },
  10.   badTurtle = { name = "computercraft:CC-TurtleExpanded", niceName = "unknown non-mining turtle", requirementDetails = "", },
  11.   diskDrive = { name = "computercraft:CC-Peripheral", damage = 0, niceName = "disk drive", requirementDetails = "", },
  12.   disk = { name = "computercraft:disk", niceName = "disk", requirementDetails = "Please make sure that it's empty, I may delete things.", },
  13.   pickaxe = { name = "minecraft:diamond_pickaxe", damage = 0, niceName = "diamond pickaxe", requirementDetails = "Undamaged please, I can't use broken tools.", },
  14.   chest = { name = "minecraft:chest", niceName = "chest", requirementDetails = "", },
  15.   sign = {name = "minecraft:sign", niceName = "sign", requirementDetails = "", },
  16. }
  17.  
  18. local oldLabel = os.getComputerLabel()
  19. oldError = error
  20. local function error(message, level)
  21.   os.setComputerLabel(oldLabel)
  22.   if type(level) ~= "number" or type(level) ~= "nil" then error("Error level must be a number", 2) end
  23.   level = level or 0
  24.   oldError(message, level + 1)
  25. end
  26.  
  27. local function termClear()
  28.   term.setCursorPos(1,1)
  29.   term.clear()
  30. end
  31.  
  32. local coverLava = nil
  33. local coverLavaBlockCost = 20
  34.  
  35. local buildingRequirements = {
  36.   buildingMaterial = 114,
  37.   waterBucket = 12,
  38.   lavaBucket = 20,
  39.   miningTurtle = 4,
  40.   diskDrive = 1,
  41.   disk = 1,
  42.   pickaxe = 1,
  43.   fuel = 64,
  44.   chest = 2,
  45.   sign = 2,
  46.   }
  47.  
  48. local currentInventory = {
  49.   buildingMaterial = 0,
  50.   waterBucket = 0,
  51.   lavaBucket = 0,
  52.   miningTurtle = 0,
  53.   diskDrive = 0,
  54.   disk = 0,
  55.   pickaxe = 0,
  56.   fuel = 0,
  57.   chest = 0,
  58.   sign = 0,
  59.   }
  60.  
  61. -----------------------
  62. -- Utility functions --
  63. -----------------------
  64.  
  65. local function selectEmptySlot()
  66.   local function checkItem()
  67.     return turtle.getItemCount() == 0
  68.   end
  69.   if checkItem() then return true end
  70.   for i = 1, 16 do
  71.     turtle.select(i)
  72.     if checkItem() then
  73.       return true
  74.      end
  75.   end
  76.   return false -- could not find an empty slot
  77. end
  78.  
  79. local dropOffChest = {1, 7, 1}
  80. local fuelChest = {9, 7, 9} -- TODO: hang onto the fuel chest until lava is deployed, may need to dump things in it temporarily
  81. local storageChest = dropOffChest
  82. local goTo
  83.  
  84. local function selectItem(item)
  85.   local function checkItem()
  86.     local i = turtle.getItemDetail()
  87.     if i and i.name == item.name and (item.damage == nil or i.damage == item.damage) then
  88.       return true
  89.     else
  90.       return false
  91.     end
  92.   end
  93.  
  94.  
  95.  
  96.   if checkItem() then return true end
  97.   for i = 1, 16 do
  98.     turtle.select(i)
  99.     if checkItem() then
  100.       return true
  101.      end
  102.   end
  103.   goTo(storageChest)
  104.   -- TODOL check storage
  105.  
  106.   return false -- could not find item
  107. end
  108.  
  109. local function checkFuel() -- refuel if we need to, complain if we can't
  110.   while turtle.getFuelLevel() < fuelMin do
  111.     if selectItem(itemID.fuel) then
  112.       while turtle.refuel(1) and turtle.getFuelLevel() < fuelMin do
  113.       end
  114.       print("refuelled")
  115.     else
  116.       print("Please add a fuel item")
  117.       local ol = os.getComputerLabel()
  118.       os.setComputerLabel("More fuel please")
  119.       os.pullEvent("turtle_inventory") -- wait until the user does something with the inventory
  120.       os.setComputerLabel(ol)
  121.     end
  122.   end
  123. end
  124.  
  125. termClear()
  126. print("It is advised that users test how the turtle places a water bucket below itself (sometimes a gap is needed). If a gap is required for placement then edit this program and set the variable 'liquidGap' to 1, this will ensure that the program makes a gap before placing water or lava. (Press any key to continue, terminate to exit)")
  127. os.setComputerLabel("Waiting for user input")
  128. os.pullEvent("key")
  129.  
  130. termClear()
  131. print("Please ensure that all walls and the floor have no holes and that there is nothing in the build area.")
  132. print("When you say to continue I'm going to look for the corner of the space. If you want to do this for me then please place me facing the wall with a wall on my right.")
  133. print("Also, I may ask for fuel, please give me coal or charcoal. (Press any key to continue)")
  134. os.setComputerLabel("Waiting for user input")
  135. os.pullEvent("key")
  136.  
  137. -- find a corner
  138. os.setComputerLabel("looking for a corner")
  139. termClear()
  140. print("looking for a corner")
  141. checkFuel()
  142. -- TODO: OPTIONAL: find corner may be able to make more efficient
  143. -- Do a spin to check if we are in a corner
  144. while not turtle.detect() do
  145.   checkFuel()
  146.   turtle.forward()
  147. end
  148. turtle.turnRight()
  149. while not turtle.detect() do
  150.   checkFuel()
  151.   turtle.forward()
  152. end
  153. while turtle.down() do
  154.   checkFuel()
  155. end
  156.  
  157. -----------------
  158. -- API Fetcher --
  159. -----------------
  160.  
  161. local LAMA_URL = "https://raw.githubusercontent.com/KingofGamesYami/LAMA/master/lama.lua"
  162.  
  163. -- Credit where due, this snippet is adapted from: http://www.computercraft.info/forums2/index.php?/topic/24253-monitor-touchscreen-woes/page__view__findpost__p__228877
  164. if not lama then
  165.   if not (fs.exists("lama") or fs.exists(shell.resolve("lama"))) then
  166.     print("Could not find LAMA API, attempting to download from github.")
  167.     if http then
  168.       if http.checkURL(LAMA_URL) then
  169.         local r = http.get(LAMA_URL)
  170.         local f = fs.open("lama","w")
  171.         f.write(r.readAll())
  172.         f.close()
  173.         r.close()
  174.         print("Successfully downloaded LAMA API")
  175.       else
  176.         error("Could not download LAMA API. Please whitelist the URL or manually download LAMA (saving the file as 'lama', make sure to not have a file extension): "..LAMA_URL)
  177.       end
  178.     else
  179.       error("Could not download LAMA API. Please enable http or manually download LAMA (saving the file as 'lama', make sure to not have a file extension): "..LAMA_URL)
  180.     end
  181.     os.loadAPI(shell.resolve("lama"))
  182.   else os.loadAPI(fs.exists("lama") and "lama" or shell.resolve("lama")) end
  183.   lama.overwrite()
  184. end
  185.  
  186. lama.setPosition(1,1,1,"north")
  187.  
  188. local goToSource = [[
  189.   local loc = ...
  190.   if type(loc) ~= "table" then error("require table", 2) end
  191.   if #loc ~= 3 or type(loc[1]) ~=  "number" or type(loc[1]) ~=  "number" or type(loc[1]) ~=  "number" then error("malformed table, should be 3 numbers", 2) end
  192.   local pos = {lama.getPosition()}
  193.  
  194.   if pos[1] ~= loc[1] then
  195.     if pos[1] > loc[1] then
  196.       -- rotate until facing west
  197.       while pos[4] ~= "west" do
  198.         turtle.turnRight()
  199.         pos = {lama.getPosition()}
  200.       end
  201.      
  202.     else
  203.    
  204.       -- rotate until facing east
  205.       while pos[4] ~= "east" do
  206.         turtle.turnRight()
  207.         pos = {lama.getPosition()}
  208.       end
  209.      
  210.     end
  211.    
  212.    
  213.     -- go until we are there
  214.     while pos[1] ~= loc[1] do
  215.       checkFuel()
  216.       turtle.forward()
  217.       pos = {lama.getPosition()}
  218.     end
  219.   end
  220.    
  221.   if pos[3] ~= loc[3] then
  222.     if pos[3] > loc[3] then
  223.       -- rotate until facing north
  224.       while pos[4] ~= "north" do
  225.         turtle.turnRight()
  226.         pos = {lama.getPosition()}
  227.       end
  228.      
  229.     else
  230.    
  231.       -- rotate until facing south
  232.       while pos[4] ~= "south" do
  233.         turtle.turnRight()
  234.         pos = {lama.getPosition()}
  235.       end
  236.      
  237.     end
  238.      
  239.     -- go until we are there
  240.     while pos[3] ~= loc[3] do
  241.       checkFuel()
  242.       turtle.forward()
  243.       pos = {lama.getPosition()}
  244.     end
  245.   end
  246.  
  247.   if pos[2] ~= loc[2] then
  248.     local verticalAction
  249.     if pos[2] > loc[2] then
  250.       verticalAction = turtle.down
  251.     else
  252.       verticalAction = turtle.up
  253.     end
  254.     while pos[2] ~= loc[2] do
  255.       checkFuel()
  256.       verticalAction()
  257.       pos = {lama.getPosition()}
  258.     end
  259.   end
  260. ]]
  261.  
  262. goTo = loadstring(goToSource)
  263.  
  264. -- check if we have a pickaxe equipped
  265. if selectEmptySlot() then
  266.   turtle.equipLeft()
  267.   local checkRight = true
  268.  
  269.   local r = turtle.getItemDetail()
  270.   if r then
  271.     if r.name == itemID.pickaxe.name then
  272.       currentInventory.pickaxe = currentInventory.pickaxe + 1
  273.       checkRight = false
  274.     end
  275.     turtle.equipLeft() -- reEquip the item, even if it was not a pickaxe
  276.   end
  277.  
  278.   if checkRight then -- if we already have a pickaxe, don't bother with what is on the right side
  279.     if selectEmptySlot() then
  280.       turtle.equipRight()
  281.       local r = turtle.getItemDetail()
  282.       if r then
  283.         if r.name == itemID.pickaxe.name then
  284.           currentInventory.pickaxe = currentInventory.pickaxe + 1
  285.         end
  286.         turtle.equipLeft()-- reEquip the item, even if it was not a pickaxe
  287.       end
  288.     end
  289.   end
  290. end  
  291.  
  292. os.setComputerLabel("Waiting for user input")
  293. repeat
  294.   termClear()
  295.   print("Do you want me to cover the lava so that you can't step in it? I will need extra blocks (I'll change my request to account for this) (Y/N)")
  296.   local _, c = os.pullEvent("char")
  297.   c = string.upper(c)
  298.   if c == "Y" then coverLava = true
  299.   elseif c == "N" then coverLava = false
  300.   end
  301. until coverLava ~= nil
  302.  
  303. if coverLava then
  304.   buildingRequirements.buildingMaterial = buildingRequirements.buildingMaterial + coverLavaBlockCost
  305. end
  306.  
  307. local gotEverything = false
  308. local ready = false
  309. local event = {}
  310.  
  311. local neededItems = {}
  312.  
  313. --[[
  314. os.setComputerLabel("I need resources")
  315. local prevInv = {}
  316. os.queueEvent("turtle_inventory") -- force first scan of inventory
  317. repeat
  318.   -- TODO: check scope
  319.  
  320.   if event[1] == "turtle_inventory" then
  321.     -- check inventory
  322.     termClear()
  323.     print("Scanning inventory")
  324.    
  325.     for i = 1, 16 do
  326.      
  327.       local _, currentItem = turtle.getItemDetail()
  328.      
  329.      
  330.       if type(currentItem) == "table" then
  331.        
  332.         if currentItem.name == itemID.buildingMaterial.name then
  333.           currentInventory.buildingMaterial = currentInventory.buildingMaterial + currentItem.count - prevInv[i].count -- TODO: what if the swap the item with a different one?
  334.             -- TODO: reject excess resources?
  335.             -- TODO: add check for badTurtle in inventory
  336.          
  337.         elseif currentItem.name == itemID.fuel.name then
  338.           currentInventory.fuel = currentInventory.fuel + currentItem.count - prevInv[i].count
  339.             -- TODO: reject two stacks of fuel?
  340.        
  341.         elseif currentItem.name == itemID.waterBucket.name then
  342.           currentInventory.waterBucket = currentInventory.waterBucket + currentItem.count - prevInv[i].count
  343.           -- TODO: sort water
  344.        
  345.         elseif currentItem.name == itemID.lavaBucket.name then
  346.           currentInventory.lavaBucket = currentInventory.lavaBucket + currentItem.count - prevInv[i].count
  347.           -- TODO: sort lava
  348.        
  349.         elseif currentItem.name == itemID.miningTurtle.name then
  350.           if currentItem.damage ~= itemID.miningTurtle.damage then
  351.             -- TODO: figure out how to identify turtles cleanly
  352.             -- Placed turtles are easier to check
  353.             -- place, check, dig, store each turtle?
  354.            
  355.            
  356.            
  357.            
  358.             print("Turtle in not a mining turtle. Turtle shroud be a stone/iron turtle (NOT gold), and have a pickaxe on the left side. Additionally, they should NOT be labelled.")
  359.           end
  360.           currentInventoryminingTurtle = currentInventory.miningTurtle + currentItem.count - prevInv[i].count
  361.        
  362.         elseif currentItem.name == itemID.diskDrive.name and currentItem.damage == itemID.diskDrive.damage then
  363.           -- TODO: sort disk drive
  364.           currentInventory.diskDrive = currentInventory.diskDrive + currentItem.count - prevInv[i].count
  365.        
  366.         elseif currentItem.name == itemID.disk.name then
  367.           -- TODO: sort disk
  368.           currentInventory.disk = currentInventory.disk + currentItem.count - prevInv[i].count
  369.        
  370.         elseif currentItem.name == itemID.pickaxe.name then
  371.           if currentInventory.pickaxe ~= 0 then
  372.             termClear()
  373.             print("I already have a pickaxe equipped, I don't need another. Please remove.")
  374.             os.pullEvent("turtle_inventory")
  375.           end
  376.           if currentItem.damage ~= itemID.pickaxe.damage then
  377.             termClear()
  378.             print("I can't use damaged tools. Please remove.")
  379.             os.pullEvent("turtle_inventory")
  380.           else
  381.             turtle.equipLeft()
  382.             currentInventory.pickaxe = currentInventory.pickaxe + 1
  383.             print("Pickaxe equipped")
  384.           end
  385.          
  386.         elseif currentItem.name == ItemID.badTurtle.name then
  387.           -- TODO: reject bad turtle
  388.        
  389.         elseif currentItem.name == ItemID.sign then
  390.           -- TODO: place sign with text on chests
  391.          
  392.         else
  393.           print("Unexpected item in slot "..tostring(turtle.getSelectedSlot())..", please remove.")
  394.         end
  395.      
  396.      
  397.      
  398.      
  399.      
  400.       end
  401.       prevInv[i] = currentItem
  402.     end
  403.   end
  404.   -- TODO: neededItemTotalCount calc more efficient
  405.   local neededItemTotalCount = 0
  406.   for k, v in pairs(itemID) do
  407.     -- TODO: fix bug
  408.     neededItems.k = buildingRequirements.k - currentInventory.k
  409.     neededItemTotalCount = neededItemTotalCount + neededItems.k
  410.   end
  411.  
  412.   -- TODO: redo everything
  413.   term.setCursorPos(1,1)
  414.   term.clear()
  415.  
  416.   print("If I stop at any time then look at my label, I will tell you there if I need something or if I'm done.")
  417.   print("I need some things before I can begin.")
  418.   print("Can you put in my inventory the following:")
  419.   print("(I will reduce these numbers as you add things.)")
  420.   print("Please give me the turtles first, I'll need to use them as extra storage.")
  421.  
  422.   -- TODO: use niceName
  423.   -- TODO: recheck requirements
  424.   -- TODO: have chests now, turtles not needed for temp storage
  425.   print("")
  426.   print(tostring(buildingRequirements[miningTurtle] - currentInventory[miningTurtle]).." mining turtles (non-advanced)")
  427.   print(tostring(buildingRequirements[buildingMaterial] - currentInventory[buildingMaterial]).." blocks of cobblestone,")
  428.   print(tostring(buildingRequirements[waterBucket] - currentInventory[waterBucket]).." water buckets,")
  429.   print("")
  430.   print(tostring(buildingRequirements[lavaBucket] - currentInventory[lavaBucket]).." lava buckets")
  431.   print(tostring(buildingRequirements[diskDrive] - currentInventory[diskDrive]).." disk drive")
  432.   print(tostring(buildingRequirements[disk] - currentInventory[disk]).." disk")
  433.   print(tostring(buildingRequirements[pickaxe] - currentInventory[pickaxe]).." diamond pickaxe")
  434.   print("")
  435.   print("I'm also going to want a good amount of fuel, a stack should be plenty but I'll change my label if I run out. P.S. I only know how to use coal and charcoal, so if you like to use something else you will have to quit this program (Command-T on MacOS - Oh no the 4th wall!).")
  436.  
  437.   if neededItemTotalCount == 0 then
  438.     print("That's everything, should I get started? (Y/N)")
  439.     if event[1] == "char" then
  440.       if string.upper(event[2]) == "Y" then
  441.         ready = true
  442.         end
  443.     end
  444.   end
  445.   event = {os.pullEvent()}
  446. until ready
  447. --]]
  448.  
  449. -- place bottom layer
  450. os.setComputerLabel("Placing layer 1/3")
  451. local pattern = {}
  452. local patternSize = 9
  453. local currentY = 2
  454. local function fillPatternWith(value)
  455.   if type(value) ~= "boolean" then error("value must be boolean",2) end
  456.   for j = 1, patternSize do
  457.     pattern[j] = {}
  458.     for i = 1, patternSize do
  459.       pattern[j][i] = value
  460.     end
  461.   end
  462. end
  463.  
  464. do -- pattern
  465. fillPatternWith(true)
  466.  
  467. pattern[3][2] = false
  468. pattern[7][2] = false
  469.  
  470. pattern[2][3] = false
  471. pattern[4][3] = false
  472. pattern[6][3] = false
  473. pattern[8][3] = false
  474.  
  475. pattern[3][4] = false
  476. pattern[5][4] = false
  477. pattern[7][4] = false
  478.  
  479. pattern[4][5] = false
  480. pattern[6][5] = false
  481.  
  482. pattern[3][6] = false
  483. pattern[5][6] = false
  484. pattern[7][6] = false
  485.  
  486. pattern[2][7] = false
  487. pattern[4][7] = false
  488. pattern[6][7] = false
  489. pattern[8][7] = false
  490.  
  491. pattern[3][8] = false
  492. pattern[7][8] = false
  493.  
  494. end
  495.  
  496. local function placeLayer(pattern,item)
  497.   if type(pattern) ~= "table" then error("pattern must be table of tables of booleans",2) end
  498.  
  499.   -- TODO: OPTIONAL: arg checking
  500.  
  501.   -- TODO: could this be made more efficient?
  502.  
  503.   for j = 1, patternSize do
  504.     for i = 1, patternSize do
  505.       goTo({j, currentY, i})
  506.       if pattern[j][i] then
  507.         checkFuel()
  508.         print("item: "..tostring(item.niceNamee))
  509.         if not selectItem(item) then error("can't select item: "..item.niceName) end
  510.         while not  turtle.placeDown() do end
  511.       end
  512.     end
  513.   end
  514. end
  515.  
  516. placeLayer(pattern, itemID.buildingMaterial)
  517.  
  518. -- place middle layer
  519. os.setComputerLabel("Placing layer 2/3")
  520. currentY = 3
  521.  
  522. do -- pattern
  523. fillPatternWith(false)
  524.  
  525. pattern[2][1] = true
  526. pattern[4][1] = true
  527. pattern[6][1] = true
  528. pattern[8][1] = true
  529.  
  530. pattern[1][2] = true
  531. pattern[5][2] = true
  532. pattern[9][2] = true
  533.  
  534. pattern[1][4] = true
  535. pattern[9][4] = true
  536.  
  537. pattern[2][5] = true
  538. pattern[8][5] = true
  539.  
  540. pattern[1][6] = true
  541. pattern[9][6] = true
  542.  
  543. pattern[1][8] = true
  544. pattern[5][8] = true
  545. pattern[9][8] = true
  546.  
  547. pattern[2][9] = true
  548. pattern[4][9] = true
  549. pattern[6][9] = true
  550. pattern[8][9] = true
  551.  
  552. end
  553.  
  554. placeLayer(pattern, itemID.buildingMaterial)
  555.  
  556. -- place water
  557.  
  558. do -- pattern
  559. fillPatternWith(false)
  560.  
  561. pattern[3][1] = true
  562. pattern[7][1] = true
  563.  
  564. pattern[1][3] = true
  565. pattern[5][3] = true
  566. pattern[9][3] = true
  567.  
  568. pattern[3][5] = true
  569. pattern[7][5] = true
  570.  
  571. pattern[1][7] = true
  572. pattern[5][7] = true
  573. pattern[9][7] = true
  574.  
  575. pattern[3][9] = true
  576. pattern[7][9] = true
  577.  
  578. end
  579.  
  580. currentY = 3 + liquidGap
  581. placeLayer(pattern, itemID.waterBucket)
  582.  
  583.  
  584. -- place top layer
  585. os.setComputerLabel("Placing layer 3/3")
  586. currentY = 4
  587. do -- pattern
  588. fillPatternWith(false)
  589.  
  590. pattern[3][1] = true
  591. pattern[7][1] = true
  592.  
  593. pattern[2][2] = true
  594. pattern[4][2] = true
  595. pattern[6][2] = true
  596. pattern[8][2] = true
  597.  
  598. pattern[1][3] = true
  599. pattern[5][3] = true
  600. pattern[9][3] = true
  601.  
  602. pattern[2][4] = true
  603. pattern[4][4] = true
  604. pattern[6][4] = true
  605. pattern[8][4] = true
  606.  
  607. pattern[3][5] = true
  608. pattern[7][5] = true
  609.  
  610. pattern[2][6] = true
  611. pattern[4][6] = true
  612. pattern[6][6] = true
  613. pattern[8][6] = true
  614.  
  615. pattern[1][7] = true
  616. pattern[5][7] = true
  617. pattern[9][7] = true
  618.  
  619. pattern[2][8] = true
  620. pattern[4][8] = true
  621. pattern[6][8] = true
  622. pattern[8][8] = true
  623.  
  624. pattern[3][9] = true
  625. pattern[7][9] = true
  626.  
  627. end
  628.  
  629. placeLayer(pattern, itemID.buildingMaterial)
  630.  
  631. -- place lava (and cover if told)
  632. os.setComputerLabel("Placing lava")
  633.  
  634. currentY = 4 + liquidGap
  635.  
  636. do -- pattern
  637. fillPatternWith(false)
  638.  
  639. pattern[3][2] = true
  640. pattern[7][2] = true
  641.  
  642. pattern[2][3] = true
  643. pattern[4][3] = true
  644. pattern[6][3] = true
  645. pattern[8][3] = true
  646.  
  647. pattern[3][4] = true
  648. pattern[5][4] = true
  649. pattern[7][4] = true
  650.  
  651. pattern[4][5] = true
  652. pattern[6][5] = true
  653.  
  654. pattern[3][6] = true
  655. pattern[5][6] = true
  656. pattern[7][6] = true
  657.  
  658. pattern[2][7] = true
  659. pattern[4][7] = true
  660. pattern[6][7] = true
  661. pattern[8][7] = true
  662.  
  663. pattern[3][8] = true
  664. pattern[7][8] = true
  665.  
  666. end
  667.  
  668. placeLayer(pattern, itemID.lavaBucket)
  669. currentY = 5
  670. os.setComputerLabel("Covering Lava")
  671. placeLayer(pattern, itemID.buildingMaterial)
  672.  
  673. -- place and program turtles
  674. os.setComputerLabel("Deploying Turtles")
  675.  
  676. -- multi line string can't have comments in
  677. -- this basically needs to copy lama and cobbleGen onto the turtle which runs it and make a startup file for cobbleGen
  678. -- TODO: check all below
  679. local diskStartupSource = [[-- TODO: put double square brackets here
  680.   fs.copy("/disk/lama", "/lama")
  681.  
  682.  
  683.   local c = fs.open("/disk/cobbleGen","r")
  684.   local f = fs.open("/cobbleGen","w")
  685.   f.writeLine("local homePos = "..textutils.serialise(pos))
  686.   f.write(c.readAll())
  687.   f.close()
  688.   c.close()
  689.  
  690.  
  691.  
  692.   f = fs.open("/startup","w")
  693.   f.writeLine("shell.run(\"cobbleGen\")")
  694.   f.close()
  695.   if not lama then
  696.     os.loadAPI("lama")
  697.     lama.overwrite()
  698.     lama.setPosition(table.unpack(pos))
  699.   end
  700.   local stateFile = "stateFile"
  701.   f.writeLine("mine")
  702.  
  703.  
  704.   turtle.down()
  705.   os.reboot()
  706. ]]
  707.  
  708. local cobbleGenSource = [[-- TODO: put double square brackets here
  709.   local fuelSlot = 1
  710.   local function checkFuel()
  711.     if turtle.getFuelLevel < 30 then
  712.       turtle.select(fuelSlot)
  713.       turtle.refuel(1)
  714.     end
  715.   end
  716.  
  717. ]]..goToSource.."\ndropOffChest = "..textutils.serialise(dropOffChest)..
  718. "\nfuelChest = "..textutils.serialise(fuelChest).."\n".. [[-- TODO: put double square brackets here
  719.   if not lama then
  720.     os.loadAPI("lama")
  721.     lama.overwrite()
  722.   end
  723.  
  724.   local stateFile = "stateFile"
  725.   local tergetFuelItemCount = 10
  726.  
  727.   local states = {
  728.     mine = "mine",
  729.     dropOff = "dropOff",
  730.     goHome = "goHome",
  731.   }
  732.  
  733.  
  734.   local f = fs.open(stateFile, "r")
  735.   local currentState = f.readAll()
  736.   f.close()
  737.  
  738.  
  739.   local function setState(newState)
  740.     if not states[newState] then error("invalid state", 2) end
  741.     local f = fs.open(stateFile, "w")
  742.     f.writeLine(states[newState])
  743.     f.close()
  744.     currentState = newState
  745.   end
  746.  
  747.  
  748.   local function getFuel()
  749.     goTo(fuelChest)
  750.     turtle.select(fuelSlot)
  751.     turtle.suckUp(tergetFuelItemCount)
  752.   end
  753.  
  754.   local function lowFuelItemCount()
  755.     turtle.select(fuelSlot)
  756.     if turtle.getItemCount() < 5 then
  757.       return true
  758.     else
  759.       return false
  760.     end
  761.   end
  762.  
  763.   while true do
  764.     if state == states.mine then
  765.       while turtle.down() do end
  766.       -- check inventory
  767.       if turtle.getSelectedSlot() == fuelSlot then
  768.         turtle.select(fuelSlot + 1)
  769.       end
  770.      
  771.       if turtle.getSelectedSlot() ~= 16 or turtle.getItemCount() ~= 64 then
  772.         repeat
  773.           if turtle.getItemCount() == 64 then
  774.             turtle.select(turtle.getSelectedSlot() + 1)
  775.           end
  776.           turtle.dig()
  777.           turtle.turnRight()
  778.         until turtle.getSelectedSlot() == 16 and turtle.getItemCount() == 64
  779.       end
  780.      
  781.       goTo(homePos)
  782.       setState(states.dropOff)
  783.      
  784.     elseif state == states.dropOff then
  785.       if lowFuelItemCount() then
  786.         getFuel()
  787.       end
  788.       goTo(dropOffChest)
  789.      
  790.    
  791.     elseif state == states.goHome then
  792.       if lowFuelItemCount() then
  793.         getFuel()
  794.       end
  795.       goTo(homePos)
  796.       setState(states.mine)
  797.    
  798.      
  799.     else
  800.       local _, i = turtle.inspectUp()
  801.       if type(i) == "table" and i.name == "minecraft:flowing_lava" then
  802.         turtle.up()
  803.         os.setComputerLabel("ERROR!")
  804.         error("bad state")
  805.       end
  806.     end
  807.   end
  808.  
  809. ]]
  810.  
  811.  
  812.  
  813.  
  814.  
  815.  
  816. local function placeAndProgramTurtle(x, y, z)
  817.   goTo(x, y, z)
  818.   selectItem(itemID.diskDrive)
  819.   turtle.placeUp()
  820.   selectItem(itemID.disk)
  821.   turtle.dropUp()
  822.   -- TODO: program disk
  823.   -- http://www.computercraft.info/wiki/Fs_(API)
  824.  
  825.   local f = fs.open("/disk/startup", "w")
  826.   f.writeLine("local pos = "..textutils.serialise({lama.getPosition()})) -- prepend position to startup so new turtle knows where it is
  827.   f.writeLine(diskStartupSource)
  828.   f.close()
  829.   fs.copy("/lama", "/disk/lama")
  830.   f = fs.open("/disk/cobbleGen", "w")
  831.   f.writeLine(cobbleGenSource)
  832.   f.close()
  833.  
  834.   turtle.back()
  835.   selectItem(itemID.miningTurtle)
  836.   turtle.place()
  837.   selectItem(itemID.fuel)
  838.   turtle.drop(5)
  839.  
  840.   peripheral.call("front", "turnOn") -- activate turtle
  841.  
  842.   goTo(x, y, z)
  843.   if not selectItem(itemID.disk) then
  844.     selectEmptySlot()
  845.   end
  846.   turtle.suckUp()
  847.   if not selectItem(itemID.diskDrive) then
  848.     selectEmptySlot()
  849.   end
  850.   turtle.digUp()
  851.  
  852. end
  853.  
  854. placeAndProgramTurtle(3, currentY, 3)
  855. placeAndProgramTurtle(3, currentY, 7)
  856. placeAndProgramTurtle(7, currentY, 7)
  857. placeAndProgramTurtle(7, currentY, 3)
  858.  
  859.  
  860.  
  861. -- deploy self
  862. os.setComputerLabel("deploying self")
  863.  
  864. -- move fuel to fuel slot
  865. for i = 1, 16 do
  866.   selectItem(itemID.fuel)
  867.   turtle.transferTo(16)
  868. end
  869. turtle.select(16)
  870. turtle.transferTo(1)
  871.  
  872. -- dump the rest of the inventory into the dropOffChest
  873. goTo(dropOffChest)
  874. for i = 2,16 do -- skip the fuel slot, we want to keep that
  875.   turtle.select(i)
  876.   turtle.dropUp()
  877. end
  878.  
  879. goTo(5, currentY, 5)
  880.  
  881. local f = fs.open("/cobbleGen","w")
  882. f.writeLine(cobbleGenSource)
  883. f.close()
  884. f = fs.open("/startup","w")
  885. f.writeLine("shell.run(\"cobbleGen\")")
  886. f.close()
  887.  
  888. local stateFile = "stateFile"
  889. f.writeLine("mine")
  890.  
  891. turtle.down()
  892. os.reboot()
Advertisement
Add Comment
Please, Sign In to add comment