private void CalculatePlantsWhichCanGrowAt(IntVec3 c, List <ThingDef> outPlants, Map map, float plantDensity)
        {
            outPlants.Clear();
            List <ThingDef> allWildPlants = map.Biome.AllWildPlants;

            for (int j = 0; j < allWildPlants.Count; j++)
            {
                ThingDef thingDef = allWildPlants[j];
                if (thingDef.CanEverPlantAt(c, map))
                {
                    if (thingDef.plant.wildOrder == map.Biome.LowestWildAndCavePlantOrder)
                    {
                        goto IL_00f8;
                    }
                    float num = 7f;
                    if (thingDef.plant.GrowsInClusters)
                    {
                        num = Math.Max(num, (float)((float)thingDef.plant.wildClusterRadius * 1.5));
                    }
                    if (this.EnoughLowerOrderPlantsNearby(c, plantDensity, num, thingDef))
                    {
                        goto IL_00f8;
                    }
                }
                continue;
IL_00f8:
                outPlants.Add(thingDef);
            }
        }
Ejemplo n.º 2
0
 public static Plant TryReproduceInto(IntVec3 dest, ThingDef plantDef, Map map)
 {
     if (!plantDef.CanEverPlantAt(dest, map))
     {
         return(null);
     }
     if (!GenPlant.SnowAllowsPlanting(dest, map))
     {
         return(null);
     }
     return((Plant)GenSpawn.Spawn(plantDef, dest, map));
 }
Ejemplo n.º 3
0
 public void TryPlantNew(IntVec3 c, ThingDef plantDef)
 {
     if (plantDef.blueprintDef != null && Utilities.SeedsPleaseActive && plantDef.blueprintDef.category == ThingCategory.Item)
     {
         if (!TryPlantNewSeedsPleaseActive(plantDef))
         {
             return;
         }
     }
     if (plantDef.CanEverPlantAt(c, Map) && PlantUtility.AdjacentSowBlocker(plantDef, c, Map) == null)
     {
         GenPlace.TryPlaceThing(ThingMaker.MakeThing(plantDef), c, Map, ThingPlaceMode.Direct);
     }
 }
Ejemplo n.º 4
0
        public static bool TryFindRandomCellToPlantInFromOffMap(ThingDef plantDef, Map map, out IntVec3 plantCell)
        {
            Predicate <IntVec3> validator = delegate(IntVec3 c)
            {
                if (c.Roofed(map))
                {
                    return(false);
                }
                if (!plantDef.CanEverPlantAt(c, map))
                {
                    return(false);
                }
                Room room = c.GetRoom(map, RegionType.Set_Passable);
                return(room != null && room.TouchesMapEdge);
            };

            return(CellFinder.TryFindRandomEdgeCellWith(validator, map, CellFinder.EdgeRoadChance_Animal, out plantCell));
        }
Ejemplo n.º 5
0
 private void CalculatePlantsWhichCanGrowAt(IntVec3 c, List <ThingDef> outPlants, bool cavePlants, float plantDensity)
 {
     outPlants.Clear();
     if (cavePlants)
     {
         for (int i = 0; i < WildPlantSpawner.allCavePlants.Count; i++)
         {
             if (WildPlantSpawner.allCavePlants[i].CanEverPlantAt(c, this.map))
             {
                 outPlants.Add(WildPlantSpawner.allCavePlants[i]);
             }
         }
     }
     else
     {
         List <ThingDef> allWildPlants = this.map.Biome.AllWildPlants;
         for (int j = 0; j < allWildPlants.Count; j++)
         {
             ThingDef thingDef = allWildPlants[j];
             if (thingDef.CanEverPlantAt(c, this.map))
             {
                 if (thingDef.plant.wildOrder != this.map.Biome.LowestWildAndCavePlantOrder)
                 {
                     float num = 7f;
                     if (thingDef.plant.GrowsInClusters)
                     {
                         num = Math.Max(num, (float)thingDef.plant.wildClusterRadius * 1.5f);
                     }
                     if (!this.EnoughLowerOrderPlantsNearby(c, plantDensity, num, thingDef))
                     {
                         goto IL_FF;
                     }
                 }
                 outPlants.Add(thingDef);
             }
             IL_FF :;
         }
     }
 }
Ejemplo n.º 6
0
        private static bool JobOnCellTest(WorkGiverDef def, Pawn pawn, IntVec3 c, bool forced = false)
        {
            Map map = pawn.Map;

            if (c.IsForbidden(pawn))
            {
#if DEBUG
                Log.Warning("IsForbidden");
#endif
                PlantSowing_Cache.ReregisterObject(map, c, awaitingPlantCellsMapDict);
                return(false);
            }

            if (!PlantUtility.GrowthSeasonNow(c, map, forSowing: true))
            {
#if DEBUG
                Log.Warning("GrowthSeasonNow");
#endif
                return(false);
            }

            ThingDef localWantedPlantDef = WorkGiver_Grower.CalculateWantedPlantDef(c, map);
            WorkGiver_GrowerSow.wantedPlantDef = localWantedPlantDef;
            if (localWantedPlantDef == null)
            {
#if DEBUG
                Log.Warning("localWantedPlantDef==null");
#endif
                return(false);
            }

            List <Thing> thingList = c.GetThingList(map);
            bool         flag      = false;
            for (int i = 0; i < thingList.Count; i++)
            {
                Thing thing = thingList[i];
                if (thing.def == localWantedPlantDef)
                {
#if DEBUG
                    Log.Warning("thing.def == localWantedPlantDef... RemoveObjectFromAwaitingHaulingHashSets");
#endif
                    PlantSowing_Cache.ReregisterObject(map, c, awaitingPlantCellsMapDict);
                    //JumboCellCache.AddObjectToActionableObjects(map, c, c, awaitingPlantCellsMapDict);
                    return(false);
                }

                if ((thing is Blueprint || thing is Frame) && thing.Faction == pawn.Faction)
                {
                    flag = true;
                }
            }

            if (flag)
            {
                Thing edifice = c.GetEdifice(map);
                if (edifice == null || edifice.def.fertility < 0f)
                {
#if DEBUG
                    Log.Warning("fertility");
#endif
                    return(false);
                }
            }

            if (localWantedPlantDef.plant.cavePlant)
            {
                if (!c.Roofed(map))
                {
#if DEBUG
                    Log.Warning("cavePlant");
#endif
                    return(false);
                }

                if (map.glowGrid.GameGlowAt(c, ignoreCavePlants: true) > 0f)
                {
#if DEBUG
                    Log.Warning("GameGlowAt");
#endif
                    return(false);
                }
            }

            if (localWantedPlantDef.plant.interferesWithRoof && c.Roofed(pawn.Map))
            {
                return(false);
            }

            Plant plant = c.GetPlant(map);
            if (plant != null && plant.def.plant.blockAdjacentSow)
            {
                if (!pawn.CanReserve(plant, 1, -1, null, forced) || plant.IsForbidden(pawn))
                {
#if DEBUG
                    Log.Warning("blockAdjacentSow");
#endif
                    return(false);
                }

                return(true);                // JobMaker.MakeJob(JobDefOf.CutPlant, plant);
            }

            Thing thing2 = PlantUtility.AdjacentSowBlocker(localWantedPlantDef, c, map);
            if (thing2 != null)
            {
                Plant plant2 = thing2 as Plant;
                if (plant2 != null && pawn.CanReserve(plant2, 1, -1, null, forced) && !plant2.IsForbidden(pawn))
                {
                    IPlantToGrowSettable plantToGrowSettable = plant2.Position.GetPlantToGrowSettable(plant2.Map);
                    if (plantToGrowSettable == null || plantToGrowSettable.GetPlantDefToGrow() != plant2.def)
                    {
                        return(true);                        // JobMaker.MakeJob(JobDefOf.CutPlant, plant2);
                    }
                }
#if DEBUG
                Log.Warning("AdjacentSowBlocker");
#endif
                PlantSowing_Cache.ReregisterObject(map, c, awaitingPlantCellsMapDict);
                return(false);
            }

            if (localWantedPlantDef.plant.sowMinSkill > 0 && pawn.skills != null && pawn.skills.GetSkill(SkillDefOf.Plants).Level < localWantedPlantDef.plant.sowMinSkill)
            {
#if DEBUG
                Log.Warning("UnderAllowedSkill");
#endif
                return(false);
            }

            for (int j = 0; j < thingList.Count; j++)
            {
                Thing thing3 = thingList[j];
                if (!thing3.def.BlocksPlanting())
                {
                    continue;
                }

                if (!pawn.CanReserve(thing3, 1, -1, null, forced))
                {
#if DEBUG
                    Log.Warning("!CanReserve");
#endif
                    PlantSowing_Cache.ReregisterObject(map, c, awaitingPlantCellsMapDict);

                    return(false);
                }

                if (thing3.def.category == ThingCategory.Plant)
                {
                    if (!thing3.IsForbidden(pawn))
                    {
                        return(true);                        // JobMaker.MakeJob(JobDefOf.CutPlant, thing3);
                    }
#if DEBUG
                    Log.Warning("Plant IsForbidden");
#endif
                    PlantSowing_Cache.ReregisterObject(map, c, awaitingPlantCellsMapDict);

                    return(false);
                }

                if (thing3.def.EverHaulable)
                {
                    return(true);                    //HaulAIUtility.HaulAsideJobFor(pawn, thing3);
                }
#if DEBUG
                Log.Warning("EverHaulable");
#endif
                PlantSowing_Cache.ReregisterObject(map, c, awaitingPlantCellsMapDict);
                return(false);
            }

            if (!localWantedPlantDef.CanEverPlantAt(c, map))
            {
#if DEBUG
                Log.Warning("CanEverPlantAt_NewTemp");
#endif
                PlantSowing_Cache.ReregisterObject(map, c, awaitingPlantCellsMapDict);
                return(false);
            }

            if (!PlantUtility.GrowthSeasonNow(c, map, forSowing: true))
            {
#if DEBUG
                Log.Warning("GrowthSeasonNow");
#endif
                return(false);
            }

            if (!pawn.CanReserve(c, 1, -1, null, forced))
            {
#if DEBUG
                Log.Warning("!pawn.CanReserve(c");
#endif
                PlantSowing_Cache.ReregisterObject(map, c, awaitingPlantCellsMapDict);
                //JumboCellCache.AddObjectToActionableObjects(map, c, c, awaitingPlantCellsMapDict);
                return(false);
            }

            //Job job = JobMaker.MakeJob(JobDefOf.Sow, c);
            //job.plantDefToSow = wantedPlantDef;
            return(true);            //job;
        }
Ejemplo n.º 7
0
        public static bool TryFindReproductionDestination(IntVec3 source, ThingDef plantDef, SeedTargFindMode mode, Map map, out IntVec3 foundCell)
        {
            float radius = -1f;

            if (mode == SeedTargFindMode.Reproduce)
            {
                radius = plantDef.plant.reproduceRadius;
            }
            else if (mode == SeedTargFindMode.MapGenCluster)
            {
                radius = plantDef.plant.WildClusterRadiusActual;
            }
            else if (mode == SeedTargFindMode.MapEdge)
            {
                radius = 40f;
            }
            else if (mode == SeedTargFindMode.Cave)
            {
                radius = plantDef.plant.WildClusterRadiusActual;
            }
            int      num      = 0;
            int      num2     = 0;
            float    num3     = 0f;
            CellRect cellRect = CellRect.CenteredOn(source, Mathf.RoundToInt(radius));

            cellRect.ClipInsideMap(map);
            for (int i = cellRect.minZ; i <= cellRect.maxZ; i++)
            {
                for (int j = cellRect.minX; j <= cellRect.maxX; j++)
                {
                    IntVec3 c2    = new IntVec3(j, 0, i);
                    Plant   plant = c2.GetPlant(map);
                    if (plant != null && (mode != SeedTargFindMode.Cave || plant.def.plant.cavePlant))
                    {
                        num++;
                        if (plant.def == plantDef)
                        {
                            num2++;
                        }
                    }
                    num3 += c2.GetTerrain(map).fertility;
                }
            }
            float num4  = (mode != SeedTargFindMode.Cave) ? map.Biome.plantDensity : 0.5f;
            float num5  = num3 * num4;
            bool  flag  = (float)num > num5;
            bool  flag2 = (float)num > num5 * 1.25f;

            if (flag2)
            {
                foundCell = IntVec3.Invalid;
                return(false);
            }
            if (mode != SeedTargFindMode.MapGenCluster && mode != SeedTargFindMode.Cave)
            {
                BiomeDef curBiome = map.Biome;
                float    num6     = curBiome.AllWildPlants.Sum((ThingDef pd) => curBiome.CommonalityOfPlant(pd));
                float    num7     = curBiome.CommonalityOfPlant(plantDef) / num6;
                float    num8     = curBiome.CommonalityOfPlant(plantDef) * plantDef.plant.wildCommonalityMaxFraction / num6;
                float    num9     = num5 * num8;
                if ((float)num2 > num9)
                {
                    foundCell = IntVec3.Invalid;
                    return(false);
                }
                float num10 = num5 * num7;
                bool  flag3 = (float)num2 < num10 * 0.5f;
                if (flag && !flag3)
                {
                    foundCell = IntVec3.Invalid;
                    return(false);
                }
            }
            Predicate <IntVec3> validator = (IntVec3 c) => plantDef.CanEverPlantAt(c, map) && (!plantDef.plant.cavePlant || GenPlantReproduction.GoodRoofForCavePlantReproduction(c, map)) && GenPlant.SnowAllowsPlanting(c, map) && source.InHorDistOf(c, radius) && GenSight.LineOfSight(source, c, map, true, null, 0, 0);

            return(CellFinder.TryFindRandomCellNear(source, map, Mathf.CeilToInt(radius), validator, out foundCell));
        }
Ejemplo n.º 8
0
        public Job JobOnCell(Pawn pawn, IntVec3 c)
        {
            if (c.IsForbidden(pawn))
            {
                return(null);
            }
            if (!GenPlant.GrowthSeasonNow(c))
            {
                return(null);
            }
            if (wantedPlantDef == null)
            {
                DetermineWantedPlantDef(c);
                if (wantedPlantDef == null)
                {
                    return(null);
                }
            }
            Plant plant = c.GetPlant();

            if (plant != null)
            {
                if (plant.def == wantedPlantDef)
                {
                    return(null);
                }
                if (plant.def.plant.blockAdjacentSow)
                {
                    if (!pawn.CanReserve(plant, 1) || plant.IsForbidden(pawn))
                    {
                        return(null);
                    }
                    return(new Job(JobDefOf.CutPlant, plant));
                }
            }
            Thing thing = GenPlant.AdjacentSowBlocker(wantedPlantDef, c);

            if (thing != null)
            {
                Plant plant2 = thing as Plant;
                if (plant2 != null && pawn.CanReserve(plant2, 1) && !plant2.IsForbidden(pawn))
                {
                    Zone_Growing zone_Growing = Find.ZoneManager.ZoneAt(plant2.Position) as Zone_Growing;
                    if (zone_Growing == null || zone_Growing.GetPlantDefToGrow() != plant2.def)
                    {
                        return(new Job(JobDefOf.CutPlant, plant2));
                    }
                }
                return(null);
            }
            List <Thing> list = Find.ThingGrid.ThingsListAt(c);
            int          i    = 0;

            while (i < list.Count)
            {
                Thing thing2 = list[i];
                if (thing2.def == wantedPlantDef)
                {
                    return(null);
                }
                if (thing2.def.BlockPlanting)
                {
                    if (!pawn.CanReserve(thing2, 1))
                    {
                        return(null);
                    }
                    if (thing2.def.category == ThingCategory.Plant)
                    {
                        if (!thing2.IsForbidden(pawn))
                        {
                            return(new Job(JobDefOf.CutPlant, thing2));
                        }
                        return(null);
                    }
                    else
                    {
                        if (thing2.def.EverHaulable)
                        {
                            return(HaulAIUtility.HaulAsideJobFor(pawn, thing2));
                        }
                        return(null);
                    }
                }
                else
                {
                    i++;
                }
            }
            if (!wantedPlantDef.CanEverPlantAt(c) || !GenPlant.GrowthSeasonNow(c) || !pawn.CanReserve(c, 1))
            {
                return(null);
            }
            return(new Job(JobDefOf.Sow, c)
            {
                plantDefToSow = wantedPlantDef
            });
        }
Ejemplo n.º 9
0
        // Not synced with CanGiveJob
        public override Job JobOnCell(Pawn pawn, IntVec3 c)
        {
            if (c.IsForbidden(pawn))
            {
                return(null);
            }
            if (!GenPlant.GrowthSeasonNow(c, pawn.Map))
            {
                return(null);
            }
            if (wantedPlantDef == null)
            {
                wantedPlantDef = CalculateWantedPlantDef(c, pawn.Map);
                if (wantedPlantDef == null)
                {
                    return(null);
                }
            }
            List <Thing> thingList = c.GetThingList(pawn.Map);

            for (int i = 0; i < thingList.Count; i++)
            {
                Thing thing = thingList[i];
                if (thing.def == wantedPlantDef)
                {
                    return(null);
                }
                if ((thing is Blueprint || thing is Frame) && thing.Faction == pawn.Faction)
                {
                    return(null);
                }
            }
            Plant plant = c.GetPlant(pawn.Map);

            if (plant != null && plant.def.plant.blockAdjacentSow)
            {
                if (!pawn.CanReserve(plant) || plant.IsForbidden(pawn))
                {
                    return(null);
                }
                return(new Job(JobDefOf.CutPlant, plant));
            }
            else
            {
                Thing thing2 = GenPlant.AdjacentSowBlocker(wantedPlantDef, c, pawn.Map);
                if (thing2 != null)
                {
                    if (thing2 is Plant plant2 && pawn.CanReserve(plant2, 1, -1, null, false) && !plant2.IsForbidden(pawn))
                    {
                        IPlantToGrowSettable plantToGrowSettable = plant2.Position.GetPlantToGrowSettable(plant2.Map);
                        if (plantToGrowSettable == null || plantToGrowSettable.GetPlantDefToGrow() != plant2.def)
                        {
                            return(new Job(JobDefOf.CutPlant, plant2));
                        }
                    }
                    return(null);
                }
                if (wantedPlantDef.plant.sowMinSkill > 0 && pawn.skills != null && pawn.skills.GetSkill(SkillDefOf.Growing).Level < wantedPlantDef.plant.sowMinSkill)
                {
                    return(null);
                }
                int j = 0;
                while (j < thingList.Count)
                {
                    Thing thing3 = thingList[j];
                    if (thing3.def.BlockPlanting)
                    {
                        if (!pawn.CanReserve(thing3, 1, -1, null, false))
                        {
                            return(null);
                        }
                        if (thing3.def.category == ThingCategory.Plant)
                        {
                            if (!thing3.IsForbidden(pawn))
                            {
                                return(new Job(JobDefOf.CutPlant, thing3));
                            }
                            return(null);
                        }
                        else
                        {
                            if (thing3.def.EverHaulable)
                            {
                                return(HaulAIUtility.HaulAsideJobFor(pawn, thing3));
                            }
                            return(null);
                        }
                    }
                    else
                    {
                        j++;
                    }
                }
                if (!wantedPlantDef.CanEverPlantAt(c, pawn.Map) || !GenPlant.GrowthSeasonNow(c, pawn.Map) || !pawn.CanReserve(c, 1, -1, null, false))
                {
                    return(null);
                }
                return(new Job(ZenDefOf.ZEN_PlantsSowSecondary, c)
                {
                    plantDefToSow = wantedPlantDef
                });
            }
        }
        public static bool JobOnCell(WorkGiver_GrowerSow __instance, ref Job __result, Pawn pawn, IntVec3 c, bool forced = false)
        {
            Map map = pawn.Map;

            if (c.IsForbidden(pawn))
            {
                __result = null;
                return(false);
            }

            if (!PlantUtility.GrowthSeasonNow(c, map, forSowing: true))
            {
                __result = null;
                return(false);
            }
            ThingDef localWantedPlantDef = WorkGiver_Grower.CalculateWantedPlantDef(c, map);

            WorkGiver_GrowerSow.wantedPlantDef = localWantedPlantDef;
            if (localWantedPlantDef == null)
            {
                __result = null;
                return(false);
            }

            List <Thing> thingList = c.GetThingList(map);
            bool         flag      = false;

            for (int i = 0; i < thingList.Count; i++)
            {
                Thing thing = thingList[i];
                if (thing.def == localWantedPlantDef)
                {
                    __result = null;
                    return(false);
                }

                if ((thing is Blueprint || thing is Frame) && thing.Faction == pawn.Faction)
                {
                    flag = true;
                }
            }

            if (flag)
            {
                Thing edifice = c.GetEdifice(map);
                if (edifice == null || edifice.def.fertility < 0f)
                {
                    __result = null;
                    return(false);
                }
            }

            if (localWantedPlantDef.plant.cavePlant)
            {
                if (!c.Roofed(map))
                {
                    JobFailReason.Is(WorkGiver_GrowerSow.CantSowCavePlantBecauseUnroofedTrans);
                    __result = null;
                    return(false);
                }

                if (map.glowGrid.GameGlowAt(c, ignoreCavePlants: true) > 0f)
                {
                    JobFailReason.Is(WorkGiver_GrowerSow.CantSowCavePlantBecauseOfLightTrans);
                    __result = null;
                    return(false);
                }
            }

            if (localWantedPlantDef.plant.interferesWithRoof && c.Roofed(pawn.Map))
            {
                __result = null;
                return(false);
            }

            Plant plant = c.GetPlant(map);

            if (plant != null && plant.def.plant.blockAdjacentSow)
            {
                if (!pawn.CanReserve(plant, 1, -1, null, forced) || plant.IsForbidden(pawn))
                {
                    __result = null;
                    return(false);
                }

                __result = JobMaker.MakeJob(JobDefOf.CutPlant, plant);
                return(false);
            }

            Thing thing2 = PlantUtility.AdjacentSowBlocker(localWantedPlantDef, c, map);

            if (thing2 != null)
            {
                Plant plant2 = thing2 as Plant;
                if (plant2 != null && pawn.CanReserve(plant2, 1, -1, null, forced) && !plant2.IsForbidden(pawn))
                {
                    IPlantToGrowSettable plantToGrowSettable = plant2.Position.GetPlantToGrowSettable(plant2.Map);
                    if (plantToGrowSettable == null || plantToGrowSettable.GetPlantDefToGrow() != plant2.def)
                    {
                        __result = JobMaker.MakeJob(JobDefOf.CutPlant, plant2);
                        return(false);
                    }
                }

                __result = null;
                return(false);
            }

            ThingDef thingdef = localWantedPlantDef;

            if (thingdef != null && thingdef.plant != null && thingdef.plant.sowMinSkill > 0 && pawn != null && pawn.skills != null && pawn.skills.GetSkill(SkillDefOf.Plants).Level < localWantedPlantDef.plant.sowMinSkill)
            {
                WorkGiver workGiver = __instance;
                JobFailReason.Is("UnderAllowedSkill".Translate(localWantedPlantDef.plant.sowMinSkill), workGiver.def.label);
                __result = null;
                return(false);
            }

            for (int j = 0; j < thingList.Count; j++)
            {
                Thing thing3 = thingList[j];
                if (!thing3.def.BlocksPlanting())
                {
                    continue;
                }
                if (!pawn.CanReserve(thing3, 1, -1, null, forced))
                {
                    __result = null;
                    return(false);
                }
                if (thing3.def.category == ThingCategory.Plant)
                {
                    if (!thing3.IsForbidden(pawn))
                    {
                        __result = JobMaker.MakeJob(JobDefOf.CutPlant, thing3);
                        return(false);
                    }

                    __result = null;
                    return(false);
                }

                if (thing3.def.EverHaulable)
                {
                    __result = HaulAIUtility.HaulAsideJobFor(pawn, thing3);
                    return(false);
                }

                __result = null;
                return(false);
            }

            if (!localWantedPlantDef.CanEverPlantAt(c, map) || !PlantUtility.GrowthSeasonNow(c, map, forSowing: true) || !pawn.CanReserve(c, 1, -1, null, forced))
            {
                __result = null;
                return(false);
            }

            Job job = JobMaker.MakeJob(JobDefOf.Sow, c);

            job.plantDefToSow = localWantedPlantDef;
            __result          = job;
            return(false);
        }