Advertisement
tdy2012

CC Astral Sorcery

Aug 7th, 2021 (edited)
1,542
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.59 KB | None | 0 0
  1. -- Define global variables
  2. VERSION_STR_ASTRAL_SORCERY = 'astralsorcery-1.16-1.16.5-1.13.12'
  3. NAME_ENVIRONMENT_DETECTOR = 'environmentDetector'
  4. NAME_CHAT_BOX = 'chatBox'
  5. NAME_AR_CONTROLLER = 'arController'
  6. CMD_DRAW = '!sky_draw'
  7. CMD_CLEAR = '!sky_clear'
  8. CMD_LOG = '!sky_log'
  9. TIME_MINECRAFT_DAY_TICK = 0
  10. TIME_MINECRAFT_NIGHT_TICK = 12000
  11. INTERVAL_MINECRAFT_DAY_TICK = 24000
  12. INTERVAL_SOLAR_ECLIPSE_DAY = 36
  13. INTERVAL_MAIN_LOOP_SLEEP_SECONDS = 1
  14.  
  15. -- Construct constellation to moon id table
  16. CONSTELLATION_TABLE = {
  17.     ['Discidia'] = { 0, 5, 6, 7 },
  18.     ['Armara'] = { 2, 3, 4, 5, 6 },
  19.     ['Vicio'] = { 3, 4, 5, 6, 7 },
  20.     ['Aevitas'] = { 2, 3, 4, 5, 6 },
  21.     ['Evorsio'] = { 2, 3, 4, 5, 6 },
  22.     ['Lucerna'] = { 0, 4, 5, 6, 7 },
  23.     ['Mineralis'] = { 0, 1, 2, 3, 7 },
  24.     ['Octans'] = { 2, 3, 4, 5, 6 },
  25.     ['Bootes'] = { 0, 1, 2, 3, 7 },
  26.     ['Fornax'] = { 0, 4, 5, 6, 7 },
  27.     ['Gelu'] = { 2, 3, 4 },
  28.     ['Ulteria'] = { 0, 2, 7 },
  29.     ['Alcara'] = { 0, 4 },
  30.     ['Vorux'] = { 5, 6, 7 }
  31. }
  32.  
  33. -- Construct moon id to constellation table
  34. INVERTED_CONSTELLATION_TABLE = { [0]={}, [1]={}, [2]={}, [3]={}, [4]={}, [5]={}, [6]={}, [7]={} }
  35. for constellation, moonIdTable in pairs( CONSTELLATION_TABLE ) do
  36.     for _, moonId in pairs( moonIdTable ) do
  37.         table.insert( INVERTED_CONSTELLATION_TABLE[ moonId ], constellation )
  38.     end
  39. end
  40.  
  41. SPECIAL_CONSTELLATION_TABLE = {
  42.     'Horologium',
  43.     'Pelotrio'
  44. }
  45.  
  46. function logConstellationTable( peripheralChatBoxController )
  47.  
  48.     -- Display constellation table in chat message
  49.     local logMessage = ''
  50.     local availableConstellationStr
  51.     for moonId, constallationTable in pairs( INVERTED_CONSTELLATION_TABLE ) do
  52.         availableConstellationStr = table.concat( INVERTED_CONSTELLATION_TABLE[ moonId ], ', ' )
  53.         logMessage = logMessage .. 'Moon (id) ' .. moonId .. ', Constellation : ' .. availableConstellationStr .. '\n'
  54.     end
  55.     peripheralChatBoxController.sendMessage( logMessage )
  56.  
  57. end
  58.  
  59. function draw( peripheralEnvironmentDetector, peripheralArController, firstSolarEclipseDate )
  60.  
  61.     -- Clear the ar screen
  62.     peripheralArController.clear()
  63.  
  64.     -- Get today date
  65.     local todayDate = math.floor( peripheralEnvironmentDetector.getTime() / INTERVAL_MINECRAFT_DAY_TICK )
  66.     local currentTime = peripheralEnvironmentDetector.getTime() % INTERVAL_MINECRAFT_DAY_TICK
  67.  
  68.     -- Draw today date
  69.     peripheralArController.drawItemIcon( 'minecraft:clock', 31, 1 )
  70.     peripheralArController.drawRightboundString( 'Date (day) : ' .. todayDate, 31, 1, 0xFFFFFF )
  71.     peripheralArController.drawRightboundString( 'Time (tick) : ' .. currentTime, 31, 2, 0xFFFFFF )
  72.  
  73.     -- Get weather
  74.     local weather = 'Unknown'
  75.     if peripheralEnvironmentDetector.isSunny() then
  76.         weather = 'Clear'
  77.     elseif peripheralEnvironmentDetector.isRaining() then
  78.         weather = 'Rain'
  79.     elseif peripheralEnvironmentDetector.isThunder() then
  80.         weather = 'Storm'
  81.     end
  82.  
  83.     -- Draw weather
  84.     peripheralArController.drawItemIcon( 'minecraft:sunflower', 31, 3 )
  85.     peripheralArController.drawRightboundString( 'Weather : ' .. weather, 31, 3, 0xFFFFFF )
  86.  
  87.     -- Get moon
  88.     local moonId = peripheralEnvironmentDetector.getMoonId()
  89.     local moonName = peripheralEnvironmentDetector.getMoonName()
  90.     local availableConstellationStr = table.concat( INVERTED_CONSTELLATION_TABLE[ moonId ], ', ' )
  91.  
  92.     -- Draw moon and constellation
  93.     peripheralArController.drawItemIcon( 'astralsorcery:constellation_paper', 31, 4 )
  94.     peripheralArController.drawRightboundString( 'Moon (id/name) : ' .. moonId .. '/' .. moonName, 31, 4, 0xFFFFFF )
  95.     peripheralArController.drawRightboundString( 'Constellation : ' .. availableConstellationStr, 31, 5, 0xFFFFFF )
  96.  
  97.     -- Get next solar eclipse day
  98.     local nextSolarEclipseMessage = 'N/A'
  99.     if firstSolarEclipseDate ~= nil then
  100.  
  101.         -- Compute next solar eclipse day
  102.         local nextSolarEclipseDay = INTERVAL_SOLAR_ECLIPSE_DAY - ( ( todayDate - firstSolarEclipseDate ) % INTERVAL_SOLAR_ECLIPSE_DAY )
  103.        
  104.         -- Set the display message regarding to the next solar eclipse day
  105.         if nextSolarEclipseDay == INTERVAL_SOLAR_ECLIPSE_DAY then
  106.             nextSolarEclipseMessage = 'TODAY!!!'
  107.         else
  108.             local nextSolarEclipseDate = todayDate + nextSolarEclipseDay
  109.             nextSolarEclipseMessage = 'Day ' .. nextSolarEclipseDate .. ' (' .. nextSolarEclipseDay .. ' day(s))'
  110.         end
  111.  
  112.     end
  113.  
  114.     -- Draw next solar eclipse day
  115.     peripheralArController.drawItemIcon( 'astralsorcery:observatory', 31, 6 )
  116.     peripheralArController.drawRightboundString( 'Next solar eclipse : ' .. nextSolarEclipseMessage, 31, 6, 0xFFFFFF )
  117.  
  118. end
  119.  
  120. function main()
  121.  
  122.     -- Display program description and note messages
  123.     print( 'This program visualizes the sky information including today date, ' ..
  124.         'current time, weather, moon phase and today available constallations via an AR Goggles.\n' ..
  125.         '(Tested with ' .. VERSION_STR_ASTRAL_SORCERY .. ')' )
  126.  
  127.     -- Bind an environment detector peripheral
  128.     local peripheralEnvironmentDetector = peripheral.find( NAME_ENVIRONMENT_DETECTOR )
  129.     if peripheralEnvironmentDetector == nil then
  130.         error( 'Cannot find ' .. NAME_ENVIRONMENT_DETECTOR .. '.' )
  131.         do return end
  132.     end
  133.  
  134.     -- Bind a chat box peripheral
  135.     local peripheralChatBoxController = peripheral.find( NAME_CHAT_BOX )
  136.     if peripheralChatBoxController == nil then
  137.         error( 'Cannot find ' .. NAME_CHAT_BOX .. '.' )
  138.         do return end
  139.     end
  140.  
  141.     -- Bind an ar controller peripheral
  142.     local peripheralArController = peripheral.find( NAME_AR_CONTROLLER )
  143.     if peripheralArController == nil then
  144.         error( 'Cannot find ' .. NAME_AR_CONTROLLER .. '.' )
  145.         do return end
  146.     end
  147.  
  148.     -- Disable relative mode
  149.     peripheralArController.setRelativeMode( true, 64, 40 )
  150.  
  151.     -- Get the date of solar eclipse
  152.     print( 'Please input the date of solar eclipse (input any string, if you don\'t know): ' )
  153.     local firstSolarEclipseDate = tonumber(read())
  154.  
  155.     -- Validate given date
  156.     if firstSolarEclipseDate ~= nil then
  157.  
  158.         -- Try convert to number
  159.         if firstSolarEclipseDate < 0 then
  160.             error( 'The date cannot be negative number.' )
  161.             do return end
  162.         end
  163.  
  164.         -- Find the first date of solar eclipse
  165.         firstSolarEclipseDate = firstSolarEclipseDate % INTERVAL_SOLAR_ECLIPSE_DAY
  166.     end
  167.  
  168.     print( 'To use this program, bind the AR Goggles to the AR Controller and type these commands in the chat box.\n' ..
  169.         '1. !sky_draw : Redraw the sky information.\n' ..
  170.         '2. !sky_clear : Clear the screen.\n' ..
  171.         '3. !sky_log : Display constellation table in chat message.' )
  172.  
  173.     local event, username, message
  174.  
  175.     while true do
  176.        
  177.         event, username, message = os.pullEvent("chat")
  178.  
  179.         -- Parse chat message to command
  180.         if message == CMD_CLEAR then
  181.             print('User [' .. username .. '] called ' .. CMD_CLEAR .. '.')
  182.             peripheralArController.clear()
  183.         elseif message == CMD_DRAW then
  184.             print('User [' .. username .. '] called ' .. CMD_DRAW .. '.')
  185.             draw( peripheralEnvironmentDetector, peripheralArController, firstSolarEclipseDate )
  186.         elseif message == CMD_LOG then
  187.             print('User [' .. username .. '] called ' .. CMD_LOG .. '.')
  188.             logConstellationTable( peripheralChatBoxController )
  189.         end
  190.  
  191.         -- I still have a performance concern for this program, so a little sleep here
  192.         sleep( INTERVAL_MAIN_LOOP_SLEEP_SECONDS )
  193.  
  194.     end
  195.  
  196. end
  197.  
  198. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement