# see README.txt for example

@position_offset_x = -14
@position_offset_y = -14
#these polar coordinates define the same point as position_offset above
@polar_offset_angle = 45
@polar_offset_radius = 19.799	# sqrt( 14^2 + 14^2 )
@plane_offset = 5

behemoth_egg = {
	entity = "kaiju_01_egg_entity"
	construction_entity = "kaiju_01_egg_entity"
	portrait = "GFX_megastructure_behemoth_egg_background"
	show_galactic_map_icon = yes
	show_in_outliner = yes
	place_entity_on_planet_plane = yes
	entity_offset = { x = @position_offset_x y = @position_offset_y }
	plane_offset = -3
	build_time = 3600

	resources = {
		category = megastructures
		cost = {
			food = 50000
			influence = 300
		}
	}

	construction_blocks_and_blocked_by = none

	potential = {
		owner = {
			has_crisis_perk = menp_behemoth_egg
		}
	}

	possible = {
		inline_script = megastructures/system_ownership_or_waystation_check
		custom_tooltip = {
			fail_text = "requires_no_existing_megastructure"
			has_no_non_gate_megastructure = yes
		}
	}

	placement_rules = {
		planet_possible = {
			custom_tooltip = {
				fail_text = "requires_surveyed_planet"
				is_surveyed = {			# prevent leaking habitability information
					who = prev.from
					status = yes
				}
			}
			custom_tooltip = {
				fail_text = "requires_no_anomaly"
				NOT = {
					has_anomaly = yes
				}
			}
			custom_tooltip = {
				fail_text = "requires_no_existing_megastructure"
				#can_build_megastructure_on_planet = yes
				NOR = {
					has_planet_flag = megastructure
					has_planet_flag = has_megastructure
					solar_system = {
						has_star_flag = ring_world_built
					}
					is_planet_class = pc_ringworld_habitable
					is_planet_class = pc_ringworld_habitable_damaged
					is_planet_class = pc_ringworld_tech
					is_planet_class = pc_ringworld_tech_damaged
					is_planet_class = pc_ringworld_seam
					is_planet_class = pc_ringworld_seam_damaged
					is_planet_class = pc_habitat
				}
			}
			custom_tooltip = {
				fail_text = "requires_no_orbital_debris"
				NOR = {
					has_carrier_flag = has_orbital_debris
				}
			}
			custom_tooltip = {
				fail_text = "requires_gas_giant"
				is_planet_class = pc_gas_giant
			}
		}
	}

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

	on_build_start = {}

	on_build_cancel = {}

	on_build_complete = {
		from = {
			set_country_flag = behemoth_egg_built
			complete_crisis_objective = crisobj_build_behemoth_egg
		}
		fromfrom.planet = {
			set_planet_flag = has_megastructure
		}
	}
}

behemoth_egg_hatch = {
	entity = "kaiju_01_egg_entity"
	construction_entity = "kaiju_01_egg_entity"
	portrait = "GFX_megastructure_behemoth_egg_background"
	show_galactic_map_icon = no
	show_in_outliner = no
	place_entity_on_planet_plane = yes
	entity_offset = { x = @position_offset_x y = @position_offset_y }
	plane_offset = -3
	build_time = 3600

	resources = {
		category = megastructures
		cost = {
			food = 100000
			influence = 150
		}
	}

	upgrade_from = {
		behemoth_egg
	}

	construction_blocks_and_blocked_by = none

	possible = {
		exists = from
		from = {
			has_crisis_perk = menp_behemoth_hatchling
		}
	}


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

	on_build_start = {}
	on_build_cancel = {}

	on_build_complete = {
		if = {
			limit = {
				from = {
					is_country_type = behemoth_hatchling
				}
			}
			random_country = {
				limit = {
					has_country_flag = behemoth_victory
				}
				save_event_target_as = behemoth_parent_country
			}
		}
		else = {
			from = {
				set_country_flag = behemoth_egg_hatched
				save_event_target_as = behemoth_parent_country
				country_event = {
					id = timeline.91
				}
			}
		}


		#Remove the megastructure
		fromfrom.planet =  {
			save_event_target_as = target_planet
			carrier_event = {
				id = biocrisis.1
			}
		}
		spawn_megastructure = {
			type = behemoth_egg_ruined
			owner = from
			planet = event_target:target_planet
			coords_from = event_target:target_planet
		}
		remove_megastructure = fromfrom
	}
}

# Ruined Version
# Always replace with the broken egg entity. No repairs possible.
behemoth_egg_ruined = {
	entity = "kaiju_01_egg_destroyed_entity"
	construction_entity = "kaiju_01_egg_destroyed_entity"
	portrait = "GFX_megastructure_behemoth_egg_background"
	show_galactic_map_icon = no
	show_in_outliner = no
	place_entity_on_planet_plane = yes
	entity_offset = { x = @position_offset_x y = @position_offset_y }
	plane_offset = -3

	potential = {
		always = no
	}
}

behemoth_big_egg_ruined = {
	entity = "kaiju_01_big_egg_destroyed_entity"
	construction_entity = "kaiju_01_big_egg_destroyed_entity"
	portrait = "GFX_megastructure_behemoth_egg_background"
	show_galactic_map_icon = no
	show_in_outliner = no
	place_entity_on_planet_plane = yes
	entity_offset = { x = @position_offset_x y = @position_offset_y }
	plane_offset = -3

	potential = {
		always = no
	}
}

# Eggs laid by Behemoths

behemoth_fleet_egg = {
	entity = "kaiju_01_egg_entity"
	construction_entity = "kaiju_01_egg_entity"
	portrait = "GFX_megastructure_behemoth_egg_background"
	show_galactic_map_icon = yes
	show_in_outliner = yes
	place_entity_on_planet_plane = yes
	entity_offset = { x = @position_offset_x y = @position_offset_y }
	plane_offset = -3
	build_time = 1

	resources = {
		category = megastructures
	}

	construction_blocks_and_blocked_by = none

	potential = {
		always = no
	}

	possible = {
		hidden_trigger = {
			exists = starbase
		}
		custom_tooltip = {
			fail_text = "requires_inside_border"
			is_inside_border = from
		}
	}

	placement_rules = {
		planet_possible = {
			custom_tooltip = {
				fail_text = "requires_surveyed_planet"
				is_surveyed = {			# prevent leaking habitability information
					who = prev.from
					status = yes
				}
			}
			custom_tooltip = {
				fail_text = "requires_no_anomaly"
				NOT = {
					has_anomaly = yes
				}
			}
			custom_tooltip = {
				fail_text = "requires_no_existing_megastructure"
				#can_build_megastructure_on_planet = yes
				NOR = {
					has_planet_flag = megastructure
					has_planet_flag = has_megastructure
					solar_system = {
						has_star_flag = ring_world_built
					}
					is_planet_class = pc_ringworld_habitable
					is_planet_class = pc_ringworld_habitable_damaged
					is_planet_class = pc_ringworld_tech
					is_planet_class = pc_ringworld_tech_damaged
					is_planet_class = pc_ringworld_seam
					is_planet_class = pc_ringworld_seam_damaged
					is_planet_class = pc_habitat
				}
			}
			custom_tooltip = {
				fail_text = "requires_no_orbital_debris"
				NOR = {
					has_carrier_flag = has_orbital_debris
				}
			}
			custom_tooltip = {
				fail_text = "requires_gas_giant"
				is_planet_class = pc_gas_giant
			}
		}
	}

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

	on_build_start = {}
	on_build_cancel = {}

	on_build_complete = {}
}

behemoth_fast_egg_hatch = {
	entity = "kaiju_01_egg_entity"
	construction_entity = "kaiju_01_egg_entity"
	portrait = "GFX_megastructure_behemoth_egg_background"
	show_galactic_map_icon = no
	show_in_outliner = no
	place_entity_on_planet_plane = yes
	entity_offset = { x = @position_offset_x y = @position_offset_y }
	plane_offset = -3
	build_time = @behemoth_fast_egg_growth_time

	resources = {
		category = megastructures
		cost = {
			food = @behemoth_fast_egg_hatch_cost
		}
	}

	upgrade_from = {
		behemoth_fleet_egg
	}

	construction_blocks_and_blocked_by = none

	potential = {
		owner = {
			has_country_flag = behemoth_fast_egg
		}
	}

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

	on_build_start = {}
	on_build_cancel = {}

	on_build_complete = {
		from = {
			set_country_flag = behemoth_egg_hatched
			save_event_target_as = behemoth_parent_country
		}

		#Remove the megastructure
		fromfrom.planet =  {
			remove_planet_flag = has_megastructure
			save_event_target_as = target_planet
		}
		remove_megastructure = fromfrom
		spawn_fast_egg_fleet_effect = yes

		create_message = {
			type = BEHEMOTH_EGG_HATCHED
			localization =  BEHEMOTH_EGG_HATCHED_DESC
			days = @toast_message_days
			target = event_target:target_planet
		}
	}
}

behemoth_slow_egg_hatch = {
	entity = "kaiju_01_egg_entity"
	construction_entity = "kaiju_01_egg_entity"
	portrait = "GFX_megastructure_behemoth_egg_background"
	show_galactic_map_icon = no
	show_in_outliner = no
	place_entity_on_planet_plane = yes
	entity_offset = { x = @position_offset_x y = @position_offset_y }
	plane_offset = -3
	build_time = @behemoth_slow_egg_growth_time

	resources = {
		category = megastructures
		cost = {
			food = @behemoth_slow_egg_hatch_cost
		}
	}

	upgrade_from = {
		behemoth_fleet_egg
	}

	construction_blocks_and_blocked_by = none

	potential = {
		owner = {
			has_country_flag = behemoth_slow_egg
		}
	}

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

	on_build_start = {}
	on_build_cancel = {}

	on_build_complete = {
		from = {
			set_country_flag = behemoth_egg_hatched
			save_event_target_as = behemoth_parent_country
		}

		#Remove the megastructure
		fromfrom.planet =  {
			remove_planet_flag = has_megastructure
			save_event_target_as = target_planet
		}
		remove_megastructure = fromfrom
		spawn_slow_egg_fleet_effect = yes

		create_message = {
			type = BEHEMOTH_EGG_HATCHED
			localization =  BEHEMOTH_EGG_HATCHED_DESC
			days = @toast_message_days
			target = event_target:target_planet
		}
	}
}

behemoth_strong_egg_hatch = {
	entity = "kaiju_01_egg_entity"
	construction_entity = "kaiju_01_egg_entity"
	portrait = "GFX_megastructure_behemoth_egg_background"
	show_galactic_map_icon = no
	show_in_outliner = no
	place_entity_on_planet_plane = yes
	entity_offset = { x = @position_offset_x y = @position_offset_y }
	plane_offset = -3
	build_time = @behemoth_strong_egg_growth_time

	resources = {
		category = megastructures
		cost = {
			food = @behemoth_strong_egg_hatch_cost
		}
	}

	upgrade_from = {
		behemoth_fleet_egg
	}

	construction_blocks_and_blocked_by = none

	potential = {
		owner? = {
			has_country_flag = behemoth_strong_egg
		}
	}

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

	on_build_start = {}
	on_build_cancel = {}

	on_build_complete = {
		if = {
			limit = {
				from = {
					is_country_type = behemoth_hatchling
				}
			}
			random_country = {
				limit = {
					has_country_flag = behemoth_victory
				}
				save_event_target_as = behemoth_parent_country
			}
		}
		else = {
			from = {
				set_country_flag = behemoth_egg_hatched
				save_event_target_as = behemoth_parent_country
			}
		}

		#Remove the megastructure
		fromfrom.planet =  {
			remove_planet_flag = has_megastructure
			save_event_target_as = target_planet
		}
		remove_megastructure = fromfrom
		spawn_behemoth_hatchling_effect = yes

		create_message = {
			type = BEHEMOTH_EGG_HATCHED
			localization =  BEHEMOTH_EGG_HATCHED_DESC
			days = @toast_message_days
			target = event_target:target_planet
		}
	}
}

behemoth_chrysalis = {
	entity = "kaiju_01_egg_entity"
	construction_entity = "kaiju_01_egg_entity"
	portrait = "GFX_megastructure_behemoth_egg_background"
	show_galactic_map_icon = yes
	show_in_outliner = yes
	place_entity_on_planet_plane = yes
	entity_offset = { x = @position_offset_x y = @position_offset_y }
	plane_offset = -3
	build_time = 1

	resources = {
		category = megastructures
		cost = {
			food = 0
		}
	}

	construction_blocks_and_blocked_by = none

	potential = {
		always = no
	}

	possible = {
		inline_script = megastructures/system_ownership_or_waystation_check
	}

	on_build_start = {}
	on_build_cancel = {}

	on_build_complete = {
	}
}

behemoth_chrysalis_hatch = {
	entity = "kaiju_01_egg_entity"
	construction_entity = "kaiju_01_egg_entity"
	portrait = "GFX_megastructure_behemoth_egg_background"
	show_galactic_map_icon = no
	show_in_outliner = no
	place_entity_on_planet_plane = yes
	entity_offset = { x = @position_offset_x y = @position_offset_y }
	plane_offset = -3
	build_time = 1080

	resources = {
		category = megastructures
		cost = {
			food = 75000
		}
	}

	upgrade_from = {
		behemoth_chrysalis
	}

	construction_blocks_and_blocked_by = none

	possible = {
		custom_tooltip = {
			fail_text = behemoth_cocoon_tt
			from? = {
				has_crisis_perk = menp_behemoth_hatchling_2
			}
		}
	}

	on_build_start = {}
	on_build_cancel = {}

	on_build_complete = {
		fromfrom = {
			save_event_target_as = behemoth_chrysalis
		}
		if = {
			limit = {
				from = {
					is_country_type = behemoth_hatchling
				}
			}
			random_country = {
				limit = {
					has_country_flag = behemoth_victory
				}
				save_event_target_as = behemoth_owner
			}
		}
		else = {
			from = {
				set_country_flag = behemoth_egg_hatched
				save_event_target_as = behemoth_owner
			}
		}
		fromfrom.planet =  {
			remove_planet_flag = has_megastructure
			save_event_target_as = target_planet
			carrier_event = {
				id = biocrisis.125
			}
		}
	}
}

behemoth_chrysalis_big = {
	entity = "kaiju_01_big_egg_entity"
	construction_entity = "kaiju_01_big_egg_entity"
	portrait = "GFX_megastructure_behemoth_egg_background"
	show_galactic_map_icon = yes
	show_in_outliner = yes
	place_entity_on_planet_plane = yes
	entity_offset = { x = @position_offset_x y = @position_offset_y }
	plane_offset = -3
	build_time = 1

	resources = {
		category = megastructures
		cost = {
			food = 0
		}
	}

	construction_blocks_and_blocked_by = none

	potential = {
		always = no
	}

	possible = {
		inline_script = megastructures/system_ownership_or_waystation_check
	}

	on_build_start = {}
	on_build_cancel = {}

	on_build_complete = {
	}
}

behemoth_chrysalis_big_hatch = {
	entity = "kaiju_01_big_egg_entity"
	construction_entity = "kaiju_01_big_egg_entity"
	portrait = "GFX_megastructure_behemoth_egg_background"
	show_galactic_map_icon = no
	show_in_outliner = no
	place_entity_on_planet_plane = yes
	entity_offset = { x = @position_offset_x y = @position_offset_y }
	plane_offset = -3
	build_time = 1080

	resources = {
		category = megastructures
		cost = {
			food = 125000
		}
	}

	upgrade_from = {
		behemoth_chrysalis_big
	}

	construction_blocks_and_blocked_by = none

	possible = {
		custom_tooltip = {
			fail_text = behemoth_cocoon_tt
			from? = {
				has_crisis_perk = menp_behemoth_hatchling_2
			}
		}
	}

	on_build_start = {}
	on_build_cancel = {}

	on_build_complete = {
		fromfrom = {
			save_event_target_as = behemoth_chrysalis
		}
		if = {
			limit = {
				from = {
					is_country_type = behemoth_hatchling
				}
			}
			random_country = {
				limit = {
					has_country_flag = behemoth_victory
				}
				save_event_target_as = behemoth_owner
			}
		}
		else = {
			from = {
				set_country_flag = behemoth_egg_hatched
				save_event_target_as = behemoth_owner
			}
		}
		fromfrom.planet =  {
			remove_planet_flag = has_megastructure
			save_event_target_as = target_planet
			carrier_event = {
				id = biocrisis.125
			}
		}
	}
}
