############################################################################################################
#	Expert AI mod - on_actions
############################################################################################################

on_actions = {
	on_startup = {
		effect = {

			random_country = {

				EAI_startup_effects = yes
			}
		}
	}
	on_daily = {
		effect = {

			EAI_daily_update = yes
		}
	}
	on_weekly = {
		effect = {

			EAI_weekly_update = yes
		}
	}
	on_monthly = {
		effect = {
			## I have to do my own monthly update because EAI_monthly_update isn't accurate enough (called in some decisions)
			IF = { 
				limit = { NOT = { has_global_flag = EAI_months_elapsed_corrected } }
				add_to_variable = { global.EAI_months_elapsed_corrected = 1 }
				set_global_flag = { flag = EAI_months_elapsed_corrected value = 1 days = 2}
			}
			EAI_monthly_update = yes
		}
	}
	on_puppet = {
		effect = {

			### Japan's peace conference with China - cleanup

			if = { # merge the warlords into one puppet
				limit = {
					FROM = { tag = JAP is_ai = yes }
					ROOT = { OR = { tag = SHX tag = GXC tag = YUN tag = PRC tag = XSM } is_ai = yes }
				}

				CHI = { annex_country = { target = ROOT } }
			}
			if = { # keep the resource rich states for itself
				limit = {
					FROM = { tag = JAP is_ai = yes }
					ROOT = { tag = CHI is_ai = yes }
				}

				# resource rich coastal
				if = { limit = { 599 = { CONTROLLER = { OR = { tag = JAP is_subject_of = JAP } } } }
					599 = { JAP = { transfer_state = PREV } }
				}
				if = { limit = { 600 = { CONTROLLER = { OR = { tag = JAP is_subject_of = JAP } } } }
					600 = { JAP = { transfer_state = PREV } }
				}
				if = { limit = { 606 = { CONTROLLER = { OR = { tag = JAP is_subject_of = JAP } } } }
					606 = { JAP = { transfer_state = PREV } }
				}
				if = { limit = { 749 = { CONTROLLER = { OR = { tag = JAP is_subject_of = JAP } } } }
					749 = { JAP = { transfer_state = PREV } }
				}
			}

			### clear ideology advisors

			ROOT = {

				if = { limit = { is_ai = yes }

					clr_country_flag = EAI_democratic_focus
					clr_country_flag = EAI_fascist_focus
					clr_country_flag = EAI_communist_focus
					clr_country_flag = EAI_neutral_focus

					remove_ideas_with_trait = communist_revolutionary
					remove_ideas_with_trait = ambitious_union_boss
					remove_ideas_with_trait = fascist_demagogue
					remove_ideas_with_trait = syncretic_revanchist
					remove_ideas_with_trait = staunch_monarchist
					remove_ideas_with_trait = colonial_communist
					remove_ideas_with_trait = social_reformer
					remove_ideas_with_trait = liberal_journalist
					remove_ideas_with_trait = ambitious_union_boss
					remove_ideas_with_trait = socialist_novelist
					remove_ideas_with_trait = anti_communist_crusader
					remove_ideas_with_trait = monarchist_media_mogul
					remove_ideas_with_trait = royalist_bulldog
					remove_ideas_with_trait = shadow_of_calles

					if = { limit = { FROM = { EAI_democratic_nation = yes } } set_country_flag = EAI_democratic_focus }
					else_if = { limit = { FROM = { EAI_fascist_nation = yes } } set_country_flag = EAI_fascist_focus }
					else_if = { limit = { FROM = { EAI_communist_nation = yes } } set_country_flag = EAI_communist_focus }
					else_if = { limit = { FROM = { EAI_nonaligned_nation = yes } } set_country_flag = EAI_neutral_focus }
				}
			}
		}
	}
	on_annex = {
		effect = {

			# Japan's peace conference with China - cleanup
			# if = { 
			# 	limit = {
			# 		ROOT = { tag = JAP is_ai = yes }
			# 		FROM = { OR = { tag = PRC tag = XSM tag = YUN tag = SHX tag = GXC } is_ai = yes }
			# 	}

			# 	# transfer inland states to puppet chi
			# 	if = { limit = { 622 = { CONTROLLER = { NOT = { tag = CHI } OR = { tag = JAP is_subject_of = JAP } } } }
			# 		622 = { CHI = { transfer_state = PREV } }
			# 	}
			# 	if = { limit = { 615 = { CONTROLLER = { NOT = { tag = CHI } OR = { tag = JAP is_subject_of = JAP } } } }
			# 		615 = { CHI = { transfer_state = PREV } }
			# 	}
			# 	if = { limit = { 621 = { CONTROLLER = { NOT = { tag = CHI } OR = { tag = JAP is_subject_of = JAP } } } }
			# 		621 = { CHI = { transfer_state = PREV } }
			# 	}
				
			# 	if = { 
			# 		limit = { 
			# 			any_state = { 
			# 				OR = { region = 200 region = 144 } 
			# 				CONTROLLER = { NOT = { tag = CHI } OR = { tag = JAP is_subject_of = JAP } }
			# 			} 
			# 		}
			# 		every_state = {
			# 			limit = { 
			# 				OR = { region = 200 region = 144 } 
			# 				CONTROLLER = { NOT = { tag = CHI } OR = { tag = JAP is_subject_of = JAP } }
			# 			} 
						
			# 			CHI = { transfer_state = PREV }
			# 		}
			# 	}
			# }

			# Italy's peace conference with Ethiopia - puppet creation
			# if = { 
			# 	limit = {
			# 		ROOT = { tag = ITA has_government = fascism }
			# 		FROM = { tag = ETH }
			# 	}

			# 	ROOT = { country_event = EAI_DM.58 }
			# }
		}
	}
	on_capitulation = {
		effect = {

			### Fixes a vanilla bug for seize gold reserves decisions

				#GERMANY - Set Flag in capitulated countries and add them to array for Seize Gold Reserves Decision purposes

				if = {
					limit = {
						FROM = {
							OR = {
								original_tag = GER
								is_ally_with = GER
							}
						}
						ROOT = {
							OR = {
								capital_scope = { is_on_continent = europe }
								original_tag = TUR
								any_state = {
									has_state_flag = EAI_original_capital_@ROOT
									is_on_continent = europe
								}
							}
						}
					}
					ROOT = {
						set_country_flag = GER_has_capitulated_to_germany
						add_to_array = { 
							array = GER.GER_seize_gold_targets_array 
							value = THIS
						}
					}
				}

			### Clear naval invasion target

			every_country = { limit = { check_variable = { EAI_naval_invasion_target = ROOT.id } }

				EAI_INVASION_cancel_target = yes
				
				if = { limit = { ROOT = { has_country_flag = EAI_naval_invasion_logging check_variable = { log_cancelled_invasion = 1 } } } log = "[GetYear] [GetMonth] | AI | [Root.GetName] | INVASION: reason: capitulated" } 
			}

			###

			if = { limit = { FROM = { tag = GER } }

				if = { limit = { OR = { tag = DEN tag = POL tag = FRA } }

					FROM = { clr_country_flag = EAI_pause_national_focuses }
				}
			}

			### Fix african states flipping to GER on capitulation

			if =  {
				limit = {
					is_historical_focus_on = yes
					date < 1941.1.1
					date > 1939.1.1
					OR = {
						ROOT = { tag = FRA is_in_faction = yes }
						ROOT = { tag = BEL is_in_faction = yes }
					}
					FROM = { tag = GER }
				}

				every_state = {
					limit = {
						is_controlled_by = GER
						OR = {
							region = 272
							region = 271
							region = 227
							region = 224
							region = 184
						}
					}

					ROOT = { set_state_controller = PREV }
				}
			}
		}
	}
	on_declare_war = { #FROM is war target
		effect = {

			set_country_flag = { flag = EAI_MILITARY_recent_wardec_90d value = 1 days = 90 }
			set_country_flag = { flag = EAI_MILITARY_wardec_90d_on_@PREV value = 1 days = 90 }

			FROM = {
				set_country_flag = { flag = EAI_MILITARY_recent_wardec_90d value = 1 days = 90 }
				set_country_flag = { flag = EAI_MILITARY_wardec_90d_from_@PREV value = 1 days = 90 }
			}

			ROOT = { EAI_MILITARY_track_war_trend_reset = yes }

			###

			if = { limit = { tag = GER FROM = { tag = SOV } }
				set_global_flag = { flag = EAI_GER_blitz_russia_365d value = 1 days = 365 }
			}

			if = { limit = { tag = GER FROM = { tag = POL } }
				set_global_flag = { flag = EAI_GER_blitz_poland_365d value = 1 days = 365 }
			}

			if = { limit = { tag = JAP FROM = { tag = CHI } }
				set_global_flag = { flag = EAI_JAP_blitz_china_1000d value = 1 days = 1000 }
			}

			if = { limit = { tag = JAP FROM = { tag = USA } }
				set_global_flag = { flag = EAI_JAP_blitz_pacific_1000d value = 1 days = 1000 }
			}

			###

			# ger wardec on sov: call all minors immediately to avoid a SOV AI frontline issue
			if = {
				limit = { 
					is_historical_focus_on = yes
					ROOT = {
						original_tag = GER 
					}
					FROM = {
						original_tag = SOV
					}
				}

				# minors join ger's war immediately
				country_event = { id = EAI_DM.23 hours = 1 }

				if = { # fin continuation war
					limit = {
						ROOT = { has_country_flag = EAI_FIN_joins_war_on_SOV }
					}

					country_event = { id = EAI_DM.28 hours = 1 }
				}
			}

			# clear wardec script flags
			ROOT = { clear_variable = EAI_wardec_script_target }
			FROM = {
				clr_country_flag = EAI_declare_war_from_@ROOT
				clr_country_flag = EAI_prepare_for_war_from_@ROOT
			}

			# clear USA neutrality
			if = { limit = { FROM = { OR = { has_idea = EAI_neutrality_idea_USA is_puppet_of = USA } } }
				hidden_effect = {
					USA = {
						remove_ideas = EAI_neutrality_idea_USA
					}
				}
			}
			
			# clear spain neutrality
			if = { limit = { FROM = { has_idea = EAI_neutrality_idea_SPR } }
				hidden_effect = {
					FROM = {
						remove_ideas = EAI_neutrality_idea_SPR
					}
				}
			}
		
			# force update scipted AI
			if = { limit = { FROM = { is_ai = yes } }
			
				hidden_effect = {
					FROM = { country_event = { id = EAI_monthly_update.0 hours = 1 } }
				}
			}
			if = { limit = { is_ai = yes }
				hidden_effect = { 
					country_event = { id = EAI_monthly_update.0 hours = 1 } 
				}
			}

			# jap invasion blitz
			if = { limit = { is_ai = yes original_tag = JAP FROM = { OR = { is_in_faction_with = ENG original_tag = PHI original_tag = USA original_tag = ENG } } }
				hidden_effect = { 
					set_global_flag = { flag = EAI_JAP_blitz_pacific_1000d value = 1 days = 1000 }
				}
			}

			# barbarossa
			if = { 
				limit = {
					is_ai = yes 
					is_in_faction_with = GER
					is_neighbor_of = SOV
					FROM = { tag = SOV } 
				}

				hidden_effect = { 
					set_country_flag = { flag = EAI_barbarossa_blitz value = 1 days = 90 }

					SOV = { set_country_flag = { flag = EAI_barbarossa_defensive value = 1 days = 270 } }
				}
			}

			# japanase invasion of china
			if = { 
				limit = {
					tag = JAP
					FROM = { tag = CHI } 
				}

				hidden_effect = { 
					CHI = { set_country_flag = { flag = EAI_japanese_war_defense value = 1 days = 365 } }
					XSM = { set_country_flag = { flag = EAI_japanese_war_defense value = 1 days = 365 } }
					PRC = { set_country_flag = { flag = EAI_japanese_war_defense value = 1 days = 365 } }
					YUN = { set_country_flag = { flag = EAI_japanese_war_defense value = 1 days = 365 } }
					GXC = { set_country_flag = { flag = EAI_japanese_war_defense value = 1 days = 365 } }
					SHX = { set_country_flag = { flag = EAI_japanese_war_defense value = 1 days = 365 } }	
				}
			}
		}
	}
	on_nuke_drop = {
		effect = {		
			if = { limit = { has_country_flag = EAI_logging } log = "[Root.GetName] nuked [From.GetName]" } 
		}
	}
	on_operation_completed = {
		effect = {
			# same scope setup as in operation outcome:
			# THIS: the operation
			# ROOT: the initiating country
			# FROM: the target country

			ROOT = {
				if = { limit = { is_ai = yes }

					# Is running the current operation whether it's this one or another - can start a new operation
					if = { limit = { is_running_operation = { target = var:EAI_operation_target operation = var:EAI_operation_type } }

						set_country_flag = EAI_operation_completed
					}

					if = { limit = { has_country_flag = EAI_espionage_logging } log = "[GetYear] [GetMonth] | AI | [Root.GetName] | ESPIONAGE: completed operation - [Prev.GetName] = [From.GetName]" }
				}
			}
		}
	}
	on_operative_captured = {
		effect = {
			# SCOPE  the operative
			# ROOT   the country the operative was performing its mission in
			# FROM   the country the operative is operating for

			add_to_variable = { ROOT.EAI_espionage_operation_risk_@FROM = 1 }
		}
	}
	on_operative_death = {
		effect = {
			# SCOPE  the operative
			# ROOT   the killer country (optional)
			# FROM   the country the operative is operating for

			add_to_variable = { ROOT.EAI_espionage_operation_risk_@FROM = 1 }
		}
	}
	#ROOT is new controller #FROM is old controller #FROM.FROM is state ID
	on_state_control_changed = {
		effect = {
			
			if = { limit = { NOT = { ROOT = { has_war_with = FROM } } }

				# Discard peaceful exchanges from war trend calculations
				FROM.FROM = { set_temp_variable = { num_provinces_changed = global.EAI_MAP_state_province_ids@THIS^num } }
				FROM = { 
					add_to_variable = { EAI_MILITARY_trend_adjust_num_provinces = num_provinces_changed }
				}
				ROOT = { 
					multiply_temp_variable = { num_provinces_changed = -1 }
					add_to_variable = { EAI_MILITARY_trend_adjust_num_provinces = num_provinces_changed }
				}
			}

			###

			FROM.FROM = {

				if = { limit = { check_variable = { EAI_PC_projects_in_state_of_@FROM^num > 0 } }
				
					for_each_loop = { array = EAI_PC_projects_in_state_of_@FROM
					
						add_to_temp_array = { _cancel_projects_IDS = v }
					}
	
					if = { limit = { check_variable = { _cancel_projects_IDS^num > 0 } }
					
						set_temp_variable = { _include_progressed = 1 }
						FROM = { EAI_PC_cancel_projects = yes }
					}
				}
			}
		}
	}
}