private void DrawTerrainPatchMakers(float x, ref float y, float width)
 {
     WindowUtil.PlusMinusLabel(x, ref y, width, "Patch Maker",
                               delegate
     {
         Find.WindowStack.Add(new Dialog_Name(
                                  "Perlin Frequency",
                                  delegate(string name)
         {
             TerrainPatchMaker m = new TerrainPatchMaker()
             {
                 perlinFrequency = float.Parse(name)
             };
             base.Def.terrainPatchMakers.Add(m);
             this.terrainPatchMakers.Add(new TerrainPatchMakerWidget(m));
         },
                                  delegate(string name)
         {
             if (!float.TryParse(name, out float freq))
             {
                 return("Must be a number");
             }
             foreach (var v in base.Def.terrainPatchMakers)
             {
                 if (v.perlinFrequency == freq)
                 {
                     return("Perlin Frequency must be unique");
                 }
             }
             return(true);
         }));
     },
        public void ApplyStats(TerrainPatchMaker to)
        {
            to.perlinFrequency   = this.perlinFrequency;
            to.perlinLacunarity  = this.perlinLacunarity;
            to.perlinPersistence = this.perlinPersistence;
            to.perlinOctaves     = this.perlinOctaves;
            to.minFertility      = this.minFertility;
            to.minFertility      = this.minFertility;
            to.maxFertility      = this.maxFertility;
            to.minSize           = this.minSize;

            if (to.thresholds != null)
            {
                to.thresholds.Clear();
            }

            if (this.thresholds != null && this.thresholds.Count > 0)
            {
                if (to.thresholds == null)
                {
                    to.thresholds = new List <TerrainThreshold>(this.thresholds.Count);
                }
                foreach (var v in this.thresholds)
                {
                    to.thresholds.Add(new TerrainThreshold()
                    {
                        terrain = v.Def,
                        min     = v.Min,
                        max     = v.Max
                    });
                }
            }
        }
        public TerrainPatchMakerWidget(TerrainPatchMaker parent)
        {
            this.Parent       = parent;
            this.inputWidgets = new List <IInputWidget>(6)
            {
                //new FloatInputWidget<TerrainPatchMaker>(this.Parent, "Perlin Frequency", (TerrainPatchMaker p) => p.perlinFrequency, (TerrainPatchMaker p, float f) => p.perlinFrequency = f),
                new FloatInputWidget <TerrainPatchMaker>(this.Parent, "Perlin Lacunarity", (TerrainPatchMaker p) => p.perlinLacunarity, (TerrainPatchMaker p, float f) => p.perlinLacunarity    = f),
                new FloatInputWidget <TerrainPatchMaker>(this.Parent, "Perlin Persistence", (TerrainPatchMaker p) => p.perlinPersistence, (TerrainPatchMaker p, float f) => p.perlinPersistence = f),
                new IntInputWidget <TerrainPatchMaker>(this.Parent, "Perlin Octaves", (TerrainPatchMaker p) => p.perlinOctaves, (TerrainPatchMaker p, int i) => p.perlinOctaves  = i),
                new FloatInputWidget <TerrainPatchMaker>(this.Parent, "Min Fertility", (TerrainPatchMaker p) => p.minFertility, (TerrainPatchMaker p, float f) => p.minFertility = f),
                new FloatInputWidget <TerrainPatchMaker>(this.Parent, "Max Fertility", (TerrainPatchMaker p) => p.maxFertility, (TerrainPatchMaker p, float f) => p.maxFertility = f),
                new IntInputWidget <TerrainPatchMaker>(this.Parent, "Min Size", (TerrainPatchMaker p) => p.minSize, (TerrainPatchMaker p, int i) => p.minSize = i),
            };

            if (this.Parent.thresholds != null)
            {
                this.thresholds = new List <MinMaxInputWidget <TerrainThreshold, float> >(this.Parent.thresholds.Count);
                foreach (var v in this.Parent.thresholds)
                {
                    this.thresholds.Add(this.CreateMinMaxInputWidget(v));
                }
            }
            else
            {
                this.thresholds = new List <MinMaxInputWidget <TerrainThreshold, float> >(0);
            }

            this.thresholdsArgs = new PlusMinusArgs <TerrainDef>()
            {
                allItems  = DefDatabase <TerrainDef> .AllDefs,
                beingUsed = delegate()
                {
                    List <TerrainDef> l = new List <TerrainDef>();
                    foreach (var v in this.thresholds)
                    {
                        l.Add(v.Parent.terrain);
                    }
                    return(l);
                },
                onAdd = delegate(TerrainDef d)
                {
                    TerrainThreshold tt = new TerrainThreshold()
                    {
                        terrain = d
                    };
                    this.Parent.thresholds.Add(tt);
                    this.thresholds.Add(this.CreateMinMaxInputWidget(tt));
                },
                onRemove = delegate(TerrainDef d)
                {
                    this.Parent.thresholds.RemoveAll((TerrainThreshold tt) => tt.terrain == d);
                    this.thresholds.RemoveAll((MinMaxInputWidget <TerrainThreshold, float> mmw) => mmw.Parent.terrain == d);
                },
                getDisplayName = (TerrainDef def) => def.label
            };
        }
        public TerrainPatchMakerStats(TerrainPatchMaker m)
        {
            this.perlinFrequency   = m.perlinFrequency;
            this.perlinLacunarity  = m.perlinLacunarity;
            this.perlinPersistence = m.perlinPersistence;
            this.perlinOctaves     = m.perlinOctaves;
            this.minFertility      = m.minFertility;
            this.maxFertility      = m.maxFertility;
            this.minSize           = m.minSize;

            Util.Populate(out this.thresholds, m.thresholds, v => new MinMaxFloatDefStat <TerrainDef>(v.terrain)
            {
                Min = v.min, Max = v.max
            });
        }
Beispiel #5
0
        public static void ApplyBiomeSettings()
        {
            Log.Message("[Map Designer] Applying settings");
            MapDesignerSettings settings = MapDesignerMod.mod.settings;

            // densities
            Dictionary <string, BiomeDefault> biomeDefaults = settings.biomeDefaults;

            float densityPlant  = settings.densityPlant;
            float densityAnimal = settings.densityAnimal;

            foreach (BiomeDef biome in DefDatabase <BiomeDef> .AllDefs)
            {
                try
                {
                    biome.animalDensity = biomeDefaults[biome.defName].animalDensity * densityAnimal;
                    biome.plantDensity  = biomeDefaults[biome.defName].plantDensity * densityPlant;

                    if (biome.plantDensity > 1f)
                    {
                        biome.wildPlantRegrowDays = biomeDefaults[biome.defName].wildPlantRegrowDays / biome.plantDensity;
                        biome.plantDensity        = 1f;
                    }
                }
                catch
                {
                    Log.Message("[Map Designer] ERROR applying plant and animal settings to " + biome.defName);
                }
            }

            Dictionary <string, FloatRange> densityDefaults = settings.densityDefaults;

            // ruins
            try
            {
                float densityRuins = settings.densityRuins;
                if (densityRuins > 1)
                {
                    densityRuins = (float)Math.Pow(densityRuins, 3);
                }
                (DefDatabase <GenStepDef> .GetNamed("ScatterRuinsSimple").genStep as GenStep_Scatterer).countPer10kCellsRange.min = densityDefaults["ScatterRuinsSimple"].min * densityRuins;
                (DefDatabase <GenStepDef> .GetNamed("ScatterRuinsSimple").genStep as GenStep_Scatterer).countPer10kCellsRange.max = densityDefaults["ScatterRuinsSimple"].max * densityRuins;
            }
            catch
            {
                Log.Message("[Map Designer] ERROR with settings: ruins");
            }

            // ancient dangers
            try
            {
                float densityDanger = settings.densityDanger;
                if (densityDanger > 1)
                {
                    densityDanger = (float)Math.Pow(densityDanger, 4);
                }
                (DefDatabase <GenStepDef> .GetNamed("ScatterShrines").genStep as GenStep_Scatterer).countPer10kCellsRange.min = densityDefaults["ScatterShrines"].min * densityDanger;
                (DefDatabase <GenStepDef> .GetNamed("ScatterShrines").genStep as GenStep_Scatterer).countPer10kCellsRange.max = densityDefaults["ScatterShrines"].max * densityDanger;
            }
            catch
            {
                Log.Message("[Map Designer] ERROR with settings: ancient dangers");
            }

            // geysers
            try
            {
                float densityGeyser = settings.densityGeyser;
                if (densityGeyser > 1)
                {
                    densityGeyser = (float)Math.Pow(densityGeyser, 2);
                }
                (DefDatabase <GenStepDef> .GetNamed("SteamGeysers").genStep as GenStep_Scatterer).countPer10kCellsRange.min = densityDefaults["SteamGeysers"].min * densityGeyser;
                (DefDatabase <GenStepDef> .GetNamed("SteamGeysers").genStep as GenStep_Scatterer).countPer10kCellsRange.max = densityDefaults["SteamGeysers"].max * densityGeyser;
            }
            catch
            {
                Log.Message("[Map Designer] ERROR with settings: geysers");
            }

            // rivers
            float widthOnMap = 6f;

            foreach (RiverDef river in DefDatabase <RiverDef> .AllDefs)
            {
                try
                {
                    river.widthOnMap *= settings.sizeRiver;
                    river.widthOnMap  = Math.Min(175, river.widthOnMap);
                }
                catch
                {
                    Log.Message("[Map Designer] ERROR with settings: river width : " + river.defName);
                }
            }

            // terrain
            if (Math.Abs(settings.terrainFert - 1f) > 0.05 || Math.Abs(settings.terrainWater - 1f) > 0.05)
            {
                Log.Message(String.Format("[Map Designer] Terrain settings: fertility: {0} | Water {1}", Math.Round(100 * settings.terrainFert), Math.Round(100 * settings.terrainWater)));
                foreach (BiomeDef biome in DefDatabase <BiomeDef> .AllDefs)
                {
                    if (!biome.terrainsByFertility.NullOrEmpty())
                    {
                        //Log.Message("Doing adjustments for " + biome.defName);
                        try
                        {
                            TerrainDefault newTerrain;
                            float          minFertBound = -0.2f;
                            float          maxFertBound = 1.2f;

                            if (ModsConfig.IsActive("BiomesTeam.BiomesCore"))
                            {
                                maxFertBound = HelperMethods.GetMaxFertByBiome(biome);
                            }
                            newTerrain = TerrainUtility.StretchTerrainFertility(biomeDefaults[biome.defName].terrain, minFertBound, maxFertBound);


                            biome.terrainsByFertility = newTerrain.terrainsByFertility;
                            biome.terrainPatchMakers  = newTerrain.terrainPatchMakers;
                        }

                        catch (Exception e)
                        {
                            Log.Message("[Map Designer] ERROR with settings: terrain : " + biome.defName);
                            Log.Message(e.Message);

                            TerrainDefault dictEntry = settings.biomeDefaults[biome.defName].terrain;
                            Log.Message("--terrainsByFertility");
                            foreach (TerrainThreshold t in dictEntry.terrainsByFertility)
                            {
                                Log.Message(String.Format("- {0} .... {1} | {2}", t.terrain.defName, Math.Round(t.min, 2), Math.Round(t.max, 2)));
                            }
                            Log.Message("--terrainPatchMakers");
                            for (int i = 0; i < dictEntry.terrainPatchMakers.Count(); i++)
                            {
                                TerrainPatchMaker p = dictEntry.terrainPatchMakers[i];
                                Log.Message(String.Format("Patchmaker #{0} | min {1} | max {2}", i, p.minFertility, p.maxFertility));
                                foreach (TerrainThreshold t in p.thresholds)
                                {
                                    Log.Message(String.Format("--- {0} | {1} | {2}", t.terrain.defName, Math.Round(t.min, 2), Math.Round(t.max, 2)));
                                }
                            }
                        }
                    }
                }
            }
            else
            {
                Log.Message("[Map Designer] Terrain settings: Default");
                foreach (BiomeDef biome in DefDatabase <BiomeDef> .AllDefs)
                {
                    biome.terrainsByFertility = biomeDefaults[biome.defName].terrain.terrainsByFertility;
                    biome.terrainPatchMakers  = biomeDefaults[biome.defName].terrain.terrainPatchMakers;
                }
            }
        }
Beispiel #6
0
        public void ApplyStats(object to)
        {
            if (to is BiomeDef t)
            {
                t.canBuildBase              = this.canBuildBase;
                t.canAutoChoose             = this.canAutoChoose;
                t.allowRoads                = this.allowRoads;
                t.allowRivers               = this.allowRivers;
                t.animalDensity             = this.animalDensity;
                t.plantDensity              = this.plantDensity;
                t.diseaseMtbDays            = this.diseaseMtbDays;
                t.settlementSelectionWeight = this.settlementSelectionWeight;
                t.impassable                = this.impassable;
                t.hasVirtualPlants          = this.hasVirtualPlants;
                t.forageability             = this.forageability;
                if (this.foragedFood == null)
                {
                    t.foragedFood = null;
                }
                else
                {
                    t.foragedFood = this.foragedFood.Def;
                }
                t.wildPlantsCareAboutLocalFertility = this.wildPlantsCareAboutLocalFertility;
                t.wildPlantRegrowDays = this.wildPlantRegrowDays;
                t.movementDifficulty  = this.movementDifficulty;
                t.hasBedrock          = this.hasBedrock;

                if (this.weatherCommonalities != null && t.baseWeatherCommonalities == null)
                {
                    t.baseWeatherCommonalities = new List <WeatherCommonalityRecord>(this.weatherCommonalities.Count);
                }
                Util.Populate(t.baseWeatherCommonalities, this.weatherCommonalities, delegate(FloatValueDefStat <WeatherDef> s)
                {
                    return(new WeatherCommonalityRecord()
                    {
                        weather = s.Def,
                        commonality = s.value
                    });
                });

                if (this.terrainsByFertility != null && t.terrainsByFertility == null)
                {
                    t.terrainsByFertility = new List <TerrainThreshold>(this.terrainsByFertility.Count);
                }
                Util.Populate(t.terrainsByFertility, this.terrainsByFertility, delegate(MinMaxFloatDefStat <TerrainDef> s)
                {
                    return(new TerrainThreshold
                    {
                        terrain = s.Def,
                        min = s.Min,
                        max = s.Max
                    });
                });

                if (this.soundsAmbient != null && t.soundsAmbient == null)
                {
                    t.soundsAmbient = new List <SoundDef>(this.soundsAmbient.Count);
                }
                Util.Populate(t.soundsAmbient, this.soundsAmbient, delegate(DefStat <SoundDef> s)
                {
                    return(s.Def);
                });

                if (this.terrainPatchMakers != null && t.terrainPatchMakers == null)
                {
                    t.terrainPatchMakers = new List <TerrainPatchMaker>(this.terrainPatchMakers.Count);
                }
                Util.Populate(t.terrainPatchMakers, this.terrainPatchMakers, delegate(TerrainPatchMakerStats s)
                {
                    var v = new TerrainPatchMaker();
                    s.ApplyStats(v);
                    return(v);
                });

                List <BiomePlantRecord> wildPlants = GetWildPlants(base.Def);
                if (this.wildPlants != null && wildPlants == null)
                {
                    wildPlants = new List <BiomePlantRecord>(this.wildPlants.Count);
                    SetWildPlants(base.Def, wildPlants);
                }
                Util.Populate(wildPlants, this.wildPlants, delegate(FloatValueDefStat <ThingDef> s)
                {
                    return(new BiomePlantRecord()
                    {
                        plant = s.Def,
                        commonality = s.value
                    });
                });

                List <BiomeAnimalRecord> wildAnimals = GetWildAnimals(base.Def);
                if (this.wildAnimals != null && wildAnimals == null)
                {
                    wildAnimals = new List <BiomeAnimalRecord>(this.wildAnimals.Count);
                    SetWildAnimals(base.Def, wildAnimals);
                }
                Util.Populate(wildAnimals, this.wildAnimals, delegate(FloatValueDefStat <PawnKindDef> s)
                {
                    return(new BiomeAnimalRecord()
                    {
                        animal = s.Def,
                        commonality = s.value
                    });
                });

                List <BiomeDiseaseRecord> diseases = GetDiseases(base.Def);
                if (this.diseases != null && diseases == null)
                {
                    diseases = new List <BiomeDiseaseRecord>(this.diseases.Count);
                    SetDiseases(base.Def, diseases);
                }
                Util.Populate(diseases, this.diseases, delegate(FloatValueDoubleDefStat <IncidentDef, BiomeDef> s)
                {
                    return(new BiomeDiseaseRecord()
                    {
                        diseaseInc = s.Def,
                        biome = s.Def2,
                        commonality = s.value
                    });
                });

                List <ThingDef> allowedPackAnimals = GetAllowedPackAnimals(base.Def);
                if (this.allowedPackAnimals != null && allowedPackAnimals == null)
                {
                    allowedPackAnimals = new List <ThingDef>(this.allowedPackAnimals.Count);
                    SetAllowedPackAnimals(base.Def, allowedPackAnimals);
                }
                Util.Populate(allowedPackAnimals, this.allowedPackAnimals, delegate(DefStat <PawnKindDef> s)
                {
                    return(s.Def.race);
                });
            }
            else
            {
                Log.Error("ThingDefStat passed none ThingDef!");
            }
        }
        public static TerrainDefault StretchTerrainFertility(TerrainDefault old, float min, float max)
        {
            oldTerrain = old;
            minMapFert = min;
            maxMapFert = max;

            newTerrain = new TerrainDefault();
            newTerrain.terrainsByFertility = new List <TerrainThreshold>(oldTerrain.terrainsByFertility);

            newTerrain.terrainPatchMakers = new List <TerrainPatchMaker>();
            for (int i = 0; i < oldTerrain.terrainPatchMakers.Count(); i++)
            {
                TerrainPatchMaker p = new TerrainPatchMaker();
                p.maxFertility      = oldTerrain.terrainPatchMakers[i].maxFertility;
                p.minFertility      = oldTerrain.terrainPatchMakers[i].minFertility;
                p.perlinFrequency   = oldTerrain.terrainPatchMakers[i].perlinFrequency;
                p.perlinLacunarity  = oldTerrain.terrainPatchMakers[i].perlinLacunarity;
                p.perlinOctaves     = oldTerrain.terrainPatchMakers[i].perlinOctaves;
                p.perlinPersistence = oldTerrain.terrainPatchMakers[i].perlinPersistence;
                p.thresholds        = new List <TerrainThreshold>(oldTerrain.terrainPatchMakers[i].thresholds);

                newTerrain.terrainPatchMakers.Add(p);
            }


            List <TerrainThreshold> oldTerrainsByFertility = oldTerrain.terrainsByFertility;

            List <TBF> listTbf = new List <TBF>();

            // convert to TBFs
            for (int i = 0; i < oldTerrainsByFertility.Count(); i++)
            {
                TBF item = new TBF()
                {
                    thresh = oldTerrainsByFertility[i],
                    size   = Math.Min(oldTerrainsByFertility[i].max, maxMapFert) - Math.Max(oldTerrainsByFertility[i].min, minMapFert)
                };
                listTbf.Add(item);
            }

            // the actual adjustments
            if (listTbf.Count() >= 1)
            {
                FertChangeTbf(listTbf);
            }

            // TerrainPatchMaker adjustments
            List <TerrainDef> patchTerrains = new List <TerrainDef>();

            foreach (TerrainPatchMaker p in newTerrain.terrainPatchMakers)
            {
                foreach (TerrainThreshold t in p.thresholds)
                {
                    patchTerrains.Add(t.terrain);
                }
            }

            // check that there are terrains
            if (patchTerrains.Count() >= 1)
            {
                FertChangePatchMakers(patchTerrains);
            }

            return(newTerrain);
        }
        private static void FertChangePatchMakers(List <TerrainDef> patchTerrains)
        {
            MapDesignerSettings settings = MapDesignerMod.mod.settings;
            float minAllowable           = -1.5f;
            float maxAllowable           = 1.5f;

            // find highest fert terrain overall
            patchTerrains.Sort((x, y) => x.fertility.CompareTo(y.fertility));

            float maxFert = patchTerrains.Max(t => t.fertility);

            for (int index = 0; index < newTerrain.terrainPatchMakers.Count; index++)
            {
                TerrainPatchMaker p = newTerrain.terrainPatchMakers[index];

                // sort terrains by min
                p.thresholds.Sort((x, y) => x.min.CompareTo(y.min));

                // Make new list:
                List <TBF> listTbf = new List <TBF>();
                float      current = -999f;

                for (int i = 0; i < p.thresholds.Count; i++)
                {
                    // if current == terrain min, add terrain to list
                    // set current = terrain max
                    // if terrain is the minFert or maxFert, change size appropriately
                    TBF tbf         = new TBF();
                    TBF placeholder = new TBF();
                    if (!Mathf.Approximately(current, p.thresholds[i].min))
                    {
                        //placeholder when needed
                        placeholder.size = Math.Min(p.thresholds[i].min, maxAllowable) - Math.Max(current, minAllowable);
                        current          = p.thresholds[i].min;
                        listTbf.Add(placeholder);
                    }

                    // real thing
                    current = p.thresholds[i].max;

                    tbf.thresh = p.thresholds[i];
                    tbf.size   = Math.Min(p.thresholds[i].max, maxAllowable) - Math.Max(p.thresholds[i].min, minAllowable);

                    if (tbf.thresh.terrain.fertility == maxFert)
                    {
                        tbf.size *= settings.terrainFert;
                    }

                    if (tbf.thresh.terrain.IsWater)
                    {
                        tbf.size *= settings.terrainWater;
                    }
                    else if (settings.flagTerrainWater && !tbf.thresh.terrain.affordances.Contains(TerrainAffordanceDefOf.Heavy))
                    {
                        tbf.size *= settings.terrainWater;
                    }
                    listTbf.Add(tbf);
                }

                // add extra placeholder at end if needed
                if (current < maxAllowable)
                {
                    listTbf.Add(new TBF()
                    {
                        size = maxAllowable - current
                    });
                }
                newTerrain.terrainPatchMakers[index].thresholds = SquishTerrain(listTbf, minAllowable, maxAllowable);
            }
        }