Sedrowow

FancyReciever

May 1st, 2025 (edited)
282
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.78 KB | None | 0 0
  1. -- Program to monitor turtle status
  2. --------------------------------------
  3. -- |¯\|¯¯] /¯]|¯¯][¯¯]\\  //|¯¯]|¯\ --
  4. -- | /| ] | [ | ]  ][  \\// | ] | / --
  5. -- | \|__] \_]|__][__]  \/  |__]| \ --
  6. --------------------------------------
  7.  
  8. local log_file = "log.txt"
  9. local options_file = "flex_options.cfg"
  10. os.loadAPI("flex.lua")
  11. local modem_channel = 6464
  12.  
  13. if fs.exists(options_file) then
  14.  local file = fs.open("flex_options.cfg", "r")
  15.  local line = file.readLine()
  16.  while line ~= nil do
  17.   if string.find(line, "modem_channel=") == 1 then
  18.    modem_channel = tonumber( string.sub(
  19.          line, 15, string.len(line) ) )
  20.    break
  21.   end --if
  22.   line = file.readLine()
  23.  end --while
  24.  file.close()
  25. end --if
  26.  
  27.  
  28. -- print("DEBUG: Looking for modem peripheral.") -- Debug print
  29. local p = flex.getPeripheral("modem")
  30. if #p > 0 then
  31.     -- print("DEBUG: Modem peripheral found: " .. tostring(p[1])) -- Debug print
  32.     modem = peripheral.wrap(p[1])
  33.     modem.open(modem_channel ) -- Open the modem on the status listen channel
  34.     -- print("DEBUG: Modem opened on channel " .. tostring(modem_channel )) -- Debug print
  35. else
  36.     -- print("DEBUG: No modem peripheral found.") -- Debug print
  37.     flex.printColors("Please attach a wireless or ender modem\n", colors.red)
  38.     sleep(2)
  39.     return
  40. end
  41.  
  42. local last_status = nil -- Variable to store the last received status
  43.  
  44. local function displayStatus()
  45.     term.clear()
  46.     term.setCursorPos(1, 1)
  47.     term.setTextColor(colors.white) -- Set default text color
  48.  
  49.     print("--- Turtle Status ---")
  50.     if last_status == nil then
  51.         print("Waiting for status update...")
  52.     else
  53.         -- Use colors from flex.lua's colors table (flex.lua is already loaded)
  54.         local colors = colors
  55.  
  56.         term.setTextColor(colors.white)
  57.         print("Turtle ID: " .. tostring(last_status.id))
  58.         if last_status.label and last_status.label ~= "" then
  59.             term.setTextColor(colors.white)
  60.             print("Turtle Label: " .. tostring(last_status.label))
  61.         end
  62.  
  63.         term.setTextColor(colors.orange) -- Fuel color (Matches turtle UI)
  64.         print("Fuel: " .. tostring(last_status.fuel))
  65.  
  66.         term.setTextColor(colors.lightGray) -- Position color (Requested color)
  67.         print("Position: X=" .. tostring(last_status.position.x) .. ", Y=" .. tostring(last_status.position.y) .. ", Z=" .. tostring(last_status.position.z))
  68.  
  69.         term.setTextColor(colors.white) -- Mining status color (Keep white)
  70.         print("Mining: " .. tostring(last_status.is_mining))
  71.  
  72.         term.setTextColor(colors.white) -- Estimated time color (Keep white)
  73.         print("Estimated Time: " .. tostring(last_status.estimated_time))
  74.  
  75.         term.setTextColor(colors.lightBlue) -- Dug blocks color (Matches turtle UI)
  76.         print("Dug: " .. tostring(last_status.dug_blocks) .. " blocks")
  77.  
  78.         term.setTextColor(colors.green) -- Depth color (Matches turtle UI)
  79.         -- Construct the depth display using the sent ymin and current y
  80.         local depth_display = tostring(-last_status.position.y) .. "m"
  81.         if last_status.ymin then -- Check if ymin was included in the status table
  82.             depth_display = depth_display .. " / " .. tostring(-last_status.ymin) .. "m"
  83.         end
  84.         print("Depth: " .. depth_display)
  85.  
  86.  
  87.         term.setTextColor(colors.white) -- Inventory header color (Keep white)
  88.         print("\nInventory Summary:")
  89.         if last_status.inventory_summary and #last_status.inventory_summary > 0 then
  90.             term.setTextColor(colors.white) -- Inventory items color (Keep white)
  91.             for _, item in ipairs(last_status.inventory_summary) do
  92.                 print("  " .. item.name .. " (" .. tostring(item.count) .. ")")
  93.             end
  94.         else
  95.             term.setTextColor(colors.white) -- Inventory empty message color (Keep white)
  96.             print("  Inventory empty or not detailed.")
  97.         end
  98.     end
  99.  
  100.     term.setTextColor(colors.white) -- Waiting message color (Keep white)
  101.     print("\nWaiting for next update...")
  102.     term.setTextColor(colors.white) -- Reset color before ending the display function
  103.     -- print("DEBUG: Display updated.") -- Optional debug
  104. end
  105.  
  106. term.setTextColor(colors.white) -- Ensure color is white initially
  107. print("Waiting for status message on channel " .. tostring(modem_channel ) .. "...")
  108.  
  109. while true do
  110.     displayStatus() -- Update display
  111.     -- Use os.pullEvent with a timeout to allow display updates even without messages
  112.     local event, modemSide, senderChannel, replyChannel, message, senderDistance =
  113.         os.pullEvent("modem_message", 0.5) -- Add a small timeout
  114.  
  115.     if event == "modem_message" then
  116.         -- print("DEBUG: Received modem message event on channel " .. tostring(senderChannel)) -- Optional debug
  117.         -- Check if the message is a status update and from the expected channel
  118.         if senderChannel == modem_channel  and type(message) == "table" and message.type == "status_update" then
  119.             -- print("DEBUG: Received valid status update.") -- Optional debug
  120.             last_status = message -- Store the latest status
  121.         -- else
  122.             -- print("DEBUG: Received unexpected message format or channel.") -- Optional debug
  123.             -- if type(message) == "table" then
  124.             --     print("DEBUG: Sender Channel: "..tostring(senderChannel)..", Message Type: "..(message.type or "no_type").. ", Message: "..textutils.serialize(message))
  125.             -- else
  126.             --      print("DEBUG: Sender Channel: "..tostring(senderChannel)..", Message Type: "..type(message).. ", Message: "..tostring(message))
  127.             -- end
  128.  
  129.         end
  130.     end
  131.     -- The loop will naturally call displayStatus() again after the event or timeout
  132. end
  133.  
  134. -- No cleanup needed as the script runs in a loop until stopped
Advertisement
Add Comment
Please, Sign In to add comment