Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- 2015 (c) psychedelixx
- Minecraft Turtle: Circle
- 2015-01-08
- Builds a circle
- Robust API:
- http://computercraft.info/wiki/Robust_Turtle_API
- Usage:
- - use turtle and type "label set <name>"
- (to give your turtle an unique name so it remembers its programs)
- - type "pastebin get wgFfCNUq circle"
- - place blocks in turtle inventory
- - type "circle <radius>"
- ]]
- -------------------------------------------------------------------------------------
- -- INIT -----------------------------------------------------------------------------
- -------------------------------------------------------------------------------------
- slot = 1
- posX = 0
- posY = 0
- posH = 0
- height = 1
- debug = 0
- x = {}
- y = {}
- local args = { ... }
- if #args < 1 or #args > 3 then
- print("")
- print("circle <radius> [<height>] [<debug {0|1}>]")
- print("place items in inventory")
- print("")
- error()
- end
- radius = tonumber(args[1])
- if radius > 100 or radius < 1 then
- print("")
- print("Are you mad? I won't do this...")
- print("")
- error()
- end
- if #args == 2 then
- height = tonumber(args[2])
- end
- if #args == 3 then
- debug = tonumber(args[2])
- end
- -------------------------------------------------------------------------------------
- -- ROUND ----------------------------------------------------------------------------
- -------------------------------------------------------------------------------------
- function round(num, idp)
- local mult = 10^(idp or 0)
- return math.floor(num * mult + 0.5) / mult
- end
- -------------------------------------------------------------------------------------
- -- CALC CIRCLE ----------------------------------------------------------------------
- -------------------------------------------------------------------------------------
- function calcCircle()
- d_ = -radius
- x_ = radius
- y_ = 0
- -- 1/8 -------------------
- while y_ <= x_ do
- table.insert(x, x_)
- table.insert(y, y_)
- d_ = d_ + 2 * x_ * y_ + 1;
- y_ = y_ + 1;
- if d_ > 0 then
- d_ = d_ - 2 * x_ * x_ + 2;
- x_ = x_ - 1;
- end
- end
- length = table.getn(x)
- -- 2/8 -------------------
- for i = length, 1, -1 do
- table.insert(x, y[i])
- table.insert(y, x[i])
- end
- -- 3/8 -------------------
- for i = 1, length, 1 do
- table.insert(x, -y[i])
- table.insert(y, x[i])
- end
- -- 4/8 -------------------
- for i = length, 1, -1 do
- table.insert(x, -x[i])
- table.insert(y, y[i])
- end
- -- 5/8 -------------------
- for i = 1, length, 1 do
- table.insert(x, -x[i])
- table.insert(y, -y[i])
- end
- -- 6/8 -------------------
- for i = length, 1, -1 do
- table.insert(x, -y[i])
- table.insert(y, -x[i])
- end
- -- 7/8 -------------------
- for i = 1, length, 1 do
- table.insert(x, y[i])
- table.insert(y, -x[i])
- end
- -- 8/8 -------------------
- for i = length, 1, -1 do
- table.insert(x, x[i])
- table.insert(y, -y[i])
- end
- end
- -------------------------------------------------------------------------------------
- -- CALC RESOURCES -------------------------------------------------------------------
- -------------------------------------------------------------------------------------
- function calcResources()
- count = 0
- for i=2, table.getn(x) do
- if not (x[i] == x[i-1] and y[i] == y[i-1]) then
- count = count + 1
- end
- end
- return count
- end
- -------------------------------------------------------------------------------------
- -- CHECK RESOURCES ------------------------------------------------------------------
- -------------------------------------------------------------------------------------
- function checkResources()
- repeat
- overwrite = 0
- sumItems = 0
- for i=1, 16, 1 do
- sumItems = sumItems + turtle.getItemCount(i)
- end
- if sumItems < calcResources() then
- print("Not enough Resources for this layer")
- print("I need " .. (calcResources() - sumItems) .. " more blocks (" .. calcResources() .. " total)")
- print()
- print("Fill up and press enter")
- print("or type in '1' to start anyway")
- overwrite = read()
- end
- if overwrite == '1' then
- break
- end
- until sumItems >= calcResources()
- end
- -------------------------------------------------------------------------------------
- -- PLACE ----------------------------------------------------------------------------
- -------------------------------------------------------------------------------------
- function place()
- if posX == x[i] and posY == y[i] and not(x[i] == x[i-1] and y[i] == y[i-1]) then
- if debug == 1 then
- print(posH .. ' # ' .. i .. ' # ' .. x[i] .. ' # ' .. y[i] .. ' # ' .. x[i+1] .. ' # ' .. y[i+1] .. ' # ' .. posX .. ' # ' .. posY)
- else
- print(round(((posH*table.getn(x) + i)/table.getn(x)-1)/height*100, 1) .. " %")
- end
- while turtle.getItemCount(slot) == 0 and slot < 16 do
- slot = slot + 1
- end
- if turtle.getItemCount(slot) == 0 and slot == 16 then
- print('Please put items into slot 1 and press enter')
- read()
- slot = 1
- end
- turtle.select(slot)
- turtle.digDown()
- turtle.placeDown()
- end
- end
- -------------------------------------------------------------------------------------
- -- MOVE -----------------------------------------------------------------------------
- -------------------------------------------------------------------------------------
- function move(var, op, turn)
- if turn == 'lr' then
- t.left()
- t.forward()
- t.right()
- elseif turn == 'rl' then
- t.right()
- t.forward()
- t.left()
- elseif turn == 'no' then
- t.forward()
- end
- if op == 'inc' then
- var = var + 1
- elseif op == 'dec' then
- var = var - 1
- end
- return var
- end
- -------------------------------------------------------------------------------------
- -- MAIN -----------------------------------------------------------------------------
- -------------------------------------------------------------------------------------
- calcCircle()
- if turtle.getFuelLevel() < calcResources()*height then
- print("I need " .. math.ceil(calcResources()*height - turtle.getFuelLevel()) .. " more fuel!")
- else
- print("======== 2015 (c) psychedelixx ========")
- print("Let's go!")
- print("Building a circle...")
- print("")
- checkResources()
- t.forward(radius)
- posX = posX + radius
- t.left()
- while posH < height do
- if posH > 0 then
- checkResources()
- end
- posH = posH + 1
- t.up()
- i = 1
- print("Building layer " .. posH .. " of " .. height)
- -- 1/8 --------------------------------------------
- while y[i] < x[i] do
- place()
- if y[i] < y[i+1] then
- posY = move(posY, 'inc', 'no')
- end
- if x[i] > x[i+1] then
- posX = move(posX, 'dec', 'lr')
- end
- i = i+1
- end
- t.left()
- -- 2/8 --------------------------------------------
- while x[i] > 0 do
- place()
- if y[i] < y[i+1] then
- posY = move(posY, 'inc', 'rl')
- end
- if x[i] > x[i+1] then
- posX = move(posX, 'dec', 'no')
- end
- i = i+1
- end
- -- 3/8 --------------------------------------------
- while math.abs(x[i]) < y[i] do
- place()
- if y[i] > y[i+1] then
- posY = move(posY, 'dec', 'lr')
- end
- if x[i] > x[i+1] then
- posX = move(posX, 'dec', 'no')
- end
- i = i+1
- end
- t.left()
- -- 4/8 --------------------------------------------
- while y[i] > 0 do
- place()
- if y[i] > y[i+1] then
- posY = move(posY, 'dec', 'no')
- end
- if x[i] > x[i+1] then
- posX = move(posX, 'dec', 'rl')
- end
- i = i+1
- end
- -- 5/8 --------------------------------------------
- while y[i] < x[i] do
- place()
- if y[i] > y[i+1] then
- posY = move(posY, 'dec', 'no')
- end
- if x[i] < x[i+1] then
- posX = move(posX, 'inc', 'lr')
- end
- i = i+1
- end
- t.left()
- -- 6/8 --------------------------------------------
- while x[i] < 0 do
- place()
- if y[i] > y[i+1] then
- posY = move(posY, 'dec', 'rl')
- end
- if x[i] < x[i+1] then
- posX = move(posX, 'inc', 'no')
- end
- i = i+1
- end
- -- 7/8 --------------------------------------------
- while math.abs(y[i]) > x[i] do
- place()
- if y[i] < y[i+1] then
- posY = move(posY, 'inc', 'lr')
- end
- if x[i] < x[i+1] then
- posX = move(posX, 'inc', 'no')
- end
- i = i+1
- end
- t.left()
- -- 8/8 --------------------------------------------
- while y[i] < 0 do
- place()
- if y[i] < y[i+1] then
- posY = move(posY, 'inc', 'no')
- end
- if x[i] < x[i+1] then
- posX = move(posX, 'inc', 'rl')
- end
- i = i+1
- end
- end
- print("Finished")
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement