﻿on_game_start = {
    on_actions = {
        #esr_on_game_start
    }    
}

quarterly_playable_pulse = {
    on_actions = {
        #esr_quarterly_opinion_update
    }
}

### SRE was called ESR (Expended Social Relations) before release. That's why esr_ prefix is used instead of sre_

### Maybe it's a good idea to put code for different features into separate files.

### Add war related opinions
on_war_started = {
    on_actions = {
        esr_on_war_started
        esr_on_war_started_for_threat
    }
}

on_join_war_as_secondary = {
    on_actions = {
        esr_on_join_war_as_secondary
    }
}

### Remove imprisonment opinion modifiers from former prisoner's relations
on_release_from_prison = {
    on_actions = {
        esr_on_release_from_prison
    }
}

### Updating Mutual Acquaintances, Family Friends and Social Standing

# Normal relations (friend)
on_set_relation_friend = {
    on_actions = {
        esr_on_become_friend
        esr_on_become_friend_for_social_standing
        esr_remove_threat
        esr_on_rival_to_friend
        esr_on_nemesis_to_friend        
    }  
}

on_remove_relation_friend = {
    on_actions = {
        esr_on_remove_friend
        esr_on_remove_friend_for_social_standing
    }  
}

on_death_relation_friend = {
    on_actions = {
        esr_on_death_friend
        esr_on_death_friend_for_social_standing
    }
}

# Life long relations (best friend)
# on_remove_relation_<normal_relation> doesn't trigger when the corresponding on_set_relation_<life_long_relation> is triggered! 
# Solution: execute esr_on_remove_<normal_relation> before esr_on_become_<life_long_relation>.
on_set_relation_best_friend = {
    on_actions = {
        esr_on_remove_friend
        esr_on_become_best_friend
        esr_on_become_best_friend_for_social_standing #Added a trigger for existing friends to prevent double applying (the same is done for all relations with a life long version)
        esr_remove_threat
        esr_on_rival_to_friend
        esr_on_nemesis_to_friend        
    }  
}

on_remove_relation_best_friend = {
    on_actions = {
        esr_on_remove_best_friend
        esr_on_remove_friend_for_social_standing
    }  
}

on_death_relation_best_friend = {
    on_actions = {
        esr_on_death_best_friend
        esr_on_death_friend_for_social_standing
    }
}

# Normal relations (rival)
on_set_relation_rival = {
    on_actions = {
        esr_on_become_rival
        esr_on_become_rival_for_social_standing
        esr_on_friend_to_rival
        esr_on_best_friend_to_rival        
    }  
}

on_remove_relation_rival = {
    on_actions = {
        esr_on_remove_rival
        esr_on_remove_rival_for_social_standing
    }  
}

on_death_relation_rival = {
    on_actions = {
        esr_on_death_rival
        esr_on_death_rival_for_social_standing
    }
}

# Life long relations (nemesis)
on_set_relation_nemesis = {
    on_actions = {
        esr_on_remove_rival
        esr_on_become_nemesis
        esr_on_become_nemesis_for_social_standing
        esr_on_friend_to_rival
        esr_on_best_friend_to_rival
    }  
}

on_remove_relation_nemesis = {
    on_actions = {
        esr_on_remove_nemesis
        esr_on_remove_rival_for_social_standing        
    }  
}

on_death_relation_nemesis = {
    on_actions = {
        esr_on_death_nemesis
        esr_on_death_rival_for_social_standing
    }
}

# Lover Relations (For Social Standing only)
on_set_relation_lover = {
    on_actions = {
        esr_on_become_lover_for_social_standing
        esr_remove_threat
    }  
}

on_remove_relation_lover = {
    on_actions = {
        esr_on_remove_lover_for_social_standing
    }  
}

on_death_relation_lover = {
    on_actions = {
        esr_on_death_lover_for_social_standing
    }
}

# Life long relations (soulmate)
on_set_relation_soulmate = {
    on_actions = {
        esr_on_become_soulmate_for_social_standing
        esr_remove_threat
    }  
}

on_remove_relation_soulmate = {
    on_actions = {
        esr_on_remove_lover_for_social_standing
    }  
}

on_death_relation_soulmate = {
    on_actions = {
        esr_on_death_lover_for_social_standing
    }
}

# Update Social Standing exp
on_rank_up = { # Not ideal since it "Will not fire during history execution or for dying characters"; Further testing shows it does fire for dying characters !?
    on_actions = {
        esr_on_rank_up
    }
}

on_rank_down = {
    on_actions = {
        esr_on_rank_down
    }
}

on_title_gain = {
    on_actions = {
        #esr_on_title_gain
    }
}

on_title_lost = {
    on_actions = {
        #esr_on_title_lost
    }
}

on_vassal_gained = {
    on_actions = {
        delay = { days = 1 }
        esr_on_vassal_gained
    }
}

on_marriage = {
    on_actions = {
        #delay = { days = 1 }
        esr_on_marriage
    }
}

on_divorce = {
    on_actions = {
        #delay = { days = 1 }
        esr_on_divorce
    }
}

on_death = {
    on_actions = {
        esr_on_death_for_social_standing
    }
}

yearly_playable_pulse = { # Remove esr_popular_among_peers track exp for independent characters
    on_actions = {
        esr_yearly_playable_pulse
    }    
}

### Add Family Friends opinion modifiers to new borns.
on_birth_child = {
    on_actions = {
        esr_on_birth_child
    }
}

###### All esr on_actions appended ######

### Updating Mutual Acquaintances, Family Friends and Social Standing
esr_on_become_friend = {
    effect = {
        #debug_log = "esr_on_become_friend"
        #debug_log_scopes = yes

        # root and scope:target seem to be randomly chosen from the two chars becoming friends (Tho the roles are consistent on game start)
        # The same effect need to be executed for both root and scope:target
        # All esr_on_become_<relation> and esr_on_remove_<relation> have similar structure (remove_opinion instead of add_opinion for esr_on_remove_<relation>)

        scope:target = { #Update opinions between scope:target's relations and root

            ### Update Mutual Acquaintances
            every_relation = {
                type = friend
                save_scope_as = common_friend_t
                limit = {
                    NOR = {
                        this = root 
                        has_relation_best_friend = scope:target
                    }
				}
                add_opinion = {
                    target = root
                    modifier = esr_common_friend
                }
                root= {
                    add_opinion = {
                        target = scope:common_friend_t
                        modifier = esr_common_friend
                    }                    
                }

                if = {
                    limit = { debug_only = yes }
                    debug_log = "(TAD) relation_dump.txt [GetCurrentDate.GetYear];Get Common Friend;[THIS.Char.GetID];[PREV.GetCharacter.GetID];[target.GetID]"
                }
            }
            
            every_relation = {
                type = best_friend
                save_scope_as = best_friend_t
                limit = {
					NOT = { this = root }
				}
                add_opinion = {
                    target = root
                    modifier = esr_common_friend
                }

                root= {
                    add_opinion = {
                        target = scope:best_friend_t
                        modifier = esr_common_friend
                    }                    
                }
                if = {
                    limit = { debug_only = yes }
                    debug_log = "(TAD) relation_dump.txt [GetCurrentDate.GetYear];Get Common Friend (BF);[THIS.Char.GetID];[PREV.GetCharacter.GetID];[target.GetID]"
                }
            }

            every_relation = {
                type = rival
                save_scope_as = rival_t
                limit = {
                    NOR = {
                        this = root 
                        has_relation_nemesis = scope:target
                    }
				}
                add_opinion = {
                    target = root
                    modifier = esr_rival_friend
                }
                root= {
                    add_opinion = {
                        target = scope:rival_t
                        modifier = esr_friend_rival
                    }                    
                }
                if = {
                    limit = { debug_only = yes }
                    debug_log = "(TAD) relation_dump.txt [GetCurrentDate.GetYear];Get Rival's Friend;[THIS.Char.GetID];[PREV.GetCharacter.GetID];[target.GetID]"
                }
            }

            every_relation = {
                type = nemesis
                save_scope_as = nemesis_t
                limit = {
					NOT = { this = root }
				}
                add_opinion = {
                    target = root
                    modifier = esr_nemesis_friend
                }
                root= {
                    add_opinion = {
                        target = scope:nemesis_t
                        modifier = esr_friend_nemesis
                    }                    
                }
                if = {
                    limit = { debug_only = yes }
                    debug_log = "(TAD) relation_dump.txt [GetCurrentDate.GetYear];Get Rival's Friend (Nemesis);[THIS.Char.GetID];[PREV.GetCharacter.GetID];[target.GetID]"
                }
            }
            ### Update Mutual Acquaintances/

            ### Update Family Friends
            if = {
                limit = {
                    NOT = {is_close_family_of = root}
                }
                every_close_family_member = {
                    if = {
                        limit = {
                            OR = {
                                has_relation_friend = root
                                has_relation_best_friend = root
                            }
                        }
                        add_to_temporary_list = friends_in_family_list_t
                    }
                    add_to_temporary_list = family_list_t
                }
                every_spouse = {
                    limit = {NOT = {is_in_list = family_list_t}}
                    if = {
                        limit = {
                            OR = {
                                has_relation_friend = root
                                has_relation_best_friend = root
                            }
                        }
                        add_to_temporary_list = friends_in_family_list_t
                    }
                    add_to_temporary_list = family_list_t 
                }
                scope:target = {
                    add_to_temporary_list = family_list_t
                }
                if ={
                    limit = {
                        any_in_list = {
                            list = friends_in_family_list_t
                            count > 1 #at least 3 friends in family (at least 2 + prev = 3)
                        }
                    }
 
                    every_in_list = {
                        list = family_list_t
                        limit = {
                            NOR = {
                                is_close_family_of = root
                                this = root
                            }
                        }
                        add_opinion = {
                            target = root
                            modifier = esr_family_friend
                        }
                        add_to_variable_list = {
							name = family_friend_list
							target = root
						}
                        if = {
                            limit = {
                                NOT = {has_relation_esr_family_friend = root}
                            }
                            set_relation_esr_family_friend = root
                        }
                    }
                    if = {
                        limit = { debug_only = yes }
                        debug_log = "(TAD) relation_dump.txt [GetCurrentDate.GetYear];Become Family Friend;[THIS.Char.GetID];[PREV.GetCharacter.GetID];[target.GetID]"
                    }
                }

                every_in_list = { # The same check need to be carried out from the perspective of every close family member
                    list = family_list_t
                    esr_family_friend_add_effect = {TARGET = root}
                }
            }
            ### Update Family Friends/
        }

        root = { #Update opinions between root's relations and scope:target

            ### Update Mutual Acquaintances
            every_relation = {
                type = friend
                save_scope_as = common_friend_r
                limit = {
                    NOR = {
                        this = scope:target
                        has_relation_best_friend = root
                    }
				}
                add_opinion = {
                    target = scope:target
                    modifier = esr_common_friend
                }
                scope:target= {
                    add_opinion = {
                        target = scope:common_friend_r
                        modifier = esr_common_friend
                    }                    
                }
                if = {
                    limit = { debug_only = yes }
                    debug_log = "(TAD) relation_dump.txt [GetCurrentDate.GetYear];Get Common Friend;[THIS.Char.GetID];[PREV.GetCharacter.GetID];[target.GetID]"
                }
            }
            
            every_relation = {
                type = best_friend
                save_scope_as = best_friend_r
                limit = {
					NOT = { this = scope:target }
				}
                add_opinion = {
                    target = scope:target
                    modifier = esr_common_friend
                }
                scope:target = {
                    add_opinion = {
                        target = scope:best_friend_r
                        modifier = esr_common_friend
                    }                    
                }
                if = {
                    limit = { debug_only = yes }
                    debug_log = "(TAD) relation_dump.txt [GetCurrentDate.GetYear];Get Common Friend (BF);[THIS.Char.GetID];[PREV.GetCharacter.GetID];[target.GetID]"
                }
            }

            every_relation = {
                type = rival
                save_scope_as = rival_r
                limit = {
                    NOR = {
                        this = scope:target
                        has_relation_nemesis = root
                    }
				}
                add_opinion = {
                    target = scope:target
                    modifier = esr_rival_friend
                }
                scope:target= {
                    add_opinion = {
                        target = scope:rival_r
                        modifier = esr_friend_rival
                    }                    
                }
                if = {
                    limit = { debug_only = yes }
                    debug_log = "(TAD) relation_dump.txt [GetCurrentDate.GetYear];Get Rival's Friend;[THIS.Char.GetID];[PREV.GetCharacter.GetID];[target.GetID]"
                }
            }

            every_relation = {
                type = nemesis
                save_scope_as = nemesis_r
                limit = {
					NOT = { this = scope:target }
				}
                add_opinion = {
                    target = scope:target
                    modifier = esr_nemesis_friend
                }
                scope:target= {
                    add_opinion = {
                        target = scope:nemesis_r
                        modifier = esr_friend_nemesis
                    }                    
                }
                if = {
                    limit = { debug_only = yes }
                    debug_log = "(TAD) relation_dump.txt [GetCurrentDate.GetYear];Get Rival's Friend (Nemesis);[THIS.Char.GetID];[PREV.GetCharacter.GetID];[target.GetID]"
                }
            }
            ### Update Mutual Acquaintances/
            
            ### Update Family Friends
            if = {
                limit = {
                    NOT = {is_close_family_of = scope:target}
                }
                every_close_family_member = {
                    if = {
                        limit = {
                            OR = {
                                has_relation_friend = scope:target
                                has_relation_best_friend = scope:target
                            }
                        }
                        add_to_temporary_list = friends_in_family_list_r
                    }
                    add_to_temporary_list = family_list_r
                }
                every_spouse = {
                    limit = {NOT = {is_in_list = family_list_r}}
                    if = {
                        limit = {
                            OR = {
                                has_relation_friend = scope:target
                                has_relation_best_friend = scope:target
                            }
                        }
                        add_to_temporary_list = friends_in_family_list_r
                    }
                    add_to_temporary_list = family_list_r 
                }
                root = {
                    add_to_temporary_list = family_list_r
                }
                if ={
                    limit = {
                        any_in_list = {
                            list = friends_in_family_list_r
                            count > 1 #at least 3 friends in family
                        }
                    }
 
                    every_in_list = {
                        list = family_list_r
                        limit = {
                            NOR = {
                                is_close_family_of = scope:target
                                this = scope:target
                            }
                        }
                        add_opinion = {
                            target = scope:target
                            modifier = esr_family_friend
                        }
                        add_to_variable_list = {
							name = family_friend_list
							target = scope:target
						}
                        if = {
                            limit = {
                                NOT = {has_relation_esr_family_friend = scope:target}
                            }
                            set_relation_esr_family_friend = scope:target
                        }
                    }
                    if = {
                        limit = { debug_only = yes }
                        debug_log = "(TAD) relation_dump.txt [GetCurrentDate.GetYear];Become Family Friend;[THIS.Char.GetID];[PREV.GetCharacter.GetID];[target.GetID]"
                    }
                }

                every_in_list = {
                    list = family_list_r
                    esr_family_friend_add_effect = {TARGET = scope:target}
                }
            }
        }
        ### Update Family Friends/
    }  
}

esr_on_become_friend_for_social_standing = {
    effect = {
        ### Add Social Standing Trait and exp
        if = {
            limit = {
                root = {
                    NOR = {
                        has_relation_lover = scope:target
                        has_relation_soulmate = scope:target
                    }
                }
            }
            scope:target = {
                save_scope_as = esr_perspective_char

                esr_set_track_variables = yes

                if = {
                    limit = {
                        OR = {
                            root.highest_held_title_tier >= tier_county
                            root = {
                                any_spouse = {
                                    highest_held_title_tier >= tier_county
                                }
                            }
                        }
                    }
                    esr_change_track_variable = {TRACK = esr_well_connected  VALUE = 6}
                }
                else = {
                    esr_change_track_variable = {TRACK = esr_well_connected  VALUE = 3}
                }
                if = {
                    limit = {
                        OR ={
                            root.highest_held_title_tier >= tier_duchy
                            root = {
                                any_spouse = {
                                    highest_held_title_tier >= tier_duchy
                                }
                            }
                        }
                    }
                    esr_change_track_variable = {TRACK = esr_friends_in_high_places  VALUE = root.esr_friends_in_high_places_exp_value}
                }
                if = {
                    limit = {
                        is_ruler = yes
                        is_independent_ruler = no
                        root.highest_held_title_tier >= tier_county
                        exists = top_liege
                        top_liege = {
                            any_vassal_or_below = {
                                this = root
                            }
                        }
                    }
                    esr_change_track_variable = {TRACK = esr_popular_among_peers  VALUE = root.esr_popular_among_peers_exp_value}
                }

                esr_update_social_standing_trait_effect = yes 

            }
            root = {
                save_scope_as = esr_perspective_char
                
                esr_set_track_variables = yes

                if = {
                    limit = {
                        OR = {
                            scope:target.highest_held_title_tier >= tier_county
                            scope:target = {
                                any_spouse = {
                                    highest_held_title_tier >= tier_county
                                }
                            }
                        }
                    }
                    esr_change_track_variable = {TRACK = esr_well_connected  VALUE = 6}
                }
                else = {
                    esr_change_track_variable = {TRACK = esr_well_connected  VALUE = 3}
                }
                if = {
                    limit = {
                        OR ={
                            scope:target.highest_held_title_tier >= tier_duchy
                            scope:target = {
                                any_spouse = {
                                    highest_held_title_tier >= tier_duchy
                                }
                            }
                        }
                    }
                    esr_change_track_variable = {TRACK = esr_friends_in_high_places  VALUE = scope:target.esr_friends_in_high_places_exp_value}
                }
                if = {
                    limit = {
                        is_ruler = yes
                        is_independent_ruler = no
                        scope:target.highest_held_title_tier >= tier_county
                        exists = top_liege
                        top_liege = {
                            any_vassal_or_below = {
                                this = scope:target
                            }
                        }
                    }
                    esr_change_track_variable = {TRACK = esr_popular_among_peers  VALUE = scope:target.esr_popular_among_peers_exp_value}
                }

                esr_update_social_standing_trait_effect = yes 
            }
        }
        ### Add Social Standing Trait and exp/        
    }
}

esr_on_become_best_friend_for_social_standing = { # To account for chars directly becoming best_friends through script
    trigger = {
        NOT = {
            is_target_in_variable_list = {
                name = esr_friends_list
                target = scope:target
            }
        }
    }

    on_actions = {
        esr_on_become_friend_for_social_standing   
    }
}

esr_on_remove_friend = {
    effect = {
        debug_log = "esr_on_remove_friend"
        #debug_log_scopes = yes
        scope:target = {
            every_relation = {
                type = friend
                save_scope_as = common_friend_t
                limit = {
                    NOR = {
                        this = root 
                        has_relation_best_friend = scope:target
                    }
				}
                remove_opinion = {
                    target = root
                    modifier = esr_common_friend
                    single = yes
                }

                root= {
                    remove_opinion = {
                        target = scope:common_friend_t
                        modifier = esr_common_friend
                        single = yes
                    }
                }

                if = {
                    limit = { debug_only = yes }
                    debug_log = "(TAD) relation_dump.txt [GetCurrentDate.GetYear];Lost Common Friend;[THIS.Char.GetID];[PREV.GetCharacter.GetID];[target.GetID]"
                }
            }
            
            every_relation = {
                type = best_friend
                save_scope_as = best_friend_t
                limit = {
					NOT = { this = root }
				}
                remove_opinion = {
                    target = root
                    modifier = esr_common_friend
                    single = yes
                }

                root= {
                    remove_opinion = {
                        target = scope:best_friend_t
                        modifier = esr_common_friend
                        single = yes
                    }                    
                }
                if = {
                    limit = { debug_only = yes }
                    debug_log = "(TAD) relation_dump.txt [GetCurrentDate.GetYear];Lost Common Friend (BF);[THIS.Char.GetID];[PREV.GetCharacter.GetID];[target.GetID]"
                }
            }

            every_relation = {
                type = rival
                save_scope_as = rival_t
                limit = {
                    NOR = {
                        this = root 
                        has_relation_nemesis = scope:target
                    }
				}
                remove_opinion = {
                    target = root
                    modifier = esr_rival_friend
                    single = yes
                }
                root= {
                    remove_opinion = {
                        target = scope:rival_t
                        modifier = esr_friend_rival
                        single = yes
                    }                    
                }
                if = {
                    limit = { debug_only = yes }
                    debug_log = "(TAD) relation_dump.txt [GetCurrentDate.GetYear];Lost Rival's Friend;[THIS.Char.GetID];[PREV.GetCharacter.GetID];[target.GetID]"
                }
            }

            every_relation = {
                type = nemesis
                save_scope_as = nemesis_t
                limit = {
					NOT = { this = root }
				}
                remove_opinion = {
                    target = root
                    modifier = esr_nemesis_friend
                    single = yes
                }
                root= {
                    remove_opinion = {
                        target = scope:nemesis_t
                        modifier = esr_friend_nemesis
                        single = yes
                    }                    
                }
                if = {
                    limit = { debug_only = yes }
                    debug_log = "(TAD) relation_dump.txt [GetCurrentDate.GetYear];Lost Rival's Friend (Nemesis);[THIS.Char.GetID];[PREV.GetCharacter.GetID];[target.GetID]"
                }
            }

            if = {
                limit = {
                    NOT = {is_close_family_of = root}
                }
                every_close_family_member = {
                    if = {
                        limit = {
                            OR = {
                                has_relation_friend = root
                                has_relation_best_friend = root
                            }
                        }
                        add_to_temporary_list = friends_in_family_list_t
                    }
                    add_to_temporary_list = family_list_t
                }
                every_spouse = {
                    limit = {NOT = {is_in_list = family_list_t}}
                    if = {
                        limit = {
                            OR = {
                                has_relation_friend = root
                                has_relation_best_friend = root
                            }
                        }
                        add_to_temporary_list = friends_in_family_list_t
                    }
                    add_to_temporary_list = family_list_t 
                }
                scope:target = {
                    add_to_temporary_list = family_list_t
                }
                if ={
                    limit = {
                        any_in_list = {
                            list = friends_in_family_list_t
                            count < 3 #at least 3 friends in family (at most 2 to trigger remove)
                        }
                    }
 
                    every_in_list = {
                        list = family_list_t
                        limit = {is_alive = yes}
                        remove_opinion = {
                            target = root
                            modifier = esr_family_friend
                        }
                        remove_list_variable = {
							name = family_friend_list
							target = root
						}
                        if = {
                            limit = {
                                has_relation_esr_family_friend = root
                            }
                            remove_relation_esr_family_friend = root
                        }
                    }

                    every_in_list = { #Will wrongly remove Family Friends from characters!
                        list = family_list_t
                        esr_family_friend_remove_effect = {TARGET = root}
                    }
                    every_in_list = { #Add back the wrongly removed Family Friends.
                        list = family_list_t
                        esr_family_friend_add_effect = {TARGET = root}
                    }

                    ### Functional but Highly Inefficient, need rework!
                } 
            }
        }

        root = {
            every_relation = {
                type = friend
                save_scope_as = common_friend_r
                limit = {
                    NOR = {
                        this = scope:target
                        has_relation_best_friend = root
                    }
				}
                remove_opinion = {
                    target = scope:target
                    modifier = esr_common_friend
                    single = yes
                }
                scope:target= {
                    remove_opinion = {
                        target = scope:common_friend_r
                        modifier = esr_common_friend
                        single = yes
                    }                 
                }
                if = {
                    limit = { debug_only = yes }
                    debug_log = "(TAD) relation_dump.txt [GetCurrentDate.GetYear];Lost Common Friend;[THIS.Char.GetID];[PREV.GetCharacter.GetID];[target.GetID]"
                }
            }
            
            every_relation = {
                type = best_friend
                save_scope_as = best_friend_r
                limit = {
					NOT = { this = scope:target }
				}
                remove_opinion = {
                    target = scope:target
                    modifier = esr_common_friend
                    single = yes
                }
                scope:target = {
                    remove_opinion = {
                        target = scope:best_friend_r
                        modifier = esr_common_friend
                        single = yes
                    }                    
                }
                if = {
                    limit = { debug_only = yes }
                    debug_log = "(TAD) relation_dump.txt [GetCurrentDate.GetYear];Lost Common Friend (BF);[THIS.Char.GetID];[PREV.GetCharacter.GetID];[target.GetID]"
                }
            }

            every_relation = {
                type = rival
                save_scope_as = rival_r
                limit = {
                    NOR = {
                        this = scope:target
                        has_relation_nemesis = root
                    }
				}
                remove_opinion = {
                    target = scope:target
                    modifier = esr_rival_friend
                    single = yes
                }
                scope:target= {
                    remove_opinion = {
                        target = scope:rival_r
                        modifier = esr_friend_rival
                        single = yes
                    }                    
                }
                if = {
                    limit = { debug_only = yes }
                    debug_log = "(TAD) relation_dump.txt [GetCurrentDate.GetYear];Lost Rival's Friend;[THIS.Char.GetID];[PREV.GetCharacter.GetID];[target.GetID]"
                }
            }

            every_relation = {
                type = nemesis
                save_scope_as = nemesis_r
                limit = {
					NOT = { this = scope:target }
				}
                remove_opinion = {
                    target = scope:target
                    modifier = esr_nemesis_friend
                    single = yes
                }
                scope:target= {
                    remove_opinion = {
                        target = scope:nemesis_r
                        modifier = esr_friend_nemesis
                        single = yes
                    }                    
                }
                if = {
                    limit = { debug_only = yes }
                    debug_log = "(TAD) relation_dump.txt [GetCurrentDate.GetYear];Lost Rival's Friend (Nemesis);[THIS.Char.GetID];[PREV.GetCharacter.GetID];[target.GetID]"
                }
            }

            if = {
                limit = {
                    NOT = {is_close_family_of = scope:target}
                }
                every_close_family_member = {
                    if = {
                        limit = {
                            OR = {
                                has_relation_friend = scope:target
                                has_relation_best_friend = scope:target
                            }
                        }
                        add_to_temporary_list = friends_in_family_list_r
                    }
                    add_to_temporary_list = family_list_r
                }
                every_spouse = {
                    limit = {NOT = {is_in_list = family_list_r}}
                    if = {
                        limit = {
                            OR = {
                                has_relation_friend = scope:target
                                has_relation_best_friend = scope:target
                            }
                        }
                        add_to_temporary_list = friends_in_family_list_r
                    }
                    add_to_temporary_list = family_list_r 
                }
                root = {
                    add_to_temporary_list = family_list_r
                }
                if ={
                    limit = {
                        any_in_list = {
                            list = friends_in_family_list_r
                            count < 3 #at least 3 friends in family
                        }
                    }
 
 
                    every_in_list = {
                        list = family_list_r
                        limit = {is_alive = yes}
                        remove_opinion = {
                            target = scope:target
                            modifier = esr_family_friend
                        }
                        remove_list_variable = {
							name = family_friend_list
							target = scope:target
						}
                        if = {
                            limit = {
                                has_relation_esr_family_friend = scope:target
                            }
                            remove_relation_esr_family_friend = scope:target
                        }
                    }

                    every_in_list = { #Will wrongly remove Family Friends from characters!
                        list = family_list_r
                        esr_family_friend_remove_effect = {TARGET = scope:target}
                    }
                    every_in_list = { #Add back the wrongly removed Family Friends.
                        list = family_list_r
                        esr_family_friend_add_effect = {TARGET = scope:target}
                    }

                    ### Functional but Highly Inefficient, need rework!
                }
            }
        }
    }  
}

