Advertisement
Houshalter

ComputerCraft Floor Builder

Mar 4th, 2013
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.00 KB | None | 0 0
  1.  
  2. --since I keep having to rewrite this code but can't find a way to pass and execute functions at runtime in lua, I will just make a template for it.
  3. --this just repeats something in 3d, doing each row, layer, and column as efficiently as possible
  4. --you need global variables that tell where the turtle's x y and z position is, usually relative to it's origin or home position.
  5. --this assumes that it is starting in the bottom left corner. Can be adjusted.
  6.  
  7. --inventory functions
  8. --getnumber(id) --gets the number of items in the inventory. For most cases use anyleft instead as it doesn't usually need to perform an entire inventory update
  9. --getslot(id) --gets the first slot that the item can be found in
  10. --asignidorder(1, 2, 3, ...) --assigns ids to each item in the order they occur in the inventory without knowing the actual slots they occur in. Spaces between items will stop the count
  11. --anyleft(id) --returns true if there are more than one of the item in the turtles inventory, else false (including if there are but it cannot identify them.)
  12. --updateinventory() --updates the inventory and tells where each item is located
  13. --sortinventory()
  14. --updateitem(id, number) --tells the inventory that number of items was added
  15. --itemadded(morethanone) --tells the inventory that an item was added, but not what it was. argument is true if more than one item was added
  16. --fuel(id, id2, ...) --asigns each item as a fuel useable by the turtle, in the order of priorty (reasigns priority if was used before)
  17. --notfuel(id) --removes an item from the fuel list
  18. --refuel(level) --if level = 0 it will consume all fuels, otherwise consumes fuel until the turtles fuel level is up the the level specified
  19. --selectitem(id) --selects the first instance of item if not already selected. Does not update the stack or inventory unless it can not find any
  20. --selectempty() --selects an empty slot
  21.  
  22. function refuel()
  23. for i = 1, 16 do
  24. turtle.select(i)
  25. turtle.refuel()
  26. end
  27. slot = 1
  28. turtle.select(1)
  29. end
  30.  
  31. function digforward(n)
  32. for i = 1, n do
  33. repeat
  34. if turtle.detect() then turtle.dig() end
  35. until turtle.forward()
  36. if direction == 1 then x = x + 1 end
  37. if direction == 2 then y = y + 1 end
  38. if direction == 3 then x = x - 1 end
  39. if direction == 4 then y = y - 1 end
  40. end
  41.  
  42. --refueling and other functions should go here, as well as the up and down functions
  43. end
  44.  
  45. function digdown(n)
  46. for i = 1, n do
  47. repeat
  48. if turtle.detectDown() then turtle.digDown() end
  49. until turtle.down()
  50. z = z - 1
  51. end
  52. end
  53.  
  54. function digup(n)
  55. for i = 1, n do
  56. repeat
  57. if turtle.detectUp() then turtle.digUp() end
  58. until turtle.up()
  59. z = z + 1
  60. end
  61. end
  62.  
  63. function turnright(n)
  64. for i = 1, n do
  65. turtle.turnRight()
  66. direction = direction + 1
  67. if direction > 4 then direction = 1 end
  68. end
  69. end
  70.  
  71. function turnleft(n)
  72. for i = 1, n do
  73. turtle.turnLeft()
  74. direction = direction - 1
  75. if direction < 1 then direction = 4 end
  76. end
  77. end
  78.  
  79. function turn(isright)
  80. if isright then turnright(1)
  81. else turnleft(1) end
  82. end
  83.  
  84. function turncorner(isright)
  85. turn(isright)
  86. digforward(1)
  87. turn(isright)
  88. end
  89.  
  90. function gotox(x)
  91. if gx > x then face(3)
  92. elseif gx < x then face(1)
  93. elseif gx == x then return end
  94. digforward(1)
  95. gotox(x)
  96. end
  97.  
  98. function gotoy(y)
  99. if gy > y then face(4)
  100. elseif gy < y then face(2)
  101. elseif gy == y then return end
  102. digforward(1)
  103. gotoy(y)
  104. end
  105.  
  106. function gotoz(z)
  107. if gz < z then digup(1) gotoz(z) end
  108. if gz > z then digdown(1) gotoz(z) end
  109. end
  110.  
  111. function face(n)
  112. if direction == n then return end
  113. if direction < 3 then turnleft(1)
  114. else turnright(1) end
  115. face(n)
  116. end
  117.  
  118. function iseven(n)
  119. if math.fmod(n, 2) == 0 then return true
  120. else return false end
  121. end
  122.  
  123. function doblock()
  124. if turtle.detectDown() then turtle.digDown() end
  125. if turtle.getItemCount(slot) < 1 then
  126. slot = slot + 1
  127. if slot > 16 then print("Out of materials.\nRefill turtle and press Enter when ready.") io.read() slot = 1 end
  128. turtle.select(slot)
  129. end
  130. turtle.placeDown()
  131. end
  132.  
  133. function dorow(fx)
  134. while true do
  135. doblock()
  136. if fx < 2 then break end
  137. digforward(1)
  138. fx = fx - 1
  139. end
  140. end
  141.  
  142. function dolayer(fx, fy, isright)
  143. while true do
  144. dorow(fx)
  145. fy = fy - 1
  146. if fy < 2 then break end
  147. turncorner(isright)
  148. isright = not isright
  149. end
  150. end
  151.  
  152. function fillfloor(fx, fy, fz)
  153. local isright = true
  154. while true do
  155. dolayer(fx, fy, isright)
  156. fz = fz - 1
  157. if fz < 2 then break end
  158. digup()
  159. turnright(2)
  160. if iseven(fy) then isright = not isright end
  161. end
  162. end
  163.  
  164. function sethome()
  165. direction = 1
  166. x = 0
  167. y = 0
  168. z = 0
  169. end
  170.  
  171.  
  172. print("Enter forward dimension: ")
  173. forward = tonumber(io.read())
  174. print("Enter sidways dimension: ")
  175. sideways = tonumber(io.read())
  176. print("Fill turtle with fuel then press Enter.")
  177. io.read()
  178. refuel()
  179. print("Fill full of building materials and press Enter when ready to begin.")
  180. io.read()
  181. sethome()
  182. fillfloor(forward, sideways, 1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement