############################################
### VANILLA SCRIPTED EFFECT OVERWRITES #####
############################################



############################################
### NSC SCRIPTED EFFECTS ###################
############################################

spawn_free_defense_platform_medium = {
	fleet = {
		if = {
			limit = {
				any_owned_ship = {
					is_ship_size = military_station_medium
				}
			}
			random_owned_ship = {
				limit = {
					is_ship_size = military_station_medium
				}
				save_event_target_as = target_platform
			}
		}
		else = {
			owner = {
				random_owned_design = {
					limit = {
						is_ship_size = military_station_medium
					}
					save_event_target_as = target_platform
				}
			}
		}
		create_ship = {
			name = random
			design = event_target:target_platform
			graphical_culture = owner
		}
	}
}


spawn_free_defense_platform_large = {
	fleet = {
		if = {
			limit = {
				any_owned_ship = {
					is_ship_size = military_station_large
				}
			}
			random_owned_ship = {
				limit = {
					is_ship_size = military_station_large
				}
				save_event_target_as = target_platform
			}
		}
		else = {
			owner = {
				random_owned_design = {
					limit = {
						is_ship_size = military_station_large
					}
					save_event_target_as = target_platform
				}
			}
		}
		create_ship = {
			name = random
			design = event_target:target_platform
			graphical_culture = owner
		}
	}
}


nsc_flagship_creation_for_ai_effect = {
	optimize_memory
	if = {
		limit = { NOT = { has_technology = tech_bh_power_3 } }
		give_technology = { tech = tech_bh_power_3 message = no }
	}
	if = {
		limit = { NOT = { has_technology = tech_Flagship_1 } }
		give_technology = { tech = tech_Flagship_1 message = no }
	}
	create_fleet = {
		effect = {
			set_owner = prev
			create_ship = {
				name = random
				random_existing_design = Flagship
				graphical_culture = prev
			}
			set_location = prev.capital_scope.starbase
		}
	}
	set_country_flag = nsc_flagship_owner
}
nsc_mothball_target_fleet_repair_cost_calc_effect = {
	optimize_memory
	if = {
		limit = { is_variable_set = nsc_mothball_target_fleet_total_cost_var }
		clear_variable = nsc_mothball_target_fleet_total_cost_var
	}
	every_owned_ship = {
		export_trigger_value_to_variable = { trigger = has_hp variable = nsc_mothball_target_ship_cost_var }
		divide_variable = { which = nsc_mothball_target_ship_cost_var value = 100 }
		prev = { change_variable = { which = nsc_mothball_target_fleet_total_cost_var value = prev.nsc_mothball_target_ship_cost_var } }
		clear_variable = nsc_mothball_target_ship_cost_var
	}
}
nsc_mothball_target_fleet_cost_count_effect = {
	optimize_memory
	random_fleet_in_orbit = {
		limit = {
			owner = { is_same_value = prev.controller } #leased fleets should not be possible to mothball 
			#A: Hey, can I borrow your stuffs and put them into my garage and forget? #B: Jeez, mate! Why do you borrow things when you don't even need them? #idk, for fun?
			OR = {
				has_fleet_flag = nsc_mothball_temporal_target_$ID$
				has_fleet_flag = nsc_mothballed_fleet_$ID$
			}
		}
		save_event_target_as = nsc_mothball_target_fleet_$ID$
		nsc_mothball_target_fleet_repair_cost_calc_effect = yes
	}
}
nsc_mothball_target_fleet_selecting_effect = {
	optimize_memory
	if = { #lock temporally selected targets so they can keep appear in event options until the player choose to close the window
		limit = { NOT = { has_planet_flag = nsc_mothball_target_fleets_counting_stopped } }
		while = {
			change_variable = { which = nsc_mothball_target_fleet_count_var value = 1 }
			random_fleet_in_orbit = {
				limit = {
					nsc_mothball_target_fleet_requirement = yes
					#exclude already selected targets
					NOR = {
						has_fleet_flag = nsc_mothball_temporal_target_1
						has_fleet_flag = nsc_mothball_temporal_target_2
						has_fleet_flag = nsc_mothball_temporal_target_3
						has_fleet_flag = nsc_mothball_temporal_target_4
						has_fleet_flag = nsc_mothball_temporal_target_5
						has_fleet_flag = nsc_mothball_temporal_target_6
						has_fleet_flag = nsc_mothball_temporal_target_7
						has_fleet_flag = nsc_mothball_temporal_target_8
						has_fleet_flag = nsc_mothball_temporal_target_9
						has_fleet_flag = nsc_mothball_temporal_target_10
						nsc_mothball_target_fleet_all_flags_trigger = yes
					}
				}
				if = {
					limit = {
						prev = {
							check_variable = { which = nsc_mothball_target_fleet_count_var value = 1 }
							NOT = { any_fleet_in_orbit = { has_fleet_flag = nsc_mothballed_fleet_1 } }
						}
					}
					set_fleet_flag = nsc_mothball_temporal_target_1
				}
				else_if = {
					limit = {
						prev = {
							check_variable = { which = nsc_mothball_target_fleet_count_var value = 2 }
							NOT = { any_fleet_in_orbit = { has_fleet_flag = nsc_mothballed_fleet_2 } }
						}
					}
					set_fleet_flag = nsc_mothball_temporal_target_2
				}
				else_if = {
					limit = {
						prev = {
							check_variable = { which = nsc_mothball_target_fleet_count_var value = 3 }
							NOT = { any_fleet_in_orbit = { has_fleet_flag = nsc_mothballed_fleet_3 } }
						}
					}
					set_fleet_flag = nsc_mothball_temporal_target_3
				}
				else_if = {
					limit = {
						prev = {
							check_variable = { which = nsc_mothball_target_fleet_count_var value = 4 }
							NOT = { any_fleet_in_orbit = { has_fleet_flag = nsc_mothballed_fleet_4 } }
						}
					}
					set_fleet_flag = nsc_mothball_temporal_target_4
				}
				else_if = {
					limit = {
						prev = {
							check_variable = { which = nsc_mothball_target_fleet_count_var value = 5 }
							NOT = { any_fleet_in_orbit = { has_fleet_flag = nsc_mothballed_fleet_5 } }
						}
					}
					set_fleet_flag = nsc_mothball_temporal_target_5
				}
				else_if = {
					limit = {
						prev = {
							check_variable = { which = nsc_mothball_target_fleet_count_var value = 6 }
							NOT = { any_fleet_in_orbit = { has_fleet_flag = nsc_mothballed_fleet_6 } }
						}
					}
					set_fleet_flag = nsc_mothball_temporal_target_6
				}
				else_if = {
					limit = {
						prev = {
							check_variable = { which = nsc_mothball_target_fleet_count_var value = 7 }
							NOT = { any_fleet_in_orbit = { has_fleet_flag = nsc_mothballed_fleet_7 } }
						}
					}
					set_fleet_flag = nsc_mothball_temporal_target_7
				}
				else_if = {
					limit = {
						prev = {
							check_variable = { which = nsc_mothball_target_fleet_count_var value = 8 }
							NOT = { any_fleet_in_orbit = { has_fleet_flag = nsc_mothballed_fleet_8 } }
						}
					}
					set_fleet_flag = nsc_mothball_temporal_target_8
				}
				else_if = {
					limit = {
						prev = {
							check_variable = { which = nsc_mothball_target_fleet_count_var value = 9 }
							NOT = { any_fleet_in_orbit = { has_fleet_flag = nsc_mothballed_fleet_9 } }
						}
					}
					set_fleet_flag = nsc_mothball_temporal_target_9
				}
				else_if = {
					limit = {
						prev = {
							check_variable = { which = nsc_mothball_target_fleet_count_var value = 10 }
							NOT = { any_fleet_in_orbit = { has_fleet_flag = nsc_mothballed_fleet_10 } }
						}
					}
					set_fleet_flag = nsc_mothball_temporal_target_10
				}
			}
		}
		clear_variable = nsc_mothball_target_fleet_count_var
		set_planet_flag = nsc_mothball_target_fleets_counting_stopped
		nsc_mothball_target_fleet_cost_count_effect = { ID = 1 }
		nsc_mothball_target_fleet_cost_count_effect = { ID = 2 }
		nsc_mothball_target_fleet_cost_count_effect = { ID = 3 }
		nsc_mothball_target_fleet_cost_count_effect = { ID = 4 }
		nsc_mothball_target_fleet_cost_count_effect = { ID = 5 }
		nsc_mothball_target_fleet_cost_count_effect = { ID = 6 }
		nsc_mothball_target_fleet_cost_count_effect = { ID = 7 }
		nsc_mothball_target_fleet_cost_count_effect = { ID = 8 }
		nsc_mothball_target_fleet_cost_count_effect = { ID = 9 }
		nsc_mothball_target_fleet_cost_count_effect = { ID = 10 }
	}
}
nsc_mothball_target_fleet_mothball_start_effect = {
	optimize_memory
	custom_tooltip = nsc_events_mothball.menu.fleet_$ID$_mothball_start.tooltip
	hidden_effect = {
		event_target:nsc_mothball_target_fleet_$ID$ = {
			every_owned_ship = { set_disabled = yes }
			remove_fleet_flag = nsc_mothball_temporal_target_$ID$
			set_fleet_flag = nsc_mothballed_fleet_$ID$
			if = {
				limit = { event_target:nsc_modmenu_data_storage = { check_variable = { which = nsc_modmenu_etc_feature_mothball_fleet_resource_upkeep_var value != 0 } } }
				#add_modifier = { modifier = nsc_modmenu_etc_feature_mothball_fleet_resource_upkeep_mod multiplier = event_target:nsc_modmenu_data_storage.nsc_modmenu_etc_feature_mothball_fleet_resource_upkeep_var }
			}
			if = {
				limit = { event_target:nsc_modmenu_data_storage = { check_variable = { which = nsc_modmenu_etc_feature_mothball_fleet_naval_capacity_upkeep_var value != 0 } } }
				#add_modifier = { modifier = nsc_modmenu_etc_feature_mothball_fleet_naval_capacity_upkeep_mod multiplier = event_target:nsc_modmenu_data_storage.nsc_modmenu_etc_feature_mothball_fleet_naval_capacity_upkeep_var }
			}
		}
		planet_event = { id = nsc_events_mothball.menu }
	}
}
nsc_mothball_target_fleet_repair_time_calc_effect = {
	optimize_memory
	export_trigger_value_to_variable = { trigger = count_owned_ship variable = nsc_fleet_in_mothball_repair_timer_var }
	multiply_variable = { which = nsc_fleet_in_mothball_repair_timer_var value = 30 } #30 days per each ship

	#divide the value by 100 and multiply by pre-set number (e.g. the default is 20, so it's 20% of the value) to get its percentage
	divide_variable = { which = nsc_fleet_in_mothball_repair_timer_var value = 100 }
	multiply_variable = { which = nsc_fleet_in_mothball_repair_timer_var value = event_target:nsc_modmenu_data_storage.nsc_modmenu_etc_feature_mothball_fleet_restore_time_var }

	ceiling_variable = nsc_fleet_in_mothball_repair_timer_var #Rounds a previously-set variable 'up' to the next integer.
	if = { #if the fleet is already being restored
		limit = { is_variable_set = nsc_fleet_in_mothball_spent_time_for_repair_var }
		subtract_variable = { which = nsc_fleet_in_mothball_repair_timer_var value = nsc_fleet_in_mothball_spent_time_for_repair_count_var }
	}

	fleet_event = { id = nsc_events_mothball.2 days = 1 } #why 'days' here only accepts integers but variables or script values? #I'm sad... :(
	clear_variable = nsc_mothball_target_fleet_total_cost_var
}
nsc_mothball_target_fleet_mothball_stop_effect = {
	optimize_memory
	custom_tooltip = nsc_events_mothball.menu.fleet_mothball_stop.tooltip
	owner = { add_resource = { alloys = -1 mult = event_target:nsc_mothball_target_fleet_$ID$.nsc_mothball_target_fleet_total_cost_var } }
	hidden_effect = {
		event_target:nsc_mothball_target_fleet_$ID$ = { nsc_mothball_target_fleet_repair_time_calc_effect = yes }
		planet_event = { id = nsc_events_mothball.menu }
	}
}
nsc_mothball_target_fleet_remove_all_flags_effect = {
	optimize_memory
	switch = {
		trigger = has_fleet_flag
		nsc_mothballed_fleet_1 = { remove_fleet_flag = nsc_mothballed_fleet_1 }
		nsc_mothballed_fleet_2 = { remove_fleet_flag = nsc_mothballed_fleet_2 }
		nsc_mothballed_fleet_3 = { remove_fleet_flag = nsc_mothballed_fleet_3 }
		nsc_mothballed_fleet_4 = { remove_fleet_flag = nsc_mothballed_fleet_4 }
		nsc_mothballed_fleet_5 = { remove_fleet_flag = nsc_mothballed_fleet_5 }
		nsc_mothballed_fleet_6 = { remove_fleet_flag = nsc_mothballed_fleet_6 }
		nsc_mothballed_fleet_7 = { remove_fleet_flag = nsc_mothballed_fleet_7 }
		nsc_mothballed_fleet_8 = { remove_fleet_flag = nsc_mothballed_fleet_8 }
		nsc_mothballed_fleet_9 = { remove_fleet_flag = nsc_mothballed_fleet_9 }
		nsc_mothballed_fleet_10 = { remove_fleet_flag = nsc_mothballed_fleet_10 }
	}
}