# Prophetic Dreams
# All effects: scope = country.

# Clears every offer flag.
apb_dreams_clear_offer = {
	remove_country_flag = apb_dream_offer_war
	remove_country_flag = apb_dream_true_war
	remove_country_flag = apb_dream_offer_growth
	remove_country_flag = apb_dream_true_growth
	remove_country_flag = apb_dream_offer_discovery
	remove_country_flag = apb_dream_true_discovery
	remove_country_flag = apb_dream_offer_wealth
	remove_country_flag = apb_dream_true_wealth
	remove_country_flag = apb_dream_offer_industry
	remove_country_flag = apb_dream_true_industry
	remove_country_flag = apb_dream_offer_unity
	remove_country_flag = apb_dream_true_unity
	remove_country_flag = apb_dream_offer_harmony
	remove_country_flag = apb_dream_true_harmony
	remove_country_flag = apb_dream_offer_building
	remove_country_flag = apb_dream_true_building
	remove_country_flag = apb_dream_offer_voyage
	remove_country_flag = apb_dream_true_voyage
	remove_country_flag = apb_dream_patron_offer
}

# Offers vision $V$: nine times in ten as a normal vision, once in ten as a true dream.
apb_dreams_offer = {
	random_list = {
		90 = { set_country_flag = apb_dream_offer_$V$ }
		10 = { set_country_flag = apb_dream_true_$V$ }
	}
}

# Picks one vision that is not already in this offer (weight 0 once offered).
apb_dreams_pick_one = {
	random_list = {
		10 = {
			modifier = {
				factor = 0
				OR = {
					has_country_flag = apb_dream_offer_war
					has_country_flag = apb_dream_true_war
				}
			}
			apb_dreams_offer = { V = war }
		}
		10 = {
			modifier = {
				factor = 0
				OR = {
					has_country_flag = apb_dream_offer_growth
					has_country_flag = apb_dream_true_growth
				}
			}
			apb_dreams_offer = { V = growth }
		}
		10 = {
			modifier = {
				factor = 0
				OR = {
					has_country_flag = apb_dream_offer_discovery
					has_country_flag = apb_dream_true_discovery
				}
			}
			apb_dreams_offer = { V = discovery }
		}
		10 = {
			modifier = {
				factor = 0
				OR = {
					has_country_flag = apb_dream_offer_wealth
					has_country_flag = apb_dream_true_wealth
				}
			}
			apb_dreams_offer = { V = wealth }
		}
		10 = {
			modifier = {
				factor = 0
				OR = {
					has_country_flag = apb_dream_offer_industry
					has_country_flag = apb_dream_true_industry
				}
			}
			apb_dreams_offer = { V = industry }
		}
		10 = {
			modifier = {
				factor = 0
				OR = {
					has_country_flag = apb_dream_offer_unity
					has_country_flag = apb_dream_true_unity
				}
			}
			apb_dreams_offer = { V = unity }
		}
		10 = {
			modifier = {
				factor = 0
				OR = {
					has_country_flag = apb_dream_offer_harmony
					has_country_flag = apb_dream_true_harmony
				}
			}
			apb_dreams_offer = { V = harmony }
		}
		10 = {
			modifier = {
				factor = 0
				OR = {
					has_country_flag = apb_dream_offer_building
					has_country_flag = apb_dream_true_building
				}
			}
			apb_dreams_offer = { V = building }
		}
		10 = {
			modifier = {
				factor = 0
				OR = {
					has_country_flag = apb_dream_offer_voyage
					has_country_flag = apb_dream_true_voyage
				}
			}
			apb_dreams_offer = { V = voyage }
		}
	}
}

# Takes a normal vision.
apb_dreams_take = {
	add_modifier = {
		modifier = apb_dream_$V$
		years = 5
	}
}

# Takes a true dream and pays its price.
apb_dreams_take_true = {
	add_modifier = {
		modifier = apb_dream_$V$_true
		years = 5
	}
	apb_dreams_pay_price = yes
}

# The price of a true dream: a random leader gains a negative trait,
# or the capital suffers 5 years of unrest.
apb_dreams_pay_price = {
	custom_tooltip = apb_dreams_price_tt
	hidden_effect = {
		random_list = {
			50 = {
				modifier = {
					factor = 0
					NOT = {
						any_owned_leader = {
							is_ruler = no
							NOT = { leader_class = envoy }
						}
					}
				}
				random_owned_leader = {
					limit = {
						is_ruler = no
						NOT = { leader_class = envoy }
					}
					add_trait = {
						trait = random_negative
					}
				}
			}
			50 = {
				modifier = {
					factor = 0
					NOT = { exists = capital_scope }
				}
				capital_scope = {
					add_modifier = {
						modifier = apb_dream_price_unrest
						years = 5
					}
				}
			}
			# Fallback so the list never has only zero weights (no eligible leader and no capital)
			1 = {
				modifier = {
					factor = 0
					OR = {
						exists = capital_scope
						any_owned_leader = {
							is_ruler = no
							NOT = { leader_class = envoy }
						}
					}
				}
			}
		}
	}
}
