Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- term = require("term")
- comp = require("component")
- --redstone = require("redstone")
- sleepDelayDefault = 1
- empRange = 50
- dPrintTicks = false
- dPrintTables = false
- dPrintMessages = false
- term.clear()
- sleepDelay = sleepDelayDefault
- idleSpinner = {"-", [[\]], "|", "/"}
- spindex = 1
- foundRadar = false
- foundChatbox = false
- print("Getting location...")
- x,y,z = 1039,65,881
- --x,y,z = gps.locate(5)
- print("x:"..x.." y:"..y.." z:"..z)
- gpu = term.gpu()
- redstoneIO = comp.getPrimary("redstone")
- redstoneIO.setWirelessFrequency(4242)
- radar = comp.getPrimary("radar_station")
- if radar then
- print("Found a Radar.")
- foundRadar = true
- else
- error("No Radar found. This program requires a Radar to function.")
- end
- chatbox = comp.getPrimary("chat_box")
- if chatbox then
- print("Found a Chatbox.")
- foundChatbox = true
- else
- print("No Chatbox found.")
- end
- --while(foundRadar) do
- -- print("tick")
- -- if checkForMissile() then chatbox.say("Missile Found") end
- -- sleep(sleepDelay)
- --end
- Missiles = {}
- Missile = {coords = {}, oldCoords = {}, name = -1} --x,y,z
- function Missile.__init__(baseClass, coords, oldCoords, name)
- --print("x"..x.."y"..y.."z"..z)
- --if not x or not y or not z then
- --error("Missile needs xyz coords")
- --end
- --oldx, oldy, oldz = oldx or nil, oldy or nil, oldz or nil
- --setmetatable(xyzTable, self)
- --self._index = self
- --return xyzTable
- self = {coords = coords, oldCoords = oldCoords, name = name}
- setmetatable(self, {__index=Missile})
- return self
- end
- setmetatable(Missile, {__call=Missile.__init__})
- --basePrint = print
- --function print(string)
- -- basePrint(string)
- --end
- --function print(string,number)
- -- basePrint(string)
- --end
- function dPrintT(string)
- if dPrintTables then print(string) end
- end
- function dprint(string)
- if dPrintMessages then print(string) end
- end
- tCol = "0"
- bCol = "15"
- --if term.isColor() then
- if gpu.getDepth() >= 4 then
- tCol = "13" --green
- bCol = "7" --gray
- end
- function idleSpin()
- local oldX, oldY = term.getCursor()
- local x, y = gpu.getResolution()
- term.setCursor(x - 1, y)
- if spindex > 4 then spindex = 1 end
- --term.blit(idleSpinner[spindex], tCol, bCol)
- term.write(idleSpinner[spindex])
- spindex = spindex + 1
- term.setCursor(oldX, oldY)
- end
- -----
- Vector = {}
- Vector.__index = Vector
- function Vector.new(x, y, z)
- return setmetatable({ x = x or 0, y = y or 0, z = z or 0 }, Vector)
- end
- function Vector.__sub(a, b)
- --if type(a) == "number" then
- --return Vector.new(b.x - a, b.y - a, b.z - a)
- --elseif type(b) == "number" then
- --return Vector.new(a.x - b, a.y - b, a.z - b)
- --else
- return Vector.new(a.x - b.x, a.y - b.y, a.z - b.z)
- --end
- end
- function Vector.tostring(a)
- return "(" .. a.x .. ", " .. a.y ..", " .. a.z .. ")"
- end
- function Vector:len()
- return math.sqrt(self.x * self.x + self.y * self.y + self.z * self.z)
- end
- function Vector.distance(a, b)
- return (b - a):len()
- end
- --------
- function Missile:diffVector()
- oldCoords = self.oldCoords
- coords = self.coords
- return Vector.new(oldCoords["x"], oldCoords["y"], oldCoords["z"]) - Vector.new(coords["x"], coords["y"], coords["z"])
- end
- function Missile:rollover(newCoords)
- dprint("newCoords:"..tabToS(newCoords).."\ncoords:"..tabToS(self.coords).."\noldCoords:"..tabToS(self.oldCoords))
- self.oldCoords = self.coords
- self.coords = newCoords
- end
- function getEntries()
- return radar.getEntities()
- end
- function tabToS(table)
- local string = ""
- local counter = 1
- table = table or {}
- for k,v in pairs(table) do
- local isT = false
- if type(v) == "table" then v = tabToS(v); isT = true end
- string = string.."<"..counter.."> "..k.." : "
- if isT then string = string.."{" end
- string = string..v
- if isT then string = string.."}" end
- counter = counter + 1
- end
- return string
- end
- function checkForMissile()
- local count = 0
- for k,_ in pairs(getEntries()) do
- --if k then return true end
- count = count + 1
- end
- if count > 0 then return count / 3 end
- return false
- end
- function threeDDist(x1, y1, z1, x2, y2, z2)
- return math.sqrt((x2-x1)^2 + (y2-y1)^2 + (z2-z1)^2)
- end
- --==Main Program==--
- wasMissileFound = false
- prevMissiles = 0
- while(foundRadar) do
- if dPrintTicks then print("tick|delay "..sleepDelay) end
- idleSpin()
- if wasMissileFound then
- --if there was at least one missile...
- mis = checkForMissile()
- if not mis then
- --... and no missiles were found this time
- print("All missiles lost")
- chatbox.say("All missiles lost")
- wasMissileFound = false
- Missiles = {} --wipe list
- prevMissiles = 0 --wipe previous
- else
- --... and there was still at least one missile...
- ent = getEntries()
- dprint(mis.." missiles, prevMissiles = "..prevMissiles)
- dPrintT(tabToS(Missiles))
- for index = 0, mis-1, 1 do
- coordsTable = {x = ent["x_"..index] , y = ent["y_"..index] , z = ent["z_"..index]}
- if prevMissiles == 0 then
- dPrintT("adding missile with "..tabToS(coordsTable))
- Missiles[index + 1] = Missile(coordsTable, coordsTable, os.clock())
- else
- dprint("updating missile " ..(index + 1).. " with " ..tabToS(coordsTable))
- dPrintT("missiles: "..tabToS(Missiles))
- Missiles[index + 1]:rollover(coordsTable)
- end
- dprint("index:"..index)
- end
- dprint("setting prevMissiles to "..mis)
- prevMissiles = mis
- --for k,v in pairs(getEntries()) do
- --print(k.." "..v)
- --end
- --mX = getEntries()["x_0"]
- --print("mX"..mX)
- --mY = getEntries()["y_0"]
- --mZ = getEntries()["z_0"]
- --mDist = threeDDist(x, y, z, mX, mY, mZ)
- --string = "Missile is "..mDist.." from computer"
- --chatbox.say(string)
- --print(string)
- print("iterating through each missile...")
- local oneInRange
- for k,v in pairs(Missiles) do
- --print("key:"..k.."|value: "..tabToS(v))
- thisMis = v
- mDist = threeDDist(x,y,z, thisMis.coords.x, thisMis.coords.y, thisMis.coords.z)
- string = "Missile "..(thisMis.name).." is "..mDist.." from computer\nChange: "..Vector.tostring(thisMis:diffVector())
- chatbox.say(string)
- print(string)
- if mDist < empRange then oneInRange = true end
- end
- if oneInRange then
- --redstone.setAnalogOutput("bottom", 15)
- redstoneIO.setWirelessOutput(true)
- os.sleep(sleepDelay)
- --redstone.setAnalogOutput("bottom", 0)
- redstoneIO.setWirelessOutput(false)
- print("Pulse sent")
- chatbox.say("Pulse sent")
- end
- end
- else
- --look for missiles
- if checkForMissile() then
- chatbox.say("Found Missile")
- wasMissileFound = true
- sleepDelay = .5
- else
- sleepDelay = sleepDelayDefault
- end
- end
- os.sleep(sleepDelay)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement