public static Map GetOrGenerateMap(int tile, IntVec3 size, WorldObjectDef suggestedMapParentDef)
        {
            Map map = Current.Game.FindMap(tile);

            if (map == null)
            {
                MapParent mapParent = Find.WorldObjects.MapParentAt(tile);
                if (mapParent == null)
                {
                    if (suggestedMapParentDef == null)
                    {
                        Log.Error("Tried to get or generate map at " + tile + ", but there isn't any MapParent world object here and map parent def argument is null.", false);
                        return(null);
                    }
                    mapParent      = (MapParent)WorldObjectMaker.MakeWorldObject(suggestedMapParentDef);
                    mapParent.Tile = tile;
                    Find.WorldObjects.Add(mapParent);
                }
                map = MapGenerator.GenerateMap(size, mapParent, mapParent.MapGeneratorDef, mapParent.ExtraGenStepDefs, null);
            }
            return(map);
        }
Example #2
0
 public static void GenerateContentsIntoMap(IEnumerable <GenStepWithParams> genStepDefs, Map map, int seed)
 {
     MapGenerator.data.Clear();
     Rand.PushState();
     try
     {
         Rand.Seed = seed;
         RockNoises.Init(map);
         MapGenerator.tmpGenSteps.Clear();
         MapGenerator.tmpGenSteps.AddRange(from x in genStepDefs
                                           orderby x.def.order, x.def.index
                                           select x);
         for (int i = 0; i < MapGenerator.tmpGenSteps.Count; i++)
         {
             DeepProfiler.Start("GenStep - " + MapGenerator.tmpGenSteps[i].def);
             try
             {
                 Rand.Seed = Gen.HashCombineInt(seed, MapGenerator.GetSeedPart(MapGenerator.tmpGenSteps, i));
                 MapGenerator.tmpGenSteps[i].def.genStep.Generate(map, MapGenerator.tmpGenSteps[i].parms);
             }
             catch (Exception arg)
             {
                 Log.Error("Error in GenStep: " + arg, false);
             }
             finally
             {
                 DeepProfiler.End();
             }
         }
     }
     finally
     {
         Rand.PopState();
         RockNoises.Reset();
         MapGenerator.data.Clear();
     }
 }
Example #3
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 #4
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);
        }
Example #5
0
        public void InitNewGame()
        {
            string str = (from mod in LoadedModManager.RunningMods
                          select mod.ToString()).ToCommaList(false);

            Log.Message("Initializing new game with mods " + str, false);
            if (this.maps.Any <Map>())
            {
                Log.Error("Called InitNewGame() but there already is a map. There should be 0 maps...", false);
                return;
            }
            if (this.initData == null)
            {
                Log.Error("Called InitNewGame() but init data is null. Create it first.", false);
                return;
            }
            MemoryUtility.UnloadUnusedUnityAssets();
            DeepProfiler.Start("InitNewGame");
            try
            {
                Current.ProgramState = ProgramState.MapInitializing;
                IntVec3           intVec      = new IntVec3(this.initData.mapSize, 1, this.initData.mapSize);
                Settlement        settlement  = null;
                List <Settlement> settlements = Find.WorldObjects.Settlements;
                for (int i = 0; i < settlements.Count; i++)
                {
                    if (settlements[i].Faction == Faction.OfPlayer)
                    {
                        settlement = settlements[i];
                        break;
                    }
                }
                if (settlement == null)
                {
                    Log.Error("Could not generate starting map because there is no any player faction base.", false);
                }
                this.tickManager.gameStartAbsTick = GenTicks.ConfiguredTicksAbsAtGameStart;
                Map currentMap = MapGenerator.GenerateMap(intVec, settlement, settlement.MapGeneratorDef, settlement.ExtraGenStepDefs, null);
                this.worldInt.info.initialMapSize = intVec;
                if (this.initData.permadeath)
                {
                    this.info.permadeathMode           = true;
                    this.info.permadeathModeUniqueName = PermadeathModeUtility.GeneratePermadeathSaveName();
                }
                PawnUtility.GiveAllStartingPlayerPawnsThought(ThoughtDefOf.NewColonyOptimism);
                this.FinalizeInit();
                Current.Game.CurrentMap = currentMap;
                Find.CameraDriver.JumpToCurrentMapLoc(MapGenerator.PlayerStartSpot);
                Find.CameraDriver.ResetSize();
                if (Prefs.PauseOnLoad && this.initData.startedFromEntry)
                {
                    LongEventHandler.ExecuteWhenFinished(delegate
                    {
                        this.tickManager.DoSingleTick();
                        this.tickManager.CurTimeSpeed = TimeSpeed.Paused;
                    });
                }
                Find.Scenario.PostGameStart();
                if (Faction.OfPlayer.def.startingResearchTags != null)
                {
                    foreach (ResearchProjectTagDef current in Faction.OfPlayer.def.startingResearchTags)
                    {
                        foreach (ResearchProjectDef current2 in DefDatabase <ResearchProjectDef> .AllDefs)
                        {
                            if (current2.HasTag(current))
                            {
                                this.researchManager.FinishProject(current2, false, null);
                            }
                        }
                    }
                }
                GameComponentUtility.StartedNewGame();
                this.initData = null;
            }
            finally
            {
                DeepProfiler.End();
            }
        }
Example #6
0
        public void InitNewGame()
        {
            string str = LoadedModManager.RunningMods.Select((ModContentPack mod) => mod.PackageIdPlayerFacing).ToLineList("  - ");

            Log.Message("Initializing new game with mods:\n" + str);
            if (maps.Any())
            {
                Log.Error("Called InitNewGame() but there already is a map. There should be 0 maps...");
                return;
            }
            if (initData == null)
            {
                Log.Error("Called InitNewGame() but init data is null. Create it first.");
                return;
            }
            MemoryUtility.UnloadUnusedUnityAssets();
            DeepProfiler.Start("InitNewGame");
            try
            {
                //InitMap
                Current.ProgramState = ProgramState.MapInitializing;
                IntVec3           intVec      = new IntVec3(initData.mapSize, 1, initData.mapSize);
                Settlement        settlement  = null;
                List <Settlement> settlements = Find.WorldObjects.Settlements;
                for (int i = 0; i < settlements.Count; i++)
                {
                    if (settlements[i].Faction == Faction.OfPlayer)
                    {
                        settlement = settlements[i];
                        break;
                    }
                }
                if (settlement == null)
                {
                    Log.Error("Could not generate starting map because there is no any player faction base.");
                }
                tickManager.gameStartAbsTick = GenTicks.ConfiguredTicksAbsAtGameStart;

                //--------------
                //Map currentMap=Map

                //Map currentMap = MapGenerator.GenerateMap(intVec, settlement, settlement.MapGeneratorDef, settlement.ExtraGenStepDefs);

                //Map currentMap=MapGenerator.GenerateMap(intVec,settlement,settlement.MapGeneratorDef,settlement.ExtraGenStepDefs)

                Map currentMap = MapGenerator.GenerateMap(intVec, settlement, settlement.MapGeneratorDef, settlement.ExtraGenStepDefs);
                worldInt.info.initialMapSize = intVec;
                if (initData.permadeath)
                {
                    info.permadeathMode           = true;
                    info.permadeathModeUniqueName = PermadeathModeUtility.GeneratePermadeathSaveName();
                }
                PawnUtility.GiveAllStartingPlayerPawnsThought(ThoughtDefOf.NewColonyOptimism);
                FinalizeInit();
                Current.Game.CurrentMap = currentMap;
                Find.CameraDriver.JumpToCurrentMapLoc(MapGenerator.PlayerStartSpot);
                Find.CameraDriver.ResetSize();
                if (Prefs.PauseOnLoad && initData.startedFromEntry)
                {
                    LongEventHandler.ExecuteWhenFinished(delegate
                    {
                        tickManager.DoSingleTick();
                        tickManager.CurTimeSpeed = TimeSpeed.Paused;
                    });
                }

                //--------------end
                Find.Scenario.PostGameStart();
                if (Faction.OfPlayer.def.startingResearchTags != null)
                {
                    foreach (ResearchProjectTagDef startingResearchTag in Faction.OfPlayer.def.startingResearchTags)
                    {
                        foreach (ResearchProjectDef allDef in DefDatabase <ResearchProjectDef> .AllDefs)
                        {
                            if (allDef.HasTag(startingResearchTag))
                            {
                                researchManager.FinishProject(allDef);
                            }
                        }
                    }
                }
                if (Faction.OfPlayer.def.startingTechprintsResearchTags != null)
                {
                    foreach (ResearchProjectTagDef startingTechprintsResearchTag in Faction.OfPlayer.def.startingTechprintsResearchTags)
                    {
                        foreach (ResearchProjectDef allDef2 in DefDatabase <ResearchProjectDef> .AllDefs)
                        {
                            if (allDef2.HasTag(startingTechprintsResearchTag))
                            {
                                int techprints = researchManager.GetTechprints(allDef2);
                                if (techprints < allDef2.techprintCount)
                                {
                                    researchManager.AddTechprints(allDef2, allDef2.techprintCount - techprints);
                                }
                            }
                        }
                    }
                }
                GameComponentUtility.StartedNewGame();
                initData = null;
            }
            finally
            {
                DeepProfiler.End();
            }
        }
Example #7
0
        public void InitNewGame()
        {
            string str = GenText.ToCommaList(from mod in LoadedModManager.RunningMods
                                             select mod.ToString(), true);

            Log.Message("Initializing new game with mods " + str);
            if (this.maps.Any())
            {
                Log.Error("Called InitNewGame() but there already is a map. There should be 0 maps...");
            }
            else if (this.initData == null)
            {
                Log.Error("Called InitNewGame() but init data is null. Create it first.");
            }
            else
            {
                MemoryUtility.UnloadUnusedUnityAssets();
                DeepProfiler.Start("InitNewGame");
                try
                {
                    Current.ProgramState = ProgramState.MapInitializing;
                    IntVec3            intVec       = new IntVec3(this.initData.mapSize, 1, this.initData.mapSize);
                    FactionBase        factionBase  = null;
                    List <FactionBase> factionBases = Find.WorldObjects.FactionBases;
                    int num = 0;
                    while (num < factionBases.Count)
                    {
                        if (factionBases[num].Faction != Faction.OfPlayer)
                        {
                            num++;
                            continue;
                        }
                        factionBase = factionBases[num];
                        break;
                    }
                    if (factionBase == null)
                    {
                        Log.Error("Could not generate starting map because there is no any player faction base.");
                    }
                    this.tickManager.gameStartAbsTick = GenTicks.ConfiguredTicksAbsAtGameStart;
                    Map visibleMap = MapGenerator.GenerateMap(intVec, factionBase, factionBase.MapGeneratorDef, factionBase.ExtraGenStepDefs, null);
                    this.worldInt.info.initialMapSize = intVec;
                    if (this.initData.permadeath)
                    {
                        this.info.permadeathMode           = true;
                        this.info.permadeathModeUniqueName = PermadeathModeUtility.GeneratePermadeathSaveName();
                    }
                    PawnUtility.GiveAllStartingPlayerPawnsThought(ThoughtDefOf.NewColonyOptimism);
                    this.FinalizeInit();
                    Current.Game.VisibleMap = visibleMap;
                    Find.CameraDriver.JumpToVisibleMapLoc(MapGenerator.PlayerStartSpot);
                    Find.CameraDriver.ResetSize();
                    if (Prefs.PauseOnLoad && this.initData.startedFromEntry)
                    {
                        LongEventHandler.ExecuteWhenFinished(delegate
                        {
                            this.tickManager.DoSingleTick();
                            this.tickManager.CurTimeSpeed = TimeSpeed.Paused;
                        });
                    }
                    Find.Scenario.PostGameStart();
                    if (Faction.OfPlayer.def.startingResearchTags != null)
                    {
                        foreach (string startingResearchTag in Faction.OfPlayer.def.startingResearchTags)
                        {
                            foreach (ResearchProjectDef allDef in DefDatabase <ResearchProjectDef> .AllDefs)
                            {
                                if (allDef.HasTag(startingResearchTag))
                                {
                                    this.researchManager.InstantFinish(allDef, false);
                                }
                            }
                        }
                    }
                    GameComponentUtility.StartedNewGame();
                    this.initData = null;
                }
                finally
                {
                    DeepProfiler.End();
                }
            }
        }