namespace = New_TradeGood_Events

country_event = {
	id = New_TradeGood_Events.1
	title = New_TradeGood_Events.1.t
	desc = New_TradeGood_Events.1.d
	picture = TRADEGOODS_eventPicture

	fire_only_once = yes	
	major = yes

	trigger = {
		dip_tech = 11
		whaling = 3
		NOT = { has_global_flag = whaling_price }
	}
	
	mean_time_to_happen = {
		months = 1
	}
	
	option = {
		name = New_TradeGood_Events.1.a
		ai_chance = { factor = 100 }
		change_price = {
			trade_goods = whaling
			key = COMMERCIAL_WHALING
			value = 0.15
			duration = -1
		}
		add_years_of_income  = 0.15
		add_prestige = 10
		
		set_global_flag = whaling_price
	}
}

country_event = {
	id = New_TradeGood_Events.2
	title = New_TradeGood_Events.2.t
	desc = New_TradeGood_Events.2.d
	picture = TRADEGOODS_eventPicture

	is_triggered_only = yes
	hidden = yes

	option = {
		name = New_TradeGood_Events.2.a
		ai_chance = { factor = 100 }	
	}
	immediate = {
		if = {
			limit = {
				num_of_subjects = 1
			}
			update_goods_subjects_monthly = { GOODS = copper }
			update_goods_subjects_monthly = { GOODS = tin }
			update_goods_subjects_monthly = { GOODS = iron }
			update_goods_subjects_monthly = { GOODS = lead }
			update_goods_subjects_monthly = { GOODS = zinc }
			update_goods_subjects_monthly = { GOODS = glass }
			update_goods_subjects_monthly = { GOODS = tobacco }
			update_goods_subjects_monthly = { GOODS = cocoa }
		}
		
		if = {
			limit = {
				AND = {
					government = theocracy
					has_reform = monastic_breweries_reform
				}
			}
			
			every_owned_province = {
				if = {
					limit = {
						trade_goods = beer
					}
					add_province_modifier = { 
						name = beer_modifier_bonus
						duration = -1
						hidden = yes
					}
				}
				if = {
					limit = {
						trade_goods = apiculture
					}
					add_province_modifier = { 
						name = honey_modifier_bonus
						duration = -1
						hidden = yes
					}
				}
			}
			set_country_flag = monastic_breweries_reform_TGE
		}
		
		
		if = {
			limit = {
				AND = {
					government = theocracy
					NOT = { has_reform = monastic_breweries_reform }
					has_country_flag = monastic_breweries_reform_TGE
				}
			}
			
			every_owned_province = {
				limit = {
					OR = {
						has_province_modifier = beer_modifier_bonus
						has_province_modifier = honey_modifier_bonus
					}
				}
				remove_province_modifier = beer_modifier_bonus
				remove_province_modifier = honey_modifier_bonus
			}
			
			clr_country_flag = monastic_breweries_reform_TGE
		}
		
		update_goods = { GOODS = copper }
		update_goods = { GOODS = tin }
		update_goods = { GOODS = iron }
		update_goods = { GOODS = lead }
		update_goods = { GOODS = zinc }
		update_goods = { GOODS = glass }
		update_goods = { GOODS = tobacco }
		update_goods = { GOODS = cocoa }
		

		
		if = {
			limit = {
				NOT = {
					AND = {
						check_variable = {
							which = copper_total 
							which = copper_used
						}
						
						check_variable = {
							which = tin_total 
							which = tin_used
						}
					
					}
				}
			}
			set_variable = {
				which = var1
				value = 0
			}
			
			while = {
				limit = {
					NOT = {
						is_variable_equal = {
							which = var1
							which = cannon_foundry_amount
						}
					}
				}
				random_owned_province = { 	
					limit = { 
						has_building = cannon_foundry
					}
					add_building = off
					owner = {
						change_variable = {            
							which = copper
							value = 0.4
						}
						subtract_variable = {            
							which = copper_used
							value = 0.4
						}
						
						change_variable = {            
							which = tin
							value = 0.3
						}
						subtract_variable = {            
							which = tin_used
							value = 0.3
						}
					}
					
					if = {
						limit = {
							owner = { ai = yes }
						}
						remove_building = off
						remove_building = cannon_foundry
						clr_province_flag = has_cannon
						clr_province_flag = has_latent_good
						clr_province_flag = back_to_off
				        goods_back = yes	
						
						owner = {
							subtract_variable = {            
								which = cannon_foundry_amount
								value = 1
							}
					
							country_event = { 
								id = New_TradeGood_Events.4
								days = 2
							}
						}
					}
				}
				change_variable = {            
					which = var1 
					value = 1 
				}	
			}
		}
		
		if = {
			limit = {
				NOT = {
					AND = {
						check_variable = {
							which = copper_total 
							which = copper_used
						}
						
						check_variable = {
							which = zinc_total 
							which = zinc_used
						}
					
					}
				}
			}
			set_variable = {
				which = var4
				value = 0
			}
			
			while = {
				limit = {
					NOT = {
						is_variable_equal = {
							which = var4
							which = steam_engine_manu_amount
						}
					}
				}
				random_owned_province = { 	
					limit = { 
						has_building = steam_engine_manu
					}
					add_building = off_4
					owner = {
						change_variable = {            
							which = copper
							value = 0.4
						}
						subtract_variable = {            
							which = copper_used
							value = 0.4
						}
						
						change_variable = {            
							which = zinc
							value = 0.3
						}
						subtract_variable = {            
							which = zinc_used
							value = 0.3
						}
					}
					
					if = {
						limit = {
							owner = { ai = yes }
						}
						remove_building = off_4
						remove_building = steam_engine_manu
						clr_province_flag = has_steam_engine
						clr_province_flag = has_latent_good
						clr_province_flag = back_to_off_4
				        goods_back = yes	
						
						owner = {
							subtract_variable = {            
								which = steam_engine_manu_amount
								value = 1
							}
					
							country_event = { 
								id = New_TradeGood_Events.7
								days = 2
							}
						}
					}
				}
				change_variable = {            
					which = var4 
					value = 1 
				}	
			}
		}
		
		
		if = {
			limit = {
				NOT = {
					AND = {
						check_variable = {
							which = iron_total 
							which = iron_used
						}	
					}
				}
			}
			set_variable = {
				which = var2
				value = 0
			}
			
			while = {
				limit = {
					NOT = {
						is_variable_equal = {
							which = var2
							which = metalworking_workshop_amount
						}
					}
				}
				random_owned_province = { 	
					limit = { 
						has_building = metalworking_workshop
					}
					add_building = off_2
					owner = {
						change_variable = {            
							which = iron
							value = 1.0
						}
						subtract_variable = {            
							which = iron_used
							value = 1.0
						}
					}
					
					if = {
						limit = {
							owner = { ai = yes }
						}
						remove_building = off_2
						remove_building = metalworking_workshop
						clr_province_flag = has_metalworking
						clr_province_flag = has_latent_good
						clr_province_flag = back_to_off_2
				        goods_back = yes	
						
						owner = {
							subtract_variable = {            
								which = metalworking_workshop_amount
								value = 1
							}
					
							country_event = { 
								id = New_TradeGood_Events.5
								days = 2
							}
						}
					}
				}
				change_variable = {            
					which = var2 
					value = 1 
				}	
			}
		}
		
		
		if = {
			limit = {
				NOT = {
					AND = {
						check_variable = {
							which = lead_total 
							which = lead_used
						}	
					}
				}
			}
			set_variable = {
				which = var3
				value = 0
			}
			
			while = {
				limit = {
					NOT = {
						is_variable_equal = {
							which = var3
							which = munition_factory_amount
						}
					}
				}
				random_owned_province = { 	
					limit = { 
						has_building = munition_factory
					}
					add_building = off_3
					owner = {
						change_variable = {            
							which = lead
							value = 1.0
						}
						subtract_variable = {            
							which = lead_used
							value = 1.0
						}
					}
					
					
					if = {
						limit = {
							owner = { ai = yes }
						}
						remove_building = off_3
						remove_building = munition_factory
						clr_province_flag = has_munitions
						clr_province_flag = has_latent_good
						clr_province_flag = back_to_off_3
				        goods_back = yes	
						
						owner = {
							subtract_variable = {            
								which = munition_factory_amount
								value = 1
							}
					
							country_event = { 
								id = New_TradeGood_Events.6
								days = 2
							}
						}
					}
				}
				change_variable = {            
					which = var3 
					value = 1 
				}	
			}
		}
		
		if = {
			limit = {
				NOT = {
					AND = {
						check_variable = {
							which = glass_total 
							which = glass_used
						}	
					}
				}
			}
			set_variable = {
				which = var5
				value = 0
			}
			
			while = {
				limit = {
					NOT = {
						is_variable_equal = {
							which = var5
							which = spectacle_factory_amount
						}
					}
				}
				random_owned_province = { 	
					limit = { 
						has_building = spectacle_factory
					}
					add_building = off_5
					owner = {
						change_variable = {            
							which = glass
							value = 0.6
						}
						subtract_variable = {            
							which = glass_used
							value = 0.6
						}
					}
					
					if = {
						limit = {
							owner = { ai = yes }
						}
						remove_building = off_5
						remove_building = spectacle_factory
						clr_province_flag = has_optics
						clr_province_flag = has_latent_good
						clr_province_flag = back_to_off_5
				        goods_back = yes	
						
						owner = {
							subtract_variable = {            
								which = spectacle_factory_amount
								value = 1
							}
					
							country_event = { 
								id = New_TradeGood_Events.8
								days = 2
							}
						}
					}
				}
				change_variable = {            
					which = var5 
					value = 1 
				}	
			}
		}
		
		if = {
			limit = {
				NOT = {
					AND = {
						check_variable = {
							which = tobacco_total 
							which = tobacco_used
						}	
					}
				}
			}
			set_variable = {
				which = var6
				value = 0
			}
			
			while = {
				limit = {
					NOT = {
						is_variable_equal = {
							which = var6
							which = cigars_factory_amount
						}
					}
				}
				random_owned_province = { 	
					limit = { 
						has_building = cigars_factory
					}
					add_building = off_6
					owner = {
						change_variable = {            
							which = tobacco
							value = 0.6
						}
						subtract_variable = {            
							which = tobacco_used
							value = 0.6
						}
					}
					
					if = {
						limit = {
							owner = { ai = yes }
						}
						remove_building = off_6
						remove_building = cigars_factory
						clr_province_flag = has_cigars
						clr_province_flag = has_latent_good
						clr_province_flag = back_to_off_6
				        goods_back = yes	
						
						owner = {
							subtract_variable = {            
								which = cigars_factory_amount
								value = 1
							}
					
							country_event = { 
								id = New_TradeGood_Events.9
								days = 2
							}
						}
					}
				}
				change_variable = {            
					which = var6
					value = 1 
				}	
			}
		}
		
		if = {
			limit = {
				NOT = {
					AND = {
						check_variable = {
							which = cocoa_total 
							which = cocoa_used
						}	
					}
				}
			}
			set_variable = {
				which = var7
				value = 0
			}
			
			while = {
				limit = {
					NOT = {
						is_variable_equal = {
							which = var7
							which = chocolate_factory_amount
						}
					}
				}
				random_owned_province = { 	
					limit = { 
						has_building = chocolate_factory
					}
					add_building = off_7
					owner = {
						change_variable = {            
							which = cocoa
							value = 0.6
						}
						subtract_variable = {            
							which = cocoa_used
							value = 0.6
						}
					}
					
					if = {
						limit = {
							owner = { ai = yes }
						}
						remove_building = off_7
						remove_building = chocolate_factory
						clr_province_flag = has_chocolate
						clr_province_flag = has_latent_good
						clr_province_flag = back_to_off_7
				        goods_back = yes	
						
						owner = {
							subtract_variable = {            
								which = chocolate_factory_amount
								value = 1
							}
					
							country_event = { 
								id = New_TradeGood_Events.10
								days = 2
							}
						}
					}
				}
				change_variable = {            
					which = var7
					value = 1 
				}	
			}
		}
	}
}

