﻿on_dynasty_created = {
	effect = {
		if = {
			limit = { dynast = { has_trait = bastard } }
			dynast = {
				remove_trait = bastard
				add_trait = bastard_founder
			}
		}
	}
}

# root = <character>
# scope:dynasty = <dynasty>
# new dynasty head and dynasty affected
on_became_dynasty_head = {
	effect = {
		if = {
			limit = {
				has_trait = denounced
			}
			remove_trait = denounced
		}
		if = {
			limit = {
				has_trait = disinherited
			}
			remove_trait = disinherited
		}
	}
}

# root = <character>
# scope:house = <house>
# new house head and house affected
on_became_house_head = {
	effect = {
		if = {
			limit = {
				has_trait = decadent
			}
			remove_trait = decadent
		}
		if = {
			limit = {
				has_trait = extolled
			}
			remove_trait = extolled
		}
		#Mandala?
		if = {
			limit = { government_has_flag = government_is_mandala }
			#Set up the Serenity modifier
			house = {
				if = {
					limit = { has_house_aspiration_parameter = aspect_of_serenity }
					if = {
						limit = { has_house_head_parameter = peaceful_rule_yearly_modifier }
						house_head = {
							remove_variable = mandala_house_power_accumulated_increments_of_peace
							set_variable = {
								name = mandala_house_power_accumulated_increments_of_peace
								value = 1
							}
							add_character_modifier = { modifier = mandala_increments_of_peace_modifier }
							trigger_event = {
								id = tgp_east_asia_mandala_events.0200
								years = 1
							}
						}
					}
					else_if = {
						limit = { house_has_peaceful_rule_3_year_trigger = yes }
						house_head = {
							remove_variable = mandala_house_power_accumulated_increments_of_peace
							set_variable = {
								name = mandala_house_power_accumulated_increments_of_peace
								value = 1
							}
							add_character_modifier = { modifier = mandala_increments_of_peace_modifier }
							trigger_event = {
								id = tgp_east_asia_mandala_events.0200
								years = 3
							}
						}
					}
					else = {
						house_head = {
							remove_variable = mandala_house_power_accumulated_increments_of_peace
							set_variable = {
								name = mandala_house_power_accumulated_increments_of_peace
								value = 1
							}
							add_character_modifier = { modifier = mandala_increments_of_peace_modifier }
							trigger_event = {
								id = tgp_east_asia_mandala_events.0200
								years = 5
							}
						}
					}
					#Start tracking years of peace
					house_head = {
						#Check if it exists first as this is also applied on_game start and in on_house_aspiration_changed
						if = {
							limit = {
								NOT = { has_variable = serenity_mandala_years_of_non_aggression_war }
							}
							set_variable = {
								name = serenity_mandala_years_of_non_aggression_war
								value = 0
							}
							trigger_event = {
								id = tgp_east_asia_mandala_events.0205
								years = 1
							}
						}
					}
				}
			}	
			#Set up the Creation modifier
			house = {
				if = {
					limit = { has_house_aspiration_parameter = aspect_of_creation }
					if = {
						limit = { has_house_head_parameter = creator_30_year_modifier }
						house_head = {
							remove_variable = mandala_house_power_accumulated_creator
							set_variable = {
								name = mandala_house_power_accumulated_creator
								value = 1
								years = 30
							}
							#We only set the modifier here, no message
							hidden_effect = { mandala_apply_30_year_modifier = yes }
						}
					}
					else_if = {
						limit = { house_has_creator_20_years_trigger = yes }
						house_head = {
							remove_variable = mandala_house_power_accumulated_creator
							set_variable = {
								name = mandala_house_power_accumulated_creator
								value = 1
								years = 20
							}
							#We only set the modifier here, no message
							hidden_effect = { mandala_apply_20_year_modifier = yes }
						}
					}
					else = {
						house_head = {
							remove_variable = mandala_house_power_accumulated_creator
							set_variable = {
								name = mandala_house_power_accumulated_creator
								value = 1
								years = 10
							}
							#We only set the modifier here, no message
							hidden_effect = { mandala_apply_10_year_modifier = yes }
						}
					}	
				}
			}
		}	
	}
}

# root = <house>
# This house inside an admin realm just became dominant.
on_house_in_admin_realm_became_dominant = {
	effect = {
		if = {
			limit = {
				# Get notified only if the dominant family is not the emperor's
				root != root.house_head.top_liege.house
			}
			save_scope_as = noble_family_house
			house_head = {
				save_scope_as = noble_family_head
				top_liege = {
					every_vassal = {
						limit = {
							this = house.house_head
							house != root
						}
						trigger_event = ep3_emperor_yearly.2420 # Reaction
					}
					trigger_event = ep3_emperor_yearly.2420 # Reaction
				}
			}
		}
	}
}

# root = <house>
# This house inside an admin realm just became powerful.
on_house_in_admin_realm_became_powerful = {
	effect = {
		if = {
			limit = {
				months_from_game_start >= 1
			}
			save_scope_as = new_family_house
			house_head = {
				save_scope_as = new_family
				random_held_title = {
					limit = { is_noble_family_title = yes }
					save_scope_as = new_family_title
				}
				trigger_event = ep3_emperor_yearly.2401 # Your own Family reaction
				top_liege = {
					every_vassal = {
						limit = {
							this = house.house_head
							house != root

							# China has hundreds of Noble Families, it gets spammy
							NOT = { government_allows = merit }
						}

						# Notify every vassal house head - If they haven't been notified recently, we send an event, otherwise a feed message will do.
						if = {
							limit = {
								has_character_flag = 8020_powerful_family_cooldown
							}
							send_interface_message = {
								title = 8020_powerful_family_tt
								type = msg_new_powerful_family
								left_icon = scope:new_family
								right_icon = scope:new_family_title
								desc = 8020_powerful_family_desc
							}
						}
						else = {
							trigger_event = ep3_powerful_families.8020 # Powerful Family reaction
							trigger_event = ep3_emperor_yearly.2400 # Non-Powerful Family reaction
						}
					}

					# Let's also notify the liege, but they don't get an event.
					send_interface_message = {
						title = 8020_powerful_family_tt
						type = msg_new_powerful_family
						left_icon = scope:new_family
						right_icon = scope:new_family_title
						desc = 8020_powerful_family_desc
					}
				}
			}
		}
	}
}


# A new relation between two houses has just been created.
# root = <house_relation>
on_house_relation_created = {
	effect = {
		set_variable = {
			name = house_relation_duration_years
			value = 0
		}
	}
}

# An existing relation between two houses has just been changed
# root = <house_relation>
on_house_relation_level_changed = {
	effect = {
		
	}
}

# A relation between two houses is just about to be destroyed.
# root = <house_relation>
on_house_relation_destroyed = {
	effect = {
		
	}
}

# This House's House Power was just upgraded
# root = <character>
# scope:house = <house>
on_house_aspiration_upgraded = {
	effect = {
		#####################
    	# Mandala Government
    	#####################
		if = {
			limit = { government_has_flag = government_is_mandala }
			#Completion memory
			if = {
				limit = {
					house ?= {
						OR = {
							has_house_aspiration_parameter = aspect_of_creation_05
							has_house_aspiration_parameter = aspect_of_serenity_05
							has_house_aspiration_parameter = aspect_of_destruction_05
							has_house_aspiration_parameter = aspect_of_trickery_05
						}
					}
				}
				if = {
					limit = {
						house = { has_house_aspiration_parameter = aspect_of_creation_05 }
					}
					create_character_memory = { type = completed_creation_aspect_memory }
				}
				if = {
					limit = {
						house = { has_house_aspiration_parameter = aspect_of_serenity_05 }
					}
					create_character_memory = { type = completed_serenity_aspect_memory }
				}
				if = {
					limit = {
						house = { has_house_aspiration_parameter = aspect_of_destruction_05 }
					}		
					create_character_memory = { type = completed_destruction_aspect_memory }
				}
				if = {
					limit = {
						house = { has_house_aspiration_parameter = aspect_of_trickery_05 }
					}	
					create_character_memory = { type = completed_trickery_aspect_memory }
				}
			}
			#Upgrade memory
			else_if = {
				limit = {
					house ?= {
						NOR = {
							has_house_aspiration_parameter = aspect_of_creation_01
							has_house_aspiration_parameter = aspect_of_serenity_01
							has_house_aspiration_parameter = aspect_of_destruction_01
							has_house_aspiration_parameter = aspect_of_trickery_01
						}
					}
				}
				if = {
					limit = {
						house = { has_house_aspiration_parameter = aspect_of_creation }
					}
					create_character_memory = { type = upgraded_creation_aspect_memory }
				}
				if = {
					limit = {
						house = { has_house_aspiration_parameter = aspect_of_serenity }
					}
					create_character_memory = { type = upgraded_serenity_aspect_memory }
				}
				if = {
					limit = {
						house = { has_house_aspiration_parameter = aspect_of_destruction }
					}		
					create_character_memory = { type = upgraded_destruction_aspect_memory }
				}
				if = {
					limit = {
						house = { has_house_aspiration_parameter = aspect_of_trickery }
					}	
					create_character_memory = { type = upgraded_trickery_aspect_memory }
				}
			}
			#Go you!
			trigger_event = tgp_east_asia_mandala_events.1000
		}
	}
}

# This House's House Power was changed
# root = <character>
# scope:house = <house>
on_house_aspiration_changed = {
    effect = {
    	#####################
    	# Mandala Government
    	#####################
    	if = {
    		limit = { government_has_flag = government_is_mandala }
	    	#Start tracking years of peace
	    	if = {
	    		limit = {
	    			house = { has_house_head_parameter = peaceful_rule_5_year_modifier }
	    			is_house_head = yes
	    			NOT = { has_variable = mandala_house_power_accumulated_increments_of_peace }
	    		}
	    		set_variable = {
	    			name = mandala_house_power_accumulated_increments_of_peace
	    			value = 1
	    		}
	    		add_character_modifier = { modifier = mandala_increments_of_peace_modifier }
	    		trigger_event = {
	    			id = tgp_east_asia_mandala_events.0200
	    			years = 5
	    		}
	    		#Check if it exists first as this is also applied on_game start
	    		if = {
	    			limit = {
	    				NOT = { has_variable = serenity_mandala_years_of_non_aggression_war }
	    			}
	    			set_variable = {
	    				name = serenity_mandala_years_of_non_aggression_war
	    				value = 0
	    			}
	    			trigger_event = {
	    				id = tgp_east_asia_mandala_events.0205
	    				years = 1
	    			}
	    		}
	    		#Clean up other Aspects if we just swapped to this one
	    		if = {
	    			limit = { has_character_modifier = mandala_creator_modifier }
	    			remove_character_modifier = mandala_creator_modifier
	    			remove_variable = mandala_house_power_accumulated_creator
	    		}
	    	}
	    	#Give or update the Creator modifier
	    	if = {
	    		limit = {
	    			house = { has_house_aspiration_parameter = aspect_of_creation } #Unop aspect_of_creation is a house aspiration parameter 
	    			is_house_head = yes
	    		}
	    		#Save what we gained so far so we can add it to the refreshed variable
	    		if = {
	    			limit = { has_variable = mandala_house_power_accumulated_creator }
	    			save_scope_value_as = {
	    				name = accumulated_creator_increments
	    				value = var:mandala_house_power_accumulated_creator
	    			}
	    			remove_variable = mandala_house_power_accumulated_creator
	    		}
	    		#Maxed out
		    	if = {
					limit = {
						house = { has_house_head_parameter = creator_30_year_modifier }
					}
					set_variable = {
						name = mandala_house_power_accumulated_creator
						value = scope:accumulated_creator_increments
						years = 30
					}
					#We only set the modifier here, no message
					hidden_effect = { mandala_apply_30_year_modifier = yes }
				}
				#Second Tier
				else_if = {
					limit = {
						house = { house_has_creator_20_years_trigger = yes }
					}
					set_variable = {
						name = mandala_house_power_accumulated_creator
						value = scope:accumulated_creator_increments
						years = 20
					}
					#We only set the modifier here, no message
					hidden_effect = { mandala_apply_20_year_modifier = yes }
				}
				#First Tier
				else = {
					if = {
						limit = { exists = scope:accumulated_creator_increments }
						set_variable = {
							name = mandala_house_power_accumulated_creator
							value = scope:accumulated_creator_increments
							years = 10
						}
					}
					else = {
						set_variable = {
							name = mandala_house_power_accumulated_creator
							value = 1
							years = 10
						}
					}
					#We only set the modifier here, no message
					hidden_effect = { mandala_apply_10_year_modifier = yes }
					#Clean up other Aspects if we just swapped to this one
					if = {
						limit = { has_character_modifier = mandala_increments_of_peace_modifier }
						remove_character_modifier = mandala_increments_of_peace_modifier
						remove_variable = mandala_house_power_accumulated_increments_of_peace
					}
				}
	    	}
	    	#Just started on this track
	    	if = {
	    		limit = {
	    			house = { has_house_aspiration_parameter = aspect_of_creation_01 }
	    		}
	    		create_character_memory = { type = picked_creation_aspect_memory }
	    	}
	    	if = {
	    		limit = {
	    			house = { has_house_aspiration_parameter = aspect_of_serenity_01 }
	    		}	
	    		create_character_memory = { type = picked_serenity_aspect_memory }
	    	}
	    	if = {
	    		limit = {
	    			house = { has_house_aspiration_parameter = aspect_of_destruction_01 }
	    		}
	    		create_character_memory = { type = picked_destruction_aspect_memory }
	    	}
	    	if = {
	    		limit = {
	    			house = { has_house_aspiration_parameter = aspect_of_trickery_01 }
	    		}
	    		create_character_memory = { type = picked_trickery_aspect_memory }
	    	}
    	}
	}
}
