What's with all the unnecessary 'while true do' loops in the setup portion? You break no matter which selection they choose, so why bother using the loop at all?
Also, you use probably the most atrocious indentation scheme I think I've ever seen. None of it makes any sense.
Okay, so this uses a slightly different file format than the one you had, but it significantly reduces the amount of space the loading and saving functions take up. Each line is 'type,color,status'. It's probably best to continue to not actually use any data from the file in the wizard and just overwrite everything. I made a few other changes and ended up dropping about eighty lines. Let me know if you have any questions or if it fails to run.
--Made by Craniumkid22
--This code is free to use, under a few conditions:
--1. DO NOT claim this as your own work!
--2. If you change this code, feel free to contact me on the CC forums, user-Craniumkid22.
--3. if you have questions about any of this code, feel free to contact me as well.
--screen functions
local function title()
term.clear()
term.setCursorPos(1,1)
print(" Welcome to Master Control initialization!")
print(" Please read each selection carefully.")
print(" If you mess up, press CTRL + T")
print(" And run the wizard again.")
term.setCursorPos(1,9)
end
--end screen functions
local function yN() --this code used with permission from the tutorials section of the CC forums.
local n=1
while true do
local x, y=term.getCursorPos()
term.clearLine()
if n==1 then write(">YES< NO") else write (" YES >NO<") end
term.setCursorPos(x, y)
a, b=os.pullEvent()
while a~="key" do
a, b=os.pullEvent()
end
if b==203 and n==2 then n=1 end
if b==205 and n==1 then n=2 end
if b==28 then print("") break end
end
if n==1 then return true end
if n==2 then return false end
return false
end
local function opt(m) --this code used with permission from the tutorials section of the CC forums.
n=1
l=#m
while true do
term.clear()
term.setCursorPos(1,2)
for i=1, l, 1 do
if i==n then print(i, " ["..m[i].."]") else print(i, " ", m[i]) end
end
a, b= os.pullEventRaw()
if a == "key" then
if b==200 and n>1 then n=n-1 end
if b==208 and n<=l then n=n+1 end
if b==28 then break end
end
end
term.clear() term.setCursorPos(1,1)
return n
end
local function saveState(saveTable)
local file = io.open("status", "w")
if file then
for eNum, eInfo in ipairs(saveTable) do
file:write(eInfo.type..","..eInfo.color..","..eInfo.status)
end
file:close()
end
end
local function loadState()
local loadTable = {}
local file = io.open("status", "r")
if file then
readLine = file:read("*l")
while readLine do
local lineTable = {}
lineTable.type, lineTable.color, lineTable.status = string.match(readLine, "(%a+),(%a+),(%a+)")
if lineTable.type then
table.insert(loadTable, lineTable)
end
readLine = file:read("*l")
end
file:close()
end
return loadTable
end
-- variables
local colorOpt={
"Redstone/Red alloy wire",
"White",
"Orange",
"Magenta",
"Light Blue",
"Yellow",
"Lime",
"Pink",
"Gray",
"Light Gray",
"Cyan",
"Purple",
"Blue",
"Brown",
"Green",
"Red",
"Black"
}
local eType={
"BatBox",
"MFE",
"MFSU"
}
local pType={
"Generator",
"Geothermal Generator",
"Water Mill",
"Wind Mill",
"Solar Panel",
"Nuclear Reactor",
"Redstone Engine",
"Steam Engine",
"Combustion Engine"
}
local controlTable = {}
--end variables
while true do
local adminTable = {type = "admin"}
local tempadmin, tempadmin2, tempusername, tempuser, tempuser2
local userTable = {type = "user"}
title()
while true do
print("Please enter Administrator name:")
adminTable.color = read()
if adminTable.color == "" then
print("Admin name cannot be blank")
sleep(1)
title()
else
break
end
end
title()
while true do
while true do
print("Please enter Administrator password:")
tempadmin = read("*")
if tempadmin == "" then
print("Password cannot be blank")
sleep(1)
title()
else
break
end
end
title()
while true do
print("Please re-enter password:")
tempadmin2 = read("*")
if tempadmin2 == "" then
print("Password cannot be blank")
sleep(1)
title()
else
break
end
end
title()
if tempadmin ~= tempadmin2 then
print("Passwords do not match. Please try again.")
sleep(1)
title()
elseif tempadmin == tempadmin2 then
adminTable.status = tempadmin
break
end
end
title()
print("Thank you. Admin profile created.")
sleep(1)
title()
while true do
print("Please enter User name:")
tempusername = read()
if tempusername == adminTable.color then
print("Cannot have User name the same as Administrator name.")
sleep(1)
title()
elseif tempusername == "" then
print("User name cannot be blank.")
sleep(1)
title()
else
userTable.color = tempusername
break
end
title()
end
title()
while true do
while true do
print("Please enter User password:")
tempuser = read("*")
if tempuser == "" then
print("Password cannot be blank.")
sleep(1)
title()
else
break
end
title()
end
while true do
title()
print("Pleas re-enter password:")
tempuser2 = read("*")
if tempuser2 == "" then
print("Password cannot be blank.")
sleep(1)
title()
else
break
end
end
title()
if tempuser ~= tempuser2 then
print("Passwords do not match. Please try again.")
sleep(1)
title()
elseif tempuser == tempuser2 then
userTable.status = tempuser
break
end
table.insert(controlTable, adminTable)
table.insert(controlTable, userTable)
end
title()
print("Thank you. User profile created.")
sleep(1)
title()
while true do
print("Do you have lighting that you want to control?")
if yN() then
local deviceTable = {type = "lights"}
term.clear()
term.setCursorPos(1,9)
print(" What color are you using?")
sleep(1.5)
deviceTable.color = tostring(opt(colorOpt))
table.insert(controlTable, deviceTable)
else
break
end
end
title()
print("What type of power are you using?")
sleep(2)
local powertype = tostring(opt(pType))
if powertype == "6" then
title()
print("Do you want this computer to control your reactor?")
if yN() then
local deviceTable = {type = "reactor"}
term.clear()
term.setCursorPos(1,9)
print(" What color are you using?")
sleep(1.5)
deviceTable.color = tostring(opt(colorOpt))
table.insert(controlTable, deviceTable)
print("Do you have an independant coolant system?")
if yN() then
local deviceTable = {type = "coolant"}
term.clear()
term.setCursorPos(1,9)
print(" What color are you using?")
sleep(1.5)
deviceTable.color = tostring(opt(colorOpt))
table.insert(controlTable, deviceTable)
end
end
else
local deviceTable = {type = tolower(string.match(pType[powertype], "^(%a+)"))}
term.clear()
term.setCursorPos(1,9)
print(" What color are you using?")
sleep(1.5)
deviceTable.color = tostring(opt(colorOpt))
table.insert(controlTable, deviceTable)
end
title()
print("What type of energy storage are you using?")
sleep(2)
local deviceTable = {type = "energy", color = tolower(eType[opt(eType)])}
table.insert(controlTable, deviceTable)
while true do
title()
print("Would you like to control another device?")
type = typeMenu() --create a type menu function to determine Macerator/Furnace/Compressor/Extractor/Cancel, returning nil on cancel.
if type == nil then break end
local deviceTable = {}
deviceTable.type = type
term.clear()
term.setCursorPos(1,9)
print(" What color are you using?")
sleep(1.5)
deviceTable.color = tostring(opt(colorOpt))
table.insert(controlTable, deviceTable)
end
end
local frun = true
saveState(controlTable)
end