
TP_refugee_pop_effect = {
	remove_modifier = "pop_recently_conquered"
	remove_culture_shock = yes
	planet = { 	
		save_event_target_as = RefugeeEscapedPlanet 
		if = {
			limit = {
				is_under_crisis_bombardment = yes
			}
			solar_system = {
				random_fleet_in_system = {
					limit = {
						owner? = {
							is_crisis_faction = yes
						}
					}
					owner = { save_event_target_as = RefugeeEscapedCrisis }
				}
			}
		}		
	}
	owner = {
		save_event_target_as = RefugeeEscapedFrom
		
		# Good habitability and some housing
		if = {
			limit = {
				any_relation = {
					# has_good_habitability_and_housing = yes
					NOT = { has_edict = cc_thought_police }
				}
			}
			random_relation = {
				limit = {
					# has_good_habitability_and_housing = yes
					NOT = { has_edict = cc_thought_police }
				}
				random_owned_planet = {
					limit = {
						habitability = { who = event_target:refugee_pop value >= 0.7 }
						free_housing >= 100
						is_under_colonization = no
						is_controlled_by = owner
						has_orbital_bombardment = no					
					}
					resettle_pop_group = {
						POP_GROUP = event_target:refugee_pop
						PLANET = this
						AMOUNT = 50
					}
					event_target:refugee_pop = { 
						if = { # refresh modifier if already refugee
							limit = { has_modifier = recent_refugee }
							remove_modifier = recent_refugee
							add_modifier = { modifier = recent_refugee years = 10 }
						}
						else = {
							add_modifier = { modifier = recent_refugee years = 10 } 
						}
					}
					if = {
						limit = {
							NOT = {
								owner = {
									any_owned_planet = {
										any_owned_pop_group = {
											NOT = { is_same_value = event_target:refugee_pop }
											is_same_species = event_target:refugee_pop
										}
									}
								}
							}				
						}		
						event_target:refugee_pop = { pop_group_event = { id = refugees.1 } }
					}	
					else = {
						event_target:refugee_pop = { pop_group_event = { id = refugees.2 } }
					}
				}
			}
		}

		# Good habitability
		else_if = {
			limit = {
				any_relation = {
					# has_good_habitability = yes
					NOT = { has_edict = cc_thought_police }
				}
			}
			random_relation = {
				limit = {
					# has_good_habitability = yes
					NOT = { has_edict = cc_thought_police }
				}
				random_owned_planet = {
					limit = {
						habitability = { who = event_target:refugee_pop value >= 0.7 }
						is_under_colonization = no
						is_controlled_by = owner
						has_orbital_bombardment = no					
					}
					resettle_pop_group = {
						POP_GROUP = event_target:refugee_pop
						PLANET = this
						AMOUNT = 50
					}
					event_target:refugee_pop = {
						if = { # refresh modifier if already refugee
							limit = { has_modifier = recent_refugee }
							remove_modifier = recent_refugee
							add_modifier = { modifier = recent_refugee years = 10 }
						}
						else = {
							add_modifier = { modifier = recent_refugee years = 10 } 
						}
					}
					if = {
						limit = {
							NOT = {
								owner = {
									any_owned_planet = {
										any_owned_pop_group = {
											NOT = { is_same_value = event_target:refugee_pop }
											is_same_species = event_target:refugee_pop
										}
									}
								}
							}				
						}		
						event_target:refugee_pop = { pop_group_event = { id = refugees.1 } }
					}	
					else = {
						event_target:refugee_pop = { pop_group_event = { id = refugees.2 } }
					}
				}
			}
		}
		
		# Decent habitability and free housing
		else_if = {
			limit = {
				any_relation = {
					has_decent_habitability_and_housing = yes
					NOT = { has_edict = cc_thought_police }
				}
			}
			random_relation = {
				limit = {
					has_decent_habitability_and_housing = yes
					NOT = { has_edict = cc_thought_police }
				}
				random_owned_planet = {
					limit = {
						habitability = { who = event_target:refugee_pop value >= 0.5 }
						free_housing >= 100
						is_under_colonization = no
						is_controlled_by = owner
						has_orbital_bombardment = no					
					}
					resettle_pop_group = {
						POP_GROUP = event_target:refugee_pop
						PLANET = this
						AMOUNT = 50
					}
					event_target:refugee_pop = {
						if = { # refresh modifier if already refugee
							limit = { has_modifier = recent_refugee }
							remove_modifier = recent_refugee
							add_modifier = { modifier = recent_refugee years = 10 }
						}
						else = {
							add_modifier = { modifier = recent_refugee years = 10 } 
						}
					}
					if = {
						limit = {
							NOT = {
								owner = {
									any_owned_planet = {
										any_owned_pop_group = {
											NOT = { is_same_value = event_target:refugee_pop }
											is_same_species = event_target:refugee_pop
										}
									}
								}
							}				
						}		
						event_target:refugee_pop = { pop_group_event = { id = refugees.1 } }
					}	
					else = {
						event_target:refugee_pop = { pop_group_event = { id = refugees.2 } }
					}
				}
			}
		}
	
		# Decent habitability
		else_if = {
			limit = {
				any_relation = {
					has_decent_habitability = yes
					NOT = { has_edict = cc_thought_police }
				}
			}
			random_relation = {
				limit = {
				has_decent_habitability = yes
				NOT = { has_edict = cc_thought_police }
			}
				random_owned_planet = {
					limit = {
						habitability = { who = event_target:refugee_pop value >= 0.5 }
						is_under_colonization = no
						is_controlled_by = owner
						has_orbital_bombardment = no					
					}
					resettle_pop_group = {
						POP_GROUP = event_target:refugee_pop
						PLANET = this
						AMOUNT = 50
					}
					event_target:refugee_pop = {
						if = { # refresh modifier if already refugee
							limit = { has_modifier = recent_refugee }
							remove_modifier = recent_refugee
							add_modifier = { modifier = recent_refugee years = 10 }
						}
						else = {
							add_modifier = { modifier = recent_refugee years = 10 } 
						}
					}
					if = {
						limit = {
							NOT = {
								owner = {
									any_owned_planet = {
										any_owned_pop_group = {
											NOT = { is_same_value = event_target:refugee_pop }
											is_same_species = event_target:refugee_pop
										}
									}
								}
							}				
						}		
						event_target:refugee_pop = { pop_group_event = { id = refugees.1 } }
					}	
					else = {
						event_target:refugee_pop = { pop_group_event = { id = refugees.2 } }
					}
				}
			}
		}

		# Some habitability and free housing
		else_if = {
			limit = {
				any_relation = {
					has_some_habitability_and_housing = yes
					NOT = { has_edict = cc_thought_police }
				}
			}
			random_relation = {
				limit = {
					has_some_habitability_and_housing = yes
					NOT = { has_edict = cc_thought_police }
				}
				random_owned_planet = {
					limit = {
						habitability = { who = event_target:refugee_pop value >= 0.2 }
						free_housing >= 100
						is_under_colonization = no
						is_controlled_by = owner
						has_orbital_bombardment = no					
					}
					resettle_pop_group = {
						POP_GROUP = event_target:refugee_pop
						PLANET = this
						AMOUNT = 50
					}
					event_target:refugee_pop = {
						if = { # refresh modifier if already refugee
							limit = { has_modifier = recent_refugee }
							remove_modifier = recent_refugee
							add_modifier = { modifier = recent_refugee years = 10 }
						}
						else = {
							add_modifier = { modifier = recent_refugee years = 10 } 
						}
					}
					if = {
						limit = {
							NOT = {
								owner = {
									any_owned_planet = {
										any_owned_pop_group = {
											NOT = { is_same_value = event_target:refugee_pop }
											is_same_species = event_target:refugee_pop
										}
									}
								}
							}				
						}		
						event_target:refugee_pop = { pop_group_event = { id = refugees.1 } }
					}	
					else = {
						event_target:refugee_pop = { pop_group_event = { id = refugees.2 } }
					}
				}
			}
		}

		# Some habitability
		else_if = {
			limit = {
				any_relation = {
					has_some_habitability = yes
					NOT = { has_edict = cc_thought_police }
				}
			}
			random_relation = {
				limit = {
					has_some_habitability = yes
					NOT = { has_edict = cc_thought_police }
				}
				random_owned_planet = {
					limit = {
						habitability = { who = event_target:refugee_pop value >= 0.2 }
						is_under_colonization = no
						is_controlled_by = owner
						has_orbital_bombardment = no					
					}
					resettle_pop_group = {
						POP_GROUP = event_target:refugee_pop
						PLANET = this
						AMOUNT = 50
					}
					event_target:refugee_pop = {
						if = { # refresh modifier if already refugee
							limit = { has_modifier = recent_refugee }
							remove_modifier = recent_refugee
							add_modifier = { modifier = recent_refugee years = 10 }
						}
						else = {
							add_modifier = { modifier = recent_refugee years = 10 } 
						}
					}
					if = {
						limit = {
							NOT = {
								owner = {
									any_owned_planet = {
										any_owned_pop_group = {
											NOT = { is_same_value = event_target:refugee_pop }
											is_same_species = event_target:refugee_pop
										}
									}
								}
							}				
						}		
						event_target:refugee_pop = { pop_group_event = { id = refugees.1 } }
					}	
					else = {
						event_target:refugee_pop = { pop_group_event = { id = refugees.2 } }
					}
				}
			}
		}

		# Any habitability and free housing
		else_if = {
			limit = {
				any_relation = {
					has_any_habitability_and_housing = yes
					NOT = { has_edict = cc_thought_police }
				}
			}
			random_relation = {
				limit = {
					has_any_habitability_and_housing = yes
					NOT = { has_edict = cc_thought_police }
				}
				random_owned_planet = {
					limit = {
						free_housing >= 100
						is_under_colonization = no
						is_controlled_by = owner
						has_orbital_bombardment = no					
					}
					resettle_pop_group = {
						POP_GROUP = event_target:refugee_pop
						PLANET = this
						AMOUNT = 50
					}
					event_target:refugee_pop = {
						if = { # refresh modifier if already refugee
							limit = { has_modifier = recent_refugee }
							remove_modifier = recent_refugee
							add_modifier = { modifier = recent_refugee years = 10 }
						}
						else = {
							add_modifier = { modifier = recent_refugee years = 10 } 
						}
					}
					if = {
						limit = {
							NOT = {
								owner = {
									any_owned_planet = {
										any_owned_pop_group = {
											NOT = { is_same_value = event_target:refugee_pop }
											is_same_species = event_target:refugee_pop
										}
									}
								}
							}				
						}		
						event_target:refugee_pop = { pop_group_event = { id = refugees.1 } }
					}	
					else = {
						event_target:refugee_pop = { pop_group_event = { id = refugees.2 } }
					}
				}
			}
		}

		# Any habitability
		else_if = {
			limit = {
				any_relation = {
					has_any_habitability = yes
				}
			}
			random_relation = {
				limit = {
					has_any_habitability = yes
					NOT = { has_edict = cc_thought_police }
				}
				random_owned_planet = {
					limit = {
						is_under_colonization = no
						is_controlled_by = owner
						has_orbital_bombardment = no					
					}
					resettle_pop_group = {
						POP_GROUP = event_target:refugee_pop
						PLANET = this
						AMOUNT = 50
					}
					event_target:refugee_pop = {
						if = { # refresh modifier if already refugee
							limit = { has_modifier = recent_refugee }
							remove_modifier = recent_refugee
							add_modifier = { modifier = recent_refugee years = 10 }
						}
						else = {
							add_modifier = { modifier = recent_refugee years = 10 } 
						}
					}
					if = {
						limit = {
							NOT = {
								owner = {
									any_owned_planet = {
										any_owned_pop_group = {
											NOT = { is_same_value = event_target:refugee_pop }
											is_same_species = event_target:refugee_pop
										}
									}
								}
							}				
						}		
						event_target:refugee_pop = { pop_group_event = { id = refugees.1 } }
					}	
					else = {
						event_target:refugee_pop = { pop_group_event = { id = refugees.2 } }
					}
				}
			}
		}	
	}	
}
