Example #1
0
        // 5th step: Get the PawnKindDef of the position from the PawnData of the blueprint.
        private static ThingData TryGetPawnKindDefFromPawnData(BaseBlueprintDef blueprint, int itemPos)
        {
            if (blueprint.pawnData == null || blueprint.pawnData.Count() - 1 < itemPos ||
                blueprint.pawnLegend == null)
            {
                return(null);
            }

            char   keyChar = blueprint.pawnData.ElementAt(itemPos);
            string key     = keyChar.ToString();

            if (!blueprint.pawnLegend.ContainsKey(key))
            {
                return(null);
            }

            return(blueprint.pawnLegend[key]);
        }
Example #2
0
        // 2nd step (b): Get the Rotation of the position from the BuildingData of the blueprint.
        private static Rot4 TryGetRotationFromBuildingData(BaseBlueprintDef blueprint, int itemPos)
        {
            // Using buildingData and rotationLegend here..
            if (blueprint.buildingData == null || blueprint.buildingData.Count() - 1 < itemPos ||
                blueprint.rotationLegend == null)
            {
                return(Rot4.Invalid);
            }

            char   keyChar = blueprint.buildingData.ElementAt(itemPos);
            string key     = keyChar.ToString();

            if (!blueprint.rotationLegend.ContainsKey(key))
            {
                return(Rot4.Invalid);
            }

            return(blueprint.rotationLegend[key]);
        }
Example #3
0
        public static void CreateBlueprintAt(IntVec3 c, Map map, BaseBlueprintDef blueprint, Faction faction, ThingDef wallStuff, out Dictionary <Pawn, LordType> pawns, out float totalThreat, bool useOneFaction = false, bool useAdditionThreat = false, float additionalPoints = 0f)
        {
            pawns       = null;
            totalThreat = 0;

            if (working)
            {
                Log.Error("Called BlueprintHandler.CreateBlueprintAt(..) while it's still working. This is not allowed!");
                return;
            }

            if (map == null)
            {
                Log.Error("Called BlueprintHandler.CreateBlueprintAt(..) with null map.");
                return;
            }

            working         = true;
            allSpawnedPawns = new Dictionary <Pawn, LordType>();
            rooms           = new HashSet <Room>();


            try
            {
                CellRect mapRect = new CellRect(c.x, c.z, blueprint.size.x, blueprint.size.z);

                mapRect.ClipInsideMap(map);

                // if mapRect was clipped -> the blueprint doesn't fit inside the map...
                if (mapRect.Width != blueprint.size.x || mapRect.Height != blueprint.size.z)
                {
                    return;
                }



                //// Don't do anything, if there is an cryosleep casket at the building site
                //foreach (IntVec3 current in mapRect.Cells)
                //{
                //    List<Thing> list = map.thingGrid.ThingsListAt(current);
                //    for (int i = 0; i < list.Count; i++)
                //    {
                //        if (list[i].def == ThingDefOf.AncientCryptosleepCasket)
                //            return;
                //    }

                //    if (usedSpots != null)
                //        usedSpots.Add(current); // prevent the base scatterer to use this spot again
                //}

                // If a building material is defined, use this
                if (blueprint.defaultBuildingMaterial != null)
                {
                    wallStuff = blueprint.defaultBuildingMaterial;
                }

                // Make all buildings from the same random stuff
                if (wallStuff == null)
                {
                    wallStuff = BaseGenUtility.RandomCheapWallStuff(faction, false);
                }

                MakeBlueprintObject(map, faction, mapRect, blueprint, wallStuff, out pawns, out totalThreat, useOneFaction, useAdditionThreat, additionalPoints);

                if (blueprint.createTrigger)
                {
                    int    nextSignalTagID = Find.UniqueIDsManager.GetNextSignalTagID();
                    string signalTag       = "unfogBaseAreaTriggerSignal-" + nextSignalTagID;
                    SignalAction_Letter signalAction_Letter = (SignalAction_Letter)ThingMaker.MakeThing(ThingDefOf.SignalAction_Letter, null);
                    signalAction_Letter.signalTag = signalTag;

                    if (blueprint.TriggerLetterMessageText != null)
                    {
                        if (blueprint.TriggerLetterDef == null)
                        {
                            blueprint.TriggerLetterDef = LetterDefOf.ThreatSmall;
                        }

                        if (blueprint.TriggerLetterLabel != null)
                        {
                            signalAction_Letter.letter = LetterMaker.MakeLetter(blueprint.TriggerLetterLabel.Translate(), blueprint.TriggerLetterMessageText.Translate(), blueprint.TriggerLetterDef, new GlobalTargetInfo(mapRect.CenterCell, map, false));
                        }
                        else
                        {
                            signalAction_Letter.letter = LetterMaker.MakeLetter("", blueprint.TriggerLetterMessageText.Translate(), blueprint.TriggerLetterDef, new GlobalTargetInfo(mapRect.CenterCell, map));
                        }

                        GenSpawn.Spawn(signalAction_Letter, mapRect.CenterCell, map);
                    }

                    RectTrigger_UnfogBaseArea rectTrigger = (RectTrigger_UnfogBaseArea)ThingMaker.MakeThing(ThingDef.Named("RectTrigger_UnfogBaseArea"), null);
                    rectTrigger.signalTag         = signalTag;
                    rectTrigger.destroyIfUnfogged = true;
                    rectTrigger.Rect = mapRect;

                    GenSpawn.Spawn(rectTrigger, mapRect.CenterCell, map);
                }


                map.regionAndRoomUpdater.RebuildAllRegionsAndRooms();

                HashSet <Room> rooms = new HashSet <Room>();
                foreach (IntVec3 current in mapRect.Cells)
                {
                    // Find all created rooms
                    Room room = current.GetRoom(map);
                    if (room != null && !room.TouchesMapEdge)
                    {
                        rooms.Add(room);
                    }
                }

                // Add rooms to unfog area
                AddRoomCentersToRootsToUnfog(rooms.ToList());


                // Create roof
                foreach (Room room in rooms)
                {
                    BuildRoofToRoom(room, false);
                }

                map.roofGrid.RoofGridUpdate();
            }
            catch (Exception ex)
            {
                Log.Error("Error in BlueprintHandler.CreateBlueprintAt(..): " + ex);
            }
            finally
            {
                // Whatever happends, when its done, reset the working state.
                working = false;

                // Clear all data holder
                //allSpawnedPawns = null;
                rooms = null;
            }
        }
Example #4
0
        private static void MakeBlueprintObject(Map map, Faction faction, CellRect mapRect, BaseBlueprintDef blueprint, ThingDef stuffDef, out Dictionary <Pawn, LordType> pawns, out float totalThreat, bool useOneFaction = false, bool useAddtionalThreat = false, float additionalPoints = 0f)
        {
            blueprint.buildingData    = GetCleanedBlueprintData(blueprint.buildingData);
            blueprint.nonbuildingData = GetCleanedBlueprintData(blueprint.nonbuildingData);
            blueprint.floorData       = GetCleanedBlueprintData(blueprint.floorData);
            blueprint.pawnData        = GetCleanedBlueprintData(blueprint.pawnData);
            blueprint.itemData        = GetCleanedBlueprintData(blueprint.itemData);

            pawns       = null;
            totalThreat = 0;

            if (blueprint.buildingData == null && blueprint.nonbuildingData == null && blueprint.floorData == null)
            {
                Log.ErrorOnce(string.Format("After cleaning the BlueprintData and FloorData of blueprint {0} -> both are null, nothing will be done!", blueprint.defName), 313001);
                return;
            }

            IntVec3 spawnBaseCell = new IntVec3(mapRect.BottomLeft.x, mapRect.TopRight.y, mapRect.TopRight.z);
            IntVec3 spawnCell;

            //// Check all cells and abort if there is something indestructible found -> disabled
            //foreach (IntVec3 cell in mapRect)
            //{
            //    if (!CheckCell(cell, map))
            //        return;
            //}

            //allSpawnedPawns = null;


            // Disable automatic room updating
            map.regionAndRoomUpdater.Enabled = false;

            int step = 1;

            while (step <= 5)
            {
                //Log.Warning("Current step: " + step);

                // Work through blueprint - Note: top-left to bottom-right
                // Work step by step: 1st all floors, 2nd all things, 3rd all items, 4th all pawns
                for (int zn = 0; zn < blueprint.size.z; zn++)
                {
                    for (int x = 0; x < blueprint.size.x; x++)
                    {
                        //// map can be clipped, don't work with the clipped parts
                        //if (x > mapRect.Width - 1 || zn > mapRect.Height - 1)
                        //    continue;

                        if (blueprint.canHaveHoles && Rand.Value < 0.08f)
                        {
                            continue;
                        }

                        spawnCell = spawnBaseCell + new IntVec3(x, 0, -zn);

                        if (!TrySetCell_prepare_CheckCell(spawnCell, map))
                        {
                            continue;
                        }


                        int itemPos = x + blueprint.size.x * zn;

                        try
                        {
                            ThingData thingData    = TryGetThingDefFromBuildingData(blueprint, itemPos);
                            Rot4      thingRot     = TryGetRotationFromBuildingData(blueprint, itemPos);
                            ThingData nonthingData = TryGetThingDefFromNonBuildingData(blueprint, itemPos);
                            ThingData terrainData  = TryGetTerrainDefFromFloorData(blueprint, itemPos);
                            ThingData pawnKindData = TryGetPawnKindDefFromPawnData(blueprint, itemPos);
                            ThingData itemData     = TryGetItemDefFromItemData(blueprint, itemPos);

                            //List<Thing> list = map.thingGrid.ThingsListAt(spawnCell);
                            //for (int i = 0; i < list.Count; i++)
                            //{
                            //    if (list[i].def == thingDef)
                            //        continue;
                            //}


                            //Only clear the space, if something will be made here
                            // Do only in step 1:
                            if (step == 1)
                            {
                                if (thingData?.Thing != null || terrainData?.Terrain != null || pawnKindData?.Kind != null || itemData?.Thing != null)
                                {
                                    ClearCell(spawnCell, map);
                                }
                            }

                            switch (step)
                            {
                            case 1:     // Terrain
                                TrySetCell_1_SetFloor(spawnCell, map, terrainData, thingData?.Thing, stuffDef);
                                break;

                            case 2:     // non-Building
                                TrySetCell_3_SetNonThing(spawnCell, map, nonthingData, stuffDef);
                                break;

                            case 3:     // Building
                                TrySetCell_2_SetThing(spawnCell, map, faction, thingData, thingRot, stuffDef);
                                break;

                            case 4:     // Item
                                TrySetCell_4_SetItem(spawnCell, map, itemData);
                                break;

                            case 5:     // Pawn
                                TrySetCell_5_SetPawn(spawnCell, map, faction, pawnKindData, useOneFaction);
                                break;

                            default:
                                return;
                            }
                        }
                        catch (Exception ex)
                        {
                            Log.Warning("MapGeneratorFactionBase - Error while creating the blueprint (" + blueprint.defName + ")\n" + ex.Message + "\n" + ex.StackTrace);
                        }
                    }
                }
                step++;
            }

            // Update the powernets
            map.powerNetManager.UpdatePowerNetsAndConnections_First();

            // Enable automatic room updating and rebuild all rooms
            map.regionAndRoomUpdater.Enabled = true;
            map.regionAndRoomUpdater.RebuildAllRegionsAndRooms();

            HashSet <Room> rooms = new HashSet <Room>();

            foreach (IntVec3 current in mapRect.Cells)
            {
                // Find all created rooms
                Room room = current.GetRoom(map);
                if (room != null && !room.TouchesMapEdge)
                {
                    rooms.Add(room);
                }
                else
                {
                    map.fogGrid.Unfog(current);
                }
            }

            if (!blueprint.FogRooms)
            {
                AddRoomCentersToRootsToUnfog(rooms.ToList());
            }
            else
            {
                AddRoomsToFog(rooms.ToList(), map);
            }

            Lord lordDefend = null;
            Lord lordAttack = null;

            foreach (var pair in allSpawnedPawns)
            {
                switch (pair.Value)
                {
                case LordType.Defend:
                {
                    if (lordDefend == null)
                    {
                        lordDefend = LordMaker.MakeNewLord(faction, new LordJob_DefendBase(faction, mapRect.CenterCell), map, null);
                        lordDefend.numPawnsLostViolently = int.MaxValue;
                    }

                    lordDefend.AddPawn(pair.Key);

                    break;
                }

                case LordType.Attack:
                {
                    if (lordAttack == null)
                    {
                        lordAttack = LordMaker.MakeNewLord(faction, new LordJob_AssaultColony(faction, canTimeoutOrFlee: false), map, null);
                        lordAttack.numPawnsLostViolently = int.MaxValue;
                    }

                    lordAttack.AddPawn(pair.Key);

                    break;
                }
                }

                totalThreat += (int)pair.Key.kindDef.combatPower;
            }


            // Make additional pawns if these are not enough!
            if (useAddtionalThreat || blueprint.UseAdditionalThreat)
            {
                if (lordDefend == null)
                {
                    lordDefend = LordMaker.MakeNewLord(faction, new LordJob_DefendBase(faction, mapRect.CenterCell), map, null);
                    lordDefend.numPawnsLostViolently = int.MaxValue;
                }

                float total = blueprint.ThreatsPoints + additionalPoints;

                //Log.Warning("Info: Creating base pawns..");
                PrepareBaseGen_PawnGroup(map, mapRect, faction, rooms.ToList(), lordDefend, total);
            }

            PrepareBaseGen_CampFires(map, mapRect, faction);

            BaseGen.Generate();

            /*
             * if (allSpawnedPawns != null)
             * {
             *  foreach (Pawn pawn in allSpawnedPawns)
             *      pawnLord.AddPawn(pawn);
             * }
             */

            pawns = allSpawnedPawns;

            //allSpawnedPawns = null;
        }