Advertisement
Guest User

missileAlert.lua

a guest
Aug 12th, 2017
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.74 KB | None | 0 0
  1. term = require("term")
  2. comp = require("component")
  3. --redstone = require("redstone")
  4.  
  5. sleepDelayDefault = 1
  6. empRange = 50
  7. dPrintTicks = false
  8. dPrintTables = false
  9. dPrintMessages = false
  10.  
  11. term.clear()
  12. sleepDelay = sleepDelayDefault
  13. idleSpinner = {"-", [[\]], "|", "/"}
  14. spindex = 1
  15. foundRadar = false
  16. foundChatbox = false
  17. print("Getting location...")
  18. x,y,z = 1039,65,881
  19. --x,y,z = gps.locate(5)
  20. print("x:"..x.." y:"..y.." z:"..z)
  21.  
  22. gpu = term.gpu()
  23. redstoneIO = comp.getPrimary("redstone")
  24. redstoneIO.setWirelessFrequency(4242)
  25. radar = comp.getPrimary("radar_station")
  26. if radar then
  27.   print("Found a Radar.")
  28.   foundRadar = true
  29. else
  30.   error("No Radar found. This program requires a Radar to function.")
  31. end
  32.  
  33. chatbox = comp.getPrimary("chat_box")
  34. if chatbox then
  35.   print("Found a Chatbox.")
  36.   foundChatbox = true
  37. else
  38.   print("No Chatbox found.")
  39. end
  40.  
  41.  
  42. --while(foundRadar) do
  43. --  print("tick")
  44.  
  45. --  if checkForMissile() then chatbox.say("Missile Found") end
  46. --  sleep(sleepDelay)
  47. --end
  48.  
  49. Missiles = {}
  50. Missile = {coords = {}, oldCoords = {}, name = -1} --x,y,z
  51. function Missile.__init__(baseClass, coords, oldCoords, name)
  52.   --print("x"..x.."y"..y.."z"..z)
  53.   --if not x or not y or not z then
  54.     --error("Missile needs xyz coords")
  55.   --end
  56.   --oldx, oldy, oldz = oldx or nil, oldy or nil, oldz or nil
  57.   --setmetatable(xyzTable, self)
  58.   --self._index = self
  59.   --return xyzTable
  60.   self = {coords = coords, oldCoords = oldCoords, name = name}
  61.   setmetatable(self, {__index=Missile})
  62.   return self
  63. end
  64. setmetatable(Missile, {__call=Missile.__init__})
  65.  
  66. --basePrint = print
  67. --function print(string)
  68. --  basePrint(string)
  69. --end
  70.  
  71. --function print(string,number)
  72. --  basePrint(string)
  73. --end
  74.  
  75. function dPrintT(string)
  76.   if dPrintTables then print(string) end
  77. end
  78.  
  79. function dprint(string)
  80.   if dPrintMessages then print(string) end
  81. end
  82.  
  83. tCol = "0"
  84. bCol = "15"
  85. --if term.isColor() then
  86. if gpu.getDepth() >= 4 then
  87.   tCol = "13" --green
  88.   bCol = "7" --gray
  89. end
  90. function idleSpin()
  91.   local oldX, oldY = term.getCursor()
  92.   local x, y = gpu.getResolution()
  93.   term.setCursor(x - 1, y)
  94.   if spindex > 4 then spindex = 1 end
  95.   --term.blit(idleSpinner[spindex], tCol, bCol)
  96.   term.write(idleSpinner[spindex])
  97.   spindex = spindex + 1
  98.   term.setCursor(oldX, oldY)
  99. end
  100.  
  101. -----
  102. Vector = {}
  103. Vector.__index = Vector
  104.  
  105. function Vector.new(x, y, z)
  106.   return setmetatable({ x = x or 0, y = y or 0, z = z or 0 }, Vector)
  107. end
  108.  
  109. function Vector.__sub(a, b)
  110.   --if type(a) == "number" then
  111.     --return Vector.new(b.x - a, b.y - a, b.z - a)
  112.   --elseif type(b) == "number" then
  113.     --return Vector.new(a.x - b, a.y - b, a.z - b)
  114.   --else
  115.     return Vector.new(a.x - b.x, a.y - b.y, a.z - b.z)
  116.   --end
  117. end
  118.  
  119. function Vector.tostring(a)
  120.   return "(" .. a.x .. ", " .. a.y ..", " .. a.z ..  ")"
  121. end
  122.  
  123. function Vector:len()
  124.   return math.sqrt(self.x * self.x + self.y * self.y + self.z * self.z)
  125. end
  126.  
  127. function Vector.distance(a, b)
  128.   return (b - a):len()
  129. end
  130. --------
  131.  
  132. function Missile:diffVector()
  133.   oldCoords = self.oldCoords
  134.   coords = self.coords
  135.   return Vector.new(oldCoords["x"], oldCoords["y"], oldCoords["z"]) - Vector.new(coords["x"], coords["y"], coords["z"])
  136. end
  137.  
  138. function Missile:rollover(newCoords)
  139.   dprint("newCoords:"..tabToS(newCoords).."\ncoords:"..tabToS(self.coords).."\noldCoords:"..tabToS(self.oldCoords))
  140.   self.oldCoords = self.coords
  141.   self.coords = newCoords
  142. end
  143.  
  144.  
  145. function getEntries()
  146.   return radar.getEntities()
  147. end
  148.  
  149. function tabToS(table)
  150.   local string = ""
  151.   local counter = 1
  152.   table = table or {}
  153.   for k,v in pairs(table) do
  154.     local isT = false
  155.     if type(v) == "table" then v = tabToS(v); isT = true end
  156.     string = string.."<"..counter.."> "..k.." : "
  157.     if isT then string = string.."{" end
  158.     string = string..v
  159.     if isT then string = string.."}" end
  160.     counter = counter + 1
  161.    end
  162.   return string
  163. end
  164.  
  165. function checkForMissile()
  166.   local count = 0
  167.   for k,_ in pairs(getEntries()) do
  168.     --if k then return true end
  169.     count = count + 1
  170.   end
  171.   if count > 0 then return count / 3 end
  172.   return false
  173. end
  174.  
  175. function threeDDist(x1, y1, z1, x2, y2, z2)
  176.   return math.sqrt((x2-x1)^2 + (y2-y1)^2 + (z2-z1)^2)
  177. end
  178.  
  179.  
  180. --==Main Program==--
  181. wasMissileFound = false
  182. prevMissiles = 0
  183. while(foundRadar) do
  184.   if dPrintTicks then print("tick|delay "..sleepDelay) end
  185.   idleSpin()
  186.   if wasMissileFound then
  187.   --if there was at least one missile...
  188.     mis = checkForMissile()
  189.     if not mis then
  190.     --... and no missiles were found this time
  191.       print("All missiles lost")
  192.       chatbox.say("All missiles lost")
  193.       wasMissileFound = false
  194.       Missiles = {} --wipe list
  195.       prevMissiles = 0 --wipe previous
  196.     else
  197.     --... and there was still at least one missile...
  198.       ent = getEntries()
  199.       dprint(mis.." missiles, prevMissiles = "..prevMissiles)
  200.       dPrintT(tabToS(Missiles))
  201.       for index = 0, mis-1, 1 do
  202.         coordsTable = {x = ent["x_"..index] , y = ent["y_"..index] , z = ent["z_"..index]}      
  203.         if prevMissiles == 0 then
  204.           dPrintT("adding missile with "..tabToS(coordsTable))
  205.           Missiles[index + 1] =  Missile(coordsTable, coordsTable, os.clock())
  206.         else                                                    
  207.           dprint("updating missile " ..(index + 1).. " with " ..tabToS(coordsTable))
  208.           dPrintT("missiles: "..tabToS(Missiles))
  209.          
  210.           Missiles[index + 1]:rollover(coordsTable)
  211.         end
  212.         dprint("index:"..index)
  213.       end
  214.       dprint("setting prevMissiles to "..mis)
  215.       prevMissiles = mis
  216.       --for k,v in pairs(getEntries()) do
  217.         --print(k.." "..v)
  218.       --end
  219.       --mX = getEntries()["x_0"]
  220.       --print("mX"..mX)
  221.       --mY = getEntries()["y_0"]
  222.       --mZ = getEntries()["z_0"]
  223.       --mDist = threeDDist(x, y, z, mX, mY, mZ)
  224.       --string = "Missile is "..mDist.." from computer"
  225.       --chatbox.say(string)
  226.       --print(string)
  227.       print("iterating through each missile...")
  228.       local oneInRange
  229.       for k,v in pairs(Missiles) do
  230.         --print("key:"..k.."|value: "..tabToS(v))
  231.         thisMis = v
  232.         mDist = threeDDist(x,y,z, thisMis.coords.x, thisMis.coords.y, thisMis.coords.z)
  233.         string = "Missile "..(thisMis.name).." is "..mDist.." from computer\nChange: "..Vector.tostring(thisMis:diffVector())
  234.         chatbox.say(string)
  235.         print(string)
  236.         if mDist < empRange then oneInRange = true end
  237.       end
  238.       if oneInRange then
  239.         --redstone.setAnalogOutput("bottom", 15)
  240.         redstoneIO.setWirelessOutput(true)
  241.         os.sleep(sleepDelay)
  242.         --redstone.setAnalogOutput("bottom", 0)
  243.         redstoneIO.setWirelessOutput(false)
  244.         print("Pulse sent")
  245.         chatbox.say("Pulse sent")
  246.       end
  247.     end
  248.   else
  249.     --look for missiles
  250.     if checkForMissile() then
  251.       chatbox.say("Found Missile")
  252.       wasMissileFound = true
  253.       sleepDelay = .5
  254.     else
  255.       sleepDelay = sleepDelayDefault
  256.     end
  257.   end
  258.   os.sleep(sleepDelay)
  259. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement