Pandana

EnderAir - Client

Feb 10th, 2022 (edited)
533
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- This program is the first of a two part system,
  2. -- which enables the player to teleport by selecting a
  3. -- destination in the interface.
  4.  
  5. -- IMPORTANT:
  6. -- This will require an Enderpearl stasis chamber to work.
  7. -- It is the mechanism this program relies on.
  8.  
  9. -- This is the list of possible destinations
  10. -- You can extend this in whichever way you like.
  11. -- To add another destination insert a table in the following format:
  12. --
  13. -- {<Destination name>, <Destination Computer ID>}
  14. --
  15. -- for example:
  16. --
  17. -- {"End", 00},
  18. -- {"Home", 01},
  19. -- {"Basement", 03}
  20. --
  21. local dests = {
  22.     {"End", 00},
  23.     {"Home", 01}
  24. }
  25.  
  26. -- Clears the screen and resets the cursor to the top left corner.
  27. function wipe_screen()
  28.     term.clear()
  29.     term.setCursorPos(1, 1)
  30. end
  31.  
  32. -- Prints all the possible destinations to the screen.
  33. function print_possible_destinations()
  34.     print("Destination:")
  35.     for i = 1, #dests, 1 do
  36.         print(dests[i][1])
  37.     end
  38.     print("Exit - To end the program")
  39.     print("-------------------------")
  40.     print("Where would you like to go?")
  41. end
  42.  
  43. -- Handles the processing of the selected destination.
  44. -- Sends RedNet signal to specified target.
  45. function handle_selection(dest_index)
  46.     term.clear()
  47.     term.setCursorPos(1, 1)
  48.  
  49.     -- Handles the special case, when home is selected.
  50.     if dests[dest_index][1]:lower() == "home" then
  51.         print("Teleporting to", dests[dest_index][1], "sweet", dests[dest_index][1])
  52.     else
  53.         print("Teleporting to", dests[dest_index][1])
  54.     end
  55.  
  56.     rednet.send(dests[dest_index][2], "tp")
  57.     sleep(2)
  58.     wipe_screen()
  59. end
  60.  
  61. -- Handles an invalid selection
  62. function handle_invalid_selection()
  63.     term.clear()
  64.     term.setCursorPos(1, 1)
  65.     print("Unknown Destination")
  66.     print("You might want to try this again.")
  67.     sleep(2)
  68.     wipe_screen()
  69. end
  70.  
  71. -- This function interates through the peripherals of the computer
  72. -- and then returns the side on which a modem is connected.
  73. -- Will terminate if there is none, since this will make the program unusable.
  74. function find_modem()
  75.     devs = peripheral.getNames()
  76.     for i = 1, #devs, 1 do
  77.         if peripheral.getType(devs[i]) == "modem" then
  78.             return devs[i]
  79.         end
  80.     end
  81.     Print("Error: No modem found. Terminating!")
  82.     Print("Connect a modem and try again.")
  83.     return -1
  84. end
  85.  
  86. function start_interaction()
  87.     while true do
  88.         -- Displaying possible destinations.
  89.         print_possible_destinations()
  90.  
  91.         -- Awaiting user input
  92.         -- Will store the selected destination
  93.         local tp = io.read()
  94.  
  95.         -- Locating the side of the device the modem is on.
  96.         -- The program will terminate if there is no terminal.
  97.         local modem_side = find_modem()
  98.         -- Terminating if there is no modem.
  99.         if modem_side == -1 then
  100.             return
  101.         end
  102.         -- Opening the Rednet Channel
  103.         rednet.open(modem_side)
  104.  
  105.         local valid = false
  106.  
  107.         -- Iterate over the possible destinations.
  108.         for i = 1, #dests, 1 do
  109.             if tp:lower() == dests[i][1]:lower() then
  110.                 handle_selection(i)
  111.                 return
  112.             end
  113.         end
  114.        
  115.         -- In case the input was exit, the program will terminate.
  116.         if tp:lower() == "exit" then
  117.             wipe_screen()
  118.             return
  119.         end
  120.  
  121.         -- Edge Case
  122.         -- Only used, if the input was invalid
  123.         handle_invalid_selection()
  124.     end
  125. end
  126.  
  127. function main()
  128.     wipe_screen()
  129.     start_interaction()
  130. end
  131.  
  132. main()
Add Comment
Please, Sign In to add comment