int closestPointOnRouteToLance(RouteGameLogic route, Lance lance)
    {
        Vector3 lanceCenter = Vector3.zero;
        int     count       = 0;

        for (int unitIndex = 0; unitIndex < lance.unitGuids.Count; ++unitIndex)
        {
            string      unitGUID = lance.unitGuids[unitIndex];
            ITaggedItem item     = tree.battleTechGame.Combat.ItemRegistry.GetItemByGUID(unitGUID);
            if (item == null)
            {
                continue;
            }
            AbstractActor unit = item as AbstractActor;
            if (unit == null)
            {
                continue;
            }
            if (unit.IsDead)
            {
                continue;
            }
            ++count;
            lanceCenter += unit.CurrentPosition;
        }

        if (count == 0)
        {
            return(0);
        }

        lanceCenter *= 1.0f / count;

        float bestDist  = float.MaxValue;
        int   bestIndex = -1;

        // Now iterate over all points, to find closest point to this center point.

        for (int waypointIndex = 0; waypointIndex < route.routePointList.Length; ++waypointIndex)
        {
            RoutePointGameLogic point = route.routePointList[waypointIndex];
            float dist = (point.Position - lanceCenter).magnitude;
            if (dist < bestDist)
            {
                bestIndex = waypointIndex;
                bestDist  = dist;
            }
        }
        return(bestIndex);
    }
    override protected BehaviorTreeResults Tick()
    {
        BehaviorVariableValue variableValue = tree.GetBehaviorVariableValue(BehaviorVariableName.String_LancePostAttackDestinationGUID);

        if (variableValue == null)
        {
            return(new BehaviorTreeResults(BehaviorNodeState.Failure));
        }

        string destinationGUID = variableValue.StringVal;

        RoutePointGameLogic destination = DestinationUtil.FindDestinationByGUID(tree, destinationGUID);

        if (destination == null)
        {
            return(new BehaviorTreeResults(BehaviorNodeState.Failure));
        }

        return(new BehaviorTreeResults(BehaviorNodeState.Success));
    }
    override protected BehaviorTreeResults Tick()
    {
        BehaviorVariableValue variableValue = tree.GetBehaviorVariableValue(destinationBVarName);

        if (variableValue == null)
        {
            return(new BehaviorTreeResults(BehaviorNodeState.Failure));
        }

        string destinationGUID = variableValue.StringVal;

        RoutePointGameLogic destination = DestinationUtil.FindDestinationByGUID(tree, destinationGUID);

        if (destination == null)
        {
            return(new BehaviorTreeResults(BehaviorNodeState.Failure));
        }

        float sprintDistance = Mathf.Max(unit.MaxSprintDistance, unit.MaxWalkDistance);

        if (waitForLance)
        {
            for (int lanceMemberIndex = 0; lanceMemberIndex < unit.lance.unitGuids.Count; ++lanceMemberIndex)
            {
                ITaggedItem item = unit.Combat.ItemRegistry.GetItemByGUID(unit.lance.unitGuids[lanceMemberIndex]);
                if (item == null)
                {
                    continue;
                }
                AbstractActor lanceUnit = item as AbstractActor;
                if (lanceUnit == null)
                {
                    continue;
                }
                float unitMoveDistance = Mathf.Max(lanceUnit.MaxWalkDistance, lanceUnit.MaxSprintDistance);
                sprintDistance = Mathf.Min(sprintDistance, unitMoveDistance);
            }
        }

        MoveType moveType = tree.GetBehaviorVariableValue(BehaviorVariableName.Bool_RouteShouldSprint).BoolVal ?
                            MoveType.Sprinting : MoveType.Walking;

        unit.Pathing.UpdateAIPath(destination.Position, destination.Position, moveType);

        Vector3 offset = unit.Pathing.ResultDestination - unit.CurrentPosition;

        if (offset.magnitude > sprintDistance)
        {
            offset = offset.normalized * sprintDistance;
        }

        Vector3 destinationThisTurn = RoutingUtil.Decrowd(unit.CurrentPosition + offset, unit);

        destinationThisTurn = RegionUtil.MaybeClipMovementDestinationToStayInsideRegion(unit, destinationThisTurn);

        float destinationRadius = unit.BehaviorTree.GetBehaviorVariableValue(BehaviorVariableName.Float_RouteWaypointRadius).FloatVal;

        List <AbstractActor> unitsToWaitFor = new List <AbstractActor>();

        if (waitForLance)
        {
            if (unit.lance != null)
            {
                for (int lanceGUIDIndex = 0; lanceGUIDIndex < unit.lance.unitGuids.Count; ++lanceGUIDIndex)
                {
                    string      guid = unit.lance.unitGuids[lanceGUIDIndex];
                    ITaggedItem item = unit.Combat.ItemRegistry.GetItemByGUID(guid);
                    if (item != null)
                    {
                        AbstractActor lanceUnit = item as AbstractActor;
                        if (lanceUnit != null)
                        {
                            unitsToWaitFor.Add(lanceUnit);
                        }
                    }
                }
            }
            else
            {
                unitsToWaitFor.Add(unit);
            }
        }
        if (RoutingUtil.AllUnitsInsideRadiusOfPoint(unitsToWaitFor, destination.Position, destinationRadius))
        {
            tree.RemoveBehaviorVariableValue(destinationBVarName);
        }

        bool isSprinting = tree.GetBehaviorVariableValue(BehaviorVariableName.Bool_RouteShouldSprint).BoolVal;

        unit.Pathing.UpdateAIPath(destinationThisTurn, destination.Position, isSprinting ? MoveType.Sprinting : MoveType.Walking);
        destinationThisTurn = unit.Pathing.ResultDestination;

        float        movementBudget = unit.Pathing.MaxCost;
        PathNodeGrid grid           = unit.Pathing.CurrentGrid;
        Vector3      successorPoint = destination.Position;

        if ((grid.GetValidPathNodeAt(destinationThisTurn, movementBudget) == null) ||
            ((destinationThisTurn - destination.Position).magnitude > 1.0f))
        {
            // can't get all the way to the destination.
            if (unit.Combat.EncounterLayerData.inclineMeshData != null)
            {
                float maxSteepnessRatio         = Mathf.Tan(Mathf.Deg2Rad * AIUtil.GetMaxSteepnessForAllLance(unit));
                List <AbstractActor> lanceUnits = AIUtil.GetLanceUnits(unit.Combat, unit.LanceId);
                destinationThisTurn = unit.Combat.EncounterLayerData.inclineMeshData.GetDestination(
                    unit.CurrentPosition,
                    destinationThisTurn,
                    movementBudget,
                    maxSteepnessRatio,
                    unit,
                    isSprinting,
                    lanceUnits,
                    unit.Pathing.CurrentGrid,
                    out successorPoint);
            }
        }

        Vector3 cur = unit.CurrentPosition;

        AIUtil.LogAI(string.Format("issuing order from [{0} {1} {2}] to [{3} {4} {5}] looking at [{6} {7} {8}]",
                                   cur.x, cur.y, cur.z,
                                   destinationThisTurn.x, destinationThisTurn.y, destinationThisTurn.z,
                                   successorPoint.x, successorPoint.y, successorPoint.z
                                   ));

        BehaviorTreeResults results      = new BehaviorTreeResults(BehaviorNodeState.Success);
        MovementOrderInfo   mvtOrderInfo = new MovementOrderInfo(destinationThisTurn, successorPoint);

        mvtOrderInfo.IsSprinting = isSprinting;
        results.orderInfo        = mvtOrderInfo;
        results.debugOrderString = string.Format("{0} moving toward destination: {1} dest: {2}", this.name, destinationThisTurn, destination.Position);
        return(results);
    }