﻿# Called from code once a quarter for "playable" (count+) characters.
# Root is the character
# scope:quarter is which quarter it's for, from 1 to 4. Note that scope:quarter = 1 isn't necessarily in Q1 by calendar time
realm_maintenance_quarterly_pulse = {
	on_actions = {
		on_revoking_heretic_holy_order_lease
		on_hard_ai
	}
}

on_revoking_heretic_holy_order_lease = {
	trigger = {
		NOT = { has_game_rule = no_extra_difficulty }
		NOT = { has_game_rule = dynamic_holy_order_lease_enabled }
		is_independent_ruler = yes
		any_religion_global = {
			any_faith = { # has a wrong holy order in their realm
				NOT = { this = root.faith }
				any_faith_holy_order = {
					any_leased_title = {
						holder.top_liege = root
					}
				}
			}
		}
	}
	effect = {
		random_list = {
			1 = {
				modifier = {
					has_trait = cynical
					add = 1
				}
			}
			1 = {
				modifier = {
					has_trait = zealous
					add = 2
				}
				modifier = {
					has_government = theocracy_government
					add = 5
				}
				every_sub_realm_barony = {
					limit = {
						is_under_holy_order_lease = yes
						trigger_if = {
							limit = { exists = lessee.faith } # to avoid any error
							NOT = { lessee.faith = root.faith }
							root.faith = {
								faith_hostility_level = {
									target = prev.lessee.faith
									value > faith_astray_level
								}
							}
							trigger_if = {
								limit = {
									NOT = { county.holder = root }
									lessee.faith = county.holder.faith
								}
								root = {
									opinion = {
										target = county.holder
										value < low_positive_opinion
									}
								}
								county.holder = { NOT = { vassal_contract_has_flag = religiously_protected } }
							}
						}
					}
					if = {
						limit = { lessee.faith = county.holder.faith }
						county.holder = {
							add_opinion = {
								target = root
								modifier = holy_order_revoked_faith_lease_opinion
							}
						}
					}
					revoke_lease = yes
				}
			}
		}
	}
}

on_hard_ai = {
	trigger = {
		is_ai = yes
		NOT = { has_game_rule = no_extra_difficulty }
	}
	on_actions = {
		ai_revolts
		ai_low_control_pulse
		ai_high_countrol_pulse
	}
}

ai_revolts = {
	trigger = {
		has_variable = rebel_leader_peasants
		var:rebel_leader_peasants.faction_war ?= {
			is_attacker = prev
			primary_defender = {
				is_ai = no
				save_temporary_scope_as = target_player
			}
		}
		exists = scope:target_player
		current_military_strength < scope:target_player.current_military_strength
	}
	effect = {
	}
}

#Low County Control of ai is restore somewhat to keep them stable
ai_low_control_pulse = {
	effect = {
		every_held_county = {
			limit = { county_control < low_county_control_limit }
			random_list = {
				256 = {
					change_county_control = 2
				}
				16 = {
					change_county_control = 4
				}
				4 = {
					change_county_control = 8
				}
				2 = {
					change_county_control = 16
				}
			}
			random = {
				chance = 20
				save_scope_as = county
				holder = {
					save_scope_as = corruption_holder
					trigger_event = {
						id = realm_maintenance.0004
						days = { 10 40 }
					}
				}
			}
		}
	}
}

#High County Control of ai gives them extra tax
ai_high_countrol_pulse = {
	effect = {
		every_held_county = {
			limit = { county_control > high_county_control_limit }
			random_list = {
				96 = { }
				16 = {
					holder = { add_gold = prev.ai_county_payout_lowest }
				}
				8 = {
					holder = { add_gold = prev.ai_county_payout_low }
				}
				4 = {
					holder = { add_gold = prev.ai_county_payout }
				}
				2 = {
					holder = { add_gold = prev.ai_county_payout_high }
				}
				1 = {
					holder = { add_gold = prev.ai_county_payout_highest }
				}
			}
			random = {
				chance = 0
				modifier = {
					this = holder.capital_county
					add = 2
				}
				modifier = {
					holder = { highest_held_title_tier >= tier_empire }
					add = 2
				}
				modifier = {
					holder = { highest_held_title_tier >= tier_kingdom }
					add = 2
				}
				modifier = {
					holder = { highest_held_title_tier >= tier_duchy }
					add = 2
				}
				modifier = {
					holder = { highest_held_title_tier >= tier_county }
					add = 2
				}
				change_development_progress_with_overflow = 5
			}
		}
	}
}