# Venture Capital (ap_venture_capital). Scope: country.
#
# Three venture slots. Per slot N (1-3), country variables:
#   apb_vc_stake_N    energy staked (> 0 = slot in use)
#   apb_vc_sector_N   1 = mining consortium, 2 = research start-up, 3 = luxury brand
#   apb_vc_outcome_N  1 = bust, 2 = steady, 3 = boom, 4 = unicorn (set when the venture matures)
#   apb_vc_payout_N   energy-equivalent payout (stake x 0 / 1.5 / 3)
# apb_vc_pending: the slot reserved by the open sector-choice event (0 = none free).
# apb_vc_unicorns_S: number of unicorns in sector S (stacks the permanent modifier).

# Reserve the first free slot (apb_venture.1 immediate)
apb_venture_reserve_slot = {
	set_variable = { which = apb_vc_pending value = 0 }
	if = {
		limit = { NOT = { check_variable = { which = apb_vc_stake_1 value > 0 } } }
		set_variable = { which = apb_vc_pending value = 1 }
		set_variable = { which = apb_vc_stake_1 value = value:apb_venture_stake }
	}
	else_if = {
		limit = { NOT = { check_variable = { which = apb_vc_stake_2 value > 0 } } }
		set_variable = { which = apb_vc_pending value = 2 }
		set_variable = { which = apb_vc_stake_2 value = value:apb_venture_stake }
	}
	else_if = {
		limit = { NOT = { check_variable = { which = apb_vc_stake_3 value > 0 } } }
		set_variable = { which = apb_vc_pending value = 3 }
		set_variable = { which = apb_vc_stake_3 value = value:apb_venture_stake }
	}
}

# Launch the reserved venture in sector SECTOR (1-3)
apb_venture_choose = {
	if = {
		limit = { check_variable = { which = apb_vc_pending value = 1 } }
		apb_venture_launch = { SLOT = 1 SECTOR = $SECTOR$ }
	}
	else_if = {
		limit = { check_variable = { which = apb_vc_pending value = 2 } }
		apb_venture_launch = { SLOT = 2 SECTOR = $SECTOR$ }
	}
	else_if = {
		limit = { check_variable = { which = apb_vc_pending value = 3 } }
		apb_venture_launch = { SLOT = 3 SECTOR = $SECTOR$ }
	}
	set_variable = { which = apb_vc_pending value = 0 }
}

apb_venture_launch = {
	set_variable = { which = apb_vc_sector_$SLOT$ value = $SECTOR$ }
	set_variable = { which = apb_vc_outcome_$SLOT$ value = 0 }
	country_event = { id = apb_venture.1$SLOT$ days = 1800 }
}

# Decide how the venture in SLOT turned out (resolution event immediate).
# A ruler with skill 4+ and again 7+ makes busts rarer and booms and unicorns likelier.
apb_venture_roll = {
	set_variable = { which = apb_vc_payout_$SLOT$ value = apb_vc_stake_$SLOT$ }
	random_list = {
		20 = {
			modifier = {
				factor = 0.75
				exists = ruler
				ruler = { has_total_skill >= 4 }
			}
			modifier = {
				factor = 0.67
				exists = ruler
				ruler = { has_total_skill >= 7 }
			}
			set_variable = { which = apb_vc_outcome_$SLOT$ value = 1 }
			set_variable = { which = apb_vc_payout_$SLOT$ value = 0 }
		}
		45 = {
			set_variable = { which = apb_vc_outcome_$SLOT$ value = 2 }
			multiply_variable = { which = apb_vc_payout_$SLOT$ value = 1.5 }
		}
		25 = {
			modifier = {
				factor = 1.2
				exists = ruler
				ruler = { has_total_skill >= 4 }
			}
			modifier = {
				factor = 1.2
				exists = ruler
				ruler = { has_total_skill >= 7 }
			}
			set_variable = { which = apb_vc_outcome_$SLOT$ value = 3 }
			multiply_variable = { which = apb_vc_payout_$SLOT$ value = 3 }
		}
		10 = {
			modifier = {
				factor = 1.25
				exists = ruler
				ruler = { has_total_skill >= 4 }
			}
			modifier = {
				factor = 1.25
				exists = ruler
				ruler = { has_total_skill >= 7 }
			}
			set_variable = { which = apb_vc_outcome_$SLOT$ value = 4 }
			multiply_variable = { which = apb_vc_payout_$SLOT$ value = 3 }
		}
	}
}

# Pay out the venture in SLOT and free the slot (resolution event option)
apb_venture_payout = {
	if = {
		limit = { check_variable = { which = apb_vc_outcome_$SLOT$ value >= 2 } }
		if = {
			limit = { check_variable = { which = apb_vc_sector_$SLOT$ value = 1 } }
			add_resource = {
				minerals = 0.5
				alloys = 0.25
				mult = apb_vc_payout_$SLOT$
			}
		}
		else_if = {
			limit = { check_variable = { which = apb_vc_sector_$SLOT$ value = 2 } }
			add_resource = {
				physics_research = 0.25
				society_research = 0.25
				engineering_research = 0.25
				mult = apb_vc_payout_$SLOT$
			}
		}
		else = {
			add_resource = {
				trade = 0.6
				unity = 0.25
				mult = apb_vc_payout_$SLOT$
			}
		}
	}
	if = {
		limit = { check_variable = { which = apb_vc_outcome_$SLOT$ value = 3 } }
		if = {
			limit = { check_variable = { which = apb_vc_sector_$SLOT$ value = 1 } }
			add_modifier = { modifier = apb_vc_boom_mining days = 3600 }
		}
		else_if = {
			limit = { check_variable = { which = apb_vc_sector_$SLOT$ value = 2 } }
			add_modifier = { modifier = apb_vc_boom_research days = 3600 }
		}
		else = {
			add_modifier = { modifier = apb_vc_boom_luxury days = 3600 }
		}
	}
	if = {
		limit = { check_variable = { which = apb_vc_outcome_$SLOT$ value = 4 } }
		if = {
			limit = { check_variable = { which = apb_vc_sector_$SLOT$ value = 1 } }
			apb_venture_add_unicorn = { SECTOR = mining }
		}
		else_if = {
			limit = { check_variable = { which = apb_vc_sector_$SLOT$ value = 2 } }
			apb_venture_add_unicorn = { SECTOR = research }
		}
		else = {
			apb_venture_add_unicorn = { SECTOR = luxury }
		}
	}
	hidden_effect = {
		set_variable = { which = apb_vc_stake_$SLOT$ value = 0 }
		set_variable = { which = apb_vc_sector_$SLOT$ value = 0 }
		set_variable = { which = apb_vc_outcome_$SLOT$ value = 0 }
		set_variable = { which = apb_vc_payout_$SLOT$ value = 0 }
	}
}

# Permanent unicorn bonus: one more stack of apb_vc_unicorn_SECTOR
apb_venture_add_unicorn = {
	hidden_effect = {
		if = {
			limit = { NOT = { is_variable_set = apb_vc_unicorns_$SECTOR$ } }
			set_variable = { which = apb_vc_unicorns_$SECTOR$ value = 0 }
		}
		change_variable = { which = apb_vc_unicorns_$SECTOR$ value = 1 }
		if = {
			limit = { has_modifier = apb_vc_unicorn_$SECTOR$ }
			remove_modifier = apb_vc_unicorn_$SECTOR$
		}
	}
	custom_tooltip = apb_vc_unicorn_$SECTOR$_tt
	hidden_effect = {
		add_modifier = {
			modifier = apb_vc_unicorn_$SECTOR$
			days = -1
			multiplier = apb_vc_unicorns_$SECTOR$
		}
	}
}
