on_actions = {
	#ROOT is new controller #FROM is old controller #FROM.FROM is state ID
	on_state_control_changed = {

		 ##  #    ##   ## ###     ###   ##   ##  ###  
		#  # #   #  # #   #       #  # #  # #  # #  # 
		#    #   #  #  #  ##      ###  #  # #### #  # 
		#  # #   #  #   # #       #  # #  # #  # #  # 
		 ##  ###  ##  ##  ###     #  #  ##  #  # ###  

		effect = {

			###  #  # ###  #   #  ##  
			#  # #  # #  # ## ## #  # 
			###  #  # ###  # # # #### 
			#  # #  # #  # #   # #  # 
			###   ##  #  # #   # #  # 
			if = {
				limit = {
					FROM.FROM = { has_state_flag = CHI_needed_for_burma_road }

					#Country that grabbed control should be interested in closing the road
					ROOT = {
						OR = {
							has_war_with = FROM
							FROM = {
								any_enemy_country = {
									OR = {
										is_in_faction_with = ROOT
										is_subject_of = ROOT
									}
								}
							}
						}
					}
				}
				FROM.FROM = {
					save_event_target_as = state_occupied
					set_state_flag = CHI_burma_road_closed_state
				}
				ROOT = { save_event_target_as = occupying_country }

				random_country = {
					limit = { check_variable = { CHI_burma_road_current_level > 0 } }
					set_variable = { tempvar = 0 }
					subtract_from_variable = { var = tempvar value = CHI_burma_road_current_level }

					#Country that had the burma road open
					add_offsite_building = { type = arms_factory level = tempvar }
					#In case we need to reopen
					set_variable = { CHI_burma_road_closed_level = CHI_burma_road_current_level }
					set_variable = { CHI_burma_road_current_level = 0 }
					clear_variable = tempvar
					country_event = { id = wtt_burma_road.1 }
				}

			}

			#   ### ###   ##  
			#   #   #  # #  # 
			#   ##  #  # #  # 
			#   #   #  # #  # 
			### ### ###   ##  
			if = {
				limit = {
					FROM.FROM = { has_state_flag = CHI_needed_for_ledo_road }

					#Country that grabbed control should be interested in closing the road
					ROOT = {
						OR = {
							has_war_with = FROM
							FROM = {
								any_enemy_country = {
									OR = {
										is_in_faction_with = ROOT
										is_subject_of = ROOT
									}
								}
							}
						}
					}
				}
				FROM.FROM = {
					save_event_target_as = state_occupied
					set_state_flag = CHI_ledo_road_closed_state
				}
				ROOT = { save_event_target_as = occupying_country }

				random_country = {
					limit = { check_variable = { CHI_ledo_road_current_level > 0 } }
					set_variable = { tempvar = 0 }
					subtract_from_variable = { var = tempvar value = CHI_ledo_road_current_level }

					#Country that had the ledo road open
					add_offsite_building = { type = arms_factory level = tempvar }
					#In case we need to reopen
					set_variable = { CHI_ledo_road_closed_level = CHI_ledo_road_current_level }
					set_variable = { CHI_ledo_road_current_level = 0 }
					clear_variable = tempvar
					country_event = { id = wtt_ledo_road.1 }
				}

			}

			#  #  ##  #  #  ##  ### 
			#  # #  # ## # #  #  #  
			#### #### # ## #  #  #  
			#  # #  # #  # #  #  #  
			#  # #  # #  #  ##  ### 
			if = {
				limit = {
					FROM.FROM = { has_state_flag = CHI_needed_for_hanoi_route }

					#Country that grabbed control should be interested in closing the road
					ROOT = {
						OR = {
							has_war_with = FROM
							FROM = {
								any_enemy_country = {
									OR = {
										is_in_faction_with = ROOT
										is_subject_of = ROOT
									}
								}
							}
						}
					}
				}
				FROM.FROM = { save_event_target_as = state_occupied }
				ROOT = { save_event_target_as = occupying_country }

				random_country = {
					limit = { check_variable = { CHI_hanoi_route_current_level > 0 } }
					set_variable = { tempvar = 0 }
					subtract_from_variable = { var = tempvar value = CHI_hanoi_route_current_level }

					#Country that had the burma road open
					add_offsite_building = { type = arms_factory level = tempvar }					
					set_variable = { CHI_hanoi_route_current_level = 0 }
					clear_variable = tempvar
					country_event = { id = wtt_hanoi_route.1 }
				}

			}

			### #  # ###     #  # #  # #   # ###  
			 #  #  # #       #  # #  # ## ## #  # 
			 #  #### ##      #### #  # # # # ###  
			 #  #  # #       #  # #  # #   # #    
			 #  #  # ###     #  #  ##  #   # #    
			if = {
				limit = {
					FROM.FROM = { has_state_flag = CHI_needed_for_the_hump }

					#Country that grabbed control should be interested in closing the road
					ROOT = {
						OR = {
							has_war_with = FROM
							FROM = {
								any_enemy_country = {
									OR = {
										is_in_faction_with = ROOT
										is_subject_of = ROOT
									}
								}
							}
						}
					}
				}
				FROM.FROM = {
					save_event_target_as = state_occupied
					set_state_flag = CHI_the_hump_closed_state
				}
				ROOT = { save_event_target_as = occupying_country }

				random_country = {
					limit = { check_variable = { CHI_the_hump_current_level > 0 } }
					set_variable = { tempvar = 0 }
					subtract_from_variable = { var = tempvar value = CHI_the_hump_current_level }

					#Country that had the airlift road open
					add_offsite_building = { type = arms_factory level = tempvar }
					#In case we need to reopen
					set_variable = { CHI_the_hump_closed_level = CHI_the_hump_current_level }
					set_variable = { CHI_the_hump_current_level = 0 }
					clear_variable = tempvar
					country_event = { id = wtt_the_hump.1 }
				}

			}


		###   ##  #   ### ### ###  ##   ##  #        ## ### ###  #  #  ##   ##  #   ### 
		#  # #  # #    #   #   #  #  # #  # #       #    #  #  # #  # #    #    #   #   
		###  #  # #    #   #   #  #    #### #        #   #  ###  #  # # ## # ## #   ##  
		#    #  # #    #   #   #  #  # #  # #         #  #  #  # #  # #  # #  # #   #   
		#     ##  ### ###  #  ###  ##  #  # ###     ##   #  #  #  ##   ##   ##  ### ### 
					
			#ROOT is new controller #FROM is old controller #FROM.FROM is state ID
			if = {
				limit = {
					ROOT = {
						has_country_flag = wtt_political_struggle
						NOT = { has_country_flag = wtt_political_struggle_paused }
					}
				}
				FROM.FROM = {
					WTT_make_ROOT_majority_1_point_over_next = yes
				}
			}
			if = {
				limit = {
					FROM.FROM = {
						WTT_is_potential_political_struggle_state = yes
					}
				}
				WTT_political_struggle_recalculate_support = yes
			}

			# Fall of Tokyo
			if = {
				limit = {
					original_tag = JAP
					has_government = communism
					has_completed_focus = JAP_cast_the_die
					282 = { is_controlled_by = ROOT }
					FROM = {
						original_tag = JAP
						has_government = fascism
					}
				}
				FROM = { save_event_target_as = jap_civ_loser }
				MAN = { country_event = { id = wtt_japan.121 days = 1 } }
			}
		}
	}

	#FROM is war target
	on_declare_war = {
		effect = {
			#Clear any tribute and support flag if new communists end up at war with SOV
			if = {
				limit = { has_country_flag = WTT_support_from_soviet }
				PRC_soviet_tribute_clear_all = yes
				clr_country_flag = WTT_support_from_soviet
			}
			FROM = {
				if = {
					limit = { has_country_flag = WTT_support_from_soviet }
					PRC_soviet_tribute_clear_all = yes
					clr_country_flag = WTT_support_from_soviet
				}
			}
			if = {
				limit = {
					OR = {
						AND = {
							tag = GER
							FROM = { tag = SOV }
						}
					}
					GER = { has_completed_focus = GER_ussr_war_goal }
				}
				hidden_effect = {
					GER = { 
						add_timed_idea = { idea = GER_barbarossa days = 180 }
						activate_mission = GER_barbarossa_mission
					}
				}
			}
			if = {
				limit = {
					OR = {
						AND = {
							tag = SOV
							FROM = { tag = GER }
						}
					}
					GER = { has_completed_focus = GER_ussr_war_goal }
					NOT = {
						any_state = {
							any_neighbor_state = {
								controller = {
									is_in_faction_with = SOV
								}
							}
							controller = {
								OR = {
									tag = GER
									is_subject_of = GER
								}
							}
							GER = {
								divisions_in_state = { state = PREV size < 8 }
							}
						}
					}
				}
				hidden_effect = {
					GER = { 
						add_timed_idea = { idea = GER_barbarossa days = 90 }
						activate_mission = GER_barbarossa_mission
					}
				}
			}
		}
	}
	#FROM is faction leader on join faction requests.
	on_offer_join_faction = {
		effect = {
			if = {
				limit = {
					FROM = { is_literally_china = yes }
					event_target:WTT_current_china_leader = {
						tag = ROOT
					}
				}
				event_target:WTT_current_china_leader = {
					CHI_unity_level_up = yes #adds surrender defense
				}					
			}
		}
	}

	on_leave_faction = {
		effect = {
			if = {
				limit = {
					is_literally_china = yes
					event_target:WTT_current_china_leader = {
						tag = FROM
					}
				}
				FROM = {
					CHI_unity_level_down = yes #adds surrender defense
				}					
			}
		}
	}

	#ROOT is subject FROM is overlord
	on_subject_annexed = {
		effect = {
			if = {
				limit = {
					FROM = { tag = CHI }
					OR = {
						tag = YUN
						tag = GXC
					}
				}
				FROM = { set_country_flag = achievement_romance_of_the_three_kingdoms_@ROOT }
			}
		}
	}

	on_nuke_drop = {
		effect = {			
			if = {
				limit = {
					tag = JAP
					FROM = {
						is_controlled_by = USA
						is_core_of = USA
					}
				}
				set_country_flag = achievement_tojo_shot_first
			}
		}
	}

	#ROOT is winner #FROM gets annexed - For civil wars on_civil_war_end is also fired
	on_annex = {
		effect = {
			if = {
				limit = {
					tag = JAP
					FROM = { tag = MEX }
					NOT = {
						any_controlled_state = {
							is_on_continent = europe
						}
					}
				}
				set_country_flag = achievement_sunrise_invasion_conquered_mexico_first
			}

			if = {
				limit = {
					tag = MAN
					is_in_faction = no
					FROM = { tag = JAP }
				}
				set_country_flag = achievement_the_dragon_swallowed_the_sun		
			}

			if = {
				limit = { FROM = { has_country_flag = wtt_refused_giving_leadership } }
				FROM = { clr_country_flag = wtt_refused_giving_leadership }
				clr_global_flag = wtt_war_over_national_leadership
				WTT_restart_political_struggle = yes
			}
		}
	}

	#ROOT is winner #FROM gets annexed - This will also fire on_annex
	on_civil_war_end = {
		effect = {
			# Give back GER focus tree to Nazi Germany if they win the civil war.
			if = {
				limit = {
					original_tag = GER
					has_government = fascism
					has_focus_tree = generic_focus
					FROM = {
						original_tag = GER
						NOT = { has_government = fascism }
					}
				}
				load_focus_tree = { tree = german_focus keep_completed = no }
				unlock_national_focus = GER_rhineland
			}
			# Give back JAP focus tree to Fascist Japan if they win the communist or democratic civil war.
			if = {
				limit = {
					original_tag = JAP
					has_government = fascism
					has_focus_tree = generic_focus
					FROM = {
						original_tag = JAP
						NOT = { has_government = fascism }
					}
				}
				load_focus_tree = { tree = japan_wtt_focus keep_completed = no }
				unlock_national_focus = JAP_purge_the_kodoha_faction
			}
			# Give back RAJ focus tree to Unaligned RAJ if they win the communist or fascist civil war.
			if = {
				limit = {
					original_tag = RAJ
					has_focus_tree = generic_focus
					FROM = {
						original_tag = RAJ
						OR = {
							has_government = fascism
							has_government = communism
						}
					}
				}
				load_focus_tree = { tree = indian_focus keep_completed = no }
				unlock_national_focus = RAJ_provincial_elections
				unlock_national_focus = RAJ_indian_national_congress
			}
			# Give back SAF focus tree to Democratic SAF if they win the fascist civil war.
			if = {
				limit = {
					original_tag = SAF
					has_focus_tree = generic_focus
					FROM = {
						original_tag = SAF
						has_government = fascism
					}
				}
				load_focus_tree = { tree = south_african_focus keep_completed = no }
				unlock_national_focus = SAF_support_the_policy_of_appeasement
			}
			# Give back ITA focus tree to Fascist ITA if they win a civil war.
			if = {
				limit = {
					original_tag = ITA
					has_government = fascism
					has_focus_tree = generic_focus
					FROM = {
						original_tag = ITA
						NOT = { has_government = fascism }
					}
				}
				load_focus_tree = { tree = italian_focus keep_completed = no }
			}
			# Give back SOV focus tree to Communist SOV if they win a civil war.
			if = {
				limit = {
					original_tag = SOV
					has_government = communism
					has_focus_tree = generic_focus
					FROM = {
						original_tag = SOV
						NOT = { has_government = communism }
					}
				}
				load_focus_tree = { tree = soviet_focus keep_completed = no }
			}
		}
	}

	#ROOT is winner #FROM is loser
	on_peaceconference_ended = {
		effect = {
			if = { limit = { has_dlc = "Waking the Tiger" }
				if = {
					limit = {
						event_target:WTT_current_china_leader = { tag = ROOT }
						FROM = { has_country_flag = wtt_refused_giving_leadership }
					}
					FROM = {
						clr_country_flag = wtt_refused_giving_leadership
						remove_ideas = CHI_illegal_regime
					}
					clr_global_flag = wtt_war_over_national_leadership
					WTT_restart_political_struggle = yes
				}
				else_if = {
					limit = {
						has_country_flag = wtt_refused_giving_leadership
						event_target:WTT_current_china_leader = { tag = FROM }
					}
					FROM = {
						WTT_political_struggle_remove_current_country = yes
						clear_global_event_target = WTT_current_china_leader
					}
					save_global_event_target_as = WTT_current_china_leader
					remove_ideas = CHI_illegal_regime
					WTT_restart_political_struggle = yes
				}
			}
			if = {
				limit = {
					original_tag = FRA
					owns_state = 28
				}
				28 = { set_state_name = STATE_28 }
				set_province_name = { id = 9503 name = VICTORY_POINTS_9503 }
				set_province_name = { id = 678 name = VICTORY_POINTS_678 }
			}
		}
	}
}