namespace = demesne_from_titles

#######################################
####   Check starting conditions   ####
#######################################
character_event = {		id = demesne_from_titles.11

	is_triggered_only = yes
	hide_window = yes
	only_playable = yes
	
	### Ensure the event will fire only once, since we don't need to recalculate
	### anything after a savegame reload if the mod was used before
	trigger = {
		NOT = { 
			has_global_flag = demesne_from_titles_activated_and_calculated 
		}
	}
	
	immediate = {
		set_global_flag = demesne_from_titles_activated_and_calculated
	
		any_character = {
			limit = {
				primary_title = {
					higher_tier_than = COUNT
				}
			}
			
			#### Calculate demesne bonus
			character_event = { id = demesne_from_titles.21 }
			#### Apply demesne bonus
			character_event = { id = demesne_from_titles.23 }
		}
	}
}


########################################################
####   Evaluate demesne bonus after a title was     ####
####   gain in a war, granted, created or usurped   ####
########################################################
character_event = {		id = demesne_from_titles.12

	is_triggered_only = yes
	hide_window = yes
	only_playable = yes
	
	trigger = {
		FROM = {
			higher_tier_than = COUNT
		}
	}
	
	immediate = {
		ROOT = {
			character_event = { id = demesne_from_titles.21 }
			character_event = { id = demesne_from_titles.22 }
			character_event = { id = demesne_from_titles.23 }
		}
		
		FROMFROM  = {
			character_event = { id = demesne_from_titles.21 }
			character_event = { id = demesne_from_titles.22 }
			character_event = { id = demesne_from_titles.23 }
		}
	}
}


#####################################################
####   Evaluate demesne bonus after succession   ####
#####################################################
character_event = {		id = demesne_from_titles.13
	
	is_triggered_only = yes
	hide_window = yes
	only_playable = yes
	
	trigger = {
		FROM = {
			higher_tier_than = COUNT
		}
	}
	
	immediate = {
		ROOT = {
			character_event = { id = demesne_from_titles.21 }
			character_event = { id = demesne_from_titles.22 }
			character_event = { id = demesne_from_titles.23 }
		}
	}
}


######################################
####   Calculate modifier bonus   ####
######################################
character_event = {		id = demesne_from_titles.21

	is_triggered_only = yes
	hide_window = yes
	only_playable = yes
	
	immediate = {
		
		#### Duke level
		export_to_variable = {
			which = duke_titles
			value = num_of_duke_titles
		}
		
		export_to_variable = {
			which = duke_titles_to_modulo
			value = num_of_duke_titles
		}
				
		modulo_variable = {
			which = duke_titles_to_modulo
			value = 3
		}
		
		subtract_variable = { 
			which = duke_titles 
			which = duke_titles_to_modulo 
		}
		
		divide_variable = { 
			which = duke_titles 
			value = 3 
		}
		
		
		#### King level
		export_to_variable = {
			which = king_titles
			value = num_of_king_titles
		}		
		
		multiply_variable = { 
			which = king_titles
			value = 4
		}
		
		
		#### Emperor level
		export_to_variable = {
			which = emperor_titles
			value = num_of_emperor_titles
		}		
		
		
		multiply_variable = { 
			which = emperor_titles
			value = 4
		}
		
		
		#### Add things up		
		set_variable = { 
			which = grand_total_bonus 
			which = emperor_titles 
		}
		
		change_variable = { 
			which = grand_total_bonus
			which = king_titles
		}
		
		change_variable = { 
			which = grand_total_bonus
			which = duke_titles 
		}
	}
}


#################################################
####   Remove all modifiers from character   ####
#################################################
character_event = {		id = demesne_from_titles.22

	is_triggered_only = yes
	hide_window = yes
	only_playable = yes
	
	immediate = {
		remove_character_modifier = demesne_from_titles_1024
		remove_character_modifier = demesne_from_titles_512
		remove_character_modifier = demesne_from_titles_256
		remove_character_modifier = demesne_from_titles_128
		remove_character_modifier = demesne_from_titles_64
		remove_character_modifier = demesne_from_titles_32
		remove_character_modifier = demesne_from_titles_16
		remove_character_modifier = demesne_from_titles_8
		remove_character_modifier = demesne_from_titles_4
		remove_character_modifier = demesne_from_titles_2
		remove_character_modifier = demesne_from_titles_1
	}
}


########################################
####   Add modifiers to character   ####
########################################
character_event = {		id = demesne_from_titles.23

	is_triggered_only = yes
	hide_window = yes
	only_playable = yes
	
	immediate = {
	
		#### Add modifiers if applicable
		if = {
			limit = {
				check_variable = { which = grand_total_bonus value >= 1024 }
			}
			add_character_modifier = {
				modifier = demesne_from_titles_1024
				duration = -1
			}
		
			subtract_variable = { 
				which = grand_total_bonus
				value = 1024
			}
		
			if = { 
				limit = {
					check_variable = { which = grand_total_bonus value <= 0 }
				}
				break = yes
			}
		}
		
		if = {
			limit = {
				check_variable = { which = grand_total_bonus value >= 512 }
			}
			add_character_modifier = {
				modifier = demesne_from_titles_512
				duration = -1
			}
		
			subtract_variable = { 
				which = grand_total_bonus
				value = 512
			}
		
			if = { 
				limit = {
					check_variable = { which = grand_total_bonus value <= 0 }
				}
				break = yes
			}
		}
		
		if = {
			limit = {
				check_variable = { which = grand_total_bonus value >= 256 }
			}
			add_character_modifier = {
				modifier = demesne_from_titles_256
				duration = -1
			}
		
			subtract_variable = { 
				which = grand_total_bonus
				value = 256
			}
		
			if = { 
				limit = {
					check_variable = { which = grand_total_bonus value <= 0 }
				}
				break = yes
			}
		}
		
		if = {
			limit = {
				check_variable = { which = grand_total_bonus value >= 128 }
			}
			add_character_modifier = {
				modifier = demesne_from_titles_128
				duration = -1
			}
		
			subtract_variable = { 
				which = grand_total_bonus
				value = 128
			}
		
			if = { 
				limit = {
					check_variable = { which = grand_total_bonus value <= 0 }
				}
				break = yes
			}
		}
		
		if = {
			limit = {
				check_variable = { which = grand_total_bonus value >= 64 }
			}
			add_character_modifier = {
				modifier = demesne_from_titles_64
				duration = -1
			}
		
			subtract_variable = { 
				which = grand_total_bonus
				value = 64
			}
		
			if = { 
				limit = {
					check_variable = { which = grand_total_bonus value <= 0 }
				}
				break = yes
			}
		}
		
		if = {
			limit = {
				check_variable = { which = grand_total_bonus value >= 32 }
			}
			add_character_modifier = {
				modifier = demesne_from_titles_32
				duration = -1
			}
		
			subtract_variable = { 
				which = grand_total_bonus
				value = 32
			}
		
			if = { 
				limit = {
					check_variable = { which = grand_total_bonus value <= 0 }
				}
				break = yes
			}
		}
		
		if = {
			limit = {
				check_variable = { which = grand_total_bonus value >= 16 }
			}
			add_character_modifier = {
				modifier = demesne_from_titles_16
				duration = -1
			}
		
			subtract_variable = { 
				which = grand_total_bonus
				value = 16
			}
		
			if = { 
				limit = {
					check_variable = { which = grand_total_bonus value <= 0 }
				}
				break = yes
			}
		}
		
		if = {
			limit = {
				check_variable = { which = grand_total_bonus value >= 8 }
			}
			add_character_modifier = {
				modifier = demesne_from_titles_8
				duration = -1
			}
		
			subtract_variable = { 
				which = grand_total_bonus
				value = 8
			}
		
			if = { 
				limit = {
					check_variable = { which = grand_total_bonus value <= 0 }
				}
				break = yes
			}
		}
		
		if = {
			limit = {
				check_variable = { which = grand_total_bonus value >= 4 }
			}
			add_character_modifier = {
				modifier = demesne_from_titles_4
				duration = -1
			}
		
			subtract_variable = { 
				which = grand_total_bonus
				value = 4
			}
		
			if = { 
				limit = {
					check_variable = { which = grand_total_bonus value <= 0 }
				}
				break = yes
			}
		}
		
		if = {
			limit = {
				check_variable = { which = grand_total_bonus value >= 2 }
			}
			add_character_modifier = {
				modifier = demesne_from_titles_2
				duration = -1
			}
		
			subtract_variable = { 
				which = grand_total_bonus
				value = 2
			}
		
			if = { 
				limit = {
					check_variable = { which = grand_total_bonus value <= 0 }
				}
				break = yes
			}
		}
		
		if = {
			limit = {
				check_variable = { which = grand_total_bonus value >= 1 }
			}
			add_character_modifier = {
				modifier = demesne_from_titles_1
				duration = -1
			}
		
			subtract_variable = { 
				which = grand_total_bonus
				value = 1
			}
		
			if = { 
				limit = {
					check_variable = { which = grand_total_bonus value <= 0 }
				}
				break = yes
			}
		}
	}
}
