Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- currentPos = {x = 0, y = 0, z = 0} -- position actuel
- currentRot = 0; -- rotation actuel
- function line(width)
- local fuelLevel = turtle.getFuelLevel()
- if fuelLevel == "unlimited" or fuelLevel >= 100 then
- for i=2,width do
- turtle.dig()
- forward()
- end
- else
- local success, er = turtle.refuel()
- if not success then
- print("Error: could not refuel")
- return
- else
- line(width)
- end
- end
- end
- function square(size)
- if turtle.getFuelLevel() < size * 2 then
- turtle.refuel()
- end
- for i=1,size do
- line(size)
- if i < size then
- if i % 2 == 1 then
- rotation("right")
- turtle.dig()
- forward()
- rotation("right")
- else
- rotation("left")
- turtle.dig()
- forward()
- rotation("left")
- end
- end
- end
- end
- function cube(size, direction)
- for i=1,size do
- square(size)
- gotoPoint({x = 0, y = currentPos.y, z = 0})
- gotoRotation(0)
- if direction == "up" then
- turtle.digUp()
- fly("up")
- else
- turtle.digDown()
- fly("down")
- end
- end
- end
- function forward()
- if currentRot == 0 then
- currentPos.x = currentPos.x + 1
- elseif currentRot == 90 then
- currentPos.z = currentPos.z + 1
- elseif currentRot == 180 then
- currentPos.x = currentPos.x - 1
- elseif currentRot == 270 then
- currentPos.z = currentPos.z - 1
- else
- print("Error rotation is not valid : " .. currentRot .. " x:" .. currentPos.x .. " y:" .. currentPos.y .. " z:" .. currentPos.z)
- return
- end
- turtle.forward()
- end
- function fly(direction)
- if direction == "up" then
- currentPos.y = currentPos.y + 1
- turtle.up()
- elseif direction == "down" then
- currentPos.y = currentPos.y - 1
- turtle.down()
- else
- print("Error fly to " .. direction .. " y:" .. currentPos.y)
- return
- end
- end
- function rotation(direction)
- if (direction == "left") then
- currentRot = currentRot + 90
- if currentRot > 270 then
- currentRot = 0
- end
- turtle.turnLeft()
- elseif (direction == "right") then
- currentRot = currentRot - 90
- if currentRot < 0 then
- currentRot = 270
- end
- turtle.turnRight()
- else
- print("Error rotate to " .. direction .. " rotation:" .. currentRot)
- return
- end
- end
- function gotoRotation(angle)
- local time = 0
- while currentRot ~= angle and time < 3000 do
- print("rotation:" .. currentRot .. " time:" .. time)
- rotation("left")
- time = time + 1
- end
- end
- function gotoPoint(point)
- local time = 0
- while currentPos.y ~= point.y and time < 3000 do
- print("positionY:" .. currentPos.y .. " time:" .. time)
- if currentPos.y > point.y then
- fly("down")
- elseif currentPos.y < point.y then
- fly("up")
- end
- end
- time = 0
- while currentPos.x ~= point.x and time < 3000 do
- print("positionX:" .. currentPos.x .. " time:" .. time)
- if currentPos.x > point.x then
- gotoRotation(180)
- forward()
- elseif currentPos.x < point.x then
- gotoRotation(0)
- forward()
- end
- end
- time = 0
- while currentPos.z ~= point.z and time < 3000 do
- print("positionZ:" .. currentPos.z .. " time:" .. time)
- if currentPos.z > point.z then
- gotoRotation(270)
- forward()
- elseif currentPos.z < point.z then
- gotoRotation(90)
- forward()
- end
- end
- end
- -- Vérifier si un argument est fourni
- if #arg < 1 then
- print("Usage:")
- print(" turtle <command> [arguments]")
- return
- end
- local command = arg[1]
- -- Vérifier si le premier argument est valide
- if command ~= "line" and command ~= "square" and command ~= "cube" then
- print("Invalid command.")
- return
- end
- -- Vérifier si les arguments nécessaires sont fournis pour la commande 'line'
- if command == "line" and (#arg < 2 or not tonumber(arg[2])) then
- print("Usage:")
- print(" turtle line <length>")
- return
- end
- -- Vérifier si les arguments nécessaires sont fournis pour la commande 'square' ou 'cube'
- if (command == "square" or command == "cube") and (#arg < 2 or not tonumber(arg[2])) then
- print("Usage:")
- print(" turtle square <size>")
- print(" turtle cube <size>")
- return
- end
- -- Vérifier si un argument facultatif est fourni pour la commande 'cube'
- if command == "cube" and #arg >= 3 and arg[3] ~= "up" and arg[3] ~= "down" then
- print("Usage:")
- print(" turtle cube <size> [direction]")
- print("Direction can be 'up' or 'down'.")
- return
- end
- -- Exécuter la commande appropriée avec les arguments fournis
- if command == "line" then -- LINE
- local length = tonumber(arg[2])
- line(length)
- gotoPoint({x = 0, y = 0, z = 0})
- gotoRotation(0)
- elseif command == "square" then -- SQUARE
- local size = tonumber(arg[2])
- square(size)
- gotoPoint({x = 0, y = 0, z = 0})
- gotoRotation(0)
- elseif command == "cube" then -- CUBE
- local size = tonumber(arg[2])
- local direction = arg[3] or "down"
- cube(size, direction)
- gotoPoint({x = 0, y = 0, z = 0})
- gotoRotation(0)
- end
Advertisement
Add Comment
Please, Sign In to add comment