##################
# Foreign Affairs
##################

# Foreign Affairs opinion base modifier
chancellor_foreign_affairs_opinion_base = {
	add = diplomacy
	divide = 2
}

# Bonus to opinion from Adaptive Traditions
chancellor_foreign_affairs_opinion_max_perk_bonus_modifier = {
	add = chancellor_foreign_affairs_opinion_base
	multiply = {
		add = adaptive_traditions_perk_impact_percent
		divide = 100
	}
}

# Bonus to opinion from Bureaucrats
chancellor_foreign_affairs_opinion_max_erudition_modifier = {
	add = chancellor_foreign_affairs_opinion_base
	multiply = {
		add = erudition_legacy_5_percentage
		divide = 100
	}
}

# Bonus to opinion from Family Business
chancellor_foreign_affairs_opinion_max_family_business_modifier = {
	add = chancellor_foreign_affairs_opinion_base
	multiply = {
		add = family_business_councillors_percentage
		divide = 100
	}
}

# Bonus to opinion from FP3 Consulted House Member
chancellor_foreign_affairs_opinion_max_consulted_house_modifier = {
	add = chancellor_foreign_affairs_opinion_base
	multiply = {
		add = consulted_house_councillors_percentage
		divide = 100
	}
}

# Bonus to opinion from Astounding Presence
chancellor_foreign_affairs_opinion_max_court_fashion_modifier = {
	add = chancellor_foreign_affairs_opinion_base
	multiply = {
		add = 10
		divide = 100
	}
}

# Calculated max opinion
chancellor_foreign_affairs_opinion_max = {
	add = chancellor_foreign_affairs_opinion_base
	# Add bonus from Clerical Justification:
	if = {
		limit = {
			scope:councillor_liege = { has_perk = adaptive_traditions_perk }
		}
		add = chancellor_foreign_affairs_opinion_max_perk_bonus_modifier
	}
	# Add bonus from Bureaucrats
	if = {
		limit = {
			councillor_liege_has_erudition_legacy_5_perk = yes
		}
		add = chancellor_foreign_affairs_opinion_max_erudition_modifier
	}
	# Add bonus from Family Business
	if = {
		limit = {
			councillor_liege_has_family_business_bonus = yes
		}
		add = chancellor_foreign_affairs_opinion_max_family_business_modifier
	}
	# Add bonus from FP3 Consulted House
	if = {
		limit = {
			councillor_liege_has_consulted_house_bonus = yes
		}
		add = chancellor_foreign_affairs_opinion_max_consulted_house_modifier
	}
	# Add bonus from Astounding Presence
	if = {
		limit = {
			scope:councillor_liege = { has_character_modifier = court_fashion_foreign_modifier }
		}
		add = chancellor_foreign_affairs_opinion_max_court_fashion_modifier
	}
}

# Set to max for AI, or gradual scale for players
chancellor_foreign_affairs_realm_opinion_scale = {
	add = 0
	if = {
		limit = {
			scope:councillor_liege = {
				is_independent_ruler = no
				is_ai = yes
			}
		}
		add = chancellor_foreign_affairs_opinion_max
	}
	else_if = {
		limit = {
			scope:councillor_liege = {
				is_independent_ruler = no
				has_variable = chancellor_foreign_affairs_opinion_value
			}
		}
		add = liege.var:chancellor_foreign_affairs_opinion_value
	}
}

chancellor_foreign_affairs_independent_opinion_scale = {
	add = 0
	if = {
		limit = {
			scope:councillor_liege = {
				is_ai = yes
				is_independent_ruler = yes
			}
		}
		add = chancellor_foreign_affairs_opinion_max
	}
	else_if = {
		limit = {
			scope:councillor_liege = {
				is_independent_ruler = yes
				has_variable = chancellor_foreign_affairs_opinion_value
			}
		}
		add = liege.var:chancellor_foreign_affairs_opinion_value
	}
}

# Calculate monthly increase
chancellor_foreign_affairs_monthly_increase = {
	add = chancellor_foreign_affairs_opinion_max
	divide = 24
}

### Prestige gain

# Base prestige gain scale
chancellor_foreign_affairs_prestige_gain_base = {
	add = diplomacy
	divide = 20
}

# Adaptive Traditions bonus
chancellor_foreign_affairs_prestige_gain_perk_bonus_modifier = {
	if = {
		limit = { scope:councillor_liege = { has_perk = adaptive_traditions_perk } }
		add = chancellor_foreign_affairs_prestige_gain_base
		multiply = {
			value = adaptive_traditions_perk_impact_percent
			divide = 100
		}
	}
}

# Bureaucrats bonus
chancellor_foreign_affairs_prestige_gain_erudition_modifier = {
	if = {
		limit = { councillor_liege_has_erudition_legacy_5_perk = yes }
		add = chancellor_foreign_affairs_prestige_gain_base
		multiply = {
			add = erudition_legacy_5_percentage
			divide = 100
		}
	}
}

# Family Business bonus
chancellor_foreign_affairs_prestige_gain_family_business_modifier = {
	if = {
		limit = { councillor_liege_has_family_business_bonus = yes }
		add = chancellor_foreign_affairs_prestige_gain_base
		multiply = {
			add = family_business_councillors_percentage
			divide = 100
		}
	}
}

# FP3 Consulted House bonus
chancellor_foreign_affairs_prestige_gain_consulted_house_modifier = {
	if = {
		limit = { councillor_liege_has_consulted_house_bonus = yes }
		add = chancellor_foreign_affairs_prestige_gain_base
		multiply = {
			add = consulted_house_councillors_percentage
			divide = 100
		}
	}
}

# Total prestige gained per month through foreign affairs, including all modifiers
chancellor_foreign_affairs_prestige_total = {
	add = chancellor_foreign_affairs_prestige_gain_base
	add = chancellor_foreign_affairs_prestige_gain_perk_bonus_modifier
	add = chancellor_foreign_affairs_prestige_gain_erudition_modifier
	add = chancellor_foreign_affairs_prestige_gain_family_business_modifier
	add = chancellor_foreign_affairs_prestige_gain_consulted_house_modifier
}

####################
# Domestic Affairs
####################
chancellor_domestic_affairs_opinion_base = {
	add = diplomacy
	divide = 2
}

chancellor_domestic_affairs_opinion_chains_of_loyalty_bonus = {
	add = chancellor_domestic_affairs_opinion_base
	multiply = {
		add = chains_of_loyalty_percentage
		divide = 100
	}
}

chancellor_domestic_affairs_opinion_erudition_bonus = {
	add = chancellor_domestic_affairs_opinion_base
	multiply = {
		add = erudition_legacy_5_percentage
		divide = 100
	}
}

chancellor_domestic_affairs_opinion_family_business_bonus = {
	add = chancellor_domestic_affairs_opinion_base
	multiply = {
		add = family_business_councillors_percentage
		divide = 100
	}
}

chancellor_domestic_affairs_opinion_consulted_house_bonus = {
	add = chancellor_domestic_affairs_opinion_base
	multiply = {
		add = consulted_house_councillors_percentage
		divide = 100
	}
}

chancellor_domestic_affairs_opinion_pandidakterion_bonus = {
	add = chancellor_domestic_affairs_opinion_base
	multiply = {
		add = 20
		divide = 100
	} 
}

chancellor_domestic_affairs_opinion_food_bonus = {
	add = chancellor_domestic_affairs_opinion_base
	multiply = {
		add = 15
		divide = 100
	} 
}

chancellor_domestic_affairs_opinion_cupbearer_bonus = {
	add = chancellor_domestic_affairs_opinion_base
	multiply = {
		add = 20
		divide = 100
	} 
}

chancellor_domestic_affairs_opinion_max = {
	add = chancellor_domestic_affairs_opinion_base
	if = {
		limit = {
			scope:councillor_liege = { has_perk = chains_of_loyalty_perk }
		}
		add = chancellor_domestic_affairs_opinion_chains_of_loyalty_bonus
	}
	if = {
		limit = {
			councillor_liege_has_erudition_legacy_5_perk = yes
		}
		add = chancellor_domestic_affairs_opinion_erudition_bonus
	}
	if = {
		limit = {
			councillor_liege_has_family_business_bonus = yes
		}
		add = chancellor_domestic_affairs_opinion_family_business_bonus
	}
	if = {
		limit = {
			councillor_liege_has_consulted_house_bonus = yes
		}
		add = chancellor_domestic_affairs_opinion_consulted_house_bonus
	}
	if = {
		limit = {
			scope:councillor_liege = {
				has_title = title:c_byzantion
				title:c_byzantion = { has_county_modifier = pandidakterion_university_modifier }
			}
		}
		add = chancellor_domestic_affairs_opinion_pandidakterion_bonus
	}
	if = {
		limit = {
			scope:councillor_liege = {
				has_character_flag = food_improves_opinion
			}
		}
		add = chancellor_domestic_affairs_opinion_food_bonus
	}
}

chancellor_domestic_affairs_opinion_scale = {
	value = 0
	if = {
		limit = {
			scope:councillor_liege = {
				is_ai = yes
				is_independent_ruler = yes
			}
		}
		add = chancellor_domestic_affairs_opinion_max
	}
	else_if = {
		limit = {
			scope:councillor_liege = { has_variable = chancellor_domestic_affairs_opinion_value }
		}
		add = scope:councillor_liege.var:chancellor_domestic_affairs_opinion_value
	}
}

chancellor_domestic_affairs_monthly_increase = {
	add = chancellor_domestic_affairs_opinion_max
	divide = 24
}

chancellor_domestic_affairs_tyranny_base = {
	add = diplomacy
	#multiply = 2
	min = 1
}

chancellor_domestic_affairs_tyranny_chains_of_loyalty_bonus = {
	value = 0
	if = {
		limit = {
			scope:councillor_liege = { has_perk = chains_of_loyalty_perk }
		}
		add = {
			value = chancellor_domestic_affairs_tyranny_base
			multiply = {
				add = chains_of_loyalty_percentage
				divide = 100
			}
		}
	}
}

chancellor_domestic_affairs_tyranny_erudition_bonus = {
	value = 0
	if = {
		limit = {
			councillor_liege_has_erudition_legacy_5_perk = yes
		}
		add = {
			value = chancellor_domestic_affairs_tyranny_base
			multiply = {
				add = erudition_legacy_5_percentage
				divide = 100
			}
		}
	}
}

chancellor_domestic_affairs_tyranny_family_business_bonus = {
	value = 0
	if = {
		limit = {
			councillor_liege_has_family_business_bonus = yes
		}
		add = {
			value = chancellor_domestic_affairs_tyranny_base
			multiply = {
				add = family_business_councillors_percentage
				divide = 100
			}
		}
	}
}

chancellor_domestic_affairs_tyranny_consulted_house_bonus = {
	value = 0
	if = {
		limit = {
			councillor_liege_has_consulted_house_bonus = yes
		}
		add = {
			value = chancellor_domestic_affairs_tyranny_base
			multiply = {
				add = consulted_house_councillors_percentage
				divide = 100
			}
		}
	}
}

chancellor_domestic_affairs_tyranny_food_bonus = {
	value = 0
	if = {
		limit = {
			scope:councillor_liege = {
				has_character_flag = food_improves_opinion
			}
		}
		add = {
			value = chancellor_domestic_affairs_tyranny_base
			multiply = {
				add = 10
				divide = 100
			}
		}
	}
}

chancellor_domestic_affairs_tyranny_total = {
	add = chancellor_domestic_affairs_tyranny_base
	add = chancellor_domestic_affairs_tyranny_chains_of_loyalty_bonus
	add = chancellor_domestic_affairs_tyranny_erudition_bonus
	add = chancellor_domestic_affairs_tyranny_family_business_bonus
	add = chancellor_domestic_affairs_tyranny_consulted_house_bonus
	add = chancellor_domestic_affairs_tyranny_food_bonus
}

#####################
# Integrate Title
#####################

chancellor_integrate_title_current_progress = {
	value = 0
	if = {
		limit = { exists = scope:county }
		add = scope:county.active_de_jure_drift_progress
	}
}

chancellor_integrate_title_progress_gain = {
	add = diplomacy
	multiply = 0.035
}

chancellor_integrate_title_erudition_bonus = {
	add = chancellor_integrate_title_progress_gain
	multiply = family_business_councillors_percentage
	divide = 100
}

chancellor_integrate_title_family_business_bonus = {
	add = chancellor_integrate_title_progress_gain
	multiply = family_business_councillors_percentage
	divide = 100
}

chancellor_integrate_title_consulted_house_bonus = {
	add = chancellor_integrate_title_progress_gain
	multiply = consulted_house_councillors_percentage
	divide = 100
}


# Relation impact
chancellor_integrate_title_monthly_increase_friend_bonus = {
	value = chancellor_integrate_title_progress_gain
	multiply = {
		add = council_friend_impact_percentage
		divide = 100
	}
}
chancellor_integrate_title_monthly_increase_best_friend_bonus = {
	value = chancellor_integrate_title_progress_gain
	multiply = {
		add = council_best_friend_impact_percentage
		divide = 100
	}
}
chancellor_integrate_title_monthly_increase_rival_bonus = {
	value = chancellor_integrate_title_progress_gain
	multiply = {
		add = council_rival_impact_percentage
		divide = 100
	}
}
chancellor_integrate_title_monthly_increase_nemesis_bonus = {
	value = chancellor_integrate_title_progress_gain
	multiply = {
		add = council_nemesis_impact_percentage
		divide = 100
	}
}

chancellor_integrate_title_contextual_bonuses = {
	# Relation Bonuses/Penalties
	if = { # Friend
		limit = {
			scope:councillor_liege = {
				has_relation_friend = scope:councillor
				NOT = { has_relation_best_friend = scope:councillor }
			}
		}
		add = {
			value = scope:councillor.chancellor_integrate_title_monthly_increase_friend_bonus
			desc = COUNCILLOR_IS_YOUR_FRIEND
		}
	}
	if = { # Best Friend
		limit = {
			scope:councillor_liege = {
				has_relation_best_friend = scope:councillor
			}
		}
		add = {
			value = scope:councillor.chancellor_integrate_title_monthly_increase_best_friend_bonus
			desc = COUNCILLOR_IS_YOUR_BEST_FRIEND
		}
	}
	if = { # Rival
		limit = {
			scope:councillor_liege = {
				has_relation_rival = scope:councillor
				NOT = { has_relation_nemesis = scope:councillor }
			}
		}
		add = {
			value = scope:councillor.chancellor_integrate_title_monthly_increase_rival_bonus
			desc = COUNCILLOR_IS_YOUR_RIVAL
		}
	}
	if = { # Nemesis
		limit = {
			scope:councillor_liege = {
				has_relation_nemesis = scope:councillor
			}
		}
		add = {
			value = scope:councillor.chancellor_integrate_title_monthly_increase_nemesis_bonus
			desc = COUNCILLOR_IS_YOUR_NEMESIS
		}
	}
	if = {
		limit = {
			councillor_liege_has_erudition_legacy_5_perk = yes
		}
		add = {
			value = scope:councillor.chancellor_integrate_title_erudition_bonus
			desc = INTEGRATE_TITLE_DYNASTY_PERK_BONUS_VALUE
		}
	}
	if = {
		limit = {
			councillor_liege_has_family_business_bonus = yes
		}
		add = {
			value = scope:councillor.chancellor_integrate_title_family_business_bonus
			desc = FAMILY_BUSINESS_BONUS_VALUE
		}
	}
	if = {
		limit = {
			councillor_liege_has_consulted_house_bonus = yes
		}
		add = {
			value = scope:councillor.chancellor_integrate_title_consulted_house_bonus
			desc = CONSULTED_HOUSE_BONUS_VALUE
		}
	}
	# EP3 Promote Integration
	if = {
		limit = {
			scope:county = { has_county_modifier = improve_de_jure_integration_modifier }
			scope:councillor_liege = scope:county.holder.top_liege
		}
		add = {
			value = chancellor_integrate_title_consulted_house_bonus
			desc = improve_de_jure_integration_modifier
		}
	}
	# EP3 Admin Emperor Bonus
	if = {
		limit = { # Task instigator is admin top liege
			scope:councillor_liege = {
				is_independent_ruler = yes
				has_government = administrative_government
			}
		}
		add = {
			value = chancellor_integrate_title_admin_top_liege_bonus
			desc = chancellor_integrate_title_admin_top_liege_bonus_desc
		}
	}
	# EP3 Admin Realm County Bonus
	if = {
		limit = { # Task target is de jure part of an admin empire/kingdom
			NOT = {
				scope:councillor_liege = { has_government = administrative_government }
			}
			trigger_if = {
				limit = { exists = scope:county.empire.holder }
				scope:county.empire.holder = { has_government = administrative_government }
			}
			trigger_else = {
				scope:county.kingdom.holder ?= {
					is_independent_ruler = yes
					has_government = administrative_government
				}
			}
		}
		add = {
			value = chancellor_integrate_title_admin_target_bonus
			desc = chancellor_integrate_title_admin_target_bonus_desc
		}
	}
}

#####################
# Bestow Royal Favor
#####################

chancellor_bestow_royal_favor_base = 5

chancellor_bestow_royal_favor_monthly_change = {
	value = {
		add = scope:councillor.diplomacy
	}
}

chancellor_bestow_royal_favor_progress_gain = {
	add = diplomacy
	multiply = 0.5
}

chancellor_bestow_royal_favor_opinion_increase_per_success_base = {
	add = {
		value = scope:councillor.diplomacy
		multiply = 0.3
		min = 1
		max = 10
		ceiling = yes
	}
}

chancellor_bestow_royal_favor_opinion_increase_per_success_grandeur = {
	add = {
		value = scope:councillor_liege.court_grandeur_current
		multiply = 0.05
		min = 1
		max = 10
		ceiling = yes
	}
}

chancellor_bestow_royal_favor_opinion_gain_erudition_bonus = {
	if = {
		limit = { councillor_liege_has_erudition_legacy_5_perk = yes }
		add = chancellor_bestow_royal_favor_opinion_increase_per_success_base
		multiply = {
			add = erudition_legacy_5_percentage
			divide = 100
		}
		round = yes
	}
}

chancellor_bestow_royal_favor_opinion_gain_family_business_bonus = {
	if = {
		limit = { councillor_liege_has_family_business_bonus = yes }
		add = chancellor_bestow_royal_favor_opinion_increase_per_success_base
		multiply = {
			add = erudition_legacy_5_percentage
			divide = 100
		}
		round = yes
	}
}

chancellor_bestow_royal_favor_opinion_gain_consulted_house_bonus = {
	if = {
		limit = { councillor_liege_has_consulted_house_bonus = yes }
		add = chancellor_bestow_royal_favor_opinion_increase_per_success_base
		multiply = {
			add = consulted_house_councillors_percentage
			divide = 100
		}
		round = yes
	}
}

chancellor_bestow_royal_favor_opinion_increase_per_success = {
	add = chancellor_bestow_royal_favor_opinion_increase_per_success_base
	add = chancellor_bestow_royal_favor_opinion_increase_per_success_grandeur
	add = chancellor_bestow_royal_favor_opinion_gain_erudition_bonus
	add = chancellor_bestow_royal_favor_opinion_gain_family_business_bonus
	add = chancellor_bestow_royal_favor_opinion_gain_consulted_house_bonus
}

# Base prestige gain scale
chancellor_bestow_royal_favor_prestige_gain_base = {
	add = scope:councillor.diplomacy
	divide = 5
	ceiling = yes
	divide = 10
}

# Vassal's base prestige gain scale (static so we can add a character modifier)
chancellor_bestow_royal_favor_prestige_gain_base_vassal = {
	add = 0.5
}

chancellor_bestow_favor_prestige_gain_erudition_modifier = {
	if = {
		limit = { councillor_liege_has_erudition_legacy_5_perk = yes }
		add = chancellor_bestow_royal_favor_prestige_gain_base
		multiply = {
			add = erudition_legacy_5_percentage
			divide = 100
		}
	}
}

chancellor_bestow_favor_prestige_gain_family_business_modifier = {
	if = {
		limit = { councillor_liege_has_family_business_bonus = yes }
		add = chancellor_bestow_royal_favor_prestige_gain_base
		multiply = {
			add = family_business_councillors_percentage
			divide = 100
		}
	}
}

chancellor_bestow_favor_prestige_gain_consulted_house_modifier = {
	if = {
		limit = { councillor_liege_has_consulted_house_bonus = yes }
		add = chancellor_bestow_royal_favor_prestige_gain_base
		multiply = {
			add = consulted_house_councillors_percentage
			divide = 100
		}
	}
}

chancellor_bestow_royal_favor_prestige_total = {
	add = chancellor_bestow_royal_favor_prestige_gain_base
	add = chancellor_bestow_favor_prestige_gain_erudition_modifier
	add = chancellor_bestow_favor_prestige_gain_family_business_modifier
	add = chancellor_bestow_favor_prestige_gain_consulted_house_modifier
}

# Relation impact
chancellor_bestow_royal_favor_base_total = {
	add = chancellor_bestow_royal_favor_base
	add = chancellor_bestow_royal_favor_monthly_change
}

chancellor_bestow_royal_favor_monthly_increase_friend_bonus = {
	value = chancellor_bestow_royal_favor_base_total
	multiply = {
		add = council_friend_impact_percentage
		divide = 100
	}
}
chancellor_bestow_royal_favor_monthly_increase_best_friend_bonus = {
	value = chancellor_bestow_royal_favor_base_total
	multiply = {
		add = council_best_friend_impact_percentage
		divide = 100
	}
}
chancellor_bestow_royal_favor_monthly_increase_rival_bonus = {
	value = chancellor_bestow_royal_favor_base_total
	multiply = {
		add = council_rival_impact_percentage
		divide = 100
	}
}
chancellor_bestow_royal_favor_monthly_increase_nemesis_bonus = {
	value = chancellor_bestow_royal_favor_base_total
	multiply = {
		add = council_nemesis_impact_percentage
		divide = 100
	}
}




### Learn on the Job modifier
chancellor_learn_on_the_job_scale = {
	value = 0
	if = {
		limit = {
			exists = liege
			liege = { has_perk = learn_on_the_job_perk }
		}
		add = diplomacy
		multiply = {
			add = learn_on_the_job_percentage
			divide = 100
		}
		floor = yes
		min = 1
	}
}

### Reliable House modifier
chancellor_reliable_house_scale = {
	value = 0
	if = {
		limit = {
			exists = liege.dynasty
			liege.dynasty = { has_dynasty_perk = fp2_coterie_legacy_2 }
			house = liege.house
		}
		add = diplomacy
		multiply = {
			add = council_reliable_house_percentage
			divide = 100
		}
		floor = yes
		min = 1
	}
}
