﻿# Travel movement
# Trigger per province moved
on_travel_plan_movement = {
    on_actions = {
        regula_check_travel_movement_on_action
		regula_spawn_regula_contracts_movement_on_action
        delay = { days = 2 } # Takes place at same time as Adventurer scout on action
        regula_search_for_women_on_action
        delay = { days = 4 } # Events below will be fired after 4 days of entering new location, to not clash with vanilla events
        regula_travel_enchant_guest
    }
}

regula_check_travel_movement_on_action = {
    trigger = {
        has_trait = magister_trait_group
    }

    effect = {
        #debug_log = "Regula: movement on action"
        regula_on_travel_contract_check_arrival = yes
    }
}

regula_check_travel_plan_arrival_on_action = {
    trigger = {
        has_trait = magister_trait_group
    }

    effect = {
        #debug_log = "Regula: trevel plan arrival on action"
        regula_on_travel_contract_check_arrival = yes
    }
}

regula_check_travel_plan_complete_on_action = {
    trigger = {
        has_trait = magister_trait_group
    }

    effect = {
        #debug_log = "Regula: trevel plan complete on action"
        regula_on_travel_contract_check_arrival = yes
    }
}

regula_travel_enchant_guest = {
    trigger = {

        # Can enchant
        has_trait = magister_trait_group
        piety >= regula_fascinare_cost

        # Has escort contract with female
        any_character_active_contract = {
            has_task_contract_type = laamp_transport_vip
            var:task_contract_object = {
                is_adult = yes
                is_male = no
                is_regula_devoted_trigger = no
                is_imprisoned = no
            }
        }

    }

    # 20% chance of happening per province travelled
    random_events = {
        chance_of_no_event = {
            value = 80
        }
        100 = regula_travel_events.1000
    }
}

regula_search_for_women_on_action = {
	trigger = {
        current_travel_plan ?= { has_travel_option = regula_search_for_women_option }
        NOT = { has_character_flag = had_regula_search_for_women_event }
	}
	random_events = {
		chance_of_no_event = {
			value = 50
		}

        # Generic Search events
        # 3001 - 3100
            100 = regula_travel_events.3001   # Find a beautiful Bard 
            100 = regula_travel_events.3002   # Find a travelling Knight attacked by bandits
            100 = regula_travel_events.3003   # Tavern wench
            100 = regula_travel_events.3004   # Farm Girl
            100 = regula_travel_events.3005   # Village Idiot
            100 = regula_travel_events.3006   # Regula Cultist

        # Specific Terrain/Holding Search events
        # 3101 - 3200
            100 = regula_travel_events.3101   # Hungry Poacher in an empty holding
            # 100 = regula_travel_events.3102   # Holy Lady - TODO
            # 100 = regula_travel_events.3103   # Mysterious Nomad - TODO

            # Sea events
                100 = regula_travel_events.3120   # Lady lost at Sea
                100 = regula_travel_events.3121   # Stowaway
                # 100 = regula_travel_events.3124   # Fisherwomen
                # 100 = regula_travel_events.3124   # Captains Daughter

        # War Search events
        # 3201 - 3300
        100 = regula_travel_events.3201   # Starving Beggers
        #100 = regula_travel_events.3202   # Wounded Women - TODO
        100 = regula_travel_events.3203   # Caged Captives

        # Legend / Disease Search events
        # 3301 - 3400
        # 100 = regula_travel_events.3301   # Nurse - TODO

        # Longer Event Chains
        # These require more events, and aren't just single one and done events
        # 3401 - 3500
        # 100 = regula_travel_events.3401   # Pirate Captain
	}
}

regula_spawn_regula_contracts_movement_on_action = {
    effect = {
        # Spawn Regula contracts on the move
		if = {
			limit = {
				current_travel_plan = { is_travel_with_domicile = yes }
			}
			# Chance of seeing Regula contracts on the road as you go.
			if = {
				limit = {
					is_ai = no
					location = { is_sea_province = no }
				}
				random = {
					chance = 5
					save_scope_value_as = {
						name = toggle_distance_ignore
						value = yes
					}
					populate_location_with_regula_contracts_effect = {
						AREA_CHAR = location.county.holder
						AMOUNT = 1
					}
				}
			}
		}
    }
}

# Reached a travel plan destination (possibly intermediate)
on_travel_plan_arrival = {
    on_actions = {
        regula_check_travel_plan_arrival_on_action
    }
}

# Completed a travel plan (arrived at final destination, or completed through script)
# A travel plan ends either via 'on_travel_plan_complete' or 'on_travel_plan_abort'
on_travel_plan_complete = {
    on_actions = {
        regula_check_travel_plan_complete_on_action
        regula_spawn_regula_contracts_stopped_on_action
    }
}

regula_spawn_regula_contracts_stopped_on_action = {
    effect = {
        # Spawn Regula contracts when we stop moving 
        if = {
            limit = { player_adventurer_sufficient_distance_for_contract_spawn_trigger = yes }
            save_temporary_scope_as = char_temp
            populate_location_with_regula_contracts_effect = {
                AREA_CHAR = scope:char_temp
                AMOUNT = 1
            }
        }
    }
}