# Warrior Lodges decisions
# By Milla Isaksson

targeted_decisions = {
	duel_decision = {
		diplomacy_icon = GFX_duel_decision

		ai_check_interval = 10

		is_high_prio = yes

		only_playable = yes

		from_potential = {
			OR = {
				has_focus = focus_war # Way of Life

				trigger_if = {
					limit = { has_dlc = "Holy Fury" }

					OR = {
						is_member_of_any_warrior_lodge_trigger = yes

						any_bloodline = {
							has_bloodline_flag = duelist_bloodline
							bloodline_is_active_for = PREV
						}
					}
				}
			}

			is_adult = yes
			prisoner = no
			has_regent = no

			trigger_if = {
				limit = {
					is_female = yes
					ai = yes
				}

				OR = {
					trait = brave
					trait = shieldmaiden
					trait = martial_lady
					trait = martial_lady_tengri
					trait = martial_lady_pagan
					religion_group = pagan_group
					is_nomadic = yes
					religion = buddhist
					religion = bogomilist
					religion = mazdaki
					religion = tondrakian
					gender_equality_trigger = yes
				}
			}
		}

		potential = {
			# Either you are both adults
			trigger_if = {
				limit = { is_adult = yes }
				FROM = { is_adult = yes }
			}
			# Or you are both children! WEIRD!
			trigger_else = {
				FROM = { is_adult = no }
			}

			prisoner = no
		}

		allow = {
			# Won't show ANY reqs. if you have "Unrestricted" duels...
			trigger_if = {
				limit = {
					NOT = {
						has_game_rule = {
							name = dueling
							value = unrestricted
						}
					}
				}

				# But otherwise: find a target, valid both relationship-, and healthwise...
				OR = {
					custom_tooltip = {
						text = duel_tooltip_rivals
						is_rival = FROM
					}

					custom_tooltip = {
						text = duel_tooltip_foes
						is_foe = FROM
					}

					trigger_if = {
						limit = { religion = FROM }

						custom_tooltip = {
							text = duel_tooltip_excommunicated

							FROM = { trait = zealous }
							trait = excommunicated
						}
					}

					trigger_if = {
						limit = { FROM = { religion_group = muslim } }

						custom_tooltip = {
							text = duel_tooltip_decadent

							trait = decadent
							FROM = { trait = zealous }
						}
					}

					trigger_if = {
						limit = {
							any_quester_targeting_this = { # FROM must have this as a quest target
								character = FROM

								OR = {
									has_quest = quest_warrior_lodge_duel_honor
									has_quest = quest_warrior_lodge_duel_deadly
								}
							}
						}

						custom_tooltip = { text = quest_target_tt }
					}
				}
			}

			# TODO: Custom tooltip?
			FROM = { is_inaccessible_trigger = no }
			is_inaccessible_trigger = no

			# Both are healthy
			custom_tooltip = {
				text = duel_tooltip_sickness

				FROM = {
					NOR = {
						has_character_modifier = bedridden_illness
						has_character_modifier = severe_illness
						is_incapable = yes
						trait = infirm
						trait = aztec_disease
						trait = smallpox
						trait = measles
						trait = bubonic_plague
						trait = camp_fever
						trait = slow_fever
						trait = consumption
						trait = leper
						trait = pneumonic
					}
				}

				NOR = {
					has_character_modifier = bedridden_illness
					has_character_modifier = severe_illness
					is_incapable = yes
					trait = infirm
					trait = aztec_disease
					trait = smallpox
					trait = measles
					trait = bubonic_plague
					trait = camp_fever
					trait = slow_fever
					trait = consumption
					trait = leper
					trait = pneumonic
					practical_age >= 70
				}
			}

			custom_tooltip = {
				text = duel_tooltip_pregnant

				FROM = { is_pregnant = no }
				is_pregnant = no
			}

			####### You won't want to fight someone who is badly injured, and if you are hurt yourself, you will want to wait...
			custom_tooltip = {
				text = duel_tooltip_recent_duel

				NOR = {
					has_character_modifier = recent_duel_timer
					FROM = { has_character_modifier = recent_duel_timer }
				}
			}

			#############################################################################

			custom_tooltip = {
				text = duel_tooltip_no_war

				FROM = { war = no }
				war = no
			}

			# If you do not have this as your mission target, you need to be within diplo-range...
			trigger_if = {
				limit = {
					NOT = {
						any_quester_targeting_this = { # FROM must have this as a quest target
							character = FROM
							OR = {
								has_quest = quest_warrior_lodge_duel_honor
								has_quest = quest_warrior_lodge_duel_deadly
							}
						}
					}
				}
				is_within_diplo_range = FROM # CPU HEAVY!
			}

			trigger_if = {
				limit = { has_character_modifier = has_recently_declined_duel_cooldown }

				custom_tooltip = {
					text = opinion_duel_decline_cooldown_tt
					always = no
				}
			}
		}

		effect = {
			# REQUIRED event targets for setting up duel evaluation!
			FROM = { save_event_target_as = combatant_1 } # the person issuing the duel...
			save_event_target_as = combatant_2 # the target of the duel...

			apply_degree_of_dishonorable_duel_effect = yes # checks a bunch of age and health statuses...
			apply_any_applicable_harsh_penalties_effect = yes # if your target is someone

			FROM = {
				show_scope_change = no

				pacifists_lose_piety_effect = yes

				if = {
					limit = { religion = cathar }
					piety = -20
				}

				hidden_effect = {
					change_variable = {
						which = lifestyle_martial
						value = 1
					}
				}

				add_character_modifier = {
					name = recent_duel_timer
					months = 3
					hidden = yes
				}
			}

			character_event = { id = HFP.10095 } # Duel evaluation (hidden) + result event as follow-up
		}

		ai_will_do = {
			factor = 0.1

			trigger = {
				# If the target has already declined a duel, AI's won't attempt to duel them while the timer is still
				NOT = {
					has_character_modifier = declined_prestige_duel_timer
					religion = cathar
					religion = jain
				}
			}

			mult_modifier = {
				factor = 1.5
				FROM = { potentially_interested_in_rivaling_root_trigger = yes }
			}
		}
	}

	########################################
	# 				CLAIM duels 		   #
	########################################

	claim_duel_decision = {
		diplomacy_icon = GFX_duel_decision

		ai_check_interval = 10

		is_high_prio = yes

		only_playable = yes

		from_potential = {
			has_dlc = "Holy Fury"
			is_tribal = yes
			num_of_claims > 0
			is_adult = yes
			prisoner = no
			has_regent = no

			trigger_if = {
				limit = {
					is_female = yes
					ai = yes
				}

				OR = {
					trait = brave
	                trait = shieldmaiden
					trait = martial_lady
					trait = martial_lady_tengri
					trait = martial_lady_pagan
					religion_group = pagan_group
					is_nomadic = yes
					religion = buddhist
					religion = bogomilist
					gender_equality_trigger = yes
				}
			}
		}

		potential = {
			any_demesne_title = {
				FROM = {
					has_strong_claim = PREV

					NOR = {
						character = ROOT
						has_character_flag = claim_duel@ROOT
					}
				}
			}

			OR = {
				independent = yes
				same_liege = FROM
			}

			# Either you are both adults
			trigger_if = {
				limit = { is_adult = yes }
				FROM = { is_adult = yes }
			}
			# Or you are both children! WEIRD!
			trigger_else = {
				FROM = { is_adult = no }
			}

			prisoner = no
		}

		allow = {
			### For CLAIM duels ###
			custom_tooltip = {
				text = both_tribal_tt

				is_tribal = yes
				FROM = { is_tribal = yes }
			}

			# Both are healthy
			custom_tooltip = {
				text = duel_tooltip_sickness

				FROM = {
					NOR = { # Not in sickly in bed, but sick if well treated is okay
						has_character_modifier = bedridden_illness
						has_character_modifier = severe_illness
						is_incapable = yes
					}
				}

				NOR = { # Not in sickly in bed, but sick if well treated is okay
					has_character_modifier = bedridden_illness
					has_character_modifier = severe_illness
					is_incapable = yes
				}
			}

			# TODO: custom tooltip?
			FROM = { is_inaccessible_trigger = no }
			is_inaccessible_trigger = no

			custom_tooltip = {
				text = duel_tooltip_recent_duel

				NOR = {
					has_character_modifier = recent_duel_timer
					FROM = { has_character_modifier = recent_duel_timer }
				}
			}

			custom_tooltip = {
				text = duel_tooltip_recent_challenge_tt

				FROM = {
					NOT = { has_character_modifier = recently_challenged_rule_timer }
				}
			}

			custom_tooltip = {
				text = duel_tooltip_no_war

				FROM = { war = no }
				war = no
			}

			is_within_diplo_range = FROM # CPU HEAVY!

			trigger_if = {
				limit = { has_character_modifier = has_recently_declined_duel_cooldown }

				custom_tooltip = {
					text = opinion_duel_decline_cooldown_tt
					always = no
				}
			}
		}

		effect = {
			FROM = {
				show_scope_change = no

				set_character_flag = claim_duel@ROOT
				pacifists_lose_piety_effect = yes
		   		increase_troublemaker_status_effect = yes

				add_character_modifier = {
					name = recent_duel_timer
					months = 3
					hidden = yes
				}
			}

			# REQUIRED event targets for setting up duel evaluation!
			FROM = { save_event_target_as = combatant_1 } # the person issuing the duel...
			save_event_target_as = combatant_2 # the target of the duel...

			apply_degree_of_dishonorable_duel_effect = yes # checks a bunch of age and health statuses...
			apply_any_applicable_harsh_penalties_effect = yes

			hidden_effect = {
				if = { # If Challenger has more than one claim to fight for...
					limit = {
						any_demesne_title = {
							count > 1
							FROM = { has_strong_claim = PREV }
						}
					}

					random_demesne_title = {
						limit = { event_target:combatant_1 = { has_strong_claim = PREV } }
						preferred_limit = { real_tier = EMPEROR }
						preferred_limit = { real_tier = KING }
						preferred_limit = { real_tier = DUKE }
						preferred_limit = { real_tier = COUNT }
						preferred_limit = { real_tier = BARON }
						save_event_target_as = target_title
					}

					event_target:combatant_1 = {
						set_character_flag = wants_multiple_titles_from@event_target:combatant_2
					}
				}
				else = { # There is only ONE claim involved...
					random_demesne_title = {
						limit = { event_target:combatant_1 = { has_strong_claim = PREV } }
						save_event_target_as = target_title
					}

					event_target:combatant_1 = {
						set_character_flag = wants_single_title_from@event_target:combatant_2
					}
				}

				FROM = {
					add_character_modifier = {
						name = recently_challenged_rule_timer
						months = 3
						hidden = yes
					}
				}

				character_event = {
					id = HF.10095 # Duel evaluation (hidden) + result event as follow-up
					days = 1
				}
			}
		}

		ai_will_do = {
			factor = 0.1

			trigger = {
				# If the target has already declined a duel, AI's won't attempt to duel them while the timer is still
				NOT = { has_character_modifier = declined_prestige_duel_timer }
			}

			mult_modifier = {
				factor = 1.1
				trait = brave
			}

			mult_modifier = {
				factor = 1.5
				trait = ambitious
			}

			title_tier_reduction_score = yes
		}
	}

	########################################

	### Recruit/Induct your child into your warrior lodge ###
	recruit_child_decision = {
		diplomacy_icon = GFX_duel_decision

		filter = close_relative
		ai_target_filter = close_relatives
		ai_check_interval = 36

		is_high_prio = yes

		only_playable = yes

		from_potential = {
			has_dlc = "Holy Fury"
			is_member_of_any_warrior_lodge_trigger = yes
			has_living_children = yes
			is_adult = yes
			prisoner = no
			has_regent = no
		}

		potential = {
			is_child_of = FROM
			prisoner = no
			NOT = { has_character_flag = flag_child_abandoned_lodge }

			FROM = {
				trigger_if = {
					limit = { ai = yes }
					player_heir = { character = ROOT }
				}
			}
		}

		allow = {
			practical_age >= 12

			trigger_if = {
				limit = { # Can't be in a society already...
					is_in_society = yes

					# except a secret one...
					society_is_criminal = no
					NOT = { society_member_of = the_assassins }
				}

				is_in_society = no
			}

			trigger_if = {
				limit = {
					OR = {
						has_character_flag = is_being_recruited_to_warrior_lodge_by_parent
						has_character_flag = refused_trial
					}
				}

				custom_tooltip = {
					text = already_attempted_recruitment_tt
					always = no
				}
			}

			trigger_if = {
				limit = {
					OR = { # Either you or the recruit have a bloodline that matters...
						FROM = {
							any_owned_bloodline = {
								has_bloodline_flag = bloodline_legendary_warrior
								bloodline_is_active_for = PREV
							}
						}

						any_owned_bloodline = {
							has_bloodline_flag = bloodline_legendary_warrior
							bloodline_is_active_for = PREV
						}

						custom_tooltip = {
							text = has_mission_to_recruit_tt

							FROM = {
								has_quest = quest_warrior_lodge_recruit
								quest_target = { character = ROOT }
							}
						}
					}
				}

				OR = {
					FROM = {
						show_scope_change = no
						has_society_currency_tiny_trigger = yes # It could cost currency...
					}

					custom_tooltip = {
						text = legendary_warrior_bloodline_trigger_tt

						OR = { # Or you or the recruit have a bloodline that matters...
							FROM = {
								any_owned_bloodline = {
									has_bloodline_flag = bloodline_legendary_warrior
									bloodline_is_active_for = PREV
								}
							}

							any_owned_bloodline = {
								has_bloodline_flag = bloodline_legendary_warrior
								bloodline_is_active_for = PREV
							}
						}
					}

					custom_tooltip = { # Or you have a mission to recruit ROOT...
						text = has_mission_to_recruit_tt

						FROM = {
							has_quest = quest_warrior_lodge_recruit
							quest_target = { character = ROOT }
						}
					}
				}
			}
			trigger_else = {
				FROM = {
					show_scope_change = no
					has_society_currency_tiny_trigger = yes # It could cost currency...
				}
			}

			FROM = { is_inaccessible_trigger = no }
			is_inaccessible_trigger = no

			custom_tooltip = {
				text = characters_are_not_busy_tt

				FROM = { war = no }
				war = no
			}

			ROOT = {
				show_scope_change = no
				show_only_failed_conditions = yes

				is_within_diplo_range = FROM # CPU HEAVY!
			}

			trigger_if = { # If landed, target character must be tribal or nomadic
				limit = { is_landed = yes }

				ROOT = {
					OR = {
						has_tribal_or_nomadic_government_trigger = yes

						custom_tooltip = {
							text = has_mission_to_recruit_tt

							FROM = {
								has_quest = quest_warrior_lodge_recruit
								quest_target = { character = ROOT }
							}
						}
					}
				}
			}
			trigger_if = { # If landed, recruiting character must be tribal or nomadic
				limit = { FROM = { is_landed = yes } }

				FROM = {
					OR = {
						has_tribal_or_nomadic_government_trigger = yes

						custom_tooltip = {
							text = has_mission_to_recruit_tt

							has_quest = quest_warrior_lodge_recruit
							quest_target = { character = ROOT }
						}
					}
				}
			}
		}

		effect = {
			if = {
				limit = {
					NOR = { #If no one has a bloodline that matters...
						FROM = {
							any_owned_bloodline = {
								has_bloodline_flag = bloodline_legendary_warrior
								bloodline_is_active_for = PREV
							}
						}

						any_owned_bloodline = {
							has_bloodline_flag = bloodline_legendary_warrior
							bloodline_is_active_for = PREV
						}

						custom_tooltip = {
							text = has_mission_to_recruit_tt

							FROM = {
								has_quest = quest_warrior_lodge_recruit
								quest_target = { character = ROOT }
							}
						}
					}
				}

				FROM = {
					show_scope_change = no
					detract_society_currency_tiny_effect = yes #It will cost currency...
				}
			}


			save_event_target_as = new_recruit
			set_character_flag = society_join_block
			set_character_flag = awaiting_initiation_trial
			set_character_flag = is_being_recruited_to_warrior_lodge_by_parent

			FROM = {
				save_event_target_as = recruiting_parent

				# Used later for joining correct society:
				society = {
					save_event_target_as = warrior_lodge_to_join
				}
			}

			character_event = { id = HF.10016 } # Find opponent to fight
		}

		ai_will_do = {
			factor = 1
		}
	}

	##############################
	########## POWERS ############
	##############################

	# Power, rank 4: Choose Military Aspect
	choose_military_aspect = {
		filter = self
		ai_target_filter = self
		ai_check_interval = 12

		is_in_society = yes

		potential = {
			has_dlc = "Holy Fury"
			is_member_of_any_warrior_lodge_trigger = yes
			society_rank == 4
			is_adult = yes
		}

		allow = {
			has_society_currency_minor_trigger = yes

			custom_tooltip = {
				text = choose_military_aspect_allow_tt
				NOT = { has_character_flag = choosing_military_aspect }
			}

			OR = {
				custom_tooltip = {
					text = does_not_have_a_lifestyle_trait_tt
					lifestyle_traits < 1
				}

				trait = duelist
				trait = hunter
				trait = strategist
			}
		}

		effect = {
			set_character_flag = choosing_military_aspect

			if = {
				limit = { lifestyle_traits < 1 }
				character_event = { id = HF.10021 } # Choose a path to go down...
			}
			else = {
				character_event = { id = HF.10020 } # Choose which trait to replace your current with...!
			}

			detract_society_currency_minor_effect = yes
		}

		ai_will_do = {
			factor = 1

			trigger = {
				lifestyle_traits < 1
			}
		}
	}

	# Power, rank 2: Gain Lodge-specific Commander trait
	warrior_training = {
		filter = self
		ai_target_filter = self
		ai_check_interval = 12

		is_in_society = yes
		only_playable = yes

		potential = {
			has_dlc = "Holy Fury"
			is_member_of_any_warrior_lodge_trigger = yes
			society_rank >= 2
			is_adult = yes
			NOT = { has_character_flag = choosing_warrior_training } # to stop you spamming the button
			has_current_warrior_lodge_leader_trait_trigger = no # to keep it hidden once you're done
		}

		allow = {
			has_society_currency_medium_trigger = yes

			custom_tooltip = {
				text = has_no_warrior_training_with_current_society_tt
				has_current_warrior_lodge_leader_trait_trigger = no
			}
		}

		effect = {
			detract_society_currency_medium_effect = yes
			set_character_flag = choosing_warrior_training
			character_event = { id = HF.10040 } # Gain trait...
		}


		ai_will_do = {
			factor = 1

			trigger = {
				can_have_more_leadership_traits = yes
			}

			mult_modifier = {
				factor = 0.1
				has_religion_matching_joined_warrior_lodge_trigger = no
			}
		}
	}

	# Power, rank 1 (Bon only): Toughness
	warrior_toughness = {
		filter = self
		ai_target_filter = self
		ai_check_interval = 12

		is_in_society = yes
		only_playable = yes

		potential = {
			has_dlc = "Holy Fury"
			society_member_of = warrior_lodge_bon
			society_rank >= 1
			is_adult = yes
		}

		allow = {
			has_society_currency_minor_trigger = yes
			has_religion_matching_joined_warrior_lodge_trigger = yes

			OR = {
				trait = wounded
				trait = severely_injured
				trait = maimed
				trait = mangled
				trait = infection
			}

			trigger_if = {
				limit = { has_character_flag = using_toughness } # to stop you spamming the button

				custom_tooltip = {
					text = not_already_using_toughness_tt
					always = no
				}
			}
		}

		effect = {
			detract_society_currency_minor_effect = yes
			set_character_flag = using_toughness
			character_event = { id = HF.10055 } # Remove physical injury...
		}

		ai_will_do = {
			factor = 1

			trigger = {
				NOT = { trait = depressed }
			}

			mult_modifier = {
				factor = 1.5
				in_command = yes
			}
		}
	}

	# Power, rank 1 (Norse only): Go Berserk
	warrior_lodge_norse_go_berserk = {
		filter = self
		ai_target_filter = self
		ai_check_interval = 12

		is_in_society = yes
		only_playable = yes

		potential = {
			has_dlc = "Holy Fury"
			society_member_of = warrior_lodge_norse
			is_adult = yes
		}

		allow = {
			has_society_currency_minor_trigger = yes
			has_religion_matching_joined_warrior_lodge_trigger = yes

			OR = {
				NOT = { trait = berserker }
				prisoner = yes
			}

			trigger_if = {
				limit = { has_character_flag = going_berserk } # to stop you spamming the button

				custom_tooltip = {
					text = not_already_going_berserk_tt
					always = no
				}
			}
		}

		effect = {
			if = {
				limit = { NOT = { trait = berserker } }

				# Specifies what the trait gives: must be updated if trait is
				custom_tooltip = { text = gain_berserker_tt }
			}

			set_character_flag = special_berserker # Will last for at least 10 years
			set_character_flag = going_berserk
			detract_society_currency_minor_effect = yes

			hidden_effect = {
				add_trait_partially_silently_wroth_effect = yes # TODO: Make not silent or visible?

				if = {
					limit = { trait = defensive_leader }
					remove_trait = defensive_leader
				}

				if = {
					limit = { leader_traits < 1 }
					add_trait = aggressive_leader
				}

				if = {
					limit = { prisoner = no }
					character_event = { id = HF.10056 } # Go berserk!
				}
				else = { # Might break you out of prison or have something bonkers happen...
					character_event = { id = HF.10070 } # Go berserk in prison!
				}
			}
		}

		ai_will_do = {
			factor = 1

			mult_modifier = {
				factor = 0.1
				trait = shy
			}

			mult_modifier = {
				factor = 0.1
				trait = craven
			}

			mult_modifier = {
				factor = 0.1
				trait = patient
			}

			mult_modifier = {
				factor = 0.1
				trait = defensive_leader
			}
		}
	}

	#Power, rank 4: Call to Glory
	warrior_lodge_call_to_glory = {
		filter = self
		ai_target_filter = self
		ai_check_interval = 12

		is_high_prio = yes
		is_mercenary = yes

		is_in_society = yes
		only_playable = yes

		potential = {
			has_dlc = "Holy Fury"
			is_member_of_any_warrior_lodge_trigger = yes
			society_rank == 4
			is_adult = yes
			NOT = { has_character_modifier = call_to_glory }

			trigger_if = {
				limit = { ai = yes }

				OR = {
					is_primary_war_defender = yes
					is_primary_war_attacker = yes
				}
			}
		}

		allow = {
			war = yes
			in_command = yes
			has_society_currency_major_trigger = yes
		}

		effect = {
			detract_society_currency_major_effect = yes

			custom_tooltip = {
				text = tooltip_lodge_call_to_glory

				set_variable = {
					which = call_to_glory_variable
					value = 0
				}

				# Removed by recurring event and on_action when at peace or if leaving society
				add_character_modifier = {
					name = call_to_glory
					duration = -1
				}

				# First recurring event
				character_event = { id = HF.25030 }
			}
		}

		ai_will_do = {
			factor = 0 # decisions with the "is_mercenary" will not be calculated from the script
		}
	}
}

society_decisions = {
	warrior_lodge_legendary_gathering = {
		ai_check_interval = 36

		is_high_prio = yes

		only_playable = yes

		potential = {
			has_dlc = "Holy Fury"
			is_member_of_any_warrior_lodge_trigger = yes
			society_rank == 4
			is_adult = yes

			trigger_if = {
				limit = { society_has_active_progress = yes }

				society = {
					had_flag = {
						flag = used_legendary_gathering
						years >= 100
					}
				}
			}
		}

		allow = {
			has_society_currency_major_trigger = yes
			is_inaccessible_trigger = no

			custom_tooltip = {
				text = legendary_gathering_cd_tt

				society = {
					trigger_if = {
						limit = { has_flag = used_legendary_gathering }

						had_flag = {
							flag = used_legendary_gathering
							years >= 100
						}
					}
				}
			}
		}

		effect = {
			detract_society_currency_major_effect = yes

			custom_tooltip = {
				text = legendary_gathering_tt
				sound_effect = pagan_warhorn


				society = {
					if = {
						limit = {
							had_flag = {
								flag = used_legendary_gathering
								years >= 100
							}
						}

						clr_flag = used_legendary_gathering
					}

					# Needs to be checked (should stick 100 years)
					set_flag = used_legendary_gathering

					clr_flag = block_society_progress

					if = {
						limit = {
							society_has_active_progress = yes

							had_flag = {
								flag = used_legendary_gathering
								years >= 100
							}
						}

						set_society_progress = 0
					}

					start_society_progress = yes
				}
			}
		}

		ai_will_do = {
			factor = 0.1

			mult_modifier = {
				factor = 1.5 # more likely if ambitious

				OR = {
					trait = ambitious
					ai_ambition >= 50
				}
			}

			mult_modifier = {
				factor = 0.1
				any_bloodline = { always = yes } # Less likely if already has a bloodline...
			}

			mult_modifier = {
				factor = 0.1
				duelist_skill_is_high_group_trigger = no # Less likely if AI is not a great duelist...
			}

			mult_modifier = {
				factor = 0.1 # Less likely if irrational
				ai_rationality < 0
			}
		}
	}

	warrior_lodge_summon_commander = {
		ai_check_interval = 36

		only_playable = yes

		potential = {
			has_dlc = "Holy Fury"
			is_member_of_any_warrior_lodge_trigger = yes
			society_rank = 2
			is_adult = yes
		}

		allow = {
			has_society_currency_medium_trigger = yes
			is_inaccessible_trigger = no
		}

		effect = {
			detract_society_currency_medium_effect = yes

			custom_tooltip = {
				text = tooltip_summoning_lodge_commander

				trigger_switch = {
					on_trigger = has_religion_feature

					religion_matriarchal = {
						create_random_soldier = {
							random_traits = yes
							dynasty = random
							female = yes

							attributes = {
								diplomacy = 4
								learning = 4
								stewardship = 4
								intrigue = 4
								martial = 14
							}
						}
					}

					religion_patriarchal = {
						create_random_soldier = {
							random_traits = yes
							dynasty = random
							female = no

							attributes = {
								diplomacy = 4
								learning = 4
								stewardship = 4
								intrigue = 4
								martial = 14
							}
						}
					}

					fallback = {
						if = {
							limit = { has_gender_equal_game_rule_trigger = yes }

							create_random_soldier = {
								random_traits = yes
								dynasty = random
								female = 50

								attributes = {
									diplomacy = 4
									learning = 4
									stewardship = 4
									intrigue = 4
									martial = 14
								}
							}
						}
						else = {
							create_random_soldier = {
								random_traits = yes
								dynasty = random
								female = 5

								attributes = {
									diplomacy = 4
									learning = 4
									stewardship = 4
									intrigue = 4
									martial = 14
								}
							}
						}
					}
				}

				new_character = {
					remove_trait = weak
					remove_trait = slow
					remove_trait = imbecile
					remove_trait = dull
					remove_lifestyle_trait_effect = yes

					if = {
						limit = {
							is_female = yes

							ROOT = {
								NOR = {
									has_religion_feature = religion_matriarchal
									has_religion_feature = religion_patriarchal
									has_gender_equal_game_rule_trigger = yes
								}
							}
						}

						set_character_flag = special_marshal # Ensures character can be marshal
					}

					random_list = { # Flavor Randomization
						35 = { } # Nothing
						15 = { change_martial = 2 }
						10 = { add_trait = brawny }
						10 = { add_trait = duelist }
						10 = { add_trait = hunter }
						 5 = { add_trait = strategist }
						 5 = { change_martial = 4 }
						 5 = { add_trait = shrewd }
						 3 = { add_trait = giant }

						 5 = {
							if = {
								limit = { has_dlc = "Reapers" }
								add_trait = one_eyed
							}
						}
					}

					join_prev_warrior_lodge_society_effect = yes

					hidden_effect = {
						random = {
							# Chance of the commander being spawned from another religion
							chance = 50

							give_this_warrior_lodge_religion_effect = yes

							#In which case don't allow liege to immediately demand conversion.
							set_character_flag = ai_flag_refuse_conversion
						}

						add_warrior_lodge_leader_trait_effect = yes

						if = {
							limit = { has_dharmic_religion_trigger = yes }

							add_trait = kshatriya
							character_event = { id = RoI.30121 }
						}

						else_if = {
							limit = {
								OR = {
									religion = hellenic_pagan
									religion = hellenic_pagan_reformed
									has_religion_feature = religion_astrology
								}
							}

							add_western_zodiac_trait_effect = yes
						}

						if = {
							limit = {
								religion_group = pagan_group
								has_religion_feature = religion_no_leader
							}

							random_list = {
								 5 = { add_trait = pagan_branch_1 }
								25 = { add_trait = pagan_branch_2 }
								25 = { add_trait = pagan_branch_3 }
								25 = { add_trait = pagan_branch_4 }
							}
						}
					}

					set_character_flag = invited_lodge_soldier
					set_character_flag = lodge_warrior_original_court_@ROOT
					set_character_flag = no_court_invites
					save_event_target_as = invited_lodge_soldier
				}
			}

			character_event = { id = HF.25000 } # Notify, give commander title, etc.
		}

		ai_will_do = {
			factor = 0.1

			trigger = {
				# Only use if empty commander spots
				can_grant_title = title_commander
			}

			mult_modifier = {
				factor = 0.5
				war = no # Only summon in time of need
			}

			mult_modifier = {
				factor = 0.1

				# Less likely to invite if there is already someone else at court
				any_courtier = { is_member_of_any_warrior_lodge_trigger = yes }
			}

			mult_modifier = {
				factor = 0.1
				job_marshal = { martial >= 16 }
			}

			mult_modifier = {
				factor = 0.25

				# Fearful to be challenged for leadership
				is_tribal = yes
				trait = paranoid
			}
		}
	}

	warrior_lodge_suomenusko_summon_warriors = {
		ai_check_interval = 20

		is_high_prio = yes
		is_mercenary = yes

		only_playable = yes

		potential = {
			has_dlc = "Holy Fury"

			society_rank = {
				society = warrior_lodge_finnish
				rank >= 3
			}

			religion_openly_finnish_or_reformed_trigger = yes
			is_adult = yes

			trigger_if = {
				limit = { ai = yes }

				OR = {
					is_primary_war_defender = yes
					is_primary_war_attacker = yes
				}
			}
		}

		allow = {
			war = yes
			has_society_currency_medium_trigger = yes
			is_inaccessible_trigger = no

			custom_tooltip = {
				text = tooltip_has_not_already_summoned_ukko_warriors
				NOT = { has_character_modifier = lodge_summon_warriors_cooldown }
			}
		}

		effect = {
			detract_society_currency_medium_effect = yes

			character_event = { id = HF.25001 }

			custom_tooltip = {
				text = tooltip_summon_warriors_ukko

				# Removed on_action when player is back at peace.
				add_character_modifier = {
					name = lodge_summon_warriors_cooldown
					duration = -1
					hidden = yes
				}
			}
		}

		ai_will_do = {
			factor = 0 # decisions with the "is_mercenary" will not be calculated from the script
		}
	}

	warrior_lodge_slavic_stand_together = {
		ai_check_interval = 20

		is_high_prio = yes
		is_mercenary = yes

		only_playable = yes

		potential = {
			has_dlc = "Holy Fury"

			society_rank = {
				society = warrior_lodge_slavic
				rank >= 2
			}

			religion_openly_slavic_or_reformed_trigger = yes
			is_adult = yes

			trigger_if = {
				limit = { ai = yes }

				OR = {
					is_primary_war_defender = yes
					is_primary_war_attacker = yes
				}
			}
		}

		allow = {
			war = yes
			has_society_currency_medium_trigger = yes
			is_inaccessible_trigger = no

			custom_tooltip = {
				text = tooltip_has_not_already_stood_together_slavic
				NOT = { has_character_modifier = lodge_slavic_stand_together_cooldown }
			}

			custom_tooltip = {
				text = tooltip_stand_together_candidate_exists

				society = {
					any_society_member = {
						is_ruler = no
						religion = ROOT
						is_unimportant_trigger = yes
						prisoner = no
						is_incapable = no
						is_inaccessible_trigger = no
						in_command = no

						host = {
							ai = yes
							NOT = { character = ROOT }
						}

						NOR = {
							is_heir = ROOT
							has_character_flag = no_court_invites
						}
					}
				}
			}
		}

		effect = {
			detract_society_currency_medium_effect = yes

			custom_tooltip = {
				text = tooltip_slavic_stand_together

				character_event = { id = HF.25008 } # Find society member

				add_character_modifier = {
					name = lodge_slavic_stand_together_cooldown
					duration = -1 #Removed on_action when player is back at peace.
					hidden = yes
				}
			}
		}

		ai_will_do = {
			factor = 0 # decisions with the "is_mercenary" will not be calculated from the script
		}
	}

	warrior_lodge_tengri_call_of_the_steppe = {
		ai_check_interval = 50

		is_high_prio = yes

		only_playable = yes

		potential = {
			has_dlc = "Holy Fury"

			society_rank = {
				society = warrior_lodge_tengri
				rank >= 2
			}

			religion_openly_tengri_or_reformed_trigger = yes
			is_adult = yes
		}

		allow = {
			has_society_currency_minor_trigger = yes
			is_inaccessible_trigger = no

			trigger_if = {
				limit = { is_nomadic = yes }

				custom_tooltip = {
					text = tooltip_call_of_the_steppe_cooldown

					NOR = {
						has_character_modifier = lodge_call_of_the_steppe_boost
						has_character_flag = flag_using_call_of_the_steppes
					}
				}
			}
		}

		effect = {
			detract_society_currency_minor_effect = yes

			if = {
				limit = { is_nomadic = yes }

				custom_tooltip = {
					text = tooltip_call_of_the_steppe_nomad

					character_event = { id = HF.25004 } # Give reward
				}
			}
			else = {
				custom_tooltip = {
					text = tooltip_call_of_the_steppe_feudal

					character_event = { id = HF.25005 } # Give reward
					set_character_flag = flag_using_call_of_the_steppes
				}
			}
		}

		ai_will_do = {
			factor = 0.1

			trigger = {
				trigger_if = {
					limit = { war = yes }
					is_nomadic = yes # No need for levy reinforcement for non-nomads when at peace
				}

				# I could use some help winning this war
				any_war = {
					defender = { character = ROOT }
					war_score < 50
				}
			}

			mult_modifier = {
				factor = 0.5
				trait = stubborn
			}

			mult_modifier = { # I am losing the war, help!
				factor = 10

				any_war = {
					defender = { character = ROOT }
					war_score < -50
				}
			}

			mult_modifier = {
				factor = 4
				war = yes
			}

			mult_modifier = {
				factor = 2
				trait = zealous
			}

			mult_modifier = {
				factor = 3
				trait = ambitious
			}
		}
	}

	warrior_lodge_war_sacrifice = {
		ai_check_interval = 6

		is_in_society = yes
		only_playable = yes

		potential = {
			has_dlc = "Holy Fury"
			is_member_of_any_warrior_lodge_trigger = yes
			society_rank >= 3
			is_adult = yes

			trigger_if = {
				limit = { ai = yes }

				OR = {
					is_primary_war_defender = yes
					is_primary_war_attacker = yes
				}
			}
		}
		allow = {
			has_society_currency_medium_trigger = yes
			is_inaccessible_trigger = no
			is_incapable = no

			custom_tooltip = {
				text = tooltip_war_sacrifice_cooldown

				NOR = {
					has_character_modifier = lodge_war_sacrifice_cooldown
					has_character_flag = flag_picking_war_sacrifice
				}
			}
		}

		effect = {
			detract_society_currency_medium_effect = yes
			custom_tooltip = { text = tooltip_lodge_war_sacrifice }
			set_character_flag = flag_picking_war_sacrifice
			character_event = { id = HF.25021 }
		}

		ai_will_do = {
			factor = 0.1

			trigger = {
				war = yes

				trigger_if = {
					limit = {
						trait = zealous
						religion_group = pagan_group
					}

					NOR = {
						religion = hellenic_pagan
						religion = bon_pagan
					}
				}
			}

			mult_modifier = {
				factor = 0.5
				trait = stubborn
			}

			mult_modifier = {
				factor = 1.5
				has_religion_matching_joined_warrior_lodge_trigger = no
			}

			mult_modifier = {
				factor = 0.5
				trait = craven
			}

			mult_modifier = { # I am losing the war, help!
				factor = 10

				any_war = {
					defender = { character = ROOT }
					war_score < -50
				}
			}

			mult_modifier = { # I am winning the war, no need!
				factor = 0.25

				any_war = {
					defender = { character = ROOT }
					war_score > 50
				}
			}

			mult_modifier = {
				factor = 2
				trait = zealous
				religion_group = pagan_group
			}

			mult_modifier = {
				factor = 3
				trait = brave
			}

			mult_modifier = {
				factor = 3

				OR = {
					has_religion_feature = religion_haruspicy
					has_religion_feature = religion_feature_celtic
				}
			}

			mult_modifier = {
				factor = 5
				has_religion_feature = religion_ritual_sacrifice
			}
		}
	}

	warrior_lodge_zun_battle_trance = {
		ai_check_interval = 12

		only_playable = yes

		potential = {
			has_dlc = "Holy Fury"

			society_rank = {
				society = warrior_lodge_zun
				rank >= 2
			}

			religion_openly_zun_or_reformed_trigger = yes
			is_adult = yes
		}

		allow = {
			has_society_currency_minor_trigger = yes
			is_inaccessible_trigger = no
			is_incapable = no

			custom_tooltip = {
				text = tooltip_zun_battle_trance_cooldown

				NOR = {
					has_character_modifier = lodge_zun_battle_trance_1
					has_character_modifier = lodge_zun_battle_trance_2
					has_character_modifier = lodge_zun_battle_trance_3
					has_character_flag = flag_picking_battle_trance
				}
			}
		}

		effect = {
			detract_society_currency_minor_effect = yes

			custom_tooltip = {
				text = tooltip_zun_battle_trance

				set_character_flag = flag_picking_battle_trance
				character_event = { id = HF.25006 }
			}
		}

		ai_will_do = {
			factor = 0.1

			trigger = {
				war = yes
				is_ill = no
				NOT = { trait = craven }

				# I could use some help winning this war
				any_war = {
					defender = { character = ROOT }
					war_score < 50
				}
			}

			mult_modifier = {
				factor = 0.5
				trait = stubborn
			}

			mult_modifier = { # I am losing the war, help!
				factor = 10

				any_war = {
					defender = { character = ROOT }
					war_score < -50
				}
			}

			mult_modifier = {
				factor = 2
				trait = zealous
			}

			mult_modifier = {
				factor = 3
				trait = brave
			}
		}
	}

	warrior_lodge_west_african_create_fetish = {
		ai_check_interval = 150

		is_high_prio = yes

		only_playable = yes

		potential = {
			has_dlc = "Holy Fury"

			society_rank = {
				society = warrior_lodge_west_african
				rank == 4
			}

			religion_openly_west_african_or_reformed_trigger = yes
			is_adult = yes

			trigger_if = {
				limit = {
					ai = yes
					is_landed = no
				}

				NOT = {
   					any_artifact = {
   						has_artifact_flag = mask
   					}
   				}
			}
		}

		allow = {
			trigger_if = {
				limit = { ai = no }
				has_society_currency_major_trigger = yes
			}

			is_inaccessible_trigger = no
			is_incapable = no

			custom_tooltip = {
				text = tooltip_west_african_create_fetish_cooldown

				# Removed upon ending of event chain
				NOT = { has_character_flag = flag_ordered_fetish_creation }
			}

			custom_tooltip = {
				text = tooltip_has_african_mask_duplicate

   				NAND = {
   					any_artifact = {
   						has_artifact_flag = mask
   					}

					any_artifact = {
   						has_artifact_flag = totem
   					}
   				}
   			}
		}

		effect = {
			if = {
				limit = { ai = no }
				detract_society_currency_major_effect = yes
			}

			custom_tooltip = {
				text = tooltip_west_african_create_fetish

				if = {
					limit = {
						ai = yes
						is_landed = no

						NOT = {
							any_artifact = {
								has_artifact_flag = mask
							}
						}
					}
					generate_mask_effect = yes
				}

				else = {
					set_character_flag = flag_ordered_fetish_creation

					character_event = { id = HF.25040 }
				}
			}
		}

		ai_will_do = {
			factor = 1

			mult_modifier = {
				factor = 0.5
				trait = stubborn
			}

			mult_modifier = {
				factor = 5
				trait = ambitious
			}

			mult_modifier = {
				factor = 5
				trait = zealous
			}

			mult_modifier = {
				factor = 2
				war = yes
			}
		}
	}
}

decisions = {
	# Create Hellenic Warrior Lodge - Dead at start
	create_warrior_lodge_hellenic = {
		ai_check_interval = 120

		is_high_prio = yes

		only_playable = yes

		potential = {
			has_dlc = "Holy Fury"
			religion_openly_hellenic_or_reformed_trigger = yes

			NOR = {
				has_global_flag = flag_restored_warrior_lodge_hellenic
				has_religion_feature = religion_peaceful

				has_alternate_start_parameter = {
					key = religion_names
					value = random
				}
			}

			warrior_lodge_hellenic = {
				society_has_members = no
			}

			is_adult = yes
		}

		allow = {
			higher_real_tier_than = DUKE
			has_education_martial_trigger = yes
			martial >= 15
			scaled_wealth = 0.75
			prestige >= 1500
			is_in_society = no
			is_incapable = no
			is_inaccessible_trigger = no
		}

		effect = {
			scaled_wealth = -0.75
			prestige = -1500

			custom_tooltip = {
				text = create_warrior_lodge_hellenic_tooltip

				set_global_flag = flag_restored_warrior_lodge_hellenic
				join_society = warrior_lodge_hellenic
				set_society_grandmaster = yes
				add_society_currency_massive_effect = yes

				while = {
					count = 6

					spawn_good_commander_effect = yes
					new_character = { join_society = warrior_lodge_hellenic }
				}

				any_vassal = {
					limit = {
						ai = yes
						can_join_society = warrior_lodge_hellenic
					}

					random = {
						chance = 75
						join_society = warrior_lodge_hellenic
					}
				}

				any_neighbor_independent_ruler = {
					limit = {
						ai = yes
						can_join_society = warrior_lodge_hellenic
					}

					random = {
						chance = 35
						join_society = warrior_lodge_hellenic
					}
				}

				any_player = {
					limit = {
						OR = {
							religion_openly_hellenic_or_reformed_trigger = yes
							culture = roman
							is_within_diplo_range = ROOT
						}
					}

					narrative_event = { id = HF.25026 } # Chance to join
				}
			}
		}

		ai_will_do = {
			factor = 1
		}
	}
}

settlement_decisions = {
	# Power, rank 3: Inspire Warriors
	warrior_lodge_inspire_warriors = {
		# filter = owned # CleanSlate: Nonfunctional
		ai_target_filter = owned
		ai_check_interval = 80

		is_in_society = yes
		only_playable = yes

		from_potential = {
			has_dlc = "Holy Fury"
			is_member_of_any_warrior_lodge_trigger = yes
			society_rank >= 3
			higher_real_tier_than = BARON
			NOT = { has_character_flag = inspiring_warriors }
		}

		potential = {
			holder = FROM
		}

		allow = {
			FROM = {
				show_scope_change = no
				has_society_currency_minor_trigger = yes
			}

			# Holding does not have a full garrison or levy
			holding_total_levy_percent < 1
		}

		effect = {
			save_event_target_as = target_holding

			FROM = {
				show_scope_change = no

				set_character_flag = inspiring_warriors
				detract_society_currency_minor_effect = yes
				character_event = { id = HF.10059 }	# Handle actual effect
			}
		}


		ai_will_do = {
			factor = 1

			trigger = {
				FROM = {
					# If I am not at war, who cares!
					war = yes

					# I could use some help winning this war
					any_war = {
						defender = { character = ROOT }
						war_score < 50
					}
				}
			}

			mult_modifier = { # I am losing the war, help!
				factor = 1.5

				any_war = {
					defender = { character = ROOT }
					war_score < -50
				}
			}
		}
	}

	# Power, rank 3: Baltic Holds
	warrior_lodge_romuva_baltic_holds = {
		# filter = owned # CleanSlate: Nonfunctional
		ai_target_filter = owned
		ai_check_interval = 80

		is_high_prio = yes

		is_in_society = yes
		only_playable = yes

		from_potential = {
			has_dlc = "Holy Fury"

			society_rank = {
				society = warrior_lodge_baltic
				rank >= 3
			}

			religion_openly_baltic_or_reformed_trigger = yes
			higher_real_tier_than = BARON
			is_nomadic = no
			is_adult = yes
		}

		potential = {
			holder = FROM

			trigger_if = {
				limit = { holding_type = tribal }

				NOR = {
					has_building = tb_basic_keep_5
					is_building = tb_basic_keep_5
				}
			}
			trigger_else = {
				holding_type = castle

				NOR = {
					has_building = ca_wall_5
					is_building = ca_wall_5
					has_building = ca_con_wall_6
					is_building = ca_con_wall_6
				}
			}
		}

		allow = { # Now it's going to get ugly...
			FROM = { has_society_currency_minor_trigger = yes }

			trigger_if = {
				trigger = { holding_type = tribal }

				trigger_if = {
					limit = { has_building = tb_basic_keep_4 }

					custom_tooltip = {
						text = tooltip_can_build_tb_basic_keep_5

						location = {
							TECH_CASTLE_CONSTRUCTION >= 1
							TECH_FORTIFICATIONS_CONSTRUCTION >= 1
						}
					}
				}
				trigger_else_if = {
					limit = { has_building = tb_basic_keep_3 }

					custom_tooltip = {
						text = tooltip_can_build_tb_basic_keep_4

						NOT = { is_building = tb_basic_keep_4 }

						location = {
							TECH_CASTLE_CONSTRUCTION >= 1
							TECH_FORTIFICATIONS_CONSTRUCTION >= 1
						}
					}
				}
				trigger_else_if = {
					limit = { has_building = tb_basic_keep_2 }

					custom_tooltip = {
						text = tooltip_can_build_tb_basic_keep_3

						NOT = { is_building = tb_basic_keep_3 }

						location = {
							TECH_CASTLE_CONSTRUCTION >= 1
							TECH_FORTIFICATIONS_CONSTRUCTION >= 1
						}
					}
				}
				trigger_else_if = {
					limit = { has_building = tb_basic_keep_1 }

					custom_tooltip = {
						text = tooltip_can_build_tb_basic_keep_2

						NOT = { is_building = tb_basic_keep_2 }
						location = { TECH_CASTLE_CONSTRUCTION >= 1 }
					}
				}
				trigger_else = {
					custom_tooltip = {
						text = tooltip_can_build_tb_basic_keep_1

						NOT = { is_building = tb_basic_keep_1 }
						location = { TECH_CASTLE_CONSTRUCTION >= 1 }
					}
				}
			}
			trigger_else_if = {
				limit = { holding_type = castle }

				trigger_if = {
					limit = { has_building = ca_wall_4 }

					custom_tooltip = {
						text = tooltip_can_build_ca_wall_5
						location = { TECH_FORTIFICATIONS_CONSTRUCTION >= 8 }
					}
				}
				trigger_else_if = {
					limit = { has_building = ca_wall_3 }

					custom_tooltip = {
						text = tooltip_can_build_ca_wall_4

						NOT = { is_building = ca_wall_4 }
						location = { TECH_FORTIFICATIONS_CONSTRUCTION >= 6 }
					}
				}
				trigger_else_if = {
					limit = { has_building = ca_wall_2 }

					custom_tooltip = {
						text = tooltip_can_build_ca_wall_3

						NOT = { is_building = ca_wall_3 }
						location = { TECH_FORTIFICATIONS_CONSTRUCTION >= 4 }
					}
				}
				trigger_else_if = {
					limit = { has_building = ca_wall_1 }

					custom_tooltip = {
						text = tooltip_can_build_ca_wall_2

						NOT = { is_building = ca_wall_2 }
						location = { TECH_FORTIFICATIONS_CONSTRUCTION >= 2 }
					}
				}
				trigger_else = {
					custom_tooltip = {
						text = tooltip_can_build_ca_wall_1

						has_building = ca_town_market_1
						has_building = ca_housing_1
						NOT = { is_building = ca_wall_1 }
						location = { TECH_FORTIFICATIONS_CONSTRUCTION >= 1 }
					}
				}
			}
		}

		effect = {
			FROM = {
				show_scope_change = no

				detract_society_currency_minor_effect = yes

				hidden_effect = {
					random_list = {
						33 = { sound_effect = baltic_holds_01 }
						33 = { sound_effect = baltic_holds_02 }
						33 = { sound_effect = baltic_holds_03 }
					}
				}
			}

			if = {
				limit = { holding_type = castle }

				if = {
					limit = {
						has_building = ca_wall_4
						location = { TECH_FORTIFICATIONS_CONSTRUCTION >= 8 }
					}

					add_building = ca_wall_5

					FROM = {
						character_event = { id = PlusBuildings.010 } # Notify
					}
				}
				else_if = {
					limit = {
						has_building = ca_wall_3
						NOT = { is_building = ca_wall_4 }
						location = { TECH_FORTIFICATIONS_CONSTRUCTION >= 6 }
					}

					add_building = ca_wall_4

					FROM = {
						character_event = { id = PlusBuildings.009 } # Notify
					}
				}
				else_if = {
					limit = {
						has_building = ca_wall_2
						NOT = { is_building = ca_wall_3 }
						location = { TECH_FORTIFICATIONS_CONSTRUCTION >= 4 }
					}

					add_building = ca_wall_3

					FROM = {
						character_event = { id = PlusBuildings.008 } # Notify
					}
				}
				else_if = {
					limit = {
						has_building = ca_wall_1
						NOT = { is_building = ca_wall_2 }
						location = { TECH_FORTIFICATIONS_CONSTRUCTION >= 2 }
					}

					add_building = ca_wall_2

					FROM = {
						character_event = { id = PlusBuildings.007 } # Notify
					}
				}
				else_if = {
					limit = {
						NOR = {
							is_building = ca_wall_1
							has_building = ca_wall_1
						}

						has_building = ca_town_market_1
						has_building = ca_housing_1

						location = { TECH_FORTIFICATIONS_CONSTRUCTION >= 1 }
					}

					add_building = ca_wall_1

					FROM = {
						character_event = { id = PlusBuildings.006 } # Notify
					}
				}
			}
			else_if = {
				limit = { holding_type = tribal }

				if = {
					limit = {
						has_building = tb_basic_keep_4

						location = {
							TECH_CASTLE_CONSTRUCTION >= 1
							TECH_FORTIFICATIONS_CONSTRUCTION >= 1
						}
					}

					add_building = tb_basic_keep_5

					FROM = {
						character_event = { id = PlusBuildings.005 } # Notify
					}
				}
				else_if = {
					limit = {
						has_building = tb_basic_keep_3

						location = {
							TECH_CASTLE_CONSTRUCTION >= 1
							TECH_FORTIFICATIONS_CONSTRUCTION >= 1
						}
					}

					add_building = tb_basic_keep_4

					FROM = {
						character_event = { id = PlusBuildings.004 } # Notify
					}
				}
				else_if = {
					limit = {
						has_building = tb_basic_keep_2
						NOT = { is_building = tb_basic_keep_3 }

						location = {
							TECH_CASTLE_CONSTRUCTION >= 1
							TECH_FORTIFICATIONS_CONSTRUCTION >= 1
						}
					}

					add_building = tb_basic_keep_3

					FROM = {
						character_event = { id = PlusBuildings.003 } # Notify
					}
				}
				else_if = {
					limit = {
						has_building = tb_basic_keep_1
						NOT = { is_building = tb_basic_keep_2 }
						location = { TECH_CASTLE_CONSTRUCTION >= 1 }
					}

					add_building = tb_basic_keep_2

					FROM = {
						character_event = { id = PlusBuildings.002 } # Notify
					}
				}
				else_if = {
					limit = {
						NOR = {
							is_building = tb_basic_keep_1
							has_building = tb_basic_keep_1
						}

						location = {
							TECH_CASTLE_CONSTRUCTION >= 1

							trigger_if = {
								limit = { has_castle = yes }

								has_city = yes
								has_temple = yes
							}
						}
					}

					add_building = tb_basic_keep_1

					FROM = {
						character_event = { id = PlusBuildings.001 } # Notify
					}
				}
			}
		}

		ai_will_do = {
			factor = 1
		}
	}
}