Module:ClaimLister/Peace

From Terra Invicta Official Wiki

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

---
---Reads the Claim data files and computes a table of Nations to 
----all nations they can peacefully unify with (possibly indirectly) without ever using war.
----And without future tech social.

--region Dependencies
local ClaimData = mw.loadData('Module:CSVReader/Claim')
local RegionData = mw.loadData('Module:CSVReader/Region')
--endregion

--region Private constants
--endregion

--region Private member variables
local peaceTable = {{},{}}
--endregion

--region Private methods

function getPeaceTable()
	-- First get the direct peaceful claims
	for rowID, row in pairs(ClaimData[4]) do
		if not peaceTable[1][rowID] then
			peaceTable[1][rowID] = {} -- Nations
			peaceTable[2][rowID] = {} -- Regions
		end
		for _, regionID in ipairs(row) do
			if RegionData[regionID] then -- Check that region actually exists.
				peaceTable[2][rowID][regionID] = true
				for _, nationID in ipairs(ClaimData[5][regionID]) do
					-- Ban Protectorate Authority nation
					if rowID ~= nationID and nationID ~= 'PRA' and nationID ~= '2070_PRA' and nationID ~= '2026_PRA' then
						peaceTable[1][rowID][nationID] = true
					end
				end
			end
		end
	end
	
	-- Dynamic Programming
	local changed = true
	local maxIterations = 10  -- Safety limit to prevent infinite loops
	local iteration = 0
	
	while changed and iteration < maxIterations do
		changed = false
		iteration = iteration + 1
		
		for rowID, row in pairs(peaceTable[1]) do
			local toAdd = {{},{}}
			for nationID, _ in pairs(row) do
				for subregionID, _ in pairs(peaceTable[2][nationID]) do
					toAdd[2][subregionID] = true
				end
				for subnationID, _ in pairs(peaceTable[1][nationID]) do
					toAdd[1][subnationID] = true
				end
			end
			
			for subnationID, _ in pairs(toAdd[1]) do
				if not peaceTable[1][rowID][subnationID] and rowID ~= subnationID then
					changed = true
					peaceTable[1][rowID][subnationID] = true
				end
			end
			for subregionID, _ in pairs(toAdd[2]) do
				if not peaceTable[2][rowID][subregionID] then
					changed = true
					peaceTable[2][rowID][subregionID] = true
				end
			end
		end
		
	end
	
	return peaceTable
end

--endregion

--region Public methods
--endregion

return getPeaceTable()