Advertisement
Guest User

Untitled

a guest
Sep 21st, 2014
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.15 KB | None | 0 0
  1. tArgs = {...}
  2. if tArgs[3] == nil then print("petal num inner_rad outer_rad") return end
  3. np = tonumber(tArgs[1])
  4. irad = tonumber(tArgs[2])
  5. orad = tonumber(tArgs[3])
  6. mode = "build"
  7. if tArgs[4] ~= nil then mode = tArgs[4] end
  8.  
  9. function place()
  10.   if turtle.getItemCount(1) > 0 then turtle.placeDown() return end
  11.   i = 1
  12.   while i < 16 do
  13.     if turtle.getItemCount(i) > 0 then break end
  14.     i = i + 1
  15.   end
  16.   if i < 16 then
  17.     turtle.select(i)
  18.     turtle.transferTo(1,turtle.getItemCount(i))
  19.     turtle.select(1)
  20.     turtle.placeDown()
  21.     return
  22.   end
  23.   print("Out of blocks!  Getting more...")
  24.   turtle.select(16)
  25.   while not turtle.placeUp() do sleep(1) end
  26.   turtle.select(1)
  27.   while not turtle.suckUp() do sleep(1) end
  28.   i = 1
  29.   while i < 15 do
  30.     turtle.suckUp()
  31.     i = i + 1
  32.   end
  33.   turtle.select(16)
  34.   while not turtle.digUp() do sleep(1) end
  35.   turtle.select(1)
  36.   turtle.placeDown()
  37. end
  38.  
  39. function getpos()
  40.   sleep(0)
  41.   x, y, z = gps.locate()
  42.   while not x do
  43.     print("Whoa, I'm lost!")
  44.     print("Couldn't find GPS position")
  45.     shell.run("lua")
  46.          x, y, z = gps.locate()
  47.   end
  48.   print("("..x..","..y..","..z..")")
  49. end
  50.  
  51. function tr(n)
  52.   while n > 0 do
  53.     turtle.turnRight()
  54.     n = n - 1
  55.   end
  56. end
  57.  
  58. function tl(n)
  59.   while n > 0 do
  60.     turtle.turnLeft()
  61.     n = n - 1
  62.   end
  63. end
  64.  
  65. tf = turtle.forward
  66. tb = turtle.back
  67. tu = turtle.up
  68. td = turtle.down
  69.  
  70. dir = nil
  71. function getface()
  72.   print("Trying to get current direction")
  73.   getpos()
  74.   lx = x
  75.   lz = z
  76.   while not tf() do sleep(1) end
  77.   getpos()
  78.   if x > lx then dir = "+x" end
  79.   if x < lx then dir = "-x" end
  80.   if z > lz then dir = "+z" end
  81.   if z < lz then dir = "-z" end
  82.   while not tb() do sleep(1) end
  83.   print("I am facing "..dir)
  84. end
  85.  
  86. function setstart()
  87.         getpos()
  88.         startx = x
  89.         starty = y
  90.         startz = z
  91.         startface = getface()
  92. end
  93. setstart()
  94.  
  95. function face(ndir)
  96.   while face == nil do getface() end
  97.   if (dir == ndir) then return end
  98.   if (ndir == "+x") then
  99.     if (dir == "-x") then tr(2) end
  100.     if (dir == "-z") then tr(1) end
  101.     if (dir == "+z") then tl(1) end
  102.   end
  103.   if (ndir == "-x") then
  104.     if (dir == "+x") then tr(2) end
  105.     if (dir == "-z") then tl(1) end
  106.     if (dir == "+z") then tr(1) end
  107.   end
  108.   if (ndir == "+z") then
  109.     if (dir == "-z") then tr(2) end
  110.     if (dir == "+x") then tr(1) end
  111.     if (dir == "-x") then tl(1) end
  112.   end
  113.   if (ndir == "-z") then
  114.     if (dir == "+z") then tr(2) end
  115.     if (dir == "+x") then tl(1) end
  116.     if (dir == "-x") then tr(1) end
  117.   end
  118.   dir = ndir
  119. end
  120.  
  121. function old_goto(tx, ty, tz)
  122.   while y < ty do turtle.up() getpos() end
  123.   while y > ty do turtle.down() getpos() end
  124.   while x > tx do face("-x") tf() getpos() end
  125.   while x < tx do face("+x") tf() getpos() end
  126.   while z > tz do face("-z") tf() getpos() end
  127.   while z < tz do face("+z") tf() getpos() end
  128. end
  129.  
  130. function goto(tx, ty, tz)
  131.   sx = false
  132.   syu = false
  133.   syd = false
  134.   sz = false
  135.   while ((tx ~= x)or(ty ~= y)or(tz ~= z)) do
  136.     if x < tx then
  137.       face("+x")
  138.       while (x < tx) do
  139.         sx = not tf()
  140.         getpos()
  141.         if sx then break end
  142.         sx = false
  143.         sz = false
  144.         syu = false
  145.         syd = false
  146.       end
  147.     end
  148.     if x > tx then
  149.       face("-x")
  150.       while (x > tx) do
  151.         sx = not tf()
  152.         getpos()
  153.         if sx then break end
  154.         sz = false
  155.         sz = false
  156.         syu = false
  157.         syd = false
  158.       end
  159.     end
  160.     if z < tz then
  161.       face("+z")
  162.       while z < tz do
  163.         sz = not tf()
  164.         getpos()
  165.         if sz then break end
  166.         sx = false
  167.         sz = false
  168.         syu = false
  169.         syd = false
  170.       end
  171.     end
  172.     if z > tz then
  173.       face("-z")
  174.       while z > tz do
  175.         sz = not tf()
  176.         getpos()
  177.         if sz then break end
  178.         sx = false
  179.         sz = false
  180.         syu = false
  181.         syd = false
  182.       end
  183.     end
  184.     if (sx or sz) then
  185.       if not syu then
  186.         syu = not tu()
  187.         getpos()
  188.         if not syu then
  189.           sx = false
  190.           sz = false
  191.         end
  192.       end
  193.       if syu and not syd then
  194.         syd = not td()
  195.         getpos()
  196.         if not syd then
  197.           sx = false
  198.           sz = false
  199.         end
  200.       end
  201.       if syu and syd then
  202.         print("Whoa, I'm stuck!")
  203.         getpos()
  204.         print("I'm at ("..x..", "..y..", "..z..")")
  205.         print("Going to ("..x..", "..y..", "..z..")")
  206.         shell.run("lua")
  207.         getpos()
  208.       end
  209.     end
  210.     if (x == tx)and(z == tz) then
  211.       while y < ty do tu() getpos() end
  212.       while y > ty do td() getpos() end
  213.     end
  214.   end
  215. end
  216.  
  217. function getdpos()
  218.   getpos()
  219.   dx = startx - dx
  220.   dy = starty - dy
  221.   dz = startz - dz
  222. end
  223.  
  224. function gotod(gx,gy,gz) goto(gx+startx,gy+starty,gz+startz) end
  225. function round(rrn) return math.floor(rrn+0.5) end
  226. function roundall(rrx, rry, rrz) return round(rrx),round(rry),round(rrz) end
  227.  
  228. function sign(sn)
  229.         if sn < 0 then return -1 end
  230.         return 1
  231. end
  232.  
  233. function empty_map(minX,maxX,minY,maxY,minZ,maxZ)
  234.   print("Allocating map space: ")
  235.   print("  X: "..minX..", "..maxX)
  236.   print("  Y: "..minY..", "..maxY)
  237.   print("  Z: "..minZ..", "..maxZ)
  238.   emap = {}
  239.   for ix = minX, maxX do
  240.     emap[ix] = {}
  241.     for iy = minY, maxY do
  242.       emap[ix][iy] = {}
  243.       for iz = minZ, maxZ do
  244.         emap[ix][iy][iz] = false
  245.       end
  246.     end
  247.       print("Allocating map... "..((ix - minX)/(maxX-minX)*100).."%")
  248.   end
  249.   print("Done.")
  250.   return emap
  251. end
  252.  
  253. function define_build_area()
  254.   tminx = - irad - orad - 2
  255.   tmaxx = irad + orad + 2
  256.   tminy = 0
  257.   tmaxy = round(orad*math.sqrt(2) + 2)
  258.   tminz = - irad - orad - 2
  259.   tmaxz = irad + orad + 2
  260.   bmap = empty_map(tminx,tmaxx,tminy,tmaxy,tminz,tmaxz)
  261.  
  262.   tmin = 0
  263.   tmax = 2*math.pi
  264.   tstep = math.pi/((irad+orad)*4*np)
  265.  
  266.   tvar = tmin
  267.   lpct = -1
  268.   while tvar <= tmax do
  269.     varrad = orad*math.cos(tvar*np/2) + irad*sign(math.cos(tvar*np/2))
  270.     varx = varrad*math.cos(tvar)
  271.     vary = orad*(1-math.cos((math.abs(varrad) - irad)/orad))
  272.     varz = varrad*math.sin(tvar)
  273. --print("Going to build at "..round(varx)..", "..round(vary)..", "..round(varz))
  274. --print("("..tvar..", "..varrad..")")
  275.     bmap[round(varx)][round(vary)][round(varz)] = true
  276.     pct = ((tvar-tmin)/(tmax-tmin)*100)
  277.     if pct > (lpct + 1) then
  278.       print("Building map... "..pct.."%")
  279.         lpct = pct
  280.     end
  281.     tvar = tvar + tstep
  282.   end
  283. --go through x,z
  284.   for varz = tminz, tmaxz do
  285.     fill = false
  286.     for varx = tminx, tmaxx do
  287.       varrad = math.sqrt((varx*varx)+(varz*varz))
  288.       for vary = tminy, tmaxy do
  289. --do nothing if rad < (irad + np)
  290.         if (varrad < (irad + np)) then break end
  291. --when boundary encountered, start fill until boundary encountered
  292.         if (bmap[varx][vary][varz]) then
  293.           --change mode
  294.           fill = not fill
  295.           skip = true
  296.           --skip to edge of boundary
  297.           for vvx = (varx+1),tmaxx do
  298.             if not skip then break end
  299.             for vvy = tminy,tmaxy do
  300.               if (bmap[vvx][vvy][varz]) then break end
  301.               if (vvy == tmaxy) then skip = false end
  302.             end
  303.           end
  304.           varx = (vvx - 1)
  305.           break
  306.         else
  307. --if we scanned all y and don't need to mode switch, and it's fill mode, do the fill
  308.           if (vary == tmaxy) and (fill) then
  309.             bmap[varx][round(orad*(1-math.cos((math.abs(varrad) - irad)/orad)))][varz] = true
  310.             break
  311.           end
  312.         end
  313.       end
  314. --fill progress
  315.       pct = ((varz-tminz)/(tmaxz-tminz) + (varx-tminx)/((tmaxx-tminx)*(tmaxz-tminz)))
  316.       if pct > (lpct + 1) then
  317.         print("Filling map... "..pct.."%")
  318.           lpct = pct
  319.       end
  320.     end
  321.   end
  322.   print("Done.")
  323.   return bmap
  324. end
  325.  
  326. function build_area(bamap)
  327.   for iz = tminz, tmaxz do
  328.     for ix = tminx, tmaxx do
  329.       for iy = tminy, tmaxy do
  330.         if bamap[ix][iy][iz] then
  331.           gotod(ix,iy,iz)
  332.           place()
  333.         end
  334.       end
  335.     end
  336.   end
  337.   gotod(0,0,0)
  338. end
  339.  
  340. if mode == "build" then
  341.   build_area(define_build_area())
  342. end
  343. if mode == "test" then
  344.   bmap = define_build_area()
  345. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement