namespace = planetmega_global

# game start
event = {
	id = planetmega_global.1
	hide_window = yes
	is_triggered_only = yes
	
	immediate = {
		shared_planetmega_global_initializer = yes
		every_system = {
			limit = { is_bottleneck_system = yes }
			set_star_flag = is_bottleneck_system
			set_global_flag = bottleneck_system_flag_cached
		}
		every_playable_country = { #this should be last, break would stop script execution
			limit = { is_ai = no }
			set_country_flag = planetmega_host_country
			country_event = { id = planetmega_global.8 } #only the host triggers this event
			break = yes #first would be the host
		}
	}
}

# game load
event = {
	id = planetmega_global.2
	hide_window = yes
	is_triggered_only = yes
	
	immediate = {
		shared_planetmega_global_initializer = yes
		# all countries that has planet
		every_country = {
			limit = { num_owned_colonies > 0 }
			every_owned_colony = {
				limit = {
					or = {
						has_plnmg_any_mega = yes
						has_plnmg_any_destroyed_mega = yes
						has_plnmg_any_mega_flag = yes
						planet_has_plnmg_situation = yes
					}
				}
				initialize_plnmg_limit_sys_planet = yes
				refresh_planetmega_flag_owned = yes
				if = {
					limit = { has_carrier_flag = plnmg_both_prod }
				    remove_carrier_flag = plnmg_both_prod
				}
				if = {
					limit = { has_carrier_flag = plnmg_sing_both_prod }
					remove_carrier_flag = plnmg_sing_both_prod
				}
				if = {
					limit = { has_deposit = d_plnmg_maginot_nexus }
					if = {
						limit = { has_global_flag = AOW_installed }
						set_carrier_flag = cannot_be_hit_by_aow_starfall_spear
					}
					# if = {
					# 	limit = { has_gigastructures = yes }
					# 	set_carrier_flag = cant_target_this_planet
					# }
				}
				# while = {
				# 	limit = { has_building = building_plnmg_admin_offspring_ext }
				# 	remove_building = building_plnmg_admin_offspring_ext
				# 	add_building = building_plnmg_admin_offspring
				# }
			}
		}
		# every player (or could be joined) country
		every_playable_country = {
			initialize_plnmg_limit_sys_country = yes
			if = {
				limit = {
					has_technology = tech_planetmega_improved
					nor = {
						has_country_flag = plnmg_shroud_bore_enabled
						has_special_project = PLANETMEGA_PROJECT_SHROUD_BORE
					}
                    or = {
                        can_access_shroud = yes
                        has_breached_shroud = yes
                    }
					not = {
						any_owned_colony = {
							or = {
								has_deposit = d_plnmg_bore_of_the_shroud
								has_carrier_flag = planet_has_plnmg_bore_of_the_shroud
							}
						}
					}
					not = { has_country_flag = planetmega_913_ready }
				}
				country_event = {
					id = planetmega.913
					days = 1000
					random = 500
				}
				set_country_flag = planetmega_913_ready
			}
			if = {
				limit = {
					any_owned_colony = {
						or = {
							has_deposit = d_plnmg_bore_of_the_shroud
							has_carrier_flag = planet_has_plnmg_bore_of_the_shroud
						}
					}
				}
				if = {
					limit = { not = { has_country_flag = plnmg_shroud_bore_enabled } }
					set_country_flag = plnmg_shroud_bore_enabled
				}
				if = {
					limit = { not = { has_country_flag = has_plnmg_bore_of_the_shroud } }
					set_country_flag = has_plnmg_bore_of_the_shroud
				}
			}
		}
		if = {
			limit = { not = { has_global_flag = bottleneck_system_flag_cached } }
			every_system = {
				limit = { is_bottleneck_system = yes }
				set_star_flag = is_bottleneck_system
				set_global_flag = bottleneck_system_flag_cached
			}
		}
	}
}

# monthly update
event = {
	id = planetmega_global.3
	is_triggered_only = yes
	hide_window = yes
	immediate = {
        every_playable_country = {
			set_variable = { which = num_plnmg_mega_owned_count value = 0 }
			country_event = { id = planetmega.90 }
			country_event = { id = planetmega.106 } #行政轴心
			country_event = { id = planetmega.801 } #主脑金字塔
			country_event = { id = planetmega.401 } #凝聚力T1
			country_event = { id = planetmega_effect.93 }
			country_event = { id = planetmega.1101 }
			every_owned_colony = {
				carrier_event = { id = planetmega.110 } #采矿枢机
				carrier_event = { id = planetmega.111 }
				carrier_event = { id = planetmega.921 } #农田T2
				carrier_event = { id = planetmega.926 } #巢田T2
				carrier_event = { id = planetmega.931 } #超算
				carrier_event = { id = planetmega.936 } #脑联
				carrier_event = { id = planetmega.971 } #全能
				carrier_event = { id = planetmega_global.20 }
				carrier_event = { id = planetmega_effect.81 }
				prev = {
					add_variable = {
						which = num_plnmg_mega_owned_count
						value = prev.num_plnmg_mega_owned_count
					}
				}
			}
			if = {
				limit = { has_technology = tech_planetmega_advanced }
				set_variable = { which = var_plnmg_adv_tech_mult value = value:sv_plnmg_adv_tech_mult }
			}
			if = {
				limit = { has_technology = tech_planetmega_ultimate }
				set_variable = { which = var_plnmg_ult_tech_mult value = value:sv_plnmg_ult_tech_mult }
			}
		}
		refresh_global_plnmg_reincarnation_count = yes
	}
}

# yearly update, also get called after loading
event = {
	id = planetmega_global.4
	is_triggered_only = yes
	hide_window = yes
	immediate = {
        plnmg_dynamic_produces_mult_implement = yes
		every_playable_country = {
			country_event = { id = planetmega_global.14 }
			country_event = { id = planetmega_global.28 }
			country_event = { id = planetmega.938 }
			country_event = { id = planetmega_global.16 }
			country_event = { id = planetmega_global.50 }
			every_owned_colony = {
				refresh_plnmg_limit_sys_planet = yes
				carrier_event = { id = planetmega.951 } #巨像
				carrier_event = { id = planetmega.906 } #堡垒T2
				carrier_event = { id = planetmega_effect.82 }
			}
			if = {
				limit = {
					any_owned_colony = {
						or = {
							has_deposit = d_plnmg_bore_of_the_shroud
							has_carrier_flag = planet_has_plnmg_bore_of_the_shroud
						}
					}
				}
				if = {
					limit = { not = { has_country_flag = plnmg_shroud_bore_enabled } }
					set_country_flag = plnmg_shroud_bore_enabled
				}
				if = {
					limit = { not = { has_country_flag = has_plnmg_bore_of_the_shroud } }
					set_country_flag = has_plnmg_bore_of_the_shroud
				}
			}
			# this = {
			# 	if = {
			# 		limit = {
			# 			or = {
			# 				has_any_improved_planetmega_in_empire = yes
			# 				has_any_advanced_planetmega_in_empire = yes
			# 			}
			# 		}
			# 		set_country_flag = has_non_base_planetmega_in_empire
			# 	}
			# 	else = { remove_country_flag = has_non_base_planetmega_in_empire }
			# }
		}
		if = {
			limit = { has_global_flag = extradimensional_invasion_happened }
			every_country = { country_event = { id = planetmega.1121 } }
		}
	}
}

# bi-yearly
event = {
	id = planetmega_global.5
	is_triggered_only = yes
	hide_window = yes
	immediate = {
		every_playable_country = {
			country_event = { id = planetmega.1000 }
		}
	}
}

#five-year
event = {
	id = planetmega_global.6
	hide_window = yes
	is_triggered_only = yes
    immediate = {
		every_playable_country = {
			every_owned_colony = {
				limit = { has_plnmg_any_mega = yes }
				refresh_planetmega_flag_owned = yes
			}
		}
    }
}

#10-year(decade)
event = {
	id = planetmega_global.7
	hide_window = yes
	is_triggered_only = yes
    immediate = {
        set_timed_global_flag = { flag = planetmega_installed years = 10 }
		#plnmg_global_event_country_maintenance = yes
		event_target:global_event_country = {
			if = {
				limit = { check_galaxy_setup_value = { setting = tech_costs_scale value > 0 } }
				set_variable = {
					which = tech_costs_scale_compensation_mult
					value = value:plnmg_tech_costs_scale_compensation
				}
			}
			else = { set_variable = { which = tech_costs_scale_compensation_mult value = 1 } }
		}
    }
}

country_event = {
	id = planetmega_global.8
	title = planetmega_global.8.name
	desc = planetmega_global.8.desc
	picture = GFX_evt_designing_engineers
	is_triggered_only = yes

	after = { hidden_effect = { notice_all_other_empires_plnmg_options_change = yes } }

	option = {
		name = planetmega_global.8.improved
		custom_tooltip = planetmega_now_disabled
		trigger = { has_global_flag = planetmega_improved_banned }
		hidden_effect = {
			remove_global_flag = planetmega_improved_banned
			country_event = { id = planetmega_global.8 }
		}
	}
	option = {
		name = planetmega_global.8.no_improved
		custom_tooltip = planetmega_now_enabled
		trigger = { not = { has_global_flag = planetmega_improved_banned } }
		hidden_effect = {
			set_global_flag = planetmega_improved_banned
			country_event = { id = planetmega_global.8 }
		}
	}

	option = {
		name = planetmega_global.8.advanced
		custom_tooltip = planetmega_now_disabled
		trigger = { has_global_flag = planetmega_advanced_banned }
		hidden_effect = {
			remove_global_flag = planetmega_advanced_banned
			country_event = { id = planetmega_global.8 }
		}
	}
	option = {
		name = planetmega_global.8.no_advanced
		custom_tooltip = planetmega_now_enabled
		trigger = { not = { has_global_flag = planetmega_advanced_banned } }
		hidden_effect = {
			set_global_flag = planetmega_advanced_banned
			country_event = { id = planetmega_global.8 }
		}
	}

	option = {
		name = planetmega_mod.24.enable
		custom_tooltip = planetmega_now_disabled
		trigger = { has_global_flag = planetmega_ai_recover_mega_disabled }
		hidden_effect = {
			remove_global_flag = planetmega_ai_recover_mega_disabled
			country_event = { id = planetmega_global.8 }
		}
	}

	option = {
		name = planetmega_mod.24.disable
		custom_tooltip = planetmega_now_enabled
		trigger = { NOT = { has_global_flag = planetmega_ai_recover_mega_disabled } }
		hidden_effect = {
			set_global_flag = planetmega_ai_recover_mega_disabled
			country_event = { id = planetmega_global.8 }
		}
	}

	option = {
		name = planetmega_mod.25.enable
		custom_tooltip = planetmega_now_disabled
		trigger = { has_global_flag = planetmega_ai_disabled_megabuild }
		hidden_effect = {
			remove_global_flag = planetmega_ai_disabled_megabuild
			country_event = { id = planetmega_global.8 }
		}
	}

	option = {
		name = planetmega_mod.25.disable
		custom_tooltip = planetmega_now_enabled
		trigger = { NOT = { has_global_flag = planetmega_ai_disabled_megabuild } }
		hidden_effect = {
			set_global_flag = planetmega_ai_disabled_megabuild
			country_event = { id = planetmega_global.8 }
		}
	}

	option = {
		name = planetmega_mod.8.respawn_destroyed
		custom_tooltip = planetmega_mod.8.rd_desc
		hidden_effect = {
			ingame_galaxy_spawn_pdm = yes
			notice_all_other_empires_plnmg_options_change = yes
			country_event = { id = planetmega_global.8 }
		}
	}

	option = {
		name = OK
		default_hide_option = yes
		hidden_effect = {
			if = {
				limit = { has_country_flag = triggered_by_planetmega_mod_8 }
				remove_country_flag = triggered_by_planetmega_mod_8
				country_event = { id = planetmega_mod.8 }
			}
		}
	}
}


carrier_event = { #unused
	id = planetmega_global.10
	title = planetmega_global.10.title
	desc = planetmega_global.10.desc
	picture = GFX_planet_mega_mod
	is_triggered_only = yes
	location = root
    trigger = { exists = owner }
    immediate = {
        refresh_plnmg_limit_sys_planet = yes
        owner = { refresh_plnmg_limit_sys_country = yes }
    }
	option = { name = OK }
	option = {
		name = planetmega_global.10.recount
		hidden_effect = { carrier_event = { id = planetmega_global.10 } }
	}
}

country_event = {
	id = planetmega_global.11
	is_triggered_only = yes
	title = planetmega_global.11.name
	desc = planetmega_global.11.desc
	picture = GFX_evt_designing_engineers

	option = {
		name = edict_planetmega_normal_panel
		hidden_effect = { country_event = { id = planetmega_mod.8 } }
	}

	option = {
		name = OK
		default_hide_option = yes
	}
}

country_event = {
	id = planetmega_global.12
	is_triggered_only = yes
	hide_window = yes
	fire_only_once = yes

	trigger = {
		is_ai = no
		or = {
			last_increased_tech = tech_plnmg_rebuild_the_abandoned
			last_increased_tech = tech_ground_megastructure
		}
	}

	immediate = {
		country_event = { id = planetmega_global.11 }
	}
}

carrier_event = {
	id = planetmega_global.13
	is_triggered_only = yes
	title = planetmega_global.13.name
	desc = planetmega_global.13.desc
	picture = GFX_evt_complete_matter_demodulator
	location = root
	immediate = {
		add_planet_devastation = 10
		random_owned_pop_group = { kill_single_pop = yes }
	}
	option = {
		name = CURSES
		ai_chance = { weight = 100 }
		tooltip = {
			add_planet_devastation = 10
			random_owned_pop_group = { kill_single_pop = yes }
		}
	}
}

# yearly update for AI to build a ground megastructure
country_event = {
	id = planetmega_global.14
	is_triggered_only = yes
	hide_window = yes
	trigger = {
		is_ai = yes
		years_passed > 1
		num_owned_colonies > 0
        is_enabled_building_planetmega = yes
		nor = {
			has_country_flag = no_more_planetmega_at_once
			has_global_flag = planetmega_ai_disabled_megabuild
		}
		resource_stockpile_compare = { resource = energy value >= 5000 }
		resource_stockpile_compare = { resource = alloys value >= 3500 }
		resource_stockpile_compare = { resource = minerals value >= 5000 }
		#any_owned_colony = { ai_can_build_planetmega_on_colony = yes }
	}
	immediate = {
		random_list = {
			90 = {}
			10 = {
                modifier = {
                    factor = 3
                    resource_stockpile_compare = { resource = energy value >= 15000 }
                    resource_stockpile_compare = { resource = alloys value >= 10000 }
                    resource_stockpile_compare = { resource = minerals value >= 15000 }
                }
                modifier = {
                    factor = 2
                    has_technology = tech_ground_megastructure
                    can_early_build_planetmega = yes
                }
                modifier = {
                    factor = 1.5
                    has_technology = tech_planetmega_improved
                }
				modifier = {
					factor = 10
					is_plnmg_quota_term_beneficial = yes
					any_agreement = {
						has_term_value = {
							term = planetmega_quota
							value = planetmega_quota_2
						}
					}
				}
				modifier = {
					factor = 15
					is_plnmg_quota_term_beneficial = yes
					any_agreement = {
						has_term_value = {
							term = planetmega_quota
							value = planetmega_quota_3
						}
					}
				}
				# modifier = {
				# 	factor = 0.5
				# 	if = {
				# 		limit = { is_script_extended = yes }
				# 		awakened_empire_decadence > 50
				# 	}
				# 	else = { always = no }
				# }
				add_resource = {
					energy = -5000
					alloys = -3500
					minerals = -5000
				}
				set_timed_country_flag = { flag = no_more_planetmega_at_once months = 20 }
				random_list = {
					1 = {
						modifier = {
							factor = 0
							is_wilderness_empire = yes
						}
						ordered_owned_colony = {
							limit = { ai_can_build_planetmega_on_colony = yes }
							position = 0
							order_by = value:sv_plnmg_plnt_res_prod|RES|energy|
							add_plnmg_mega = { TYPE = giant_power_battery on_actions = yes }
						}
					}
					1 = {
						modifier = {
							factor = 5
							is_lithoid_empire = yes
						}
						ordered_owned_colony = {
                            limit = { ai_can_build_planetmega_on_colony = yes }
                            position = 0
                            order_by = value:sv_plnmg_plnt_res_prod|RES|minerals|
							add_plnmg_mega = { TYPE = matter_demodulator on_actions = yes }
						}
					}
					1 = {
                        modifier = {
                            country_uses_food = no
                            factor = 0
                        }
						ordered_owned_colony = {
                            limit = { ai_can_build_planetmega_on_colony = yes }
                            position = 0
                            order_by = value:sv_plnmg_plnt_res_prod|RES|food|
                            if = {
                                limit = { owner = { is_hive_empire = yes } }
                                add_plnmg_mega = { TYPE = hive_farm_edifice on_actions = yes }
                            }
                            else = { add_plnmg_mega = { TYPE = vertical_farm_edifice on_actions = yes } }
						}
					}
					1 = {
						ordered_owned_colony = {
                            limit = { ai_can_build_planetmega_on_colony = yes }
                            position = 0
                            order_by = value:sv_plnmg_plnt_sci_prod
							add_plnmg_mega = { TYPE = great_laboratory on_actions = yes }
						}
					}
					1 = {
						modifier = {
							factor = 0
							is_gestalt = yes
						}
						ordered_owned_colony = {
                            limit = {
                                is_capital = no
                                ai_can_build_planetmega_on_colony = yes
                            }
                            position = 0
                            order_by = value:sv_plnmg_plnt_res_prod|RES|unity|
                            if = {
                                limit = { owner = { can_build_hakkou_ichiu_tower = yes } }
                                add_plnmg_mega = { TYPE = hakkou_ichiu_tower on_actions = yes }
                            }
                            else = { add_plnmg_mega = { TYPE = heaven_colossal on_actions = yes } }
						}
					}
					1 = {
						modifier = {
							country_uses_consumer_goods = no
							factor = 0
						}
						ordered_owned_colony = {
                            limit = { ai_can_build_planetmega_on_colony = yes }
                            position = 0
                            order_by = value:sv_plnmg_plnt_res_prod|RES|consumer_goods|
							add_plnmg_mega = { TYPE = centralized_manufactory on_actions = yes }
						}
					}
					1 = {
						modifier = {
							factor = 0
							is_wilderness_empire = yes
						}
						ordered_owned_colony = {
                            limit = { ai_can_build_planetmega_on_colony = yes }
                            position = 0
                            order_by = value:sv_plnmg_plnt_res_prod|RES|alloys|
							add_plnmg_mega = { TYPE = advanced_forge on_actions = yes }
						}
					}
					1 = {
						modifier = {
							factor = 0
							is_wilderness_empire = yes
						}
						ordered_owned_colony = {
                            limit = {
                                or = {
                                    is_capital = yes
                                    is_sector_capital = yes
                                }
                                ai_can_build_planetmega_on_colony = yes
                            }
                            position = 0
                            order_by = value:sv_plnmg_plnt_res_prod|RES|unity|
                            if = {
                                limit = { owner = { is_gestalt = yes } }
                                add_plnmg_mega = { TYPE = primary_mind_pyramid on_actions = yes }
                            }
                            else = { add_plnmg_mega = { TYPE = admin_axis } }
						}
					}
                    1 = {
						modifier = {
							factor = 0
							or = {
								is_nomadic = yes
								is_wilderness_empire = yes
							}
						}
                        ordered_owned_colony = {
                            limit = { ai_can_build_planetmega_on_colony = yes }
                            position = 0
                            order_by = value:sv_plnmg_plnt_res_prod|RES|trade|
                            if = {
                                limit = { owner = { is_gestalt = yes } }
                                add_plnmg_mega = { TYPE = logistics_shikara on_actions = yes }
                            }
                            else = { add_plnmg_mega = { TYPE = commercial_shikara on_actions = yes } }
                        }
                    }
					1 = {
						modifier = {
							factor = 0
							is_nomadic = yes
						}
						ordered_owned_colony = {
							limit = { ai_can_build_planetmega_on_colony = yes }
							position = 0
							order_by = modifier:planet_defense_armies_add
							if = {
								limit = { owner = { is_hive_empire = yes } }
								add_plnmg_mega = { TYPE = resistance_organ on_actions = yes }
							}
							else = { add_plnmg_mega = { TYPE = war_headquarter on_actions = yes } }
						}
					}
					1 = {
						modifier = {
							factor = 10
							has_civic = civic_machine_servitor
						}
						modifier = {
							factor = 0
							is_wilderness_empire = yes
						}
						ordered_owned_colony = {
							limit = { ai_can_build_planetmega_on_colony = yes }
							position = 0
							inverse = yes
							order_by = trigger:free_amenities
							add_plnmg_mega = { TYPE = ecodome_complex on_actions = yes }
						}
					}
					1 = {
						modifier = {
							factor = 0
							is_wilderness_empire = no
						}
						ordered_owned_colony = {
							limit = { ai_can_build_planetmega_on_colony = yes }
							position = 0
							order_by = trigger:pop_amount
							add_plnmg_mega = { TYPE = plnmg_wilderness_flora on_actions = yes }
						}
					}
					1 = {
						modifier = {
							factor = 0
							is_wilderness_empire = no
						}
						ordered_owned_colony = {
							limit = { ai_can_build_planetmega_on_colony = yes }
							position = 0
							order_by = trigger:pop_amount
							add_plnmg_mega = { TYPE = plnmg_wilderness_terra on_actions = yes }
						}
					}
				}
			}
		}
        reroll_random = yes
	}
}

# used to count fe techs
country_event = {
	id = planetmega_global.15
	hide_window = yes
	is_triggered_only = yes
	trigger = { just_researched_fe_tech = yes }
	immediate = {
		initialize_mys_variable = { VAR = num_researched_cosmogenesis_techs }
		change_variable = { which = num_researched_cosmogenesis_techs value = 1 }
	}
}

# used to compress modifier alt_plnmg_mega_and_job_prod_mult
country_event = {
	id = planetmega_global.16
	is_triggered_only = yes
	hide_window = yes
	trigger = { check_modifier_value = { modifier = alt_plnmg_mega_and_job_prod_mult value > 0 } }
	immediate = {
		if = {
			limit = { has_modifier = comp_plnmg_mega_and_job_prod_mult_mod }
			remove_modifier = comp_plnmg_mega_and_job_prod_mult_mod
		}
		add_modifier = {
			modifier = comp_plnmg_mega_and_job_prod_mult_mod
			mult = value:sv_comp_plnmg_mega_and_job_prod_mult
		}
	}
}
country_event = {
	id = planetmega_global.17
	is_triggered_only = yes
	hide_window = yes
	trigger = { last_increased_tech = tech_plnmg_res_repeatable }
	immediate = {
		if = {
			limit = { has_modifier = comp_plnmg_mega_and_job_prod_mult_mod }
			remove_modifier = comp_plnmg_mega_and_job_prod_mult_mod
		}
		add_modifier = {
			modifier = comp_plnmg_mega_and_job_prod_mult_mod
			mult = value:sv_comp_plnmg_mega_and_job_prod_mult
		}
	}
}

# only for debug, do not use it for other purpose
event = {
    id = planetmega_global.19
    is_triggered_only = yes
    hide_window = yes
    immediate = {
        every_system = {
            every_system_planet = {
                limit = { owner? = { is_ai = yes } }
                if = {
                    limit = {
                        or = {
                            has_deposit = d_plnmg_reincarnation
                            has_carrier_flag = planet_has_plnmg_reincarnation
                        }
                    }
                    remove_plnmg_mega = { TYPE = plnmg_reincarnation }
                }
                if = {
                    limit = {
                        or = {
                            has_deposit = d_plnmg_wilderness_flora
                            has_carrier_flag = planet_has_plnmg_wilderness_flora
                        }
                    }
                    remove_plnmg_mega = { TYPE = plnmg_wilderness_flora }
                }
                if = {
                    limit = {
                        or = {
                            has_deposit = d_plnmg_wilderness_terra
                            has_carrier_flag = planet_has_plnmg_wilderness_terra
                        }
                    }
                    remove_plnmg_mega = { TYPE = plnmg_wilderness_terra }
                }
            }
        }
    }
}

# unused
carrier_event = {
	id = planetmega_global.20
	is_triggered_only = yes
	hide_window = yes
	trigger = { always = no }
	immediate = {
		#
	}
}

country_event = {
	id = planetmega_global.21
	is_triggered_only = yes
	title = planetmega_global.11.name
	desc = {
		trigger = { not = { has_global_flag = plnmg_imba_changed_recently } }
		text = planetmega_global.21.desc
	}
	desc = {
		trigger = { has_global_flag = plnmg_imba_changed_recently }
		text = planetmega_global.21.desc_imba
	}
	picture = GFX_evt_designing_engineers
	trigger = { from = { is_scope_valid = yes } }
	option = { name = OK }
}

country_event = {
	id = planetmega_global.22
	is_triggered_only = yes
	hide_window = yes
	trigger = {
		is_country_type = default
		not = { has_origin = origin_scion }
		last_plnmg_tech_upsets_fe = yes
	}
	immediate = {
		every_country = {
			limit = {
				OR = {
					is_country_type = fallen_empire
					is_country_type = awakened_fallen_empire
				}
			}
			if = {
				limit = { has_ai_personality = fallen_empire_materialist }
				add_opinion_modifier = {
					modifier = opinion_fallen_wannabe_materialist
					who = root
				}
			}
			else = {
				add_opinion_modifier = {
					modifier = opinion_fallen_wannabe
					who = root
				}
			}
		}
	}
}

system_event = { #triggered via on_dyson_shoot_started
	id = planetmega_global.23
	is_triggered_only = yes
	hide_window = yes
	trigger = { any_system_ship_colony = { owner? = { is_nomadic = yes } } }
	immediate = {
		#play_sound = { sound = event_super_explosion location = star }
		every_system_ship_colony = {
			limit = {
				owner = {
					is_nomadic = yes
					not = { has_country_flag = dyson_shoot_warned_about_@root }
				}
			}
			owner = {
				set_timed_country_flag = { flag = dyson_shoot_warned_about_@root days = 9 }
				create_message = {
					type = MESSAGE_DYSON_TARGETED
					localization = MESSAGE_DYSON_TARGETED_DESC
					days = 15 #@toast_message_days
					target = root
					variable = { type = name localization = SYSTEM scope = root }
				}
			}
		}
	}
}


country_event = {
	id = planetmega_global.25
	is_triggered_only = yes
	title = planetmega_global.25.name
	desc = planetmega_global.25.desc
	picture = GFX_evt_giant_building_in_progress
	trigger = { always = no }
	option = {
		name = DELETE
		hidden_effect = {
			# every_owned_colony = {
			# 	limit = {
			# 		or = {
			# 			num_districts = { type = district_planetmega_supercom_extra value > 0 }
			# 			num_districts = { type = district_planetmega_brain_extra value > 0 }
			# 		}
			# 	}
			# 	while = {
			# 		limit = { num_districts = { type = district_planetmega_supercom_extra value > 0 } }
			# 		remove_district = district_planetmega_supercom_extra
			# 	}
			# 	while = {
			# 		limit = { num_districts = { type = district_planetmega_brain_extra value > 0 } }
			# 		remove_district = district_planetmega_brain_extra
			# 	}
			# }
			set_country_flag = district_plnmg_lab2_banned
		}
		ai_chance = { weight = 1000 }
	}
	option = {
		name = NO
		ai_chance = { weight = 1 }
	}
}

# build improved ground mega
country_event = {
	id = planetmega_global.28
	is_triggered_only = yes
	hide_window = yes
	trigger = {
        always = no
		# is_ai = yes
		# has_technology = tech_planetmega_improved
		# nor = {
		# 	has_country_flag = no_more_planetmega_at_once
		# 	has_global_flag = planetmega_ai_disabled_megabuild
		# 	has_global_flag = planetmega_improved_banned
		# }
		# resource_stockpile_compare = { resource = energy value >= 10000 }
		# resource_stockpile_compare = { resource = alloys value >= 10000 }
		# resource_stockpile_compare = { resource = minerals value >= 10000 }
		# any_owned_colony = { has_plnmg_ordinary_mega = yes }
	}
	immediate = {
		random_list = {
			19 = {}
			1 = {
				add_resource = {
					energy = -10000
					alloys = -10000
					minerals = -10000
				}
				set_timed_country_flag = { flag = no_more_planetmega_at_once months = 30 }
			}
		}
	}
}


country_event = {
	id = planetmega_global.30
	is_triggered_only = yes
	hide_window = yes
	trigger = { has_origin = origin_planetmega_builder }
	immediate = {
		capital_scope = {
			every_deposit = {
				limit = { is_plnmg_any_mega = yes }
				remove_deposit = yes
			}
			every_deposit = {
				limit = { is_plnmg_any_destroyed_mega = yes }
				clear_blocker = yes
			}
			spawn_random_plnmg_mega = yes
		}
	}
}
country_event = {
	id = planetmega_global.31
	is_triggered_only = yes
	hide_window = yes
	trigger = { has_origin = origin_planetmega_worshipper }
	immediate = {
		capital_scope = {
			every_deposit = {
				limit = { is_plnmg_any_mega = yes }
				remove_deposit = yes
			}
			every_deposit = {
				limit = { is_plnmg_any_destroyed_mega = yes }
				clear_blocker = yes
			}
			spawn_random_plnmg_destroyed_mega = yes
		}
	}
}
country_event = {
	id = planetmega_global.32
	is_triggered_only = yes
	hide_window = yes
	trigger = {
        always = no
        #has_origin = origin_plnmg_scp_apotheosis
    }
	immediate = {
		capital_scope = {
			every_deposit = {
				limit = { is_plnmg_any_mega = yes }
				remove_deposit = yes
			}
			every_deposit = {
				limit = { is_plnmg_any_destroyed_mega = yes }
				clear_blocker = yes
			}
			spawn_random_plnmg_destroyed_mega = yes
		}
	}
}
country_event = {
	id = planetmega_global.33
	is_triggered_only = yes
	hide_window = yes
	trigger = { has_origin = origin_planetmega_rationalist }
	immediate = {
		capital_scope = {
			every_deposit = {
				limit = { is_plnmg_any_destroyed_mega = yes }
				clear_blocker = yes
			}
			while = {
				count = value:sv_plnmg_rationalist_mega_count
				spawn_random_plnmg_mega = yes
			}
		}
	}
}

country_event = {
	id = planetmega_global.34
	is_triggered_only = yes
	hide_window = yes
	trigger = {
		or = {
			has_origin = origin_planetmega_builder
			capital_scope = {
				count_deposit = {
					count = 1
					limit = { is_plnmg_any_mega = yes }
				}
			}
		}
		not = { has_country_flag = custom_start_screen }
	}
	immediate = { capital_scope = { add_modifier = { modifier = origin_planetmega_builder_init_modifier } } }
}


#megas
country_event = {
    id = planetmega_global.40
    is_triggered_only = yes
    hide_window = yes
    trigger = {
        from = {
            or = {
                is_megastructure_type = planetmega_eg_gateway_0
                is_megastructure_type = planetmega_colossus_assistant_0
            }
        }
    }
    immediate = {
        create_message = {
            type = MEGASTRUCTURE_BUILT
            localization = MESSAGE_MEGASTRUCTURE_BUILT
            days = 30
            target = from # the Go To
            variable = { type = name localization = MEGASTRUCTURE scope = from }
            variable = { type = name localization = SYSTEM scope = fromfrom }
        }
    }
}

country_event = {
    id = planetmega_global.42
    is_triggered_only = yes
    hide_window = yes
    trigger = {
        from = {
            or = {
                is_megastructure_type = planetmega_eg_gateway_final
                is_megastructure_type = planetmega_eg_gateway_restored
                is_megastructure_type = planetmega_colossus_assistant_1
                is_megastructure_type = planetmega_colossus_assistant_2
                is_megastructure_type = planetmega_colossus_assistant_restored
            }
            if = {
                limit = {
                    exists = planet
                    or = {
                        is_megastructure_type = planetmega_eg_gateway_final
                        is_megastructure_type = planetmega_eg_gateway_restored
                    }
                }
                planet = {
                    nor = {
                        has_carrier_flag = has_plnmg_eg_gateway_final
                        has_carrier_flag = has_plnmg_eg_gateway_restored
                    }
                }
            }
        }
    }
    immediate = {
        create_message = {
            type = MEGASTRUCTURE_UPGRADED
            localization = MESSAGE_MEGASTRUCTURE_UPGRADED
            days = 30
            target = from # the Go To
            variable = { type = name localization = MEGASTRUCTURE scope = from }
            variable = { type = name localization = SYSTEM scope = fromfrom }
        }
        if = {
            limit = {
                has_country_flag = did_not_repair_gateway
                from = {
                    OR = {
                        is_megastructure_type = planetmega_eg_gateway_final
                        is_megastructure_type = planetmega_eg_gateway_restored
                    }
                }
            }
            remove_country_flag = did_not_repair_gateway
        }
        else = {
            every_relation = {
                limit = {
                    not = { is_same_value = root }
                    is_ai = no
                    OR = {
                        has_intel_level = { who = root category = economy level >= 1 }
                        has_intel_level = { who = root category = technology level >= 1 }
                    }
                }
                create_message = {
                    type = ALIEN_MEGASTRUCTURE_BUILT
                    localization = MESSAGE_ALIEN_MEGASTRUCTURE_BUILT
                    days = 30
                    target = fromfrom #Go to system since you might not have enough intel for the megastructure
                    variable = { type = name localization = COUNTRY scope = root }
                    variable = { type = name localization = MEGASTRUCTURE scope = root.from }
                    variable = { type = name localization = SYSTEM scope = root.fromfrom }
                }
            }
        }
    }
}


###传统
country_event = {
	id = planetmega_global.50
	is_triggered_only = yes
	hide_window = yes
	trigger = { has_tradition = tr_pm_granchor_adopt }
	immediate = {
        country_event = { id = planetmega_global.51 }
        country_event = { id = planetmega_global.52 }
        country_event = { id = planetmega_global.53 }
	}
}
country_event = {
	id = planetmega_global.51
	is_triggered_only = yes
	hide_window = yes
    trigger = { has_tr_pm_granchor_swap_1 = yes }
	immediate = {
		export_resource_income_to_variable = {
			resource = physics_research
			variable = pm_granchor_qt_reconst_weight
		}
		export_resource_income_to_variable = {
			resource = engineering_research
			variable = pm_granchor_pmm_core_weight
		}
	}
}
country_event = {
	id = planetmega_global.52
	is_triggered_only = yes
	hide_window = yes
    trigger = { has_tr_pm_granchor_swap_3 = yes }
	immediate = {
		export_resource_income_to_variable = {
			resource = nanites
			variable = pm_granchor_autodef_weight
		}
        if = {
            limit = { has_technology = tech_planetmega_active_defense }
            set_variable = { which = pm_granchor_orbit_deter_weight value = value:count_planet_defense_techs }
        }
	}
}
country_event = {
	id = planetmega_global.53
	is_triggered_only = yes
	hide_window = yes
    trigger = { has_tr_pm_granchor_swap_5 = yes }
	immediate = {
		export_resource_income_to_variable = {
			resource = sr_zro
			variable = pm_granchor_alter_dim_const_weight
		}
		export_resource_income_to_variable = {
			resource = sr_living_metal
			variable = pm_granchor_dynexp_2_weight
		}
	}
}

country_event = {
	id = planetmega_global.999
	is_triggered_only = yes
	title = BLANK_STRING
	desc = BLANK_STRING
	picture = GFX_evt_placeholder_text
	location = event_target:bridge_system
	immediate = {
		random_system = {
			limit = { is_bridge = yes }
			save_event_target_as = bridge_system
		}
		reroll_random = yes
	}
	option = { name = OK }
	option = {
		name = CONTINUE
		country_event = { id = planetmega_global.999 }
	}
}

country_event = {
	id = planetmega_global.1000
	is_triggered_only = yes
	title = planetmega_color_test
	desc = planetmega_color_test_desc
	picture = GFX_evt_placeholder_text
	option = { name = OK }
}
