Advertisement
Guest User

5gridchest.lua

a guest
Sep 21st, 2014
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.11 KB | None | 0 0
  1. -- Lays lights for a horizontal 5-grid plane
  2. -- place on an existing light, facing the new area
  3.  
  4. -- storage: lamp ender chest = slot 1, structural material chest = slot 2
  5. -- lamps = slot 3, structural material = slot 4
  6.  
  7. -- mode 2: build window arrays - lamp chest = slot 1, structural chest = slot 2, window chest = slot 3
  8. -- lamps = slot 4, structural = slot 5, window = slot 6
  9.  
  10. lamp_chest_slot = 1
  11. structure_chest_slot = 2
  12. window_chest_slot = 0
  13. lamp_slot = 3
  14. structural_slot = 4
  15. window_slot = 0
  16.  
  17. function go_forward()
  18.   while(turtle.forward() == false) do
  19.     turtle.dig()
  20.     turtle.attack()
  21.   end
  22. end
  23.  
  24. function refill_lamps()
  25.   turtle.select(lamp_chest_slot)
  26.   while(turtle.placeUp() == false) do
  27.     turtle.attackUp()
  28.     turtle.digUp()
  29.   end
  30.   -- if we broke or killed something, we need to drop it
  31.   turtle.select(lamp_slot)
  32.   turtle.drop()
  33.   turtle.select(lamp_chest_slot)
  34.   turtle.drop()
  35.   turtle.suckUp()
  36.   turtle.transferTo(lamp_slot)
  37.   turtle.digUp()
  38. end
  39.  
  40. function refill_structural()
  41.   turtle.select(structure_chest_slot)
  42.   while(turtle.placeUp() == false) do
  43.     turtle.attackUp()
  44.     turtle.digUp()
  45.   end
  46.   -- if we broke or killed something, we need to drop it
  47.   turtle.select(structural_slot)
  48.   turtle.drop()
  49.   turtle.select(structure_chest_slot)
  50.   turtle.drop()
  51.   turtle.suckUp()
  52.   turtle.transferTo(structural_slot)
  53.   turtle.digUp()
  54. end
  55.  
  56. function refill_window()
  57.   turtle.select(window_chest_slot)
  58.   while(turtle.placeUp() == false) do
  59.     turtle.attackUp()
  60.     turtle.digUp()
  61.   end
  62.   -- if we broke or killed something, we need to drop it
  63.   turtle.select(window_slot)
  64.   turtle.drop()
  65.   turtle.select(window_chest_slot)
  66.   turtle.drop()
  67.   turtle.suckUp()
  68.   turtle.transferTo(window_slot)
  69.   turtle.digUp()
  70. end
  71.  
  72. function place_lamp()
  73. while(turtle.getItemCount(lamp_slot) == 0) do
  74.     refill_lamps()
  75.   end
  76.  
  77.   turtle.select(lamp_slot)
  78.  
  79.   while(turtle.placeDown() == false) do
  80.     turtle.digDown()
  81.     turtle.attackDown()
  82.   end
  83.  
  84.   while(turtle.getItemCount(lamp_slot) == 0) do
  85.     refill_lamps()
  86.   end
  87. end
  88.  
  89. function place_structural()
  90. while(turtle.getItemCount(structural_slot) == 0) do
  91.     refill_structural()
  92.   end
  93.  
  94.   turtle.select(structural_slot)
  95.  
  96.   while(turtle.placeDown() == false) do
  97.     turtle.digDown()
  98.     turtle.attackDown()
  99.   end
  100.  
  101.   while(turtle.getItemCount(structural_slot) == 0) do
  102.     refill_structural()
  103.   end
  104. end
  105.  
  106.  
  107.  
  108. function place_window()
  109.   while(turtle.getItemCount(window_slot) == 0) do
  110.     refill_window()
  111.   end
  112.  
  113.   turtle.select(window_slot)
  114.  
  115.   while(turtle.placeDown() == false) do
  116.     turtle.digDown()
  117.     turtle.attackDown()
  118.   end
  119.  
  120.   while(turtle.getItemCount(window_slot) == 0) do
  121.     refill_window()
  122.   end
  123. end
  124.  
  125.  
  126. function build_lamp_row(num)
  127.  -- for each number, move forward 3 times while placing structural blocks underneath
  128.   -- then move forward once more and place a lamp block underneath
  129.   for i = 1, num do
  130.     for j = 1, 3 do
  131.       go_forward()
  132.       place_structural()
  133.     end
  134.    
  135.     go_forward()
  136.     place_lamp()
  137.    
  138.   end
  139. end
  140.  
  141. function build_structural_row(num)
  142.   -- for each number, move forward 4 times while placing structural blocks underneath
  143.   for i = 1, num do
  144.     for j = 1, 4 do
  145.       go_forward()
  146.       place_structural()
  147.     end
  148.    
  149.   end
  150. end
  151.  
  152.  
  153. function build_window_row(num)
  154.   -- for each number, move forward 3 times while placing window blocks underneath
  155.   -- then move forward once more and place a structural block underneath
  156.   for i = 1, num do
  157.     for j = 1, 3 do
  158.       go_forward()
  159.       place_window()
  160.     end
  161.    
  162.     go_forward()
  163.     place_structural()
  164.    
  165.   end
  166. end
  167.  
  168. function turn_turtle(is_lamp)
  169.  
  170.   if(previous == "right") then
  171.     turtle.turnLeft()
  172.     go_forward()
  173.     turtle.turnLeft()
  174.     if(is_lamp) then
  175.       place_lamp()
  176.     else
  177.       place_structural()
  178.     end
  179.     previous = "left"
  180.   elseif(previous == "left") then
  181.     turtle.turnRight()
  182.     go_forward()
  183.     turtle.turnRight()
  184.     if(is_lamp) then
  185.       place_lamp()
  186.     else
  187.       place_structural()
  188.     end
  189.     previous = "right"
  190.   elseif(previous == "up") then
  191.     while(turtle.up() == false) do
  192.       turtle.digUp()
  193.       turtle.attackUp()
  194.     end
  195.     turtle.turnRight()
  196.     turtle.turnRight()
  197.     if(is_lamp) then
  198.       place_lamp()
  199.     else
  200.       place_structural()
  201.     end
  202.     previous = "up"
  203.   end
  204.  
  205. end
  206.  
  207.  
  208. print("Initialising 5-grid construction program.")
  209. print("This program will construct a horizontal or vertical 5-grid plane using items from ender chests. Place on an existing lamp facing towards the new area. It must be pre-loaded with fuel (use an electric charging station).")
  210. print("The turtle will lay lamps and structure blocks in a rectangle ahead and to the left, right or above of its current position.")
  211. print("Enter l for left, r for right or v for vertical:\n")
  212.  
  213. direction=io.read(io.stdin)
  214.  
  215. while (direction ~= "l" and direction ~= "r" and direction ~= "v") do
  216.  print("Error: direction must be \"l\", \"r\" or \"v\"..")
  217.  direction = io.read(io.stdin)
  218. end
  219.  
  220. print("Enter mode (a for wall, b for window):\n")
  221.  
  222. mode=io.read(io.stdin)
  223.  
  224. while (mode ~= "a" and mode ~= "b") do
  225.   print("Error: mode must be \"a\" (wall) or \"b\" (window).")
  226.   mode = io.read(io.stdin)
  227. end
  228.  
  229. if(mode == "b") then
  230.   window_chest_slot = 3
  231.   lamp_slot = 4
  232.   structural_slot = 5
  233.   window_slot = 6
  234. end
  235.  
  236. print("Place the lamp ender chest in the first slot, the material ender chest in the second slot and (if applicable) the window ender chest in the third slot.")
  237. tempvar = io.read(io.stdin)
  238.  
  239. print("Enter number of grid units to build forward:")
  240. numforward = tonumber(io.read(io.stdin))
  241.  
  242. while(numforward <= 0) do
  243.  print("Error: must build more than 1 ahead. Enter number of grid units to build forward.")
  244.  numforward = tonumber(io.read(io.stdin))
  245. end
  246.  
  247. print("Enter number of grid units built to the side:")
  248. numside = tonumber(io.read(io.stdin))
  249.  
  250. while(numside < 0) do
  251.  print("Error: cannot build negative grids. Ender number of grid units to build to the side.")
  252.  numside = tonumber(io.read(io.stdin))
  253. end
  254.  
  255.  
  256. fuelcost = (numside + 1) * numforward * 4 + numside * 4
  257.  
  258. if(fuelcost > turtle.getFuelLevel()) then
  259.  print("Error: do not have enough fuel for this operation. Requires ", fuelcount, ", have ", turtle.getFuelLevel(), ".")
  260.  return
  261. else
  262.  print("Fuel cost: ", fuelcost, ", current fuel level is ", turtle.getFuelLevel(), ".")
  263. end
  264.  
  265. refill_lamps()
  266. refill_structural()
  267.  
  268. if(mode == "b") then
  269.   refill_window()
  270. end
  271.  
  272. previous = "blankety"
  273.  
  274. if(direction == "l") then
  275.  previous = "right"
  276. elseif(direction == "r") then
  277.  previous = "left"
  278. else
  279.   previous = "up"
  280. end
  281.  
  282. build_lamp_row(numforward)
  283. -- the first line will always be lamp/structural
  284. -- the next three will either be pure structural or structural/window, depending on the mode
  285.  
  286. while(numside > 0) do
  287.   for i = 1, 3 do
  288.     turn_turtle(false)
  289.     if(mode == "a") then
  290.       build_structural_row(numforward)
  291.     elseif(mode == "b") then
  292.       build_window_row(numforward)
  293.     end
  294.   end
  295.  
  296.   turn_turtle(true)
  297.   build_lamp_row(numforward)
  298.  
  299.   numside = numside - 1
  300. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement