# 23
coastal_republic = {
	name = CB_NAME_COASTAL_REPUBLIC
	war_name = WAR_NAME_COASTAL_REPUBLIC
	sprite = 1
	truce_days = 3650
	sort_priority = 700

	is_permanent = yes
	check_all_titles = yes
	can_ask_to_join_war = yes
	allow_distant = yes # AI parameter
	allowed_to_target_tributaries = no

	can_use_gui = {
		check_if_crusader_trigger = yes

		# CK2Plus
		# Attacker
		ROOT = {
			can_use_cb_preamble = yes
		}
	}

	can_use = {
		# Attacker
		ROOT = {
			is_patrician = yes

			NOR = {
				same_realm = FROM
				has_character_modifier = rep_cb_cd
			}
		}
	}

	can_use_title = {
		real_tier = COUNT

		location = {
			is_coastal = yes
			has_trade_post = yes

			trade_post_owner = {
				character = ROOT
			}

			# Not allowed to use this CB to take cities in the capital province of a rival merchant republic
			FROM = {
				trigger_if = {
					limit = { is_merchant_republic = yes }

					capital_scope = {
						NOT = { province_id = PREVPREV }
					}
				}
			}
		}

		# Attacker, or a vassal of the attacker, owns a city in the county
		any_direct_de_jure_vassal_title = {
			real_tier = BARON
			holding_type = city

			holder_scope = {
				OR = {
					character = ROOT
					is_vassal_or_below_of = ROOT
				}
			}
		}
	}

	is_valid = {
		# Attacker
		ROOT = {
			is_patrician = yes
		}
	}

	on_add = {
		fire_haruspicy_event_effect = yes
		anger_china_effect = yes

		# CK2Plus
		hidden_effect = {
			attacker = {
				show_scope_change = no

				add_character_modifier = {
					name = rep_cb_cd
					years = 5
					inherit = yes
				}
			}
		}
	}

	on_success_title = {
		log = "[Root.GetBestName] (ID: [Root.GetID]) succeeded with [Root.GetHerHis] coastal county republic war for the [This.GetFullName] against [From.GetBestName] (ID: [From.GetID])"

		if = {
			limit = {
				location = {
					is_coastal = yes

					ROOT = {
						capital_scope = {
							distance = {
								where = PREVPREV # Comparing the distance between ROOT's capital and the title in question
								value < 650	# From  aprox.Venice to East Cyprus and from Venice to Gibraltar
							}
						}
					}
				}
			}
			custom_tooltip = { text = CTT_REP_IN_RANGE }
		}
		else = {
			custom_tooltip = { text = CTT_REP_OUT_RANGE }
		}

		# Owner of trade post takes city
		if = {
			limit = {
				location = {
					trade_post_owner = {
						is_vassal_or_below_of = attacker
					}
				}
			}

			location = {
				show_scope_change = no

				trade_post_owner = {
					show_scope_change = no

					usurp_title = PREVPREV
				}
			}
		}

		# attacker takes city instead
		else = {
			usurp_title = attacker
		}

		attacker = {
			show_scope_change = no

			participation_scaled_prestige = 100

			hidden_effect = {
				remove_character_modifier = rep_cb_cd

				add_character_modifier = {
					name = rep_cb_cd
					years = 5
					inherit = yes
				}
			}
		}

		any_attacker = {
			limit = { NOT = { character = attacker } }
			hidden_effect = { participation_scaled_prestige = 100 }
		}
	}

	on_fail = {
		attacker = {
			show_scope_change = no

			prestige = -100

			hidden_effect = {
				remove_character_modifier = rep_cb_cd
			}
		}

		defender = {
			show_scope_change = no

			participation_scaled_prestige = 50
		}

		any_defender = {
			limit = { NOT = { character = defender } }
			hidden_effect = { participation_scaled_prestige = 50 }
		}
	}

	on_reverse_demand = {
		attacker = {
			show_scope_change = no

			prestige = -200

			transfer_scaled_wealth = {
				to = defender
				value = 4.0
			}

			hidden_effect = {
				remove_character_modifier = rep_cb_cd
			}
		}

		defender = {
			show_scope_change = no

			participation_scaled_prestige = 100
		}

		any_defender = {
			limit = { NOT = { character = defender } }
			hidden_effect = { participation_scaled_prestige = 100 }
		}
	}

	attacker_ai_victory_worth = {
		factor = -1 # always accept
	}

	attacker_ai_defeat_worth = {
		factor = 50
	}

	defender_ai_victory_worth = {
		factor = -1 # always accept
	}

	defender_ai_defeat_worth = {
		factor = 50
	}

	# CK2Plus
	ai_will_do = {
		factor = 1

		trigger = {
			ROOT = {
				sufficient_treasury_for_war_trigger = yes

				# Most Christian and Jewish AI will not attack their religion head
				can_attack_religion_head_trigger = yes

				trigger_if = {
					limit = { rightful_religious_head = FROM }

					OR = {
						trait = ambitious
						trait = cynical
					}
				}
			}
		}
	}
}

# 24 - Disabled in CK2Plus
coastal_county_republic = {
	name = CB_NAME_COASTAL_COUNTY_REPUBLIC
	war_name = WAR_NAME_COASTAL_COUNTY_REPUBLIC
	sprite = 1
	truce_days = 3650
	sort_priority = 700

	is_permanent = yes
	check_all_titles = yes
	can_ask_to_join_war = no
	allow_distant = yes # AI parameter
	allowed_to_target_tributaries = no

	can_use_gui = {
		check_if_crusader_trigger = yes
	}

	can_use = {
		always = no # Disabled in CK2Plus

		# Attacker
		ROOT = {
			is_patrician = yes
			NOT = { same_realm = FROM }
		}
	}

	can_use_title = {
		real_tier = COUNT

		location = {
			is_coastal = yes

			county = { # city part of attacker's subrealm
				any_direct_de_jure_vassal_title = {
					holder_scope = {
						OR = {
							character = ROOT
							is_vassal_or_below_of = ROOT
						}
					}
				}
			}

			any_neighbor_province = {
				trigger_if = { # part of attacker's realm
					limit = { has_owner = yes } # not wasteland or sea zone

					owner = {
						OR = {
							character = ROOT
							is_vassal_or_below_of = ROOT
						}
					}
				}
				trigger_else = { # or across a one or two sea zones owned by attacker
					is_land = no # first sea zone

					any_neighbor_province = {
						trigger_if = { # part of attacker's realm
							limit = { has_owner = yes  } # not wasteland or sea zone

							owner = {
								OR = {
									character = ROOT
									is_vassal_or_below_of = ROOT
								}
							}
						}
						trigger_else = {
							is_land = no # second sea zone

							any_neighbor_province = { # part of attacker's realm
								owner = {
									OR = {
										character = ROOT
										is_vassal_or_below_of = ROOT
									}
								}
							}
						}
					}
				}
			}
		}
	}

	is_valid = {
		# Attacker
		ROOT = {
			is_patrician = yes
		}
	}

	is_valid_title = {
		# Must control a city in this county at all times
		any_direct_de_jure_vassal_title = {
			holder_scope = {
				OR = {
					character = ROOT
					is_vassal_or_below_of = ROOT
				}
			}
		}
	}

	on_add = {
		fire_haruspicy_event_effect = yes
		anger_china_effect = yes
	}

	on_success_title = {
		if = { # attacker takes title
			limit = {
				any_direct_de_jure_vassal_title = {
					holder = attacker
				}
			}

			usurp_title = attacker
		}

		else = { # owner of city takes county instead
			any_de_jure_vassal_title = {
				show_scope_change = no

				limit = {
					holder_scope = {
						is_vassal_or_below_of = attacker
					}
				}

				holder_scope = {
					show_scope_change = no

					usurp_title = PREVPREV
					set_defacto_liege = attacker
				}
			}
		}

		attacker = {
			show_scope_change = no

			participation_scaled_prestige = 100
		}

		any_attacker = {
			limit = { NOT = { character = attacker } }
			hidden_effect = { participation_scaled_prestige = 100 }
		}

		hidden_effect = {
			check_if_reconquista_finished_effect = yes

			defender = {
				landless_pope_effect = yes
			}
		}
	}

	on_fail = {
		attacker = {
			show_scope_change = no

			prestige = -100
		}

		defender = {
			show_scope_change = no

			participation_scaled_prestige = 50
		}

		any_defender = {
			limit = { NOT = { character = defender } }
			hidden_effect = { participation_scaled_prestige = 50 }
		}
	}

	on_reverse_demand = {
		attacker = {
			show_scope_change = no

			prestige = -200

			transfer_scaled_wealth = {
				to = defender
				value = 4.0
			}
		}

		defender = {
			show_scope_change = no

			participation_scaled_prestige = 100
		}

		any_defender = {
			limit = { NOT = { character = defender } }
			hidden_effect = { participation_scaled_prestige = 100 }
		}
	}

	attacker_ai_victory_worth = {
		factor = -1 # always accept
	}

	attacker_ai_defeat_worth = {
		factor = 100
	}

	defender_ai_victory_worth = {
		factor = -1 # always accept
	}

	defender_ai_defeat_worth = {
		factor = 100
	}
}

# 30: Embargo Merchant Republic CB
embargo_cb = {
	name = CB_NAME_EMBARGO
	war_name = WAR_NAME_EMBARGO
	sprite = 11
	truce_days = 3650
	sort_priority = 2000

	is_permanent = yes
	can_ask_to_join_war = no
	allowed_to_target_tributaries = no

	can_use_gui = {
		check_if_crusader_trigger = yes

		# CK2Plus
		# Attacker
		ROOT = {
			can_use_cb_preamble = yes
		}
	}

	can_use = {
		# Attacker
		ROOT = {
			independent = yes
			mercenary = no
			NOT = { has_embargo = FROM }

			any_realm_province = {
				has_trade_post = yes

				trade_post_owner = {
					OR = {
						character = FROM
						is_vassal_or_below_of = FROM
					}
				}
			}
		}
	}

	is_valid = {
		# Attacker
		ROOT = {
			independent = yes
			NOT = { has_embargo = FROM }
		}

		# Defender
		FROM = {
			OR = { # is (liege of) a merchant republic
				is_merchant_republic = yes

				any_realm_lord = {
					is_merchant_republic = yes
				}
			}
		}
	}

	on_add = {
		fire_haruspicy_event_effect = yes
		anger_china_effect = yes
	}

	on_success = {
		log = "[Root.GetBestName] (ID: [Root.GetID]) succeeded with [Root.GetHerHis] embargo war against [From.GetBestName] (ID: [From.GetID])"

		attacker = {
			show_scope_change = no

			embargo = defender
		}
	}

	on_fail = {
		attacker = {
			show_scope_change = no

			if = { # only lose prestige if not instigated by another merchant republic
				limit = {
					NOT = {
						any_attacker = {
							is_merchant_republic = yes
							NOT = { character = attacker }
						}
					}
				}
			}

			prestige = -100
		}

		any_attacker = { # attacking merchant republics pay dearly for not winning
			show_scope_change = no

			limit = {
				is_merchant_republic = yes
				NOT = { character = attacker }
			}

			prestige = -1000
		}

		defender = {
			show_scope_change = no

			prestige = 50
		}
	}

	on_reverse_demand = {
		attacker = {
			show_scope_change = no

			if = { # only lose prestige and pay reparations if not instigated by another merchant republic
				limit = {
					NOT = {
						any_attacker = {
							is_merchant_republic = yes
							NOT = { character = attacker }
						}
					}
				}

				prestige = -200

				transfer_scaled_wealth = {
					to = defender
					value = 4.0
				}
			}
		}

		any_attacker = { # attacking merchant republics also pay dearly
			show_scope_change = no

			limit = {
				is_merchant_republic = yes
				NOT = { character = attacker }
			}

			prestige = -200

			transfer_scaled_wealth = {
				to = defender
				value = 4.0
			}
		}

		defender = {
			show_scope_change = no

			prestige = 100
		}
	}

	attacker_ai_victory_worth = {
		factor = -1 # always accept
	}

	attacker_ai_defeat_worth = {
		factor = 50
	}

	defender_ai_victory_worth = {
		factor = -1 # always accept
	}

	defender_ai_defeat_worth = {
		factor = 50
	}

	ai_will_do = {
		factor = 0.9

		trigger = {
			ROOT = {
				# CK2Plus
				sufficient_treasury_for_war_trigger = yes

				# The AI needs at least three trade posts to attack
				any_realm_province = {
					count >= 3

					has_trade_post = yes

					trade_post_owner = {
						OR = {
							character = FROM
							is_vassal_or_below_of = FROM
						}
					}
				}

				# CK2Plus
				NOT = { trait = charitable }

				trigger_if = {
					limit = {
						OR = {
							trait = greedy
							trait = envious
						}
					}

					opinion = {
						who = FROM
						value < 0
					}
				}
				trigger_else = {
					opinion = {
						who = FROM
						value < -25
					}
				}
			}
		}

		coronation_factor = yes

		mult_modifier = {
			factor = 0.5

			ROOT = {
				opinion = {
					who = FROM
					value >= -25
				}
			}
		}
	}
}

# 31
seize_trade_post = {
	name = CB_NAME_SEIZE_TRADE_POST
	war_name = WAR_NAME_SEIZE_TRADE_POST
	sprite = 1
	truce_days = 3650
	att_ticking_war_score_multiplier = 5.0
	def_ticking_war_score_multiplier = 5.0
	sort_priority = 1900

	is_permanent = yes
	check_all_trade_posts = yes
	can_ask_to_join_war = yes
	allow_distant = yes
	allowed_to_target_tributaries = no

	can_use_gui = {
		check_if_crusader_trigger = yes

		# CK2Plus
		# Attacker
		ROOT = {
			can_use_cb_preamble = yes
		}
	}

	can_use = {
		# Attacker
		ROOT = {
			is_patrician = yes

			NOR = {
				is_vassal_or_below_of = FROM # Can't attack liege
				is_liege_or_above_of = FROM # and vassals
				has_character_modifier = rep_tp_cd
			}

			trigger_if = {
				limit = { same_realm = FROM }

				# We cannot overtly attack much weaker Patricians in the same republic
				num_of_trade_post_diff = {
					character = FROM
					value < 3
				}
			}
		}
	}

	can_use_title = {
		real_tier = COUNT
		has_trade_post = yes

		location = {
			is_coastal = yes

			# CK2Plus
			# Within range of the capital
			ROOT = {
				capital_scope = {
					distance = {
						where = PREVPREV # Comparing the distance between ROOT's capital and the title in question
						value < 650	#  From aprox. Venice to East Cyprus and from Venice to Gibraltar
					}
				}
			}
		}

		trade_post_owner = {
			OR = {
				character = FROM
				is_vassal_or_below_of = FROM
			}
		}

		# NOT = { holder = FROM }
	}

	is_valid = {
		# Attacker
		ROOT = {
			is_patrician = yes

			NOR = {
				is_vassal_or_below_of = FROM # Can't attack liege
				is_liege_or_above_of = FROM # and vassals
			}
		}
	}

	is_valid_title = {
		has_trade_post = yes

		trade_post_owner = {
			OR = {
				character = FROM
				is_vassal_or_below_of = FROM
			}
		}

		# NOT = { holder = FROM }
	}

	on_add = {
		fire_haruspicy_event_effect = yes
		anger_china_effect = yes

		add_character_modifier = {
			name = rep_tp_cd
			years = 5
			inherit = yes
		}
	}

	on_success_title = {
		log = "[Root.GetBestName] (ID: [Root.GetID]) succeeded with [Root.GetHerHis] seize_trade_post CB against [From.GetBestName] (ID: [From.GetID])"

		seize_trade_post = attacker

		attacker = {
			show_scope_change = no

			participation_scaled_prestige = 100
			pf_prosperity_plus2_effect = yes

			hidden_effect = {
				remove_character_modifier = rep_tp_cd

				add_character_modifier = {
					name = rep_tp_cd
					years = 5
					inherit = yes
				}
			}
		}

		defender = {
			show_scope_change = no

			pf_prosperity_minus2_effect = yes
		}

		any_attacker = {
			limit = { NOT = { character = attacker } }
			hidden_effect = { participation_scaled_prestige = 100 }
		}
	}

	on_fail = {
		attacker = {
			show_scope_change = no

			prestige = -50
			hidden_effect = { remove_character_modifier = rep_tp_cd }
		}

		defender = {
			show_scope_change = no

			participation_scaled_prestige = 50
		}

		any_defender = {
			limit = { NOT = { character = defender } }
			hidden_effect = { participation_scaled_prestige = 50 }
		}
	}

	on_reverse_demand = {
		attacker = {
			show_scope_change = no

			prestige = -100

			transfer_scaled_wealth = {
				to = defender
				value = 2.0
			}

			hidden_effect = { remove_character_modifier = rep_tp_cd }
		}

		defender = {
			show_scope_change = no

			participation_scaled_prestige = 100
		}

		any_defender = {
			limit = { NOT = { character = defender } }
			hidden_effect = { participation_scaled_prestige = 100 }
		}
	}

	attacker_ai_victory_worth = {
		factor = -1 # always accept
	}

	attacker_ai_defeat_worth = {
		factor = 25
	}

	defender_ai_victory_worth = {
		factor = -1 # always accept
	}

	defender_ai_defeat_worth = {
		factor = 25
	}

	ai_will_do = {
		factor = 1

		# CK2Plus
		trigger = {
			ROOT = {
				sufficient_treasury_for_war_trigger = yes
			}
		}

		coronation_factor = yes

		mult_modifier = {
			factor = 0.25

			ROOT = {
				NOT = { same_realm = FROM }
			}
		}
	}
}