private HashSet <Vector2I> DigFeature(ProcGen.Room.Shape shape, float size, List <int> bordersWidths, SeededRandom rnd, out List <Vector2I> featureCenterPoints, out List <List <Vector2I> > featureBorders) { HashSet <Vector2I> hashSet = new HashSet <Vector2I>(); featureCenterPoints = new List <Vector2I>(); featureBorders = new List <List <Vector2I> >(); if (size < 1f) { return(hashSet); } Vector2 center = site.poly.Centroid(); finalSize = size; switch (shape) { case ProcGen.Room.Shape.Blob: featureCenterPoints = ProcGen.Util.GetBlob(center, finalSize, rnd.RandomSource()); break; case ProcGen.Room.Shape.Circle: featureCenterPoints = ProcGen.Util.GetFilledCircle(center, finalSize); break; case ProcGen.Room.Shape.Square: featureCenterPoints = ProcGen.Util.GetFilledRectangle(center, finalSize, finalSize, rnd, 2f, 2f); break; case ProcGen.Room.Shape.TallThin: featureCenterPoints = ProcGen.Util.GetFilledRectangle(center, finalSize / 4f, finalSize, rnd, 2f, 2f); break; case ProcGen.Room.Shape.ShortWide: featureCenterPoints = ProcGen.Util.GetFilledRectangle(center, finalSize, finalSize / 4f, rnd, 2f, 2f); break; case ProcGen.Room.Shape.Splat: featureCenterPoints = ProcGen.Util.GetSplat(center, finalSize, rnd.RandomSource()); break; } hashSet.UnionWith(featureCenterPoints); if (featureCenterPoints.Count == 0) { Debug.LogWarning("Room has no centerpoints. Terrain Cell [ shape: " + shape.ToString() + " size: " + finalSize + "] [" + node.node.Id + " " + node.type + " " + node.position + "]"); } else if (bordersWidths != null && bordersWidths.Count > 0 && bordersWidths[0] > 0) { for (int i = 0; i < bordersWidths.Count && bordersWidths[i] > 0; i++) { featureBorders.Add(ProcGen.Util.GetBorder(hashSet, bordersWidths[i])); hashSet.UnionWith(featureBorders[i]); } } return(hashSet); }
public static List <Vector2> GetSpaceFillingRandom(Rect boundingArea, float density, SeededRandom rnd) { List <Vector2> filliedRectangle = GetFilliedRectangle(boundingArea, density); filliedRectangle.ShuffleSeeded(rnd.RandomSource()); return(filliedRectangle); }
private static List <int> GetMobPossibleSpawnPoints(Mob mob, List <int> possibleSpawnPoints, Sim.Cell[] cells, HashSet <int> alreadyOccupiedCells, SeededRandom rnd) { List <int> list = possibleSpawnPoints.FindAll((int cell) => IsSuitableMobSpawnPoint(cell, mob, cells, ref alreadyOccupiedCells)); list.ShuffleSeeded(rnd.RandomSource()); return(list); }
private void ApplyForeground(WorldGenSettings settings, Chunk world, SetValuesFunction SetValues, float temperatureMin, float temperatureRange, SeededRandom rnd) { LogInfo("Apply foregreound", (node.tags != null).ToString(), (float)((node.tags != null) ? node.tags.Count : 0)); if (node.tags != null) { FeatureSettings featureSettings = settings.TryGetFeature(node.type); LogInfo("\tFeature?", (featureSettings != null).ToString(), 0f); if (featureSettings == null && node.tags != null) { List <Tag> list = new List <Tag>(); foreach (Tag tag2 in node.tags) { if (settings.HasFeature(tag2.Name)) { list.Add(tag2); } } LogInfo("\tNo feature, checking possible feature tags, found", string.Empty, (float)list.Count); if (list.Count > 0) { Tag tag = list[rnd.RandomSource().Next(list.Count)]; featureSettings = settings.GetFeature(tag.Name); LogInfo("\tPicked feature", tag.Name, 0f); } } if (featureSettings != null) { LogInfo("APPLY FOREGROUND", node.type, 0f); float num = featureSettings.blobSize.GetRandomValueWithinRange(rnd); float num2 = poly.DistanceToClosestEdge(null); if (!node.tags.Contains(WorldGenTags.AllowExceedNodeBorders) && num2 < num) { if (debugMode) { Debug.LogWarning(node.type + " " + featureSettings.shape + " blob size too large to fit in node. Size reduced. " + num + "->" + (num2 - 6f).ToString()); } num = num2 - 6f; } if (!(num <= 0f)) { featureSpawnPoints = DigFeature(featureSettings.shape, num, featureSettings.borders, rnd, out List <Vector2I> featureCenterPoints, out List <List <Vector2I> > featureBorders); LogInfo("\t\t", "claimed points", (float)featureSpawnPoints.Count); availableTerrainPoints.ExceptWith(featureSpawnPoints); ApplyPlaceElementForRoom(featureSettings, "RoomCenterElements", featureCenterPoints, world, SetValues, temperatureMin, temperatureRange, rnd); if (featureBorders != null) { for (int i = 0; i < featureBorders.Count; i++) { ApplyPlaceElementForRoom(featureSettings, "RoomBorderChoices" + i, featureBorders[i], world, SetValues, temperatureMin, temperatureRange, rnd); } } if (featureSettings.tags.Contains(WorldGenTags.HighPriorityFeature.Name)) { AddHighPriorityCells(featureSpawnPoints); } } } } }
public static void CalculateGeysers(SeededRandom rnd) { Dictionary <string, Dictionary <string, int> > calculatedGeysers = new Dictionary <string, Dictionary <string, int> >(); foreach (KeyValuePair <string, GeyserState> data in WorldGenReloadedData.GeyserConfig) { int geyserCount = rnd.RandomRange(data.Value.Minimum, data.Value.Maximum + 1); string[] subworlds = data.Value.SubWorlds; subworlds.ShuffleSeeded(rnd.RandomSource()); for (int i = 0; i < geyserCount; i++) { string subworld = subworlds[i % subworlds.Length]; if (!calculatedGeysers.ContainsKey(subworld)) { calculatedGeysers[subworld] = new Dictionary <string, int>(); } if (!calculatedGeysers[subworld].ContainsKey(data.Key)) { calculatedGeysers[subworld][data.Key] = 0; } calculatedGeysers[subworld][data.Key]++; } } Debug.Log("Calculated Geysers: "); foreach (KeyValuePair <string, Dictionary <string, int> > subworld in calculatedGeysers) { Debug.Log("Subworld: " + subworld.Key); foreach (KeyValuePair <string, int> geyser in subworld.Value) { Debug.Log("\t" + geyser.Key + " = " + geyser.Value); } } WorldGenReloadedData.CalculatedGeysers = calculatedGeysers; }
public void ResetMobs(SeededRandom rnd) { if (mobselection == Selection.WeightedBucket) { if (bucket == null) { bucket = new List <WeightedMob>(); for (int i = 0; i < mobs.Count; i++) { for (int j = 0; (float)j < mobs[i].weight; j++) { bucket.Add(new WeightedMob(mobs[i].tag, 1f)); } } } bucket.ShuffleSeeded(rnd.RandomSource()); mobIter = bucket.GetEnumerator(); } else { mobIter = mobs.GetEnumerator(); } }
public static List <Vector2> GetRandomPoints(Polygon boundingArea, float density, float avoidRadius, List <Vector2> avoidPoints, SampleBehaviour behaviour, bool testInsideBounds, SeededRandom rnd, bool doShuffle = true, bool testAvoidPoints = true) { float num = boundingArea.bounds.width; float num2 = boundingArea.bounds.height; float num3 = num / 2f; float num4 = num2 / 2f; int num5 = (int)Mathf.Floor(num * num2 / density); uint num6 = (uint)Mathf.Sqrt((float)num5); int pointsPerIteration = 10; uint num7 = (uint)((float)num5 * 0.98f); Vector2 min = boundingArea.bounds.min; Vector2 max = boundingArea.bounds.max; List <Vector2> list = new List <Vector2>(); switch (behaviour) { case SampleBehaviour.PoissonDisk: list = new UniformPoissonDiskSampler(rnd).SampleRectangle(min, max, density, pointsPerIteration); break; case SampleBehaviour.UniformSquare: for (float num25 = 0f - num4 + density; num25 < num4 - density; num25 += density) { for (float num26 = 0f - num3 + density; num26 < num3 - density; num26 += density) { list.Add(boundingArea.Centroid() + new Vector2(num26, num25)); } } break; case SampleBehaviour.UniformHex: for (uint num10 = 0u; num10 < num6; num10++) { for (uint num11 = 0u; num11 < num6; num11++) { list.Add(boundingArea.Centroid() + new Vector2(0f - num3 + (0.5f + (float)(double)num10) / (float)(double)num6 * num, 0f - num4 + (0.25f + 0.5f * (float)(double)(num10 % 2u) + (float)(double)num11) / (float)(double)num6 * num2)); } } break; case SampleBehaviour.UniformSpiral: for (uint num19 = 0u; num19 < num7; num19++) { double num20 = (double)num19 / (32.0 * (double)density * 8.0); double num21 = Math.Sqrt(num20 * 512.0 * (double)density); double num22 = Math.Sqrt(num20); double num23 = Math.Sin(num21) * num22; double num24 = Math.Cos(num21) * num22; list.Add(boundingArea.bounds.center + new Vector2((float)num23 * boundingArea.bounds.width, (float)num24 * boundingArea.bounds.height)); } break; case SampleBehaviour.UniformCircle: { float num12 = 6.28318548f * avoidRadius; float num13 = num12 / density; float num14 = rnd.RandomValue(); for (uint num15 = 1u; (float)(double)num15 < num13; num15++) { float num16 = num14 + (float)(double)num15 / num13 * 3.14159274f * 2f; double num17 = Math.Cos((double)num16) * (double)avoidRadius; double num18 = Math.Sin((double)num16) * (double)avoidRadius; list.Add(boundingArea.bounds.center + new Vector2((float)num17, (float)num18)); } break; } default: for (float num8 = 0f - num4 + avoidRadius * 0.3f + rnd.RandomValue() * 2f; num8 < num4 - (avoidRadius * 0.3f + rnd.RandomValue() * 2f); num8 += density + rnd.RandomValue()) { for (float num9 = 0f - num3 + avoidRadius * 0.3f + rnd.RandomValue() * 2f + rnd.RandomValue() * 2f; num9 < num3 - (avoidRadius * 0.3f + rnd.RandomValue() * 2f); num9 += density + rnd.RandomValue()) { list.Add(boundingArea.Centroid() + new Vector2(num9, num8 + rnd.RandomValue() - 0.5f)); } } break; } List <Vector2> list2 = new List <Vector2>(); for (int i = 0; i < list.Count; i++) { if (!testInsideBounds || boundingArea.Contains(list[i])) { bool flag = false; if (testAvoidPoints && avoidPoints != null) { for (int j = 0; j < avoidPoints.Count; j++) { if (Mathf.Abs((avoidPoints[j] - list[i]).magnitude) < avoidRadius) { flag = true; break; } } } if (!flag) { list2.Add(list[i]); } } } if (doShuffle) { list2.ShuffleSeeded(rnd.RandomSource()); } return(list2); }
public static void CalculateGeysers(SeededRandom rnd, WorldGen worldgen) { Dictionary <string, Dictionary <string, int> > calculatedGeysers = new Dictionary <string, Dictionary <string, int> >(); foreach (KeyValuePair <string, GeyserState> data in WorldGenReloadedData.GeyserConfig) { int geyserCount = rnd.RandomRange(data.Value.Minimum, data.Value.Maximum + 1); Debug.Log("geyserCount [" + data.Key + "] = " + geyserCount); if (geyserCount <= 0) { continue; } List <string> _subworlds = new List <string>(data.Value.SubWorlds); // Remove invalid subworlds for (int i = _subworlds.Count - 1; i >= 0; i--) { List <TerrainCell> terrainCellsForTag2 = WorldGen.GetTerrainCellsForTag(_subworlds[i]); for (int num = terrainCellsForTag2.Count - 1; num >= 0; num--) { if (!worldgen.IsSafeToSpawnPOI(terrainCellsForTag2[num])) { terrainCellsForTag2.RemoveAt(num); } } Debug.Log("Available cells [" + _subworlds[i] + "] = " + terrainCellsForTag2.Count); if (terrainCellsForTag2.Count <= 0 || terrainCellsForTag2.Count / geyserCount < 3) { Debug.Log("Invalid subworld: " + _subworlds[i]); _subworlds.RemoveAt(i); } } _subworlds.ShuffleSeeded(rnd.RandomSource()); string[] subworlds = _subworlds.ToArray(); for (int i = 0; i < geyserCount; i++) { string subworld = subworlds[i % subworlds.Length]; if (!calculatedGeysers.ContainsKey(subworld)) { calculatedGeysers[subworld] = new Dictionary <string, int>(); } if (!calculatedGeysers[subworld].ContainsKey(data.Key)) { calculatedGeysers[subworld][data.Key] = 0; } calculatedGeysers[subworld][data.Key]++; } } Debug.Log("Calculated Geysers: "); foreach (KeyValuePair <string, Dictionary <string, int> > subworld in calculatedGeysers) { Debug.Log("Subworld: " + subworld.Key); foreach (KeyValuePair <string, int> geyser in subworld.Value) { Debug.Log("\t" + geyser.Key + " = " + geyser.Value); } } WorldGenReloadedData.CalculatedGeysers = calculatedGeysers; }
private void ApplyBackground(WorldGen worldGen, Chunk world, SetValuesFunction SetValues, float temperatureMin, float temperatureRange, SeededRandom rnd) { LogInfo("Apply Background", node.type, 0f); float floatSetting = worldGen.Settings.GetFloatSetting("CaveOverrideMaxValue"); float floatSetting2 = worldGen.Settings.GetFloatSetting("CaveOverrideSliverValue"); Leaf leafForTerrainCell = worldGen.GetLeafForTerrainCell(this); bool flag = leafForTerrainCell.tags.Contains(WorldGenTags.IgnoreCaveOverride); bool flag2 = leafForTerrainCell.tags.Contains(WorldGenTags.CaveVoidSliver); bool flag3 = leafForTerrainCell.tags.Contains(WorldGenTags.ErodePointToCentroid); bool flag4 = leafForTerrainCell.tags.Contains(WorldGenTags.ErodePointToCentroidInv); bool flag5 = leafForTerrainCell.tags.Contains(WorldGenTags.ErodePointToEdge); bool flag6 = leafForTerrainCell.tags.Contains(WorldGenTags.ErodePointToEdgeInv); bool flag7 = leafForTerrainCell.tags.Contains(WorldGenTags.ErodePointToBorder); bool flag8 = leafForTerrainCell.tags.Contains(WorldGenTags.ErodePointToBorderInv); bool flag9 = leafForTerrainCell.tags.Contains(WorldGenTags.ErodePointToWorldTop); bool flag10 = leafForTerrainCell.tags.Contains(WorldGenTags.DistFunctionPointCentroid); bool flag11 = leafForTerrainCell.tags.Contains(WorldGenTags.DistFunctionPointEdge); Sim.DiseaseCell diseaseCell = default(Sim.DiseaseCell); diseaseCell.diseaseIdx = byte.MaxValue; if (node.tags.Contains(WorldGenTags.Infected)) { diseaseCell.diseaseIdx = (byte)rnd.RandomRange(0, WorldGen.diseaseIds.Count); node.tags.Add(new Tag("Infected:" + WorldGen.diseaseIds[diseaseCell.diseaseIdx])); diseaseCell.elementCount = rnd.RandomRange(10000, 1000000); } LogInfo("Getting Element Bands", node.type, 0f); ElementBandConfiguration elementBandConfiguration = worldGen.Settings.GetElementBandForBiome(node.type); if (elementBandConfiguration == null && node.biomeSpecificTags != null) { LogInfo("\tType is not a biome, checking tags", string.Empty, (float)node.tags.Count); List <ElementBandConfiguration> list = new List <ElementBandConfiguration>(); foreach (Tag biomeSpecificTag in node.biomeSpecificTags) { ElementBandConfiguration elementBandForBiome = worldGen.Settings.GetElementBandForBiome(biomeSpecificTag.Name); if (elementBandForBiome != null) { list.Add(elementBandForBiome); LogInfo("\tFound biome", biomeSpecificTag.Name, 0f); } } if (list.Count > 0) { int num = rnd.RandomSource().Next(list.Count); elementBandConfiguration = list[num]; LogInfo("\tPicked biome", string.Empty, (float)num); } } DebugUtil.Assert(elementBandConfiguration != null, "A node didn't get assigned a biome! ", node.type); foreach (Vector2I availableTerrainPoint in availableTerrainPoints) { Vector2I current2 = availableTerrainPoint; int num2 = Grid.XYToCell(current2.x, current2.y); if (!highPriorityClaims.Contains(num2)) { float num3 = world.overrides[num2]; if (!flag && num3 >= 100f) { if (num3 >= 300f) { SetValues(num2, WorldGen.voidElement, WorldGen.voidElement.defaultValues, Sim.DiseaseCell.Invalid); } else if (num3 >= 200f) { SetValues(num2, WorldGen.unobtaniumElement, WorldGen.unobtaniumElement.defaultValues, Sim.DiseaseCell.Invalid); } else { SetValues(num2, WorldGen.katairiteElement, WorldGen.katairiteElement.defaultValues, Sim.DiseaseCell.Invalid); } } else { float num4 = 1f; Vector2 vector = new Vector2((float)current2.x, (float)current2.y); if (flag3 || flag4) { float num5 = 15f; if (flag11) { float timeOnEdge = 0f; MathUtil.Pair <Vector2, Vector2> closestEdge = poly.GetClosestEdge(vector, ref timeOnEdge); Vector2 a = closestEdge.First + (closestEdge.Second - closestEdge.First) * timeOnEdge; num5 = Vector2.Distance(a, vector); } num4 = Vector2.Distance(poly.Centroid(), vector) / num5; num4 = Mathf.Max(0f, Mathf.Min(1f, num4)); if (flag4) { num4 = 1f - num4; } } if (flag6 || flag5) { float timeOnEdge2 = 0f; MathUtil.Pair <Vector2, Vector2> closestEdge2 = poly.GetClosestEdge(vector, ref timeOnEdge2); Vector2 a2 = closestEdge2.First + (closestEdge2.Second - closestEdge2.First) * timeOnEdge2; float num6 = 15f; if (flag10) { num6 = Vector2.Distance(poly.Centroid(), vector); } num4 = Vector2.Distance(a2, vector) / num6; num4 = Mathf.Max(0f, Mathf.Min(1f, num4)); if (flag6) { num4 = 1f - num4; } } if (flag8 || flag7) { List <Edge> edgesWithTag = worldGen.WorldLayout.overworldGraph.GetEdgesWithTag(WorldGenTags.EdgeClosed); float num7 = 3.40282347E+38f; foreach (Edge item in edgesWithTag) { MathUtil.Pair <Vector2, Vector2> segment = new MathUtil.Pair <Vector2, Vector2>(item.corner0.position, item.corner1.position); float closest_point = 0f; float a3 = Mathf.Abs(MathUtil.GetClosestPointBetweenPointAndLineSegment(segment, vector, ref closest_point)); num7 = Mathf.Min(a3, num7); } float num8 = 7f; if (flag10) { num8 = Vector2.Distance(poly.Centroid(), vector); } num4 = num7 / num8; num4 = Mathf.Max(0f, Mathf.Min(1f, num4)); if (flag8) { num4 = 1f - num4; } } if (flag9) { Vector2I worldSize = worldGen.WorldSize; int y = worldSize.y; float num9 = 38f; float num10 = 58f; float num11 = (float)y - vector.y; num4 = ((num11 < num9) ? 0f : ((!(num11 < num10)) ? 1f : Mathf.Clamp01((num11 - num9) / (num10 - num9)))); } worldGen.GetElementForBiomePoint(world, elementBandConfiguration, current2, out Element element, out Sim.PhysicsData pd, out Sim.DiseaseCell dc, num4); if (!element.IsVacuum && element.id != SimHashes.Katairite && element.id != SimHashes.Unobtanium) { if (element.lowTempTransition != null && temperatureMin < element.lowTemp) { temperatureMin = element.lowTemp + 20f; } pd.temperature = temperatureMin + world.heatOffset[num2] * temperatureRange; } if (element.IsSolid && !flag && num3 > floatSetting && num3 < 100f) { element = ((!flag2 || !(num3 > floatSetting2)) ? WorldGen.vacuumElement : WorldGen.voidElement); pd = element.defaultValues; } if (dc.diseaseIdx == 255) { dc = diseaseCell; } SetValues(num2, element, pd, dc); } } } if (node.tags.Contains(WorldGenTags.SprinkleOfOxyRock)) { HandleSprinkleOfElement(worldGen.Settings, WorldGenTags.SprinkleOfOxyRock, world, SetValues, temperatureMin, temperatureRange, rnd); } if (node.tags.Contains(WorldGenTags.SprinkleOfMetal)) { HandleSprinkleOfElement(worldGen.Settings, WorldGenTags.SprinkleOfMetal, world, SetValues, temperatureMin, temperatureRange, rnd); } }
public static Dictionary <int, string> PlaceBiomeAmbientMobs(WorldGenSettings settings, TerrainCell tc, SeededRandom rnd, Sim.Cell[] cells, float[] bgTemp, Sim.DiseaseCell[] dc, HashSet <int> avoidCells, bool isDebug) { Dictionary <int, string> spawnedMobs = new Dictionary <int, string>(); Node node = tc.node; HashSet <int> alreadyOccupiedCells = new HashSet <int>(); List <Tag> list = new List <Tag>(); if (node.biomeSpecificTags == null) { tc.LogInfo("PlaceBiomeAmbientMobs", "No tags", (float)node.node.Id); return(null); } foreach (Tag biomeSpecificTag in node.biomeSpecificTags) { if (settings.HasMob(biomeSpecificTag.Name) && settings.GetMob(biomeSpecificTag.Name) != null) { list.Add(biomeSpecificTag); } } if (list.Count <= 0) { tc.LogInfo("PlaceBiomeAmbientMobs", "No biome MOBS", (float)node.node.Id); return(null); } List <int> list2 = (!node.tags.Contains(WorldGenTags.PreventAmbientMobsInFeature)) ? tc.GetAvailableSpawnCellsAll() : tc.GetAvailableSpawnCellsBiome(); tc.LogInfo("PlaceBiomAmbientMobs", "possibleSpawnPoints", (float)list2.Count); for (int num = list2.Count - 1; num > 0; num--) { int num2 = list2[num]; if (ElementLoader.elements[cells[num2].elementIdx].id == SimHashes.Katairite || ElementLoader.elements[cells[num2].elementIdx].id == SimHashes.Unobtanium || avoidCells.Contains(num2)) { list2.RemoveAt(num); } } tc.LogInfo("mob spawns", "Id:" + node.node.Id + " possible cells", (float)list2.Count); if (list2.Count == 0) { if (isDebug) { Debug.LogWarning("No where to put mobs possibleSpawnPoints [" + tc.node.node.Id + "]"); } return(null); } list.ShuffleSeeded(rnd.RandomSource()); for (int i = 0; i < list.Count; i++) { Mob mob = settings.GetMob(list[i].Name); if (mob == null) { Debug.LogError("Missing sample description for tag [" + list[i].Name + "]"); } else { List <int> mobPossibleSpawnPoints = GetMobPossibleSpawnPoints(mob, list2, cells, alreadyOccupiedCells, rnd); if (mobPossibleSpawnPoints.Count == 0) { if (!isDebug) { continue; } } else { tc.LogInfo("\t\tpossible", list[i].ToString() + " mps: " + mobPossibleSpawnPoints.Count + " ps:", (float)list2.Count); float num3 = mob.density.GetRandomValueWithinRange(rnd) * MobSettings.AmbientMobDensity; if (num3 > 1f) { if (isDebug) { Debug.LogWarning("Got a mob density greater than 1.0 for " + list[i].Name + ". Probably using density as spacing!"); } num3 = 1f; } tc.LogInfo("\t\tdensity:", string.Empty, num3); int num4 = Mathf.RoundToInt((float)mobPossibleSpawnPoints.Count * num3); tc.LogInfo("\t\tcount", list[i].ToString(), (float)num4); Tag mobPrefab = (mob.prefabName != null) ? new Tag(mob.prefabName) : list[i]; SpawnCountMobs(mob, mobPrefab, num4, mobPossibleSpawnPoints, tc, ref spawnedMobs, ref alreadyOccupiedCells); } } } return(spawnedMobs); }