
PDP_depose_antipope = {
	name = CB_NAME_PDP_DEPOSE_ANTIPOPE
	war_name = WAR_NAME_PDP_DEPOSE_ANTIPOPE
	sprite = 29
	truce_days = 3650

    is_permanent = no
	#can_call_vassals = yes
	hostile_against_others = no
	#can_ask_to_join_war = no
	can_call_allies = yes
	infamy_modifier = 0.0
	attacker_rel_head_is_ally = yes
    can_use_gui = {
 	  always = no
	}

	can_use = {
		ROOT = {
	     religion = catholic
		}
		FROM = {
	      rightful_religious_head_scope = {
			  NOT = {has_landed_title = k_papal_state}
		  }
		}
	}

	is_valid = {
		ROOT = {
	     religion = catholic
		}
		FROM = {
	      rightful_religious_head_scope = {
			  NOT = {has_landed_title = k_papal_state}
		  }
		}
	}

	on_add = {
		hidden_tooltip = { fire_haruspicy_event_effect = yes }
		if = {
			limit = { defender = { is_offmap_governor = offmap_china } }
			attacker = {
				sound_effect = china_angered_emperor
				detract_grace_super_huge_effect = yes
			}
		}
	}

	on_invalidation = {
	}

	on_success = {
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = {
				participation_scaled_piety = 750
				participation_scaled_prestige = 400
			}
		}

		ROOT = {
			participation_scaled_piety = 750
			participation_scaled_prestige = 400
			religion_authority = {
				modifier = deposed_antipope
				years = 50
			}

			if = {
				limit = {
					rightful_religious_head_scope = {
						ROOT = {
							excommunicated_for = PREV
						}
					}
				}
				excommunicate = no
			}

			rightful_religious_head_scope = {
				opinion = {
					who = ROOT
					modifier = opinion_deposed_antipope
				}
			}
		}

		FROM = {
			piety = -500
			prestige = -250

			rightful_religious_head_scope = {
				remove_claim = k_papal_state
				remove_claim = d_fraticelli
				if = {
					limit = {
						any_demesne_title = {has_title_flag = PDP_antipope_title}
					}
					any_demsne_title = {
						clr_title_flag = PDP_antipope_title
					}
				}
				else {
					#edge case, should never occur (hopefully)
					#did not find the corresponding title (might have lost it in a war), so rather check all titles to be safe.
					any_title = {
						limit = {has_title_flag = {PDP_antipope_title}}
						clr_title_flag = PDP_antipope_title
					}
					k_papal_state = {
						any_claimant = {
							primary_title = {
								set_title_flag = PDP_antipope_title
							}
						}
					}
				}
			}
			#abdicate = yes
		}
	}

	on_fail = {
		ROOT = {
			piety = -250
			prestige = -125
		}
		FROM = {
			piety = 100
			prestige = 50
		}
	}

	on_reverse_demand = {
		ROOT = {
			transfer_scaled_wealth = {
				to = FROM
				value = 4.0
			}
			piety = -500
			prestige = -250
		}

		FROM = {
			piety = 750
			prestige = 450
		}
	}

	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
	}
}


PDP_depose_pope = {
	name = CB_NAME_PDP_DEPOSE_POPE
	war_name = WAR_NAME_PDP_DEPOSE_POPE
	sprite = 29
	truce_days = 3650

    is_permanent = yes
	#can_call_vassals = yes
	hostile_against_others = yes
	can_ask_to_join_war = yes
	can_call_allies = yes
    can_use_gui = {
 	  always = yes
	}

	can_use = {
		ROOT = {
	     religion = catholic
		 has_claim = k_papal_state
		}
		FROM = {
	      has_landed_title = k_papal_state
		}
	}

	is_valid = {
		ROOT = {
	     religion = catholic
		 has_claim = k_papal_state
		}
		FROM = {
	      has_landed_title = k_papal_state
		}
	}

	on_add = {
		hidden_tooltip = { fire_haruspicy_event_effect = yes }
		if = {
			limit = { defender = { is_offmap_governor = offmap_china } }
			attacker = {
				sound_effect = china_angered_emperor
				detract_grace_super_huge_effect = yes
			}
		}
		ROOT = {
			top_liege = {
				any_vassal = {
					#limit = {NOT = {character = ROOT}}
					character_event = {id = PDPAnti.211 days = 2 random = 3}
				}
			}
		}
		any_independent_ruler = {
			limit = {
				religion = catholic
				rightful_religious_head_scope = {
					has_landed_title = k_papal_state
				}
			}
			character_event = {id = PDPAnti.210 days = 2 random = 3} #ask to join...
		}
	}

	on_invalidation = {
	}

	on_success = {
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = {
				participation_scaled_piety = 500
				participation_scaled_prestige = 250
			}
			reverse_opinion = { modifier = opinion_supported_pope  who = ROOT months = 1000 }
		}

		ROOT = {
			participation_scaled_piety = 500
			participation_scaled_prestige = 250
			religion_authority = { #from his perspective, the other one was the antipope
				modifier = deposed_antipope
				years = 50
			}
		}

		FROM = {
			piety = -500
			prestige = -250
			custom_tooltip = {
				text = PDP_depose_pope_desc
				random_playable_ruler = {
					limit = {
						religion = catholic
						is_feudal = yes
					}
					preferred_limit = {dynasty = ROOT}
					save_event_target_as = player_placeholder
				}
				ROOT = {
					save_event_target_as = antipope
					if = {
						limit = {
							current_heir = {always = yes}
						}
						current_heir = {
							save_event_target_as = antipope_heir
						}
					}
					else = {
						create_character = {
							random_traits = yes
							dynasty = random
							culture = ROOT
							religion = catholic
							female = no
							age = 67
						}
						new_character = {
							save_event_target_as = antipope_heir
						}
					}
				}
				save_event_target_as = pope
				event_target:antipope = { #remove flag from province
					remove_claim = k_papal_state
					if = {
						limit = {
							any_demesne_title = {has_title_flag = PDP_antipope_title}
						}
						any_demsne_title = {
							clr_title_flag = PDP_antipope_title
						}
					}
					else {
						#edge case, should never occur (hopefully)
						#did not find the corresponding title (might have lost it in a war), so rather check all titles to be safe.
						any_title = {
							limit = {has_title_flag = {PDP_antipope_title}}
							clr_title_flag = PDP_antipope_title
						}
						k_papal_state = {
							any_claimant = {
								primary_title = {
									set_title_flag = PDP_antipope_title
								}
							}
						}
					}
				}
				event_target:antipope = {set_player_character = event_target:player_placeholder}
				event_target:antipope = {
					abdicate = yes
					wealth = event_target:pope
				}
				event_target:pope = {
					abdicate_to = event_target:antipope
					clear_wealth = yes
					any_artifact = {
						transfer_artifact = {from = event_target:pope to = event_target:antipope }
					}
				}
				event_target:antipope = { #ensure playability
					set_government_type = playable_theocracy_government
					character_event = {id = PDP.5 days = 1}
					character_event = {id = PDPAnti.200 days = 1}
				}
				event_target:player_placeholder = {
					set_player_character = event_target:antipope #the new pope
				}
			}
		}
	}

	on_fail = {
		ROOT = {
			piety = -250
			prestige = -125
		}
		FROM = {
			piety = 100
			prestige = 50
		}
	}

	on_reverse_demand = {
		ROOT = {
			transfer_scaled_wealth = {
				to = FROM
				value = 4.0
			}
			piety = -500
			prestige = -250
		}

		FROM = {
			piety = 500
			prestige = 250
		}
	}

	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
	}
}
