# IDs JD.4000 - 4020

namespace = JD

character_event = { # Yearly event for Raiding China
	id = JD.4000

	hide_window = yes
	is_triggered_only = yes # on_yearly_pulse

	has_dlc = "Jade Dragon"

	trigger = {
		has_character_modifier = china_raid_active

		offmap_china = {
			NOT = {
				governor = {
					war_with = ROOT
				}
			}
		}
	}

	immediate = {
		if = { # Remove raiding if no longer allowed to raid
			limit = {
				OR = {
					independent = no
					is_allowed_to_loot = no
					is_landed = no
				}
			}

			remove_character_modifier = china_raid_active

			add_character_modifier = {
				name = china_raid_grace_cd
				years = 5
				hidden = yes
			}

			character_event = { id = JD.4001 }
		}
		else = {
			random_list = { # All raids give Gold + Prestige and Population for Nomads
				400 = { # Only Gold + Prestige
					set_character_flag = normal_raid_outcome
					character_event = { id = JD.4020 }
				}

				30 = { # Prisoner of opposite sex with attractive trait
					trigger = { is_adult = yes }

					set_character_flag = normal_raid_outcome_character
					character_event = { id = JD.4020 }
				}

				30 = { # Captured siege engineer
					set_character_flag = normal_raid_outcome_siege_engineer
					character_event = { id = JD.4020 }
				}

				30 = { # Slightly more gold + prestige
					set_character_flag = normal_raid_outcome_extra
					character_event = { id = JD.4020 }
				}

				30 = { # Bad outcome
					set_character_flag = normal_raid_outcome_bad
					character_event = { id = JD.4020 }
				}

				15 = { # Minor artifact
					set_character_flag = normal_raid_outcome_artifact
					character_event = { id = JD.4020 }
				}

				5 = { # An enormous amount of gold + prestige
					set_character_flag = normal_raid_outcome_extra_plus
					character_event = { id = JD.4020 }
				}

				5 = { # A panda
					trigger = {
						NOT = { has_character_modifier = panda_pet }
					}

					set_character_flag = normal_raid_outcome_panda
					character_event = { id = JD.4020 }
				}
			}
		}
	}
}

character_event = { # No longer allowed to raid
	id = JD.4001
	desc = EVTDESC_JD_4001
	picture = GFX_evt_china_stable
	border = GFX_event_normal_frame_war

	is_triggered_only = yes
	notification = yes

	has_dlc = "Jade Dragon"
	ai = no

	option = {
		name = EVTOPTA_JD_4001
	}
}

character_event = { # Normal Outcome
	id = JD.4020
	picture = GFX_evt_china_unrest
	border = GFX_event_normal_frame_war

	desc = {
		text = EVTDESC_JD_4020_normal_raid_outcome
		trigger = { has_character_flag = normal_raid_outcome }
	}
	desc = {
		text = EVTDESC_JD_4020_normal_raid_outcome_artifact
		trigger = { has_character_flag = normal_raid_outcome_artifact }
	}
	desc = {
		text = EVTDESC_JD_4020_normal_raid_outcome_character
		trigger = { has_character_flag = normal_raid_outcome_character }
	}
	desc = {
		text = EVTDESC_JD_4020_normal_raid_outcome_siege_engineer
		trigger = { has_character_flag = normal_raid_outcome_siege_engineer }
	}
	desc = {
		text = EVTDESC_JD_4020_normal_raid_outcome_extra
		trigger = { has_character_flag = normal_raid_outcome_extra }
	}
	desc = {
		text = EVTDESC_JD_4020_normal_raid_outcome_extra_plus
		trigger = { has_character_flag = normal_raid_outcome_extra_plus }
	}
	desc = {
		text = EVTDESC_JD_4020_normal_raid_outcome_bad
		trigger = { has_character_flag = normal_raid_outcome_bad }
	}
	desc = {
		text = EVTDESC_JD_4020_normal_raid_outcome_panda
		trigger = { has_character_flag = normal_raid_outcome_panda }
	}

	is_triggered_only = yes

	has_dlc = "Jade Dragon"

	immediate = {
		if = {
			limit = { has_character_flag = normal_raid_outcome_character }

			if = {
				limit = { is_female = yes }

				create_character = {
					age = 20
					dynasty = actually_culture
					religion = taoist
					culture = han
					female = no
					health = 6
					random_traits = yes
				}
			}
			else = {
				create_character = {
					age = 20
					dynasty = actually_culture
					religion = taoist
					culture = han
					female = yes
					health = 6
					random_traits = yes
				}
			}

			new_character = {
				remove_trait = clubfooted
				remove_trait = harelip
				remove_trait = hunchback
				remove_trait = lisp
				remove_trait = stutter
				remove_trait = ugly
				remove_trait = dwarf
				remove_trait = weak

				add_trait = attractive

				random = {
					chance = 35
					add_trait = strong
				}

				random = {
					chance = 35
					add_trait = quick
				}

				save_event_target_as = captured_character
			}
		}
		else_if = {
			limit = { has_character_flag = normal_raid_outcome_siege_engineer }

			create_character = {
				age = 32
				dynasty = actually_culture
				religion = taoist
				culture = han
				female = no
				health = 6
				random_traits = yes

				attributes = {
					diplomacy = 2
					learning = 2
					stewardship = 2
					intrigue = 2
					martial = 13
				}
			}

			new_character = {
				set_character_flag = originated_from_chinese_court
				set_character_flag = no_court_invites
				set_character_flag = taught_chinese_strategy
				set_character_flag = is_chinese_siege_engineer
				set_character_flag = ignore_refund

				remove_trait = weak
				remove_trait = slow
				remove_trait = imbecile
				remove_trait = dull

				remove_trait = craven
				remove_trait = mystic
				remove_trait = socializer
				remove_trait = game_master
				remove_trait = hedonist
				remove_trait = duelist
				remove_trait = hunter
				remove_trait = strategist
				remove_trait = administrator
				remove_trait = architect
				remove_trait = gardener
				remove_trait = master_schemer
				remove_trait = impaler
				remove_trait = master_seducer
				remove_trait = master_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 = brawny }
					10 = { add_trait = duelist }
					10 = { add_trait = architect }
					 5 = { add_trait = strategist }
					 5 = { add_trait = shrewd }
					 5 = { change_martial = 4 }

					 5 = {
						if = {
							limit = { has_dlc = "Reapers" }
							add_trait = one_eyed
						}
					}
				}

				save_event_target_as = captured_engineer
			}
		}
	}

	option = {
		name = EVTOPT_JD_4020_normal_raid_outcome

		trigger = { has_character_flag = normal_raid_outcome }

		prestige = 50

		if = {
			limit = { is_nomadic = yes }
			scaled_wealth = { value = 0.25 min = 30 max = 100 }
			population = 150
		}
		else = {
			scaled_wealth = { value = 0.25 min = 15 max = 75 }
		}

		clr_character_flag = normal_raid_outcome
	}

	option = {
		name = EVTOPT_JD_4020_normal_raid_outcome_artifact

		trigger = { has_character_flag = normal_raid_outcome_artifact }

		prestige = 75

		if = {
			limit = { is_nomadic = yes }
			scaled_wealth = { value = 0.75 min = 70 max = 175 }
			population = 150
		}
		else = {
			scaled_wealth = { value = 0.75 min = 30 max = 125 }
		}

		custom_tooltip = {
			text = artifact_raid_tt

			hidden_effect = {
				random_list = {
					10 = {
						trigger = {
							NOT = { has_artifact = chinese_artwork }
						}

						add_artifact = chinese_artwork

						new_artifact = {
							set_name = chinese_artwork_name
						}
					}

					10 = {
						trigger = {
							NOT = { has_artifact = chinese_calligraphy }
						}

						add_artifact = chinese_calligraphy

						new_artifact = {
							set_name = chinese_calligraphy_name
						}
					}

					10 = {
						trigger = {
							NOT = { has_artifact = chinese_sculpture }
						}

						add_artifact = chinese_sculpture

						new_artifact = {
							set_name = chinese_sculpture_name
						}
					}

					10 = {
						trigger = {
							NOT = { has_artifact = chinese_ceremonial_robes }
						}

						add_artifact = chinese_ceremonial_robes
					}

					10 = {
						trigger = {
							NOT = { has_artifact = chinese_prev_emperor_sculpture }

							offmap_china = {
								offmap_prev_ruler = {
									always = yes
								}
							}
						}

						add_artifact = chinese_prev_emperor_sculpture

						new_artifact = {
							set_name = chinese_figurine_name
						}
					}

					10 = {
						trigger = {
							NOT = { has_artifact = chinese_arm_protector }
						}

						add_artifact = chinese_arm_protector

						new_artifact = {
							set_name = chinese_arm_protector_name
						}
					}

					10 = {
						trigger = {
							NOT = { has_artifact = chinese_serpent_spear }
						}

						add_artifact = chinese_serpent_spear
					}

					10 = {
						trigger = {
							NOT = { has_artifact = chinese_ji }
						}

						add_artifact = chinese_ji
					}

					10 = {
						trigger = {
							NOT = { has_artifact = chinese_crossbow }
						}

						add_artifact = chinese_crossbow
					}

					10 = {
						trigger = {
							NOT = { has_artifact = chinese_bronze_sculpture }
						}

						add_artifact = chinese_bronze_sculpture

						new_artifact = {
							set_name = chinese_bronze_sculpture_name
						}
					}

					10 = {
						trigger = {
							NOT = { has_artifact = chinese_book_health }
						}

						add_artifact = chinese_book_health

						new_artifact = {
							set_name = chinese_book_health_name
						}
					}

					10 = {
						trigger = {
							NOT = { has_artifact = chinese_book_law }
						}

						add_artifact = chinese_book_law

						new_artifact = {
							set_name = chinese_book_law_name
						}
					}

					10 = {
						trigger = {
							NOT = { has_artifact = chinese_book_history }
						}

						add_artifact = chinese_book_history

						new_artifact = {
							set_name = chinese_book_history_name
						}
					}

					10 = {
						trigger = {
							NOT = { has_artifact = chinese_book_economy }
						}

						add_artifact = chinese_book_economy

						new_artifact = {
							set_name = chinese_book_economy_name
						}
					}

					1 = {
						trigger = {
							NOT = { has_artifact = chinese_painting_of_glitterhoof }
						}

						add_artifact = chinese_painting_of_glitterhoof
					}

					10 = {
						trigger = {
							NOT = { has_artifact = chinese_tapestry }
						}

						random_independent_ruler = {
							limit = {
								any_realm_province = {
									trade_route = silk_road
									held_under_PREV = yes
								}
							}

							random_realm_province = {
								limit = {
									trade_route = silk_road
									held_under_PREV = yes
								}

								save_event_target_as = tapestry_target
							}
						}

						add_artifact = chinese_tapestry

						new_artifact = {
							set_name = chinese_tapestry_name
						}
					}

					10 = {
						trigger = {
							NOT = { has_artifact = chinese_tapestry_grand }
						}

						add_artifact = chinese_tapestry_grand

						new_artifact = {
							set_name = chinese_tapestry_grand_name
						}
					}
				}
			}
		}

		clr_character_flag = normal_raid_outcome_artifact
	}

	option = {
		name = EVTOPT_JD_4020_normal_raid_outcome_character

		trigger = { has_character_flag = normal_raid_outcome_character }

		prestige = 75

		if = {
			limit = { is_nomadic = yes }
			scaled_wealth = { value = 0.6 min = 70 max = 175 }
			population = 150
		}
		else = {
			scaled_wealth = { value = 0.6 min = 30 max = 125 }
		}

		reverse_opinion = {
			name = opinion_abducted
			who = event_target:captured_character
			years = 10
		}

		clr_character_flag = normal_raid_outcome_character
	}

	# TODO: Add better checks for concubinage. And extend to women?
	option = {
		name = EVTOPT_JD_4020_normal_raid_outcome_character_b

		trigger = {
			has_character_flag = normal_raid_outcome_character
			is_female = no
			has_concubinage = yes
			is_opposite_sex = event_target:captured_character
			num_of_consorts < 3
		}

		prestige = 75

		if = {
			limit = { is_nomadic = yes }
			scaled_wealth = { value = 0.6 min = 70 max = 175 }
			population = 150
		}
		else = {
			scaled_wealth = { value = 0.6 min = 30 max = 125 }
		}

		add_consort = event_target:captured_character

		reverse_opinion = {
			name = opinion_abducted
			who = event_target:captured_character
			years = 10
		}

		clr_character_flag = normal_raid_outcome_character
	}

	option = {
		name = EVTOPT_JD_4020_normal_raid_outcome_siege_engineer

		trigger = { has_character_flag = normal_raid_outcome_siege_engineer }

		prestige = 75

		if = {
			limit = { is_nomadic = yes }
			scaled_wealth = { value = 0.6 min = 70 max = 175 }
			population = 150
		}
		else = {
			scaled_wealth = { value = 0.6 min = 30 max = 125 }
		}

		reverse_opinion = {
			name = opinion_abducted
			who = event_target:captured_engineer
			years = 10
		}

		clr_character_flag = normal_raid_outcome_siege_engineer
	}

	option = {
		name = EVTOPT_JD_4020_normal_raid_outcome_siege_engineer_b

		trigger = { has_character_flag = normal_raid_outcome_siege_engineer }

		prestige = 75

		if = {
			limit = { is_nomadic = yes }
			scaled_wealth = { value = 0.6 min = 70 max = 175 }
			population = 150
		}
		else = {
			scaled_wealth = { value = 0.6 min = 30 max = 125 }
		}

		reverse_opinion = {
			name = opinion_abducted
			who = event_target:captured_engineer
			years = 10
		}

		custom_tooltip = {
			text = fire_commander_tt

			event_target:captured_engineer = {
				if = {
					limit = { can_hold_title = title_commander }

					if = {
						limit = {
							ROOT = { can_grant_title = title_commander }
						}

						give_minor_title = title_commander
					}
					else = {
						ROOT = {
							random_courtier_or_vassal = {
								limit = {
									has_minor_title = title_commander

									NOR = {
										has_character_flag = originated_from_chinese_court
										has_character_flag = taught_chinese_strategy
									}
								}

								preferred_limit = {
									lower_real_tier_than = COUNT
									martial < 10
								}

								preferred_limit = {
									lower_real_tier_than = COUNT
								}

								remove_title = title_commander
							}
						}

						give_minor_title = title_commander
					}
				}
			}
		}

		clr_character_flag = normal_raid_outcome_siege_engineer
	}

	option = {
		name = EVTOPT_JD_4020_normal_raid_outcome_extra

		trigger = { has_character_flag = normal_raid_outcome_extra }

		prestige = 150

		if = {
			limit = { is_nomadic = yes }
			scaled_wealth = { value = 1.5 min = 60 max = 300 }
			population = 300
		}
		else = {
			scaled_wealth = { value = 1.5 min = 30 max = 200 }
		}

		clr_character_flag = normal_raid_outcome_extra
	}

	option = {
		name = EVTOPT_JD_4020_normal_raid_outcome_extra_plus

		trigger = { has_character_flag = normal_raid_outcome_extra_plus }

		prestige = 300

		if = {
			limit = { is_nomadic = yes }
			scaled_wealth = { value = 2.5 min = 120 max = 400 }
			population = 600
		}
		else = {
			scaled_wealth = { value = 2.5 min =  60 max = 300 }
		}

		clr_character_flag = normal_raid_outcome_extra_plus
	}

	option = {
		name = EVTOPT_JD_4020_normal_raid_outcome_bad

		trigger = { has_character_flag = normal_raid_outcome_bad }

		prestige = -50
		clr_character_flag = normal_raid_outcome_bad
	}

	option = {
		name = EVTOPT_JD_4020_normal_raid_outcome_panda

		trigger = { has_character_flag = normal_raid_outcome_panda }

		prestige = 200

		add_character_modifier = {
			name = panda_pet
			duration = -1
		}

		if = {
			limit = { is_nomadic = yes }
			scaled_wealth = { value = 1 min = 70 max = 225 }
			population = 200
		}
		else = {
			scaled_wealth = { value = 1 min = 35 max = 150 }
		}

		character_event = {
			id = JD.4030 # Panda dies
			days = 7000
			random = 2000
		}

		clr_character_flag = normal_raid_outcome_panda
	}
}

# Panda dies
character_event = {
	id = JD.4030
	desc = EVTDESC_JD_4030
	picture = GFX_evt_bad_news
	border = GFX_event_normal_frame_religion

	is_triggered_only = yes

	has_dlc = "Jade Dragon"

	trigger = { has_character_modifier = panda_pet }

	option = { # I will miss it
		name = EVTOPTA_JD_4030

		prestige = 5
	}

	option = { # Give in to despair
		name = EVTOPTB_JD_4030

		random = {
			chance = 50
			add_trait_depressed_effect = yes
		}
	}

	after = {
		remove_character_modifier = panda_pet
	}
}