Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- This program is the first of a two part system,
- -- which enables the player to teleport by selecting a
- -- destination in the interface.
- -- IMPORTANT:
- -- This will require an Enderpearl stasis chamber to work.
- -- It is the mechanism this program relies on.
- -- This is the list of possible destinations
- -- You can extend this in whichever way you like.
- -- To add another destination insert a table in the following format:
- --
- -- {<Destination name>, <Destination Computer ID>}
- --
- -- for example:
- --
- -- {"End", 00},
- -- {"Home", 01},
- -- {"Basement", 03}
- --
- local dests = {
- {"End", 00},
- {"Home", 01}
- }
- -- Clears the screen and resets the cursor to the top left corner.
- function wipe_screen()
- term.clear()
- term.setCursorPos(1, 1)
- end
- -- Prints all the possible destinations to the screen.
- function print_possible_destinations()
- print("Destination:")
- for i = 1, #dests, 1 do
- print(dests[i][1])
- end
- print("Exit - To end the program")
- print("-------------------------")
- print("Where would you like to go?")
- end
- -- Handles the processing of the selected destination.
- -- Sends RedNet signal to specified target.
- function handle_selection(dest_index)
- term.clear()
- term.setCursorPos(1, 1)
- -- Handles the special case, when home is selected.
- if dests[dest_index][1]:lower() == "home" then
- print("Teleporting to", dests[dest_index][1], "sweet", dests[dest_index][1])
- else
- print("Teleporting to", dests[dest_index][1])
- end
- rednet.send(dests[dest_index][2], "tp")
- sleep(2)
- wipe_screen()
- end
- -- Handles an invalid selection
- function handle_invalid_selection()
- term.clear()
- term.setCursorPos(1, 1)
- print("Unknown Destination")
- print("You might want to try this again.")
- sleep(2)
- wipe_screen()
- end
- -- This function interates through the peripherals of the computer
- -- and then returns the side on which a modem is connected.
- -- Will terminate if there is none, since this will make the program unusable.
- function find_modem()
- devs = peripheral.getNames()
- for i = 1, #devs, 1 do
- if peripheral.getType(devs[i]) == "modem" then
- return devs[i]
- end
- end
- Print("Error: No modem found. Terminating!")
- Print("Connect a modem and try again.")
- return -1
- end
- function start_interaction()
- while true do
- -- Displaying possible destinations.
- print_possible_destinations()
- -- Awaiting user input
- -- Will store the selected destination
- local tp = io.read()
- -- Locating the side of the device the modem is on.
- -- The program will terminate if there is no terminal.
- local modem_side = find_modem()
- -- Terminating if there is no modem.
- if modem_side == -1 then
- return
- end
- -- Opening the Rednet Channel
- rednet.open(modem_side)
- local valid = false
- -- Iterate over the possible destinations.
- for i = 1, #dests, 1 do
- if tp:lower() == dests[i][1]:lower() then
- handle_selection(i)
- return
- end
- end
- -- In case the input was exit, the program will terminate.
- if tp:lower() == "exit" then
- wipe_screen()
- return
- end
- -- Edge Case
- -- Only used, if the input was invalid
- handle_invalid_selection()
- end
- end
- function main()
- wipe_screen()
- start_interaction()
- end
- main()
Add Comment
Please, Sign In to add comment