Example #1
0
        public static bool Init(RegionCostCalculator __instance, CellRect destination, HashSet <Region> destRegions, TraverseParms parms, int moveTicksCardinal, int moveTicksDiagonal, ByteGrid avoidGrid, Area allowedArea, bool drafted)
        {
            regionGridField(__instance)        = map(__instance).regionGrid.DirectGrid;
            traverseParmsField(__instance)     = parms;
            destinationCellField(__instance)   = destination.CenterCell;
            moveTicksCardinalField(__instance) = moveTicksCardinal;
            moveTicksDiagonalField(__instance) = moveTicksDiagonal;
            avoidGridField(__instance)         = avoidGrid;
            allowedAreaField(__instance)       = allowedArea;
            draftedField(__instance)           = drafted;

            //temps?
            regionMinLink(__instance).Clear();
            distances(__instance).Clear();
            linkTargetCells(__instance).Clear();
            minPathCosts(__instance).Clear();
            if (!queueDict.TryGetValue(__instance, out FastPriorityQueueRegionLinkQueueEntry2 queue))
            {
                queue = new FastPriorityQueueRegionLinkQueueEntry2(new DistanceComparer2());
            }

            foreach (Region destRegion in destRegions)
            {
                int minPathCost = __instance.RegionMedianPathCost(destRegion);
                for (int i = 0; i < destRegion.links.Count; i++)
                {
                    RegionLink regionLink = destRegion.links[i];
                    if (!regionLink.GetOtherRegion(destRegion).Allows(traverseParmsField(__instance), isDestination: false))
                    {
                        continue;
                    }

                    int num = funcRegionLinkDistanceIRI(__instance, destinationCellField(__instance), regionLink, minPathCost);
                    if (distances(__instance).TryGetValue(regionLink, out int value))
                    {
                        if (num < value)
                        {
                            linkTargetCells(__instance)[regionLink] = funcGetLinkTargetCell(__instance, destinationCellField(__instance), regionLink);
                        }

                        num = Math.Min(value, num);
                    }
                    else
                    {
                        linkTargetCells(__instance)[regionLink] = funcGetLinkTargetCell(__instance, destinationCellField(__instance), regionLink);
                    }

                    distances(__instance)[regionLink] = num;
                }

                GetPreciseRegionLinkDistances(__instance, destRegion, destination, preciseRegionLinkDistances(__instance));
                for (int j = 0; j < preciseRegionLinkDistances(__instance).Count; j++)
                {
                    Pair <RegionLink, int> pair  = preciseRegionLinkDistances(__instance)[j];
                    RegionLink             first = pair.First;
                    int num2 = distances(__instance)[first];
                    int num3;
                    if (pair.Second > num2)
                    {
                        distances(__instance)[first] = pair.Second;
                        num3 = pair.Second;
                    }
                    else
                    {
                        num3 = num2;
                    }

                    queue.Push(new RegionLinkQueueEntry2(destRegion, first, num3, num3));
                }
            }
            return(false);
        }
Example #2
0
        public static bool GetRegionDistance(RegionCostCalculator __instance, ref int __result, Region region, out RegionLink minLink)
        {
            if (regionMinLink(__instance).TryGetValue(region.id, out minLink))
            {
                __result = distances(__instance)[minLink];
                return(false);
            }
            FastPriorityQueueRegionLinkQueueEntry2 queue;

            if (!queueDict.TryGetValue(__instance, out queue))
            {
                queue = new FastPriorityQueueRegionLinkQueueEntry2(new DistanceComparer2());
            }
            while (queue.Count != 0)
            {
                RegionLinkQueueEntry2 regionLinkQueueEntry = queue.Pop();
                int num = distances(__instance)[regionLinkQueueEntry.Link];
                if (regionLinkQueueEntry.Cost != num)
                {
                    continue;
                }

                Region otherRegion = regionLinkQueueEntry.Link.GetOtherRegion(regionLinkQueueEntry.From);
                if (otherRegion == null || !otherRegion.valid)
                {
                    continue;
                }

                int num2 = 0;
                if (otherRegion.door != null)
                {
                    num2 = PathFinder.GetBuildingCost(otherRegion.door, traverseParms(__instance), traverseParms(__instance).pawn);
                    if (num2 == int.MaxValue)
                    {
                        continue;
                    }
                    //num2 += OctileDistance(1, 0);
                    num2 += funcOctileDistance(__instance, 1, 0);
                }

                int minPathCost = __instance.RegionMedianPathCost(otherRegion);
                for (int i = 0; i < otherRegion.links.Count; i++)
                {
                    RegionLink regionLink = otherRegion.links[i];
                    if (regionLink == null || regionLink.GetOtherRegion(otherRegion) == null || regionLink == regionLinkQueueEntry.Link || !regionLink.GetOtherRegion(otherRegion).type.Passable())
                    {
                        continue;
                    }

                    //int val = (otherRegion.door != null) ? num2 : RegionLinkDistance(regionLinkQueueEntry.Link, regionLink, minPathCost);
                    int val = (otherRegion.door != null) ? num2 : funcRegionLinkDistanceRRI(__instance, regionLinkQueueEntry.Link, regionLink, minPathCost);
                    val = Math.Max(val, 1);
                    int num3 = num + val;
                    //int estimatedPathCost = MinimumRegionLinkDistance(destinationCell, regionLink) + num3;
                    int estimatedPathCost = funcMinimumRegionLinkDistance(__instance, destinationCell(__instance), regionLink) + num3;
                    if (distances(__instance).TryGetValue(regionLink, out int value))
                    {
                        if (num3 < value)
                        {
                            distances(__instance)[regionLink] = num3;
                            if (!queueDict.TryGetValue(__instance, out queue))
                            {
                                queue = new FastPriorityQueueRegionLinkQueueEntry2(new DistanceComparer2());
                            }
                            queue.Push(new RegionLinkQueueEntry2(otherRegion, regionLink, num3, estimatedPathCost));
                        }
                    }
                    else
                    {
                        if (!queueDict.TryGetValue(__instance, out queue))
                        {
                            queue = new FastPriorityQueueRegionLinkQueueEntry2(new DistanceComparer2());
                        }
                        distances(__instance).Add(regionLink, num3);
                        queue.Push(new RegionLinkQueueEntry2(otherRegion, regionLink, num3, estimatedPathCost));
                    }
                }

                if (!regionMinLink(__instance).ContainsKey(otherRegion.id))
                {
                    regionMinLink(__instance).Add(otherRegion.id, regionLinkQueueEntry.Link);
                    if (otherRegion == region)
                    {
                        minLink  = regionLinkQueueEntry.Link;
                        __result = regionLinkQueueEntry.Cost;
                        return(false);
                    }
                }
            }

            __result = 10000;
            return(false);
        }