# orbital Arc Furnace First Stage
orbital_arc_furnace_1 = {
	entity = "arc_crucible_stage_1_entity"
	construction_entity = "arc_crucible_stage_1_entity"
	construction_scale = 1.02 #to avoid z-fighting of consrucion entity with the base entity
	portrait = "GFX_megastructure_arc_furnace_background"
	place_entity_on_planet_plane = yes
	entity_offset = { x = 0 y = 0 }
	rotate_to_center = no
	scale_offset = yes
	#show_in_outliner = no
	use_planet_resource = yes
	scales_with_planet = yes
	build_time = 360 # 1 year
	resources = {
		category = megastructures
		cost = {
			unity = 500
		}
		cost = {
			trigger = {
				country_uses_bio_ships = no
			}
			alloys = 500
		}
		cost = {
			trigger = {
				country_uses_bio_ships = yes
			}
			alloys = 500
			mult = 0.5
		}
		cost = {
			trigger = {
				country_uses_bio_ships = yes
			}
			food = 500
			mult = @halved_alloy_to_food_cost_ratio
		}

		upkeep = {
			energy = 20
		}
	}

	construction_blocks_and_blocked_by = self_type
	build_system_tooltip = arc_furnace_tooltip
	tooltip_system_score = {
		base = 0
		complex_trigger_modifier = {
			trigger = count_system_planet
			parameters = {
				limit = {
					NOT = { has_deposit_for = shipclass_research_station }
					is_star = no
					is_astral_scar = no
					colonizable_planet = no
				}
			}
			mode = add
		}
	}
	tooltip_system_score_low_threshold = 10
	tooltip_system_score_high_threshold = 16
	tooltip_best_systems_header = "MEGASTRUCTURE_ARC_FURNACE_BEST_SYSTEMS_TOOLTIP_HEADER"
	tooltip_system_filter = {
		system_has_arc_furnace = no
		any_system_planet = {
			is_planet_class = pc_molten
		}
	}

	dismantle_cost = {
		category = megastructures
		cost = {
			energy = 500
		}
	}

	dismantle_time = 360

	dismantle_potential = {
		always = yes
	}

	dismantle_possible = {
		can_dismantle_megastructure = {
			TECH = tech_orbital_arc_furnace
		}
	}

	on_dismantle_complete = {
		every_system_planet = {
			dismantle_arc_furnace_effect = yes
		}
		random_system_planet = {
			limit = {
				has_planet_flag = has_arc_furnace
			}
			set_planet_entity = {
				entity = pc_molten
			}
			remove_planet_flag = has_megastructure
			remove_planet_flag = has_arc_furnace
			move_consecration_aura_to_body_effect = yes
		}
		from = {
			add_resource = {
				alloys = 500
				mult = modifier:megastructure_dismantle_refund_mult
			}
			if = {
				limit = {
					check_variable = {
						which = arc_furnace_counter
						value >= 1
					}
				}
				change_variable = {
					which = arc_furnace_counter
					value = -1
				}
			}
		}
	}

	prerequisites = { "tech_orbital_arc_furnace" }

	potential = {
		always = yes
	}

	possible = {
		inline_script = megastructures/system_ownership_or_waystation_check
		inline_script = megastructures/system_surveyed_or_waystation_check
		custom_tooltip = {
			fail_text = "requires_no_dyson_sphere"
			system_has_dyson_sphere = no
		}
		custom_tooltip = {
			fail_text = "requires_no_dyson_sphere_construction"
			if = {
				limit = {
					system_has_dyson_sphere = no
				}
				NOT = {
					solar_system = {
						has_star_flag = dyson_sphere_construction
					}
				}
			}
		}
		custom_tooltip = {
			fail_text = "requires_no_arc_furnace"
			system_has_arc_furnace = no
		}
		custom_tooltip = {
			fail_text = "requires_less_than_x_arc_furnaces"
			from = {
				check_variable = {
					which = arc_furnace_counter
					value <= value:arc_furnace_limit
				}
			}
		}
	}

	placement_rules = {
		planet_possible = {
			custom_tooltip = {
				fail_text = "requires_no_anomaly"
				NOT = {
					has_anomaly = yes
				}
			}
			is_planet_class = pc_molten
			custom_tooltip = {
				fail_text = "requires_no_existing_megastructure"
				NOR = {
					has_planet_flag = megastructure
					has_planet_flag = has_megastructure
				}
			}
			if = {
				limit = {
					from = {
						is_ai = yes
					}
				}
				solar_system = {
					count_system_planet = {
						count >= 8
						limit = {
							NOT = {
								has_deposit_for = shipclass_research_station
							}
							is_star = no
							is_astral_scar = no
							colonizable_planet = no
						}
					}
				}
			}
		}
	}

	custom_tooltip_with_modifiers = orbital_arc_furnace_1_mod_tooltip

	# root = system
	# from = country
	ai_weight = {
		factor = 5

		modifier = {
			factor = 0.1
			from = {
				is_nomadic = no
			}
			starbase = {
				NOT = {
					has_starbase_size >= starbase_starfortress
				}
			}
		}

		modifier = {
			factor = 0
			from = {
				is_nomadic = yes
			}
			NOT = {
				any_fleet_in_system = {
					OR = {
						is_ship_size = starbase_waystation_2
						is_ship_size = starbase_waystation_3
					}
					owner? = {
						is_same_value = from
					}
				}
			}
		}

		modifier = {
			factor = 0.1
			from = {
				is_nomadic = yes
			}
			owner? = {
				NOT = {
					benefits_from_waystation_network = {
						NOMAD = from
						SYSTEM_OWNER = this
					}
				}
			}
		}
		modifier = {
			factor = 0
			from = {
				check_variable = {
					which = arc_furnace_counter
					value >= value:arc_furnace_limit
				}
			}
		}
	}

	on_build_start = {
		set_star_flag = arc_furnace_construction
		if = {
			limit = {
				exists = from
			}
			from = {
				change_variable = {
					which = arc_furnace_counter
					value = 1
				}
			}
		}
	}
	on_build_cancel = {
		remove_star_flag = arc_furnace_construction
		if = {
			limit = {
				exists = from
			}
			from = {
				if = {
					limit = {
						check_variable = {
							which = arc_furnace_counter
							value >= 1
						}
					}
					change_variable = {
						which = arc_furnace_counter
						value = -1
					}
				}
			}
		}
	}
	on_build_complete = {
		remove_star_flag = arc_furnace_construction
		fromfrom.planet = {
			set_planet_flag = has_megastructure
			set_planet_flag = has_arc_furnace
			if = {
				limit = {
					has_orbital_station = yes
				}
				orbital_station = {
					dismantle = yes
				}
			}
			set_planet_entity = {
				entity = invisible_turret_entity
			}
			move_consecration_aura_to_furnace_effect = yes
		}
		from = {
			country_event = {
				id = cybernetics.1000
			}
			country_event = {
				id = machine_age.3405 #2nd Arc Furnace built
			}
		}
	}
}

# orbital Arc Furnace Second Stage
orbital_arc_furnace_2 = {
	entity = "arc_crucible_stage_2_entity"
	construction_entity = "arc_crucible_stage_2_entity"
	construction_scale = 1.02 #to avoid z-fighting of consrucion entity with the base entity
	portrait = "GFX_megastructure_arc_furnace_background"
	entity_offset = { x = 0 y = 0 }
	rotate_to_center = no
	scale_offset = yes
	#show_in_outliner = no
	use_planet_resource = yes
	scales_with_planet = yes
	build_time = 1080 # 3 years
	resources = {
		category = megastructures
		cost = {
			unity = 1000
		}
		cost = {
			trigger = {
				country_uses_bio_ships = no
			}
			alloys = 1000
		}
		cost = {
			trigger = {
				country_uses_bio_ships = yes
			}
			alloys = 1000
			mult = 0.5
		}
		cost = {
			trigger = {
				country_uses_bio_ships = yes
			}
			food = 1000
			mult = @halved_alloy_to_food_cost_ratio
		}

		upkeep = {
			energy = 40
		}
	}

	construction_blocks_and_blocked_by = none

	dismantle_cost = {
		category = megastructures
		cost = {
			energy = 1000
		}
	}

	dismantle_time = 360

	dismantle_potential = {
		always = yes
	}

	dismantle_possible = {
		can_dismantle_megastructure = {
			TECH = tech_orbital_arc_furnace
		}
	}

	on_dismantle_complete = {
		every_system_planet = {
			dismantle_arc_furnace_effect = yes
		}
		random_system_planet = {
			limit = {
				has_planet_flag = has_arc_furnace
			}
			set_planet_entity = {
				entity = pc_molten
			}
			remove_planet_flag = has_megastructure
			remove_planet_flag = has_arc_furnace
			move_consecration_aura_to_body_effect = yes
		}
		from = {
			add_resource = {
				alloys = 1000
				mult = modifier:megastructure_dismantle_refund_mult
			}
			if = {
				limit = {
					check_variable = {
						which = arc_furnace_counter
						value >= 1
					}
				}
				change_variable = {
					which = arc_furnace_counter
					value = -1
				}
			}
		}
	}

	upgrade_from = {
		orbital_arc_furnace_1
	}

	prerequisites = { "tech_orbital_arc_furnace" }

	potential = {
		always = yes
	}


	custom_tooltip_with_modifiers = orbital_arc_furnace_2_mod_tooltip

	on_build_start = {
		set_star_flag = arc_furnace_construction
	}
	on_build_cancel = {
		remove_star_flag = arc_furnace_construction
	}
	on_build_complete = {
		remove_star_flag = arc_furnace_construction
		from = {
			country_event = {
				id = cybernetics.1005
			}
		}
	}
}

# Orbital arc furnace Third Stage
orbital_arc_furnace_3 = {
	entity = "arc_crucible_stage_3_entity"
	construction_entity = "arc_crucible_stage_3_entity"
	construction_scale = 1.02 #to avoid z-fighting of consrucion entity with the base entity
	portrait = "GFX_megastructure_arc_furnace_background"
	entity_offset = { x = 0 y = 0 }
	rotate_to_center = no
	scale_offset = yes
	place_entity_on_planet_plane = yes
	#show_in_outliner = no
	use_planet_resource = yes
	scales_with_planet = yes
	build_time = 1080 # 3 years
	resources = {
		category = megastructures
		cost = {
			unity = 2000
		}
		cost = {
			trigger = {
				country_uses_bio_ships = no
			}
			alloys = 1500
		}
		cost = {
			trigger = {
				country_uses_bio_ships = yes
			}
			alloys = 1500
			mult = 0.5
		}
		cost = {
			trigger = {
				country_uses_bio_ships = yes
			}
			food = 1500
			mult = @halved_alloy_to_food_cost_ratio
		}

		upkeep = {
			energy = @arc_furnace_3_upkeep_energy
		}
	}

	overclock_loc_key = SMELTING
	overclock_cooldown = @arc_furnace_overclock_cooldown
	overclock_types = {
		overclock_arc_furnace_3_minerals
		overclock_arc_furnace_3_balanced
		overclock_arc_furnace_3_alloys
		overclock_arc_furnace_3_obsessional_directive
	}

	construction_blocks_and_blocked_by = none

	dismantle_cost = {
		category = megastructures
		cost = {
			energy = 1500
		}
	}

	dismantle_time = 360

	dismantle_potential = {
		always = yes
	}

	dismantle_possible = {
		can_dismantle_megastructure = {
			TECH = tech_orbital_arc_furnace
		}
	}

	on_dismantle_complete = {
		every_system_planet = {
			dismantle_arc_furnace_effect = yes
		}
		random_system_planet = {
			limit = {
				has_planet_flag = has_arc_furnace
			}
			set_planet_entity = {
				entity = pc_molten
			}
			remove_planet_flag = has_megastructure
			remove_planet_flag = has_arc_furnace
			move_consecration_aura_to_body_effect = yes
		}
		from = {
			add_resource = {
				alloys = 1500
				mult = modifier:megastructure_dismantle_refund_mult
			}
			if = {
				limit = {
					check_variable = {
						which = arc_furnace_counter
						value >= 1
					}
				}
				change_variable = {
					which = arc_furnace_counter
					value = -1
				}
			}
		}
	}

	upgrade_from = {
		orbital_arc_furnace_2
	}

	prerequisites = { "tech_orbital_arc_furnace" }

	potential = {
		always = yes
	}


	custom_tooltip_with_modifiers = orbital_arc_furnace_3_mod_tooltip

	on_build_start = {
		set_star_flag = arc_furnace_construction
	}
	on_build_cancel = {
		remove_star_flag = arc_furnace_construction
	}
	on_build_complete = {
		remove_star_flag = arc_furnace_construction
		from = {
			country_event = {
				id = cybernetics.1010
			}
		}
	}
}

# Orbital arc furnace Final Stage
orbital_arc_furnace_4 = {
	entity = "arc_crucible_stage_4_entity"
	construction_entity = "arc_crucible_stage_4_entity"
	construction_scale = 1.02 #to avoid z-fighting of consrucion entity with the base entity
	portrait = "GFX_megastructure_arc_furnace_background"
	entity_offset = { x = 0 y = 0 }
	rotate_to_center = no
	scale_offset = yes
	place_entity_on_planet_plane = yes
	show_in_outliner = no
	use_planet_resource = yes
	scales_with_planet = yes
	build_time = 1080 # 3 years
	resources = {
		category = megastructures
		cost = {
			unity = 4000
		}
		cost = {
			trigger = {
				country_uses_bio_ships = no
			}
			alloys = 2000
		}
		cost = {
			trigger = {
				country_uses_bio_ships = yes
			}
			alloys = 2000
			mult = 0.5
		}
		cost = {
			trigger = {
				country_uses_bio_ships = yes
			}
			food = 2000
			mult = @halved_alloy_to_food_cost_ratio
		}

		upkeep = {
			energy = @arc_furnace_4_upkeep_energy
		}
	}

	overclock_loc_key = SMELTING
	overclock_cooldown = @arc_furnace_overclock_cooldown
	overclock_types = {
		overclock_arc_furnace_4_minerals
		overclock_arc_furnace_4_balanced
		overclock_arc_furnace_4_alloys
		overclock_arc_furnace_4_obsessional_directive
	}

	construction_blocks_and_blocked_by = none

	dismantle_cost = {
		category = megastructures
		cost = {
			energy = 2000
		}
	}

	dismantle_time = 360

	dismantle_potential = {
		always = yes
	}

	dismantle_possible = {
		can_dismantle_megastructure = {
			TECH = tech_orbital_arc_furnace
		}
	}

	on_dismantle_complete = {
		every_system_planet = {
			dismantle_arc_furnace_effect = yes
		}
		random_system_planet = {
			limit = {
				has_planet_flag = has_arc_furnace
			}
			set_planet_entity = {
				entity = pc_molten
			}
			remove_planet_flag = has_megastructure
			remove_planet_flag = has_arc_furnace
			move_consecration_aura_to_body_effect = yes
		}
		from = {
			add_resource = {
				alloys = 2000
				mult = modifier:megastructure_dismantle_refund_mult
			}
			if = {
				limit = {
					check_variable = {
						which = arc_furnace_counter
						value >= 1
					}
				}
				change_variable = {
					which = arc_furnace_counter
					value = -1
				}
			}
		}
	}

	upgrade_from = {
		orbital_arc_furnace_3
	}

	prerequisites = { "tech_orbital_arc_furnace" }

	potential = {
		always = yes
	}


	custom_tooltip_with_modifiers = orbital_arc_furnace_4_mod_tooltip

	on_build_start = {
		set_star_flag = arc_furnace_construction
	}
	on_build_cancel = {
		remove_star_flag = arc_furnace_construction
	}
	on_build_complete = {
		remove_star_flag = arc_furnace_construction
		from = {
			country_event = {
				id = cybernetics.1015
			}
			country_event = {
				id = focus.535
			}
		}
		if = {
			limit = {
				any_system_megastructure = {
					is_megastructure_type = dyson_swarm_3
				}
			}
			from = {
				set_country_flag = burning_brightly_achievement
			}
		}
	}
}
# Orbital Arc Furnace Destroyed
orbital_arc_furnace_destroyed = {
	entity = "arc_crucible_destroyed_entity"
	portrait = "GFX_megastructure_construction_background"
	entity_offset = { x = 0 y = 0 }
	rotate_to_center = no
	scale_offset = yes
	place_entity_on_planet_plane = yes
	show_in_outliner = no
	scales_with_planet = yes
	nomad_reactivatable = yes

	potential = {
		always = no
	}
}

# Orbital arc furnace Restored Stage
orbital_arc_furnace_restored = {
	entity = "arc_crucible_stage_4_entity"
	construction_entity = "arc_crucible_stage_4_entity"
	construction_scale = 1.02 #to avoid z-fighting of consrucion entity with the base entity
	portrait = "GFX_megastructure_arc_furnace_background"
	entity_offset = { x = 0 y = 0 }
	rotate_to_center = no
	scale_offset = yes
	place_entity_on_planet_plane = yes
	show_in_outliner = no
	use_planet_resource = yes
	scales_with_planet = yes
	build_time = 720 # 2 years
	resources = {
		category = megastructures
		cost = {
			alloys = 3000
			unity = 2000
		}

		upkeep = {
			energy = @arc_furnace_4_upkeep_energy
		}
	}

	overclock_loc_key = SMELTING
	overclock_cooldown = @arc_furnace_overclock_cooldown
	overclock_types = {
		overclock_arc_furnace_4_minerals
		overclock_arc_furnace_4_balanced
		overclock_arc_furnace_4_alloys
		overclock_arc_furnace_4_obsessional_directive
	}

	construction_blocks_and_blocked_by = none

	upgrade_from = {
		orbital_arc_furnace_destroyed
	}

	prerequisites = { "tech_orbital_arc_furnace" }

	potential = {
		always = yes
	}

	custom_tooltip_with_modifiers = orbital_arc_furnace_4_mod_tooltip

	on_build_start = {
		set_star_flag = arc_furnace_construction
	}
	on_build_cancel = {
		remove_star_flag = arc_furnace_construction
	}
	on_build_complete = {
		remove_star_flag = arc_furnace_construction
		from = {
			country_event = {
				id = cybernetics.1016
			}
		}
		fromfrom.planet = {
			set_planet_entity = {
				entity = invisible_turret_entity
			}
			move_consecration_aura_to_furnace_effect = yes
		}
		if = {
			limit = {
				any_system_megastructure = {
					is_megastructure_type = dyson_swarm_3
				}
			}
			from = {
				set_country_flag = burning_brightly_achievement
			}
		}
	}
}
