lhe_offmap_chapter_logic = { ##can't touch the call to bos_offmap_chapter_logic, but I can gut it!
	#BLS
	if = {
		limit = {
			bos_ecr_is_enabled = no
		}	
		random_list = {
			#Shephard or BoS
			15 = {
				country_event = {
					id = nf_brotherhood_off_map_chapter_logic.1 #Shephard win
					days = 365
				}
			}
			85 = {
				#Enclave or BoS
				random_list = {
					25 = {
						country_event = {
							id = nf_brotherhood_off_map_chapter_logic.2 #Enclave win
							days = 1095
						}
					}
					75 = {
						country_event = {
							id = nf_brotherhood_off_map_chapter_logic.3 #Project Purity
							days = 1095
						}
						#Outcasts, Arthur or Sarah
						random_list = {
							50 = {
								country_event = {
									id = nf_brotherhood_off_map_chapter_logic.4 # #Sarah or Arthur
									days = 1825
								}
								random_list = {
									50 = {
										country_event = {
											id = nf_brotherhood_off_map_chapter_logic.5 #Boston
											days = 2920
										}
									}
									50 = {
										country_event = {
											id = nf_brotherhood_off_map_chapter_logic.6 #Institute
											days = 2920
										}
									}
								}
							}
							50 = {
								country_event = {
									id = nf_brotherhood_off_map_chapter_logic.7 #Outcasts
									days = 1825
								}
								random_list = {
									50 = {
										country_event = {
											id = nf_brotherhood_off_map_chapter_logic.5 #Boston
											days = 2920
										}
									}
									50 = {
										country_event = {
											id = nf_brotherhood_off_map_chapter_logic.6 #Institute
											days = 2920
										}
									}
								}
							}
						}
					}
				}
			}
		}
	}
	if = {
			limit = {
				bos_rr_is_enabled = no
			}
		#BMW
		random_list = {
			50 = {
				country_event = {
					id = nf_brotherhood_off_map_chapter_logic.8 #bmw lost
					days = 1095
				}
			}
			50 = {
				country_event = {
					id = nf_brotherhood_off_map_chapter_logic.9 #bmw won
					days = 1095
				}
			}
		}
	}
	if = {
		limit = {
			bos_fod_is_enabled = no
		}
		#BGF
		random_list = {
			25 = {
				country_event = {
					id = nf_brotherhood_off_map_chapter_logic.10 #bgf won
					days = 547
				}
			}
			75 = {
				country_event = {
					id = nf_brotherhood_off_map_chapter_logic.11 #bgf lost
					days = 547
				}
			}
		}
	}
}

bos_add_knowledge = {
		custom_effect_tooltip = bos_scribes_x_knowledge_gained
		add_to_variable = {
			bos_scribes_knowledge = BOS_knowledge_gain
		}
}
##Hoi4 doesn't allow duplicate activations of the same mission. What will happen is the time for a mission is reset, effectively "eating" the paladin.
##To get around this, just return to paladin early. Two other solutions were considered: Each focus having a unique set of missions (huge bloat) or having"fallback" missions scripted in this way so there's a second mission that different under the hood.
##Two would still not guarantee it as impossible for something to get eaten, and three has the same bloat problem....
bos_smart_delayed_expedition_small = {
	add_to_variable = {
		var = bos_paladins_ready
		value = -1
	}
	if = {
		limit={
			has_active_mission = bos_delayed_expedition_small
		}
		hidden_effect = {
			add_to_variable = {
				var = bos_paladins_ready
				value = +1
				tooltip = paladin_math_tooltip
			}
			remove_mission = bos_delayed_expedition_small
		}
	}
	activate_mission = bos_delayed_expedition_small
}
bos_smart_delayed_expedition_medium = {
	add_to_variable = {
		var = bos_paladins_ready
		value = -1
	}
	if = {
		limit={
			has_active_mission = bos_delayed_expedition_medium
		}
		hidden_effect = {
			add_to_variable = {
				var = bos_paladins_ready
				value = +1
				tooltip = paladin_math_tooltip
			}
			remove_mission = bos_delayed_expedition_medium
		}
	}
	activate_mission = bos_delayed_expedition_medium
}
bos_smart_delayed_expedition_large = { ##Currently not used
	add_to_variable = {
		var = bos_paladins_ready
		value = -1
	}
	if = {
		limit={
			has_active_mission = bos_delayed_expedition_large
		}
		hidden_effect = {
			add_to_variable = {
				var = bos_paladins_ready
				value = +1
				tooltip = paladin_math_tooltip
			}
			remove_mission = bos_delayed_expedition_large
		}
	}
	activate_mission = bos_delayed_expedition_large
}
bos_smart_delayed_expedition2_small = {
	add_to_variable = {
		var = bos_paladins_ready
		value = -2
	}
	if = {
		limit={
			has_active_mission = bos_delayed_expedition2_small
		}
		hidden_effect = {
			add_to_variable = {
				var = bos_paladins_ready
				value = +2
				tooltip = paladin_math_tooltip
			}
			remove_mission = bos_delayed_expedition2_small
		}
	}
	activate_mission = bos_delayed_expedition2_small
}
bos_smart_delayed_expedition2_medium = {
	add_to_variable = {
		var = bos_paladins_ready
		value = -2
	}
	if = {
		limit={
			has_active_mission = bos_delayed_expedition2_medium
		}
		hidden_effect = {
			add_to_variable = {
				var = bos_paladins_ready
				value = +2
				tooltip = paladin_math_tooltip
			}
			remove_mission = bos_delayed_expedition2_medium
		}
	}
	activate_mission = bos_delayed_expedition2_medium
}
bos_smart_delayed_expedition2_large = { ##Currently not used
	add_to_variable = {
		var = bos_paladins_ready
		value = -2
	}
	if = {
		limit={
			has_active_mission = bos_delayed_expedition2_large
		}
		hidden_effect = {
			add_to_variable = {
				var = bos_paladins_ready
				value = +2
				tooltip = paladin_math_tooltip
			}
			remove_mission = bos_delayed_expedition2_large
		}
	}
	activate_mission = bos_delayed_expedition2_large
}
bos_smart_delayed_scribes_small = {
	add_to_variable = {
		var = bos_scribes_ready
		value = -1
	}
	if = {
		limit = {
			has_active_mission = bos_delayed_scribes_small
		}
		hidden_effect = {
			add_to_variable = {
				var = bos_scribes_ready
				value = +1
				tooltip = scribe_math_tooltip
			}
			remove_mission = bos_delayed_scribes_small
		}
	}
	activate_mission = bos_delayed_scribes_small
}
bos_smart_delayed_scribes_medium = {
	add_to_variable = {
		var = bos_scribes_ready
		value = -1
	}
	if = {
		limit={
			has_active_mission = bos_delayed_scribes_medium
		}
		hidden_effect = {
			add_to_variable = {
				var = bos_scribes_ready
				value = +1
				tooltip = scribe_math_tooltip
			}
			remove_mission = bos_delayed_scribes_medium
		}
	}
	activate_mission = bos_delayed_scribes_medium
}
bos_smart_delayed_scribes_large = {
	add_to_variable = {
		var = bos_scribes_ready
		value = -1
	}
	if = {
		limit={
			has_active_mission = bos_delayed_scribes_large
		}
		hidden_effect = {
			add_to_variable = {
				var = bos_scribes_ready
				value = +1
				tooltip = scribe_math_tooltip
			}
			remove_mission = bos_delayed_scribes_large
		}
	}
activate_mission = bos_delayed_scribes_large
}
bos_upgrade_expeditionary_outposts = {
	if = {
		limit = {
			OR = {
				has_idea = bos_expeditionary_outposts_5
				has_idea = bos_expeditionary_outposts_6
			}
		}
		swap_ideas = {
			remove_idea = bos_expeditionary_outposts_5
			add_idea = bos_expeditionary_outposts_6
		}
	}
	else_if = {
		limit = {
			has_idea = bos_expeditionary_outposts_4
		}
		swap_ideas = {
			remove_idea = bos_expeditionary_outposts_4
			add_idea = bos_expeditionary_outposts_5
		}
	} 
	else_if = {
		limit = {
			has_idea = bos_expeditionary_outposts_3
		}
		swap_ideas = {
			remove_idea = bos_expeditionary_outposts_3
			add_idea = bos_expeditionary_outposts_4
		}
	}
	else_if = {
		limit = {
			has_idea = bos_expeditionary_outposts_2
		}
		swap_ideas = {
			remove_idea = bos_expeditionary_outposts_2
			add_idea = bos_expeditionary_outposts_3
		}
	}
	else_if = {
		limit = {
			has_idea = bos_expeditionary_outposts_1
		}
		swap_ideas = {
			remove_idea = bos_expeditionary_outposts_1
			add_idea = bos_expeditionary_outposts_2
		}
	}
	else = {
		add_ideas = bos_expeditionary_outposts_1
	}
}
bos_upgrade_foreign_airbases = {
	if = {
		limit = {
			OR = {
				has_idea = bos_foreign_airbases_9
				has_idea = bos_foreign_airbases_10
			}
		}
		swap_ideas = {
			remove_idea = bos_foreign_airbases_9
			add_idea = bos_foreign_airbases_10
		}
	}
	else_if = {
		limit = {
			OR = {
				has_idea = bos_foreign_airbases_8
			}
		}
		swap_ideas = {
			remove_idea = bos_foreign_airbases_8
			add_idea = bos_foreign_airbases_9
		}
	}
	else_if = {
		limit = {
			OR = {
				has_idea = bos_foreign_airbases_7
			}
		}
		swap_ideas = {
			remove_idea = bos_foreign_airbases_7
			add_idea = bos_foreign_airbases_8
		}
	}
	else_if = {
		limit = {
			OR = {
				has_idea = bos_foreign_airbases_6
			}
		}
		swap_ideas = {
			remove_idea = bos_foreign_airbases_6
			add_idea = bos_foreign_airbases_7
		}
	}
	else_if = {
		limit = {
			OR = {
				has_idea = bos_foreign_airbases_5
			}
		}
		swap_ideas = {
			remove_idea = bos_foreign_airbases_5
			add_idea = bos_foreign_airbases_6
		}
	}
	else_if = {
		limit = {
			has_idea = bos_foreign_airbases_4
		}
		swap_ideas = {
			remove_idea = bos_foreign_airbases_4
			add_idea = bos_foreign_airbases_5
		}
	} 
	else_if = {
		limit = {
			has_idea = bos_foreign_airbases_3
		}
		swap_ideas = {
			remove_idea = bos_foreign_airbases_3
			add_idea = bos_foreign_airbases_4
		}
	}
	else_if = {
		limit = {
			has_idea = bos_foreign_airbases_2
		}
		swap_ideas = {
			remove_idea = bos_foreign_airbases_2
			add_idea = bos_foreign_airbases_3
		}
	}
	else_if = {
		limit = {
			has_idea = bos_foreign_airbases_1
		}
		swap_ideas = {
			remove_idea = bos_foreign_airbases_1
			add_idea = bos_foreign_airbases_2
		}
	}
	else = {
		add_ideas = bos_foreign_airbases_1
	}
}
bos_srg_upgrade_army_corps = {	
	if = {
		limit = {
			has_idea = srg_steel_rangers_armed_corps_4
		}
		swap_ideas = {
			remove_idea = srg_steel_rangers_armed_corps_4
			add_idea = srg_steel_rangers_armed_corps_5
		}
	}
	else_if = {
		limit = {
			has_idea = srg_steel_rangers_armed_corps_3
		}
		swap_ideas = {
			remove_idea = srg_steel_rangers_armed_corps_3
			add_idea = srg_steel_rangers_armed_corps_4
		}
	}
	else_if = {
		limit = {
			has_idea = srg_steel_rangers_armed_corps_2
		}
		swap_ideas = {
			remove_idea = srg_steel_rangers_armed_corps_2
			add_idea = srg_steel_rangers_armed_corps_3
		}
	}
	else_if = {
		limit = {
			has_idea = srg_steel_rangers_armed_corps_1
		}
		swap_ideas = {
			remove_idea = srg_steel_rangers_armed_corps_1
			add_idea = srg_steel_rangers_armed_corps_2
		}
	}
	else = {
		add_ideas = srg_steel_rangers_armed_corps_1
	}
}
bos_troika_install_augment_in_elder = {
	random_list = {
		1 = {
			custom_effect_tooltip = fallout_augment_21
			hidden_effect = {
			add_country_leader_trait = fallout_augment_21
			}
		}
		1 = {
			custom_effect_tooltip = fallout_augment_20
			hidden_effect = {
			add_country_leader_trait = fallout_augment_20
			}
		}
		1 = {
			custom_effect_tooltip = fallout_augment_19
			hidden_effect = {
			add_country_leader_trait = fallout_augment_19
			}
		}
		1 = {
			custom_effect_tooltip = fallout_augment_18
			hidden_effect = {
			add_country_leader_trait = fallout_augment_18
			}
			# if = { 
				# limit = { 
					# THIS.GetLeader = {##however you refer to a country leader? Can't find this anywhere, if it's possible.
						# is_army_leader = no 
					# }
				# }
				add_field_marshal_role = {
				  character = THIS.GetLeader
				  skill = 3
				  attack_skill = 2
				  defense_skill = 3
				  planning_skill = 3
				  logistics_skill = 3
				}
			# }
		}
		1 = {
			custom_effect_tooltip = fallout_augment_17
			hidden_effect = {
			add_country_leader_trait = fallout_augment_17
			}
		}
		1 = {
			custom_effect_tooltip = fallout_augment_9
			hidden_effect = {
			add_country_leader_trait = fallout_augment_9
			}
		}
		1 = {
			custom_effect_tooltip = fallout_augment_7
			hidden_effect = {
			add_country_leader_trait = fallout_augment_7
			}
		}
	}
}
bos_troika_upgrade_bos_experience_law = {
	if = {
		limit = {
			has_idea = bos_bos_fighting_experience_1
		}
		##do nothing
	}
	else = {
		add_ideas = bos_bos_fighting_experience_1
	}
}
bos_disallow_all_but_troika_and_ruling = {
	custom_effect_tooltip = bos_disallow_all_but_troika_and_ruling_tt
	if = {
		limit = {
			NOT = {
				has_country_flag = bos_intellectuals_allowed
			}
		}
		custom_effect_tooltip = bos_set_codex_law
		custom_effect_tooltip = bos_intellectuals_allowed
		set_country_flag = bos_intellectuals_allowed
	}
	if = {
		limit = {
			AND = {
				has_country_flag = bos_people_allowed
				NOT = {
					has_government = people
				}
			}
		}
		custom_effect_tooltip = bos_remove_codex_law
		custom_effect_tooltip = bos_people_allowed
		clr_country_flag = bos_people_allowed
	}
	if = {
		limit = {
			AND = {
				has_country_flag = bos_elites_allowed
				NOT = {
					has_government = elites
				}
			}
		}
		custom_effect_tooltip = bos_remove_codex_law
		custom_effect_tooltip = bos_elites_allowed
		clr_country_flag = bos_elites_allowed
	}
	if = {
		limit = {
			AND = {
				has_country_flag = bos_ruler_allowed
				NOT = {
					has_government = ruler
				}
			}
		}
		custom_effect_tooltip = bos_remove_codex_law
		custom_effect_tooltip = bos_ruler_allowed
		clr_country_flag = bos_ruler_allowed
	}
}
bos_liberate_all_bos_members = {
	release = BSF
	release = MOJ
	release = GRD
	release = WBH 
	release = TCA
	release = MXC
	release = MNC
	release = TBH
	release = BMW
	release = BAP 
	release = BGF
	release = BLS 
	release = ACH
	# for_each_scope_loop = {
		# array = global.bos_ledger_members
		# if = {
			# limit = {
				# NOT = { tag = ROOT }
				# NOT = { country_exists = PREV}
			# }
			# release = PREV
		# }
	# }
	# for_each_scope_loop = {
		# array = global.bos_ledger_members
		# if = {
			# limit = {
				# NOT = { tag = ROOT }
			# }
			# every_owned_state = {
				# limit = {
					# is_core_of = PREV.PREV
				# }						 	
				# transfer_state = THIS
			# }
		# }
	# }
#BOS BSF MOJ GRD WBH TCA MXC MNC TBH BMW BAP BGF BLS 
}
bos_troikan_loyalist_buff_effect = {
	BOS = {
		set_country_flag = troika_loyalist_chapter ##This could be bypassed with a manual effect, but it is a convenient canary for the effect breaking.
	}
	every_country = { 
		limit = {
			has_country_flag = troika_loyalist_chapter
		}
		if = {
			limit = {
				tag = BOS
			}
			country_event = nf_brotherhood_elh_events.703
		}
		else = {
			country_event = nf_brotherhood_elh_events.701
		}
		every_army_leader = {
			add_trait = { trait = strong_trait }
			add_trait = { trait = perceptive_trait }
			add_trait = { trait = enduring_trait }
			add_trait = { trait = charismatic_trait }
			add_trait = { trait = intelligent_trait }
			add_trait = { trait = agile_trait }
			add_trait = { trait = lucky_trait }
		}
	}
	BOS = {
		clr_country_flag = troika_loyalist_chapter
	}
}
bos_troikan_loyalist_diplomacy_buff_effect = {
	every_country = { 
		limit = {
			has_country_flag = troika_loyalist_chapter
		}
		add_opinion_modifier = {
			target = THIS
			modifier = tca_shared_value
		}
	}
}
bos_troikan_loyalist_self_only_buff_effect = {
	THIS = {
		every_army_leader = {
			add_trait = { trait = strong_trait }
			add_trait = { trait = perceptive_trait }
			add_trait = { trait = enduring_trait }
			add_trait = { trait = charismatic_trait }
			add_trait = { trait = intelligent_trait }
			add_trait = { trait = agile_trait }
			add_trait = { trait = lucky_trait }
		}
	}
}
bos_troikan_only_troika_buff_effect = {
	BOS_manawyddan = {
		add_trait = { trait = urban_assault_specialist }
		add_trait = { trait = fortress_buster }
		if = {
			limit = {
				NOT = {
					has_trait = junk_rounds_trait
				}
				NOT = {
					has_trait = melee_hacker_trait
				}
			}
			add_trait = { trait = melee_hacker_trait }
		}
		
	}
	BOS_rhiannon = {
		add_trait = { trait = expert_improviser }
		add_trait = { trait = logistics_wizard }
		if = {
			limit = {
				NOT = {
					has_trait = flexible_trait
				}
				NOT = {
					has_trait = negotiator_trait
				}
			}
			add_trait = { trait = negotiator_trait }
		}
	}
	BOS_pryderi = {
		add_trait = { trait = hill_fighter }
		add_trait = { trait = trait_mountaineer }
		if = {
			limit = {
				NOT = {
					has_trait = fight_the_power_trait
				}
				NOT = {
					has_trait = sneering_imperialist_trait
				}
			}
			add_trait = { trait = sneering_imperialist_trait }
		}
	}
}
bos_adjust_famine_time = {
	if = {
		limit = {
			has_active_mission = bos_ncr_incoming_famine
		}
		add_days_mission_timeout = {
			mission = bos_ncr_incoming_famine
			days = bos_famine_time
		}
	}
	else_if = {
		limit = {
			has_active_mission = bos_ncr_famine_active
		}
		add_days_mission_timeout = {
			mission = bos_ncr_famine_active
			days = bos_famine_time
		}
	}
	else_if = {
		limit = {
			has_active_mission = bos_ncr_terrible_famine_active
		}
		add_days_mission_timeout = {
			mission = bos_ncr_terrible_famine_active
			days = bos_famine_time
		}
	}
}
bos_reduce_famine = {
	add_to_variable = {
		var = slh_famine_progress
		value = +1
	}
}
bos_improve_bunker_construction = {
	if = {
		limit = {
			NOT = {
				has_variable = BOS_underground_construction_speed_up
			}
		}
		set_variable = {
			BOS_underground_construction_speed_up = 0
		}
	}
	if = {
		limit = {
			check_variable = {
				var = BOS_underground_construction_speed_up
				value = 100
				compare = less_than
			}
		}
		add_to_variable = {
			var = BOS_underground_construction_speed_up
			value = 10
			tooltip = bos_construction_speed_up_math
		}
	}
	if = {
		limit = {
			check_variable = {
				var = BOS_underground_construction_speed_up
				value = 100
				compare = greater_than_or_equals
			}
		}
		set_country_flag = bos_faster_underground_construction
	}
}

