﻿# On actions for war events, triggered automatically by the game

# called for wars after being transferred to a new defender
# root is the primary attacker
# scope:war is the war
# scope:defender is the new defender

on_war_transferred = {
	events = {
		war_event.2001 # allows attacker to back out of the war
	}
}

# called for people joining as a secondary attacker or defender
# root is the joiner
# scope:war is the war

on_join_war_as_secondary = {
	events = {
		war_event.3000 # Ally gets mad if you do not participate in their wars.
	}
}

# called when a war is started.
# same scopes are available in these events as in the CBs' on_declaration, this just fires for all CBs instead of a specific CB.

on_war_started = {
	effect = {
		scope:attacker = {
			if = {
				limit = {
					any_vassal_or_below = { is_ai = no }
				}
				every_vassal_or_below = {
					limit = { is_ai = no }
					send_interface_toast = {
						type = msg_war_declared_by_liege
						title = msg_war_declared_by_liege
						desc = msg_war_declared_by_liege_desc
						left_icon = scope:attacker
						right_icon = scope:defender
					}
				}
			}
		}
		scope:defender = {
			if = {
				limit = {
					any_vassal_or_below = { is_ai = no }
				}
				every_vassal_or_below = {
					limit = { is_ai = no }
					send_interface_toast = {
						type = msg_war_declared_on_liege
						title = msg_war_declared_on_liege
						desc = msg_war_declared_on_liege_desc
						left_icon = scope:defender
						right_icon = scope:attacker
					}
				}
			}
		}
	}
}

# called for when a casus belli resolves in one of the following ways.
# same scopes are available in these events as in the CBs themselves, this just fires for all CBs instead of a specific CB.
# note that any events/effects fired here WILL NOT show up in the war summary tooltip; they fire at the same time as the war resolution, but are not actually part of the war resolution itself.

on_war_won_attacker = {
	# The war notifications are handled in `effect` instead of `events` due to order of operations.
	# `effect` fires on THIS tick, `events` fires on the NEXT tick, and the war gets destroyed between this tick and the next.
	effect = {
		# Save scopes for localization.
		scope:attacker = {
			random_character_war = {
				limit = {
					primary_defender = scope:defender
				}
				save_scope_as = this_war
			}
		}
		scope:attacker = { save_scope_as = winner }
		scope:defender = { save_scope_as = loser }

		# Run scripted effect that iterates through all players and notifies them if necessary.
		notify_players_about_neighboring_war_resolution_effect = yes
	
		###Imperial mod###
		scope:defender = { 
			if = {
				limit = {
					any_targeting_faction = {
						faction_is_type = military_faction
					}
				}
				send_interface_toast = {
					title = military_faction_LOST_WAR_TITLE
					right_icon = this
					random_targeting_faction = {
						limit = { faction_is_type = military_faction }
						add_faction_discontent = 50
					}
				}				 
			}	
		}
		scope:attacker = { 
			if = {
				limit = {
					has_government = military_command
					is_independent_ruler = yes
				}
				if = {
					limit = {
						OR = {
							culture_group = culture_group:arabic_group
							culture_group = culture_group:iranian_group
							culture = culture:turkish
							religion = religion:islam_religion
						}
					}
					change_government = clan_government
				}
				else = {
					change_government = feudal_government
				}				
			}
		}
	}
}

on_war_won_defender = {
	effect = {
		# Save scopes for localization.
		scope:defender = {
			random_character_war = {
				limit = {
					primary_attacker = scope:attacker
				}
				save_scope_as = this_war
			}
		}
		scope:attacker = { save_scope_as = loser }
		scope:defender = { save_scope_as = winner }

		# Run scripted effect that iterates through all players and notifies them if necessary.
		notify_players_about_neighboring_war_resolution_effect = yes
	
		###Imperial mod###
		scope:attacker = {
			if = {
				limit = {
					any_targeting_faction = {
						faction_is_type = military_faction
					}
				}
				send_interface_toast = {
					title = military_faction_LOST_WAR_TITLE
					right_icon = this
					random_targeting_faction = {
						limit = { faction_is_type = military_faction }
						add_faction_discontent = 50
					}
				}
			}	
		}
	}
}

on_war_white_peace = {

}

on_war_invalidated = {
	effect = {
		scope:attacker = {
			send_interface_message = {
				type = event_war_invalidated
				title = END_WAR_INVALIDATED_MESSAGE_TITLE
				desc = END_WAR_INVALIDATED_MESSAGE_DESC
				left_icon = scope:attacker
				right_icon = scope:defender
			}
		}
	}
}
