﻿V_kCAFG_county_setup_faith_conversion_factors_impact = 0.9 # Used to reduce the impact of the following vanilla bonuses and maluses when setupping the whole map
V_kCAFG_county_setup_holy_site_followers_influence_factor = 0.025

V_kCAFG_province_natural_faith_conversion_base = 100
V_kCAFG_province_natural_faith_conversion_max_tension = 0.75
V_kCAFG_province_natural_faith_conversion_tension_impact = 115
V_kCAFG_province_natural_faith_conversion_low_adherents_impact = 195

V_kCAFG_province_natural_faith_conversion_bad_weight_threshold = 50 # Under this weight, conversion will not be guaranteed and be chance-based
V_kCAFG_province_natural_faith_conversion_bad_weight_max_chance = 95 # Under the bad weight, conversion will have this % chance at best


# Requires faith Scope: kCAFG_faith
V_kCAFG_province_faith_factor = {
    scope:kCAFG_faith = { save_temporary_scope_as = V_kCAFG_province_faith_factor_param }
    value = V_kCAFG_province_faith_factor_internal
}

# Requires faith Scope: kCAFG_faith
V_kCAFG_province_faith_factor_rounded = {
    value = V_kCAFG_province_faith_factor
    multiply = 100
    round = yes
    min = 1
    divide = 100
}

# Requires faith Scope: kCAFG_faith
V_kCAFG_province_faith_amount = {
    value = V_kCAFG_province_faith_factor
    multiply = V_kCAFG_province_values_scale
}

# Requires faith Scope: kCAFG_faith
V_kCAFG_province_faith_amount_rounded = {
    value = V_kCAFG_province_faith_amount
    multiply = 100
    round = yes
    min = 1
    divide = 100
}

V_kCAFG_province_prev_faith_factor = {
    prev = { save_temporary_scope_as = V_kCAFG_province_faith_factor_param }
    value = V_kCAFG_province_faith_factor_internal
}

V_kCAFG_province_prev_faith_amount = {
    value = V_kCAFG_province_prev_faith_factor
    multiply = V_kCAFG_province_values_scale
}

# Requires faith Scope: V_kCAFG_province_faith_factor_param
V_kCAFG_province_faith_factor_internal = {
    value = 0
    if = {
        limit = { T_kCAFG_does_province_contain_faith = { FAITH = scope:V_kCAFG_province_faith_factor_param } }
        every_in_list = {
            variable = kCAFG_province_faith_structs
            limit = { this.var:faith = scope:V_kCAFG_province_faith_factor_param }
            value = this.var:factor
        }
    }
}

V_kCAFG_province_root_faith_factor = {
    value = 0
    if = {
        limit = { T_kCAFG_does_province_contain_faith = { FAITH = root } }
        every_in_list = {
            variable = kCAFG_province_faith_structs
            limit = { this.var:faith = root }
            value = this.var:factor
        }
    }
}

# Vanilla multiplicative (i.e. Scaled by councillor skill) modifiers applied to a conversion from converted faith to converter faith
# Scope : converting faith
# kCAFG_converted_faith : faith
# Optional Scopes :
# kCAFG_converting_culture
# kCAFG_converted_culture
# kCAFG_converted_province
V_kCAFG_county_setup_faith_conversion_power_mult = {
    value = 1
    if = {
		limit = {
			has_doctrine_parameter = pluralism_fundamentalist_proselytization_bonus
		}
        add = V_kCAFG_faith_conversion_pluralism_fundamentalist_proselytization_bonus
    }

    if = {
		limit = {
		    has_doctrine_parameter = pluralism_pluralistic_proselytization_penalty
		}
        add = V_kCAFG_faith_conversion_pluralism_pluralistic_proselytization_malus
    }

    if = {
		limit = {
			scope:kCAFG_converted_faith = { has_doctrine_parameter = pluralism_fundamentalist_proselytization_bonus }
        }
        subtract = V_kCAFG_faith_conversion_pluralism_fundamentalist_proselytization_bonus
    }

    if = {
		limit = {
			scope:kCAFG_converted_faith = { has_doctrine_parameter = pluralism_pluralistic_proselytization_penalty }
        }
        subtract = V_kCAFG_faith_conversion_pluralism_pluralistic_proselytization_malus
    }

    if = {
        limit = {
			exists = scope:kCAFG_converted_culture
            exists = scope:kCAFG_converting_culture
        }

        if = {
            limit = {
                has_doctrine_parameter = same_culture_conversion_bonus_active
                scope:kCAFG_converted_culture = scope:kCAFG_converting_culture
            }
            add = {
                value = V_kCAFG_faith_conversion_tenet_communal_identity_bonus
                multiply = 1.5 # Boost a bit
            }
        }

        if = {
            limit = {
				has_doctrine_parameter = other_culture_conversion_penalty_active
                NOT = { scope:kCAFG_converted_culture = scope:kCAFG_converting_culture }
            }
            add = {
                value = V_kCAFG_faith_conversion_tenet_communal_identity_malus
                multiply = 1.5 # Boost a bit
            }
        }
    }
}

# Multiplier applied to a conversion from converted faith to converter faith
# Scope : converting faith
# kCAFG_converted_faith : faith
V_kCAFG_county_setup_faith_conversion_mult = {
    value = 1
    
    if = {
        limit = { this.religion = scope:kCAFG_converted_faith.religion }
        add = 0.45
    }

    if = {
        limit = {
            scope:kCAFG_converted_faith = {
                OR = {
                    has_doctrine = doctrine_pluralism_pluralistic
                    faith_hostility_level = {
                        target = prev
                        value = faith_fully_accepted_level
                    }
                }
            }
        }
        add = 0.25
    }

    if = {
        limit = {
            has_doctrine_parameter = unreformed
            scope:kCAFG_converted_faith = { NOT = { has_doctrine_parameter = unreformed } }
        }
        multiply = 0.7
    }

    if = {
        limit = {
            scope:kCAFG_converted_faith = {
                faith_hostility_level = {
                    target = prev
                    value >= faith_astray_level
                }
            }
        }
        multiply = 0.9
    }
    else_if = {
        limit = {
            scope:kCAFG_converted_faith = {
                faith_hostility_level = {
                    target = prev
                    value >= faith_hostile_level
                }
            }
        }
        multiply = 0.55
    }
    else_if = {
        limit = {
            scope:kCAFG_converted_faith = {
                faith_hostility_level = {
                    target = prev
                    value >= faith_evil_level
                }
            }
        }
        multiply = 0.01
    }
}

# Multiplier applied to a county conversion from converted faith to converter faith
# Scope : converting faith
# kCAFG_converted_faith : faith
# kCAFG_converted_province
V_kCAFG_county_setup_faith_province_conversion_mult = {
    value = 1
    
    if = {
        limit = { scope:kCAFG_converted_province.barony = { is_holy_site_of = scope:kCAFG_converted_faith } }
        multiply = 0.8
    }
    
    if = {
        limit = { scope:kCAFG_converted_province.barony = { is_holy_site_of = prev } }
        multiply = 1.2
    }

    multiply = scope:kCAFG_converted_province.V_kCAFG_province_terrain_conversion_factor
}

V_kCAFG_county_setup_faith_province_conversion_mult_VANILLA_B = {
    value = 1

    if = {
        limit = { scope:kCAFG_converted_province.county = { has_county_modifier = persecuted_minorities_expelled } }
        multiply = 1.5
    }

    if = {
        limit = { scope:kCAFG_converted_province.county = { has_county_modifier = minority_persecution_forbidden } }
        multiply = 0.1
    }
}

V_kCAFG_county_setup_faith_province_conversion_mult_VANILLA = {
    value = 1

    if = {
        limit = {
            scope:kCAFG_converted_province.county = { has_county_modifier = fp2_jewish_community_denied_modifier }
        }
        multiply = 0.1
    }
}

# Multiplier applied to a county conversion from converted faith to converter faith - Setup county
# Scope : converting faith
# kCAFG_converted_faith : faith
V_kCAFG_province_faith_setup_mult = {
    value = 1
    multiply = {
        value = scope:kCAFG_converted_province.V_kCAFG_province_faith_conversion_multiplier
        multiply = V_kCAFG_county_setup_faith_conversion_factors_impact
    }
    # Bonuses to some values
    multiply = {
        value = 1
        add = {
            value = V_kCAFG_county_setup_faith_conversion_power_mult
            multiply = V_kCAFG_county_setup_faith_conversion_factors_impact
        }
        min = 0
    }
    multiply = V_kCAFG_county_setup_faith_conversion_mult
    multiply = V_kCAFG_county_setup_faith_province_conversion_mult
}

# Scope : faith
# prev : faith
V_kCAFG_fervor_difference_with_prev = {
    add = fervor
    subtract = prev.fervor
}

# Scope : faith
V_kCAFG_fervor_factor = {
    value = fervor
    divide = define:NFaith|MAX_FERVOR # Scale to 0-1
}

# Scope : county
# Requires faith Scope: kCAFG_faith
V_kCAFG_county_faith_factor = {
    if = {
        limit = { V_kCAFG_county_values_scale > 0 }
        value = 0
        every_county_province = {
            add = {
                value = V_kCAFG_province_faith_factor
                multiply = V_kCAFG_province_values_scale
            }
        }
        divide = V_kCAFG_county_values_scale
    }
    else = {
        value = V_kCAFG_province_relative_scale
    }
}

# Scope : faith
# Required scopes :
# kCAFG_converted_province : the province we want to convert the faith in
# kCAFG_converting_faith : the faith we want to convert to
V_kCAFG_province_faith_conversion_weight = {
    if = {
        limit = { scope:kCAFG_converting_faith = this }
        value = 0 # Cannot convert to itself
    }
    else = {
        # Scale based on level
        save_temporary_scope_as = kCAFG_faith # used by V_kCAFG_province_faith_factor
        value = scope:kCAFG_converted_province.V_kCAFG_province_faith_factor
		save_temporary_value_as = converted_faith_factor
        multiply = 10000 # Maybe I'm imagining things but weight might work better with higher numbers
        # Rescale by excluding converting faith
        divide = {
            value = 1
            scope:kCAFG_converting_faith = {
                scope:kCAFG_converted_province = {
                    subtract = V_kCAFG_province_prev_faith_factor
                }
            }
            min = V_kCAFG_smallest_minority_factor
        }
        # Kei : maybe have a "resistance" or "health" factor for certain faiths in certain provinces or faith types/tenets/doctrines
        # The smaller, the harder to convert if there are other faiths with higher levels
        if = {
            limit = {
                scope:kCAFG_converted_province = {
                    scope:converted_faith_factor <= 0.5
                    any_in_list = {
                        variable = kCAFG_province_faith_structs
                        scope:kCAFG_converting_faith != var:faith
                        var:factor > scope:converted_faith_factor
                    }
                }
            }
            multiply = {
                value = 0.1
                add = {
                    value = scope:converted_faith_factor
                    divide = 0.5
                    multiply = 0.9
                }
            }
        }
        # If very small minority, improbable to convert if there are other faiths with higher levels
        if = {
            limit = {
                scope:kCAFG_converted_province = {
                    scope:converted_faith_factor <= V_kCAFG_very_small_minority_factor
                    any_in_list = {
                        variable = kCAFG_province_faith_structs
                        scope:kCAFG_converting_faith != var:faith
                        var:factor > V_kCAFG_very_small_minority_factor
                    }
                }
            }
            multiply = 0.01 # Already reduced by the "small minority" multiplier so we don't ovedo it, the conversion might still happen 
        }
    }
}

# The less a faith is present in the world, the harder it is to convert (preserve small minority faiths)
# kCAFG_converting_faith : faith
V_kCAFG_faith_world_presence_conversion_factor = {
    value = num_county_followers
    subtract = V_kCAFG_world_counties_very_small_amount
    divide = {
        value = V_kCAFG_world_counties_medium_amount
        subtract = V_kCAFG_world_counties_very_small_amount
    }
    max = 1

    # heresy mitigation
    if = {
        limit = {
            religion = scope:kCAFG_converting_faith.religion
            is_valid_heresy = {
                ORIGIN_FAITH = scope:kCAFG_converting_faith
                HERETICAL_FAITH = this
            }
        }
        multiply = 2
        add = 1
        divide = 3
    }
    # unreformed mitigation
    if = {
        limit = {
            has_doctrine_parameter = unreformed
        }
        add = 1
        divide = 2
    }

    multiply = 0.9
    add = 0.1
}

# Can be negative
# Scope : province
# kCAFG_converting_faith : faith
V_kCAFG_province_natural_faith_conversion_weight = {
    ## Save factor
    value = V_kCAFG_province_faith_factor
    save_temporary_value_as = faith_factor
    ## ----

    value = V_kCAFG_province_natural_faith_conversion_base
    multiply = 0.5

    # Same faith neighbours
    if = {
        limit = { any_neighboring_province = { always = yes } }
        add = {
            value = 0
            every_neighboring_province = {
                every_in_list = {
                    variable = kCAFG_province_faith_structs
                    limit = { scope:kCAFG_converting_faith != var:faith }
                    add = var:factor
                }
            }
            divide = {
                value = 0
                every_neighboring_province = { add = 1 }
            }
            multiply = V_kCAFG_province_natural_faith_conversion_base
            multiply = 0.5
        }
    }

    # Tension reduces conversion weight
    subtract = {
        value = var:kCAFG_faith_tension
        subtract = 0.1
        min = 0
        divide = 0.9
        divide = V_kCAFG_province_natural_faith_conversion_max_tension
        multiply = V_kCAFG_province_natural_faith_conversion_tension_impact
    }
    
    # # Small Minorities are harder to convert
    if = {
        limit = {
            scope:faith_factor >= V_kCAFG_inverted_small_minority_factor
        }
        add = {
            value = 1
            # Especially if they have low world presence
            subtract = {
                value = 0
                every_in_list = {
                    variable = kCAFG_province_faith_structs
                    limit = { scope:kCAFG_converting_faith != var:faith }

                    add = {
                        value = var:faith.V_kCAFG_faith_world_presence_conversion_factor
                        multiply = var:factor # Scale by converted faith factor
                    }
                }
                # rescale by excluding converting faith
                divide = {
                    value = 1
                    subtract = scope:faith_factor
                }
            }

            multiply = {
                value = scope:faith_factor
                subtract = V_kCAFG_inverted_small_minority_factor
                min = 0
                divide = {
                    value = V_kCAFG_small_minority_factor
                    subtract = V_kCAFG_very_small_minority_factor
                }

                # Reduce impact
                multiply = 0.5
                add = 0.5
            }

            multiply = V_kCAFG_province_natural_faith_conversion_low_adherents_impact
            multiply = -1
        }
    }
    
    multiply = V_kCAFG_province_faith_conversion_multiplier

    # The closer to zero we are, the more likely the province will be converted
    # 100% multiplier at 0% faith presence
    # 50% multiplier at 50% faith presence
    # 15% multiplier at 100% faith presence
    if = {
        limit = { scope:faith_factor <= 0.5 }
        multiply = {
            value = 1
            subtract = scope:faith_factor
        }
    }
    if = {
        limit = { scope:faith_factor > 0.5 }
        multiply = {
            value = 0.5
            subtract = {
                value = scope:faith_factor # 0.5-1
                subtract = 0.5 # 0-0.5
                multiply = 2 # 0-1
                multiply = 0.35 # 0-0.35
            }
        }
    }
    # unwelcoming holder
    if = {
        limit = { county.holder.faith != scope:kCAFG_converting_faith }
        divide = 2
    }
    # holder is hof
    if = {
        limit = {
            exists = scope:kCAFG_converting_faith.religious_head
            county.holder = scope:kCAFG_converting_faith.religious_head
        }
        multiply = 1.5
    }

    # tolerance laws
    multiply = V_kCAFG_faith_tension_tolerance_law_factor
}