﻿dual_cultivation_interaction = {
    icon = icon_relation
    category = interaction_category_friendly
    interface_priority = 80
    desc = dual_cultivation_interaction_desc
    use_diplomatic_range = no

    is_shown = {
        NOT = { scope:actor = scope:recipient }

        scope:actor = {
            is_adult = yes
            dynasty = { has_dynasty_perk = cultivation_legacy_3 }
            # Must have at least one cultivation trait
			OR = {    
				has_trait = cultivation_mortal
				has_trait = cultivation_qi_gathering
				has_trait = cultivation_qi_refining
				has_trait = cultivation_qi_building
				has_trait = cultivation_core_formation
				has_trait = cultivation_nascent_soul
				has_trait = cultivation_heavenly_being
				has_trait = cultivation_four_axis
				has_trait = cultivation_integration
				has_trait = cultivation_star_shattering
				has_trait = cultivation_sacred_vessel
				has_trait = cultivation_entering_nirvana
				has_trait = cultivation_true_immortal
			}

            OR = {
                scope:actor = {
                    carn_relationship_allows_free_sex_trigger = {
                        PARTNER = scope:recipient
                    }
                }
                scope:recipient = {
                    carn_is_working_as_prostitute_trigger = yes
                }
            }            

            NOR = {
                has_trait_with_flag = carn_block_send_sex_interaction
                has_character_flag = carn_block_send_sex_interaction
                has_opinion_modifier = {
                    target = scope:recipient
                    modifier = carn_block_sex_interaction_to_opinion
                }
            }
        }

        scope:recipient = { 
            is_adult = yes
            NOR = {
                has_trait_with_flag = carn_block_receive_sex_interaction
                has_character_flag = carn_block_receive_sex_interaction
                has_opinion_modifier = {
                    target = scope:actor
                    modifier = carn_block_sex_interaction_from_opinion
                }
            }
        }
    }
    
    cooldown = { months = dual_cultivation_cooldown }

    is_valid_showing_failures_only = {
        # For disabling interaction if they can't have sex
        scope:actor = {
            carn_can_have_sex_trigger = yes
        }
        scope:recipient = {
            carn_can_have_sex_trigger = yes
        }
        
        # Cannot dual cultivate if either has qi deviation
        scope:actor = {
            NOT = { has_character_modifier = qi_deviation }
        }
        scope:recipient = {
            NOT = { has_character_modifier = qi_deviation }
        }
    }

    # Cooldown between dual cultivation sessions
    cooldown = { months = 3 }

    on_accept = {
        scope:actor = {
            scope:recipient = {
                save_scope_as = carn_sex_partner_scope
            }

            # Add opinion modifiers
            add_opinion = {
                target = scope:recipient
                modifier = taught_me_cultivation_opinion
            }

            scope:recipient = {
                add_opinion = {
                    target = scope:actor
                    modifier = taught_them_cultivation_opinion
                }
                add_character_flag = {
                    flag = taught_cultivation_cooldown
                    years = 1
                }
            }
            
            # Apply dual cultivation effects
            dual_cultivation_effect = yes
            
            # Additional benefits based on realm compatibility
            if = {
                limit = {
                    # Same cultivation realm = bonus
                    scope:actor = {
                        has_trait = cultivation_mortal
                    }
                    scope:recipient = {
                        has_trait = cultivation_mortal
                    }
                }
                add_character_modifier = {
                    modifier = dual_cultivation_boost
                    months = 6
                }
                scope:recipient = {
                    add_character_modifier = {
                        modifier = dual_cultivation_boost
                        months = 6
                    }
                }
            }
            
            # Higher realm helping lower realm = teaching bonus
            if = {
                limit = {
                    scope:actor = {
                        OR = {
                            has_trait = cultivation_mortal
                            has_trait = unrestrained_realm
                            has_trait = immortal_realm
                        }
                    }
                    scope:recipient = {
                        OR = {
                            has_trait = cultivation_mortal
                            has_trait = core_formation
                        }
                    }
                }
                scope:recipient = {
                    # Double XP gain for lower realm partner
                    if = {
                        limit = { has_trait = cultivation_mortal }
                        add_cultivation_lifestyle_xp = medium_lifestyle_xp
                    }
                    else_if = {
                        limit = { has_trait = cultivation_mortal }
                        add_cultivation_lifestyle_xp = medium_lifestyle_xp
                    }
                }
            }
            
            # Stress relief
            stress_impact = {
                base = medium_stress_impact_loss
                lustful = minor_stress_impact_loss
                chaste = medium_stress_impact_gain
            }
            
            scope:recipient = {
                stress_impact = {
                    base = medium_stress_impact_loss
                    lustful = minor_stress_impact_loss
                    chaste = medium_stress_impact_gain
                }
            }

            hidden_effect = {

                # lovers and soulmates always consensual
                if = {
                    limit = {
                        scope:actor = {
                            OR = {
                                has_relation_lover = scope:recipient
                                has_relation_soulmate = scope:recipient
                            }
                        }
                    }
                    carn_sex_scene_is_consensual_effect = yes
                }
                # for slaves and concubines, get scenes ranging from consensual to noncon
                else_if = {
                    limit = {
                        scope:actor = {
                            OR = {
                                is_concubine_of = scope:recipient
                                has_relation_slave_owner = scope:recipient
                            }
                        }
                    }
                    # Consensual: I'm into you, and I either like you or you're super hot
                    if = {
                        limit = {
                            scope:recipient = {
                                OR = {
                                    opinion = {
                                        target = scope:actor
                                        value >= 50
                                    }
                                    scope:actor = { attraction >= 20 } # if you're hot enough they'll happily hatefuck you
                                }
                                is_attracted_to_gender_of = scope:actor
                            }
                        }
                        carn_sex_scene_is_consensual_effect = yes
                    }
                    # dubcon: Either I'm into you but not that into you, or I don't swing that way but I like you enough to humor you
                    else_if = {
                        limit = {
                            scope:recipient = {
                                OR = {
                                    AND = {
                                        is_attracted_to_gender_of = scope:actor
                                        OR = {
                                            opinion = {
                                                target = scope:actor
                                                value > 0
                                            }
                                            scope:actor = { attraction >= 10 } # if you're hot enough they'll be kinda ok with it
                                        }
                                    }
                                    AND = {
                                        NOT = { is_attracted_to_gender_of = scope:actor }
                                        opinion = {
                                            target = scope:actor
                                            value >= 75
                                        }
                                    }
                                }
                            }
                        }
                        carn_sex_scene_is_dubcon_effect = yes
				        carn_sex_scene_character_is_dom_player_effect = yes
                    }
                    # Noncon: I either hate you or I don't swing this way, get me outta here
                    else = {
                        carn_sex_scene_is_noncon_effect = yes
				        carn_sex_scene_character_is_dom_player_effect = yes
                    }
                }
                # get dubcon for characters that dislike you / are not attracted to you (but accept sex with you for some reason, like being a prostitute)
                else_if = {
                    limit = {
                        scope:recipient = {
                            OR = {
                                opinion = {
                                    target = scope:actor
                                    value < 0
                                }
                                NOT = { is_attracted_to_gender_of = scope:actor }
                            }
                        }
                    }
                    carn_sex_scene_is_dubcon_effect = yes					
                    carn_sex_scene_character_is_dom_player_effect = yes
                }
                # fallback to consensual scene
                else = {
                    carn_sex_scene_is_consensual_effect = yes
                }

				random_list = {
					60 = {
						carn_sex_scene_is_vaginal_effect = yes
						carn_sex_scene_is_cum_inside_effect = yes
					}
					20 = {
						carn_sex_scene_is_vaginal_effect = yes
						carn_sex_scene_is_cum_outside_effect = yes
					}
					10 = {
						trigger = { has_game_rule = carn_content_anal_enabled }
						carn_sex_scene_is_anal_effect = yes
					}
					10 = {
						carn_sex_scene_is_oral_effect = yes
					}
				}
				carn_sex_scene_character_is_giving_player_effect = yes
                # actually fire the sex scene
				carn_sex_scene_effect_v2 = {
						PARTNER = scope:recipient
				}
            }       
        }
    }

    auto_accept = no
    
    ai_accept = {
        base = 0
        
        modifier = {
            add = 50
            scope:recipient = {
                OR = {
                    has_relation_lover = scope:actor
                    has_relation_soulmate = scope:actor
                }
            }
        }
        
        modifier = {
            add = 30
            scope:recipient = {
                is_spouse_of = scope:actor
            }
        }
        
        modifier = {
            add = 20
            scope:recipient = {
                opinion = {
                    target = scope:actor
                    value >= 40
                }
            }
        }
        
        modifier = {
            add = 15
            scope:recipient = {
                has_trait = lustful
            }
        }
        
        modifier = {
            add = -50
            scope:recipient = {
                has_trait = chaste
            }
        }
        
        modifier = {
            add = -30
            scope:recipient = {
                opinion = {
                    target = scope:actor
                    value <= -20
                }
            }
        }
    }
}