Advertisement
Incomprehensible

vs testing

Jul 6th, 2023 (edited)
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.24 KB | None | 0 0
  1. monitor = peripheral.find("monitor")
  2. monitorLine = 0
  3. shipReader = peripheral.find("ship_reader")
  4. radar = peripheral.find("radar")
  5.  
  6. shipList = {}
  7.  
  8. targetID = 0
  9.  
  10. myPos = {}
  11. targetPos = {}
  12. currentBearing = 0
  13. bearingToTarget = 0
  14.  
  15. --///////////////////////////////////////////////////TABLE UTILS
  16. function dump(o)
  17. if type(o) == 'table' then
  18. local s = '{ '
  19. for k,v in pairs(o) do
  20. if type(k) ~= 'number' then k = '"'..k..'"' end
  21. s = s .. '['..k..'] = ' .. dump(v) .. ','
  22. end
  23. return s .. '} '
  24. else
  25. return tostring(o)
  26. end
  27. end
  28. --///////////////////////////////////////////////////BLACKLIST
  29. function removeByBlacklist(blacklist,ships)
  30. local idsToRemove = {}
  31. for i = #ships, 1, -1 do
  32. currentShip = ships[i]
  33. for v = 0,#blacklist,1 do
  34. if (blacklist[v] == currentShip['id']) then
  35. table.remove(ships,i)
  36. end
  37. end
  38. end
  39. end
  40. --///////////////////////////////////////////////////MONITOR
  41. function displayStr(str)
  42. monitor.setCursorPos(1,monitorLine)
  43. monitor.write(str)
  44. monitorLine = monitorLine+1
  45. end
  46. --///////////////////////////////////////////////////POSITION STUFF
  47. function getPositions()
  48. myPos = shipReader.getWorldspacePosition()
  49. targetPos = shipList[1].position
  50. end
  51. --///////////////////////////////////////////////////ANGLE STUFF
  52. function to_deg(ang)
  53. local newAngle = ang*180/math.pi
  54. if (newAngle < 0) then
  55. newAngle = newAngle + 360
  56. end
  57. return newAngle
  58. end
  59.  
  60. function short_angle_dist(from, to)
  61. local max_angle = math.pi * 2
  62. local difference = math.fmod(to - from, max_angle)
  63. return math.fmod(2 * difference, max_angle) - difference
  64. end
  65.  
  66. function angle_vec2(vec1,vec2)
  67. return (math.atan2(vec1.x - vec2.x , vec1.z - vec2.z))
  68. end
  69.  
  70. function rad_to_target()
  71. local angle = angle_vec2(myPos,targetPos)
  72. return short_angle_dist(currentBearing,angle)
  73. end
  74. --///////////////////////////////////////////////////REDSTONE
  75. function rotate_left()
  76. redstone.setOutput("left", true)
  77. redstone.setOutput("right", false)
  78. end
  79.  
  80. function rotate_right()
  81. redstone.setOutput("left", false)
  82. redstone.setOutput("right", true)
  83. end
  84.  
  85. function yaw_to_target()
  86. if(bearingToTarget<-0.03) then
  87. rotate_right()
  88. elseif (bearingToTarget > 0.03) then
  89. rotate_left()
  90. else
  91. redstone.setOutput("left", false)
  92. redstone.setOutput("right", false)
  93. end
  94. end
  95.  
  96. --///////////////////////////////////////////////////START
  97.  
  98. local blacklist = {shipReader.getShipID()}
  99.  
  100. monitor.setCursorPos(1,1)
  101. monitor.setTextScale(0.5)
  102. monitor.clear()
  103.  
  104. --//////////////////////////////////////////////////UPDATE
  105. while true do
  106. monitorLine = 1
  107. monitor.clear()
  108. monitor.setCursorPos(1,1)
  109.  
  110. shipList = radar.scan(250)[1]
  111. removeByBlacklist(blacklist,shipList)
  112. getPositions()
  113. currentBearing = shipReader.getRotation()['roll']
  114.  
  115. bearingToTarget = rad_to_target()
  116.  
  117. displayStr("My pos: " .. math.floor(myPos.x) .. "," .. math.floor(myPos.y) .. "," .. math.floor(myPos.z))
  118. displayStr("My bearing: " .. math.floor(currentBearing))
  119. displayStr("Target pos: " .. math.floor(targetPos.x) .. "," .. math.floor(targetPos.y) .. "," .. math.floor(targetPos.z))
  120. displayStr("Degress to target:" .. bearingToTarget)
  121. --print(bearingToTarget)
  122. yaw_to_target()
  123.  
  124. sleep(0.08)
  125. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement