﻿# Generates laamp_regula_transport_daughter contracts for all valid characters
regula_generate_transport_daughter_contracts = {
    #debug_log = "Regula: regula_generate_transport_daughter_contracts"
    if = {
        limit = {
            magister_alive_trigger = yes
            global_var:magister_character ?= {
                is_landless_adventurer = yes
            }
        }

        # TODO: Instead of iterating over every character, we should iterate over titles that
        #  are close to the player to improve performance
        every_living_character = {
            limit = {
                has_trait = devoted_trait_group
                global_var:magister_character = {
                    can_create_task_contract = {
                        type_name = laamp_regula_transport_daughter
                        employer = prev
                    }
                }
            }
            #debug_log = "Regula: Found a valid contract giver"


            save_scope_as = cur_employer

            global_var:magister_character = {
                create_task_contract = {
                    task_contract_employer = scope:cur_employer
                    task_contract_type = laamp_regula_transport_daughter
                    task_contract_tier = scope:cur_employer.task_contract_tier_value
                    location = scope:cur_employer.location
                    save_scope_as = new_contract
                }

                send_interface_message = {
                    type = msg_task_contract_created
                    title = laamp_regula_transport_daughter.t
                    desc = laamp_regula_transport_daughter.desc
                    #left_icon = scope:cur_employer
                    right_icon = scope:new_contract.var:task_contract_object
                }
            }
            scope:new_contract = {
                set_variable = {
                    name = retain_despite_distance
                    value = yes
                }
                set_variable = {
                    name = task_contract_employer
                    value = scope:cur_employer
                }
            }

        }
    }
}

# Checks if new location matches the contract destination for Regula-related contracts
regula_on_travel_contract_check_arrival = {
    #trigger contract completion event, if you have a contract with this location as contract destination
    #debug_log = "Regula: Checking if we have a contract with this location as destination"

    # root.current_travel_plan.current_location = {
    #     save_scope_as = tmp_current_location
    #     debug_log = "Regula: Current travel plan location"
    #     debug_log_scopes = yes
    # }

    if = {
        limit = {
            is_landless_adventurer = yes
            any_character_active_contract = {
                var:task_contract_destination ?= root.current_travel_plan.current_location

                # This is a check needed to ensure that task_contract_object is still travels with player. However it seems to be redundant.
                # trigger_if = {
                #     limit = { exists = var:task_contract_object }
                #     var:task_contract_object = {
                #         current_travel_plan ?= {
                #             travel_plan_owner = root
                #         }
                #     }
                # }
            }
        }
        #debug_log = "Regula: Has contract with this location as destination"
        every_character_active_contract = {
            limit = {
                var:task_contract_destination ?= root.current_travel_plan.current_location
                trigger_if = {
                    limit = { exists = var:task_contract_object }
                    var:task_contract_object = {
                        current_travel_plan ?= {
                            travel_plan_owner = root
                        }
                    }
                }
            }
            save_scope_as = task_contract
            if = {
                limit = {
                    has_task_contract_type = laamp_regula_transport_daughter
                }
                regular_generate_transport_daughter_contract_arrived_to_location = yes
            }
        }
    }
}

# Advances progress of laamp_regula_transport_daughter contract
regular_generate_transport_daughter_contract_arrived_to_location = {
    if = {
        limit = {
            var:visited_journey_final_location ?= yes
        }
        # The contract is already completed - do nothing
        # This branch is needed to prevent the contract from being completed twice
    }
    else_if = {
        limit = {
            var:visited_journey_target_location ?= yes
        }

        # Prevent triggering the event twice
        scope:task_contract = {
            set_variable = visited_journey_final_location
        }
        
        # We have returned home - end of contract
        task_contract_taker = {
            trigger_event = regula_travel_contract_event.0028
        }
    }
    else_if = {
        limit = {
            var:visited_pickup_location ?= yes
        }
        # We arrived to the leyline
        task_contract_taker = {
            trigger_event = regula_travel_contract_event.0022
        }
    }
    else_if = {
        # We arrived to pickup location
        task_contract_taker = {
            trigger_event = regula_travel_contract_event.0021
        }
    }
}


