Advertisement
denesik

farm

Feb 3rd, 2013
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.34 KB | None | 0 0
  1. --pastebin get Wn9kYJcT farm
  2. local lenght,width = 9,9
  3. local pos = {
  4. ["x"] = 0,
  5. ["y"] = -1,
  6. }
  7. local direction = {
  8. ["forw"] = 1,
  9. ["right"] = 2,
  10. ["back"] = 3,
  11. ["left"] = 4,
  12. }
  13. local dir = direction.forw
  14.  
  15. local function farm()
  16. turtle.digDown()
  17. turtle.placeDown()
  18. end
  19.  
  20. local function takeSeeds()
  21. turtle.select(1)
  22. local ammount = lenght * width
  23. if ammount > 64 then ammount = 64 end
  24. if turtle.getItemCount(1) < ammount then
  25. print("Don't have seeds")
  26. end
  27. while turtle.getItemCount(1) < ammount do
  28. turtle.suckDown()
  29. os.sleep(1)
  30. end
  31. print("I took the seeds")
  32. end
  33.  
  34. local function returnSupplies()
  35. for i = 2,16 do
  36. if turtle.getItemCount(i)>0 then
  37. turtle.select(i)
  38. turtle.drop()
  39. end
  40. end
  41. turtle.select(1)
  42. end
  43.  
  44. local function turnRight()
  45. dir = dir + 1
  46. if dir == 5 then dir = direction.forw end
  47. -- saveState()
  48. turtle.turnRight()
  49. end
  50.  
  51. local function turnLeft()
  52. dir = dir - 1
  53. if dir == 0 then dir = direction.left end
  54. -- saveState()
  55. turtle.turnLeft()
  56. end
  57.  
  58. local function forward()
  59. turtle.forward()
  60. if dir == direction.forw then
  61. pos.y = pos.y + 1
  62. elseif dir == direction.right then
  63. pos.x = pos.x + 1
  64. elseif dir == direction.back then
  65. pos.y = pos.y - 1
  66. elseif dir == direction.left then
  67. pos.x = pos.x - 1
  68. end
  69. end
  70.  
  71. function goTo( x, y, d)
  72. if pos.x > x then
  73. while dir ~= direction.left do
  74. turnRight()
  75. end
  76. while pos.x > x do
  77. forward()
  78. end
  79. elseif pos.x < x then
  80. while dir ~= direction.right do
  81. turnRight()
  82. end
  83. while pos.x < x do
  84. forward()
  85. end
  86. end
  87. if pos.y > y then
  88. while dir ~= direction.back do
  89. turnLeft()
  90. end
  91. while pos.y > y do
  92. forward()
  93. end
  94. elseif pos.y < y then
  95. while dir ~= direction.forw do
  96. turnLeft()
  97. end
  98. while pos.y < y do
  99. forward()
  100. end
  101. end
  102. while d ~= dir do
  103. turnLeft()
  104. end
  105. end
  106.  
  107. local function endFarm()
  108. return (pos.x == width - 1) and ((pos.y == lenght - 1 and math.fmod(pos.x,2) == 0) or (pos.y == 0 and math.fmod(pos.x,2) == 1))
  109. end
  110.  
  111. local function main()
  112. while not endFarm() do
  113. if math.fmod(pos.x,2) == 0 then -- 010101
  114. if pos.y == lenght-1 then
  115. if dir == direction.forw then
  116. turnRight()
  117. elseif dir == direction.right then
  118. forward()
  119. farm()
  120. end
  121. elseif pos.y == 0 and dir == direction.right then
  122. turnLeft()
  123. else
  124. forward()
  125. farm()
  126. end
  127. else
  128. if pos.y == 0 then
  129. if dir == direction.back then
  130. turnLeft()
  131. elseif dir == direction.right then
  132. forward()
  133. farm()
  134. end
  135. elseif pos.y == lenght-1 and dir == direction.right then
  136. turnRight()
  137. else
  138. forward()
  139. farm()
  140. end
  141. end
  142. end
  143. goTo( 0, -1, direction.back)
  144. returnSupplies()
  145. goTo( 0, -1, direction.forw)
  146. end
  147.  
  148. while true do
  149. if redstone.getInput("top") == true then
  150. takeSeeds()
  151. main()
  152. end
  153. os.sleep(5)
  154. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement