topspeed

stairs2

May 21st, 2022 (edited)
925
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.29 KB | None | 0 0
  1. --
  2. -- KINDA WORKING DON'T TRY IT
  3. --
  4.  
  5. iArg = { ... }
  6. side=tonumber(iArg[1]) or 10
  7. computerId = os.getComputerID()
  8. print("computer ID =", computerId)
  9. x = 0
  10. y = 0
  11. z = 0
  12.  
  13. function refuel( amount )
  14.     print("Refueling...")
  15.     local fuelLevel = turtle.getFuelLevel()
  16.     if fuelLevel == "unlimited" then
  17.         return true
  18.     end
  19.    
  20.     local needed = amount or (xPos + zPos + depth + 2)
  21.     if turtle.getFuelLevel() < needed then
  22.         local fueled = false
  23.         for n=1,16 do
  24.             if turtle.getItemCount(n) > 0 then
  25.                 turtle.select(n)
  26.                 if turtle.refuel(1) then
  27.                     while turtle.getItemCount(n) > 0 and turtle.getFuelLevel() < needed do
  28.                         turtle.refuel(1)
  29.                     end
  30.                     if turtle.getFuelLevel() >= needed then
  31.                         turtle.select(1)
  32.                         return true
  33.                     end
  34.                 end
  35.             end
  36.         end
  37.         turtle.select(1)
  38.         return false
  39.     end
  40.    
  41.     return true
  42. end
  43.  
  44. function fuel()
  45.     if turtle.getFuelLevel() < 100 then
  46.         refuel(120)
  47.     end
  48.     print("Fuel",turtle.getFuelLevel(),"/",turtle.getFuelLimit())
  49. end
  50.  
  51. function placetorch()
  52.     data = turtle.getItemDetail(16,true)
  53.     if data == nil then return end
  54.     if data.name.find(data.name,"torch") then
  55.         turtle.back()
  56.         turtle.turnRight()
  57.         turtle.turnRight()
  58.         previouslySelectedSlot = turtle.getSelectedSlot()
  59.         turtle.select(16)
  60.         turtle.select(previouslySelectedSlot)
  61.         turtle.place()
  62.         turtle.back()
  63.         turtle.turnLeft()
  64.         turtle.turnLeft()
  65.     end
  66. end
  67.  
  68. function adv(x,t)
  69.     for y=1,x do
  70.         tries = 0
  71.         turtle.digUp()
  72.         while turtle.forward() ~= true do
  73.             turtle.digUp()
  74.             turtle.dig()
  75.             turtle.attack()
  76.             sleep(0.2)
  77.             tries = tries + 1
  78.             if tries>500 then
  79.             print("Error: can't move forward.")
  80.         end
  81.     end
  82.     if x >= 10 and y%12 == 2 and t ~= 0 then placetorch() end
  83.         fuel()
  84.     end
  85. end
  86.  
  87. local function openRednet()
  88.     local listOfSides = rs.getSides()
  89.     for i = 1,6 do
  90.         if peripheral.isPresent(listOfSides[i]) and peripheral.getType(listOfSides[i]) == "modem" then
  91.             rednet.open(listOfSides[i])
  92.             return listOfSides[i]
  93.         end
  94.     end
  95. end
  96.  
  97. function orientation()  --get my bearings by comparing location to location+1
  98.     openRednet()
  99.     x1,y1,z1=gps.locate(computerId)
  100.     if (x1 == nil) then
  101.         print("Cannot find Rednet. Move closer or build another GPS system.")
  102.         os.exit()
  103.     end
  104.     sleep(0.5)
  105.     -----------
  106.     while not turtle.forward() do turtle.dig() turtle.attack() end  
  107.     sleep(0.5)
  108.     x2,y2,z2=gps.locate(computerId)
  109.     print("Location:",x1,y1,z1)
  110.     turtle.back() -- now move back to original location
  111.     if x2>x1 then ori = "east" pos=1 end
  112.     if x2<x1 then ori = "west" pos=2 end
  113.     if z2<z1 then ori = "north" pos=3 end
  114.     if z2>z1 then ori = "south" pos=4 end
  115.     return ori, pos
  116. end
  117.  
  118. function orient(pos,dir)            -- turn until aligned to new direction
  119.   while dir ~= pos do
  120.       turtle.turnLeft()
  121.       if pos==2 then pos=4
  122.         elseif pos==4 then pos=1
  123.         elseif pos==1 then pos=3
  124.         elseif pos==3 then pos=2
  125.       end
  126.   end
  127. end
  128.  
  129. function clear(sides)   -- recursively move tighter and tighter
  130.    if tonumber(sides) < 1 then -- return home
  131.         moveto(ox,oy,oz)    
  132.         facing,from=orientation()
  133.         orient(from,or1)
  134.         print("orient(" .. from .. "," .. or1 .. ")")
  135.         return
  136.    end
  137.    for i=1,3 do
  138.       adv(sides,1)
  139.       turtle.turnLeft()
  140.    end
  141.    clear(sides-1)
  142. end
  143.  
  144. function avoid(orientation)
  145.   local stuck = 0
  146.   while not turtle.up() do
  147.      if turtle.back() then
  148.        if orientation==1 then x=x-1 print("x-1") end
  149.        if orientation==2 then x=x+1 print("x+1") end
  150.        if orientation==3 then z=z+1 print("z+1") end
  151.        if orientation==4 then z=z-1 print("z-1") end
  152.      else
  153.        stuck = stuck + 1
  154.      end
  155.      if stuck == 20 then turtle.digUp() stuck = 0 end --attempt to free the turtle.
  156.   end
  157.   y=y+1 --moved up once
  158. end
  159.  
  160. function reset()
  161.     down=0
  162.     while not turtle.detectDown() do
  163.         turtle.down()
  164.         down=down+1
  165.     end
  166.     return down
  167. end
  168.  
  169. function move(dir)
  170.     print("Orienting to dir =",dir)
  171.     orient(dir)
  172.     while not turtle.forward() do
  173.         avoid(dir)
  174.     end
  175.     if dir=="up" then
  176.      turtle.digUp()  --?
  177.      turtle.up()
  178.     end
  179.     if dir=="down" then
  180.         turtle.digDown() --?
  181.         turtle.down()
  182.     end
  183.     openRednet()
  184.     rednet.broadcast(loc)
  185.     rednet.close()
  186. end
  187.  
  188. function moveto(x,y,z)
  189.     pos=""
  190.     fuel()
  191.     dx=tonumber(x)
  192.     dy=tonumber(y)
  193.     dz=tonumber(z)
  194.     x,y,z=gps.locate(computerId)
  195.  
  196.     ori,pos = orientation()
  197.     print("position is " .. pos .. ".  Orientation is " .. ori)
  198.     print("I am at x=",x," y=",y," z=",z)
  199.     print("I am going to x=",dx," y=",dy," z=",dz)
  200.  
  201.     lx = math.abs(dx-x)
  202.     ly = math.abs(dy-y)
  203.     lz = math.abs(dz-z)
  204.     if (lx > 300) or (ly > 300) or (lz > 300) then
  205.         print("Warning, distance beyond rednet range. Turtle will be unaccessible.")
  206.         --  if not rednet.isOpen("right") then rednet.open("right") end
  207.         openRednet()       
  208.         rednet.broadcast("Warning, distance beyond rednet range. Turtle will be unaccessible.")
  209.         rednet.close("right")
  210.     end
  211.  
  212.     while dx>x do print("x",dx,">",x,"  Need to move to 1 (east)") ori="east" x=x+1 move(pos,1) end  --east
  213.     while dx<x do print("x",dx,"<",x,"  Need to move to 2 (west)") ori="west" x=x-1 move(pos,2) end  --west
  214.     while dz>z do print("z",dz,">",z,"  Need to move to 4 (south)") ori="south" z=z+1 move(pos,4) end  --south
  215.     while dz<z do print("z",dz,"<",z,"  Need to move to 3 (north)") ori="north" z=z-1 move(pos,3) end  --north
  216.     reset()
  217. end
  218.  
  219. openRednet()
  220. facing,or1=orientation()
  221. print("Facing " .. facing .. ". which is number " .. or1 ..".")
  222.  
  223. ox,oy,oz=gps.locate(computerId) -- grab current location so we may return here
  224. print("Return location = " .. ox .. " " .. oy .. " " .. oz)
  225.  
  226. fuel()
  227.  
  228. print("Side = " .. side .. ".")
  229. for x=side,1,-1 do
  230.    clear(x)
  231.    turtle.digDown()
  232.    turtle.down()
  233. end
  234.  
  235. for d=8,1,-1 do
  236.    turtle.digDown()
  237.    turtle.down()
  238.    for r=d,1,-1 do
  239.      for t=r*4,1,-1 do
  240.        adv(1,1)
  241.        if t/r == math.floor(t/r) then
  242.           turtle.turnLeft()
  243.        end
  244.      end
  245.    end
  246. end
Add Comment
Please, Sign In to add comment