﻿#### Japanese Bloc effects

## Create Japanese Bloc
#
# Create an appropriately named block (i.e. Japanese style confederation),
# with the root scoped characters house as the leading house.
#
# root: house which will lead the bloc
# TYPE: flag representing the confederation (and by extension aspiration) type
#
tgp_create_house_bloc_effect = {
	save_scope_as = bloc_creator
	save_scope_value_as = {
		name = type
		value = flag:$TYPE$
	}
	custom_description = {
		text = create_house_bloc_effect
		subject = scope:bloc_creator.house_head
		if = {
			limit = {
				OR = {
					has_house_power_parameter = ceremony_cheaper_feasts
					scope:type = flag:ceremony
				}
			}
			house_head = {
				create_confederation = {
					name = CONFEDERATION_HOUSE_NAME
					type = confederation_type:house_bloc_ceremony
					leader = house
				}
			}
		}
		else_if = {
			limit = {
				OR = {
					has_house_power_parameter = unlocks_japanese_manor_watch_house
					scope:type = flag:determination
				}
			}
			house_head = {
				create_confederation = {
					name = CONFEDERATION_HOUSE_NAME
					type = confederation_type:house_bloc_determination
					leader = house
				}
			}
		}
		else_if = {
			limit = {
				OR = {
					has_house_power_parameter = unlocks_japanese_manor_shrine
					scope:type = flag:humility
				}
			}
			house_head = {
				create_confederation = {
					name = CONFEDERATION_HOUSE_NAME
					type = confederation_type:house_bloc_humility
					leader = house
				}
			}
		}
		else_if = {
			limit = {
				OR = {
					has_house_power_parameter = unlocks_japanese_manor_brewery
					scope:type = flag:prosperity
				}
			}
			house_head = {
				create_confederation = {
					name = CONFEDERATION_HOUSE_NAME
					type = confederation_type:house_bloc_prosperity
					leader = house
				}
			}
		}
		else_if = {
			limit = {
				OR = {
					has_house_power_parameter = unlocks_japanese_manor_archive
					scope:type = flag:service
				}
			}
			house_head = {
				create_confederation = {
					name = CONFEDERATION_HOUSE_NAME
					type = confederation_type:house_bloc_service
					leader = house
				}
			}
		}
		else_if = {
			limit = {
				OR = {
					has_house_power_parameter = unlocks_japanese_manor_armory
					scope:type = flag:strength
				}
			}
			house_head = {
				create_confederation = {
					name = CONFEDERATION_HOUSE_NAME
					type = confederation_type:house_bloc_strength
					leader = house
				}
			}
		}
		house_confederation ?= {
			save_scope_as = bloc
			set_coa = scope:bloc_creator
		}
	}
}

## Add additional character, and all members of their house, to a 'block'.
#
# root: house - who was invited
# INVITER: house - who is a member of the bloc to join
#
tgp_join_house_bloc_effect = {
	save_scope_as = joiner
	house_confederation ?= { save_scope_as = old_bloc }
	$INVITER$ ?= {
		save_scope_as = inviter
		house_confederation ?= { save_scope_as = bloc }
	}
	if = { # Tooltip for interaction
		limit = { NOT = { exists = scope:bloc } }
		scope:joiner.house_head = { custom_tooltip = join_house_bloc_tt_no_scope }
	}
	else_if = {
		limit = {
			exists = scope:joiner
			exists = scope:inviter
			exists = scope:bloc
			trigger_if = {
				limit = { exists = scope:old_bloc }
				scope:old_bloc != scope:bloc
			}
		}
		# ADD MEMBER HOUSE
		custom_description = {
			text = join_house_bloc_effect
			subject = scope:joiner.house_head
			object = scope:inviter.house_head
			scope:bloc ?= {
				hidden_effect = { add_confederation_member_house = scope:joiner }
			}
		}
		# OPINION
		if = {
			limit = {
				$OPINION$ = flag:yes
				exists = scope:joiner
				scope:inviter != scope:joiner
			}
			scope:inviter ?= {
				house_head ?= {
					if = { # Hide for players
						limit = { is_ai = yes }
						if = {
							limit = {
								has_opinion_modifier = {
									modifier = left_house_bloc_opinion
									target = scope:joiner.house_head
								}
							}
							remove_opinion = {
								modifier = left_house_bloc_opinion
								target = scope:joiner.house_head
							}
						}
						add_opinion = {
							modifier = joined_house_bloc_opinion
							target = scope:joiner.house_head
							opinion = 15
						}
					}
				}
				change_house_relation_effect = {
					HOUSE = scope:joiner
					VALUE = house_relation_improve_medium_value
					REASON = joined_bloc
					CHAR = scope:joiner.house_head
					TARGET_CHAR = scope:inviter.house_head
					TITLE = scope:dummy_gender
				}
			}
		}
	}
}


## Leave block taking all rulers of the same house with you
#
# root: house - leaving the bloc
#
tgp_leave_house_bloc_effect = {
	save_scope_as = leaver
	house_confederation ?= {
		save_scope_as = bloc
		leading_house ?= { save_scope_as = leader }
	}
	if = {
		limit = {
			exists = scope:bloc
			scope:leaver.house_head.house.house_confederation ?= scope:bloc
		}
		if = { # DISBAND
			limit = {
				scope:bloc = {
					any_confederation_member_house = { count <= 2 }
				}
			}
			custom_description = {
				text = leave_house_bloc_effect
				subject = scope:leaver.house_head
				object = scope:bloc.leading_house.house_head
			}
			custom_description = {
				text = disband_house_bloc_effect
				subject = scope:bloc.leading_house.house_head
			}
		}
		else_if = { # COOLDOWN
			limit = { scope:leader != scope:leaver }
			custom_description = {
				text = leave_house_bloc_cooldown_effect
				subject = scope:leaver.house_head
				object = scope:bloc.leading_house.house_head
				add_to_variable_list = {
					name = left_bloc_cooldown
					target = scope:bloc
					years = tgp_leave_house_bloc_cooldown_years_value
				}
			}
		}
		if = {
			limit = { scope:leader = scope:leaver }
			# NEW LEADER TOOLTIP
			scope:bloc = {
				show_as_tooltip = { tgp_find_new_house_bloc_leader_effect = yes }
			}
		}
		else = {
			# OPINION
			if = {
				limit = { $OPINION$ = flag:yes }
				scope:leader ?= {
					house_head ?= {
						if = {
							limit = {
								has_opinion_modifier = {
									target = scope:leaver.house_head
									modifier = joined_house_bloc_opinion
								}
							}
							remove_opinion = {
								target = scope:leaver.house_head
								modifier = joined_house_bloc_opinion
							}
						}
						add_opinion = {
							target = scope:leaver.house_head
							modifier = left_house_bloc_opinion
							opinion = -15
						}
					}
					change_house_relation_effect = {
						HOUSE = scope:leaver
						VALUE = house_relation_damage_medium_value
						REASON = left_bloc
						CHAR = scope:leaver.house_head
						TARGET_CHAR = scope:leader.house_head
						TITLE = scope:dummy_gender
					}
				}
			}
			# TRUCE
			if = {
				limit = { $TRUCE$ = flag:yes }
				scope:leaver.house_head = {
					custom_tooltip = {
						text = tgp_leave_house_bloc_truce_tt
						scope:bloc = {
							every_confederation_member_house = {
								every_house_member = {
									limit = {
										top_liege = scope:leaver.house_head.top_liege
										is_landed = yes
									}
									if = {
										limit = { house != scope:leaver }
										add_to_list = truce_targets
									}
									else = { add_to_list = truce_getters }
								}
							}
							every_in_list = {
								list = truce_targets
								save_temporary_scope_as = truce_target_temp
								every_in_list = {
									list = truce_getters
									add_truce_one_way = {
										character = scope:truce_target_temp
										years = tgp_leave_house_bloc_truce_years_value
										name = TRUCE_LEFT_BLOC
									}
								}
							}
						}
					}
				}
			}
		}
		# REMOVE MEMBER HOUSE
		scope:bloc ?= {
			hidden_effect = { remove_confederation_member_house = scope:leaver }
		}
	}
}


## Clear bloc membership related flags
#
# root: character
#
tgp_clear_house_bloc_flags_effect = {
	if = {
		limit = { has_character_flag = forced_bloc_membership }
		remove_character_flag = forced_bloc_membership
	}
	if = {
		limit = { has_character_flag = recently_joined_bloc }
		remove_character_flag = recently_joined_bloc
	}
}


## Find new leading house if necessary
#
# root: confederation - to find a new leader for
#
tgp_find_new_house_bloc_leader_effect = {
	save_scope_as = bloc_temp
	ordered_confederation_member_house = {
		limit = {
			exists = house_head
			save_temporary_scope_as = house_temp
		}
		order_by = { # Most members
			value = 1
			scope:house_temp.house_head.top_liege = {
				every_vassal = {
					limit = {
						highest_held_title_tier > tier_barony
						is_landed = yes
						exists = house
						house = { has_same_house_power_as = scope:house_temp }
						house = scope:house_temp
					}
					add = 1
				}
			}
		}
		save_temporary_scope_as = new_leader_temp
		scope:bloc_temp = {
			tgp_set_house_bloc_leading_house_effect = { LEADER = scope:new_leader_temp }
		}
	}
}

# Set house leading a 'bloc' (i.e. Japanese style confederation)
#
# root: confederation - bloc getting a new leading house
# LEADER: house - new leading house
#
#
tgp_set_house_bloc_leading_house_effect = {
	save_scope_as = bloc
	leading_house = { save_scope_as = old_leader }
	$LEADER$ ?= { save_scope_as = leader }
	if = {
		limit = {
			exists = scope:leader
			exists = scope:bloc
			trigger_if = {
				limit = { exists = scope:old_leader }
				scope:old_leader != scope:leader
			}
		}
		custom_description = {
			text = change_house_bloc_leader_effect
			subject = scope:leader.house_head
			object = scope:old_leader.house_head
			scope:bloc ?= {
				hidden_effect = {
					tgp_bloc_set_leading_house_effect = { HOUSE = scope:leader }
				}
				if = {
					limit = { exists = scope:old_leader }
					set_variable = {
						name = recent_leader_change
						value = scope:old_leader
						years = 5
					}
				}
				hidden_effect = {
					every_player = {
						limit = { confederation ?= scope:bloc }
						send_interface_message = {
							type = msg_house_bloc_leader
							title = msg_bloc_new_leading_house
							desc = msg_bloc_new_leading_house_desc
							left_icon = scope:leader.house_head
							right_icon = scope:leader
							custom_description = {
								text = change_house_bloc_leader_effect
								subject = scope:leader.house_head
								object = scope:old_leader.house_head
							}
						}
					}
				}
			}
		}
	}
}


## Create historical blocs and add members at game start
#
# root: none
#
tgp_setup_historical_house_bloc_effect = {
	### 867
	if = {
		limit = { game_start_date = 867.1.1 }
		# Ceremony
		dynasty:japanese_fujiwara.dynasty_founder.house ?= {
			tgp_create_historical_house_bloc_effect = { TYPE = ceremony } # Northern Fujiwara
			# Fujiwara
			tgp_join_historical_house_bloc_effect = { JOINER = house:house_fujiwara_kyo_ke } # Capital Fujiwara
			tgp_join_historical_house_bloc_effect = { JOINER = house:house_fujiwara_nan_ke } # Southern Fujiwara
			tgp_join_historical_house_bloc_effect = { JOINER = house:house_fujiwara_shiki_ke } # Ceremonial Fujiwara
			tgp_join_historical_house_bloc_effect = { JOINER = house:house_fujiwara_yamakage } # Yamakage Fujiwara
			# Taira
			tgp_join_historical_house_bloc_effect = { JOINER = dynasty:japanese_taira_kanmu.dynasty_founder.house } # Kanmu Taira
		}
		# Service
		dynasty:japanese_sugawara.dynasty_founder.house ?= {
			tgp_create_historical_house_bloc_effect = { TYPE = service } # Sugawara
			# Minamoto
			tgp_join_historical_house_bloc_effect = { JOINER = dynasty:japanese_minamoto_ninmyo.dynasty_founder.house } # Ninmyo Minamoto
			# Other
			tgp_join_historical_house_bloc_effect = { JOINER = dynasty:japanese_ariwara.dynasty_founder.house } # Ariwara
			tgp_join_historical_house_bloc_effect = { JOINER = dynasty:japanese_harusumi.dynasty_founder.house } # Harusumi
			tgp_join_historical_house_bloc_effect = { JOINER = dynasty:japanese_ki.dynasty_founder.house } # Ki
			tgp_join_historical_house_bloc_effect = { JOINER = dynasty:japanese_kudara.dynasty_founder.house } # Kudara
			tgp_join_historical_house_bloc_effect = { JOINER = dynasty:japanese_oe.dynasty_founder.house } # Oe
			tgp_join_historical_house_bloc_effect = { JOINER = dynasty:japanese_shimada.dynasty_founder.house } # Shimada
			tgp_join_historical_house_bloc_effect = { JOINER = dynasty:japanese_yamato.dynasty_founder.house } # Yamato
		}
		# Prosperity
		dynasty:japanese_minamoto_saga.dynasty_founder.house ?= {
			tgp_create_historical_house_bloc_effect = { TYPE = prosperity } # Saga Minamoto
			# Fujiwara
			tgp_join_historical_house_bloc_effect = { JOINER = house:house_fujiwara_manatsu } # Manatsu Fujiwara
			tgp_join_historical_house_bloc_effect = { JOINER = house:house_fujiwara_uona } # Uona Fujiwara
			# Minamoto
			tgp_join_historical_house_bloc_effect = { JOINER = dynasty:japanese_minamoto_montoku.dynasty_founder.house } # Montoku Minamoto
			# Other
			tgp_join_historical_house_bloc_effect = { JOINER = dynasty:japanese_kiyohara.dynasty_founder.house } # Kiyohara
			tgp_join_historical_house_bloc_effect = { JOINER = dynasty:japanese_tachibana.dynasty_founder.house } # Tachibana
		}
		# Strength
		dynasty:japanese_yoshimine.dynasty_founder.house ?= {
			tgp_create_historical_house_bloc_effect = { TYPE = strength } # Yoshmine
			# Other
			tgp_join_historical_house_bloc_effect = { JOINER = dynasty:japanese_abe.dynasty_founder.house } # Abe
		}
		# Humility
		house:house_nakatomi_onakatomi ?= {
			tgp_create_historical_house_bloc_effect = { TYPE = humility } # Onakatomi
			# Other
			tgp_join_historical_house_bloc_effect = { JOINER = dynasty:japanese_tomo.dynasty_founder.house } # Tomo
		}
	}
	### 1066
	else_if = {
		limit = { game_start_date = 1066.9.15 }
		# Ceremony
		dynasty:japanese_fujiwara.dynasty_founder.house ?= {
			tgp_create_historical_house_bloc_effect = { TYPE = ceremony } # Northern Fujiwara
			# Fujiwara
			tgp_join_historical_house_bloc_effect = { JOINER = house:house_fujiwara_koichijo } # Koichijo Fujiwara
			tgp_join_historical_house_bloc_effect = { JOINER = house:house_fujiwara_manatsu } # Manatsu Fujiwara
			tgp_join_historical_house_bloc_effect = { JOINER = house:house_fujiwara_nakamikado } # Nakamikado Fujiwara
			tgp_join_historical_house_bloc_effect = { JOINER = house:house_fujiwara_nan_ke } # Southern Fujiwara
			tgp_join_historical_house_bloc_effect = { JOINER = house:house_fujiwara_ononomiya } # Ononomiya Fujiwara
			tgp_join_historical_house_bloc_effect = { JOINER = house:house_fujiwara_sesonji } # Sesonji Fujiwara
			tgp_join_historical_house_bloc_effect = { JOINER = house:house_fujiwara_shiki_ke } # Ceremonial Fujiwara
			tgp_join_historical_house_bloc_effect = { JOINER = house:house_fujiwara_yamakage } # Yamakage Fujiwara
			tgp_join_historical_house_bloc_effect = { JOINER = house:house_fujiwara_yoshikado } # Yoshikado Fujiwara
			tgp_join_historical_house_bloc_effect = { JOINER = house:house_fujiwara_yoshiyo } # Yoshiyo Fujiwara
			# Other
			tgp_join_historical_house_bloc_effect = { JOINER = dynasty:japanese_koremune.dynasty_founder.house } # Koremune
		}
		# Service
		dynasty:japanese_minamoto_murakami.dynasty_founder.house ?= { # Murakami Minamoto
			tgp_create_historical_house_bloc_effect = { TYPE = service } 
			# Fujiwara
			tgp_join_historical_house_bloc_effect = { JOINER = house:house_fujiwara_kajuji } # Kajuji
			# Minamoto
			tgp_join_historical_house_bloc_effect = { JOINER = dynasty:japanese_minamoto_daigo.dynasty_founder.house } # Daigo Minamoto
			tgp_join_historical_house_bloc_effect = { JOINER = dynasty:japanese_minamoto_sanjo.dynasty_founder.house } # Sanjo Minamoto
			tgp_join_historical_house_bloc_effect = { JOINER = dynasty:japanese_minamoto_uda.dynasty_founder.house } # Uda Minamoto
			# Other
			tgp_join_historical_house_bloc_effect = { JOINER = dynasty:japanese_ki.dynasty_founder.house } # Ki
			tgp_join_historical_house_bloc_effect = { JOINER = dynasty:japanese_oe.dynasty_founder.house } # Oe
			tgp_join_historical_house_bloc_effect = { JOINER = dynasty:japanese_sugawara.dynasty_founder.house } # Sugawara
			tgp_join_historical_house_bloc_effect = { JOINER = dynasty:japanese_takashina.dynasty_founder.house } # Takashina
		}
		# Strength
		house:house_minamoto_kawachi ?= {
			tgp_create_historical_house_bloc_effect = { TYPE = strength } # Kawachi Minamoto
			# Minamoto
			tgp_join_historical_house_bloc_effect = { JOINER = house:house_minamoto_mino } # Mino Minamoto
			tgp_join_historical_house_bloc_effect = { JOINER = house:house_minamoto_settsu } # Settsu Minamoto
			tgp_join_historical_house_bloc_effect = { JOINER = house:house_minamoto_shinano } # Shinano Minamoto
			# Taira
			tgp_join_historical_house_bloc_effect = { JOINER = house:house_taira_boso } # Boso Taira
			tgp_join_historical_house_bloc_effect = { JOINER = house:house_taira_chichibu } # Chichibu Taira
			tgp_join_historical_house_bloc_effect = { JOINER = house:house_taira_hitachi } # Daijo Taira
			tgp_join_historical_house_bloc_effect = { JOINER = house:house_taira_miura } # Miura Taira
			tgp_join_historical_house_bloc_effect = { JOINER = house:house_taira_iwaki } # Iwaki Taira
		}
		# Prosperity
		dynasty:japanese_kiyohara.dynasty_founder.house ?= {
			tgp_create_historical_house_bloc_effect = { TYPE = prosperity } # Kiyohara
			# Taira
			tgp_join_historical_house_bloc_effect = { JOINER = house:house_taira_echigo } # Echigo Taira
		}
		# Determination
		dynasty:japanese_kikuchi.dynasty_founder.house ?= {
			tgp_create_historical_house_bloc_effect = { TYPE = determination } # Kikuchi
			# Minamoto
			tgp_join_historical_house_bloc_effect = { JOINER = house:house_minamoto_watanabe } # Watanabe Minamoto
			# Taira
			tgp_join_historical_house_bloc_effect = { JOINER = house:house_taira_satsuma } # Satsuma Taira
			# Other
			tgp_join_historical_house_bloc_effect = { JOINER = dynasty:japanese_abiru.dynasty_founder.house } # Abiru
			tgp_join_historical_house_bloc_effect = { JOINER = dynasty:japanese_kimotsuki.dynasty_founder.house } # Kimotsuki
			tgp_join_historical_house_bloc_effect = { JOINER = dynasty:japanese_omiwa.dynasty_founder.house } # Omiwa
		}
	}
	### 1178 ###
	else_if = {
		limit = { game_start_date = 1178.10.1 }
		# Ceremony
		house:house_fujiwara_matsudono = {
			tgp_create_historical_house_bloc_effect = { TYPE = ceremony } # Matsudono Fujiwara
			# Fujiwara
			tgp_join_historical_house_bloc_effect = { JOINER = dynasty:japanese_fujiwara.dynasty_founder.house } # Fujiwara
			tgp_join_historical_house_bloc_effect = { JOINER = house:house_fujiwara_hamuro } # Hamuro Fujiwara
			tgp_join_historical_house_bloc_effect = { JOINER = house:house_fujiwara_kajuji } # Kajuji Fujiwara
			tgp_join_historical_house_bloc_effect = { JOINER = house:house_fujiwara_kasannoin } # Kasannoin Fujiwara
			tgp_join_historical_house_bloc_effect = { JOINER = house:house_fujiwara_kujo } # Kujo Fujiwara
			tgp_join_historical_house_bloc_effect = { JOINER = house:house_fujiwara_mikohida } # Mikohida Fujiwara
			tgp_join_historical_house_bloc_effect = { JOINER = house:house_fujiwara_oinomikado } # Oinomikado Fujiwara
			tgp_join_historical_house_bloc_effect = { JOINER = house:house_fujiwara_rokujo } # Rokujo Fujiwara
			tgp_join_historical_house_bloc_effect = { JOINER = house:house_fujiwara_shijo } # Shijo Fujiwara
			tgp_join_historical_house_bloc_effect = { JOINER = house:house_fujiwara_yoshikado } # Yoshikado Fujiwara
			# Minamoto
			tgp_join_historical_house_bloc_effect = { JOINER = dynasty:japanese_minamoto_uda.dynasty_founder.house } # Uda Minamoto
			# Taira
			tgp_join_historical_house_bloc_effect = { JOINER = house:house_taira_takamune } # Takamune Taira
		}
		# Determination
		dynasty:japanese_taira_kanmu.dynasty_founder.house = {
			tgp_create_historical_house_bloc_effect = { TYPE = determination } # Kanmu Taira
			# Fujiwara
			tgp_join_historical_house_bloc_effect = { JOINER = house:house_fujiwara_ashikaga } # Fujiwara Ashikaga
			# Minamoto
			tgp_join_historical_house_bloc_effect = { JOINER = house:house_minamoto_hitachi } # Satake Minamoto
			# Taira
			tgp_join_historical_house_bloc_effect = { JOINER = house:house_taira_echigo } # Jo Taira
			tgp_join_historical_house_bloc_effect = { JOINER = house:house_taira_kawagoe } # Kawagoe Taira
			tgp_join_historical_house_bloc_effect = { JOINER = house:house_taira_oba } # Oba Taira
			tgp_join_historical_house_bloc_effect = { JOINER = house:house_taira_satsuma } # Satsuma Taira
			# Other
			tgp_join_historical_house_bloc_effect = { JOINER = dynasty:japanese_ito.dynasty_founder.house } # Ito
			tgp_join_historical_house_bloc_effect = { JOINER = dynasty:japanese_osada.dynasty_founder.house } # Osada
			tgp_join_historical_house_bloc_effect = { JOINER = dynasty:japanese_saeki_sanyo.dynasty_founder.house } # Saeki
			tgp_join_historical_house_bloc_effect = { JOINER = dynasty:japanese_senoo.dynasty_founder.house } # Seno'o
			tgp_join_historical_house_bloc_effect = { JOINER = house:house_tachibana_tokai } # Tachibana Tokai
			tgp_join_historical_house_bloc_effect = { JOINER = dynasty:japanese_taguchi.dynasty_founder.house } # Taguchi
			tgp_join_historical_house_bloc_effect = { JOINER = dynasty:japanese_yuasa.dynasty_founder.house } # Yuasa
		}
		# Prosperity 
		dynasty:japanese_kikuchi.dynasty_founder.house = {
			tgp_create_historical_house_bloc_effect = { TYPE = prosperity } # Kikuchi
			# Other
			tgp_join_historical_house_bloc_effect = { JOINER = dynasty:japanese_kimotsuki.dynasty_founder.house } # Kimotsuki
			tgp_join_historical_house_bloc_effect = { JOINER = dynasty:japanese_kono.dynasty_founder.house } # Kono
			tgp_join_historical_house_bloc_effect = { JOINER = dynasty:japanese_kusakabe.dynasty_founder.house } # Kusakabe
			tgp_join_historical_house_bloc_effect = { JOINER = dynasty:japanese_ouchi.dynasty_founder.house } # Ouchi
			tgp_join_historical_house_bloc_effect = { JOINER = house:house_omiwa_usuki } # Usuki
		}
		# Strength
		house:house_minamoto_settsu ?= {
			tgp_create_historical_house_bloc_effect = { TYPE = strength }
			# Minamoto
			tgp_join_historical_house_bloc_effect = { JOINER = house:house_minamoto_ishikawa } # Ishikawa Minamoto
			tgp_join_historical_house_bloc_effect = { JOINER = house:house_minamoto_kai } # Kai Minamoto (Takeda)
			tgp_join_historical_house_bloc_effect = { JOINER = house:house_minamoto_kawachi } # Kawachi Minamoto
			tgp_join_historical_house_bloc_effect = { JOINER = house:house_minamoto_kiso } # Kiso Minamoto
			tgp_join_historical_house_bloc_effect = { JOINER = house:house_minamoto_kozuke } # Kozuke Minamoto (Nitta)
			tgp_join_historical_house_bloc_effect = { JOINER = house:house_minamoto_toki } # Toki Minamoto
			tgp_join_historical_house_bloc_effect = { JOINER = house:house_minamoto_yamamoto } # Yamamoto Minamoto
			tgp_join_historical_house_bloc_effect = { JOINER = house:house_minamoto_yamato } # Yamato Minamoto
			# Taira
			tgp_join_historical_house_bloc_effect = { JOINER = house:house_taira_chiba } # Chiba Taira
			tgp_join_historical_house_bloc_effect = { JOINER = house:house_taira_kazusa } # Kazusa Taira
			tgp_join_historical_house_bloc_effect = { JOINER = house:house_taira_miura } # Miura Taira
		}
		# Prosperity
		dynasty:japanese_fujiwara_oshu.dynasty_founder.house = {
			tgp_create_historical_house_bloc_effect = { TYPE = prosperity } # Oshu Fujiwara
			# Minamoto
			tgp_join_historical_house_bloc_effect = { JOINER = dynasty:house_minamoto_oshu_ishikawa } # Oshu Ishikawa Minamoto
		}
	}
	# Fix cooldowns
	every_confederation = {
		every_confederation_member_house = { remove_variable = recently_joined_bloc }
	}
}


## Create historical bloc
#
# root: house - initial leader of bloc
#
tgp_create_historical_house_bloc_effect = {
	house_head ?= {
		save_temporary_scope_as = bloc_creator
		if = {
			limit = { 
				is_alive = yes
				NOT = { exists = house.house_confederation }
			}
			if = {
				limit = { is_ruler = yes }
				house = {
					tgp_create_house_bloc_effect = { TYPE = $TYPE$ }
					add_to_list = historical_house_bloc_house_list
				}
			}
		}
	}
	clear_saved_scope = bloc_creator
}


## Add a house to this Japanese Bloc
#
# In addition to adding the house (and thereby all eligible rulers in it),
# a friendly relation will be established between the involved houses.
#
# root: the leading house of the bloc
# JOINER: the house to add
#
tgp_join_historical_house_bloc_effect = {
	if = {
		limit = { exists = house_confederation }
		save_temporary_scope_as = leader_temp
		house_confederation = { save_temporary_scope_as = historical_bloc_temp }
		$JOINER$ = { save_temporary_scope_as = joiner_temp }
		scope:joiner_temp ?= {
			if = {
				limit = {
					exists = house_head
					NOT = { exists = house_confederation }
					exists = scope:leader_temp
					exists = scope:historical_bloc_temp
				}
				scope:historical_bloc_temp = { add_confederation_member_house = scope:joiner_temp }
				scope:leader_temp = {
					tgp_set_historical_house_relation_effect = {
						OTHER_HOUSE = scope:joiner_temp
						LEVEL = cordial
					}
				}
				if = {
					limit = { NOT = { exists = scope:joiner_temp.house_confederation } }
					assert_if = {
						limit = { always = yes }
						text = "tgp_join_historical_house_bloc_effect: joiner did not join!"
					}
					debug_log = "tgp_join_historical_house_bloc_effect: joiner did not join!"
				}
			}
		}
		clear_saved_scope = leader_temp
		clear_saved_scope = historical_bloc_temp
		clear_saved_scope = joiner_temp
	}
}

tgp_on_member_joined_shared_effect = {
	save_scope_as = bloc
	scope:character = {
		save_scope_as = joiner
		add_character_flag = {
			flag = bloc_message_flag
			days = 1
		}
		trigger_event = {
			id = tgp_house_blocs.0002
			days = 1
		}
		# JOIN LEADERS FACTION
		if = {
			limit = {
				scope:bloc.leading_house.house_head.joined_faction.faction_leader ?= scope:bloc.leading_house.house_head
				can_join_faction = scope:bloc.leading_house.house_head.joined_faction
			}
			join_faction = scope:bloc.leading_house.house_head.joined_faction
		}
	}
	# Message setup
	scope:bloc = {
		add_to_variable_list = {
			name = joining_house_members
			target = scope:joiner
			days = 1
		}
	}
}

tgp_on_member_left_shared_effect = {
	save_scope_as = bloc
	scope:character = {
		save_scope_as = leaver
		if = {
			limit = { is_alive = yes }
			add_character_flag = {
				flag = bloc_message_flag
				days = 1
			}
		}
		# Clear flags
		tgp_clear_house_bloc_flags_effect = yes
		trigger_event = {
			id = tgp_house_blocs.0001
			days = 1
		}
		# LEAVE LEADERS FACTION
		joined_faction ?= {
			save_temporary_scope_as = faction_temp
			if = {
				limit = {
					faction_leader = {
						NOT = { house = scope:character.house }
					}
				}
				scope:vassal_house_temp = { leave_faction = scope:faction_temp }
			}
		}
	}
	# Message setup
	scope:bloc = {
		add_to_variable_list = {
			name = leaving_house_members
			target = scope:leaver
			days = 1
		}
	}
}

tgp_on_member_house_joined_shared_effect = {
	save_scope_as = bloc
	scope:house ?= {
		save_scope_as = joiner
		set_variable = {
			name = recently_joined_bloc
			years = 5
		}
	}
	# MESSAGES
	every_player = {
		limit = { house.house_confederation ?= scope:bloc }
		if = {
			limit = {
				house = scope:bloc.leading_house
				scope:bloc = {
					any_confederation_member_house = { count = 2 }
				}
			}
			send_interface_message = {
				type = msg_house_bloc_created
				title = msg_house_bloc_created
				desc = msg_house_bloc_created_desc
				left_icon = scope:bloc.leading_house.house_head
				right_icon = scope:bloc.leading_house
				custom_description = {
					text = create_house_bloc_effect
					subject = scope:bloc.leading_house.house_head
				}
			}
		}
		else = {
			send_interface_message = {
				type = msg_house_joined_house_bloc 
				title = msg_house_joined_bloc
				desc = msg_house_joined_bloc_desc
				left_icon = scope:joiner
				right_icon = scope:bloc.leading_house
				custom_description = {
					text = join_house_bloc_effect
					subject = scope:joiner.house_head
					object = scope:bloc.leading_house.house_head
				}
				# MEMBER LIST TOOLTIP
				#show_as_tooltip = {
				#	scope:bloc = {
				#		every_in_list = {
				#			variable = joining_house_members
				#			limit = { house = scope:joiner }
				#			custom = house_joined_bloc_member_custom
				#			custom_tooltip = house_bloc_member_tt
				#		}
				#	}
				#}
			}
		}
	}
	every_in_list = {
		variable = joining_house_members
		limit = { house = scope:joiner }
		save_temporary_scope_as = house_member_temp
		remove_character_flag = bloc_message_flag
		scope:bloc = {
			remove_list_variable = {
				name = joining_house_members
				target = scope:house_member_temp
			}
		}
		# Find Sub-Vassal houses of joining Vassal Houses
		if = {
			limit = { is_independent_ruler = no }
			every_vassal_or_below = {
				limit = {
					is_house_head = yes
					any_held_title = { is_noble_family_title = yes }
					is_independent_ruler = no
					house = {
						NOR = {
							this = scope:house
							house_confederation ?= scope:bloc
							is_in_list = joining_vassal_houses
						}
					}
				}
				house = { add_to_list = joining_vassal_houses }
			}
		}
	}
	every_in_list = {
		list = joining_vassal_houses
		save_temporary_scope_as = vassal_house_temp
		scope:bloc = { add_confederation_member_house = scope:vassal_house_temp }
	}
	if = {
		limit = { debug_only = yes }
		debug_log = "house joined bloc"
		debug_log_scopes = yes
	}
}

tgp_on_member_house_left_shared_effect = {
    save_scope_as = bloc
    scope:house = {
        save_scope_as = leaver
        if = {
            limit = { has_variable = recently_joined_bloc }
            remove_variable = recently_joined_bloc
        }
		house_head ?= {
			save_scope_as = house_head
		}
    }
    # MESSAGES
    every_player = {
        limit = {
            OR = {
                house = scope:house
                confederation ?= scope:bloc
            }
        }
        # Inform Members of Leavers
        send_interface_toast = {
            type = msg_house_left_house_bloc 
            title = msg_house_left_bloc
            desc = msg_house_left_bloc_desc
            left_icon = scope:leaver
            right_icon = scope:bloc.leading_house
			custom_tooltip = msg_house_left_bloc_desc
            # MEMBER LIST TOOLTIP
            #show_as_tooltip = {
            #   scope:bloc = {
            #       every_in_list = {
            #           variable = leaving_house_members
            #           limit = { house = scope:leaver }
            #           #custom = house_left_bloc_member_custom
            #           custom_tooltip = house_bloc_member_tt
            #       }
            #   }
            #}
        }
    }
    every_in_list = {
        variable = leaving_house_members
        limit = { house = scope:leaver }
        save_temporary_scope_as = house_member_temp
	    if = {
	     	limit = { is_alive = yes }
	     	remove_character_flag = bloc_message_flag
	    }
        scope:bloc = {
            remove_list_variable = {
                name = leaving_house_members
                target = scope:house_member_temp
            }
        }
        # Find sub-Vassal houses of leaving vassal Houses
        if = {
            limit = {
                house.house_head = { is_independent_ruler = no } 
            }
            scope:bloc = {
                every_confederation_member_house = {
                    limit = {
                        house_head = {
                            is_independent_ruler = no
                            liege.house ?= scope:house
                        }
                        NOR = {
                            this = scope:house
                            is_in_list = leaving_vassal_houses
                        }
                    }
                    add_to_list = leaving_vassal_houses
                }
            }
        }
    }
    # Find leader if necessary
    if = {
        limit = { NOT = { exists = leading_house } }
		if = {
			limit = {
				any_confederation_member = { has_title = title:e_japan }
			}
			random_confederation_member = {
				limit = { has_title = title:e_japan }
				save_scope_as = new_leader_scope
			}
			tgp_set_house_bloc_leading_house_effect = { LEADER = scope:new_leader_scope }
		}
		else = {
			tgp_find_new_house_bloc_leader_effect = yes
		}
    }
    # Remove Vassal houses of leaving Houses from above
    every_in_list = {
        list = leaving_vassal_houses
        save_temporary_scope_as = vassal_house_temp
        scope:bloc = { remove_confederation_member_house = scope:vassal_house_temp }
    }
    # Disband if necessary
    if = {
        limit = {
            OR = {
                NOT = { exists = leading_house }
                any_confederation_member_house = { count = 1 }
            }
        }
        every_player = {
            limit = {
                house.house_confederation ?= scope:bloc
                NOT = { house = scope:leaver }
            }
            send_interface_message = {
                type = msg_house_bloc_disbanded
                title = msg_house_bloc_disbanded
                desc = msg_house_bloc_disbanded_desc
                left_icon = scope:leaver.house_head
                right_icon = scope:leaver
                show_as_tooltip = {
                    scope:bloc ?= { disband_confederation = yes }
                }
            }
        }
        hidden_effect = { disband_confederation = yes }
    }
    if = {
        limit = { debug_only = yes }
        debug_log = "house left bloc"
        debug_log_scopes = yes
    }
}

bloc_leader_change_aspiration_cohesion_effect = {
	if = {
		limit = { exists = scope:house.house_confederation }
		if = {
			limit = { scope:house.house_confederation.leading_house ?= scope:house }
			scope:house.house_confederation = {
				tgp_bloc_change_cohesion_effect = {
					VALUE = bloc_leader_change_aspiration_cohesion_value
					ICON = scope:house.house_head
					REASON = leader_aspiration
				}
			}
		}
		else_if = {
			limit = {
				scope:house = { has_same_house_power_as = scope:house.house_confederation.leading_house }
			}
			# Message
			every_player = {
				limit = { house.house_confederation ?= scope:house.house_confederation }
				send_interface_message = {
					type = msg_house_bloc_cohesion_change_good
					title = msg_bloc_member_aspiration_changed
					left_icon = scope:house.house_head
					right_icon = scope:house
					desc = msg_bloc_member_aspiration_changed_tt
					custom_tooltip = msg_bloc_member_aspiration_match_tt
				}
			}
		}
		else = {
			# Message
			every_player = {
				limit = { house.house_confederation ?= scope:house.house_confederation }
				send_interface_message = {
					type = msg_house_bloc_cohesion_change_bad
					title = msg_bloc_member_aspiration_changed
					left_icon = scope:house.house_head
					right_icon = scope:house
					desc = msg_bloc_member_aspiration_changed_tt
					custom_tooltip = msg_bloc_member_aspiration_diverge_tt
				}
			}
		}
	}
}

add_attacker_bloc_members_on_declared_effect = {
	scope:attacker.house.house_confederation ?= {
		every_confederation_member_house = {
			every_house_member = {
				limit = {
					top_liege = scope:attacker.top_liege
					is_landed = yes
				}
				save_temporary_scope_as = bloc_member_temp
				scope:war = { add_attacker = scope:bloc_member_temp }
			}
		}
	}
}

tgp_bloc_war_defeat_member_effect = {
	$WINNER$ = {
		save_temporary_scope_as = winner_temp
		house.house_confederation = { save_temporary_scope_as = winner_bloc_temp }
	}
	$LOSER$ = {
		save_temporary_scope_as = loser_temp
		house.house_confederation = { save_temporary_scope_as = loser_bloc_temp }
	}
	custom_description_no_bullet = { text = tgp_bloc_war_defeat_member_description }
	scope:loser_bloc_temp = {
		every_confederation_member_house = {
			limit = {
				house_head = {
					tgp_bloc_war_transfer_trigger = { BLOC_LEADER = scope:loser_temp }
				}
			}
			custom_description_no_bullet = {
				text = tgp_bloc_war_defeat_member_house_description
				subject = house_head
			}
			hidden_effect = {
				if = { # LEAVE OLD BLOC IF NECESSARY
					limit = { exists = house_confederation }
					set_variable = {
						name = bloc_leaving_reason
						value = flag:reason_war
						days = 4
					}
					tgp_leave_house_bloc_effect = {
						OPINION = flag:no
						TRUCE = flag:yes
					}
					tgp_join_house_bloc_effect = {
						INVITER = scope:winner_temp.house
						OPINION = flag:no
					}
				}
			}
		}
	}
}

tgp_bloc_change_cohesion_effect = {
	save_temporary_scope_as = bloc_temp
	$ICON$ = { save_scope_as = icon }
	save_scope_value_as = {
		name = cohesion_change
		value = $VALUE$
	}
	hidden_effect = {
		if = {
			limit = { scope:cohesion_change >= 0 }
			every_player = {
				limit = { house.house_confederation ?= scope:bloc_temp }
				send_interface_message = {
					type = msg_house_bloc_cohesion_change_good
					title = msg_bloc_cohesion_increased
					left_icon = scope:icon
					right_icon = scope:bloc_temp.leading_house
					desc = tgp_bloc_change_cohesion_$REASON$_tt
					show_as_tooltip = {
						scope:bloc_temp = { change_cohesion = scope:cohesion_change }
					}
				}
			}
		}
		else = {
			every_player = {
				limit = { house.house_confederation ?= scope:bloc_temp }
				send_interface_message = {
					type = msg_house_bloc_cohesion_change_bad
					title = msg_bloc_cohesion_decreased
					left_icon = scope:icon
					right_icon = scope:bloc_temp.leading_house
					desc = tgp_bloc_change_cohesion_$REASON$_tt
					show_as_tooltip = {
						scope:bloc_temp = { change_cohesion = scope:cohesion_change }
					}
				}
			}
		}
	}
	change_cohesion = scope:cohesion_change
}

tgp_bloc_set_leading_house_effect = {
	$HOUSE$ = { save_scope_as = bloc_creator }
	set_leading_house = scope:bloc_creator
	set_coa = scope:bloc_creator
	set_confederation_name = "CONFEDERATION_HOUSE_NAME"
}

tgp_on_bloc_destroyed_shared_effect = {
	save_scope_as = bloc
	every_confederation_member_house = { add_to_list = members }
	leading_house = { save_scope_as = leader }
	every_player = {
		limit = { top_liege = scope:bloc.leading_house.house_head.top_liege }
		if = {
			limit = {
				house = { is_in_list = members }
			}
			send_interface_message = {
				type = msg_house_bloc_destroyed
				title = msg_house_bloc_disbanded
				desc = msg_house_bloc_disbanded_desc
				left_icon = scope:bloc.leading_house.house_head
				right_icon = scope:bloc.leading_house
				house = { save_temporary_scope_as = player_house_temp }
				show_as_tooltip = {
					scope:bloc = {
						remove_confederation_member_house = scope:player_house_temp
						disband_confederation = yes
					}
				}
			}
		}
		else = {
			send_interface_message = {
				type = msg_house_bloc_disbanded
				title = msg_house_bloc_disbanded
				desc = msg_house_bloc_disbanded_desc
				left_icon = scope:bloc.leading_house.house_head
				right_icon = scope:bloc.leading_house
				show_as_tooltip = {
					scope:bloc = { disband_confederation = yes }
				}
			}
		}
	}
    if = {
        limit = { debug_only = yes }
        debug_log = "bloc destroyed"
        debug_log_scopes = yes
    }
}

set_cadet_aspiration_cooldown_effect = {
	hidden_effect = {
		save_temporary_scope_as = house_temp
		scope:old_house.house_confederation.leading_house = {
			switch = {
				trigger = has_house_power_parameter
				ceremony_cheaper_feasts = {
					scope:house_temp = { set_house_aspiration = { type = ceremony } }
				}
				unlocks_japanese_manor_watch_house = {
					scope:house_temp = { set_house_aspiration = { type = determination } }
				}
				unlocks_japanese_manor_shrine = {
					scope:house_temp = { set_house_aspiration = { type = humility } }
				}
				unlocks_japanese_manor_brewery = {
					scope:house_temp = { set_house_aspiration = { type = prosperity } }
				}
				unlocks_japanese_manor_archive = {
					scope:house_temp = { set_house_aspiration = { type = service } }
				}
				unlocks_japanese_manor_armory = {
					scope:house_temp = { set_house_aspiration = { type = strength } }
				}
			}
		}
		if = {
			limit = { house_head = { is_ai = yes } }
			set_variable = {
				name = recently_changed_aspiration
				years = 5
			}
		}
	}
}

tgp_save_bloc_leaving_reason_effect = {
	if = {
		limit = { NOT = { has_variable = bloc_leaving_reason } }
		if = {
			limit = { scope:joiner.house_head ?= { is_ai = no } }
			set_variable = {
				name = bloc_leaving_reason
				value = flag:reason_player
				days = 4
			}
		}
		else = {
			if = {
				limit = { NOT = { exists = scope:inviter } }
				scope:leaver = { save_scope_as = inviter } #If there is no bloc join, we compare to the inviter
			}
			if = {
				limit = { exists = scope:joiner_temp.confederation.leading_house.house_head }
				scope:joiner_temp.confederation.leading_house.house_head = { save_scope_as = prev_bloc_leader }
				if = { # PRESTIGE LEVEL
					limit = { 
						OR = {
							scope:leaver.prestige_level > scope:prev_bloc_leader.prestige_level
							scope:inviter.prestige_level > scope:prev_bloc_leader.prestige_level
						}
					}
					set_variable = {
						name = bloc_leaving_reason
						value = flag:reason_prestige
						days = 4
					}
				}
				else_if = { # INFLUENCE LEVEL
					limit = {
						OR = {
							scope:leaver.influence_level > scope:prev_bloc_leader.influence_level
							scope:inviter.influence_level > scope:prev_bloc_leader.influence_level
						}
					}
					set_variable = {
						name = bloc_leaving_reason
						value = flag:reason_influence
						days = 4
					}
				}
				else_if = { # MILITARY POWER
					limit = {
						OR = {
							scope:leaver.military_power > scope:prev_bloc_leader.military_power
							scope:inviter.military_power > scope:prev_bloc_leader.military_power
						}
					}
					set_variable = {
						name = bloc_leaving_reason
						value = flag:reason_military_strength
						days = 4
					}
				}
				else_if = { # INVITER OPINION
					limit = {
						scope:leaver != scope:inviter
						scope:leaver = {
							opinion = {
								target = scope:inviter
								value >= 40
							}
						}
					}
					set_variable = {
						name = bloc_leaving_reason
						value = flag:reason_inviter_bloc_opinion
						days = 4
					}
				}
				else_if = { # PREV LEADER OPINION
					limit = {
						scope:leaver = {
							opinion = {
								target = scope:prev_bloc_leader
								value < 0
							}
						}
					}
					set_variable = {
						name = bloc_leaving_reason
						value = flag:reason_current_bloc_opinion
						days = 4
					}
				}
				else_if = { # DYNASTY
					limit = {
						scope:leaver != scope:inviter
						scope:leaver.dynasty = scope:inviter.dynasty
					}
					set_variable = {
						name = bloc_leaving_reason
						value = flag:reason_dynasty
						days = 4
					}
				}
				else_if = { # ASPIRATIONS
					limit = {
						NOT = {
							scope:leaver.house = { has_same_house_power_as = scope:prev_bloc_leader.house }
						}
					}
					set_variable = {
						name = bloc_leaving_reason
						value = flag:reason_aspirations
						days = 4
					}
				}
				else_if = { # COHESION
					limit = { scope:joiner_temp.house.house_confederation ?= { cohesion <= 25 } }
					set_variable = {
						name = bloc_leaving_reason
						value = flag:reason_cohesion
						days = 4
					}
				}
			}
		}
	}
}
