Example #1
0
        public static Map GenerateMap(IntVec3 mapSize, MapParent parent, MapGeneratorDef mapGenerator, IEnumerable <GenStepWithParams> extraGenStepDefs = null, Action <Map> extraInitBeforeContentGen = null)
        {
            ProgramState programState = Current.ProgramState;

            Current.ProgramState            = ProgramState.MapInitializing;
            MapGenerator.playerStartSpotInt = IntVec3.Invalid;
            MapGenerator.rootsToUnfog.Clear();
            MapGenerator.data.Clear();
            MapGenerator.mapBeingGenerated = null;
            DeepProfiler.Start("InitNewGeneratedMap");
            Rand.PushState();
            int seed = Gen.HashCombineInt(Find.World.info.Seed, parent.Tile);

            Rand.Seed = seed;
            Map result;

            try
            {
                if (parent != null && parent.HasMap)
                {
                    Log.Error("Tried to generate a new map and set " + parent + " as its parent, but this world object already has a map. One world object can't have more than 1 map.", false);
                    parent = null;
                }
                DeepProfiler.Start("Set up map");
                Map map = new Map();
                map.uniqueID = Find.UniqueIDsManager.GetNextMapID();
                MapGenerator.mapBeingGenerated = map;
                map.info.Size   = mapSize;
                map.info.parent = parent;
                map.ConstructComponents();
                DeepProfiler.End();
                Current.Game.AddMap(map);
                if (extraInitBeforeContentGen != null)
                {
                    extraInitBeforeContentGen(map);
                }
                if (mapGenerator == null)
                {
                    Log.Error("Attempted to generate map without generator; falling back on encounter map", false);
                    mapGenerator = MapGeneratorDefOf.Encounter;
                }
                IEnumerable <GenStepWithParams> enumerable = from x in mapGenerator.genSteps
                                                             select new GenStepWithParams(x, default(GenStepParams));
                if (extraGenStepDefs != null)
                {
                    enumerable = enumerable.Concat(extraGenStepDefs);
                }
                map.areaManager.AddStartingAreas();
                map.weatherDecider.StartInitialWeather();
                DeepProfiler.Start("Generate contents into map");
                MapGenerator.GenerateContentsIntoMap(enumerable, map, seed);
                DeepProfiler.End();
                Find.Scenario.PostMapGenerate(map);
                DeepProfiler.Start("Finalize map init");
                map.FinalizeInit();
                DeepProfiler.End();
                DeepProfiler.Start("MapComponent.MapGenerated()");
                MapComponentUtility.MapGenerated(map);
                DeepProfiler.End();
                if (parent != null)
                {
                    parent.PostMapGenerate();
                }
                result = map;
            }
            finally
            {
                DeepProfiler.End();
                MapGenerator.mapBeingGenerated = null;
                Current.ProgramState           = programState;
                Rand.PopState();
            }
            return(result);
        }
Example #2
0
        public static Map GenerateMap(IntVec3 mapSize, MapParent parent, MapGeneratorDef mapGenerator, IEnumerable <GenStepDef> extraGenStepDefs = null, Action <Map> extraInitBeforeContentGen = null)
        {
            ProgramState programState = Current.ProgramState;

            Current.ProgramState            = ProgramState.MapInitializing;
            MapGenerator.playerStartSpotInt = IntVec3.Invalid;
            MapGenerator.rootsToUnfog.Clear();
            MapGenerator.data.Clear();
            MapGenerator.mapBeingGenerated = null;
            Map result;

            try
            {
                DeepProfiler.Start("InitNewGeneratedMap");
                if (parent != null && parent.HasMap)
                {
                    Log.Error("Tried to generate a new map and set " + parent + " as its parent, but this world object already has a map. One world object can't have more than 1 map.");
                    parent = null;
                }
                DeepProfiler.Start("Set up map");
                Map map = new Map();
                map.uniqueID = Find.UniqueIDsManager.GetNextMapID();
                MapGenerator.mapBeingGenerated = map;
                map.info.Size   = mapSize;
                map.info.parent = parent;
                map.ConstructComponents();
                DeepProfiler.End();
                Current.Game.AddMap(map);
                if (extraInitBeforeContentGen != null)
                {
                    extraInitBeforeContentGen(map);
                }
                if (mapGenerator == null)
                {
                    mapGenerator = DefDatabase <MapGeneratorDef> .AllDefsListForReading.RandomElementByWeight((MapGeneratorDef x) => x.selectionWeight);
                }
                IEnumerable <GenStepDef> enumerable = mapGenerator.GenSteps;
                if (extraGenStepDefs != null)
                {
                    enumerable = enumerable.Concat(extraGenStepDefs);
                }
                map.areaManager.AddStartingAreas();
                map.weatherDecider.StartInitialWeather();
                DeepProfiler.Start("Generate contents into map");
                MapGenerator.GenerateContentsIntoMap(enumerable, map);
                DeepProfiler.End();
                Find.Scenario.PostMapGenerate(map);
                DeepProfiler.Start("Finalize map init");
                map.FinalizeInit();
                DeepProfiler.End();
                DeepProfiler.Start("MapComponent.MapGenerated()");
                MapComponentUtility.MapGenerated(map);
                DeepProfiler.End();
                if (parent != null)
                {
                    parent.PostMapGenerate();
                }
                result = map;
            }
            finally
            {
                DeepProfiler.End();
                MapGenerator.mapBeingGenerated = null;
                Current.ProgramState           = programState;
            }
            return(result);
        }