# =============================================================================
# Giga + Age of Wonder Compatibility Patch — Giga Dyson Swarm/Sphere overrides
# AOW overrides dyson_swarm_1 (redirects to AOW swarm) and dyson_swarm_3 (removes
# Giga's auto-upgrade to dyson_swarm_3_basic). This file restores Giga's original
# behaviour so the Giga Dyson Swarm chain works correctly.
#
# User design: the ONLY Dyson Sphere path is Giga Dyson Swarm -> AOW Dyson Sphere.
#   * dyson_swarm_1 : restored to Giga original (no AOW redirect to AOW swarm).
#   * dyson_swarm_3 : restored to Giga original, then on_build_complete routes
#     DIRECTLY to AOW_MEGA_dyson_sphere_entry (bypasses swarm_3_basic / star-class
#     passthroughs which are virtual inline_script shells that cannot carry a
#     reliable on_build_complete).
#   * dyson_swarm_3_basic + 7 star-class variants : on_build_complete overrides
#     kept as a safety net in case any other path reaches them (dead code under
#     normal flow). These were the cause of the "stuck at swarm stage 3" bug.
#   * dyson_sphere_0 : direct Giga Dyson Sphere build DISABLED.
#   * dyson_sphere_2 : Giga swarm->sphere link severed.
# =============================================================================

# =============================================================================
# dyson_swarm_1 — Giga original restored (AOW's redirect removed)
# =============================================================================
dyson_swarm_1 = {
	entity = "dyson_swarm_phase_1_entity"
	construction_entity = "dyson_swarm_phase_1_entity"
	portrait = "GFX_megastructure_dyson_swarm_background"
	entity_offset = { x = 0.1 y = 0.1 }
	rotate_to_center = no
	scale_offset = yes
	place_entity_on_planet_plane = yes
	use_planet_resource = yes
	build_time = 1080 # 3 years
	resources = {
		category = giga_kilostructures
		cost = {
			unity = 1000
		}
		inline_script = {
			script = megastructures/generic_parts/giga_mega_alloy_cost
			alloys = 500
		}
		upkeep = {
			alloys = 5
		}
	}

	outliner_trigger = { exists = owner NOT = { has_megastructure_flag = giga_outliner_hidden_by_@owner } }

	construction_blocks_and_blocked_by = none

	prerequisites = { "tech_dyson_swarm" }

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

	dismantle_time = 360

	dismantle_potential = {
		always = yes
	}

	dismantle_possible = {
		can_dismantle_megastructure = {
			TECH = tech_dyson_swarm
		}
	}

	on_dismantle_start = {
		fromfrom.planet = { set_planet_flag = giga_dismantle_not_cancelled planet_event = { id = giga_dismantle.001 days = 361 } }
	}

	on_dismantle_cancel = {
		fromfrom.planet = { remove_planet_flag = giga_dismantle_not_cancelled }
	}

	on_dismantle_complete = {
		from = {
			add_resource = {
				alloys = 500
				mult = modifier:megastructure_dismantle_refund_mult
			}
			if = {
				limit = {
					check_variable = {
						which = dyson_swarm_counter
						value >= 1
					}
				}
				change_variable = {
					which = dyson_swarm_counter
					value = -1
				}
			}
		}
	}

	potential = {
		NOT = { has_country_flag = compat_mega_hide_giga } # compat (SPIKE): AoW-only build-menu tab hides Giga structures
		has_machine_age_dlc = yes
		NOT = { has_global_flag = vanilla_dyson_swarm_disabled}
		OR = {
			check_variable = {
				which = dyson_swarm_counter
				value < value:dyson_swarm_limit
			}
			has_global_flag = vanilla_dyson_swarm_capped_u
		}
	}

	possible = {
		hidden_trigger = {
			exists = starbase
		}
		custom_tooltip = {
			fail_text = "requires_inside_border"
			is_inside_border = from
		}
		custom_tooltip = {
			fail_text = "requires_surveyed_system"
			NOT = {
				any_system_planet = {
					is_surveyed = {
						who = prev.from
						status = no
					}
				}
			}
		}
		custom_tooltip = {
			fail_text = "requires_less_than_x_dyson_swarms"
			from = {
				OR = {
					check_variable = {
						which = dyson_swarm_counter
						value < value:dyson_swarm_limit
					}
					has_global_flag = vanilla_dyson_swarm_capped_u
				}
			}
		}
	}

	placement_rules = {
		planet_possible = {
			custom_tooltip = { fail_text = "requires_no_existing_megastructure"	NOR = { has_planet_flag = megastructure has_planet_flag = has_megastructure } }
			custom_tooltip = { fail_text = "must_build_around_star"					is_star = yes }
			custom_tooltip = { fail_text = "requires_no_anomaly"					has_anomaly = no }
			custom_tooltip = { fail_text = "requires_further_stars" 				NOR = { AND = { giga_is_close_pair_star = yes is_primary_star = no } giga_is_close_pair_primary = yes } }
			custom_tooltip = {
				fail_text = "requires_standard_star_class"
				giga_brown_dwarf = no
				OR = {
					giga_is_standard_star = yes
					is_planet_class = pc_toxoid_star
				}
				giga_is_o_star_for_megas = no
				NOT = { has_planet_flag = giga_eawaf_sun }
			}
			if = {
				limit = {
					from = { is_ai = yes }
				}
				NOR = {
					has_deposit = d_energy_1
					has_deposit = d_energy_2
				}
			}
		}
	}

	country_modifier = {
		custom_tooltip = dyson_swarm_1_mod_tooltip
	}

	# root = system
	# from = country
	ai_weight = {
		weight = value:giga_ai_base_mega

		mult = value:giga_ai_core_territory

		mult = value:giga_ai_value_deposits

		mult = value:giga_ai_prevent_spam|MEGA|dyson_swarm|
	}

	on_build_start = {
		from = {
			set_country_flag = is_currently_building_dyson_swarm
		}
	}
	on_build_cancel = {
		from = {
			remove_country_flag = is_currently_building_dyson_swarm
		}
	}
	on_build_complete = {
		set_star_flag = conduit_excluded
		fromfrom.planet = {
			set_planet_flag = has_megastructure
			add_modifier = {
				modifier = dyson_swarm_1_mod
				days = -1
			}
			if = {
				limit = { has_orbital_station = yes }
				orbital_station = {
					dismantle = yes
				}
			}
		}
		from = {
			remove_country_flag = is_currently_building_dyson_swarm
			set_timed_country_flag = { years = 5 flag = has_recently_built_dyson_swarm }
			change_variable = {
				which = dyson_swarm_counter
				value = 1
			}
		}
	}
}

# =============================================================================
# dyson_sphere_0 — Direct Giga Dyson Sphere build DISABLED (build a Giga Dyson Swarm instead)
# =============================================================================
dyson_sphere_0 = {
	entity = "construction_platform_entity"
	construction_entity = "construction_platform_entity"
	portrait = "GFX_megastructure_construction_background"
	place_entity_on_planet_plane = no
	entity_offset = { x = -7 y = -7 }
	prerequisites = { "tech_dyson_sphere" }
	build_time = 0
	potential = { always = no }
	possible = { always = no }
	placement_rules = { planet_possible = { always = no } }
}

# =============================================================================
# dyson_sphere_2 — Giga swarm->Giga sphere link severed (rerouted to AOW). Unreachable.
# =============================================================================
dyson_sphere_2 = {
	entity = "dyson_sphere_phase_02_entity"
	construction_entity = "dyson_sphere_part_1_entity"
	portrait = "GFX_megastructure_construction_background"
	place_entity_on_planet_plane = no
	upgrade_from = { dyson_sphere_1 }
	build_time = 0
	potential = { always = no }
	possible = { always = no }
}

# =============================================================================
# dyson_swarm_3 — Giga original restored (AOW removed the auto-upgrade).
# COMPAT: on_build_complete routes DIRECTLY to AOW_MEGA_dyson_sphere_entry.
# The Giga star-class scaling (giga_dyson_scaling) is irrelevant here because
# the AOW sphere entry bridge reads the actual star class and routes to the
# correct sphere_0 variant. Bypassing swarm_3_basic / swarm_3_*_star avoids
# the "stuck at swarm stage 3" bug — those are virtual inline_script shells
# that cannot carry a functioning on_build_complete.
# =============================================================================
dyson_swarm_3 = {
	entity = "dyson_swarm_phase_3_entity"
	construction_entity = "dyson_swarm_phase_3_entity"
	portrait = "GFX_megastructure_dyson_swarm_background"
	entity_offset = { x = -0.1 y = -0.1 }
	rotate_to_center = no
	scale_offset = yes
	place_entity_on_planet_plane = yes
	outliner_trigger = {
		exists = owner
		system_has_arc_furnace = no
		NOT = {
			solar_system = {
				has_star_flag = arc_furnace_construction
			}
		}
		NOT = {
			solar_system = {
				any_system_planet = {
					is_colony = yes
					is_artificial = no
					exists = owner
					owner = {
						is_primitive = no
					}
				}
			}
		}
		if = {
			limit = {
				solar_system = {
					any_system_planet = {
						exists = owner
						owner = {
							is_primitive = yes
						}
					}
				}
			}
			owner = {
				has_policy_flag = interference_aggressive
			}
		}
		owner = {
			has_technology = tech_dyson_sphere
			OR = {
				has_global_flag = vanilla_dyson_capped_u
				check_variable = { which = giga_current_vanilla_dyson value < giga_vanilla_dyson_cap }
				root = { is_upgrading = yes }
			}
		}
		NOT = { has_megastructure_flag = giga_outliner_hidden_by_@owner }
	}

	construction_blocks_and_blocked_by = none
	use_planet_resource = yes

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

	dismantle_time = 360

	dismantle_potential = {
		always = yes
	}

	dismantle_possible = {
		can_dismantle_megastructure = {
			TECH = tech_dyson_swarm
		}
	}

	on_dismantle_start = {
		fromfrom.planet = { set_planet_flag = giga_dismantle_not_cancelled planet_event = { id = giga_dismantle.003 days = 361 } }
	}

	on_dismantle_cancel = {
		fromfrom.planet = { remove_planet_flag = giga_dismantle_not_cancelled }
	}

	on_dismantle_complete = {
		from = {
			add_resource = {
				alloys = 1500
				mult = modifier:megastructure_dismantle_refund_mult
			}
			if = {
				limit = {
					check_variable = {
						which = dyson_swarm_counter
						value >= 1
					}
				}
				change_variable = {
					which = dyson_swarm_counter
					value = -1
				}
			}
		}
	}

	build_time = 1080 # 3 years
	resources = {
		category = giga_kilostructures
		cost = {
			unity = 5000
		}
		inline_script = {
			script = megastructures/generic_parts/giga_mega_alloy_cost
			alloys = 1500
		}

		upkeep = {
			alloys = 20
		}
	}
	upgrade_from = {
		dyson_swarm_2
	}

	prerequisites = { "tech_dyson_swarm" }

	potential = {
		always = yes
	}

	country_modifier = {
		custom_tooltip = dyson_swarm_3_mod_tooltip
	}

	ai_weight = { weight = value:giga_ai_base_continue }

	on_build_complete = {
		fromfrom.planet = {
			remove_modifier = dyson_swarm_2_mod
			add_modifier = {
				modifier = dyson_swarm_3_mod
				days = -1
			}
		}
		if = {
			limit = {
				any_system_megastructure = {
					OR = {
						is_megastructure_type = orbital_arc_furnace_4
						is_megastructure_type = orbital_arc_furnace_restored
					}
				}
			}
			from = {
				set_country_flag = burning_brightly_achievement
			}
		}
		# COMPAT: route directly to AOW sphere entry (bypassing Giga's
		# swarm_3_basic / swarm_3_*_star virtual passthroughs). The entry
		# bridge reads the actual star class and routes to the correct
		# AOW sphere_0 variant.
		fromfrom = {
			upgrade_megastructure_to = AOW_MEGA_dyson_sphere_entry
			finish_upgrade = yes
		}
		fromfrom = {
			set_megastructure_flag = giga_dyson_swarm
			if = {
				limit = {
					OR = {
						has_global_flag = vanilla_dyson_disabled
						NOT = { has_global_flag = vanilla_dyson_swarm_replace_disabled }
					}
				}
				set_megastructure_flag = giga_outliner_hidden_by_@owner
			}
		}
	}
}

# =============================================================================
# Giga swarm stage-3 final variants — SAFETY NET overrides.
# These are normally UNREACHED because dyson_swarm_3 now routes directly to
# AOW_MEGA_dyson_sphere_entry (see above). The on_build_complete redirects below
# exist only as dead-code insurance in case any other path creates these
# inline_script virtual megastructures. They were the original compat design
# but proved unreliable — the inline_script body does not support a functioning
# on_build_complete override, which caused the "stuck at swarm stage 3" bug.
# =============================================================================

dyson_swarm_3_basic = {
	inline_script = {
		script = megastructures/dyson_swarm/dyson_swarm_scale
	}
	on_build_complete = {
		fromfrom = {
			upgrade_megastructure_to = AOW_MEGA_dyson_sphere_entry
			finish_upgrade = yes
		}
	}
}

dyson_swarm_3_a_star = {
	inline_script = {
		script = megastructures/dyson_swarm/dyson_swarm_scale
	}
	on_build_complete = {
		fromfrom = {
			upgrade_megastructure_to = AOW_MEGA_dyson_sphere_entry
			finish_upgrade = yes
		}
	}
}

dyson_swarm_3_b_star = {
	inline_script = {
		script = megastructures/dyson_swarm/dyson_swarm_scale
	}
	on_build_complete = {
		fromfrom = {
			upgrade_megastructure_to = AOW_MEGA_dyson_sphere_entry
			finish_upgrade = yes
		}
	}
}

dyson_swarm_3_f_star = {
	inline_script = {
		script = megastructures/dyson_swarm/dyson_swarm_scale
	}
	on_build_complete = {
		fromfrom = {
			upgrade_megastructure_to = AOW_MEGA_dyson_sphere_entry
			finish_upgrade = yes
		}
	}
}

dyson_swarm_3_g_star = {
	inline_script = {
		script = megastructures/dyson_swarm/dyson_swarm_scale
	}
	on_build_complete = {
		fromfrom = {
			upgrade_megastructure_to = AOW_MEGA_dyson_sphere_entry
			finish_upgrade = yes
		}
	}
}

dyson_swarm_3_k_star = {
	inline_script = {
		script = megastructures/dyson_swarm/dyson_swarm_scale
	}
	on_build_complete = {
		fromfrom = {
			upgrade_megastructure_to = AOW_MEGA_dyson_sphere_entry
			finish_upgrade = yes
		}
	}
}

dyson_swarm_3_m_giant_star = {
	inline_script = {
		script = megastructures/dyson_swarm/dyson_swarm_scale
	}
	on_build_complete = {
		fromfrom = {
			upgrade_megastructure_to = AOW_MEGA_dyson_sphere_entry
			finish_upgrade = yes
		}
	}
}

dyson_swarm_3_m_star = {
	inline_script = {
		script = megastructures/dyson_swarm/dyson_swarm_scale
	}
	on_build_complete = {
		fromfrom = {
			upgrade_megastructure_to = AOW_MEGA_dyson_sphere_entry
			finish_upgrade = yes
		}
	}
}
