Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local dturtle = {}
- function dturtle.gets_lengths (height, width, reversed)
- reversed = reversed or false
- h = height
- w = width - 1
- lengths = {}
- while true do
- if h == 0 then
- return lengths
- end
- if h == height and reversed ~= true then
- lengths[#lengths + 1] = h - 1
- else
- lengths[#lengths + 1] = h
- end
- h = h - 1
- if w == 0 then
- return lengths
- end
- lengths[#lengths + 1] = w
- w = w - 1
- end
- end
- function dturtle.gets_lengths_reversed (height, width)
- lengths = dturtle.gets_lengths(height, width, true)
- reversed_lengths = {}
- counter = 1
- for i = #lengths,1,-1 do
- if i ~= 1 then
- reversed_lengths[counter] = lengths[i]
- else
- reversed_lengths[counter] = lengths[i] - 1
- end
- counter = counter + 1
- end
- return reversed_lengths
- end
- function dturtle.gets_face_direction (h, w)
- if h >= w then
- if w % 2 ~= 0 then
- return 1
- else
- return 3
- end
- else
- if h % 2 ~= 0 then
- return 2
- else
- return 4
- end
- end
- end
- function dturtle.gets_position (h, w)
- if w > h then
- w = h
- end
- if w % 2 == 0 then
- return math.floor(w / 2), math.floor((w / 2) - 1)
- else
- return math.floor((w - 1) / 2), h - math.floor((w - 1) / 2)
- end
- end
- function dturtle.dig_spiral (turn, lengths)
- for l = 1, #lengths do
- for p = 1, lengths[l] do
- turtle.dig()
- turtle.forward()
- end
- turn()
- end
- end
- function dturtle.exit_from_center (height, width)
- x_pos, y_pos = dturtle.gets_position(height, width)
- direction = dturtle.gets_face_direction(height, width)
- if direction == 2 then
- turtle.turnLeft()
- elseif direction == 3 then
- turtle.turnLeft()
- turtle.turnLeft()
- elseif direction == 4 then
- turtle.turnRight()
- end
- for i = 1, y_pos do
- turtle.back()
- end
- turtle.turnLeft()
- for i = 1, x_pos do
- turtle.forward()
- end
- turtle.turnRight()
- end
- function dturtle.dig_area (height, width, depth)
- local state = 1
- turtle.dig()
- turtle.forward()
- for i = 1, depth do
- if i % 2 == 0 then
- lengths = dturtle.gets_lengths_reversed(height, width)
- turtle.turnRight()
- turtle.turnRight()
- dturtle.dig_spiral(turtle.turnLeft, lengths)
- turtle.turnRight()
- else
- lengths = dturtle.gets_lengths(height, width)
- if i ~= 1 then
- turtle.turnRight()
- turtle.turnRight()
- end
- dturtle.dig_spiral(turtle.turnRight, lengths)
- turtle.turnLeft()
- end
- if i ~= depth then
- turtle.digDown()
- turtle.down()
- end
- end
- if depth % 2 ~= 0 then
- dturtle.exit_from_center(height, width, depth)
- end
- if depth ~= 1 then
- for i = 1, depth - 1 do
- turtle.up()
- end
- end
- direction = dturtle.gets_face_direction(height, width)
- if direction == 3 then
- turtle.turnRight()
- turtle.turnRight()
- end
- turtle.back()
- end
- return dturtle
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement