﻿acs_sv_claim_value = {
    value = 0
    if = {
        limit = { tier < tier_duchy }
        add = 1
    }
    if = {
        limit = { tier = tier_duchy }
        add = 6
    }
    if = {
        limit = { tier = tier_kingdom }
        add = 216
    }
    if = {
        limit = { tier = tier_empire }
        add = 1296
    }
}

acs_sv_total_claim_value = {
    value = 0
    every_claim = {
        add = acs_sv_claim_value
    }
}

total_claim_county_value = {
    value = 0
    every_claim = {
        if = {
            limit = { tier < tier_duchy }
            add = 1
        }
    }
}

total_claim_duchy_value = {
    value = 0
    every_claim = {
        if = {
            limit = { tier = tier_duchy }
            add = 1
        }
    }
}

total_claim_kingdom_value = {
    value = 0
    every_claim = {
        if = {
            limit = { tier = tier_kingdom }
            add = 1
        }
    }
}

total_claim_empire_value = {
    value = 0
    every_claim = {
        if = {
            limit = { tier = tier_empire }
            add = 1
        }
    }
}

total_heritage_barony_value = {
    value = 0
    every_pretender_title = {
        if = {
            limit = { tier < tier_county }
            add = 1
        }
    }
    every_heir_title = {
        if = {
            limit = { tier < tier_county }
            add = 1
        }
    }
}

total_heritage_county_value = {
    value = 0
    every_pretender_title = {
        if = {
            limit = { tier = tier_county }
            add = 1
        }
    }
    every_heir_title = {
        if = {
            limit = { tier = tier_county }
            add = 1
        }
    }
}

total_heritage_duchy_value = {
    value = 0
    every_pretender_title = {
        if = {
            limit = { tier = tier_duchy }
            add = 1
        }
    }
    every_heir_title = {
        if = {
            limit = { tier = tier_duchy }
            add = 1
        }
    }
}

total_heritage_kingdom_value = {
    value = 0
    every_pretender_title = {
        if = {
            limit = { tier = tier_kingdom }
            add = 1
        }
    }
    every_heir_title = {
        if = {
            limit = { tier = tier_kingdom }
            add = 1
        }
    }
}

total_heritage_empire_value = {
    value = 0
    every_pretender_title = {
        if = {
            limit = { tier = tier_empire }
            add = 1
        }
    }
    every_heir_title = {
        if = {
            limit = { tier = tier_empire }
            add = 1
        }
    }
}

acs_sv_total_claim_value_for_sort = {
    value = 0
    every_claim = {
        explicit = yes
        pressed = yes
        add = acs_sv_claim_value
    }
    every_claim = {
        explicit = yes
        pressed = no
        add = acs_sv_claim_value
        add = -3
    }
    every_claim = {
        explicit = no
        add = acs_sv_claim_value
        add = -4
    }
}
age_sort_function = {
    value = 1
    if = {
        limit = { age <= 45 }
        add = {
            value = -0.31
            multiply = age
            multiply = age
        }
        add = {
            value = 15.24
            multiply = age
        }
        add = 1
        multiply = 0.01
    }
    if = {
        limit = { age > 45 }
        add = {
            value = 0.04
            multiply = age
            multiply = age
        }
        add = {
            value = -12.94
            multiply = age
        }
        add = 996.93
        
        multiply = 0.001
    }
}
health_sort_function = {
    add = {
        value = 0.09
        multiply = health
        multiply = health
        multiply = health
        multiply = health
    }
    add = {
        value = -1.2
        multiply = health
        multiply = health
        multiply = health
    }
    add = {
        value = 7.17
        multiply = health
        multiply = health
    }
    add = {
        value = 4.1
        multiply = health
    }
    add = -0.16
}
magic_souce = {
    value = diplomacy
    multiply = 1.5
    add = {
        value = martial
    }
    add = {
        value = stewardship
        multiply = 3
    }
    add = {
        value = intrigue
        multiply = 0.75
    }
    add = {
        value = learning
        multiply = 1.3
    }
    add = {
        value = prowess
        multiply = 0.9
    }
    add = {
        value = num_of_good_genetic_traits
        multiply = 3
    }
    add = {
        value = num_of_bad_genetic_traits
        multiply = -3
    }
    add = num_virtuous_traits
    subtract = num_sinful_traits
    multiply = age_sort_function
    multiply = health_sort_function
}

acs_sort_value = {
    value = 0
    if = {
        limit = { global_var:acs_sort_by < 10 }
        if = {
            limit = { global_var:acs_sort_by < 5 }
            if = {
                limit = { global_var:acs_sort_by = 0 }
                add = health
            }
            else_if = {
                limit = { global_var:acs_sort_by = 1 }
                if = {
                    limit = { is_ruler = yes }
                    add = primary_title.tier
                }
                else =    {
                    if = {
                        limit = { is_lowborn = yes }
                        add = -1
                    }
                }    
            }
            else_if = {
                limit = { global_var:acs_sort_by = 2 }
                add = age
            }
            else_if = {
                limit = { global_var:acs_sort_by = 3 }
        
                if = {
                    limit = {save_temporary_opinion_value_as  = {name = temp_op target = scope:acs_sort_root}}
                    add = scope:temp_op
                }
            }
            else = {
                #limit = { global_var:acs_sort_by = 4 }
                add = diplomacy
            }
        }
        else = {
            if = {
                limit = { global_var:acs_sort_by = 5 }
                add = martial
            }
            else_if = {
                limit = { global_var:acs_sort_by = 6 }
                add = stewardship
            }
            else_if = {
                limit = { global_var:acs_sort_by = 7 }
                add = intrigue
            }
            else_if = {
                limit = { global_var:acs_sort_by = 8 }
                add = learning
            }
            else = {
                #limit = { global_var:acs_sort_by = 9 }
                add = prowess
            }
        }    
    }
    else_if = {
        limit = { 
            global_var:acs_sort_by >= 10
            global_var:acs_sort_by < 20
        }
        if = {
            limit = { global_var:acs_sort_by < 15 }
            if = {
                limit = { global_var:acs_sort_by = 10 }
                add = diplomacy
                add = martial
                add = stewardship
                add = intrigue
                add = learning
                add = prowess
            }
            else_if = {
                limit = { global_var:acs_sort_by = 11 }
                add = magic_souce
            }
            else_if = {
                limit = { global_var:acs_sort_by = 12 }
                add = gold
            }
            else_if = {
                limit = { global_var:acs_sort_by = 13 }
                add = prestige
            }
            else = {
                #limit = { global_var:acs_sort_by = 14 }
                add = piety
            }
        }
        else = {
            if = {
                limit = { global_var:acs_sort_by = 15 }
                add = max_military_strength
            }
            else_if = {
                limit = { global_var:acs_sort_by = 16 }
                add = dread
            }
            else_if = {
                limit = { global_var:acs_sort_by = 17 }
                every_child = {
                    if = {
                        limit = {is_alive = yes }
                        add = 1
                    }
                    every_child = {
                        if = {
                            limit = {is_alive = yes }
                            add = 1
                        }
                        every_child = {
                            if = {
                                limit = {is_alive = yes }
                                add = 1
                            }
                            every_child = {
                                if = {
                                    limit = {is_alive = yes }
                                    add = 1
                                }
                                every_child = {
                                    if = {
                                        limit = {is_alive = yes }
                                        add = 1
                                    }
                                }
                            }
                        }
                    }
                }
            }
            else_if = {
                limit = { global_var:acs_sort_by = 18 }
                add = acs_sv_total_claim_value_for_sort
            }
            else = {
                #limit = { global_var:acs_sort_by = 19 }
                add = acs_invite_to_court_for_sort
            }
        }    
    }
    else = {
        if = {
            limit = { global_var:acs_sort_by < 25 }
            if = {
                limit = { global_var:acs_sort_by = 20 }
                add = monthly_character_balance
            }
            else_if = {
                limit = { global_var:acs_sort_by = 21 }
                add = monthly_character_income
            }
            else_if = {
                limit = { global_var:acs_sort_by = 22 }
                add = monthly_character_expenses
            }
        }
        
    }

    if = {
        limit = { has_global_variable = acs_sort_by_ascending }
        multiply = -1
    }
}

acs_total_number_of_characters = {
    value = 0
    every_living_character = {
        add = 1
    }
}

acs_select_count = {
    value = global_var:acs_select_count
}

acs_num_of_bad_genetic_traits_modified = {
    value = num_of_bad_genetic_traits
    if = {
        limit = {
            has_trait = depressed_genetic
        }
        add = -1
    }
    if = {
        limit = {
            has_trait = lunatic_genetic
        }
        add = -1
    }
    if = {
        limit = {
            has_trait = possessed_genetic
        }
        add = -1
    }
    if = {
        limit = {
            has_trait = infertile
        }
        add = -1
    }
}

acs_modulo_2 = {
    value = scope:to_get_modulo
    modulo = 2
}

acs_position_value_offset = {
    value = scope:position
    add = scope:ctrl_value
    add = scope:offset
}

acs_position_offset = {
    value = scope:position
    add = scope:offset
}

acs_position_total_count = {
    value =  scope:position
    add = scope:total_element_count
}


acs_distance = {
    value = var:acs_distance 
    subtract = local_var:distance
}

acs_invite_to_court_for_sort = {
    value = acs_invite_to_court
    if = {
        limit = {
            OR = {
                is_courtier_of = scope:acs_invite_to_court
                is_foreign_court_guest = yes
                AND = {
                    exists = host
                    host = scope:acs_invite_to_court
                }
            }            
        }
        add = -5000
    }
    if = {
        limit = {
            is_ruler = yes    
        }
        add = -10000
    }
}


acs_invite_to_court = {
    value = -50
    #Opinions
        #Opinions of their Liege, if this is good, you get a negative modifier to invite to your court; why would they leave?
        if = {
            limit = {
                exists = liege
                is_courtier_of = liege
                save_temporary_opinion_value_as  = { name = temp_op target = this.liege }
            }
            add = {
                value = scope:temp_op
                multiply = -0.1 # Range of Opinions -100 to 100, modifier range -50 to 25, and if they like their leige, you get a negative modifier
                floor = yes
                multiply = 5
                max = 25
                min = -50
            }
        }
        #Opinion of you, if this is good, you get a positive modifier to invite to your court, you are able to convince them to leave
        if = {
            limit = {
                save_temporary_opinion_value_as  = { name = temp_op target = scope:acs_invite_to_court }
            }
            add = {
                value = scope:temp_op
                multiply = 0.1 # Range of Opinions -100 to 100, modifier range -25 to 25, and if better, you get a positive modifier
                floor = yes
                multiply = 5
                max = 25
                min = -25
            }
        }
    #Current roles
    if = {
        limit = {
            is_knight = yes
        }
        add = -20
    }  
    if = {
        limit = {
            is_diarch = yes
        }
        add = -500
    }  
    if = {
        limit = {
            is_commanding_army = yes
        }
        add = -30
    } 
    if = {
        limit = {
            is_councillor = yes
        }
        add = -40
    } 
    if = {
        limit = {
            has_court_position = court_physician_court_position
        }
        add = -20
    }

    #Relations & relatives (with actor)
    if = {
        limit = {
            has_relation_lover = scope:acs_invite_to_court
        }
        add = 100
    } 
    if = {
        limit = {
            has_relation_friend = scope:acs_invite_to_court
        }
        add = 75
    } 
    if = {
        limit = {
            is_child_of = scope:acs_invite_to_court
        }
        add = 160
    } 
    if = {
        limit = {
            this.dynasty.dynast = scope:acs_invite_to_court
        }
        add = 30
    } 
    if = {
        limit = {
            this.house.house_head = scope:acs_invite_to_court
        }
        add = 30
    } 
    if = {
        limit = {
            this.top_liege = scope:acs_invite_to_court
        }
 		add = 60
    }
    if = {
        limit = {
            NOT = { is_child_of = scope:acs_invite_to_court }
             is_close_family_of = scope:acs_invite_to_court
        }
        add = 75
    } 
    if = {
        limit = {
            is_spouse_of = scope:acs_invite_to_court
        }
        add = 160
    } 

    #Relations & relatives (with host)
    if = {
        limit = {
            exists = liege
        }
        if = {
            limit = {
                 has_relation_lover = liege
            }
            add = -100
        } 
        if = {
            limit = {
                 has_relation_friend = liege
            }
            add = -75
        } 
        if = {
            limit = {
                is_child_of = liege
            }
            add = -100
        } 
        if = {
            limit = {
                NOT = { is_child_of = liege }
                 is_close_family_of = liege
            }
            add = -75
        } 
        if = {
            limit = {
                liege.primary_title ?= { is_mercenary_company = yes }
            }
            add = 500
        }
    }

    #Family (at location)
    if = {
        limit = { exists = location }
        if = {
            limit = {
                any_consort_not_in_traveling_family_trigger = yes
            }
            add = -100
        } 
        if = {
            limit = {
                any_child_not_in_traveling_family_trigger = yes
            }
            add = -100
        } 
    }
    if = {
        limit = {
            is_married = yes
            is_male = yes
            any_spouse = {
                matrilinear_marriage = yes
                host = prev.host
            }
        }
        add = -50
    }
    if = {
        limit = {
            is_married = yes
             is_female = yes
             any_spouse = {
                 patrilinear_marriage = yes
                 host = prev.host
             }
        }
        add = -50
    }

    if = {
        limit = { exists = inspiration }
        if = {
            limit = {
                inspiration = {
                    NOT = { exists = inspiration_sponsor }
                }
            }
            add = -50
        }
        else = {
            add = -500
        }
    }

    if = {
        limit = {
            is_child_of = scope:acs_invite_to_court
             has_character_modifier = lust_for_adventure
        }
        add = -500
    }

    #Wandering characters (who aren't doing anything else)
    if = {
        limit = {
            is_pool_guest = no
            exists = location
            location = {
                exists = province_owner
                province_owner = {
                    OR = {
                        any_liege_or_above = { this = scope:acs_invite_to_court }
                        this = scope:acs_invite_to_court
                    }
                }
            }
            NOR = {
                has_relation_rival = scope:acs_invite_to_court
                exists = liege
                is_child_of = scope:acs_invite_to_court
                has_character_modifier = lust_for_adventure
            }
        }
        add = {
            value = 10
            add = scope:acs_invite_to_court.diplomacy

            if = {
                limit = {
                    faith = scope:acs_invite_to_court.faith
                }
                add = 10
            }
            else_if = {
                limit = {
                    faith = { #Same religion - But faith should not be considered Hostile or Evil
                        religion = scope:acs_invite_to_court.faith.religion
                        faith_hostility_level = {
                            target = scope:acs_invite_to_court.faith
                            value < 2
                        }
                    }
                }
                add = 5
            }
            if = {
                limit = {
                    culture = scope:acs_invite_to_court.culture
                }
                add = 10
            }
            else_if = {
                limit = {
                    culture = {
                        has_same_culture_heritage = scope:acs_invite_to_court.culture
                    }
                }
                add = 5
            }
            if = {
                limit = {
                    OR = {
                        has_trait = content
                        has_trait = lazy
                        has_trait = trusting
                    }
                }
                add = 10
            }
        }
    }
    #Adding in modifiers for paying for travel expenses.
        add = {
            value = 20
            if = {
                limit = {
                    has_trait = greedy
                }
                multiply = 1.5
            }
            else_if = {
                limit = {
                    has_trait = generous
                }
                multiply = 0.5
            }
        }   
    # Amenities impact
    ## Actor's amenities increases acceptance
    if = {
        limit = {
            scope:acs_invite_to_court = {
                has_royal_court = yes
                amenity_level = { type = court_lodging_standards value >=  medium_amenity_level }
            }
        }
        add = {
            value = 10
            if = {
                limit = {
                    scope:acs_invite_to_court = {
                        has_royal_court = yes
                        amenity_level = { type = court_lodging_standards value >=  high_amenity_level }
                    }
                }
                add = 10
            }
            if = {
                limit = {
                    scope:acs_invite_to_court = {
                        amenity_level = { type = court_lodging_standards value >=  very_high_amenity_level }
                    }
                }
                add = 10
            }
            if = {
                limit = {
                    scope:acs_invite_to_court = {
                        amenity_level = { type = court_lodging_standards value >=  max_amenity_level }
                    }
                }
                add = 20
            }
        }
    }
    ## Target's liege amenities decreases acceptance
    if = {
        limit = {
            exists = liege
            is_courtier_of = liege
            liege = {
                has_royal_court = yes
                amenity_level = { type = court_lodging_standards value >=  medium_amenity_level }
            }
        }
        add = {
            value = -10
            if = {
                limit = {
                    liege = {
                        amenity_level = { type = court_lodging_standards value >=  high_amenity_level }
                    }
                }
                add = -10
            }
            if = {
                limit = {
                    liege = {
                        amenity_level = { type = court_lodging_standards value >=  very_high_amenity_level }
                    }
                }
                add = -10
            }
            if = {
                limit = {
                    liege = {
                        amenity_level = { type = court_lodging_standards value >=  max_amenity_level }
                    }
                }
                add = -20
            }
        }
    }
}

acs_sv_max_military_strength = { value = max_military_strength }
acs_sv_current_military_strength = { value = current_military_strength }

acs_sv_current_military_strength_percent = { 
    value = current_military_strength
    divide = { 
        value = max_military_strength
        min = 1
    }
}