province_event = {
	id = New_TradeGood_Events.3
	title = New_TradeGood_Events.3.t
	desc = New_TradeGood_Events.3.d
	picture = TRADEGOODS_eventPicture

	is_triggered_only = yes
	hidden = yes

	option = {
		name = New_TradeGood_Events.3.a
		ai_chance = { factor = 100 }
	}
	immediate = {
		1 = {
			province_event = { id = New_TradeGood_Events.3 days = 365 }
		}
		if = {
			limit = { NOT = { has_global_flag = New_TradeGood_First  } }

			every_country = { 
				set_variable = {
					which = copper_used
					value = 0
				}	
				
				set_variable = {
					which = tin_used
					value = 0
				}
				
				set_variable = {
					which = iron_used
					value = 0
				}
				
				set_variable = {
					which = lead_used
					value = 0
				}
				
				set_variable = {
					which = zinc_used
					value = 0
				}
				
				set_variable = {
					which = glass_used
					value = 0
				}
				
				set_variable = {
					which = tobacco_used
					value = 0
				}
				
				set_variable = {
					which = cocoa_used
					value = 0
				}
				
				set_variable = {
					which = cannon_foundry_amount
					value = 0
				}

				set_variable = {
					which = metalworking_workshop_amount
					value = 0
				}	
				
				set_variable = {
					which = munition_factory_amount
					value = 0
				}
				
				set_variable = {
					which = steam_engine_manu_amount
					value = 0
				}
				
				set_variable = {
					which = spectacle_factory_amount
					value = 0
				}
				
				set_variable = {
					which = cigars_factory_amount
					value = 0
				}
				
				set_variable = {
					which = chocolate_factory_amount
					value = 0
				}
				
				## NEW TEST STUFF
				set_variable = {
					which = from_subjects_copper
					value = 0
				}
				
				set_variable = {
					which = from_subjects_tin
					value = 0
				}
				
				set_variable = {
					which = from_subjects_iron
					value = 0
				}
				
				set_variable = {
					which = from_subjects_lead
					value = 0
				}
				
				set_variable = {
					which = from_subjects_zinc
					value = 0
				}
				
				set_variable = {
					which = from_subjects_glass
					value = 0
				}
				
				set_variable = {
					which = from_subjects_tobacco
					value = 0
				}
				
				set_variable = {
					which = from_subjects_cocoa
					value = 0
				}

				country_event = { id = New_TradeGood_Events.2 days = 1 }
			}
			set_global_flag = New_TradeGood_First
			
		}
		every_country = { 
			limit = {
				OR = {
					copper = 1 
					tin = 1
					iron = 1
					lead = 1 
					zinc = 1
					glass = 1
					tobacco = 1
					cocoa = 1
					any_owned_province = { 
						OR = {
							has_province_modifier = tin_mine
							has_province_modifier = tin_mine_small
							has_province_modifier = copper_mine
							has_province_modifier = copper_mine_small
							has_province_modifier = iron_mine
							has_province_modifier = iron_mine_small
							has_province_modifier = zinc_mine
							has_province_modifier = lead_mine
							has_province_modifier = zinc_mine_small
							has_province_modifier = lead_mine_small
						}
					}
				}
			}
			if = {
				limit = { NOT = { ai = yes } }
				country_event = { id = New_TradeGood_Events.2 days = 120 }
				country_event = { id = New_TradeGood_Events.2 days = 240 }
				country_event = { id = New_TradeGood_Events.2 days = 360 }
			}
			else = {
				country_event = { id = New_TradeGood_Events.2 days = 360 }
			}
		}
		
	}
}

country_event = {
	id = New_TradeGood_Events.4
	title = New_TradeGood_Events.4.t
	desc = New_TradeGood_Events.4.d
	picture = TRADEGOODS_eventPicture

	is_triggered_only = yes
	hidden = yes

	option = {
		name = New_TradeGood_Events.4.a
		ai_chance = { factor = 100 }	
	}
	immediate = {
		update_goods = { GOODS = copper }
		update_goods = { GOODS = tin }
		
		if = {
			limit = {
				any_owned_province = { 
					has_province_flag = back_to_off
				}
			}
			every_owned_province = { 
				limit = {
					has_province_flag = back_to_off
				}
				add_building = off
			}
		}
	}
}

country_event = {
	id = New_TradeGood_Events.5
	title = New_TradeGood_Events.5.t
	desc = New_TradeGood_Events.5.d
	picture = TRADEGOODS_eventPicture

	is_triggered_only = yes
	hidden = yes

	option = {
		name = New_TradeGood_Events.5.a
		ai_chance = { factor = 100 }	
	}
	immediate = {
		update_goods = { GOODS = iron }
		
		if = {
			limit = {
				any_owned_province = { 
					has_province_flag = back_to_off_2
				}
			}
			every_owned_province = { 
				limit = {
					has_province_flag = back_to_off_2
				}
				add_building = off_2
			}
		}
	}
}

