This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
kentar11's profile picture

Multi menu

Started by kentar11, 09 September 2013 - 10:16 AM
kentar11 #1
Posted 09 September 2013 - 12:16 PM
Topic Title : Multi menu

I am trying to program a multiple menu to control things with bundled wires

Like I want it start off saying

type cows for cow farm
type wheat for wheat farm
type fish for fish farm

then I want it to say for each on

on for farm on
off for farm off

this is also going through bundled cable to colors

I don't know how to make it print different menus to pop up and output the current to the color cable.
bigbaddevil6 #2
Posted 10 September 2013 - 05:32 AM
As for a complete walk through i cannot help but i can redirect you to a code I had some people from the forums help me write. Ill try to comment it as best as i can to explain it

Here is the pastebin http://pastebin.com/ugMpcHfL
Below is the code
Spoiler

-- [[ Variables ]] --
-- Files
s_stateDir = "states/"
s_stateDoors = ".doors.txt"
s_stateLights = ".lights.txt"
s_stateShields = ".shields.txt"
s_statePumps = ".pumps.txt"
s_stateRefinery = ".refinery.txt"
s_statePowerProduction = ".powerProduction.txt"
s_stateNuclearReactor = ".nuclearReactor.txt"
s_statePeatFarm = ".peatFarm.txt"
s_stateTreeFarm = ".treeFarm.txt"
s_stateRubberTreeFarm = ".rubberTreeFarm.txt"
s_stateCactusFarm = ".cactusFarm.txt"
s_statePumpkinFarm = ".pumpkinFarm.txt"
s_stateReedFarm = ".reedFarm.txt"
s_stateStill = ".still.txt"
s_stateBioGenerator = ".bioGenerator.txt"
-- Booleans
b_stateDoors = false
b_stateLights = false
b_stateShields = false
b_statePumps = false
b_stateRefinery = false
b_statePowerProduction = false
b_stateNuclearReactor = false
b_statePeatFarm = false
b_stateTreeFarm = false
b_stateRubberTreeFarm = false
b_stateCactusFarm = false
b_statePumpkinFarm = false
b_stateReedFarm = false
b_stateStill = false
b_stateBioGenerator = false
running = true

-- Tables
local t_mainOptions = {
"Lights",
"Doors",
"Shields",
"Options: Admins Only",
"Buildcraft Machines",
"Industrial Machines",
"Forestry Machines",
"Exit"
}
local t_lightsOptions = {
"On",
"Off",
"Back"
}
local t_doorsOptions = {
"Open",
"Close",
"Back"
}
local t_shieldsOptions = {
"On",
"Off",
"Back"
}
local t_buildcraftMachinesOptions = {
"Pumps",
"Refinery",
"Back"
}
local t_industrialCraftMachinesOptions = {
"Nuclear Reactor",
"Power Production",
"Back"
}
local t_forestryMachinesOptions = {
"Peat Farm",
"Tree Farm",
"Rubber Tree Farm",
"Cactus Farm",
"Pumpkin Farm",
"Reed Farm",
"Still",
"Bio Generator",
"Back"
}
local t_subOptions_OnOffBack = {
"On",
"Off",
"Back"
}

-- Colours
white = colours.white
blue = colours.blue
green = colours.green
lime = colours.lime
lightBlue = colours.lightBlue
red = colours.red
black = colours.black
yellow = colours.yellow
-- [[ End of Variables ]] --
-- [[ Functions ]] --
-- Colour write
local function colorW(...)
  local curColor
  for i=1, #arg do -- arg is ...
	if type(arg[i]) == 'number' then
	  curColor = arg[i]
	else
	  if curColor then
		term.setTextColor(curColor)
	  end
	  write(arg[i])
	end
  end
  term.setTextColour(colours.white)
  print() -- this is a print function, so it needs a new line.
end
function clear()
term.setCursorPos(1,1)
term.clear()
end


-- Menu Function
function CUI(m)
selected = 1
while true do
  term.setCursorPos(1,3)
  for i = 1, #m do
   if i == selected then
	colorW(lightBlue, " >", blue, m[i], lightBlue, "< ")
   else
	colorW(white, "  ", red, m[i], "  ")
   end
  end
  event, key = os.pullEventRaw("key")
  if key == 200 then selected=selected-1
  elseif key == 208 then selected=selected+1
  elseif key == 28 then break end
  if selected < 1 then selected = #m
  elseif selected > #m then selected = 1 end
end
clear()
return m[selected], selected
end


-- Admin password function
function password()
term.clear()
term.setCursorPos(1,1)
term.write("Admin Enter Password: ")
password = read("*")
if password == "placeholder" then
  print("Correct")
else
  print("Incorrect") sleep(2)
end
end


-- Write function
function writeState(fileName, bState)
s_Path = fs.combine(s_stateDir, fileName)
	file = fs.open(s_Path, "w")
	file.writeLine(bState)
	file.close()
end


-- Read Function
function readState(fileName)
s_Path = fs.combine(s_stateDir, fileName)
if not fs.exists(s_Path) then
  file = fs.open(s_Path, "w")
  file.writeLine("false")
  file.close()
  return false
end
file = fs.open(s_Path, "r")
s_tState = file.readLine()
file.close()
if s_tState == nil then return false
elseif s_tState == "true" then return true
elseif s_tState == "false" then return false end
end
-- [[ End of Functions ]] --

--[[Executed Commands]]--
-- Reading states
if not fs.isDir(s_stateDir) then fs.makeDir(s_stateDir) end
b_stateDoors = readState(s_stateDoors)
b_stateLights = readState(s_stateLights)
b_stateShields = readState(s_stateShields)
b_statePumps = readState(s_statePumps)
b_stateRefinery = readState(s_stateRefinery)
b_statePowerProduction = readState(s_statePowerProduction)
b_stateNuclearReactor = readState(s_stateNuclearReactor)
b_statePeatFarm = readState(s_statePeatFarm)
b_stateTreeFarm = readState(s_stateTreeFarm)
b_stateRubberTreeFarm = readState(s_stateRubberTreeFarm)
b_stateCactusFarm = readState(s_stateCactusFarm)
b_statePumpkinFarm = readState(s_statePumpkinFarm)
b_stateReedFarm = readState(s_stateReedFarm)
b_stateStill = readState(s_stateStill)
b_stateBioGenerator = readState(s_stateBioGenerator)
s_mainState = "mainOptions"
while running do
--[[ Main Options ]]--
if s_mainState == "mainOptions" then
  clear()
  colorW(green, "Devil Inc. Softwares")
  s_mainState = CUI(t_mainOptions)
--[[ Lights ]]--
elseif s_mainState == "Lights" then
  clear()
  colorW(yellow, "Lights: ", b_stateLights and lime or red, b_stateLights and "On" or "Off")
  s, n = CUI(t_lightsOptions)
  if n == 1 and not b_stateLights then b_stateLights = true
  elseif n == 2 and b_stateLights then b_stateLights = false
  elseif n == 3 then s_mainState = "mainOptions" end
  writeState(s_stateLights, b_stateLights)
--[[ Doors ]]--
elseif s_mainState == "Doors" then
  clear()
  colorW(yellow, "Doors: ", b_stateDoors and lime or red, b_stateDoors and "Open" or "Closed")
  s, n = CUI(t_doorsOptions)
  if n == 1 and not b_stateDoors then b_stateDoors = true
  elseif n == 2 and b_stateDoors then b_stateDoors = false
  elseif n == 3 then s_mainState = "mainOptions" end
  writeState(s_stateDoors, b_stateDoors)
--[[ Shields ]]--
elseif s_mainState == "Shields" then
  clear()
  colorW(yellow, "Shields: ", b_stateShields and lime or red, b_stateShields and "On" or "Off")
  s, n = CUI(t_shieldsOptions)
  if n == 1 and not b_stateShields then b_stateShields = true
  elseif n == 2 and b_stateShields then b_stateShields = false
  elseif n == 3 then s_mainState = "mainOptions" end
  writeState(s_stateShields, b_stateShields)
--[[ Password ]] --
elseif s_mainState == "Options: Admins Only" then
  password()
  s_mainState = "mainOptions"
--[[ Buildcraft Machines ]]--
elseif s_mainState == "Buildcraft Machines" then
  clear()
  colorW(yellow, "BuildCraft Machinery")
  s_subOption, n = CUI(t_buildcraftMachinesOptions)
  if s_subOption == "Pumps" then
   while true do
	clear()
	colorW(yellow, "Pumps: ", b_statePumps and lime or red, b_statePumps and "On" or "Off")
	s, n = CUI(t_subOptions_OnOffBack)
	if n == 1 and not b_statePumps then b_statePumps = true
	elseif n == 2 and b_statePumps then b_statePumps = false
	elseif n == 3 then break end
	writeState(s_statePumps, b_statePumps)
   end
  elseif s_subOption == "Refinery" then
   while true do
	clear()
	colorW(yellow, "Refinery: ", b_stateRefinery and lime or red, b_stateRefinery and "On" or "Off")
	s, n = CUI(t_subOptions_OnOffBack)
	if n == 1 and not b_stateRefinery then b_stateRefinery = true
	elseif n == 2 and b_stateRefinery then b_stateRefinery = false
	elseif n == 3 then break end
	writeState(s_stateRefinery, b_stateRefinery)
   end
  elseif s_subOption == "Back" then s_mainState = "mainOptions" end

--[[ Industrialcraft Nuclear Reactor ]]--
elseif s_mainState == "Industrial Machines" then
  clear()
  colorW(yellow, "IndustrialCraft Machinery")
  s_subOption, n = CUI(t_industrialCraftMachinesOptions)
  if s_subOption == "Nuclear Reactor" then
   while true do
	clear()
	colorW(yellow, "Nuclear Reactor: ", b_stateNuclearReactor and lime or red, b_stateNuclearReactor and "On" or "Off")
	s, n = CUI(t_subOptions_OnOffBack)
	if n == 1 and not b_stateNuclearReactor then b_stateNuclearReactor = true
	elseif n == 2 and b_stateNuclearReactor then b_stateNuclearReactor = false
	elseif n == 3 then break end
	writeState(s_stateNuclearReactor, b_stateNuclearReactor)
   end
  elseif s_subOption == "Power Production" then
   while true do
	clear()
	colorW(yellow, "Power Production: ", b_statePowerProduction and lime or red, b_statePowerProduction and "On" or "Off")
	s, n = CUI(t_subOptions_OnOffBack)
	if n == 1 and not b_statePowerProduction then b_statePowerProduction = true
	elseif n == 2 and b_statePowerProduction then b_statePowerProduction = false
	elseif n == 3 then break end
	writeState(s_statePowerProduction, b_statePowerProduction)
   end
  elseif s_subOption == "Back" then s_mainState = "mainOptions" end
