Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local args = {...}
- local MAIN_VERSION = "1.1"
- local SGC_CONTROLLER_VERSION = "0.1(beta)"
- local LANTEA_CONTROLLER_VERSION = "1pre1"
- term.clear()
- term.setCursorPos(1,1)
- term.write("define config")
- -- CONFIGURATION START
- term.write(" (Edit script to change config)") --Comment out this line
- local SGCRAFT_REFRESHRATE=0.5
- local LANTEA_REFRESHRATE=1
- -- CONFIGURATION END
- -- Menu API provided by cyanisaac and ProjectB, licensed under the MIT license.
- print()
- local sg=nil
- local running = false
- local rd_timeout=nil
- print("define bridge")
- local gmsg=nil
- function gloop() --SGCraft loop
- local gdat = {os.pullEvent()}
- if sg.energyAvailable ~= nil then
- gpwr=sg.energyAvailable()
- end
- gir=sg.irisState()
- --tclear()
- if gdat[1]=="key" then
- if gdat[2]==keys.d then
- local h, j = term.getCursorPos()
- print("Enter Address: ")
- term.setCursorPos(1,j+2)
- gtxt=slrd()
- ok, gdp=pcall(sg.energyToDial, gtxt)
- if ok then
- if gpwr==0 then
- print("WARNING: Energy buffer is empty!")
- ok, result = pcall(sg.dial, gtxt)
- if ok then
- print("dial "..gtxt.." now!")
- else
- print("Dialling failed: "..result)
- sleep(3)
- end
- elseif gdp > gpwr then
- print("Not enough energy: "..gtxt.." needs "..tostring(gdp).."SU but only "..tostring(gpwr).."SU are available!")
- sleep(5)
- else
- ok, result = pcall(sg.dial, gtxt)
- if ok then
- print("dial "..gtxt.." now!")
- else
- print("Dialling failed: "..result)
- sleep(3)
- end
- end
- else
- print("Address error: "..gdp)
- sleep(4)
- end
- elseif gdat[2]==keys.b then
- gtxt=selAddr()
- if gtxt ~= nil then
- ok, gdp=pcall(sg.energyToDial, gtxt)
- if ok then
- if gpwr==0 then
- print("WARNING: Energy buffer is empty!")
- ok, result = pcall(sg.dial, gtxt)
- if ok then
- print("dial "..gtxt.." now!")
- else
- print("Dialling failed: "..result)
- sleep(3)
- end
- elseif gdp == nil then
- print("invalid address?")
- sleep(1)
- elseif gdp > gpwr then
- print("Not enough energy: "..gtxt.." needs "..tostring(gdp).."SU but only "..tostring(gpwr).."SU are available!")
- sleep(5)
- else
- ok, result = pcall(sg.dial, gtxt)
- if ok then
- print("dial "..gtxt.." now!")
- else
- print("Dialling failed: "..result)
- sleep(3)
- end
- end
- else
- print("Address error: "..gdp)
- sleep(4)
- end
- else
- print("DialBook FAILED")
- sleep(0.4)
- end
- elseif gdat[2]==keys.c then
- sg.disconnect()
- elseif gdat[2]==keys.i then
- if gir=="Opening" or gir=="Open" then
- sg.closeIris()
- elseif gir=="Closed" or gir=="Closing" then
- sg.openIris()
- end
- elseif gdat[2]==keys.m then
- local h, j = term.getCursorPos()
- print("Enter Message: ")
- term.setCursorPos(1,j+2)
- local ok, reta = pcall(sg.sendMessage, slrd())
- if ok then
- print("Sent message")
- else
- print("ERROR: "..reta)
- sleep(3)
- end
- elseif gdat[2]==keys.x then
- running=false
- end
- elseif gdat[1]=="sgMessageReceived" then
- gmsg=string.gsub(textutils.serialize(gdat),"\n","")
- elseif gdat[1]=="timer" and gdat[2]==rd_timeout then
- rd_timeout = os.startTimer(SGCRAFT_REFRESHRATE)
- end
- gst, gchr, gdir = sg.stargateState()
- gla=sg.localAddress()
- grm=sg.remoteAddress()
- tclear()
- --print("State: "..gst)
- prclstate(gst)
- --print("Direction: "..gdir)
- prclwormhole(gdir)
- --print("Iris: "..gir)
- prcliris(gir)
- print()
- print("Local Address: "..gla.."\nRemote Address: "..tostring(grm))
- print()
- print("SGCraft Controller "..SGC_CONTROLLER_VERSION.." - SU Available: "..tostring(gpwr))
- if gmsg ~= nil then
- print("last message: "..gmsg)
- end
- if gchr ~= nil then
- print("engaged chevron "..tostring(gchr))
- end
- print("\nKeys: Dial (D), DialBook (B), Disconnect (C), Toggle Iris(I), Send Message (M), Exit (X)")
- end
- local gcs = {Idle=colors.green, Offline=colors.white, Connected=colors.blue, Opening=colors.red, Closing=colors.yellow, Dialling=colors.orange}
- function prclstate(mesg)
- if mesg~=nil and mesg~="" then
- term.write("State: ")
- coltex(gcs[mesg])
- print(mesg)
- coltex(colors.white)
- else
- print("State: Idle")
- end
- end
- local gwh={Outgoing=colors.blue, Incoming=colors.red}
- function prclwormhole(mesg)
- if mesg~=nil and mesg~="" then
- term.write("Direction: ")
- coltex(gwh[mesg])
- print(mesg)
- coltex(colors.white)
- else
- term.write("Direction: ")
- coltex(colors.green)
- print("Closed")
- coltex(colors.white)
- end
- end
- local giirriiss={Closed=colors.green, Closing=colors.lightBlue, Open=colors.blue, Opening=colors.lime, Offline=colors.yellow}
- function prcliris(mesg)
- if mesg~=nil and mesg~="" then
- term.write("Iris: ")
- coltex(giirriiss[mesg])
- print(mesg)
- coltex(colors.white)
- else
- print("Iris: "..tostring(mesg))
- end
- end
- --local lincoming=nil
- --local loutgoing=nil
- --local lidle=true
- --local lwop=false
- --local lwos=false
- --local lwoc=false
- local lstate="idle"
- local lcs=colors.green
- local lwh="closed"
- local lcw=colors.green
- local lencoded=nil
- local liv=nil
- local loa=nil
- function lloop() --LanteaCraft loop
- local evt, earg = os.pullEvent()
- if not sg.isDialing() then
- lencoded=nil
- end
- if evt=="key" then
- if earg==keys.d then
- local h, j = term.getCursorPos()
- print("Enter Address: ")
- term.setCursorPos(1,j+2)
- ltxt=slrd()
- ok, result = pcall(sg.dial, ltxt)
- if ok then
- print("dial "..ltxt.." now!")
- else
- print("Dialling failed: "..result)
- sleep(3)
- end
- elseif earg==keys.b then
- ltxt=selAddr()
- if ltxt ~= nil then --Able to do?
- ok, result = pcall(sg.dial, ltxt)
- if ok then
- print("dial "..ltxt.." now!")
- else
- print("Dialling failed: "..result)
- sleep(4)
- end
- else
- print("DialBook FAILED")
- sleep(0.4)
- end
- elseif earg==keys.c then
- sg.disconnect()
- elseif earg==keys.x then
- running=false
- end
- elseif evt=="sgIncoming" then
- lcs=colors.red
- lstate="incoming from - "..earg
- elseif evt=="sgOutgoing" then
- lcs=colors.blue
- lstate="outgoing to - "..earg
- elseif evt=="sgChevronEncode" then
- lencoded=earg
- elseif evt=="sgIdle" then
- if earg then
- lcs=colors.green
- lstate="idle"
- lcw=colors.green
- lwh="closed"
- end
- elseif evt=="sgWormholeOpening" then
- if earg then
- lcw=colors.red
- lwh="opening"
- end
- elseif evt=="sgWormholeClosing" then
- if earg then
- lcw=colors.yellow
- lwh="closing"
- end
- elseif evt=="sgWormholeStable" then
- if earg then
- lcw=colors.blue
- lwh="open"
- end
- elseif evt=="timer" and earg==rd_timeout then
- rd_timeout = os.startTimer(LANTEA_REFRESHRATE)
- end
- lhf=sg.hasFuel()
- tclear()
- coltex(colors.white)
- term.write("State: ")
- coltex(lcs)
- print(lstate)
- coltex(colors.white)
- term.write("Wormhole: ")
- coltex(lcw)
- print(lwh)
- coltex(colors.white)
- print()
- print("LanteaCraft controller "..LANTEA_CONTROLLER_VERSION.." - address="..loa)
- term.write(" ModVersion="..liv)
- if lhf then
- coltex(colors.green)
- print(" (hasFuel=true)")
- coltex(colors.white)
- else
- coltex(colors.red)
- print(" (hasFuel=false)")
- coltex(colors.white)
- end
- if lencoded ~= nil then
- print("encoded chevron "..tostring(lencoded))
- end
- print("\nKeys: Dial (D), DialBook (B), Disconnect (C), Exit (X)")
- end
- local gsc=nil
- function checkmod(stbl) --write SGCraft boolean to gsc
- gsc=(stbl.stargateState ~= nil)
- end
- print("define utils")
- local colsup=term.isColor()
- function coltex(col)
- if colsup then
- term.setTextColor(col)
- end
- end
- function PREflushX(inp)
- if inp=="X" or inp=="x" then
- error()
- else
- return inp
- end
- end
- function flushX(inp)
- if inp=="X" or inp=="x" then
- running=false
- else
- return inp
- end
- end
- function curpos(x,y)
- term.setCursorPos(x, y+2)
- end
- function slrd()
- to_sluk=os.startTimer(0.1)
- while true do
- evrt = {os.pullEvent()}
- --print(string.sub(evrt[1], 1, 2))
- if evrt[1]=="timer" and evrt[2]==to_sluk then
- return read()
- --elseif evrt[1]=="key" then
- -- evrt=nil --Ignore key
- elseif string.sub(evrt[1], 1, 2)=="sg" then
- --sleep(0.1)
- os.queueEvent(unpack(evrt))
- end
- end
- end
- function tclear()
- term.clear()
- term.setCursorPos(1,1)
- print("SGctl ("..MAIN_VERSION..") -- by qwertz19281")
- print("")
- end
- -- Address Book
- -- Menu API provided by cyanisaac and ProjectB, licensed under the MIT license.
- -- Menu API provided by cyanisaac and ProjectB, licensed under the MIT license.
- -- Menu API provided by cyanisaac and ProjectB, licensed under the MIT license.
- local menuloaded=false
- function forceLibLoaded()
- if(not menuloaded) then
- if(not fs.exists("sgctl_menu")) then--UK4ATXvH
- shell.run("pastebin","get","UK4ATXvH","sgctl_menu")
- if(not fs.exists("sgctl_menu")) then
- print("cyanisaac's basic menu API Download failed")
- print("Do you have the HTTP API enabled?")
- sleep(4)
- return
- end
- end
- if((sgctl_menu ~= nil)) then
- if((sgctl_menu.doMenu ~=nil)) then
- menuloaded=true
- return
- else
- print("Something wrong with the menu api?")
- sleep(4)
- os.unloadAPI("sgctl_menu")
- os.loadAPI("sgctl_menu")
- end
- elseif((menuloaded)) then
- print("Something wrong with the menu api?")
- sleep(4)
- os.unloadAPI("sgctl_menu")
- os.loadAPI("sgctl_menu")
- end
- os.loadAPI("sgctl_menu")
- end
- end
- local addrs=nil
- function forceListLoaded()
- if(addrN==nil) then
- --Not loaded
- if(fs.exists("sgctl_addr")) then
- h=fs.open("sgctl_addr","r")
- addrs=textutils.unserialize(h.readAll())
- h.close()
- else
- addrs={{},{}}
- end
- end
- end
- function writeList()
- h=fs.open("sgctl_addr","w")
- h.write(textutils.serialize(addrs))
- h.close()
- end
- function addAddr()
- tclear()
- print("Enter name: ")
- name=read()
- if(name=="Add" or name=="Delete" or name=="Cancel") then
- print("Invalid name")
- sleep(3)
- return
- end
- print("Enter Address: ")
- addr=read()
- addrs[1][(#addrs[1])+1]=name
- addrs[2][(#addrs[2])+1]=addr
- writeList()
- end
- function delete()
- name=sgctl_menu.doMenu("Select address to DELETE",{"Cancel",unpack(addrs[1])})
- if(name==1) then
- return
- end
- tclear()
- print("Do you really want to delete "..addrs[1][name-1].." ? write \"y\" to delete")
- ans=read()
- if(ans=="y") then
- --OK. Now delete entry
- table.remove(addrs[1], name-1)
- table.remove(addrs[2], name-1)
- writeList()
- end
- end
- function selAddr()
- --Begin Selection Loop
- forceLibLoaded()
- forceListLoaded()
- while true do
- selid=sgctl_menu.doMenu("SGCtl Dial Book",{"Cancel","Add","Delete",unpack(addrs[1])})
- if(selid==1) then
- return nil
- elseif(selid==2) then
- addAddr()
- elseif(selid==3) then
- delete()
- else
- return addrs[2][selid-3]
- end
- end
- end
- -- Main loop
- print("execute")
- print("search stargate")
- local sc = {peripheral.find("stargate")}
- local scn=1
- if sc==nil or #sc<1 then
- print("ERROR: No stargate peripheral found!")
- error()
- end
- if #sc > 1 then
- selsolved=false
- if args~=nil and #args>0 then
- scn=tonumber(args[1])
- if not (scn==nil or scn>#sc or scn<1) then
- selsolved=true
- end
- end
- if not selsolved then
- tclear()
- curpos(1, 1)
- for i=1,#sc do
- term.write("stargate_"..tostring(i)..": ")
- if(sc[i].stargateState ~= nil) then
- print("SGCraft "..tostring(sc[i].localAddress()))
- else
- print("LanteaCraft "..tostring(sc[i].getAddress()))
- end
- end
- print("Please select one from "..tostring(#sc).." stargates! (X for exit)")
- term.write("Enter Number: ")
- scn=tonumber(PREflushX(slrd()))
- while scn==nil or scn>#sc or scn<1 do
- tclear()
- print("NUMBER MALFORMED OR OUT OF BOUNDS!")
- curpos(1, 2)
- for i=1,#sc do
- term.write("stargate_"..tostring(i)..": ")
- if(sc[i].stargateState ~= nil) then
- print("SGCraft "..tostring(sc[i].localAddress()))
- else
- print("LanteaCraft "..tostring(sc[i].getAddress()))
- end
- end
- print("Please select one from "..tostring(#sc).." stargates! (type X for exit)")
- term.write("Enter Number: ")
- scn=tonumber(PREflushX(slrd()))
- end
- end
- end
- sg=sc[scn]
- checkmod(sg)
- running = true
- if not gsc then
- loa=sg.getAddress()
- liv=sg.getInterfaceVersion()
- end
- rd_timeout = os.startTimer(LANTEA_REFRESHRATE)
- while running do
- if gsc then
- gloop()
- else
- lloop()
- end
- end
- term.clear()
- term.setCursorPos(1,1)
- print("SGctl exited")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement