# Supported types:
# character, spouse, rivals, friends, liege_titles, vassal_titles, realm_titles, realm_characters, realm_characters_plus, independent_rulers

###############
# AMBITIONS
###############

# Create a treasury
obj_create_treasury = {
	type = character

	potential = {
		has_dlc = "Holy Fury"
		is_playable = yes
		is_adult = yes
		is_incapable = no
		is_inaccessible_trigger = no

		NOR = {
			has_character_flag = created_treasury

			any_artifact = {
				quality >= 2
			}
		}
	}

	success = {
		custom_tooltip = {
			text = create_treasury_condition_tooltip

			any_artifact = {
				count >= 3
				quality >= 2
			}
		}
	}

	effect = {
		change_stewardship = 1
		set_character_flag = created_treasury

		add_character_modifier = {
			name = impressive_treasury
			years = 5
		}
	}

	abort = {
		is_incapable = yes
	}

	chance = {
		factor = 1
	}
}

obj_make_friends = {
	type = character

	potential = {
		always = no # Plus already had this
		has_dlc = "Holy Fury"
		is_playable = yes
		is_adult = yes
		num_of_friends < 1
		is_incapable = no
		is_inaccessible_trigger = no

		NOR = {
			has_character_flag = made_friends
			trait = shy
		}
	}

	success = {
		custom_tooltip = {
			text = obj_make_friends_condition_tooltip
			num_of_friends >= 1
		}
	}

	effect = {
		change_diplomacy = 1
		set_character_flag = made_friends

		add_character_modifier = {
			name = popular_modifier
			years = 5
		}
	}

	abort = {
		is_incapable = yes
	}

	chance = {
		factor = 1
	}
}

obj_forge_bloodline = {
	type = character

	can_cancel = no

	potential = {
		has_dlc = "Holy Fury"
		is_landed = yes
		is_playable = yes
		higher_real_tier_than = COUNT
		age >= 6
		is_incapable = no

		NOR = {
			trait = content

			any_owned_bloodline = { # Should not already have a created bloodline
				has_bloodline_flag = created_bloodline
				bloodline_is_active_for = PREV
			}
		}

		trigger_if = { # The AI shouldn't create bloodlines when they already have a historical one
			limit = { ai = yes }

			NOT = {
				any_owned_bloodline = {
					has_bloodline_flag = historical_bloodline
					bloodline_is_active_for = PREV
				}
			}
		}

		trigger_if = {
			limit = { has_dynasty_flag = flag_dynasty_forged_bloodline }

			trigger_if = {
				limit = { is_adult = no }

				prestige >= 15000

				OR = {
					trait = willful
					trait = proud
					trait = ambitious
					trait = quick
					trait = genius
				}
			}
			trigger_else = {
				prestige >= 20000
			}
		}
		trigger_else = {
			trigger_if = {
				limit = { is_adult = no }

				prestige >= 2500

				OR = {
					trait = willful
					trait = proud
					trait = ambitious
					trait = quick
					trait = genius
				}
			}
			trigger_else = {
				prestige >= 5000
			}
		}
	}

	creation_effect = {
		if = {
			limit = { age < 12 }

			# Checked to unlock special flavor
			set_character_flag = flag_forge_bloodline_was_child
		}

		# Set variables immediately so that the 0s show in the tooltip
		if = {
			limit = {
				check_variable = {
					which = bloodline_murdered_people
					value < 1
				}
			}

			set_variable = {
				which = bloodline_murdered_people
				value = 0
			}
		}

		if = {
			limit = {
				check_variable = {
					which = bloodline_won_wars
					value < 1
				}
			}

			set_variable = {
				which = bloodline_won_wars
				value = 0
			}
		}

		if = {
			limit = {
				check_variable = {
					which = bloodline_built_holdings
					value < 1
				}
			}

			set_variable = {
				which = bloodline_built_holdings
				value = 0
			}
		}

		if = {
			limit = {
				check_variable = {
					which = bloodline_built_cities
					value < 1
				}
			}

			set_variable = {
				which = bloodline_built_cities
				value = 0
			}
		}

		if = {
			limit = {
				check_variable = {
					which = bloodline_built_castles
					value < 1
				}
			}

			set_variable = {
				which = bloodline_built_castles
				value = 0
			}
		}

		if = {
			limit = {
				check_variable = {
					which = bloodline_built_temples
					value < 1
				}
			}

			set_variable = {
				which = bloodline_built_temples
				value = 0
			}
		}

		# If you already fulfilled one of the paths, fire event immediately
		if = {
			limit = {
				check_variable = {
					which = bloodline_murdered_people
					value >= 30
				}
			}

			set_character_flag = flag_about_to_murder_bloodline

			character_event = {
				id = HF.24025
				days = 25
				random = 25
			}
		}
		else_if = {
			limit = {
				check_variable = {
					which = bloodline_won_wars
					value >= 15
				}
			}

			set_character_flag = flag_about_to_war_bloodline

			character_event = {
				id = HF.24031
				days = 25
				random = 25
			}
		}
		else_if = {
			limit = {
				check_variable = {
					which = bloodline_built_holdings
					value >= 24
				}
			}

			set_character_flag = flag_about_to_builder_bloodline

			narrative_event = {
				id = HF.24047
				days = 25
				random = 25
			}
		}
		else_if = {
			limit = {
				check_variable = {
					which = bloodline_built_cities
					value >= 10
				}
			}

			set_character_flag = flag_about_to_builder_bloodline

			narrative_event = {
				id = HF.24042
				days = 25
				random = 25
			}
		}
		else_if = {
			limit = {
				check_variable = {
					which = bloodline_built_castles
					value >= 10
				}
			}

			set_character_flag = flag_about_to_builder_bloodline

			narrative_event = {
				id = HF.24043
				days = 25
				random = 25
			}
		}
		else_if = {
			limit = {
				check_variable = {
					which = bloodline_built_temples
					value >= 10
				}
			}

			set_character_flag = flag_about_to_builder_bloodline

			narrative_event = {
				id = HF.24044
				days = 25
				random = 25
			}
		}

		# Otherwise delayed fire of event bloodlines chain
		else_if = {
			limit = {
				higher_real_tier_than = DUKE
				ai = no
			}

			character_event = {
				id = HF.24052
				days = 800
				random = 800
			}
		}

		# Finally, set counter for years at peace
		set_variable = {
			which = bloodline_peaceful_years
			value = 0
		}

		# And start increasing it. on_started_war actions will reset it
		character_event = {
			id = HF.24020
			days = 365
		}
	}

	success = {
		custom_tooltip = {
			text = obj_forge_bloodline_condition_tooltip

			# Could be either one of the Bloodlines tied to this ambition or one of the other earned bloodlines scattered throughout the game.
			any_owned_bloodline = {
				has_bloodline_flag = created_bloodline
				bloodline_is_active_for = PREV
			}

			# Possible Bloodlines specifically tied to this ambition:
			# Murder Bloodline (assassinate 30 people) 	---> 	(Little Killer/Dreaded/Feast Macabre/Silent)
			# War Bloodline (win 15 wars) 				---> 	(Young Warrior/Knight/Warlord/Warrior Philosopher)
			# Peace Bloodline (ruled 30 years at peace)	--->	(protector/munificent/angel)
			# Architect Bloodline (built 10 cities)
			# Castellan Bloodline (built 10 castles)
			# Monumental Bloodline (built 10 temples)

			# Hybrid (Murder + War)								(20 kills + 10 wars)
			# Hybrid (Murder + Peace)							(20 kills + 10 years)
			# Hybrid (Architect + Castellan + Monumental)		(24 holdings)
			# Hybrid (Murder + Architect/Castellan/Monumental)	(20 kills + 15 holdings)
			# Hybrid (Peace + Architect)						(15 years + 5 cities)
			# Hybrid (Peace + Castellan)						(15 years + 5 castles)
			# Hybrid (Peace + Monumental)						(15 years + 5 temples)

			# Event Bloodlines (event chain, with several variations)
				# Family
				# Technology
				# Religion - Open
				# Religion - Fanatical
				# Benevolent
				# Ruthless

			# Alexander/Ashoka (event chain)
		}
	}

	effect = {
		prestige = 1000
		set_dynasty_flag = flag_dynasty_forged_bloodline
	}

	abort = {
		is_incapable = yes
	}

	abort_effect = {
		prestige = -1000
	}

	chance = {
		factor = 10

		trigger = {
			trait = ambitious

			NOR = {
				# Don't step on the player's dreams, AI.
				any_player = { dynasty = ROOT }

				# The AI shouldn't try to create a new bloodline if they are already the founder of one (from legendary journey, crusades, etc.), but that's fine for the player
				any_owned_bloodline = {
					founder = {
						character = ROOT
					}
				}
			}
		}
	}
}