systemcraft_assembler_0 = {
	entity = "construction_platform_entity"
	construction_entity = "construction_platform_entity"
	portrait = "GFX_megastructure_construction_background"
	place_entity_on_planet_plane = no
	prerequisites = {
		tech_stellar_assembler
	}

	build_time = 1800
	resources = {
		category = giga_gigastructures
		cost = {
			influence = 300
			unity = @giga_tera_start_unity_cost
		}
		inline_script = {
			script = megastructures/generic_parts/giga_mega_alloy_cost
			alloys = 5000
		}
		upkeep = {
			energy = 5
		}
	}
	potential = {
		is_ai = no
	}

	possible = {
		custom_tooltip = { fail_text = "requires_inside_border"								is_inside_border = from }
		custom_tooltip = { fail_text = "requires_surveyed_system"							giga_system_is_surveyed = yes }
		custom_tooltip = { fail_text = "requires_no_colonized_planets"						NOT = { any_system_planet = { is_colony = yes } } }
		custom_tooltip = { fail_text = "requires_no_existing_megastructure"					has_no_non_gate_megastructure = yes }
		custom_tooltip = { fail_text = "requires_no_crisis_system"							NOT = { any_system_planet = { has_planet_flag = crisis_vital_planet } } }
	}

	placement_rules = {
		planet_possible = {
			custom_tooltip = { fail_text = "requires_no_existing_megastructure"		planet_has_no_megastructure = yes }
			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_standard_planet_class" 		giga_is_standard_star = yes }
			custom_tooltip = { fail_text = "requires_no_binary_trinary"				giga_is_bitrinary = no }
		}
	}

	on_build_complete = {
		save_event_target_as = star_manip_system
		fromfrom.planet = {
			save_event_target_as = star_manip_planet
		}
		from = {
			country_event = {
				id = stellar_manip_dialogue.11
			}
		}
		fromfrom.planet = {
			if = {
				limit = {
					exists = orbital_station
				}
				orbital_station = {
					dismantle = yes
				}
			}
			set_carrier_flag = megastructure			# Vanilla uses to check if a megastructure can be built here
			set_carrier_flag = has_megastructure			# Vanilla uses to check if an orbital station can be built here
		}
	}
}

systemcraft_assembler_1 = {
	entity = "war_system_printer_1_entity"
	construction_entity = "war_system_printer_1_entity"
	portrait = "GFX_evt_binary_stars"
	place_entity_on_planet_plane = no

	upgrade_from = {
		systemcraft_assembler_0
	}

	build_time = 2400
	resources = {
		category = giga_gigastructures
		cost = {
			unity = @giga_tera_unity_cost
			rare_crystals = 1500
			volatile_motes = 1500
			exotic_gases = 1500
		}
		inline_script = {
			script = megastructures/generic_parts/giga_mega_alloy_cost
			alloys = 80000
		}

		upkeep = {
			energy = 250
			alloys = 125
		}
	}

	on_build_complete = {
		save_event_target_as = star_manip_system
		fromfrom.planet = {
			save_event_target_as = star_manip_planet
		}
		from = {
			country_event = {
				id = stellar_manip_dialogue.12
			}
		}
	}
}

systemcraft_assembler_2 = {
	entity = "war_system_printer_2_entity"
	construction_entity = "war_system_printer_2_entity"
	portrait = "GFX_evt_binary_stars"
	upgrade_from = {
		systemcraft_assembler_1
	}
	place_entity_on_planet_plane = no
	build_time = 3600
	resources = {
		category = giga_gigastructures
		cost = {
			unity = @giga_tera_unity_cost
			rare_crystals = 2500
			volatile_motes = 2500
			exotic_gases = 2500
		}
		inline_script = {
			script = megastructures/generic_parts/giga_mega_alloy_cost
			alloys = 160000
		}

		upkeep = {
			energy = 400
			alloys = 200
		}
	}

	on_build_complete = {
		giga_remove_system_planets = yes
		giga_remove_system_debris = yes
		set_asteroid_belt = {
			radius = 0
		}
		set_star_class = sc_empty_space

		fromfrom.planet = {
			change_pc = pc_giga_empty_space
		}

		space_owner = {
			add_resource = {
				sr_stellar_plasma = 15000
			}
		}
		save_event_target_as = star_manip_system
		fromfrom.planet = {
			save_event_target_as = star_manip_planet
		}
		from = {
			country_event = {
				id = stellar_manip_dialogue.13
			}
		}
	}
}

systemcraft_assembler_3 = {
	entity = "war_system_printer_3_entity"
	construction_entity = "war_system_printer_3_entity"
	portrait = "GFX_evt_binary_stars"
	upgrade_from = {
		systemcraft_assembler_2
	}
	place_entity_on_planet_plane = no
	build_time = 4200
	resources = {
		category = giga_gigastructures
		cost = {
			unity = @giga_tera_unity_cost
			rare_crystals = 4000
			volatile_motes = 4000
			exotic_gases = 4000
		}
		inline_script = {
			script = megastructures/generic_parts/giga_mega_alloy_cost
			alloys = 240000
		}

		upkeep = {
			energy = 1000
			alloys = 500
		}
	}

	on_build_complete = {
		save_event_target_as = star_manip_system
		fromfrom.planet = {
			save_event_target_as = star_manip_planet
		}
		from = {
			country_event = {
				id = stellar_manip_dialogue.14
			}
		}
	}
}

systemcraft_assembler_4 = {
	entity = "war_system_printer_4_entity"
	construction_entity = "war_system_printer_4_entity"
	portrait = "GFX_evt_binary_stars"
	upgrade_from = {
		systemcraft_assembler_3
	}
	place_entity_on_planet_plane = no
	build_time = 5600
	resources = {
		category = giga_gigastructures
		cost = {
			unity = @giga_tera_unity_cost
			rare_crystals = 6000
			volatile_motes = 6000
			exotic_gases = 6000
		}
		inline_script = {
			script = megastructures/generic_parts/giga_mega_alloy_cost
			alloys = 320000
		}

		upkeep = {
			energy = 1500
			alloys = 750
		}
	}

	on_build_complete = {
		save_event_target_as = star_manip_system
		fromfrom.planet = {
			save_event_target_as = star_manip_planet
		}
		from = {
			country_event = {
				id = stellar_manip_dialogue.15
			}
		}
	}
}

systemcraft_assembler_5 = {
	entity = "war_system_printer_5_entity"
	construction_entity = "war_system_printer_5_entity"
	portrait = "GFX_evt_binary_stars"
	upgrade_from = {
		systemcraft_assembler_4
	}
	place_entity_on_planet_plane = no
	build_time = 7200
	resources = {
		category = giga_gigastructures
		cost = {
			unity = @giga_tera_unity_cost
		}
		inline_script = {
			script = megastructures/generic_parts/giga_mega_alloy_cost
			alloys = 400000
		}

		upkeep = {
			energy = 3000
			alloys = 1500
		}
	}
	country_modifier = {
		country_resource_max_alloys_add = 1500000
		country_resource_max_energy_add = 1500000
	}

	on_build_complete = {
		save_event_target_as = star_manip_system
		fromfrom.planet = {
			save_event_target_as = star_manip_planet
		}
		from = {
			if = {  #This check and flag exists because after printing the structure is automatically upgraded back to this stage, the next stage sets the country flag and that stops the notification event from triggering every time
				limit = {
					NOT = {
						has_country_flag = just_assembled_systemcraft
					}
				}
				country_event = {
					id = stellar_manip_dialogue.16
				}
			}
			else_if = {
				limit = {
					has_country_flag = just_assembled_systemcraft
				}
				remove_country_flag = just_assembled_systemcraft
			}
		}
	}
}

systemcraft_assembler_make_system = {
	entity = "war_system_printer_printing_entity"
	construction_entity = "war_system_printer_printing_entity"
	portrait = "GFX_evt_binary_stars"
	upgrade_from = {
		systemcraft_assembler_5
	}
	place_entity_on_planet_plane = no
	build_time = 18000 # How long it takes to build a regular systemcraft (not including constituent celestials and construction site)

	# Cost Breakdown:
	#	A systemcraft is made up of 5 attack moons + 4 planetcraft
	#
	#	Fabrication of 1 attack moon: 24,500 alloys, 20,000 energy, 7 bulk matter (x5= 122,500 alloys, 100,000 energy, 35 bulk matter)
	#	Fabrication of 1 planetcraft: 285,000 alloys, 1,000,000 energy, 26 bulk matter (x4= 1,140,000 alloys 4,000,000 energy, 104 bulk matter)
	#	Systemcraft cost: 500,000 alloys (not including the 5 moons and 4 planets or the construction site)
	#	Class 0 Systemcraft cost: 1,500,000 alloys (not including the 5 moons and 4 planets or the construction site)
	#
	#	Systemcraft total fabrication cost: 1,762,500 alloys, 4,100,000 energy, 139 bulk matter (1,762,500 alloys before cost increase, 2,379,375 alloys after, rounded to 2,380,000)
	#	Systemcraft total fabrication cost: 2,762,500 alloys, 4,100,000 energy, 139 bulk matter (2,762,500 alloys before cost increase, 3,729,375 alloys after, rounded to 3,730,000)

	resources = {
		category = giga_gigastructures
		cost = {
			giga_sr_bulk_matter = 139
			energy = 4100000
			sr_stellar_plasma = 2500
		}
		inline_script = {
			script = megastructures/generic_parts/giga_mega_alloy_cost
			alloys = 2380000 # ~35% more than building it normally
		}
	}
	potential = {
		NOT = {
			has_global_flag = systemcraft_disabled
		}
	}
	possible = {
		from = {
			has_technology = tech_stellar_assembler
		}
		custom_tooltip = {
			fail_text = "requires_not_capped"
			from = {
				OR = {
					check_variable = {
						which = giga_systemcraft_cap
						value = 0
					}
					check_variable = {
						which = giga_current_systemcraft
						value < giga_systemcraft_cap
					}
				}
			}
		}
	}

	on_build_complete = {
		save_event_target_as = giga_systemcraft_print
		from = {
			set_country_flag = just_assembled_systemcraft
			country_event = {
				id = stellar_manip_dialogue.17
			}
			if = {
				limit = {
					NOR = {
						has_global_flag = giga_achievements_disabled
						has_country_flag = giga_achievement_91
					}
				}
				set_country_flag = giga_achievement_91
				set_timed_country_flag = {
					flag = giga_achievement_91_notification
					days = 30
				}
				giga_achievement_sound = yes
			}
		}
	}
}
systemcraft_assembler_make_o_system = {
	entity = "war_system_printer_printing_entity"
	construction_entity = "war_system_printer_printing_entity"
	portrait = "GFX_evt_binary_stars"
	upgrade_from = {
		systemcraft_assembler_5
	}
	place_entity_on_planet_plane = no
	build_time = 54000 # How long it takes to build a class o systemcraft (not including constituent celestials and construction site)

	# Cost Breakdown:
	#	A systemcraft is made up of 5 attack moons + 4 planetcraft
	#
	#	Fabrication of 1 attack moon: 24,500 alloys, 20,000 energy, 7 bulk matter (x5= 122,500 alloys, 100,000 energy, 35 bulk matter)
	#	Fabrication of 1 planetcraft: 285,000 alloys, 1,000,000 energy, 26 bulk matter (x4= 1,140,000 alloys 4,000,000 energy, 104 bulk matter)
	#	Systemcraft cost: 500,000 alloys (not including the 5 moons and 4 planets or the construction site)
	#	Class 0 Systemcraft cost: 1,500,000 alloys (not including the 5 moons and 4 planets or the construction site)
	#
	#	Systemcraft total fabrication cost: 1,762,500 alloys, 4,100,000 energy, 139 bulk matter (1,762,500 alloys before cost increase, 2,379,375 alloys after, rounded to 2,380,000)
	#	Systemcraft total fabrication cost: 2,762,500 alloys, 4,100,000 energy, 139 bulk matter (2,762,500 alloys before cost increase, 3,729,375 alloys after, rounded to 3,730,000)

	resources = {
		category = giga_gigastructures
		cost = {
			giga_sr_bulk_matter = 139
			energy = 4100000
			sr_stellar_plasma = 25000
		}
		inline_script = {
			script = megastructures/generic_parts/giga_mega_alloy_cost
			alloys = 3730000 # ~35% more than building it normally
		}
	}

	potential = {
		NOT = {
			has_global_flag = stellar_manip_o_systemcraft_disabled
		}
	}

	possible = {
		from = {
			has_technology = tech_stellar_assembler
			has_technology = tech_o_class_war_system
		}
		custom_tooltip = {
			fail_text = "requires_not_capped"
			from = {
				OR = {
					check_variable = {
						which = giga_systemcraft_cap
						value = 0
					}
					check_variable = {
						which = giga_current_systemcraft
						value < giga_systemcraft_cap
					}
				}
			}
		}
	}

	on_build_complete = {
		save_event_target_as = giga_systemcraft_print
		from = {
			set_country_flag = just_assembled_systemcraft
			country_event = {
				id = stellar_manip_dialogue.18
			}
			if = {
				limit = {
					NOR = {
						has_global_flag = giga_achievements_disabled
						has_country_flag = giga_achievement_91
					}
				}
				set_country_flag = giga_achievement_91
				set_timed_country_flag = {
					flag = giga_achievement_91_notification
					days = 30
				}
				giga_achievement_sound = yes
			}
		}
	}
}
