namespace = harmonious_planet

#星球和谐化处理

#星球和谐化处理入口(条件是拥有和谐国是，并点出理想家园传统)
# No scope, like on_game_start
#on_monthly_pulse = {
event = {
	id = harmonious_planet.1
	hide_window = yes
	is_triggered_only = yes
	
	trigger = {
			any_country = {
					can_harmonize = yes
			}
	}

	immediate = {
	  #蜂群和谐国家
		every_country = {
			limit = { 
					can_harmonize = yes
			}
			#第五阶段盖亚化,盖亚星球有盖亚科技可直接获得修正
			if = {
					limit = {
							#has_technology = "tech_gaia_creation"
							any_owned_planet = {
										NOT = { has_modifier = m_harmony_5 }
										OR = {
												has_modifier = m_harmony_4
												is_planet_class = pc_gaia
												habitable_structure = yes
										}										
			         			can_harmonize_planet = yes
			     		}
					}
					#可以改造的星球，改造进度+1
			   every_owned_planet = {
			   		limit = {
			   				NOT = { has_modifier = m_harmony_5 }
			   				OR = {
			   		      has_modifier = m_harmony_4
			   		      is_planet_class = pc_gaia
									habitable_structure = yes
			   		    }
			         	can_harmonize_planet = yes
			   		}
			   		if = {
			   			limit = {  has_modifier = m_destroy_harmony  }
			   			remove_modifier =  m_destroy_harmony
			   		}
			   		if = {
			   			limit = {  NOT = { has_modifier = m_build_harmony }  }
			   			add_modifier = { modifier =  "m_build_harmony"  days = -1 }
			   		}			   		
			   		change_variable =  { which = "harmony_5"  value = 1 }			   		
			   		if = { #进度满则加修正
			   		    limit = { check_variable = { which = "harmony_5" value > 119 } }
			   		    remove_modifier =  m_harmony_4
			   		    remove_modifier =  m_harmony_3
			   		    remove_modifier =  m_harmony_2
			   		    remove_modifier =  m_harmony_1
			   		    add_modifier = { modifier =  "m_harmony_5"  days = -1 }
			   		    remove_modifier =  m_build_harmony
			   		    if = {
			   		    	limit = {
			   		    		NOR = {
			   		    			is_planet_class = pc_gaia
											habitable_structure = yes
			   		    		}
			   		    	}
			   		    	change_pc = pc_gaia			   		    
			   		    }
			   		    #移除可永久移除的负面特性
			   		    remove_neg_planet_modifier = yes
			   		    #升华行星特性,若失去和谐则退化为原特性
			   		    sublime_planet_modifier = yes
			   		    #随机增加有益新特性,若失去和谐则移除该特性
			   		    
			   		}
			   }
					
			}
			#第四阶段需要气候恢复科技
			if = {
					limit = {
							#has_technology = "tech_gaia_creation"
							has_technology = tech_climate_restoration
							any_owned_planet = {
										has_modifier = m_harmony_3
			         			can_harmonize_planet = yes
			         			NOR = {
			   		    				is_planet_class = pc_gaia
												habitable_structure = yes			   		    				
			         			}	
			     		}
					}
					#可以改造的星球，改造进度+1
			   every_owned_planet = {
			   		limit = {
			   		    has_modifier = m_harmony_3
			         	can_harmonize_planet = yes
			         	NOR = {
			   		    		is_planet_class = pc_gaia
										habitable_structure = yes			   		    				
			         	}
			   		}
			   		if = {
			   			limit = {  has_modifier = m_destroy_harmony  }
			   			remove_modifier =  m_destroy_harmony
			   		}
			   		if = {
			   			limit = {  NOT = { has_modifier = m_build_harmony }  }
			   			add_modifier = { modifier =  "m_build_harmony"  days = -1 }
			   		}			   		
			   		change_variable =  { which = "harmony_4"  value = 1 }			   		
			   		if = { #进度满则加修正
			   		    limit = { check_variable = { which = "harmony_4" value > 119 } }
			   		    remove_modifier =  m_harmony_3
			   		    add_modifier = { modifier =  "m_harmony_4"  days = -1 }
			   		    remove_modifier =  m_build_harmony
			   		    #移除星球部分负面特性
			   		    remove_neg_planet_modifier = yes
			   		}
			   }
					
			}	
			#第三阶段需要操控大气
			if = {
					limit = {
							#has_technology = "tech_atmospheric_manipulation"
							any_owned_planet = {
										has_modifier = m_harmony_2
			         			can_harmonize_planet = yes
			         			NOR = {
			   		    				is_planet_class = pc_gaia
												habitable_structure = yes			   		    				
			         			}
			     		}
					}
					#可以改造的星球，改造进度+1
			   every_owned_planet = {
			   		limit = {
			   		    has_modifier = m_harmony_2
			         	can_harmonize_planet = yes
			         	NOR = {
			   		    				is_planet_class = pc_gaia
												habitable_structure = yes			   		    				
			         	}
			   		}
			   		if = {
			   			limit = {  has_modifier = m_destroy_harmony  }
			   			remove_modifier =  m_destroy_harmony
			   		}
			   		if = {
			   			limit = {  NOT = { has_modifier = m_build_harmony }  }
			   			add_modifier = { modifier =  "m_build_harmony"  days = -1 }
			   		}			   		
			   		change_variable =  { which = "harmony_3"  value = 1 }			   		
			   		if = { #进度满则加修正
			   		    limit = { check_variable = { which = "harmony_3" value > 119 } }
			   		    remove_modifier =  m_harmony_2
			   		    add_modifier = { modifier =  "m_harmony_3"  days = -1 }
			   		    remove_modifier =  m_build_harmony
			   		}
			   }
					
			}			
			#第二阶段需地貌改造
			if = {
					limit = {
							has_technology = "tech_terrestrial_sculpting"
							any_owned_planet = {
										has_modifier = m_harmony_1
			         			can_harmonize_planet = yes
			         			NOR = {
			   		    				is_planet_class = pc_gaia
												habitable_structure = yes			   		    				
			         			}
			     		}
					}
					#可以改造的星球，改造进度+1
			   every_owned_planet = {
			   		limit = {
			   		    has_modifier = m_harmony_1
			         	can_harmonize_planet = yes
			         	NOR = {
			   		    				is_planet_class = pc_gaia
												habitable_structure = yes			   		    				
			         	}	
			   		}
			   		#log = "sescond harmony progress is [This.harmony_2]"
			   		if = {
			   			limit = {  has_modifier = m_destroy_harmony  }
			   			remove_modifier =  m_destroy_harmony
			   		}
			   		if = {
			   			limit = {  NOT = { has_modifier = m_build_harmony }  }
			   			add_modifier = { modifier =  "m_build_harmony"  days = -1 }
			   		}			   		
			   		change_variable =  { which = "harmony_2"  value = 1 }			   		
			   		if = { #进度满则加修正
			   		    limit = { check_variable = { which = "harmony_2" value > 119 } }
			   		     remove_modifier =  m_harmony_1
			   		    add_modifier = { modifier =  "m_harmony_2"  days = -1 }
			   		    remove_modifier =  m_build_harmony
			   		}
			   }
					
			}
			##########
			#第一阶段和谐改造，无需科技,若是核世界或机械世界则需气候恢复后才能进行
			if = {
			   limit = {
			     any_owned_planet = {
			         			NOR = { 
			         					has_modifier = m_harmony_1 
			         					has_modifier = m_harmony_2
			         					has_modifier = m_harmony_3
			         					has_modifier = m_harmony_4
			         					has_modifier = m_harmony_5
			         			}
			         			can_harmonize_planet = yes
			         			NOR = {
			         					is_planet_class = pc_nuked
			         					is_planet_class = pc_machine
			         			}	
			         			NOR = {
			   		    				is_planet_class = pc_gaia
												habitable_structure = yes			   		    				
			         			}	         			
			     }
			   }
			  
			   #有突触的星球，改造进度+1
			   every_owned_planet = {
			   		limit = {
			   		    NOR = { 
			         					has_modifier = m_harmony_1 
			         					has_modifier = m_harmony_2
			         					has_modifier = m_harmony_3
			         					has_modifier = m_harmony_4
			         					has_modifier = m_harmony_5
			         	}
			         	can_harmonize_planet = yes
			         	NOR = {
			         					is_planet_class = pc_nuked
			         					is_planet_class = pc_machine
			         	}	
			         	NOR = {
			   		    				is_planet_class = pc_gaia
												habitable_structure = yes			   		    				
			         	}
			   		}
			   		if = {
			   			limit = {  has_modifier = m_destroy_harmony  }
			   			remove_modifier =  m_destroy_harmony
			   		}
			   		if = {
			   			limit = {  NOT = { has_modifier = m_build_harmony }  }
			   			add_modifier = { modifier =  "m_build_harmony"  days = -1 }
			   		}			   		
			   		change_variable =  { which = "harmony_1"  value = 1 }			   		
			   		if = { #进度满则加修正
			   		    limit = { check_variable = { which = "harmony_1" value > 119 } }
			   		    add_modifier = { modifier =  "m_harmony_1"  days = -1 }
			   		    remove_modifier =  m_build_harmony
			   		}
			   }
			   
			}
			##################
			#第零阶段，气候恢复
			#第零阶段和谐改造，无需气候恢复科技
			if = {
			   limit = {
			   		#has_technology = tech_climate_restoration
			      any_owned_planet = {
			         			can_harmonize_planet = yes
			         			OR = {
			         					is_planet_class = pc_nuked
			         					is_planet_class = pc_machine
			         			}			         			
			       }
			   }
			  
			   #有突触的星球，改造进度+1
			   every_owned_planet = {
			   		limit = {
			         	can_harmonize_planet = yes
			         	OR = {
			         					is_planet_class = pc_nuked
			         					is_planet_class = pc_machine
			         	}	
			   		}
			   		if = {
			   			limit = {  has_modifier = m_destroy_harmony  }
			   			remove_modifier =  m_destroy_harmony
			   		}	
			   		if = {
			   			limit = {  NOT = { has_modifier = m_build_harmony }  }
			   			add_modifier = { modifier =  "m_build_harmony"  days = -1 }
			   		}			   		
			   		change_variable =  { which = "harmony_0"  value = 1 }			   		
			   		if = { #进度满则气候恢复
			   		    limit = { check_variable = { which = "harmony_0" value > 119 } }
			   		    change_pc = rl_habitable_normal 
			   		    set_variable =  { which = "harmony_0"  value = 0 }	
			   		    remove_modifier =  m_build_harmony
			   		    remove_modifier = "irradiated_planet"
			   		}
			   }
			   
			}
			#######################################################
			#退化
			
			#没有突触的星球，改造进度-4
			#气候恢复退化
			if = {
					limit = {
			      any_owned_planet = {
			         			can_harmonize_planet = no
			         			OR = {
			         					is_planet_class = pc_nuked
			         					is_planet_class = pc_machine
			         			}	
			         			OR = {
			         					has_modifier = m_build_harmony
			         					has_modifier = m_destroy_harmony
			         			}		         			
			       }
			   }
			   every_owned_planet = {
			   		limit = {
			   					  can_harmonize_planet = no
			         			OR = {
			         					is_planet_class = pc_nuked
			         					is_planet_class = pc_machine
			         			}	
			         			OR = {
			         					has_modifier = m_build_harmony
			         					has_modifier = m_destroy_harmony
			         			}	
			   		}
			   		if = {
			   			limit = {   has_modifier = m_build_harmony   }
			   			remove_modifier =  m_build_harmony
			   		}
			   		if = {
			   			limit = {  NOT = { has_modifier = m_destroy_harmony }  }
			   			add_modifier = { modifier =  "m_destroy_harmony"  days = -1 }
			   		}
			   		change_variable =  { which = "harmony_0"  value = -4 }	
			   		if = {
			   		    limit = { check_variable = { which = "harmony_0" value < 1 } }			   		   
			   		    remove_modifier =  m_destroy_harmony
			   		}
			   }
			}
			#第一级和谐退化
			if = {
				limit = {
			     any_owned_planet = {
			     					NOR = {  
			     							has_modifier = m_harmony_2
			     							has_modifier = m_harmony_3
			     							has_modifier = m_harmony_4
			     							has_modifier = m_harmony_5
			     					}
			     					OR = {
			     							has_modifier = m_harmony_1
			         					has_modifier = m_build_harmony
			         					has_modifier = m_destroy_harmony
			         			}			         			
			         			can_harmonize_planet = no
			         			NOR = {
			         					is_planet_class = pc_nuked
			         					is_planet_class = pc_machine
			         			}				         					         			
			     }
			   }
			  
			   #退化速度-4
			   every_owned_planet = {
			   		limit = {
			   		    		NOR = {  
			     							has_modifier = m_harmony_2
			     							has_modifier = m_harmony_3
			     							has_modifier = m_harmony_4
			     							has_modifier = m_harmony_5
			     					}
			     					OR = {
			     							has_modifier = m_harmony_1
			         					has_modifier = m_build_harmony
			         					has_modifier = m_destroy_harmony
			         			}			         			
			         			can_harmonize_planet = no
			         			NOR = {
			         					is_planet_class = pc_nuked
			         					is_planet_class = pc_machine
			         			}	
			         			
			   		}
			   		if = {
			   			limit = {   has_modifier = m_build_harmony   }
			   			remove_modifier =  m_build_harmony
			   		}
			   		if = {
			   			limit = {  NOT = { has_modifier = m_destroy_harmony }  }
			   			add_modifier = { modifier =  "m_destroy_harmony"  days = -1 }
			   		}
			   		change_variable =  { which = "harmony_1"  value = -4 }	
			   		if = {
			   		    limit = { check_variable = { which = "harmony_1" value < 1 } }			   		   
			   		    remove_modifier =  m_destroy_harmony
			   		    remove_modifier =  m_harmony_1
			   		}
			   }			
			}
			#第二阶退化
			if = {
					limit = {						
							any_owned_planet = {
			     					has_modifier = m_harmony_2	
			         			can_harmonize_planet = no
			     		}
					}
					#退化进度-4
			   every_owned_planet = {
			   		limit = {
			     					has_modifier = m_harmony_2	
			         			can_harmonize_planet = no
			   		}
			   		if = {
			   			limit = {   has_modifier = m_build_harmony   }
			   			remove_modifier =  m_build_harmony
			   		}
			   		if = {
			   			limit = {  NOT = { has_modifier = m_destroy_harmony }  }
			   			add_modifier = { modifier =  "m_destroy_harmony"  days = -1 }
			   		}		   		
			   		change_variable =  { which = "harmony_2"  value = -4 }			   		
			   		if = { 
			   		    limit = { check_variable = { which = "harmony_2" value < 1 } }
			   		     remove_modifier =  m_harmony_2
			   		    add_modifier = { modifier =  "m_harmony_1"  days = -1 }
			   		    
			   		}
			   }
					
			}			
			#第三阶退化
			if = {
					limit = {						
							any_owned_planet = {
			     					has_modifier = m_harmony_3	
			         			can_harmonize_planet = no
			     		}
					}
					#退化进度-4
			   every_owned_planet = {
			   		limit = {
			     					has_modifier = m_harmony_3	
			         			can_harmonize_planet = no
			   		}
			   		if = {
			   			limit = {   has_modifier = m_build_harmony   }
			   			remove_modifier =  m_build_harmony
			   		}
			   		if = {
			   			limit = {  NOT = { has_modifier = m_destroy_harmony }  }
			   			add_modifier = { modifier =  "m_destroy_harmony"  days = -1 }
			   		}		   		
			   		change_variable =  { which = "harmony_3"  value = -4 }			   		
			   		if = { 
			   		    limit = { check_variable = { which = "harmony_3" value < 1 } }
			   		     remove_modifier =  m_harmony_3
			   		    add_modifier = { modifier =  "m_harmony_2"  days = -1 }
			   		    
			   		}
			   }
					
			}	
			
			#第四阶退化
			if = {
					limit = {						
							any_owned_planet = {
			     					has_modifier = m_harmony_4	
			         			can_harmonize_planet = no
			     		}
					}
					#退化进度-4
			   every_owned_planet = {
			   		limit = {
			     					has_modifier = m_harmony_4	
			         			can_harmonize_planet = no
			   		}
			   		if = {
			   			limit = {   has_modifier = m_build_harmony   }
			   			remove_modifier =  m_build_harmony
			   		}
			   		if = {
			   			limit = {  NOT = { has_modifier = m_destroy_harmony }  }
			   			add_modifier = { modifier =  "m_destroy_harmony"  days = -1 }
			   		}		   		
			   		change_variable =  { which = "harmony_4"  value = -4 }			   		
			   		if = { 
			   		    limit = { check_variable = { which = "harmony_4" value < 1 } }
			   		     remove_modifier =  m_harmony_4
			   		    add_modifier = { modifier =  "m_harmony_3"  days = -1 }
			   		    #恢复负面特性
			   		    recover_neg_planet_modifier = yes
			   		}
			   }
					
			}	
			
			#第五阶退化   
			if = {
					limit = {						
							any_owned_planet = {
			     					has_modifier = m_harmony_5	
			         			can_harmonize_planet = no
			     		}
					}
					#退化进度-4
			   every_owned_planet = {
			   		limit = {
			     					has_modifier = m_harmony_5	
			         			can_harmonize_planet = no
			   		}
			   		if = {
			   			limit = {   has_modifier = m_build_harmony   }
			   			remove_modifier =  m_build_harmony
			   		}
			   		if = {
			   			limit = {  NOT = { has_modifier = m_destroy_harmony }  }
			   			add_modifier = { modifier =  "m_destroy_harmony"  days = -1 }
			   		}		   		
			   		change_variable =  { which = "harmony_5"  value = -4 }			   		
			   		if = { 
			   		    limit = { check_variable = { which = "harmony_5" value < 1 } }
			   		     remove_modifier =  m_harmony_5
								 remove_modifier =  m_destroy_harmony		
								 #行星特性退化	
								 degenerate_planet_modifier = yes   		    
			   		}
			   }
					
			}	
		}
	}
}

#和谐星球被割走，会破坏和谐
planet_event = {
	id = harmonious_planet.2
	hide_window = yes
	is_triggered_only = yes
	
	trigger = {
			from = { allow_harmonize = no }
			OR = {
					has_modifier = m_harmony_1
					has_modifier = m_harmony_2
					has_modifier = m_harmony_3
					has_modifier = m_harmony_4
					has_modifier = m_harmony_5
					has_modifier = m_destroy_harmony
					has_modifier = m_build_harmony
			}
	}
	
	immediate = {
			remove_modifier =  m_build_harmony
			
			if = {
				limit = {
					NOR = {
							has_modifier = m_harmony_1
							has_modifier = m_harmony_2
							has_modifier = m_harmony_3
							has_modifier = m_harmony_4
							has_modifier = m_harmony_5
					}
				}
			  remove_modifier =  m_destroy_harmony
			  break = yes
			}
			if = {
			   			limit = {  NOT = { has_modifier = m_destroy_harmony }  }
			   			add_modifier = { modifier =  "m_destroy_harmony"  days = -1 }
			}
			planet_event = { id = harmonious_planet.3  days = 180   random = 20 }
	}
}

planet_event = {
	id = harmonious_planet.3
	hide_window = yes
	is_triggered_only = yes
		
	immediate = {
			if = {
				limit = { owner = { allow_harmonize = no }}
				remove_modifier =  m_destroy_harmony
				remove_modifier =  m_harmony_1
				remove_modifier =  m_harmony_2
				remove_modifier =  m_harmony_3
				remove_modifier =  m_harmony_4
				remove_modifier =  m_harmony_5
				#所有进度都设置为0
				set_variable =  { which = "harmony_0"  value = 0 }
				set_variable =  { which = "harmony_1"  value = 0 }
				set_variable =  { which = "harmony_2"  value = 0 }
				set_variable =  { which = "harmony_3"  value = 0 }
				set_variable =  { which = "harmony_4"  value = 0 }
				set_variable =  { which = "harmony_5"  value = 0 }
				#普通星球，恢复负面特性
				if = {
					limit = {
						 NOR = {
								is_planet_class = pc_gaia
								habitable_structure = yes
						 }
					}
					recover_neg_planet_modifier = yes				
				}
				#行星特性退化	
				degenerate_planet_modifier = yes  
			}			
	}
}

########
#和谐星球普通人口获得灵能特性

planet_event = {
	id = harmonious_planet.4
	hide_window = yes
	
	trigger = {
		 has_owner = yes
		 owner = { allow_harmonize = yes }
		 OR = {
		 		has_modifier = m_harmony_3
				has_modifier = m_harmony_4
				has_modifier = m_harmony_5
		 }
		 any_pop = {
		 		NOR = {
		 				has_trait = trait_hive_mind
		 				has_trait = trait_magic_machine
		 				has_trait = trait_machine_unit
		 				has_trait = trait_mechanical
		 		}
		 		is_sapient = yes
		 		NOT = { has_trait = trait_psionic }
		 }
	}
	
	mean_time_to_happen = {
		months = 8
	}
		
	immediate = {
			random_pop = {
					limit = {
							NOR = {
		 						has_trait = trait_hive_mind
		 						has_trait = trait_magic_machine
		 						has_trait = trait_machine_unit
		 						has_trait = trait_mechanical
		 					}
		 					is_sapient = yes
		 					NOT = { has_trait = trait_psionic  }
					}
					if = {
							limit = { NOT = { has_trait = trait_latent_psionic } }
							modify_species = { species = This   add_trait = trait_latent_psionic  }
							else = {
									modify_species = { species = This   add_trait = trait_psionic    remove_trait = trait_latent_psionic  }
							}
					}
			}		
	}
}

#种子种下，变成生命树保护区
#A building construction has been completed.
# This = Planet
# From = Tile
#on_building_complete
#planet_event = {#(弃用，有更好的写法)
#	id = harmonious_planet.5
#	hide_window = yes
#	is_triggered_only = yes
	
#	trigger = {
#			from = {
#					has_building = building_life_tree_seed
#			}
#	}
	
#	immediate = {
#			from = {
#					#set_blocker = tb_growing_life_tree
#			}
			#10年后长成生命树
#			planet_event = { id = harmonious_planet.6  days = 10  random = 2 }
#	}
#}

#10年后长成生命树
planet_event = {
	id = harmonious_planet.6
	hide_window = yes
	is_triggered_only = yes
	
	immediate = {
			#检查是否还符合条件(星球被割据会导致条件不符合)
			if = {
					limit = { owner = { allow_harmonize = no } }
					break = yes
			}
			#成长为生命树
			if = {
					limit ={ has_blocker =  tb_growing_life_tree  }
					random_tile = {
							limit ={ has_blocker =  tb_growing_life_tree  }
							remove_blocker = yes
							set_building = building_life_tree							
					}	
					#移除生命树保护区行星修正
					remove_modifier = m_growing_life_tree
					#创建一个蜂群人口
					life_tree_create_pop = yes
					#若是希灵帝国则打上眷族标记
					last_created_pop = {
							pop_event = { id = xilin_pop.2 } 
					}
					#第一棵生命树提示
					if = {
						  limit = {
						  		owner = {
						  				 NOT = { has_country_flag = f_first_life_tree }  
						  		}						  		
						  }
						  owner = { 
						  		set_country_flag = f_first_life_tree
						  		country_event = { id = harmonious_planet.7 } 
						  }
					}	
					#还未拥有过母树，10年后成长为母树	
					if = {
							limit = {
									owner = {
						  				 allow_grow_up_to_mother_life_tree = yes 
						  				 NOT = { has_country_flag = f_ever_have_mother_life_tree } 
						  		}								
							}
							planet_event = { id = harmonious_planet.8  days = 3600  random = 20 } 							
					}#if	
					#判断是否要触发卡拉事件链
					if = {
							limit = {
							   owner = {
							   		has_country_flag = f_xilin_empire
							   		has_country_flag =  f_kala_hive_mind #蜂群支线
							   		NOT = { has_country_flag = f_kala_child_end }
							   		has_modifier = m_kala_gift
							   }
								 any_playable_country = {
								 		has_country_flag = f_kala
								 }
							}
							random_playable_country = {
								 limit = { has_country_flag = f_kala }
							   country_event = { id = kala_story_rdlg.8  days = 10 random = 5 } 			
							}	
							owner = { set_country_flag = f_kala_child_end }			
					}
					#判断是否发生叮当的救赎
					if = {
							limit = {
									owner = {
									  has_country_flag = f_xilin_empire
									  has_country_flag =  f_kala_dingdang
										NOT = {	has_country_flag = f_redemption_of_dingdang_end } #叮当救赎支线
									}
							}
							owner = {
									add_event_chain_counter = { event_chain = redemption_of_dingdang_chain  counter = plant_tree   amount = 1 }
									set_country_flag = f_redemption_of_dingdang_end
					    		#触发女神的祝福
							 		country_event = { id = kala_story_dlg.18  days = 10  random = 5 } 
							}
					}
			}			
	}
}
#第一棵生命树提示
country_event = {
  id = harmonious_planet.7
  title = harmonious_planet.7.name
  
	desc = {
		text = harmonious_planet.7.desc_1
		trigger = {
			NOT = { has_country_flag = f_xilin_empire } 
		}
	}
	desc = {
		text = harmonious_planet.7.desc_2
		trigger = {
			has_country_flag = f_xilin_empire
		}
	}
	picture = GFX_evt_alien_nature
	is_triggered_only = yes
	
	option = {
		name = OK
	}
}

#第一棵生命母树
planet_event = {
  id = harmonious_planet.8
  title = harmonious_planet.8.name
	desc = harmonious_planet.8.desc
	picture = GFX_evt_alien_nature
	is_triggered_only = yes
	
	trigger = {
			owner = {
					allow_harmonize = yes
					NOT = { has_country_flag = f_ever_have_mother_life_tree }  
			}		
			has_building = building_life_tree		
	}
	
	immediate = {
			#成长为母树
			random_tile = {
					limit = { has_building = building_life_tree }
					set_building = building_mother_life_tree
			}
			owner = { 
					set_country_flag = f_ever_have_mother_life_tree  
					#获得第一粒种子
					add_life_tree_seed = yes
			}
	}
	
	option = {
		name = OK
	}
  
}

#失去母树后，普通生命树迅速成长为母树
planet_event = {
  id = harmonious_planet.9
  title = harmonious_planet.9.name
	desc = harmonious_planet.9.desc
	picture = GFX_evt_alien_nature
	is_triggered_only = yes
	
	trigger = {
			owner = {					
					has_country_flag = f_ever_have_mother_life_tree
					allow_grow_up_to_mother_life_tree = yes
					NOT = {
								any_owned_planet = {
													has_building = building_mother_life_tree																
								}
					}
			}	
			has_building = building_life_tree	
	}
	
	immediate = {
			#成长为母树
			random_tile = {
					limit = { has_building = building_life_tree }
					set_building = building_mother_life_tree
			}
	}
	
	option = {
		name = OK
	}
  
}

#生命母树产种子(蜂群不能获得种子)
event = {
	id = harmonious_planet.10
	hide_window = yes
	is_triggered_only = yes

	immediate = {
		every_playable_country = {
			limit = {
					allow_grow_up_to_mother_life_tree = yes
					#种子数<2000, 避免溢出
					check_variable =  { which = "seed_num"  value < 2000 }	
			}
			every_owned_planet = {
				limit = {
					has_building = building_mother_life_tree
				}
				planet_event = { id = harmonious_planet.11 }
			}
		}
	}
}

#20年1粒种子
planet_event = {
  id = harmonious_planet.11
  hide_window = yes
	is_triggered_only = yes
	
	trigger = {	
			has_building = building_mother_life_tree
	}
	
	immediate = {
			#年份计数+1
			change_variable =  { which = "seed_year"  value = 1 }
			if = {
					limit = { check_variable =  { which = "seed_year"  value = 20 }	 }
			    owner = { add_life_tree_seed = yes }
			    set_variable =  { which = "seed_year"  value = 0 } #计数归零
			}	
	}
}

#生命树与生命母树不能被毁坏
# A tile building has been ruined
# This = Planet
# From = Tile
#on_building_ruined
planet_event = {
  id = harmonious_planet.12
  hide_window = yes
	is_triggered_only = yes
	
	trigger = {	
		OR = {
			has_building = building_life_tree
			has_building = building_mother_life_tree
		}
	}
	
	immediate = {
			from = { set_ruined = no }
	}
}

#失去生命母树的国家,生命树成长为母树
event = {
	id = harmonious_planet.13
	hide_window = yes
	is_triggered_only = yes

	immediate = {
		every_playable_country = {
			limit = {
					has_country_flag = f_ever_have_mother_life_tree
					allow_grow_up_to_mother_life_tree = yes
					NOT = {
							any_owned_planet = {
									has_building = building_mother_life_tree
							}	
					}
					any_owned_planet = {
									has_building = building_life_tree
					}					
			}
			random_owned_planet = {
				limit = {
					has_building = building_life_tree
				}
				planet_event = { id = harmonious_planet.9 }
			}
		}
	}
}

#非蜂群和谐国家，生命树使得蜂群成为树灵
event = {
	id = harmonious_planet.14
	hide_window = yes
	is_triggered_only = yes

	immediate = {
		every_playable_country = {
			limit = {
					allow_grow_up_to_mother_life_tree = yes				
			}
			every_owned_planet = {
					limit = {
							OR = {
									has_blocker =  tb_growing_life_tree
									has_building = building_life_tree
									has_building = building_mother_life_tree						
							}
							any_pop = {
									has_trait = trait_hive_mind
									NOT = { has_trait =  trait_tree_spirit }
							}
					}
					random_pop = {
							limit = {
									has_trait = trait_hive_mind
									NOT = { has_trait =  trait_tree_spirit }
							}
							modify_species =  { species = This add_trait = trait_tree_spirit   ideal_planet_class = Prev }
					}
			}
		}
	}
}

#和谐星球被割走，生命树保护区被清除
#Fired whenever a new owner is set for a planet,
#be it after a war or through a trade
#From = Country scope (new owner)
#This = Planet scope
#on_planet_transfer
planet_event = {
	id = harmonious_planet.15
	hide_window = yes
	is_triggered_only = yes
	
	trigger = {
			from = { allow_harmonize = no }
			has_blocker =  tb_growing_life_tree
	}
	
	immediate = {
			random_tile = {
							limit ={ has_blocker =  tb_growing_life_tree  }
							remove_blocker = yes						
			}	
			remove_modifier = m_growing_life_tree
	}
}

#拥有和谐国是非蜂群国家初始化
# secondary species: assign trait_machine_unit, turn into robotics, then create pops
# this: capital planet
# from: secondary species
planet_event = {
	id = harmonious_planet.16
	hide_window = yes
	is_triggered_only = yes

	immediate = {
		from = { save_event_target_as = secondary_species }
		if = { limit = { owner = { has_valid_civic = civic_harmonious_tree } }
			while = {
				count = 4
				random_tile = {
					limit = {
						OR = {
							has_building = building_mining_network_1
							has_building = building_hydroponics_farm_1
						}
						has_pop = no
					}
					create_pop = {
						species = event_target:secondary_species
						ethos = random
					}
				}
			}
			#创建生命树
			random_tile = {
      				limit = {      					
      						has_building = building_hydroponics_farm_1 
      				}
      				#创建生命母树
      				remove_building = yes
      				add_deposit = d_rich_society_deposit
      				set_building = building_mother_life_tree
      				
      } 
      owner = {
      		#设置country_flag
					set_country_flag = f_first_life_tree
					set_country_flag = f_ever_have_mother_life_tree
					#添加种子计数moddifier
					add_modifier = { modifier =  "m_life_tree_seed_num_0"  days = -1 }     
      }
		}
		
	}
}

#和谐星球夺回，没有生命树则树灵处于净化状态
#Fired whenever a new owner is set for a planet,
#be it after a war or through a trade
#From = Country scope (new owner)
#This = Planet scope
#on_planet_transfer
planet_event = {
	id = harmonious_planet.17
	hide_window = yes
	is_triggered_only = yes
	
	trigger = {
			from = { 
				#非蜂群国家拥有和谐理念
				has_valid_civic = civic_harmonious_tree 
				#拥有卡拉的礼物的国家
				has_modifier = m_kala_gift
			}
			NOR = {
				has_blocker =  tb_growing_life_tree
				has_building = building_life_tree
				has_building = building_mother_life_tree
			}	
			any_pop = {
				 has_trait = trait_tree_spirit
			}		
	}
	
	immediate = {
			every_owned_pop = {
							limit ={ has_trait = trait_tree_spirit  }
							purge = yes		
			}	
	}
}

#和谐星球被蜂群夺走，没有生命树则树灵失去树灵特质
#Fired whenever a new owner is set for a planet,
#be it after a war or through a trade
#From = Country scope (new owner)
#This = Planet scope
#on_planet_transfer
planet_event = {
	id = harmonious_planet.18
	hide_window = yes
	is_triggered_only = yes
	
	trigger = {
			from = { 
				has_authority = auth_hive_mind
			}
			NOR = {
				has_blocker =  tb_growing_life_tree
				has_building = building_life_tree
				has_building = building_mother_life_tree
			}	
			any_pop = {
				 has_trait = trait_tree_spirit 
			}		
	}
	
	immediate = {
			every_owned_pop = {
							limit ={ has_trait = trait_tree_spirit  }
							modify_species = { species = This   remove_trait = trait_tree_spirit  }
			}	
	}
}

#重建生命树，树灵停止灭绝
planet_event = {
	id = harmonious_planet.19
	hide_window = yes
	is_triggered_only = yes
	
	trigger = {
			any_pop = {
				 has_trait = trait_tree_spirit
			}		
	}
	
	immediate = {
			every_owned_pop = {
							limit ={ has_trait = trait_tree_spirit  is_being_purged = yes }
							purge = no
			}	
	}
}

#发起培育生命母树研究项目入口
# This = Country
#on_tech_increased
country_event = {
	id = harmonious_planet.20
	hide_window = yes
	is_triggered_only = yes
	
	trigger = {
			OR = {
				has_valid_civic = civic_harmonious_tree
				has_modifier = m_kala_gift
		 } 
		 #拥有基因表达科技
		 #has_technology = "tech_gaia_creation"
		 has_technology = tech_gene_expressions
		 #曾拥有生命母树
		 has_country_flag = f_ever_have_mother_life_tree
		 #触发后就不再触发
		 NOT = { has_country_flag = f_pro_cultivation_of_mother_life_tree } 
	}	
	
	immediate = { 
		set_country_flag = f_pro_cultivation_of_mother_life_tree
		country_event = { id = harmonious_planet.21  days = 10  random = 5 } 
	}	
	
}
#发起培育生命母树研究项目
country_event = {
	id = harmonious_planet.21
	title = harmonious_planet.21.name
	desc = harmonious_planet.21.desc
	picture = GFX_evt_alien_nature
	is_triggered_only = yes
			
	option = {
		name = OK
		#进行特别工程
		capital_scope = {
			enable_special_project = {
				name = "PRO_CULTIVATION_OF_MOTHER_LIFE_TREE_RESEACH"
				location = this
				owner = root
			}
		}
	}	
}

#项目研究成功，获得moddifier
country_event = {
	id = harmonious_planet.22
	title = harmonious_planet.22.name
	desc = harmonious_planet.22.desc
	picture = GFX_evt_alien_nature
	is_triggered_only = yes
			
	option = {
		name = OK
		#获得moddifier
		add_modifier = { modifier =  "m_cultivation_of_mother_life_tree"  days = -1 }
	}	
}

#培育母树成功
planet_event = {
	id = harmonious_planet.23
	title = harmonious_planet.23.name
	desc = harmonious_planet.23.desc
	picture = GFX_evt_alien_nature
	is_triggered_only = yes
	location = root
	
	immediate = {
		  #成长为母树
			random_tile = {
					limit = { has_building = building_life_tree }
					set_building = building_mother_life_tree
			}	
	}
			
	option = {
		name = OK		
	}	
}

#移民进来或非树灵人口成长完成，检查非树灵人口是否达到2/3，树灵是否未达到1/3,若是则树灵保护，给行星加buff
pop_event = {
	id = "harmonious_planet.24"
	hide_window = yes
	
	is_triggered_only = yes

	trigger = {
	  #非树灵
		NOT = { has_trait = trait_hive_mind }
		#有树灵保护法令
		owner = { has_modifier = m_protect_tree_spirit_edict }
	  planet ={
	    #行星有生命树
	  	has_life_tree = yes
	  	#没有树灵保护修正
	  	NOT = { has_modifier = m_protect_tree_spirit }
	    #树灵未达行星1/3
	  	harmonize_hive_is_enough = no
	  	#非树灵人口达到或超过2/3
	  	can_grow_no_affect_tree_spirit = no	  
	  }
	}
	
	immediate = {
		planet = {
				add_modifier = { modifier =  "m_protect_tree_spirit"  days = -1 }
		}
	}
}

#树灵人口达到1/3,移除树灵保护法令buff
pop_event = {
	id = "harmonious_planet.25"
	hide_window = yes
	
	is_triggered_only = yes

	trigger = {
	  #树灵
		has_trait = trait_hive_mind
		#有树灵保护法令
		owner = { has_modifier = m_protect_tree_spirit_edict }
	  planet ={
	  	#有树灵保护法令修正
	  	has_modifier = m_protect_tree_spirit 
	    #树灵达行星1/3
	  	harmonize_hive_is_enough = yes	  	
	  }
	}
	
	immediate = {
		planet = {
				remove_modifier = m_protect_tree_spirit 
		}
	}
}

#人口移民出去，非树灵人口未达2/3，则移除树灵保护法令buff
# A pop has finished migrating to another planet
# Root = pop
# From = Tile on the new planet
# FromFrom = Tile on the old planet
# on_pop_migration_end
pop_event = {
	id = "harmonious_planet.26"
	hide_window = yes
	
	is_triggered_only = yes

	trigger = {
	  #非树灵
		NOT = { has_trait = trait_hive_mind }
		
		fromfrom = {
			planet = {
					#原行星所属国家有树灵保护法令
					owner = { has_modifier = m_protect_tree_spirit_edict }
					#有树灵保护法令修正
	  			has_modifier = m_protect_tree_spirit 
					#非树灵人口未达到2/3
	  			can_grow_no_affect_tree_spirit = yes	  			
			}
		}
	}
	
	immediate = {
		fromfrom = {
			planet = {
					remove_modifier = m_protect_tree_spirit 
			}
		}
	}
}

#人口安置出去，非树灵人口未达2/3，则移除树灵保护法令buff
# A pop has been resettled to another planet
# Root = pop
# From = Tile on the old planet
# on_pop_resettled
pop_event = {
	id = "harmonious_planet.27"
	hide_window = yes
	
	is_triggered_only = yes

	trigger = {
	  #非树灵
		NOT = { has_trait = trait_hive_mind }
		#有树灵保护法令
		owner = { has_modifier = m_protect_tree_spirit_edict }
		from = {
			planet = {	
					#有树灵保护法令修正
	  			has_modifier = m_protect_tree_spirit 				
					#非树灵人口未达到2/3
	  			can_grow_no_affect_tree_spirit = yes	  			
			}
		}
	}
	
	immediate = {
		from = {
			planet = {
					remove_modifier = m_protect_tree_spirit 
			}
		}
	}
}

#采取行星保护法令政策，检查所属行星，符合条件的打上标记
country_event = {
	id = harmonious_planet.28
	hide_window = yes
	is_triggered_only = yes
					
	immediate = {
			every_owned_planet = {
					limit = {	
							#没有行星保护法令修正	   
							NOT = { has_modifier = m_protect_tree_spirit }
							#行星有生命树
							has_life_tree = yes
							#树灵未达行星1/3
	  					harmonize_hive_is_enough = no
	  					#非树灵人口达到或超过2/3
	  					can_grow_no_affect_tree_spirit = no
					}
					add_modifier = { modifier =  "m_protect_tree_spirit"  days = -1 }
			}
	}
}

#检查行星树灵保护修正是否还需要
event = {
	id = harmonious_planet.29
	hide_window = yes
	is_triggered_only = yes

	immediate = {
		#检查行星树灵保护修正是否还需要
		every_playable_country = {
			limit = {	has_modifier = m_protect_tree_spirit_edict	}
			every_owned_planet = {
					limit = {
						has_modifier = m_protect_tree_spirit
						can_grow_no_affect_tree_spirit = yes
					}
					remove_modifier = m_protect_tree_spirit	
			}
		}
	}
}

#生命树星球被夺走
#Fired whenever a new owner is set for a planet,
#be it after a war or through a trade
#From = Country scope (new owner)
#This = Planet scope
#on_planet_transfer
planet_event = {
	id = harmonious_planet.30
	hide_window = yes
	is_triggered_only = yes
	
	trigger = {
			from = { 
				 NOR = {
				 		has_valid_civic = civic_harmonious_tree
						has_modifier = m_kala_gift
				 } 
			}
	}
	immediate = {
			#移除树灵控制修正
			if = {
			   			limit = { has_modifier = m_tree_spirit_population_control }
			   			remove_modifier =  m_tree_spirit_population_control
			}
			#移除树灵保护修正
			if = {
			   			limit = { has_modifier = m_protect_tree_spirit  }
			   			remove_modifier =  m_protect_tree_spirit 
			}
	}
}

#建造一个机器人，检查是否允许建造
# A pop has been built
# This = pop
#开始建机器人
#on_buildable_pop_created
pop_event = {
	id = "harmonious_planet.31"
	hide_window = yes
	
	is_triggered_only = yes

	trigger = {
	  #是机器人
		has_trait = trait_mechanical
		#行星有树灵保护法令
		planet = { has_modifier = m_protect_tree_spirit  }
		
	}
	
	immediate = {
			if = {
					limit = { planet = { sector_controlled = yes } }
					planet = {
						#	sector = { add_minerals = 100 } #报错，暂时跳过
					}
					else = {
							owner = { add_minerals = 100 }
					}
			}
		  kill_pop = yes
	}
}

#生命树刚种下时，检查是否要给行星添加树灵保护修正
planet_event = {
	id = harmonious_planet.32
	hide_window = yes
	is_triggered_only = yes
		
	immediate = {
			#移除树灵控制修正
			if = {
			   			limit = { 
			   					#国家有树灵保护法令
			   			    owner = { has_modifier = m_protect_tree_spirit_edict }
			   					#行星有生命树
	  							has_life_tree = yes
	  							#没有树灵保护修正
	  							NOT = { has_modifier = m_protect_tree_spirit }
			   					#树灵未达行星1/3
	  							harmonize_hive_is_enough = no
	  							#非树灵人口达到或超过2/3
	  							can_grow_no_affect_tree_spirit = no 
	  					}
			   			add_modifier = { modifier =  "m_protect_tree_spirit"  days = -1 }
			}
			
	}
}