PK WH"`= = ccexamples/compat-- -- Library code used by the example programs -- Computercraft version -- -- Utility for calling a function and printing the -- error message if it throws an exception function try(func, ...) ok, result = pcall(func, ...) if not ok then print("Error: " .. result) end end -- Utility for checking the result of a function call and -- throwing an exception if it returned an error indication function check(a, b, ...) if a == nil then error(b, 0) end return a, b, ... end -- Terminal API compatibility functions screen_width, screen_height = term.getSize() function setCursor(col, row) term.setCursorPos(col, row) end -- Event API compatibility functions function pull_event() return os.pullEvent() end key_event_name = "char" function key_event_char(e) return e[2] end PK NYHmmj ccexamples/config-- -- Configuration file used by the example programs -- Computercraft version -- Change the interface name below to suit -- interfaceName = "stargate_1" sg = peripheral.wrap(interfaceName) if sg == nil then error(interfaceName .. " not found", 0) end if PK TXE ccexamples/addresses-- -- Put your own stargate addresses here -- Used by the 'interactive' example program -- addresses = { {"Island", "FFOW-X48"}, {"Mainland", "PFIV-8GF"}, {"Nether 1", "ZFFB-XP8-1V"}, {"Beach", "T9F9-IP4"}, {"Home", "59LB-RP2"}, {"Nether 2", "599F-01A-1V"} } PK WWHD ccexamples/dial-- -- Dial an address -- -- Usage: -- dial
-- dofile("config") dofile("compat") args = {...} function main() check(sg.dial(args[1])) end try(main) PK lWH!| | ccexamples/disconnect-- -- Close the stargate -- dofile("config") dofile("compat") function main() check(sg.disconnect()) end try(main) PK WHPRX X ccexamples/energy-- -- Display available energy or energy required -- to dial an address -- -- Usage: -- energy -- energy -- dofile("config") dofile("compat") args = {...} function main() if #args >= 1 then print(check(sg.energyToDial(args[1]))) else print(check(sg.energyAvailable())) end end try(main) PK 2XH6 ccexamples/events-- -- Loop reading and displaying stargate events -- dofile("config") dofile("compat") print("Press q to quit") while true do e = {pull_event()} --print(e[1], e[2], e[3]) if e[1] == key_event_name and key_event_char(e) == "q" then break end if string.sub(e[1], 1, 2) == "sg" then print("Event:") for k, v in pairs(e) do print(string.format("%s %s", k, v)) end end end PK ҘwIk{ { ccexamples/interactive-- -- Interactive stargate control program -- Shows stargate state and allows dialling -- addresses selected from a list -- dofile("config") dofile("compat") dofile("addresses") function pad(s, n) if string.len(s) < n then s = s .. string.rep(" ", n - string.len(s)) end return s end function showMenu() setCursor(1, 1) for i, na in pairs(addresses) do print(string.format("%d %s", i, na[1])) end print("") print("D Disconnect") print("O Open Iris") print("C Close Iris") print("Q Quit") print("") write("Option? ") end --function getIrisState() -- ok, result = pcall(sg.irisState) -- return result --end function showState() locAddr = check(sg.localAddress()) remAddr = check(sg.remoteAddress()) state, chevrons, direction = check(sg.stargateState()) energy = check(sg.energyAvailable()) iris = check(sg.irisState()) showAt(30, 1, "Local: " .. locAddr) showAt(30, 2, "Remote: " .. remAddr) showAt(30, 3, "State: " .. state) showAt(30, 4, "Energy: " .. string.format("%d", energy)) showAt(30, 5, "Iris: " .. iris) showAt(30, 6, "Engaged: " .. chevrons) showAt(30, 7, "Direction: " .. direction) end function showAt(x, y, s) setCursor(x, y) write(pad(s, 50 - x)) end function showMessage(mess) showAt(1, screen_height, mess) end function showError(mess) showMessage("Error: " .. mess) end handlers = {} function dial(name, addr) showMessage(string.format("Dialling %s (%s)", name, addr)) check(sg.dial(addr)) end handlers[key_event_name] = function(e) c = key_event_char(e) if c == "d" then check(sg.disconnect()) elseif c == "o" then check(sg.openIris()) elseif c == "c" then check(sg.closeIris()) elseif c == "q" then running = false elseif c >= "1" and c <= "9" then na = addresses[tonumber(c)] if na then dial(na[1], na[2]) end end end function handlers.sgChevronEngaged(e) chevron = e[3] symbol = e[4] showMessage(string.format("Chevron %s engaged! (%s)", chevron, symbol)) end function eventLoop() while running do showState() e = {pull_event()} name = e[1] f = handlers[name] if f then showMessage("") ok, result = pcall(f, e) if not ok then showError(result) end end end end function main() term.clear() showMenu() eventLoop() term.clear() setCursor(1, 1) end running = true main() PK ûWHǩ;u ccexamples/iris-- -- Iris control -- -- Usage: -- iris -- Display iris state -- iris open -- iris close -- dofile("config") dofile("compat") args = {...} function main() if #args == 0 then print(check(sg.irisState())) elseif args[1] == "open" then check(sg.openIris()) elseif args[1] == "close" then check(sg.closeIris()) else print(args[1] .. " not understood") end end try(main) PK WHʷ6O ccexamples/sendmsg-- -- Send a message through the wormhole -- -- Usage: -- sendmsg arg, ... -- dofile("config") dofile("compat") function main(...) check(sg.sendMessage(...)) end try(main, ...) PK WHB ccexamples/state-- -- Display stargate state -- dofile("config") dofile("compat") function main() state, n = check(sg.stargateState()) la = check(sg.localAddress()) ra = check(sg.remoteAddress()) e = check(sg.energyAvailable()) print("Local address: " .. la) print("Remote address: " .. ra) print("State: " .. state) print("Engaged chevrons: " .. n) print("Energy available: " .. e) end try(main, ...) PK WH"`= = ccexamples/compatPK NYHmmj l ccexamples/configPK TXE ccexamples/addressesPK WWHD ccexamples/dialPK lWH!| | ccexamples/disconnectPK WHPRX X t ccexamples/energyPK 2XH6 ccexamples/eventsPK ҘwIk{ { ccexamples/interactivePK ûWHǩ;u o ccexamples/irisPK WHʷ6O D ccexamples/sendmsgPK WHB 5 ccexamples/statePK