country_event = {
	id = New_TradeGood_Events.6
	title = New_TradeGood_Events.6.t
	desc = New_TradeGood_Events.6.d
	picture = TRADEGOODS_eventPicture

	is_triggered_only = yes
	hidden = yes

	option = {
		name = New_TradeGood_Events.6.a
		ai_chance = { factor = 100 }	
	}
	immediate = {
		update_goods = { GOODS = lead }
		
		if = {
			limit = {
				any_owned_province = { 
					has_province_flag = back_to_off_3
				}
			}
			every_owned_province = { 
				limit = {
					has_province_flag = back_to_off_3
				}
				add_building = off_3
			}
		}
	}
}

country_event = {
	id = New_TradeGood_Events.7
	title = New_TradeGood_Events.7.t
	desc = New_TradeGood_Events.7.d
	picture = TRADEGOODS_eventPicture

	is_triggered_only = yes
	hidden = yes

	option = {
		name = New_TradeGood_Events.7.a
		ai_chance = { factor = 100 }	
	}
	immediate = {
		update_goods = { GOODS = zinc }
		update_goods = { GOODS = copper }
		
		if = {
			limit = {
				any_owned_province = { 
					has_province_flag = back_to_off_4
				}
			}
			every_owned_province = { 
				limit = {
					has_province_flag = back_to_off_4
				}
				add_building = off_4
			}
		}
	}
}

