#以下的SCOPE为PLANET
refresh_plnmg_mega_flag_indiv = {
	if = {
		limit = { has_deposit = d_$TYPE$ }
		set_carrier_flag = planet_has_$TYPE$
	}
}
refresh_planetmega_flag_owned = {
	set_carrier_flag = planet_has_groundmega
	set_variable = { which = num_plnmg_mega_owned_count value = 0 }
	every_deposit = {
		limit = { is_plnmg_any_mega = yes }
		set_deposit_flag = keep_deposit_on_terraform
		prev = { change_variable = { which = num_plnmg_mega_owned_count value = 1 } }
	}
	if = {
		limit = { not = { has_modifier = planetmega_has_a_megastructure } }
		add_modifier = { modifier = planetmega_has_a_megastructure days = -1 }
	}
	if = {
		limit = { has_plnmg_improved_mega = yes }
		set_carrier_flag = planet_has_improved_groundmega
	}
	if = {
		limit = { has_plnmg_advanced_mega = yes }
		set_carrier_flag = planet_has_advanced_groundmega
	}
	if = {
		limit = { has_plnmg_ultimate_mega = yes }
		set_carrier_flag = planet_has_ultimate_groundmega
	}
	#
	refresh_plnmg_mega_flag_indiv = { TYPE = great_laboratory }
	refresh_plnmg_mega_flag_indiv = { TYPE = advanced_forge }
	refresh_plnmg_mega_flag_indiv = { TYPE = heaven_colossal }
	refresh_plnmg_mega_flag_indiv = { TYPE = giant_power_battery }
	refresh_plnmg_mega_flag_indiv = { TYPE = commercial_shikara }
	refresh_plnmg_mega_flag_indiv = { TYPE = war_headquarter }
	refresh_plnmg_mega_flag_indiv = { TYPE = centralized_manufactory }
	refresh_plnmg_mega_flag_indiv = { TYPE = primary_mind_pyramid }
	refresh_plnmg_mega_flag_indiv = { TYPE = matter_demodulator }
	refresh_plnmg_mega_flag_indiv = { TYPE = vertical_farm_edifice }
	refresh_plnmg_mega_flag_indiv = { TYPE = hive_farm_edifice }
	refresh_plnmg_mega_flag_indiv = { TYPE = admin_axis }
	refresh_plnmg_mega_flag_indiv = { TYPE = ecodome_complex }
	#
	refresh_plnmg_mega_flag_indiv = { TYPE = plnmg_singularity_generator }
	refresh_plnmg_mega_flag_indiv = { TYPE = plnmg_bore_of_the_shroud }
	refresh_plnmg_mega_flag_indiv = { TYPE = plnmg_maginot_nexus }
	refresh_plnmg_mega_flag_indiv = { TYPE = plnmg_advanced_agriculture_edifice }
	refresh_plnmg_mega_flag_indiv = { TYPE = plnmg_gene_farm_hive }
	refresh_plnmg_mega_flag_indiv = { TYPE = plnmg_supercom }
	refresh_plnmg_mega_flag_indiv = { TYPE = plnmg_fusion_brain }
	refresh_plnmg_mega_flag_indiv = { TYPE = plnmg_nirvana_factory }
	refresh_plnmg_mega_flag_indiv = { TYPE = plnmg_financial_shikara }
	#
	refresh_plnmg_mega_flag_indiv = { TYPE = plnmg_colossus_assist }
	refresh_plnmg_mega_flag_indiv = { TYPE = plnmg_extra_galaxy }
	refresh_plnmg_mega_flag_indiv = { TYPE = plnmg_omnipotence }
	refresh_plnmg_mega_flag_indiv = { TYPE = plnmg_nano_storm }
}

spawn_random_plnmg_destroyed_mega = {
	random_list = {
		7 = { # tier 1
			random_list = {
				3 = { add_blocker = { type = d_plnmg_lab_destroyed } }
				2 = { add_blocker = { type = d_plnmg_forge_destroyed } }
				3 = { add_blocker = { type = d_plnmg_heaven_destroyed } }
				2 = { add_blocker = { type = d_plnmg_power_destroyed } }
				1 = { add_blocker = { type = d_plnmg_commercial_destroyed } }
				2 = { add_blocker = { type = d_plnmg_headquarter_destroyed } }
				2 = { add_blocker = { type = d_plnmg_factory_destroyed }}
				1 = { add_blocker = { type = d_plnmg_pyramid_destroyed } }
				1 = { add_blocker = { type = d_plnmg_matter_destroyed } }
				2 = { add_blocker = { type = d_plnmg_farm_destroyed } }
				1 = { add_blocker = { type = d_plnmg_hive_farm_destroyed } }
				3 = { add_blocker = { type = d_plnmg_admin_destroyed } }
			}
		}
		3 = { # tier 2 & byd
			random_list = {
				3 = { add_blocker = { type = d_plnmg_singularity_destroyed } }
				2 = { add_blocker = { type = d_plnmg_bore_destroyed } }
				1 = { add_blocker = { type = d_plnmg_war2_destroyed } }
				4 = { add_blocker = { type = d_plnmg_farm2_destroyed } }
				1 = { add_blocker = { type = d_plnmg_farm2_hive_destroyed } }
				3 = { add_blocker = { type = d_plnmg_supercom_destroyed } }
				1 = { add_blocker = { type = d_plnmg_fusion_brain_destroyed } }
				1 = { add_blocker = { type = d_plnmg_nirfac_destroyed } }
				#1 = { add_deposit = d_plnmg_ca_destroyed }
				#1 = { add_deposit = d_plnmg_eg_destroyed }
			}
		}
	}
    reroll_random = yes
}

spawn_random_plnmg_mega = {
	optimize_memory
	random_list = {
		1 = {
			modifier = {
				factor = 5
				owner = { has_corporate_government = yes }
			}
			modifier = {
				factor = 0
				has_plnmg_any_commerce = yes
			}
			if = {
				limit = { owner = { is_gestalt = yes } }
				add_plnmg_mega = { TYPE = logistics_shikara }
			}
			else = { add_plnmg_mega = { TYPE = commercial_shikara } }
		}
		1 = {
			modifier = {
				factor = 0
				has_plnmg_any_heaven = yes
			}
			add_plnmg_mega = { TYPE = heaven_colossal }
		}
		1 = {
			modifier = {
				factor = 0
				has_plnmg_any_militarist_tower = yes
			}
			modifier = {
				factor = 3
				can_build_hakkou_ichiu_tower = yes
			}
			add_plnmg_mega = { TYPE = hakkou_ichiu_tower }
		}
		1 = {
			modifier = {
				factor = 0
				has_plnmg_any_mine = yes
			}
			add_plnmg_mega = { TYPE = matter_demodulator }
		}
		1 = {
			modifier = {
				factor = 0
				owner.founder_species = { is_robotic_species = yes }
			}
			modifier = {
				factor = 0
				has_plnmg_any_farm = yes
			}
			if = {
				limit = { owner = { is_hive_empire = yes } }
				add_plnmg_mega = { TYPE = hive_farm_edifice }
			}
			else = { add_plnmg_mega = { TYPE = vertical_farm_edifice } }
		}
		1 = {
			modifier = {
				factor = 0
				has_plnmg_any_generator = yes
			}
			add_plnmg_mega = { TYPE = giant_power_battery }
		}
		1 = {
			modifier = {
				factor = 0
				has_plnmg_any_forge = yes
			}
			add_plnmg_mega = { TYPE = advanced_forge }
		}
		1 = {
			modifier = {
				factor = 0
				has_plnmg_any_factory = yes
			}
			modifier = {
				factor = 2
				owner = { has_civic = civic_machine_servitor }
			}
			modifier = {
				factor = 5
				owner = { has_civic = civic_machine_obsessional_directive }
			}
			add_plnmg_mega = { TYPE = centralized_manufactory }
		}
		1 = {
			modifier = {
				factor = 0
				has_plnmg_any_lab = yes
			}
			modifier = {
				factor = 2
				owner = { is_materialist = yes }
			}
			add_plnmg_mega = { TYPE = great_laboratory }
		}
		1 = {
			modifier = {
				factor = 0
				has_plnmg_any_fortress = yes
			}
			if = {
				limit = { owner = { is_hive_empire = yes } }
				add_plnmg_mega = { TYPE = resistance_organ }
			}
			else = { add_plnmg_mega = { TYPE = war_headquarter } }
		}
		1 = {
			modifier = {
				factor = 0
				has_plnmg_any_admin = yes
			}
			if = {
				limit = { owner = { is_gestalt = yes } }
				add_plnmg_mega = { TYPE = primary_mind_pyramid }
			}
			else = { add_plnmg_mega = { TYPE = admin_axis } }
		}
		1 = {
			modifier = {
				factor = 0
				or = {
					has_deposit = d_ecodome_complex
					has_carrier_flag = planet_has_ecodome_complex
				}
			}
			modifier = {
				factor = 5
				owner = { has_civic = civic_machine_servitor }
			}
			add_plnmg_mega = { TYPE = ecodome_complex }
		}
	}
    reroll_random = yes
}

initialize_plnmg_limit_sys_planet = {
	#星球的地面巨构已拥有
	set_variable = { which = num_plnmg_mega_owned_count value = value:plnmg_mega_owned_count_value }
    set_variable = { which = num_plnmg_mega_owned_limit value = value:plnmg_mega_owned_limit_value }
	#星球的地面巨构正在建造
	set_variable = { which = num_plnmg_mega_build_count value = value:plnmg_mega_build_count_value }
    set_variable = { which = num_plnmg_mega_build_limit value = value:plnmg_mega_build_limit_value }
}

refresh_plnmg_limit_sys_planet = {
    initialize_plnmg_limit_sys_planet = yes
}

initialize_plnmg_base_mega_check = {
	# this = planet, not useful
	set_variable = { which = plnmg_same_mega_count_temp_$TYPE$ value = 0 }
	every_deposit = {
		limit = { is_deposit_type = d_$TYPE$ }
		prev = { change_variable = { which = plnmg_same_mega_count_temp_$TYPE$ value = 1 } }
	}
	every_targeting_situation = {
		limit = { has_situation_flag = building_$TYPE$ }
		prev = { change_variable = { which = plnmg_same_mega_count_temp_$TYPE$ value = 1 } }
	}
}

#以下的SCOPE为COUNTRY
initialize_plnmg_limit_sys_country = {
	#自定义地面巨构产出增加倍率，初始值为0
	set_variable = { which = num_plnmg_mega_produce_custom_mult value = 0 }
	#国家全局同类地面巨构拥有数量限制
	#set_variable = { which = num_plnmg_mega_same_owned_limit value = 1 }
	#自定义地面巨构建造速度增加倍率，初始值为0
	set_variable = { which = num_plnmg_mega_speed_custom_mult value = 0 }
}

refresh_plnmg_limit_sys_country = {
	initialize_mys_variable = { VAR = num_plnmg_mega_produce_custom_mult }
	initialize_mys_variable = { VAR = num_plnmg_mega_speed_custom_mult }
}

notice_all_other_empires_plnmg_options_change = {
	every_playable_country = {
		limit = {
			is_ai = no
			not = { is_same_value = prev }
		}
		country_event = { id = planetmega_global.21 }
	}
	[[imba_changed]
		set_timed_global_flag = { flag = plnmg_imba_changed_recently days = 2 }
	]
}

# any scope
galaxy_spawn_plnmg_destroyed_mega = {
	every_galaxy_planet = {
		limit = {
			is_colony = no
			is_colonizable = yes
			is_artificial = no
			#is_ideal = no
		}
		random_list = {
			19 = {}
			1 = { spawn_random_plnmg_destroyed_mega = yes }
		}
        reroll_random = yes
	}
}

ingame_galaxy_spawn_pdm = {
	every_galaxy_planet = {
		limit = {
			or = {
				is_colony = yes
				and = {
					is_colony = no
					is_colonizable = yes
				}
			}
		}
		random_list = {
			19 = {}
			1 = { spawn_random_plnmg_destroyed_mega = yes }
		}
        reroll_random = yes
	}
}

galaxy_spawn_pdm_forced = {
	every_galaxy_planet = {
		limit = {
			or = {
				is_colony = yes
				and = {
					is_colony = no
					is_colonizable = yes
				}
			}
			nor = {
				is_artificial = yes
				is_ideal = yes
			}
		}
		spawn_random_plnmg_destroyed_mega = yes
	}
}

# plnmg_global_event_country_maintenance = {
# 	if = {
# 		limit = { not = { exists = event_target:global_event_country } }
# 		if = {
# 			limit = { any_country = { is_country_type = global_event } }
# 			random_country = {
# 				limit = { is_country_type = global_event }
# 				save_global_event_target_as = global_event_country
# 			}
# 		}
# 		else = {
# 			create_country = {
# 				name = global_event_country
# 				type = global_event
# 				effect = { save_global_event_target_as = global_event_country }
# 			}
# 		}
# 	}
# }
check_and_create_plnmg_reserve_country = {
	if = {
		limit = { not = { exists = event_target:planetmega_reserve_country } }
		if = {
			limit = { any_country = { is_country_type = planetmega_reserve } }
			random_country = {
				limit = { is_country_type = planetmega_reserve }
				save_global_event_target_as = planetmega_reserve_country
			}
		}
		else = {
			create_country = {
				name = planetmega_reserve_country
				type = planetmega_reserve
				effect = { save_global_event_target_as = planetmega_reserve_country }
			}
		}
	}
}

shared_planetmega_global_initializer = {
	#plnmg_global_event_country_maintenance = yes
	event_target:global_event_country = {
		#游戏全局地面巨构产出增加倍率，初始值为0
		if = {
			limit = { not = { is_variable_set = num_plnmg_mega_produce_custom_mult } }
			set_variable = { which = num_plnmg_mega_produce_custom_mult value = 0 }
		}
		if = {
			limit = { not = { is_variable_set = num_plnmg_mega_produce_player_mult } }
			set_variable = { which = num_plnmg_mega_produce_player_mult value = 0 }
		}
		if = {
			limit = { not = { is_variable_set = num_plnmg_mega_produce_ai_mult } }
			set_variable = { which = num_plnmg_mega_produce_ai_mult value = 0 }
		}
		#游戏全局地面巨构建造速度增加倍率，初始值为0
		if = {
			limit = { not = { is_variable_set = num_plnmg_mega_speed_custom_mult } }
			set_variable = { which = num_plnmg_mega_speed_custom_mult value = 0 }
		}
		if = {
			limit = { not = { is_variable_set = num_plnmg_mega_speed_player_mult } }
			set_variable = { which = num_plnmg_mega_speed_player_mult value = 0 }
		}
		if = {
			limit = { not = { is_variable_set = num_plnmg_mega_speed_ai_mult } }
			set_variable = { which = num_plnmg_mega_speed_ai_mult value = 0 }
		}
	}
	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
			}
		}
	}
}

refresh_global_plnmg_reincarnation_count = {
	event_target:global_event_country = {
		set_variable = { which = var_plnmg_reincarnation_count value = 0 }
		every_country = {
			limit = {
				is_variable_set = var_plnmg_reincarnation_count
				check_variable = { which = var_plnmg_reincarnation_count value > 0 }
			}
			prev = {
				change_variable = {
					which = var_plnmg_reincarnation_count
					value = prev.var_plnmg_reincarnation_count
				}
			}
		}
	}
}

