#	Example:
#
#	example_trigger = {
#		is_country_type = default
#		num_communications > 0
#	}
#
#
#	In a script file:
#
#	trigger = {
#		example_trigger = yes
#	}
#

# 以下无SCOPE，兼容用
has_planetmega_mod = { always = yes }

#以下的SCOPE应当为LEADER
has_any_plnmg_clean_trait = {
	or = {
		has_trait = leader_trait_eager
		#has_trait = leader_trait_eager_2
		has_trait = leader_trait_adaptable
		#has_trait = leader_trait_adaptable_2
		has_trait = trait_ruler_champion_of_the_people
		has_trait = trait_ruler_champion_of_the_people_2
		#has_trait = trait_ruler_champion_of_the_people_3
		has_trait = leader_trait_bureaucrat
		#has_trait = leader_trait_bureaucrat_2
		has_trait = leader_trait_righteous
		has_trait = leader_trait_righteous_2
		# has_trait = leader_trait_architectural_interest
		# has_trait = leader_trait_architectural_interest_2
		# has_trait = trait_ruler_architectural_sense
		# has_trait = trait_ruler_architectural_sense_2
		# has_trait = trait_ruler_architectural_sense_3
		# has_trait = leader_trait_urbanist
		has_trait = leader_trait_utopian_idealist
		has_trait = leader_trait_principled
		has_trait = leader_trait_principled_2
		has_trait = leader_trait_legendary_pious_ascet
		has_trait = leader_trait_legendary_pious_ascet_2
		has_trait = leader_trait_legendary_pious_ascet_3
	}
}
has_any_plnmg_corrupted_trait = {
	or = {
		# has_trait = leader_trait_slipshod
		# has_trait = leader_trait_slipshod_2
		has_trait = leader_trait_fallible
		has_trait = leader_trait_fallible_2
		has_trait = leader_trait_lavish_lifestyle
		has_trait = leader_trait_lavish_lifestyle_2
		has_trait = leader_trait_excessive
		has_trait = leader_trait_excessive_2
		has_trait = leader_trait_embezzler
		has_trait = leader_trait_embezzler_2
		# has_trait = leader_trait_bureaucratic
		# has_trait = leader_trait_bureaucratic_2
		has_trait = leader_trait_corrupt
		has_trait = leader_trait_corrupt_2
		has_trait = leader_trait_profligate
		has_trait = leader_trait_profligate_2
		# has_trait = leader_trait_procrastinator
		# has_trait = leader_trait_procrastinator_2
		has_trait = leader_trait_insider_trading
		has_trait = leader_trait_insider_trading_2
		has_trait = leader_trait_legendary_too_open
		#has_trait = leader_trait_legendary_hoplofobia
	}
}

has_any_plnmg_builder_trait = {
	or = {
		has_trait = leader_trait_architectural_interest
		#has_trait = leader_trait_architectural_interest_2
		has_trait = trait_ruler_architectural_sense
		has_trait = trait_ruler_architectural_sense_2
		has_trait = trait_ruler_architectural_sense_3
		has_trait = leader_trait_urbanist
	}
}

#以下的SCOPE应当为SHIP

# has_plnmg_common_component = {
# 	or = {
# 		has_component = TEST_DMG_TRANS_ARRAY
# 		has_component = PLNMG_DMG_TRANS_ARRAY
# 	}
# }

# is_plnmg_plntmatter_heroship = {
# 	optimize_memory
# 	or = {
# 		is_ship_size = plnmg_plntmatter_heroship_1
# 		is_ship_size = plnmg_plntmatter_heroship_2
# 		is_ship_size = plnmg_plntmatter_heroship_3
# 	}
# }

can_design_plnmg_ib_defence = { #design scope
	optimize_memory
	# or = {
	# 	is_ship_class = shipclass_starbase
	# 	is_ship_class = shipclass_military_station
	# }
	is_designable = yes
	#has_ship_owner_type = country
	from = { is_ai = no }
}

#以下SCOPE应当为pop_job
is_plnmg_miner = {
    optimize_memory
    or = {
        has_job_type = miner
        has_job_type = mining_drone
        has_job_type = plnmg_demodulation_mining_drone
        has_job_type = plnmg_demodulation_miner
        has_job_type = low_tech_miner
        #has_job_type = primitive_miner
    }
}

#以下的SCOPE为SPECIES
has_plnmg_synth_leader_ability = {
	any_playable_country = {
		any_pool_leader = {
			species = {
				is_robotic = yes
				is_same_species = prevprevprev
			}
		}
	}
}

is_plnmg_psionic = {
	optimize_memory
	or = {
        is_psionic_species = yes
		has_trait = trait_shroud_forged
        # #DAP
        # has_trait = trait_soul_induction
        # has_trait = trait_SS_super
        # has_trait = trait_SS_super_ex
        # has_trait = trait_Ge_P_pop
        # has_trait = trait_dora_dragon_spe
        # #魔女之国
        # has_trait = trait_magician
        # has_trait = trait_advanced_magician
        # has_trait = trait_psionic_magician
        # has_trait = trait_black_magician
        # has_trait = trait_advanced_black_magician
        # #魔女之旅
        # has_trait = trait_witch
        # has_trait = trait_victorika
        # has_trait = trait_elaina_descendant
        # has_trait = trait_mage
        # has_trait = trait_dummy
        # has_trait = trait_robot_psionic_processors
        # has_trait = trait_cyberwitch
		if = { #DAP
			limit = { hidden_trigger = { DAP_loaded = yes } }
			or = {
				has_trait = trait_soul_induction
				has_trait = trait_SS_super
				has_trait = trait_SS_super_ex
				has_trait = trait_Ge_P_pop
				has_trait = trait_dora_dragon_spe
			}
		}
		if = { #魔女之国
			limit = { hidden_trigger = { has_global_flag = mod_nation_of_witch } }
			or = {
				has_trait = trait_magician
				has_trait = trait_advanced_magician
				has_trait = trait_psionic_magician
				has_trait = trait_black_magician
				has_trait = trait_advanced_black_magician
			}
		}
		if = { #魔女之旅
			limit = { hidden_trigger = { has_global_flag = has_mod_wwvoyagers } }
			or = {
				has_trait = trait_witch
				has_trait = trait_victorika
				has_trait = trait_elaina_descendant
				has_trait = trait_mage
				has_trait = trait_dummy
				has_trait = trait_robot_psionic_processors
				has_trait = trait_cyberwitch
			}
		}
	}
}

is_plnmg_omnipotent = {
    or = {
        has_trait = trait_plnmg_local_omnipotent_bio
        has_trait = trait_plnmg_local_omnipotent_mac
		#species? = { has_species_flag = treated_as_plnmg_omnipotent }
    }
}

# this = situation

is_planetmega_speed_customized = {
	optimize_memory
	or = {
		owner = {
			is_variable_set = num_plnmg_mega_speed_custom_mult
			check_variable = { which = num_plnmg_mega_speed_custom_mult value != 0 }
		}
		event_target:global_event_country = {
			or = {
				and = {
					is_variable_set = num_plnmg_mega_speed_custom_mult
					check_variable = { which = num_plnmg_mega_speed_custom_mult value != 0 }
				}
				and = {
					prev.owner = { is_ai = no }
					is_variable_set = num_plnmg_mega_speed_player_mult
					check_variable = { which = num_plnmg_mega_speed_player_mult value != 0 }
				}
				and = {
					prev.owner = { is_ai = yes }
					is_variable_set = num_plnmg_mega_speed_ai_mult
					check_variable = { which = num_plnmg_mega_speed_ai_mult value != 0 }
				}
			}
		}
	}
}

# this = megastructure

has_plnmg_expedition_fleet = {
	any_fleet_in_orbit = {
		is_ship_class = shipclass_science_ship
		exists = leader
		owner? = { is_same_value = $owner$ }
	}
	any_fleet_in_orbit = {
		is_ship_class = shipclass_constructor
		owner? = { is_same_value = $owner$ }
	}
	any_fleet_in_orbit = {
		is_ship_class = shipclass_colonizer
		owner? = { is_same_value = $owner$ }
	}
}
has_plnmg_nomad_exp_fleet = {
	any_fleet_in_orbit = {
		owner? = { is_same_value = $owner$ }
		any_owned_ship = {
			exists = colony
			# nor = {
			# 	has_deposit = d_plnmg_extra_galaxy
			# 	has_ship_flag = planet_has_plnmg_extra_galaxy
			# }
		}
	}
	any_fleet_in_orbit = {
		is_ship_size = nomads_constructor
		owner? = { is_same_value = $owner$ }
	}
	any_fleet_in_orbit = {
		is_ship_class = shipclass_science_ship
		exists = leader
		owner? = { is_same_value = $owner$ }
	}
}

# this = planet

plnmg_omnipotent_pop_amount = {
	count_owned_pop_amount = { limit = { is_plnmg_omnipotent = yes } }
}

check_plnmg_build_reach_limit = {
	custom_tooltip = {
		fail_text = plnmg_reach_limit_text
		check_variable = { which = num_plnmg_mega_build_count value < num_plnmg_mega_build_limit }
		check_variable = { which = num_plnmg_mega_owned_count value < num_plnmg_mega_owned_limit }
	}
}
check_plnmg_upgrade_reach_limit = {
	custom_tooltip = {
		fail_text = plnmg_reach_limit_text
        check_variable = { which = num_plnmg_mega_build_count value <= num_plnmg_mega_build_limit }
	}
}

check_plnmg_t1_space = {
	free_district_slots >= 2
}
check_plnmg_t2_space = {
	free_district_slots >= 3
}
check_plnmg_1to2_space = {
	free_district_slots >= 1
}

is_plnmg_gaia_like = {
	is_ideal = yes
	nor = {
		is_artificial = yes
		is_ringworld = yes
	}
	# or = {
	# 	is_planet_class = pc_gaia
	# 	if = {
	# 		limit = { hidden_trigger = { has_global_flag = planetarydiversity } }
	# 		or = {
	# 			is_planet_class = pc_cold_gaia
	# 			is_planet_class = pc_hot_gaia
	# 		}
	# 	}
	# 	#舰R
	# 	if = {
	# 		limit = { hidden_trigger = { has_global_flag = has_wsg_mod } }
	# 		is_planet_class = pc_swim
	# 	}
	# 	#多彩
	# 	if = {
	# 		limit = { hidden_trigger = { has_global_flag = love_between_us } }
	# 		or = {
	# 			is_planet_class = pc_holylight_flower
	# 			is_planet_class = pc_depraved_tentacle
	# 			is_planet_class = pc_ten_prostitution
	# 			is_planet_class = pc_dragon_nest_planet
	# 		}
	# 	}
	# }
}

has_any_plnmg_nanites = {
	optimize_memory
	or = {
		has_deposit = d_nanites_deposit
		has_deposit = d_nanotech_devastation
		has_deposit = d_nanite_harvester_deposit
		has_deposit = d_nanite_harvester_deposit_regular
		has_deposit = d_nanite_harvester_deposit_large
		has_deposit = d_hab_nanites_3
		has_deposit = d_hab_harvester_nanites_1
		has_deposit = d_hab_harvester_nanites_2
		has_deposit = d_hab_harvester_nanites_3
	}
}

has_any_plnmg_fe_building = {
	optimize_memory
	or = {
		has_building = building_affluence_emporium
		has_building = building_affluence_center
		has_building = building_micro_forge
		has_building = building_nano_forge
		has_building = building_nourishment_complex
		has_building = building_nourishment_center
		has_building = building_dimensional_replicator
		has_building = building_dimensional_fabricator
		has_building = building_class_3_singularity
		has_building = building_class_4_singularity
		has_building = building_fe_mine_1
		has_building = building_fe_mine_2
	}
}

has_any_extra_galaxy_deposit = {
	planet = {
		or = {
			and = {
				is_variable_set = plnmg_small_eg_connects
				check_variable = { which = plnmg_small_eg_connects value > 0 }
			}
			and = {
				is_variable_set = plnmg_medium_eg_connects
				check_variable = { which = plnmg_medium_eg_connects value > 0 }
			}
			and = {
				is_variable_set = plnmg_large_eg_connects
				check_variable = { which = plnmg_large_eg_connects value > 0 }
			}
		}
	}
}

is_plnmg_governor_clean = { #廉洁
	or = {
		leader? = { has_any_plnmg_clean_trait = yes }
		and = {
			exists = sector.sector_capital.leader
			sector.sector_capital.leader = { has_any_plnmg_clean_trait = yes }
		}
		owner = {
			or = {
				has_valid_civic = civic_gospel_of_the_masses
				has_valid_civic = civic_worker_coop
				has_valid_civic = civic_idealistic_foundation
				has_valid_civic = civic_efficient_bureaucracy
				has_valid_civic = civic_shared_burden
				and = {
					has_civic = civic_planetmega_builder
					any_owned_leader = {
						is_councilor_type = councilor_planetmega_builder
						has_any_plnmg_clean_trait = yes
					}
				}
			}
		}
	}
}

is_plnmg_governor_corrupted = { #贪腐
	or = {
		leader? = { has_any_plnmg_corrupted_trait = yes }
		and = {
			exists = sector.sector_capital.leader
			sector.sector_capital.leader = { has_any_plnmg_corrupted_trait = yes }
		}
		owner = {
			or = {
				has_valid_civic = civic_ruthless_competition
				has_valid_civic = civic_franchising
				has_civic = civic_criminal_heritage
				has_valid_civic = civic_crusader_spirit_corporate
				has_valid_civic = civic_slaver_guilds
				has_valid_civic = civic_aristocratic_elite
				has_valid_civic = civic_byzantine_bureaucracy
				and = {
					has_civic = civic_planetmega_builder
					any_owned_leader = {
						is_councilor_type = councilor_planetmega_builder
						has_any_plnmg_corrupted_trait = yes
					}
				}
			}
		}
	}
}

has_plnmg_any_strategic_resource = {
	optimize_memory
	or = {
		check_modifier_value = { modifier = planet_miners_volatile_motes_produces_add value > 0 }
		check_modifier_value = { modifier = planet_miners_exotic_gases_produces_add value > 0 }
		check_modifier_value = { modifier = planet_miners_rare_crystals_produces_add value > 0 }
	}
}

is_planetmega_byd_space_need = {
	optimize_memory
	or = {
        has_carrier_flag = planetmega_no_space_need
        and = {
            is_capital = yes
            owner = {
                or = {
                    has_origin = origin_planetmega_rationalist
                    has_country_flag = has_origin_plnmg_ration_like
                }
            }
        }
        solar_system = {
			has_star_flag = planetmega_no_space_need
			or = {
				prev = { carrier_is_type = planet }
				and = {
					has_owner = no
					any_starbase_in_system = {
						is_waystation_starbase = yes
						is_owned_by = prevprev.owner
					}
				}
			}
		}
		sector? = {
			prev = { carrier_is_type = planet }
			has_sector_flag = planetmega_no_space_need
		}
		owner = {
            or = {
                has_technology = tech_planetmega_ultimate_sp
                has_country_flag = planetmega_no_space_need
                and = {
                    has_federation = yes
                    federation = { has_federation_flag = planetmega_no_space_need }
                }
            }
        }
        has_global_flag = planetmega_no_space_need
	}
}

is_plnmg_const_sit_byd_cap_need = { #obviously PREV is situation scope
	optimize_memory
	or = {
		has_deposit = d_plnmg_world_shaper
		has_carrier_flag = planet_has_plnmg_world_shaper
		owner = { has_active_tradition = tr_pm_granchor_swap_5_dynexp_2 }
		and = {
			prev = { is_situation_type = planet_megastructure_constuti }
			owner = { has_technology = tech_planetmega_realedit }
		}
	}
}

is_plnmg_mod_city_world = { #used along with planet_is_ecu_equivalent = yes
	optimize_memory
	or = {
        has_carrier_flag = treated_as_city_world
        # #行星多样性
        # uses_district_set = pd_hive_world
        # uses_district_set = pd_machine_world
        # #舰R
        # uses_district_set = dyson_ring_world
        # uses_district_set = wg_hive_city_world
        # uses_district_set = port_world
        # uses_district_set = sh_world
        # uses_district_set = uf_world
        # uses_district_set = auto_uf_world
        # #多彩
        # uses_district_set = holylight_city
        # uses_district_set = depraved_tentacle
        # uses_district_set = prostitution_city
        # uses_district_set = sex_city
        # uses_district_set = sexgod_city
        # uses_district_set = pc_dc_spore_city
        # #大碧蓝
        # uses_district_set = azur_mind_crisis
        # uses_district_set = azur_harbour
        # #游戏人生
        # uses_district_set = elf_vault
        if = { #行星多样性
            limit = {
                or = {
                    has_planetary_diversity = yes
                    has_global_flag = planetarydiversitymorearcologies
                }
            }
            or = {
                uses_district_set = pd_hive_world
                uses_district_set = pd_machine_world
            }
        }
		if = { #真实空间子模组
			limit = { has_global_flag = "has_new_arcologies_mod" }
			or = {
				uses_district_set = manufactorum
				uses_district_set = agropolis
				uses_district_set = fortoria
				uses_district_set = machinarium
				uses_district_set = genoworld
			}
		}
        if = { #舰R
            limit = { has_global_flag = has_wsg_mod }
            or = {
                uses_district_set = dyson_ring_world
                uses_district_set = wg_hive_city_world
                uses_district_set = port_world
                uses_district_set = sh_world
                uses_district_set = uf_world
                uses_district_set = auto_uf_world
            }
        }
        if = { #多彩
            limit = { has_global_flag = love_between_us }
            or = {
                uses_district_set = holylight_city
                uses_district_set = depraved_tentacle
                uses_district_set = prostitution_city
                uses_district_set = sex_city
                uses_district_set = sexgod_city
                #uses_district_set = dragon_nest_planet
                uses_district_set = pc_dc_spore_city
                #uses_district_set = pc_ten_prostitution
            }
        }
        if = { #大碧蓝
            limit = { has_global_flag = azurlane_KuRUMiiii }
            or = {
                uses_district_set = azur_mind_crisis
                uses_district_set = azur_harbour
            }
        }
        if = { #游戏人生
            limit = { has_global_flag = has_ngnl_mod_change_version3 }
            uses_district_set = elf_vault
        }
	}
}

