Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- tArgs = {...}
- if tArgs[3] == nil then print("petal num inner_rad outer_rad") return end
- np = tonumber(tArgs[1])
- irad = tonumber(tArgs[2])
- orad = tonumber(tArgs[3])
- mode = "build"
- if tArgs[4] ~= nil then mode = tArgs[4] end
- function place()
- if turtle.getItemCount(1) > 0 then turtle.placeDown() return end
- i = 1
- while i < 16 do
- if turtle.getItemCount(i) > 0 then break end
- i = i + 1
- end
- if i < 16 then
- turtle.select(i)
- turtle.transferTo(1,turtle.getItemCount(i))
- turtle.select(1)
- turtle.placeDown()
- return
- end
- print("Out of blocks! Getting more...")
- turtle.select(16)
- while not turtle.placeUp() do sleep(1) end
- turtle.select(1)
- while not turtle.suckUp() do sleep(1) end
- i = 1
- while i < 15 do
- turtle.suckUp()
- i = i + 1
- end
- turtle.select(16)
- while not turtle.digUp() do sleep(1) end
- turtle.select(1)
- turtle.placeDown()
- end
- function getpos()
- sleep(0)
- x, y, z = gps.locate()
- while not x do
- print("Whoa, I'm lost!")
- print("Couldn't find GPS position")
- shell.run("lua")
- x, y, z = gps.locate()
- end
- print("("..x..","..y..","..z..")")
- end
- function tr(n)
- while n > 0 do
- turtle.turnRight()
- n = n - 1
- end
- end
- function tl(n)
- while n > 0 do
- turtle.turnLeft()
- n = n - 1
- end
- end
- tf = turtle.forward
- tb = turtle.back
- tu = turtle.up
- td = turtle.down
- dir = nil
- function getface()
- print("Trying to get current direction")
- getpos()
- lx = x
- lz = z
- while not tf() do sleep(1) end
- getpos()
- if x > lx then dir = "+x" end
- if x < lx then dir = "-x" end
- if z > lz then dir = "+z" end
- if z < lz then dir = "-z" end
- while not tb() do sleep(1) end
- print("I am facing "..dir)
- end
- function setstart()
- getpos()
- startx = x
- starty = y
- startz = z
- startface = getface()
- end
- setstart()
- function face(ndir)
- while face == nil do getface() end
- if (dir == ndir) then return end
- if (ndir == "+x") then
- if (dir == "-x") then tr(2) end
- if (dir == "-z") then tr(1) end
- if (dir == "+z") then tl(1) end
- end
- if (ndir == "-x") then
- if (dir == "+x") then tr(2) end
- if (dir == "-z") then tl(1) end
- if (dir == "+z") then tr(1) end
- end
- if (ndir == "+z") then
- if (dir == "-z") then tr(2) end
- if (dir == "+x") then tr(1) end
- if (dir == "-x") then tl(1) end
- end
- if (ndir == "-z") then
- if (dir == "+z") then tr(2) end
- if (dir == "+x") then tl(1) end
- if (dir == "-x") then tr(1) end
- end
- dir = ndir
- end
- function old_goto(tx, ty, tz)
- while y < ty do turtle.up() getpos() end
- while y > ty do turtle.down() getpos() end
- while x > tx do face("-x") tf() getpos() end
- while x < tx do face("+x") tf() getpos() end
- while z > tz do face("-z") tf() getpos() end
- while z < tz do face("+z") tf() getpos() end
- end
- function goto(tx, ty, tz)
- sx = false
- syu = false
- syd = false
- sz = false
- while ((tx ~= x)or(ty ~= y)or(tz ~= z)) do
- if x < tx then
- face("+x")
- while (x < tx) do
- sx = not tf()
- getpos()
- if sx then break end
- sx = false
- sz = false
- syu = false
- syd = false
- end
- end
- if x > tx then
- face("-x")
- while (x > tx) do
- sx = not tf()
- getpos()
- if sx then break end
- sz = false
- sz = false
- syu = false
- syd = false
- end
- end
- if z < tz then
- face("+z")
- while z < tz do
- sz = not tf()
- getpos()
- if sz then break end
- sx = false
- sz = false
- syu = false
- syd = false
- end
- end
- if z > tz then
- face("-z")
- while z > tz do
- sz = not tf()
- getpos()
- if sz then break end
- sx = false
- sz = false
- syu = false
- syd = false
- end
- end
- if (sx or sz) then
- if not syu then
- syu = not tu()
- getpos()
- if not syu then
- sx = false
- sz = false
- end
- end
- if syu and not syd then
- syd = not td()
- getpos()
- if not syd then
- sx = false
- sz = false
- end
- end
- if syu and syd then
- print("Whoa, I'm stuck!")
- getpos()
- print("I'm at ("..x..", "..y..", "..z..")")
- print("Going to ("..x..", "..y..", "..z..")")
- shell.run("lua")
- getpos()
- end
- end
- if (x == tx)and(z == tz) then
- while y < ty do tu() getpos() end
- while y > ty do td() getpos() end
- end
- end
- end
- function getdpos()
- getpos()
- dx = startx - dx
- dy = starty - dy
- dz = startz - dz
- end
- function gotod(gx,gy,gz) goto(gx+startx,gy+starty,gz+startz) end
- function round(rrn) return math.floor(rrn+0.5) end
- function roundall(rrx, rry, rrz) return round(rrx),round(rry),round(rrz) end
- function sign(sn)
- if sn < 0 then return -1 end
- return 1
- end
- function empty_map(minX,maxX,minY,maxY,minZ,maxZ)
- print("Allocating map space: ")
- print(" X: "..minX..", "..maxX)
- print(" Y: "..minY..", "..maxY)
- print(" Z: "..minZ..", "..maxZ)
- emap = {}
- for ix = minX, maxX do
- emap[ix] = {}
- for iy = minY, maxY do
- emap[ix][iy] = {}
- for iz = minZ, maxZ do
- emap[ix][iy][iz] = false
- end
- end
- print("Allocating map... "..((ix - minX)/(maxX-minX)*100).."%")
- end
- print("Done.")
- return emap
- end
- function define_build_area()
- tminx = - irad - orad - 2
- tmaxx = irad + orad + 2
- tminy = 0
- tmaxy = round(orad*math.sqrt(2) + 2)
- tminz = - irad - orad - 2
- tmaxz = irad + orad + 2
- bmap = empty_map(tminx,tmaxx,tminy,tmaxy,tminz,tmaxz)
- tmin = 0
- tmax = 2*math.pi
- tstep = math.pi/((irad+orad)*4*np)
- tvar = tmin
- lpct = -1
- while tvar <= tmax do
- varrad = orad*math.cos(tvar*np/2) + irad*sign(math.cos(tvar*np/2))
- varx = varrad*math.cos(tvar)
- vary = orad*(1-math.cos((math.abs(varrad) - irad)/orad))
- varz = varrad*math.sin(tvar)
- --print("Going to build at "..round(varx)..", "..round(vary)..", "..round(varz))
- --print("("..tvar..", "..varrad..")")
- bmap[round(varx)][round(vary)][round(varz)] = true
- pct = ((tvar-tmin)/(tmax-tmin)*100)
- if pct > (lpct + 1) then
- print("Building map... "..pct.."%")
- lpct = pct
- end
- tvar = tvar + tstep
- end
- --go through x,z
- for varz = tminz, tmaxz do
- fill = false
- for varx = tminx, tmaxx do
- varrad = math.sqrt((varx*varx)+(varz*varz))
- for vary = tminy, tmaxy do
- --do nothing if rad < (irad + np)
- if (varrad < (irad + np)) then break end
- --when boundary encountered, start fill until boundary encountered
- if (bmap[varx][vary][varz]) then
- --change mode
- fill = not fill
- skip = true
- --skip to edge of boundary
- for vvx = (varx+1),tmaxx do
- if not skip then break end
- for vvy = tminy,tmaxy do
- if (bmap[vvx][vvy][varz]) then break end
- if (vvy == tmaxy) then skip = false end
- end
- vvvx = vvx
- end
- varx = (vvvx - 1)
- break
- else
- --if we scanned all y and don't need to mode switch, and it's fill mode, do the fill
- if (vary == tmaxy) and (fill) then
- bmap[varx][round(orad*(1-math.cos((math.abs(varrad) - irad)/orad)))][varz] = true
- break
- end
- end
- end
- --fill progress
- pct = ((varz-tminz)/(tmaxz-tminz) + (varx-tminx)/((tmaxx-tminx)*(tmaxz-tminz)))
- if pct > (lpct + 1) then
- print("Filling map... "..pct.."%")
- lpct = pct
- end
- end
- end
- print("Done.")
- return bmap
- end
- function build_area(bamap)
- for iz = tminz, tmaxz do
- for ix = tminx, tmaxx do
- for iy = tminy, tmaxy do
- if bamap[ix][iy][iz] then
- gotod(ix,iy,iz)
- place()
- end
- end
- end
- end
- gotod(0,0,0)
- end
- if mode == "build" then
- build_area(define_build_area())
- end
- if mode == "test" then
- bmap = define_build_area()
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement