﻿############################
# Regula Important Actions #
############################
# This file has the "Important actions", reminders for the player to do certain things related to the Regula Magistri mod
# Always make sure to put the logic after the limit of the if statement, otherwise the check does nothing!
# Eg, your check_create_action should look like this
# if = {
#     limit = {
#         has_trait = magister_trait_group
#         has_trait_rank = {
#             trait = magister_trait_group
#             rank >= 2
#         }
#     }
#     every_vassal = {      # Or whatever the action needs/does
#       # Some code here
#     }
# }
####
# action_regula_make_paelex - Reminds the player to use Domitans Tribunal, which turns a landed Mulsa into a Paelex (Or Domina if primary spouse)
# action_can_fascinare_vassal - Reminds player to Fascinare (Charm) any female vassals they have that have a county title (or greater)
# action_regula_take_orba - Reminds player to claim any Orba that currently exist (as their vassals)
# action_regula_potestas_non_transfunde - Reminds player that they can vassalise a foreign realm (as the ruler is charmed) using the Potestas non Transfunde interaction
# action_can_exhaurire_vitale - Reminds player they can use the Exhaurire Vitale interaction, and picks the highest piety consort as the target.
# action_can_mutare_corpus_paelex_or_domina - Reminds player that the Mutare Corpus interaction is not on cooldown, and shows a list of all Paelex/Domina targets. Does not show if target is pregnant due to below rule.
# action_can_mutare_corpus_to_bless_consort_pregnancy - Reminds player that they can use Mutare Corpus on a pregnant spouse
# action_can_charm_prisoner - Reminds player that they have a charmable prisoner (If the prisoner is already Magi this alert will not fire)
# action_can_curo_privignos - Reminds player that he has stepchildren (from his Domina/Paelex) that he can decide the fate off.
# action_child_does_not_have_charmed_guardian - Reminds player that his non-adult female family members (extended family) do not have a guardian at all, and should have a charmed guardian.
# action_child_does_not_have_charmed_guardian_remove - Reminds player that his non-adult female family members (extended family) have a non-charmed guardian, which should be removed for a charmed guardian.
# action_devoted_not_parent_of_their_primary_heir - Reminds player that a leader devoted (aka Domina/Paelex/Famili Paelex) has a heir that is not her child/grandchild
######################################################

# Use Domitans Tribunal
action_regula_make_paelex = {
    combine_into_one = yes
    priority = 1090

    check_create_action = {
        if = {
            limit = {
                has_trait = magister_trait_group
            }
            ordered_vassal_or_below = {
                order_by = primary_title.tier
                max = 100
                check_range_bounds = no
                limit = {
                    OR = {
                        has_trait = mulsa
                        has_trait = tropaeum
                    }
                    is_vassal_or_below_of = root
                    is_imprisoned = no
                    highest_held_title_tier >= tier_county

                    # Check consanguinity doctrine
                    NOT = {
                        relation_with_character_is_incestuous_in_faith_trigger = {
                            FAITH = root.faith
                            CHARACTER = root
                        }
                    }

                    # Ensure we don't Domitans our own Dynasty (if already married!)
                    NAND = {
                        is_married = yes
                        NOT = { is_consort_of = root }    # If we are married to Magister, then we should alert!
                        OR = {
                            dynasty ?= {
                                this = root.dynasty
                            }
                            primary_spouse.dynasty ?= {
                                this = root.dynasty
                            }
                        }
                    }
                }
                try_create_important_action = {
                    important_action_type = action_regula_make_paelex
                    actor = root
                    recipient = this
                }
            }
        }
    }

    effect = {
        open_interaction_window = {
            interaction = regula_make_paelex_interaction
            actor = scope:actor
            recipient = scope:recipient
        }
    }
}

# Fascinare a female vassal
action_can_fascinare_vassal = {
    combine_into_one = yes
    priority = 1100

    check_create_action = {
        if = {
            limit = {
                has_trait = magister_trait_group
            }
            ordered_vassal_or_below = {
                order_by = primary_title.tier
                max = 100
                check_range_bounds = no
                limit = {
                    is_regula_devoted_trigger = no
                    is_imprisoned = no
                    highest_held_title_tier >= tier_county
                    is_female = yes
                    age >= 16
                    root = {
                        is_character_interaction_valid = {
                            recipient = prev
                            interaction = regula_fascinare_interaction
                        }
                    }
                }
                try_create_important_action = {
                    important_action_type = action_can_fascinare_vassal
                    actor = root
                    recipient = this
                }
            }
        }
    }

    effect = {
        open_interaction_window = {
            interaction = regula_fascinare_interaction
            actor = scope:actor
            recipient = scope:recipient
        }
    }
}

# Claim an Orba
action_regula_take_orba = {
    combine_into_one = yes
    priority = 1070
    is_dangerous = yes

    check_create_action = {
        if = {
            limit = {
                has_trait = magister_trait_group
                has_trait_rank = {
                    trait = magister_trait_group
                    rank >= 2
                }
            }
            every_vassal_or_below = {
                limit = {
                    has_trait = orba
                    is_imprisoned = no
                }
                try_create_important_action = {
                    important_action_type = action_regula_take_orba
                    actor = root
                    recipient = this
                }
            }
        }
    }

    effect = {
        open_interaction_window = {
            interaction = regula_take_orba_interaction
            actor = scope:actor
            recipient = scope:recipient
        }
    }
}

# Vassalise a foreign realm
action_regula_potestas_non_transfunde = {
    combine_into_one = yes
    priority = 910


    check_create_action = {
        if = {
            limit = {
                has_trait = magister_trait_group
                has_trait_rank = {
                    trait = magister_trait_group
                    rank >= 5
                }
                is_landless_adventurer = no
            }
            every_ruler = {
                limit = {
                    OR = { # Devoted (implied secret faith) or shared faith
                        is_regula_devoted_trigger = yes
                        faith = root.faith
                        AND = {
                            secret_faith ?= root.faith
                            any_secret = {
                                OR = {
                                    secret_type = secret_crypto_religionist
                                    secret_type = regula_crypto_religionist_secret
                                }
                                is_known_by = root
                            }
                        }
                    }
                    NOT = { this = root }
                    is_landed = yes
                    is_available = yes
                    is_independent_ruler = yes
                    is_male = no # Leaving the door open for futas, etc.
                    is_adult = yes
                    highest_held_title_tier < root.highest_held_title_tier
                    NOT = { government_has_flag = can_not_be_vassal_or_liege }
                    NOT = { is_at_war_with = root }
                }
                save_temporary_scope_as = transfunde_target

                # Check if Magister (root) has the prestige for this interaction
                if = {
                    limit = {
                        root = {
                            save_temporary_scope_as = actor # regula_potestas_non_transfunde_cost expects scope:actor to be the Magister to compare primary title differences for calculations
                            prestige >= scope:transfunde_target.regula_potestas_non_transfunde_cost
                        }
                    }

                    # If vassal and above check are good, create action
                    try_create_important_action = {
                        important_action_type = action_regula_potestas_non_transfunde
                        actor = root
                        recipient = scope:transfunde_target
                    }
                }
            }
        }
    }

    effect = {
        open_interaction_window = {
            interaction = regula_potestas_non_transfunde_interaction
            actor = scope:actor
            recipient = scope:recipient
        }
    }
}

# Can use the Exhaurie Vitale interaction
action_can_exhaurire_vitale = {
    priority = 1000

    check_create_action = {
        if = {
            limit = {
                has_trait = magister_trait_group
            }
            ordered_consort = {
                order_by = piety
                max = 1

                limit = {                         
                    root = {
                        is_character_interaction_valid = {
                            recipient = prev
                            interaction = regula_exhaurire_vitale_interaction
                        }
                    } 
                }

                # If not on cooldown, create action
                try_create_important_action = {
                    important_action_type = action_can_exhaurire_vitale
                    actor = root
                    recipient = this
                }
            }
        }
    }

    effect = {
        open_interaction_window = {
            interaction = regula_exhaurire_vitale_interaction
            actor = scope:actor
            recipient = scope:recipient
        }
    }
}

# Use Mutare Corpus on one of your Paelex/Domina
# Does not show if target is pregnant to not overlap with below rule
action_can_mutare_corpus_paelex_or_domina = {
    combine_into_one = yes
    priority = 1080

    check_create_action = {
        if = {
            limit = {
                has_trait = magister_trait_group
                piety >= regula_mutare_corpus_interaction_piety_cost
            }
            every_consort = {
                # Check if consort is a Paelex or Domina
                # And not pregnant
                limit = {
                    is_regula_leader_devoted_trigger = yes
                    is_pregnant = no

                    root = {
                        OR = {
                            is_character_interaction_valid = {
                                recipient = prev
                                interaction = regula_mutare_corpus_interaction
                            }
                            is_character_interaction_valid = {
                                recipient = prev
                                interaction = regula_mutare_corpus_no_cooldown_interaction
                            }
                        }
                    }
                }
                save_temporary_scope_as = mutare_target

                # If not on cooldown and we have appropriate consort, create action
                try_create_important_action = {
                    important_action_type = action_can_mutare_corpus_paelex_or_domina
                    actor = root
                    recipient = this
                }
            }
        }
    }

    effect = {
        if = {
            limit = {
                scope:actor = {
                    is_character_interaction_valid = {
                        recipient = scope:recipient
                        interaction = regula_mutare_corpus_interaction
                    }
                }
            }
            open_interaction_window = {
                interaction = regula_mutare_corpus_interaction
                actor = scope:actor
                recipient = scope:recipient
            }
        }
        else_if = {
            limit = {
                scope:actor = {
                    is_character_interaction_valid = {
                        recipient = scope:recipient
                        interaction = regula_mutare_corpus_no_cooldown_interaction
                    }
                }
            }
            open_interaction_window = {
                interaction = regula_mutare_corpus_no_cooldown_interaction
                actor = scope:actor
                recipient = scope:recipient
            }
        }
    }
}

# Can "Bless" the pregnancy of a pregnant consort using Mutare Corpus
action_can_mutare_corpus_to_bless_consort_pregnancy = {
    combine_into_one = yes
    priority = 1090

    check_create_action = {
        if = {
            limit = {
                has_trait = magister_trait_group
                piety >= regula_mutare_corpus_interaction_piety_cost
            }
            ordered_consort = {
                order_by = pregnancy_month
                max = 100
                check_range_bounds = no
                # Check if consort is pregnant and does not already have a blessed pregnacy
                limit = {
                    is_pregnant = yes
                    NOT = { has_trait = regula_blessed_pregnancy }
                    has_trait = pregnant # So that we show this alert when target "shows" pregnancy

                    root = {
                        OR = {
                            is_character_interaction_valid = {
                                recipient = prev
                                interaction = regula_mutare_corpus_interaction
                            }
                            is_character_interaction_valid = {
                                recipient = prev
                                interaction = regula_mutare_corpus_no_cooldown_interaction
                            }
                        }
                    }
                }

                try_create_important_action = {
                    important_action_type = action_can_mutare_corpus_to_bless_consort_pregnancy
                    actor = root
                    recipient = this
                }
            }
        }
    }

    effect = {
        if = {
            limit = {
                scope:actor = {
                    is_character_interaction_valid = {
                        recipient = scope:recipient
                        interaction = regula_mutare_corpus_interaction
                    }
                }
            }
            open_interaction_window = {
                interaction = regula_mutare_corpus_interaction
                actor = scope:actor
                recipient = scope:recipient
            }
        }
        else_if = {
            limit = {
                scope:actor = {
                    is_character_interaction_valid = {
                        recipient = scope:recipient
                        interaction = regula_mutare_corpus_no_cooldown_interaction
                    }
                }
            }
            open_interaction_window = {
                interaction = regula_mutare_corpus_no_cooldown_interaction
                actor = scope:actor
                recipient = scope:recipient
            }
        }
    }
}

# Non-Magi prisoner can be charmed
action_can_charm_prisoner = {
    combine_into_one = yes

    priority = 1090

    check_create_action = {
        if = {
            limit = {
                has_trait = magister_trait_group
            }
            every_prisoner = {
                limit = {
                    is_regula_devoted_trigger = no
                    is_female = yes
                    age >= 16
                }
                try_create_important_action = {
                    important_action_type = action_can_charm_prisoner
                    actor = root
                    recipient = this
                }
            }
        }
    }

    effect = {
        open_interaction_window = {
            interaction = regula_prisoner_interaction
            actor = scope:actor
            recipient = scope:recipient
        }
    }
}

# Decide the fate of a stepchild
action_can_curo_privignos = {
    combine_into_one = yes
    priority = 350


    check_create_action = {
        if = {
            limit = {
                has_trait = magister_trait_group
            }
            every_consort = {
                limit = {
                    is_regula_devoted_trigger = yes
                }
                every_child = {
                    limit = {
                        character_is_valid_curo_privignos_target_trigger = {
                            ACTOR = root
                        }
                        character_is_valid_curo_privignos_target_show_failures_trigger = {
                            ACTOR = root
                        }
                    }
                    try_create_important_action = {
                        important_action_type = action_can_curo_privignos
                        actor = root
                        recipient = this
                    }
                }
            }
        }
    }

    effect = {
        open_interaction_window = {
            interaction = regula_curo_privignos_interaction
            actor = scope:actor
            recipient = scope:recipient
        }
    }
}

# Ensure female family children have charmed guardians
# Ignore this if the child has the obedience bloodline trait
action_child_does_not_have_charmed_guardian = {
	priority = 360
	combine_into_one = yes

	check_create_action = {
        if = {
            limit = {
                has_trait = magister_trait_group
            }
            every_close_or_extended_family_member = {
                limit = {
                    is_adult = no
                    is_female = yes
                    age >= childhood_education_start_age
                    exists = liege
                    liege = root
                    num_of_relation_guardian = 0
                    NOT = { has_trait = regula_obedience_bloodline } # Obedient children are always charmed, regardless of warden
                }

                # If they dont have a guardian at all
                try_create_important_action = {
                    important_action_type = action_child_does_not_have_charmed_guardian
                    actor = root
                    recipient = this
                    secondary_recipient = this
                }
            }
        }
	}

	effect = {
        # Give them a guardian
        open_interaction_window = {
            interaction = educate_child_interaction
            actor = scope:actor
            recipient = scope:actor
            secondary_recipient = scope:secondary_recipient
        }
	}
}

# Remove an existing non-charmed guardian for our female family children
# Ignore this if the child has the obedience bloodline trait
action_child_does_not_have_charmed_guardian_remove = {
	priority = 359
	combine_into_one = yes

	check_create_action = {
        if = {
            limit = {
                has_trait = magister_trait_group
            }
            every_close_or_extended_family_member = {
                limit = {
                    is_adult = no
                    is_female = yes
                    age >= childhood_education_start_age
                    exists = liege
                    liege = root
                    num_of_relation_guardian > 0
                    NOT = { has_trait = regula_obedience_bloodline } # Obedient children are always charmed, regardless of warden
                }

                # If they have a guardian who is not devoted (or the magister)
                every_relation = {
                    type = guardian
                    limit = {
                        AND = {
                            NOT = { has_trait = devoted_trait_group }
                            NOT = { has_trait = magister_trait_group }
                        }
                    }
                    try_create_important_action = {
                        important_action_type = action_child_does_not_have_charmed_guardian_remove
                        actor = root
                        recipient = prev
                        secondary_recipient = prev
                    }
                }
            }
        }
	}

	effect = {
        # They already have a guardian we need to remove
        open_interaction_window = {
            interaction = remove_guardian_interaction
            actor = scope:actor
            recipient = scope:secondary_recipient
        }
	}
}

# A Devoted (leader) has a primary heir that is not their child / grandchild
# We don't worry about the dynasty of the primary heir (aka if its the Magisters child), just that its the child of the Paelex
# This does not apply if Government is Administrative
action_devoted_not_parent_of_their_primary_heir = {
	is_dangerous = yes
	combine_into_one = yes
	
	check_create_action = {

        if = {
            limit = {
                # Does not apply to Admin Government
                NOT = { government_has_flag = government_is_administrative }
            }
            ordered_consort = {
                limit = {
                    is_regula_leader_devoted_trigger = yes
                    primary_heir ?= {
                        NOT = {
                            OR = {
                                is_child_of = prev
                                is_grandchild_of = prev
                            }
                        }
                    }
                    is_pregnant = no   # If they are pregnant then we can supress this alert
                    highest_held_title_tier >= tier_county
                }
                order_by = age
                max = 100
                check_range_bounds = no
                try_create_important_action = {
                    important_action_type = action_devoted_not_parent_of_their_primary_heir
                    actor = root
                    recipient = this
                }
            }
        }
	}

	effect = {
		scope:recipient = {
			open_view_data = {
				view = character
			}
		}
	}
}