Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local args = {...}
- for k,v in ipairs(redstone.getSides()) do
- redstone.setOutput(v,false)
- end
- local tX,tY,tZ = tonumber(args[1]),tonumber(args[2]),tonumber(args[3])
- local LOAD_SIDE = "back"
- local FIRE_SIDE = "front"
- local PITCH_UP_SIDE = "left"
- local PITCH_DOWN_SIDE = "right"
- local YAW_SIDE = "top"
- local ROTATION_ERROR = 0.125
- local blockReader = assert(peripheral.find("blockReader"))
- local rotationController = assert(peripheral.find("Create_RotationSpeedController"))
- local blib = require("blib")
- local expect = require("cc.expect").expect
- local pos = require("cannonData")
- local x,y,z = pos[1],pos[2],pos[3]
- local nCannonLength = pos[4]
- local nPowderCharges = pos[5]
- expect(1,tX,"number")
- expect(2,tY,"number")
- expect(3,tZ,"number")
- --Convert target x,y,z into valid targets
- local T_X,T_Z = tX-x,tZ-z
- local T_Y = tY - (y+2) - 600 - 128
- local T_W = math.sqrt(T_X^2+T_Z^2)
- local _,pitchInfo = assert(blib.getAngle(T_W,T_Y,nCannonLength,nPowderCharges))
- local PITCH = math.deg(pitchInfo.pitch)
- assert(PITCH >= -30 and PITCH < 60,"pitch "..PITCH.." is out of range of the cannon!")
- local YAW = math.deg(math.atan2(-T_X,-T_Z))%360
- local function doWhile(f,predicate)
- while predicate() do
- f()
- end
- end
- --Start the cannon setup
- sleep(0.05)
- if blockReader.getBlockData().Running == 1 then error("Cannon is already running!") end
- print("loading cannon")
- redstone.setOutput(LOAD_SIDE,true)
- sleep(0.5)
- redstone.setOutput(LOAD_SIDE,false)
- sleep(10)
- print("Adjusting yaw to",YAW)
- rotationController.setTargetSpeed(10)
- doWhile(function()
- local yaw = blockReader.getBlockData().CannonYaw;
- rotationController.setTargetSpeed(math.max(1,math.min(32,math.floor(math.abs(YAW-yaw)/3))))
- redstone.setOutput(YAW_SIDE,true);
- sleep(0.05) end,
- function() local yaw = blockReader.getBlockData().CannonYaw; return math.abs(yaw-YAW) > ROTATION_ERROR end)
- redstone.setOutput(YAW_SIDE,false)
- print("Adjusting pitch to",PITCH)
- rotationController.setTargetSpeed(1)
- while true do
- local pitch = blockReader.getBlockData().CannonPitch
- rotationController.setTargetSpeed(math.max(1,math.min(32,math.floor(math.abs(PITCH-pitch)/3))))
- if pitch > PITCH + ROTATION_ERROR then
- redstone.setOutput(PITCH_UP_SIDE,false)
- sleep(0.05)
- redstone.setOutput(PITCH_DOWN_SIDE,true)
- elseif pitch < PITCH - ROTATION_ERROR then
- redstone.setOutput(PITCH_DOWN_SIDE,false)
- sleep(0.05)
- redstone.setOutput(PITCH_UP_SIDE,true)
- else
- redstone.setOutput(PITCH_DOWN_SIDE,false)
- redstone.setOutput(PITCH_UP_SIDE,false)
- break
- end
- sleep(0.05)
- end
- print("Awaiting key input to fire")
- os.pullEvent("key")
- redstone.setOutput(FIRE_SIDE,true)
- sleep(0.5)
- redstone.setOutput(FIRE_SIDE,false)
- print("All done!")
Advertisement
Add Comment
Please, Sign In to add comment