﻿# For on_actions launched by the code itself.

# Root = The applicable country
# scope:technology = The technology type they just acquired
on_acquired_technology = {
	effect = {
		if = {
			limit = {
				has_variable = grand_exhibition_points_change
			}
			change_variable = {
				name = grand_exhibition_points_change
				add = 3
			}
		}
		if = {
			limit = {
				has_game_started = yes
			}
			post_notification = acquired_technology_notification
		}
		if = {
			limit = {
				exists = c:GBR
				this = c:GBR
				root = { has_technology_researched = pumpjacks }
				NOT = {
				has_global_variable = finding_oil_var
				has_global_variable = persia_finding_oil_var
				}
			}
			trigger_event = { id = 03_persia_events.26 popup = yes }
			
			set_global_variable = finding_oil_var
			
		}
		if = {
			limit = {
				root = { has_technology_researched = nitroglycerin }
				has_modifier = deep_gold_reserves_modifier
			}
			remove_modifier = deep_gold_reserves_modifier
			
		}
		if = {
			limit = {
				OR = {
				this = c:PER
				this = c:QAJ
				}
				root = { has_technology_researched = dynamite }
				root = { has_technology_researched = steam_donkey }
				NOT = {
				has_global_variable = persia_finding_oil_var
				has_global_variable = finding_oil_var
				}
			}
			trigger_event = { id = 03_persia_events.27 popup = yes }
			
			set_global_variable = persia_finding_oil_var
			
		}
		if = {
			limit = {
				is_player = yes
				has_technology_researched = pumpjacks
				NOT = { has_variable = first_oil_discovered }
			}
			set_variable = first_oil_discovered
			if = {
				limit = {
					any_scope_state = {
						has_potential_resource = bg_oil_extraction
					}
				}
				random_scope_state = {
					limit = {
						has_potential_resource = bg_oil_extraction
					}
					force_resource_discovery = bg_oil_extraction
				}
			}
			else_if = {
				limit = {
					any_country = {
						has_overlapping_interests = ROOT
						any_scope_state = {
							has_potential_resource = bg_oil_extraction
						}
					}
				}
				random_country = {
					limit = {
						has_overlapping_interests = ROOT
						any_scope_state = {
							has_potential_resource = bg_oil_extraction
						}
					}
					random_scope_state = {
						limit = {
							has_potential_resource = bg_oil_extraction
						}
						force_resource_discovery = bg_oil_extraction
					}
				}
			}
			else = {
				random_state = {
					limit = {
						has_potential_resource = bg_oil_extraction
					}
					force_resource_discovery = bg_oil_extraction
				}
			}
		}
	}

	events = {
		historical_events.1
		marx_events.1
		uf_general_events.2
		uf_general_events.3
	}
}

# Root = Character
on_character_death = {
	effect = {
		if = {
			limit = {
				is_ruler = yes
				exists = c:PER
				owner = c:PER
				owner = {
					has_law = law_type:law_monarchy
					NOT = { has_variable = death_of_qajar }
				}
			}
			owner = {
				trigger_event = 03_persia_events.4
			}
		}
		if = {
			limit = {
				is_ruler = yes
				exists = c:PER
				owner = c:PER
				owner = {
					has_variable = amir_kabir_is_president
					NOT = { has_variable = death_of_amir_kabir }
				}
			}
			owner = {
				trigger_event = 03_persia_events.60
			}
		}
		if = {
			limit = {
				is_ruler = yes
				exists = c:HAW
				owner = c:HAW
				owner = {
					has_law = law_type:law_monarchy
					NOT = { has_variable = death_of_kamehameha }
					NOT = { has_global_variable = gp_hawaii_interest }
				}
			}
			owner = {
				trigger_event = hawaii_events.1
			}
		}
		
		if = {
			limit = {
				has_variable = expedition_leader_var
			}
			owner = {
				trigger_event = expedition_events.110
			}
		}

		### Force-generating French monarchs of the correct dynasty if something goes wrong
		if = {
			limit = {
				is_ruler = yes
				owner = {
					NOT = {
						any_scope_character = {
							is_heir = yes
						}
					}
				}
			}
			if = {
				limit = { 
					has_variable = legitimist_leader 
				}
				owner = {
					create_character = {
						template = dynamic_legitimist_ruler_template
					}
				}
			}
			else_if = {
				limit = { 
					has_variable = orleanist_leader 
				}
				owner = {
					create_character = {
						template = dynamic_orleanist_ruler_template
					}
				}				
			}
			else_if = {
				limit = { 
					has_variable = bonapartist_leader 
				}
				owner = {
					create_character = {
						template = dynamic_bonapartist_ruler_template
					}
				}				
			}
		}
		###
		# Garibaldi exist check
		if = {
			limit = {
				has_template = garibaldi_character_template
			}
			remove_global_variable = garibaldi_is_alive_global_var
		}
		### Caudillo Kingmaker
		if = {
			limit = {
				NOT = { has_variable = caudillo_kingmaker }
				has_role = politician
				interest_group = {
					is_interest_group_type = ig_armed_forces
					is_marginal = no
				}
				owner = {
					in_election_campaign = no
					country_has_voting_franchise = yes
					OR = {
						has_journal_entry = je_caudillo
						has_modifier = age_of_caudillos_modifier
					}
				}
			}
			owner = {
				trigger_event = caudillo.5
			}
		}
	}
}
# Root = State
#on_resource_discovered = {
	#events = {
	#	goldrush.1
	#	oil_rush.1
	#	rubber_events.1
		#03_persia_events.28		
	#}
#}


# Root = Country
on_wargoal_enforced = {
	effect = {
		set_variable = {
            name = recently_won_war
            days = 1825
            value = yes
        }
        if = {
        	limit = {
        		c:PRC ?= root
        		NOT = {
        			exists = c:FRA
        		}
        	}
        	c:PRC = {
				set_immune_to_revolutions = no
				change_tag = FRA
				every_scope_state = {
					limit = {
						state_region = {
							is_homeland = cu:french
						}
					}
					set_state_type = incorporated
				}
        	}
        }
		if = {
			limit = {
				c:FRA ?= root
				NOT = {
					exists = c:PRC
				}
			}
			if = {
				limit = {
					c:FRA = {
						is_immune_to_revolutions = yes
					}
				}
				set_immune_to_revolutions = no
			}
		}
        if = {
        	limit = {
        		owner = {
	        		c:PNI ?= this
	        		has_variable = ragamuffin_nation_var
	        		NOT = { has_variable = won_ragamuffin_war_var }
					owns_entire_state_region = STATE_RIO_GRANDE_DO_SUL
					owns_entire_state_region = STATE_SANTA_CATARINA
				}
        	}
        	owner = {
        		set_variable = won_ragamuffin_war_var
        		trigger_event = { id = brazilian_minors.1 days = 0 popup = yes }
        	}
        }
        if = {
        	limit = {
        		owner = {
        			game_date < 1850.1.1 # Just in case.
        			NOT = {
        				has_variable = cabanagem_defeat_var
        				exists = c:PRA
        			}
	        		c:BRZ ?= this
					owns_entire_state_region = STATE_PARA
					any_scope_state = { state_region = s:STATE_AMAZONAS }
				}
        	}
        	owner = {
        		set_variable = cabanagem_defeat_var
        		trigger_event = { id = brazilian_minors.2 days = 0 popup = yes }
        	}
        }
        if = {
        	limit = {
        		owner = {
        			game_date < 1850.1.1 # Just in case.
        			NOT = {
        				has_variable = ragamuffin_defeat_var
        				exists = c:PNI
        			}
	        		c:BRZ ?= this
					owns_entire_state_region = STATE_RIO_GRANDE_DO_SUL
				}
        	}
        	owner = {
        		set_variable = ragamuffin_defeat_var
        		trigger_event = { id = brazilian_minors.3 days = 0 popup = yes }
        	}
        }
		if = {
			limit = {
				c:BIC ?= scope:target
				}
			c:BIC = {
			trigger_event = { id = 01_india_events.16 days = 0 popup = yes }
			}
		}
	}
	
}

on_monthly_pulse_country = { #Krakatoa special pulse
	random_events = {
		10 = 0
		50 = 03_persia_events.67 #Birth of Mozaffar ad-Din Shah Qajar 
		50 = 03_persia_events.70 #Assassination of Nasir Shah Qajar 
	}
}

# Root = Character
on_new_ruler = {
	effect = {
		post_notification = new_ruler
		if = {
			limit = {
				owner = {
					c:RUS ?= this
					has_law = law_type:law_monarchy
				}
				is_ruler = yes
			}
			if = {
				limit = {
					has_template = RUS_alexander_ii
				}
				owner = {
					trigger_event = { id = russia_monarchy.1 popup = yes }
				}
			}
			else_if = {
				limit = {
					has_template = RUS_tsarevich_nicholas_alexandrovich
				}
				owner = {
					trigger_event = { id = russia_monarchy.3 popup = yes }
				}
			}
			else_if = {
				limit = {
					has_template = RUS_alexander_iii
				}
				owner = {
					trigger_event = { id = russia_monarchy.4 popup = yes }
				}
			}
			else_if = {
				limit = {
					has_template = RUS_nicholas_ii
				}
				owner = {
					trigger_event = { id = russia_monarchy.5 popup = yes }
				}
			}
			else_if = {
				limit = {
					has_template = RUS_alexei_ii
				}
				owner = {
					trigger_event = { id = russia_monarchy.9 popup = yes }
				}
			}
			else_if = {
				limit = {
					has_template = RUS_michael_ii
				}
				owner = {
					trigger_event = { id = russia_monarchy.10 popup = yes }
				}
			}
			else_if = {
				limit = {
					has_template = RUS_george_i
				}
				owner = {
					trigger_event = { id = russia_monarchy.11 popup = yes }
				}
			}
		}
		if = {
			limit = {
				owner = {
					c:PER ?= this
					has_law = law_type:law_monarchy
				}
				is_ruler = yes
			}
		if = {
				limit = {
					has_template = per_mozaffar_al_din_qajar_template
				}
				owner = {
					trigger_event = { id = 03_persia_events.68 popup = yes } #Accession of Mozaffar ad-Din Shah Qajar 
				}
			}
		else_if = {
				limit = {
					has_template = per_muhammad_ali_qajar_template
				}
				owner = {
					trigger_event = { id = 03_persia_events.69 popup = yes } #Accession of Muhammad Ali
				}
			}
		}
		if = {
			limit = {
				owner = {
					c:PRG ?= this
				}
				has_template = PRG_francisco_solano_lopez
				is_ruler = yes
			}
			owner = {
				random_scope_character = {
					limit = {
						has_template = PRG_francisco_solano_lopez
					}
					if = {
						limit = {
							has_modifier = napoleon_of_the_americas_modifier
						}
						remove_modifier = napoleon_of_the_americas_modifier
					}
					add_modifier = {
						name = napoleon_of_the_americas_modifier
						multiplier = 2
					}
				}
			}
		}
	}
	
}
	
# Fires when a diplomatic play is started
# root = The diplomatic play that has been started
# scope:initiator = Initiator of the play
# scope:target = Target of the play
on_diplomatic_play_started = {
	effect = {
		if = {
			limit = { is_diplomatic_play_type = dp_unify_germany }
			every_country = {
				limit = {
					is_supporting_unification_candidate = { who = scope:initiator country_formation = GER }
				}
				
				scope:initiator = { annex = prev }
			}
		}
		
		if = {
			limit = { is_diplomatic_play_type = dp_unify_italy }
			every_country = {
				limit = {
					is_supporting_unification_candidate = { who = scope:initiator country_formation = ITA }
				}
				
				scope:initiator = { annex = prev }
			}
		}

		if = {
			limit = { is_diplomatic_play_type = dp_unify_scandinavia }
			every_country = {
				limit = {
					is_supporting_unification_candidate = { who = scope:initiator country_formation = SCA }
				}
				
				scope:initiator = { annex = prev }
			}
		}
		if = {
			limit = { is_diplomatic_play_type = dp_unify_india }
			every_country = {
				limit = {
					is_supporting_unification_candidate = { who = scope:initiator country_formation = BHT }
				}
				
				scope:initiator = { annex = prev }
			}
		}
		
	}
}
