public static void Postfix(MapParent __instance, ref MapGeneratorDef __result)
 {
     if (Find.World.worldObjects.AllWorldObjects.Find(o => o.Tile == __instance.Tile && o.def.HasModExtension <CustomGenOption>()) is WorldObject worldObject)
     {
         KLog.Message($"Generating worldObject {worldObject.LabelCap}");
         __result = DefDatabase <MapGeneratorDef> .GetNamed("KCSG_WorldObject_Gen");
     }
 }
Exemple #2
0
        public static List <CustomVector> Run(CustomVector start, CustomVector end, CustomVector[][] grid, bool neig8)
        {
            gridCols = grid.Count();
            gridRows = grid[0].Count();

            List <CustomVector> Path       = new List <CustomVector>();
            List <CustomVector> OpenList   = new List <CustomVector>();
            List <CustomVector> ClosedList = new List <CustomVector>();
            List <CustomVector> adjacencies;
            CustomVector        current = start;

            OpenList.Add(start);

            while (OpenList.Count > 0 && !ClosedList.Exists(v => v.X == end.X && v.Y == end.Y))
            {
                current = OpenList[0];
                OpenList.Remove(current);
                ClosedList.Add(current);
                adjacencies = GetAdjacent(current, grid, neig8);

                foreach (CustomVector n in adjacencies)
                {
                    if (!ClosedList.Contains(n) && n.Type != CellType.WATER && n.Type != CellType.BUILDING)
                    {
                        if (!OpenList.Contains(n))
                        {
                            n.Parent           = current;
                            n.DistanceToTarget = (float)(Math.Abs(n.X - end.X) + Math.Abs(n.Y - end.Y));
                            n.Cost             = n.Weight + n.Parent.Cost;
                            OpenList.Add(n);
                            OpenList = OpenList.OrderBy(v => v.F).ToList();
                        }
                    }
                }
            }

            if (!ClosedList.Exists(v => v.X == end.X && v.Y == end.Y))
            {
                KLog.Message($"AStar error: end point not found in ClosedList. Closed list count {ClosedList.Count}. Path wanted from {start} to {end}.");
                return(null);
            }

            CustomVector temp = ClosedList[ClosedList.IndexOf(current)];

            if (temp == null)
            {
                KLog.Message("AStar error: temp is null.");
                return(null);
            }

            while (temp != start && temp != null)
            {
                Path.Add(temp);
                temp = temp.Parent;
            }
            return(Path);
        }
 public static void Postfix(Settlement __instance, ref MapGeneratorDef __result)
 {
     if (__instance != null && __instance.Faction != null && __instance.Faction != Faction.OfPlayer)
     {
         KLog.Message($"Faction: {__instance.Faction.NameColored} - Generating");
         if (__instance.Faction.def.HasModExtension <CustomGenOption>())
         {
             KLog.Message($"Faction: {__instance.Faction.NameColored} - Faction have CustomGenOption extension");
             __result = DefDatabase <MapGeneratorDef> .GetNamed("KCSG_Base_Faction");
         }
         else if (Find.World.worldObjects.AllWorldObjects.FindAll(o => o.Tile == __instance.Tile && o.def.HasModExtension <CustomGenOption>()).Any())
         {
             KLog.Message($"Faction: {__instance.Faction.NameColored} - Faction do not have CustomGenOption extension");
             __result = DefDatabase <MapGeneratorDef> .GetNamed("KCSG_WorldObject_Gen");
         }
     }
 }
Exemple #4
0
 public static bool Prefix(Caravan caravan, Settlement settlement)
 {
     if (settlement.Faction != null && settlement.Faction.def.HasModExtension <CustomGenOption>() ||
         (Find.World.worldObjects.AllWorldObjects.Find(o => o.Tile == settlement.Tile && o.def.HasModExtension <CustomGenOption>()) is WorldObject worldObject))
     {
         if (!settlement.HasMap)
         {
             CGO.useCustomWindowContent = true;
             CGO.dateTime = DateTime.Now;
             LongEventHandler.QueueLongEvent(delegate()
             {
                 CGO.allTip = DefDatabase <TipSetDef> .AllDefsListForReading.SelectMany((TipSetDef set) => set.tips).InRandomOrder().ToList();
                 if (CGO.allTip.Count > 0)
                 {
                     CGO.tipAvailable = true;
                 }
                 CustomAttackNowNoLetter(caravan, settlement);
                 LongEventHandler.ExecuteWhenFinished(() =>
                 {
                     KLog.Message($"Generation done in {(DateTime.Now - CGO.dateTime).Duration().TotalSeconds}");
                     // Send letter
                     if (settlement.Faction != null)
                     {
                         SendAttackLetter(caravan, settlement);
                     }
                     // Clear
                     CGO.ClearUI();
                     CGO.ClearAll();
                     LongEventHandler_Patches.LongEventsOnGUI_Prefix.structure = null;
                 });
             }, "GeneratingMapForNewEncounter", true, delegate(Exception e)
             {
                 Log.Error($"{e}");
                 CGO.ClearUI();
                 CGO.ClearAll();
             }, true);
         }
         else
         {
             AccessTools.Method(typeof(SettlementUtility), "AttackNow").Invoke(null, new object[] { caravan, settlement });
         }
         return(false);
     }
     return(true);
 }
        internal int RandomTileFor(WorldObject wo, SpawnAtWorldGen ext, int maxTries, Predicate<int> extraValidator = null)
        {
            for (int i = 0; i < maxTries; i++)
            {
                if (Enumerable.Range(0, 100).Select(_ => Rand.Range(0, Find.WorldGrid.TilesCount)).TryRandomElementByWeight(x =>
                {
                    Tile tile = Find.WorldGrid[x];
                    if (!tile.biome.canBuildBase || !tile.biome.implemented || tile.hilliness == Hilliness.Impassable)
                    {
                        return 0f;
                    }
                    else if (Find.WorldObjects.AnyWorldObjectAt(x) || Find.WorldObjects.AnySettlementBaseAtOrAdjacent(x))
                    {
                        return 0f;
                    }
                    else if (ext.allowedBiomes?.Count > 0 && !ext.allowedBiomes.Contains(tile.biome))
                    {
                        return 0f;
                    }
                    else if (ext.disallowedBiomes?.Count > 0 && ext.disallowedBiomes.Contains(tile.biome))
                    {
                        return 0f;
                    }
                    else if (extraValidator != null && !extraValidator(x))
                    {
                        return 0f;
                    }
                    return tile.biome.settlementSelectionWeight;

                }, out int result))
                {
                    return result;
                }
            }
            KLog.Message($"Failed to find world tile for {wo.def.defName}");
            return 0;
        }
Exemple #6
0
        public static void Generate(Map map, IntVec3 c, CustomGenOption sf, string symbolResolver = "kcsg_settlement")
        {
            CGO.useStructureLayout = sf.useStructureLayout;

            if (sf.useStructureLayout)
            {
                CGO.structureLayoutDef = LayoutUtils.ChooseLayoutFrom(sf.chooseFromlayouts);
            }
            else
            {
                CGO.settlementLayoutDef = sf.chooseFromSettlements.RandomElement();
            }

            // Get faction
            Faction faction;

            if (map.ParentFaction == null || map.ParentFaction == Faction.OfPlayer)
            {
                faction = Find.FactionManager.RandomEnemyFaction(false, false, true, TechLevel.Undefined);
            }
            else
            {
                faction = map.ParentFaction;
            }

            // Get settlement size
            int width;
            int height;

            if (sf.useStructureLayout)
            {
                RectUtils.HeightWidthFromLayout(CGO.structureLayoutDef, out height, out width);
            }
            else
            {
                SettlementLayoutDef temp = CGO.settlementLayoutDef;
                height = temp.settlementSize.x;
                width  = temp.settlementSize.z;
            }

            IntVec3 intVec3 = c;

            if (CGO.factionSettlement.tryFindFreeArea)
            {
                bool success = RCellFinder.TryFindRandomCellNearTheCenterOfTheMapWith(cell => new CellRect(cell.x - width / 2, cell.z - height / 2, width, height).Cells.All(pC => pC.Walkable(map) && !pC.GetTerrain(map).affordances.Contains(TerrainAffordanceDefOf.Bridgeable)), map, out intVec3);
                KLog.Message($"Trying to find free spawn area success: {success}");
            }

            CellRect rect = new CellRect(intVec3.x - width / 2, intVec3.z - height / 2, width, height);

            rect.ClipInsideMap(map);

            ResolveParams rp = default;

            rp.faction = faction;
            rp.rect    = rect;

            BaseGen.globalSettings.map = map;
            BaseGen.symbolStack.Push(symbolResolver, rp, null);
            BaseGen.Generate();
        }
        public override void PostMapGenerate(Map map)
        {
            if (Find.TickManager.TicksGame > 5f || chooseFrom.Count <= 0 || PrepareCarefully_Util.pcScenariosSave.Count <= 0)
            {
                return;
            }

            StructureLayoutDef structureLayoutDef;

            if (ModLister.GetActiveModWithIdentifier("EdB.PrepareCarefully") != null)
            {
                structureLayoutDef = PrepareCarefully_Util.pcScenariosSave.First().Key;
                nearMapCenter      = PrepareCarefully_Util.pcScenariosSave.First().Value;
            }
            else
            {
                structureLayoutDef = chooseFrom.RandomElement();
            }

            RectUtils.HeightWidthFromLayout(structureLayoutDef, out int h, out int w);
            CellRect cellRect = this.CreateCellRect(map, h, w);

            if (preGenClear)
            {
                GenUtils.PreClean(map, cellRect, structureLayoutDef.roofGrid, fullClear);
            }

            foreach (List <string> item in structureLayoutDef.layouts)
            {
                GenUtils.GenerateRoomFromLayout(item, cellRect, map, structureLayoutDef, spawnConduits);
            }

            if (spawnTheStartingPawn && Find.GameInitData != null)
            {
                List <List <Thing> > thingsGroups = new List <List <Thing> >();
                foreach (Pawn startingAndOptionalPawn in Find.GameInitData.startingAndOptionalPawns)
                {
                    thingsGroups.Add(new List <Thing>()
                    {
                        startingAndOptionalPawn
                    });
                }

                List <Thing> thingList = new List <Thing>();
                foreach (ScenPart allPart in Find.Scenario.AllParts)
                {
                    thingList.AddRange(allPart.PlayerStartingThings());
                }

                int index = 0;
                foreach (Thing thing in thingList)
                {
                    if (thing.def.CanHaveFaction)
                    {
                        thing.SetFactionDirect(Faction.OfPlayer);
                    }

                    thingsGroups[index].Add(thing);
                    ++index;
                    if (index >= thingsGroups.Count)
                    {
                        index = 0;
                    }
                }

                IntVec3 center = map.Center;
                Pos     offset = structureLayoutDef.spawnAtPos.RandomElement();
                center.x += offset.x;
                center.y += offset.y;
                KLog.Message($"Spawning pawns and stuff at {center}");
                this.DropThingGroupsAt(center, map, thingsGroups, instaDrop: (Find.GameInitData.QuickStarted || this.method != PlayerPawnsArriveMethod.DropPods), leaveSlag: true, allowFogged: false);
            }

            if (map.mapPawns.FreeColonistsSpawned.Count > 0)
            {
                FloodFillerFog.DebugRefogMap(map);
            }
        }