forked from DarkholmeTenk/WarpDrive
-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added OpenComputers support Code cleanup
- Loading branch information
Showing
4 changed files
with
206 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
src/main/resources/assets/warpdrive/lua.OpenComputers/warpdriveRadar/ping.lua
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <scanRadius>") | ||
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("") |
117 changes: 117 additions & 0 deletions
117
src/main/resources/assets/warpdrive/lua.OpenComputers/warpdriveRadar/scan.lua
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); |