﻿namespace = 1648_ai.events

1648_ai.events.1 = {
	type = country_event
	hidden = yes

	trigger = {}

	immediate = {
		
		# Purely technical stuff not dependant on the country
		
		#ec_check_building_type_array = yes
		#ec_collect_arable_resources = yes
		
		# Perform script if country is allowed or clear country variables otherwise
		
		if = {
			limit = {
				1648_country_allowed = yes
			}
			
			# Applying accelerated construction modifiers to AI countries if needed
			
			#ec_apply_construction_buffs = yes
			
			# Calculate incorporated population and weekly income of the country
			
			ec_calculate_incorporated_population = yes
			ec_calculate_army_size = yes
			ec_calculate_country_budget_surplus = yes
			
			# Collect budget costs and restart costs collection cycle 7 days later
			
			ec_collect_budget_costs = yes
			trigger_event = {
				id = ARoAI_economy_events.4
				days = 7
			}
			
			# Check if military expenses are higher than usual
			
			ec_check_wartime_spending = yes
			
			# Change tax level and government wages
			
			ec_change_budget_settings = yes
			
			# Downsizing and building share a bunch of data both of them require to function
			
			if = {
				limit = {
					OR = {
						ec_downsizing_allowed = yes
						ec_building_allowed = yes
					}
				}
				
				# Here's that data
				
				ec_preliminary_calculations = yes
				ec_collect_state_level_data = yes
				
				# Perform downsizing and immediately clear variables that won't be needed in building
				
				if = {
					limit = {
						ec_downsizing_allowed = yes
					}
					ec_downsize_excess_buildings = yes
				}
				ec_clear_downsize_variables = yes
				
				# Start building if it is allowed or clear that data variables otherwise
				
				if = {
					limit = {
						ec_building_allowed = yes
					}
					
					# Before building anything we always check if there are free construction points
					
					ec_check_construction_points = yes
					
					# First we check if we want to construct some special buildings
					
					if = {
						limit = {
							ec_has_free_construction_points = yes
							ec_regular_building = yes
						}
						ec_construct_special_buildings = yes
					}
					
					# Then we start constructing standard buildings if there are still some free points
					
					ec_check_construction_points = yes
					
					if = {
						limit = {
							ec_has_free_construction_points = yes
						}
						trigger_event = {
							id = ARoAI_economy_events.2
							days = 1
						}
					}
					else = {
						ec_clear_building_variables = yes
					}
				}
				else = {
					ec_clear_building_variables = yes
				}
			}
		}
		else = {
			ec_clear_country_variables = yes
		}
	}
}

ARoAI_economy_events.2 = {
	type = country_event
	hidden = yes
	
	trigger = {}

	immediate = {
		if = {
			limit = {
				ec_country_allowed = no
			}
			ec_clear_country_variables = yes
			ec_clear_building_variables = yes
		}
		else_if = {
			limit = {
				ec_building_allowed = no
			}
			ec_clear_building_variables = yes
		}
		else = {
			ec_prepare_data_for_construction = yes
			trigger_event = {
				id = ARoAI_economy_events.3
				days = 1
			}
		}
	}
}

ARoAI_economy_events.3 = {
	type = country_event
	hidden = yes
	
	trigger = {}
	
	immediate = {
		if = {
			limit = {
				ec_country_allowed = no
			}
			ec_clear_country_variables = yes
			ec_clear_building_variables = yes
		}
		else_if = {
			limit = {
				ec_building_allowed = no
			}
			ec_clear_building_variables = yes
		}
		else = {
			if = {
				limit = {
					NOT = {
						has_variable = ec_building_days
					}
				}
				set_variable = {
					name = ec_building_days
					value = 1
				}
			}
			else = {
				change_variable = {
					name = ec_building_days
					add = 1
				}
			}
			ec_check_construction_points = yes
			if = {
				limit = {
					ec_has_free_construction_points = yes
				}
				ec_prepare_building_construction = yes
			}
			else = {
				if = {
					limit = {
						var:ec_building_days <= 5
					}
					trigger_event = {
						id = ARoAI_economy_events.2
						days = 7
					}
					change_variable = {
						name = ec_building_days
						add = 6
					}
				}
				else = {
					ec_clear_building_variables = yes
				}
			}
		}
	}
}

# constructions costs cycle

ARoAI_economy_events.4 = {
	type = country_event
	hidden = yes
	
	trigger = {}
	
	immediate = {
		if = {
			limit = {
				ec_country_allowed = yes
			}
			ec_clear_budget_cost_variables = yes
			ec_collect_budget_costs = yes
			trigger_event = {
				id = ARoAI_economy_events.5
				days = 7
			}
			trigger_event = {
				id = ARoAI_economy_events.5
				days = 14
			}
		}
		else = {
			ec_clear_country_variables = yes
		}
	}
}

ARoAI_economy_events.5 = {
	type = country_event
	hidden = yes
	
	trigger = {}
	
	immediate = {
		if = {
			limit = {
				ec_country_allowed = yes
			}
			ec_collect_budget_costs = yes
		}
		else = {
			ec_clear_country_variables = yes
		}
	}
}

### AUTOBUILD SETTINGS

ARoAI_economy_events.6 = {
	type = country_event
	placement = root

	title = ARoAI_economy_events.6.t
	desc = ARoAI_economy_events.6.d
	flavor = ARoAI_economy_events.6.f

	duration = 3

	event_image = {
		video = "gfx/event_pictures/unspecific_gears_pistons.bk2"
	}

	on_created_soundeffect = "event:/SFX/UI/Alerts/event_appear"
	on_opened_soundeffect = ""

	icon = "gfx/interface/icons/event_icons/event_industry.dds"

	trigger = {}

	immediate = {
		gb_renew_variable = {
			name = ec_autobuild_settings
			days = 93
		}
	}
	
	option = {
		name = ARoAI_economy_events.6.1a
		trigger = {
			NOT = {
				has_variable = ec_autobuild
			}
		}
		set_variable = ec_autobuild
		trigger_event = {
			id = ARoAI_economy_events.6
			popup = yes
		}
	}
	option = {
		name = ARoAI_economy_events.6.1b
		trigger = {
			has_variable = ec_autobuild
		}
		remove_variable = ec_autobuild
		trigger_event = {
			id = ARoAI_economy_events.6
			popup = yes
		}
	}
	
	option = {
		name = ARoAI_economy_events.6.2a
		trigger = {
			NOT = {
				has_variable = ec_autobuild_government_administration
			}
		}
		set_variable = ec_autobuild_government_administration
		trigger_event = {
			id = ARoAI_economy_events.6
			popup = yes
		}
	}
	option = {
		name = ARoAI_economy_events.6.2b
		trigger = {
			has_variable = ec_autobuild_government_administration
		}
		remove_variable = ec_autobuild_government_administration
		trigger_event = {
			id = ARoAI_economy_events.6
			popup = yes
		}
	}
	
	option = {
		name = ARoAI_economy_events.6.3a
		trigger = {
			NOT = {
				has_variable = ec_autobuild_construction_sector
			}
		}
		set_variable = ec_autobuild_construction_sector
		trigger_event = {
			id = ARoAI_economy_events.6
			popup = yes
		}
	}
	option = {
		name = ARoAI_economy_events.6.3b
		trigger = {
			has_variable = ec_autobuild_construction_sector
		}
		remove_variable = ec_autobuild_construction_sector
		trigger_event = {
			id = ARoAI_economy_events.6
			popup = yes
		}
	}
	
	option = {
		name = ARoAI_economy_events.6.4a
		trigger = {
			NOT = {
				has_variable = ec_autobuild_university
			}
		}
		set_variable = ec_autobuild_university
		trigger_event = {
			id = ARoAI_economy_events.6
			popup = yes
		}
	}
	option = {
		name = ARoAI_economy_events.6.4b
		trigger = {
			has_variable = ec_autobuild_university
		}
		remove_variable = ec_autobuild_university
		trigger_event = {
			id = ARoAI_economy_events.6
			popup = yes
		}
	}
	
	option = {
		name = ARoAI_economy_events.6.5a
		trigger = {
			NOT = {
				has_variable = ec_autobuild_railway
			}
		}
		set_variable = ec_autobuild_railway
		trigger_event = {
			id = ARoAI_economy_events.6
			popup = yes
		}
	}
	option = {
		name = ARoAI_economy_events.6.5b
		trigger = {
			has_variable = ec_autobuild_railway
		}
		remove_variable = ec_autobuild_railway
		trigger_event = {
			id = ARoAI_economy_events.6
			popup = yes
		}
	}
	
	option = {
		name = ARoAI_economy_events.6.6a
		trigger = {
			NOT = {
				has_variable = ec_autobuild_port
			}
		}
		set_variable = ec_autobuild_port
		trigger_event = {
			id = ARoAI_economy_events.6
			popup = yes
		}
	}
	option = {
		name = ARoAI_economy_events.6.6b
		trigger = {
			has_variable = ec_autobuild_port
		}
		remove_variable = ec_autobuild_port
		trigger_event = {
			id = ARoAI_economy_events.6
			popup = yes
		}
	}
	
	option = {
		name = ARoAI_economy_events.6.7a
		trigger = {
			NOT = {
				has_variable = ec_autobuild_barracks
			}
		}
		set_variable = ec_autobuild_barracks
		trigger_event = {
			id = ARoAI_economy_events.6
			popup = yes
		}
	}
	option = {
		name = ARoAI_economy_events.6.7b
		trigger = {
			has_variable = ec_autobuild_barracks
		}
		remove_variable = ec_autobuild_barracks
		trigger_event = {
			id = ARoAI_economy_events.6
			popup = yes
		}
	}
	
	option = {
		name = ARoAI_economy_events.6.8a
		trigger = {
			NOT = {
				has_variable = ec_autobuild_naval_base
			}
		}
		set_variable = ec_autobuild_naval_base
		trigger_event = {
			id = ARoAI_economy_events.6
			popup = yes
		}
	}
	option = {
		name = ARoAI_economy_events.6.8b
		trigger = {
			has_variable = ec_autobuild_naval_base
		}
		remove_variable = ec_autobuild_naval_base
		trigger_event = {
			id = ARoAI_economy_events.6
			popup = yes
		}
	}
	
	option = {
		name = ARoAI_economy_events.6.x
		default_option = yes
		remove_variable = ec_autobuild_settings
	}
}

### CHECK ARABLE RESOURCES

ARoAI_economy_events.300 = {
	type = country_event
	hidden = yes
	
	trigger = {}
	
	immediate = {
		if = {
			limit = {
				has_global_variable = ec_collect_arable_resources
				global_var:ec_collect_arable_resources < 2
			}
			set_global_variable = {
				name = ec_collect_arable_resources
				value = 2
			}
			ec_collect_arables_in_queue = yes
		}
		trigger_event = {
			id = ARoAI_economy_events.301
			days = 1
		}
	}
}

ARoAI_economy_events.301 = {
	type = country_event
	hidden = yes
	
	trigger = {}
	
	immediate = {
		if = {
			limit = {
				has_global_variable = ec_collect_arable_resources
				global_var:ec_collect_arable_resources < 3
			}
			set_global_variable = {
				name = ec_collect_arable_resources
				value = 3
			}
			every_country = {
				every_scope_state = {
					state_region = {
						remove_variable = eccar
					}
				}
			}
		}
	}
}