Untitled

 avatar
unknown
lua
22 days ago
1.3 kB
2
Indexable
--function to get yaw or pitch depending on int choice of 1/2
function getYawPitch(choice)
    redstone.setOutput("front",true)
    sleep(0.05)
    redstone.setOutput("front",false)
    sleep(0.2)
    local rawPitch = peripheral.call("right","getLine",choice)
    local Pitch = string.match(rawPitch, "(.-)º")
    return tonumber(Pitch)
end

function sign(x)
    return x>=0 and 1 or x<0 and -1
end

local params = {...}
if #params<2 then 
    print("Not enough parameters")
    exit()
end
local targetYaw = params[1]
local targetPitch = params[2]
local currentYaw = getYawPitch(1)
local currentPitch = getYawPitch(2)
print("Current Yaw: ",currentYaw," Target Yaw: ",targetYaw," Current Pitch: ",currentPitch," Target Pitch: ",targetPitch)
local yawDegreesNeeded = (targetYaw - currentYaw)*8
local yawDifference = targetYaw - currentYaw
local yawDegreesNeeded = (math.abs(yawDifference) <= 180) and yawDifference*8 or (yawDifference - 360 * sign(yawDifference))
local pitchDegreesNeeded = (targetPitch - currentPitch)*8
peripheral.call("bottom","rotate",yawDegreesNeeded,sign(yawDegreesNeeded))
peripheral.call("top","rotate",pitchDegreesNeeded,sign(pitchDegreesNeeded))
sleep((math.max(math.abs(yawDegreesNeeded), math.abs(pitchDegreesNeeded))/480)+0.5)