Incomprehensible

turret computer

Apr 4th, 2025 (edited)
645
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.05 KB | None | 0 0
  1. local monitor = peripheral.find("monitor")
  2. local modem = peripheral.find("modem", rednet.open) -- doing it like this finds all modem peripherals and then opens rednet on them
  3. local tPos
  4. local pos
  5. local tolerance = 5
  6. --- MAIN
  7. monitor.clear()
  8. monitor.setCursorPos(1,1)
  9. monitor.setTextScale(0.5)
  10. monitor.write("Hello, ID: " .. os.computerID()) --the id of the computer
  11.  
  12. function vec3ToString(vec3)
  13.     local str = ("(x:" .. math.floor(vec3.x+0.5) .. ", y:" .. math.floor(vec3.y+0.5) .. ", z:" .. math.floor(vec3.z+0.5) .. ")")
  14.     return str
  15. end
  16.  
  17. function receivePosMessage()
  18.     local id, message = rednet.receive(nil,0.1)
  19.     if (message == nil) then return end
  20.     tPos = message
  21.  
  22. end
  23.  
  24.  
  25. function update()
  26.     pos = ship.getWorldspacePosition()
  27.     receivePosMessage()
  28.     if(tPos == nil or pos == nil) then
  29.         return
  30.     end
  31.     monitor.clear()
  32.     monitor.setCursorPos(1,1)
  33.     monitor.write("Message from control: " .. vec3ToString(tPos))
  34.     monitor.setCursorPos(1,2)
  35.     monitor.write("Our position: " .. vec3ToString(pos))
  36.    
  37.     local deltaPos = vector.new(tPos.x-pos.x,0.0,tPos.z-pos.z)
  38.     local ang = (-(math.pi/2)+(math.atan2(deltaPos.z,deltaPos.x))) * 180/math.pi
  39.     if(ang<0) then
  40.         ang = ang + 360
  41.     end
  42.    
  43.     local yaw = (ship.getEulerAnglesYXZ().y * -180/math.pi)
  44.  
  45.     yaw = (yaw + 360) % 360
  46.     ang = (ang + 360) % 360
  47.    
  48.     monitor.setCursorPos(1,3)
  49.     monitor.write("ANG: " .. ang .. ", MY ANG: " .. yaw)
  50.     monitor.setCursorPos(1,4)
  51.    
  52.     local angDiff = yaw-ang
  53.     angDiff = (angDiff + 180) % 360-180
  54.    
  55.     if(angDiff> 180) then
  56.         angDiff = angDiff-360
  57.     elseif (angDiff <-180) then
  58.         angDiff = angDiff + 360
  59.     end
  60.    
  61.     monitor.write("ANG DIFF: " .. angDiff)
  62.     print("ANG: " .. ang .. ", MY ANG: " .. yaw)
  63.     print("ANG DIFF: " .. (angDiff))
  64.    
  65.     if(angDiff < -tolerance) then
  66.         redstone.setOutput("right", true)
  67.         redstone.setOutput("left", false)
  68.     elseif(angDiff > tolerance) then
  69.         redstone.setOutput("right", false)
  70.         redstone.setOutput("left", true)
  71.     else
  72.         redstone.setOutput("right", false)
  73.         redstone.setOutput("left", false)
  74.     end
  75. end
  76.  
  77. while true do
  78.     sleep(0.1)
  79.     update()
  80. end
Advertisement
Add Comment
Please, Sign In to add comment