###EVENTS FOR THE CHINESE GRACE DECISIONS###

#Written by:
#Mathilda Bjarnehed
#Milla Isaksson
#Joel Hansson
#Matthew Clohessy
# Alexander Oltner

namespace = JD

#GAINING GRACE

#Sending gift
character_event = {
    id = JD.20023
	has_dlc = "Jade Dragon"
    hide_window = yes
    is_triggered_only = yes
    immediate = { FROMFROM = { letter_event = { id = JD.20024 } } }
}


letter_event = {
    id = JD.20024
	has_dlc = "Jade Dragon"
    desc = EVTDESC_JD_20024
    border = GFX_event_letter_frame_diplomacy

    is_triggered_only = yes

    option = { 
        name = EVTOPTA_JD_20024
		
		sound_effect = china_grace_gain
		if = {
            limit = { NOT = { has_character_flag = jd_gifting_artifact } }
    		if = {
				limit = {
					OR={
						liked_by_offmap = {
						type = offmap_china
							context= wealth
						}
						liked_by_offmap = {
							type = offmap_china
						}
					}
					NOR={
						disliked_by_offmap = {
							type = offmap_china
						}
						disliked_by_offmap = {
							type = offmap_china
							context= wealth
						}
					}
				}
				add_offmap_currency = {
					offmap = offmap_china
					value = 450
				}
			}
			else_if = {
				limit = {
					OR={
						disliked_by_offmap = {
							type = offmap_china
							context = wealth
						}
						disliked_by_offmap = {
							type = offmap_china
						}
					}
					NOR={ 
						liked_by_offmap = {
							type = offmap_china
						}
						liked_by_offmap ={
							type = offmap_china
							context = wealth
						}
					}
				}
				add_offmap_currency = {
					offmap = offmap_china
					value = 150
				}
			}
			else ={
				add_offmap_currency = {
					offmap = offmap_china
					value = 300
				}
			}
        }
        clr_character_flag = jd_gifting_artifact
    }
}

#Asking to become tributary
character_event = {
    id = JD.20027
	has_dlc = "Jade Dragon"
    hide_window = yes
    is_triggered_only = yes
    immediate = { FROMFROM = { letter_event = { id = JD.20028 } } }
}

letter_event = {
    id = JD.20028
	has_dlc = "Jade Dragon"
    desc = EVTDESC_JD_20028
    border = GFX_event_letter_frame_diplomacy

    is_triggered_only = yes

    option = { 
        name = EVTOPTA_JD_20028
		
		sound_effect = china_grace_gain
		if = {
			limit = {
				OR={
					liked_by_offmap = {
					type = offmap_china
						context= tributary
					}
					liked_by_offmap = {
						type = offmap_china
					}
				}
				NOR={
					disliked_by_offmap = {
						type = offmap_china
					}
					disliked_by_offmap = {
						type = offmap_china
						context= tributary
					}
				}
			}
			add_offmap_currency = {
				offmap = offmap_china
				value = 400
			}
		}
		else_if = {
			limit = {
				OR={
					disliked_by_offmap = {
						type = offmap_china
						context = tributary
					}
					disliked_by_offmap = {
						type = offmap_china
					}
				}
				NOR={ 
					liked_by_offmap = {
						type = offmap_china
					}
					liked_by_offmap ={
						type = offmap_china
						context = tributary
					}
				}
			}
			add_offmap_currency = {
				offmap = offmap_china
				value = 100
			}
		}
		else ={
			add_offmap_currency = {
				offmap = offmap_china
				value = 200
			}
		}
    }
}

#SPENDING GRACE

#Chinese doctor
character_event = {
    id = JD.20021
	has_dlc = "Jade Dragon"
    hide_window = yes
    is_triggered_only = yes
    immediate = { 
        FROMFROM = { 
            random_list = {
                50 = { #Female
                    modifier = {
                        NOT = {
                            has_game_rule = {
                                name = gender
                                value = all
                            }
                        }
                        factor = 0.05
                    }
                    create_character = {
                        age = 35
                        dynasty = actually_culture
                        religion = ROOT
                        culture = ROOT
                        female = yes
                        health = 6
                        random_traits = no
                        attributes = {
                            martial = 2
                            diplomacy = 5
                            stewardship = 3
                            intrigue = 3
                            learning = 6
                        }
                    }
                }
                50 = { #Male
                    create_character = {
                        age = 35
                        dynasty = actually_culture
                        religion = ROOT
                        culture = ROOT
                        female = no
                        health = 6
                        random_traits = no
                        attributes = {
                            martial = 2
                            diplomacy = 5
                            stewardship = 3
                            intrigue = 3
                            learning = 6
                        }
                    }
                }
            }
            new_character = {
                #Education & other basic stuff
                save_event_target_as = chinese_doctor
				set_character_flag = ai_flag_refuse_concubinage
				set_character_flag = ai_flag_refuse_marriage
				set_character_flag = originated_from_chinese_court
				set_character_flag = chinese_courtier_original_court_@PREV
                set_character_flag = no_court_invites
                set_character_flag = is_chinese_physician
                random_list = {
                    50 = { add_trait = scholarly_theologian }
                    25 = { add_trait = mastermind_theologian }
                }
                add_trait = physician
                random_list = {
                    20 = { } #Nothing
                    30 = { add_trait = erudite }
                    30 = { add_trait = shrewd }
                    15 = { add_trait = quick }
                    5 = { add_trait = genius }
                }
                random_list = {
                    80 = { } #Nothing
                    2 = { add_trait = clubfooted }
                    2 = { add_trait = harelip }
                    2 = { add_trait = hunchback }
                    4 = { add_trait = lisp }
                    4 = { add_trait = stutter }
                    2 = { add_trait = ugly }
                    2 = { add_trait = dwarf }
                    2 = { add_trait = weak }
                }
                random_list = {
                    50 = { } #Nothing
                    25 = { add_trait = patient }
                    25 = { add_trait = diligent }
                }
                random = {
                    chance = 10
                    add_trait = homosexual
                }
                opinion = {
                    who = PREV
                    modifier = opinion_servant
                    months = 1200
                }

                #Add archetype 
                random_list = {
                    20 = { #Combat medic
                        set_character_flag = archetype_decisive
                        random_list = {
                            50 = { add_trait = duelist }
                            50 = { add_trait = scholar }
                        }
                        add_trait = scarred
                        add_trait = robust
                        random_list = {
                            60 = { } #Nothing
                            20 = { 
                                trigger = { has_dlc = Reapers }
                                add_trait = one_legged 
                            }
                            20 = { 
                                trigger = { has_dlc = Reapers }
                                add_trait = one_eyed 
                            }
                        }
                        add_trait = brave
                        random_list = {
                            30 = { } #Nothing
                            50 = { add_trait = cynical }
                            10 = {
                                add_trait = cynical
                                add_trait = depressed
                            }
                            10 = {
                                add_trait = cynical
                                add_trait = depressed
                                add_trait = drunkard
                            }
                        }
                        random_list = {
                            50 = { } #Nothing
                            20 = { add_trait = kind }
                            30 = { add_trait = honest }
                        }
                        remove_trait = dwarf
                        remove_trait = weak
                    }
                    #Dr Jerk
                    15 = { 
                        set_character_flag = archetype_decisive
                        add_trait = scholar
                        add_trait = wroth
                        remove_trait = patient
                        random_list = {
                            30 = { add_trait = proud }
                            30 = { add_trait = envious }
                            40 = { } #Nothing
                        }
                        add_trait = honest
                        add_trait = cynical
                        random = {
                            chance = 30
                            add_trait = drunkard
                        }
                        random_list = {
                            60 = { }
                            20 = { 
                                add_trait = clubfooted
                                set_name = Wu #House
                            }
                            20 = {
                                trigger = { has_dlc = Reapers }
                                add_trait = one_legged
                                set_name = Wu #House
                            }
                        }
                    }
                    10 = { #Mad genius 
                        set_character_flag = archetype_sociable
                        random_list = {
                            50 = { add_trait = impaler }
                            50 = { add_trait = mystic }
                        }
                        random_list = {
                            33 = { add_trait = drunkard } 
                            33 = { add_trait = lunatic }
                            33 = { add_trait = possessed }
                        }
                        add_trait = genius
                        remove_trait = quick
                        remove_trait = shrewd
                        add_trait = paranoid
                        random_list = {
                            25 = { add_trait = gluttonous } 
                            25 = { add_trait = wroth }
                            25 = { add_trait = proud }
                            25 = { add_trait = greedy }
                        }   
                    }
                    20 = { #Herbalist
                        set_character_flag = archetype_supportive
                        add_trait = gardener
                        add_trait = diligent
                        add_trait = humble
                        random_list = {
                            33 = { add_trait = content } 
                            33 = { add_trait = shy }
                            33 = { add_trait = poet }
                        }
                        random = {
                            chance = 50
                            add_trait = robust
                        }
                        remove_trait = weak
                    }
                    20 = { #Health nut
                        set_character_flag = archetype_conservative
                        add_trait = mystic
                        add_trait = celibate
                        add_trait = chaste
                        add_trait = temperate
                        random_list = {
                            50 = { add_trait = craven } 
                            25 = { add_trait = shy }
                            25 = { add_trait = content }
                        }
                        set_focus = focus_scholarship
                    }
                    20 = { #Compassionate doctor
                        set_character_flag = archetype_supportive
                        random_list = {
                            50 = { add_trait = socializer }
                            50 = { add_trait = scholar }
                        }
                        add_trait = kind
                        add_trait = patient
                        random_list = {
                            25 = { add_trait = content } 
                            25 = { add_trait = humble }
                            25 = { add_trait = trusting }
                            25 = { add_trait = charitable }
                        }
                        set_focus = focus_family
                    }
                    15 = { #McDreamy
                        set_character_flag = archetype_sociable
                        if = {
                            limit = { is_female = yes }
                            add_trait = seductress
                        }
                        if = {
                            limit = { is_female = no }
                            add_trait = seducer
                        }
                        random_list = {
                            50 = { } #Nothing
                            25 = { add_trait = poet }
                            25 = { add_trait = falconer }
                        }
                        random_list = {
                            30 = { add_trait = fair }
                            50 = { add_trait = strong }
                        }
                        add_trait = lustful
                        random_list = {
                            35 = { add_trait = ambitious }
                            35 = { add_trait = gregarious }
                            30 = { } #Nothing
                        }
                        set_focus = focus_seduction
                        remove_trait = clubfooted
                        remove_trait = harelip
                        remove_trait =  hunchback 
                        remove_trait = lisp
                        remove_trait = stutter
                        remove_trait = ugly
                        remove_trait = dwarf
                        remove_trait = weak
                    }
                }
				if = {
					limit = {
						offmap_china = {
							has_status = china_golden_age
						}
					}
					change_learning = 5
				}
            }
            letter_event = { id = JD.20022 } 
        } 
    }
}

#Governor presents doctor
letter_event = {
    id = JD.20022
	has_dlc = "Jade Dragon"
    desc = EVTDESC_JD_20022
    border = GFX_event_letter_frame_diplomacy

    hide_new = yes

    is_triggered_only = yes

    option = { 
        name = EVTOPTA_JD_20022
        custom_tooltip = { text = EVTOPTA_JD_20022_TT }
        event_target:chinese_doctor = {
            show_scope_change = no
            give_minor_title = title_court_physician
        }
    }
}


#Peace deal
character_event = {
    id = JD.20025
	has_dlc = "Jade Dragon"
    hide_window = yes
    is_triggered_only = yes
    immediate = { FROMFROM = { letter_event = { id = JD.20026 } } }
}

letter_event = {
    id = JD.20026
	has_dlc = "Jade Dragon"
    desc = EVTDESC_JD_20026
    border = GFX_event_letter_frame_diplomacy

    is_triggered_only = yes
    
    option = { 
        name = EVTOPTA_JD_20026

        add_character_modifier = {
            inherit = yes
            name = peace_deal_with_china
            years = 50
        }
    }
}

#Administrator (scholar-bureaucrat)
character_event = {
    id = JD.20029
	has_dlc = "Jade Dragon"
    hide_window = yes
    is_triggered_only = yes
    immediate = { 
        FROMFROM = { 
            random_list = {
                50 = { #Female
                    modifier = {
                        NOT = {
                            has_game_rule = {
                                name = gender
                                value = all
                            }
                        }
                        factor = 0.05
                    }
                    create_character = {
                        age = 30
                        dynasty = actually_culture
                        religion = ROOT
                        culture = ROOT
                        female = yes
                        health = 6
                        random_traits = no
                        attributes = {
                            martial = 2
                            diplomacy = 4
                            stewardship = 6
                            intrigue = 2
                            learning = 5
                        }
                    }
                }
                50 = { #Male
                    create_character = {
                        age = 30
                        dynasty = actually_culture
                        religion = ROOT
                        culture = ROOT
                        female = no
                        health = 6
                        random_traits = no
                        attributes = {
                            martial = 2
                            diplomacy = 4
                            stewardship = 6
                            intrigue = 2
                            learning = 5
                        }
                    }
                }
            }
            new_character = {
                #Education & other basic stuff
                save_event_target_as = chinese_administrator
                add_character_modifier = {
                    name = jd_administrator
                    duration = -1
                }
				set_character_flag = ai_flag_refuse_concubinage
				set_character_flag = ai_flag_refuse_marriage
				set_character_flag = originated_from_chinese_court
				set_character_flag = chinese_courtier_original_court_@PREV
                set_character_flag = no_court_invites
                set_character_flag = is_chinese_administrator
                random_list = {
                    30 = { add_trait = thrifty_clerk }
                    15 = { add_trait = fortune_builder }
                    5 = { add_trait = midas_touched }
                    30 = { add_trait = martial_cleric }
                    15 = { add_trait = scholarly_theologian }
                    5 = { add_trait = mastermind_theologian }
                }
                random_list = {
                    20 = { } #Nothing
                    30 = { add_trait = erudite }
                    25 = { add_trait = shrewd }
                    15 = { add_trait = quick }
                    5 = { add_trait = genius }
                    5 = { add_trait = fair }
                }
                random_list = {
                    85 = { } #Nothing
                    1 = { add_trait = clubfooted }
                    2 = { add_trait = harelip }
                    2 = { add_trait = hunchback }
                    3 = { add_trait = lisp }
                    3 = { add_trait = stutter }
                    2 = { 
                        add_trait = ugly 
                        remove_trait = fair 
                    }
                    1 = { add_trait = dwarf }
                    1 = {  add_trait = weak }
                }
                random = {
                    chance = 10
                    add_trait = homosexual
                }
                opinion = {
                    who = PREV
                    modifier = opinion_servant
                    months = 1200
                }

                #Add archetype 
                random_list = {
                    #Conservative 
                    25 = {
                        set_character_flag = archetype_conservative
                        if = {
                            limit = { has_dlc = "Way of Life" }
                            random_list = {
                                50 = { add_trait = scholar }
                                50 = { add_trait = administrator }
                            }
                        }
                        random = {
                            chance = 50
                            add_trait = zealous
                        }
                        add_trait = stubborn
                        add_trait = temperate
                        random_list = {
                            20 = { add_trait = greedy }
                            20 = { add_trait = wroth }
                            20 = { add_trait = deceitful }
                            20 = { add_trait = shy }
                            20 = { add_trait = cruel }
                        }
                        random_list = {
                            25 = { add_trait = poet }
                            25 = { add_trait = chaste }
                            25 = { 
                                add_trait = patient
                                remove_trait = wroth
                            }
                            25 = { add_trait = just }
                        }
                    }
                    #Supportive
                    25 = {
                        set_character_flag = archetype_supportive
                        if = {
                            limit = { has_dlc = "Way of Life" }    
                            random_list = {
                                50 = { add_trait = administrator }
                                25 = { add_trait = scholar }
                                25 = { add_trait = gardener }
                            }
                        }
                        add_trait = humble
                        add_trait = patient
                        random_list = {
                            33 = { add_trait = slothful }
                            33 = { add_trait = craven }
                            33 = { add_trait = shy }
                        }
                        random_list = {
                            25 = { add_trait = content }
                            25 = { add_trait = charitable }
                            25 = { add_trait = kind }
                            25 = { add_trait = trusting }
                        }      
                    }
                    #Decisive
                    25 = {
                        set_character_flag = archetype_decisive
                        if = {
                            limit = { has_dlc = "Way of Life" }
                            random_list = {
                                75 = { add_trait = administrator }
                                25 = { add_trait = architect }
                            }
                        }
                        add_trait = ambitious
                        add_trait = cynical
                        random_list = {
                            20 = { add_trait = cruel }
                            20 = { add_trait = stubborn }
                            20 = { add_trait = greedy }
                            20 = { add_trait = wroth }
                            20 = { add_trait = proud }
                        }
                        random_list = {
                            20 = { add_trait = falconer }
                            20 = { add_trait = diligent }
                            20 = { add_trait = brave }
                            20 = { add_trait = honest }
                            20 = { add_trait = gregarious }
                        }    
                    }
                    #Sociable
                    25 = {
                        set_character_flag = archetype_sociable
                        if = {
                            limit = { has_dlc = "Way of Life" }
                            random_list = {
                                25 = { add_trait = administrator }
                                30 = { add_trait = socializer }
                                25 = { add_trait = hedonist}
                                20 = {
                                    trigger = { is_female = yes }
                                    add_trait = seductress
                                    add_trait = lustful
                                }
                                20 = {
                                    trigger = { is_female = no }
                                    add_trait = seducer
                                    add_trait = lustful
                                }
                            }
                        }
                        add_trait = gregarious
                        add_trait = charitable
                        random_list = {
                            25 = { add_trait = craven }
                            25 = { add_trait = envious }
                            25 = { add_trait = gluttonous }
                            25 = { add_trait = proud }
                        }
                        random_list = {
                            25 = { add_trait = poet }
                            25 = { add_trait = kind }
                            25 = { add_trait = honest }
                            25 = { add_trait = lustful }
                        }   
                    }
                } 
            }
            letter_event = { id = JD.20030 } 
        } 
    }
}