pm_relay_job_is_offspring_supervisor = {
	owner? = { has_origin = origin_progenitor_hive }
}

has_plnmg_culture_shock = {
    optimize_memory
    or = {
        has_culture_shock = yes
        has_modifier = culture_shock_broken_shackles
        has_modifier = fotd_alien_shock
        has_modifier = culture_shock_diplomacy
    }
}

has_plnmg_torture_building = {
    optimize_memory
    or = {
        has_active_building = building_planetmega_lab2_terminator
        has_active_building = building_planetmega_lab2_purifier
        has_active_building = building_planetmega_lab2_devourer
    }
}

planetmega_pret1_visible = {
	optimize_memory
	owner? = {
		or = {
			has_valid_civic = civic_planetmega_rationalist
			has_global_flag = planetmega_pret1_test #effect set_global_flag = planetmega_pret1_test
		}
	}
}
planetmega_pret1_allowed = {
	free_district_slots >= 1
}

ai_can_build_planetmega_on_colony = {
    optimize_memory
	pop_amount > @plnmg_most_pops
    not = { has_carrier_flag = planet_building_groundmega }
	or = {
		and = {
			is_variable_set = num_plnmg_mega_owned_count
			or = {
				check_variable = { which = num_plnmg_mega_owned_count value < modifier:planetmega_owned_limit_add }
				and = {
					is_variable_set = num_plnmg_mega_owned_limit
					check_variable = { which = num_plnmg_mega_owned_count value < num_plnmg_mega_owned_limit }
				}
			}
		}
		and = {
			free_district_slots > 1
			nor = {
				has_carrier_flag = planet_has_groundmega
				has_modifier = planetmega_has_a_megastructure
			}
		}
	}
    # [[RES]
    #     planet_resource_compare = {
    #         type = produces
    #         resource = $RES$
    #         value >= 100
    #     }
    # ]
}

can_planetmega_automate = {
	optimize_memory
	owner? = {
		or = {
			has_menace_perk = menp_cosmo_buildings_4
			has_technology = tech_planetmega_cosmogenesis
			is_tankbound_empire = yes
		}
		nor = {
			has_valid_civic = civic_planetmega_evolution
			has_edict = planetmega_automation_convert
		}
		# added to sync with vanilla
		is_wilderness_empire = no
		is_virtual_empire = no
	}
}

# 以下的SCOPE应当为SYSTEM

has_plnmg_eg_gateway = {
	or = {
		has_megastructure = planetmega_eg_gateway_0
		has_megastructure = planetmega_eg_gateway_final
		has_megastructure = planetmega_eg_gateway_ruined
		has_megastructure = planetmega_eg_gateway_restored
	}
}

has_plnmg_hyper_relay_like = {
	or = {
		has_megastructure = hyper_relay
		has_megastructure = hyper_relay_ruined
		has_megastructure = hyper_relay_restored
		has_megastructure = planetmega_eg_relay
	}
}

#以下的SCOPE应当为COUNTRY

is_plnmg_regular_empire = {
    optimize_memory
    or = {
        is_regular_empire = yes
        is_gestalt = yes
    }
}

is_planetmega_genetic = {
	optimize_memory
	OR = {
		has_tradition = tr_genetics_adopt
		has_tradition = tr_purity_adopt
		has_tradition = tr_mutation_adopt
		has_tradition = tr_cloning_adopt
        #舰R
        # has_ascension_perk = ap_project_r
        # has_ascension_perk = ap_shenhai_upgrade
        # has_ascension_perk = ap_uf_1
        # #多彩
        # has_ascension_perk = ap_lustgod_perfect_idiosoma
        # has_ascension_perk = ap_holylight_god_blood
        # has_ascension_perk = ap_lbm_lust_node
        # has_ascension_perk = ap_memory_awakens
        # has_ascension_perk = ap_ten_fenhua
        # #扶她
        # has_ascension_perk = ap_perfect_pantyhose_body
        # has_ascension_perk = ap_elf_star_common2
        # #狐狸
        # has_ascension_perk = ap_KZ_VOY_extrasensory_perception_evolution
        # #完型世界/童话天平
        # has_ascension_perk = ap_libra_genetic_potential
		if = { #舰R
			limit = { hidden_trigger = { has_global_flag = has_wsg_mod } }
			or = {
				has_ascension_perk = ap_project_r
				has_ascension_perk = ap_shenhai_upgrade
				has_ascension_perk = ap_uf_1
			}
		}
		if = { #多彩
			limit = { hidden_trigger = { has_global_flag = love_between_us } }
			or = {
				has_ascension_perk = ap_lustgod_perfect_idiosoma
				has_ascension_perk = ap_holylight_god_blood
				has_ascension_perk = ap_lbm_lust_node
				has_ascension_perk = ap_memory_awakens
				has_ascension_perk = ap_ten_fenhua
			}
		}
		if = { #扶她
			limit = { hidden_trigger = { has_global_flag = ICE_MOON_ELF_MOD } }
			or = {
				has_ascension_perk = ap_perfect_pantyhose_body
				has_ascension_perk = ap_elf_star_common2
			}
		}
		if = { #狐狸
			limit = { hidden_trigger = { KZ_VOY_MOD = yes } }
			has_ascension_perk = ap_KZ_VOY_extrasensory_perception_evolution
		}
		if = { #完型世界/童话天平
			limit = { hidden_trigger = { libra_mod_plnmg_check = yes } }
			has_ascension_perk = ap_libra_genetic_potential
		}
	}
}

has_planetmega_nanotech = {
	optimize_memory
	or = {
		has_tradition = tr_nanotech_adopt
		has_technology = tech_nanite_repair_system_synth_queen
        # has_origin = origin_machine_nanites
        # has_civic = civic_machine_nanites_culture
        # has_technology = esc_tech_synthetic_lifeform_studies
        #has_technology = tech_disignable_wsg_lady
		if = {
			limit = { hidden_trigger = { has_global_flag = a_deadly_tempest_mod_active } }
			or = {
				has_origin = origin_machine_nanites
				has_civic = civic_machine_nanites_culture
			}
		}
		if = {
			limit = { hidden_trigger = { has_global_flag = Extra_Ship_Components_NEXT } }
			has_technology = esc_tech_synthetic_lifeform_studies
		}
		# if = {
		# 	limit = { has_global_flag = has_wsg_mod }
		# 	has_technology = tech_disignable_wsg_lady
		# }
	}
}

plnmg_mega_produce_imba_check = {
	optimize_memory
	or = {
		and = {
			is_variable_set = num_plnmg_mega_produce_custom_mult
			check_variable = { which = num_plnmg_mega_produce_custom_mult value != 0 }
		}
		and = {
			event_target:global_event_country = {
				is_variable_set = num_plnmg_mega_produce_custom_mult
				check_variable = { which = num_plnmg_mega_produce_custom_mult value != 0 }
			}
		}
		and = {
			is_ai = no
			event_target:global_event_country = {
				is_variable_set = num_plnmg_mega_produce_player_mult
				check_variable = { which = num_plnmg_mega_produce_player_mult value != 0 }
			}
		}
		and = {
			is_ai = yes
			event_target:global_event_country = {
				is_variable_set = num_plnmg_mega_produce_ai_mult
				check_variable = { which = num_plnmg_mega_produce_ai_mult value != 0 }
			}
		}
	}
}

is_planetmega_crisis = {
	optimize_memory
	or = {
		is_crisis_faction = yes
        is_player_crisis = yes
		#is_plnmg_galaxy_defender = yes
		if = {
			limit = { sxx_loaded = yes }
			has_ascension_perk = ap_sxx_worldupgrade
		}
        # #多彩
        # is_country_type = ten_crisis_empire
        # is_country_type = old_empire_cg
        # is_country_type = sower_cg
        # is_country_type = holylight_god_heaven
        # is_country_type = stalwart_dragon_cg
        # is_country_type = tearalaments
        # is_country_type = guardian_cg_monkfish
        # #舰R
        # is_country_type = wg_crisis_country
        # is_country_type = wg_crisis_sub_gate_country
        # is_country_type = wg_shroud_spirits
        # is_country_type = monolith_crisis_country
        # #狐狸
        # is_country_type = KZ_VOY_demon_country
        # #魔物娘
        # is_country_type = lust_shroud
		if = {  #多彩
			limit = { has_global_flag = love_between_us }
			or = {
                is_country_type = ten_crisis_empire
                is_country_type = old_empire_cg
                is_country_type = sower_cg
                is_country_type = holylight_god_heaven
                is_country_type = stalwart_dragon_cg
                is_country_type = tearalaments
                is_country_type = guardian_cg_monkfish
			}
		}
		if = {  #舰R
			limit = { has_global_flag = has_wsg_mod }
			or = {
				has_ascension_perk = ap_wg_crisis #player crisis
				is_country_type = wg_crisis_country
				is_country_type = wg_crisis_sub_gate_country
				is_country_type = wg_shroud_spirits
				is_country_type = monolith_crisis_country
			}
		}
		if = { #狐狸
			limit = { KZ_VOY_MOD = yes }
			is_country_type = KZ_VOY_demon_country
		}
		if = {
			limit = { has_global_flag = monster_girl_flag }
			is_country_type = lust_shroud
		}
	}
}

can_early_recover_planetmega = {
	or = {
		has_civic = civic_planetmega_worshipper
		has_valid_civic = civic_planetmega_rationalist
		hidden_trigger = { has_country_flag = early_rebuild_planetmega }
	}
}
is_enabled_recovering_planetmega = {
	optimize_memory
	or = {
		has_technology = tech_plnmg_rebuild_the_abandoned
		can_early_recover_planetmega = yes
	}
	hidden_trigger = {
		or = {
			is_ai = no
			NOT = { has_global_flag = planetmega_ai_recover_mega_disabled }
		}
	}
}

can_early_build_planetmega = {
    optimize_memory
    or = {
        has_civic = civic_planetmega_builder
        #has_valid_civic = civic_planetmega_rationalist
        hidden_trigger = { has_country_flag = early_build_planetmega }
    }
}
is_enabled_building_planetmega = {
    optimize_memory
    or = {
        has_technology = tech_ground_megastructure
        can_early_build_planetmega = yes
    }
}

is_planetmega_psionic = {
	optimize_memory
	or = {
        is_psionic = yes
        # #DAP
        # has_ascension_perk = ap_soul_induction
        # #魔女之国
        # has_ascension_perk = ap_magic_creation
        # has_tradition = tr_magic_adopt
        # #魔女之旅
        # has_ascension_perk = ap_carolines_wish
        # has_ascension_perk = ap_synthetic_psychics_hal
        # has_tradition = tr_vision_adopt
        # #多彩
        # has_ascension_perk = ap_holy_light_remove_seal
        # has_ascension_perk = ap_lbm_end_of_night
        # has_ascension_perk = ap_ten_magic #我觉得我是（指灵能飞升）
        # #魔物娘
        # has_ascension_perk = ap_elf_star_angel1 #字面上是天使化，而众所周知天使有强大的灵能抛瓦，此事在魔物娘亦有记载
		if = { #DAP
			limit = { hidden_trigger = { DAP_loaded = yes } }
			has_ascension_perk = ap_soul_induction
		}
		if = { #魔女之国
			limit = { hidden_trigger = { has_global_flag = mod_nation_of_witch } }
			or = {
				has_ascension_perk = ap_magic_creation
				has_tradition = tr_magic_adopt
			}
		}
		if = { #魔女之旅
			limit = { hidden_trigger = { has_global_flag = has_mod_wwvoyagers } }
			or = {
				has_ascension_perk = ap_carolines_wish
				has_ascension_perk = ap_synthetic_psychics_hal
				has_tradition = tr_vision_adopt
			}
		}
		if = { #多彩
			limit = { hidden_trigger = { has_global_flag = love_between_us } }
			or = {
				has_ascension_perk = ap_holy_light_remove_seal
				has_ascension_perk = ap_lbm_end_of_night
				has_ascension_perk = ap_ten_magic #我觉得我是（指灵能飞升）
			}
		}
		if = { #魔物娘
			limit = { hidden_trigger = { has_global_flag = ICE_MOON_ELF_MOD } }
			has_ascension_perk = ap_elf_star_angel1 #字面上是天使化，而众所周知天使有强大的灵能抛瓦，此事在魔物娘亦有记载
		}
	}
}

last_plnmg_tech_upsets_fe = {
	optimize_memory
	or = {
		# fe techs
		last_increased_tech = tech_plnmg_space_mega_cosmogenesis
		last_increased_tech = tech_planetmega_cosmogenesis
		# adv&above
		last_increased_tech = tech_plnmg_nano_field_cntr
		last_increased_tech = tech_planetmega_omnipotence_physics
		last_increased_tech = tech_planetmega_omnipotence_society
		last_increased_tech = tech_planetmega_omnipotence_engineering
		last_increased_tech = tech_planetmega_ultimate
		last_increased_tech = tech_planetmega_ultimate_sp
		last_increased_tech = tech_planetmega_realedit
		last_increased_tech = tech_planetmega_reincarnation
	}
}

has_tr_pm_granchor_swap_1 = {
    optimize_memory
	custom_tooltip = {
		fail_text = has_tr_pm_granchor_swap_1_failed_tt
		or = {
            has_tradition = tr_pm_granchor_swap_1
			has_active_tradition = tr_pm_granchor_swap_1
            has_active_tradition = tr_pm_granchor_swap_1_pmm
            has_active_tradition = tr_pm_granchor_swap_1_qt
		}
	}
}
has_tr_pm_granchor_swap_3 = {
    optimize_memory
    custom_tooltip = {
        fail_text = has_tr_pm_granchor_swap_3_failed_tt
        or = {
            has_tradition = tr_pm_granchor_swap_3
            has_active_tradition = tr_pm_granchor_swap_3
            has_active_tradition = tr_pm_granchor_swap_3_autodef
            has_active_tradition = tr_pm_granchor_swap_3_orbit_deter
        }
    }
}
has_tr_pm_granchor_swap_5 = {
    optimize_memory
    custom_tooltip = {
        fail_text = has_tr_pm_granchor_swap_5_failed_tt
        or = {
            has_tradition = tr_pm_granchor_swap_5
            has_active_tradition = tr_pm_granchor_swap_5
            has_active_tradition = tr_pm_granchor_swap_5_alter_dim_const
            has_active_tradition = tr_pm_granchor_swap_5_dynexp_2
            has_active_tradition = tr_pm_granchor_swap_5_lively_use
        }
    }
}

has_plnmg_virtual_jobs = {
    optimize_memory
	or = {
		has_tradition = tr_virtuality_finish
		is_wilderness_empire = yes
		has_country_flag = virtual_jobs
	}
}

is_plnmg_gov_financial = {
    optimize_memory
    or = {
		has_corporate_government = yes
        has_civic = civic_corporate_dominion
        #has_country_flag = treated_as_megacorp
        has_civic = civic_merchant_guilds
        has_civic = civic_relentless_industrialists
		founder_species = { has_trait = trait_thrifty }
    }
}

wanna_extend_plnmg_financial_influence = {
    optimize_memory
    or = {
        has_government = gov_megachurch
        has_government = gov_criminal_syndicate
        has_government = gov_pirate_haven
        has_government = gov_subversive_cult
        has_government = gov_cyber_creed_megachurch
        has_government = gov_cyber_creed_subversive_cult
        #has_government = gov_shroud_transcendent_corporate
    }
}

can_do_any_plnmg_dyn_prod_custom = {
	has_country_flag = planetmega_imba_start
	or = {
		has_country_flag = planetmega_ultimate_adjusting
		has_country_flag = plnmg_dynamic_produces_all
		has_country_flag = plnmg_dynamic_produces_player
		has_country_flag = plnmg_dynamic_produces_ai
	}
}
can_do_any_plnmg_dyn_upkp_custom = {
	has_country_flag = planetmega_imba_start
	or = {
		has_country_flag = planetmega_ultimate_adjusting
		has_country_flag = plnmg_dynamic_upkeep_all
		has_country_flag = plnmg_dynamic_upkeep_player
		has_country_flag = plnmg_dynamic_upkeep_ai
	}
}

can_do_planetmega_ultimate_adjusting = {
	or = {
		has_technology = tech_planetmega_ultimate_sp
		has_country_flag = can_do_planetmega_ultimate_adjusting
	}
}

any_plnmg_neighbor_has_tech = {
	optimize_memory
	or = {
		any_neighbor_country = { has_technology = $TECH$ }
		any_owned_colony = {
			carrier_is_type = ship
			solar_system.owner = {
				exists = this
				has_technology = $TECH$
			}
		}
	}
}

is_plnmg_host_country = {
	has_country_flag = planetmega_host_country
}

# any scope / no scope
met_synth_queen = {
	or = {
		has_global_flag = synth_queen_ongoing
		has_global_flag = synth_queen_defeated
	}
}

#for button_effect
check_if_owner_is_plnmg_from = {
	optimize_memory
	owner? = { is_same_value = from }
}
