Exemplo n.º 1
0
        private Thing TryGetTreasure(ThingDef treasureDef, ThingDef stuffDef)
        {
            Thing treasure = null;

            // make treasure
            if (treasureDef == null)
            {
                return(null);
            }

            treasure = ThingMaker.MakeThing(treasureDef, stuffDef);

            // try adjust quality
            CompQuality treasureCQ = treasure.TryGetComp <CompQuality>();

            if (treasureCQ != null)
            {
                treasureCQ.SetQuality(QualityUtility.GenerateQualityBaseGen(), ArtGenerationContext.Outsider);
            }

            // adjust Stack to a random stack size
            if (treasure.def.stackLimit > 1)
            {
                if (treasure.def.stackLimit > 50)
                {
                    treasure.stackCount = Rand.RangeInclusive(1, 45);
                }
                else
                {
                    treasure.stackCount = Rand.RangeInclusive(1, treasure.def.stackLimit);
                }
            }

            // adjust Hitpoints (40% to 100%)
            if (treasure.stackCount == 1)
            {
                treasure.HitPoints = Rand.RangeInclusive((int)(treasure.MaxHitPoints * 0.4), treasure.MaxHitPoints);
            }

            return(treasure);
        }
        public override void Resolve(ResolveParams rp)
        {
            Map map = BaseGen.globalSettings.map;

            wineFermenters.Clear();

            foreach (var pos in rp.rect)
            {
                var thingList = pos.GetThingList(map);
                for (int i = 0; i < thingList.Count; i++)
                {
                    var wineFermenterComp = thingList[i].TryGetComp <CompWineFermenter>();
                    if (wineFermenterComp != null && !wineFermenters.Contains(wineFermenterComp))
                    {
                        wineFermenters.Add(wineFermenterComp);
                    }
                }
            }

            float legendaryAgeTicksFactor = Rand.Range(0.1f, 1.1f);

            for (int i = 0; i < wineFermenters.Count; i++)
            {
                var fermenter = wineFermenters[i];
                fermenter.targetQuality = (QualityCategory)Mathf.Min((int)QualityUtility.GenerateQualityBaseGen() + 1, (int)QualityCategory.Legendary);
                if (!fermenter.Fermented)
                {
                    int mustToAdd = Mathf.Min(Rand.Range(1, fermenter.Props.mustCapacity), fermenter.SpaceLeftForMust);
                    if (mustToAdd > 0)
                    {
                        fermenter.AddMust(mustToAdd);
                        int ageTicks = Mathf.RoundToInt((fermenter.Props.legendaryQualityAgeDaysThreshold * GenDate.TicksPerDay * legendaryAgeTicksFactor) % fermenter.TicksToReachTargetQuality);
                        fermenter.AgeTicks = ageTicks;
                    }
                }
            }

            wineFermenters.Clear();
        }
 public override void Resolve(ResolveParams rp)
 {
     if (rp.singleThingToSpawn is Pawn)
     {
         ResolveParams resolveParams = rp;
         resolveParams.singlePawnToSpawn = (Pawn)rp.singleThingToSpawn;
         BaseGen.symbolStack.Push("pawn", resolveParams);
     }
     else
     {
         if (rp.singleThingToSpawn != null && rp.singleThingToSpawn.Spawned)
         {
             return;
         }
         ThingDef thingDef = (rp.singleThingToSpawn != null) ? rp.singleThingToSpawn.def : (rp.singleThingDef ?? ThingSetMakerUtility.allGeneratableItems.Where((ThingDef x) => x.IsWeapon || x.IsMedicine || x.IsDrug).RandomElement());
         Rot4?    rot      = rp.thingRot;
         IntVec3  result;
         if (thingDef.category == ThingCategory.Item)
         {
             rot = Rot4.North;
             if (!TryFindSpawnCellForItem(rp.rect, out result))
             {
                 if (rp.singleThingToSpawn != null)
                 {
                     rp.singleThingToSpawn.Destroy();
                 }
                 return;
             }
         }
         else
         {
             result = FindBestSpawnCellForNonItem(rp.rect, thingDef, ref rot, out bool hasToWipeBuilding, out bool doesntFit);
             if ((hasToWipeBuilding | doesntFit) && rp.skipSingleThingIfHasToWipeBuildingOrDoesntFit.HasValue && rp.skipSingleThingIfHasToWipeBuildingOrDoesntFit.Value)
             {
                 return;
             }
         }
         if (!rot.HasValue)
         {
             Log.Error("Could not resolve rotation. Bug.");
         }
         Thing thing;
         if (rp.singleThingToSpawn == null)
         {
             ThingDef stuff = (rp.singleThingStuff == null || !rp.singleThingStuff.stuffProps.CanMake(thingDef)) ? GenStuff.RandomStuffInexpensiveFor(thingDef, rp.faction) : rp.singleThingStuff;
             thing            = ThingMaker.MakeThing(thingDef, stuff);
             thing.stackCount = (rp.singleThingStackCount ?? 1);
             if (thing.stackCount <= 0)
             {
                 thing.stackCount = 1;
             }
             if (thing.def.CanHaveFaction && thing.Faction != rp.faction)
             {
                 thing.SetFaction(rp.faction);
             }
             thing.TryGetComp <CompQuality>()?.SetQuality(QualityUtility.GenerateQualityBaseGen(), ArtGenerationContext.Outsider);
             if (rp.postThingGenerate != null)
             {
                 rp.postThingGenerate(thing);
             }
         }
         else
         {
             thing = rp.singleThingToSpawn;
         }
         if (!rp.spawnBridgeIfTerrainCantSupportThing.HasValue || rp.spawnBridgeIfTerrainCantSupportThing.Value)
         {
             BaseGenUtility.CheckSpawnBridgeUnder(thing.def, result, rot.Value);
         }
         thing = GenSpawn.Spawn(thing, result, BaseGen.globalSettings.map, rot.Value);
         if (thing != null && thing.def.category == ThingCategory.Item)
         {
             thing.SetForbidden(value: true, warnOnFail: false);
         }
         if (rp.postThingSpawn != null)
         {
             rp.postThingSpawn(thing);
         }
     }
 }
 public override void Resolve(ResolveParams rp)
 {
     if (rp.singleThingToSpawn is Pawn)
     {
         ResolveParams resolveParams = rp;
         resolveParams.singlePawnToSpawn = (Pawn)rp.singleThingToSpawn;
         BaseGen.symbolStack.Push("pawn", resolveParams);
     }
     else if (rp.singleThingToSpawn == null || !rp.singleThingToSpawn.Spawned)
     {
         ThingDef thingDef2;
         if (rp.singleThingToSpawn == null)
         {
             ThingDef thingDef;
             if ((thingDef = rp.singleThingDef) == null)
             {
                 thingDef = (from x in ThingSetMakerUtility.allGeneratableItems
                             where x.IsWeapon || x.IsMedicine || x.IsDrug
                             select x).RandomElement <ThingDef>();
             }
             thingDef2 = thingDef;
         }
         else
         {
             thingDef2 = rp.singleThingToSpawn.def;
         }
         Rot4?   rot = rp.thingRot;
         IntVec3 loc;
         if (thingDef2.category == ThingCategory.Item)
         {
             rot = new Rot4?(Rot4.North);
             if (!this.TryFindSpawnCellForItem(rp.rect, out loc))
             {
                 if (rp.singleThingToSpawn != null)
                 {
                     rp.singleThingToSpawn.Destroy(DestroyMode.Vanish);
                 }
                 return;
             }
         }
         else
         {
             bool flag;
             bool flag2;
             loc = this.FindBestSpawnCellForNonItem(rp.rect, thingDef2, ref rot, out flag, out flag2);
             if ((flag || flag2) && rp.skipSingleThingIfHasToWipeBuildingOrDoesntFit != null && rp.skipSingleThingIfHasToWipeBuildingOrDoesntFit.Value)
             {
                 return;
             }
         }
         if (rot == null)
         {
             Log.Error("Could not resolve rotation. Bug.", false);
         }
         Thing thing;
         if (rp.singleThingToSpawn == null)
         {
             ThingDef stuff;
             if (rp.singleThingStuff != null && rp.singleThingStuff.stuffProps.CanMake(thingDef2))
             {
                 stuff = rp.singleThingStuff;
             }
             else
             {
                 stuff = GenStuff.RandomStuffInexpensiveFor(thingDef2, rp.faction);
             }
             thing = ThingMaker.MakeThing(thingDef2, stuff);
             Thing thing2 = thing;
             int?  singleThingStackCount = rp.singleThingStackCount;
             thing2.stackCount = ((singleThingStackCount == null) ? 1 : singleThingStackCount.Value);
             if (thing.stackCount <= 0)
             {
                 thing.stackCount = 1;
             }
             if (thing.def.CanHaveFaction && thing.Faction != rp.faction)
             {
                 thing.SetFaction(rp.faction, null);
             }
             CompQuality compQuality = thing.TryGetComp <CompQuality>();
             if (compQuality != null)
             {
                 compQuality.SetQuality(QualityUtility.GenerateQualityBaseGen(), ArtGenerationContext.Outsider);
             }
             if (rp.postThingGenerate != null)
             {
                 rp.postThingGenerate(thing);
             }
         }
         else
         {
             thing = rp.singleThingToSpawn;
         }
         thing = GenSpawn.Spawn(thing, loc, BaseGen.globalSettings.map, rot.Value, WipeMode.Vanish, false);
         if (thing != null && thing.def.category == ThingCategory.Item)
         {
             thing.SetForbidden(true, false);
         }
         if (rp.postThingSpawn != null)
         {
             rp.postThingSpawn(thing);
         }
     }
 }
        public static void GenerateRoomFromLayout(List <string> layoutList, CellRect roomRect, Map map, StructureLayoutDef rld, bool generateConduit = true)
        {
            bool parentFaction = map.ParentFaction != null;

            if (rld.roofGrid != null)
            {
                GenerateRoofGrid(rld.roofGrid, roomRect, map);
            }

            List <string> allSymbList = new List <string>();

            foreach (string str in layoutList)
            {
                allSymbList.AddRange(str.Split(','));
            }

            int l = 0;

            foreach (IntVec3 cell in roomRect)
            {
                if (l < allSymbList.Count && allSymbList[l] != ".")
                {
                    SymbolDef temp = DefDatabase <SymbolDef> .GetNamedSilentFail(allSymbList[l]);

                    Thing thing;
                    if (temp != null)
                    {
                        if (temp.isTerrain && temp.terrainDef != null)
                        {
                            GenerateTerrainAt(map, cell, temp.terrainDef);
                        }
                        else if (temp.pawnKindDefNS != null && CGO.factionSettlement?.shouldRuin == false)
                        {
                            if (temp.lordJob != null)
                            {
                                Lord lord = CreateNewLord(temp.lordJob, map, cell);
                                for (int i = 0; i < temp.numberToSpawn; i++)
                                {
                                    Pawn pawn = temp.spawnPartOfFaction ? PawnGenerator.GeneratePawn(temp.pawnKindDefNS, map.ParentFaction) : PawnGenerator.GeneratePawn(temp.pawnKindDefNS);
                                    if (pawn != null)
                                    {
                                        if (temp.isSlave && parentFaction)
                                        {
                                            pawn.guest.SetGuestStatus(map.ParentFaction, GuestStatus.Prisoner);
                                        }

                                        GenSpawn.Spawn(pawn, cell, map, WipeMode.FullRefund);
                                        lord.AddPawn(pawn);
                                    }
                                }
                            }
                            else
                            {
                                for (int i = 0; i < temp.numberToSpawn; i++)
                                {
                                    Pawn pawn = temp.spawnPartOfFaction ? PawnGenerator.GeneratePawn(temp.pawnKindDefNS, map.ParentFaction) : PawnGenerator.GeneratePawn(temp.pawnKindDefNS);
                                    if (pawn != null)
                                    {
                                        if (temp.isSlave && parentFaction)
                                        {
                                            pawn.guest.SetGuestStatus(map.ParentFaction, GuestStatus.Prisoner);
                                        }
                                        GenSpawn.Spawn(pawn, cell, map, WipeMode.FullRefund);
                                    }
                                }
                            }
                        }
                        else if (temp.thingDef?.category == ThingCategory.Item && cell.Walkable(map))
                        {
                            thing            = ThingMaker.MakeThing(temp.thingDef, temp.stuffDef ?? (temp.thingDef.stuffCategories?.Count > 0 ? GenStuff.RandomStuffFor(temp.thingDef) : null));
                            thing.stackCount = Mathf.Clamp(Rand.RangeInclusive(1, temp.thingDef.stackLimit), 1, 75);

                            CompQuality quality = thing.TryGetComp <CompQuality>();
                            quality?.SetQuality(QualityUtility.GenerateQualityBaseGen(), ArtGenerationContext.Outsider);

                            GenSpawn.Spawn(thing, cell, map, WipeMode.FullRefund);
                            thing.SetForbidden(true, false);
                        }
                        else if (temp.thingDef != null)
                        {
                            thing = ThingMaker.MakeThing(temp.thingDef, temp.thingDef.CostStuffCount > 0 ? (temp.stuffDef ?? temp.thingDef.defaultStuff ?? ThingDefOf.WoodLog) : null);

                            CompRefuelable refuelable = thing.TryGetComp <CompRefuelable>();
                            refuelable?.Refuel(refuelable.Props.fuelCapacity);

                            CompPowerBattery battery = thing.TryGetComp <CompPowerBattery>();
                            battery?.AddEnergy(battery.Props.storedEnergyMax);

                            if (thing is Building_CryptosleepCasket cryptosleepCasket && Rand.Value < temp.chanceToContainPawn)
                            {
                                Pawn pawn = GeneratePawnForContainer(temp, map);
                                if (!cryptosleepCasket.TryAcceptThing(pawn))
                                {
                                    pawn.Destroy();
                                }
                            }
                            else if (thing is Building_CorpseCasket corpseCasket && Rand.Value < temp.chanceToContainPawn)
                            {
                                Pawn pawn = GeneratePawnForContainer(temp, map);
                                if (!corpseCasket.TryAcceptThing(pawn))
                                {
                                    pawn.Destroy();
                                }
                            }
                            else if (thing is Building_Crate crate)
                            {
                                List <Thing> thingList = new List <Thing>();
                                if (map.ParentFaction == Faction.OfPlayer && temp.thingSetMakerDefForPlayer != null)
                                {
                                    thingList = temp.thingSetMakerDefForPlayer.root.Generate(new ThingSetMakerParams());
                                }
                                else if (temp.thingSetMakerDef != null)
                                {
                                    thingList = temp.thingSetMakerDef.root.Generate(new ThingSetMakerParams());
                                }

                                foreach (Thing t in thingList)
                                {
                                    t.stackCount = Math.Min((int)(t.stackCount * temp.crateStackMultiplier), t.def.stackLimit);
                                }

                                thingList.ForEach(t =>
                                {
                                    if (!crate.TryAcceptThing(t, false))
                                    {
                                        t.Destroy();
                                    }
                                });
                            }

                            if (thing.def.category == ThingCategory.Pawn && CGO.factionSettlement?.shouldRuin == true)
                            {
                                l++;
                                continue;
                            }
                            else if (cell.GetFirstMineable(map) is Mineable m && thing.def.designationCategory == DesignationCategoryDefOf.Security)
                            {
                                l++;
                                continue;
                            }
                            else if (thing.def.category == ThingCategory.Plant && cell.GetTerrain(map).fertility > 0.5 && cell.Walkable(map)) // If it's a plant
                            {
                                Plant plant = thing as Plant;
                                plant.Growth = temp.plantGrowth; // apply the growth
                                GenSpawn.Spawn(plant, cell, map, WipeMode.VanishOrMoveAside);
                            }
                            else if (thing.def.category == ThingCategory.Building)
                            {
                                if (!cell.GetTerrain(map).affordances.Contains(TerrainAffordanceDefOf.Heavy))
                                {
                                    if (thing.def.building.isNaturalRock)
                                    {
                                        TerrainDef t = DefDatabase <TerrainDef> .GetNamedSilentFail($"{thing.def.defName}_Rough");

                                        map.terrainGrid.SetTerrain(cell, t ?? TerrainDefOf.Soil);
                                        foreach (IntVec3 intVec3 in CellRect.CenteredOn(cell, 1))
                                        {
                                            if (!intVec3.GetTerrain(map).BuildableByPlayer)
                                            {
                                                map.terrainGrid.SetTerrain(intVec3, t ?? TerrainDefOf.Soil);
                                            }
                                        }
                                    }
                                    else
                                    {
                                        map.terrainGrid.SetTerrain(cell, TerrainDefOf.Bridge);
                                    }
                                }

                                if (thing.def.rotatable)
                                {
                                    GenSpawn.Spawn(thing, cell, map, new Rot4(temp.rotation.AsInt), WipeMode.VanishOrMoveAside);
                                }
                                else
                                {
                                    GenSpawn.Spawn(thing, cell, map, WipeMode.VanishOrMoveAside);
                                }

                                if (parentFaction)
                                {
                                    thing.SetFactionDirect(map.ParentFaction);
                                }
                            }

                            if (generateConduit && rld.spawnConduits && !thing.def.mineable && (thing.def.passability == Traversability.Impassable || thing.def.IsDoor) && map.ParentFaction?.def.techLevel >= TechLevel.Industrial) // Add power cable under all impassable
                            {
                                Thing c = ThingMaker.MakeThing(ThingDefOf.PowerConduit);
                                if (parentFaction)
                                {
                                    c.SetFactionDirect(map.ParentFaction);
                                }
                                GenSpawn.Spawn(c, cell, map, WipeMode.FullRefund);
                            }
                            // Handle mortar and mortar pawns
                            if (thing?.def?.building?.buildingTags?.Count > 0)
                            {
                                if (thing.def.building.IsMortar && thing.def.category == ThingCategory.Building && thing.def.building.buildingTags.Contains("Artillery_MannedMortar") && thing.def.HasComp(typeof(CompMannable)) && parentFaction)
                                {
                                    // Spawn pawn
                                    Lord singlePawnLord          = LordMaker.MakeNewLord(map.ParentFaction, new LordJob_ManTurrets(), map, null);
                                    PawnGenerationRequest value  = new PawnGenerationRequest(map.ParentFaction.RandomPawnKind(), map.ParentFaction, PawnGenerationContext.NonPlayer, map.Tile, mustBeCapableOfViolence: true, inhabitant: true);
                                    ResolveParams         rpPawn = new ResolveParams
                                    {
                                        faction = map.ParentFaction,
                                        singlePawnGenerationRequest = new PawnGenerationRequest?(value),
                                        rect           = CellRect.SingleCell(thing.InteractionCell),
                                        singlePawnLord = singlePawnLord
                                    };
                                    BaseGen.symbolStack.Push("pawn", rpPawn);
                                    // Spawn shells
                                    ThingDef shellDef = TurretGunUtility.TryFindRandomShellDef(thing.def, false, true, map.ParentFaction.def.techLevel, false, 250f);
                                    if (shellDef != null)
                                    {
                                        ResolveParams rpShell = new ResolveParams
                                        {
                                            faction               = map.ParentFaction,
                                            singleThingDef        = shellDef,
                                            singleThingStackCount = Rand.RangeInclusive(8, Math.Min(12, shellDef.stackLimit))
                                        };
                                        BaseGen.symbolStack.Push("thing", rpShell);
                                    }
                                }
                            }
                        }