﻿PoW_commander_story = {
	on_setup = {
		story_owner = {
			set_variable = {
				name = commander_story
				value = scope:story
			}
		}
		set_variable = {
			name = commander
			value = story_owner
		}
		set_variable = {
			name = army_owner
			value = story_owner.commanding_army.army_owner
		}
		set_variable = {
			name = army
			value =  story_owner.commanding_army
		}
		set_variable = { name = location value = story_owner.location }
		if = {
			limit = { PoW_debug = yes }
			debug_log = "PoW_commander_story on_setup"
			debug_log = "PoW_commander_story  on_setup Commander [THIS.Var('commander').Char.GetShortUINameNoTooltip] Owner [THIS.Var('owner').Char.GetNameNoTooltip] Army [THIS.Var('army').Army.GetNameNoTooltip] Location [THIS.Var('commander').Char.GetCurrentLocation.GetNameNoTooltip]"
		}
	}
	on_end = {
		if = {
			limit = { PoW_debug = yes }
			debug_log = "PoW_commander_story on_end"
			story_owner = { debug_log_scopes = no }
		}
		if = {
			limit = { story_owner = { is_alive = yes }}
			story_owner = {
				if = {
					limit = { is_landed = yes }
					capital_province = { save_scope_as = home }	
				}
				else_if = {
					limit = { exists = court_owner }
					court_owner.capital_province = { save_scope_as = home }
				}
				if = {
					limit = { exists = scope:home }
					start_travel_plan = {
						destination = scope:home
						players_use_planner = no
						return_trip = no
						on_arrival_on_action = PoW_on_travel_plan_complete_prisoner_transit
					}
					current_travel_plan = {
						save_scope_as = PoW_commander_return_travel
						set_variable = block_travel_plan_home_button
						set_variable = block_travel_plan_customize_button
						set_variable = PoW_gather_commanders
						if = {
							limit = { NOT = { has_game_rule = PoW_gather_commanders_enabled_travel_events }}
							set_variable = FF_vanilla_on_travel_plan_movement_blocker
							set_variable = FF_vanilla_on_travel_plan_arrival_blocker
							set_variable = FF_vanilla_on_travel_plan_start_blocker
							set_variable = FF_vanilla_on_travel_plan_complete_blocker
							set_variable = FF_vanilla_on_travel_plan_abort_blocker
							set_variable = FF_vanilla_on_travel_leader_removed_blocker
						}
					}
					if = {
						limit = { any_in_list = { variable = PoW count > 0 }}
						scope:PoW_commander_return_travel = {
							set_variable = PoW_prisoner_transit
							
							if = {
								limit = { NOT = { scope:story.var:army_owner.capital_province = scope:home }}
								add_travel_waypoint = scope:story.var:army_owner.capital_province
							}
						}
					}
				}
				else = {
					PoW_liberate_prisoners = yes
				}
			}
		}
	}

	on_owner_death = {
		if = {
			limit = { PoW_debug = yes }
			debug_log = "PoW_commander_story on_owner_death"
			story_owner = { debug_log_scopes = no }
		}
		story_owner = { PoW_liberate_prisoners = yes }
		end_story = yes
	}

	effect_group = {
		days = 1
		trigger = { story_owner = { is_commanding_army = yes }}
		first_valid = {
			triggered_effect = {
				trigger = { NOT = { var:army ?= story_owner.commanding_army }}
				effect = {
					if = {
						limit = { PoW_debug = yes }
						debug_log = "PoW_commander_story triggered_effect Wrong Army [THIS.Var('commander').Char.GetShortUINameNoTooltip] Owner [THIS.Var('owner').Char.GetNameNoTooltip]"
					}
					set_variable = {
						name = army
						value = story_owner.commanding_army
					}
					if = {
						limit = { var:army.army_owner != var:army_owner }
						set_variable = {
							name = army_owner
							value = var:army.army_owner
						}
					}
					if = {
						limit = { var:location != var:army.location }
						set_variable = {
							name = location
							value = var:army.location
						}
						story_owner = {
							set_location = {
								location = scope:story.var:location
								stick_to_location = yes
							}
							every_in_list = {
								variable = PoW
								set_location = {
									location = scope:story.var:location
									stick_to_location = yes
								}
								set_variable = {
									name = PoW_current_location
									value = scope:story.var:location
								}
							}
						}
					}
				}
			}
			triggered_effect = {
				trigger = { var:army.army_owner != var:army_owner }
				effect = {
					if = {
						limit = { PoW_debug = yes }
						debug_log = "PoW_commander_story triggered_effect Wrong Army Owner [THIS.Var('commander').Char.GetShortUINameNoTooltip] Owner [THIS.Var('owner').Char.GetNameNoTooltip]"
					}
					set_variable = {
						name = army_owner
						value = var:army.army_owner
					}
					if = {
						limit = { var:location != var:army.location }
						set_variable = {
							name = location
							value = var:army.location
						}
						story_owner = {
							set_location = {
								location = scope:story.var:location
								stick_to_location = yes
							}
							every_in_list = {
								variable = PoW
								set_location = {
									location = scope:story.var:location
									stick_to_location = yes
								}
								set_variable = {
									name = PoW_current_location
									value = scope:story.var:location
								}
							}
						}
					}
				}
			}
			triggered_effect = {
				trigger = { var:location != var:army.location }
				effect = {
					if = {
						limit = { PoW_debug = yes }
						debug_log = "PoW_commander_story triggered_effect Wrong Army Location [THIS.Var('commander').Char.GetShortUINameNoTooltip] Owner [THIS.Var('owner').Char.GetNameNoTooltip]"
					}
					set_variable = {
						name = location
						value = var:army.location
					}
					story_owner = {
						set_location = {
							location = scope:story.var:location
							stick_to_location = yes
						}
						every_in_list = {
							variable = PoW
							set_location = {
								location = scope:story.var:location
								stick_to_location = yes
							}
							set_variable = {
								name = PoW_current_location
								value = scope:story.var:location
							}
						}
					}
				}
			}
			triggered_effect = {
				trigger = { var:location != story_owner.location }
				effect = {
					if = {
						limit = { PoW_debug = yes }
						debug_log = "PoW_commander_story triggered_effect Wrong Commander Location [THIS.Var('commander').Char.GetShortUINameNoTooltip] Owner [THIS.Var('owner').Char.GetNameNoTooltip]"
					}
					story_owner = {
						set_location = {
							location = scope:story.var:location
							stick_to_location = yes
						}
						every_in_list = {
							variable = PoW
							set_location = {
								location = scope:story.var:location
								stick_to_location = yes
							}
							set_variable = {
								name = PoW_current_location
								value = scope:story.var:location
							}
						}
					}
				}
			}
		}
	}
	effect_group = {
		days = 1
		trigger = { story_owner = { is_commanding_army = no }}
		triggered_effect = {
			effect = {
				if = {
					limit = { PoW_debug = yes }
					debug_log = "PoW_commander_story triggered_effect Not leading army Commander [THIS.Var('commander').Char.GetShortUINameNoTooltip] Owner [THIS.Var('owner').Char.GetNameNoTooltip]"
				}
				end_story = yes
			}
		}
	}
}