static bool IsCellOpenForSowingPlantOfType(IntVec3 cell, Map map, ThingDef plantDef)
        {
            var playerSetPlantForCell = GetPlayerSetPlantForCell(cell, map);

            if (playerSetPlantForCell == null || !playerSetPlantForCell.CanAcceptSowNow())
            {
                return(false);
            }

            var plantDefToGrow = playerSetPlantForCell.GetPlantDefToGrow();

            if (plantDefToGrow == null || plantDefToGrow != plantDef)
            {
                return(false);
            }

            if (cell.GetPlant(map) != null)
            {
                return(false);
            }

            if (PlantUtility.AdjacentSowBlocker(plantDefToGrow, cell, map) != null)
            {
                return(false);
            }

            foreach (Thing current in map.thingGrid.ThingsListAt(cell))
            {
                if (current.def.BlockPlanting)
                {
                    return(false);
                }
            }
            return(plantDefToGrow.CanEverPlantAt(cell, map) && PlantUtility.GrowthSeasonNow(cell, map));
        }
Exemplo n.º 2
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);
     }
 }
Exemplo n.º 3
0
        public override Job JobOnCell(Pawn pawn, IntVec3 c, bool forced = false)
        {
            Map map = pawn.Map;

            if (c.IsForbidden(pawn))
            {
                return(null);
            }
            if (!PlantUtility.GrowthSeasonNow(c, map, true))
            {
                return(null);
            }
            if (WorkGiver_GrowerBotany.wantedPlantDef == null)
            {
                WorkGiver_GrowerBotany.wantedPlantDef = WorkGiver_GrowerBotany.CalculateWantedPlantDef(c, map);
                if (WorkGiver_GrowerBotany.wantedPlantDef == null)
                {
                    return(null);
                }
            }
            List <Thing> thingList = c.GetThingList(map);
            bool         flag      = false;

            for (int i = 0; i < thingList.Count; i++)
            {
                Thing thing = thingList[i];
                if (thing.def == WorkGiver_GrowerBotany.wantedPlantDef)
                {
                    return(null);
                }
                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)
                {
                    return(null);
                }
            }
            if (WorkGiver_GrowerBotany.wantedPlantDef.plant.cavePlant)
            {
                if (!c.Roofed(map))
                {
                    JobFailReason.Is(WorkGiver_GrowerSowBotany.CantSowCavePlantBecauseUnroofedTrans, null);
                    return(null);
                }
                if (map.glowGrid.GameGlowAt(c, true) > 0f)
                {
                    JobFailReason.Is(WorkGiver_GrowerSowBotany.CantSowCavePlantBecauseOfLightTrans, null);
                    return(null);
                }
            }
            if (WorkGiver_GrowerBotany.wantedPlantDef.plant.interferesWithRoof && c.Roofed(pawn.Map))
            {
                return(null);
            }
            Plant plant = c.GetPlant(map);

            if (plant != null && plant.def.plant.blockAdjacentSow)
            {
                LocalTargetInfo target = plant;
                if (!pawn.CanReserve(target, 1, -1, null, forced) || plant.IsForbidden(pawn))
                {
                    return(null);
                }
                return(new Job(JobDefOf.CutPlant, plant));
            }
            else
            {
                Thing thing2 = PlantUtility.AdjacentSowBlocker(WorkGiver_GrowerBotany.wantedPlantDef, c, map);
                if (thing2 != null)
                {
                    Plant plant2 = thing2 as Plant;
                    if (plant2 != null)
                    {
                        LocalTargetInfo target = plant2;
                        if (pawn.CanReserve(target, 1, -1, null, forced) && !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 (WorkGiver_GrowerBotany.wantedPlantDef.plant.sowMinSkill > 0 && pawn.skills != null && pawn.skills.GetSkill(SkillDefOf.Plants).Level < WorkGiver_GrowerBotany.wantedPlantDef.plant.sowMinSkill)
                {
                    return(null);
                }
                int j = 0;
                while (j < thingList.Count)
                {
                    Thing thing3 = thingList[j];
                    if (thing3.def.BlockPlanting)
                    {
                        LocalTargetInfo target = thing3;
                        if (!pawn.CanReserve(target, 1, -1, null, forced))
                        {
                            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 (WorkGiver_GrowerBotany.wantedPlantDef.CanEverPlantAt(c, map) && PlantUtility.GrowthSeasonNow(c, map, true))
                {
                    LocalTargetInfo target = c;
                    if (pawn.CanReserve(target, 1, -1, null, forced))
                    {
                        return(new Job(JobDefOf.Sow, c)
                        {
                            plantDefToSow = WorkGiver_GrowerBotany.wantedPlantDef
                        });
                    }
                }
                return(null);
            }
        }
        public override Job JobOnCell(Pawn pawn, IntVec3 c, bool forced = false)
        {
            //if (!pawn.IsPrisoner)
            //    return null;
            if (c.IsForbidden(pawn))
            {
                return(null);
            }
            if (!PlantUtility.GrowthSeasonNow(c, pawn.Map))
            {
                return(null);
            }
            if (wantedPlantDef == null)
            {
                wantedPlantDef = CalculateWantedPlantDef(c, pawn.Map);
                if (wantedPlantDef == null)
                {
                    return(null);
                }
            }
            var thingList = c.GetThingList(pawn.Map);

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

            if (plant != null && plant.def.plant.blockAdjacentSow)
            {
                if (!pawn.CanReserve(plant, 1, -1, null, false) || plant.IsForbidden(pawn))
                {
                    return(null);
                }
                return(new Job(JobDefOf.CutPlant, plant));
            }
            var thing2 = PlantUtility.AdjacentSowBlocker(wantedPlantDef, c, pawn.Map);

            if (thing2 != null)
            {
                var plant2 = thing2 as Plant;
                if (plant2 != null && pawn.CanReserve(plant2, 1, -1, null, false) && !plant2.IsForbidden(pawn))
                {
                    var 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.Plants).Level < wantedPlantDef.plant.sowMinSkill)
            {
                return(null);
            }
            if (pawn.IsPrisoner && wantedPlantDef.plant.sowMinSkill > 6 && !PrisonLaborPrefs.AdvancedGrowing)
            {
                Tutorials.Growing();
                return(null);
            }
            var j = 0;

            while (j < thingList.Count)
            {
                var 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);
                    }
                    if (thing3.def.EverHaulable)
                    {
                        return(HaulAIUtility.HaulAsideJobFor(pawn, thing3));
                    }
                    return(null);
                }
                j++;
            }
            if (!wantedPlantDef.CanEverPlantAt(c, pawn.Map) || !PlantUtility.GrowthSeasonNow(c, pawn.Map) ||
                !pawn.CanReserve(c, 1, -1, null, false))
            {
                return(null);
            }
            return(new Job(JobDefOf.Sow, c)
            {
                plantDefToSow = wantedPlantDef
            });
        }
Exemplo n.º 5
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;
        }
        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);

            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(CantSowCavePlantBecauseUnroofedTrans);
                    __result = null;
                    return(false);
                }

                if (map.glowGrid.GameGlowAt(c, ignoreCavePlants: true) > 0f)
                {
                    JobFailReason.Is(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_NewTemp(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);
        }
Exemplo n.º 7
0
        public static void AddObjectToActionableObjects(Map map, IntVec3 location, List <HashSet <IntVec3>[]> awaitingActionZoomLevels)
        {
            int jumboCellWidth;
            int mapSizeX = map.Size.x;
            int mapSizeZ = map.Size.z;
            int zoomLevel;
            //---START--- For plant sowing
            ThingDef localWantedPlantDef = WorkGiver_Grower.CalculateWantedPlantDef(location, map);

            if (localWantedPlantDef == null)
            {
                return;
            }
            List <Thing> thingList = location.GetThingList(map);

            for (int i = 0; i < thingList.Count; i++)
            {
                Thing thing = thingList[i];
                if (thing.def == localWantedPlantDef)
                {
                    return;
                }
            }
            if (map.physicalInteractionReservationManager.IsReserved(location))
            {
                return;
            }
            Thing thing2 = PlantUtility.AdjacentSowBlocker(localWantedPlantDef, location, map);

            if (thing2 != null)
            {
                if (thing2 is Plant plant2 && !plant2.IsForbidden(Faction.OfPlayer))
                {
                    IPlantToGrowSettable plantToGrowSettable = plant2.Position.GetPlantToGrowSettable(plant2.Map);
                    if (plantToGrowSettable != null && plantToGrowSettable.GetPlantDefToGrow() == plant2.def)
                    {
                        return;
                    }
                }
            }

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

                if (thing3.def.category == ThingCategory.Plant)
                {
                    if (!thing3.IsForbidden(Faction.OfPlayer))
                    {
                        break;                         // JobMaker.MakeJob(JobDefOf.CutPlant, thing3);
                    }
                    Log.Warning("Plant IsForbidden");
                    return;
                }

                if (thing3.def.EverHaulable)
                {
                    break;                     //HaulAIUtility.HaulAsideJobFor(pawn, thing3);
                }
                return;
            }

            //TODO fix null check? find root cause. Or maybe it was just from a bad save?
            //if (localWantedPlantDef != null &&!localWantedPlantDef.CanEverPlantAt_NewTemp(location, map, true))
            // this change helps with boulders blocking growing zones. likely at a small performance cost
            if (localWantedPlantDef != null && (!location.InBounds(map) || (double)map.fertilityGrid.FertilityAt(location) < localWantedPlantDef.plant.fertilityMin))
            {
                return;
            }
            //---END--

            zoomLevel = 0;
            do
            {
                jumboCellWidth = getJumboCellWidth(zoomLevel);
                HashSet <IntVec3>[] awaitingActionGrid = awaitingActionZoomLevels[zoomLevel];
                int jumboCellIndex        = CellToIndexCustom(location, mapSizeX, jumboCellWidth);
                HashSet <IntVec3> hashset = awaitingActionGrid[jumboCellIndex];
                if (hashset == null)
                {
                    hashset = new HashSet <IntVec3>();
                    lock (awaitingActionGrid)
                    {
                        awaitingActionGrid[jumboCellIndex] = hashset;
                    }
                }
                lock (hashset)
                {
                    hashset.Add(location);
                }
                zoomLevel++;
            } while (jumboCellWidth < mapSizeX || jumboCellWidth < mapSizeZ);
        }