esr_on_remove_friend_for_social_standing = {
    effect = {
        ### Remove Social Standing exp
        if = {
            limit = {
                root = {
                    NOR = {
                        has_relation_lover = scope:target
                        has_relation_soulmate = scope:target
                    }
                }
            }
            scope:target = {
                save_scope_as = esr_perspective_char

                esr_set_track_variables = yes

                if = {
                    limit = {
                        OR = {
                            root.highest_held_title_tier >= tier_county
                            root = {
                                any_spouse = {
                                    highest_held_title_tier >= tier_county
                                }
                            }
                        }
                    }
                    esr_change_track_variable = {TRACK = esr_well_connected  VALUE = -6}
                }
                else = {
                    esr_change_track_variable = {TRACK = esr_well_connected  VALUE = -3}
                }
                if = {
                    limit = {
                        OR ={
                            root.highest_held_title_tier >= tier_duchy
                            root = {
                                any_spouse = {
                                    highest_held_title_tier >= tier_duchy
                                }
                            }
                        }
                    }
                    esr_change_track_variable = {TRACK = esr_friends_in_high_places  VALUE = root.esr_lost_friends_in_high_places_exp_value}
                }
                if = {
                    limit = {
                        is_ruler = yes
                        is_independent_ruler = no
                        root.highest_held_title_tier >= tier_county
                        exists = top_liege
                        top_liege = {
                            any_vassal_or_below = {
                                this = root
                            }
                        }
                    }
                    esr_change_track_variable = {TRACK = esr_popular_among_peers  VALUE = root.esr_lost_popular_among_peers_exp_value}
                }

                esr_update_social_standing_trait_effect = yes 
                
            }
            root = {
                save_scope_as = esr_perspective_char

                esr_set_track_variables = yes

                if = {
                    limit = {
                        OR = {
                            scope:target.highest_held_title_tier >= tier_county
                            scope:target = {
                                any_spouse = {
                                    highest_held_title_tier >= tier_county
                                }
                            }
                        }
                    }
                    esr_change_track_variable = {TRACK = esr_well_connected  VALUE = -6}
                }
                else = {
                    esr_change_track_variable = {TRACK = esr_well_connected  VALUE = -3}
                }
                if = {
                    limit = {
                        OR ={
                            scope:target.highest_held_title_tier >= tier_duchy
                            scope:target = {
                                any_spouse = {
                                    highest_held_title_tier >= tier_duchy
                                }
                            }
                        }
                    }
                    esr_change_track_variable = {TRACK = esr_friends_in_high_places  VALUE = scope:target.esr_lost_friends_in_high_places_exp_value}
                }
                if = {
                    limit = {
                        is_ruler = yes
                        is_independent_ruler = no
                        scope:target.highest_held_title_tier >= tier_county
                        exists = top_liege
                        top_liege = {
                            any_vassal_or_below = {
                                this = scope:target
                            }
                        }
                    }
                    esr_change_track_variable = {TRACK = esr_popular_among_peers  VALUE = scope:target.esr_lost_popular_among_peers_exp_value}
                }

                esr_update_social_standing_trait_effect = yes

            }
        }
        ### Remove Social Standing exp/        
    }
}

esr_on_death_friend = {
    # All on_death_<relation> seem to trigger twice. Switching the root character the second time.
    # Therefore, remove_opinion only need to be called in one scope for all every_relation blocks. (Calling in both scopes lead to two stacks of opinion modifier being removed)
    # "Update Family Friends" section is unaffected by this change. Besides more unnecessary iterations. (Still functional) (idea: Maybe add a flag on one of the characters to prevent it from being executed twice?)

    effect = {
        #debug_log = "esr_on_death_friend"
        #debug_log_scopes = yes
        scope:target = {
            every_relation = {
                type = friend
                save_scope_as = common_friend_t
                limit = {
                    NOR = {
                        this = root 
                        has_relation_best_friend = scope:target
                    }
				}
                remove_opinion = {
                    target = root
                    modifier = esr_common_friend
                    single = yes
                }
            }
            
            every_relation = {
                type = best_friend
                save_scope_as = best_friend_t
                limit = {
					NOT = { this = root }
				}
                remove_opinion = {
                    target = root
                    modifier = esr_common_friend
                    single = yes
                }
            }

            every_relation = {
                type = rival
                save_scope_as = rival_t
                limit = {
                    NOR = {
                        this = root 
                        has_relation_nemesis = scope:target
                    }
				}
                remove_opinion = {
                    target = root
                    modifier = esr_rival_friend
                    single = yes
                }
            }

            every_relation = {
                type = nemesis
                save_scope_as = nemesis_t
                limit = {
					NOT = { this = root }
				}
                remove_opinion = {
                    target = root
                    modifier = esr_nemesis_friend
                    single = yes
                }
            }

            if = {
                limit = {
                    NOT = {is_close_family_of = root}
                }
                every_close_family_member = {
                    if = {
                        limit = {
                            OR = {
                                has_relation_friend = root
                                has_relation_best_friend = root
                            }
                        }
                        add_to_temporary_list = friends_in_family_list_t
                    }
                    add_to_temporary_list = family_list_t
                }
                every_spouse = {
                    limit = {NOT = {is_in_list = family_list_t}}
                    if = {
                        limit = {
                            OR = {
                                has_relation_friend = root
                                has_relation_best_friend = root
                            }
                        }
                        add_to_temporary_list = friends_in_family_list_t
                    }
                    add_to_temporary_list = family_list_t 
                }
                scope:target = {
                    add_to_temporary_list = family_list_t
                }
                if ={
                    limit = {
                        any_in_list = {
                            list = friends_in_family_list_t
                            count < 3 #at least 3 friends in family
                        }
                    }
 
                    every_in_list = {
                        list = family_list_t
                        limit = {is_alive = yes}
                        remove_opinion = {
                            target = root
                            modifier = esr_family_friend
                        }
                        remove_list_variable = {
							name = family_friend_list
							target = root
						}
                        if = {
                            limit = {
                                has_relation_esr_family_friend = root
                            }
                            remove_relation_esr_family_friend = root
                        }
                    }
                }

                every_in_list = {
                    list = family_list_t
                    esr_family_friend_death_remove_effect = {TARGET = root}
                }
                every_in_list = {
                    list = family_list_t
                    esr_family_friend_death_add_effect = {TARGET = root}
                }
            }
        }

        root = {
            every_relation = {
                type = friend
                save_scope_as = common_friend_r
                limit = {
                    NOR = {
                        this = scope:target
                        has_relation_best_friend = root
                    }
				}
                remove_opinion = {
                    target = scope:target
                    modifier = esr_common_friend
                    single = yes
                }
            }
            
            every_relation = {
                type = best_friend
                save_scope_as = best_friend_r
                limit = {
					NOT = { this = scope:target }
				}
                remove_opinion = {
                    target = scope:target
                    modifier = esr_common_friend
                    single = yes
                }
            }

            every_relation = {
                type = rival
                save_scope_as = rival_r
                limit = {
                    NOR = {
                        this = scope:target
                        has_relation_nemesis = root
                    }
				}
                remove_opinion = {
                    target = scope:target
                    modifier = esr_rival_friend
                    single = yes
                }
            }

            every_relation = {
                type = nemesis
                save_scope_as = nemesis_r
                limit = {
					NOT = { this = scope:target }
				}
                remove_opinion = {
                    target = scope:target
                    modifier = esr_nemesis_friend
                    single = yes
                }
            }

            if = {
                limit = {
                    NOT = {is_close_family_of = scope:target}
                }
                every_close_family_member = {
                    if = {
                        limit = {
                            OR = {
                                has_relation_friend = scope:target
                                has_relation_best_friend = scope:target
                            }
                        }
                        add_to_temporary_list = friends_in_family_list_r
                    }
                    add_to_temporary_list = family_list_r
                }
                every_spouse = {
                    limit = {NOT = {is_in_list = family_list_r}}
                    if = {
                        limit = {
                            OR = {
                                has_relation_friend = scope:target
                                has_relation_best_friend = scope:target
                            }
                        }
                        add_to_temporary_list = friends_in_family_list_r
                    }
                    add_to_temporary_list = family_list_r 
                }
                root = {
                    add_to_temporary_list = family_list_r
                }
                if ={
                    limit = {
                        any_in_list = {
                            list = friends_in_family_list_r
                            count < 3 #at least 3 friends in family
                        }
                    }
                    every_in_list = {
                        list = family_list_r
                        limit = {is_alive = yes}
                        remove_opinion = {
                            target = scope:target
                            modifier = esr_family_friend
                        }
                        remove_list_variable = {
							name = family_friend_list
							target = scope:target
						}
                        if = {
                            limit = {
                                has_relation_esr_family_friend = scope:target
                            }
                            remove_relation_esr_family_friend = scope:target
                        }
                    }

                    every_in_list = {
                        list = family_list_r
                        esr_family_friend_death_remove_effect = {TARGET = scope:target}
                    }
                    every_in_list = {
                        list = family_list_r
                        esr_family_friend_death_add_effect = {TARGET = scope:target}
                    }
                }
            }
        }
    }  
}

esr_on_death_friend_for_social_standing = {
    effect = {
        ### Remove Social Standing exp on death
        if = {
            limit = {
                always = yes # Prevent double removal for the death of friend - lovers
            }
            scope:target = {
                save_scope_as = esr_perspective_char

                esr_set_track_variables = yes

                if = {
                    limit = {
                        OR = {
                            root.highest_held_title_tier >= tier_county
                            root = {
                                any_spouse = {
                                    highest_held_title_tier >= tier_county
                                }
                            }
                        }
                    }
                    esr_change_track_variable = {TRACK = esr_well_connected  VALUE = -6}
                }
                else = {
                    esr_change_track_variable = {TRACK = esr_well_connected  VALUE = -3}
                }
                if = {
                    limit = {
                        OR ={
                            root.highest_held_title_tier >= tier_duchy
                            root = {
                                any_spouse = {
                                    highest_held_title_tier >= tier_duchy
                                }
                            }
                        }
                    }
                    esr_change_track_variable = {TRACK = esr_friends_in_high_places  VALUE = root.esr_lost_friends_in_high_places_exp_value}
                }
                if = {
                    limit = {
                        is_ruler = yes
                        is_independent_ruler = no
                        root.highest_held_title_tier >= tier_county
                        exists = top_liege
                        top_liege = {
                            any_vassal_or_below = {
                                this = root
                            }
                        }
                    }
                    esr_change_track_variable = {TRACK = esr_popular_among_peers  VALUE = root.esr_lost_popular_among_peers_exp_value}
                }

                esr_update_social_standing_trait_effect = yes

            }
            root = {
                save_scope_as = esr_perspective_char

                esr_set_track_variables = yes

                if = {
                    limit = {
                        OR = {
                            scope:target.highest_held_title_tier >= tier_county
                            scope:target = {
                                any_spouse = {
                                    highest_held_title_tier >= tier_county
                                }
                            }
                        }
                    }
                    esr_change_track_variable = {TRACK = esr_well_connected  VALUE = -6}
                }
                else = {
                    esr_change_track_variable = {TRACK = esr_well_connected  VALUE = -3}
                }
                if = {
                    limit = {
                        OR ={
                            scope:target.highest_held_title_tier >= tier_duchy
                            scope:target = {
                                any_spouse = {
                                    highest_held_title_tier >= tier_duchy
                                }
                            }
                        }
                    }
                    esr_change_track_variable = {TRACK = esr_friends_in_high_places  VALUE = scope:target.esr_lost_friends_in_high_places_exp_value}
                }
                if = {
                    limit = {
                        is_ruler = yes
                        is_independent_ruler = no
                        scope:target.highest_held_title_tier >= tier_county
                        exists = top_liege
                        top_liege = {
                            any_vassal_or_below = {
                                this = scope:target
                            }
                        }
                    }
                    esr_change_track_variable = {TRACK = esr_popular_among_peers  VALUE = scope:target.esr_lost_popular_among_peers_exp_value}
                }

                esr_update_social_standing_trait_effect = yes

            }
        }
        ### Remove Social Standing exp on death/        
    }    
}

esr_on_become_best_friend = {
    effect = {
        #debug_log = "esr_on_become_best_friend"
        #debug_log_scopes = yes
        scope:target = {
            every_relation = {
                type = friend
                save_scope_as = common_friend_t
                limit = {
                    NOR = {
                        this = root 
                        has_relation_best_friend = scope:target
                    }
				}

                add_opinion = {
                    target = root
                    modifier = esr_common_friend
                }

                root= {
                    add_opinion = {
                        target = scope:common_friend_t
                        modifier = esr_common_friend
                    }                    
                }


            }

            every_relation = {
                type = rival
                save_scope_as = rival_t
                limit = {
					NOR = {
                        this = root 
                        has_relation_nemesis = scope:target
                    }
				}
                add_opinion = {
                    target = root
                    modifier = esr_rival_best_friend
                }
                root= {
                    add_opinion = {
                        target = scope:rival_t
                        modifier = esr_best_friend_rival
                    }                    
                }

            }

            every_relation = {
                type = nemesis
                save_scope_as = nemesis_t
                limit = {
					NOT = { this = root }
				}
                add_opinion = {
                    target = root
                    modifier = esr_nemesis_best_friend
                }
                root= {
                    add_opinion = {
                        target = scope:nemesis_t
                        modifier = esr_best_friend_nemesis
                    }                    
                }

            }
        }

        root = {
            every_relation = {
                type = friend
                save_scope_as = common_friend_r
                limit = {
                    NOR = {
                        this = scope:target
                        has_relation_best_friend = root
                    }
				}

                add_opinion = {
                    target = scope:target
                    modifier = esr_common_friend
                }

                scope:target= {
                    add_opinion = {
                        target = scope:common_friend_r
                        modifier = esr_common_friend
                    }                    
                }

            }

            every_relation = {
                type = rival
                save_scope_as = rival_r
                limit = {
                    NOR = {
                        this = scope:target
                        has_relation_nemesis = root
                    } 
				}
                add_opinion = {
                    target = scope:target
                    modifier = esr_rival_best_friend
                }
                scope:target= {
                    add_opinion = {
                        target = scope:rival_r
                        modifier = esr_best_friend_rival
                    }                    
                }

            }

            every_relation = {
                type = nemesis
                save_scope_as = nemesis_r
                limit = {
					NOT = { this = scope:target }
				}
                add_opinion = {
                    target = scope:target
                    modifier = esr_nemesis_best_friend
                }
                scope:target= {
                    add_opinion = {
                        target = scope:nemesis_r
                        modifier = esr_best_friend_nemesis
                    }                    
                }

            }
        }

    }  
}

esr_on_remove_best_friend = {
    effect = {
        #debug_log = "esr_on_remove_best_friend"
        #debug_log_scopes = yes
        scope:target = {
            every_relation = {
                type = friend
                save_scope_as = common_friend_t
                limit = {
                    NOR = {
                        this = root 
                        has_relation_best_friend = scope:target
                    }
				}
                remove_opinion = {
                    target = root
                    modifier = esr_common_friend
                    single = yes
                }
                root= {
                    remove_opinion = {
                        target = scope:common_friend_t
                        modifier = esr_common_friend
                        single = yes
                    }                    
                }


            }

            every_relation = {
                type = rival
                save_scope_as = rival_t
                limit = {
                    NOR = {
                        this = root 
                        has_relation_nemesis = scope:target
                    }
				}
                remove_opinion = {
                    target = root
                    modifier = esr_rival_best_friend
                    single = yes
                }
                root= {
                    remove_opinion = {
                        target = scope:rival_t
                        modifier = esr_best_friend_rival
                        single = yes
                    }                    
                }

            }

            every_relation = {
                type = nemesis
                save_scope_as = nemesis_t
                limit = {
					NOT = { this = root }
				}
                remove_opinion = {
                    target = root
                    modifier = esr_nemesis_best_friend
                    single = yes
                }
                root= {
                    remove_opinion = {
                        target = scope:nemesis_t
                        modifier = esr_best_friend_nemesis
                        single = yes
                    }                    
                }

            }
        }

        root = {
            every_relation = {
                type = friend
                save_scope_as = common_friend_r
                limit = {
                    NOR = {
                        this = scope:target
                        has_relation_best_friend = root
                    }
				}
                remove_opinion = {
                    target = scope:target
                    modifier = esr_common_friend
                    single = yes
                }
                scope:target= {
                    remove_opinion = {
                        target = scope:common_friend_r
                        modifier = esr_common_friend
                        single = yes
                    }                    
                }

            }

            every_relation = {
                type = rival
                save_scope_as = rival_r
                limit = {
                    NOR = {
                        this = scope:target
                        has_relation_nemesis = root
                    }
				}
                remove_opinion = {
                    target = scope:target
                    modifier = esr_rival_best_friend
                    single = yes
                }
                scope:target= {
                    remove_opinion = {
                        target = scope:rival_r
                        modifier = esr_best_friend_rival
                        single = yes
                    }                    
                }

            }

            every_relation = {
                type = nemesis
                save_scope_as = nemesis_r
                limit = {
					NOT = { this = scope:target }
				}
                remove_opinion = {
                    target = scope:target
                    modifier = esr_nemesis_best_friend
                    single = yes
                }
                scope:target= {
                    remove_opinion = {
                        target = scope:nemesis_r
                        modifier = esr_best_friend_nemesis
                        single = yes
                    }                    
                }

            }
        }

    }  
}

esr_on_death_best_friend = {
    effect = {
        #debug_log = "esr_on_death_best_friend"
        #debug_log_scopes = yes
        scope:target = {
            every_relation = {
                type = friend
                save_scope_as = common_friend_t
                limit = {
                    NOR = {
                        this = root 
                        has_relation_best_friend = scope:target
                    }
				}
                remove_opinion = {
                    target = root
                    modifier = esr_common_friend
                    single = yes
                }
            }

            every_relation = {
                type = rival
                save_scope_as = rival_t
                limit = {
                    NOR = {
                        this = root 
                        has_relation_nemesis = scope:target
                    }
				}
                remove_opinion = {
                    target = root
                    modifier = esr_rival_best_friend
                    single = yes
                }
            }

            every_relation = {
                type = nemesis
                save_scope_as = nemesis_t
                limit = {
					NOT = { this = root }
				}
                remove_opinion = {
                    target = root
                    modifier = esr_nemesis_best_friend
                    single = yes
                }
            }

            if = {
                limit = {
                    NOT = {is_close_family_of = root}
                }
                every_close_family_member = {
                    if = {
                        limit = {
                            OR = {
                                has_relation_friend = root
                                has_relation_best_friend = root
                            }
                        }
                        add_to_temporary_list = friends_in_family_list_t
                    }
                    add_to_temporary_list = family_list_t
                }
                every_spouse = {
                    limit = {NOT = {is_in_list = family_list_t}}
                    if = {
                        limit = {
                            OR = {
                                has_relation_friend = root
                                has_relation_best_friend = root
                            }
                        }
                        add_to_temporary_list = friends_in_family_list_t
                    }
                    add_to_temporary_list = family_list_t 
                }
                scope:target = {
                    add_to_temporary_list = family_list_t
                }
                if ={
                    limit = {
                        any_in_list = {
                            list = friends_in_family_list_t
                            count < 3 #at least 3 friends in family
                        }
                    }
                    every_in_list = {
                        list = family_list_t
                        limit = {is_alive = yes}
                        remove_opinion = {
                            target = root
                            modifier = esr_family_friend
                        }
                        remove_list_variable = {
							name = family_friend_list
							target = root
						}
                        if = {
                            limit = {
                                has_relation_esr_family_friend = root
                            }
                            remove_relation_esr_family_friend = root
                        }
                    }
                }

                every_in_list = {
                    list = family_list_t
                    esr_family_friend_death_remove_effect = {TARGET = root}
                }
                every_in_list = {
                    list = family_list_t
                    esr_family_friend_death_add_effect = {TARGET = root}
                }
            }
        }

        root = {
            every_relation = {
                type = friend
                save_scope_as = common_friend_r
                limit = {
                    NOR = {
                        this = scope:target
                        has_relation_best_friend = root
                    }
				}
                remove_opinion = {
                    target = scope:target
                    modifier = esr_common_friend
                    single = yes
                }
            }

            every_relation = {
                type = rival
                save_scope_as = rival_r
                limit = {
                    NOR = {
                        this = scope:target
                        has_relation_nemesis = root
                    }
				}
                remove_opinion = {
                    target = scope:target
                    modifier = esr_rival_best_friend
                    single = yes
                }
            }

            every_relation = {
                type = nemesis
                save_scope_as = nemesis_r
                limit = {
					NOT = { this = scope:target }
				}
                remove_opinion = {
                    target = scope:target
                    modifier = esr_nemesis_best_friend
                    single = yes
                }
            }

            if = {
                limit = {
                    NOT = {is_close_family_of = scope:target}
                }
                every_close_family_member = {
                    if = {
                        limit = {
                            OR = {
                                has_relation_friend = scope:target
                                has_relation_best_friend = scope:target
                            }
                        }
                        add_to_temporary_list = friends_in_family_list_r
                    }
                    add_to_temporary_list = family_list_r
                }
                every_spouse = {
                    limit = {NOT = {is_in_list = family_list_r}}
                    if = {
                        limit = {
                            OR = {
                                has_relation_friend = scope:target
                                has_relation_best_friend = scope:target
                            }
                        }
                        add_to_temporary_list = friends_in_family_list_r
                    }
                    add_to_temporary_list = family_list_r 
                }
                root = {
                    add_to_temporary_list = family_list_r
                }
                if ={
                    limit = {
                        any_in_list = {
                            list = friends_in_family_list_r
                            count < 3 #at least 3 friends in family
                        }
                    }
                    every_in_list = {
                        list = family_list_r
                        limit = {is_alive = yes}
                        remove_opinion = {
                            target = scope:target
                            modifier = esr_family_friend
                        }
                        remove_list_variable = {
							name = family_friend_list
							target = scope:target
						}
                        if = {
                            limit = {
                                has_relation_esr_family_friend = scope:target
                            }
                            remove_relation_esr_family_friend = scope:target
                        }
                    }
                }

                every_in_list = {
                    list = family_list_r
                    esr_family_friend_death_remove_effect = {TARGET = scope:target}
                }
                every_in_list = {
                    list = family_list_r
                    esr_family_friend_death_add_effect = {TARGET = scope:target}
                }
            }
        }

    }  
}

esr_on_become_rival = {
    effect = {
        #debug_log = "esr_on_become_rival"
        #debug_log_scopes = yes
        scope:target = {
            every_relation = {
                type = rival
                save_scope_as = common_rival_t
                limit = {
                    NOR = {
                        this = root 
                        has_relation_nemesis = scope:target
                    }
				}
                add_opinion = {
                    target = root
                    modifier = esr_common_rival
                }
                root= {
                    add_opinion = {
                        target = scope:common_rival_t
                        modifier = esr_common_rival
                    }                    
                }


            }
            
            every_relation = {
                type = nemesis
                save_scope_as = nemesis_t
                limit = {
					NOT = { this = root }
				}
                add_opinion = {
                    target = root
                    modifier = esr_common_rival
                }

                root= {
                    add_opinion = {
                        target = scope:nemesis_t
                        modifier = esr_common_rival
                    }                    
                }

            }

            every_relation = {
                type = friend
                save_scope_as = friend_t
                limit = {
                    NOR = {
                        this = root 
                        has_relation_best_friend = scope:target
                    }
				}
                add_opinion = {
                    target = root
                    modifier = esr_friend_rival
                }
                root= {
                    add_opinion = {
                        target = scope:friend_t
                        modifier = esr_rival_friend
                    }                    
                }

            }

            every_relation = {
                type = best_friend
                save_scope_as = best_friend_t
                limit = {
					NOT = { this = root }
				}
                add_opinion = {
                    target = root
                    modifier = esr_best_friend_rival
                }
                root= {
                    add_opinion = {
                        target = scope:best_friend_t
                        modifier = esr_rival_best_friend
                    }                    
                }

            }
        }

        root = {
            every_relation = {
                type = rival
                save_scope_as = common_rival_r
                limit = {
                    NOR = {
                        this = scope:target
                        has_relation_nemesis = root
                    }          
				}
                add_opinion = {
                    target = scope:target
                    modifier = esr_common_rival
                }
                scope:target= {
                    add_opinion = {
                        target = scope:common_rival_r
                        modifier = esr_common_rival
                    }                    
                }


            }
            
            every_relation = {
                type = nemesis
                save_scope_as = nemesis_r
                limit = {
					NOT = { this = scope:target }
				}
                add_opinion = {
                    target = scope:target
                    modifier = esr_common_rival
                }

                scope:target= {
                    add_opinion = {
                        target = scope:nemesis_r
                        modifier = esr_common_rival
                    }                    
                }
 
            }

            every_relation = {
                type = friend
                save_scope_as = friend_r
                limit = {
                    NOR = {
                        this = scope:target
                        has_relation_best_friend = root
                    }
				}
                add_opinion = {
                    target = scope:target
                    modifier = esr_friend_rival
                }
                scope:target= {
                    add_opinion = {
                        target = scope:friend_r
                        modifier = esr_rival_friend
                    }                    
                }

            }

            every_relation = {
                type = best_friend
                save_scope_as = best_friend_r
                limit = {
					NOT = { this = scope:target }
				}
                add_opinion = {
                    target = scope:target
                    modifier = esr_best_friend_rival
                }
                scope:target= {
                    add_opinion = {
                        target = scope:best_friend_r
                        modifier = esr_rival_best_friend
                    }                    
                }

            }
        }       
    }  
}

esr_on_become_rival_for_social_standing = {
    effect = {
        ### Add Social Standing Trait and exp
        if = {
            limit = {
                always = yes
            }
            scope:target = {
                save_scope_as = esr_perspective_char

                esr_set_track_variables = yes

                if = {
                    limit = {
                        OR ={
                            root.highest_held_title_tier >= tier_duchy
                            root = {
                                any_spouse = {
                                    highest_held_title_tier >= tier_duchy
                                }
                            }
                        }
                    }
                    esr_change_track_variable = {TRACK = esr_social_pariah  VALUE = root.esr_social_pariah_exp_value}
                }
                
                esr_update_social_standing_trait_effect = yes 
            }
            root = {
                save_scope_as = esr_perspective_char

                esr_set_track_variables = yes

                if = {
                    limit = {
                        OR ={
                            scope:target.highest_held_title_tier >= tier_duchy
                            scope:target = {
                                any_spouse = {
                                    highest_held_title_tier >= tier_duchy
                                }
                            }
                        }
                    }
                    esr_change_track_variable = {TRACK = esr_social_pariah  VALUE = scope:target.esr_social_pariah_exp_value}
                }

                esr_update_social_standing_trait_effect = yes 
            }
        }
        ### Add Social Standing Trait and exp/        
    }
}

esr_on_become_nemesis_for_social_standing = {
    trigger = {
        NOT = {
            is_target_in_variable_list = {
                name = esr_rivals_list
                target = scope:target
            }
        }
    }

    on_actions = {
        esr_on_become_rival_for_social_standing
    }
}

esr_on_remove_rival = {
    effect = {
        #debug_log = "esr_on_remove_rival"
        #debug_log_scopes = yes
        scope:target = {
            every_relation = {
                type = rival
                save_scope_as = common_rival_t
                limit = {
                    NOR = {
                        this = root 
                        has_relation_nemesis = scope:target
                    }
				}
                remove_opinion = {
                    target = root
                    modifier = esr_common_rival
                    single = yes
                }
                root= {
                    remove_opinion = {
                        target = scope:common_rival_t
                        modifier = esr_common_rival
                        single = yes
                    }                    
                }


            }
            
            every_relation = {
                type = nemesis
                save_scope_as = nemesis_t
                limit = {
					NOT = { this = root }
				}
                remove_opinion = {
                    target = root
                    modifier = esr_common_rival
                    single = yes
                }

                root= {
                    remove_opinion = {
                        target = scope:nemesis_t
                        modifier = esr_common_rival
                        single = yes
                    }                    
                }

            }

            every_relation = {
                type = friend
                save_scope_as = friend_t
                limit = {
                    NOR = {
                        this = root 
                        has_relation_best_friend = scope:target
                    }
				}
                remove_opinion = {
                    target = root
                    modifier = esr_friend_rival
                    single = yes
                }
                root= {
                    remove_opinion = {
                        target = scope:friend_t
                        modifier = esr_rival_friend
                        single = yes
                    }                    
                }

            }

            every_relation = {
                type = best_friend
                save_scope_as = best_friend_t
                limit = {
					NOT = { this = root }
				}
                remove_opinion = {
                    target = root
                    modifier = esr_best_friend_rival
                    single = yes
                }
                root= {
                    remove_opinion = {
                        target = scope:best_friend_t
                        modifier = esr_rival_best_friend
                        single = yes
                    }                    
                }

            }
        }

        root = {
            every_relation = {
                type = rival
                save_scope_as = common_rival_r
                limit = {
                    NOR = {
                        this = scope:target
                        has_relation_nemesis = root
                    }          
				}
                remove_opinion = {
                    target = scope:target
                    modifier = esr_common_rival
                    single = yes
                }
                scope:target= {
                    remove_opinion = {
                        target = scope:common_rival_r
                        modifier = esr_common_rival
                        single = yes
                    }                    
                }


            }
            
            every_relation = {
                type = nemesis
                save_scope_as = nemesis_r
                limit = {
					NOT = { this = scope:target }
				}
                remove_opinion = {
                    target = scope:target
                    modifier = esr_common_rival
                    single = yes
                }

                scope:target= {
                    remove_opinion = {
                        target = scope:nemesis_r
                        modifier = esr_common_rival
                        single = yes
                    }                    
                }
 
            }

            every_relation = {
                type = friend
                save_scope_as = friend_r
                limit = {
                    NOR = {
                        this = scope:target
                        has_relation_best_friend = root
                    }
				}
                remove_opinion = {
                    target = scope:target
                    modifier = esr_friend_rival
                    single = yes
                }
                scope:target= {
                    remove_opinion = {
                        target = scope:friend_r
                        modifier = esr_rival_friend
                        single = yes
                    }                    
                }

            }

            every_relation = {
                type = best_friend
                save_scope_as = best_friend_r
                limit = {
					NOT = { this = scope:target }
				}
                remove_opinion = {
                    target = scope:target
                    modifier = esr_best_friend_rival
                    single = yes
                }
                scope:target= {
                    remove_opinion = {
                        target = scope:best_friend_r
                        modifier = esr_rival_best_friend
                        single = yes
                    }                    
                }
            }
        }
    }  
}

esr_on_remove_rival_for_social_standing = {
    effect = {
        ### Remove Social Standing Trait and exp
        if = {
            limit = {
                always = yes
            }
            scope:target = {
                save_scope_as = esr_perspective_char

                esr_set_track_variables = yes

                if = {
                    limit = {
                        OR ={
                            root.highest_held_title_tier >= tier_duchy
                            root = {
                                any_spouse = {
                                    highest_held_title_tier >= tier_duchy
                                }
                            }
                        }
                    }
                    esr_change_track_variable = {TRACK = esr_social_pariah  VALUE = root.esr_lost_social_pariah_exp_value}
                }
                
                esr_update_social_standing_trait_effect = yes 
            }
            root = {
                save_scope_as = esr_perspective_char

                esr_set_track_variables = yes

                if = {
                    limit = {
                        OR ={
                            scope:target.highest_held_title_tier >= tier_duchy
                            scope:target = {
                                any_spouse = {
                                    highest_held_title_tier >= tier_duchy
                                }
                            }
                        }
                    }
                    esr_change_track_variable = {TRACK = esr_social_pariah  VALUE = scope:target.esr_lost_social_pariah_exp_value}
                }

                esr_update_social_standing_trait_effect = yes 
            }
        }
        ### Remove Social Standing Trait and exp/
    }
}

esr_on_death_rival = {
    effect = {
        #debug_log = "esr_on_death_rival"
        #debug_log_scopes = yes
        scope:target = {
            every_relation = {
                type = rival
                save_scope_as = common_rival_t
                limit = {
                    NOR = {
                        this = root 
                        has_relation_nemesis = scope:target
                    }
				}
                remove_opinion = {
                    target = root
                    modifier = esr_common_rival
                    single = yes
                }
            }
            
            every_relation = {
                type = nemesis
                save_scope_as = nemesis_t
                limit = {
					NOT = { this = root }
				}
                remove_opinion = {
                    target = root
                    modifier = esr_common_rival
                    single = yes
                }
            }

            every_relation = {
                type = friend
                save_scope_as = friend_t
                limit = {
                    NOR = {
                        this = root 
                        has_relation_best_friend = scope:target
                    }
				}
                remove_opinion = {
                    target = root
                    modifier = esr_friend_rival
                    single = yes
                }
            }

            every_relation = {
                type = best_friend
                save_scope_as = best_friend_t
                limit = {
					NOT = { this = root }
				}
                remove_opinion = {
                    target = root
                    modifier = esr_best_friend_rival
                    single = yes
                }
            }
        }

        root = {
            every_relation = {
                type = rival
                save_scope_as = common_rival_r
                limit = {
                    NOR = {
                        this = scope:target
                        has_relation_nemesis = root
                    }          
				}
                remove_opinion = {
                    target = scope:target
                    modifier = esr_common_rival
                    single = yes
                }
            }
            
            every_relation = {
                type = nemesis
                save_scope_as = nemesis_r
                limit = {
					NOT = { this = scope:target }
				}
                remove_opinion = {
                    target = scope:target
                    modifier = esr_common_rival
                    single = yes
                }
            }

            every_relation = {
                type = friend
                save_scope_as = friend_r
                limit = {
                    NOR = {
                        this = scope:target
                        has_relation_best_friend = root
                    }
				}
                remove_opinion = {
                    target = scope:target
                    modifier = esr_friend_rival
                    single = yes
                }
            }

            every_relation = {
                type = best_friend
                save_scope_as = best_friend_r
                limit = {
					NOT = { this = scope:target }
				}
                remove_opinion = {
                    target = scope:target
                    modifier = esr_best_friend_rival
                    single = yes
                }
            }
        }
    }  
}

esr_on_death_rival_for_social_standing = {
    effect = {
        ### Remove Social Standing Trait and exp
        if = {
            limit = {
                always = yes
            }
            scope:target = {
                save_scope_as = esr_perspective_char

                esr_set_track_variables = yes

                if = {
                    limit = {
                        OR ={
                            root.highest_held_title_tier >= tier_duchy
                            root = {
                                any_spouse = {
                                    highest_held_title_tier >= tier_duchy
                                }
                            }
                        }
                    }
                    esr_change_track_variable = {TRACK = esr_social_pariah  VALUE = root.esr_lost_social_pariah_exp_value}
                }

                esr_update_social_standing_trait_effect = yes 
            }
            root = {
                save_scope_as = esr_perspective_char

                esr_set_track_variables = yes

                if = {
                    limit = {
                        OR ={
                            scope:target.highest_held_title_tier >= tier_duchy
                            scope:target = {
                                any_spouse = {
                                    highest_held_title_tier >= tier_duchy
                                }
                            }
                        }
                    }
                    esr_change_track_variable = {TRACK = esr_social_pariah  VALUE = scope:target.esr_lost_social_pariah_exp_value}
                }

                esr_update_social_standing_trait_effect = yes 
            }
        }
        ### Remove Social Standing Trait and exp/        
    }
}

esr_on_become_nemesis = {
    effect = {
        #debug_log = "esr_on_become_nemesis"
        #debug_log_scopes = yes
        scope:target = {
            every_relation = {
                type = rival
                save_scope_as = common_rival_t
                limit = {
                    NOR = {
                        this = root 
                        has_relation_nemesis = scope:target
                    }
				}

                add_opinion = {
                    target = root
                    modifier = esr_common_rival
                }

                root= {
                    
                    add_opinion = {
                        target = scope:common_rival_t
                        modifier = esr_common_rival
                    }                    
                }


            }

            every_relation = {
                type = friend
                save_scope_as = friend_t
                limit = {
                    NOR = {
                        this = root 
                        has_relation_best_friend = scope:target
                    }
				}
                add_opinion = {
                    target = root
                    modifier = esr_friend_nemesis
                }
                root= {
                    add_opinion = {
                        target = scope:friend_t
                        modifier = esr_nemesis_friend
                    }                    
                }

            }

            every_relation = {
                type = best_friend
                save_scope_as = best_friend_t
                limit = {
					NOT = { this = root }
				}
                add_opinion = {
                    target = root
                    modifier = esr_best_friend_nemesis
                }
                root= {
                    add_opinion = {
                        target = scope:best_friend_t
                        modifier = esr_nemesis_best_friend
                    }                    
                }

            }
        }

        root = {
            every_relation = {
                type = rival
                save_scope_as = common_rival_r
                limit = {
                    NOR = {
                        this = scope:target
                        has_relation_nemesis = root
                    }   
				}

                add_opinion = {
                    target = scope:target
                    modifier = esr_common_rival
                }

                scope:target= {
                    add_opinion = {
                        target = scope:common_rival_r
                        modifier = esr_common_rival
                    }                    
                }


            }

            every_relation = {
                type = friend
                save_scope_as = friend_r
                limit = {
                    NOR = {
                        this = scope:target
                        has_relation_best_friend = root
                    }
				}
                add_opinion = {
                    target = scope:target
                    modifier = esr_friend_nemesis
                }
                scope:target= {
                    add_opinion = {
                        target = scope:friend_r
                        modifier = esr_nemesis_friend
                    }                    
                }

            }

            every_relation = {
                type = best_friend
                save_scope_as = best_friend_r
                limit = {
					NOT = { this = scope:target }
				}
                add_opinion = {
                    target = scope:target
                    modifier = esr_best_friend_nemesis
                }
                scope:target= {
                    add_opinion = {
                        target = scope:best_friend_r
                        modifier = esr_nemesis_best_friend
                    }                    
                }

            }
        }

    }  
}

esr_on_remove_nemesis = {
    effect = {
        #debug_log = "esr_on_remove_nemesis"
        #debug_log_scopes = yes
        scope:target = {
            every_relation = {
                type = rival
                save_scope_as = common_rival_t
                limit = {
                    NOR = {
                        this = root 
                        has_relation_nemesis = scope:target
                    }
				}
                remove_opinion = {
                    target = root
                    modifier = esr_common_rival
                    single = yes
                }
                root= {
                    remove_opinion = {
                        target = scope:common_rival_t
                        modifier = esr_common_rival
                        single = yes
                    }                    
                }


            }

            every_relation = {
                type = friend
                save_scope_as = friend_t
                limit = {
                    NOR = {
                        this = root 
                        has_relation_best_friend = scope:target
                    }
				}
                remove_opinion = {
                    target = root
                    modifier = esr_friend_nemesis
                    single = yes
                }
                root= {
                    remove_opinion = {
                        target = scope:friend_t
                        modifier = esr_nemesis_friend
                        single = yes
                    }                    
                }

            }

            every_relation = {
                type = best_friend
                save_scope_as = best_friend_t
                limit = {
					NOT = { this = root }
				}
                remove_opinion = {
                    target = root
                    modifier = esr_best_friend_nemesis
                    single = yes
                }
                root= {
                    remove_opinion = {
                        target = scope:best_friend_t
                        modifier = esr_nemesis_best_friend
                        single = yes
                    }                    
                }

            }
        }

        root = {
            every_relation = {
                type = rival
                save_scope_as = common_rival_r
                limit = {
                    NOR = {
                        this = scope:target
                        has_relation_nemesis = root
                    }          
				}
                remove_opinion = {
                    target = scope:target
                    modifier = esr_common_rival
                    single = yes
                }
                scope:target= {
                    remove_opinion = {
                        target = scope:common_rival_r
                        modifier = esr_common_rival
                        single = yes
                    }                    
                }


            }

            every_relation = {
                type = friend
                save_scope_as = friend_r
                limit = {
                    NOR = {
                        this = scope:target
                        has_relation_best_friend = root
                    }
				}
                remove_opinion = {
                    target = scope:target
                    modifier = esr_friend_nemesis
                    single = yes
                }
                scope:target= {
                    remove_opinion = {
                        target = scope:friend_r
                        modifier = esr_nemesis_friend
                        single = yes
                    }                    
                }

            }

            every_relation = {
                type = best_friend
                save_scope_as = best_friend_r
                limit = {
					NOT = { this = scope:target }
				}
                remove_opinion = {
                    target = scope:target
                    modifier = esr_best_friend_nemesis
                    single = yes
                }
                scope:target= {
                    remove_opinion = {
                        target = scope:best_friend_r
                        modifier = esr_nemesis_best_friend
                        single = yes
                    }                    
                }

            }
        }

    }  
}

esr_on_death_nemesis = {
    effect = {
        #debug_log = "esr_on_death_nemesis"
        #debug_log_scopes = yes
        scope:target = {
            every_relation = {
                type = rival
                save_scope_as = common_rival_t
                limit = {
                    NOR = {
                        this = root 
                        has_relation_nemesis = scope:target
                    }
				}
                remove_opinion = {
                    target = root
                    modifier = esr_common_rival
                    single = yes
                }
            }

            every_relation = {
                type = friend
                save_scope_as = friend_t
                limit = {
                    NOR = {
                        this = root 
                        has_relation_best_friend = scope:target
                    }
				}
                remove_opinion = {
                    target = root
                    modifier = esr_friend_nemesis
                    single = yes
                }
            }

            every_relation = {
                type = best_friend
                save_scope_as = best_friend_t
                limit = {
					NOT = { this = root }
				}
                remove_opinion = {
                    target = root
                    modifier = esr_best_friend_nemesis
                    single = yes
                }
            }
        }

        root = {
            every_relation = {
                type = rival
                save_scope_as = common_rival_r
                limit = {
                    NOR = {
                        this = scope:target
                        has_relation_nemesis = root
                    }          
				}
                remove_opinion = {
                    target = scope:target
                    modifier = esr_common_rival
                    single = yes
                }
            }

            every_relation = {
                type = friend
                save_scope_as = friend_r
                limit = {
                    NOR = {
                        this = scope:target
                        has_relation_best_friend = root
                    }
				}
                remove_opinion = {
                    target = scope:target
                    modifier = esr_friend_nemesis
                    single = yes
                }
            }

            every_relation = {
                type = best_friend
                save_scope_as = best_friend_r
                limit = {
					NOT = { this = scope:target }
				}
                remove_opinion = {
                    target = scope:target
                    modifier = esr_best_friend_nemesis
                    single = yes
                }
            }
        }

    }  
}

esr_on_become_lover_for_social_standing = {
    effect = {
        ### Add Social Standing Trait and exp
        if = {
            limit = {
                root = {
                    NOR = {
                        has_relation_friend = scope:target
                        has_relation_best_friend = scope:target
                    }
                }
            }
            scope:target = {
                save_scope_as = esr_perspective_char

                esr_set_track_variables = yes

                if = {
                    limit = {
                        OR = {
                            root.highest_held_title_tier >= tier_county
                            root = {
                                any_spouse = {
                                    highest_held_title_tier >= tier_county
                                }
                            }
                        }
                    }
                    esr_change_track_variable = {TRACK = esr_well_connected  VALUE = 6}
                }
                else = {
                    esr_change_track_variable = {TRACK = esr_well_connected  VALUE = 3}
                }
                if = {
                    limit = {
                        OR ={
                            root.highest_held_title_tier >= tier_duchy
                            root = {
                                any_spouse = {
                                    highest_held_title_tier >= tier_duchy
                                }
                            }
                        }
                    }
                    esr_change_track_variable = {TRACK = esr_friends_in_high_places  VALUE = root.esr_friends_in_high_places_exp_value}
                }
                if = {
                    limit = {
                        is_ruler = yes
                        is_independent_ruler = no
                        root.highest_held_title_tier >= tier_county
                        exists = top_liege
                        top_liege = {
                            any_vassal_or_below = {
                                this = root
                            }
                        }
                    }
                    esr_change_track_variable = {TRACK = esr_popular_among_peers  VALUE = root.esr_popular_among_peers_exp_value}
                }

                esr_update_social_standing_trait_effect = yes 
            }
            root = {
                save_scope_as = esr_perspective_char

                esr_set_track_variables = yes

                if = {
                    limit = {
                        OR = {
                            scope:target.highest_held_title_tier >= tier_county
                            scope:target = {
                                any_spouse = {
                                    highest_held_title_tier >= tier_county
                                }
                            }
                        }
                    }
                    esr_change_track_variable = {TRACK = esr_well_connected  VALUE = 6}
                }
                else = {
                    esr_change_track_variable = {TRACK = esr_well_connected  VALUE = 3}
                }
                if = {
                    limit = {
                        OR ={
                            scope:target.highest_held_title_tier >= tier_duchy
                            scope:target = {
                                any_spouse = {
                                    highest_held_title_tier >= tier_duchy
                                }
                            }
                        }
                    }
                    esr_change_track_variable = {TRACK = esr_friends_in_high_places  VALUE = scope:target.esr_friends_in_high_places_exp_value}
                }
                if = {
                    limit = {
                        is_ruler = yes
                        is_independent_ruler = no
                        scope:target.highest_held_title_tier >= tier_county
                        exists = top_liege
                        top_liege = {
                            any_vassal_or_below = {
                                this = scope:target
                            }
                        }
                    }
                    esr_change_track_variable = {TRACK = esr_popular_among_peers  VALUE = scope:target.esr_popular_among_peers_exp_value}
                }
                
                esr_update_social_standing_trait_effect = yes
            }
        }
        ### Add Social Standing Trait and exp/
    }
}

esr_on_become_soulmate_for_social_standing = {
    trigger = {
        NOT = {
            is_target_in_variable_list = {
                name = esr_lovers_list
                target = scope:target
            }
        }
    }

    on_actions = {
        esr_on_become_lover_for_social_standing        
    }
}

esr_on_remove_lover_for_social_standing = {
    effect = {
        ### Remove Social Standing exp
        if = {
            limit = {
                root = {
                    NOR = {
                        has_relation_friend = scope:target
                        has_relation_best_friend = scope:target
                    }
                }
            }
            scope:target = {
                save_scope_as = esr_perspective_char

                esr_set_track_variables = yes

                if = {
                    limit = {
                        OR = {
                            root.highest_held_title_tier >= tier_county
                            root = {
                                any_spouse = {
                                    highest_held_title_tier >= tier_county
                                }
                            }
                        }
                    }
                    esr_change_track_variable = {TRACK = esr_well_connected  VALUE = -6}
                }
                else = {
                    esr_change_track_variable = {TRACK = esr_well_connected  VALUE = -3}
                }
                if = {
                    limit = {
                        OR ={
                            root.highest_held_title_tier >= tier_duchy
                            root = {
                                any_spouse = {
                                    highest_held_title_tier >= tier_duchy
                                }
                            }
                        }
                    }
                    esr_change_track_variable = {TRACK = esr_friends_in_high_places  VALUE = root.esr_lost_friends_in_high_places_exp_value}
                }
                if = {
                    limit = {
                        is_ruler = yes
                        is_independent_ruler = no
                        root.highest_held_title_tier >= tier_county
                        exists = top_liege
                        top_liege = {
                            any_vassal_or_below = {
                                this = root
                            }
                        }
                    }
                    esr_change_track_variable = {TRACK = esr_popular_among_peers  VALUE = root.esr_lost_popular_among_peers_exp_value}
                }

                esr_update_social_standing_trait_effect = yes 
            }
            root = {
                save_scope_as = esr_perspective_char

                esr_set_track_variables = yes

                if = {
                    limit = {
                        OR = {
                            scope:target.highest_held_title_tier >= tier_county
                            scope:target = {
                                any_spouse = {
                                    highest_held_title_tier >= tier_county
                                }
                            }
                        }
                    }
                    esr_change_track_variable = {TRACK = esr_well_connected  VALUE = -6}
                }
                else = {
                    esr_change_track_variable = {TRACK = esr_well_connected  VALUE = -3}
                }
                if = {
                    limit = {
                        OR ={
                            scope:target.highest_held_title_tier >= tier_duchy
                            scope:target = {
                                any_spouse = {
                                    highest_held_title_tier >= tier_duchy
                                }
                            }
                        }
                    }
                    esr_change_track_variable = {TRACK = esr_friends_in_high_places  VALUE = scope:target.esr_lost_friends_in_high_places_exp_value}
                }
                if = {
                    limit = {
                        is_ruler = yes
                        is_independent_ruler = no
                        scope:target.highest_held_title_tier >= tier_county
                        exists = top_liege
                        top_liege = {
                            any_vassal_or_below = {
                                this = scope:target
                            }
                        }
                    }
                    esr_change_track_variable = {TRACK = esr_popular_among_peers  VALUE = scope:target.esr_lost_popular_among_peers_exp_value}
                }

                esr_update_social_standing_trait_effect = yes
            }
        }
        ### ### Remove Social Standing exp/        
    }
}

esr_on_death_lover_for_social_standing = {
    effect = {
        ### Remove Social Standing exp on death
        if = {
            limit = { # Prevent double removal for the death of friend - lovers (executed in esr_on_death_friend)
                root = {
                    NOR = {
                        has_relation_friend = scope:target
                        has_relation_best_friend = scope:target
                    }
                } 
            }
            scope:target = {
                save_scope_as = esr_perspective_char

                esr_set_track_variables = yes

                if = {
                    limit = {
                        OR = {
                            root.highest_held_title_tier >= tier_county
                            root = {
                                any_spouse = {
                                    highest_held_title_tier >= tier_county
                                }
                            }
                        }
                    }
                    esr_change_track_variable = {TRACK = esr_well_connected  VALUE = -6}
                }
                else = {
                    esr_change_track_variable = {TRACK = esr_well_connected  VALUE = -3}
                }
                if = {
                    limit = {
                        OR ={
                            root.highest_held_title_tier >= tier_duchy
                            root = {
                                any_spouse = {
                                    highest_held_title_tier >= tier_duchy
                                }
                            }
                        }
                    }
                    esr_change_track_variable = {TRACK = esr_friends_in_high_places  VALUE = root.esr_lost_friends_in_high_places_exp_value}
                }
                if = {
                    limit = {
                        is_ruler = yes
                        is_independent_ruler = no
                        root.highest_held_title_tier >= tier_county
                        exists = top_liege
                        top_liege = {
                            any_vassal_or_below = {
                                this = root
                            }
                        }
                    }
                    esr_change_track_variable = {TRACK = esr_popular_among_peers  VALUE = root.esr_lost_popular_among_peers_exp_value}
                }

                esr_update_social_standing_trait_effect = yes
            }
            root = {
                save_scope_as = esr_perspective_char

                esr_set_track_variables = yes

                if = {
                    limit = {
                        OR = {
                            scope:target.highest_held_title_tier >= tier_county
                            scope:target = {
                                any_spouse = {
                                    highest_held_title_tier >= tier_county
                                }
                            }
                        }
                    }
                    esr_change_track_variable = {TRACK = esr_well_connected  VALUE = -6}
                }
                else = {
                    esr_change_track_variable = {TRACK = esr_well_connected  VALUE = -3}
                }
                if = {
                    limit = {
                        OR ={
                            scope:target.highest_held_title_tier >= tier_duchy
                            scope:target = {
                                any_spouse = {
                                    highest_held_title_tier >= tier_duchy
                                }
                            }
                        }
                    }
                    esr_change_track_variable = {TRACK = esr_friends_in_high_places  VALUE = scope:target.esr_lost_friends_in_high_places_exp_value}
                }
                if = {
                    limit = {
                        is_ruler = yes
                        is_independent_ruler = no
                        scope:target.highest_held_title_tier >= tier_county
                        exists = top_liege
                        top_liege = {
                            any_vassal_or_below = {
                                this = scope:target
                            }
                        }
                    }
                    esr_change_track_variable = {TRACK = esr_popular_among_peers  VALUE = scope:target.esr_lost_popular_among_peers_exp_value}
                }

                esr_update_social_standing_trait_effect = yes
            }
        }
        ### Remove Social Standing exp on death/        
    }
}

esr_on_title_change = { # NOT USED: Breaked down into esr_on_title_gain and esr_on_title_lost
    effect = {
        #debug_log_scopes = yes
        ordered_held_title = {
            limit = {
                NOT = {this = scope:title}
            }
            order_by = tier
            save_scope_as = root_old_highest_title # for rank up only; for rank down it's actually current highest title
        }
        if = {
            limit = { # scope:root_old_highest_title is root's old highest title
                OR = {
                    NOT = {exists = scope:root_old_highest_title}
                    highest_held_title_tier > scope:root_old_highest_title.tier
                }
            }
            scope:root_old_highest_title = {
                save_scope_as = title
            }
            esr_on_rank_up_effect = yes

            if = {
                limit = {debug_only = yes}
                scope:title = {
                    debug_log = "[ROOT.Char.GetID] esr_ranked_up; scope:title: [THIS.Title.GetNameNoTooltip]"
                }
            }
        }
        else_if = { # scope:title is root's old highest title
            limit = {
                OR = {
                    NOT = {exists = scope:root_old_highest_title}
                    scope:root_old_highest_title.tier < scope:title.tier
                }
            } 
            esr_on_rank_down_effect = yes

            if = {
                limit = {debug_only = yes}
                scope:title = {
                    debug_log = "[ROOT.Char.GetID] esr_ranked_down; scope:title: [THIS.Title.GetNameNoTooltip]"
                }
            }
        }
    }
}

esr_on_title_gain = { # NOR USED: Buggy due to how on_title_gain is handled in vanilla (Bulk process); replaced by esr_on_rank_up
    effect = {
        #debug_log_scopes = yes
        ordered_held_title = {
            limit = {
                NOT = {this = scope:title}
            }
            order_by = tier
            save_scope_as = root_old_highest_title # for rank up only; for rank down it's actually current highest title
        }
        if = {
            limit = { # scope:root_old_highest_title is root's old highest title
                OR = {
                    NOT = {exists = scope:root_old_highest_title}
                    highest_held_title_tier > scope:root_old_highest_title.tier
                }
            }
            if = {
                limit = {exists = scope:root_old_highest_title}
                scope:root_old_highest_title = {
                    save_scope_as = title
                }
            }
            else = {
                clear_saved_scope = title
            }
            esr_on_rank_up_effect = yes

            if = {
                limit = {debug_only = yes}
                scope:title = {
                    debug_log = "[PREV.Char.GetID] esr_ranked_up; scope:title: [THIS.Title.GetNameNoTooltip]"
                }
            }
        }
    }
}

esr_on_title_lost = { # NOT USED: Buggy due to how on_title_gain is handled in vanilla (Bulk process); replaced by esr_on_rank_up
    effect = {
        #debug_log_scopes = yes
        ordered_held_title = {
            limit = {
                NOT = {this = scope:title}
            }
            order_by = tier
            save_scope_as = root_old_highest_title # for rank up only; for rank down it's actually current highest title
        }
        if = { # scope:title is root's old highest title
            limit = {
                OR = {
                    NOT = {exists = scope:root_old_highest_title}
                    #scope:root_old_highest_title.tier < scope:title.tier
                    highest_held_title_tier < scope:title.tier
                }
            } 
            esr_on_rank_down_effect = yes

            if = {
                limit = {debug_only = yes}
                scope:title = {
                    debug_log = "[PREV.Char.GetID] esr_ranked_down; scope:title: [THIS.Title.GetNameNoTooltip]"
                }
            }
        }
    }
}

esr_on_rank_up = { 
    effect = {
        esr_on_rank_up_effect = yes
    }
}

esr_on_rank_down = { 
    effect = {
        esr_on_rank_down_effect = yes
    }
}

esr_on_vassal_gained = {
    effect = {
        root = {
            if = {
                limit = {
                    NOT = {has_variable = esr_on_vassal_gained_blocker}
                    highest_held_title_tier >= tier_duchy
                }
                set_variable = {
					name = esr_on_vassal_gained_blocker
					days = 1
				}
                esr_popular_among_peers_update_effect = {TARGET = this}
            }
        }
        if = {
            limit = {
                exists = scope:old_liege
                scope:old_liege = {is_alive = yes}
            }
            scope:old_liege = {
                if = {
                    limit = {
                        NOT = {has_variable = esr_on_vassal_lost_blocker}
                        highest_held_title_tier >= tier_duchy
                    }
                    set_variable = {
                        name = esr_on_vassal_lost_blocker
                        days = 1
                    }
                    esr_popular_among_peers_update_effect = {TARGET = this}
                }
            }
        }
    }
}

esr_on_marriage = {
    effect = {
        if = {
            limit = {
                exists = root
                exists = scope:spouse
                OR = {
                    root.highest_held_title_tier >= tier_county
                    scope:spouse.highest_held_title_tier >= tier_county
                }
            }
            save_scope_value_as = {
                name = esr_is_marriage_or_divorce
                value = flag:marriage
            }
            if = {
                limit = {
                    root.highest_held_title_tier >= scope:spouse.highest_held_title_tier
                }
                esr_marriage_xp_update_effect = {HIGH_SPOUSE = root LOW_SPOUSE = scope:spouse TYPE = scope:esr_is_marriage_or_divorce}
            }
            else_if = {
                limit = {
                    root.highest_held_title_tier < scope:spouse.highest_held_title_tier
                }
                esr_marriage_xp_update_effect = {HIGH_SPOUSE = scope:spouse LOW_SPOUSE = root TYPE = scope:esr_is_marriage_or_divorce}
            }
        }
    }
}

esr_on_divorce = {
    effect = {
        if = {
            limit = {
                exists = root
                exists = scope:spouse
                OR = {
                    root.highest_held_title_tier >= tier_county
                    scope:spouse.highest_held_title_tier >= tier_county
                }
            }
            save_scope_value_as = {
                name = esr_is_marriage_or_divorce
                value = flag:divorce
            }
            if = {
                limit = {
                    root.highest_held_title_tier >= scope:spouse.highest_held_title_tier
                }
                esr_marriage_xp_update_effect = {HIGH_SPOUSE = root LOW_SPOUSE = scope:spouse TYPE = scope:esr_is_marriage_or_divorce}
            }
            else_if = {
                limit = {
                    root.highest_held_title_tier < scope:spouse.highest_held_title_tier
                }
                esr_marriage_xp_update_effect = {HIGH_SPOUSE = scope:spouse LOW_SPOUSE = root TYPE = scope:esr_is_marriage_or_divorce}
            }
        }
    }
}

esr_on_death_for_social_standing = {
    effect = {
        if = {
            limit = {
                exists = root
                highest_held_title_tier >= tier_county
            }
            every_spouse = {
                limit = {
                    highest_held_title_tier < root.highest_held_title_tier
                }
                save_scope_value_as = {
                    name = esr_is_marriage_or_divorce
                    value = flag:spouse_death
                }
                esr_marriage_xp_update_effect = {HIGH_SPOUSE = root LOW_SPOUSE = this TYPE = scope:esr_is_marriage_or_divorce}
            }
        }
    }
}

esr_yearly_playable_pulse = { # Remove esr_popular_among_peers track exp for independent characters
    trigger = {
        is_independent_ruler = yes
        has_variable = esr_popular_among_peers
        var:esr_popular_among_peers > 0
    }
    effect = {
        set_variable = {
            name = esr_popular_among_peers
            value = 0
        }
        esr_update_social_standing_trait_effect = yes
    }
}

esr_on_friend_to_rival = { #Trigger on_become_rival/nemesis
    trigger = {
        has_variable_list = esr_friends_list
        is_target_in_variable_list = {
            name = esr_friends_list
            target = scope:target
        }
        NAND = {
            has_variable_list = esr_best_friend_list
            is_target_in_variable_list = {
                name = esr_best_friend_list
                target = scope:target
            } 
        }
    }

    on_actions = {
        esr_record_on_remove_friend
        esr_on_remove_friend
        esr_on_remove_friend_for_social_standing
    }
}

esr_on_best_friend_to_rival = { #Trigger on_become_rival/nemesis
    trigger = {
        has_variable_list = esr_best_friend_list #Need to be made
        is_target_in_variable_list = {
            name = esr_best_friend_list
            target = scope:target
        }
    }

    on_actions = {
        esr_record_on_remove_best_friend #Need to be made
        esr_record_on_remove_friend
        esr_on_remove_best_friend
        esr_on_remove_friend_for_social_standing
    }
}

esr_on_rival_to_friend = { #Trigger on_become_friend/best_friend
    trigger = {
        has_variable_list = esr_rivals_list
        is_target_in_variable_list = {
            name = esr_rivals_list
            target = scope:target
        }
        NAND = {
            has_variable_list = esr_nemesis_list
            is_target_in_variable_list = {
                name = esr_nemesis_list
                target = scope:target
            }
        }
    }

    on_actions = {
        esr_record_on_remove_rival
        esr_on_remove_rival
        esr_on_remove_rival_for_social_standing
    }
}

esr_on_nemesis_to_friend = { #Trigger on_become_friend/best_friend
    trigger = {
        has_variable_list = esr_nemesis_list #Need to be made
        is_target_in_variable_list = {
            name = esr_nemesis_list
            target = scope:target
        }
    }

    on_actions = {
        esr_record_on_remove_nemesis #Need to be made
        esr_record_on_remove_rival
        esr_on_remove_nemesis
        esr_on_remove_rival_for_social_standing
    }
}

### Updating Mutual Acquaintances, Family Friends and Social Standing/

### Add war related opinions
esr_on_war_started = {

    effect = {

        scope:defender = {

            ### A chance for attacker and defender to progress towards rivals/nemesis when a war is declared (Modified by AI vengefulness)
            if = {
                limit = { can_set_relation_rival_if_adult_trigger = { CHARACTER = scope:attacker } }
                random = {
                    chance = 20
                    modifier = {
                        factor = 2
                        scope:defender = { ai_vengefulness >= medium_positive_vengefulness }
                    }
                    modifier = {
                        factor = 2.5
                        scope:defender = { ai_vengefulness >= high_positive_vengefulness }
                    }
                    progress_towards_rival_effect = {
                        CHARACTER = scope:attacker
                        REASON = esr_rival_declared_war_vengeful
                        OPINION = 0
                    }
                }
            }

            if = {
				limit = {
					can_set_relation_nemesis_trigger = { CHARACTER = scope:attacker }
					ai_vengefulness >= low_positive_vengefulness
				}
                random = {
                    chance = 20
                    modifier = {
                        factor = 0.5
                        scope:defender = { ai_vengefulness <= medium_negative_vengefulness }
                    }
                    modifier = {
                        factor = 2
                        scope:defender = { ai_vengefulness >= medium_positive_vengefulness }
                    }
                    modifier = {
                        factor = 2
                        scope:defender = { ai_vengefulness >= high_positive_vengefulness }
                    }
                    set_relation_nemesis = {
                        target = scope:attacker
                        reason = esr_rival_declared_war_vengeful
                    }
                }
			}

            ### I thought we are besties...
            if = {
                limit = {
                    OR = {
                        has_relation_friend = scope:attacker
                        has_relation_lover = scope:attacker
                    }
                }
                add_opinion = {
                    target = scope:attacker
                    modifier = betrayed_me_opinion
                    opinion = -60
                }
            }

            if = {
                limit = {
                    OR = {
                        has_relation_best_friend = scope:attacker
                        has_relation_soulmate = scope:attacker
                    }
                }
                add_opinion = {
                    target = scope:attacker
                    modifier = betrayed_me_opinion
                    opinion = -120
                }
            }


            ### Add opinion modifiers from the perspective of defender's relations.
            every_close_or_extended_family_member = {
                
                if = {
                    limit= {
                        NOR = {
                            this = scope:attacker 
                            has_relation_rival = prev
                            has_relation_nemesis = prev
                        }
                    }
                    
                    if = {
                        limit = {is_close_family_of = scope:defender}
                        add_opinion = {
                            target = scope:attacker
                            modifier = esr_declared_war_on_close_family
                        }   
                    }

                    else = {                       
                        add_opinion = {
                            target = scope:attacker
                            modifier = esr_declared_war_on_family
                        }
                    }

                }
            }

            every_spouse = {
                limit= {
                    NOR = {
                        this = scope:attacker 
                        has_relation_rival = prev
                        has_relation_nemesis = prev
                    }
                }
                add_opinion = {
                    target = scope:attacker
                    modifier = esr_declared_war_on_close_family
                }
            }

            every_relation = {
                type = friend

                limit = {
                    NOR = {
                        this = scope:attacker
                        has_relation_best_friend = scope:defender  
                    }
                }

                add_opinion = {
                    target = scope:attacker
                    modifier = esr_declared_war_on_friend
                }
            }

            every_relation = {
                type = rival

                limit = {
                    NOR = {
                        this = scope:attacker
                        has_relation_nemesis = scope:defender  
                    }
                }
                
                add_opinion = {
                    target = scope:attacker
                    modifier = esr_declared_war_on_rival
                }
            }

            every_relation = {
                type = lover

                limit = {
                    NOR = {
                        this = scope:attacker
                        has_relation_soulmate = scope:defender  
                    }
                }

                add_opinion = {
                    target = scope:attacker
                    modifier = esr_declared_war_on_lover
                }
            }

            every_relation = {
                type = nemesis
                limit = {NOT = {this = scope:attacker}}
                add_opinion = {
                    target = scope:attacker
                    modifier = esr_declared_war_on_nemesis
                }
            }

            every_relation = {
                type = best_friend
                limit = {NOT = {this = scope:attacker}}
                add_opinion = {
                    target = scope:attacker
                    modifier = esr_declared_war_on_best_friend
                }
            }

            every_relation = {
                type = soulmate
                limit = {NOT = {this = scope:attacker}}
                add_opinion = {
                    target = scope:attacker
                    modifier = esr_declared_war_on_soulmate
                }
            }
        }
    }
}

esr_on_join_war_as_secondary = {

    effect = {
        save_scope_as = newparty

        if = {
            limit = {
                scope:war = {is_attacker = scope:newparty}
            }

            ### Et tu, Brute?
            scope:war.primary_defender = {

                add_opinion = {
                    target = scope:newparty
                    modifier = esr_joined_war_on_me
                }

                if = {
                    limit = {
                        OR = {
                            has_relation_friend = scope:newparty
                            has_relation_lover = scope:newparty
                        }
                    }
                    add_opinion = {
                        target = scope:newparty
                        modifier = betrayed_me_opinion
                        opinion = -30
                    }
                }
    
                if = {
                    limit = {
                        OR = {
                            has_relation_best_friend = scope:newparty
                            has_relation_soulmate = scope:newparty
                        }
                    }
                    add_opinion = {
                        target = scope:newparty
                        modifier = betrayed_me_opinion
                        opinion = -60
                    }
                }

                ### People cared less about secondary attackers
                every_close_or_extended_family_member = {
                    limit= {
                        NOR = {
                            is_attacker_in_war = scope:war
                            has_relation_rival = prev
                            has_relation_nemesis = prev
                        }
                    }
                    add_opinion = {
                        target = scope:newparty
                        modifier = esr_joined_war_on_family
                    }
                }

                every_spouse = {
                    limit= {
                        NOR = {
                            is_attacker_in_war = scope:war
                            has_relation_rival = prev
                            has_relation_nemesis = prev
                        }
                    }
                    add_opinion = {
                        target = scope:newparty
                        modifier = esr_joined_war_on_family
                    }
                }
            }
        }
    }
}
# Maybe add opinion modifiers weighted by war score on_war_won_xxx, on_war_white_peace and on_war_invalidated (hard to balance + AI don't care)
### Add war related opinions/

### Remove imprisonment opinion modifiers from former prisoner's relations
esr_on_release_from_prison = {
    effect = {
		remove_opinion = {
			target = scope:imprisoner
			modifier = imprisoned_me
		}

        ### Victim's families
        every_spouse = {
			limit = {
				NOT = {
					this = scope:imprisoner
				}
			}
			add_to_temporary_list = close_family_imprison_opinion_list
			add_to_temporary_list = esr_spouse_imprison_opinion_list		
		}

		every_close_or_extended_family_member ={
			limit = {
				NOT = {
					this = scope:imprisoner
				}
			}
			add_to_temporary_list = close_family_imprison_opinion_list
			###Note close_family_imprison_opinion_list stores both close and extened family in esr
		}

		if = {
			limit = {
				any_in_list = {
					list = close_family_imprison_opinion_list
					always = yes
				}
			}
			every_in_list = {
				list = close_family_imprison_opinion_list
				custom = all_family_members

				if = {
					limit = {
						OR = {
							is_close_family_of = root
							is_in_list = esr_spouse_imprison_opinion_list
						}
					}
					remove_opinion = {
						target = scope:imprisoner
						modifier = esr_imprisoned_close_family
                        single = yes
					}					
				}

				else = {
					remove_opinion = {
						target = scope:imprisoner
						modifier = esr_imprisoned_extended_family
                        single = yes
					}		
				}
			}
		}

		#Victim's normal relations
		every_relation = {
			type = friend
			limit = {
				NOR = {
					this = root
					this = scope:imprisoner
					has_relation_best_friend = root				
				}
			}
			add_to_list = victim_close_relations_list
		}
		every_relation = {
			type = lover
			limit = {
				NOR = {
					this = root
					this = scope:imprisoner
					has_relation_soulmate = root					
					is_in_list = victim_close_relations_list
				}
			}
			add_to_list = victim_close_relations_list
		}
		if = {
			limit = {
				any_in_list = {
					list = victim_close_relations_list
					always = yes
				}
			}
			every_in_list = {
				list = victim_close_relations_list
				custom = all_friends_and_lovers
				remove_opinion = {
					target = scope:imprisoner
					modifier = esr_imprisoned_close_relation_opinion
                    single = yes
				}
			}
		}

		every_relation = {
			type = rival
			limit = {
				NOR = {
					this = scope:imprisoner
					has_relation_nemesis = root
					has_trait = just # Just, compassionate and forgiving chars won't cheer for their enemy's imprisonment
					has_trait = compassionate
					has_trait = forgiving
				}
			}
			custom = esr_rivals_execution
			remove_opinion = {
				target = scope:imprisoner
				modifier = esr_imprisoned_rival
                single = yes
			}
		}

		#Victim's lifelong relations
		every_relation = {
			type = best_friend
			limit = { NOT = {this = scope:imprisoner}}
			remove_opinion = {
				target = scope:imprisoner
				modifier = esr_imprisoned_best_friend
                single = yes
			}
		}

		every_relation = {
			type = soulmate
			limit = { NOT = {this = scope:imprisoner}}
			remove_opinion = {
				target = scope:imprisoner
				modifier = esr_imprisoned_soulmate
                single = yes
			}
		}

		every_relation = {
			type = nemesis

			limit = {
				NOR = {
					this = scope:imprisoner
					has_trait = just
					has_trait = compassionate
					has_trait = forgiving
				}
			}
			remove_opinion = {
				target = scope:imprisoner
				modifier = esr_imprisoned_nemesis
                single = yes
			}
		}
        
        # clear list just in case this is called multiple times (maybe group release? not tested)
        every_in_list = {
            list = close_family_imprison_opinion_list
            remove_from_list = close_family_imprison_opinion_list
        }
        every_in_list = {
            list = esr_spouse_imprison_opinion_list
            remove_from_list = esr_spouse_imprison_opinion_list
        }
        every_in_list = {
            list = victim_close_relations_list
            remove_from_list = victim_close_relations_list
        }
        # clear list just in case this is called multiple times/
    }
}
### Remove imprisonment opinion modifiers from former prisoner's relations/

### Add Family Friends opinion modifiers to new borns
esr_on_birth_child = {
    effect = {
        ### Check Family friend on child birth
        if = { #Update father side of the family friend (Need further testing to confirm whether it works when father is dead)(Might need to scope to a close family of scope:father in case he's dead)
            limit = {
                exists = scope:father
                scope:father = {
                    is_alive = yes
                    has_variable_list = family_friend_list
                }
            }
            scope:father = {
                every_in_list = {
                    variable =  family_friend_list #father's family friend
                    save_scope_as = potential_family_friend
                    scope:father = {
                        every_close_family_member = {
                            add_to_temporary_list = family_list_f
                        }
                        every_spouse = {
                            limit = {NOT = {is_in_list = family_list_f}}
                            add_to_temporary_list = family_list_f
                        }
                        add_to_temporary_list = family_list_f
                        every_in_list = {
                            list = family_list_f
                            esr_family_friend_add_effect = {TARGET = scope:potential_family_friend}
                        }
                    }
                }
            }
        }

        if = { #Update mother side of the family friend (always alive? Need testing for death_childbirth)
            limit = {
                scope:mother = {
                    has_variable_list = family_friend_list
                }
            }
            scope:mother = {
                every_in_list = {
                    variable =  family_friend_list #mother's family friend
                    save_scope_as = potential_family_friend
                    scope:mother = {
                        every_close_family_member = {
                            add_to_temporary_list = family_list_m
                        }
                        every_spouse = {
                            limit = {NOT = {is_in_list = family_list_m}}
                            add_to_temporary_list = family_list_m
                        }
                        add_to_temporary_list = family_list_m
                        every_in_list = {
                            list = family_list_m
                            esr_family_friend_add_effect = {TARGET = scope:potential_family_friend}
                        }
                    }
                }
            }
        }
    }
}
### Add Family Friends opinion modifiers to new borns/

### For threat experienced by neighbors

esr_on_war_started_for_threat = {
    effect = {
        scope:war = {
            if = {
                limit = {
                    #casus_belli = {
                        #any_target_title = {
                            #count >= 1
                        #}
                    #}
                    NOR = {
                        using_cb = fp3_free_house_member_cb
                        using_cb = excommunication_war
						using_cb = peasant_war
						using_cb = independence_faction_war
						using_cb = liberty_faction_war
						using_cb = populist_war
						using_cb = claimant_faction_war
						using_cb = depose_war
						using_cb = refused_liege_demand_war
						using_cb = independence_war
                        using_cb = fp2_expel_interloper
                        using_cb = artifact_war
                        using_cb = reclaim_relic_artifact_war
                        using_cb = remove_regent_cb
                        AND = {
                            has_global_variable = rv_is_loaded
                            OR = {
                                using_cb = rv_rescue_war
                                using_cb = rv_revenge_war
                            }
                        }
                    }
                }
                scope:attacker = {
                    if = {
                        limit = {
                            is_independent_ruler = no
                            exists = top_liege 
                        }
                        top_liege = {
                            every_vassal_or_below = {
                                limit = {
                                    NOR = {
                                        this = scope:attacker
                                        is_liege_or_above_of = scope:attacker
                                        is_vassal_or_below_of = scope:attacker
                                    }
                                    esr_is_threatened_trigger = yes
                                }
        
                                add_opinion = {
                                    target = scope:attacker
                                    modifier = esr_threatened_by_war_declared
                                    opinion = esr_threat_opinion_value
                                }

                                if = {
                                    limit = { always = no } #Disabled until needed for other features
                                    save_scope_as = esr_threatened_char
                                    scope:attacker = {
                                        if = {
                                            limit = {
                                                NOT = {
                                                    any_owned_story = {
                                                        story_type = esr_coalition_story
                                                    }
                                                }
                                            }
                                            create_story = {
                                                type = esr_coalition_story
                                                save_scope_as = esr_coalition_story
                                            }
                                            set_variable = {
                                                name = esr_coalition_story
                                                value = scope:esr_coalition_story
                                            }
                                            scope:esr_coalition_story = {
                                                add_to_variable_list = {
                                                    name = esr_threatened_chars
                                                    target = scope:esr_threatened_char
                                                }
                                            }
                                        }
                                        else = {
                                            random_owned_story = {
                                                limit = { 
                                                    story_type = esr_coalition_story
                                                }
                                                add_to_variable_list = {
                                                    name = esr_threatened_chars
                                                    target = scope:esr_threatened_char
                                                }
                                            }
                                        }
                                    }
                                }
                            }
        
                        }
                    }
        
                    every_neighboring_and_across_water_top_liege_realm_owner = {
                        add_to_temporary_list = esr_potential_threatened_list
        
                        #every_neighboring_and_across_water_top_liege_realm_owner = { #Very performance expensive
                            #esr_threat_evaluation_top_liege = yes
                        #}
                    }

                    scope:defender = {
                        every_neighboring_and_across_water_top_liege_realm_owner = {
                            add_to_temporary_list = esr_potential_threatened_list
                        }
                    }

                    every_in_list = {
                        list = esr_potential_threatened_list
                        esr_threat_evaluation_top_liege = yes
                    }
                }
            }
        }
    }
}

esr_remove_threat = {
    effect = {
        root = {
            if = {
                limit = {
                    has_opinion_modifier = {
                        modifier = esr_threatened_by_war_declared
                        target = scope:target
                    }
                }
                remove_opinion = {
                    target = scope:target
                    modifier = esr_threatened_by_war_declared
                } 
            }
        }

        scope:target = {
            if = {
                limit = {
                    has_opinion_modifier = {
                        modifier = esr_threatened_by_war_declared
                        target = root
                    }
                }
                remove_opinion = {
                    target = root
                    modifier = esr_threatened_by_war_declared
                } 
            }
        }
    }
}