#Governor presents administrator
letter_event = {
    id = JD.20030
	has_dlc = "Jade Dragon"
    desc = EVTDESC_JD_20030
    border = GFX_event_letter_frame_diplomacy

    hide_new = yes

    is_triggered_only = yes

    option = { 
        name = EVTOPTA_JD_20022
        custom_tooltip = { text = EVTOPTA_JD_20030_TT }
        event_target:chinese_administrator = {
            show_scope_change = no
            if = {
                limit = {
                    offmap_china = {
                        has_status = china_golden_age
                    }
                }
                set_character_flag = jd_administrator_golden_age #so you can't reappoint during golden age to get better modifier
            }
            give_minor_title = title_administrator #effects in minor title definition
        }
        tooltip = {
    		if = {
    			limit = {
    				offmap_china = {
    					has_status = china_golden_age
    				}
    			}
    			add_character_modifier = {
    				name = employing_jd_administrator_golden_age
    				duration = -1
    			}
    		}
    		else = {
    			add_character_modifier = {
    				name = employing_jd_administrator
    				duration = -1
    			}
    		}
        }
    }
}

character_event = {
    id = JD.9000
    hide_window = yes
    is_triggered_only = yes
	
	has_dlc = "Jade Dragon"
	
    immediate = { 
		character_event = { id = JD.9001 days = 1 }
	}
}

character_event = { # Reapply modifier on succession
    id = JD.9001
    hide_window = yes
    is_triggered_only = yes
	
	has_dlc = "Jade Dragon"
	
	trigger = {
		NOR = {
			has_character_modifier = employing_jd_administrator_golden_age
			has_character_modifier = employing_jd_administrator
		}
		any_courtier = {
			has_minor_title = title_administrator
		}
	}
	
    immediate = {
		if = {
    		limit = {
    			any_courtier_or_vassal = {
					has_minor_title = title_administrator
					has_character_flag = jd_administrator_golden_age
				}
    		}
    		add_character_modifier = {
    			name = employing_jd_administrator_golden_age
    			duration = -1
    		}
    	}
    	else_if = {
			limit = {
    			any_courtier_or_vassal = {
					has_minor_title = title_administrator
				}
    		}
    		add_character_modifier = {
    			name = employing_jd_administrator
    			duration = -1
    		}
    	}
	}
}


#Master engineer
character_event = {
    id = JD.20031
	has_dlc = "Jade Dragon"
    hide_window = yes
    is_triggered_only = yes
    immediate = { 
        FROMFROM = { 
            random_list = {
                50 = { #Female
                    modifier = {
                        NOT = {
                            has_game_rule = {
                                name = gender
                                value = all
                            }
                        }
                        factor = 0.05
                    }
                    create_character = {
                        age = 30
                        dynasty = actually_culture
                        religion = ROOT
                        culture = ROOT
                        female = yes
                        health = 6
                        random_traits = no
                        attributes = {
                            martial = 5
                            diplomacy = 2
                            stewardship = 6
                            intrigue = 3
                            learning = 3
                        }
                    }
                }
                50 = { #Male
                    create_character = {
                        age = 30
                        dynasty = actually_culture
                        religion = ROOT
                        culture = ROOT
                        female = no
                        health = 6
                        random_traits = no
                        attributes = {
                            martial = 5
                            diplomacy = 2
                            stewardship = 6
                            intrigue = 3
                            learning = 3
                        }
                    }
                }
            }
            new_character = {
                #Education & other basic stuff
                save_event_target_as = chinese_master_engineer
                add_character_modifier = {
                    name = jd_master_engineer
                    duration = -1
                }
				set_character_flag = ai_flag_refuse_concubinage
				set_character_flag = ai_flag_refuse_marriage
				set_character_flag = originated_from_chinese_court
				set_character_flag = chinese_courtier_original_court_@PREV
                set_character_flag = no_court_invites
                set_character_flag = is_chinese_master_engineer
                random_list = {
                    30 = { add_trait = thrifty_clerk }
                    15 = { add_trait = fortune_builder }
                    5 = { add_trait = midas_touched }
                    30 = { add_trait = tough_soldier }
                    15 = { add_trait = skilled_tactician }
                    5 = { add_trait = brilliant_strategist }
                }
                random_list = {
                    20 = { } #Nothing
                    20 = { add_trait = erudite }
                    20 = { add_trait = shrewd }
                    15 = { add_trait = quick }
                    5 = { add_trait = genius }
                    10 = { add_trait = robust }
                    5 = { add_trait = strong }
                    5 = { add_trait = fair }
                }
                random_list = {
                    85 = { } #Nothing
                    1 = { add_trait = clubfooted }
                    2 = { add_trait = harelip }
                    2 = { add_trait = hunchback }
                    3 = { add_trait = lisp }
                    3 = { add_trait = stutter }
                    2 = { 
                        add_trait = ugly 
                        remove_trait = fair 
                    }
                    1 = { add_trait = dwarf }
                    1 = { 
                        add_trait = weak 
                        remove_trait = strong 
                        remove_trait = robust
                    }
                }
                random = {
                    chance = 10
                    add_trait = homosexual
                }
                opinion = {
                    who = PREV
                    modifier = opinion_servant
                    months = 1200
                }

                #Add archetype 
                random_list = {
                    #Conservative
                    25 = {
                        set_character_flag = archetype_conservative
                        if = {
                            limit = { has_dlc = "Way of Life" }
                            random_list = {
                                75 = { add_trait = architect }
                                25 = { add_trait = theologian }
                            }
                        }
                        random = {
                            chance = 50
                            add_trait = zealous
                        }
                        add_trait = stubborn
                        add_trait = temperate
                        random_list = {
                            20 = { add_trait = greedy }
                            20 = { add_trait = wroth }
                            20 = { add_trait = deceitful }
                            20 = { add_trait = shy }
                            20 = { add_trait = cruel }
                        }
                        random_list = {
                            25 = { add_trait = poet }
                            25 = { add_trait = chaste }
                            25 = { 
                                add_trait = patient
                                remove_trait = wroth
                            }
                            25 = { add_trait = just }
                        }
                    }
                    #Supportive
                    25 = {
                        set_character_flag = archetype_supportive
                        if = {
                            limit = { has_dlc = "Way of Life" }    
                            random_list = {
                                50 = { add_trait = architect }
                                25 = { add_trait = socializer }
                                25 = { add_trait = gardener }
                            }
                        }
                        add_trait = humble
                        add_trait = patient
                        random_list = {
                            33 = { add_trait = slothful }
                            33 = { add_trait = craven }
                            33 = { add_trait = shy }
                        }
                        random_list = {
                            25 = { add_trait = content }
                            25 = { add_trait = charitable }
                            25 = { add_trait = kind }
                            25 = { add_trait = trusting }
                        }      
                    }
                    #Decisive
                    25 = {
                        set_character_flag = archetype_decisive
                        if = {
                            limit = { has_dlc = "Way of Life" }
                            random_list = {
                                50 = { add_trait = architect }
                                50 = { add_trait = strategist }
                            }
                        }
                        add_trait = ambitious
                        add_trait = cynical
                        random_list = {
                            20 = { add_trait = cruel }
                            20 = { add_trait = stubborn }
                            20 = { add_trait = greedy }
                            20 = { add_trait = wroth }
                            20 = { add_trait = proud }
                        }
                        random_list = {
                            20 = { add_trait = falconer }
                            20 = { add_trait = diligent }
                            20 = { add_trait = brave }
                            20 = { add_trait = honest }
                            20 = { add_trait = gregarious }
                        }    
                    }
                    #Sociable
                    25 = {
                        set_character_flag = archetype_sociable
                        if = {
                            limit = { has_dlc = "Way of Life" }
                            random_list = {
                                25 = { add_trait = architect }
                                30 = { add_trait = socializer }
                                25 = { add_trait = hedonist}
                                20 = {
                                    trigger = { is_female = yes }
                                    add_trait = seductress
                                    add_trait = lustful
                                }
                                20 = {
                                    trigger = { is_female = no }
                                    add_trait = seducer
                                    add_trait = lustful
                                }
                            }
                        }
                        add_trait = gregarious
                        add_trait = charitable
                        random_list = {
                            25 = { add_trait = craven }
                            25 = { add_trait = envious }
                            25 = { add_trait = gluttonous }
                            25 = { add_trait = proud }
                        }
                        random_list = {
                            25 = { add_trait = poet }
                            25 = { add_trait = kind }
                            25 = { add_trait = honest }
                            25 = { add_trait = lustful }
                        }   
                    }
                } 
            }
            letter_event = { id = JD.20032 } 
        } 
    }
}

#Governor presents master engineer
letter_event = {
    id = JD.20032
	has_dlc = "Jade Dragon"
    desc = EVTDESC_JD_20032
    border = GFX_event_letter_frame_diplomacy

    hide_new = yes

    is_triggered_only = yes

    option = { 
        name = EVTOPTA_JD_20022
        custom_tooltip = { text = EVTOPTA_JD_20032_TT }
        event_target:chinese_master_engineer = { 
            show_scope_change = no
            if = {
                limit = {
                    offmap_china = {
                        has_status = china_golden_age
                    }
                }
                set_character_flag = china_master_engineer_golden_age #so you can't reappoint in golden age to get better effects
            }
            set_character_flag = master_engineer_just_spawned
            give_minor_title = title_master_engineer #effects in minor title definition
        }
    }
}

#Master engineer builds something
character_event = {
    id = JD.20033
	has_dlc = "Jade Dragon"
    hide_window = yes
    is_triggered_only = yes

    trigger = {
        is_alive = yes
    }

    immediate = {
        #Renews the event
        if = {
            limit = {
                has_character_flag = china_master_engineer_golden_age
            }
            character_event = { id = JD.20033 days = 1460 random = 542 } 
        }
		else = {
            character_event = { id = JD.20033 days = 2920 random = 1095 } 
        }
 
        #Building 
        liege = {
            #Saves 3 provinces which are candidates for getting built in
            capital_scope = { 
                if = {
                    limit = {
                        NAND = {
                            has_province_modifier = jd_paper_mill
                            has_province_modifier = jd_fireworks_guild
                            has_province_modifier = jd_administrative_outpost
                            OR = {
                                has_province_modifier = jd_dry_docks
                                port = no
                            }
                            has_province_modifier = jd_blast_furnace
                        }
                    }
                    save_event_target_as = jd_me_capital 
                }
            }
            random_demesne_province = {
                limit = {
                    is_capital = no
                    NOR = {
                        has_province_modifier = jd_paper_mill
                        has_province_modifier = jd_fireworks_guild
                        has_province_modifier = jd_dry_docks
                        has_province_modifier = jd_administrative_outpost
                        has_province_modifier = jd_blast_furnace
                    }
                }
                save_event_target_as = jd_empty_province
            }
            random_demesne_province = {
                limit = {
                    is_capital = no
                    OR = {
                        has_province_modifier = jd_paper_mill
                        has_province_modifier = jd_fireworks_guild
                        has_province_modifier = jd_dry_docks
                        has_province_modifier = jd_administrative_outpost
                        has_province_modifier = jd_blast_furnace
                    }
                    NAND = {
                        has_province_modifier = jd_paper_mill
                        has_province_modifier = jd_fireworks_guild
                        has_province_modifier = jd_administrative_outpost
                        OR = {
                            has_province_modifier = jd_dry_docks
                            port = no
                        }
                        has_province_modifier = jd_blast_furnace
                    }
                }
                save_event_target_as = jd_built_province
            }
        }
        #Decide which place to build in
        random_list = {
            35 = { #Build in capital
                trigger = {  event_target:jd_me_capital = { always = yes  } }
                modifier = { NOT = { event_target:jd_me_capital = { has_province_modifier = jd_paper_mill } } factor = 1.1 }
                modifier = { NOT = { event_target:jd_me_capital = { has_province_modifier = jd_fireworks_guild } } factor = 1.1 }
                modifier = { NOT = { event_target:jd_me_capital = { has_province_modifier = jd_dry_docks } } factor = 1.1 }
                modifier = { NOT = { event_target:jd_me_capital = { has_province_modifier = jd_administrative_outpost } } factor = 1.1 }
                modifier = { NOT = { event_target:jd_me_capital = { has_province_modifier = jd_blast_furnace } } factor = 1.1 }

                event_target:jd_me_capital = { save_event_target_as = jd_build_province }
            }
            40 = { #Build in empty province
                trigger = {  event_target:jd_empty_province = { always = yes  } }
                modifier = { NOT = { event_target:jd_built_province = { always = yes } } factor = 1.6 }

                event_target:jd_empty_province = { save_event_target_as = jd_build_province }
            }
            25 = { #Build in a province which already has some stuff
                trigger = {  event_target:jd_built_province = { always = yes  } }
                modifier = { NOT = { event_target:jd_built_province = { has_province_modifier = jd_paper_mill } } factor = 1.1 }
                modifier = { NOT = { event_target:jd_built_province = { has_province_modifier = jd_fireworks_guild } } factor = 1.1 }
                modifier = { NOT = { event_target:jd_built_province = { has_province_modifier = jd_dry_docks } } factor = 1.1 }
                modifier = { NOT = { event_target:jd_built_province = { has_province_modifier = jd_administrative_outpost } } factor = 1.1 }
                modifier = { NOT = { event_target:jd_built_province = { has_province_modifier = jd_blast_furnace } } factor = 1.1 }
                modifier = { NOT = { event_target:jd_empty_province = { always = yes } } factor = 2.6 }

                event_target:jd_built_province = { save_event_target_as = jd_build_province }
            }
        }
        event_target:jd_build_province = {
            random_list = {
                20 = {
                    trigger = { NOT = { has_province_modifier = jd_paper_mill } }
                    owner = { character_event = { id = JD.20034 } }
                }
                20 = {
                    trigger = { NOT = { has_province_modifier = jd_fireworks_guild } }
                    owner = { character_event = { id = JD.20035 } }
                }
                20 = {
                    trigger = { 
                        NOT = { has_province_modifier = jd_dry_docks } 
                        port = yes
                    }
                    owner = { character_event = { id = JD.20036 } }
                }
                20 = {
                    trigger = { NOT = { has_province_modifier = jd_administrative_outpost } }
                    owner = { character_event = { id = JD.20037 } }
                }
                20 = {
                    trigger = { NOT = { has_province_modifier = jd_blast_furnace } }
                    owner = { character_event = { id = JD.20038 } }
                }
            }
        }
    }
}

#Announcement paper mill
character_event = {
    id = JD.20034
	has_dlc = "Jade Dragon"
    desc = EVTDESC_JD_20034
    picture = GFX_evt_bandits
    notification = yes

    is_triggered_only = yes

    immediate = {
        event_target:jd_build_province = {
            add_province_modifier = {
                name = jd_paper_mill
                years = 20
            }
			hidden_tooltip = {
				if = {
					limit = {
						has_dlc = "Reapers"
					}
					hidden_tooltip = {
						change_variable = { which = prosperity_value value = 6 }
					}
				}
			}
        }
    }

    option = { 

    }
}   


character_event = {
    id = JD.20035
	has_dlc = "Jade Dragon"
    desc = EVTDESC_JD_20035
    picture = GFX_evt_bandits
    notification = yes

    is_triggered_only = yes

    immediate = {
        event_target:jd_build_province = {
            add_province_modifier = {
                name = jd_fireworks_guild
                years = 20
            }
			hidden_tooltip = {
				if = {
					limit = {
						has_dlc = "Reapers"
					}
					hidden_tooltip = {
						change_variable = { which = prosperity_value value = 6 }
					}
				}
			}
        }
    }

    option = { 

    }
}   

character_event = {
    id = JD.20036
	has_dlc = "Jade Dragon"
    desc = EVTDESC_JD_20036
    picture = GFX_evt_bandits
    notification = yes

    is_triggered_only = yes

    immediate = {
        event_target:jd_build_province = {
            add_province_modifier = {
                name = jd_dry_docks
                years = 20
            }
			hidden_tooltip = {
				if = {
					limit = {
						has_dlc = "Reapers"
					}
					hidden_tooltip = {
						change_variable = { which = prosperity_value value = 6 }
					}
				}
			}
        }
    }

    option = { 

    }
}   

character_event = {
    id = JD.20037
	has_dlc = "Jade Dragon"
    desc = EVTDESC_JD_20037
    picture = GFX_evt_bandits
    notification = yes

    is_triggered_only = yes

    immediate = {
        event_target:jd_build_province = {
            add_province_modifier = {
                name = jd_administrative_outpost
                years = 20
            }
			hidden_tooltip = {
				if = {
					limit = {
						has_dlc = "Reapers"
					}
					hidden_tooltip = {
						change_variable = { which = prosperity_value value = 6 }
					}
				}
			}
        }
    }

    option = { 

    }
}   

character_event = {
    id = JD.20038
	has_dlc = "Jade Dragon"
    desc = EVTDESC_JD_20038
    picture = GFX_evt_bandits
    notification = yes

    is_triggered_only = yes

    immediate = {
        event_target:jd_build_province = {
            add_province_modifier = {
                name = jd_blast_furnace
                years = 20
            }
			hidden_tooltip = {
				if = {
					limit = {
						has_dlc = "Reapers"
					}
					hidden_tooltip = {
						change_variable = { which = prosperity_value value = 6 }
					}
				}
			}
        }
    }

    option = { 

    }
}   

#Strategist
character_event = {
    id = JD.20039
	has_dlc = "Jade Dragon"
    hide_window = yes
    is_triggered_only = yes
    immediate = { 
        FROMFROM = { 
            random_list = {
                50 = { #Female
                    modifier = {
                        NOT = {
                            has_game_rule = {
                                name = gender
                                value = all
                            }
                        }
                        factor = 0
                    }
                    create_character = {
                        age = 30
                        dynasty = actually_culture
                        religion = ROOT
                        culture = ROOT
                        female = yes
                        health = 7
                        random_traits = no
                        attributes = {
                            martial = 6
                            diplomacy = 2
                            stewardship = 3
                            intrigue = 4
                            learning = 2
                        }
                    }
                }
                50 = { #Male
                    create_character = {
                        age = 30
                        dynasty = actually_culture
                        religion = ROOT
                        culture = ROOT
                        female = no
                        health = 7
                        random_traits = no
                        attributes = {
                            martial = 6
                            diplomacy = 2
                            stewardship = 3
                            intrigue = 4
                            learning = 2
                        }
                    }
                }
            }
            new_character = {
                #Education & other basic stuff
                save_event_target_as = chinese_strategist
                add_character_modifier = {
                    name = jd_strategist
                    duration = -1
                }
				set_character_flag = ai_flag_refuse_concubinage
				set_character_flag = ai_flag_refuse_marriage
				set_character_flag = originated_from_chinese_court
				set_character_flag = chinese_courtier_original_court_@PREV
                set_character_flag = no_court_invites
                set_character_flag = is_chinese_strategist
                random_list = {
                    80 = { add_trait = skilled_tactician }
                    20 = { add_trait = brilliant_strategist }
                }
                random_list = {
                    20 = { add_trait = brave } 
                    20 = { add_trait = strong }
                    20 = { add_trait = shrewd }
                    40 = { add_trait = robust }
                }
                random_list = {
                    71 = { } #Nothing
                    20 = { add_trait = scarred }
                    5 = { 
                        trigger = { has_dlc = Reapers }
                        add_trait = one_eyed 
                        change_martial = 1
                    }
                    2 = { 
                        trigger = { has_dlc = Reapers }
                        add_trait = one_handed
                        change_martial = 1
                    }
                    2 = { add_trait = ugly }
                }
                random = {
                    chance = 10
                    add_trait = homosexual
                }
                opinion = {
                    who = PREV
                    modifier = opinion_servant
                    months = 1200
                }

                #Add archetype 
                random_list = {
                    #Conservative 
                    25 = {
                        set_character_flag = archetype_conservative
                        if = {
                            limit = { has_dlc = "Way of Life" }
                            add_trait = strategist
                        }
                        add_trait = logistics_expert
                        random = {
                            chance = 50
                            add_trait = zealous
                        }
                        add_trait = stubborn
                        add_trait = temperate
                        random_list = {
                            20 = { add_trait = greedy }
                            20 = { add_trait = wroth }
                            20 = { add_trait = deceitful }
                            20 = { add_trait = shy }
                            20 = { add_trait = cruel }
                        }
                        random_list = {
                            25 = { add_trait = poet }
                            25 = { add_trait = chaste }
                            25 = { 
                                add_trait = patient
                                remove_trait = wroth
                            }
                            25 = { add_trait = just }
                        }
                    }
                    #Supportive
                    25 = {
                        set_character_flag = archetype_supportive
                        if = {
                            limit = { has_dlc = "Way of Life" }    
                            random_list = {
                                50 = { add_trait = hunter }
                                50 = { add_trait = strategist }
                            }
                        }
                        add_trait = sapper 
                        add_trait = humble
                        add_trait = patient
                        random_list = {
                            30 = { add_trait = slothful }
                            70 = { add_trait = shy }
                        }
                        random_list = {
                            25 = { add_trait = content }
                            25 = { add_trait = charitable }
                            25 = { add_trait = kind }
                            25 = { add_trait = trusting }
                        }      
                    }
                    #Decisive
                    25 = {
                        set_character_flag = archetype_decisive
                        if = {
                            limit = { has_dlc = "Way of Life" }
                            random_list = {
                                50 = { add_trait = strategist }
                                50 = { add_trait = duelist }
                            }
                        }
                        add_trait = master_of_flame 
                        add_trait = ambitious
                        add_trait = cynical
                        random_list = {
                            20 = { add_trait = cruel }
                            20 = { add_trait = stubborn }
                            20 = { add_trait = greedy }
                            20 = { add_trait = wroth }
                            20 = { add_trait = proud }
                        }
                        random_list = {
                            20 = { add_trait = falconer }
                            20 = { add_trait = diligent }
                            20 = { add_trait = brave }
                            20 = { add_trait = honest }
                            20 = { add_trait = gregarious }
                        }    
                    }
                    #Sociable
                    25 = {
                        set_character_flag = archetype_sociable
                        if = {
                            limit = { has_dlc = "Way of Life" }
                            random_list = {
                                20 = { add_trait = duelist }
                                40 = { add_trait = hunter }
                                40 = { add_trait = gamer }
                            }
                        }
                        add_trait = levy_coordinator 
                        add_trait = gregarious
                        add_trait = charitable
                        random_list = {
                            33 = { add_trait = envious }
                            33 = { add_trait = gluttonous }
                            33 = { add_trait = proud }
                        }
                        random_list = {
                            25 = { add_trait = falconer }
                            25 = { add_trait = kind }
                            25 = { add_trait = honest }
                            25 = { add_trait = lustful }
                        }   
                    }
                }

                #Add second commander trait
                if = {
                    limit = { offmap_china = { has_status = china_golden_age } }
                    random_list = {
                        25 = {
                            trigger = { NOT = {trait = master_of_flame } }
                            add_trait = master_of_flame
                        }
                        25 = {
                            trigger = { NOT = {trait = logistics_expert } }
                            add_trait = logistics_expert
                        }
                        25 = {
                            trigger = { NOT = {trait = levy_coordinator } }
                            add_trait = levy_coordinator
                        }
                        25 = {
                            trigger = { NOT = {trait = sapper } }
                            add_trait = sapper
                        }
                    } 
					set_character_flag = jd_strategist_golden_age
                }
			}
            letter_event = { id = JD.20040 } 
        } 
    }
}

#Governor presents strategist
letter_event = {
    id = JD.20040
	has_dlc = "Jade Dragon"
    desc = EVTDESC_JD_20040
    border = GFX_event_letter_frame_diplomacy

    hide_new = yes

    is_triggered_only = yes

    option = { 
        name = EVTOPTA_JD_20022
        custom_tooltip = { text = EVTOPTA_JD_20040_TT }
        event_target:chinese_strategist = {
            show_scope_change = no
            set_character_flag = appear_in_option
            clr_character_flag = appear_in_option
            if = {
                limit = { 
                    can_hold_title = title_commander 
                    ROOT = { can_grant_title = title_commander }
                }
                give_minor_title = title_commander
            }
        }
    }
}




#Strategist has taught someone else, event for picking commander and trait (hidden)
character_event = {
    id = JD.20041
    hide_window = yes

    is_triggered_only = yes

    only_playable = yes
	
	has_dlc = "Jade Dragon"

    trigger = {
        any_courtier_or_vassal = { #Chinese commander
            has_character_modifier = jd_strategist

            liege = {
                any_courtier_or_vassal = { #Potential student commander
                    has_minor_title = title_commander
                    NOT = { has_character_modifier = jd_strategist }
                    can_have_more_leadership_traits = yes
                    NAND = { #They do not have all chinese leadership traits
                        trait = master_of_flame
                        trait = logistics_expert
                        trait = levy_coordinator
                        trait = sapper
                    }
                }
            }
        }
    }
	
	weight_multiplier = {
		days = 1
		modifier = {
			factor = 2
			any_courtier_or_vassal = {
				has_character_modifier = jd_strategist
				has_character_flag = jd_strategist_golden_age
			}
		}
	}

    immediate = {
        random_courtier_or_vassal = {
            limit = { 
                has_character_modifier = jd_strategist
            }
            save_event_target_as = commander_teacher
        }
        random_courtier_or_vassal = {
            limit = {
                has_minor_title = title_commander
                NOT = { has_character_modifier = jd_strategist }
                can_have_more_leadership_traits = yes
                NAND = { #They do not have all chinese leadership traits
                    trait = master_of_flame
                    trait = logistics_expert
                    trait = levy_coordinator
                    trait = sapper
                }
            }
            save_event_target_as = commander_student

            random_list = {
                25 = {
                    trigger = {
                        NOT = { trait = master_of_flame }
                    }
                    modifier = {
                        factor = 2
                        event_target:commander_teacher = { trait = master_of_flame }
                    }
                    set_character_flag = give_master_of_flame
                }
                25 = {
                    trigger = {
                        NOT = { trait = logistics_expert }
                    }
                    modifier = {
                        factor = 2
                        event_target:commander_teacher = { trait = logistics_expert }
                    }
                    set_character_flag = give_logistics_expert
                }
                25 = {
                    trigger = {
                        NOT = { trait = levy_coordinator }
                    }
                    modifier = {
                        factor = 2
                        event_target:commander_teacher = { trait = levy_coordinator }
                    }
                    set_character_flag = give_levy_coordinator
                }
                25 = {
                    trigger = {
                        NOT = { trait = sapper }
                    }
                    modifier = {
                        factor = 2
                        event_target:commander_teacher = { trait = sapper }
                    }
                    set_character_flag = give_sapper
                }
            }
            character_event = { id = JD.20042 }
        }
    }
}

#Ping event
character_event = {
    id = JD.20042
	has_dlc = "Jade Dragon"
    hide_window = yes
    is_triggered_only = yes
    immediate = { 
    	FROM = { character_event = { id = JD.20043 } }
    	character_event = { id = JD.20044 days = 1 } #clear flag
    }
}

#Handing out trait and notifying liege
character_event = {
    id = JD.20043
	has_dlc = "Jade Dragon"
    picture = GFX_evt_china_invades
    border = GFX_event_normal_frame_war
    notification = yes

    is_triggered_only = yes

    desc = {
        trigger = { event_target:commander_student = { has_character_flag = give_master_of_flame } }
        text = EVTDESC_JD_20041_A
    }
    desc = {
        trigger = { event_target:commander_student = { has_character_flag = give_logistics_expert } }
        text = EVTDESC_JD_20041_B
    }
    desc = {
        trigger = { event_target:commander_student = { has_character_flag = give_levy_coordinator } }
        text = EVTDESC_JD_20041_C
    }
    desc = {
        trigger = { event_target:commander_student = { has_character_flag = give_sapper } }
        text = EVTDESC_JD_20041_D
    }

    option = { 

        event_target:commander_student = {
			set_character_flag = taught_chinese_strategy
            trigger_switch = {
                on_trigger = has_character_flag
                give_master_of_flame    = { add_trait = master_of_flame }
                give_logistics_expert   = { add_trait = logistics_expert }
                give_levy_coordinator   = { add_trait = levy_coordinator }
                give_sapper             = { add_trait = sapper }
            }
        }
    }
}

#Clear flag separately so desc works for notification
character_event = {
	id = JD.20044
	has_dlc = "Jade Dragon"
	is_triggered_only = yes
	hide_window = yes
	immediate = {
		clr_character_flag = give_master_of_flame
		clr_character_flag = give_logistics_expert
		clr_character_flag = give_levy_coordinator
		clr_character_flag = give_sapper
	}
}


###Imperial marriage

#Randomizing the spouse 
character_event = {
    id = JD.20045
	has_dlc = "Jade Dragon"
    hide_window = yes
    is_triggered_only = yes
    immediate = { 

        FROMFROMFROM  = { save_event_target_as = imperial_marriage_spouse_1 }

        offmap_china = {
            offmap_ruler = { 
                ROOT = {
                    if = {
                        limit = { event_target:imperial_marriage_spouse_1 = { is_female = yes } }
                        create_character = {
                            age = 16
                            dynasty = PREV
                            religion = PREV
                            culture = PREV
                            female = no
                            health = 6
                            random_traits = no
                            attributes = {
                                martial = 4
                                diplomacy = 5
                                stewardship = 4
                                intrigue = 4
                                learning = 5
                            }
                        }
                    }
                    if = {
                        limit = { event_target:imperial_marriage_spouse_1 = { is_female = no } }
                        create_character = {
                            age = 16
                            dynasty = PREV
                            religion = PREV
                            culture = PREV
                            female = yes
                            health = 6
                            random_traits = no
                            attributes = {
                                martial = 4
                                diplomacy = 5
                                stewardship = 4
                                intrigue = 4
                                learning = 5
                            }
                        }
                    }
                    new_character = {
						set_character_flag = always_show_in_marriage_selection
                        save_event_target_as = imperial_marriage_spouse_2
                        add_trait = cannot_marry #Is removed upon marriage
						set_character_flag = ai_flag_refuse_concubinage
						set_character_flag = ai_flag_refuse_marriage

                        set_character_flag = chinese_honor_guard
						
						set_character_flag = originated_from_chinese_court
						set_character_flag = chinese_courtier_original_court_@PREV
						set_character_flag = is_chinese_imperial_marriage
						
						add_trait = child_of_consort
						
						if = {
							limit = {
								event_target:imperial_marriage_spouse_1 = {
									religion = hindu
								}
							}
							add_trait = kshatriya
						}

                        PREVPREV = { #Emperor/Empress
							trigger_switch = {
								on_trigger = is_female
								yes = {
									if = { #If Empress has father make them siblings
										limit = {
											father_even_if_dead = {
												could_be_parent_of = event_target:imperial_marriage_spouse_2
											}
										}
										father_even_if_dead = { save_event_target_as = imperial_marriage_parent }
									}
									else_if = { #Else if Emperor has a grandfather make them siblings
										limit = {
											father_even_if_dead = {
												father_even_if_dead = {
													could_be_parent_of = event_target:imperial_marriage_spouse_2
												}
											}
										}
										father_even_if_dead = {
											father_even_if_dead = {
												save_event_target_as = imperial_marriage_parent
											}
										}
									}
									else_if = {
										limit = {
											any_dynasty_member_even_if_dead  = {
												is_or_was_offmap_power_ruler = yes
												could_be_parent_of = event_target:imperial_marriage_spouse_2
											}
										}
										random_dynasty_member_even_if_dead = {
											limit = {
												is_or_was_offmap_power_ruler = yes
												could_be_parent_of = event_target:imperial_marriage_spouse_2
											}
											save_event_target_as = imperial_marriage_parent
										}
									}
									else_if = {
										limit = {
											any_dynasty_member_even_if_dead  = {
												could_be_parent_of = event_target:imperial_marriage_spouse_2
											}
										}
										random_dynasty_member_even_if_dead = {
											limit = {
												could_be_parent_of = event_target:imperial_marriage_spouse_2
											}
											save_event_target_as = imperial_marriage_parent
										}
									}
									else_if = { #Else make a new father to make them siblings
										limit = {
											father_even_if_dead = {
												always = no
											}
										}
										create_character = {
											age = 60
											dynasty = PREV
											religion = PREV
											culture = PREV
										}
										new_character = { 
											death = { death_reason = death_in_china_historic }
											save_event_target_as = imperial_marriage_parent
										}
										set_father = event_target:imperial_marriage_parent
									}
								}
								no = {
									if = { #If Emperor is 32+ and the imperial target is female then make it his child
										limit = {
											could_be_parent_of = event_target:imperial_marriage_spouse_2
											event_target:imperial_marriage_spouse_2 = {
												is_female = yes
											}
										}
										save_event_target_as = imperial_marriage_parent
									}
									else_if = { #Else if Emperor has a father make them siblings
										limit = {
											father_even_if_dead = {
												could_be_parent_of = event_target:imperial_marriage_spouse_2
											}
										}
										father_even_if_dead = {
											save_event_target_as = imperial_marriage_parent
										}
									}
									else_if = { #Else if Emperor has a grandfather make them siblings
										limit = {
											father_even_if_dead = {
												father_even_if_dead = {
													could_be_parent_of = event_target:imperial_marriage_spouse_2
												}
											}
										}
										father_even_if_dead = {
											father_even_if_dead = {
												save_event_target_as = imperial_marriage_parent
											}
										}
									}
									else_if = {
										limit = {
											any_dynasty_member_even_if_dead = {
												is_or_was_offmap_power_ruler = yes
												could_be_parent_of = event_target:imperial_marriage_spouse_2
											}
										}
										random_dynasty_member_even_if_dead = {
											limit = {
												is_or_was_offmap_power_ruler = yes
												could_be_parent_of = event_target:imperial_marriage_spouse_2
											}
											save_event_target_as = imperial_marriage_parent
										}
									}
									else_if = {
										limit = {
											any_dynasty_member_even_if_dead = {
												could_be_parent_of = event_target:imperial_marriage_spouse_2
											}
										}
										random_dynasty_member_even_if_dead = {
											limit = {
												could_be_parent_of = event_target:imperial_marriage_spouse_2
											}
											save_event_target_as = imperial_marriage_parent
										}
									}
									else_if = { #Else make a new father to make them siblings
										limit = {
											father_even_if_dead = {
												always = no
											}
										}
										create_character = {
											age = 60
											dynasty = PREV
											religion = PREV
											culture = PREV
										}
										new_character = { 
											death = { death_reason = death_in_china_historic }
											save_event_target_as = imperial_marriage_parent
										}
										set_father = event_target:imperial_marriage_parent
									}
								}
                            }
							if = {
								limit = {
									event_target:imperial_marriage_parent = {
										is_female = no
									}
								}
								event_target:imperial_marriage_spouse_2 = { set_father = event_target:imperial_marriage_parent }
							}
							if = {
								limit = {
									event_target:imperial_marriage_parent = {
										is_female = yes
									}
								}
								event_target:imperial_marriage_spouse_2 = { set_mother = event_target:imperial_marriage_parent }
							}
                        }

                        #Positive genetic trait
                        random_list = {
                            60 = { 
								trigger = { 
									NOT = { offmap_china = { has_status = china_golden_age } } 
								}  
								#Nothing 
							}
                            15 = { add_trait = fair }
                            5 = { add_trait = strong }
                            10 = { add_trait = quick }
                            5 = { add_trait = genius }
                        }

                        #Negative genetic trait
						random_list = {
							90 = { } #Nothing
							1 = { add_trait = clubfooted }
							1 = { add_trait = harelip }
							1 = { add_trait = hunchback }
							1 = { add_trait = lisp }
							2 = { add_trait = stutter }
							1 = { 
								remove_trait = fair
								add_trait = ugly 
							}
							1 = { add_trait = dwarf }
							1 = { 
								remove_trait = genius
								remove_trait = quick
								add_trait = slow 
							}
							1 = { 
								remove_trait = strong
								add_trait = weak 
							}
						}

                        random = {
                            chance = 5
                            add_trait = homosexual
                        }

                        #Add archetype 1 (personality: conservative, supportive, decisive, sociable)
                        random_list = {
                            #Conservative 
                            25 = {
                                set_character_flag = archetype_conservative
                                
                                random = {
                                    chance = 50
                                    add_trait = zealous
                                }
                                add_trait = temperate
                                random_list = {
                                    20 = { add_trait = greedy }
                                    20 = { add_trait = wroth }
                                    20 = { add_trait = deceitful }
                                    20 = { add_trait = stubborn }
                                    20 = { add_trait = cruel }
                                }
                                random_list = {
                                    25 = { add_trait = poet }
                                    25 = { add_trait = chaste }
                                    25 = { 
                                        add_trait = patient
                                        remove_trait = wroth
                                    }
                                    25 = { add_trait = just }
                                }
                            }
                            #Supportive
                            25 = {
                                set_character_flag = archetype_supportive
                                
                                add_trait = humble
                                add_trait = patient
                                random_list = {
                                    30 = { add_trait = slothful }
                                    70 = { add_trait = shy }
                                }
                                random_list = {
                                    25 = { add_trait = content }
                                    25 = { add_trait = charitable }
                                    25 = { add_trait = kind }
                                    25 = { add_trait = trusting }
                                }      
                            }
                            #Decisive
                            25 = {
                                modifier = {
                                    is_female = yes
                                    NOT = {
                                        has_game_rule = {
                                            name = gender
                                            value = all
                                        }
                                    }
                                    factor = 0.5
                                }
                                set_character_flag = archetype_decisive
                                
                                add_trait = ambitious
                                add_trait = cynical
                                random_list = {
                                    20 = { add_trait = cruel }
                                    20 = { add_trait = stubborn }
                                    20 = { add_trait = greedy }
                                    20 = { add_trait = wroth }
                                    20 = { add_trait = proud }
                                }
                                random_list = {
                                    20 = { add_trait = falconer }
                                    20 = { add_trait = diligent }
                                    20 = { add_trait = brave }
                                    20 = { add_trait = honest }
                                    20 = { add_trait = gregarious }
                                }    
                            }
                            #Sociable
                            25 = {
                                modifier = {
                                    is_female = yes
                                    NOT = {
                                        has_game_rule = {
                                            name = gender
                                            value = all
                                        }
                                    }
                                    factor = 0.7
                                }
                                set_character_flag = archetype_sociable
                                
                                add_trait = gregarious
                                add_trait = charitable
                                random_list = {
                                    25 = { add_trait = envious }
                                    25 = { add_trait = gluttonous }
                                    25 = { add_trait = proud }
                                    25 = { add_trait = slothful }
                                }
                                random_list = {
                                    33 = { add_trait = kind }
                                    33 = { add_trait = honest }
                                    33 = { add_trait = lustful }
                                }   
                            }
                        }

                        #Set archetype 2 (lifestyle)
                        random_list = {
                            #War
                            15 = {
                                modifier = {
                                    is_female = yes
                                    NOT = {
                                        has_game_rule = {
                                            name = gender
                                            value = all
                                        }
                                    }
                                    factor = 0.1
                                }
                                modifier = {
                                    has_character_flag = archetype_decisive
                                    factor = 2
                                }
                                #Set education
                                random_list = {
                                    30 = { add_trait = tough_soldier }
                                    50 = { add_trait = skilled_tactician }
                                    20 = { add_trait = brilliant_strategist }
                                }
                                #Set appropriate traits
                                random_list = {
                                    33 = {
                                        if = {
                                            limit = { has_dlc = "Way of Life" }
                                            add_trait = duelist
                                        }
                                        remove_trait = craven
                                        add_trait = brave
                                        if = {
                                            limit = { is_strong_trigger = no }
                                            add_trait = robust
                                        }
                                    }
                                    33 = {
                                        if = {
                                            limit = { has_dlc = "Way of Life" }
                                            add_trait = hunter
                                        }
                                        add_trait = falconer
                                        if = {
                                            limit = { is_strong_trigger = no }
                                            add_trait = robust
                                        }
                                    }
                                    33 = {
                                        if = {
                                            limit = { has_dlc = "Way of Life" }
                                            add_trait = strategist
                                        }
                                        if = {
                                            limit = { is_smart_trigger = no }
                                            add_trait = shrewd
                                        }
                                    }
                                }      
                            }

                            #Diplomacy
                            30 = {
                                modifier = {
                                    has_character_flag = archetype_supportive
                                    factor = 1.5
                                }
                                modifier = {
                                    has_character_flag = archetype_sociable
                                    factor = 2
                                }
                                #Set education
                                random_list = {
                                    30 = { add_trait = underhanded_rogue }
                                    50 = { add_trait = charismatic_negotiator }
                                    20 = { add_trait = grey_eminence }
                                }
                                random = {
                                    chance = 30
                                    add_trait = arbitrary
                                }
                                #Set appropriate traits
                                random_list = {
                                    33 = {
                                        if = {
                                            limit = { has_dlc = "Way of Life" }
                                            add_trait = hedonist
                                        }
                                        remove_trait = chaste
                                        add_trait = lustful
                                    }
                                    33 = {
                                        if = {
                                            limit = { has_dlc = "Way of Life" }
                                            add_trait = socializer
                                        }
                                        add_trait = gregarious
                                        remove_trait = shy
                                    }
                                    33 = {
                                        if = {
                                            limit = { has_dlc = "Way of Life" }
                                            add_trait = gamer
                                        }
                                        if = {
                                            limit = { is_smart_trigger = no }
                                            add_trait = shrewd
                                        }
                                    }
                                }      
                            }

                            #Intrigue
                            10 = {
                                modifier = {
                                    has_character_flag = archetype_supportive
                                    factor = 0.5
                                }
                                modifier = {
                                    has_character_flag = archetype_decisive
                                    factor = 1.5
                                }
                                modifier = {
                                    has_character_flag = archetype_sociable
                                    factor = 1.5
                                }
                                #Set education
                                random_list = {
                                    30 = { add_trait = flamboyant_schemer }
                                    50 = { add_trait = intricate_webweaver }
                                    20 = { add_trait = elusive_shadow }
                                }
                                #Set appropriate traits
                                random_list = {
                                    33 = {
                                        trigger = { NOT = { has_character_flag = archetype_supportive } } 
                                        if = {
                                            limit = { has_dlc = "Way of Life" }
                                            add_trait = impaler
                                        }
                                        random = {
                                            chance = 50
                                            add_trait = arbitrary
                                        }
                                        remove_trait = kind
                                        add_trait = cruel
                                    }
                                    33 = {
                                        if = {
                                            limit = { has_dlc = "Way of Life" }
                                            add_trait = schemer
                                        }
                                        add_trait = deceitful
                                        remove_trait = honest
                                    }
                                    33 = {
                                        if = {
                                            limit = { has_dlc = "Way of Life" }
                                            if = {
                                                limit = { is_female = yes }
                                                add_trait = seductress
                                            }
                                            if = {
                                                limit = { is_female = no }
                                                add_trait = seducer
                                            }
                                        }
                                        remove_trait = chaste
                                        add_trait = lustful
                                    }
                                }      
                            }

                            #Stewardship
                            20 = {
                                modifier = {
                                    has_character_flag = archetype_conservative
                                    factor = 0.5
                                }
                                #Set education
                                random_list = {
                                    30 = { add_trait = thrifty_clerk }
                                    50 = { add_trait = fortune_builder }
                                    20 = { add_trait = midas_touched }
                                }
                                #Set appropriate traits
                                random_list = {
                                    33 = {
                                        if = {
                                            limit = { has_dlc = "Way of Life" }
                                            add_trait = gardener
                                        }
                                        remove_trait = slothful
                                        add_trait = diligent
                                    }
                                    33 = {
                                        if = {
                                            limit = { has_dlc = "Way of Life" }
                                            add_trait = architect
                                        }
                                        add_trait = ambitious
                                        remove_trait = content
                                    }
                                    33 = {
                                        if = {
                                            limit = { has_dlc = "Way of Life" }
                                            add_trait = administrator
                                        }
                                        add_trait = erudite
                                    }
                                }      
                            }

                            #Learning
                            25 = {
                                modifier = {
                                    has_character_flag = archetype_conservative
                                    factor = 2
                                }
                                #Set education
                                random_list = {
                                    30 = { add_trait = martial_cleric }
                                    50 = { add_trait = scholarly_theologian }
                                    20 = { add_trait = mastermind_theologian }
                                }
                                #Set appropriate traits
                                random_list = {
                                    33 = {
                                        if = {
                                            limit = { has_dlc = "Way of Life" }
                                            add_trait = scholar
                                        }
                                        add_trait = erudite
                                    }
                                    33 = {
                                        if = {
                                            limit = { has_dlc = "Way of Life" }
                                            add_trait = mystic
                                        }
                                        add_trait = paranoid
                                        remove_trait = trusting
                                    }
                                    33 = {
                                        if = {
                                            limit = { has_dlc = "Way of Life" }
                                            add_trait = theologian
                                        }
                                        add_trait = erudite  
                                    }
                                }      
                            }
                        }
						
						if = {
							limit = {
								offmap_china = {
									has_status = china_golden_age
								}
							}
							change_diplomacy = 3
							change_martial = 3
							change_stewardship = 3
							change_intrigue = 3
							change_learning = 3
						}

                        #Sympathy
                        if = {
                            limit = { NOT = { trait = zealous } }
                            event_target:imperial_marriage_spouse_1 = { event_target:imperial_marriage_spouse_2 = { add_sympathy_for_prevs_open_religion_effect = yes } }
                        }
                    }
                }
            }
        }

        FROMFROM = { #The arranger of the marriage
            letter_event = { id = JD.20046 } 
        } 
    }
}

#The spouse is on its way
letter_event = {
    id = JD.20046
	has_dlc = "Jade Dragon"
    border = GFX_event_letter_frame_diplomacy

    desc = {
        trigger = {
			event_target:imperial_marriage_spouse_2 = { is_female = yes }
			offmap_china = { offmap_ruler = { age = 32 } }
		}
        text = EVTDESC_JD_20046_A
    }
    desc = {
        trigger = { event_target:imperial_marriage_spouse_2 = { is_female = no } }
        text = EVTDESC_JD_20046_B
    }
	desc = {
        trigger = {
			event_target:imperial_marriage_spouse_2 = { is_female = yes }
			offmap_china = { offmap_ruler = { NOT = { age = 32 } } }
		}
        text = EVTDESC_JD_20046_C
    }

    is_triggered_only = yes
    
    option = { 
        name = EVTOPTA_JD_20046
        custom_tooltip = { text = EVTOPTA_JD_20046_TT }

        hidden_effect = {
            event_target:imperial_marriage_spouse_2 = { character_event = { id = JD.20047 days = 14 random = 10 } } 
        }
    }
}

#The chinese spouse ping event
character_event = {
    id = JD.20047
	has_dlc = "Jade Dragon"
    hide_window = yes
    is_triggered_only = yes
    immediate = { 
        if = {
            limit = { is_female = yes }
            add_character_modifier = {
                name = jd_princess
                duration = -1
            }
        }
        if = {
            limit = { is_female = no }
            add_character_modifier = {
                name = jd_prince
                duration = -1
            }
        }
        remove_trait = cannot_marry
        event_target:imperial_marriage_spouse_1 = { 
            remove_trait = cannot_marry
            narrative_event = { id = JD.20048 } 
        } 
    }
}
 
#The marriage takes place
narrative_event = {
    id = JD.20048
	has_dlc = "Jade Dragon"
    title = EVTTITLE_JD_20048
    picture = GFX_evt_china_golden_age
    border = GFX_event_narrative_frame_diplomacy

    desc = {
        trigger = { FROM = { has_character_flag = archetype_conservative } }
        text = EVTDESC_JD_20048_CONSERVATIVE
    }
    desc = {
        trigger = { FROM = { has_character_flag = archetype_supportive } }
        text = EVTDESC_JD_20048_SUPPORTIVE
    }
    desc = {
        trigger = { FROM = { has_character_flag = archetype_decisive } }
        text = EVTDESC_JD_20048_DECISIVE
    }
    desc = {
        trigger = { FROM = { has_character_flag = archetype_sociable } }
        text = EVTDESC_JD_20048_SOCIABLE
    }

    is_triggered_only = yes
    

    option = { 
        name = EVTOPTA_JD_20048

        #Marry
        if = {
            limit = { is_female = yes }
            add_spouse_matrilineal = event_target:imperial_marriage_spouse_2
        }
        if = {
            limit = { is_female = no }
            add_spouse = event_target:imperial_marriage_spouse_2
        }

        #Prestige
        prestige = 500

        #Peace Deal
        if = {
            limit = { FROMFROM = { character = ROOT } }
            custom_tooltip = {
                text = chinese_imperial_marriage_peace_deal_effect_tt
                add_character_modifier = {
                    inherit = yes
                    name = peace_deal_with_china
                    years = 50
                }
            }
        }

        hidden_effect = { FROMFROM = { character_event = { id = JD.20049 } } } #Letting the decision taker know
        clr_character_flag = awaiting_imperial_wedding
    }
}

#Anouncement
character_event = {
    id = JD.20049
	has_dlc = "Jade Dragon"
    desc = EVTDESC_JD_20049
    picture = GFX_evt_china_golden_age
    border = GFX_event_normal_frame_diplomacy

    is_triggered_only = yes
    
    trigger = {
        NOT = { character = FROM }
    }

    option = {
        name = EVTOPTA_JD_20049
        custom_tooltip = { text = EVTOPTA_JD_20049_TT }
        custom_tooltip = {
            text = chinese_imperial_marriage_peace_deal_effect_tt
            add_character_modifier = {
                inherit = yes
                name = peace_deal_with_china
                years = 50
            }
        }
        FROM = {
            show_portrait = yes
        }
        event_target:imperial_marriage_spouse_2 = {
            show_portrait = yes
        }
    }
}

#Siege Engineers
character_event = {
    id = JD.3000
	has_dlc = "Jade Dragon"
    hide_window = yes
    is_triggered_only = yes
    immediate = { 
        FROMFROM = {
            create_random_soldier = {
                age = 55
                dynasty = actually_culture
                religion = ROOT
                culture = ROOT
                female = no
                random_traits = yes
                attributes = {
                    diplomacy = 2
					learning = 2
					stewardship = 2
					intrigue = 2
					martial = 16
                }
            }
            new_character = {
                save_event_target_as = chinese_siege_engineer_1
				set_character_flag = ai_flag_refuse_concubinage
				set_character_flag = ai_flag_refuse_marriage
				set_character_flag = originated_from_chinese_court
				set_character_flag = chinese_courtier_original_court_@PREV
                set_character_flag = no_court_invites
				set_character_flag = taught_chinese_strategy
				set_character_flag = is_chinese_siege_engineer
				
				remove_trait = weak
				remove_trait = slow
				remove_trait = imbecile
				remove_trait = dull
				
				remove_trait = craven
				remove_trait = mystic
				remove_trait = socializer
				remove_trait = gamer
				remove_trait = hedonist
				remove_trait = duelist
				remove_trait = hunter
				remove_trait = strategist
				remove_trait = administrator
				remove_trait = architect
				remove_trait = gardener
				remove_trait = schemer
				remove_trait = impaler
				remove_trait = seducer
				remove_trait = seductress
				remove_trait = theologian
				remove_trait = scholar
				
				add_trait = sapper
				
				if = {
					limit = {
						offmap_china = {
							has_status = china_golden_age
						}
					}
					change_martial = 5
				}
				
                random_list = { # Flavor Randomization
					15 = {
						change_martial = 2
					}
					10 = {
						add_trait = robust
					}
					10 = {
						add_trait = duelist
					}
					10 = {
						add_trait = architect
					}
					5 = {
						add_trait = strategist
					}
					5 = {
						change_martial = 4
					}
					5 = {
						if = {
							limit = {
								has_dlc = "Reapers"
							}
							add_trait = one_eyed
						}
					}
					5 = { 
						add_trait = shrewd
					}
				}
                opinion = {
                    who = PREV
                    modifier = opinion_servant
                    months = 1200
                }
            }
			create_random_soldier = {
                age = 35
                dynasty = actually_culture
                religion = ROOT
                culture = ROOT
                female = no
                random_traits = yes
                attributes = {
                    diplomacy = 2
					learning = 2
					stewardship = 2
					intrigue = 2
					martial = 8
                }
            }
            new_character = {
                save_event_target_as = chinese_siege_engineer_2
				set_character_flag = ai_flag_refuse_concubinage
				set_character_flag = ai_flag_refuse_marriage
				set_character_flag = originated_from_chinese_court
				set_character_flag = chinese_courtier_original_court_@PREV
                set_character_flag = no_court_invites
				set_character_flag = taught_chinese_strategy
				set_character_flag = is_chinese_siege_engineer
				
				remove_trait = weak
				remove_trait = slow
				remove_trait = imbecile
				remove_trait = dull
				
				remove_trait = craven
				remove_trait = mystic
				remove_trait = socializer
				remove_trait = gamer
				remove_trait = hedonist
				remove_trait = duelist
				remove_trait = hunter
				remove_trait = strategist
				remove_trait = administrator
				remove_trait = architect
				remove_trait = gardener
				remove_trait = schemer
				remove_trait = impaler
				remove_trait = seducer
				remove_trait = seductress
				remove_trait = theologian
				remove_trait = scholar
				
				add_trait = sapper
				
				if = {
					limit = {
						offmap_china = {
							has_status = china_golden_age
						}
					}
					change_martial = 5
				}
				
                random_list = { # Flavor Randomization
					15 = {
						change_martial = 2
					}
					10 = {
						add_trait = robust
					}
					10 = {
						add_trait = duelist
					}
					10 = {
						add_trait = architect
					}
					5 = {
						add_trait = strategist
					}
					5 = {
						change_martial = 4
					}
					5 = {
						if = {
							limit = {
								has_dlc = "Reapers"
							}
							add_trait = one_eyed
						}
					}
					5 = { 
						add_trait = shrewd
					}
				}
                opinion = {
                    who = PREV
                    modifier = opinion_servant
                    months = 1200
                }
            }
			create_random_soldier = {
                age = 37
                dynasty = actually_culture
                religion = ROOT
                culture = ROOT
                female = no
                random_traits = yes
                attributes = {
                    diplomacy = 2
					learning = 2
					stewardship = 2
					intrigue = 2
					martial = 8
                }
            }
            new_character = {
                save_event_target_as = chinese_siege_engineer_3
				set_character_flag = ai_flag_refuse_concubinage
				set_character_flag = ai_flag_refuse_marriage
				set_character_flag = originated_from_chinese_court
				set_character_flag = chinese_courtier_original_court_@PREV
                set_character_flag = no_court_invites
				set_character_flag = taught_chinese_strategy
				set_character_flag = is_chinese_siege_engineer
				
				remove_trait = weak
				remove_trait = slow
				remove_trait = imbecile
				remove_trait = dull
				
				remove_trait = craven
				remove_trait = mystic
				remove_trait = socializer
				remove_trait = gamer
				remove_trait = hedonist
				remove_trait = duelist
				remove_trait = hunter
				remove_trait = strategist
				remove_trait = administrator
				remove_trait = architect
				remove_trait = gardener
				remove_trait = schemer
				remove_trait = impaler
				remove_trait = seducer
				remove_trait = seductress
				remove_trait = theologian
				remove_trait = scholar
				
				add_trait = sapper
				
				if = {
					limit = {
						offmap_china = {
							has_status = china_golden_age
						}
					}
					change_martial = 5
				}
				
                random_list = { # Flavor Randomization
					15 = {
						change_martial = 2
					}
					10 = {
						add_trait = robust
					}
					10 = {
						add_trait = duelist
					}
					10 = {
						add_trait = architect
					}
					5 = {
						add_trait = strategist
					}
					5 = {
						change_martial = 4
					}
					5 = {
						if = {
							limit = {
								has_dlc = "Reapers"
							}
							add_trait = one_eyed
						}
					}
					5 = { 
						add_trait = shrewd
					}
				}
                opinion = {
                    who = PREV
                    modifier = opinion_servant
                    months = 1200
                }
            }
            letter_event = { id = JD.3001 } 
        }
    }
}

#Governor presents Siege Engineers
letter_event = {
    id = JD.3001
	has_dlc = "Jade Dragon"
    desc = EVTDESC_JD_3001
    border = GFX_event_letter_frame_diplomacy

    hide_new = yes

    is_triggered_only = yes

    option = { 
        name = EVTOPTA_JD_3001
		custom_tooltip = {
			text = EVTOPTA_JD_3001_tt
		}
        event_target:chinese_siege_engineer_1 = {
            show_scope_change = no
            set_character_flag = appear_in_option
            clr_character_flag = appear_in_option
            if = {
                limit = { 
                    can_hold_title = title_commander 
                    ROOT = { can_grant_title = title_commander }
                }
                give_minor_title = title_commander
            }
        }
		event_target:chinese_siege_engineer_2 = {
            show_scope_change = no
            set_character_flag = appear_in_option
            clr_character_flag = appear_in_option
            if = {
                limit = { 
                    can_hold_title = title_commander 
                    ROOT = { can_grant_title = title_commander }
                }
                give_minor_title = title_commander
            }
        }
		event_target:chinese_siege_engineer_3 = {
            show_scope_change = no
            set_character_flag = appear_in_option
            clr_character_flag = appear_in_option
            if = {
                limit = { 
                    can_hold_title = title_commander 
                    ROOT = { can_grant_title = title_commander }
                }
                give_minor_title = title_commander
            }
        }
    }
	option = { 
        name = EVTOPTB_JD_3001
		custom_tooltip = {
			text = EVTOPTB_JD_3001_tt
		}
		event_target:chinese_siege_engineer_1 = {
			set_character_flag = appear_in_option
			clr_character_flag = appear_in_option
		}
		event_target:chinese_siege_engineer_2 = {
			set_character_flag = appear_in_option
			clr_character_flag = appear_in_option
		}
		event_target:chinese_siege_engineer_3 = {
			set_character_flag = appear_in_option
			clr_character_flag = appear_in_option
		}
		hidden_effect = {
			event_target:chinese_siege_engineer_1 = {
				show_scope_change = no
				if = {
					limit = { 
						can_hold_title = title_commander 
						ROOT = { can_grant_title = title_commander }
					}
					give_minor_title = title_commander
				}
				else_if = {
					limit = {
						NOT = {
							has_minor_title = title_commander
						}
						ROOT = {
							any_realm_character = {
								NOR = {
									has_character_flag = originated_from_chinese_court
									has_character_flag = taught_chinese_strategy
								}
								NOT = {
									higher_tier_than = BARON
								}
								liege = { character = ROOT }
								has_minor_title = title_commander
								NOT = {
									martial = 10
								}
							}
						}
					}
					ROOT = {
						random_realm_character = {
							limit = {
								NOR = {
									has_character_flag = originated_from_chinese_court
									has_character_flag = taught_chinese_strategy
								}
								NOT = {
									higher_tier_than = BARON
								}
								liege = { character = ROOT }
								has_minor_title = title_commander
								NOT = {
									martial = 10
								}
							}
							remove_title = title_commander
						}
					}
					give_minor_title = title_commander
				}
				else_if = {
					limit = {
						NOT = {
							has_minor_title = title_commander
						}
						ROOT = {
							any_realm_character = {
								NOR = {
									has_character_flag = originated_from_chinese_court
									has_character_flag = taught_chinese_strategy
								}
								NOT = {
									higher_tier_than = BARON
								}
								liege = { character = ROOT }
								has_minor_title = title_commander
								martial = 10
							}
						}
					}
					ROOT = {
						random_realm_character = {
							limit = {
								NOR = {
									has_character_flag = originated_from_chinese_court
									has_character_flag = taught_chinese_strategy
								}
								NOT = {
									higher_tier_than = BARON
								}
								liege = { character = ROOT }
								has_minor_title = title_commander
								martial = 10
							}
							remove_title = title_commander
						}
					}
					give_minor_title = title_commander
				}
				else_if = {
					limit = {
						NOT = {
							has_minor_title = title_commander
						}
						ROOT = {
							any_realm_character = {
								NOR = {
									has_character_flag = originated_from_chinese_court
									has_character_flag = taught_chinese_strategy
								}
								liege = { character = ROOT }
								has_minor_title = title_commander
							}
						}
					}
					ROOT = {
						random_realm_character = {
							limit = {
								NOR = {
									has_character_flag = originated_from_chinese_court
									has_character_flag = taught_chinese_strategy
								}
								liege = { character = ROOT }
								has_minor_title = title_commander
							}
							remove_title = title_commander
						}
					}
					give_minor_title = title_commander
				}
			}
			event_target:chinese_siege_engineer_2 = {
				show_scope_change = no
				if = {
					limit = { 
						can_hold_title = title_commander 
						ROOT = { can_grant_title = title_commander }
					}
					give_minor_title = title_commander
				}
				else_if = {
					limit = {
						NOT = {
							has_minor_title = title_commander
						}
						ROOT = {
							any_realm_character = {
								NOR = {
									has_character_flag = originated_from_chinese_court
									has_character_flag = taught_chinese_strategy
								}
								NOT = {
									higher_tier_than = BARON
								}
								liege = { character = ROOT }
								has_minor_title = title_commander
								NOT = {
									martial = 10
								}
							}
						}
					}
					ROOT = {
						random_realm_character = {
							limit = {
								NOR = {
									has_character_flag = originated_from_chinese_court
									has_character_flag = taught_chinese_strategy
								}
								NOT = {
									higher_tier_than = BARON
								}
								liege = { character = ROOT }
								has_minor_title = title_commander
								NOT = {
									martial = 10
								}
							}
							remove_title = title_commander
						}
					}
					give_minor_title = title_commander
				}
				else_if = {
					limit = {
						NOT = {
							has_minor_title = title_commander
						}
						ROOT = {
							any_realm_character = {
								NOR = {
									has_character_flag = originated_from_chinese_court
									has_character_flag = taught_chinese_strategy
								}
								NOT = {
									higher_tier_than = BARON
								}
								liege = { character = ROOT }
								has_minor_title = title_commander
								martial = 10
							}
						}
					}
					ROOT = {
						random_realm_character = {
							limit = {
								NOR = {
									has_character_flag = originated_from_chinese_court
									has_character_flag = taught_chinese_strategy
								}
								NOT = {
									higher_tier_than = BARON
								}
								liege = { character = ROOT }
								has_minor_title = title_commander
								martial = 10
							}
							remove_title = title_commander
						}
					}
					give_minor_title = title_commander
				}
				else_if = {
					limit = {
						NOT = {
							has_minor_title = title_commander
						}
						ROOT = {
							any_realm_character = {
								NOR = {
									has_character_flag = originated_from_chinese_court
									has_character_flag = taught_chinese_strategy
								}
								liege = { character = ROOT }
								has_minor_title = title_commander
							}
						}
					}
					ROOT = {
						random_realm_character = {
							limit = {
								NOR = {
									has_character_flag = originated_from_chinese_court
									has_character_flag = taught_chinese_strategy
								}
								liege = { character = ROOT }
								has_minor_title = title_commander
							}
							remove_title = title_commander
						}
					}
					give_minor_title = title_commander
				}
			}
			event_target:chinese_siege_engineer_3 = {
				show_scope_change = no
				if = {
					limit = { 
						can_hold_title = title_commander 
						ROOT = { can_grant_title = title_commander }
					}
					give_minor_title = title_commander
				}
				else_if = {
					limit = {
						NOT = {
							has_minor_title = title_commander
						}
						ROOT = {
							any_realm_character = {
								NOR = {
									has_character_flag = originated_from_chinese_court
									has_character_flag = taught_chinese_strategy
								}
								NOT = {
									higher_tier_than = BARON
								}
								liege = { character = ROOT }
								has_minor_title = title_commander
								NOT = {
									martial = 10
								}
							}
						}
					}
					ROOT = {
						random_realm_character = {
							limit = {
								NOR = {
									has_character_flag = originated_from_chinese_court
									has_character_flag = taught_chinese_strategy
								}
								NOT = {
									higher_tier_than = BARON
								}
								liege = { character = ROOT }
								has_minor_title = title_commander
								NOT = {
									martial = 10
								}
							}
							remove_title = title_commander
						}
					}
					give_minor_title = title_commander
				}
				else_if = {
					limit = {
						NOT = {
							has_minor_title = title_commander
						}
						ROOT = {
							any_realm_character = {
								NOR = {
									has_character_flag = originated_from_chinese_court
									has_character_flag = taught_chinese_strategy
								}
								NOT = {
									higher_tier_than = BARON
								}
								liege = { character = ROOT }
								has_minor_title = title_commander
								martial = 10
							}
						}
					}
					ROOT = {
						random_realm_character = {
							limit = {
								NOR = {
									has_character_flag = originated_from_chinese_court
									has_character_flag = taught_chinese_strategy
								}
								NOT = {
									higher_tier_than = BARON
								}
								liege = { character = ROOT }
								has_minor_title = title_commander
								martial = 10
							}
							remove_title = title_commander
						}
					}
					give_minor_title = title_commander
				}
				else_if = {
					limit = {
						NOT = {
							has_minor_title = title_commander
						}
						ROOT = {
							any_realm_character = {
								NOR = {
									has_character_flag = originated_from_chinese_court
									has_character_flag = taught_chinese_strategy
								}
								liege = { character = ROOT }
								has_minor_title = title_commander
							}
						}
					}
					ROOT = {
						random_realm_character = {
							limit = {
								NOR = {
									has_character_flag = originated_from_chinese_court
									has_character_flag = taught_chinese_strategy
								}
								liege = { character = ROOT }
								has_minor_title = title_commander
							}
							remove_title = title_commander
						}
					}
					give_minor_title = title_commander
				}
			}
		}
    }
}

# Relief Expedition
character_event = {
    id = JD.3010
	has_dlc = "Jade Dragon"
    hide_window = yes
    is_triggered_only = yes
    immediate = { FROMFROM = { letter_event = { id = JD.3011 } } }
}

letter_event = {
    id = JD.3011
	has_dlc = "Jade Dragon"
    desc = EVTDESC_JD_3011
    border = GFX_event_letter_frame_diplomacy

    is_triggered_only = yes

    option = { 
        name = EVTOPTA_JD_3011
    }
}

# Request Invasion
character_event = {
    id = JD.3020
	has_dlc = "Jade Dragon"
    hide_window = yes
    is_triggered_only = yes
    immediate = { FROMFROM = { letter_event = { id = JD.3021 } } }
}

letter_event = {
    id = JD.3021
	has_dlc = "Jade Dragon"
    desc = EVTDESC_JD_3021
    border = GFX_event_letter_frame_diplomacy

    is_triggered_only = yes

    option = { 
        name = EXCELLENT
    }
	
	option = {
        name = {
        	text = EVTOPTA_JD_3021 # Join wars
        }
        trigger = {
         	event_target:chinese_invasion_instigator = { character = ROOT }
			independent = yes
			war = no
			NOR = {
				has_non_aggression_pact_with = event_target:chinese_invasion_target
				is_allied_with = event_target:chinese_invasion_target
				event_target:chinese_invasion_target = {
					is_tributary = { suzerain = ROOT }
				}
			}
        }
		event_target:chinese_invasion_target = {
			show_portrait = yes
		}
		event_target:chinese_invasion_instigator = {
			show_portrait = yes
		}
		join_attacker_wars = event_target:invasion_governor
    }
}

narrative_event = { # NEWS: visible event for player (shatter realm invasion)
    id = JD.3022
	has_dlc = "Jade Dragon"
    title = NEWS_FROM_CHINA
	picture = GFX_evt_china_expansionist
    portrait = offmap_china
	window = EventWindowOffmap
	background = GFX_event_window_news_from_china

	desc = {
		text = EVTDESC_JD_3022_A
		trigger = {
			event_target:chinese_invasion_target = { character = ROOT }
		}
	}
	desc = {
		text = EVTDESC_JD_3022_B
		trigger = {
			NOT = { event_target:chinese_invasion_target = { character = ROOT } }
		}
	}

	has_dlc = "Jade Dragon"

    is_triggered_only = yes

    option = {
        name = {
        	text = EVTOPTA_JD_10102_A_brave
        	trigger = {
				trait = brave
        	}
        }
        name = {
        	text = EVTOPTA_JD_10102_A_craven
        	trigger = {
				trait = craven
        	}
        }
        name = {
        	text = EVTOPTA_JD_10102_A_pious
        	trigger = {
				is_pious_trigger = yes
        	}
        }
        name = {
        	text = EVTOPTA_JD_10102_A_neutral
        	trigger = {
				NOT = { trait = craven }
				NOT = { trait = brave }
				NOT = { is_pious_trigger = yes }
        	}
        }
        trigger = {
         	event_target:chinese_invasion_target = { character = ROOT }
        }
		event_target:chinese_invasion_target = {
			show_portrait = yes
		}
		event_target:chinese_invasion_instigator = {
			show_portrait = yes
		}
    }

    option = {
        name = {
        	text = EVTOPTA_JD_10102_B_same_religion #May [HighGod] protect them
        	trigger = {	event_target:chinese_invasion_target = { religion_group = ROOT } }
        }
        name = {
        	text = EVTOPTA_JD_10102_B_other_religion #May their gods protect them
        	trigger = {	NOT = { event_target:chinese_invasion_target = { religion_group = ROOT } } }
        }
        trigger = {
         	NOR = {
				event_target:chinese_invasion_target = { character = ROOT }
				event_target:chinese_invasion_instigator = { character = ROOT }
			}
        }
		event_target:chinese_invasion_target = {
			show_portrait = yes
		}
		event_target:chinese_invasion_instigator = {
			show_portrait = yes
		}
    }
	
	option = {
        name = {
        	text = EVTOPTC_JD_3022 # Just as planned
        }
        trigger = {
         	event_target:chinese_invasion_instigator = { character = ROOT }
        }
		event_target:chinese_invasion_target = {
			show_portrait = yes
		}
		event_target:chinese_invasion_instigator = {
			show_portrait = yes
		}
    }
}

# Request Artifact
character_event = {
    id = JD.3030
	has_dlc = "Jade Dragon"
    hide_window = yes
    is_triggered_only = yes
    immediate = {
		if = {
			random_list = {
				10 = {
					trigger = {
						FROMFROM = {
							NOT = {
								has_artifact = chinese_artwork
							}
						}
					}
					add_artifact = chinese_artwork
					new_artifact = {
						set_name="chinese_artwork_name"
						transfer_artifact = {
							from = ROOT
							to = FROMFROM
						}
						save_event_target_as = chinese_artifact
					}
				}
				10 = {
					trigger = {
						FROMFROM = {
							NOT = {
								has_artifact = chinese_calligraphy
							}
						}
					}
					add_artifact = chinese_calligraphy
					new_artifact = {
						set_name="chinese_calligraphy_name"
						transfer_artifact = {
							from = ROOT
							to = FROMFROM
						}
						save_event_target_as = chinese_artifact
					}
				}
				10 = {
					trigger = {
						FROMFROM = {
							NOT = {
								has_artifact = chinese_sculpture
							}
						}
					}
					add_artifact = chinese_sculpture
					new_artifact = {
						set_name="chinese_sculpture_name"
						transfer_artifact = {
							from = ROOT
							to = FROMFROM
						}
						save_event_target_as = chinese_artifact
					}
				}
				10 = {
					trigger = {
						FROMFROM = {
							NOT = {
								has_artifact = chinese_ceremonial_robes
							}
						}
					}
					add_artifact = chinese_ceremonial_robes
					new_artifact = {
						transfer_artifact = {
							from = ROOT
							to = FROMFROM
						}
						save_event_target_as = chinese_artifact
					}
				}
				10 = {
					trigger = {
						FROMFROM = {
							NOT = {
								has_artifact = chinese_prev_emperor_sculpture
							}
						}
						offmap_china = {
							offmap_prev_ruler = {
								always = yes
							}
						}
					}
					offmap_china = {
						offmap_prev_ruler = {
							save_event_target_as = prev_emperor
						}
					}
					add_artifact = chinese_prev_emperor_sculpture
					new_artifact = {
						set_name="chinese_figurine_name"
						transfer_artifact = {
							from = ROOT
							to = FROMFROM
						}
						save_event_target_as = chinese_artifact
					}
				}
				10 = {
					trigger = {
						FROMFROM = {
							NOT = {
								has_artifact = chinese_arm_protector
							}
						}
					}
					add_artifact = chinese_arm_protector
					new_artifact = {
						set_name="chinese_arm_protector_name"
						transfer_artifact = {
							from = ROOT
							to = FROMFROM
						}
						save_event_target_as = chinese_artifact
					}
				}
				10 = {
					trigger = {
						FROMFROM = {
							NOT = {
								has_artifact = chinese_serpent_spear
							}
						}
					}
					add_artifact = chinese_serpent_spear
					new_artifact = {
						transfer_artifact = {
							from = ROOT
							to = FROMFROM
						}
						save_event_target_as = chinese_artifact
					}
				}
				10 = {
					trigger = {
						FROMFROM = {
							NOT = {
								has_artifact = chinese_ji
							}
						}
					}
					add_artifact = chinese_ji
					new_artifact = {
						transfer_artifact = {
							from = ROOT
							to = FROMFROM
						}
						save_event_target_as = chinese_artifact
					}
				}
				10 = {
					trigger = {
						FROMFROM = {
							NOT = {
								has_artifact = chinese_crossbow
							}
						}
					}
					add_artifact = chinese_crossbow
					new_artifact = {
						transfer_artifact = {
							from = ROOT
							to = FROMFROM
						}
						save_event_target_as = chinese_artifact
					}
				}
				10 = {
					trigger = {
						FROMFROM = {
							NOT = {
								has_artifact = chinese_bronze_sculpture
							}
						}
					}
					add_artifact = chinese_bronze_sculpture
					new_artifact = {
						set_name="chinese_bronze_sculpture_name"
						transfer_artifact = {
							from = ROOT
							to = FROMFROM
						}
						save_event_target_as = chinese_artifact
					}
				}
				10 = {
					trigger = {
						FROMFROM = {
							NOT = {
								has_artifact = chinese_book_health
							}
						}
					}
					add_artifact = chinese_book_health
					new_artifact = {
						set_name="chinese_book_health_name"
						transfer_artifact = {
							from = ROOT
							to = FROMFROM
						}
						save_event_target_as = chinese_artifact
					}
				}
				10 = {
					trigger = {
						FROMFROM = {
							NOT = {
								has_artifact = chinese_book_law
							}
						}
					}
					add_artifact = chinese_book_law
					new_artifact = {
						set_name="chinese_book_law_name"
						transfer_artifact = {
							from = ROOT
							to = FROMFROM
						}
						save_event_target_as = chinese_artifact
					}
				}
				10 = {
					trigger = {
						FROMFROM = {
							NOT = {
								has_artifact = chinese_book_history
							}
						}
					}
					add_artifact = chinese_book_history
					new_artifact = {
						set_name="chinese_book_history_name"
						transfer_artifact = {
							from = ROOT
							to = FROMFROM
						}
						save_event_target_as = chinese_artifact
					}
				}
				10 = {
					trigger = {
						FROMFROM = {
							NOT = {
								has_artifact = chinese_book_economy
							}
						}
					}
					add_artifact = chinese_book_economy
					new_artifact = {
						set_name="chinese_book_economy_name"
						transfer_artifact = {
							from = ROOT
							to = FROMFROM
						}
						save_event_target_as = chinese_artifact
					}
				}
				1 = {
					trigger = {
						FROMFROM = {
							NOT = {
								has_artifact = chinese_painting_of_glitterhoof
							}
						}
					}
					add_artifact = chinese_painting_of_glitterhoof
					new_artifact = {
						transfer_artifact = {
							from = ROOT
							to = FROMFROM
						}
						save_event_target_as = chinese_artifact
					}
				}
				10 = {
					trigger = {
						FROMFROM = {
							NOT = {
								has_artifact = chinese_tapestry
							}
						}
					}
					random_independent_ruler = {
						limit = {
							any_realm_province = {
								trade_route = silk_road
							}
						}
						random_realm_province = {
							limit = {
								trade_route = silk_road
							}
							save_event_target_as = tapestry_target
						}
					}
					add_artifact = chinese_tapestry
					new_artifact = {
						set_name="chinese_tapestry_name"
						transfer_artifact = {
							from = ROOT
							to = FROMFROM
						}
						save_event_target_as = chinese_artifact
					}
				}
				10 = {
					trigger = {
						FROMFROM = {
							NOT = {
								has_artifact = chinese_tapestry_grand
							}
						}
					}
					add_artifact = chinese_tapestry_grand
					new_artifact = {
						set_name="chinese_tapestry_grand_name"
						transfer_artifact = {
							from = ROOT
							to = FROMFROM
						}
						save_event_target_as = chinese_artifact
					}
				}
			}
		}
		FROMFROM = { letter_event = { id = JD.3031 } } 
	}
}

letter_event = {
    id = JD.3031
	has_dlc = "Jade Dragon"
    desc = EVTDESC_JD_3031
    border = GFX_event_letter_frame_diplomacy

    is_triggered_only = yes

    option = { 
        name = EVTOPTA_JD_3031
    }
}

# Supply Horses
character_event = {
    id = JD.3040
	has_dlc = "Jade Dragon"
    hide_window = yes
    is_triggered_only = yes
    immediate = { FROMFROM = { letter_event = { id = JD.3041 } } }
}

letter_event = {
    id = JD.3041
	has_dlc = "Jade Dragon"
    desc = EVTDESC_JD_3041
    border = GFX_event_letter_frame_diplomacy

    is_triggered_only = yes

    option = { 
        name = EVTOPTA_JD_3041
		
		sound_effect = china_grace_gain
		add_character_modifier = {
			name = china_supply_horses
			duration = -1
			inherit = yes
		}
		if = {
			limit = {
				liked_by_offmap = {
					type = offmap_china
					context = tributary
				}
			}
			add_offmap_currency = {
				offmap = offmap_china
				value = 150
			}
		}
		if = {
			limit = {
				disliked_by_offmap = {
					type = offmap_china
					context = tributary
				}
			}
			add_offmap_currency = {
				offmap = offmap_china
				value = 50
			}
		}
		if = {
			limit = {
				NOR = {
					liked_by_offmap = {
						type = offmap_china
						context = tributary
					}
					disliked_by_offmap = {
						type = offmap_china
						context = tributary
					}
				}
			}
			add_offmap_currency = {
				offmap = offmap_china
				value = 100
			}
		}
    }
}

character_event = { # Clear modifier on death if no longer Nomadic
	id = JD.3042
	has_dlc = "Jade Dragon"
	is_triggered_only = yes
	hide_window = yes

	trigger = {
		NOT = { is_nomadic = yes }
		has_character_modifier = china_supply_horses
	}

	immediate = {
		remove_character_modifier = china_supply_horses
	}
}

# Raid China
character_event = {
    id = JD.3050
	has_dlc = "Jade Dragon"
    hide_window = yes
    is_triggered_only = yes
    immediate = { FROMFROM = { letter_event = { id = JD.3051 } } }
}

letter_event = {
    id = JD.3051
	has_dlc = "Jade Dragon"
    desc = EVTDESC_JD_3051
    border = GFX_event_letter_frame_diplomacy

    is_triggered_only = yes

    option = { 
        name = EVTOPTA_JD_3051
    }
}

#Response for sending a tribute character to China...
letter_event = {
    id = JD.10050
	has_dlc = "Jade Dragon"
    desc = EVTDESC_JD_10050
    border = GFX_event_letter_frame_diplomacy

    is_triggered_only = yes

    option = { 
        name = {
            text = EVTOPTA_JD_10050_bad
            trigger = {
                has_character_flag = sent_bad_tribute
                NOT = { is_close_relative = event_target:person_sent_as_tribute }
            }
        }
        name = {
            text = EVTOPTA_JD_10050_decent
            trigger = {
                has_character_flag = sent_decent_tribute
                NOT = { is_close_relative = event_target:person_sent_as_tribute }
            }
        }
        name = {
            text = EVTOPTA_JD_10050_great
            trigger = {
                has_character_flag = sent_great_tribute
                NOT = { is_close_relative = event_target:person_sent_as_tribute }
            }
        }
        name = {
            text = EVTOPTA_JD_10050_family
            trigger = { is_close_relative = event_target:person_sent_as_tribute }
        }

        event_target:person_sent_as_tribute = { show_portrait = yes }

        clr_character_flag = sent_bad_tribute
        clr_character_flag = sent_decent_tribute
        clr_character_flag = sent_great_tribute
        clr_character_flag = sent_eunuch_tribute
        clr_character_flag = sent_concubine_tribute
    }
}

letter_event = {
	id = JD.30014
	has_dlc = "Jade Dragon"
	
	desc = EVTDESC_JD_30014
    border = GFX_event_letter_frame_economy
	
	is_triggered_only = yes
	
	option = {
		name = EVTOPTA_JD_30014
		tooltip = {
			add_character_modifier = {
				modifier = chinese_imperial_trade_contract
				inherit = yes
				years = 25
			}
		}
	}
}

#Fired from on-action war started. Cleans up trade contract modifiers if you end up fighting China.
character_event = {
	id = JD.30015
	has_dlc = "Jade Dragon"
	hide_window = yes
	
	is_triggered_only = yes
	
	trigger = {
		offmap_china = {
			governor = {
				OR = { 
					character = ROOT
					character = FROM
				}
			}
		}
	}
	
	immediate = {
		remove_character_modifier = chinese_imperial_trade_contract
		any_vassal = {
			remove_character_modifier = chinese_imperial_trade_contract
		}
		FROM = {
			remove_character_modifier = chinese_imperial_trade_contract
			any_vassal = {
				remove_character_modifier = chinese_imperial_trade_contract
			}
		}
		remove_character_modifier = china_supply_horses
		any_vassal = {
			remove_character_modifier = china_supply_horses
		}
		FROM = {
			remove_character_modifier = china_supply_horses
			any_vassal = {
				remove_character_modifier = china_supply_horses
			}
		}
	}
}

### Opening China by Force ###
#Ping for WP
character_event = {
    id = JD.60300
	has_dlc = "Jade Dragon"
    hide_window = yes
    is_triggered_only = yes

    immediate = {
        FROMFROM = { character_event = { id = JD.60301 } }
    }
}
character_event = {
    id = JD.60301
	has_dlc = "Jade Dragon"
    desc = EVT_DESC_JD_60301
    picture = GFX_evt_china_isolationist

    is_triggered_only = yes

    option = { #War
        name = EVT_OPTA_JD_60301
		sound_effect = china_angered_emperor
        detract_grace_super_huge_effect = yes
        war = {
            casus_belli = force_open_china_cb
            target = event_target:target_governor
        }
        custom_tooltip = { text = force_china_open_cb_outcome_tt }
    }
    option = { #Silk Road dominance
        name = EVT_OPTB_JD_60301
        trigger = {
            any_realm_province = {
                OR = {
                    province_id = 1614 #Jiuquan
                    province_id = 1603 #Markam
                    province_id = 1114 #Mahoyadapuram
                }
            }
            trade_route_control = {
                trade_route = silk_road
                value >= 0.25
                type = wealth
                indirectly_owned = yes
            }
        }
        tooltip_info = yes
        custom_tooltip = { text = force_open_china_silk_road_conditions_tt }
        detract_grace_massive_effect = yes
        offmap_china = { 
            set_offmap_flag = no_policy_news
            set_offmap_flag = no_status_news
            set_policy = china_open
			log = "-------------------------------------"
			log = "China Policy Logging:"
			log = "Chinas new Policy is now OPEN"
			log = "-------------------------------------"
            set_offmap_tmp_flag = china_forced_open
            custom_tooltip = { text = china_forced_open_tt }
			governor = {
				hidden_effect = {
					random = {
						chance = 30
						war = {
							casus_belli = destabilization_china_cb
							target = ROOT
						}
					}
				}
			}
            if = {
                limit = { has_status = china_golden_age }
                random = {
                    chance = 30
                    set_status = china_stable
                }
            }
            else_if = {
                limit = { has_status = china_stable }
                random = {
                    chance = 30
                    set_status = china_unrest
					set_offmap_flag = china_had_unrest
                }
            }
            clr_offmap_flag = no_policy_news
            clr_offmap_flag = no_status_news
            clr_offmap_tmp_flag = china_force_open_in_progress
        }
        custom_tooltip = {
            text = force_open_china_silk_road_tt
            any_player = {
                limit = {
                    has_offmap_news_enabled = offmap_china
                    is_within_diplo_range = event_target:target_governor
                }
                narrative_event = { id = JD.60304 }
            }
        }
    }
    option = { #Negotiate with China
        name = EVT_OPTC_JD_60301
        event_target:target_governor = { letter_event = { id = JD.60305 tooltip = force_open_china_negotiate_tt } }
    }
    option = { #Maybe not
        name = EVT_OPTD_JD_60301
        hidden_effect = { 
            remove_character_modifier = china_force_open_cooldown
            offmap_china = { clr_offmap_tmp_flag = china_force_open_in_progress }
        }
    }
}

#News Event: Successfully force open China by war
narrative_event = {
    id = JD.60302
	has_dlc = "Jade Dragon"
    title = NEWS_FROM_CHINA
    desc = EVTDESC_JD_60302
    picture = GFX_evt_china_isolationist
    portrait = offmap_china
	window = EventWindowOffmap
	background = GFX_event_window_news_from_china
    is_triggered_only = yes

    option = {
        name = EVT_OPTA_JD_60302
    }
}

#News Event: Fail to force open China by war
narrative_event = {
    id = JD.60303
	has_dlc = "Jade Dragon"
    title = NEWS_FROM_CHINA
    desc = EVTDESC_JD_60303
    picture = GFX_evt_china_isolationist
    portrait = offmap_china
	window = EventWindowOffmap
	background = GFX_event_window_news_from_china
    is_triggered_only = yes

    option = {
        name = EVT_OPTA_JD_60303
    }
}

#News Event: Silk Road dominance forces open China
narrative_event = {
    id = JD.60304
	has_dlc = "Jade Dragon"
    title = NEWS_FROM_CHINA
    desc = EVTDESC_JD_60304
    picture = GFX_evt_china_isolationist
    portrait = offmap_china
	window = EventWindowOffmap
	background = GFX_event_window_news_from_china
    is_triggered_only = yes
	
	immediate = {
		offmap_china = {
			ruler = {
				save_event_target_as = portrait_target
			}
		}
	}
	
	portrait = event_target:portrait_target
	
    option = {
        name = EVT_OPTA_JD_60304
    }
}

#Governor: Allow Negotiation with China
letter_event = {
    id = JD.60305
	has_dlc = "Jade Dragon"
    hide_window = yes
    is_triggered_only = yes

    immediate = {
        random_list = {
            30 = { #No
                modifier = {
                    factor = 1.5
                    china_is_suffering_trigger = yes
                }
                modifier = {
                    factor = 2
                    FROM = { is_disliked_by_China_trigger = yes }
                }
                modifier = {
                    factor = 1.2
                    FROM = { diplomacy < 10 }
                }
                modifier = {
                    factor = 1.2
                    relative_power_including_allies_defender = {
                        who = FROM
                        power = 1.2
                    }
                }
                modifier = {
                    factor = 1.2
                    relative_power_including_allies_defender = {
                        who = FROM
                        power = 1.4
                    }
                }
                modifier = {
                    factor = 1.2
                    relative_power_including_allies_defender = {
                        who = FROM
                        power = 1.6
                    }
                }
                modifier = {
                    factor = 1.2
                    relative_power_including_allies_defender = {
                        who = FROM
                        power = 1.8
                    }
                }
                modifier = {
                    factor = 1.5
                    relative_power_including_allies_defender = {
                        who = FROM
                        power = 2
                    }
                }
                modifier = {
                    factor = 3
                    relative_power_including_allies_defender = {
                        who = FROM
                        power = 3
                    }
                }
                FROM = { letter_event = { id = JD.60306 days = 5 } }
            }
            70 = { #Yes
                modifier = {
                    factor = 0
                    has_status = china_civil_war
                }
                modifier = {
                    factor = 2
                    FROM = { is_liked_by_China_trigger = yes }
                }
                modifier = {
                    factor = 1.2
                    FROM = { diplomacy >= 12 }
                }
                modifier = {
                    factor = 1.2
                    FROM = { diplomacy >= 14 }
                }
                modifier = {
                    factor = 1.2
                    FROM = { diplomacy >= 16 }
                }
                modifier = {
                    factor = 1.2
                    FROM = { diplomacy >= 18 }
                }
                modifier = {
                    factor = 1.5
                    FROM = { diplomacy >= 20 }
                }
                modifier = {
                    factor = 1.2
                    NOT = {
                        relative_power_including_allies_defender = {
                            who = FROM
                            power = 1
                        }
                    }
                }
                modifier = {
                    factor = 1.2
                    NOT = {
                        relative_power_including_allies_defender = {
                            who = FROM
                            power = 0.8
                        }
                    }
                }
                modifier = {
                    factor = 1.2
                    NOT = {
                        relative_power_including_allies_defender = {
                            who = FROM
                            power = 0.6
                        }
                    }
                }
                modifier = {
                    factor = 1.2
                    NOT = {
                        relative_power_including_allies_defender = {
                            who = FROM
                            power = 0.4
                        }
                    }
                }
                modifier = {
                    factor = 1.5
                    NOT = {
                        relative_power_including_allies_defender = {
                            who = FROM
                            power = 0.2
                        }
                    }
                }
                modifier = {
                    factor = 5
                    NOT = {
                        relative_power_including_allies_defender = {
                            who = FROM
                            power = 0.1
                        }
                    }
                }
                FROM = {
                    random_list = { #Decide demands, add to trigger_switch in next event and GetChineseForceOpenDemand custom loc
                        100 = { 
                            trigger = { 
                                is_tributary = no
                                independent = yes
                            }
                            modifier = {
                                factor = 2
                                offmap_china = { offmap_has_like = likes_tributaries }
                            }
                            modifier = {
                                factor = 2
                                offmap_china = { offmap_has_dislike = likes_tributaries }
                            }
                            set_character_flag = force_open_china_demands_tribute
                        }
                        75 = {
                            trigger = {
                                any_trade_post = {
                                    trade_route = silk_road
                                }
                            }
                            set_character_flag = force_open_china_demands_trade_post
                        }
                        25 = { 
                            trigger = { 
                                wealth = 2000
                                scaled_wealth = 2
                            }
                            modifier = {
                                factor = 2
                                offmap_china = { offmap_has_like = likes_wealth }
                            }
                            modifier = {
                                factor = 2
                                offmap_china = { offmap_has_dislike = likes_wealth }
                            }
                            set_character_flag = force_china_open_demands_wealth
                        }
                        25 = {
                            trigger = {
                                any_courtier = {
                                    NOR = {
                                        has_character_flag = originated_from_chinese_court
                                        has_character_flag = eunuch_gift
                                        is_landed = yes
                                        is_female = yes
                                        is_betrothed = yes
                                        spouse = { always = yes }
                                        trait = incapable
                                        trait = inbred
                                        trait = imbecile
                                    }
                                    OR = {
                                        is_close_relative = ROOT_FROM
                                        diplomacy = 20
                                        martial = 20
                                        stewardship = 20
                                        intrigue = 20
                                        learning = 20
                                        trait = strong
                                        trait = genius
                                        trait = quick
                                        trait = fair
                                        trait = robust
                                        trait = shrewd
                                        trait = architect
                                        trait = strategist
                                        trait = scholar
                                        trait = faqih
                                    }
                                    OR = {
                                        is_adult = yes
                                        AND = {
                                            is_close_relative = ROOT_FROM
                                            age = 12
                                        }
                                    }
                                    is_heir = no
                                    is_ill = no
                                    is_inaccessible_trigger = no
                                }
                            }
                            modifier = {
                                factor = 2
                                offmap_china = { offmap_has_like = likes_eunuchs }
                            }
                            modifier = {
                                factor = 2
                                offmap_china = { offmap_has_dislike = likes_eunuchs }
                            }
                            random_courtier = {
                                limit = { 
                                    NOR = {
                                        has_character_flag = originated_from_chinese_court
                                        has_character_flag = eunuch_gift
                                        is_landed = yes
                                        is_female = yes
                                        is_betrothed = yes
                                        spouse = { always = yes }
                                        trait = incapable
                                        trait = inbred
                                        trait = imbecile
                                    }
                                    OR = {
                                        is_close_relative = ROOT_FROM
                                        diplomacy = 20
                                        martial = 20
                                        stewardship = 20
                                        intrigue = 20
                                        learning = 20
                                        trait = strong
                                        trait = genius
                                        trait = quick
                                        trait = fair
                                        trait = robust
                                        trait = shrewd
                                        trait = architect
                                        trait = strategist
                                        trait = scholar
                                        trait = faqih
                                    }
                                    OR = {
                                        is_adult = yes
                                        AND = {
                                            is_close_relative = ROOT_FROM
                                            age = 12
                                        }
                                    }
                                    is_heir = no
                                    is_ill = no
                                    is_inaccessible_trigger = no
                                }
                                save_event_target_as = target_courtier
                            }
                            set_character_flag = force_open_china_demands_eunuch
                        }
                        25 = {
                            trigger = {
                                any_courtier = {
                                    OR = {
                                        AND = {
                                            offmap_china = {
                                                offmap_ruler = {
                                                    is_female = yes
                                                }
                                            }
                                            is_female = no
                                        }
                                        AND = {
                                            offmap_china = {
                                                offmap_ruler = {
                                                    is_female = no
                                                }
                                            }
                                            is_female = yes
                                        }
                                    }
                                    is_close_relative = ROOT_FROM
                                    is_adult = yes
                                    NOT = { age = 45 }
                                    NOR = {
                                        has_character_flag = originated_from_chinese_court
                                        is_landed = yes
                                        is_betrothed = yes
                                        is_married = yes
                                        trait = incapable
                                        trait = inbred
                                        trait = imbecile
                                        trait = celibate
                                    }
                                    is_heir = no
                                    is_ill = no
                                    NOT = { trait = wounded }
                                    is_inaccessible_trigger = no
                                }
                            }
                            modifier = {
                                factor = 2
                                offmap_china = { offmap_has_like = likes_concubines }
                            }
                            modifier = {
                                factor = 2
                                offmap_china = { offmap_has_dislike = likes_concubines }
                            }
                            random_courtier = {
                                limit = { 
                                    OR = {
                                        AND = {
                                            offmap_china = {
                                                offmap_ruler = {
                                                    is_female = yes
                                                }
                                            }
                                            is_female = no
                                        }
                                        AND = {
                                            offmap_china = {
                                                offmap_ruler = {
                                                    is_female = no
                                                }
                                            }
                                            is_female = yes
                                        }
                                    }
                                    is_close_relative = ROOT_FROM
                                    is_adult = yes
                                    NOT = { age = 45 }
                                    NOR = {
                                        has_character_flag = originated_from_chinese_court
                                        is_landed = yes
                                        is_betrothed = yes
                                        is_married = yes
                                        trait = incapable
                                        trait = inbred
                                        trait = imbecile
                                        trait = celibate
                                    }
                                    is_heir = no
                                    is_ill = no
                                    NOT = { trait = wounded }
                                    is_inaccessible_trigger = no
                                }
                                save_event_target_as = target_courtier
                            }
                            set_character_flag = force_open_china_demands_concubine
                        }
                        50 = {
                            trigger = {
                                any_courtier = {
                                    has_character_flag = originated_from_chinese_court
                                }
                            }
                            set_character_flag = force_open_china_demands_return_chinese
                        }
                    }
                    if = {
                        limit = {
                            NOR = {
                                has_character_flag = force_open_china_demands_tribute
                                has_character_flag = force_open_china_demands_trade_post
                                has_character_flag = force_open_china_demands_wealth
                                has_character_flag = force_open_china_demands_eunuch
                                has_character_flag = force_open_china_demands_concubine
                                has_character_flag = force_open_china_demands_return_chinese
                            }
                        }
                        set_character_flag = force_open_china_demands_backup
                    }
                    letter_event = { id = JD.60307 days = 5 }
                }  
            }
        }
    }
}
#Governor says no
letter_event = {
    id = JD.60306
	has_dlc = "Jade Dragon"
    desc = EVT_DESC_JD_60306
    is_triggered_only = yes

    option = { #Then war it is!
        name = EVT_OPTB_60306
		sound_effect = china_angered_emperor
        detract_grace_super_huge_effect = yes
        war = {
            casus_belli = force_open_china_cb
            target = event_target:target_governor
        }
        custom_tooltip = { text = force_china_open_cb_outcome_tt }
    }
    option = { #I shall not press the matter
        name = EVT_OPTA_60306
        detract_grace_minor_effect = yes
        hidden_effect = { offmap_china = { clr_offmap_tmp_flag = china_force_open_in_progress } }
    }
}
#Governor says yes
letter_event = {
    id = JD.60307
	has_dlc = "Jade Dragon"
    desc = EVT_DESC_JD_60307
    is_triggered_only = yes

    option = { #Get their demands
        name = EVT_OPTA_60307
        trigger_switch = {
            on_trigger = has_character_flag
            force_open_china_demands_tribute = {
                FROM = { 
                    show_scope_change = no
                    make_tributary = { who = ROOT tributary_type = offmap }
                    custom_tooltip = { text = chinese_grace_become_tributary_effect_TT } #To get the correct FROM in tooltip
                }
            }
            force_open_china_demands_trade_post = {
                random_trade_post = {
                    limit = {
                        trade_route = silk_road
                    }
                    destroy_tradepost = THIS
                }
            }
            force_open_china_demands_wealth = {
                scaled_wealth = { value = -2 min = -2000 }
            }
            force_open_china_demands_eunuch = {
                event_target:target_courtier = { 
                    show_scope_change = no
                    hidden_effect = {
                        if = {
                            limit = {
                                NOT = {
                                    trait = eunuch
                                }
                            }
                            add_trait = eunuch
                        }
                    }
					set_character_flag = ignore_refund
                    leave_court_for_china_effect = yes
                }
            }
            force_open_china_demands_concubine = {
                event_target:target_courtier = { 
                    show_scope_change = no
					set_character_flag = ignore_refund
                    leave_court_for_china_effect = yes
                }
            }
            force_open_china_demands_return_chinese = {
                any_courtier = {
                    limit = { has_character_flag = originated_from_chinese_court }
					set_character_flag = ignore_refund
                    return_to_china_effect = yes
                }
            }
            force_open_china_demands_backup = {
                wealth = -2000
            }
        }
        detract_grace_major_effect = yes
        offmap_china = { 
            set_offmap_flag = no_policy_news
            set_policy = china_open
			log = "-------------------------------------"
			log = "China Policy Logging:"
			log = "Chinas new Policy is now OPEN"
			log = "-------------------------------------"
            set_offmap_tmp_flag = china_forced_open
            custom_tooltip = { text = china_forced_open_tt }
            clr_offmap_flag = no_policy_news
            clr_offmap_tmp_flag = china_force_open_in_progress
        }
        hidden_effect = {
            any_player = {
                limit = {
                    has_offmap_news_enabled = offmap_china
                    is_within_diplo_range = event_target:target_governor
                }
                narrative_event = { id = JD.60308 }
            }
        }
    }
    option = { #Then war it is!
        name = EVT_OPTC_60307
		sound_effect = china_angered_emperor
        detract_grace_super_huge_effect = yes
        war = {
            casus_belli = force_open_china_cb
            target = event_target:target_governor
        }
        custom_tooltip = { text = force_china_open_cb_outcome_tt }
    }
    option = { #I shall not press the matter
        name = EVT_OPTB_60307
        detract_grace_medium_effect = yes
        hidden_effect = { offmap_china = { clr_offmap_tmp_flag = china_force_open_in_progress } }
    }

    after = { clear_flags_with_prefix = force_open_china_demands_ }
}
#News Event: Negotiations end, China opens up
narrative_event = {
    id = JD.60308
	has_dlc = "Jade Dragon"
    title = NEWS_FROM_CHINA
    desc = EVTDESC_JD_60308
    picture = GFX_evt_china_open
    portrait = offmap_china
	window = EventWindowOffmap
	background = GFX_event_window_news_from_china
    is_triggered_only = yes
	
	immediate = {
		offmap_china = {
			ruler = {
				save_event_target_as = portrait_target
			}
		}
	}
	
	portrait = event_target:portrait_target

    option = {
        name = EVT_OPTA_JD_60308
        trigger = { FROM = { character = ROOT } }
    }
    option = {
        name = EVT_OPTB_JD_60308
        trigger = { NOT = { FROM = { character = ROOT } } }
    }
}

### Grace courtier dies, inform ###
character_event = {
    id = JD.60310
    hide_window = yes
    is_triggered_only = yes
    has_character_flag = originated_from_chinese_court
    has_dlc = "Jade Dragon"
    only_rulers = no
	
	trigger = {
		NOT = {
			has_character_flag = ignore_refund
		}
	}

    immediate = {
        if = {
            limit = {
                NOT = { had_character_flag = { flag = originated_from_chinese_court days = 1825 } }
            }
            trigger_switch = {
                on_trigger = has_character_flag
                is_chinese_administrator = { liege = { set_variable = { which = "grace_refund" value = 1000 } } }
                is_chinese_master_engineer = { liege = { set_variable = { which = "grace_refund" value = 1000 } } }
                is_chinese_siege_engineer = { liege = { set_variable = { which = "grace_refund" value = 250 } } }
                is_chinese_imperial_marriage = { liege = { set_variable = { which = "grace_refund" value = 1000 } } }
                is_chinese_strategist = { liege = { set_variable = { which = "grace_refund" value = 750 } } }
                is_chinese_physician = { liege = { set_variable = { which = "grace_refund" value = 250 } } }
            }
        }
        else = { liege = { set_variable = { which = "grace_refund" value = 0 } } }
        liege = {
        	save_event_target_as = grace_courtier_liege
        }
        offmap_china = { governor = { character_event = { id = JD.60311 } } }
    }
}
character_event = { #Bounce
	id = JD.60311
	has_dlc = "Jade Dragon"
	is_triggered_only = yes
	hide_window = yes
	immediate = {
		event_target:grace_courtier_liege = { letter_event = { id = JD.60312 } }
	}
}
letter_event = {
    id = JD.60312
	has_dlc = "Jade Dragon"
    desc = EVT_DESC_JD_60312
    is_triggered_only = yes

    option = {
    	name = EVT_OPTA_JD_60312
    	show_portrait = FROMFROM
    	if = {
    		limit = { check_variable = { which = "grace_refund" value > 0 } }
    		add_offmap_currency = { offmap = offmap_china value = grace_refund }
    		hidden_effect = { set_variable = { which = "grace_refund" value = 0 } }
    	}
    }
}