Pre-trade, a countries damaged and total controlled factories are collected from three sources with three decimal places of precision:
- each controlled state's CIC/MIC/NIC and rocket sites, respectively scaled by 1 +
local_factories (e.g. compliance, resistance, occupation law, Support Bulgarian Occupation decision)
- the country's offsite buildings (e.g. Burma Road, various equipment purchase decisions etc.)
- donated factories through the modifiers
industrial_factory_donations, military_factory_donations, and dockyard_donations (i.e. for exiled governments)
These fractional numbers are then rounded towards zero as the countries controlled CIC, damaged CIC, controlled MIC, etc.
Next up is exchange of factories with other countries, which is counted in whole factories. "Export" in the furthest sense comes first, yielding "borrowed factories":
- CIC from exported equipment licenses
- CIC from exported resources
- CIC and MIC from subjects. CIC_total *
cic_to_overlord_factor and MIC_total * mic_to_overlord_factor (e.g. subject level and Ferdinand's Puppet national spirit), rounded to closest integer.
- CIC and MIC from other countries. CIC_total *
cic_to_target_factor and MIC_total * mic_to_target_factor (e.g. PRC Soviet tribute and... that's it), rounded to closest integer
- You may note that CIC_total and MIC_total were not defined yet. Recursion (for subjects) and prayers (for other countries) cover those cases.
"Imports" are limited to the difference of total CIC and damaged CIC from the pre-trade phase and are accounted as "lended factories" (that's a word now, deal with it):
- CIC for imported equipment licenses
- CIC for imported resources
- the limited CIC amount available for trade is used on a first-come-first-serve basis for above two points
- CIC and MIC to overlord: CIC_total *
cic_to_overlord_factor and MIC_total * mic_to_overlord_factor (same as above)
- CIC and MIC to other countries. CIC_total *
cic_to_target_factor and MIC_total * mic_to_target_factor (same as above)
CIC_total = controlled CIC + borrowed CIC
MIC_total = controlled MIC + borrowed MIC; (skipping over the further details for MIC)
Now consumer goods and special projects CIC gets calculated. The targeted amount for the former is CIC_total + MIC_total, scaled by
consumer_goods_factor and rounded towards zero (economy law etc. as listed in the wiki). CIC_total - damaged CIC serves as an upper bound for CG
Special projects encompasses everything using the
civilian_factory_use modifier (e.g. various construction decisions, resource prospecting, agent recruitment, agency upgrades etc.).
Regardless, if borrowed CIC is not enough, CG/projects are paid from controlled CIC - lended CIC. And finally if that's still not enough (leaving no CIC for construction), imports get reduced too. However many factories get allocated are counted as used.
There
might be a bug here because CG and project CIC are accounted for independently. If CG and projects independently don't cut into imports, their sum potentially doesn't either even if it should. I don't have any of the prospecting or spy agency DLCs to test this but as an example, say a country has 20 CIC total and spends 5 on imports. If CG and projects are both 9 CIC, the country should be forced to reduce imports. But if my prediction is correct, trade will not be reduced because 9 < controlled CIC - lended CIC.
Now we're already assigning CIC to construction lines! Every line can use up to NDefines::NProduction::MAX_CIV_FACTORIES_PER_LINE (15) factories out of a remaining R = CIC_total - lended CIC - used CIC. If the overlord is already contributing to the construction line, the subject can only do what is left. No construction in provinces with land combat. Of the N factories being assigned, N - R + damaged CIC will be damaged, the others not.