# Planet scope
auto_construct_mega_setup = {
	$FLEET$ = {
		set_event_locked = yes
		set_fleet_flag = auto_construct_blocked_fleet_flag
		set_variable = {
			which = lazy_automation_month_timer
			value = owner.value:days_to_months_speed_converter|DAYS|$BUILD_TIME$|MODIFIER|megastructure_build_speed_mult|
		}
		prev = {
			set_timed_carrier_flag = {
				flag = auto_construct_planet_flag
				days = prev.owner.value:speed_modifier_mult|DAYS|$BUILD_TIME$|MODIFIER|megastructure_build_speed_mult|
			}
			carrier_event = {
				id = $EVENT$
				days = 10
			}
		}
	}
}

auto_construct_mega_cleanup = {
	$FLEET$ = {
		set_event_locked = no
		remove_fleet_flag = auto_construct_blocked_fleet_flag
	}
	$SYSTEM$ = {
		every_system_megastructure = {
			limit = {
				OR = {
					is_megastructure_type = constructing_og_megastructure
					is_megastructure_type = constructing_megastructure
				}
			}
			remove_megastructure = this
		}
	}
	[[PLANET]
		$PLANET$ = {
			remove_planet = yes
		}
	]
}

reset_and_set_mega_fleet_flag = {
	remove_fleet_flag = auto_construct_dyson_swarm_flag
	remove_fleet_flag = auto_construct_orbital_ring_flag
	remove_fleet_flag = auto_construct_habitat_central_flag
	# remove_fleet_flag = auto_construct_major_orbital_flag
	# remove_fleet_flag = auto_construct_minor_orbital_flag
	remove_fleet_flag = auto_construct_hyper_relays_flag
	remove_fleet_flag = auto_construct_gateway_flag
	remove_fleet_flag = auto_construct_frameworld_outpost_flag
	remove_fleet_flag = auto_construct_frameworld_harvest_flag
	remove_fleet_flag = auto_construct_frameworld_salvage_habitat_flag
	remove_fleet_flag = auto_construct_arc_furnace_flag
	remove_fleet_flag = auto_construct_deep_space_citadel_flag
	remove_fleet_flag = auto_construct_planetary_computer_flag
	remove_fleet_flag = auto_construct_terraform_site_flag
	remove_fleet_flag = auto_construct_asteroid_manufactory_flag

	remove_fleet_flag = auto_construct_sra_orbital_ring_flag
	remove_fleet_flag = auto_construct_sra_barrier_guard_station_flag
	remove_fleet_flag = auto_construct_sra_gz_platform_flag

	set_fleet_flag = auto_mega_ship_flag
	set_fleet_flag = $FLAG$
}

auto_construct_mega_fleet_deactivate = {
	clear_fleet_actions = this
	clear_orders = yes
	set_event_locked = no
	set_name = random

	reset_and_set_mega_fleet_flag = {
		FLAG = placeholder_no_flag
	}
	remove_fleet_flag = auto_mega_ship_flag
	remove_fleet_flag = auto_construct_blocked_fleet_flag

	set_variable = {
		which = lazy_automation_month_timer
		value = 0
	}

	solar_system = {
		every_system_megastructure = {
			limit = {
				OR = {
					is_megastructure_type = constructing_og_megastructure
					is_megastructure_type = constructing_megastructure
				}
			}
			remove_megastructure = this
		}
		every_system_planet = {
			limit = {
				has_carrier_flag = auto_construct_carrier_pos_flag
			}
			remove_planet = yes
		}
		every_system_planet = {
			remove_carrier_flag = auto_construct_carrier_flag
		}
	}
}