kerrnel

Podman 1.0

Jun 12th, 2014
297
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.90 KB | None | 0 0
  1. -- Podman 1.0
  2. -- by MCKerrnel
  3. --
  4. -- A ComputerCraft program to
  5. -- operate a farm in a 15dia
  6. -- pod/dome. Turtle will
  7. -- harvest and re-plant.
  8.  
  9. -- Size of farm grid that turtle
  10. -- will traverse. Middle position
  11. -- is water.
  12. local width = 9
  13. local length = 9
  14.  
  15. -- Fuel needed to start a cycle.
  16. local fuelCycle = 250
  17.  
  18. -- Harvests and replants the crop.
  19. function harvestCrops(endCol)
  20. local colCount = 0
  21. while colCount < endCol do
  22. for rowCount = 1, length, 1 do
  23. turtle.select(1)
  24.  
  25. -- Don't move forward if this is
  26. -- the first block in the row;
  27. -- we're already on it.
  28. if rowCount ~= 1 then
  29. turtle.forward()
  30. end
  31.  
  32. -- Harvest the crop. If there
  33. -- is only one seed left in slot 1
  34. -- then do not plant it.
  35. turtle.digDown()
  36. if turtle.getItemCount(1) > 1 then
  37. turtle.placeDown()
  38. end
  39. end
  40. colCount = colCount + 1
  41.  
  42. -- Rotate around to start
  43. -- the new row.
  44. endColTurn(colCount)
  45. turtle.forward()
  46. endColTurn(colCount)
  47. end
  48. end
  49.  
  50. -- Determine which direction the
  51. -- turtle should turn, based on
  52. -- which row it just finished.
  53. function endColTurn(column)
  54. -- Turtle will snake, so odd
  55. -- numbered columns turn left,
  56. -- even turn right. Odd
  57. -- numbers will leave a
  58. -- remainder when divided by 2.
  59. local direction = column % 2
  60.  
  61. if direction == 1 then
  62. turtle.turnLeft()
  63. else
  64. turtle.turnRight()
  65. end
  66. end
  67.  
  68. -- Unloads the crops from the turtle
  69. -- into the chest for crops.
  70. function emptyHarvest()
  71. for slot = 2, 16, 1 do
  72. turtle.select(slot)
  73. turtle.drop()
  74. end
  75. end
  76.  
  77. -- A quick function to convert a decimal
  78. -- value into an integer. Dunno why
  79. -- LUA has to be so difficult with this.
  80. -- Should be a built-in function!
  81. function toInt(decimalNum)
  82. local D2I = string.format("%i", decimalNum)
  83. return D2I
  84. end
  85.  
  86. function refuel(fuelCycles)
  87. -- Refuel. We don't need a stupid
  88. -- amount of fuel, so cap it
  89. -- on refuel at 10k.
  90. local fuelMax = 10000
  91.  
  92. if fuelCycles < 1 then
  93. -- Assume the turtle is facing
  94. -- forward and in-between the
  95. -- two chests. Left chest is
  96. -- to dump harvest into, right
  97. -- is for fuel.
  98. turtle.turnRight()
  99. turtle.select(2)
  100.  
  101. -- Try to fill up, but if not enough to
  102. -- fill up, that's ok.
  103. while fuelCycles < 1 or turtle.getFuelLevel() < fuelMax do
  104. -- If there's nothing to grab, error out.
  105. if turtle.suck() == false then
  106. break
  107. end
  108. turtle.refuel()
  109. fuelCycles = turtle.getFuelLevel() / fuelCycle
  110. end
  111.  
  112. -- If we still need fuel
  113. -- then alert console.
  114. if fuelCycles < 1 then
  115. print "NO FUEL: Reload the FUEL chest."
  116. end
  117.  
  118. -- Restore original orientation.
  119. turtle.turnLeft()
  120. end
  121. return fuelCycles
  122. end
  123.  
  124. -- Bring the turtle back at the end
  125. -- of the harvest cycle, dumping off
  126. -- the harvest and returning to
  127. -- between the chests. Assumption
  128. -- here is that the full 9x9 grid has
  129. -- been traveled. Function does not
  130. -- account for custom number of
  131. -- columns.
  132. function turtleReturn()
  133. for count = 1, 8, 1 do
  134. turtle.forward()
  135. end
  136. turtle.turnLeft()
  137. for count = 1, 5, 1 do
  138. turtle.forward()
  139. end
  140. turtle.turnRight()
  141. turtle.forward()
  142. turtle.turnRight()
  143. turtle.down()
  144. emptyHarvest()
  145. turtle.turnRight()
  146. end
  147.  
  148.  
  149. -- MAINLINE
  150. local fuelCycles = turtle.getFuelLevel() / fuelCycle
  151. print("Enough fuel to harvest ", toInt(fuelCycles), " cycles.")
  152.  
  153. -- This is where it all happens.
  154. while true do
  155. -- We must have at least 1 seed in slot 1
  156. -- so that the slot never is empty, otherwise
  157. -- the turtle might try planting harvest items.
  158. -- If we do not have a seed, then abort.
  159. if turtle.getItemCount(1) == 0 then
  160. print("There is no target seed in slot 1.")
  161. print("Need at least ONE seed of the crop.")
  162. break
  163. end
  164.  
  165. -- Fuel Up
  166. fuelCycles = turtle.getFuelLevel() / fuelCycle
  167. if fuelCycles < 1 then
  168. fuelCycles = refuel(fuelCycles)
  169. end
  170.  
  171. -- If we have enough fuel, let's harvest!
  172. if fuelCycles >= 1 then
  173. turtle.up()
  174. turtle.forward()
  175. turtle.turnRight()
  176. turtle.forward()
  177. turtle.forward()
  178. turtle.forward()
  179. turtle.forward()
  180. turtle.turnLeft()
  181. harvestCrops(width)
  182.  
  183. -- Return to start
  184. turtleReturn()
  185. end
  186.  
  187. -- To allow crops to grow to make
  188. -- harvesting worthwhile, we will
  189. -- wait 40 minutes. 60s * 40
  190. -- Can probably reduce this if
  191. -- using lilypad of fertility or
  192. -- something.
  193. sleep(2400)
  194. end
Advertisement
Add Comment
Please, Sign In to add comment