--[[ Peat Farm ]]--
elseif s_mainState == "Forestry Machines" then
  clear()
  colorW(yellow, "Forestry Machinery")
  s_subOption, n = CUI(t_forestryMachinesOptions)
  if s_subOption == "Peat Farm" then
   while true do
	clear()
	colorW(yellow, "Peat Farm: ", b_statePeatFarm and lime or red, b_statePeatFarm and "On" or "Off")
	s, n = CUI(t_subOptions_OnOffBack)
	if n == 1 and not b_statePeatFarm then b_statePeatFarm = true
	elseif n == 2 and b_statePeatFarm then b_statePeatFarm = false
	elseif n == 3 then break end
	writeState(s_statePeatFarm, b_statePeatFarm)
   end
  elseif s_subOption == "Tree Farm" then
   while true do
	clear()
	colorW(yellow, "Tree Farm: ", b_stateTreeFarm and lime or red, b_stateTreeFarm and "On" or "Off")
	s, n = CUI(t_subOptions_OnOffBack)
	if n == 1 and not b_stateTreeFarm then b_stateTreeFarm = true
	elseif n == 2 and b_stateTreeFarm then b_stateTreeFarm = false
	elseif n == 3 then break end
	writeState(s_stateTreeFarm, b_stateTreeFarm)
   end
  elseif s_subOption == "Rubber Tree Farm" then
   while true do
	clear()
	colorW(yellow, "Rubber Tree Farm: ", b_stateRubberTreeFarm and lime or red, b_stateRubberTreeFarm and "On" or "Off")
	s, n = CUI(t_subOptions_OnOffBack)
	if n == 1 and not b_stateRubberTreeFarm then b_stateRubberTreeFarm = true
	elseif n == 2 and b_stateRubberTreeFarm then b_stateRubberTreeFarm = false
	elseif n == 3 then break end
	writeState(s_stateRubberTreeFarm, b_stateRubberTreeFarm)
   end
  elseif s_subOption == "Cactus Farm" then
   while true do
	clear()
	colorW(yellow, "Cactus Farm: ", b_stateCactusFarm and lime or red, b_stateCactusFarm and "On" or "Off")
	s, n = CUI(t_subOptions_OnOffBack)
	if n == 1 and not b_stateCactusFarm then b_stateCactusFarm = true
	elseif n == 2 and b_stateCactusFarm then b_stateCactusFarm = false
	elseif n == 3 then break end
	writeState(s_stateCactusFarm, b_stateCactusFarm)
   end
  elseif s_subOption == "Pumpkin Farm" then
   while true do
	clear()
	colorW(yellow, "Pumpkin Farm: ", b_statePumpkinFarm and lime or red, b_statePumpkinFarm and "On" or "Off")
	s, n = CUI(t_subOptions_OnOffBack)
	if n == 1 and not b_statePumpkinFarm then b_statePumpkinFarm = true
	elseif n == 2 and b_statePumpkinFarm then b_statePumpkinFarm = false
	elseif n == 3 then break end
	writeState(s_statePumpkinFarm, b_statePumpkinFarm)
   end
  elseif s_subOption == "Reed Farm" then
   while true do
	clear()
	colorW(yellow, "Reed Farm: ", b_stateReedFarm and lime or red, b_stateReedFarm and "On" or "Off")
	s, n = CUI(t_subOptions_OnOffBack)
	if n == 1 and not b_stateReedFarm then b_stateReedFarm = true
	elseif n == 2 and b_stateReedFarm then b_stateReedFarm = false
	elseif n == 3 then break end
	writeState(s_stateReedFarm, b_stateReedFarm)
   end
  elseif s_subOption == "Still" then
   while true do
	clear()
	colorW(yellow, "Still: ", b_stateStill and lime or red, b_stateStill and "On" or "Off")
	s, n = CUI(t_subOptions_OnOffBack)
	if n == 1 and not b_stateStill then b_stateStill = true
	elseif n == 2 and b_stateStill then b_stateStill = false
	elseif n == 3 then break end
	writeState(s_stateStill, b_stateStill)
   end
  elseif s_subOption == "Bio Generator" then
   while true do
	clear()
	colorW(yellow, "Bio Generator: ", b_stateBioGenerator and lime or red, b_stateBioGenerator and "On" or "Off")
	s, n = CUI(t_subOptions_OnOffBack)
	if n == 1 and not b_stateBioGenerator then b_stateBioGenerator = true
	elseif n == 2 and b_stateBioGenerator then b_stateBioGenerator = false
	elseif n == 3 then break end
	writeState(s_stateBioGenerator, b_stateBioGenerator)
   end
  elseif s_subOption == "Back" then s_mainState = "mainOptions" end

--[[ Exit ]]--
elseif s_mainState == "Exit" then
  running = false
end
end

Now, I know this seems like a lot but what this does is allow you to use the arrow keys to navigate through the menus and press enter to select a menu, there are sub menus so that you would select "Forestry Machines" after that you would select "Tree Farm" and then finally say "on", "off", or "back" to go back a menu. It saves the states to files so during a server crash/restart or a reload of a SSP world. It will remember where it left off without needs to turn everything on again. This may be more that what your looking for but was one I liked to used. Ill break it down into sections to try to explain it easier,



This, is state directories this is where all the states of the menu options get saved to be called upon again. So as you see the state for Doors which is represented as "s_stateDoors" gets saved to ".doors.txt" which is a file that is made in the ROM of the computer.
Spoiler

-- [[ Variables ]] --
-- Files
s_stateDir = "states/"
s_stateDoors = ".doors.txt"
s_stateLights = ".lights.txt"
s_stateShields = ".shields.txt"
s_statePumps = ".pumps.txt"
s_stateRefinery = ".refinery.txt"
s_statePowerProduction = ".powerProduction.txt"
s_stateNuclearReactor = ".nuclearReactor.txt"
s_statePeatFarm = ".peatFarm.txt"
s_stateTreeFarm = ".treeFarm.txt"
s_stateRubberTreeFarm = ".rubberTreeFarm.txt"
s_stateCactusFarm = ".cactusFarm.txt"
s_statePumpkinFarm = ".pumpkinFarm.txt"
s_stateReedFarm = ".reedFarm.txt"
s_stateStill = ".still.txt"
s_stateBioGenerator = ".bioGenerator.txt"


next we have the booleans all this is, is a beginning state for everything when the program very first starts up for the first time. After that the states will change according to if you made the items on or off
Spoiler

-- Booleans
b_stateDoors = false
b_stateLights = false
b_stateShields = false
b_statePumps = false
b_stateRefinery = false
b_statePowerProduction = false
b_stateNuclearReactor = false
b_statePeatFarm = false
b_stateTreeFarm = false
b_stateRubberTreeFarm = false
b_stateCactusFarm = false
b_statePumpkinFarm = false
b_stateReedFarm = false
b_stateStill = false
b_stateBioGenerator = false
running = true
[/SPOLIER]

Now we have the tables which has all the menus and options in them. "t_mainOptions" is the very first menu you will see. The others such as "t_lightsOptions" is a sub-menu that allows you to turn it On, Off, or go back a menu.
Spoiler

-- Tables
local t_mainOptions = {
"Lights",
"Doors",
"Shields",
"Options: Admins Only",
"Buildcraft Machines",
"Industrial Machines",
"Forestry Machines",
"Exit"
}
local t_lightsOptions = {
"On",
"Off",
"Back"
}
local t_doorsOptions = {
"Open",
"Close",
"Back"
}
local t_shieldsOptions = {
"On",
"Off",
"Back"
}
local t_buildcraftMachinesOptions = {
"Pumps",
"Refinery",
"Back"
}
local t_industrialCraftMachinesOptions = {
"Nuclear Reactor",
"Power Production",
"Back"
}
local t_forestryMachinesOptions = {
"Peat Farm",
"Tree Farm",
"Rubber Tree Farm",
"Cactus Farm",
"Pumpkin Farm",
"Reed Farm",
"Still",
"Bio Generator",
"Back"
}
local t_subOptions_OnOffBack = {
"On",
"Off",
"Back"
}

The next section mainly just for looks it makes it easy to make text and menus with different colors. I wish I could explain this to you more in depth but the person I got it from couldn't even explain it to me. I'm sure a smarter mind than mine can explain how it works if you ask on here.
Spoiler

-- Colours
white = colours.white
blue = colours.blue
green = colours.green
lime = colours.lime
lightBlue = colours.lightBlue
red = colours.red
black = colours.black
yellow = colours.yellow
-- [[ End of Variables ]] --

-- [[ Functions ]] --
-- Colour write
local function colorW(...)
  local curColor
  for i=1, #arg do -- arg is ...
	if type(arg[i]) == 'number' then
	  curColor = arg[i]
	else
	  if curColor then
		term.setTextColor(curColor)
	  end
	  write(arg[i])
	end
  end
  term.setTextColour(colours.white)
  print() -- this is a print function, so it needs a new line.
end
function clear()
term.setCursorPos(1,1)
term.clear()
end
I do know how to use it so lets put an example to see what it will look like.

colorW(green, " >", blue, "Example", green, "< ")

so to use it of course you will need to call the function which is "colorW()" the function allows arguments to be passed into it. the "green" will make the ">" appear green and anything else after that until it is told to change the color. So we see here we say blue. That will make the "Example" appear blue. finally we say "green" which again will make the "<" appear green. So something like this will show up >Example< when it is printed to the screen. Again this is mainly for appearance to make it look pretty.


This is what does the redstone currents for bundled cables
Spoiler

--bundle on
function addOutput(side, color) -- able to pass what side and color, if you had a black bundled cable on the right side of the computer you would put addOutput("right", colors.black)
  c = colors.combine(rs.getBundledInput(side), colours[color]) -- this combines it so that the colors can be turned on one at a time
  rs.setBundledOutput(side, c) this turns is what actually turns on the cable
end

--bundle off
-- this works exactly the same but takes away one color at a time therefore turning that color off
function removeOutput(side, color)
  c = colors.subtract(rs.getBundledInput(side), colours[color])
  rs.setBundledOutput(side, c)
end



