override protected BehaviorTreeResults Tick()
    {
        BehaviorVariableValue variableValue = tree.GetBehaviorVariableValue(BehaviorVariableName.String_RouteGUID);

        if (variableValue == null)
        {
            Debug.Log("No behavior variable for patrol route GUID found");
            return(new BehaviorTreeResults(BehaviorNodeState.Failure));
        }

        string         patrolRouteGUID = variableValue.StringVal;
        RouteGameLogic route           = RoutingUtil.FindRouteByGUID(tree, patrolRouteGUID);

        if (route == null)
        {
            Debug.Log("No route matching GUID found");
            return(new BehaviorTreeResults(BehaviorNodeState.Failure));
        }

        if (unit.lance == null)
        {
            Debug.Log("No lance for this unit found");
            return(new BehaviorTreeResults(BehaviorNodeState.Failure));
        }

        BehaviorVariableValue closestPointBV = tree.GetBehaviorVariableValue(BehaviorVariableName.Bool_RouteStartAtClosestPoint);
        BehaviorVariableValue forwardBV      = tree.GetBehaviorVariableValue(BehaviorVariableName.Bool_RouteFollowingForward);

        int routeTargetPoint = 0;

        if (closestPointBV.BoolVal)
        {
            routeTargetPoint = closestPointOnRouteToLance(route, unit.lance);
        }
        else
        {
            if (forwardBV.BoolVal)
            {
                routeTargetPoint = 0;
            }
            else
            {
                routeTargetPoint = route.routePointList.Length - 1;
            }
        }
        unit.lance.BehaviorVariables.SetVariable(BehaviorVariableName.Int_RouteTargetPoint, new BehaviorVariableValue(routeTargetPoint));
        unit.lance.BehaviorVariables.SetVariable(BehaviorVariableName.Bool_RouteStarted, new BehaviorVariableValue(true));
        unit.lance.BehaviorVariables.SetVariable(BehaviorVariableName.Bool_RouteCompleted, new BehaviorVariableValue(false));
        unit.lance.BehaviorVariables.SetVariable(BehaviorVariableName.Bool_RouteFollowingForward, new BehaviorVariableValue(forwardBV.BoolVal));

        return(new BehaviorTreeResults(BehaviorNodeState.Success));
    }
    RouteGameLogic getRoute()
    {
        BehaviorVariableValue variableValue = tree.GetBehaviorVariableValue(BehaviorVariableName.String_RouteGUID);

        if (variableValue == null)
        {
            // no route
            AIUtil.LogAI("No behavior variable for route GUID found");
            return(null);
        }

        string routeGUID = variableValue.StringVal;

        return(RoutingUtil.FindRouteByGUID(tree, routeGUID));
    }
    override protected BehaviorTreeResults Tick()
    {
        BehaviorVariableValue variableValue = tree.GetBehaviorVariableValue(BehaviorVariableName.String_RouteGUID);

        if (variableValue == null)
        {
            // No route, this is normal.
            return(new BehaviorTreeResults(BehaviorNodeState.Failure));
        }

        string patrolRouteGUID = variableValue.StringVal;

        RouteGameLogic route = RoutingUtil.FindRouteByGUID(tree, patrolRouteGUID);

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

        return(new BehaviorTreeResults(BehaviorNodeState.Success));
    }
예제 #4
0
        protected override BehaviorTreeResults Tick()
        {
            if (unit.HasMovedThisRound)
            {
                return(new BehaviorTreeResults(BehaviorNodeState.Failure));
            }

            BehaviorVariableValue targetLanceGuidValue = this.tree.GetCustomBehaviorVariableValue(FOLLOW_LANCE_TARGET_GUID_KEY);

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

            string targetLanceGuid = targetLanceGuidValue.StringVal;
            Lance  targetLance     = DestinationUtil.FindLanceByGUID(this.tree, targetLanceGuid);

            if (targetLance == null)
            {
                return(new BehaviorTreeResults(BehaviorNodeState.Failure));
            }
            List <AbstractActor> lanceMembers = AIUtil.GetLanceUnits(this.unit.Combat, this.unit.LanceId);

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

            if (this.waitForLance)
            {
                for (int i = 0; i < lanceMembers.Count; i++)
                {
                    AbstractActor abstractActor = lanceMembers[i] as AbstractActor;

                    if (abstractActor != null)
                    {
                        float lanceMemberTravelDistance = Mathf.Max(abstractActor.MaxWalkDistance, abstractActor.MaxSprintDistance);
                        travelDistance = Mathf.Min(travelDistance, lanceMemberTravelDistance);
                    }
                }
            }

            AbstractActor targetActor   = null;
            float         targetTonnage = 0;

            for (int i = 0; i < targetLance.unitGuids.Count; i++)
            {
                ITaggedItem itemByGUID = this.unit.Combat.ItemRegistry.GetItemByGUID(targetLance.unitGuids[i]);

                if (itemByGUID != null)
                {
                    AbstractActor abstractActor = itemByGUID as AbstractActor;

                    if (abstractActor != null && !abstractActor.IsDead)
                    {
                        if (abstractActor is Mech)
                        {
                            Mech mech = (Mech)abstractActor;
                            if (mech.tonnage > targetTonnage)
                            {
                                targetActor   = mech;
                                targetTonnage = mech.tonnage;
                            }
                        }
                        else if (abstractActor is Vehicle)
                        {
                            Vehicle vehicle = (Vehicle)abstractActor;
                            if (vehicle.tonnage > targetTonnage)
                            {
                                targetActor   = vehicle;
                                targetTonnage = vehicle.tonnage;
                            }
                        }
                    }
                }
            }

            if (targetActor == null)
            {
                Main.Logger.LogError("[MoveToFollowLanceNode] Target Actor is null");
                return(new BehaviorTreeResults(BehaviorNodeState.Failure));
            }

            Main.LogDebug($"[MoveToFollowLanceNode] Target to follow is '{targetActor.DisplayName} {targetActor.VariantName}'");

            bool shouldSprint = this.tree.GetCustomBehaviorVariableValue(FOLLOW_LANCE_SHOULD_SPRINT_KEY).BoolVal;

            shouldSprint = (!this.unit.HasAnyContactWithEnemy);
            shouldSprint = (this.unit.CurrentPosition - targetActor.CurrentPosition).magnitude > 200f; // sprint if the unit is over 200 metres away

            AbstractActor closestDetectedEnemy = AiUtils.GetClosestDetectedEnemy(this.unit, targetLance);
            Vector3       lookDirection        = (closestDetectedEnemy == null) ? targetActor.CurrentPosition : closestDetectedEnemy.CurrentPosition;

            MoveType moveType = (shouldSprint) ? MoveType.Sprinting : MoveType.Walking;

            this.unit.Pathing.UpdateAIPath(targetActor.CurrentPosition, lookDirection, moveType);

            Vector3 vectorToTarget   = this.unit.Pathing.ResultDestination - this.unit.CurrentPosition;
            float   distanceToTarget = vectorToTarget.magnitude;

            if (distanceToTarget > travelDistance)
            {
                // If the target is out of range, head in the direction of that unit to the maximum possible travel distance for this turn
                vectorToTarget = vectorToTarget.normalized * travelDistance;
            }

            // Ensure the units aren't crowded
            Vector3 targetDestination = RoutingUtil.Decrowd(this.unit.CurrentPosition + vectorToTarget, this.unit);

            targetDestination = RegionUtil.MaybeClipMovementDestinationToStayInsideRegion(this.unit, targetDestination);

            float followLanceZoneRadius = this.unit.BehaviorTree.GetCustomBehaviorVariableValue(FOLLOW_LANCE_ZONE_RADIUS_KEY).FloatVal;

            if (RoutingUtils.IsUnitInsideRadiusOfPoint(this.unit, targetActor.CurrentPosition, followLanceZoneRadius))
            {
                return(new BehaviorTreeResults(BehaviorNodeState.Failure));
            }

            this.unit.Pathing.UpdateAIPath(targetDestination, lookDirection, (shouldSprint) ? MoveType.Sprinting : MoveType.Walking);
            targetDestination = this.unit.Pathing.ResultDestination;
            float        maxCost             = this.unit.Pathing.MaxCost;
            PathNodeGrid currentGrid         = this.unit.Pathing.CurrentGrid;
            Vector3      targetActorPosition = targetActor.CurrentPosition;

            if ((currentGrid.GetValidPathNodeAt(targetDestination, maxCost) == null || (targetDestination - targetActor.CurrentPosition).magnitude > 1f) && this.unit.Combat.EncounterLayerData.inclineMeshData != null)
            {
                float maxSlope = Mathf.Tan(0.0174532924f * AIUtil.GetMaxSteepnessForAllLance(this.unit));
                List <AbstractActor> lanceUnits = AIUtil.GetLanceUnits(this.unit.Combat, this.unit.LanceId);
                targetDestination = this.unit.Combat.EncounterLayerData.inclineMeshData.GetDestination(this.unit.CurrentPosition, targetDestination, maxCost, maxSlope, this.unit, shouldSprint, lanceUnits, this.unit.Pathing.CurrentGrid, out targetActorPosition);
            }

            Vector3 currentPosition = this.unit.CurrentPosition;

            AIUtil.LogAI(string.Format("issuing order from [{0} {1} {2}] to [{3} {4} {5}] looking at [{6} {7} {8}]", new object[] {
                currentPosition.x,
                currentPosition.y,
                currentPosition.z,
                targetDestination.x,
                targetDestination.y,
                targetDestination.z,
                targetActorPosition.x,
                targetActorPosition.y,
                targetActorPosition.z
            }), "AI.DecisionMaking");

            return(new BehaviorTreeResults(BehaviorNodeState.Success)
            {
                orderInfo = new MovementOrderInfo(targetDestination, targetActorPosition)
                {
                    IsSprinting = shouldSprint
                },
                debugOrderString = string.Format("{0} moving toward destination: {1} dest: {2}", this.name, targetDestination, targetActor.CurrentPosition)
            });
        }
예제 #5
0
    override protected BehaviorTreeResults Tick()
    {
        string regionGUID = RegionUtil.GetStayInsideRegionGUID(unit);

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

        if (unit.IsInRegion(regionGUID))
        {
            return(new BehaviorTreeResults(BehaviorNodeState.Success));
        }

        ITaggedItem item = unit.Combat.ItemRegistry.GetItemByGUID(regionGUID);

        if (item == null)
        {
            Debug.Log("no item with GUID: " + regionGUID);
            return(new BehaviorTreeResults(BehaviorNodeState.Failure));
        }
        RegionGameLogic region = item as RegionGameLogic;

        if (region == null)
        {
            Debug.Log("item is not region: " + regionGUID);
            return(new BehaviorTreeResults(BehaviorNodeState.Failure));
        }

        // TODO: find a point inside the region, for now using the average of all vertices.
        int numPoints = region.regionPointList.Length;

        Vector3 destination = new Vector3();

        for (int pointIndex = 0; pointIndex < numPoints; ++pointIndex)
        {
            destination += region.regionPointList[pointIndex].Position;
        }
        if (numPoints == 0)
        {
            Debug.Log("no points in region: " + regionGUID);
            return(new BehaviorTreeResults(BehaviorNodeState.Failure));
        }

        destination = RoutingUtil.Decrowd(destination * 1.0f / numPoints, unit);
        destination = RegionUtil.MaybeClipMovementDestinationToStayInsideRegion(unit, destination);

        var cell = unit.Combat.MapMetaData.GetCellAt(destination);

        destination.y = cell.cachedHeight;

        if ((destination - unit.CurrentPosition).magnitude < 1)
        {
            // already close (should probably have been caught, above)
            return(new BehaviorTreeResults(BehaviorNodeState.Success));
        }

        bool shouldSprint = unit.CanSprint;

        //float sprintRange = Mathf.Max(unit.MaxSprintDistance, unit.MaxWalkDistance);
        float moveRange = unit.MaxWalkDistance;

        if ((destination - unit.CurrentPosition).magnitude < moveRange)
        {
            shouldSprint = false;
        }

        if (shouldSprint)
        {
            unit.Pathing.SetSprinting();
        }
        else
        {
            unit.Pathing.SetWalking();
        }

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

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

        var longRangeToShorRangeDistanceThreshold = unit.BehaviorTree.GetBehaviorVariableValue(BehaviorVariableName.Float_LongRangeToShortRangeDistanceThreshold).FloatVal;

        if (grid.GetValidPathNodeAt(destinationThisTurn, movementBudget) == null || (destinationThisTurn - destination).magnitude > longRangeToShorRangeDistanceThreshold)
        {
            List <AbstractActor> lanceUnits = AIUtil.GetLanceUnits(unit.Combat, unit.LanceId);
            List <Vector3>       path       = DynamicLongRangePathfinder.GetDynamicPathToDestination(destination, movementBudget, unit, shouldSprint, lanceUnits, grid, 0);
            if (path == null || path.Count == 0)
            {
                return(new BehaviorTreeResults(BehaviorNodeState.Failure));
            }

            destinationThisTurn = path[path.Count - 1];

            Vector2 flatDestination = new Vector2(destination.x, destination.z);

            float   currentClosestPointInRegionDistance = float.MaxValue;
            Vector3?closestPoint = null;

            for (int i = 0; i < path.Count; ++i)
            {
                Vector3 pointOnPath = path[i];

                if (RegionUtil.PointInRegion(unit.Combat, pointOnPath, regionGUID))
                {
                    var distance = (flatDestination - new Vector2(pointOnPath.x, pointOnPath.z)).sqrMagnitude;

                    if (distance < currentClosestPointInRegionDistance)
                    {
                        currentClosestPointInRegionDistance = distance;
                        closestPoint = pointOnPath;
                    }
                }
            }

            if (closestPoint != null)
            {
                destinationThisTurn = closestPoint.Value;
            }
        }

        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 = shouldSprint;
        results.orderInfo        = mvtOrderInfo;
        results.debugOrderString = string.Format("{0}: dest:{1} sprint:{2}", this.name, destination, mvtOrderInfo.IsSprinting);
        return(results);
    }
예제 #6
0
        protected override BehaviorTreeResults Tick()
        {
            if (unit.HasMovedThisRound)
            {
                return(new BehaviorTreeResults(BehaviorNodeState.Failure));
            }

            BehaviorVariableValue targetLanceGuidValue = this.tree.GetCustomBehaviorVariableValue(FOLLOW_LANCE_TARGET_GUID_KEY);

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

            string targetLanceGuid = targetLanceGuidValue.StringVal;
            Lance  targetLance     = DestinationUtil.FindLanceByGUID(this.tree, targetLanceGuid);

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

            AbstractActor closestEnemy = null;

            if (Main.Settings.AiSettings.FollowAiSettings.StopWhen == "OnEnemyVisible")
            {
                Main.LogDebug($"[MoveToFollowLanceNode] Looking for closest visible enemy.");
                closestEnemy = AiUtils.GetClosestVisibleEnemy(this.unit, targetLance);
            }
            else // OnEnemyDetected
            {
                Main.LogDebug($"[MoveToFollowLanceNode] Looking for closest detected enemy.");
                closestEnemy = AiUtils.GetClosestDetectedEnemy(this.unit, targetLance);
            }

            if (closestEnemy != null)
            {
                if (Main.Settings.AiSettings.FollowAiSettings.StopWhen != "WhenNotNeeded")
                {
                    Main.LogDebug($"[MoveToFollowLanceNode] Detected enemy. No longer following player mech.");
                    return(new BehaviorTreeResults(BehaviorNodeState.Failure));
                }
                else
                {
                    Main.LogDebug($"[MoveToFollowLanceNode] Enemies detected but keeping tight formation still. Following player mech.");
                }
            }
            else
            {
                Main.LogDebug($"[MoveToFollowLanceNode] No enemies detected. Following player mech.");
            }


            List <AbstractActor> lanceMembers = AIUtil.GetLanceUnits(this.unit.Combat, this.unit.LanceId);
            float travelDistance = Mathf.Max(this.unit.MaxSprintDistance, this.unit.MaxWalkDistance);

            if (this.waitForLance)
            {
                for (int i = 0; i < lanceMembers.Count; i++)
                {
                    AbstractActor abstractActor = lanceMembers[i] as AbstractActor;

                    if (abstractActor != null)
                    {
                        float lanceMemberTravelDistance = Mathf.Max(abstractActor.MaxWalkDistance, abstractActor.MaxSprintDistance);
                        travelDistance = Mathf.Min(travelDistance, lanceMemberTravelDistance);
                    }
                }
            }

            AbstractActor targetActor = GetMechToFollow(targetLance);

            if (targetActor == null)
            {
                Main.Logger.LogError("[MoveToFollowLanceNode] Target Actor is null");
                return(new BehaviorTreeResults(BehaviorNodeState.Failure));
            }

            Main.LogDebug($"[MoveToFollowLanceNode] Target to follow is '{targetActor.DisplayName} {targetActor.VariantName}'");

            bool shouldSprint = this.tree.GetCustomBehaviorVariableValue(FOLLOW_LANCE_SHOULD_SPRINT_KEY).BoolVal;

            Main.LogDebug($"[MoveToFollowLanceNode] Should sprint by behaviour value being set? '{shouldSprint}'");

            shouldSprint = (!this.unit.HasAnyContactWithEnemy);

            Main.LogDebug($"[MoveToFollowLanceNode] Should sprint by contact with enemy? '{shouldSprint}'");

            shouldSprint = (this.unit.CurrentPosition - targetActor.CurrentPosition).magnitude > Main.Settings.AiSettings.FollowAiSettings.MaxDistanceFromTargetBeforeSprinting; // sprint if the unit is over 200 metres away
            Main.LogDebug($"[MoveToFollowLanceNode] Is the follow target further than 200m? Should sprint? '{shouldSprint}'");

            Vector3  lookDirection = (closestEnemy == null) ? targetActor.CurrentPosition : closestEnemy.CurrentPosition;
            MoveType moveType      = (shouldSprint) ? MoveType.Sprinting : MoveType.Walking;

            this.unit.Pathing.UpdateAIPath(targetActor.CurrentPosition, lookDirection, moveType);

            Vector3 vectorToTarget   = this.unit.Pathing.ResultDestination - this.unit.CurrentPosition;
            float   distanceToTarget = vectorToTarget.magnitude;

            if (distanceToTarget > travelDistance)
            {
                Main.LogDebug($"[MoveToFollowLanceNode] Can't reach follow target in one go so will go as far as I can");
                // If the target is out of range, head in the direction of that unit to the maximum possible travel distance for this turn
                vectorToTarget = vectorToTarget.normalized * travelDistance;
            }

            // Ensure the units aren't crowded
            Vector3 targetDestination = RoutingUtil.Decrowd(this.unit.CurrentPosition + vectorToTarget, this.unit);

            targetDestination = RegionUtil.MaybeClipMovementDestinationToStayInsideRegion(this.unit, targetDestination);

            float followLanceZoneRadius = this.unit.BehaviorTree.GetCustomBehaviorVariableValue(FOLLOW_LANCE_ZONE_RADIUS_KEY).FloatVal;

            Main.LogDebug($"[MoveToFollowLanceNode] My follow zone radius is '{followLanceZoneRadius}'");
            if (RoutingUtils.IsUnitInsideRadiusOfPoint(this.unit, targetActor.CurrentPosition, followLanceZoneRadius))
            {
                Main.LogDebug($"[MoveToFollowLanceNode] ...and I am inside that zone.");
                return(new BehaviorTreeResults(BehaviorNodeState.Failure));
            }
            else
            {
                Main.LogDebug($"[MoveToFollowLanceNode] ...and I am NOT inside that zone.");
            }

            this.unit.Pathing.UpdateAIPath(targetDestination, lookDirection, moveType);
            targetDestination = this.unit.Pathing.ResultDestination;
            float        maxCost             = this.unit.Pathing.MaxCost;
            PathNodeGrid currentGrid         = this.unit.Pathing.CurrentGrid;
            Vector3      targetActorPosition = targetActor.CurrentPosition;

            // This method seems to get called all the time - this is meant to be a last resort method I think. I wonder why the other AI pathfinding methods don't work?
            if ((currentGrid.GetValidPathNodeAt(targetDestination, maxCost) == null || (targetDestination - targetActor.CurrentPosition).magnitude > 1f) && this.unit.Combat.EncounterLayerData.inclineMeshData != null)
            {
                float maxSlope = Mathf.Tan(0.0174532924f * AIUtil.GetMaxSteepnessForAllLance(this.unit));
                List <AbstractActor> lanceUnits = AIUtil.GetLanceUnits(this.unit.Combat, this.unit.LanceId);
                targetDestination = this.unit.Combat.EncounterLayerData.inclineMeshData.GetDestination(this.unit.CurrentPosition, targetDestination, maxCost, maxSlope, this.unit, shouldSprint, lanceUnits, this.unit.Pathing.CurrentGrid, out targetActorPosition);
            }

            Vector3 currentPosition = this.unit.CurrentPosition;

            AIUtil.LogAI(string.Format("issuing order from [{0} {1} {2}] to [{3} {4} {5}] looking at [{6} {7} {8}]", new object[] {
                currentPosition.x,
                currentPosition.y,
                currentPosition.z,
                targetDestination.x,
                targetDestination.y,
                targetDestination.z,
                targetActorPosition.x,
                targetActorPosition.y,
                targetActorPosition.z
            }), "AI.DecisionMaking");

            // TODO: Factor in jump mechs
            return(new BehaviorTreeResults(BehaviorNodeState.Success)
            {
                orderInfo = new MovementOrderInfo(targetDestination, targetActorPosition)
                {
                    IsSprinting = shouldSprint
                },
                debugOrderString = string.Format("{0} moving toward destination: {1} dest: {2}", this.name, targetDestination, targetActor.CurrentPosition)
            });
        }
