Example #1
0
 private static bool SpawnAnimalPrefix(WildAnimalSpawner __instance, ref IntVec3 loc,
                                       ref bool __result, Map ___map)
 {
     if (ZLevelsMod.settings.allowZLevelsInfestation)
     {
         try
         {
             bool result   = false;
             var  comp     = ZUtils.GetMapComponentZLevel(___map);
             var  ZTracker = ZUtils.ZTracker;
             if (___map.Parent is MapParent_ZLevel && comp != null &&
                 ZTracker.GetUpperLevel(___map.Tile, ___map) != null &&
                 !ZUtils.GetMapComponentZLevel(ZTracker.GetUpperLevel(___map.Tile, ___map))
                 .hasCavesBelow.GetValueOrDefault(false))
             {
                 result = false;
             }
             else
             {
                 PawnKindDef pawnKindDef = (from a in ___map.Biome.AllWildAnimals
                                            where ___map.mapTemperature.SeasonAcceptableFor(a.race)
                                            select a)
                                           .RandomElementByWeight((PawnKindDef def) =>
                                                                  ___map.Biome.CommonalityOfAnimal(def) / def.wildGroupSize.Average);
                 if (pawnKindDef == null)
                 {
                     Log.Error("No spawnable animals right now.");
                     result = false;
                 }
                 else
                 {
                     ZLogger.Message("Spawning animal: " + pawnKindDef + " in biome: " + ___map.Biome);
                     int randomInRange = pawnKindDef.wildGroupSize.RandomInRange;
                     int radius        = Mathf.CeilToInt(Mathf.Sqrt((float)pawnKindDef.wildGroupSize.max));
                     if (___map.Parent is MapParent_ZLevel && !loc.Walkable(___map))
                     {
                         loc = CellFinderLoose.RandomCellWith((IntVec3 sq) => sq.Walkable(___map), ___map);
                     }
                     for (int i = 0; i < randomInRange; i++)
                     {
                         IntVec3 loc2 = CellFinder.RandomClosewalkCellNear(loc, ___map, radius, null);
                         GenSpawn.Spawn(PawnGenerator.GeneratePawn(pawnKindDef, null), loc2, ___map, WipeMode.Vanish);
                     }
                     result = true;
                 }
             }
             __result = result;
         }
         catch (Exception ex)
         {
             Log.Error("[Z-Levels] SpawnAnimalPrefix patch produced an error. That should not happen and will break things. Send a Hugslib log to the Z-Levels developers. Error message: " + ex);
         }
         return(false);
     }
     return(true);
 }
Example #2
0
 private static bool PreFix(ref bool __result, IncidentParms parms)
 {
     if (ZLevelsMod.settings.allowZLevelsInfestation)
     {
         try
         {
             Map map  = (Map)parms.target;
             var comp = ZUtils.GetMapComponentZLevel(map);
             if (comp.hasCavesBelow.HasValue && comp.hasCavesBelow.Value)
             {
                 var foods = map.listerThings.AllThings.Where(x => !(x is Plant) && !(x is Pawn) &&
                                                              x.GetStatValue(StatDefOf.Nutrition, true) > 0.1f);
                 if (foods != null && foods.Any())
                 {
                     List <PawnKindDef> infestators = new List <PawnKindDef>
                     {
                         ZLevelsDefOf.ZL_UndegroundBiome.AllWildAnimals.RandomElement()
                     };
                     var infestationPlace = foods.RandomElement().Position;
                     var infestationData  = new InfestationData(infestators, parms.points, infestationPlace);
                     if (comp.ActiveInfestations == null)
                     {
                         comp.ActiveInfestations = new List <InfestationData>();
                     }
                     comp.ActiveInfestations.Add(infestationData);
                     if (comp.TotalInfestations == null)
                     {
                         comp.TotalInfestations = new List <InfestationData>();
                     }
                     comp.TotalInfestations.Add(infestationData);
                     var naturalHole = (Building_StairsDown)ThingMaker.MakeThing(ZLevelsDefOf.ZL_NaturalHole);
                     naturalHole.infestationData = infestationData;
                     GenSpawn.Spawn(naturalHole, infestationPlace, map, WipeMode.Vanish);
                     Find.LetterStack.ReceiveLetter("ZLevelInfestation"
                                                    .Translate(infestators.RandomElement().race.label), "ZLevelInfestationDesc".Translate(),
                                                    LetterDefOf.ThreatBig, naturalHole);
                 }
             }
             else
             {
                 ZLogger.Message("The map has no caves below to generate infestation");
             }
             __result = false;
         }
         catch (Exception ex)
         {
             Log.Error("[Z-Levels] Patch_Infestation_TryExecuteWorker patch produced an error. That should not happen and will break things. Send a Hugslib log to the Z-Levels developers. Error message: " + ex);
         }
         return(false);
     }
     return(true);
 }
Example #3
0
        public override IEnumerable <Toil> MakeNewToils()
        {
            yield return(Toils_Goto.GotoThing(TargetIndex.A, PathEndMode.OnCell));

            Toil useStairs = Toils_General.Wait(60, 0);

            ToilEffects.WithProgressBarToilDelay(useStairs, TargetIndex.A, false, -0.5f);
            ToilFailConditions.FailOnDespawnedNullOrForbidden <Toil>(useStairs, TargetIndex.A);
            ToilFailConditions.FailOnCannotTouch <Toil>(useStairs, TargetIndex.A, PathEndMode.OnCell);
            yield return(useStairs);

            yield return(new Toil
            {
                initAction = delegate()
                {
                    var ZTracker = ZUtils.ZTracker;
                    Pawn pawn = GetActor();
                    if (TargetA.Thing is Building_StairsUp stairsUp)
                    {
                        Map map = ZTracker.GetUpperLevel(this.pawn.Map.Tile, this.pawn.Map);
                        if (map == null)
                        {
                            map = ZTracker.CreateUpperLevel(this.pawn.Map, stairsUp.Position);
                            if (!string.IsNullOrEmpty(stairsUp.pathToPreset))
                            {
                                var comp = ZUtils.GetMapComponentZLevel(map);
                                comp.DoGeneration = true;
                                comp.path = stairsUp.pathToPreset;
                            }
                            ZTracker.TeleportPawn(pawn, pawn.Position, map, true, false, true);
                        }
                        else
                        {
                            if (!string.IsNullOrEmpty(stairsUp.pathToPreset))
                            {
                                var comp = ZUtils.GetMapComponentZLevel(map);
                                comp.DoGeneration = true;
                                comp.path = stairsUp.pathToPreset;
                            }
                            ZTracker.TeleportPawn(pawn, pawn.Position, map, false, false, stairsUp.shouldSpawnStairsUpper);
                            stairsUp.shouldSpawnStairsUpper = false;
                        }
                    }

                    else if (TargetA.Thing is Building_StairsDown stairsDown)
                    {
                        Map map = ZTracker.GetLowerLevel(this.pawn.Map.Tile, this.pawn.Map);
                        if (map == null)
                        {
                            //ZLogger.Message("CREATING LOWER LEVEL AGAIG", true);
                            map = ZTracker.CreateLowerLevel(this.pawn.Map, stairsDown.Position);
                            if (!string.IsNullOrEmpty(stairsDown.pathToPreset))
                            {
                                var comp = ZUtils.GetMapComponentZLevel(map);
                                comp.DoGeneration = true;
                                comp.path = stairsDown.pathToPreset;
                            }
                            ZTracker.TeleportPawn(pawn, pawn.Position, map, true, true);
                        }
                        else
                        {
                            if (!string.IsNullOrEmpty(stairsDown.pathToPreset))
                            {
                                var comp = ZUtils.GetMapComponentZLevel(map);
                                comp.DoGeneration = true;
                                comp.path = stairsDown.pathToPreset;
                            }
                            ZTracker.TeleportPawn(pawn, pawn.Position, map, false, stairsDown.shouldSpawnStairsBelow);
                            stairsDown.shouldSpawnStairsBelow = false;
                        }
                    }
                }
            });
Example #4
0
        public static void Prefix(ColonistBar __instance, ref List <ColonistBar.Entry> ___cachedEntries,
                                  ref List <Vector2> ___cachedDrawLocs)
        {
            try
            {
                if (___cachedDrawLocs.Count == ___cachedEntries.Count)
                {
                    for (int i = 0; i < ___cachedDrawLocs.Count; i++)
                    {
                        if (___cachedEntries[i].pawn == null && ___cachedEntries[i].map.Parent is MapParent_ZLevel)
                        {
                            //ZLogger.Message("Rect: " + ___cachedDrawLocs[i].x + " - " + ___cachedDrawLocs[i].y + " - "
                            //      + __instance.Size.x + " - " + __instance.Size.y);
                            Rect rect = new Rect(___cachedDrawLocs[i].x + (__instance.Size.x / 1.25f),
                                                 ___cachedDrawLocs[i].y + (__instance.Size.y / 1.25f),
                                                 __instance.Size.x / 3, __instance.Size.y / 3);
                            GUI.DrawTexture(rect, AbandonButtonTex);
                            if (Mouse.IsOver(rect))
                            {
                                if (Input.GetMouseButtonDown(0) && ___cachedEntries[i].map != null)
                                {
                                    Map map = ___cachedEntries[i].map;
                                    Find.WindowStack.Add(new Dialog_MessageBox("ZAbandonConfirmation".Translate(), "Yes".Translate(), delegate()
                                    {
                                        var comp        = ZUtils.GetMapComponentZLevel(map);
                                        var pathToWrite = Path.Combine(Path.Combine(GenFilePaths.ConfigFolderPath,
                                                                                    "SavedMaps"), map.Tile + " - " + comp.Z_LevelIndex + ".xml");
                                        if (map.listerThings.AllThings.Count > 0)
                                        {
                                            BlueprintUtility.SaveEverything(pathToWrite, map, "SavedMap");
                                            ZLogger.Message("Removing map: " + map);
                                        }
                                        var parent   = map.Parent as MapParent_ZLevel;
                                        var ZTracker = ZUtils.ZTracker;
                                        parent.Abandon();
                                        ZTracker.ZLevelsTracker[map.Tile].ZLevels.Remove(comp.Z_LevelIndex);

                                        foreach (var map2 in Find.Maps)
                                        {
                                            var comp2 = ZUtils.GetMapComponentZLevel(map2);
                                            if (ZTracker.ZLevelsTracker[map2.Tile] != null)
                                            {
                                                foreach (var d in ZTracker.ZLevelsTracker[map2.Tile].ZLevels)
                                                {
                                                    ZLogger.Message(map2 + ": " + d.Key + " - " + d.Value);
                                                }
                                            }
                                        }
                                    }, "No".Translate(), null, null, false, null, null));
                                }
                                else if (Input.GetMouseButtonDown(1) && ___cachedEntries[i].map != null)
                                {
                                    Map map = ___cachedEntries[i].map;
                                    Find.WindowStack.Add(new Dialog_MessageBox("ZAbandonPermanentlyConfirmation".Translate(), "Yes".Translate(), delegate()
                                    {
                                        var comp = ZUtils.GetMapComponentZLevel(map);
                                        try
                                        {
                                            var pathToDelete = Path.Combine(Path.Combine(GenFilePaths.ConfigFolderPath,
                                                                                         "SavedMaps"), map.Tile + " - " + comp.Z_LevelIndex + ".xml");
                                            var file = new FileInfo(pathToDelete);
                                            file.Delete();
                                        }
                                        catch
                                        {
                                        }

                                        var parent   = map.Parent as MapParent_ZLevel;
                                        var ZTracker = ZUtils.ZTracker;
                                        parent.Abandon();
                                        ZTracker.ZLevelsTracker[map.Tile].ZLevels.Remove(comp.Z_LevelIndex);

                                        foreach (var map2 in Find.Maps)
                                        {
                                            var comp2 = ZUtils.GetMapComponentZLevel(map2);
                                            if (ZTracker.ZLevelsTracker[map2.Tile] != null)
                                            {
                                                foreach (var d in ZTracker.ZLevelsTracker[map2.Tile].ZLevels)
                                                {
                                                    ZLogger.Message(map2 + ": " + d.Key + " - " + d.Value);
                                                }
                                            }
                                        }
                                    }, "No".Translate(), null, null, false, null, null));
                                }
                            }
                        }
                    }
                }
            }
            catch { };
        }