This next part is where the work starts happening. Below is the code that makes the menu scroll-able and be able for you to press enter to select the menu.
Spoiler

-- Menu Function
function CUI(m) -- this m here goes with the for i = 1, #m do, its so for when you say CUI(t_mainOptions) it uses the table back up at the top of the code
selected = 1 -- this is so that when it starts up the first option is always selected if ou wanted to exapnd this you could so it remembers your last selection with file saving
while true do
  term.setCursorPos(1,3)
  for i = 1, #m do -- this is a standard for loop but the #m gets the length of the table you called so that it knows how many options there is
   if i == selected then
	colorW(lightBlue, " >", blue, m[i], lightBlue, "< ") -- here is one of the colorW() I explained earlier that makes it so the option u have selected has > < arround it
   else
	colorW(white, "  ", red, m[i], "  ")  -- this makes it so that when it isnt selected it doenst have the > < around it
   end
  end
  event, key = os.pullEventRaw("key") -- this is an os.pullEvent that checks for key presses
  if key == 200 then selected=selected-1 -- the number 200 represents the up arrow so if the up arrow was pressed it will move the selection up 1
  elseif key == 208 then selected=selected+1 -- 208 represents the down arrow and move the selection down 1
  elseif key == 28 then break end -- 28 represents the Enter key and will break the While loop at the beggining at this block of code
  if selected < 1 then selected = #m -- these lines here are so that when u go beyond the avalible numbers will will wrap arround so when u hit the bottom and press down it will jumo to the very top same goes for the other way
  elseif selected > #m then selected = 1 end
end
clear() -- this just clears the menu so that the one selected can be displayed without messing up anything
return m[selected], selected -- this returns the selected menu option to be passed to the next part of code
end

this is just a simple password code the current pass is "placeholder" because I never set one and as far as I recall never used it. Just there if I ever wanted to make a password protected menu.
Spoiler

-- Admin password function
function password()
term.clear()
term.setCursorPos(1,1)
term.write("Admin Enter Password: ")
password = read("*")
if password == "placeholder" then
  print("Correct")
else
  print("Incorrect") sleep(2)
end
end

This is the code that writes the state of whatever option you picked to a file that was determined above at the beginning of this code.
[SPOLIER]

-- Write function
function writeState(fileName, bState) -- the function allows the file name and the Boolean state to be passed into it
s_Path = fs.combine(s_stateDir, fileName) -- this combines it where the states of all menus will be stored into one file, in this case its "states/" that stores all other files in it
	file = fs.open(s_Path, "w") -- this opens the file to be written so that the new state can overwrite the old one once changed
	file.writeLine(bState) -- this writes the state into the file
	file.close() -- this closes the file which is very important always close the files afterwards
end

Of course we need a function to be able to read everything we are saving. This is the code to do it
Spoiler

-- Read Function
function readState(fileName) -- this allows the file name to be passed into it so it know what file to read
s_Path = fs.combine(s_stateDir, fileName)
if not fs.exists(s_Path) then -- this checks to make sure the file exists in the rom. if it doesn't it will make one open it up and assign a false boolean to it
  file = fs.open(s_Path, "w")
  file.writeLine("false")
  file.close()
  return false -- this returns that the file has a false boolean meaning that it is off
end
file = fs.open(s_Path, "r") -- this opens the file to be read
s_tState = file.readLine() -- this reads the line that is inside the file
file.close() -- again closing very important
if s_tState == nil then return false -- this if for some reason the file didnt return true or false it automaticaly returns false
elseif s_tState == "true" then return true -- if the boolean in it was true then it returns true meaning that it is currently on
elseif s_tState == "false" then return false end if the boolean in it was false then it returns false meaning it is off
end
This here assigns all the b_states or boolean states to the readStates which is the function just above here so when u have b_stateLights it will pass the information to the function above telling it that it needs to get the information on the lights seeing if they are on or off.
Spoiler

--[[Executed Commands]]--
-- Reading states
if not fs.isDir(s_stateDir) then fs.makeDir(s_stateDir) end
b_stateDoors = readState(s_stateDoors)
b_stateLights = readState(s_stateLights)
b_stateShields = readState(s_stateShields)
b_statePumps = readState(s_statePumps)
b_stateRefinery = readState(s_stateRefinery)
b_statePowerProduction = readState(s_statePowerProduction)
b_stateNuclearReactor = readState(s_stateNuclearReactor)
b_statePeatFarm = readState(s_statePeatFarm)
b_stateTreeFarm = readState(s_stateTreeFarm)
b_stateRubberTreeFarm = readState(s_stateRubberTreeFarm)
b_stateCactusFarm = readState(s_stateCactusFarm)
b_statePumpkinFarm = readState(s_statePumpkinFarm)
b_stateReedFarm = readState(s_stateReedFarm)
b_stateStill = readState(s_stateStill)
b_stateBioGenerator = readState(s_stateBioGenerator)
this next part is checks the redstone currents based on the booleans of the stated since all booleans are false when it starts up for the very first time all redstone output will be false as well
Spoiler

-- Setting redstone currents according to the above booleans
if b_stateDoors then addOutput("back", colors.white) end
if b_stateLights then addOutput("back", colors.orange) end
if b_stateShields then addOutput("back", colors.magenta) end
if b_statePumps then addOutput("back", colors.lightBlue) end
if b_stateRefinery then addOutput("back", colors.yellow) end
if b_statePowerProduction then addOutput("back", colors.pink) end
if b_stateNuclearReactor then addOutput("back", colors.lime) end
if b_statePeatFarm then addOutput("back", colors.gray) end
if b_stateTreeFarm then addOutput("back", colors.lightGray) end
if b_stateRubberTreeFarm then addOutput("back", colors.cyan) end
if b_stateCactusFarm then addOutput("back", colors.purple) end
if b_statePumpkinFarm then addOutput("back", colors.blue) end
if b_stateReedFarm then addOutput("back", colors.brown) end
if b_stateStill then addOutput("back", colors.green) end
if b_stateBioGenerator then addOutput("back", colors.red) end
The little line here is a default starting point for the menu which in this case is the Main Options Menu.

s_mainState = "mainOptions"

Now the biggest part of the code is this. This allows it to navigate in between menus and use all the functions we made before this. It will call for what files will need to be read of written. I am only going to comment the top section because it is literally the same thing over and over again using elseif statements just it has different variable names its looking for.
Spoiler

while running do
		--[[ Main Options ]]--
		if s_mainState == "mainOptions" then -- this check to see what menu is selected so this one is check if the main options was selected if not checks the next one if so does the following code
				clear() -- clears the screen for the menu to appear
				colorW(green, "Devil Inc. Softwares") -- this is just a header line using the colorW() this one makes it where the string is green
				s_mainState = CUI(t_mainOptions) -- remember when i explained the menu function or CUI() of passing the tables to the menu? This is it.
		--[[ Lights ]]--
		elseif s_mainState == "Lights" then -- this here check to see if the lights were selected as a menu from the main options
				clear() -- again clears the screen so it can show the new one
				colorW(yellow, "Lights: ", b_stateLights and lime or red, b_stateLights and "On" or "Off") -- this puts a line at the top of the screen showing the status of the lights if they are on is displays "On" in green if they are off it displayes "Off" in red
				s, n = CUI(t_lightsOptions) -- again this refers ti the menu function but now its showing the lights table
				if n == 1 and not b_stateLights then b_stateLights = true addOutput("back", colors.white) -- this one represents the "On" option if you hit enter here it will set the redstone output to on jump down to the writeState() and updates the boolean value for the lights
				elseif n == 2 and b_stateLights then b_stateLights = false removeOutput("back", colors.white) -- this one represents the "Off" option if you hit enter here it will set the redstone output to off jump down to the writeState() and updates the boolean value for the lights
				elseif n == 3 then s_mainState = "mainOptions" end -- this represents the "back" option if you hit enter here all it does is take you back a menu
				writeState(s_stateLights, b_stateLights) -- this is what updates the file writing the boolean state of the lights to the lights file

--Everything from here on is the same pretty much. The difference is that the menus such as forestry machine have the sub menu and the the options after that but still exact same concept. Please jump to the bottom to finish this long explination.

		--[[ Doors ]]--
		elseif s_mainState == "Doors" then
				clear()
				colorW(yellow, "Doors: ", b_stateDoors and lime or red, b_stateDoors and "Open" or "Closed")
				s, n = CUI(t_doorsOptions)
				if n == 1 and not b_stateDoors then b_stateDoors = true addOutput("back", colors.orange)
				elseif n == 2 and b_stateDoors then b_stateDoors = false removeOutput("back", colors.orange)
				elseif n == 3 then s_mainState = "mainOptions" end
				writeState(s_stateDoors, b_stateDoors)

		--[[ Shields ]]--
		elseif s_mainState == "Shields" then
				clear()
				colorW(yellow, "Shields: ", b_stateShields and lime or red, b_stateShields and "On" or "Off")
				s, n = CUI(t_shieldsOptions)
				if n == 1 and not b_stateShields then b_stateShields = true addOutput("back", colors.magenta)
				elseif n == 2 and b_stateShields then b_stateShields = false removeOutput("back", colors.magenta)
				elseif n == 3 then s_mainState = "mainOptions" end
				writeState(s_stateShields, b_stateShields)

		--[[ Buildcraft Machines ]]--
		elseif s_mainState == "Buildcraft Machines" then
				clear()
				colorW(yellow, "BuildCraft Machinery")
				s_subOption, n = CUI(t_buildcraftMachinesOptions)
				if s_subOption == "Pumps" then
						while true do
								clear()
								colorW(yellow, "Pumps: ", b_statePumps and lime or red, b_statePumps and "On" or "Off")
								s, n = CUI(t_subOptions_OnOffBack)
								if n == 1 and not b_statePumps then b_statePumps = true addOutput("back", colors.lightBlue)
								elseif n == 2 and b_statePumps then b_statePumps = false removeOutput("back", colors.lightBlue)
								elseif n == 3 then break end
								writeState(s_statePumps, b_statePumps)
						end
				elseif s_subOption == "Refinery" then
						while true do
								clear()
								colorW(yellow, "Refinery: ", b_stateRefinery and lime or red, b_stateRefinery and "On" or "Off")
								s, n = CUI(t_subOptions_OnOffBack)
								if n == 1 and not b_stateRefinery then b_stateRefinery = true addOutput("back", colors.yellow)
								elseif n == 2 and b_stateRefinery then b_stateRefinery = false removeOutput("back", colors.yellow)
								elseif n == 3 then break end
								writeState(s_stateRefinery, b_stateRefinery)
						end
				elseif s_subOption == "Back" then s_mainState = "mainOptions" end
			  
		--[[ Industrialcraft Nuclear Reactor ]]--
		elseif s_mainState == "Industrial Machines" then
				clear()
				colorW(yellow, "IndustrialCraft Machinery")
				s_subOption, n = CUI(t_industrialCraftMachinesOptions)
				if s_subOption == "Nuclear Reactor" then
						while true do
								clear()
								colorW(yellow, "Nuclear Reactor: ", b_stateNuclearReactor and lime or red, b_stateNuclearReactor and "On" or "Off")
								s, n = CUI(t_subOptions_OnOffBack)
								if n == 1 and not b_stateNuclearReactor then b_stateNuclearReactor = true addOutput("back", colors.lime)
								elseif n == 2 and b_stateNuclearReactor then b_stateNuclearReactor = false removedOutput("back", colors.lime)
								elseif n == 3 then break end
								writeState(s_stateNuclearReactor, b_stateNuclearReactor)
						end
				elseif s_subOption == "Power Production" then
						while true do
								clear()
								colorW(yellow, "Power Production: ", b_statePowerProduction and lime or red, b_statePowerProduction and "On" or "Off")
								s, n = CUI(t_subOptions_OnOffBack)
								if n == 1 and not b_statePowerProduction then b_statePowerProduction = true addOutput("back", colors.pink)
								elseif n == 2 and b_statePowerProduction then b_statePowerProduction = false removeOutput("back", colors.pink)
								elseif n == 3 then break end
								writeState(s_statePowerProduction, b_statePowerProduction)
						end
				elseif s_subOption == "Back" then s_mainState = "mainOptions" end

		--[[ Peat Farm ]]--
		elseif s_mainState == "Forestry Machines" then
				clear()
				colorW(yellow, "Forestry Machinery")
				s_subOption, n = CUI(t_forestryMachinesOptions)
				if s_subOption == "Peat Farm" then
						while true do
								clear()
								colorW(yellow, "Peat Farm: ", b_statePeatFarm and lime or red, b_statePeatFarm and "On" or "Off")
								s, n = CUI(t_subOptions_OnOffBack)
								if n == 1 and not b_statePeatFarm then b_statePeatFarm = true addOutput("back", colors.gray)
								elseif n == 2 and b_statePeatFarm then b_statePeatFarm = false removeOutput("back", colors.gray)
								elseif n == 3 then break end
								writeState(s_statePeatFarm, b_statePeatFarm)
						end
				elseif s_subOption == "Tree Farm" then
						while true do
								clear()
								colorW(yellow, "Tree Farm: ", b_stateTreeFarm and lime or red, b_stateTreeFarm and "On" or "Off")
								s, n = CUI(t_subOptions_OnOffBack)
								if n == 1 and not b_stateTreeFarm then b_stateTreeFarm = true addOutput("back", colors.lightGray)
								elseif n == 2 and b_stateTreeFarm then b_stateTreeFarm = false removeOutput("back", colors.lightGray)
								elseif n == 3 then break end
								writeState(s_stateTreeFarm, b_stateTreeFarm)
						end
				elseif s_subOption == "Rubber Tree Farm" then
						while true do
								clear()
								colorW(yellow, "Rubber Tree Farm: ", b_stateRubberTreeFarm and lime or red, b_stateRubberTreeFarm and "On" or "Off")
								s, n = CUI(t_subOptions_OnOffBack)
								if n == 1 and not b_stateRubberTreeFarm then b_stateRubberTreeFarm = true addOutput("back", colors.cyan)
								elseif n == 2 and b_stateRubberTreeFarm then b_stateRubberTreeFarm = false removeOutput("back", colors.cyan)
								elseif n == 3 then break end
								writeState(s_stateRubberTreeFarm, b_stateRubberTreeFarm)
						end
				elseif s_subOption == "Cactus Farm" then
						while true do
								clear()
								colorW(yellow, "Cactus Farm: ", b_stateCactusFarm and lime or red, b_stateCactusFarm and "On" or "Off")
								s, n = CUI(t_subOptions_OnOffBack)
								if n == 1 and not b_stateCactusFarm then b_stateCactusFarm = true addOutput("back", colors.purple)
								elseif n == 2 and b_stateCactusFarm then b_stateCactusFarm = false removeOutput("back", colors.purple)
								elseif n == 3 then break end
								writeState(s_stateCactusFarm, b_stateCactusFarm)
						end
				elseif s_subOption == "Pumpkin Farm" then
						while true do
								clear()
								colorW(yellow, "Pumpkin Farm: ", b_statePumpkinFarm and lime or red, b_statePumpkinFarm and "On" or "Off")
								s, n = CUI(t_subOptions_OnOffBack)
								if n == 1 and not b_statePumpkinFarm then b_statePumpkinFarm = true addOutput("back", colors.blue)
								elseif n == 2 and b_statePumpkinFarm then b_statePumpkinFarm = false removeOutput("back", colors.blue)
								elseif n == 3 then break end
								writeState(s_statePumpkinFarm, b_statePumpkinFarm)
						end
				elseif s_subOption == "Reed Farm" then
						while true do
								clear()
								colorW(yellow, "Reed Farm: ", b_stateReedFarm and lime or red, b_stateReedFarm and "On" or "Off")
								s, n = CUI(t_subOptions_OnOffBack)
								if n == 1 and not b_stateReedFarm then b_stateReedFarm = true addOutput("back", colors.brown)
								elseif n == 2 and b_stateReedFarm then b_stateReedFarm = false removeOutput("back", colors.brown)
								elseif n == 3 then break end
								writeState(s_stateReedFarm, b_stateReedFarm)
						end
				elseif s_subOption == "Still" then
						while true do
								clear()
								colorW(yellow, "Still: ", b_stateStill and lime or red, b_stateStill and "On" or "Off")
								s, n = CUI(t_subOptions_OnOffBack)
								if n == 1 and not b_stateStill then b_stateStill = true addOutput("back", colors.green)
								elseif n == 2 and b_stateStill then b_stateStill = false removeOutput("back", colors.green)
								elseif n == 3 then break end
								writeState(s_stateStill, b_stateStill)
						end
				elseif s_subOption == "Bio Generator" then
						while true do
								clear()
								colorW(yellow, "Bio Generator: ", b_stateBioGenerator and lime or red, b_stateBioGenerator and "On" or "Off")
								s, n = CUI(t_subOptions_OnOffBack)
								if n == 1 and not b_stateBioGenerator then b_stateBioGenerator = true addOutput("back", colors.red)
								elseif n == 2 and b_stateBioGenerator then b_stateBioGenerator = false removeOutput("back", colors.red)
								elseif n == 3 then break end
								writeState(s_stateBioGenerator, b_stateBioGenerator)
						end
				elseif s_subOption == "Back" then s_mainState = "mainOptions" end

		--[[ Exit ]]--
		elseif s_mainState == "Exit" then -- this is the very bottom option in the main menu if you hti enter on this is closes the program entirely which allows for editing or whatever. This is where I would use that password function if I had this on a public server.
				running = false -- this make that while loop around this big block of code exit closing the program
		end
end

I understand this is alot I hope I dont get yelled at for making this post massive but I unfortunately do not know how to do the spoilers button…. This is an interesting piece of code it uses RP2 bundled cable(will need to be changed to match machines or items) the way you can change the menus is by just changing the stuff within the tables if you go into the t_mainOptions table and change the "Lights", to for example "Cow Farm" it will show up as cowfarm of course the files would be named wrong so more or less anything name lights would be named cow farm to keep along. This code was not built to be easily changeable made for my own needs in a SMP world so it may prove hard to change around. If you have any question fill free to ask.

This is what the main menu looks like


Here is a sub menu for example forestry machines and lights


Here is the pastebin http://pastebin.com/ugMpcHfL
jay5476 #3
Posted 10 September 2013 - 05:49 AM
that's a handful … if you just want them to type input its simple

while true do
term.clear()
term.setCursorPos(1,1)
write("Input Program: ")
prog = read() -- read what the user types
write("Input State: ")
state = read() -- read what they input again
you can then say use prog(state) to run a function defined by 'prog' like 'cow' and with 'state' so 'on' or 'off
bigbaddevil6 #4
Posted 10 September 2013 - 05:59 AM
Well this one is a multi menu which altogether has 8 menus. The problem with doing just a typed input like that is yea it will do the action but as soon as the computer is rebooted or whatever it doesn't keep that. So he would have to come along every time he starts his world and turn it back on/off which why there as half the program full of stuff just to write and read files. Yes the one I posted is technical but it does everything he wants/needs it to do to be able to control multiple things. Mine allows menu within menu interaction and able to navigate through them while saving the states of the menus so that the redstone stays on or stays off when it needs to.
jay5476 #5
Posted 10 September 2013 - 07:23 AM
386 lines of code! not necessary why not save all in 1 file maybe even the ones that are just turned on u use 50 lines of code for variables and such
Lyqyd #6
Posted 10 September 2013 - 10:45 AM
If you're going to just advertise your code here, at least make sure it's reasonably good code. There are quite a few changes you could make to shrink the file size while also making the program easier to use. Perhaps you should start an Ask a Pro thread to get some help with it.

And when someone says that they are trying to program it, our first response should be to steer them in the right direction, not to throw code at them that you think will do what they are trying to create a program to do.
bigbaddevil6 #7
Posted 10 September 2013 - 12:09 PM
I got help with in the Ask a pro thread when this was first done asked the same question jay did. One of the very dedicated people on this forum, or use to be, that have an excellent rating is who showed me it. I don't want to say his name here to throw him under the bus, but I can show exactly who helped me and the entire discussion of it through a pm if wanted.
Lyqyd #8
Posted 10 September 2013 - 01:14 PM
RemiX generally does okay, but the pastebin links in that topic are down, so I can't tell if the copypasta code was his or yours. Either way, that code could use significant improvement and does not represent best coding practices. If you post a "code review" sort of topic here, I'm sure it could be refactored to be fewer lines as well as easier to configure.
bigbaddevil6 #9
Posted 10 September 2013 - 01:47 PM
yes I do have to admit while attempting to explain this code that I saw some things that could be changed such as all the elseif statements at the bottom can be done away with and replaced. I would update this code but have other projects I would rather do.