Some suggested changes to ProductionPopup.lua

ww2commander

Emperor
Joined
Aug 23, 2003
Messages
1,243
Location
Australia
I stumbled on a few bugs in the ProductionPopup.lua file located within CAT.

The bugs are triggered by some custom civs that use units and buildings for special civ traits (i.e colonial civs series). If modders don't specify a DOMAIN for a unit or DESCRIPTION for building then the production window fails to show properly.

I have added a few checking conditions to weed out these types of entries as shown in red below.

Hope this helps :)

Code:
function InitializeProductionLists()
	--[[
	
	Unmodded game scans every data table, and re-sorts every list every time we call UpdateWindow.
	
	Mod method creates the lists just 1 time with more helpful sorting:
	
	- Units sort by domain (civilian, land, sea, air), then tech column (newest first), then name (alphabetical).
	- Buildings sort by flavor (growth, science, etc), then tech column (oldest first), then name (alphabetical).
	- Wonders sort by type (project, national, world), then tech column (oldest first), then name (alphabetical).
	
	--]]
	
	productionList				= {}
	productionList.Units		= {}
	productionList.Buildings	= {}
	productionList.Projects		= {}
	productionList.Processes	= {}
	
	local mainFlavor = {}
	local tech
	local itemPriority = 0
	local itemName
	
	-- Units
	for unit in GameInfo.Units() do
		itemName = Locale.ConvertTextKey(unit.Description)
		itemPriority = unit.ListPriority
		if [COLOR="Red"][B]([/B][/COLOR]itemPriority == nil or itemPriority == -1[COLOR="Red"][B])[/B][/COLOR] [COLOR="Red"][B]and unit.Domain[/B][/COLOR] then
			itemPriority = GameInfo.Domains[unit.Domain].ListPriority
		end
		
		if unit.Combat == 0 and unit.RangedCombat == 0 then
			itemPriority = itemPriority + GameDefines.LIST_PRIORITY_NONCOMBAT
		end
		
		tech = unit.PrereqTech
		tech = tech and GameInfo.Technologies[tech].GridX or 0
		table.insert(productionList.Units, {
			ID=unit.ID,
			Priority=itemPriority,
			TechColumn=-1 * tech,
			Name=itemName
		})
	end

	-- Buildings
	for row in GameInfo.Building_Flavors() do
		if row.FlavorType == nil then
			log:Fatal("ProductionPopup Sorting: %s FlavorType is nil", row.buildingType)
		elseif row.Flavor == nil then
			log:Fatal("ProductionPopup Sorting: %s Flavor is nil", row.buildingType)
		elseif string.find(row.FlavorType, "FLAVOR_RES") then
			-- skip these
		else
			local buildingType = row.BuildingType
			if mainFlavor[buildingType] == nil then
				mainFlavor[buildingType] = {}
				mainFlavor[buildingType].Flavor = row.Flavor
				mainFlavor[buildingType].FlavorType = row.FlavorType
			elseif row.Flavor > mainFlavor[buildingType].Flavor then
				mainFlavor[buildingType].Flavor = row.Flavor
				mainFlavor[buildingType].FlavorType = row.FlavorType
			end
		end
	end
	for building in GameInfo.Buildings() do
		local buildingClass = GameInfo.BuildingClasses[building.BuildingClass]
		[B][COLOR="Red"]if building.Description then[/COLOR][/B]
			itemName = Locale.ConvertTextKey(building.Description)
		[COLOR="Red"][B]else[/B][/COLOR]
		[COLOR="Red"][B]	itemName = ""
		end[/B][/COLOR]
		itemPriority = building.ListPriority
		if itemPriority == nil or itemPriority == -1 then
			if buildingClass.MaxGlobalInstances == 1 then
				itemPriority = GameDefines.LIST_PRIORITY_WORLD_WONDER
			elseif buildingClass.MaxPlayerInstances == 1 then
				itemPriority = GameDefines.LIST_PRIORITY_NATIONAL_WONDER
			else
				local flavor = mainFlavor[building.Type]
				if flavor then
					itemPriority = GameInfo.Flavors[flavor.FlavorType].ListPriority
					--print(tostring(itemPriority).." "..flavor.FlavorType.." "..itemName)
				end
			end
		end	
		tech = building.PrereqTech
		if tech then
			tech = GameInfo.Technologies[tech]
			if tech then
				tech = tech.GridX
			else
				log:Error("%s PrereqTech=%s", building.Type, building.PrereqTech)
				tech = 0
			end
		else
			tech = 0
		end
		table.insert(productionList.Buildings, {
			ID=building.ID,
			Priority=itemPriority,
			TechColumn=tech,
			Name=itemName
		})
	end

	-- Projects
	for project in GameInfo.Projects() do
		itemName = Locale.ConvertTextKey(project.Description)
		itemPriority = project.ListPriority or 0
		tech = 0
		table.insert(productionList.Projects, {
			ID=project.ID,
			Priority=itemPriority,
			TechColumn=tech,
			Name=itemName
		})
	end

	-- Processes
	for process in GameInfo.Processes() do
		itemPriority = process.ListPriority or 0
		tech = 0
		itemName = Locale.ConvertTextKey(process.Description)
		table.insert(productionList.Processes, {
			ID=process.ID,
			Priority=itemPriority,
			TechColumn=tech,
			Name=itemName
		})
	end

	function SortProductionTable(a,b)
		if a.Priority ~= b.Priority then
			return a.Priority > b.Priority
		elseif a.TechColumn ~= b.TechColumn then
			return a.TechColumn < b.TechColumn
		else
			return a.Name < b.Name
		end
	end

	table.sort(productionList.Units, SortProductionTable)
	table.sort(productionList.Buildings, SortProductionTable)
	table.sort(productionList.Projects, SortProductionTable)
	table.sort(productionList.Processes, SortProductionTable)
end

EDIT: This probably explains a few of the production bugs posts below which I just noticed.
 
Top Bottom