country_event = {
	id = New_TradeGood_Events.8
	title = New_TradeGood_Events.8.t
	desc = New_TradeGood_Events.8.d
	picture = TRADEGOODS_eventPicture

	is_triggered_only = yes
	hidden = yes

	option = {
		name = New_TradeGood_Events.8.a
		ai_chance = { factor = 100 }	
	}
	immediate = {
		update_goods = { GOODS = glass }
		
		if = {
			limit = {
				any_owned_province = { 
					has_province_flag = back_to_off_5
				}
			}
			every_owned_province = { 
				limit = {
					has_province_flag = back_to_off_5
				}
				add_building = off_5
			}
		}
	}
}


country_event = {
	id = New_TradeGood_Events.9
	title = New_TradeGood_Events.9.t
	desc = New_TradeGood_Events.9.d
	picture = TRADEGOODS_eventPicture

	is_triggered_only = yes
	hidden = yes

	option = {
		name = New_TradeGood_Events.9.a
		ai_chance = { factor = 100 }	
	}
	immediate = {
		update_goods = { GOODS = tobacco }
		
		if = {
			limit = {
				any_owned_province = { 
					has_province_flag = back_to_off_6
				}
			}
			every_owned_province = { 
				limit = {
					has_province_flag = back_to_off_6
				}
				add_building = off_6
			}
		}
	}
}

country_event = {
	id = New_TradeGood_Events.10
	title = New_TradeGood_Events.10.t
	desc = New_TradeGood_Events.10.d
	picture = TRADEGOODS_eventPicture

	is_triggered_only = yes
	hidden = yes

	option = {
		name = New_TradeGood_Events.10.a
		ai_chance = { factor = 100 }	
	}
	immediate = {
		update_goods = { GOODS = cocoa }
		
		if = {
			limit = {
				any_owned_province = { 
					has_province_flag = back_to_off_7
				}
			}
			every_owned_province = { 
				limit = {
					has_province_flag = back_to_off_7
				}
				add_building = off_7
			}
		}
	}
}




country_event = {
	id = New_TradeGood_Events.50
	title = New_TradeGood_Events.50.t
	desc = New_TradeGood_Events.50.d
	picture = TRADEGOODS_eventPicture

	is_triggered_only = yes
	hidden = yes

	option = {
		name = New_TradeGood_Events.50.a
		ai_chance = { factor = 100 }	
	}
	immediate = {
		
		if = {
			limit = {
				any_owned_province = { 
					has_province_modifier = import_from_subjects_modifier
				}
			}
			every_owned_province = { 
				limit = {
					has_province_modifier = import_from_subjects_modifier
				}
				remove_province_modifier = import_from_subjects_modifier
			}
		}
	}
}




