Integration of OC and CC LUA scripts

Added a few null checks to radar scan scripts
Added explicit closing message to farm and mine scripts
Fixed a few LUA warnings
Fixed multiple tree farm support
Fixed #264 OC scripts crash on GPU check
Code cleanup
This commit is contained in:
LemADEC 2017-05-23 12:20:32 +02:00
parent 2e24b99735
commit 70692f4dbe
12 changed files with 146 additions and 100 deletions

View file

@ -3,9 +3,9 @@ if not term.isColor() then
exit()
end
sides = peripheral.getNames()
local sides = peripheral.getNames()
treefarms = {}
for key,side in pairs(sides) do
for _, side in pairs(sides) do
if peripheral.getType(side) == "warpdriveLaserTreeFarm" then
print("Wrapping " .. side)
table.insert(treefarms, peripheral.wrap(side))
@ -13,11 +13,11 @@ for key,side in pairs(sides) do
end
noExit = true
local noExit = true
breakLeaves = true
tapTrees = true
silktouch = false
args = {...}
local args = {...}
if #args > 0 then
if args[1] == "help" or args[1] == "?" then
print("Usage: farm <breakLeaves> <tapTrees> <silktouch>")
@ -57,8 +57,8 @@ if #treefarms == 0 then
noExit = false
end
if noExit then
for key,treefarm in pairs(treefarms) do
statusString, isActive = treefarm.state()
for _, treefarm in pairs(treefarms) do
local _, isActive = treefarm.state()
if not isActive then
treefarm.breakLeaves(breakLeaves)
treefarm.tapTrees(tapTrees)
@ -67,10 +67,10 @@ if noExit then
treefarm.start()
end
end
sleep(1)
os.sleep(1)
end
label = os.getComputerLabel()
local label = os.getComputerLabel()
if label then
else
label = "" .. os.getComputerID()
@ -78,10 +78,11 @@ end
term.setTextColor(colors.blue)
if noExit then
local areActive
repeat
isActive = false
areActive = false
for key,treefarm in pairs(treefarms) do
status, isActive, energy, totalHarvested, currentValuable, totalValuables = treefarm.state()
local status, isActive, energy, totalHarvested, currentValuable, totalValuables = treefarm.state()
term.setBackgroundColor(colors.black)
term.clear()
@ -100,14 +101,17 @@ if noExit then
term.write("Harvested " .. totalHarvested .. " items and counting... ")
if isActive then
sleep(1)
areActive = true
os.sleep(1)
else
sleep(0.1)
os.sleep(0.1)
end
end
until not isActive
until not areActive
end
term.setBackgroundColor(colors.black)
term.setTextColor(colors.white)
print()
print("Program closed")

View file

@ -3,16 +3,16 @@ if not term.isColor() then
exit()
end
sides = peripheral.getNames()
local sides = peripheral.getNames()
treefarms = {}
for key,side in pairs(sides) do
for _, side in pairs(sides) do
if peripheral.getType(side) == "warpdriveLaserTreeFarm" then
print("Wrapping " .. side)
table.insert(treefarms, peripheral.wrap(side))
end
end
label = os.getComputerLabel()
local label = os.getComputerLabel()
if label then
else
label = "" .. os.getComputerID()
@ -28,9 +28,9 @@ if #treefarms == 0 then
term.setTextColor(colors.white)
term.write("No laser tree farm detected")
else
for key,treefarm in pairs(treefarms) do
for key, treefarm in pairs(treefarms) do
term.setCursorPos(1, 2 + key)
statusString, isActive = treefarm.state()
local _, isActive = treefarm.state()
if not isActive then
term.setBackgroundColor(colors.red)
term.setTextColor(colors.white)
@ -46,4 +46,5 @@ end
term.setBackgroundColor(colors.black)
term.setTextColor(colors.white)
print()

View file

