Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- v. 1.0 : Build a sphere with radius r
- local tArgs = {...}
- if #tArgs < 1 then
- print("Usage : sphere <radius> \n sphere <radius> <wallSlotId>")
- end
- local radius = tonumber(tArgs[1])
- local slot = 0
- if #tArgs > 1 then
- slot = tonumber(tArgs[2])
- end
- local FRONT = 0
- local LEFT = 1
- local BACK = 2
- local RIGHT = 3
- local x = 0
- local y = 0
- local z = -radius
- local direction = 0
- -- Fuel methods
- local function checkFuel()
- local minFuelLevel = math.abs(x) + math.abs(y) + math.abs(z)
- if turtle.getFuelLevel() <= minFuelLevel then
- refuel()
- end
- if turtle.getFuelLevel() <= minFuelLevel then
- waitForFuel()
- end
- end
- local function waitForFuel()
- print("Waiting for fuel")
- while refuel() == false do
- os.sleep(1)
- end
- end
- local function refuel()
- local fueled = false
- for i=1,16 do
- if i ~= slot then
- turtle.select(i)
- fueled = fueled or turtle.refuel()
- end
- end
- return fueled
- end
- -- Inventory methods
- local function checkItems()
- if slot ~= 0 and turtle.getItemCount(slot) < 5 then
- for i=1,16 do
- if i ~= slot then
- turtle.select(i)
- if turtle.compareTo(slot) then
- turtle.transferTo(slot)
- end
- end
- end
- local wallItemMessageShown = false
- while turtle.getItemCount(slot) < 5 do
- os.sleep(1)
- if wallItemMessageShown == false then
- print("Waiting for more wall items")
- wallItemMessageShown = true
- end
- end
- end
- while true do
- local messageShown = false
- for i=1,16 do
- if i ~= slot then
- if turtle.getItemSpace(i) > 5 then
- return
- end
- end
- end
- if messageShown == false then
- print("Please empty some items")
- messageShown = true
- end
- os.sleep(1)
- end
- end
- -- Move method
- local function setDirection(targetDirection)
- local testDirection = direction
- local moveCountLeft = 0
- while testDirection ~= targetDirection do
- testDirection = testDirection + 1
- if testDirection > 3 then
- testDirection = 0
- end
- moveCountLeft = moveCountLeft + 1
- end
- testDirection = direction
- local moveCountRight = 0
- while testDirection ~= targetDirection do
- testDirection = testDirection - 1
- if testDirection < 0 then
- testDirection = 3
- end
- moveCountRight = moveCountRight + 1
- end
- if moveCountLeft < moveCountRight then
- for i=1,moveCountLeft do
- turtle.turnLeft()
- end
- else
- for i=1,moveCountRight do
- turtle.turnRight()
- end
- end
- direction = targetDirection
- end
- local function moveUp()
- checkFuel()
- checkItems()
- turtle.digUp()
- turtle.up()
- end
- local function moveDown()
- checkFuel()
- checkItems()
- turtle.digDown()
- turtle.down()
- end
- local function moveForward()
- checkFuel()
- checkItems()
- while turtle.forward() == false do
- turtle.dig()
- os.sleep(1)
- end
- end
- local function goto(targetX,targetY,targetZ)
- local currentX = x
- local currentY = y
- local currentZ = z
- if currentZ > targetZ then
- while currentZ > targetZ do
- moveDown()
- currentZ = currentZ - 1
- end
- else
- while currentZ < targetZ do
- moveUp()
- currentZ = currentZ + 1
- end
- end
- local doXFirst = math.abs(x) > math.abs(y)
- for i=1,2 do
- if (doXFirst and i == 1) or i == 2 then
- if currentX > targetX then
- setDirection(RIGHT)
- while currentX > targetX do
- moveForward()
- currentX = currentX - 1
- end
- else
- setDirection(LEFT)
- while currentX < targetX do
- moveForward()
- currentX = currentX + 1
- end
- end
- end
- if (doXFirst and i == 2) or i == 1 then
- if currentY > targetY then
- setDirection(BACK)
- while currentY > targetY do
- moveForward()
- currentY = currentY - 1
- end
- else
- setDirection(FRONT)
- while currentY < targetY do
- moveForward()
- currentY = currentY + 1
- end
- end
- end
- end
- x = targetX
- y = targetY
- z = targetZ
- end
- -- Drawing methods
- local function testIsInShape(targetX,targetY,targetZ)
- local localRadius = radius - math.abs(targetZ)
- local targetRadius = math.abs(targetX) + math.abs(targetY)
- return targetRadius <= localRadius
- end
- local function checkWall()
- if slot ~= 0 then
- turtle.select(slot)
- if testIsInShape(x, y + 1, z) == false then
- setDirection(FRONT)
- if turtle.compare(slot) == false then
- turtle.dig()
- turtle.place()
- end
- end
- if testIsInShape(x + 1, y, z) == false then
- setDirection(LEFT)
- if turtle.compare(slot) == false then
- turtle.dig()
- turtle.place()
- end
- end
- if testIsInShape(x, y - 1, z) == false then
- setDirection(BACK)
- if turtle.compare(slot) == false then
- turtle.dig()
- turtle.place()
- end
- end
- if testIsInShape(x - 1, y, z) == false then
- setDirection(RIGHT)
- if turtle.compare(slot) == false then
- turtle.dig()
- turtle.place()
- end
- end
- end
- end
- -- Render
- for i=-radius,radius do
- goto(0,0,i)
- for j=-radius,radius do
- for k=-radius,radius do
- local yValue = k
- if j % 2 == 0 then
- yValue = -k
- end
- if testIsInShape(j,yValue,i) then
- goto(j,yValue,i)
- checkWall()
- end
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement