Esempio n. 1
0
        public ThingWithComps GetNearestTrainingWeaponOfType(ThingDef weaponType)
        {
            ThingRequest req = ThingRequest.ForDef(weaponType);
            int          regionsSeen;

            return((ThingWithComps)GenClosest.RegionwiseBFSWorker(base.TargetA.Thing.Position, pawn.Map, req, PathEndMode.OnCell, TraverseParms.For(pawn), (Thing x) => pawn.CanReserve(x), null, 0, 12, 50f, out regionsSeen, RegionType.Set_Passable, ignoreEntirelyForbiddenRegions: true));
        }
Esempio n. 2
0
        public ThingWithComps GetNearestTrainingWeapon(Thing currentWeapon)
        {
            ThingWithComps result = null;

            if (currentWeapon?.def.IsMeleeWeapon ?? false)
            {
                result = GetNearestTrainingWeaponMelee();
            }
            if (currentWeapon != null && !currentWeapon.def.IsMeleeWeapon)
            {
                result = GetNearestTrainingWeaponRanged();
            }
            if (currentWeapon == null && !HugsLibUtility.HasDesignation(base.TargetThingA, CombatTrainingDefOf.TrainCombatDesignation))
            {
                if (HugsLibUtility.HasDesignation(base.TargetThingA, CombatTrainingDefOf.TrainCombatDesignationMeleeOnly))
                {
                    result = GetNearestTrainingWeaponMelee();
                }
                else if (HugsLibUtility.HasDesignation(base.TargetThingA, CombatTrainingDefOf.TrainCombatDesignationRangedOnly))
                {
                    result = GetNearestTrainingWeaponRanged();
                }
            }
            if (currentWeapon == null && HugsLibUtility.HasDesignation(base.TargetThingA, CombatTrainingDefOf.TrainCombatDesignation))
            {
                ThingRequest req = ThingRequest.ForGroup(ThingRequestGroup.Weapon);
                result = (ThingWithComps)GenClosest.RegionwiseBFSWorker(base.TargetA.Thing.Position, pawn.Map, req, PathEndMode.OnCell, TraverseParms.For(pawn), (Thing x) => CombatTrainingDefOf.TrainingWeapons.DescendantThingDefs.Contains(x.def) && pawn.CanReserve(x), null, 0, 12, 50f, out int _, RegionType.Set_Passable, ignoreEntirelyForbiddenRegions: true);
            }
            return(result);
        }
        private ThingWithComps GetNearestTrainingWeaponOfType(ThingDef weaponType)
        {
            ThingRequest   request = ThingRequest.ForDef(weaponType);
            ThingWithComps nearestTrainingWeapon = (ThingWithComps)GenClosest.RegionwiseBFSWorker(this.TargetA.Thing.Position, pawn.Map, request, PathEndMode.OnCell, TraverseParms.For(pawn, Danger.Deadly, TraverseMode.ByPawn, false), (Thing x) => pawn.CanReserve(x, 1, -1, null, false), null, 0, 12, 50f, out int regionsSearched, RegionType.Set_Passable, true);

            return(nearestTrainingWeapon);
        }
Esempio n. 4
0
        public static bool ClosestThingReachable(ref Thing __result,
                                                 IntVec3 root,
                                                 Map map,
                                                 ThingRequest thingReq,
                                                 PathEndMode peMode,
                                                 TraverseParms traverseParams,
                                                 float maxDistance           = 9999f,
                                                 Predicate <Thing> validator = null,
                                                 IEnumerable <Thing> customGlobalSearchSet = null,
                                                 int searchRegionsMin                = 0,
                                                 int searchRegionsMax                = -1,
                                                 bool forceAllowGlobalSearch         = false,
                                                 RegionType traversableRegionTypes   = RegionType.Set_Passable,
                                                 bool ignoreEntirelyForbiddenRegions = false)
        {
            bool flag1 = searchRegionsMax < 0 | forceAllowGlobalSearch;

            if (!flag1 && customGlobalSearchSet != null)
            {
                Log.ErrorOnce("searchRegionsMax >= 0 && customGlobalSearchSet != null && !forceAllowGlobalSearch. customGlobalSearchSet will never be used.", 634984, false);
            }
            if (!flag1 && !thingReq.IsUndefined && !thingReq.CanBeFoundInRegion)
            {
                Log.ErrorOnce("ClosestThingReachable with thing request group " + thingReq.group + " and global search not allowed. This will never find anything because this group is never stored in regions. Either allow global search or don't call this method at all.", 518498981, false);
                __result = null;
                return(false);
            }
            if (EarlyOutSearch(root, map, thingReq, customGlobalSearchSet, validator))
            {
                __result = null;
                return(false);
            }
            Thing thing = null;
            bool  flag2 = false;

            if (!thingReq.IsUndefined && thingReq.CanBeFoundInRegion)
            {
                int maxRegions = searchRegionsMax > 0 ? searchRegionsMax : 30;
                int regionsSeen;
                thing = GenClosest.RegionwiseBFSWorker(root, map, thingReq, peMode, traverseParams, validator, null, searchRegionsMin, maxRegions, maxDistance, out regionsSeen, traversableRegionTypes, ignoreEntirelyForbiddenRegions);
                flag2 = thing == null && regionsSeen < maxRegions;
            }
            if (thing == null & flag1 && !flag2)
            {
                if (traversableRegionTypes != RegionType.Set_Passable)
                {
                    Log.ErrorOnce("ClosestThingReachable had to do a global search, but traversableRegionTypes is not set to passable only. It's not supported, because Reachability is based on passable regions only.", 14384767, false);
                }
                // Predicate<Thing> validator1 = t => map.reachability.CanReach(root, t, peMode, traverseParams) && (validator == null || validator(t));
                Predicate <Thing>   validator1 = t => Reachability_Patch.CanReach2(map.reachability, root, t, peMode, traverseParams) && (validator == null || validator(t));
                IEnumerable <Thing> things     = customGlobalSearchSet ?? map.listerThings.ThingsMatching(thingReq);
                thing = GenClosest.ClosestThing_Global(root, things, maxDistance, validator1, null);
            }
            __result = thing;
            return(false);
        }
