﻿# Our AI Oatholder sends us, the Oathbound, to county with low control
oathholder_send_increase_control_decision_all_counties_ai = { 
    picture = {
		reference = "gfx/interface/illustrations/decisions/decision_misc.dds"
	}
	desc = oathholder_send_increase_control_decision_ai_desc
	selection_tooltip = oathholder_send_increase_control_decision_ai_tooltip

    cooldown = { months = 4 }

	is_shown = {
        NOT = { has_character_flag = oathholder_travel_job_set }
        any_relation = {
            type = oathbound
            this = {
                NOT = { has_character_flag = oathbound_on_travel_job }
                is_ai = no
            }
        }

        is_ai = yes
        is_ruler = yes
        is_landed = yes
        is_landless_adventurer = no

		num_of_relation_oathbound > 0

        any_sub_realm_county = {
            county_control <= 85
            is_titular = no
            NOT = { holder = { is_at_war_with = root } }
            NOT = { has_province_with_epidemic = { intensity <= apocalyptic } }
            NOT = {
                OR = {
                    has_county_modifier = oathbound_enforcing_order_strong
                    has_county_modifier = oathbound_enforcing_order_normal
                    has_county_modifier = oathbound_enforcing_order_weak
                }
            }
        }
	}

	is_valid = {
		NOT = { 
			is_imprisoned = yes
			has_trait = incapable 
            is_at_war = yes
            is_travelling = yes
		}
        any_relation = {
            type = oathbound
            this = {
                NOT = {
                    is_imprisoned = yes
                    has_trait = incapable 
                    is_at_war = yes
                    is_travelling = yes
                }
            }
        }
	}

    ai_check_interval = 4
    ai_potential = { num_of_relation_oathbound > 0 }
    ai_will_do = {
        base = 25

        # 1. THE CRISIS MODIFIERS: How bad is the county?
        modifier = {
            add = 50
            any_sub_realm_county = { 
                county_control < 50 
                is_titular = no
                NOT = { has_province_with_epidemic = { intensity <= apocalyptic } }
            }
        }
        modifier = {
            add = 100
            any_sub_realm_county = { 
                county_control < 25
                is_titular = no
                NOT = { has_province_with_epidemic = { intensity <= apocalyptic } }
            }
        }

        # 2. THE ECONOMIC DESPERATION: Broke rulers need taxes now
        modifier = {
            factor = 2.0
            short_term_gold < 0
        }

        # 3. THE TYRANTS: Iron-fisted rulers do not tolerate defiance
        modifier = {
            factor = 2.0
            OR = {
                has_trait = wrathful
                has_trait = callous
                has_trait = arrogant
                has_trait = arbitrary
                has_trait = sadistic
            }
        }

        # 4. THE ADMINISTRATORS: Perfectionists want an orderly realm
        modifier = {
            factor = 1.5
            OR = {
                has_trait = diligent
                has_trait = just
                has_trait = ambitious
            }
        }

        # 5. THE APATHETIC: Weak or soft rulers hesitate to send the hounds
        modifier = {
            factor = 0.25 # Drastically reduces the chance they click it
            OR = {
                has_trait = lazy
                has_trait = craven
                has_trait = forgiving
                has_trait = compassionate
            }
        }
    }

	effect = {
        random_sub_realm_county = {
            limit = {
                is_titular = no
                #exists = title_province
                county_control <= 85
                NOT = { holder = { is_at_war_with = root } }
                NOT = { has_province_with_epidemic = { intensity <= apocalyptic } }
                NOT = {
                    has_county_modifier = oathbound_enforcing_order_strong
                    has_county_modifier = oathbound_enforcing_order_normal
                    has_county_modifier = oathbound_enforcing_order_weak
                }
            }
            
            # The Lottery System
            weight = {
                base = 50

                modifier = {
                    add = 25 
                    county_control < 75 
                }
                modifier = {
                    add = 50 
                    county_control < 50 
                }
                modifier = { 
                    add = 100 
                    county_control < 25 
                }

                modifier = { 
                    factor = 5
                    this = root.capital_county
                }
                modifier = {
                    factor = 3
                    holder = root
                }

            }

            save_scope_as = chosen_target_county
        }



        add_character_flag = {
            flag = oathholder_travel_job_set
            years = 200
        }
        var:val_oathkeeper = {
            add_character_flag = {
                flag = oathbound_on_travel_job
                years = 200
            }
            add_character_flag = {
                flag = oathbound_control_job
                years = 200
            }
            set_variable = {
                name = oathbound_control_task_county_var
                value = scope:chosen_target_county
            }
            trigger_event = oathbound_task_events.0001
        }
    }
}


oathbound_task_increasing_control = { # We use the decision as an interface tracker. Also makes it easy to travel there if making own way.
    picture = {
		reference = "gfx/interface/illustrations/decisions/decision_misc.dds"
	}
	desc = oathbound_task_increasing_control_desc
	selection_tooltip = oathbound_task_increasing_control_tooltip
    decision_group_type = major

	is_shown = {
        has_character_flag = oathbound_control_job 
        is_ai = no

        is_landless_adventurer = yes

		num_of_relation_oathholder > 0
	}

	is_valid = {
	}

	ai_check_interval = 0

	effect = {
        # Two separate tooltips = Two separate bullet points!
        custom_tooltip = oathbound_task_increasing_control_effect_tt_1
        custom_tooltip = oathbound_task_increasing_control_effect_tt_2
        
        hidden_effect = {
            
            if = {
                limit = {
                    OR = {
                        # Condition 1: We are simply not in the right county
                        NOT = { location.county = var:oathbound_control_task_county_var }

                        # Condition 2: The target county itself lacks the job modifiers
                        # (We step inside the county variable to check its modifiers properly!)
                        var:oathbound_control_task_county_var = {
                            NOT = {
                                OR = {
                                    has_county_modifier = oathbound_enforcing_order_weak
                                    has_county_modifier = oathbound_enforcing_order_normal
                                    has_county_modifier = oathbound_enforcing_order_strong
                                }
                            }
                        }
                    }
                }
                
                domicile = { change_provisions = max_provisions }
                root = { 
                    add_character_modifier = {
                        modifier = laamp_provisions_forgiveness_travel_modifier 
                        months = 6
                    }
                }
                start_travel_plan = {
                    destination = var:oathbound_control_task_county_var.title_province
                    travel_with_domicile = yes
                    return_trip = no
                    on_arrival_event = oathbound_task_events.0002
                    on_arrival_destinations = last
                }  
            }
        }
    }
}







oathholder_send_to_duel_rival_ai = {
    picture = {
        reference = "gfx/interface/illustrations/decisions/decision_misc.dds"
    }
    desc = oathholder_send_to_duel_rival_ai_desc
    selection_tooltip = oathholder_send_to_duel_rival_ai_tooltip

    cooldown = { months = 24 }

    is_shown = {
        has_character_flag = oathholder_prefers_duel
        #NOT = { has_character_flag = oathholder_travel_job_set } <- should only set this when task requires moving domicile.
        any_relation = {
            type = oathbound
            this = {
                NOT = { has_character_flag = oathbound_duel_job }
                is_ai = no
                is_adult = yes
                is_travelling = no
            }
        }

        is_ai = yes
        is_ruler = yes
        is_landed = yes
        is_landless_adventurer = no

        num_of_relation_oathbound > 0

        OR = {
            any_relation = {
                type = rival
                this = {
                    is_alive = yes
                    is_imprisoned = no
                    prowess < root.var:val_oathkeeper.prowess
                }
            }
            any_relation = {
                type = nemesis
                this = {
                    is_alive = yes
                    is_imprisoned = no
                    prowess < root.var:val_oathkeeper.prowess
                }
            }
        }
    }

    is_valid = {
        NOT = {
            is_imprisoned = yes
            has_trait = incapable
        }
        any_relation = {
            type = oathbound
            this = {
                NOT = {
                    is_imprisoned = yes
                    has_trait = incapable
                }
            }
        }
        OR = {
            any_relation = {
                type = rival
                this = {
                    is_alive = yes
                    is_imprisoned = no
                }
            }
            any_relation = {
                type = nemesis
                this = {
                    is_alive = yes
                    is_imprisoned = no
                }
            }
        }
    }

    ai_check_interval = 3
    ai_potential = { num_of_relation_oathbound > 0 }
    ai_will_do = {
        base = -100 # War alone is not enough, there must be a decent prowess diff.

        # 1. THE RIVAL IS AT WAR: An active military threat demands an urgent reckoning
        modifier = {
            add = 75
            any_relation = {
                type = rival
                this = { is_at_war_with = root }
            }
        }
        modifier = {
            add = 75
            any_relation = {
                type = nemesis
                this = { is_at_war_with = root }
            }
        }

        # 2. How much better are we?
        modifier = {
            add = 200
            any_relation = {
                type = rival
                this = { 
                    prowess_diff = {
                        target = root.var:val_oathkeeper
                        value <= -10
                    }
                }
            }
        }
        modifier = {
            add = 200
            any_relation = {
                type = nemesis
                this = { 
                    prowess_diff = {
                        target = root.var:val_oathkeeper
                        value <= -10
                    }
                }
            }
        }

        modifier = {
            add = 50
            any_relation = {
                type = rival
                this = { 
                    prowess_diff = {
                        target = root.var:val_oathkeeper
                        value <= -5
                    }
                }
            }
        }
        modifier = {
            add = 50
            any_relation = {
                type = nemesis
                this = { 
                    prowess_diff = {
                        target = root.var:val_oathkeeper
                        value <= -5
                    }
                }
            }
        }




        # 8. Already dispatched them to murder the same target — do not double-assign
        modifier = {
            factor = 0
            has_character_flag = oathholder_kill_job_set
        }
    }

    effect = {
        # 1. Choose the target pool using random_list
        # If one pool is empty or invalid, the factor = 0 will ensure it's not chosen.
        random_list = {
            # Pool A: Nemeses (Weighted higher because they are more important targets)
            100 = {
                modifier = {
                    factor = 0
                    NOT = {
                        any_relation = {
                            type = nemesis
                            this = {
                                is_alive = yes
                                is_imprisoned = no
                                prowess < root.var:val_oathkeeper.prowess
                            }
                        }
                    }
                }
                random_relation = {
                    type = nemesis
                    limit = {
                        is_alive = yes
                        is_imprisoned = no
                        prowess < root.var:val_oathkeeper.prowess
                    }
                    weight = {
                        base = 50
                        modifier = { add = 100 is_at_war_with = root }
                        modifier = {
                            add = 200
                            prowess_diff = {
                                target = root.var:val_oathkeeper
                                value <= -10
                            }
                        }
                        modifier = {
                            add = 50
                            prowess_diff = {
                                target = root.var:val_oathkeeper
                                value <= -5
                            }
                        }
                    }
                    save_scope_as = chosen_target_duel
                }
            }

            # Pool B: Rivals (Weighted lower, but still a valid choice)
            50 = {
                modifier = {
                    factor = 0
                    NOT = {
                        any_relation = {
                            type = rival
                            this = {
                                is_alive = yes
                                is_imprisoned = no
                                prowess < root.var:val_oathkeeper.prowess
                            }
                        }
                    }
                }
                random_relation = {
                    type = rival
                    limit = {
                        is_alive = yes
                        is_imprisoned = no
                        prowess < root.var:val_oathkeeper.prowess
                    }
                    weight = {
                        base = 50
                        modifier = { add = 100 is_at_war_with = root }
                        modifier = {
                            add = 200
                            prowess_diff = {
                                target = root.var:val_oathkeeper
                                value <= -10
                            }
                        }
                        modifier = {
                            add = 50
                            prowess_diff = {
                                target = root.var:val_oathkeeper
                                value <= -5
                            }
                        }
                    }
                    save_scope_as = chosen_target_duel
                }
            }
        }

        # 2. Set the Oathholder's flags
        add_character_flag = {
            flag = oathholder_travel_job_set
            years = 200
        }
        add_character_flag = {
            flag = oathholder_duel_job_set
            years = 200
        }

        # 3. Apply the job to the Oathbound (The Player)
        var:val_oathkeeper = {
            add_character_flag = {
                flag = oathbound_on_travel_job
                years = 200
            }
            add_character_flag = {
                flag = oathbound_duel_job
                years = 200
            }
            set_variable = {
                name = oathbound_duel_task_target_var
                value = scope:chosen_target_duel
            }
            trigger_event = oathbound_task_events.0004
        }
    }
}


# We (the player) use the decision as an interface tracker. Also makes it easy to travel there if making own way.
oathbound_task_duel_rival = {
    picture = {
        reference = "gfx/interface/illustrations/decisions/decision_misc.dds"
    }
    desc = oathbound_task_duel_rival_desc
    selection_tooltip = oathbound_task_duel_rival_tooltip
    decision_group_type = major

    is_shown = {
        has_character_flag = oathbound_duel_job
        is_ai = no

        is_landless_adventurer = yes

        num_of_relation_oathholder > 0
    }

    is_valid = {
    }

    ai_check_interval = 0

    effect = {
        custom_tooltip = oathbound_task_duel_rival_effect_tt_1
        custom_tooltip = oathbound_task_duel_rival_effect_tt_2

        hidden_effect = {
            domicile = { change_provisions = max_provisions }

            if = {
                limit = {
                    NOT = { location = var:oathbound_duel_task_target_var.location }
                }

                start_travel_plan = {
                    destination = var:oathbound_duel_task_target_var.location
                    travel_with_domicile = no
                    return_trip = yes
                    on_arrival_event = oathbound_task_events.0005
                    on_arrival_destinations = all_but_last
                }
            }
        }
    }
}


