﻿task_organize_levies = {
	default_task = yes
	position = councillor_marshal

	task_type = task_type_general
	task_progress = task_progress_infinite

	effect_desc = {
		desc = task_organize_levies_effect_desc
		triggered_desc = {
			trigger = {
				exists = scope:councillor_liege.dynasty
				scope:councillor_liege.dynasty = { has_dynasty_perk = erudition_legacy_5 }
			}
			desc = task_organize_levies_erudition_bonus
		}
		desc = {
			desc = council_task_possible_side_effects
			triggered_desc = {
				trigger = {
					any_sub_realm_county = {
						marshal_task_1101_county_trigger = yes
					}
					martial > mediocre_skill_rating
				}
				desc = task_organize_levies_well_organized_service
			}
			triggered_desc = {
				trigger = {
					any_sub_realm_county = {
						marshal_task_1102_county_trigger = yes
					}
					martial > mediocre_skill_rating
				}
				desc = task_organize_levies_increased_military_presence
			}
			triggered_desc = {
				trigger = {
					NOT = {
						has_character_modifier = marshal_task_levy_desertions_modifier
					}
					martial < high_skill_rating
				}
				desc = task_organize_levies_levy_desertion
			}
			triggered_desc = {
				trigger = {
					any_sub_realm_county = {
						marshal_task_1002_county_trigger = yes
					}
					martial < high_skill_rating
				}
				desc = task_organize_levies_unused_farmland
			}
			triggered_desc = {
				trigger = {
					any_sub_realm_county = {
						marshal_task_1003_county_trigger = yes
					}
					martial < high_skill_rating
				}
				desc = task_organize_levies_insufficient_guards
			}
		}
	}
	
	council_owner_modifier = {
		name = task_organize_levies_modifier
		levy_size = 1
		garrison_size = 1
		levy_reinforcement_rate = 1
		scale = {
			value = marshal_organize_levies_modifier_value
			divide = 100
		}
	}

	on_start_task = {
		liege = {
			add_character_flag = {
				flag = no_marshal_side_effects_first_month
				days = 30
			}
		}
	}

	on_monthly = {
		liege = {
			if = {
				limit = {
					has_character_flag = no_marshal_side_effects_first_month
				}
				remove_character_flag = no_marshal_side_effects_first_month
			}
			else = {
				trigger_event = {
					on_action = organize_the_levies_marshal_task_side_effects
					days = { 1 30 }
				}
			}
		}
	}

	ai_will_do = {
		value = 1 # Always a good backup
	}
}

task_train_commanders = {
	position = councillor_marshal

	task_type = task_type_general
	task_progress = task_progress_infinite
	
	effect_desc = {
		### MaA Maintenance
		desc = task_train_commanders_maa_effect_desc
		triggered_desc = {
			trigger = {
				exists = scope:councillor_liege.dynasty
				scope:councillor_liege.dynasty = { has_dynasty_perk = erudition_legacy_5 }
			}
			desc = task_train_commanders_maa_erudition_bonus_desc
		}

		### Commander Improvement
		desc = task_train_commanders_effect_desc
		triggered_desc = {
			trigger = {
				exists = scope:councillor_liege.dynasty
				scope:councillor_liege.dynasty = { has_dynasty_perk = erudition_legacy_5 }
			}
			desc = task_train_commanders_commander_erudition_bonus_desc
		}

		desc = {
			desc = council_task_possible_side_effects
			triggered_desc = {
				trigger = {
					martial > mediocre_skill_rating
				}
				desc = task_train_commanders_commander_learns_from_commander
			}
			triggered_desc = {
				trigger = {
					martial > mediocre_skill_rating
				}
				desc = task_train_commanders_knight_improved
			}
			triggered_desc = {
				trigger = {
					martial < high_skill_rating
				}
				desc = task_train_commanders_commander_wounded
			}
			triggered_desc = {
				trigger = {
					martial < high_skill_rating
				}
				desc = task_train_commanders_commander_maimed
			}
			triggered_desc = {
				trigger = {
					martial < high_skill_rating
				}
				desc = task_train_commanders_commander_killed
			}
		}
	}
	
	council_owner_modifier = {
		name = task_train_commanders_modifier
		men_at_arms_maintenance = -1
		scale = marshal_train_commanders_maa_total
	}

	on_start_task = {
		liege = {
			add_character_flag = {
				flag = no_marshal_side_effects_first_month
				days = 30
			}
		}
	}

	on_monthly = {
		liege = {
			random = {
				chance = scope:councillor.marshal_train_commanders_monthly_chance_value
				trigger_event = {
					on_action = train_commanders_success_effect
				}
			}
			if = {
				limit = {
					has_character_flag = no_marshal_side_effects_first_month
				}
				remove_character_flag = no_marshal_side_effects_first_month
			}
			else = {
				trigger_event = {
					on_action = train_commanders_marshal_task_side_effects
					days = { 1 30 }
				}
			}
		}
	}

	ai_will_do = {
		value = 0
	}
}

task_increase_control = {
	position = councillor_marshal

	task_type = task_type_county
	county_target = realm
	ai_county_target = domain
	task_progress = task_progress_value
	task_current_value = scope:councillor_liege.marshal_increase_control_current_progress
	task_max_value = 100 # Highest possible county control
	highlight_own_realm = yes
	restart_on_finish = yes # If it "finishes" by removing a corruption modifier, there'll still be control to regain

	effect_desc = {
		desc = task_increase_control_effect_desc
		desc = {
			desc = council_task_possible_side_effects
			triggered_desc = {
				trigger = {
					martial > mediocre_skill_rating
				}
				desc = task_increase_control_increase_opinion
			}
			triggered_desc = {
				trigger = {
					martial > mediocre_skill_rating
				}
				desc = task_increase_control_baron_opinion_increase
			}
			triggered_desc = {
				trigger = {
					martial < high_skill_rating
				}
				desc = task_increase_control_lose_opinion
			}
			triggered_desc = {
				trigger = {
					martial < high_skill_rating
				}
				desc = task_increase_control_lose_control
			}
			triggered_desc = {
				trigger = {
					martial < high_skill_rating
				}
				desc = task_increase_control_baron_opinion_loss
			}
		}
	}
	
	# TOTAL PROGRESS MUST MATCH THE TOTAL OF THE COUNTY MODIFIERS BELOW
		# Add all new progress values to the script value marshal_increase_control_total
		# Note that for Increase Control, the County Modifiers apply the actual values, while progress is for player info only
	progress = {
		value = 0
		add = {
			value = marshal_increase_control_base
			desc = INCREASE_CONTROL_BASE
		}
		add = {
			value = scope:councillor.marshal_increase_county_control_monthly_change
			desc = SCALED_COUNCILLOR_MARTIAL_VALUE
		}
		# Relation Bonuses/Penalties
		if = { # Friend
			limit = {
				scope:councillor_liege = {
					has_relation_friend = scope:councillor
					NOT = { has_relation_best_friend = scope:councillor }
				}
			}
			add = {
				value = scope:councillor.marshal_increase_control_monthly_increase_friend_bonus
				desc = COUNCILLOR_IS_YOUR_FRIEND
			}
		}
		if = { # Best Friend
			limit = {
				scope:councillor_liege = {
					has_relation_best_friend = scope:councillor
				}
			}
			add = {
				value = scope:councillor.marshal_increase_control_monthly_increase_best_friend_bonus
				desc = COUNCILLOR_IS_YOUR_BEST_FRIEND
			}
		}
		if = { # Rival
			limit = {
				scope:councillor_liege = {
					has_relation_rival = scope:councillor
					NOT = { has_relation_nemesis = scope:councillor }
				}
			}
			add = {
				value = scope:councillor.marshal_increase_control_monthly_increase_rival_bonus
				desc = COUNCILLOR_IS_YOUR_RIVAL
			}
		}
		if = { # Nemesis
			limit = {
				scope:councillor_liege = {
					has_relation_nemesis = scope:councillor
				}
			}
			add = {
				value = scope:councillor.marshal_increase_control_monthly_increase_nemesis_bonus
				desc = COUNCILLOR_IS_YOUR_NEMESIS
			}
		}
		if = {
			limit = {
				scope:councillor_liege = { has_perk = strict_organization_perk }
			}
			add = {
				value = scope:councillor.marshal_increase_county_control_perk_bonus_monthly_change
				desc = INCREASE_CONTROL_PERK_BONUS_VALUE
			}
		}
		if = {
			limit = {
				exists = scope:councillor_liege.dynasty
				scope:councillor_liege.dynasty = { has_dynasty_perk = erudition_legacy_5 }
			}
			add = {
				value = scope:councillor.marshal_increase_county_control_erudition_bonus_monthly_change
				desc = ERUDITION_DYNASTY_PERK_BONUS_VALUE
			}
		}
		if = {
			limit = { exists = scope:county }
			multiply = {
				value = scope:county.county_control_rate_modifier
				desc = MARSHAL_COUNTY_SPEED_MODIFIERS
			}
		}
	}

	full_progress = marshal_increase_control_full_progress
	custom_other_loc = MARSHAL_COUNTY_SPEED_OTHER

	potential_county = {
		scope:county = {
			custom_description = {
				text = potential_county_control_task_trigger
				OR = {
					has_county_corruption_trigger = yes
					county_control < full_county_control
				}
			}
			county_control_rate_modifier > 0
		}
	}

	county_modifier = {
		name = marshal_increase_control_modifier
		monthly_county_control_change_add = 1
		scale = marshal_increase_control_total
	}

	on_finish_task_county = {
		if = {
			limit = {
				scope:county = {
					has_county_corruption_trigger = yes
				}
			}
			scope:county = {
				marshal_remove_random_county_corruption_modifier_effect = yes
				change_county_control = -25
			}
		}
	}

	on_start_task = {
		liege = {
			add_character_flag = {
				flag = no_marshal_side_effects_first_month
				days = 30
			}
		}
	}

	on_monthly_county = {
		random = {
			chance = scope:councillor.marshal_remove_county_corruption_chance
			scope:county = {
				marshal_remove_random_county_corruption_modifier_effect = yes
			}
		}
		if = {
			limit = {
				has_character_flag = no_marshal_side_effects_first_month
			}
			remove_character_flag = no_marshal_side_effects_first_month
		}
		else = {
			#Side effects
			scope:councillor_liege = {
				trigger_event = {
					on_action = task_increase_control_side_effects
					days = { 1 30 }
				}
			}
		}
	}

	ai_will_do = {
		value = 0
	}
}
