Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- print("please enter radius")
- radius = tonumber(read())
- -- Record Keeping Variables: These are for recoding the blocks and fuel used
- local blocks = 0
- local fuel = 0
- local facing = 0
- function checkFuel()
- if (not(tonumber(turtle.getFuelLevel()) == nil)) then
- while turtle.getFuelLevel() < 50 do
- writeOut("Turtle almost out of fuel, pausing. Please drop fuel in inventory. And press enter.")
- io.read()
- turtle.refuel()
- end
- end
- end
- function simulationCheck() -- checks state of the simulation
- if sim_mode then
- if compareProgress() then
- setSimFlags(false) -- If we're caught up, un-set flags
- else
- setSimFlags(true) -- If not caught up, just re-affirm that the flags are set
- end
- end
- end
- function progressUpdate() -- This ONLY updates the local table variable. Writing is handled above. -- I want to change this to allow for any number of params
- progTable = {shape = choice, enderchest_refilling = tempProgTable.enderchest_refilling, param1 = tempProgTable.param1, param2 = tempProgTable.param2, param3 = tempProgTable.param3, param4 = tempProgTable.param4, x = positionX, y = positionY, z = positionZ, facing = facing, blocks = blocks}
- if not sim_mode then
- writeProgress()
- end
- end
- function placeBlock()
- blocks = blocks + 1
- simulationCheck()
- if cost_only then
- return
- end
- if turtle.detectDown() and not turtle.compareDown() then
- turtle.digDown()
- end
- checkResources()
- turtle.placeDown()
- progressUpdate()
- end
- function turnRightTrack()
- simulationCheck()
- facing = facing + 1
- if facing >= 4 then
- facing = 0
- end
- progressUpdate()
- if cost_only then
- return
- end
- turtle.turnRight()
- end
- function turnLeftTrack()
- simulationCheck()
- facing = facing - 1
- if facing < 0 then
- facing = 3
- end
- progressUpdate()
- if cost_only then
- return
- end
- turtle.turnLeft()
- end
- function turnAroundTrack()
- turnLeftTrack()
- turnLeftTrack()
- end
- function safeForward()
- simulationCheck()
- if facing == 0 then
- positionY = positionY + 1
- elseif facing == 1 then
- positionX = positionX + 1
- elseif facing == 2 then
- positionY = positionY - 1
- elseif facing == 3 then
- positionX = positionX - 1
- end
- fuel = fuel + 1
- progressUpdate()
- if cost_only then
- return
- end
- checkFuel()
- local success = false
- local tries = 0
- while not success do
- success = turtle.forward()
- if not success then
- while (not success) and tries < 6 do
- tries = tries + 1
- turtle.dig()
- success = turtle.forward()
- sleep(0.3)
- end
- if not success then
- writeOut("Blocked attempting to move forward.")
- writeOut("Please clear and press enter to continue.")
- io.read()
- end
- end
- end
- end
- function safeBack()
- simulationCheck()
- if facing == 0 then
- positionY = positionY - 1
- elseif facing == 1 then
- positionX = positionX - 1
- elseif facing == 2 then
- positionY = positionY + 1
- elseif facing == 3 then
- positionX = positionX + 1
- end
- fuel = fuel + 1
- progressUpdate()
- if cost_only then
- return
- end
- checkFuel()
- local success = false
- local tries = 0
- while not success do
- success = turtle.back()
- if not success then
- turnAroundTrack()
- while turtle.detect() and tries < 6 do
- tries = tries + 1
- if turtle.dig() then
- break
- end
- sleep(0.3)
- end
- turnAroundTrack()
- success = turtle.back()
- if not success then
- writeOut("Blocked attempting to move back.")
- writeOut("Please clear and press enter to continue.")
- io.read()
- end
- end
- end
- end
- function safeUp()
- simulationCheck()
- positionZ = positionZ + 1
- fuel = fuel + 1
- progressUpdate()
- if cost_only then
- return
- end
- checkFuel()
- local success = false
- while not success do
- success = turtle.up()
- if not success then
- while turtle.detectUp() do
- if not turtle.digUp() then
- writeOut("Blocked attempting to move up.")
- writeOut("Please clear and press enter to continue.")
- io.read()
- end
- end
- end
- end
- end
- function safeDown()
- simulationCheck()
- positionZ = positionZ - 1
- fuel = fuel + 1
- progressUpdate()
- if cost_only then
- return
- end
- checkFuel()
- local success = false
- while not success do
- success = turtle.down()
- if not success then
- while turtle.detectDown() do
- if not turtle.digDown() then
- writeOut("Blocked attempting to move down.")
- writeOut("Please clear and press enter to continue.")
- io.read()
- end
- end
- end
- end
- end
- function moveY(targetY)
- if targetY == positionY then
- return
- end
- if (facing ~= 0 and facing ~= 2) then -- Check axis
- turnRightTrack()
- end
- while targetY > positionY do
- if facing == 0 then
- safeForward()
- else
- safeBack()
- end
- end
- while targetY < positionY do
- if facing == 2 then
- safeForward()
- else
- safeBack()
- end
- end
- end
- function moveX(targetX)
- if targetX == positionX then
- return
- end
- if (facing ~= 1 and facing ~= 3) then -- Check axis
- turnRightTrack()
- end
- while targetX > positionX do
- if facing == 1 then
- safeForward()
- else
- safeBack()
- end
- end
- while targetX < positionX do
- if facing == 3 then
- safeForward()
- else
- safeBack()
- end
- end
- end
- function moveZ(targetZ)
- if targetZ == positionZ then
- return
- end
- while targetZ < positionZ do
- safeDown()
- end
- while targetZ > positionZ do
- safeUp()
- end
- end
- function navigateTo(targetX, targetY, targetZ, move_z_first)
- targetZ = targetZ or positionZ -- If targetZ isn't used in the function call, it defaults to its current z position, this should make it compatible with all previous implementations of navigateTo()
- move_z_first = move_z_first or false -- Defaults to moving z last, if true is passed as 4th argument, it moves vertically first
- if move_z_first then
- moveZ(targetZ)
- end
- if facing == 0 or facing == 2 then -- Y axis
- moveY(targetY)
- moveX(targetX)
- else
- moveX(targetX)
- moveY(targetY)
- end
- if not move_z_first then
- moveZ(targetZ)
- end
- end
- function blockInSphereIsFull(offset, x, y, z, radiusSq)
- x = x - offset
- y = y - offset
- z = z - offset
- x = x ^ 2
- y = y ^ 2
- z = z ^ 2
- return x + y + z <= radiusSq
- end
- function isSphereBorder(offset, x, y, z, radiusSq)
- spot = blockInSphereIsFull(offset, x, y, z, radiusSq)
- if spot then
- spot = not blockInSphereIsFull(offset, x, y - 1, z, radiusSq) or
- not blockInSphereIsFull(offset, x, y + 1, z, radiusSq) or
- not blockInSphereIsFull(offset, x - 1, y, z, radiusSq) or
- not blockInSphereIsFull(offset, x + 1, y, z, radiusSq) or
- not blockInSphereIsFull(offset, x, y, z - 1, radiusSq) or
- not blockInSphereIsFull(offset, x, y, z + 1, radiusSq)
- end
- return spot
- end
- function circle(diameter)
- odd = not (math.fmod(diameter, 2) == 0)
- radius = diameter / 2
- if odd then
- width = (2 * math.ceil(radius)) + 1
- offset = math.floor(width/2)
- else
- width = (2 * math.ceil(radius)) + 2
- offset = math.floor(width/2) - 0.5
- end
- --diameter --radius * 2 + 1
- sqrt3 = 3 ^ 0.5
- boundaryRadius = radius + 1.0
- boundary2 = boundaryRadius ^ 2
- radius2 = radius ^ 2
- z = math.floor(radius)
- cz2 = (radius - z) ^ 2
- limitOffsetY = (boundary2 - cz2) ^ 0.5
- maxOffsetY = math.ceil(limitOffsetY)
- -- We do first the +x side, then the -x side to make movement efficient
- for side = 0,1 do
- -- On the right we go from small y to large y, on the left reversed
- -- This makes us travel clockwise (from below) around each layer
- if (side == 0) then
- yStart = math.floor(radius) - maxOffsetY
- yEnd = math.floor(radius) + maxOffsetY
- yStep = 1
- else
- yStart = math.floor(radius) + maxOffsetY
- yEnd = math.floor(radius) - maxOffsetY
- yStep = -1
- end
- for y = yStart,yEnd,yStep do
- cy2 = (radius - y) ^ 2
- remainder2 = (boundary2 - cz2 - cy2)
- if remainder2 >= 0 then
- -- This is the maximum difference in x from the centre we can be without definitely being outside the radius
- maxOffsetX = math.ceil((boundary2 - cz2 - cy2) ^ 0.5)
- -- Only do either the +x or -x side
- if (side == 0) then
- -- +x side
- xStart = math.floor(radius)
- xEnd = math.floor(radius) + maxOffsetX
- else
- -- -x side
- xStart = math.floor(radius) - maxOffsetX
- xEnd = math.floor(radius) - 1
- end
- -- Reverse direction we traverse xs when in -y side
- if y > math.floor(radius) then
- temp = xStart
- xStart = xEnd
- xEnd = temp
- xStep = -1
- else
- xStep = 1
- end
- for x = xStart,xEnd,xStep do
- -- Only blocks within the radius but still within 1 3d-diagonal block of the edge are eligible
- if isSphereBorder(offset, x, y, z, radius2) then
- navigateTo(x, y)
- placeBlock()
- end
- end
- end
- end
- end
- end
- checkFuel()
- circle(radius * 2)
Advertisement
Add Comment
Please, Sign In to add comment