예제 #7
0
        public float EvaluateUnit(AbstractActor unit)
        {
            if (unit.IsDead)
            {
                return(float.MinValue);
            }

            var routeGUID = unit.BehaviorTree.GetBVValue(BehaviorVariableName.String_RouteGUID).StringVal;

            if (string.IsNullOrEmpty(routeGUID))
            {
                return(float.MinValue);
            }

            // this is taken from decompiled HBS code, and not subject to license
            var route          = RoutingUtil.FindRouteByGUID(unit.BehaviorTree, routeGUID);
            var routeCompleted = unit.BehaviorTree.GetBVValue(BehaviorVariableName.Bool_RouteCompleted).BoolVal;

            if (routeCompleted)
            {
                return(route.routePointList.Length);
            }

            var isForward      = unit.BehaviorTree.GetBVValue(BehaviorVariableName.Bool_RouteFollowingForward).BoolVal;
            var nextRouteIndex = unit.BehaviorTree.GetBVValue(BehaviorVariableName.Int_RouteTargetPoint).IntVal;

            var num = 0f;

            if (!isForward)
            {
                num += route.routePointList.Length;
                num += route.routePointList.Length - nextRouteIndex;
            }
            else
            {
                num += nextRouteIndex;
            }

            var lastRouteIndex = nextRouteIndex + ((!isForward) ? 1 : -1);

            if (lastRouteIndex < 0)
            {
                lastRouteIndex = 1;
            }

            if (lastRouteIndex >= route.routePointList.Length)
            {
                lastRouteIndex = route.routePointList.Length - 1;
            }

            var magnitude  = (route.routePointList[nextRouteIndex].transform.position - unit.CurrentPosition).magnitude;
            var magnitude2 = (route.routePointList[lastRouteIndex].transform.position - unit.CurrentPosition).magnitude;
            var num3       = magnitude + magnitude2;
            var num4       = (num3 <= 0f) ? 0f : (magnitude2 / num3);

            return(num + num4);

            // This was my untested attempt at it, I gave up and used the game's version
            //var lastRouteIndex = Mathf.Clamp(nextRouteIndex + (isForward ? -1 : 1), 0, route.routePointList.Length - 1);

            //var nextPoint = route.routePointList[nextRouteIndex].transform.position;
            //var lastPoint = route.routePointList[lastRouteIndex].transform.position;

            //var distanceBetween = Vector3.Distance(nextPoint, lastPoint);
            //var distanceAway = Vector3.Distance(lastPoint, unit.CurrentPosition);

            //if (distanceAway > distanceBetween)
            //    distanceAway = distanceBetween;

            //var percentTowards = distanceAway / distanceBetween;
            //return lastRouteIndex + percentTowards;
        }
    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);
    }