Advertisement
tman1123

Controller_setup.lua

Apr 1st, 2024 (edited)
774
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- floor_manager.lua
  2.  
  3. local floorsFile = "floors.txt"
  4.  
  5. local colorMap = {
  6.     white = colors.white,
  7.     orange = colors.orange,
  8.     magenta = colors.magenta,
  9.     lightBlue = colors.lightBlue,
  10.     yellow = colors.yellow,
  11.     lime = colors.lime,
  12.     pink = colors.pink,
  13.     gray = colors.gray,
  14.     lightGray = colors.lightGray,
  15.     cyan = colors.cyan,
  16.     purple = colors.purple,
  17.     blue = colors.blue,
  18.     brown = colors.brown,
  19.     green = colors.green,
  20.     red = colors.red,
  21.     black = colors.black,
  22. }
  23.  
  24. function saveFloors(floors)
  25.     local file = fs.open(floorsFile, "w")
  26.     file.write(textutils.serialize(floors))
  27.     file.close()
  28. end
  29.  
  30. function loadFloors()
  31.     if not fs.exists(floorsFile) then
  32.         return {}
  33.     else
  34.         local file = fs.open(floorsFile, "r")
  35.         local data = file.readAll()
  36.         file.close()
  37.         return textutils.unserialize(data)
  38.     end
  39. end
  40.  
  41. local floors = loadFloors()
  42.  
  43. function drawMenu()
  44.     term.clear()
  45.     term.setCursorPos(1, 1)
  46.     print("Elevator Monitoring\n")
  47.     print("Floors:")
  48.     for i, floor in ipairs(floors) do
  49.         for name, value in pairs(colorMap) do
  50.             if value == floor.col then
  51.                 print(i .. ". " .. floor.name .. " (" .. floor.num .. ") - Color: " .. name)
  52.                 break
  53.             end
  54.         end
  55.     end
  56.     print("\n1. Add Floor")
  57.     print("2. Delete Floor")
  58.     print("3. Edit Floor")
  59.     print("4. Exit")
  60.     print("5. Exit & Run")
  61. end
  62.  
  63. function getColorFromName(name)
  64.     return colorMap[name]
  65. end
  66.  
  67. function addFloor()
  68.     print("\nAdding a new floor...")
  69.     print("Enter floor number:")
  70.     local num = read()
  71.     print("Enter floor name:")
  72.     local name = read()
  73.     print("Enter color name (e.g., pink, blue, white):")
  74.     local colorName = read()
  75.     local col = getColorFromName(colorName)
  76.     if col then
  77.         table.insert(floors, {num = num, name = name, col = col})
  78.         saveFloors(floors)
  79.         print("Floor added.")
  80.     else
  81.         print("Invalid color name.")
  82.     end
  83. end
  84.  
  85. function deleteFloor()
  86.     print("\nEnter floor index to delete:")
  87.     local index = tonumber(read())
  88.     if index and index > 0 and index <= #floors then
  89.         table.remove(floors, index)
  90.         saveFloors(floors)
  91.         print("Floor deleted.")
  92.     else
  93.         print("Invalid index.")
  94.     end
  95. end
  96.  
  97. function editFloor()
  98.     print("\nEnter floor index to edit:")
  99.     local index = tonumber(read())
  100.     if floors[index] then
  101.         print("Enter new floor number (currently " .. floors[index].num .. "):")
  102.         floors[index].num = read()
  103.         print("Enter new floor name (currently " .. floors[index].name .. "):")
  104.         floors[index].name = read()
  105.         print("Enter new color name (e.g., pink, blue, white):")
  106.         local colorName = read()
  107.         local col = getColorFromName(colorName)
  108.         if col then
  109.             floors[index].col = col
  110.             saveFloors(floors)
  111.             print("Floor updated.")
  112.         else
  113.             print("Invalid color name.")
  114.         end
  115.     else
  116.         print("Invalid index.")
  117.     end
  118. end
  119.  
  120. function mainMenu()
  121.     drawMenu()
  122.     local choice = read()
  123.     if choice == "1" then
  124.         addFloor()
  125.     elseif choice == "2" then
  126.         deleteFloor()
  127.     elseif choice == "3" then
  128.         editFloor()
  129.     elseif choice == "4" then
  130.         return false -- Exit the program
  131.     elseif choice == "5" then
  132.         shell.run('controller')
  133.     end
  134.     return true
  135. end
  136.  
  137. while mainMenu() do
  138.     -- Loop back to the main menu after each action
  139. end
  140.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement