﻿namespace = heresy_historical

heresy_historical.0001 = {
    hidden = yes
    scope = faith

    immediate = {
        save_scope_as = origin_faith

        if = {
            limit = {
                pam_heresy_theoadamist_eligible_trigger = yes
                pam_heresy_marblist_eligible_trigger = yes
                pam_heresy_elysian_eligible_trigger = yes
            }
            random_list = {
                100 = { faith:theoadamist_faith = { save_scope_as = heresy_faith } }
                100 = { faith:marblist = { save_scope_as = heresy_faith } }
                100 = { faith:elysian = { save_scope_as = heresy_faith } }
            }
        }
        else_if = {
            limit = {
                pam_heresy_theoadamist_eligible_trigger = yes
                pam_heresy_marblist_eligible_trigger = yes
            }
            random_list = {
                100 = { faith:theoadamist_faith = { save_scope_as = heresy_faith } }
                100 = { faith:marblist = { save_scope_as = heresy_faith } }
            }
        }
        else_if = {
            limit = {
                pam_heresy_theoadamist_eligible_trigger = yes
                pam_heresy_elysian_eligible_trigger = yes
            }
            random_list = {
                100 = { faith:theoadamist_faith = { save_scope_as = heresy_faith } }
                100 = { faith:elysian = { save_scope_as = heresy_faith } }
            }
        }
        else_if = {
            limit = {
                pam_heresy_marblist_eligible_trigger = yes
                pam_heresy_elysian_eligible_trigger = yes
            }
            random_list = {
                100 = { faith:marblist = { save_scope_as = heresy_faith } }
                100 = { faith:elysian = { save_scope_as = heresy_faith } }
            }
        }
        else_if = {
            limit = {
                pam_heresy_theoadamist_eligible_trigger = yes
            }
            faith:theoadamist_faith = { save_scope_as = heresy_faith }
        }
        else_if = {
            limit = {
                pam_heresy_marblist_eligible_trigger = yes
            }
            faith:marblist = { save_scope_as = heresy_faith }
        }
        else_if = {
            limit = {
                pam_heresy_elysian_eligible_trigger = yes
            }
            faith:elysian = { save_scope_as = heresy_faith }
        }

        if = {
            limit = { exists = scope:heresy_faith }
            if = {
                limit = { scope:heresy_faith = { this = faith:marblist } }
                random_faith_ruler = {
                    limit = { pam_heresy_marblist_epicenter_trigger = yes }
                    weight = {
                        base = 100
                        modifier = { factor = 1.5 has_trait = zealous }
                        modifier = { factor = 1.5 has_trait = theologian }
                    }
                    save_scope_as = epicenter_ruler
                    save_scope_as = founder
                    capital_county = { save_scope_as = heresy_origin_county }
                }
            }
            else = {
                random_faith_ruler = {
                    limit = { pam_heresy_neow_epicenter_trigger = yes }
                    weight = {
                        base = 100
                        modifier = { factor = 1.5 has_trait = zealous }
                        modifier = { factor = 1.5 has_trait = theologian }
                        modifier = { factor = 0.75 highest_held_title_tier >= tier_empire }
                    }
                    save_scope_as = epicenter_ruler
                    save_scope_as = founder
                    capital_county = { save_scope_as = heresy_origin_county }
                }
            }

            if = {
                limit = {
                    exists = scope:epicenter_ruler
                    exists = scope:heresy_origin_county
                }

                if = {
                    limit = { scope:heresy_faith = { this = faith:marblist } }
                    scope:epicenter_ruler = {
                        create_character = {
                            template = priest_character_template
                            name = "Konst_a_act_ntios"
                            gender = male
                            age = 38
                            dynasty = none
                            employer = scope:epicenter_ruler
                            culture = culture:aromanian 
                            faith = scope:heresy_faith
                            rite = scope:heresy_faith.main_rite
                            save_scope_as = founder
                        }
                    }
                }

                scope:heresy_faith.main_rite = { set_can_convert_to_rite = yes }
                if = {
                    limit = { scope:epicenter_ruler = { is_ai = yes } }
                    scope:epicenter_ruler = {
                        pam_heresy_convert_ruler_to_heresy_faith_effect = yes
                        if = {
                            limit = { NOT = { has_trait = heresiarch } }
                            add_trait = heresiarch
                        }
                    }
                }

                if = {
                    limit = {
                        scope:heresy_faith = { this = faith:theoadamist_faith }
                        NOT = { exists = title:k_theoadamist_hof.holder }
                    }
                    create_title_and_vassal_change = {
                        type = created
                        save_scope_as = theoadamist_title_change
                        add_claim_on_loss = no
                    }
                    title:k_theoadamist_hof = {
                        change_title_holder = {
                            holder = scope:epicenter_ruler
                            change = scope:theoadamist_title_change
                        }
                    }
                    resolve_title_and_vassal_change = scope:theoadamist_title_change
                }

                scope:heresy_origin_county = {
                    set_county_faith = scope:heresy_faith
                    every_neighboring_county = {
                        limit = { faith = scope:origin_faith }
                        set_county_faith = scope:heresy_faith
                    }
                }

                scope:heresy_faith = { save_scope_as = c3_spread_owner }
                scope:c3_spread_owner = {
                    set_variable = {
                        name = c3_spread_remaining_converts
                        value = pam_heresy_c3_spread_budget_base_value
                    }
                }
                trigger_event = { id = heresy_historical.0011 days = { 5 10 } }

                every_player = {
                    limit = {
                        faith = scope:origin_faith
                    }
                    if = {
                        limit = { scope:heresy_faith = { this = faith:theoadamist_faith } }
                        trigger_event = heresy_historical.0050
                    }
                    else_if = {
                        limit = { scope:heresy_faith = { this = faith:marblist } }
                        trigger_event = heresy_historical.0051
                    }
                    else_if = {
                        limit = { scope:heresy_faith = { this = faith:elysian } }
                        trigger_event = heresy_historical.0052
                    }
                }

                if = {
                    limit = { scope:heresy_faith = { this = faith:theoadamist_faith } }
                    set_global_variable = {
                        name = pam_heresy_theoadamist_cooldown
                        value = yes
                        years = 50
                    }
                }
                else_if = {
                    limit = { scope:heresy_faith = { this = faith:marblist } }
                    set_global_variable = {
                        name = pam_heresy_marblist_cooldown
                        value = yes
                        years = 50
                    }
                }
                else_if = {
                    limit = { scope:heresy_faith = { this = faith:elysian } }
                    set_global_variable = {
                        name = pam_heresy_elysian_cooldown
                        value = yes
                        years = 50
                    }
                }
                pam_heresy_fervor_reset_effect = { COMPONENT = 3 }
            }
        }
    }
}

heresy_historical.0050 = {
    type = character_event
    window = big_event_window
    title = neow_theoadamist_heresy.0050.t
    desc = neow_theoadamist_heresy.0050.desc
    major = yes
    theme = christian
    major_trigger = { faith = scope:origin_faith }
    left_portrait = {
        character = scope:founder
        animation = preaching
        camera = camera_event_right_forward
    }
    option = {
        name = neow_theoadamist_heresy.0050.a
        add_internal_flag = dangerous
        trigger = {
            NOR = {
                faith.religious_head ?= this
                rite.head_of_rite ?= this
            }
        }
        set_character_faith_with_conversion = scope:heresy_faith
        add_character_flag = { flag = recently_converted_by_heresy years = 10 }
        change_spiritual_fulfillment = 20
        ai_chance = { base = 0 }
    }
    option = {
        name = neow_theoadamist_heresy.0050.b
        add_piety = medium_piety_gain
        ai_chance = { base = 100 }
    }
}

heresy_historical.0051 = {
    type = character_event
    window = big_event_window
    title = neow_marblist_heresy.0051.t
    desc = neow_marblist_heresy.0051.desc
    major = yes
    theme = christian
    major_trigger = { faith = scope:origin_faith }
    left_portrait = {
        character = scope:founder
        animation = preaching
        camera = camera_event_right_forward
    }
    option = {
        name = neow_marblist_heresy.0051.a
        add_internal_flag = dangerous
        trigger = {
            NOR = {
                faith.religious_head ?= this
                rite.head_of_rite ?= this
            }
        }
        set_character_faith_with_conversion = scope:heresy_faith
        add_character_flag = { flag = recently_converted_by_heresy years = 10 }
        change_spiritual_fulfillment = 20
        ai_chance = { base = 0 }
    }
    option = {
        name = neow_marblist_heresy.0051.b
        add_piety = medium_piety_gain
        ai_chance = { base = 100 }
    }
}

heresy_historical.0052 = {
    type = character_event
    window = big_event_window
    title = neow_elysian_heresy.0052.t
    desc = neow_elysian_heresy.0052.desc
    major = yes
    theme = christian
    major_trigger = { faith = scope:origin_faith }
    left_portrait = {
        character = scope:founder
        animation = preaching
        camera = camera_event_right_forward
    }
    option = {
        name = neow_elysian_heresy.0052.a
        add_internal_flag = dangerous
        trigger = {
            NOR = {
                faith.religious_head ?= this
                rite.head_of_rite ?= this
            }
        }
        set_character_faith_with_conversion = scope:heresy_faith
        add_character_flag = { flag = recently_converted_by_heresy years = 10 }
        change_spiritual_fulfillment = 20
        ai_chance = { base = 0 }
    }
    option = {
        name = neow_elysian_heresy.0052.b
        add_piety = medium_piety_gain
        ai_chance = { base = 100 }
    }
}

heresy_historical.0011 = {
	hidden = yes
	scope = faith

	trigger = {
		exists = scope:origin_faith
		exists = scope:heresy_faith
		exists = scope:epicenter_ruler
        exists = scope:heresy_origin_county
		exists = scope:founder
		exists = scope:c3_spread_owner
		scope:c3_spread_owner = {
			var:c3_spread_remaining_converts > 0
		}
	}

	immediate = {
		clear_saved_scope = convert_ruler

		scope:origin_faith = {
			random_faith_ruler = {
				limit = {
					is_physically_able_ai = yes
					OR = {
						AND = {
							is_landed = yes
							highest_held_title_tier >= tier_county
						}
						any_held_title = { has_clerical_region = yes }
					}
					NAND = {
						scope:heresy_faith = { has_doctrine = doctrine_theocracy_lay_clergy }
						any_held_title = { has_clerical_region = yes }
					}
					NOR = {
						this = scope:founder
						this = scope:epicenter_ruler
						this = faith.religious_head
						any_held_title = { is_head_of_faith = yes }
						has_character_flag = recently_converted_by_heresy
						recently_converted_faith_trigger = yes
					}
					exists = capital_county
					OR = {
						AND = {
							faith = { has_doctrine_parameter = has_clerical_regions }
							exists = capital_county.clerical_region_title
							scope:heresy_origin_county.clerical_region_title ?= capital_county.clerical_region_title
						}
						capital_county = {
							squared_distance = {
								target = scope:heresy_origin_county
								value <= squared_distance_small
							}
						}
					}
				}

				weight = {
					base = 100

					modifier = {
						add = -40
						capital_county = {
							squared_distance = {
								target = scope:heresy_origin_county
								value > squared_distance_small
							}
						}
					}

					modifier = {
						factor = 1.5
						culture = { has_same_culture_heritage = scope:epicenter_ruler.culture }
					}

					modifier = {
						factor = 0.4
						highest_held_title_tier >= tier_kingdom
					}

					modifier = {
						factor = 5
						any_held_title = { has_clerical_region = yes }
					}

					modifier = {
						factor = 3
						faith = { has_doctrine_parameter = has_clerical_regions }
						exists = capital_county.clerical_region_title
						scope:heresy_origin_county.clerical_region_title ?= capital_county.clerical_region_title
					}

					modifier = {
						factor = 1.5
						OR = {
							has_trait = zealous
							has_trait = lifestyle_mystic
							has_trait = lifestyle_scholar
							has_trait = theologian
						}
					}

					modifier = {
						factor = 0.001
						OR = {
							government_has_flag = government_is_nomadic
							government_has_flag = government_is_herder
						}
					}
				}

				save_scope_as = convert_ruler
			}
		}

		if = {
			limit = { exists = scope:convert_ruler }

			if = {
				limit = {
					scope:heresy_faith = { pam_heresy_faith_is_pre_declaration_trigger = yes }
					exists = scope:new_rite
				}
				scope:convert_ruler = {
					pam_heresy_convert_ruler_to_new_rite_effect = yes
				}
			}
			else = {
				scope:convert_ruler = {
					pam_heresy_convert_ruler_to_heresy_faith_effect = yes
				}
			}

			scope:c3_spread_owner = {
				if = {
					limit = {
						scope:convert_ruler = {
							highest_held_title_tier >= tier_kingdom
						}
					}
					change_variable = {
						name = c3_spread_remaining_converts
						subtract = 4
					}
				}
				else_if = {
					limit = {
						scope:convert_ruler = {
							highest_held_title_tier = tier_duchy
						}
					}
					change_variable = {
						name = c3_spread_remaining_converts
						subtract = 2
					}
				}
				else = {
					change_variable = {
						name = c3_spread_remaining_converts
						subtract = 1
					}
				}

				if = {
					limit = { var:c3_spread_remaining_converts <= 0 }
					remove_variable = c3_spread_remaining_converts
				}
			}
			if = {
				limit = {
					scope:c3_spread_owner = { has_variable = c3_spread_remaining_converts }
				}
				trigger_event = {
					id = heresy_historical.0011
					days = { 5 12 }
				}
			}

			if = {
				limit = {
					trigger_if = {
						limit = {
							scope:heresy_faith = { pam_heresy_faith_is_pre_declaration_trigger = yes }
							exists = scope:new_rite
						}
						scope:convert_ruler ?= { rite = scope:new_rite }
					}
					trigger_else = {
						scope:convert_ruler ?= { faith = scope:heresy_faith }
					}
				}
				pam_heresy_send_spread_message_effect = { TITLE = heresy_historical.spread.title }
			}
		}
		else = {
			scope:c3_spread_owner = {
				remove_variable = c3_spread_remaining_converts
			}
		}
	}
}
