From 8f14d0eb0eda050df83ea28a3afde0a6a42fb1dc Mon Sep 17 00:00:00 2001 From: LemADEC Date: Sat, 15 Aug 2015 16:43:27 +0200 Subject: [PATCH] Improved default radar scripts Added OpenComputers support Code cleanup --- .../lua.ComputerCraft/warpdriveRadar/ping | 28 +++-- .../lua.ComputerCraft/warpdriveRadar/scan | 34 ++--- .../lua.OpenComputers/warpdriveRadar/ping.lua | 50 ++++++++ .../lua.OpenComputers/warpdriveRadar/scan.lua | 117 ++++++++++++++++++ 4 files changed, 206 insertions(+), 23 deletions(-) create mode 100644 src/main/resources/assets/warpdrive/lua.OpenComputers/warpdriveRadar/ping.lua create mode 100644 src/main/resources/assets/warpdrive/lua.OpenComputers/warpdriveRadar/scan.lua diff --git a/src/main/resources/assets/warpdrive/lua.ComputerCraft/warpdriveRadar/ping b/src/main/resources/assets/warpdrive/lua.ComputerCraft/warpdriveRadar/ping index ea9e5b99..47ee730e 100644 --- a/src/main/resources/assets/warpdrive/lua.ComputerCraft/warpdriveRadar/ping +++ b/src/main/resources/assets/warpdrive/lua.ComputerCraft/warpdriveRadar/ping @@ -3,31 +3,43 @@ if not term.isColor() then exit() end +function error(message) + term.setBackgroundColor(colors.red) + term.setTextColor(colors.white) + term.write("No radar detected") + term.setBackgroundColor(colors.black) + term.setTextColor(colors.white) + print() +end + sides = peripheral.getNames() -mininglasers = {} for key,side in pairs(sides) do if peripheral.getType(side) == "warpdriveRadar" then - print("Wrapping " .. side) + print("Radar found on " .. side) radar = peripheral.wrap(side) end end +if radar == nil then + error("No radar detected") + return +end local argv = { ... } if #argv ~= 1 then - print("Usage: scan ") + error("Usage: ping ") return end local radius = tonumber(argv[1]) if radius < 1 or radius > 9999 then - print("Radius must be between 1 and 9999") + error("Radius must be between 1 and 9999") return end energy, energyMax = radar.getEnergyLevel() if energy < radius * radius then - print("Low energy level...") + error("Low energy level... (" + energy + "/" + radius * radius + ")") return end radar.scanRadius(radius) @@ -42,13 +54,13 @@ repeat sleep(1) seconds = seconds + 1 until (count ~= nil and count ~= -1) or seconds > 10 -print("took "..seconds.." seconds") +print("took " .. seconds .. " seconds") if count ~= nil and count > 0 then for i=0, count-1 do freq, x, y, z = radar.getResult(i) - print("Ship '"..freq.."' @ ("..x.. " " .. y .. " " .. z .. ")") + print("Ship '" .. freq .. "' @ (" .. x .. " " .. y .. " " .. z .. ")") end else - print("Nothing is found =(") + print("Nothing was found =(") end diff --git a/src/main/resources/assets/warpdrive/lua.ComputerCraft/warpdriveRadar/scan b/src/main/resources/assets/warpdrive/lua.ComputerCraft/warpdriveRadar/scan index 5ecec5bf..4144dbaa 100644 --- a/src/main/resources/assets/warpdrive/lua.ComputerCraft/warpdriveRadar/scan +++ b/src/main/resources/assets/warpdrive/lua.ComputerCraft/warpdriveRadar/scan @@ -7,13 +7,18 @@ if not term.isColor() then end sides = peripheral.getNames() -mininglasers = {} for key,side in pairs(sides) do if peripheral.getType(side) == "warpdriveRadar" then - print("Wrapping " .. side) + print("Radar found on " .. side) radar = peripheral.wrap(side) end end +if radar == nil then + term.setBackgroundColor(colors.red) + term.setTextColor(colors.white) + term.write("No radar detected") + return +end w, h = term.getSize() @@ -22,7 +27,7 @@ term.clear() function colorScreen(color) for a = 2,w-1 do for b = 1,h do - paintutils.drawPixel(a,b,color) + paintutils.drawPixel(a, b, color) end end end @@ -92,18 +97,17 @@ function scanAndDraw() end function redraw() - --shell.run("clear") - colorScreen(colors.green) - - paintutils.drawLine(1, 1, w, 1, colors.black) - - textOut(h, 1, "= Q-Radar v0.1 =", colors.white, colors.black) - - textOut(w - 3, 1, "[X]", colors.white, colors.red) - - paintutils.drawLine(1, h, w, h, colors.black); - local energy, energyMax = radar.getEnergyLevel() - textOut(4, h, "Energy: " .. energy .. " EU | Scan radius: " .. radius, colors.white, colors.black) + colorScreen(colors.green) + + paintutils.drawLine(1, 1, w, 1, colors.black) + + textOut(h, 1, "= Q-Radar v0.1 =", colors.white, colors.black) + + textOut(w - 3, 1, "[X]", colors.white, colors.red) + + paintutils.drawLine(1, h, w, h, colors.black); + local energy, energyMax = radar.getEnergyLevel() + textOut(4, h, "Energy: " .. energy .. " EU | Scan radius: " .. radius, colors.white, colors.black) end mrun = true diff --git a/src/main/resources/assets/warpdrive/lua.OpenComputers/warpdriveRadar/ping.lua b/src/main/resources/assets/warpdrive/lua.OpenComputers/warpdriveRadar/ping.lua new file mode 100644 index 00000000..2f5b9029 --- /dev/null +++ b/src/main/resources/assets/warpdrive/lua.OpenComputers/warpdriveRadar/ping.lua @@ -0,0 +1,50 @@ +local component = require("component") + +if not component.isAvailable("warpdriveRadar") then + print("No radar detected") + return +end + +local radar = component.warpdriveRadar + +local argv = { ... } +if #argv ~= 1 then + print("Usage: ping ") + return +end +local radius = tonumber(argv[1]) + +if radius < 1 or radius > 9999 then + print("Radius must be between 1 and 9999") + return +end + +energy, energyMax = radar.getEnergyLevel() +if energy < radius * radius then + print("Low energy level... (" + energy + "/" + radius * radius + ")") + return +end +radar.scanRadius(radius) +os.sleep(0.5) + +print("Scanning...") + +local seconds = 0 +local count = nil +repeat + count = radar.getResultsCount() + os.sleep(1) + seconds = seconds + 1 +until (count ~= nil and count ~= -1) or seconds > 10 +print("took " .. seconds .. " seconds") + +if count ~= nil and count > 0 then + for i=0, count-1 do + freq, x, y, z = radar.getResult(i) + print("Ship '" .. freq .. "' @ (" .. x .. " " .. y .. " " .. z .. ")") + end +else + print("Nothing was found =(") +end + +print("") diff --git a/src/main/resources/assets/warpdrive/lua.OpenComputers/warpdriveRadar/scan.lua b/src/main/resources/assets/warpdrive/lua.OpenComputers/warpdriveRadar/scan.lua new file mode 100644 index 00000000..193998e5 --- /dev/null +++ b/src/main/resources/assets/warpdrive/lua.OpenComputers/warpdriveRadar/scan.lua @@ -0,0 +1,117 @@ +local component = require("component") +local term = require("term") +radius = 500 +scale = 50 + +if not term.isAvailable() then + computer.beep() + return +end +if not component.isAvailable("warpdriveRadar") then + computer.beep() + print("No radar detected") + return +end +radar = component.warpdriveRadar + +w, h = component.gpu.getResolution() + +term.clear() + +function textOut(x, y, text, fg, bg) + if term.isAvailable() then + local w, h = component.gpu.getResolution() + if w then + component.gpu.setBackground(bg) + component.gpu.setForeground(fg) + component.gpu.set(x, y, text) + end + end +end + +function drawBox(x, y, width, height, color) + if term.isAvailable() then + local w, h = component.gpu.getResolution() + if w then + component.gpu.setBackground(color) + component.gpu.fill(x, y, width, height, " ") + end + end +end + +function translateXZ(oldX, oldZ, i) + local x = radarX - oldX + local z = radarZ - oldZ + + x = x / (radius / scale) + z = z / (radius / scale) + + x = x + (w / 2) + z = z + (h / 2) + + x = math.floor(x); + z = math.floor(z); + + return x,z +end + +function drawContact(x, y, z, name, color) + local newX, newZ = translateXZ(x, z) + + textOut(newX, newZ, " ", 0x000000, color) + textOut(newX - 3, newZ + 1, "[" .. name .. "]", 0xFFFFFF, 0x000000) +end + +function scanAndDraw() + local energy, energyMax = radar.getEnergyLevel() + if (energy < radius * radius) then + hh = math.floor(h / 2); + hw = math.floor(w / 2); + + drawBox(hw - 5, hh - 1, 11, 3, 0xFF0000); + textOut(hw - 4, hh, "LOW POWER", 0xFFFFFF, 0xFF0000); + os.sleep(1); + + return 0; + end; + radar.scanRadius(radius); + os.sleep(2); + + redraw(); + + numResults = radar.getResultsCount(); + + if (numResults ~= 0) then + for i = 0, numResults-1 do + freq, cx, cy, cz = radar.getResult(i); + + drawContact(cx, cy, cz, freq, 0xFF0000) + end + end + + drawContact(radarX, radarY, radarZ, "RAD", 0xFFFF00); +end + +function redraw() + drawBox(2, 1, w - 2, h - 1, 0x00FF00) + + drawBox(1, 1, w, 1, 0x000000) + drawBox(1, 1, 1, h, 0x000000) + drawBox(1, h, w, 1, 0x000000); + drawBox(w, h, 1, h, 0x000000); + + textOut(h, 1, "= Q-Radar v0.1 =", 0xFFFFFF, 0x000000) + + textOut(w - 3, 1, "[X]", 0xFFFFFF, 0xFF0000) + + local energy, energyMax = radar.getEnergyLevel() + textOut(4, h, "Energy: " .. energy .. " EU | Scan radius: " .. radius, 0xFFFFFF, 0x000000) +end + +mrun = true +while (mrun) do + radarX, radarY, radarZ = radar.pos(); + scanAndDraw(); +end + +term.clear();