seiterarch

CC sBuild API

Jan 10th, 2013
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.51 KB | None | 0 0
  1. if not turtle then
  2.     error( "Cannot load turtle API on computer" )
  3. end
  4.  
  5. local function placeNext(face, sn, slots)
  6.     sTurtle.place(face)
  7.     local n = turtle.getItemCount(slots[sn])
  8.     if n == 0 then
  9.         sn = sn + 1
  10.         sTurtle.select(slots[sn])
  11.     end
  12.     return sn
  13. end
  14.  
  15. function platformDown(v1, v2, pos, s, ignoreRCount)
  16.     --Sterilize
  17.     print('Preparing to build platform')
  18.     v1 = sUtil.vForm(v1)
  19.     v2 = sUtil.vForm(v2)
  20.     if v1 == v2 then error('Cannot build between identical points') end
  21.     if v1[2] ~= v2[2] then error('Platform corners must be on same y level') end
  22.     s = sUtil.slotForm(s, true)
  23.     pos = sUtil.posForm(pos)
  24.     sFuel = sUtil.slotForm(s, false, 16)
  25.    
  26.     --Calculate
  27.     local dir = 0
  28.     local depth = 0
  29.     local width = 0
  30.     if v1[1] >= v2[1] and v1[3] >= v2[3] then
  31.         dir = 1
  32.         depth = v2[1] - v1[1]
  33.         width = v2[3] - v1[3]
  34.     elseif v1[1] >= v2[1] and v1[3] < v2[3] then
  35.         dir = 0
  36.         depth = v2[3] - v1[3]
  37.         width = v1[1] - v2[1]
  38.     elseif v1[1] < v2[1] and v1[3] >= v2[3] then
  39.         dir = 2
  40.         depth = v1[3] - v2[3]
  41.         width = v2[1] - v1[1]
  42.     elseif v1[1] < v2[1] and v1[3] < v2[3] then
  43.         dir = 3
  44.         depth = v2[1] - v1[1]
  45.         width = v2[3] - v1[3]
  46.     end
  47.     depth = depth + 1
  48.     width = width + 1
  49.     print('Platform depth: ' .. depth)
  50.     print('Platform width: ' .. width)
  51.    
  52.     local slots = sTurtle.findSlots(s)
  53.     if not ignoreRCount then
  54.         local rCount = sTurtle.sumCount(slots)
  55.         if rCount < (depth * width) then
  56.             error('Platform requires too many resources.')
  57.         else
  58.             print('Turtle has enough resources.')
  59.         end
  60.     else print('Ignoring resource count.')
  61.     end
  62.     local sn = 1
  63.    
  64.     --Align
  65.     print('Travelling to start')
  66.     pos = sTurtle.mineTo({v1[1], v1[2]+1, v1[3], dir}, pos)
  67.    
  68.     --Build
  69.     local forwards = true
  70.     turtle.select(slots[sn])
  71.     print('Building platform')
  72.     for j=1,width do
  73.         for i=1,depth-1 do
  74.             sn = placeNext('down', sn, slots)
  75.             pos = sTurtle.mine(1, pos)
  76.         end
  77.         sn = placeNext('down', sn, slots)
  78.         if j ~= width then
  79.             if forwards then
  80.                 pos = sTurtle.turn('right', pos)
  81.                 pos = sTurtle.mine(1, pos)
  82.                 pos = sTurtle.turn('right', pos)
  83.                 forwards = false
  84.             else
  85.                 pos = sTurtle.turn('left', pos)
  86.                 pos = sTurtle.mine(1, pos)
  87.                 pos = sTurtle.turn('left', pos)
  88.                 forwards = true
  89.             end
  90.         end
  91.     end
  92.     print('Platform finished')
  93.     return pos
  94. end
  95. function platformUp(v1, v2, pos, s, ignoreRCount)
  96.     --Sterilize
  97.     print('Preparing to build ceiling')
  98.     v1 = sUtil.vForm(v1)
  99.     v2 = sUtil.vForm(v2)
  100.     if v1 == v2 then error('Cannot build ceiling between identical points') end
  101.     if v1[2] ~= v2[2] then error('Ceiling corners must be on same y level') end
  102.     s = sUtil.slotForm(s, true)
  103.     pos = sUtil.posForm(pos)
  104.     sFuel = sUtil.slotForm(s, false, 16)
  105.    
  106.     --Calculate
  107.     local dir = 0
  108.     local depth = 0
  109.     local width = 0
  110.     if v1[1] >= v2[1] and v1[3] >= v2[3] then
  111.         dir = 1
  112.         depth = v2[1] - v1[1]
  113.         width = v2[3] - v1[3]
  114.     elseif v1[1] >= v2[1] and v1[3] < v2[3] then
  115.         dir = 0
  116.         depth = v2[3] - v1[3]
  117.         width = v1[1] - v2[1]
  118.     elseif v1[1] < v2[1] and v1[3] >= v2[3] then
  119.         dir = 2
  120.         depth = v1[3] - v2[3]
  121.         width = v2[1] - v1[1]
  122.     elseif v1[1] < v2[1] and v1[3] < v2[3] then
  123.         dir = 3
  124.         depth = v2[1] - v1[1]
  125.         width = v2[3] - v1[3]
  126.     end
  127.     depth = depth + 1
  128.     width = width + 1
  129.     print('Ceiling depth: ' .. depth)
  130.     print('Ceiling width: ' .. width)
  131.    
  132.     local slots = sTurtle.findSlots(s)
  133.     if not ignoreRCount then
  134.         local rCount = sTurtle.sumCount(slots)
  135.         if rCount < (depth * width) then
  136.             error('Ceiling requires too many resources.')
  137.         else
  138.             print('Turtle has enough resources.')
  139.         end
  140.     else print('Ignoring resource count.')
  141.     end
  142.     local sn = 1
  143.    
  144.     --Align
  145.     print('Travelling to start')
  146.     pos = sTurtle.mineTo({v1[1], v1[2]-1, v1[3], dir}, pos)
  147.    
  148.     --Build
  149.     local forwards = true
  150.     turtle.select(slots[sn])
  151.     print('Building ceiling')
  152.     for j=1,width do
  153.         for i=1,depth-1 do
  154.             sn = placeNext('up', sn, slots)
  155.             pos = sTurtle.mine(1, pos)
  156.         end
  157.         sn = placeNext('up', sn, slots)
  158.         if j ~= width then
  159.             if forwards then
  160.                 pos = sTurtle.turn('right', pos)
  161.                 pos = sTurtle.mine(1, pos)
  162.                 pos = sTurtle.turn('right', pos)
  163.                 forwards = false
  164.             else
  165.                 pos = sTurtle.turn('left', pos)
  166.                 pos = sTurtle.mine(1, pos)
  167.                 pos = sTurtle.turn('left', pos)
  168.                 forwards = true
  169.             end
  170.         end
  171.     end
  172.     print('Ceiling finished')
  173.     return pos
  174. end
  175. function wall(v1, v2, pos, s, ignoreRCount)
  176.     --Sterilize
  177.     print('Preparing to build wall')
  178.     v1 = sUtil.vForm(v1)
  179.     v2 = sUtil.vForm(v2)
  180.     if v1 == v2 then error('Cannot wall build between identical points') end
  181.     if v1[1] ~= v2[1] and v1[3] ~= v2[3] then
  182.         error('Wall corners must be equal in either x or z.')
  183.     end
  184.     s = sUtil.slotForm(s, true)
  185.     pos = sUtil.posForm(pos)
  186.     sFuel = sUtil.slotForm(s, false, 16)
  187.    
  188.     --Calculate
  189.     local dir = 0
  190.     local length = math.abs(v2[1] - v1[1])
  191.     if length == 0 then
  192.         length = math.abs(v2[3] - v1[3])
  193.     end
  194.     length = length + 1
  195.     local height = math.abs(v2[2] - v1[2]) + 1
  196.    
  197.     if v1[1] > v2[1] then
  198.         dir = 1
  199.     elseif v1[1] < v2[1] then
  200.         dir = 3
  201.     elseif v1[3] > v2[3] then
  202.         dir = 2
  203.     elseif v1[3] < v2[3] then
  204.         dir = 0
  205.     end
  206.    
  207.     print('Wall length: ' .. length)
  208.     print('Wall height: ' .. height)
  209.    
  210.     --Register building materials
  211.     local slots = sTurtle.findSlots(s)
  212.     if not ignoreRCount then
  213.         local rCount = sTurtle.sumCount(slots)
  214.         if rCount < (length * height) then
  215.             error('Wall requires too many resources.')
  216.         else
  217.             print('Turtle has enough resources.')
  218.         end
  219.     else print('Ignoring resource count.')
  220.     end
  221.     local sn = 1
  222.    
  223.     --Build
  224.     turtle.select(slots[sn])
  225.     print('Building wall')
  226.     for h=1,height do
  227.         for i=1,length-1 do
  228.             sn = placeNext('down', sn, slots)
  229.             pos = sTurtle.mine(1, pos)
  230.         end
  231.         sn = placeNext('down', sn, slots)
  232.         if h ~= height then
  233.             pos = sTurtle.mineUp(1, pos)
  234.             pos = sTurtle.turn('back', pos)
  235.         end
  236.     end
  237.     print('Wall finished')
  238.     return pos
  239. end
  240. function stripDown(length, pos, s, ignoreRCount)
  241.     --Sterilization
  242.     length = sUtil.nForm(length, 1)
  243.     if length <= 0 then
  244.         error('Build must have positive dimensions')
  245.     end
  246.     pos = sUtil.posForm(pos)
  247.    
  248.     --Resources
  249.     local slots = sTurtle.findSlots(s)
  250.     if not ignoreRCount then
  251.         local rCount = sTurtle.sumCount(slots)
  252.         if rCount < (length) then
  253.             error('Strip requires too many resources.')
  254.         else
  255.             print('Turtle has enough resources.')
  256.         end
  257.     else print('Ignoring resource count.')
  258.     end
  259.     local sn = slots[1]
  260.    
  261.     for i=1,length do
  262.         sn = placeNext('down', sn, slots)
  263.         if i < length then
  264.             pos = sTurtle.mine(1, pos)
  265.         end
  266.     end
  267.    
  268.     return pos 
  269. end
  270.  
  271. function tunnel(length, height, pos, inc)
  272.     length = sUtil.nForm(length, 8)
  273.     height = sUtil.nForm(height, 2)
  274.     pos = sUtil.posForm(pos)
  275.     print('Cutting tunnel '..length..' long and '..height..' high.')
  276.    
  277.     if inc ~= true then
  278.         pos = sTurtle.mine(1, pos)
  279.     end
  280.     pos = sTurtle.mineUp(height-1, pos)
  281.    
  282.     while height > 0 do
  283.         for i=1,length-1 do
  284.             pos = sTurtle.mine(1, pos)
  285.             if height > 1 then sTurtle.dig('down') end
  286.         end
  287.         if height > 2 then
  288.             height = height - 2
  289.             pos = sTurtle.turn('back', pos)
  290.             pos = sTurtle.mineDown(2, pos)
  291.             if height > 1 then sTurtle.dig('down') end
  292.         else
  293.             height = 0
  294.         end
  295.     end
  296.    
  297.     return pos
  298. end
  299. function excavate(v1, v2, pos, s, depo)
  300.     print('Preparing to excavate')
  301.    
  302.     --Sterilize
  303.     v1 = sUtil.vForm(v1)
  304.     v2 = sUtil.vForm(v2)
  305.     pos = sUtil.posForm(pos)
  306.     s = sUtil.slotForm(s, true)
  307.     if depo ~= nil then
  308.         depo = sUtil.vForm(depo)
  309.         depo[4] = sUtil.faceForm(depo[4], 'down')
  310.     end
  311.    
  312.     --Compute
  313.     local corners = sUtil.corners(v1, v2)
  314.     local length = 0
  315.     if (corners[1][2] - corners[1][1]) >= (corners[3][2] - corners[3][1]) then
  316.         dir = 3
  317.         length = corners[1][2] - corners[1][1] + 1
  318.     else
  319.         dir = 0
  320.         length = corners[3][2] - corners[3][1] + 1
  321.     end
  322.     local height = corners[2][2] - corners[2][1] + 1
  323.     local vol = 1
  324.     for i = 1,3 do
  325.         vol = vol * (corners[i][2] - corners[i][1] + 1)
  326.     end
  327.    
  328.     --Excavate
  329.     print('Excavating '..vol..'blocks.')
  330.     local target = {0, corners[2][1], 0, dir}
  331.     if dir == 3 then
  332.         target[1] = corners[1][1]
  333.         for i=corners[3][1],corners[3][2] do
  334.             target[3] = i
  335.            
  336.             print(pos[1]..' '..pos[2]..' '..pos[3]..' '..pos[4])
  337.             pos = sTurtle.mineTo(target, pos)
  338.             print(pos[1]..' '..pos[2]..' '..pos[3]..' '..pos[4])
  339.             pos = tunnel(length, height, pos, true)
  340.             print(pos[1]..' '..pos[2]..' '..pos[3]..' '..pos[4])
  341.         end
  342.     else
  343.         target[3] = corners[3][1]
  344.         for i=corners[1][1],corners[1][2] do
  345.             target[1] = i
  346.            
  347.             pos = sTurtle.mineTo(target, pos)
  348.             pos = tunnel(length, height, pos, true)
  349.         end
  350.     end
  351.    
  352.     print('Excavation finished')
  353.     return pos
  354. end
Advertisement
Add Comment
Please, Sign In to add comment