Advertisement
gelatine87

colonyStatus

May 11th, 2023 (edited)
737
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.94 KB | Gaming | 0 0
  1.  -- Startup Stuff
  2.  local colony = peripheral.wrap("left")
  3.  mon = peripheral.wrap("top")
  4.  -- End Startup Stuff
  5.  
  6. function SortJob(a, b)
  7.   if (a == nil or a["job"] == nil) then return false end
  8.   if (b == nil or b["job"] == nil) then return false end
  9.   return a["job"] < b["job"]
  10. end
  11.  
  12. function FirstName(name)
  13.   returnStr = ""
  14.   for i=1, #name do
  15.       local char = string.sub(name, i, i)
  16.       if (char == " " or i == 9) then
  17.           return returnStr
  18.       end
  19.       returnStr = returnStr .. char
  20.   end
  21.   return returnStr
  22. end
  23.  
  24. function GetHappiness(happiness)
  25.   local mult = 10 -- # of places
  26.   happiness = math.floor(happiness * mult + 0.5) / mult
  27.   if (happiness == 10.0) then
  28.       mon.setTextColor(colors.green)
  29.   else
  30.       mon.setTextColor(colors.pink)
  31.   end
  32.   mon.write(happiness)
  33. end
  34.  
  35. function GetJobStatus(status)
  36.   local statusText = {
  37.       ["Working"] = "Working",
  38.       ["Farming"] = "Working",
  39.       ["Delivering"] = "Working",
  40.       ["Mining"] = "Working",
  41.       ["Composting"] = "Working",
  42.       ["Searching for trees"] = "Working"
  43.   }
  44.  
  45.   if (statusText[status] == nil) then
  46.       mon.setTextColor(colors.red)
  47.       mon.write(status)
  48.   else
  49.       mon.setTextColor(colors.green)
  50.       mon.write(statusText[status])
  51.   end
  52. end
  53.  
  54. function SetJobColor(job)
  55.   local jobColors = {
  56.       ["Knight"] = colors.magenta,
  57.       ["Courier"] = colors.yellow,
  58.       ["Archer"] = colors.pink,
  59.       ["Builder"] = colors.brown,
  60.       ["Druid"] = colors.lime,
  61.       ["Enchanter"] = colors.purple,
  62.       ["Farmer"] = colors.cyan,
  63.       ["Student"] = colors.orange,
  64.       ["Researcher"] = colors.lightBlue
  65.   }
  66.   if (jobColors[job] == nil) then
  67.       mon.setTextColor(colors.blue)
  68.   else
  69.       mon.setTextColor(jobColors[job])
  70.   end
  71. end
  72.  
  73. function GetBedDistance(bed, work)
  74.   local bedY = bed and bed["z"]
  75.   local bedX = bed and bed["x"]
  76.   if not (wobedYrkY and bedX) then
  77.     return
  78.   end
  79.   local workY = work and work["z"]
  80.   local workX = work and work["x"]
  81.   if not (workY and workX) then
  82.     return
  83.   end
  84.   local distanceY = math.abs(workY - bedY)
  85.   local distanceX = math.abs(workX - bedX)
  86.   local distance = math.sqrt(distanceX^2 + distanceY^2)
  87.   local mult = 10 -- # of decimal places
  88.   distance = math.floor(distance * mult + 0.5) / mult
  89.  
  90.   mon.setTextColor(colors.green)
  91.   if (distance > 99) then
  92.       mon.setTextColor(colors.red)
  93.   end
  94.   if (distance > 75 and distance < 100) then
  95.       mon.setTextColor(colors.orange)
  96.   end
  97.   if (distance > 50 and distance < 76) then
  98.       mon.setTextColor(colors.yellow)
  99.   end
  100.    mon.write(distance)
  101. end
  102.  
  103. function ShowCitizens()
  104.   local counter = 1
  105.   local row = 1
  106.   local column = 1
  107.   local citizens = colony.getCitizens()
  108.   local job = "unemployed"
  109.   table.sort(citizens, SortJob)
  110.  
  111.   -- Erstelle eine Tabelle zur Zwischenspeicherung der neuen Anzeigen
  112.   local newDisplays = {}
  113.  
  114.   for _, citizen in ipairs(citizens) do
  115.     local status = citizen["status"]
  116.     local job = citizen["job"]
  117.     local bedDistance  -- define bedDistance here
  118.       if (row > 80) then
  119.         row = 1
  120.         column = 82
  121.       end
  122.  
  123.       -- Erstelle die neuen Anzeigen für den Bürger
  124.       local displayName = citizen["name"]
  125.       local jobStatus = status
  126.       local happiness = citizen["happiness"]
  127.       --local bedDistance = {bed = citizen["home"]["location"], work = citizen["work"]["location"]}
  128.  
  129.       if citizen ~= nil and citizen["home"] ~= nil and citizen["home"]["location"] ~= nil and citizen["work"] ~= nil and citizen["work"]["location"] ~= nil then
  130.         local bedDistance = {bed = citizen["home"]["location"], work = citizen["work"]["location"]}
  131.       end
  132.  
  133.       -- Speichere die neuen Anzeigen in der Tabelle
  134.       newDisplays[counter] = {
  135.         displayName = displayName,
  136.         job = job,
  137.         jobStatus = jobStatus,
  138.         happiness = happiness,
  139.         bedDistance = bedDistance,
  140.         row = row,
  141.         column = column
  142.       }
  143.  
  144.       row = row + 1
  145.       counter = counter + 1
  146.   end
  147.  
  148.   -- Lösche den Monitor, bevor die neuen Anzeigen angezeigt werden
  149.   mon.clear()
  150.  
  151.   -- Schreibe die neuen Anzeigen auf den Monitor
  152.   for _, display in ipairs(newDisplays) do
  153.     mon.setCursorPos(display.column, display.row)
  154.     mon.setTextColor(colors.blue)
  155.     mon.setTextScale(0.5)
  156.     mon.write(FirstName(display.displayName))
  157.  
  158.     mon.setCursorPos(display.column + 10, display.row)
  159.     SetJobColor(display.job)
  160.     mon.write(display.job)
  161.  
  162.     mon.setCursorPos(display.column + 25 , display.row)
  163.     mon.write(GetJobStatus(display.jobStatus))
  164.  
  165.     mon.setCursorPos(display.column + 43, display.row)
  166.     mon.write(GetHappiness(display.happiness))
  167.  
  168.     local bed = display.bedDistance["bed"]
  169.     local work = display.bedDistance["work"]
  170.     mon.setCursorPos(display.column + 55, display.row)
  171.     GetBedDistance(display.bedDistance.bed, display.bedDistance.work)  -- the error was here
  172.   end
  173. end
  174.  
  175. while true do
  176.   ShowCitizens()
  177.   sleep(0.5)
  178. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement