
# this = situation

plnmg_sabotage_deficit_initializer = {
	# root.owner must exist
	add_situation_progress = @plnmg_deficit_initializer # a backup scripted var
	set_situation_flag = deficit_situation_affected_by_@root.owner
    #save_global_event_target_as = deficit_situation_affected_by_@root.owner
}

# unused for now
check_plnmg_situation_on_action = {
	if = {
		limit = { situation_is_plnmg_building = yes }
		target = { fire_on_action = { on_action = on_planetmega_built } }
	}
	else_if = {
		limit = { is_situation_type = planet_megastructure_restoration }
		target = { fire_on_action = { on_action = on_planetmega_restored } }
	}
}

# this = mega or planet

send_plnmg_expedition_fleet_out = {
	random_fleet_in_orbit = {
		limit = {
			is_ship_class = shipclass_science_ship
			exists = leader
			owner? = { is_same_value = $owner$ }
		}
		leader = {
			save_event_target_as = extra_galaxy_explorer
			unassign_leader = this
		}
		delete_fleet = this
	}
	random_fleet_in_orbit = {
		limit = {
			is_ship_class = shipclass_constructor
			owner? = { is_same_value = $owner$ }
		}
		delete_fleet = this
	}
	random_fleet_in_orbit = {
		limit = {
			is_ship_class = shipclass_colonizer
			owner? = { is_same_value = $owner$ }
		}
		delete_fleet = this
	}
	check_and_create_plnmg_reserve_country = yes
	event_target:extra_galaxy_explorer = { set_owner = event_target:planetmega_reserve_country }
}
send_plnmg_nomad_exp_fleet_out = {
	random_fleet_in_orbit = {
		limit = {
			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
				# }
			}
		}
		every_owned_ship = {
			limit = { exists = colony }
			destroy_colony = yes
		}
		delete_fleet = this
	}
	random_fleet_in_orbit = {
		limit = {
			owner? = { is_same_value = $owner$ }
			is_ship_size = nomads_constructor
		}
		delete_fleet = this
	}
	random_fleet_in_orbit = {
		limit = {
			is_ship_class = shipclass_science_ship
			exists = leader
			owner? = { is_same_value = $owner$ }
		}
		leader = {
			save_event_target_as = extra_galaxy_explorer
			unassign_leader = this
		}
		delete_fleet = this
	}
	check_and_create_plnmg_reserve_country = yes
	event_target:extra_galaxy_explorer = { set_owner = event_target:planetmega_reserve_country }
}

# this = planet

plnmg_kill_with_mwc = { # mwc means more while count # no longer necessary but kept for compatibility
	# VARIABLE is pop amount in 3.14 which means that of 4.0 version should be divided by 100
	if = {
		limit = { any_owned_pop_group = { pop_group_size >= $VARIABLE$ } }
		random_owned_pop_group = {
			limit = { pop_group_size >= $VARIABLE$ }
			kill_pop_group = { pop_group = this amount = $VARIABLE$ }
		}
	}
	else = {
		while = {
			count = $VARIABLE$
			weighted_random_owned_pop_group = {
				limit = { pop_group_size >= 100 }
				kill_single_pop = yes
			}
		}
	}
}

plnmg_kill_bio_with_mwc = { # mwc means more while count # no longer necessary but kept for compatibility
	# VARIABLE is num_pops in 3.14 which means that pop_amount of 4.0 version should be divided by 100
	if = {
		limit = {
			any_owned_pop_group = {
				pop_group_size >= $VARIABLE$
				is_robotic_species = no
			}
		}
		random_owned_pop_group = {
			limit = {
				pop_group_size >= $VARIABLE$
				is_robotic_species = no
			}
			kill_pop_group = { pop_group = this amount = $VARIABLE$ }
		}
	}
	else = {
		while = {
			count = $VARIABLE$
			weighted_random_owned_pop_group = {
				limit = {
					pop_group_size >= 100
					is_robotic_species = no
				}
				kill_single_pop = yes
			}
		}
	}
}

raid_pop_to_our_planetmega = {
	optimize_memory
	# event_target mentioned should exist!
	# variable should be in event_target:plnmg_body_raided_planet!
	event_target:plnmg_body_raider_country = {
		random_owned_colony = {
			limit = { has_carrier_flag = plnmg_bodyraid_tt }
			save_event_target_as = plnmg_bodyraid_tt_planet
			initialize_mys_variable = { VAR = num_plnmg_unfiltered_bodies }
			change_variable = {
				which = num_plnmg_unfiltered_bodies
				value = event_target:plnmg_body_raided_planet.$variable$
			}
		}
		create_message = {
			type = MESSAGE_TYPE_POP_ABDUCTED
			localization = PLNMG_BODYRAID_MESSAGE
			days = 30
			target = event_target:plnmg_body_raided_planet
		}
		if = {
			limit = { any_owned_colony = { has_deposit = d_hakkou_ichiu_tower } }
			initialize_mys_variable = { VAR = var_plnmg_purged_pops }
			change_variable = {
				which = var_plnmg_purged_pops
				value = event_target:plnmg_body_raided_planet.$variable$
			}
		}
	}
}

randomly_raid_pop_to_our_planetmega = {
	optimize_memory
	# this = raided planet
	# event_target mentioned should exist!
	set_variable = { which = num_plnmg_ready2use_bodies value = 0 }
	if = {
		limit = { pop_amount > 300 }
		random_list = {
			3 = { randomly_raid_pop_step1 = { POP = 100 } }
			2 = { randomly_raid_pop_step1 = { POP = 200 } }
			1 = { randomly_raid_pop_step1 = { POP = 300 } }
		}
	}
	else_if = {
		limit = { pop_amount > 200 }
		random_list = {
			3 = { randomly_raid_pop_step1 = { POP = 100 } }
			2 = { randomly_raid_pop_step1 = { POP = 200 } }
		}
	}
	else_if = {
		limit = { pop_amount > 100 }
		random_list = {
			3 = { randomly_raid_pop_step1 = { POP = 100 } }
			2 = {}
		}
	}
    reroll_random = yes
	raid_pop_to_our_planetmega = { variable = num_plnmg_ready2use_bodies }
	clear_variable = num_plnmg_ready2use_bodies
}

randomly_raid_pop_step1 = {
	optimize_memory
	# this = raided planet
	weighted_random_owned_pop_group = {
		limit = {
			pop_group_size > $POP$
			can_be_plnmg_abducted = yes
			or = {
				is_robotic_species = no
				and = {
					event_target:plnmg_body_raider_country = {
						has_technology = tech_planetmega_junik
					}
					is_robotic_species = yes
				}
			}
		}
		kill_pop_group = { pop_group = this amount = $POP$ }
	}
	event_target:plnmg_body_raider_country = {
		if = {
			limit = { has_ascension_perk = ap_become_the_crisis }
			while = {
				count = @\[ $POP$ * 0.01 ]
				complete_crisis_objective = crisobj_purge_pops
			}
		}
	}
	change_variable = { which = num_plnmg_ready2use_bodies value = $POP$ }
	owner = {
		if = {
			limit = { is_at_war_with = event_target:plnmg_body_raider_country }
			while = {
				count = @\[ $POP$ * 0.01 ]
				add_opinion_modifier = {
					modifier = opinion_plnmg_raided_pop
					who = event_target:plnmg_body_raider_country
				}
			}
			if = {
				limit = { prev = { planet_devastation < 25 } }
				add_static_war_exhaustion = {
					attacker = event_target:plnmg_body_raider_country
					location = prev
					value_for_planet_destruction = 0.85
				}
			}
			else_if = {
				limit = { prev = { planet_devastation < 50 } }
				add_static_war_exhaustion = {
					attacker = event_target:plnmg_body_raider_country
					location = prev
					value_for_planet_destruction = 0.45
				}
			}
			else_if = {
				limit = { prev = { planet_devastation < 75 } }
				add_static_war_exhaustion = {
					attacker = event_target:plnmg_body_raider_country
					location = prev
					value_for_planet_destruction = 0.20
				}
			}
			else = {
				add_static_war_exhaustion = {
					attacker = event_target:plnmg_body_raider_country
					location = prev
					value_for_planet_destruction = 0.10
				}
			}
		}
	}
}

add_plnmg_small_extra_galaxy = {
	custom_tooltip = add_plnmg_small_extra_galaxy_desc
	hidden_effect = {
		initialize_mys_variable = { VAR = plnmg_small_eg_connects }
		change_variable = { which = plnmg_small_eg_connects value = 1 }
		if = {
			limit = { not = { has_modifier = planetmega_extra_galaxy } }
			add_modifier = { modifier = planetmega_extra_galaxy days = -1 }
		}
	}
}
add_plnmg_medium_extra_galaxy = {
	custom_tooltip = add_plnmg_medium_extra_galaxy_desc
	hidden_effect = {
		if = {
			limit = { not = { is_variable_set = plnmg_medium_eg_connects } }
			set_variable = { which = plnmg_medium_eg_connects value = 0 }
		}
		change_variable = { which = plnmg_medium_eg_connects value = 1 }
		if = {
			limit = { not = { has_modifier = planetmega_extra_galaxy } }
			add_modifier = { modifier = planetmega_extra_galaxy days = -1 }
		}
	}
}
add_plnmg_large_extra_galaxy = {
	custom_tooltip = add_plnmg_large_extra_galaxy_desc
	hidden_effect = {
		if = {
			limit = { not = { is_variable_set = plnmg_large_eg_connects } }
			set_variable = { which = plnmg_large_eg_connects value = 0 }
		}
		change_variable = { which = plnmg_large_eg_connects value = 1 }
		if = {
			limit = { not = { has_modifier = planetmega_extra_galaxy } }
			add_modifier = { modifier = planetmega_extra_galaxy days = -1 }
		}
	}
}

refresh_plnmg_white_mice_traits = {
	set_variable = { which = var_plnmg_white_mice_pts_mult value = value:planetmega_white_mice_pts_mult }
	set_variable = { which = var_plnmg_white_mice_pks_mult value = value:planetmega_white_mice_pks_mult }
}

decide_planetmega_building_leader = {
	optimize_memory
	if = {
		limit = { owner = { has_councilor = { COUNCILOR = councilor_planetmega_builder } } }
		owner = {
			random_owned_leader = {
				limit = { is_councilor_type = councilor_planetmega_builder }
				save_event_target_as = planetmega_building_leader
			}
		}
	}
	else_if = {
		limit = { exists = leader }
		leader = { save_event_target_as = planetmega_building_leader }
	}
	else_if = {
		limit = { exists = sector.sector_capital.leader }
		sector.sector_capital.leader = { save_event_target_as = planetmega_building_leader }
	}
	else_if = {
        limit = {
            exists = ruler
            owner = {
                or = {
                    is_autocracy = yes
                    is_authoritarian = yes
                    is_militarist = yes
                    is_homicidal = yes
                }
            }
        }
        ruler = { save_event_target_as = planetmega_building_leader }
    }
    else = { owner_main_species = { save_event_target_as = planetmega_building_leader } }
}

decide_planetmega_military_leader = {
	optimize_memory
	if = {
		limit = {
			owner = {
				any_owned_leader = {
					is_councilor = yes
					leader_class = commander
				}
			}
		}
		owner = {
			random_owned_leader = {
				limit = {
					is_councilor = yes
					leader_class = commander
				}
				save_event_target_as = planetmega_military_leader
			}
		}
	}
	else_if = {
		limit = { exists = ruler }
		ruler = { save_event_target_as = planetmega_military_leader }
	}
	else = { owner_main_species = { save_event_target_as = planetmega_military_leader } }
}

plnmg_lab2_sacrifice_prod_upkp_effect = {
	optimize_memory
    # owner must exist!
    # TYPE can be brain or supercom
    if = {
        limit = { owner = { has_policy_flag = plnmg_sacrifice_curve_J_J } }
        set_variable = {
            which = plnmg_$TYPE$_sacrifice_produces_mult
            value = value:planetmega_sacrifice_jfit_func|TYPE|$TYPE$|
        }
        set_variable = {
            which = plnmg_$TYPE$_sacrifice_upkeep_mult
            value = value:planetmega_sacrifice_jfit_func|TYPE|$TYPE$|
        }
    }
    else = {
        set_variable = { 
			which = plnmg_$TYPE$_sacrifice_upkeep_mult 
			value = value:per_100_planetmega_sacrifice|TYPE|$TYPE$|
		}
        if = {
            limit = { owner = { has_policy_flag = plnmg_sacrifice_curve_J_L } }
            set_variable = {
                which = plnmg_$TYPE$_sacrifice_produces_mult
                value = value:planetmega_sacrifice_jfit_func|TYPE|$TYPE$|
            }
        }
        else_if = {
            limit = { owner = { has_policy_flag = plnmg_sacrifice_curve_S_L } }
            set_variable = {
                which = plnmg_$TYPE$_sacrifice_produces_mult
                value = value:planetmega_sacrifice_sfit_func|TYPE|$TYPE$|
            }
        }
        else_if = {
            limit = { owner = { has_policy_flag = plnmg_sacrifice_curve_L_L } }
            set_variable = {
                which = plnmg_$TYPE$_sacrifice_produces_mult
                value = value:per_100_planetmega_sacrifice|TYPE|$TYPE$|
            }
        }
    }
    # if = {
    #     limit = {
    #         owner = {
    #             or = {
    #                 has_civic = civic_shared_burden
    #                 is_worker_coop_empire = yes
    #             }
    #         }
    #     }
    #     multiply_variable = { which = plnmg_supercom_sacrifice_produces_mult value = 1.05 }
    # }
    # if = {
    #     limit = {
    #         owner = {
    #             or = {
    #                 has_valid_civic = civic_dystopian_society
    #                 is_pleasure_seeker_empire = yes
    #             }
    #         }
    #     }
    #     multiply_variable = { which = plnmg_supercom_sacrifice_produces_mult value = 0.9 }
    # }
}

refund_plnmg_pret1_building = {
    owner = {
        add_resource = {
            minerals = @plnmg_minerals_t0
            alloys = @plnmg_alloys_t0
            energy = @plnmg_energy_t0
            mult = prev.value:scripted_modifier_mult|MODIFIER|planet_buildings_cost_mult|
        }
    }
}

plnmg_wilderness_biomass_spender = {
	# if no parameter "value" then variable "temp_spent_biomass" should be existing in planet scope
	[[value]
		set_variable = { which = temp_spent_biomass value = $value$ }
	]
	if = {
		limit = {
			any_owned_pop_group = {
				has_trait = trait_wilderness
				pop_group_size > temp_spent_biomass
			}
		}
		weighted_random_owned_pop_group = {
			limit = {
				has_trait = trait_wilderness
				pop_group_size > temp_spent_biomass
			}
			kill_pop_group = { pop_group = this amount = temp_spent_biomass }
		}
		clear_variable = temp_spent_biomass
	}
	else_if = {
		limit = {
			owner? = {
				any_owned_pop_group = {
					has_trait = trait_wilderness
					pop_group_size > temp_spent_biomass
				}
			}
		}
		weighted_random_owned_pop_group = {
			limit = { has_trait = trait_wilderness pop_group_size > temp_spent_biomass }
			kill_pop_group = { pop_group = this amount = temp_spent_biomass }
		}
		clear_variable = temp_spent_biomass
	}
	# else_if = {
	# 	limit = { owner? = { pop_amount > temp_spent_biomass } }
	# }
	else = {
		owner = {
			set_variable = { which = temp_spent_biomass value = prev.temp_spent_biomass }
			while = {
				limit = {
					check_variable = { which = temp_spent_biomass value > 0 }
					any_owned_pop_group = { pop_group_size > 100 }
				}
				ordered_owned_pop_group = {
					limit = { pop_group_size > 100 }
					position = 0
					order_by = trigger:pop_group_size
					export_trigger_value_to_variable = { trigger = pop_group_size variable = temp_pop_group_size }
					subtract_variable = { which = temp_pop_group_size value = 100 }
					kill_pop_group = { pop_group = this amount = temp_pop_group_size }
					prev = {
						subtract_variable = { which = temp_spent_biomass value = prev.temp_pop_group_size }
					}
					clear_variable = temp_pop_group_size
				}
			}
			clear_variable = temp_spent_biomass
		}
		clear_variable = temp_spent_biomass
	}
}

plnmg_planet_pop_killing_effect = {
	# if no parameter "value" then variable "temp_pop_amount_killed" should be existing in planet scope
	[[value]
		set_variable = { which = temp_pop_amount_killed value = $value$ }
	]
	if = {
		limit = { any_owned_pop_group = { pop_group_size > temp_pop_amount_killed } }
		weighted_random_owned_pop_group = {
			limit = { pop_group_size > temp_pop_amount_killed }
			kill_pop_group = { pop_group = this amount = temp_pop_amount_killed }
		}
		clear_variable = temp_pop_amount_killed
	}
	else = {
		while = {
			limit = {
				check_variable = { which = temp_pop_amount_killed value > 0 }
				any_owned_pop_group = { pop_group_size > 100 }
			}
			ordered_owned_pop_group = {
				limit = { pop_group_size > 100 }
				position = 0
				order_by = trigger:pop_group_size
				export_trigger_value_to_variable = { trigger = pop_group_size variable = temp_pop_group_size }
				subtract_variable = { which = temp_pop_group_size value = 100 }
				kill_pop_group = { pop_group = this amount = temp_pop_group_size }
				prev = {
					subtract_variable = { which = temp_pop_amount_killed value = prev.temp_pop_group_size }
				}
				clear_variable = temp_pop_group_size
			}
		}
		clear_variable = temp_pop_amount_killed
	}
}

refresh_plnmg_plnt_minerals_prod_var = {
	set_variable = { which = var_plnmg_plnt_minerals_prod value = value:sv_plnmg_plnt_res_prod|RES|minerals| }
}

#####以下SCOPE为SYSTEM#####

check_plnmg_nano_storm_effect_system = {
	optimize_memory
	owner = { save_event_target_as = system_owner }
	every_fleet_in_system = {
		every_controlled_ship = {
			if = {
				limit = { has_modifier = plnmg_nano_storm_effect_ship }
				remove_modifier = plnmg_nano_storm_effect_ship
			}
		}
		if = {
			limit = {
				owner = {
					is_hostile = event_target:system_owner
					if = {
						limit = {
							event_target:system_owner = {
								has_country_flag = nanostorm_similar_cetana_nanites
							}
						}
						is_synth_queen_country_type = no
					}
				}
			}
			owner = { country_event = { id = planetmega_effect.98 } }
			every_controlled_ship = {
				add_modifier = {
					modifier = plnmg_nano_storm_effect_ship
					mult = event_target:system_owner.cntr_nanostorm_powerlevel
				}
			}
		}
	}
}

check_if_planetmega_eg_relay_enabled = {
	# from must be country, since it is specially used in common/megastructure script
	if = {
		limit = {
			nor = {
				is_rim_system = yes #deepseek根据源码：与银心距离前10%的星系
				has_megastructure = planetmega_eg_gateway_final
				has_megastructure = planetmega_eg_gateway_restored
				any_neighbor_system_euclidean = {
					or = {
						has_megastructure = planetmega_eg_gateway_final
						has_megastructure = planetmega_eg_gateway_restored
					}
				}
			}
		}
		set_timed_star_flag = { flag = cant_build_plnmg_eg_relay days = 10 }
		save_event_target_as = location_system
		from = { country_event = { id = planetmega_exp.3 } }
	}
}

#####以下SCOPE为COUNTRY#####

randomly_choose_plnmg_supercom_planet = {
	random_owned_colony = {
		limit = {
			has_deposit = d_plnmg_supercom
			not = { has_carrier_flag = plnmg_absorption_chosen }
		}
		if = {
			limit = { not = { is_variable_set = num_plnmg_supercom_sacrifice } }
			set_variable = {
				which = num_plnmg_supercom_sacrifice
				value = 0
			}
		}
		save_event_target_as = plnmg_chosen_planet_$NUMBER$
		set_carrier_flag = plnmg_absorption_chosen
	}
}

randomly_choose_plnmg_brain_planet = {
	random_owned_colony = {
		limit = {
			has_deposit = d_plnmg_fusion_brain
			not = { has_carrier_flag = plnmg_absorption_chosen }
		}
		if = {
			limit = { not = { is_variable_set = num_plnmg_brain_sacrifice } }
			set_variable = {
				which = num_plnmg_brain_sacrifice
				value = 0
			}
		}
		save_event_target_as = plnmg_chosen_planet_$NUMBER$
		set_carrier_flag = plnmg_absorption_chosen
	}
}

plnmg_purge_with_mwc = { #I do not know where it could be used
	optimize_memory
	set_variable = {
		which = num_plnmg_mwc
		value = $VARIABLE$
	}
	set_variable = {
		which = plnmg_mwc_times
		value = 0
	}
	while = {
		limit = {
			check_variable = {
				which = num_plnmg_mwc
				value > 100
			}
		}
		subtract_variable = {
			which = num_plnmg_mwc
			value = 100
		}
		change_variable = {
			which = plnmg_mwc_times
			value = 1
		}
	}
	if = {
		limit = { has_ascension_perk = ap_become_the_crisis }
		if = {
			limit = {
				check_variable = {
					which = plnmg_mwc_times
					value > 0
				}
			}
			while = {
				count = plnmg_mwc_times
				while = {
					count = 100
					complete_crisis_objective = crisobj_purge_pops
				}
			}
		}
		if = {
			limit = {
				check_variable = {
					which = num_plnmg_mwc
					value > 0
				}
			}
			while = {
				count = num_plnmg_mwc
				complete_crisis_objective = crisobj_purge_pops
			}
		}
	}
	clear_variable = num_plnmg_mwc
	clear_variable = plnmg_mwc_times
}

randomly_choose_plnmg_farm2_planet = {
	random_owned_colony = {
		limit = {
			has_deposit = d_plnmg_advanced_agriculture_edifice
			not = { has_carrier_flag = plnmg_raiding_tt_temp }
		}
		save_event_target_as = plnmg_raiding_planet_$NUMBER$
		set_carrier_flag = plnmg_raiding_tt_temp
	}
}

randomly_choose_plnmg_farm2_hive_planet = {
	random_owned_colony = {
		limit = {
			has_deposit = d_plnmg_gene_farm_hive
			not = { has_carrier_flag = plnmg_raiding_tt_temp }
		}
		save_event_target_as = plnmg_raiding_planet_$NUMBER$
		set_carrier_flag = plnmg_raiding_tt_temp
	}
}

pk111_pre_immediate = {
	if = {
		limit = { is_hive_empire = no }
		randomly_choose_plnmg_farm2_planet = { NUMBER = 1 }
		randomly_choose_plnmg_farm2_planet = { NUMBER = 2 }
		randomly_choose_plnmg_farm2_planet = { NUMBER = 3 }
	}
	else = {
		randomly_choose_plnmg_farm2_hive_planet = { NUMBER = 1 }
		randomly_choose_plnmg_farm2_hive_planet = { NUMBER = 2 }
		randomly_choose_plnmg_farm2_hive_planet = { NUMBER = 3 }
	}
}

initialize_all_plnmg_dyn_prod_vars = {
	initialize_mys_variable = { VAR = num_plnmg_mega_produce_custom_mult }
	#plnmg_global_event_country_maintenance = yes
	event_target:global_event_country = {
		initialize_mys_variable = { VAR = num_plnmg_mega_produce_custom_mult }
		initialize_mys_variable = { VAR = num_plnmg_mega_produce_player_mult }
		initialize_mys_variable = { VAR = num_plnmg_mega_produce_ai_mult }
	}
}

initialize_all_plnmg_dyn_upkp_vars = {
	initialize_mys_variable = { VAR = num_plnmg_mega_upkeep_custom_mult }
	#plnmg_global_event_country_maintenance = yes
	event_target:global_event_country = {
		initialize_mys_variable = { VAR = num_plnmg_mega_upkeep_custom_mult }
		initialize_mys_variable = { VAR = num_plnmg_mega_upkeep_player_mult }
		initialize_mys_variable = { VAR = num_plnmg_mega_upkeep_ai_mult }
	}
}

plnmg_dynamic_produces_var_change_effect = {
	# event_target:global_event_country must exist! (generally created on game start)
	# this = player's country!
	if = {
		limit = { has_country_flag = planetmega_ultimate_adjusting }
		change_variable = { which = num_plnmg_mega_produce_custom_mult value = $value$ }
	}
	event_target:global_event_country = {
		# change
		if = {
			limit = { prev = { has_country_flag = plnmg_dynamic_produces_all } }
			change_variable = { which = num_plnmg_mega_produce_custom_mult value = $value$ }
		}
		if = {
			limit = { prev = { has_country_flag = plnmg_dynamic_produces_player } }
			change_variable = { which = num_plnmg_mega_produce_player_mult value = $value$ }
		}
		if = {
			limit = { prev = { has_country_flag = plnmg_dynamic_produces_ai } }
			change_variable = { which = num_plnmg_mega_produce_ai_mult value = $value$ }
		}
	}
}

plnmg_dynamic_upkeep_var_change_effect = {
	# event_target:global_event_country must exist! (generally created on game start)
	# this = player's country!
	if = {
		limit = { has_country_flag = planetmega_ultimate_adjusting }
		change_variable = { which = num_plnmg_mega_upkeep_custom_mult value = $value$ }
	}
	event_target:global_event_country = {
		# change
		if = {
			limit = { prev = { has_country_flag = plnmg_dynamic_upkeep_all } }
			change_variable = { which = num_plnmg_mega_upkeep_custom_mult value = $value$ }
		}
		if = {
			limit = { prev = { has_country_flag = plnmg_dynamic_upkeep_player } }
			change_variable = { which = num_plnmg_mega_upkeep_player_mult value = $value$ }
		}
		if = {
			limit = { prev = { has_country_flag = plnmg_dynamic_upkeep_ai } }
			change_variable = { which = num_plnmg_mega_upkeep_ai_mult value = $value$ }
		}
	}
}

