Skip to content

Instantly share code, notes, and snippets.

@MyNameIsKodos
Forked from MalkContent/veinTracker.lua
Created September 19, 2016 23:58
Show Gist options
  • Save MyNameIsKodos/e63363c56982df08f26ab37087995148 to your computer and use it in GitHub Desktop.
Save MyNameIsKodos/e63363c56982df08f26ab37087995148 to your computer and use it in GitHub Desktop.
OpenComputers program to keep track of ImmersiveEngineering Mineral Deposits
local term = require("term")
local gpu = term.gpu()
local seri = require("serialization")
local p_set = "^set%s+(-?%d+)%s+(-?%d+)%s+(%d+)$"
local p_setStr = "^set%s+(-?%d+)%s+(-?%d+)%s+([%a%s]+)$"
local p_get = "^get%s+(-?%d+)%s+(-?%d+)$"
local p_quit = "^quit$"
local p_save = "^save$"
local p_load = "^load$"
local savName = "veins.sav"
local veinMap = {}
local veinNames = {"Bauxite", "Coal", "Copper", "Galena", "Gold", "Iron", "Lapis", "Lead", "Magnetite", "Nickel", "Platinum", "Pyrite", "Quarzite", "Silver", "Uranium"}
veinNames[0] = "No Mineral"
veinNames[-1] = "Unscanned"
local veinColors = {2, 12, 1, 3, 4, 8, 11, 3, 1, 13, 10, 14, 0, 3, 5}
veinColors[0] = 7
veinColors[-1] = 7
local function omSet(x, z, ore)
if veinMap[x] == nil and ore > 0 then
veinMap[x] = {}
end
if veinMap[x] ~= nil then
if ore > 0 then
veinMap[x][z] = ore
else
veinMap[x][z] = nil
if next(veinMap) == nil then
veinMap[x] = nil
end
end
end
end
local function omGet(x, z)
local veinType = 0
if veinMap[x] ~= nil and veinMap[x][z] ~= nil then
veinType = veinMap[x][z]
end
return veinType
end
local function squareChToBlk(x1, z1, x2, z2)
if x2 == nil then
x2 = x1
z2 = z1
else
if x1 > x2 then
local cx = x1
x1 = x2
x2 = cx
end
if z1 > z2 then
local cz = z1
z1 = z2
z2 = cz
end
end
return {16*x1, 16*z1, 15+16*x2, 15+16*z2}
end
local function loadFromFile()
local file = io.open(savName)
if file ~= nil then
local savVeins = file:read()
if savVeins ~= nil and savVeins ~= "" then
veinMap = seri.unserialize(savVeins)
end
file:close()
end
end
local function saveToFile()
local pre = savName .. ".pre"
do
local file = io.open(pre, "w")
file:write(seri.serialize(veinMap))
file:close()
end
local fsys = require("filesystem")
local path = fsys.path(require("process").info().path)
fsys.copy(path .. pre, path .. savName)
fsys.remove(path .. pre)
end
local function drawVeinInfo(x, z)
local tox = 4
local toy = 3
local squaresize = 9
local sqsh = 1 + squaresize//2
local mainfield = false
x = x - sqsh
z = z - sqsh
local vein
for cz = 1, squaresize do
for cx = 1, squaresize do
if cx == sqsh and cx == cz then
gpu.setBackground(9, true)
local mx = (cx - 1)*4 + tox
local mz = (cz - 1)*2 + toy
for i = 1, 3 do
term.setCursor(mx, mz + i)
term.write(" ")
end
mainfield = true
end
term.setCursor(cx*4 - 2 + tox, cz*2 + toy)
veinIndex = omGet(x + cx, z + cz)
gpu.setForeground(veinColors[veinIndex], true)
term.write((veinIndex > 0 and veinIndex or "?") .. " ")
if cx == sqsh and cx == cz then
gpu.setBackground(15, true)
mainfield = false
end
end
end
term.setCursor(1, 2 + toy + 2 * squaresize)
local borders = squareChToBlk(x+1, z+1, x+squaresize, z+squaresize)
gpu.setForeground(0, true)
print("Area drawn: " .. borders[1] .. ", " .. borders[2] .. " to " .. borders[3] .. ", " .. borders[4])
end
local run = true
local saved = true
local function parse(instr)
local results = {string.match(instr, p_get)}
if results[1] == nil then
results = {string.match(instr, p_set)}
end
if results[1] == nil then
results = {string.match(instr, p_setStr)}
local oreName = results[3]
if oreName ~= nil then
oreName = string.lower(oreName)
local nomatch = true
for i = -1, #veinNames do
if oreName == string.lower(veinNames[i]) then
results[3] = i
nomatch = false
break
end
end
if nomatch then
print("Invalid Input")
return
end
end
end
if results[1] ~= nil then
local x, z = tonumber(results[1])//16, tonumber(results[2])//16
if results[3] ~= nil then
omSet(x, z, tonumber(results[3]))
saved = false
end
local borders = squareChToBlk(x, z)
term.write(borders[1] .. ", " .. borders[2] .. " to " .. borders[3] .. ", " .. borders[4] .. ": ")
do
local veinIndx = omGet(x, z)
gpu.setForeground(veinColors[veinIndx], true)
term.write(veinNames[veinIndx] .. " ")
gpu.setForeground(0, true)
end
drawVeinInfo(x, z)
return
end
results = {string.match(instr, p_quit)}
if results[1] ~= nil then
if saved == false then
term.write("Unsaved changes. Save before quitting (Y/n): ")
local c, r = term.getCursor()
local yn = string.lower(io.read())
local width = gpu.getResolution() - c
while yn ~= "y" and yn ~= "n" do
gpu.fill(c, r, width, 1, " ")
term.setCursor(c, r)
yn = string.lower(io.read())
end
if yn == "y" then
saveToFile()
end
end
run = false
return
end
results = {string.match(instr, p_save)}
if results[1] ~= nil then
saveToFile()
saved = true
print("Saved to File")
return
end
results = {string.match(instr, p_load)}
if results[1] ~= nil then
loadFromFile()
saved = true
print("Loaded from File")
return
end
print("Invalid Input")
end
local function getInput()
term.setCursor(1,2)
local ins = io.read()
term.setCursor(1,2)
term.clearLine()
term.setCursor(1,3)
term.clearLine()
return ins
end
loadFromFile()
term.clear()
print("Veintracker ™")
do
local tox = 60
local toy = 4
local numStr
for i = -1, #veinNames do
numStr = i .. (i<0 and "/?" or "")
term.setCursor(tox - string.len(numStr), toy + i)
term.write(numStr .. ": ")
gpu.setForeground(veinColors[i], true)
term.write(veinNames[i])
gpu.setForeground(0, true)
end
end
while run do
local ins = getInput()
parse(ins)
end
term.clear()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment