solidity

roombuilder

May 27th, 2013
247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 11.49 KB | None | 0 0
  1. -- builds a room with inner dimensions width, depth, height as specified by args out of the given materials
  2. -- supply with:
  3. -- slot 1: nothing (used to store wall materials
  4. -- slot 2: resupply chest
  5. -- slot 3: refuel chest
  6. -- slot 4: dump chest
  7. -- and put into bottom front left corner of the room
  8.  
  9. local swall = 1
  10. local ssupply = 2
  11. local sfuel = 3
  12. local sdump = 4
  13. local sfree = math.max(sdump,sfuel,ssupply,swall)+1
  14. local args = {...}
  15. local w, d, h = tonumber(args[1]), tonumber(args[2]), tonumber(args[3])
  16.  
  17. function dump()
  18.     local isFull = true
  19.     for s = 16,sfree,-1 do
  20.         if turtle.getItemCount(s) == 0 then
  21.             isFull = false
  22.             break
  23.         end
  24.     end
  25.     if isFull then
  26.         print("dumping inventory")
  27.         -- put down dump chest
  28.         turtle.select(sdump)
  29.         while not turtle.placeDown() do
  30.             if turtle.detectDown() then
  31.                 turtle.digDown()
  32.             else
  33.                 turtle.attackDown()
  34.             end
  35.             sleep(0.2)
  36.         end
  37.         -- drop stuff
  38.         for s = 16,sfree,-1 do
  39.             if turtle.getItemCount(s) > 0 then
  40.                 turtle.select(s)
  41.                 while not turtle.dropDown() do
  42.                     sleep(0.2)
  43.                 end
  44.             end
  45.         end
  46.         -- pick up dump chest
  47.         turtle.select(sdump)
  48.         turtle.digDown()
  49.         turtle.select(swall)
  50.     end
  51. end
  52.  
  53. function refuel(level)
  54.     if turtle.getFuelLevel() < level then
  55.         -- free up inventory
  56.         dump()
  57.         -- put down fuel chest
  58.         print("refueling")
  59.         turtle.select(sfuel)
  60.         while not turtle.placeDown() do
  61.             if turtle.detectDown() then
  62.                 turtle.digDown()
  63.             else
  64.                 turtle.attackDown()
  65.             end
  66.             sleep(0.2)
  67.         end
  68.         -- get fuel from chest
  69.         while turtle.getFuelLevel() < level do
  70.             if turtle.getItemCount(sfuel) < 1 then
  71.                 turtle.suckDown()
  72.             end
  73.             turtle.refuel(1)
  74.             print("fueled up to "..turtle.getFuelLevel().." / "..level)
  75.         end
  76.         -- put remaining fuel back in chest and move it out of the way if the chest is full
  77.         turtle.dropDown()
  78.         if turtle.getItemCount(sfuel) > 0 then
  79.             for s = 16,sfree,-1 do
  80.                 if turtle.transferTo(s) then
  81.                     break
  82.                 end
  83.             end
  84.             if turtle.getItemCount(sfuel) > 0 then
  85.                 turtle.select(sfuel)
  86.                 turtle.drop()
  87.             end
  88.         end
  89.         -- pick up fuel chest
  90.         turtle.select(sfuel)
  91.         turtle.digDown()
  92.         turtle.select(swall)
  93.     end
  94. end
  95.  
  96. function resupply()
  97.     -- free up inventory
  98.     dump()
  99.     print("resupplying")
  100.     -- remove any items that could be in the way
  101.     if turtle.getItemCount(swall) > 0 then
  102.         turtle.select(swall)
  103.         for s = 16,sfree,-1 do
  104.             if turtle.transferTo(s) then
  105.                 break
  106.             end
  107.         end
  108.         if turtle.getItemCount(swall) > 0 then
  109.             turtle.select(swall)
  110.             turtle.drop()
  111.         end
  112.     end
  113.     -- put down supply chest
  114.     turtle.select(ssupply)
  115.     while not turtle.placeDown() do
  116.         if turtle.detectDown() then
  117.             turtle.digDown()
  118.         else
  119.             turtle.attackDown()
  120.         end
  121.         sleep(0.2)
  122.     end
  123.     -- get supplies
  124.     turtle.select(swall)
  125.     turtle.suckDown()
  126.     -- pick up supply chest
  127.     turtle.select(ssupply)
  128.     if turtle.getItemCount(ssupply) > 0 then
  129.         for s = 16,sfree,-1 do
  130.             if turtle.transferTo(s) then
  131.                 break
  132.             end
  133.         end
  134.         if turtle.getItemCount(ssupply) > 0 then
  135.             turtle.select(ssupply)
  136.             turtle.drop()
  137.         end
  138.     end
  139.     turtle.select(ssupply)
  140.     turtle.digDown()
  141.     turtle.select(swall)
  142. end
  143.  
  144. -- movement functions
  145.  
  146. function left(turns)
  147.     if turns == nil then
  148.         turns = 1
  149.     end
  150.     for i = 1,turns do
  151.         turtle.turnLeft()
  152.     end
  153. end
  154.  
  155. function right(turns)
  156.     if turns == nil then
  157.         turns = 1
  158.     end
  159.     for i = 1,turns do
  160.         turtle.turnRight()
  161.     end
  162. end
  163.  
  164. function move(dir, dist)
  165.     if dist == nil then
  166.         dist = 1
  167.     end
  168.     for d = 1, dist do
  169.         if dir == "f" then
  170.             while not turtle.forward() do
  171.                 if turtle.getFuelLevel() == 0 then
  172.                     refuel(1000)
  173.                 elseif turtle.detect() then
  174.                     turtle.dig()
  175.                 else
  176.                     turtle.attack()
  177.                 end
  178.                 sleep(0.2)
  179.             end
  180.         elseif dir == "u" then
  181.             while not turtle.up() do
  182.                 if turtle.getFuelLevel() == 0 then
  183.                     refuel(1000)
  184.                 elseif turtle.detectUp() then
  185.                     turtle.digUp()
  186.                 else
  187.                     turtle.attackUp()
  188.                 end
  189.                 sleep(0.2)
  190.             end
  191.         elseif dir == "d" then
  192.             while not turtle.down() do
  193.                 if turtle.getFuelLevel() == 0 then
  194.                     refuel(1000)
  195.                 elseif turtle.detectDown() then
  196.                     turtle.digDown()
  197.                 else
  198.                     turtle.attackDown()
  199.                 end
  200.                 sleep(0.2)
  201.             end
  202.         elseif dir == "b" then
  203.             while not turtle.back() do
  204.                 if turtle.getFuelLevel() == 0 then
  205.                     refuel(1000)
  206.                 else
  207.                     left(2)
  208.                     while not turtle.forward() do
  209.                         if turtle.detect() then
  210.                             turtle.dig()
  211.                         else
  212.                             turtle.attack()
  213.                         end
  214.                         sleep(0.2)
  215.                     end
  216.                     left(2)
  217.                     break
  218.                 end
  219.             end
  220.         end
  221.     end
  222. end
  223.  
  224. function buildCeiling(w,d)
  225.     for cw = 1,w do
  226.         for cd = 1,d do
  227.             --replace ceiling if necessary
  228.             if not turtle.compareUp() then
  229.                 while not turtle.placeUp() do
  230.                     if turtle.detectUp() then
  231.                         turtle.digUp()
  232.                     else
  233.                         turtle.attackUp()
  234.                     end
  235.                     sleep(0.2)
  236.                 end
  237.             end
  238.             -- check if supplies are still okay
  239.             if turtle.getItemCount(swall) == 0 then
  240.                 resupply()
  241.             end
  242.             -- dig floor
  243.             if turtle.detectDown() then
  244.                 turtle.digDown()
  245.             end
  246.             -- move forward if not at end, else turn to next line
  247.             if cd < d then
  248.                 move("f")
  249.             elseif cw < w then
  250.                 if cw % 2 == 1 then
  251.                     right()
  252.                     move("f")
  253.                     right()
  254.                 else
  255.                     left()
  256.                     move("f")
  257.                     left()
  258.                 end
  259.             end
  260.         end
  261.     end
  262. end
  263.  
  264. function buildWallSegment(w,d,h,sh)
  265.     m1, m2 = w, d
  266.     if w%2 == 0 then
  267.         right()
  268.     else
  269.         left()
  270.     end
  271.     dir = "f"
  272.     for i=1,4 do
  273.         if i == 3 then
  274.             dir = "b"
  275.             m1, m2 = m2, m1
  276.         end
  277.         for c1 = 1,m1 do
  278.             if i < 3 then
  279.                 if turtle.detectUp() then
  280.                     if not turtle.compareUp() then
  281.                         turtle.digUp()
  282.                         dump()
  283.                     end
  284.                 end
  285.                 if turtle.detectDown() then
  286.                     if not turtle.compareDown() then
  287.                         turtle.digDown()
  288.                         dump()
  289.                     end
  290.                 end
  291.             else
  292.                 if not turtle.compareUp() then
  293.                     while not turtle.placeUp() do
  294.                         if turtle.detectUp() then
  295.                             turtle.digUp()
  296.                             dump()
  297.                         else
  298.                             turtle.attackUp()
  299.                         end
  300.                         sleep(0.2)
  301.                     end
  302.                     if turtle.getItemCount(1) < 1 then
  303.                         resupply()
  304.                     end
  305.                 end
  306.                 if not turtle.compareDown() then
  307.                     while not turtle.placeDown() do
  308.                         if turtle.detectDown()  then
  309.                             turtle.digDown()
  310.                             dump()
  311.                         else
  312.                             turtle.attackDown()
  313.                         end
  314.                         sleep(0.2)
  315.                     end
  316.                     if turtle.getItemCount(1) < 1 then
  317.                         resupply()
  318.                     end
  319.                     if not turtle.compareDown() then
  320.                         while not turtle.placeDown() do
  321.                             if turtle.detectDown()  then
  322.                                 turtle.digDown()
  323.                                 dump()
  324.                             else
  325.                                 turtle.attackDown()
  326.                             end
  327.                             sleep(0.2)
  328.                         end
  329.                     end
  330.                 end
  331.             end
  332.             if c1 < m1 then
  333.                 move(dir)
  334.                 if i>2 then
  335.                     if not turtle.compare() then
  336.                         while not turtle.place() do
  337.                             if turtle.detect() then
  338.                                 turtle.dig()
  339.                                 dump()
  340.                             else
  341.                                 turtle.attack()
  342.                             end
  343.                             sleep(0.2)
  344.                         end
  345.                         if turtle.getItemCount(1) < 1 then
  346.                             resupply()
  347.                         end
  348.                     end
  349.                 end
  350.             end
  351.         end
  352.         if w%2 == 0 then
  353.             if i < 3 then
  354.                 right()
  355.             else
  356.                 left()
  357.             end
  358.         else
  359.             if i < 3 then
  360.                 left()
  361.             else
  362.                 right()
  363.             end
  364.         end
  365.         for c2 = 1,m2 do
  366.             if i<3 then
  367.                 if turtle.detectUp() then
  368.                     if not turtle.compareUp() then
  369.                         turtle.digUp()
  370.                         dump()
  371.                     end
  372.                 end
  373.                 if turtle.detectDown() then
  374.                     if not turtle.compareDown() then
  375.                         turtle.digDown()
  376.                         dump()
  377.                     end
  378.                 end
  379.             else
  380.                 if not turtle.compareUp() then
  381.                     while not turtle.placeUp() do
  382.                         if turtle.detectUp() then
  383.                             turtle.digUp()
  384.                             dump()
  385.                         else
  386.                             turtle.attackUp()
  387.                         end
  388.                         sleep(0.2)
  389.                     end
  390.                     if turtle.getItemCount(1) < 1 then
  391.                         resupply()
  392.                     end
  393.                 end
  394.                 if not turtle.compareDown() then
  395.                     while not turtle.placeDown() do
  396.                         if turtle.detectDown()  then
  397.                             turtle.digDown()
  398.                             dump()
  399.                         else
  400.                             turtle.attackDown()
  401.                         end
  402.                         sleep(0.2)
  403.                     end
  404.                     if turtle.getItemCount(1) < 1 then
  405.                         resupply()
  406.                     end
  407.                     if not turtle.compareDown() then
  408.                         while not turtle.placeDown() do
  409.                             if turtle.detectDown()  then
  410.                                 turtle.digDown()
  411.                                 dump()
  412.                             else
  413.                                 turtle.attackDown()
  414.                             end
  415.                             sleep(0.2)
  416.                         end
  417.                     end
  418.                 end
  419.             end
  420.             if ((c2 < m2) and (i < 4)) or ((c2 < m2-1) and (i==4)) then
  421.                 move(dir)
  422.                 if i>2 then
  423.                     if not turtle.compare() then
  424.                         while not turtle.place() do
  425.                             if turtle.detect() then
  426.                                 turtle.dig()
  427.                                 dump()
  428.                             else
  429.                                 turtle.attack()
  430.                             end
  431.                             sleep(0.2)
  432.                         end
  433.                         if turtle.getItemCount(1) < 1 then
  434.                             resupply()
  435.                         end
  436.                     end
  437.                 end
  438.             end
  439.         end
  440.         if (i==1) or (i==3) then
  441.             if w%2 == 0 then
  442.                 if i < 3 then
  443.                     right()
  444.                 else
  445.                     left()
  446.                 end
  447.             else
  448.                 if i < 3 then
  449.                     left()
  450.                 else
  451.                     right()
  452.                 end
  453.             end
  454.         end
  455.     end
  456. end
  457.  
  458. function buildWalls(w,d,h)
  459.     move("d")
  460.     local segments = math.floor((h-1)/3)
  461.     local sh = 3
  462.     for s = 1,segments+1 do
  463.         if s==segments then
  464.             sh = (h-1)%3
  465.         end
  466.         buildWallSegment(w,d,h,sh)
  467.         if s <= segments then
  468.             if w%2 == 0 then
  469.                 right()
  470.             else
  471.                 left()
  472.             end
  473.             move("f")
  474.             left(2)
  475.             while not turtle.place() do
  476.                 if turtle.detect() then
  477.                     turtle.dig()
  478.                 else
  479.                     turtle.attack()
  480.                 end
  481.                 sleep(0.2)
  482.             end
  483.             if turtle.getItemCount(swall) < 1 then
  484.                 resupply()
  485.             end
  486.             move("d",sh)
  487.             move("f")
  488.             if w%2 == 0 then
  489.                 left()
  490.             else
  491.                 right()
  492.             end
  493.             move("f")
  494.             if w%2 == 0 then
  495.                 right()
  496.             else
  497.                 left()
  498.             end
  499.         end
  500.     end
  501.     if w%2 == 0 then
  502.         right()
  503.     else
  504.         left()
  505.     end
  506.     move("f")
  507.     left(2)
  508.     while not turtle.place() do
  509.         if turtle.detect() then
  510.             turtle.dig()
  511.         else
  512.             turtle.attack()
  513.         end
  514.         sleep(0.2)
  515.     end
  516.     if turtle.getItemCount(swall) < 1 then
  517.         resupply()
  518.     end
  519.     left(2)
  520. end
  521.  
  522. function clearInside(w,d,h)
  523.     move("u",h-2)
  524.     layers = math.floor(h/3)
  525.     ch = 3
  526.     m1, m2 = w, d
  527.     for l=1,layers+1 do
  528.         if l == layers then
  529.             ch = (h+1)%3
  530.         end
  531.         for c1 = 1,m1 do
  532.             for c2 = 1,m2 do
  533.                 if turtle.detectUp() then
  534.                     turtle.digUp()
  535.                     dump()
  536.                 end
  537.                 if l==layers+1 then
  538.                     if not turtle.compareDown() then
  539.                         while not turtle.placeDown() do
  540.                             if turtle.detectDown() then
  541.                                 turtle.digDown()
  542.                                 dump()
  543.                             else
  544.                                 turtle.attackDown()
  545.                             end
  546.                             sleep(0.2)
  547.                         end
  548.                         if turtle.getItemCount(swall) < 1 then
  549.                             resupply()
  550.                         end
  551.                         if not turtle.compareDown() then
  552.                             while not turtle.placeDown() do
  553.                                 if turtle.detectDown()  then
  554.                                     turtle.digDown()
  555.                                     dump()
  556.                                 else
  557.                                     turtle.attackDown()
  558.                                 end
  559.                                 sleep(0.2)
  560.                             end
  561.                         end
  562.                     end
  563.                 else
  564.                     if turtle.detectDown() then
  565.                         turtle.digDown()
  566.                         dump()
  567.                     end
  568.                 end
  569.                 if c2 < m2 then
  570.                     move("f")
  571.                 end
  572.             end
  573.             if c1 < m1 then
  574.                 if m1%2 == 1 then
  575.                     right()
  576.                     move("f")
  577.                     right()
  578.                 else
  579.                     left()
  580.                     move("f")
  581.                     left()
  582.                 end
  583.             end
  584.         end
  585.         if l <= layers then
  586.             move("d",ch)
  587.             if m1%2 == 1 then
  588.                 left(2)
  589.             else
  590.                 right()
  591.                 m1, m2 = m2, m1
  592.             end
  593.         end
  594.     end
  595. end
  596.  
  597. if h > 3 then
  598.     print("building a "..w.." wide, "..d.." deep, "..h.." high room.")
  599.     -- refuel
  600.     refuel(w*d*h)
  601.     resupply()
  602.     -- goto one block below top left front corner
  603.     for i = 1,2 do
  604.         left()
  605.         move("f")
  606.     end
  607.     left(2)
  608.     move("u",h-1)
  609.     turtle.select(swall)
  610.     buildCeiling(w+2,d+2)
  611.     buildWalls(w+2,d+2,h+2)
  612.     clearInside(w,d,h)
  613. end
Advertisement
Add Comment
Please, Sign In to add comment