-- Startup Stuff local colony = peripheral.wrap("left") mon = peripheral.wrap("top") -- End Startup Stuff function SortJob(a, b) if (a == nil or a["job"] == nil) then return false end if (b == nil or b["job"] == nil) then return false end return a["job"] < b["job"] end function FirstName(name) returnStr = "" for i=1, #name do local char = string.sub(name, i, i) if (char == " " or i == 9) then return returnStr end returnStr = returnStr .. char end return returnStr end function GetHappiness(happiness) local mult = 10 -- # of places happiness = math.floor(happiness * mult + 0.5) / mult if (happiness == 10.0) then mon.setTextColor(colors.green) else mon.setTextColor(colors.pink) end mon.write(happiness) end function GetJobStatus(status) local statusText = { ["Working"] = "Working", ["Farming"] = "Working", ["Delivering"] = "Working", ["Mining"] = "Working", ["Composting"] = "Working", ["Searching for trees"] = "Working" } if (statusText[status] == nil) then mon.setTextColor(colors.red) mon.write(status) else mon.setTextColor(colors.green) mon.write(statusText[status]) end end function SetJobColor(job) local jobColors = { ["Knight"] = colors.magenta, ["Courier"] = colors.yellow, ["Archer"] = colors.pink, ["Builder"] = colors.brown, ["Druid"] = colors.lime, ["Enchanter"] = colors.purple, ["Farmer"] = colors.cyan, ["Student"] = colors.orange, ["Researcher"] = colors.lightBlue } if (jobColors[job] == nil) then mon.setTextColor(colors.blue) else mon.setTextColor(jobColors[job]) end end function GetBedDistance(bed, work) local bedY = bed and bed["z"] local bedX = bed and bed["x"] if not (wobedYrkY and bedX) then return end local workY = work and work["z"] local workX = work and work["x"] if not (workY and workX) then return end local distanceY = math.abs(workY - bedY) local distanceX = math.abs(workX - bedX) local distance = math.sqrt(distanceX^2 + distanceY^2) local mult = 10 -- # of decimal places distance = math.floor(distance * mult + 0.5) / mult mon.setTextColor(colors.green) if (distance > 99) then mon.setTextColor(colors.red) end if (distance > 75 and distance < 100) then mon.setTextColor(colors.orange) end if (distance > 50 and distance < 76) then mon.setTextColor(colors.yellow) end mon.write(distance) end function ShowCitizens() local counter = 1 local row = 1 local column = 1 local citizens = colony.getCitizens() local job = "unemployed" table.sort(citizens, SortJob) -- Erstelle eine Tabelle zur Zwischenspeicherung der neuen Anzeigen local newDisplays = {} for _, citizen in ipairs(citizens) do local status = citizen["status"] local job = citizen["job"] local bedDistance -- define bedDistance here if (row > 80) then row = 1 column = 82 end -- Erstelle die neuen Anzeigen für den Bürger local displayName = citizen["name"] local jobStatus = status local happiness = citizen["happiness"] --local bedDistance = {bed = citizen["home"]["location"], work = citizen["work"]["location"]} if citizen ~= nil and citizen["home"] ~= nil and citizen["home"]["location"] ~= nil and citizen["work"] ~= nil and citizen["work"]["location"] ~= nil then local bedDistance = {bed = citizen["home"]["location"], work = citizen["work"]["location"]} end -- Speichere die neuen Anzeigen in der Tabelle newDisplays[counter] = { displayName = displayName, job = job, jobStatus = jobStatus, happiness = happiness, bedDistance = bedDistance, row = row, column = column } row = row + 1 counter = counter + 1 end -- Lösche den Monitor, bevor die neuen Anzeigen angezeigt werden mon.clear() -- Schreibe die neuen Anzeigen auf den Monitor for _, display in ipairs(newDisplays) do mon.setCursorPos(display.column, display.row) mon.setTextColor(colors.blue) mon.setTextScale(0.5) mon.write(FirstName(display.displayName)) mon.setCursorPos(display.column + 10, display.row) SetJobColor(display.job) mon.write(display.job) mon.setCursorPos(display.column + 25 , display.row) mon.write(GetJobStatus(display.jobStatus)) mon.setCursorPos(display.column + 43, display.row) mon.write(GetHappiness(display.happiness)) local bed = display.bedDistance["bed"] local work = display.bedDistance["work"] mon.setCursorPos(display.column + 55, display.row) GetBedDistance(display.bedDistance.bed, display.bedDistance.work) -- the error was here end end while true do ShowCitizens() sleep(0.5) end