Esempio n. 5
0
        public static Thing ClosestThingReachable2(
            IntVec3 root, Map map, ThingRequest thingReq, PathEndMode peMode, TraverseParms traverseParams, float maxDistance = 9999f, Predicate <Thing> validator = null, IEnumerable <Thing> customGlobalSearchSet = null, int searchRegionsMin = 0, int searchRegionsMax = -1, bool forceAllowGlobalSearch = false, RegionType traversableRegionTypes = RegionType.Set_Passable, bool ignoreEntirelyForbiddenRegions = false)
        {
            bool flag = searchRegionsMax < 0 || forceAllowGlobalSearch;

            if (!flag && customGlobalSearchSet != null)
            {
                Log.ErrorOnce("searchRegionsMax >= 0 && customGlobalSearchSet != null && !forceAllowGlobalSearch. customGlobalSearchSet will never be used.", 634984);
            }

            if (!flag && !thingReq.IsUndefined && !thingReq.CanBeFoundInRegion)
            {
                Log.ErrorOnce(string.Concat("ClosestThingReachable with thing request group ", thingReq.group, " and global search not allowed. This will never find anything because this group is never stored in regions. Either allow global search or don't call this method at all."), 518498981);
            }

            if (EarlyOutSearch(root, map, thingReq, customGlobalSearchSet, validator))
            {
                return(null);
            }

            Thing thing = null;
            bool  flag2 = false;

            if (!thingReq.IsUndefined && thingReq.CanBeFoundInRegion)
            {
                int num = (searchRegionsMax > 0) ? searchRegionsMax : 30;
                thing = GenClosest.RegionwiseBFSWorker(root, map, thingReq, peMode, traverseParams, validator, null, searchRegionsMin, num, maxDistance, out int regionsSeen, traversableRegionTypes, ignoreEntirelyForbiddenRegions);
                flag2 = (thing == null && regionsSeen < num);
            }

            if (thing == null && flag && !flag2)
            {
                if (traversableRegionTypes != RegionType.Set_Passable)
                {
                    Log.ErrorOnce("ClosestThingReachable had to do a global search, but traversableRegionTypes is not set to passable only. It's not supported, because Reachability is based on passable regions only.", 14384767);
                }

                bool validator2(Thing t)
                {
                    if (validator != null && !validator(t))
                    {
                        return(false);
                    }
                    if (!map.reachability.CanReach(root, t, peMode, traverseParams))
                    {
                        return(false);
                    }
                    return(true);
                }

                IEnumerable <Thing> searchSet = customGlobalSearchSet ?? map.listerThings.ThingsMatching(thingReq);
                thing = GenClosest.ClosestThing_Global(root, searchSet, maxDistance, validator2);
            }

            return(thing);
        }
        /*
         * Returns the nearest training weapon.  Enforces training weapons of the same type (melee or ranged) of the
         * weapon passed in, unless the pawn is unarmed.
         */
        private ThingWithComps GetNearestTrainingWeapon(Thing currentWeapon)
        {
            ThingWithComps nearestTrainingWeapon = null;

            // If the pawn has a melee weapon, look for a training knife.
            if (currentWeapon != null && currentWeapon.def.IsMeleeWeapon)
            {
                nearestTrainingWeapon = GetNearestTrainingWeaponMelee();
            }
            // If the pawn has a ranged weapon, look for a training ranged weapon.
            if (currentWeapon != null && !currentWeapon.def.IsMeleeWeapon)
            {
                nearestTrainingWeapon = GetNearestTrainingWeaponRanged();
            }

            // If the pawn does not have a weapon, and the dummy is restricted, look for the appropriate weapon type.
            if (currentWeapon == null && !this.TargetThingA.HasDesignation(CombatTrainingDefOf.TrainCombatDesignation))
            {
                if (this.TargetThingA.HasDesignation(CombatTrainingDefOf.TrainCombatDesignationMeleeOnly))
                {
                    nearestTrainingWeapon = GetNearestTrainingWeaponMelee();
                }
                else if (this.TargetThingA.HasDesignation(CombatTrainingDefOf.TrainCombatDesignationRangedOnly))
                {
                    nearestTrainingWeapon = GetNearestTrainingWeaponRanged();
                }
            }

            // If the pawn does not have a weapon, and the dummy is not restricted, look for the closest training weapon of any kind.
            if (currentWeapon == null && this.TargetThingA.HasDesignation(CombatTrainingDefOf.TrainCombatDesignation))
            {
                ThingRequest request = ThingRequest.ForGroup(ThingRequestGroup.Weapon);
                nearestTrainingWeapon = (ThingWithComps)GenClosest.RegionwiseBFSWorker(this.TargetA.Thing.Position,
                                                                                       pawn.Map, request, PathEndMode.OnCell,
                                                                                       TraverseParms.For(pawn, Danger.Deadly, TraverseMode.ByPawn, false),
                                                                                       (Thing x) => CombatTrainingDefOf.TrainingWeapons.DescendantThingDefs.Contains(x.def) &&
                                                                                       pawn.CanReserve(x, 1, -1, null, false), null, 0, 12, 50f, out int regionsSearched,
                                                                                       RegionType.Set_Passable, true);
            }

            return(nearestTrainingWeapon);
        }
        public static Thing ClosestThingReachable_JT(IntVec3 root, ThingRequest thingReq, PathEndMode peMode, TraverseParms traverseParams, float maxDistance = 9999f, Predicate <Thing> validator = null, IEnumerable <Thing> customGlobalSearchSet = null, int searchRegionsMax = -1, bool forceGlobalSearch = false)
        {
            //customGlobalSearchSet in context of hauling is list of things to be hauled
            //forceGlobalSearch is true when customGlobalSearchSet is not null
            //searchRegionMax is only used for cleaning
            ProfilerThreadCheck.BeginSample("ClosestThingReachable");
            //Guessing if searchRegionsMax is > 0, customGlobalSearchSet is not suppose to be used.
            if (searchRegionsMax > 0 && customGlobalSearchSet != null && !forceGlobalSearch)
            {
                Log.ErrorOnce("searchRegionsMax > 0 && customGlobalSearchSet != null && !forceGlobalSearch. customGlobalSearchSet will never be used.", 634984);
            }
            //There is no null check for thingReq, always not null?
            //if thingReq group == nothing ||
            //if there is thingReq and customGlobalSearchSet is null and there are things matching thingReq
            if (EarlyOutSearch_JT(root, thingReq, customGlobalSearchSet))
            {
                ProfilerThreadCheck.EndSample();
                return(null);
            }
            //Use either searchRegionsMax or customGlobalSearchSet
            //We're interested in thingReq.group == HaulableEver, HaulableAlways, HaulableEverOrMinifiable
            //This means customGlobalSearch has to have something (when there are such items on map) --> correct
            //Actual stuff begins?
            Thing thing = null;

            //IsUndefined == singleDef == null && thingReq group == Undefined
            if (!thingReq.IsUndefined)
            {
                //The debug bellow only resuted in group == Pawn, 7 times with 3 pawns all set only to haul, perhaps dogs loving?
                //Therefore probably ignore this if
                //Log.Message("First if was called. " + thingReq.group.ToString());
                //Hauling: searchRegionsMax should be -1 --> maxRegions = 30
                int maxRegions = (searchRegionsMax <= 0) ? 30 : searchRegionsMax;
                thing = GenClosest.RegionwiseBFSWorker(root, thingReq, peMode, traverseParams, validator, null, 0, maxRegions, maxDistance);
            }
            if (thing == null && (searchRegionsMax < 0 || forceGlobalSearch))
            {
                //validator = (Thing t) => !t.IsForbidden(pawn) && scanner.HasJobOnThing(pawn, t);
                //Debug bellow gives thingReq group to be Undefined, skips first
                //Log.Message("First if resulted in null " + thingReq.group.ToString());
                Predicate <Thing>   validator2 = (Thing t) => root.CanReach(t, peMode, traverseParams) && (validator == null || validator(t));
                IEnumerable <Thing> searchSet  = customGlobalSearchSet ?? Find.ListerThings.ThingsMatching(thingReq);
                //Main start of my code
                List <Thing> degrade   = new List <Thing>();
                List <Thing> undegrade = new List <Thing>();
                //Seperate into degrade or not
                foreach (Thing t in searchSet)
                {
                    if (t.GetStatValue(StatDefOf.DeteriorationRate) > 0)
                    {
                        degrade.Add(t);
                    }
                    else
                    {
                        undegrade.Add(t);
                    }
                }
                //Loop through all haul areas in order
                foreach (Area a in AreaFinder.getHaulAreas())
                {
                    //Check if got degradable item
                    thing = GenClosest.ClosestThing_Global(root, AreaFinder.searcher(a, degrade), maxDistance, validator2);
                    if (thing != null)
                    {
                        break;
                    }

                    //Check if got undegradable item
                    thing = GenClosest.ClosestThing_Global(root, AreaFinder.searcher(a, undegrade), maxDistance, validator2);
                    if (thing != null)
                    {
                        break;
                    }
                }
                if (thing == null)
                {
                    thing = GenClosest.ClosestThing_Global(root, degrade, maxDistance, validator2);
                    if (thing == null)
                    {
                        thing = GenClosest.ClosestThing_Global(root, undegrade, maxDistance, validator2);
                    }
                }

                /* old 50 cell code
                 *              thing = GenClosest.ClosestThing_Global(root, degrade, 50f, validator2); //If there is degradable with 50 cells
                 * if (thing == null)
                 * {
                 *  thing = GenClosest.ClosestThing_Global(root, undegrade, 50f, validator2);//If there is undegradable with 50 cells
                 *  if (thing == null)
                 *  {
                 *      thing = GenClosest.ClosestThing_Global(root, degrade, maxDistance, validator2); //If there is degradable
                 *      if (thing == null)
                 *      {
                 *          thing = GenClosest.ClosestThing_Global(root, undegrade, maxDistance, validator2); //If there is undegradable
                 *      }
                 *  }
                 * }
                 */
                //Main end of my code
            }

            /*
             * if (thing != null)
             * {
             *  Log.Message(thing.def.defName);
             * }
             * else {
             *  Log.Message("Resulted in null");
             * }*/
            ProfilerThreadCheck.EndSample();
            return(thing);
        }