# Generic Focus Tree Rework (Updated & Rebalanced)
#
# Implements the "Focus Tree Access" custom game rule (RCP_tree_access).
# on_startup fires once per session (new game AND every save load), so this
# also repairs tree assignment after loading and after countries change
# between player and AI control.
#
# The focus_tree country weight blocks only carry has_country_flag checks
# (RCP_vanilla_tree_ai) - evaluating game rules inside tree weights crashes
# the game during startup, so all rule logic lives here instead.

on_actions = {
	on_startup = {
		effect = {
			if = {
				limit = { has_game_rule = { rule = RCP_tree_access option = PLAYERS_ONLY } }

				#AI nations on the reworked generic tree move to the bundled
				#vanilla generic tree copy and are flagged so the tree weight
				#re-evaluation on save load keeps them there.
				every_country = {
					limit = {
						is_ai = yes
						OR = {
							has_focus_tree = RCP_generic_focus
							has_country_flag = RCP_vanilla_tree_ai
						}
					}
					set_country_flag = RCP_vanilla_tree_ai
					load_focus_tree = { tree = RCP_vanilla_generic_focus keep_completed = yes }
				}

				#A flagged country that is now under player control gets the
				#reworked tree back.
				every_country = {
					limit = {
						is_ai = no
						has_country_flag = RCP_vanilla_tree_ai
					}
					clr_country_flag = RCP_vanilla_tree_ai
					load_focus_tree = { tree = RCP_generic_focus keep_completed = yes }
				}
			}
		}
	}
}