plnmg_dynamic_produces_var_subtract_effect = {
	# event_target:global_event_country must exist! (generally created on game start)
	# this = player's country!
	if = {
		limit = { has_country_flag = planetmega_ultimate_adjusting }
		subtract_variable = { which = num_plnmg_mega_produce_custom_mult value = $value$ }
	}
	event_target:global_event_country = {
		if = {
			limit = { prev = { has_country_flag = plnmg_dynamic_produces_all } }
			subtract_variable = { which = num_plnmg_mega_produce_custom_mult value = $value$ }
		}
		if = {
			limit = { prev = { has_country_flag = plnmg_dynamic_produces_player } }
			subtract_variable = { which = num_plnmg_mega_produce_player_mult value = $value$ }
		}
		if = {
			limit = { prev = { has_country_flag = plnmg_dynamic_produces_ai } }
			subtract_variable = { which = num_plnmg_mega_produce_ai_mult value = $value$ }
		}
	}
}

plnmg_dynamic_upkeep_var_subtract_effect = {
	# event_target:global_event_country must exist! (generally created on game start)
	# this = player's country!
	if = {
		limit = { has_country_flag = planetmega_ultimate_adjusting }
		subtract_variable = { which = num_plnmg_mega_upkeep_custom_mult value = $value$ }
	}
	event_target:global_event_country = {
		if = {
			limit = { prev = { has_country_flag = plnmg_dynamic_upkeep_all } }
			subtract_variable = { which = num_plnmg_mega_upkeep_custom_mult value = $value$ }
		}
		if = {
			limit = { prev = { has_country_flag = plnmg_dynamic_upkeep_player } }
			subtract_variable = { which = num_plnmg_mega_upkeep_player_mult value = $value$ }
		}
		if = {
			limit = { prev = { has_country_flag = plnmg_dynamic_upkeep_ai } }
			subtract_variable = { which = num_plnmg_mega_upkeep_ai_mult value = $value$ }
		}
	}
}

plnmg_dynamic_produces_var_guiling_effect = {
	# event_target:global_event_country must exist! (generally created on game start)
	# this = player's country!
	if = {
		limit = { has_country_flag = planetmega_ultimate_adjusting }
		set_variable = { which = num_plnmg_mega_produce_custom_mult value = 0 }
	}
	event_target:global_event_country = {
		if = {
			limit = { prev = { has_country_flag = plnmg_dynamic_produces_all } }
			set_variable = { which = num_plnmg_mega_produce_custom_mult value = 0 }
		}
		if = {
			limit = { prev = { has_country_flag = plnmg_dynamic_produces_player } }
			set_variable = { which = num_plnmg_mega_produce_player_mult value = 0 }
		}
		if = {
			limit = { prev = { has_country_flag = plnmg_dynamic_produces_ai } }
			set_variable = { which = num_plnmg_mega_produce_ai_mult value = 0 }
		}
	}
}

plnmg_dynamic_upkeep_var_guiling_effect = {
	# event_target:global_event_country must exist! (generally created on game start)
	# this = player's country!
	if = {
		limit = { has_country_flag = planetmega_ultimate_adjusting }
		set_variable = { which = num_plnmg_mega_upkeep_custom_mult value = 0 }
	}
	event_target:global_event_country = {
		if = {
			limit = { prev = { has_country_flag = plnmg_dynamic_upkeep_all } }
			set_variable = { which = num_plnmg_mega_upkeep_custom_mult value = 0 }
		}
		if = {
			limit = { prev = { has_country_flag = plnmg_dynamic_upkeep_player } }
			set_variable = { which = num_plnmg_mega_upkeep_player_mult value = 0 }
		}
		if = {
			limit = { prev = { has_country_flag = plnmg_dynamic_upkeep_ai } }
			set_variable = { which = num_plnmg_mega_upkeep_ai_mult value = 0 }
		}
	}
}

plnmg_dynamic_produces_mult_implement = {
	optimize_memory
	every_playable_country = {
		if = {
			limit = { has_modifier = planetmega_custom_produces_mult }
			remove_modifier = planetmega_custom_produces_mult
		}
		if = {
			limit = {
				exists = event_target:global_event_country
				or = {
					and = {
						is_variable_set = num_plnmg_mega_produce_custom_mult
						check_variable = { which = num_plnmg_mega_produce_custom_mult value = 0 }
					}
					event_target:global_event_country = {
						or = {
							and = {
								is_variable_set = num_plnmg_mega_produce_custom_mult
								check_variable = { which = num_plnmg_mega_produce_custom_mult value != 0 }
							}
							and = {
								is_variable_set = num_plnmg_mega_produce_player_mult
								check_variable = { which = num_plnmg_mega_produce_player_mult value != 0 }
							}
							and = {
								is_variable_set = num_plnmg_mega_produce_ai_mult
								check_variable = { which = num_plnmg_mega_produce_ai_mult value != 0 }
							}
						}
					}
				}
			}
			add_modifier = {
				modifier = planetmega_custom_produces_mult
				mult = value:planetmega_custom_produce_mult_net
			}
		}
	}
	if = {
		limit = { is_scope_type = country }
		clear_all_plnmg_dyn_prod_flags = yes
	}
}

clear_all_plnmg_dyn_prod_flags = {
	if = {
		limit = { has_country_flag = plnmg_dynamic_produces_all }
		remove_country_flag = plnmg_dynamic_produces_all
	}
	if = {
		limit = { has_country_flag = plnmg_dynamic_produces_player }
		remove_country_flag = plnmg_dynamic_produces_player
	}
	if = {
		limit = { has_country_flag = plnmg_dynamic_produces_ai }
		remove_country_flag = plnmg_dynamic_produces_ai
	}
	if = {
		limit = { has_country_flag = planetmega_ultimate_adjusting }
		remove_country_flag = planetmega_ultimate_adjusting
	}
}

plnmg_dynamic_upkeep_mult_implement = {
	optimize_memory
	every_playable_country = {
		if = {
			limit = { has_modifier = planetmega_custom_upkeep_mult }
			remove_modifier = planetmega_custom_upkeep_mult
		}
		if = {
			limit = {
				exists = event_target:global_event_country
				or = {
					and = {
						is_variable_set = num_plnmg_mega_upkeep_custom_mult
						check_variable = { which = num_plnmg_mega_upkeep_custom_mult value != 0 }
					}
					event_target:global_event_country = {
						or = {
							and = {
								is_variable_set = num_plnmg_mega_upkeep_custom_mult
								check_variable = { which = num_plnmg_mega_upkeep_custom_mult value != 0 }
							}
							and = {
								is_variable_set = num_plnmg_mega_upkeep_player_mult
								check_variable = { which = num_plnmg_mega_upkeep_player_mult value != 0 }
							}
							and = {
								is_variable_set = num_plnmg_mega_upkeep_ai_mult
								check_variable = { which = num_plnmg_mega_upkeep_ai_mult value != 0 }
							}
						}
					}
				}
			}
			add_modifier = {
				modifier = planetmega_custom_upkeep_mult
				mult = value:planetmega_custom_upkeep_mult_net
			}
		}
	}
	if = {
		limit = { is_scope_type = country }
		clear_all_plnmg_dyn_upkp_flags = yes
	}
}

clear_all_plnmg_dyn_upkp_flags = {
	if = {
		limit = { has_country_flag = plnmg_dynamic_upkeep_all }
		remove_country_flag = plnmg_dynamic_upkeep_all
	}
	if = {
		limit = { has_country_flag = plnmg_dynamic_upkeep_player }
		remove_country_flag = plnmg_dynamic_upkeep_player
	}
	if = {
		limit = { has_country_flag = plnmg_dynamic_upkeep_ai }
		remove_country_flag = plnmg_dynamic_upkeep_ai
	}
	if = {
		limit = { has_country_flag = planetmega_ultimate_adjusting }
		remove_country_flag = planetmega_ultimate_adjusting
	}
}

randomly_decide_nanostorm_similarity = {
	random_list = {
		9 = {}
		1 = { set_country_flag = nanostorm_similar_cetana_nanites }
	}
}

start_plnmg_deficit_situation = {
	# this = target country
	# root.owner must exist
	start_situation = {
		target = this
		type = situation_$RES$_deficit
		effect = {
			plnmg_sabotage_deficit_initializer = yes
			set_situation_flag = deficit_situation_added_by_@root.owner
		}
	}
}

plnmg_brainwash_neighbor_systems = {
	optimize_memory
    #root.owner must be brainwasher country!!!
    #this must be victim country!!!
    custom_tooltip_with_params = {
        description = plnmg_brainwash_neighbor_systems_tt
        description_parameters = { DIST = $DIST$ }
    }
    hidden_effect = {
        save_event_target_as = victim_country
        every_system_within_border = {
            limit = { distance_to_empire = { who = root.owner distance <= $DIST$ use_bypasses = yes type = hyperlane } }
            every_system_planet_colony = {
                limit = {
                    is_owned_by = event_target:victim_country
                    not = { has_carrier_flag = plnmg_brainwashed_by_@root.owner }
                }
                set_timed_carrier_flag = { flag = plnmg_brainwashed_by_@root.owner years = 5 }
                event_target:victim_country = {
                    if = {
                        limit = { prev = { is_capital = yes } }
                        add_opinion_modifier = { who = root.owner modifier = opinion_plnmg_tms_cap }
                        if = {
                            limit = { is_gestalt = no }
                            add_opinion_modifier = {
                                who = root.owner
                                modifier = opinion_plnmg_tms_cap
                            }
                        }
                    }
                    else_if = {
                        limit = { prev = { is_sector_capital = yes } }
                        add_opinion_modifier = { who = root.owner modifier = opinion_plnmg_tms_sec_cap }
                        if = {
                            limit = { is_gestalt = no }
                            add_opinion_modifier = {
                                who = root.owner
                                modifier = opinion_plnmg_tms_sec_cap
                            }
                        }
                    }
                    else = {
                        add_opinion_modifier = { who = root.owner modifier = opinion_plnmg_tms_reg_plnt }
                        if = {
                            limit = { is_gestalt = no }
                            add_opinion_modifier = {
                                who = root.owner
                                modifier = opinion_plnmg_tms_reg_plnt
                            }
                        }
                    }
                }
                if = {
                    limit = { event_target:victim_country = { is_gestalt = yes } }
                    #按人口加反常度
                    add_modifier = {
                        modifier = plnmg_tms_brainwashed_gestalt
                        mult = value:per_100_pop_amount
                        years = 3
                    }
                }
                else = {
                    #加思潮吸引
                    add_modifier = { modifier = plnmg_tms_brainwashed_regular years = 4 }
                }
            }
			every_system_ship_colony = {
				limit = {
					is_owned_by = event_target:victim_country
					not = { has_carrier_flag = plnmg_brainwashed_by_@root.owner }
				}
				set_timed_carrier_flag = { flag = plnmg_brainwashed_by_@root.owner years = 5 }
				event_target:victim_country = {
					if = {
						limit = { prev = { is_capital = yes } }
						add_opinion_modifier = { who = root.owner modifier = opinion_plnmg_tms_cap }
						if = {
							limit = { is_gestalt = no }
							add_opinion_modifier = {
								who = root.owner
								modifier = opinion_plnmg_tms_cap
							}
						}
					}
					else_if = {
						limit = { prev = { is_sector_capital = yes } }
						add_opinion_modifier = { who = root.owner modifier = opinion_plnmg_tms_sec_cap }
						if = {
							limit = { is_gestalt = no }
							add_opinion_modifier = {
								who = root.owner
								modifier = opinion_plnmg_tms_sec_cap
							}
						}
					}
					else = {
						add_opinion_modifier = { who = root.owner modifier = opinion_plnmg_tms_reg_plnt }
						if = {
							limit = { is_gestalt = no }
							add_opinion_modifier = {
								who = root.owner
								modifier = opinion_plnmg_tms_reg_plnt
							}
						}
					}
				}
				if = {
					limit = { event_target:victim_country = { is_gestalt = yes } }
					#按人口加反常度
					add_modifier = {
						modifier = plnmg_tms_brainwashed_gestalt
						mult = value:per_100_pop_amount
						years = 3
					}
				}
				else = {
					#加思潮吸引
					add_modifier = { modifier = plnmg_tms_brainwashed_regular years = 4 }
				}
			}
        }
    }
}

calculate_cntr_nanostorm_powerlevel = {
	set_variable = { which = cntr_nanostorm_powerlevel value = 0 }
	every_owned_colony = {
		limit = { is_variable_set = plnt_nanostorm_powerlevel }
		prev = {
			change_variable = {
				which = cntr_nanostorm_powerlevel
				value = prev.plnt_nanostorm_powerlevel
			}
		}
	}
}

#以下不限SCOPE，除了GLOBAL
plnmg_round_down_with_checking = { #下舍验算
	set_variable = {
		which = num_plnmg_rdc_temp1
		value = $VARIABLE$
	}
	set_variable = {
		which = num_plnmg_rdc_temp2
		value = $VARIABLE$
	}
	floor_variable = num_plnmg_rdc_temp2
	subtract_variable = {
		which = num_plnmg_rdc_temp1
		value = num_plnmg_rdc_temp2
	}
	if = {
		limit = {
			check_variable = {
				which = num_plnmg_rdc_temp1
				value < 1
			}
		}
		floor_variable = $VARIABLE$
	}
	clear_variable = num_plnmg_rdc_temp1
	clear_variable = num_plnmg_rdc_temp2
}

###specially for button effects

plnmg_situation_speed_plus_subtract_effect = {
	if = {
		limit = { exists = event_target:plnmg_speed_adjusting_by_@from }
		event_target:plnmg_speed_adjusting_by_@from = {
			initialize_mys_variable = { VAR = var_plnmg_sit_speed_plus_mult }
			subtract_variable = { which = var_plnmg_sit_speed_plus_mult value = $value$ }
		}
	}
	else = {
		from = {
			initialize_mys_variable = { VAR = var_plnmg_sit_speed_plus_mult }
			subtract_variable = { which = var_plnmg_sit_speed_plus_mult value = $value$ }
		}
	}
}
plnmg_situation_speed_plus_guiling_effect = {
	if = {
		limit = { exists = event_target:plnmg_speed_adjusting_by_@from }
		event_target:plnmg_speed_adjusting_by_@from = {
			#set_variable = { which = var_plnmg_sit_speed_plus_mult value = 0 }
			clear_variable = var_plnmg_sit_speed_plus_mult
		}
	}
	else = {
		from = {
			#set_variable = { which = var_plnmg_sit_speed_plus_mult value = 0 }
			clear_variable = var_plnmg_sit_speed_plus_mult
		}
	}
}
plnmg_situation_speed_plus_change_effect = {
	if = {
		limit = { exists = event_target:plnmg_speed_adjusting_by_@from }
		event_target:plnmg_speed_adjusting_by_@from = {
			initialize_mys_variable = { VAR = var_plnmg_sit_speed_plus_mult }
			change_variable = { which = var_plnmg_sit_speed_plus_mult value = $value$ }
		}
	}
	else = {
		from = {
			initialize_mys_variable = { VAR = var_plnmg_sit_speed_plus_mult }
			change_variable = { which = var_plnmg_sit_speed_plus_mult value = $value$ }
		}
	}
}
