Module:GeneralPortOperations: Difference between revisions

From MicrasWiki
Jump to navigationJump to search
NewZimiaGov (talk | contribs)
Undo revision 359969 by NewZimiaGov (talk)
Tag: Undo
NewZimiaGov (talk | contribs)
No edit summary
 
(96 intermediate revisions by the same user not shown)
Line 1: Line 1:
-- Module:GeneralPortDailyLog
-- Module:GeneralPortDailyLog
local p = {}
local p = {}
-- compatibility shim for unpack
local unpack = table.unpack or unpack
-- 0) Daily tonnage cap
local MAX_DAILY_TONNAGE = 1359348.096


-- 1) Date logic
-- 1) Date logic
function p.getCurrentDate()
function p.getCurrentDate()
     local startDate = os.time{year=1999, month=8, day=6}
     local start  = os.time{year=1999, month=8, day=6}
     local secs, daysecs = os.time() - startDate, 86400
     local elapsed = math.floor((os.time() - start) / 86400)
    local daysPerYear = 183
     local year   = math.floor(elapsed / 183)
    local totalDays = math.floor(secs/ daysecs)
     local day     = (elapsed % 183) + 1
     local year = math.floor(totalDays / daysPerYear)
     local day = (totalDays % daysPerYear) + 1
     return year, day
     return year, day
end
end


-- 2) Helpers
-- 2) Random helpers
local function randomChoice(t) return t[math.random(#t)] end
local function randomChoice(tbl)
    if type(tbl) ~= "table" or #tbl == 0 then return nil end
    return tbl[math.random(#tbl)]
end
 
local function randomN(tbl, n)
    if type(tbl) ~= "table" or n < 1 then return {} end
    local copy, out = {unpack(tbl)}, {}
    for i = 1, math.min(n, #copy) do
        local idx = math.random(#copy)
        table.insert(out, table.remove(copy, idx))
    end
    return out
end


-- 3) Fixed data sets
-- 3) Home ports + flags
-- DO NOT ALTER: existing fixed home-city list retained exactly
local cities = {
local cities = {
     "Vaeringheim","Somniumpolis","Delphica","Ephyra","Norsolyra",
     "Aegirheim","Skýrophos","Bjornopolis","Norsolyra","Ephyra",
     "Skýrophos","Riddersborg","Ardclach","Jogi","Sufriya",
     "Symphonara","Delphica","Vaeringheim","Somniumpolis",
     "Keybir-Aviv","Aegirheim","Bjornopolis"
     "Keybir-Aviv","Sufriya","Jogi" , "Ardclach" , "Riddersborg"
}
}
local cityFlags = {
local cityFlag = {}
     Vaeringheim="[[File:VaeringheimFlag.png|20px]] Vaeringheim",
for _, name in ipairs(cities) do
    Somniumpolis="[[File:SomniumpolisFlag.png|20px]] Somniumpolis",
     local norm = name
    Delphica="[[File:DelphicaFlag.png|20px]] Delphica",
        :gsub("[%s%-’]", "")
    Ephyra="[[File:EphyraFlag.png|20px]] Ephyra",
        :gsub("[Áá]", "A"):gsub("[Éé]", "E")
     Norsolyra="[[File:NorsolyraFlag.png|20px]] Norsolyra",
        :gsub("[Íí]", "I"):gsub("[Óó]", "O")
    ["Skýrophos"]="[[File:SkyrophosFlag.png|20px]] Skýrophos",
        :gsub("[Úú]", "U"):gsub("[Ýý]", "Y")
    Riddersborg="[[File:RiddersborgFlag.png|20px]] Riddersborg",
     cityFlag[name] = string.format("[[File:%sFlag.png|20px]]", norm)
    Ardclach="[[File:ArdclachFlag.png|20px]] Ardclach",
end
    Jogi="[[File:JogiFlag.png|20px]] Jogi",
cityFlag["Skýrophos"] = "[[File:SkyrophosFlag.png|20px]]"
     Sufriya="[[File:SufriyaFlag.png|20px]] Sufriya",
cityFlag["Port of Corumia"] = "[[File:CorumiaFlag.png|20px]]"
     ["Keybir-Aviv"]="[[File:KeybirAvivFlag.png|20px]] Keybir-Aviv",
cityFlag["Port of Euranidom"] = "[[File:Euranidom.png|20px]]"
     Aegirheim="[[File:AegirheimFlag.png|20px]] Aegirheim",
cityFlag["Port of Southern Keltiania"] = "[[File:SouthKeltiania.png|20px]]"
    Bjornopolis="[[File:BjornopolisFlag.png|20px]] Bjornopolis",
 
local ownerOperatorFlag = {
     ["Port of Corumia"] = "CorumiaFlag.png",
     ["Port of Euranidom"] = "Euranidom.png",
     ["Port of Southern Keltiania"] = "SouthKeltiania.png",
}
}


local vesselTypes     = { "Cargo", "Military", "Ritual", "Privateer" }
-- 3b) Hatch Ministry-aligned cluster map
local cargoTypes      = { "Agricultural", "Mining/Manufacturing", "Services", "Unspecified" }
local cityRegion = {
local ritualCults     = {
     ["Aegirheim"]    = "New South Jangsong",
     "Ordo Solis Invicti","Order of Aurora Mystica","Harmony Sanctum","Ignis Aeternum",
    ["Skýrophos"]    = "New South Jangsong",
     "Celestial Harmony Sect","Guild of Golden Shadows","Azure Sentinel Sect","Reverie Nebulous",
    ["Bjornopolis"]  = "New South Jangsong",
     "Eon Fellowship","Order of the Umbral Oracle","Mystery of the Verdant Embrace","Conclave Illuminara",
    ["Norsolyra"]    = "New South Jangsong",
     "Sanctum Vitalis","Temple Alabaster","Court of the Ironclad","Accord Concordia",
    ["Ephyra"]      = "New South Jangsong",
     "Mystery of the Stygian Veil","Sylvan Fellowship","Mystery of Red Mirth","Rex Catonis",
 
     "Sanctum Delphica","Ordo Amicitia","Temple Illuminata"
     ["Symphonara"]  = "Vaeringheim",
     ["Delphica"]    = "Vaeringheim",
    ["Vaeringheim"]  = "Vaeringheim",
     ["Somniumpolis"] = "Odiferia",
 
    ["Keybir-Aviv"]  = "Haifan Bassaridia",
     ["Sufriya"]      = "Haifan Bassaridia",
    ["Jogi"]        = "Haifan Bassaridia",
 
     ["Ardclach"]    = "Bassaridian Normark",
    ["Riddersborg"]  = "Bassaridian Normark",
 
     ["Port of Corumia"] = "Port of Corumia",
    ["Port of Euranidom"] = "Port of Euranidom",
     ["Port of Southern Keltiania"] = "Port of Southern Keltiania",
}
}
local militaryDivs    = {
 
     "Alpazkigz Division","Odiferia Division","Vaeringheim Division","Haifa Division",
local regionHomeCities = {
     "Jeseri Division","Hatch Ministry Division","New South Jangsong Division","Haifan Bassaridia Division"
     ["New South Jangsong"] = {"Aegirheim","Skýrophos","Bjornopolis","Norsolyra","Ephyra"},
     ["Vaeringheim"]        = {"Symphonara","Delphica","Vaeringheim"},
    ["Odiferia"]          = {"Somniumpolis"},
    ["Haifan Bassaridia"]  = {"Keybir-Aviv","Sufriya","Jogi"},
    ["Bassaridian Normark"]= {"Ardclach","Riddersborg"},
 
    ["Port of Corumia"] = {"Port of Corumia"},
    ["Port of Euranidom"] = {"Port of Euranidom"},
    ["Port of Southern Keltiania"] = {"Port of Southern Keltiania"},
}
}
local privateerCaptains = {
 
    {first="Talvios", last="Karnis",    nick="Erebektos"},
local allCargoRegions = {
    {first="Mirek",  last="Davros",    nick="Zammarion"},
     "Haifan Bassaridia",
    {first="Ilion",  last="Quoros"},
     "Odiferia",
    {first="Sevon",  last="Halek",    nick="Thamuthis"},
     "Alpazkigz",
    {first="Caldris", last="Onir",      nick="Votharion"},
     "Vaeringheim",
    {first="Loras",  last="Tarnok"},
     "New South Jangsong",
    {first="Vaelen",  last="Koros"},
     "Bassaridian Normark",
    {first="Daskos",  last="Meyron",    nick="Orontion"},
     "Ouriana",
    {first="Kyric",  last="Fenros",    nick="Mekharsan"},
     "Caledonian Dependencies"
    {first="Omir",    last="Thalos"},
    {first="Baryon",  last="Syrek",    nick="Satharkon"},
    {first="Orien",  last="Damar"},
    {first="Tharkos", last="Nyros",    nick="Vathmenos"},
    {first="Halion",  last="Trevik"},
    {first="Kelvir",  last="Raan",      nick="Nabarrion"},
    {first="Lethan",  last="Kohr"},
    {first="Zarik",  last="Tharn"},
     {first="Varas",  last="Jorik",    nick="Tharnukhet"},
     {first="Yasrek",  last="Tymon"},
     {first="Aethon",  last="Morvek",    nick="Vathromen"},
     {first="Crivos",  last="Elyen"},
     {first="Saran",  last="Valic"},
     {first="Drevos",  last="Helkan",    nick="Khamsuron"},
     {first="Nyros",  last="Qalor"},
     {first="Talen",  last="Vorric",    nick="Valkhoret"}
}
}


-- 4) master product lists
local function randomHomeForRegion(region)
local agriProducts = {
    local pool = regionHomeCities[region]
    "Rice — Gladeseed Farmers' Union",
    if pool and #pool > 0 then return randomChoice(pool) end
    "Edible Algae — Gladeseed Farmers' Union",
    return randomChoice(cities)
    "Shrimp — Gladeseed Farmers' Union",
end
    "Anterran Spicy Onion — Gladeseed Farmers' Union",
 
    "Queen's Garlic — Gladeseed Farmers' Union",
-- 4) Vessel & cargo types
     "Morovian Water Buffalo — Atterian Cattle Drivers",
local vesselTypes = {"Cargo","Military","Ritual","Privateer"}
    "Morovian Water Buffalo Beef — Atterian Cattle Drivers",
local cargoTypes  = {"Agricultural","Mining/Manufacturing","Services","Unspecified"}
     "Unprocessed Noctic-Rabrev Flower — Noctic Fleet",
 
    "Processed Noctic Rabrev Flower — Noctic Fleet",
-- 5) Owner pools
    "Noctic-Rabrev Flower Concentrate — Noctic Fleet",
local ritualCults = {
     "Unprocessed Noctic-Rabrev Leaf — Noctic Fleet",
     "Order of Aurora Mystica","Harmony Sanctum",
    "Processed Noctic-Rabrev Leaf — Noctic Fleet",
     "Ignis Aeternum","Celestial Harmony Sect","Guild of Golden Shadows",
    "Unprocessed Noctic-Rabrev Leaf Concentrate — Noctic Fleet",
     "Azure Sentinel Sect","Reverie Nebulous","Eon Fellowship",
     "Processed Noctic-Rabrev Leaf Concentrate — Noctic Fleet",
     "Order of the Umbral Oracle","Mystery of the Verdant Embrace",
    "Manwine — Jogiani Merchants of Manwine",
     "Conclave Illuminara","Sanctum Vitalis","Temple Alabaster",
     "Milkwine — Jogiani Merchants of Manwine",
     "Court of the Ironclad","Accord Concordia","Mystery of the Stygian Veil",
     "Zoe Elm Resin (Unrefined) — Keepers of the Grove of Zoe Elm",
     "Sylvan Fellowship","Mystery of Red Mirth","Rex Catonis",
     "Zoe Elm Resin (Refined) — Keepers of the Grove of Zoe Elm",
     "Sanctum Delphica","Ordo Amicitia","Temple Illuminata"
     "Sea Elm Flavored Mead — Sea-Elm Southern Meadery",
}
     "Wheat — Plains of Jogi",
local militaryDivs = {
     "Oats — Plains of Jogi",
     "Alpazkigz Division","Odiferia Division","Vaeringheim Division",
     "Cereal — Plains of Jogi",
     "Haifa Division","Jeseri Division","Hatch Ministry Division",
     "Hay — Plains of Jogi",
     "New South Jangsong Division","Haifan Bassaridia Division"
    "Ale of the Night — Ale of the Night Central Brewery",
}
     "Camels — Camel Herders of the Jangsong Province",
local agriCompanies = {
    "Camel Meat — Camel Herders of the Jangsong Province",
     "Gladeseed Farmers' Union","Atterian Cattle Drivers",
     "Camel Milk — Camel Herders of the Jangsong Province",
     "Jogiani Merchants of Manwine","Keepers of the Grove of Zoe Elm",
    "Camel Hump Water — Camel Herders of the Jangsong Province",
     "Plains of Jogi","Ale of the Night Central Brewery",
     "Camel Hump Water Tea — Camel Herders of the Jangsong Province",
     "Camel Herders of the Jangsong Province","Anterran Imports and Services",
    "Pearls — Anterran Imports and Services",
     "Merchant Guild of the Poppy Goddess","Grand Cave Bee Meadery",
     "Abalone — Anterran Imports and Services",
     "Cinammon Plains of Rouge","The Velvet Company","Seamstresses of Rouge",
    "Opium — Merchant Guild of the Poppy Goddess",
     "Salurian Temple of Sacred Horehound","Bulhanu Ranchers' Association",
    "Hash — Merchant Guild of the Poppy Goddess",
     "Suncliff Fisheries","Ministers of Mistress Mugwort",
    "Mead — Grand Cave Bee Meadery",
     "Blood Vineyards of the Far North",
     "Cinammon — Cinammon Plains of Roue",
     "Norsolyrian Wisp Rice Farmers Association",
    "Velvet Worm Meat — The Velvet Company",
     "Giid-Lisea Ranching Import Company","Bird Keepers of Saluria",
    "Velvet Worm Eggs — The Velvet Company",
     "Hunters of the Giant Fanged Penguin","Knights Castle Reindeer Herders",
    "Silk — Seamstresses of Rouge",
    "Lindley Whaling Company","Aminian Coffee Company","Granadita Collective",
     "Horehound Mead — Salurian Temple of Sacred Horehound",
     "Herd Caton","Wasp Hunters of the Austral Isles", "Sea Elm Southern Meadery",
    "Horehound Beer — Salurian Temple of Sacred Horehound",
     "Nefel Village","Diamonia Sugar Cooperatives", "Ouriana Lowlands Farmers' Association",
    "Sea Cow Beef — Bulhanu Ranchers' Association",
     "Nefel Village", "Ouriad River Fisheries Cooperative", "Caspazani Livestock Company",
     "Atterian Armored Pufferfish — Suncliff Fisheries",
     "White Ruby Communes", "Fange Velvet", "Lamian Ward", "Sleva Farming Supplies Cooperative",
    "Morovian Saucer Eyed Giant Eel — Suncliff Fisheries",
     "Hrimmanes Woolhorse Company"," King's Home Fur Trappers' Cooperative", "Caledonj Bay Fishermen’s’ Cooperatives", "Salain's Landing Granary Complex","Grove Synaxis of Tel-Amin",
    "Amina Crab — Suncliff Fisheries",
    "Mugwort Oil — Ministers of Mistress Mugwort",
     "Ground Mugwort — Ministers of Mistress Mugwort",
    "Mugwort Tincture — Ministers of Mistress Mugwort",
    "Blood InfusedRed Wine — Blood Vineyards of the Far North",
     "Wisp Rice — Norsolyrian Wisp Rice Farmers Association",
     "Giid-Lisea-Eda — Giid Lisea Ranching Import Company",
     "Giid-Lisea-Eda Beef — Giid Lisea Ranching Import Company",
     "Vaeringheim Turkey Meat — Bird Keepers of Saluria",
     "Salurian Swamp Quail Eggs — Bird Keepers of Saluria",
     "Giant Fanged Penguin Blubber — Hunters of the Giant Fanged Penguin",
     "Giant Fanged Penguin Blubber Oil Extract — Hunters of the Giant Fanged Penguin",
     "Coffee — Aminian Coffee Company"
}
}
local miningProducts = {
local miningCompanies = {
     "Purple Granite — Thalassian Temples Granite Export Cooperative",
     "Roving Wind Farm Corporation",
     "Iron — East Keltian Iron Company",
     "Thalassian Temple Granite Export Cooperative","East Keltian Iron Company",
    "Steel — East Keltian Iron Company",
     "Mylecian Coal Ports","Merchants of the Valley of Diamonds",
    "Concrete — East Keltian Iron Company",
     "East Keltian Timber Company","Onceanic Boot Company",
     "Coal — Mylecian Coal Ports",
     "Commune of the Garganid Apostles","Shï Collective","Order of the Sphinx",
    "Diamonds — Merchants of the Valley of Diamonds",
     "Market Hanavusu","Somniant Stock Fund","Copper Mines of Southern Jogi",
     "Timber — East Keltian Timber Company",
     "Horned Bear Timber Company","Federation of North Brabipant",
    "Men’s Boots — Onceanic Boot Company",
     "Southern Strait Preservation Corporation","Eosphorus Motor Company",
    "Women’s Boots — Onceanic Boot Company",
     "Vortelin Electronics", "Thyrsiad Rubber Company","Stygium Mines of Acheron",
     "Garganram Sign Plains Fertilizer — Commune of the Garganid Apostles",
     "Trans-Rodinian Granite Agency", "Amber Ports of Slevik", "Thesean Iron Exports",
    "Gold Necklaces — Shï Collective",
     "Fanghorn Highland Gas Company","Gadial Oil", "Tharsyr Shoal Salt Works",
     "Gold Rings — Shï Collective",
    "Gold Bracelets — Shï Collective",
     "Gold Earrings — Shï Collective",
    "Robes — Order of the Sphinx",
     "Dresses — Order of the Sphinx",
     "Tarnkappes — Order of the Sphinx",
     "Coats — Order of the Sphinx",
    "Perfume — Market Hanavusu",
     "Cologne — Market Hanavusu",
    "Incense — Market Hanavusu",
    "Candles — Market Hanavusu"
}
}
local serviceCompanies = {
local serviceCompanies = {
    "Ergonian Spy Agency",
    "Temple Bank of the Reformed Stripping Path",
    "Temple University of Delphica","La Sái Ebile",
    "Pharmacon Sect",
    "Skyrophian Security Contractors",
    "Haifadora Postal Service",
    "Fleet of Rouge Divers",
    "Merchant Caravans of the Candy Kingdom",
    "Hellhound Breeders of Dragevik",
    "Noctic Fleet",
    "Maritime Guild of the Cult of Maskmakers",
    "Couriers of the Lizard Queen",
    "Temple of Aprobelle",
    "Sårensby Depot*",
}
-- special pools
local privateerCompanies = {
     "Lake Morovia Blockade Fund",
     "Lake Morovia Blockade Fund",
     "Court of the Dark Harpy",
     "Baratar Corporation",
     "Hatch Ministry",
     "Hatch Ministry",
    "Skyrophian Security Contractors",
    "Temple of Aprobelle",
    "Noctic Fleet",
     "Maritime Guild of the Cult of Maskmakers",
     "Maritime Guild of the Cult of Maskmakers",
    "Merchant Caravans of the Candy Kingdom",
    "Haifadora Postal Service",
    "Fleet of Rouge Divers",
     "Ergonian Spy Agency",
     "Ergonian Spy Agency",
     "Temple Bank of the Reformed Stripping Path",
     "Southern Strait Preservation Corporation",
    "Couriers of the Lizard Queen",
    "Somniant Stock Fund"
     "Temple University of Delphica",
}
     "La Sái Ebile",
 
     "Hellhound Breeders of Dragevik"
local hatchMinistryCompanies = {
    "Hatch Ministry"
}
 
local portAuthorityCompanies = {
    "Port of Corumia",
    "Port of Euranidom",
    "Port of Southern Keltiania",
}
 
local portAuthorityLookup = {}
for _, name in ipairs(portAuthorityCompanies) do
    portAuthorityLookup[name] = true
end
 
local function isPortAuthority(name)
    return portAuthorityLookup[name] == true
end
 
-- 5b) Company classification + Hatch-aligned source region metadata
local companyMeta = {
    -- Agriculture
    ["Gladeseed Farmers' Union"] =                {class="Agricultural", subtype="grain", region="Vaeringheim"},
    ["Atterian Cattle Drivers"] =                  {class="Agricultural", subtype="livestock", region="New South Jangsong"},
    ["Jogiani Merchants of Manwine"] =            {class="Agricultural", subtype="wine_spirits", region="Haifan Bassaridia"},
    ["Keepers of the Grove of Zoe Elm"] =          {class="Agricultural", subtype="forest_products", region="Vaeringheim"},
    ["Plains of Jogi"] =                          {class="Agricultural", subtype="grain", region="Haifan Bassaridia"},
    ["Ale of the Night Central Brewery"] =        {class="Agricultural", subtype="wine_spirits", region="Vaeringheim"},
    ["Camel Herders of the Jangsong Province"] =  {class="Agricultural", subtype="livestock", region="New South Jangsong"},
    ["Anterran Imports and Services"] =            {class="Agricultural", subtype="mixed_foodstuffs", region="Vaeringheim"},
    ["Merchant Guild of the Poppy Goddess"] =      {class="Agricultural", subtype="herbs", region="Vaeringheim"},
    ["Grand Cave Bee Meadery"] =                  {class="Agricultural", subtype="mead_spirits", region="Vaeringheim"},
    ["Cinammon Plains of Rouge"] =                {class="Agricultural", subtype="spices", region="Vaeringheim"},
    ["The Velvet Company"] =                      {class="Agricultural", subtype="textiles_raw", region="Vaeringheim"},
    ["Seamstresses of Rouge"] =                    {class="Agricultural", subtype="textiles_raw", region="Vaeringheim"},
    ["Salurian Temple of Sacred Horehound"] =      {class="Agricultural", subtype="herbs", region="Alpazkigz"},
    ["Bulhanu Ranchers' Association"] =            {class="Agricultural", subtype="livestock", region="Odiferia"},
    ["Suncliff Fisheries"] =                      {class="Agricultural", subtype="fish", region="Vaeringheim"},
    ["Ministers of Mistress Mugwort"] =            {class="Agricultural", subtype="herbs", region="Vaeringheim"},
    ["Blood Vineyards of the Far North"] =        {class="Agricultural", subtype="wine_spirits", region="New South Jangsong"},
    ["Norsolyrian Wisp Rice Farmers Association"] = {class="Agricultural", subtype="grain", region="New South Jangsong"},
    ["Giid-Lisea Ranching Import Company"] =      {class="Agricultural", subtype="livestock", region="Vaeringheim"},
    ["Bird Keepers of Saluria"] =                  {class="Agricultural", subtype="animal_products", region="Alpazkigz"},
    ["Hunters of the Giant Fanged Penguin"] =      {class="Agricultural", subtype="animal_products", region="Caledonian Dependencies"},
    ["Knights Castle Reindeer Herders"] =          {class="Agricultural", subtype="livestock", region="Bassaridian Normark"},
    ["Lindley Whaling Company"] =                  {class="Agricultural", subtype="marine_oil", region="Caledonian Dependencies"},
    ["Aminian Coffee Company"] =                  {class="Agricultural", subtype="coffee", region="Haifan Bassaridia"},
    ["Grove Synaxis of Tel-Amin"] =                {class="Agricultural", subtype="fruit", region="Haifan Bassaridia"},
    ["Granadita Collective"] =                    {class="Agricultural", subtype="fruit", region="Haifan Bassaridia"},
    ["Herd Caton"] =                              {class="Agricultural", subtype="livestock", region="Vaeringheim"},
    ["Wasp Hunters of the Austral Isles"] =        {class="Agricultural", subtype="animal_products", region="Caledonian Dependencies"},
    ["Sea Elm Southern Meadery"] =                {class="Agricultural", subtype="mead_spirits", region="Vaeringheim"},
    ["Nefel Village"] =                            {class="Agricultural", subtype="mixed_foodstuffs", region="Vaeringheim"},
    ["Diamonia Sugar Cooperatives"] =              {class="Agricultural", subtype="sugar", region="Haifan Bassaridia"},
    ["Ouriana Lowlands Farmers' Association"] =    {class="Agricultural", subtype="grain", region="Ouriana"},
    ["Ouriad River Fisheries Cooperative"] =      {class="Agricultural", subtype="fish", region="Ouriana"},
    ["Caspazani Livestock Company"] =              {class="Agricultural", subtype="livestock", region="Ouriana"},
    ["White Ruby Communes"] =                      {class="Agricultural", subtype="mixed_foodstuffs", region="Vaeringheim"},
    ["Fange Velvet"] =                            {class="Agricultural", subtype="textiles_raw", region="Caledonian Dependencies"},
    ["Lamian Ward"] =                              {class="Agricultural", subtype="mixed_foodstuffs", region="Vaeringheim"},
    ["Sleva Farming Supplies Cooperative"] =      {class="Agricultural", subtype="farm_inputs", region="Caledonian Dependencies"},
    ["Hrimmanes Woolhorse Company"] =              {class="Agricultural", subtype="livestock", region="Caledonian Dependencies"},
    [" King's Home Fur Trappers' Cooperative"] =  {class="Agricultural", subtype="animal_products", region="Bassaridian Normark"},
    ["Caledonj Bay Fishermen’s’ Cooperatives"] =  {class="Agricultural", subtype="fish", region="Caledonian Dependencies"},
    ["Salain's Landing Granary Complex"] =        {class="Agricultural", subtype="grain", region="Vaeringheim"},
 
    -- Mining / Manufacturing / Energy
    ["Roving Wind Farm Corporation"] =            {class="Mining/Manufacturing", subtype="energy", region="Vaeringheim"},
    ["Thalassian Temple Granite Export Cooperative"] = {class="Mining/Manufacturing", subtype="stone", region="Vaeringheim"},
    ["East Keltian Iron Company"] =                {class="Mining/Manufacturing", subtype="metals", region="Vaeringheim"},
    ["Mylecian Coal Ports"] =                      {class="Mining/Manufacturing", subtype="coal", region="Vaeringheim"},
    ["Merchants of the Valley of Diamonds"] =      {class="Mining/Manufacturing", subtype="gemstones", region="Haifan Bassaridia"},
    ["East Keltian Timber Company"] =              {class="Mining/Manufacturing", subtype="timber", region="Vaeringheim"},
    ["Onceanic Boot Company"] =                    {class="Mining/Manufacturing", subtype="consumer_goods", region="Vaeringheim"},
    ["Commune of the Garganid Apostles"] =        {class="Mining/Manufacturing", subtype="stone", region="Vaeringheim"},
    ["Shï Collective"] =                          {class="Mining/Manufacturing", subtype="luxury_goods", region="Vaeringheim"},
    ["Order of the Sphinx"] =                      {class="Mining/Manufacturing", subtype="luxury_goods", region="Haifan Bassaridia"},
    ["Market Hanavusu"] =                          {class="Mining/Manufacturing", subtype="consumer_goods", region="New South Jangsong"},
    ["Somniant Stock Fund"] =                      {class="Mining/Manufacturing", subtype="arms_industrial", region="Vaeringheim"},
    ["Copper Mines of Southern Jogi"] =            {class="Mining/Manufacturing", subtype="metals", region="Haifan Bassaridia"},
    ["Horned Bear Timber Company"] =              {class="Mining/Manufacturing", subtype="timber", region="New South Jangsong"},
    ["Federation of North Brabipant"] =            {class="Mining/Manufacturing", subtype="consumer_goods", region="Caledonian Dependencies"},
    ["Southern Strait Preservation Corporation"] = {class="Mining/Manufacturing", subtype="marine_materials", region="Haifan Bassaridia"},
    ["Eosphorus Motor Company"] =                  {class="Mining/Manufacturing", subtype="vehicles", region="Vaeringheim"},
    ["Vortelin Electronics"] =                    {class="Mining/Manufacturing", subtype="electronics", region="Vaeringheim"},
    ["Thyrsiad Rubber Company"] =                  {class="Mining/Manufacturing", subtype="rubber", region="Haifan Bassaridia"},
    ["Stygium Mines of Acheron"] =                {class="Mining/Manufacturing", subtype="metals", region="Alpazkigz"},
    ["Trans-Rodinian Granite Agency"] =            {class="Mining/Manufacturing", subtype="stone", region="Vaeringheim"},
    ["Amber Ports of Slevik"] =                    {class="Mining/Manufacturing", subtype="amber", region="Caledonian Dependencies"},
    ["Thesean Iron Exports"] =                    {class="Mining/Manufacturing", subtype="metals", region="Ouriana"},
    ["Fanghorn Highland Gas Company"] =            {class="Mining/Manufacturing", subtype="gas", region="Caledonian Dependencies"},
    ["Gadial Oil"] =                              {class="Mining/Manufacturing", subtype="oil", region="Vaeringheim"},
    ["Tharsyr Shoal Salt Works"] =                {class="Mining/Manufacturing", subtype="salt", region="Vaeringheim"},
 
    -- Services
    ["Ergonian Spy Agency"] =                      {class="Services", subtype="intelligence", region="Vaeringheim"},
    ["Temple Bank of the Reformed Stripping Path"] = {class="Services", subtype="finance", region="Vaeringheim"},
     ["Temple University of Delphica"] =            {class="Services", subtype="education", region="Vaeringheim"},
     ["La Sái Ebile"] =                            {class="Services", subtype="commerce", region="Haifan Bassaridia"},
     ["Pharmacon Sect"] =                          {class="Services", subtype="medical", region="Alpazkigz"},
    ["Skyrophian Security Contractors"] =          {class="Services", subtype="security", region="New South Jangsong"},
    ["Haifadora Postal Service"] =                {class="Services", subtype="courier", region="Haifan Bassaridia"},
    ["Fleet of Rouge Divers"] =                    {class="Services", subtype="marine_services", region="Vaeringheim"},
    ["Merchant Caravans of the Candy Kingdom"] =  {class="Services", subtype="logistics", region="Vaeringheim"},
    ["Hellhound Breeders of Dragevik"] =          {class="Services", subtype="animal_services", region="Bassaridian Normark"},
    ["Noctic Fleet"] =                            {class="Services", subtype="marine_services", region="Vaeringheim"},
    ["Maritime Guild of the Cult of Maskmakers"] = {class="Services", subtype="marine_services", region="Vaeringheim"},
    ["Couriers of the Lizard Queen"] =            {class="Services", subtype="courier", region="Vaeringheim"},
    ["Temple of Aprobelle"] =                      {class="Services", subtype="security", region="Vaeringheim"},
    ["Sårensby Depot*"] =                          {class="Services", subtype="logistics", region="Bassaridian Normark"},
    ["Port of Corumia"] =                          {class="Services", subtype="port_authority", region="Port of Corumia"},
    ["Port of Euranidom"] =                        {class="Services", subtype="port_authority", region="Port of Euranidom"},
    ["Port of Southern Keltiania"] =              {class="Services", subtype="port_authority", region="Port of Southern Keltiania"},
 
    -- Special pools
    ["Lake Morovia Blockade Fund"] =              {class="Services", subtype="security", region="Vaeringheim"},
    ["Baratar Corporation"] =                      {class="Mining/Manufacturing", subtype="arms_industrial", region="Haifan Bassaridia"},
    ["Hatch Ministry"] =                          {class="Services", subtype="security", region="Vaeringheim"},
}
}


-- 5) Pick a random privateer name
-- build a “general” pool excluding special-only firms
local generalPools = {}
for _, list in ipairs({agriCompanies, miningCompanies, serviceCompanies}) do
    for _, name in ipairs(list) do
        if name ~= "Hatch Ministry" then
            local isPri = false
            for _, pr in ipairs(privateerCompanies) do
                if name == pr then isPri = true break end
            end
            if not isPri then
                table.insert(generalPools, name)
            end
        end
    end
end
 
local function getCompanyMeta(name)
    return companyMeta[name] or {class="Unspecified", subtype="mixed", region="Vaeringheim"}
end
 
local function buildPoolByClassSubtypeRegion(className, subtype, region)
    local base = ({
        Agricultural            = agriCompanies,
        ["Mining/Manufacturing"] = miningCompanies,
        Services                = serviceCompanies
    })[className] or {}
 
    local pool = {}
    for _, name in ipairs(base) do
        if name ~= "Hatch Ministry" then
            local isPri = false
            for _, pr in ipairs(privateerCompanies) do
                if name == pr then isPri = true break end
            end
            if not isPri then
                local meta = getCompanyMeta(name)
                if meta.class == className and meta.subtype == subtype and meta.region == region then
                    table.insert(pool, name)
                end
            end
        end
    end
    return pool
end
 
local function buildGeneralPoolBySubtypeRegion(subtype, region)
    local pool = {}
    for _, name in ipairs(generalPools) do
        local meta = getCompanyMeta(name)
        if meta.subtype == subtype and meta.region == region then
            table.insert(pool, name)
        end
    end
    return pool
end
 
local function chooseManifestProfile(vt, ct)
    if vt == "Privateer" then
        local subtype = randomChoice({"security","arms_industrial","logistics","medical"})
        local region = randomChoice({"Haifan Bassaridia","Odiferia","Alpazkigz","Vaeringheim","New South Jangsong","Bassaridian Normark","Ouriana"})
        return subtype, region
    elseif vt == "Military" then
        local subtype = randomChoice({"security","arms_industrial","medical","logistics"})
        local region = randomChoice({"Haifan Bassaridia","Odiferia","Alpazkigz","Vaeringheim","New South Jangsong","Bassaridian Normark","Ouriana"})
        return subtype, region
    elseif vt == "Ritual" then
        local subtype = randomChoice({"finance","medical","courier","security","education","logistics"})
        local region = randomChoice({"Haifan Bassaridia","Odiferia","Alpazkigz","Vaeringheim","New South Jangsong","Bassaridian Normark"})
        return subtype, region
    elseif ct == "Agricultural" then
        local subtype = randomChoice({
            "grain","livestock","wine_spirits","forest_products","herbs",
            "mead_spirits","spices","textiles_raw","fish","animal_products",
            "marine_oil","coffee","fruit","sugar","mixed_foodstuffs","farm_inputs"
        })
        local region = randomChoice(allCargoRegions)
        return subtype, region
elseif ct == "Mining/Manufacturing" then
    local subtype = randomChoice({
        "energy","stone","salt","metals","coal","gemstones","timber","consumer_goods",
        "luxury_goods","arms_industrial","marine_materials","vehicles",
        "electronics","rubber","amber","gas","oil"
    })
        local region = randomChoice(allCargoRegions)
        return subtype, region
elseif ct == "Services" then
    if math.random() < 0.20 then
        local port = randomChoice(portAuthorityCompanies)
        return "port_authority", port
    else
        local subtype = randomChoice({
            "intelligence","finance","education","commerce","medical",
            "security","courier","marine_services","logistics","animal_services"
        })
        local region = randomChoice(allCargoRegions)
        return subtype, region
    end
    else
        local source = randomChoice(generalPools)
        local meta = getCompanyMeta(source)
        return meta.subtype, meta.region
    end
end
 
local function pickCargoCompanies(vt, owner, ct, subtype, region, count)
    if vt == "Cargo" and isPortAuthority(region) then
        return { region }
    end
    if vt == "Privateer" then
        local pool = {}
 
        for _, name in ipairs(privateerCompanies) do
            local meta = getCompanyMeta(name)
            if meta.region == region then
                if subtype == "security" and meta.subtype == "security" then
                    table.insert(pool, name)
                elseif subtype == "arms_industrial" and meta.subtype == "arms_industrial" then
                    table.insert(pool, name)
                elseif subtype == "logistics" and (
                    meta.subtype == "logistics" or
                    meta.subtype == "courier" or
                    meta.subtype == "marine_services"
                ) then
                    table.insert(pool, name)
                elseif subtype == "medical" and meta.subtype == "medical" then
                    table.insert(pool, name)
                end
            end
        end
 
        if #pool == 0 then
            for _, name in ipairs(privateerCompanies) do
                local meta = getCompanyMeta(name)
                if meta.subtype == "security"
                    or meta.subtype == "arms_industrial"
                    or meta.subtype == "logistics"
                    or meta.subtype == "courier"
                    or meta.subtype == "marine_services"
                    or meta.subtype == "medical" then
                    table.insert(pool, name)
                end
            end
        end
 
        return randomN(pool, math.min(count, #pool))
 
    elseif owner == "Hatch Ministry Division" then
        return randomN(hatchMinistryCompanies, 1)
 
    elseif ct == "Unspecified" then
        local pool = buildGeneralPoolBySubtypeRegion(subtype, region)
        if #pool == 0 then
            local fallback = {}
            for _, name in ipairs(generalPools) do
                local meta = getCompanyMeta(name)
                if meta.region == region then
                    table.insert(fallback, name)
                end
            end
            pool = fallback
        end
        return randomN(pool, math.min(count, #pool))
 
    else
        local pool = buildPoolByClassSubtypeRegion(ct, subtype, region)
        if #pool == 0 then
            local fallback = {}
            local base = ({
                Agricultural            = agriCompanies,
                ["Mining/Manufacturing"] = miningCompanies,
                Services                = serviceCompanies
            })[ct] or {}
            for _, name in ipairs(base) do
                if name ~= "Hatch Ministry" then
                    local isPri = false
                    for _, pr in ipairs(privateerCompanies) do
                        if name == pr then isPri = true break end
                    end
                    if not isPri then
                        local meta = getCompanyMeta(name)
                        if meta.region == region then
                            table.insert(fallback, name)
                        end
                    end
                end
            end
            pool = fallback
        end
        return randomN(pool, math.min(count, #pool))
    end
end
 
-- 6) Company logos mapping
local companyLogo = {}
local function addLogo(name,file) companyLogo[name]=file end
addLogo("Lake Morovia Blockade Fund","BlockadeFund420.png")
addLogo("Gladeseed Farmers' Union","Gladeseed420.png")
addLogo("Atterian Cattle Drivers","Atterian420.png")
addLogo("Noctic Fleet","Noctic420.png")
addLogo("Jogiani Merchants of Manwine","Jogiani420.png")
addLogo("Keepers of the Grove of Zoe Elm","KeepersoftheGrove.png")
addLogo("Plains of Jogi","PlainsofJogi.png")
addLogo("Ale of the Night Central Brewery","AleofNight420.png")
addLogo("Camel Herders of the Jangsong Province","CamelHerders420.png")
addLogo("Thalassian Temple Granite Export Cooperative","ThalassianGranite420.png")
addLogo("East Keltian Iron Company","EKIron420.png")
addLogo("Mylecian Coal Ports","CoalPorts420.png")
addLogo("Merchants of the Valley of Diamonds","ValleyofDiamonds420.png")
addLogo("Baratar Corporation","Baratar420.png")
addLogo("Roving Wind Farm Corporation","RovingWind420.png")
addLogo("Grand Cave Bee Meadery","GrandCave420.png")
addLogo("Merchant Guild of the Poppy Goddess","PoppyGoddess420.png")
addLogo("Cinammon Plains of Rouge","CinammonPlains420.png")
addLogo("The Velvet Company","VelvetHome420.png")
addLogo("Seamstresses of Rouge","Seamstresses420.png")
addLogo("Salurian Temple of Sacred Horehound","SalurianTemple421.png")
addLogo("Bulhanu Ranchers' Association","Bulhanu421.png")
addLogo("Suncliff Fisheries","Suncliff421.png")
addLogo("Ministers of Mistress Mugwort","Mugwort421.png")
addLogo("Blood Vineyards of the Far North","BloodVineyards421.png")
addLogo("Norsolyrian Wisp Rice Farmers Association","Norsolyrian421.png")
addLogo("Giid-Lisea Ranching Import Company","GiidLisea421.png")
addLogo("Bird Keepers of Saluria","SaluriaBK421.png")
addLogo("Knights Castle Reindeer Herders","KnightsCastle421.png")
addLogo("Lindley Whaling Company","LindleyWhalingCompany.png")
addLogo("Aminian Coffee Company","Aminian421.png")
addLogo("Grove Synaxis of Tel-Amin","GroveSynaxisLogo.png")
addLogo("Commune of the Garganid Apostles","Garganid421.png")
addLogo("Onceanic Boot Company","OnceanicBoot421.png")
addLogo("Maritime Guild of the Cult of Maskmakers","MaritimeGuild420.png")
addLogo("Ergonian Spy Agency","ErgonianSpy421.png")
addLogo("Temple Bank of the Reformed Stripping Path","TempleBankCrest.png")
addLogo("Couriers of the Lizard Queen","LizardQueen421.png")
addLogo("Temple University of Delphica","TempleUniversity421.png")
addLogo("Shï Collective","ShiCollective421.png")
addLogo("Order of the Sphinx","OrderofSphinx421.png")
addLogo("Market Hanavusu","MarketHanavusu421.png")
addLogo("Somniant Stock Fund","SomniantStockFund.png")
addLogo("Hatch Ministry","HatchMinistry420.png")
addLogo("Anterran Imports and Services","AnterranImports420.png")
addLogo("East Keltian Timber Company","EKTimber420.png")
addLogo("Hellhound Breeders of Dragevik","Dragevik421.png")
addLogo("La Sái Ebile","LaSie421.png")
addLogo("Hunters of the Giant Fanged Penguin","FangedPenguin421.png")
addLogo("Commune of the Garganid Apostles","Garganid421.png")
addLogo("Copper Mines of Southern Jogi","CopperMinesLogoC.png")
addLogo("Horned Bear Timber Company","HornedBearTimberLogo.png")
addLogo("Granadita Collective","GranaditaCollectiveLogo.png")
addLogo("Pharmacon Sect","PharmaconSect.png")
addLogo("Skyrophian Security Contractors","SkyrophianSecurity.png")
addLogo("Haifadora Postal Service","HaifadoraPostalLogo.png")
addLogo("Herd Caton","HerdCatonLogo.png")
addLogo("Wasp Hunters of the Austral Isles","WaspHuntersLogo.png")
addLogo("Fleet of Rouge Divers","RougeDiversLogo.png")
addLogo("Sea Elm Southern Meadery","SeaElmLogo.png")
addLogo("Candy Merchants of the Candy Kingdom","Candymerchantslogo.png")
addLogo("Federation of North Brabipant","NorthBrabipantlogo.png")
addLogo("Southern Strait Preservation Corporation","SouthernStraitPreservationLogo.png")
addLogo("Eosphorus Motor Company","EosphorusLogo.png")
addLogo("Vortelin Electronics","VortelinElectronicsLogo.png")
addLogo("Thyrsiad Rubber Company","ThyrsiadRubberLogo.png")
addLogo("Nefel Village","NefelVillageLogo.png")
addLogo("Stygium Mines of Acheron","StygiumMinesLogo.png")
addLogo("Temple of Aprobelle","TempleAprobelleLogo.png")
addLogo("Diamonia Sugar Cooperatives","DiamoniaSugarLogo.png")
addLogo("Ouriana Lowlands Farmers' Association","OurianaLowlandsLogo.png")
addLogo("Ouriad River Fisheries Cooperative","OuriadRiverLogo.png")
addLogo("Caspazani Livestock Company","CaspazaniLogo.png")
addLogo("Trans-Rodinian Granite Agency","TransRodinanLogo.png")
addLogo("White Ruby Communes","WhiteRubyLogo.png")
addLogo("Fange Velvet","FangeVelvetLogo.png")
addLogo("Sårensby Depot","SårensbyDepotLogo.png")
addLogo("Amber Ports of Slevik","AmberPortsLogo2.png")
addLogo("Port of Corumia","PortofCorumiaLogo.png")
addLogo("Port of Euranidom","PortofEuranidomLogo.png")
addLogo("Port of Southern Keltiania","PortofSouthernKeltianiaLogo.png")
addLogo("Lamian Ward","LamianWardLogo.png")
addLogo("Sleva Farming Supplies Cooperatives","SlevaFarmingLogo.png")
addLogo("Hrimmanes Woolhorse Company","HrimmanesLogoB.png.png")
addLogo("Thesean Iron Exports","TheseanIronExports.png")
addLogo("King's Home Fur Trappers' Cooperative","KingsHomeCoLogo.png.png")
addLogo("Fanghorn Highland Gas Company","FanghornGasLogo.png")
addLogo("Caledonj Bay Fishermen’s’ Cooperatives","CaledonjBayLogo.png")
addLogo("Gadial Oil","GadialOilLogoB.png")
addLogo("Alpazkigz Division",        "AlpazkigzDivisionFlag.png")
addLogo("Odiferia Division",        "OdiferiaFlag.png")
addLogo("Vaeringheim Division",      "VaeringheimDivisionFlag.png")
addLogo("Haifa Division",            "HafaanFlag.png")
addLogo("Jeseri Division",          "JeseriFlag.png")
addLogo("Hatch Ministry Division",  "HatchMinistryFlag.png")
addLogo("New South Jangsong Division","NewSouthJangsongFlag.png")
addLogo("Haifan Bassaridia Division", "HaifaDivisionFlag.png")
addLogo("Order of Aurora Mystica",        "Auroramystica.png")
addLogo("Harmony Sanctum",        "Harmonysanctum.png")
addLogo("Ignis Aeternum",      "Ignis Aeternum.png")
addLogo("Celestial Harmony Sect",            "HarmonySect.png")
addLogo("Guild of Golden Shadows",          "GoldenShadows.png")
addLogo("Azure Sentinel Sect",  "AzureSentinel.png")
addLogo("Reverie Nebulous","RevNeb.png")
addLogo("Eon Fellowship", "EonFel.png")
addLogo("Order of the Umbral Oracle", "Umbral.png")
addLogo("Mystery of the Verdant Embrace", "Verdant.png")
addLogo("Conclave Illuminara", "Conclave.png")
addLogo("Sanctum Vitalis", "Vitalis.png")
addLogo("Temple Alabaster", "Alabaster.png")
addLogo("Court of the Ironclad", "Ironclad.png")
addLogo("Accord Concordia", "Concordia.png")
addLogo("Mystery of the Stygian Veil", "StygianVe.png")
addLogo("Sylvan Fellowship", "Sylvan.png")
addLogo("Mystery of Red Mirth", "MirthFlag.png")
addLogo("Rex Catonis", "Catonis.png")
addLogo("Sanctum Delphica", "SanctumDel.png")
addLogo("Ordo Amicitia", "OrdoFl.png")
addLogo("Temple Illuminata", "AureumFlag.png")
addLogo("Tharsyr Shoal Salt Works","TharsyrShoalSaltWorksLogo.png")
addLogo("Salain's Landing Granary Complex","SalainsLandingGranaryLogo.png")
 
-- 7) Privateer flags + helper
local privateers = {
    {first="Arion",    last="Theron"},
    {first="Cassius",  last="Valerus"},
    {first="Nefra",    last="Sekeri",  nick="Sekhem"},
    {first="Lysander", last="Pallas"},
    {first="Marwan",  last="Othman"},
    {first="Octavia",  last="Marcellus"},
    {first="Amenhotep",last="Ankhu"},
    {first="Thalia",  last="Chrysos"},
    {first="Demetrius",last="Gravis"},
    {first="Selim",    last="Bey"},
    {first="Julius",  last="Drusus"},
    {first="Leyla",    last="Han"},
    {first="Marcus",  last="Flavianus"},
    {first="Fatima",  last="Pasha"},
    {first="Cornelia", last="Urbanus"},
    {first="Eudora",  last="Merit"},
    {first="Perseus",  last="Phoebus"},
    {first="Khepri",  last="Sobek"},
    {first="Lucius",  last="Aurelian"},
    {first="Aisha",    last="Dinar"},
    {first="Gaius",    last="Cassianus"},
    {first="Selene",  last="Argus"},
    {first="Arsinoe",  last="Menkaure"},
    {first="Omar",    last="Zahir"},
    {first="Dorian",  last="Grimm"},
    {first="Quintus",  last="Nero"},
    {first="Zainab",  last="Sultan"},
    {first="Cassian",  last="Corinth"},
    {first="Valeria",  last="Maximus",  nick="Ferox"},
    {first="Thorne",  last="Noctis",  nick="Mainomenos"}
}
local function getRandomPrivateer()
local function getRandomPrivateer()
     local c= randomChoice(privateerCaptains)
     local c = randomChoice(privateers)
     if c.nick and math.random()<0.25 then
    local flag = string.format("[[File:%s.png|20px]] ", c.last)
         return string.format("Captain %s “%s” %s",c.first,c.nick,c.last)
     if c.nick and math.random() < 0.20 then
         return string.format("%sCaptain %s ‘%s’ %s", flag, c.first, c.nick, c.last)
     else
     else
         return string.format("Captain %s %s",c.first,c.last)
         return string.format("%sCaptain %s %s", flag, c.first, c.last)
     end
     end
end
end


-- 6) Attach Owner/Operator
-- 8) Determine Owner/Operator
local function getOwner(vtype, cargotype)
local function getOwner(vt, ct, subtype, region)
     if vtype=="Ritual"   then return randomChoice(ritualCults) end
    if vt == "Cargo" and isPortAuthority(region) then
     if vtype=="Military" then return randomChoice(militaryDivs) end
        return region
     if vtype=="Privateer" then return getRandomPrivateer() end
    end
     -- Cargo
     if vt == "Ritual" then
    if cargotype=="Agricultural" then return randomChoice(serviceCompanies) end
        return randomChoice(ritualCults)
    if cargotype=="Agricultural" then return "Gladeseed Farmers' Union" end
     elseif vt == "Military" then
    if cargotype=="Mining/Manufacturing" then
        return randomChoice(militaryDivs)
        return randomChoice{ "Baratar Corporation","Roving Wind Farm Corporation",
     elseif vt == "Privateer" then
             "Thalassian Temples Granite Export Cooperative","East Keltian Iron Company",
        return getRandomPrivateer()
             "Mylecian Coal Ports","Merchants of the Valley of Diamonds",
     elseif vt == "Cargo" then
             "East Keltian Timber Company","Onceanic Boot Company","Commune of the Garganid Apostles",
        if ct == "Agricultural" then
             "Shï Collective","Order of the Sphinx","Market Hanavusu","Somniant Stock Fund" }
            local pool = buildPoolByClassSubtypeRegion("Agricultural", subtype, region)
            return randomChoice(pool) or randomChoice(agriCompanies)
        elseif ct == "Mining/Manufacturing" then
            local pool = buildPoolByClassSubtypeRegion("Mining/Manufacturing", subtype, region)
            return randomChoice(pool) or randomChoice(miningCompanies)
        elseif ct == "Services" then
             local pool = buildPoolByClassSubtypeRegion("Services", subtype, region)
             return randomChoice(pool) or randomChoice(serviceCompanies)
        elseif ct == "Unspecified" then
             local pool = buildGeneralPoolBySubtypeRegion(subtype, region)
             return randomChoice(pool) or "Independent Consortium"
        end
     end
     end
    if cargotype=="Services" then return randomChoice(serviceCompanies) end
     return "Independent Consortium"
     return "Independent Consortium"
end
end


-- 7) Build the table
-- 9) Build the table
function p.generateShipTable()
function p.generateShipTable()
    math.randomseed(os.time())
    local y, d = p.getCurrentDate()
    local poolMap = {
        Agricultural = agriCompanies,
        ["Mining/Manufacturing"] = miningCompanies,
        Services = serviceCompanies
    }
    local totalTonnage = 0
     local ships = {}
     local ships = {}
     for i=1,math.random(50,80) do
 
         local hr, mn = math.random(0,23), math.random(0,59)
     for i = 1, math.random(50, 80) do
         local t = hr*60+mn
         local hr, mn = math.random(0, 23), math.random(0, 59)
         local vt= randomChoice(vesselTypes)
 
        local ct= randomChoice(cargoTypes)
         local vt, ct = randomChoice(vesselTypes), randomChoice(cargoTypes)
         local prodList={}
         local subtype, sourceRegion = chooseManifestProfile(vt, ct)
         if ct=="Agricultural" then
 
            for _=1,math.random(1,3) do
local owner = getOwner(vt, ct, subtype, sourceRegion) or "Independent Consortium"
                prodList[#prodList+1]=randomChoice(agriProducts)
 
             end
local home = randomHomeForRegion(sourceRegion)
        elseif ct=="Mining/Manufacturing" then
 
             for _=1,math.random(1,3) do
local count = math.random(3, 4)
                 prodList[#prodList+1]=randomChoice(miningProducts)
local picks = pickCargoCompanies(vt, owner, ct, subtype, sourceRegion, count)
 
-- Port authority safety lock:
-- If the owner or selected company is one of the port authorities,
-- force Home City, Owner/Operator, and Companies to all match.
local portLock = nil
 
if isPortAuthority(sourceRegion) then
    portLock = sourceRegion
elseif isPortAuthority(owner) then
    portLock = owner
else
    for _, nm in ipairs(picks) do
         if isPortAuthority(nm) then
            portLock = nm
            break
         end
    end
end
 
if portLock then
    vt = "Cargo"
    ct = "Services"
    subtype = "port_authority"
    sourceRegion = portLock
    home = portLock
    owner = portLock
    picks = { portLock }
end
 
local ownerLogo = ownerOperatorFlag[owner] and ("[[File:" .. ownerOperatorFlag[owner] .. "|20px]] ")
    or (companyLogo[owner] and ("[[File:" .. companyLogo[owner] .. "|20px]] ") or "")
 
        if #picks == 0 then
             local fallback = {}
            local base = (ct == "Unspecified") and generalPools or (poolMap[ct] or {})
             for _, name in ipairs(base) do
                 local meta = getCompanyMeta(name)
                if meta.region == sourceRegion then
                    table.insert(fallback, name)
                end
             end
             end
            picks = randomN(fallback, math.min(count, #fallback))
        end
        local compCell = "<ul>"
        for _, nm in ipairs(picks) do
            local logo = companyLogo[nm] and ("[[File:" .. companyLogo[nm] .. "|20px]] ") or ""
            compCell = compCell .. string.format("<li>%s%s</li>", logo, nm)
        end
        compCell = compCell .. "</ul>"
        local errList = {"Cargo Mislabeling","Improper Documentation","Surveillance Anomaly","Inspection Failure","Unregistered Ritual Cargo"}
        local err = math.random() < 0.2 and randomChoice(errList) or "None"
        local rawT = math.random() * 19000 + 1000
        local allowedT = math.min(rawT, MAX_DAILY_TONNAGE - totalTonnage)
        totalTonnage = totalTonnage + allowedT
        table.insert(ships, {
            date          = string.format("Day %d, Y.%d", d, y),
            time          = string.format("%02d:%02d", hr, mn),
            sort          = hr * 60 + mn,
            vid            = "VM-" .. math.random(10000, 99999),
            home          = cityFlag[home] .. " " .. home,
            typ            = vt,
            ownerCell      = ownerLogo .. owner,
            cargotype      = string.format("%s / %s / %s", ct, subtype, sourceRegion),
            cargoCompanies = compCell,
            berth          = math.random(1, 124),
            teus          = math.random(100, 1000),
            tonnage        = string.format("%.2f", allowedT),
            queue          = string.format("%.2f", math.random() * 5.5 + 0.5),
            turnaround    = string.format("%.2f", math.random() * 32 + 4),
            error          = err
        })
        if totalTonnage >= MAX_DAILY_TONNAGE then
            break
         end
         end
        ships[#ships+1]={
            date        = string.format("Day %d, Y.%d",p.getCurrentDate()),
            time        = string.format("%02d:%02d",hr,mn),
            sort        = t,
            vid        = "VM-"..math.random(10000,99999),
            home        = cityFlags[randomChoice(cities)],
            type        = vt,
            owner      = getOwner(vt,ct),
            cargotype  = ct,
            products    = prodList,
            berth      = math.random(1,124),
            teus        = math.random(100,1000),
            tonnage    = string.format("%.2f",math.random()*19000+1000),
            queue      = string.format("%.2f",math.random()*5.5+0.5),
            turnaround  = string.format("%.2f",math.random()*32+4),
            error      = (math.random()<0.2 and randomChoice{
                "Cargo Mislabeling","Improper Documentation",
                "Surveillance Anomaly","Inspection Failure",
                "Unregistered Ritual Cargo"
            }) or "None"
        }
     end
     end
     table.sort(ships,function(a,b) return a.sort<b.sort end)
 
     table.sort(ships, function(a, b) return a.sort < b.sort end)


     local out = {
     local out = {
         '{| class="wikitable sortable"',
         '{| class="wikitable sortable"',
         '! Date !! Time !! Vessel ID !! Home City !! Type !! Owner/Operator !! Cargo Type !! Products !! Berth !! TEUs !! Tonnage (t) !! Queue (hrs) !! Turnaround (hrs) !! Error'
         '! Date !! Time !! Vessel ID !! Home City !! Type !! Owner/Operator !! Cargo Type !! Companies',
        '!! Berth !! TEUs !! Tonnage (t) !! Queue (hrs) !! Turnaround (hrs) !! Error'
     }
     }
     for _,s in ipairs(ships) do
     for _, s in ipairs(ships) do
         local prodCell = "#N/A"
         local row = (s.error ~= "None") and '|- bgcolor="#ffcccc"' or '|-'
        if #s.products>0 then
        table.insert(out, row)
            prodCell = "<ul>"
         table.insert(out, table.concat({
            for _,p in ipairs(s.products) do prodCell=prodCell.."<li>"..p.."</li>" end
             '|', s.date, '||', s.time, '||', s.vid, '||', s.home, '||', s.typ,
            prodCell = prodCell.."</ul>"
             '||', s.ownerCell, '||', s.cargotype, '||', s.cargoCompanies,
        end
             '||', s.berth, '||', s.teus, '||', s.tonnage, '||', s.queue,
         out[#out+1] = string.format(
            '||', s.turnaround, '||', s.error
             "|-\n| %s || %s || %s || %s || %s || %s || %s || %s || %d || %d || %s || %s || %s || %s",
         }, ' '))
             s.date,s.time,s.vid,s.home,s.type,s.owner,s.cargotype,
             prodCell,s.berth,s.teus,s.tonnage,s.queue,s.turnaround,s.error
         )
     end
     end
     out[#out+1] = "|}\n\n'''Error Key:'''"
     table.insert(out, '|}')
     out[#out+1] = "* '''Cargo Mislabeling:''' Manifest ≠ actual goods"
    table.insert(out, "'''Error Key:'''")
     out[#out+1] = "* '''Improper Documentation:''' Missing/outdated papers"
     table.insert(out, "* '''Cargo Mislabeling:''' Manifest ≠ actual goods")
     out[#out+1] = "* '''Surveillance Anomaly:''' Odd heat/motion signature"
     table.insert(out, "* '''Improper Documentation:''' Missing or outdated papers")
     out[#out+1] = "* '''Inspection Failure:''' Customs/hatch failure"
     table.insert(out, "* '''Surveillance Anomaly:''' Unexpected heat/motion")
     out[#out+1] = "* '''Unregistered Ritual Cargo:''' Not pre-cleared by Temple Bank"
     table.insert(out, "* '''Inspection Failure:''' Customs/hatch failure")
     return table.concat(out,"\n")
     table.insert(out, "* '''Unregistered Ritual Cargo:''' Not pre-cleared by Temple Bank")
     return table.concat(out, "\n")
end
end


function p.dailyReport()
function p.dailyReport()
     local y,d = p.getCurrentDate()
     local y, d = p.getCurrentDate()
     return string.format(
     return string.format(
         "'''General Port of Lake Morovia: Operational Log – Day %d, Year %d PSSC'''","\n"
         "'''General Port of Lake Morovia: Operational Log – Day %d, Year %d PSSC'''",
    )..p.generateShipTable()
        d, y
    ) .. "\n" .. p.generateShipTable()
end
end


return p
return p

Latest revision as of 01:03, 26 May 2026

Documentation for this module may be created at Module:GeneralPortOperations/doc

-- Module:GeneralPortDailyLog
local p = {}

-- compatibility shim for unpack
local unpack = table.unpack or unpack

-- 0) Daily tonnage cap
local MAX_DAILY_TONNAGE = 1359348.096

-- 1) Date logic
function p.getCurrentDate()
    local start   = os.time{year=1999, month=8, day=6}
    local elapsed = math.floor((os.time() - start) / 86400)
    local year    = math.floor(elapsed / 183)
    local day     = (elapsed % 183) + 1
    return year, day
end

-- 2) Random helpers
local function randomChoice(tbl)
    if type(tbl) ~= "table" or #tbl == 0 then return nil end
    return tbl[math.random(#tbl)]
end

local function randomN(tbl, n)
    if type(tbl) ~= "table" or n < 1 then return {} end
    local copy, out = {unpack(tbl)}, {}
    for i = 1, math.min(n, #copy) do
        local idx = math.random(#copy)
        table.insert(out, table.remove(copy, idx))
    end
    return out
end

-- 3) Home ports + flags
-- DO NOT ALTER: existing fixed home-city list retained exactly
local cities = {
    "Aegirheim","Skýrophos","Bjornopolis","Norsolyra","Ephyra",
    "Symphonara","Delphica","Vaeringheim","Somniumpolis",
    "Keybir-Aviv","Sufriya","Jogi" , "Ardclach" , "Riddersborg"
}
local cityFlag = {}
for _, name in ipairs(cities) do
    local norm = name
        :gsub("[%s%-’]", "")
        :gsub("[Áá]", "A"):gsub("[Éé]", "E")
        :gsub("[Íí]", "I"):gsub("[Óó]", "O")
        :gsub("[Úú]", "U"):gsub("[Ýý]", "Y")
    cityFlag[name] = string.format("[[File:%sFlag.png|20px]]", norm)
end
cityFlag["Skýrophos"] = "[[File:SkyrophosFlag.png|20px]]"
cityFlag["Port of Corumia"] = "[[File:CorumiaFlag.png|20px]]"
cityFlag["Port of Euranidom"] = "[[File:Euranidom.png|20px]]"
cityFlag["Port of Southern Keltiania"] = "[[File:SouthKeltiania.png|20px]]"

local ownerOperatorFlag = {
    ["Port of Corumia"] = "CorumiaFlag.png",
    ["Port of Euranidom"] = "Euranidom.png",
    ["Port of Southern Keltiania"] = "SouthKeltiania.png",
}

-- 3b) Hatch Ministry-aligned cluster map
local cityRegion = {
    ["Aegirheim"]    = "New South Jangsong",
    ["Skýrophos"]    = "New South Jangsong",
    ["Bjornopolis"]  = "New South Jangsong",
    ["Norsolyra"]    = "New South Jangsong",
    ["Ephyra"]       = "New South Jangsong",

    ["Symphonara"]   = "Vaeringheim",
    ["Delphica"]     = "Vaeringheim",
    ["Vaeringheim"]  = "Vaeringheim",
    ["Somniumpolis"] = "Odiferia",

    ["Keybir-Aviv"]  = "Haifan Bassaridia",
    ["Sufriya"]      = "Haifan Bassaridia",
    ["Jogi"]         = "Haifan Bassaridia",

    ["Ardclach"]     = "Bassaridian Normark",
    ["Riddersborg"]  = "Bassaridian Normark",

    ["Port of Corumia"] = "Port of Corumia",
    ["Port of Euranidom"] = "Port of Euranidom",
    ["Port of Southern Keltiania"] = "Port of Southern Keltiania",
}

local regionHomeCities = {
    ["New South Jangsong"] = {"Aegirheim","Skýrophos","Bjornopolis","Norsolyra","Ephyra"},
    ["Vaeringheim"]        = {"Symphonara","Delphica","Vaeringheim"},
    ["Odiferia"]           = {"Somniumpolis"},
    ["Haifan Bassaridia"]  = {"Keybir-Aviv","Sufriya","Jogi"},
    ["Bassaridian Normark"]= {"Ardclach","Riddersborg"},

    ["Port of Corumia"] = {"Port of Corumia"},
    ["Port of Euranidom"] = {"Port of Euranidom"},
    ["Port of Southern Keltiania"] = {"Port of Southern Keltiania"},
}

local allCargoRegions = {
    "Haifan Bassaridia",
    "Odiferia",
    "Alpazkigz",
    "Vaeringheim",
    "New South Jangsong",
    "Bassaridian Normark",
    "Ouriana",
    "Caledonian Dependencies"
}

local function randomHomeForRegion(region)
    local pool = regionHomeCities[region]
    if pool and #pool > 0 then return randomChoice(pool) end
    return randomChoice(cities)
end

-- 4) Vessel & cargo types
local vesselTypes = {"Cargo","Military","Ritual","Privateer"}
local cargoTypes  = {"Agricultural","Mining/Manufacturing","Services","Unspecified"}

-- 5) Owner pools
local ritualCults = {
    "Order of Aurora Mystica","Harmony Sanctum",
    "Ignis Aeternum","Celestial Harmony Sect","Guild of Golden Shadows",
    "Azure Sentinel Sect","Reverie Nebulous","Eon Fellowship",
    "Order of the Umbral Oracle","Mystery of the Verdant Embrace",
    "Conclave Illuminara","Sanctum Vitalis","Temple Alabaster",
    "Court of the Ironclad","Accord Concordia","Mystery of the Stygian Veil",
    "Sylvan Fellowship","Mystery of Red Mirth","Rex Catonis",
    "Sanctum Delphica","Ordo Amicitia","Temple Illuminata"
}
local militaryDivs = {
    "Alpazkigz Division","Odiferia Division","Vaeringheim Division",
    "Haifa Division","Jeseri Division","Hatch Ministry Division",
    "New South Jangsong Division","Haifan Bassaridia Division"
}
local agriCompanies = {
    "Gladeseed Farmers' Union","Atterian Cattle Drivers",
    "Jogiani Merchants of Manwine","Keepers of the Grove of Zoe Elm",
    "Plains of Jogi","Ale of the Night Central Brewery",
    "Camel Herders of the Jangsong Province","Anterran Imports and Services",
    "Merchant Guild of the Poppy Goddess","Grand Cave Bee Meadery",
    "Cinammon Plains of Rouge","The Velvet Company","Seamstresses of Rouge",
    "Salurian Temple of Sacred Horehound","Bulhanu Ranchers' Association",
    "Suncliff Fisheries","Ministers of Mistress Mugwort",
    "Blood Vineyards of the Far North",
    "Norsolyrian Wisp Rice Farmers Association",
    "Giid-Lisea Ranching Import Company","Bird Keepers of Saluria",
    "Hunters of the Giant Fanged Penguin","Knights Castle Reindeer Herders",
    "Lindley Whaling Company","Aminian Coffee Company","Granadita Collective",
    "Herd Caton","Wasp Hunters of the Austral Isles", "Sea Elm Southern Meadery",
    "Nefel Village","Diamonia Sugar Cooperatives", "Ouriana Lowlands Farmers' Association",
    "Nefel Village", "Ouriad River Fisheries Cooperative", "Caspazani Livestock Company",
    "White Ruby Communes", "Fange Velvet", "Lamian Ward", "Sleva Farming Supplies Cooperative",
    "Hrimmanes Woolhorse Company","	King's Home Fur Trappers' Cooperative", "Caledonj Bay Fishermen’s’ Cooperatives", "Salain's Landing Granary Complex","Grove Synaxis of Tel-Amin",
}
local miningCompanies = {
    "Roving Wind Farm Corporation",
    "Thalassian Temple Granite Export Cooperative","East Keltian Iron Company",
    "Mylecian Coal Ports","Merchants of the Valley of Diamonds",
    "East Keltian Timber Company","Onceanic Boot Company",
    "Commune of the Garganid Apostles","Shï Collective","Order of the Sphinx",
    "Market Hanavusu","Somniant Stock Fund","Copper Mines of Southern Jogi",
    "Horned Bear Timber Company","Federation of North Brabipant",
    "Southern Strait Preservation Corporation","Eosphorus Motor Company",
    "Vortelin Electronics", "Thyrsiad Rubber Company","Stygium Mines of Acheron",
    "Trans-Rodinian Granite Agency", "Amber Ports of Slevik", "Thesean Iron Exports",
    "Fanghorn Highland Gas Company","Gadial Oil", "Tharsyr Shoal Salt Works",
}
local serviceCompanies = {
    "Ergonian Spy Agency",
    "Temple Bank of the Reformed Stripping Path",
    "Temple University of Delphica","La Sái Ebile",
    "Pharmacon Sect",
    "Skyrophian Security Contractors",
    "Haifadora Postal Service",
    "Fleet of Rouge Divers",
    "Merchant Caravans of the Candy Kingdom",
    "Hellhound Breeders of Dragevik",
    "Noctic Fleet",
    "Maritime Guild of the Cult of Maskmakers",
    "Couriers of the Lizard Queen",
    "Temple of Aprobelle",
    "Sårensby Depot*",
}

-- special pools
local privateerCompanies = {
    "Lake Morovia Blockade Fund",
    "Baratar Corporation",
    "Hatch Ministry",
    "Skyrophian Security Contractors",
    "Temple of Aprobelle",
    "Noctic Fleet",
    "Maritime Guild of the Cult of Maskmakers",
    "Merchant Caravans of the Candy Kingdom",
    "Haifadora Postal Service",
    "Fleet of Rouge Divers",
    "Ergonian Spy Agency",
    "Southern Strait Preservation Corporation",
    "Somniant Stock Fund"
}

local hatchMinistryCompanies = {
    "Hatch Ministry"
}

local portAuthorityCompanies = {
    "Port of Corumia",
    "Port of Euranidom",
    "Port of Southern Keltiania",
}

local portAuthorityLookup = {}
for _, name in ipairs(portAuthorityCompanies) do
    portAuthorityLookup[name] = true
end

local function isPortAuthority(name)
    return portAuthorityLookup[name] == true
end

-- 5b) Company classification + Hatch-aligned source region metadata
local companyMeta = {
    -- Agriculture
    ["Gladeseed Farmers' Union"] =                 {class="Agricultural", subtype="grain", region="Vaeringheim"},
    ["Atterian Cattle Drivers"] =                  {class="Agricultural", subtype="livestock", region="New South Jangsong"},
    ["Jogiani Merchants of Manwine"] =             {class="Agricultural", subtype="wine_spirits", region="Haifan Bassaridia"},
    ["Keepers of the Grove of Zoe Elm"] =          {class="Agricultural", subtype="forest_products", region="Vaeringheim"},
    ["Plains of Jogi"] =                           {class="Agricultural", subtype="grain", region="Haifan Bassaridia"},
    ["Ale of the Night Central Brewery"] =         {class="Agricultural", subtype="wine_spirits", region="Vaeringheim"},
    ["Camel Herders of the Jangsong Province"] =   {class="Agricultural", subtype="livestock", region="New South Jangsong"},
    ["Anterran Imports and Services"] =            {class="Agricultural", subtype="mixed_foodstuffs", region="Vaeringheim"},
    ["Merchant Guild of the Poppy Goddess"] =      {class="Agricultural", subtype="herbs", region="Vaeringheim"},
    ["Grand Cave Bee Meadery"] =                   {class="Agricultural", subtype="mead_spirits", region="Vaeringheim"},
    ["Cinammon Plains of Rouge"] =                 {class="Agricultural", subtype="spices", region="Vaeringheim"},
    ["The Velvet Company"] =                       {class="Agricultural", subtype="textiles_raw", region="Vaeringheim"},
    ["Seamstresses of Rouge"] =                    {class="Agricultural", subtype="textiles_raw", region="Vaeringheim"},
    ["Salurian Temple of Sacred Horehound"] =      {class="Agricultural", subtype="herbs", region="Alpazkigz"},
    ["Bulhanu Ranchers' Association"] =            {class="Agricultural", subtype="livestock", region="Odiferia"},
    ["Suncliff Fisheries"] =                       {class="Agricultural", subtype="fish", region="Vaeringheim"},
    ["Ministers of Mistress Mugwort"] =            {class="Agricultural", subtype="herbs", region="Vaeringheim"},
    ["Blood Vineyards of the Far North"] =         {class="Agricultural", subtype="wine_spirits", region="New South Jangsong"},
    ["Norsolyrian Wisp Rice Farmers Association"] = {class="Agricultural", subtype="grain", region="New South Jangsong"},
    ["Giid-Lisea Ranching Import Company"] =       {class="Agricultural", subtype="livestock", region="Vaeringheim"},
    ["Bird Keepers of Saluria"] =                  {class="Agricultural", subtype="animal_products", region="Alpazkigz"},
    ["Hunters of the Giant Fanged Penguin"] =      {class="Agricultural", subtype="animal_products", region="Caledonian Dependencies"},
    ["Knights Castle Reindeer Herders"] =          {class="Agricultural", subtype="livestock", region="Bassaridian Normark"},
    ["Lindley Whaling Company"] =                  {class="Agricultural", subtype="marine_oil", region="Caledonian Dependencies"},
    ["Aminian Coffee Company"] =                   {class="Agricultural", subtype="coffee", region="Haifan Bassaridia"},
    ["Grove Synaxis of Tel-Amin"] =                {class="Agricultural", subtype="fruit", region="Haifan Bassaridia"},
    ["Granadita Collective"] =                     {class="Agricultural", subtype="fruit", region="Haifan Bassaridia"},
    ["Herd Caton"] =                               {class="Agricultural", subtype="livestock", region="Vaeringheim"},
    ["Wasp Hunters of the Austral Isles"] =        {class="Agricultural", subtype="animal_products", region="Caledonian Dependencies"},
    ["Sea Elm Southern Meadery"] =                 {class="Agricultural", subtype="mead_spirits", region="Vaeringheim"},
    ["Nefel Village"] =                            {class="Agricultural", subtype="mixed_foodstuffs", region="Vaeringheim"},
    ["Diamonia Sugar Cooperatives"] =              {class="Agricultural", subtype="sugar", region="Haifan Bassaridia"},
    ["Ouriana Lowlands Farmers' Association"] =    {class="Agricultural", subtype="grain", region="Ouriana"},
    ["Ouriad River Fisheries Cooperative"] =       {class="Agricultural", subtype="fish", region="Ouriana"},
    ["Caspazani Livestock Company"] =              {class="Agricultural", subtype="livestock", region="Ouriana"},
    ["White Ruby Communes"] =                      {class="Agricultural", subtype="mixed_foodstuffs", region="Vaeringheim"},
    ["Fange Velvet"] =                             {class="Agricultural", subtype="textiles_raw", region="Caledonian Dependencies"},
    ["Lamian Ward"] =                              {class="Agricultural", subtype="mixed_foodstuffs", region="Vaeringheim"},
    ["Sleva Farming Supplies Cooperative"] =       {class="Agricultural", subtype="farm_inputs", region="Caledonian Dependencies"},
    ["Hrimmanes Woolhorse Company"] =              {class="Agricultural", subtype="livestock", region="Caledonian Dependencies"},
    ["	King's Home Fur Trappers' Cooperative"] =  {class="Agricultural", subtype="animal_products", region="Bassaridian Normark"},
    ["Caledonj Bay Fishermen’s’ Cooperatives"] =   {class="Agricultural", subtype="fish", region="Caledonian Dependencies"},
    ["Salain's Landing Granary Complex"] =         {class="Agricultural", subtype="grain", region="Vaeringheim"},

    -- Mining / Manufacturing / Energy
    ["Roving Wind Farm Corporation"] =             {class="Mining/Manufacturing", subtype="energy", region="Vaeringheim"},
    ["Thalassian Temple Granite Export Cooperative"] = {class="Mining/Manufacturing", subtype="stone", region="Vaeringheim"},
    ["East Keltian Iron Company"] =                {class="Mining/Manufacturing", subtype="metals", region="Vaeringheim"},
    ["Mylecian Coal Ports"] =                      {class="Mining/Manufacturing", subtype="coal", region="Vaeringheim"},
    ["Merchants of the Valley of Diamonds"] =      {class="Mining/Manufacturing", subtype="gemstones", region="Haifan Bassaridia"},
    ["East Keltian Timber Company"] =              {class="Mining/Manufacturing", subtype="timber", region="Vaeringheim"},
    ["Onceanic Boot Company"] =                    {class="Mining/Manufacturing", subtype="consumer_goods", region="Vaeringheim"},
    ["Commune of the Garganid Apostles"] =         {class="Mining/Manufacturing", subtype="stone", region="Vaeringheim"},
    ["Shï Collective"] =                           {class="Mining/Manufacturing", subtype="luxury_goods", region="Vaeringheim"},
    ["Order of the Sphinx"] =                      {class="Mining/Manufacturing", subtype="luxury_goods", region="Haifan Bassaridia"},
    ["Market Hanavusu"] =                          {class="Mining/Manufacturing", subtype="consumer_goods", region="New South Jangsong"},
    ["Somniant Stock Fund"] =                      {class="Mining/Manufacturing", subtype="arms_industrial", region="Vaeringheim"},
    ["Copper Mines of Southern Jogi"] =            {class="Mining/Manufacturing", subtype="metals", region="Haifan Bassaridia"},
    ["Horned Bear Timber Company"] =               {class="Mining/Manufacturing", subtype="timber", region="New South Jangsong"},
    ["Federation of North Brabipant"] =            {class="Mining/Manufacturing", subtype="consumer_goods", region="Caledonian Dependencies"},
    ["Southern Strait Preservation Corporation"] = {class="Mining/Manufacturing", subtype="marine_materials", region="Haifan Bassaridia"},
    ["Eosphorus Motor Company"] =                  {class="Mining/Manufacturing", subtype="vehicles", region="Vaeringheim"},
    ["Vortelin Electronics"] =                     {class="Mining/Manufacturing", subtype="electronics", region="Vaeringheim"},
    ["Thyrsiad Rubber Company"] =                  {class="Mining/Manufacturing", subtype="rubber", region="Haifan Bassaridia"},
    ["Stygium Mines of Acheron"] =                 {class="Mining/Manufacturing", subtype="metals", region="Alpazkigz"},
    ["Trans-Rodinian Granite Agency"] =            {class="Mining/Manufacturing", subtype="stone", region="Vaeringheim"},
    ["Amber Ports of Slevik"] =                    {class="Mining/Manufacturing", subtype="amber", region="Caledonian Dependencies"},
    ["Thesean Iron Exports"] =                     {class="Mining/Manufacturing", subtype="metals", region="Ouriana"},
    ["Fanghorn Highland Gas Company"] =            {class="Mining/Manufacturing", subtype="gas", region="Caledonian Dependencies"},
    ["Gadial Oil"] =                               {class="Mining/Manufacturing", subtype="oil", region="Vaeringheim"},
    ["Tharsyr Shoal Salt Works"] =                 {class="Mining/Manufacturing", subtype="salt", region="Vaeringheim"},

    -- Services
    ["Ergonian Spy Agency"] =                      {class="Services", subtype="intelligence", region="Vaeringheim"},
    ["Temple Bank of the Reformed Stripping Path"] = {class="Services", subtype="finance", region="Vaeringheim"},
    ["Temple University of Delphica"] =            {class="Services", subtype="education", region="Vaeringheim"},
    ["La Sái Ebile"] =                             {class="Services", subtype="commerce", region="Haifan Bassaridia"},
    ["Pharmacon Sect"] =                           {class="Services", subtype="medical", region="Alpazkigz"},
    ["Skyrophian Security Contractors"] =          {class="Services", subtype="security", region="New South Jangsong"},
    ["Haifadora Postal Service"] =                 {class="Services", subtype="courier", region="Haifan Bassaridia"},
    ["Fleet of Rouge Divers"] =                    {class="Services", subtype="marine_services", region="Vaeringheim"},
    ["Merchant Caravans of the Candy Kingdom"] =   {class="Services", subtype="logistics", region="Vaeringheim"},
    ["Hellhound Breeders of Dragevik"] =           {class="Services", subtype="animal_services", region="Bassaridian Normark"},
    ["Noctic Fleet"] =                             {class="Services", subtype="marine_services", region="Vaeringheim"},
    ["Maritime Guild of the Cult of Maskmakers"] = {class="Services", subtype="marine_services", region="Vaeringheim"},
    ["Couriers of the Lizard Queen"] =             {class="Services", subtype="courier", region="Vaeringheim"},
    ["Temple of Aprobelle"] =                      {class="Services", subtype="security", region="Vaeringheim"},
    ["Sårensby Depot*"] =                          {class="Services", subtype="logistics", region="Bassaridian Normark"},
    ["Port of Corumia"] =                          {class="Services", subtype="port_authority", region="Port of Corumia"},
    ["Port of Euranidom"] =                        {class="Services", subtype="port_authority", region="Port of Euranidom"},
    ["Port of Southern Keltiania"] =               {class="Services", subtype="port_authority", region="Port of Southern Keltiania"},

    -- Special pools
    ["Lake Morovia Blockade Fund"] =               {class="Services", subtype="security", region="Vaeringheim"},
    ["Baratar Corporation"] =                      {class="Mining/Manufacturing", subtype="arms_industrial", region="Haifan Bassaridia"},
    ["Hatch Ministry"] =                           {class="Services", subtype="security", region="Vaeringheim"},
}

-- build a “general” pool excluding special-only firms
local generalPools = {}
for _, list in ipairs({agriCompanies, miningCompanies, serviceCompanies}) do
    for _, name in ipairs(list) do
        if name ~= "Hatch Ministry" then
            local isPri = false
            for _, pr in ipairs(privateerCompanies) do
                if name == pr then isPri = true break end
            end
            if not isPri then
                table.insert(generalPools, name)
            end
        end
    end
end

local function getCompanyMeta(name)
    return companyMeta[name] or {class="Unspecified", subtype="mixed", region="Vaeringheim"}
end

local function buildPoolByClassSubtypeRegion(className, subtype, region)
    local base = ({
        Agricultural             = agriCompanies,
        ["Mining/Manufacturing"] = miningCompanies,
        Services                 = serviceCompanies
    })[className] or {}

    local pool = {}
    for _, name in ipairs(base) do
        if name ~= "Hatch Ministry" then
            local isPri = false
            for _, pr in ipairs(privateerCompanies) do
                if name == pr then isPri = true break end
            end
            if not isPri then
                local meta = getCompanyMeta(name)
                if meta.class == className and meta.subtype == subtype and meta.region == region then
                    table.insert(pool, name)
                end
            end
        end
    end
    return pool
end

local function buildGeneralPoolBySubtypeRegion(subtype, region)
    local pool = {}
    for _, name in ipairs(generalPools) do
        local meta = getCompanyMeta(name)
        if meta.subtype == subtype and meta.region == region then
            table.insert(pool, name)
        end
    end
    return pool
end

local function chooseManifestProfile(vt, ct)
    if vt == "Privateer" then
        local subtype = randomChoice({"security","arms_industrial","logistics","medical"})
        local region = randomChoice({"Haifan Bassaridia","Odiferia","Alpazkigz","Vaeringheim","New South Jangsong","Bassaridian Normark","Ouriana"})
        return subtype, region
    elseif vt == "Military" then
        local subtype = randomChoice({"security","arms_industrial","medical","logistics"})
        local region = randomChoice({"Haifan Bassaridia","Odiferia","Alpazkigz","Vaeringheim","New South Jangsong","Bassaridian Normark","Ouriana"})
        return subtype, region
    elseif vt == "Ritual" then
        local subtype = randomChoice({"finance","medical","courier","security","education","logistics"})
        local region = randomChoice({"Haifan Bassaridia","Odiferia","Alpazkigz","Vaeringheim","New South Jangsong","Bassaridian Normark"})
        return subtype, region
    elseif ct == "Agricultural" then
        local subtype = randomChoice({
            "grain","livestock","wine_spirits","forest_products","herbs",
            "mead_spirits","spices","textiles_raw","fish","animal_products",
            "marine_oil","coffee","fruit","sugar","mixed_foodstuffs","farm_inputs"
        })
        local region = randomChoice(allCargoRegions)
        return subtype, region
elseif ct == "Mining/Manufacturing" then
    local subtype = randomChoice({
        "energy","stone","salt","metals","coal","gemstones","timber","consumer_goods",
        "luxury_goods","arms_industrial","marine_materials","vehicles",
        "electronics","rubber","amber","gas","oil"
    })
        local region = randomChoice(allCargoRegions)
        return subtype, region
elseif ct == "Services" then
    if math.random() < 0.20 then
        local port = randomChoice(portAuthorityCompanies)
        return "port_authority", port
    else
        local subtype = randomChoice({
            "intelligence","finance","education","commerce","medical",
            "security","courier","marine_services","logistics","animal_services"
        })
        local region = randomChoice(allCargoRegions)
        return subtype, region
    end
    else
        local source = randomChoice(generalPools)
        local meta = getCompanyMeta(source)
        return meta.subtype, meta.region
    end
end

local function pickCargoCompanies(vt, owner, ct, subtype, region, count)
    if vt == "Cargo" and isPortAuthority(region) then
        return { region }
    end
    if vt == "Privateer" then
        local pool = {}

        for _, name in ipairs(privateerCompanies) do
            local meta = getCompanyMeta(name)
            if meta.region == region then
                if subtype == "security" and meta.subtype == "security" then
                    table.insert(pool, name)
                elseif subtype == "arms_industrial" and meta.subtype == "arms_industrial" then
                    table.insert(pool, name)
                elseif subtype == "logistics" and (
                    meta.subtype == "logistics" or
                    meta.subtype == "courier" or
                    meta.subtype == "marine_services"
                ) then
                    table.insert(pool, name)
                elseif subtype == "medical" and meta.subtype == "medical" then
                    table.insert(pool, name)
                end
            end
        end

        if #pool == 0 then
            for _, name in ipairs(privateerCompanies) do
                local meta = getCompanyMeta(name)
                if meta.subtype == "security"
                    or meta.subtype == "arms_industrial"
                    or meta.subtype == "logistics"
                    or meta.subtype == "courier"
                    or meta.subtype == "marine_services"
                    or meta.subtype == "medical" then
                    table.insert(pool, name)
                end
            end
        end

        return randomN(pool, math.min(count, #pool))

    elseif owner == "Hatch Ministry Division" then
        return randomN(hatchMinistryCompanies, 1)

    elseif ct == "Unspecified" then
        local pool = buildGeneralPoolBySubtypeRegion(subtype, region)
        if #pool == 0 then
            local fallback = {}
            for _, name in ipairs(generalPools) do
                local meta = getCompanyMeta(name)
                if meta.region == region then
                    table.insert(fallback, name)
                end
            end
            pool = fallback
        end
        return randomN(pool, math.min(count, #pool))

    else
        local pool = buildPoolByClassSubtypeRegion(ct, subtype, region)
        if #pool == 0 then
            local fallback = {}
            local base = ({
                Agricultural             = agriCompanies,
                ["Mining/Manufacturing"] = miningCompanies,
                Services                 = serviceCompanies
            })[ct] or {}
            for _, name in ipairs(base) do
                if name ~= "Hatch Ministry" then
                    local isPri = false
                    for _, pr in ipairs(privateerCompanies) do
                        if name == pr then isPri = true break end
                    end
                    if not isPri then
                        local meta = getCompanyMeta(name)
                        if meta.region == region then
                            table.insert(fallback, name)
                        end
                    end
                end
            end
            pool = fallback
        end
        return randomN(pool, math.min(count, #pool))
    end
end

-- 6) Company logos mapping
local companyLogo = {}
local function addLogo(name,file) companyLogo[name]=file end
addLogo("Lake Morovia Blockade Fund","BlockadeFund420.png")
addLogo("Gladeseed Farmers' Union","Gladeseed420.png")
addLogo("Atterian Cattle Drivers","Atterian420.png")
addLogo("Noctic Fleet","Noctic420.png")
addLogo("Jogiani Merchants of Manwine","Jogiani420.png")
addLogo("Keepers of the Grove of Zoe Elm","KeepersoftheGrove.png")
addLogo("Plains of Jogi","PlainsofJogi.png")
addLogo("Ale of the Night Central Brewery","AleofNight420.png")
addLogo("Camel Herders of the Jangsong Province","CamelHerders420.png")
addLogo("Thalassian Temple Granite Export Cooperative","ThalassianGranite420.png")
addLogo("East Keltian Iron Company","EKIron420.png")
addLogo("Mylecian Coal Ports","CoalPorts420.png")
addLogo("Merchants of the Valley of Diamonds","ValleyofDiamonds420.png")
addLogo("Baratar Corporation","Baratar420.png")
addLogo("Roving Wind Farm Corporation","RovingWind420.png")
addLogo("Grand Cave Bee Meadery","GrandCave420.png")
addLogo("Merchant Guild of the Poppy Goddess","PoppyGoddess420.png")
addLogo("Cinammon Plains of Rouge","CinammonPlains420.png")
addLogo("The Velvet Company","VelvetHome420.png")
addLogo("Seamstresses of Rouge","Seamstresses420.png")
addLogo("Salurian Temple of Sacred Horehound","SalurianTemple421.png")
addLogo("Bulhanu Ranchers' Association","Bulhanu421.png")
addLogo("Suncliff Fisheries","Suncliff421.png")
addLogo("Ministers of Mistress Mugwort","Mugwort421.png")
addLogo("Blood Vineyards of the Far North","BloodVineyards421.png")
addLogo("Norsolyrian Wisp Rice Farmers Association","Norsolyrian421.png")
addLogo("Giid-Lisea Ranching Import Company","GiidLisea421.png")
addLogo("Bird Keepers of Saluria","SaluriaBK421.png")
addLogo("Knights Castle Reindeer Herders","KnightsCastle421.png")
addLogo("Lindley Whaling Company","LindleyWhalingCompany.png")
addLogo("Aminian Coffee Company","Aminian421.png")
addLogo("Grove Synaxis of Tel-Amin","GroveSynaxisLogo.png")
addLogo("Commune of the Garganid Apostles","Garganid421.png")
addLogo("Onceanic Boot Company","OnceanicBoot421.png")
addLogo("Maritime Guild of the Cult of Maskmakers","MaritimeGuild420.png")
addLogo("Ergonian Spy Agency","ErgonianSpy421.png")
addLogo("Temple Bank of the Reformed Stripping Path","TempleBankCrest.png")
addLogo("Couriers of the Lizard Queen","LizardQueen421.png")
addLogo("Temple University of Delphica","TempleUniversity421.png")
addLogo("Shï Collective","ShiCollective421.png")
addLogo("Order of the Sphinx","OrderofSphinx421.png")
addLogo("Market Hanavusu","MarketHanavusu421.png")
addLogo("Somniant Stock Fund","SomniantStockFund.png")
addLogo("Hatch Ministry","HatchMinistry420.png")
addLogo("Anterran Imports and Services","AnterranImports420.png")
addLogo("East Keltian Timber Company","EKTimber420.png")
addLogo("Hellhound Breeders of Dragevik","Dragevik421.png")
addLogo("La Sái Ebile","LaSie421.png")
addLogo("Hunters of the Giant Fanged Penguin","FangedPenguin421.png")
addLogo("Commune of the Garganid Apostles","Garganid421.png")
addLogo("Copper Mines of Southern Jogi","CopperMinesLogoC.png")
addLogo("Horned Bear Timber Company","HornedBearTimberLogo.png")
addLogo("Granadita Collective","GranaditaCollectiveLogo.png")
addLogo("Pharmacon Sect","PharmaconSect.png")
addLogo("Skyrophian Security Contractors","SkyrophianSecurity.png")
addLogo("Haifadora Postal Service","HaifadoraPostalLogo.png")
addLogo("Herd Caton","HerdCatonLogo.png")
addLogo("Wasp Hunters of the Austral Isles","WaspHuntersLogo.png")
addLogo("Fleet of Rouge Divers","RougeDiversLogo.png")
addLogo("Sea Elm Southern Meadery","SeaElmLogo.png")
addLogo("Candy Merchants of the Candy Kingdom","Candymerchantslogo.png")
addLogo("Federation of North Brabipant","NorthBrabipantlogo.png")
addLogo("Southern Strait Preservation Corporation","SouthernStraitPreservationLogo.png")
addLogo("Eosphorus Motor Company","EosphorusLogo.png")
addLogo("Vortelin Electronics","VortelinElectronicsLogo.png")
addLogo("Thyrsiad Rubber Company","ThyrsiadRubberLogo.png")
addLogo("Nefel Village","NefelVillageLogo.png")
addLogo("Stygium Mines of Acheron","StygiumMinesLogo.png")
addLogo("Temple of Aprobelle","TempleAprobelleLogo.png")
addLogo("Diamonia Sugar Cooperatives","DiamoniaSugarLogo.png")
addLogo("Ouriana Lowlands Farmers' Association","OurianaLowlandsLogo.png")
addLogo("Ouriad River Fisheries Cooperative","OuriadRiverLogo.png")
addLogo("Caspazani Livestock Company","CaspazaniLogo.png")
addLogo("Trans-Rodinian Granite Agency","TransRodinanLogo.png")
addLogo("White Ruby Communes","WhiteRubyLogo.png")
addLogo("Fange Velvet","FangeVelvetLogo.png")
addLogo("Sårensby Depot","SårensbyDepotLogo.png")
addLogo("Amber Ports of Slevik","AmberPortsLogo2.png")
addLogo("Port of Corumia","PortofCorumiaLogo.png")
addLogo("Port of Euranidom","PortofEuranidomLogo.png")
addLogo("Port of Southern Keltiania","PortofSouthernKeltianiaLogo.png")
addLogo("Lamian Ward","LamianWardLogo.png")
addLogo("Sleva Farming Supplies Cooperatives","SlevaFarmingLogo.png")
addLogo("Hrimmanes Woolhorse Company","HrimmanesLogoB.png.png")
addLogo("Thesean Iron Exports","TheseanIronExports.png")
addLogo("King's Home Fur Trappers' Cooperative","KingsHomeCoLogo.png.png")
addLogo("Fanghorn Highland Gas Company","FanghornGasLogo.png")
addLogo("Caledonj Bay Fishermen’s’ Cooperatives","CaledonjBayLogo.png")
addLogo("Gadial Oil","GadialOilLogoB.png")
addLogo("Alpazkigz Division",        "AlpazkigzDivisionFlag.png")
addLogo("Odiferia Division",         "OdiferiaFlag.png")
addLogo("Vaeringheim Division",      "VaeringheimDivisionFlag.png")
addLogo("Haifa Division",            "HafaanFlag.png")
addLogo("Jeseri Division",           "JeseriFlag.png")
addLogo("Hatch Ministry Division",   "HatchMinistryFlag.png")
addLogo("New South Jangsong Division","NewSouthJangsongFlag.png")
addLogo("Haifan Bassaridia Division", "HaifaDivisionFlag.png")
addLogo("Order of Aurora Mystica",        "Auroramystica.png")
addLogo("Harmony Sanctum",         "Harmonysanctum.png")
addLogo("Ignis Aeternum",      "Ignis Aeternum.png")
addLogo("Celestial Harmony Sect",            "HarmonySect.png")
addLogo("Guild of Golden Shadows",           "GoldenShadows.png")
addLogo("Azure Sentinel Sect",   "AzureSentinel.png")
addLogo("Reverie Nebulous","RevNeb.png")
addLogo("Eon Fellowship", "EonFel.png")
addLogo("Order of the Umbral Oracle", "Umbral.png")
addLogo("Mystery of the Verdant Embrace", "Verdant.png")
addLogo("Conclave Illuminara", "Conclave.png")
addLogo("Sanctum Vitalis", "Vitalis.png")
addLogo("Temple Alabaster", "Alabaster.png")
addLogo("Court of the Ironclad", "Ironclad.png")
addLogo("Accord Concordia", "Concordia.png")
addLogo("Mystery of the Stygian Veil", "StygianVe.png")
addLogo("Sylvan Fellowship", "Sylvan.png")
addLogo("Mystery of Red Mirth", "MirthFlag.png")
addLogo("Rex Catonis", "Catonis.png")
addLogo("Sanctum Delphica", "SanctumDel.png")
addLogo("Ordo Amicitia", "OrdoFl.png")
addLogo("Temple Illuminata", "AureumFlag.png")
addLogo("Tharsyr Shoal Salt Works","TharsyrShoalSaltWorksLogo.png")
addLogo("Salain's Landing Granary Complex","SalainsLandingGranaryLogo.png")

-- 7) Privateer flags + helper
local privateers = {
    {first="Arion",    last="Theron"},
    {first="Cassius",  last="Valerus"},
    {first="Nefra",    last="Sekeri",   nick="Sekhem"},
    {first="Lysander", last="Pallas"},
    {first="Marwan",   last="Othman"},
    {first="Octavia",  last="Marcellus"},
    {first="Amenhotep",last="Ankhu"},
    {first="Thalia",   last="Chrysos"},
    {first="Demetrius",last="Gravis"},
    {first="Selim",    last="Bey"},
    {first="Julius",   last="Drusus"},
    {first="Leyla",    last="Han"},
    {first="Marcus",   last="Flavianus"},
    {first="Fatima",   last="Pasha"},
    {first="Cornelia", last="Urbanus"},
    {first="Eudora",   last="Merit"},
    {first="Perseus",  last="Phoebus"},
    {first="Khepri",   last="Sobek"},
    {first="Lucius",   last="Aurelian"},
    {first="Aisha",    last="Dinar"},
    {first="Gaius",    last="Cassianus"},
    {first="Selene",   last="Argus"},
    {first="Arsinoe",  last="Menkaure"},
    {first="Omar",     last="Zahir"},
    {first="Dorian",   last="Grimm"},
    {first="Quintus",  last="Nero"},
    {first="Zainab",   last="Sultan"},
    {first="Cassian",  last="Corinth"},
    {first="Valeria",  last="Maximus",  nick="Ferox"},
    {first="Thorne",   last="Noctis",   nick="Mainomenos"}
}
local function getRandomPrivateer()
    local c = randomChoice(privateers)
    local flag = string.format("[[File:%s.png|20px]] ", c.last)
    if c.nick and math.random() < 0.20 then
        return string.format("%sCaptain %s ‘%s’ %s", flag, c.first, c.nick, c.last)
    else
        return string.format("%sCaptain %s %s", flag, c.first, c.last)
    end
end

-- 8) Determine Owner/Operator
local function getOwner(vt, ct, subtype, region)
    if vt == "Cargo" and isPortAuthority(region) then
        return region
    end
    if vt == "Ritual" then
        return randomChoice(ritualCults)
    elseif vt == "Military" then
        return randomChoice(militaryDivs)
    elseif vt == "Privateer" then
        return getRandomPrivateer()
    elseif vt == "Cargo" then
        if ct == "Agricultural" then
            local pool = buildPoolByClassSubtypeRegion("Agricultural", subtype, region)
            return randomChoice(pool) or randomChoice(agriCompanies)
        elseif ct == "Mining/Manufacturing" then
            local pool = buildPoolByClassSubtypeRegion("Mining/Manufacturing", subtype, region)
            return randomChoice(pool) or randomChoice(miningCompanies)
        elseif ct == "Services" then
            local pool = buildPoolByClassSubtypeRegion("Services", subtype, region)
            return randomChoice(pool) or randomChoice(serviceCompanies)
        elseif ct == "Unspecified" then
            local pool = buildGeneralPoolBySubtypeRegion(subtype, region)
            return randomChoice(pool) or "Independent Consortium"
        end
    end
    return "Independent Consortium"
end

-- 9) Build the table
function p.generateShipTable()
    math.randomseed(os.time())
    local y, d = p.getCurrentDate()

    local poolMap = {
        Agricultural = agriCompanies,
        ["Mining/Manufacturing"] = miningCompanies,
        Services = serviceCompanies
    }

    local totalTonnage = 0
    local ships = {}

    for i = 1, math.random(50, 80) do
        local hr, mn = math.random(0, 23), math.random(0, 59)

        local vt, ct = randomChoice(vesselTypes), randomChoice(cargoTypes)
        local subtype, sourceRegion = chooseManifestProfile(vt, ct)

local owner = getOwner(vt, ct, subtype, sourceRegion) or "Independent Consortium"

local home = randomHomeForRegion(sourceRegion)

local count = math.random(3, 4)
local picks = pickCargoCompanies(vt, owner, ct, subtype, sourceRegion, count)

-- Port authority safety lock:
-- If the owner or selected company is one of the port authorities,
-- force Home City, Owner/Operator, and Companies to all match.
local portLock = nil

if isPortAuthority(sourceRegion) then
    portLock = sourceRegion
elseif isPortAuthority(owner) then
    portLock = owner
else
    for _, nm in ipairs(picks) do
        if isPortAuthority(nm) then
            portLock = nm
            break
        end
    end
end

if portLock then
    vt = "Cargo"
    ct = "Services"
    subtype = "port_authority"
    sourceRegion = portLock
    home = portLock
    owner = portLock
    picks = { portLock }
end

local ownerLogo = ownerOperatorFlag[owner] and ("[[File:" .. ownerOperatorFlag[owner] .. "|20px]] ")
    or (companyLogo[owner] and ("[[File:" .. companyLogo[owner] .. "|20px]] ") or "")

        if #picks == 0 then
            local fallback = {}
            local base = (ct == "Unspecified") and generalPools or (poolMap[ct] or {})
            for _, name in ipairs(base) do
                local meta = getCompanyMeta(name)
                if meta.region == sourceRegion then
                    table.insert(fallback, name)
                end
            end
            picks = randomN(fallback, math.min(count, #fallback))
        end

        local compCell = "<ul>"
        for _, nm in ipairs(picks) do
            local logo = companyLogo[nm] and ("[[File:" .. companyLogo[nm] .. "|20px]] ") or ""
            compCell = compCell .. string.format("<li>%s%s</li>", logo, nm)
        end
        compCell = compCell .. "</ul>"

        local errList = {"Cargo Mislabeling","Improper Documentation","Surveillance Anomaly","Inspection Failure","Unregistered Ritual Cargo"}
        local err = math.random() < 0.2 and randomChoice(errList) or "None"

        local rawT = math.random() * 19000 + 1000
        local allowedT = math.min(rawT, MAX_DAILY_TONNAGE - totalTonnage)
        totalTonnage = totalTonnage + allowedT

        table.insert(ships, {
            date           = string.format("Day %d, Y.%d", d, y),
            time           = string.format("%02d:%02d", hr, mn),
            sort           = hr * 60 + mn,
            vid            = "VM-" .. math.random(10000, 99999),
            home           = cityFlag[home] .. " " .. home,
            typ            = vt,
            ownerCell      = ownerLogo .. owner,
            cargotype      = string.format("%s / %s / %s", ct, subtype, sourceRegion),
            cargoCompanies = compCell,
            berth          = math.random(1, 124),
            teus           = math.random(100, 1000),
            tonnage        = string.format("%.2f", allowedT),
            queue          = string.format("%.2f", math.random() * 5.5 + 0.5),
            turnaround     = string.format("%.2f", math.random() * 32 + 4),
            error          = err
        })

        if totalTonnage >= MAX_DAILY_TONNAGE then
            break
        end
    end

    table.sort(ships, function(a, b) return a.sort < b.sort end)

    local out = {
        '{| class="wikitable sortable"',
        '! Date !! Time !! Vessel ID !! Home City !! Type !! Owner/Operator !! Cargo Type !! Companies',
        '!! Berth !! TEUs !! Tonnage (t) !! Queue (hrs) !! Turnaround (hrs) !! Error'
    }
    for _, s in ipairs(ships) do
        local row = (s.error ~= "None") and '|- bgcolor="#ffcccc"' or '|-'
        table.insert(out, row)
        table.insert(out, table.concat({
            '|', s.date, '||', s.time, '||', s.vid, '||', s.home, '||', s.typ,
            '||', s.ownerCell, '||', s.cargotype, '||', s.cargoCompanies,
            '||', s.berth, '||', s.teus, '||', s.tonnage, '||', s.queue,
            '||', s.turnaround, '||', s.error
        }, ' '))
    end
    table.insert(out, '|}')
    table.insert(out, "'''Error Key:'''")
    table.insert(out, "* '''Cargo Mislabeling:''' Manifest ≠ actual goods")
    table.insert(out, "* '''Improper Documentation:''' Missing or outdated papers")
    table.insert(out, "* '''Surveillance Anomaly:''' Unexpected heat/motion")
    table.insert(out, "* '''Inspection Failure:''' Customs/hatch failure")
    table.insert(out, "* '''Unregistered Ritual Cargo:''' Not pre-cleared by Temple Bank")
    return table.concat(out, "\n")
end

function p.dailyReport()
    local y, d = p.getCurrentDate()
    return string.format(
        "'''General Port of Lake Morovia: Operational Log – Day %d, Year %d PSSC'''",
        d, y
    ) .. "\n" .. p.generateShipTable()
end

return p