bos_invite_to_market = {
	FROM = {
		#set_rule = {desc = bos_capitalism can_access_market = yes}
		add_relation_rule_override = {
			target = BOS
			usage_desc = bos_market_access
			can_access_market = yes
		}
	}
	ROOT = { ##implicit
		set_rule = {
			desc = bos_capitalism 
			can_access_market = yes
		}
	}
	FROM = {
		give_market_access = ROOT
	}
	ROOT = {
		give_market_access = FROM
	}
	FROM = {
		set_country_flag = should_use_international_market
	}
	##Loop through and get market access between all current members.
	for_each_scope_loop = {
		array = global.bos_market_members
		PREV = {
			#set_rule = {desc = bos_capitalism can_access_market = yes}
			add_relation_rule_override = {
				target = FROM
				usage_desc = bos_market_access
				can_access_market = yes
			}
		}
		FROM = { ##implicit
			set_rule = { 
				desc = bos_capitalism 
				can_access_market = yes
			}
		}
		PREV = {
			give_market_access = ROOT
		}
		FROM = {
			give_market_access = PREV
		}
		PREV = {
			set_country_flag = should_use_international_market
		}
	}
	##After giving market access between all members, add this chapter to the array
	add_to_array = { global.bos_market_members = FROM.id}
}

bos_make_chapter_send_volunteers = {
	custom_effect_tooltip = bos_chapter_sends_volunteers_tt
	THIS = {
		add_ai_strategy = {
			type = send_volunteers_desire
			id = BOS
			value = 900
		}
		#add_manpower = 100 --just to check scope
	}
}