Advertisement
Guest User

fence

a guest
Nov 23rd, 2014
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.03 KB | None | 0 0
  1. local index = 1
  2. turtle.select(1)
  3.  
  4. function place()
  5. while turtle.detectUp() do
  6. turtle.digUp()
  7. end
  8. turtle.digDown()
  9. turtle.placeDown()
  10. if turtle.getItemCount(index) == 0 then
  11. index = index + 1
  12. end
  13. if index > 16 then
  14. index = 1
  15. end
  16. turtle.select(index)
  17. end
  18.  
  19. function removeFence(length,width)
  20. turtle.digDown()
  21. for i = 1, 4 do
  22. if i==1 or i==3 then
  23. side=length
  24. else
  25. side=width
  26. end
  27. for j = 1, side-1 do
  28. turtle.dig()
  29. turtle.forward()
  30. turtle.digDown()
  31. end
  32. turtle.turnRight()
  33. end
  34. end
  35.  
  36. function buildFence(length,width)
  37. place()
  38. for i = 1, 4 do
  39. if i==1 or i==3 then
  40. side=length
  41. else
  42. side=width
  43. end
  44. for j = 1, side-1 do
  45. while turtle.detect() do
  46. turtle.dig()
  47. end
  48. turtle.forward()
  49. place()
  50. end
  51. turtle.turnRight() -- Goes clockwise
  52. end
  53. end
  54.  
  55. function myName()
  56. fullName = shell.getRunningProgram()
  57. return fs.getName(fullName)
  58. end
  59.  
  60. function mats()
  61. count=0
  62. for i=1,16 do
  63. count = count + turtle.getItemCount(i)
  64. end
  65. return count
  66. end
  67.  
  68. function needed(length,width)
  69. return (length*2)+(width*2)
  70. end
  71.  
  72. args = {...}
  73.  
  74. if #args < 3 then
  75. print("Usage:")
  76. print(" "..myName().." <length> <width> <remove|build>")
  77. print(" Turtle should also contain materials to be used as fences, preferably fences.")
  78. return
  79. end
  80.  
  81. if tonumber(args[1])==nil or tonumber(args[2])==nil then
  82. print("Dimensions must be numerical.")
  83. return
  84. end
  85.  
  86. if tonumber(args[1]) < 1 or tonumber(args[2]) < 1 then
  87. print("A dimension may not be less than 1")
  88. return
  89. end
  90.  
  91. if mats() < needed(tonumber(args[1]),tonumber(args[2])) then
  92. print("Not enough materials in turtle to build fence, please add more materials.")
  93. return
  94. end
  95.  
  96. if args[3]== "remove" then
  97. removeFence(tonumber(args[1]),tonumber(args[2]))
  98. elseif args[3]=="build" then
  99. buildFence(tonumber(args[1]),tonumber(args[2]))
  100. else
  101. print("The third option must be either 'remove' or 'build'")
  102. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement