Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- Coded by HPWebcamAble
- http://pastebin.com/u/HPWebcamAble
- WARNING! USE THE INSTALLER, DO NOT DOWNLOAD THIS ALONE! IT WONT WORK BY ITSELF.
- **********
- Installer
- **********
- In a CC Computer (Advanced only!):
- pastebin get eMCqJEBs installer
- From pastebin site:
- http://pastebin.com/eMCqJEBs
- **********
- Description
- **********
- This program creates the config files for my Redstone Engine program.
- If you got this by itself, run the installer program. (see above)
- MORE INFO IN THE REDSTONE ENGINE (RE) PROGRAM DESCRIPTION
- **********
- FAQ
- **********
- Q:What is this?
- A:See description
- Q:How does bundled/rednet cable work?
- A:Look it up. Probably best on youtube.
- Q:The bundled cable isn't attaching to the computer correctly/it doesnt work
- A:At this moment, you need to use ComputerCraft 1.5, not 1.6
- See http://pastebin.com/1XY12jQZ for more info
- Q:Im confused
- A:Here's a video! (Leave a comment with questions):
- http://youtu.be/zPvUVptSnro
- Q:Where do I report bugs?
- A:On its Youtube Video (Leave it as a comment):
- http://youtu.be/zPvUVptSnro
- **********
- Version
- **********
- |1.1| <-- This program
- Date: 10/4/14
- -Removed a few unused functions
- -Reworked config selection
- *Program starts on config selection
- *You can now delete configs in the program
- *Configs can no longer be made with a space in the name (derp)
- -You can now use 're edit <name>' to open a config from the console
- *If it doesn't exist, it will allow you to create it.
- -Setting sleep time is now much more logical :P
- |1.0 (Release)|
- Date: 9/29/14
- -Makes configs for my RE program!
- **********
- Bugs
- **********
- None! (Yet)
- **********
- Notes
- **********
- This program is almost identical to my config editor for
- Advanced Redstone Control
- ARC allows you to easily control bundled cable outputs from a CC monitor
- ]]
- --Variables--
- w,h = term.getSize()
- actions = {}
- local page = 1
- local sConfig = nil
- local toprint = nil
- args = {...}
- local deleteMode = false
- --Functions--
- function clear() shell.run("clear") end
- function tc(...) term.setTextColor(...) end
- function bc(...) term.setBackgroundColor(...) end
- function tw(...) term.write(...) end
- function cp(...) term.setCursorPos(...) end
- function color(textColor,backgroundColor)
- if textColor then
- term.setTextColor(textColor)
- end
- if backgroundColor then
- term.setBackgroundColor(backgroundColor)
- end
- end
- function printC(text,y)
- if not y then
- error("printC: for text '"..text.."', expected y value")
- end
- tLenght = #tostring(text)
- local sStart = math.ceil(w/2-tLenght/2)
- local sEnd = sStart + tLenght
- cp(sStart,y)
- tw(text)
- return sStart,sEnd
- end
- function mainScreen(_text)
- bc(colors.white)
- clear()
- bc(colors.blue)
- cp(1,1)
- tw(string.rep(" ",w))
- tc(colors.orange)
- if _text then
- printC(_text,1)
- else
- printC("Redstone Engine - Config Creator",1)
- end
- end
- function drawMain()
- clear()
- mainScreen()
- bc(colors.white)
- tc(colors.black)
- printC("Config '"..sConfig.."'",2)
- cp(3,2)
- tc(colors.white)
- bc(colors.lime)
- tw("Add Event")
- if actions[1] == "" or actions[1] == nil then
- tc(colors.red)
- bc(colors.white)
- printC("None",3)
- else
- for i = 1, #actions do
- current = i+16*(page-1)
- if i == 17 or not actions[current] then break end
- bc(colors.white)
- tc(colors.black)
- cp(1,i+2)
- tw(tostring(current))
- cp(4,i+2)
- firstChar = string.sub(actions[current],1,1)
- if firstChar == "s" then --sleep
- tempvar = string.sub(actions[current],2)
- if not tonumber(tempvar) then
- tw("sleep ")
- tc(colors.red)
- tw("[Not a number]")
- else
- tw("sleep("..tempvar..")")
- end
- elseif firstChar == "+" then --add color
- tempvar = string.sub(actions[current],2)
- if not tonumber(tempvar) then
- tw("add color ")
- tc(colors.red)
- tw("[Not a number] ("..tempvar..")")
- else
- tw("add color "..tempvar.." (")
- bc(tonumber(tempvar))
- tw(" ")
- bc(colors.white)
- tw(")")
- end
- elseif firstChar == "-" then --subtract color
- tempvar = string.sub(actions[current],2)
- if not tonumber(tempvar) then
- tw("remove color ")
- tc(colors.red)
- tw("[Not a number] ("..tempvar..")")
- else
- tw("remove color "..tempvar.." (")
- bc(tonumber(tempvar))
- tw(" ")
- bc(colors.white)
- tw(")")
- end
- elseif firstChar == "=" then --set output to
- tempvar = string.sub(actions[current],2)
- if not tonumber(tempvar) then
- tw("set to ")
- tc(colors.red)
- tw("[Not a number] ("..tempvar..")")
- else
- tw("set to "..tempvar)
- end
- else
- tc(colors.red)
- tw("[Not a recognized event] ("..actions[current]..")")
- end
- bc(colors.red)
- tc(colors.white)
- cp(w-7,i+2)
- tw("Delete")
- end
- end
- tc(colors.white)
- if page > 1 then
- canScrollUp = true
- bc(colors.lime)
- else
- canScrollUp = false
- bc(colors.lightGray)
- end
- cp(w,3)
- tw("^")
- if page < #actions/16 then
- canScrollDown = true
- bc(colors.lime)
- else
- canScrollDown = false
- bc(colors.lightGray)
- end
- cp(w,h-1)
- tw("v")
- cp(1,h)
- bc(colors.lime)
- tw(string.rep(" ",w))
- tc(colors.white)
- printC("Save",h)
- end
- function scrollRead(x,y,maxX,insertText) --This basically scrolls but not very well
- if insertText then
- cPos = #insertText+1
- cInput = insertText
- else
- cPos = 1
- cInput = ""
- end
- term.setCursorBlink(true)
- while true do
- term.setCursorPos(x,y)
- term.write(string.rep(" ",maxX))
- term.setCursorPos(x,y)
- if string.len(cInput) > maxX-1 then
- term.write(string.sub(cInput,(maxX-1)*-1))
- else
- term.write(cInput)
- end
- if cPos > maxX-1 then
- term.setCursorPos(x+maxX-1,y)
- else
- term.setCursorPos(x+cPos-1,y)
- end
- event,p1 = os.pullEvent()
- if event == "char" then
- cInput = string.sub(cInput,1,cPos)..p1..string.sub(cInput,cPos+1)
- cPos = cPos+1
- elseif event == "key" then
- if p1 == keys.enter then
- break
- elseif p1 == keys.backspace then
- if cPos > 1 then
- cInput = string.sub(cInput,1,cPos-2)..string.sub(cInput,cPos)
- cPos = cPos - 1
- end
- elseif p1 == keys["end"] then
- cPos = string.len(cInput)+1
- end
- end
- end
- term.setCursorBlink(false)
- return cInput
- end
- function loadConfig()
- --Try to load existing config
- f = fs.open("/RED/configs/"..sConfig,"r")
- actions = {}
- local l = 0
- repeat
- l = l + 1
- actions[l] = f.readLine()
- until actions[l] == nil
- f.close()
- end
- function drawSelectConfig()
- clear()
- mainScreen()
- color(colors.black,colors.white)
- printC("Select config:",2)
- configs = {}
- if fs.isDir("/RED/configs") then
- configs = fs.list("/RED/configs")
- for i = 1, #configs do
- cName = configs[i+16*(page-1)]
- if cName == nil or i == 17 then break end
- color(colors.black,colors.white)
- cp(8,i+2)
- if #cName > 30 then
- tw(string.sub(cName,1,27).."...")
- else
- tw(cName)
- end
- cp(40,i+2)
- if deleteMode then
- color(colors.white,colors.red)
- tw("Delete")
- else
- color(colors.white,colors.lightBlue)
- tw("Select")
- end
- end
- else
- color(colors.red,colors.white)
- printC("Missing config directory",4)
- end
- cp(w,3)
- canScrollup = false
- if page > 1 then
- color(colors.white,colors.lime)
- canScrollUp = true
- else
- color(colors.white,colors.lightGray)
- end
- tw("^")
- cp(w,h-1)
- canScrollDown = false
- if page < #configs/15 then
- canScrollDown = true
- color(colors.white,colors.lime)
- else
- color(colors.white,colors.lightGray)
- end
- tw("v")
- color(colors.white,colors.lightBlue)
- cp(w-12,2)
- tw("Toggle Mode")
- cp(2,2)
- color(colors.white,colors.lime)
- tw("New Config")
- end
- function save()
- bc(colors.yellow)
- tc(colors.white)
- cp(1,h)
- tw(string.rep(" ",w))
- printC("Checking config for errors...",h)
- problem = false
- for i = 1, #actions do
- firstChar = string.sub(actions[i],1,1)
- if firstChar == "+" then
- if not tonumber(string.sub(actions[i],2)) then
- problem = i
- break
- end
- elseif firstChar == "-" then
- if not tonumber(string.sub(actions[i],2)) then
- problem = i
- break
- end
- elseif firstChar == "=" then
- if not tonumber(string.sub(actions[i],2)) then
- problem = i
- break
- end
- elseif firstChar == "s" then
- if not tonumber(string.sub(actions[i],2)) then
- problem = i
- break
- end
- else
- problem = i
- break
- end
- end
- if problem then
- bc(colors.red)
- cp(1,h)
- tw(string.rep(" ",w))
- printC("Error found on line "..problem,h)
- sleep(2)
- return false
- else
- f = fs.open("/RED/configs/"..sConfig,"w")
- if f then
- for i = 1, #actions do
- f.write(actions[i].."\n")
- end
- f.close()
- return true
- else
- bc(colors.red)
- cp(1,h)
- tw(string.rep(" ",w))
- printC("Could not open save file",h)
- sleep(2)
- return false
- end
- end
- end
- function selectColor()
- local cColor = 1
- local colorPos = {}
- local lastColor = 32768 --the color black
- local xpos = 17
- local ypos = 5
- cp(xpos,ypos)
- tc(colors.black)
- bc(colors.white)
- tw("[")
- for i = 1, 20 do
- bc(cColor)
- tw(" ")
- colorPos[i] = cColor
- if cColor == lastColor then break end
- cColor = cColor*2
- end
- tc(colors.black)
- bc(colors.white)
- tw("]")
- while true do
- event,p1,p2,p3 = os.pullEvent("mouse_click")
- if p3 == ypos and p2 > xpos and p2 < xpos+17 then
- return colorPos[p2-xpos]
- end
- end
- end
- function addEvent()
- bc(colors.lime)
- tc(colors.white)
- for i = 1, #actions do
- cp(w-7,i+2)
- tw(" ^ ")
- if i == 16 then break end
- end
- cp(3,2)
- tw("Add at end")
- tc(colors.lightGray)
- bc(colors.white)
- cp(1,h)
- tw(string.rep(" ",w))
- printC("Use '^' to insert before",h)
- local addBefore = false
- while true do
- event,p1,p2,p3 = os.pullEvent("mouse_click")
- if p2 >= 3 and p2 <= 12 and p3 == 2 then --at end
- break
- elseif p3 > 2 and p3 < h and p2 >= w-7 and p2 <= w-3 then
- if p3-2 <= #actions-16*(page-1) then --before one
- addBefore = (p3-2)+16*(page-1)
- break
- end
- end
- end
- mainScreen("Add Event...")
- tc(colors.black)
- bc(colors.white)
- printC("Select an event:",2)
- bc(colors.lime)
- tc(colors.white)
- printC("Add color",4)
- printC("Remove color",6)
- printC("Set output",8)
- printC("Sleep",10)
- local action = nil
- while true do
- event,p1,p2,p3 = os.pullEvent("mouse_click")
- if p3 == 4 then --add
- mainScreen("Add Event...")
- tc(colors.black)
- bc(colors.white)
- printC("Select color to add:",2)
- _color = selectColor()
- if addBefore then
- table.insert(actions,addBefore,"+".._color)
- else
- table.insert(actions,"+".._color)
- end
- break
- elseif p3 == 6 then -- remove
- mainScreen("Add Event...")
- tc(colors.black)
- bc(colors.white)
- printC("Select color to remove:",2)
- _color = selectColor()
- if addBefore then
- table.insert(actions,addBefore,"-".._color)
- else
- table.insert(actions,"-".._color)
- end
- break
- elseif p3 == 8 then --set
- mainScreen("Add Event...")
- tc(colors.black)
- bc(colors.white)
- printC("Enter number to set output to:",2)
- tc(colors.white)
- bc(colors.black)
- repeat
- input = scrollRead(20,5,10)
- until input~="" and tonumber(input)
- if addBefore then
- table.insert(actions,addBefore,"="..tonumber(input))
- else
- table.insert(actions,"="..tonumber(input))
- end
- break
- elseif p3 == 10 then --sleep
- local sTime = 0
- mainScreen("Add Event...")
- color(colors.black,colors.white)
- printC("Enter sleep time:",3)
- input = ""
- while true do
- color(colors.white,colors.black)
- input = scrollRead(22,5,7,input)
- if input == lastInput then break else lastInput = nil end
- if not input or not tonumber(input) then
- color(nil,colors.white)
- cp(1,6)
- tw(string.rep(" ",w))
- cp(1,7)
- tw(string.rep(" ",w))
- color(colors.red,colors.white)
- printC("Must be a number",6)
- elseif tonumber(input) > 10 then
- color(nil,colors.white)
- cp(1,6)
- tw(string.rep(" ",w))
- color(colors.red,colors.white)
- printC("Sleep times above 10 aren't recomended",6)
- color(colors.lightGray)
- printC("Hit enter again to confirm",7)
- lastInput = input
- else
- break
- end
- end
- sTime = tonumber(input)
- if addBefore then
- table.insert(actions,addBefore,"s"..sTime)
- else
- table.insert(actions,"s"..sTime)
- end
- break
- end
- end
- end
- function newConfig(name)
- color(colors.orange,colors.blue)
- cp(10,6)
- tw(string.rep(" ",30))
- cp(10,7) tw(" ")
- cp(10,8) tw(" ")
- cp(10,9) tw(" ")
- cp(10,10)
- tw(string.rep(" ",30))
- cp(39,7) tw(" ")
- cp(39,8) tw(" ")
- cp(39,9) tw(" ")
- printC("New Config",6)
- color(colors.black,colors.white)
- printC("Enter name:",7)
- color(colors.lightGray,colors.blue)
- printC("Leave blank to cancel",10)
- useExisting = nil
- if name then
- input = tostring(name)
- else
- input = ""
- end
- while true do
- color(colors.white,colors.black)
- input = scrollRead(12,8,26,input)
- if input == "" then
- return false
- elseif string.find(input," ") then
- color(nil,colors.white)
- cp(11,9)
- tw(string.rep(" ",28))
- color(colors.red,colors.white)
- printC("Cannot contain spaces",9)
- elseif string.find(input,"/") then
- color(nil,colors.white)
- cp(11,9)
- tw(string.rep(" ",28))
- color(colors.red,colors.white)
- printC("Cannot contain '/'",9)
- elseif fs.exists("/RED/configs/"..input) then
- if useExisting == input then
- return input
- else
- color(nil,colors.white)
- cp(11,9)
- tw(string.rep(" ",28))
- color(colors.red,colors.white)
- printC("Use 'enter' to edit",9)
- useExisting = input
- end
- else
- return input
- end
- end
- end
- function main() --Having this as a function allows error catching
- if args[1] then --Check the given args
- if fs.exists("/RED/configs/"..args[1]) then
- os.queueEvent("load_config")
- else
- os.queueEvent("create_config")
- end
- end
- continue = true
- drawSelectConfig()
- while continue do --have the user select a config (or create one)
- event,p1,p2,p3 = os.pullEvent()
- if event == "mouse_click" then
- if p2 <= w-1 and p2 >= w-12 and p3 == 2 then --toggle mode
- deleteMode = not deleteMode
- drawSelectConfig()
- elseif p2 >= 2 and p2 <= 12 and p3 == 2 then --new config
- success = newConfig()
- if success then
- sConfig = success
- break
- else
- drawSelectConfig()
- end
- elseif p3 > 2 and p3 < h then --delete or select config
- if p3 <= #configs-16*(page-1)+2 then
- if deleteMode then
- fs.delete("/RED/configs/"..configs[p3-2+16*(page-1)])
- drawSelectConfig()
- else
- sConfig = configs[p3-2+16*(page-1)]
- loadConfig()
- break
- end
- end
- elseif p3 == 3 and p2 == w then --page up
- if canScrollUp then
- page = page-1
- end
- elseif p3 == h-1 and p2 == w then --page down
- if canScrollDown then
- page = page+1
- end
- end
- elseif event == "load_config" then
- sConfig = args[1]
- loadConfig()
- break
- elseif event == "create_config" then
- success = newConfig(args[1])
- if success then
- sConfig = success
- break
- else
- drawSelectConfig()
- end
- end
- end
- continue = true
- page = 1
- while continue do --edit the config
- drawMain()
- while true do
- event,p1,p2,p3 = os.pullEvent("mouse_click")
- if p2 >= 3 and p2 <= 11 and p3 == 2 then --Add
- addEvent()
- drawMain()
- elseif p2 >= w-7 and p2 <= w-2 and p3 > 2 and p3 < h then --Delete
- if p3-2 <= #actions+16*(page-1) then
- table.remove(actions,(p3-2)+16*(page-1))
- drawMain()
- end
- elseif p3 == 3 and p2 == w and canScrollUp then --Scroll up
- page = page-1
- drawMain()
- elseif p3 == h-1 and p2 == w and canScrollDown then --Scroll down
- page = page+1
- drawMain()
- elseif p3 == h then --save
- if save() then
- continue = false
- break
- else
- drawMain()
- end
- end
- end
- end
- end
- --Program--
- state,err = pcall(function() main() end)
- if err then
- if string.find(err,"Terminated") then
- term.setCursorBlink(false)
- cp(1,1)
- bc(colors.red)
- tc(colors.white)
- tw(string.rep(" ",w))
- printC("Terminated - Click to clear screen",1)
- os.pullEvent()
- bc(colors.black)
- clear()
- print("No changes made")
- elseif string.find(err,"end") then
- tc(colors.black)
- print("Ended")
- else
- tc(colors.black)
- print("CRITICAL ERROR:")
- print(err)
- end
- else
- bc(colors.black)
- tc(colors.white)
- clear()
- tc(colors.lime)
- print("Config Saved")
- end
- --Cleanup
- if f then
- f.close()
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement