t8rt0t

cannonController.lua

Jul 9th, 2024 (edited)
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.82 KB | None | 0 0
  1. local args = {...}
  2. for k,v in ipairs(redstone.getSides()) do
  3.     redstone.setOutput(v,false)
  4. end
  5.  
  6. local tX,tY,tZ = tonumber(args[1]),tonumber(args[2]),tonumber(args[3])
  7.  
  8. local LOAD_SIDE = "back"
  9. local FIRE_SIDE = "front"
  10. local PITCH_UP_SIDE = "left"
  11. local PITCH_DOWN_SIDE = "right"
  12. local YAW_SIDE = "top"
  13.  
  14. local ROTATION_ERROR = 0.125
  15.  
  16. local blockReader = assert(peripheral.find("blockReader"))
  17. local rotationController = assert(peripheral.find("Create_RotationSpeedController"))
  18.  
  19. local blib = require("blib")
  20. local expect = require("cc.expect").expect
  21. local pos = require("cannonData")
  22. local x,y,z = pos[1],pos[2],pos[3]
  23. local nCannonLength = pos[4]
  24. local nPowderCharges = pos[5]
  25.  
  26. expect(1,tX,"number")
  27. expect(2,tY,"number")
  28. expect(3,tZ,"number")
  29.  
  30. --Convert target x,y,z into valid targets
  31. local T_X,T_Z = tX-x,tZ-z
  32. local T_Y = tY - (y+2) - 600 - 128
  33. local T_W = math.sqrt(T_X^2+T_Z^2)
  34.  
  35. local _,pitchInfo = assert(blib.getAngle(T_W,T_Y,nCannonLength,nPowderCharges))
  36. local PITCH = math.deg(pitchInfo.pitch)
  37. assert(PITCH >= -30 and PITCH < 60,"pitch "..PITCH.." is out of range of the cannon!")
  38. local YAW = math.deg(math.atan2(-T_X,-T_Z))%360
  39.  
  40. local function doWhile(f,predicate)
  41.     while predicate() do
  42.         f()
  43.     end
  44. end
  45.  
  46. --Start the cannon setup
  47. sleep(0.05)
  48. if blockReader.getBlockData().Running == 1 then error("Cannon is already running!") end
  49. print("loading cannon")
  50. redstone.setOutput(LOAD_SIDE,true)
  51. sleep(0.5)
  52. redstone.setOutput(LOAD_SIDE,false)
  53. sleep(10)
  54. print("Adjusting yaw to",YAW)
  55. rotationController.setTargetSpeed(10)
  56. doWhile(function()
  57.         local yaw = blockReader.getBlockData().CannonYaw;
  58.         rotationController.setTargetSpeed(math.max(1,math.min(32,math.floor(math.abs(YAW-yaw)/3))))
  59.         redstone.setOutput(YAW_SIDE,true);
  60.         sleep(0.05) end,
  61.     function() local yaw = blockReader.getBlockData().CannonYaw; return math.abs(yaw-YAW) > ROTATION_ERROR end)
  62. redstone.setOutput(YAW_SIDE,false)
  63. print("Adjusting pitch to",PITCH)
  64. rotationController.setTargetSpeed(1)
  65. while true do
  66.     local pitch = blockReader.getBlockData().CannonPitch
  67.     rotationController.setTargetSpeed(math.max(1,math.min(32,math.floor(math.abs(PITCH-pitch)/3))))
  68.     if pitch > PITCH + ROTATION_ERROR then
  69.         redstone.setOutput(PITCH_UP_SIDE,false)
  70.         sleep(0.05)
  71.         redstone.setOutput(PITCH_DOWN_SIDE,true)
  72.     elseif pitch < PITCH - ROTATION_ERROR then
  73.         redstone.setOutput(PITCH_DOWN_SIDE,false)
  74.         sleep(0.05)
  75.         redstone.setOutput(PITCH_UP_SIDE,true)
  76.     else
  77.         redstone.setOutput(PITCH_DOWN_SIDE,false)
  78.         redstone.setOutput(PITCH_UP_SIDE,false)
  79.         break
  80.     end
  81.     sleep(0.05)
  82. end
  83. print("Awaiting key input to fire")
  84. os.pullEvent("key")
  85. redstone.setOutput(FIRE_SIDE,true)
  86. sleep(0.5)
  87. redstone.setOutput(FIRE_SIDE,false)
  88. print("All done!")
Advertisement
Add Comment
Please, Sign In to add comment