@ -3,9 +3,9 @@ if not term.isColor() then
exit()
end
sides = peripheral.getNames()
local sides = peripheral.getNames()
mininglasers = {}
for key,side in pairs(sides) do
for _, side in pairs(sides) do
if peripheral.getType(side) == "warpdriveMiningLaser" then
print("Wrapping " .. side)
table.insert(mininglasers, peripheral.wrap(side))
@ -13,11 +13,11 @@ for key,side in pairs(sides) do
end
noExit = true
local noExit = true
layerOffset = 1
onlyOres = false
silktouch = false
args = {...}
local args = {...}
if #args > 0 then
if args[1] == "help" or args[1] == "?" then
print("Usage: mine <layerOffset> <onlyOres> <silktouch>")
@ -55,8 +55,8 @@ if #mininglasers == 0 then
noExit = false
end
if noExit then
for key,mininglaser in pairs(mininglasers) do
statusString, isActive = mininglaser.state()
for _, mininglaser in pairs(mininglasers) do
local _, isActive = mininglaser.state()
if not isActive then
mininglaser.offset(layerOffset)
mininglaser.onlyOres(onlyOres)
@ -65,10 +65,10 @@ if noExit then
mininglaser.start()
end
end
sleep(1)
os.sleep(1)
end
label = os.getComputerLabel()
local label = os.getComputerLabel()
if label then
else
label = "" .. os.getComputerID()
@ -76,10 +76,11 @@ end
term.setTextColor(colors.blue)
if noExit then
local areActive
repeat
areActive = false
for key,mininglaser in pairs(mininglasers) do
status, isActive, energy, currentLayer, mined, total = mininglaser.state()
local status, isActive, energy, currentLayer, mined, total = mininglaser.state()
term.setBackgroundColor(colors.black)
term.clear()
@ -97,9 +98,9 @@ if noExit then
if isActive then
areActive = true
sleep(1)
os.sleep(1)
else
sleep(0.1)
os.sleep(0.1)
end
end
until not areActive
@ -107,4 +108,6 @@ end
term.setBackgroundColor(colors.black)
term.setTextColor(colors.white)
print()
print("Program closed")

View file

@ -3,16 +3,16 @@ if not term.isColor() then
exit()
end
sides = peripheral.getNames()
local sides = peripheral.getNames()
mininglasers = {}
for key,side in pairs(sides) do
for _, side in pairs(sides) do
if peripheral.getType(side) == "warpdriveMiningLaser" then
print("Wrapping " .. side)
table.insert(mininglasers, peripheral.wrap(side))
end
end
label = os.getComputerLabel()
local label = os.getComputerLabel()
if label then
else
label = "" .. os.getComputerID()
@ -28,9 +28,9 @@ if #mininglasers == 0 then
term.setTextColor(colors.white)
term.write("No mining laser detected")
else
for key,mininglaser in pairs(mininglasers) do
for key, mininglaser in pairs(mininglasers) do
term.setCursorPos(1, 2 + key)
statusString, isActive = mininglaser.state()
local _, isActive = mininglaser.state()
if not isActive then
term.setBackgroundColor(colors.red)
term.setTextColor(colors.white)
@ -46,4 +46,5 @@ end
term.setBackgroundColor(colors.black)
term.setTextColor(colors.white)
print()

View file

@ -1,4 +1,3 @@
local scale = 50
if not term.isColor() then
print("Advanced computer required")
@ -15,8 +14,8 @@ function error(message)
end
local radar
sides = peripheral.getNames()
for key,side in pairs(sides) do
local sides = peripheral.getNames()
for _, side in pairs(sides) do
if peripheral.getType(side) == "warpdriveRadar" then
print("Radar found on " .. side)
radar = peripheral.wrap(side)
@ -34,14 +33,16 @@ if #argv ~= 1 then
end
local radius = tonumber(argv[1])
local scale = 50
local w, h = term.getSize()
local radarX, radarY, radarZ = radar.position()
term.clear()
function colorScreen(color)
for a = 2,w-1 do
for b = 1,h do
for a = 2, w - 1 do
for b = 1, h do
paintutils.drawPixel(a, b, color)
end
end
@ -52,11 +53,11 @@ function textOut(x, y, text, fg, bg)
term.setTextColor(fg)
term.setBackgroundColor(bg)
term.write(text)
local xt,yt = term.getCursorPos()
local _, yt = term.getCursorPos()
term.setCursorPos(1, yt + 1)
end
function translateXZ(oldX, oldZ, i)
function translateXZ(oldX, oldZ)
local x = radarX - oldX
local z = radarZ - oldZ
@ -69,10 +70,10 @@ function translateXZ(oldX, oldZ, i)
x = math.floor(x)
z = math.floor(z)
return x,z
return x, z
end
function drawContact(x, y, z, name, color)
function drawContact(x, _, z, name, color)
local newX, newZ = translateXZ(x, z)
paintutils.drawPixel(newX, newZ, color)
@ -81,8 +82,13 @@ end
function scanAndDraw()
local energy, energyMax = radar.energy()
if energy == nil then energy = 0 end
if energyMax == nil or energyMax == 0 then energyMax = 1 end
local energyRequired = radar.getEnergyRequired(radius)
if (energy < energyRequired) then
if energyRequired == nil then energyRequired = 0 end
if (energyRequired <= 0 or energy < energyRequired) then
local hh = math.floor(h / 2)
local hw = math.floor(w / 2)
@ -101,13 +107,15 @@ function scanAndDraw()
redraw()
numResults = radar.getResultsCount()
local numResults = radar.getResultsCount()
if (numResults ~= 0) then
for i = 0, numResults-1 do
success, type, name, cx, cy, cz = radar.getResult(i)
local success, _, name, cx, cy, cz = radar.getResult(i)
drawContact(cx, cy, cz, name, colors.red)
if success then
drawContact(cx, cy, cz, name, colors.red)
end
end
end
@ -124,11 +132,11 @@ function redraw()
textOut(w - 3, 1, "[X]", colors.white, colors.red)
paintutils.drawLine(1, h, w, h, colors.black)
local energy, energyMax = radar.energy()
local energy, _ = radar.energy()
if energy == nil then energy = 0 end
textOut(4, h, "Energy: " .. energy .. " EU | Scan radius: " .. radius, colors.white, colors.black)
end
local radarX, radarY, radarZ = radar.position()
local continue = true
while continue do
scanAndDraw()

View file

@ -22,6 +22,7 @@ local ship_rotationSteps = 0
local ship_indexPlayer = 0
local ship_indexTarget = 0
local ship = nil
local ship_actualDistance
function ship_read(parData)
data = parData
@ -56,6 +57,7 @@ function ship_boot()
w.setColorNormal()
w.write("- detecting Ship Core: ")
local timeout = 10
local isAttached
repeat
isAttached = ship.isAttached()
w.sleep(0.05)
@ -79,6 +81,7 @@ function ship_boot()
w.setColorNormal()
w.write("- global position : ")
timeout = 10
local pos
repeat
pos = ship.position()
w.sleep(0.05)
@ -567,7 +570,7 @@ function ship_page_navigation()
local locationCurrent = "somewhere..." -- @TODO ship.getLocation()
w.writeLn("Current ship location : " .. locationCurrent)
w.writeLn("Jumpgates or beacons in range:")
local stringTargets, _ = "not implemented" -- ship.getTargets()
local stringTargets, _ = "not implemented", nil -- ship.getTargets()
if stringTargets == nil or stringTargets == "" then
stringTargets = "~ no beacon nor jumpgate in range ~"
end

View file

@ -6,20 +6,20 @@ if not term.isAvailable() then
computer.beep()
os.exit()
end
if not component.gpu.getDepth() < 4 then
if component.gpu.getDepth() < 4 then
print("Tier 2 GPU required")
os.exit()
end
treefarms = {}
for address,type in component.list("warpdriveLaserTreeFarm", true) do
for address, _ in component.list("warpdriveLaserTreeFarm", true) do
print("Wrapping " .. address)
table.insert(treefarms, component.proxy(address))
end
function textOut(x, y, text, fg, bg)
if term.isAvailable() then
local w, h = component.gpu.getResolution()
local w, _ = component.gpu.getResolution()
if w then
component.gpu.setBackground(bg)
component.gpu.setForeground(fg)
@ -30,11 +30,11 @@ function textOut(x, y, text, fg, bg)
end
noExit = true
local noExit = true
breakLeaves = true
tapTrees = true
silktouch = false
args = {...}
local args = {... }
if #args > 0 then
if args[1] == "help" or args[1] == "?" then
print("Usage: farm <breakLeaves> <tapTrees> <silktouch>")
@ -72,8 +72,8 @@ if #treefarms == 0 then
noExit = false
end
if noExit then
for key,treefarm in pairs(treefarms) do
statusString, isActive = treefarm.state()
for _, treefarm in pairs(treefarms) do
local _, isActive = treefarm.state()
if not isActive then
treefarm.breakLeaves(breakLeaves)
treefarm.tapTrees(tapTrees)
@ -86,6 +86,7 @@ if noExit then
end
local file = io.open("/etc/hostname")
local label
if file then
label = file:read("*l")
file:close()
@ -94,10 +95,11 @@ else
end
if noExit then
local areActive
repeat
isActive = false
for key,treefarm in pairs(treefarms) do
status, isActive, energy, totalHarvested, currentValuable, totalValuables = treefarm.state()
areActive = false
for key, treefarm in pairs(treefarms) do
local status, isActive, energy, totalHarvested, currentValuable, totalValuables = treefarm.state()
term.clear()
textOut(1, 1, label .. " - Laser tree farm " .. key .. " of " .. #treefarms, 0x0000FF, 0x00FF00)
@ -107,15 +109,24 @@ if noExit then
textOut(1, 9, "Harvested " .. totalHarvested .. " items and counting... ", 0xFFFFFF, 0x000000)
if isActive then
areActive = true
os.sleep(1)
else
os.sleep(0.1)
end
end
until not isActive
until not areActive
end
textOut(1, 1, "", 0xFFFFFF, 0x000000)
textOut(1, 9, "", 0xFFFFFF, 0x000000)
print("")
print("")
print()
print()
print()
print()
print()
print()
print()
print()
print()
print("Program closed")

View file

@ -6,7 +6,7 @@ if not term.isAvailable() then
computer.beep()
os.exit()
end
if not component.gpu.getDepth() < 4 then
if component.gpu.getDepth() < 4 then
print("Tier 2 GPU required")
os.exit()
end

View file

@ -6,20 +6,20 @@ if not term.isAvailable() then
computer.beep()
os.exit()
end
if not component.gpu.getDepth() < 4 then
if component.gpu.getDepth() < 4 then
print("Tier 2 GPU required")
os.exit()
end
mininglasers = {}
for address,type in component.list("warpdriveMiningLaser", true) do
for address, _ in component.list("warpdriveMiningLaser", true) do
print("Wrapping " .. address)
table.insert(mininglasers, component.proxy(address))
end
function textOut(x, y, text, fg, bg)
if term.isAvailable() then
local w, h = component.gpu.getResolution()
local w, _ = component.gpu.getResolution()
if w then
component.gpu.setBackground(bg)
component.gpu.setForeground(fg)
@ -30,11 +30,11 @@ function textOut(x, y, text, fg, bg)
end
noExit = true
local noExit = true
layerOffset = 1
onlyOres = false
silktouch = false
args = {...}
local args = {...}
if #args > 0 then
if args[1] == "help" or args[1] == "?" then
print("Usage: mine <layerOffset> <onlyOres> <silktouch>")
@ -70,8 +70,8 @@ if #mininglasers == 0 then
noExit = false
end
if noExit then
for key,mininglaser in pairs(mininglasers) do
statusString, isActive = mininglaser.state()
for _, mininglaser in pairs(mininglasers) do
local _, isActive = mininglaser.state()
if not isActive then
mininglaser.offset(layerOffset)
mininglaser.onlyOres(onlyOres)
@ -84,6 +84,7 @@ if noExit then
end
local file = io.open("/etc/hostname")
local label
if file then
label = file:read("*l")
file:close()
@ -92,10 +93,11 @@ else
end
if noExit then
local areActive
repeat
areActive = false
for key,mininglaser in pairs(mininglasers) do
status, isActive, energy, currentLayer, mined, total = mininglaser.state()
for key, mininglaser in pairs(mininglasers) do
local status, isActive, energy, currentLayer, mined, total = mininglaser.state()
term.clear()
textOut(1, 1, label .. " - Mining laser " .. key .. " of " .. #mininglasers, 0x0000FF, 0x00FF00)
@ -115,5 +117,11 @@ end
textOut(1, 1, "", 0xFFFFFF, 0x000000)
print("")
print("")
print()
print()
print()
print()
print()
print()
print()
print("Program closed")

View file

@ -6,17 +6,18 @@ if not term.isAvailable() then
computer.beep()
os.exit()
end
if not component.gpu.getDepth() < 4 then
if component.gpu.getDepth() < 4 then
print("Tier 2 GPU required")
os.exit()
end
mininglasers = {}
for address,type in component.list("warpdriveMiningLaser", true) do
for address, _ in component.list("warpdriveMiningLaser", true) do
table.insert(mininglasers, component.proxy(address))
end
local file = io.open("/etc/hostname")
local label
if file then
label = file:read("*l")
file:close()
@ -46,8 +47,8 @@ if #mininglasers == 0 then
computer.beep()
textOut(1, 2, "No mining laser detected", 0xFF0000, 0x000000)
else
for key,mininglaser in pairs(mininglasers) do
statusString, isActive = mininglaser.state()
for key, mininglaser in pairs(mininglasers) do
local _, isActive = mininglaser.state()
if not isActive then
textOut(1, 2 + key, "Mining laser " .. key .. " of " .. #mininglasers .. " is already stopped", 0xFFFFFF, 0xFF0000)
else

View file

@ -1,14 +1,12 @@
local component = require("component")
local computer = require("computer")
local term = require("term")
local radius = 500
local scale = 50
if not term.isAvailable() then
computer.beep()
os.exit()
end
if not component.gpu.getDepth() < 4 then
if component.gpu.getDepth() < 4 then
print("Tier 2 GPU required")
os.exit()
end
@ -37,14 +35,16 @@ if #argv ~= 1 then
end
local radius = tonumber(argv[1])
local scale = 50
local w, h = component.gpu.getResolution()
local radarX, radarY, radarZ = radar.position()
term.clear()
function textOut(x, y, text, fg, bg)
if term.isAvailable() then
local w, h = component.gpu.getResolution()
local w, _ = component.gpu.getResolution()
if w then
component.gpu.setBackground(bg)
component.gpu.setForeground(fg)
@ -56,7 +56,7 @@ end
function drawBox(x, y, width, height, color)
if term.isAvailable() then
local w, h = component.gpu.getResolution()
local w, _ = component.gpu.getResolution()
if w then
component.gpu.setBackground(color)
component.gpu.fill(x, y, width, height, " ")
@ -65,7 +65,7 @@ function drawBox(x, y, width, height, color)
end
end
function translateXZ(oldX, oldZ, i)
function translateXZ(oldX, oldZ)
local x = radarX - oldX
local z = radarZ - oldZ
@ -78,10 +78,10 @@ function translateXZ(oldX, oldZ, i)
x = math.floor(x)
z = math.floor(z)
return x,z
return x, z
end
function drawContact(x, y, z, name, color)
function drawContact(x, _, z, name, color)
local newX, newZ = translateXZ(x, z)
textOut(newX, newZ, " ", 0x000000, color)
@ -90,8 +90,13 @@ end
function scanAndDraw()
local energy, energyMax = radar.energy()
if energy == nil then energy = 0 end
if energyMax == nil or energyMax == 0 then energyMax = 1 end
local energyRequired = radar.getEnergyRequired(radius)
if (energy < energyRequired) then
if energyRequired == nil then energyRequired = 0 end
if (energyRequired <= 0 or energy < energyRequired) then
local hh = math.floor(h / 2)
local hw = math.floor(w / 2)
@ -101,6 +106,7 @@ function scanAndDraw()
return 0
end
radar.radius(radius)
radar.start()
local scanDuration = radar.getScanDuration(radius)
@ -108,13 +114,14 @@ function scanAndDraw()
redraw()
numResults = radar.getResultsCount()
local numResults = radar.getResultsCount()
if (numResults ~= 0) then
for i = 0, numResults-1 do
success, type, name, cx, cy, cz = radar.getResult(i)
drawContact(cx, cy, cz, name, 0xFF0000)
local success, _, name, cx, cy, cz = radar.getResult(i)
if success then
drawContact(cx, cy, cz, name, 0xFF0000)
end
end
end
@ -133,12 +140,11 @@ function redraw()
textOut(w - 3, 1, "[X]", 0xFFFFFF, 0xFF0000)
local energy, energyMax = radar.energy()
local energy, _ = radar.energy()
if energy == nil then energy = 0 end
textOut(4, h, "Energy: " .. energy .. " EU | Scan radius: " .. radius, 0xFFFFFF, 0x000000)
end
local radarX, radarY, radarZ = radar.position()
local continue = true
while component.isAvailable("warpdriveRadar") and continue do
scanAndDraw()

View file

@ -23,6 +23,7 @@ local ship_rotationSteps = 0
local ship_indexPlayer = 0
local ship_indexTarget = 0
local ship = nil
local ship_actualDistance
function ship_read(parData)
data = parData
@ -57,6 +58,7 @@ function ship_boot()
w.setColorNormal()
w.write("- detecting Ship Core: ")
local timeout = 10
local isAttached
repeat
isAttached = ship.isAttached()
w.sleep(0.05)
@ -80,6 +82,7 @@ function ship_boot()
w.setColorNormal()
w.write("- global position : ")
timeout = 10
local pos
repeat
pos = ship.position()
w.sleep(0.05)
@ -551,9 +554,6 @@ function ship_key_crew(character, keycode)
w.status_showWarning("Failed to summon crew member")
end
return true
elseif character == 'g' or character == 'G' then -- G
ship_page_jumpToGate()
return true
elseif keycode == 200 or keycode == 203 or character == '-' then -- Up or Left or -
ship_indexPlayer = ship_indexPlayer - 1
return true
@ -574,7 +574,7 @@ function ship_page_navigation()
local locationCurrent = "somewhere..." -- @TODO ship.getLocation()
w.writeLn("Current ship location : " .. locationCurrent)
w.writeLn("Jumpgates or beacons in range:")
local stringTargets, _ = "not implemented" -- ship.getTargets()
local stringTargets, _ = "not implemented", nil -- ship.getTargets()
if stringTargets == nil or stringTargets == "" then
stringTargets = "~ no beacon nor jumpgate in range ~"
end