carnal_court_enter_court_effect = {
    if = {
        limit = {
            scope:carn_sex_player.age >= 18
            scope:carn_sex_partner.age >= 18
        }
        # Saves player as ruler, since Royal Court does ask for that scope hardcoded.
        # Saves the liege as ruler when player is vassal and below king tier, since the game tries to open the liege's court.
        if = {
            limit = {
                has_royal_court = no
                is_independent_ruler = no
            }
            liege = { save_scope_as = ruler }
        }
        else = {
            save_scope_as = ruler
        }

        set_global_variable = {
            name = carnal_court_sex_animation_event
            value = yes
            days = 30 # So court won't break when something goes wrong
        }
        
        set_global_variable = {
            name = carn_sex_player
            value = scope:carn_sex_player
            days = 30 # So court won't break when something goes wrong
        }

        set_global_variable = {
            name = carn_sex_partner
            value = scope:carn_sex_partner
            days = 30 # So court won't break when something goes wrong
        }

        scope:carn_sex_player = {
            add_character_flag = {
                flag = sex_animation_event
                days = 30 # So court won't break when something goes wrong
            }
            carn_undress_character_effect = yes
        }

        scope:carn_sex_partner = {
            add_character_flag = {
                flag = sex_animation_event
                days = 30 # So court won't break when something goes wrong
            }
            carn_undress_character_effect = yes
        }

        scope:ruler = {
            carnal_court_evaluate_sex_location_culture_effect = yes
        }
    }
}

carnal_court_leave_court_effect = {
    scope:carn_sex_player = {
        carn_dress_character_effect = yes
        remove_character_flag = sex_animation_event
        remove_variable = sex_animation
        remove_variable = offset_xp255
        remove_variable = offset_yp255
        remove_variable = offset_zp255
        remove_variable = offset_xn255
        remove_variable = offset_yn255
        remove_variable = offset_zn255
        carn_sex_scene_clean_up_character_flags_effect = yes
    }
    scope:carn_sex_partner = {
        carn_dress_character_effect = yes
        remove_character_flag = sex_animation_event
        remove_variable = sex_animation
        remove_variable = offset_xp255
        remove_variable = offset_yp255
        remove_variable = offset_zp255
        remove_variable = offset_xn255
        remove_variable = offset_yn255
        remove_variable = offset_zn255
        carn_sex_scene_clean_up_character_flags_effect = yes
    }

    scope:ruler = {
        remove_character_flag = carnal_court_sex_western
        remove_character_flag = carnal_court_sex_mediterranean
        remove_character_flag = carnal_court_sex_mena
        remove_character_flag = carnal_court_sex_indian
    }

    carn_sex_scene_clean_up_flags_effect = yes
    remove_global_variable = sex_animation_event
    remove_global_variable = carn_sex_player
    remove_global_variable = carn_sex_partner

    close_view = {
        view = royal_court
        player = root
        secondary_actor = root
    }
}

carnal_court_sex_scene_effect = {
    save_scope_as = carn_sex_player
    $PARTNER$ = { save_scope_as = carn_sex_partner }
    scope:carn_sex_player = {
        carnal_court_enter_court_effect = yes
        trigger_event = { on_action = carnal_court_sex_scene }
    }
}
