Example #1
0
        private void HandleSprinkleOfElement(WorldGenSettings settings, Tag targetTag, Chunk world, SetValuesFunction SetValues, float temperatureMin, float temperatureRange, SeededRandom rnd)
        {
            FeatureSettings feature  = settings.GetFeature(targetTag.Name);
            string          element  = feature.GetOneWeightedSimHash("SprinkleOfElementChoices", rnd).element;
            Element         element2 = ElementLoader.FindElementByName(element);

            ProcGen.Room value = null;
            SettingsCache.rooms.TryGetValue(targetTag.Name, out value);
            SampleDescriber sampleDescriber = value;

            Sim.PhysicsData defaultValues = element2.defaultValues;
            Sim.DiseaseCell invalid       = Sim.DiseaseCell.Invalid;
            for (int i = 0; i < terrainPositions.Count; i++)
            {
                if (!(terrainPositions[i].Value != targetTag))
                {
                    float           radius       = rnd.RandomRange(sampleDescriber.blobSize.min, sampleDescriber.blobSize.max);
                    Vector2         center       = Grid.CellToPos2D(terrainPositions[i].Key);
                    List <Vector2I> filledCircle = ProcGen.Util.GetFilledCircle(center, radius);
                    for (int j = 0; j < filledCircle.Count; j++)
                    {
                        Vector2I vector2I  = filledCircle[j];
                        int      x         = vector2I.x;
                        Vector2I vector2I2 = filledCircle[j];
                        int      num       = Grid.XYToCell(x, vector2I2.y);
                        if (Grid.IsValidCell(num))
                        {
                            defaultValues.mass        = GetDensityMassForCell(world, num, element2.defaultValues.mass);
                            defaultValues.temperature = temperatureMin + world.heatOffset[num] * temperatureRange;
                            SetValues(num, element2, defaultValues, invalid);
                        }
                    }
                }
            }
        }
Example #2
0
        public void ConvertToMap(Chunk world, TerrainCell.SetValuesFunction SetValues, float temperatureMin, float temperatureRange, SeededRandom rnd)
        {
            Element element = ElementLoader.FindElementByName(base.backgroundElement);

            Sim.PhysicsData defaultValues = element.defaultValues;
            Element         element2      = ElementLoader.FindElementByName(base.element);

            Sim.PhysicsData defaultValues2 = element2.defaultValues;
            defaultValues2.temperature = base.temperature;
            Sim.DiseaseCell invalid = Sim.DiseaseCell.Invalid;
            for (int i = 0; i < pathElements.Count; i++)
            {
                Segment         segment    = pathElements[i];
                Vector2         vector     = segment.e1 - segment.e0;
                Vector2         normalized = new Vector2(0f - vector.y, vector.x).normalized;
                List <Vector2I> line       = ProcGen.Util.GetLine(segment.e0, segment.e1);
                for (int j = 0; j < line.Count; j++)
                {
                    for (float num = 0.5f; num <= base.widthCenter; num += 1f)
                    {
                        Vector2 vector2 = (Vector2)line[j] + normalized * num;
                        int     num2    = Grid.XYToCell((int)vector2.x, (int)vector2.y);
                        if (Grid.IsValidCell(num2))
                        {
                            SetValues(num2, element2, defaultValues2, invalid);
                        }
                        Vector2 vector3 = (Vector2)line[j] - normalized * num;
                        num2 = Grid.XYToCell((int)vector3.x, (int)vector3.y);
                        if (Grid.IsValidCell(num2))
                        {
                            SetValues(num2, element2, defaultValues2, invalid);
                        }
                    }
                    for (float num3 = 0.5f; num3 <= base.widthBorder; num3 += 1f)
                    {
                        Vector2 vector4 = (Vector2)line[j] + normalized * (base.widthCenter + num3);
                        int     num4    = Grid.XYToCell((int)vector4.x, (int)vector4.y);
                        if (Grid.IsValidCell(num4))
                        {
                            defaultValues.temperature = temperatureMin + world.heatOffset[num4] * temperatureRange;
                            SetValues(num4, element, defaultValues, invalid);
                        }
                        Vector2 vector5 = (Vector2)line[j] - normalized * (base.widthCenter + num3);
                        num4 = Grid.XYToCell((int)vector5.x, (int)vector5.y);
                        if (Grid.IsValidCell(num4))
                        {
                            defaultValues.temperature = temperatureMin + world.heatOffset[num4] * temperatureRange;
                            SetValues(num4, element, defaultValues, invalid);
                        }
                    }
                }
            }
        }
Example #3
0
 public void ConvertToMap(Chunk world, TerrainCell.SetValuesFunction SetValues, float temperatureMin, float temperatureRange, SeededRandom rnd)
 {
     Sim.DiseaseCell invalid = Sim.DiseaseCell.Invalid;
     for (int i = 0; i < pathElements.Count; i++)
     {
         Segment         segment    = pathElements[i];
         Vector2         e          = segment.e1;
         Segment         segment2   = pathElements[i];
         Vector2         vector     = e - segment2.e0;
         Vector2         normalized = new Vector2(0f - vector.y, vector.x).normalized;
         Segment         segment3   = pathElements[i];
         Vector2         e2         = segment3.e0;
         Segment         segment4   = pathElements[i];
         List <Vector2I> line       = ProcGen.Util.GetLine(e2, segment4.e1);
         for (int j = 0; j < line.Count; j++)
         {
             Vector2I vector2I  = line[j];
             int      x         = vector2I.x;
             Vector2I vector2I2 = line[j];
             int      num       = Grid.XYToCell(x, vector2I2.y);
             if (Grid.IsValidCell(num))
             {
                 Element         element       = ElementLoader.FindElementByName(WeightedRandom.Choose(this.element, rnd).element);
                 Sim.PhysicsData defaultValues = element.defaultValues;
                 defaultValues.temperature = temperatureMin + world.heatOffset[num] * temperatureRange;
                 SetValues(num, element, defaultValues, invalid);
             }
             for (float num2 = 0.5f; num2 <= width; num2 += 1f)
             {
                 Vector2 vector2 = (Vector2)line[j] + normalized * num2;
                 num = Grid.XYToCell((int)vector2.x, (int)vector2.y);
                 if (Grid.IsValidCell(num))
                 {
                     Element         element2       = ElementLoader.FindElementByName(WeightedRandom.Choose(this.element, rnd).element);
                     Sim.PhysicsData defaultValues2 = element2.defaultValues;
                     defaultValues2.temperature = temperatureMin + world.heatOffset[num] * temperatureRange;
                     SetValues(num, element2, defaultValues2, invalid);
                 }
                 Vector2 vector3 = (Vector2)line[j] - normalized * num2;
                 num = Grid.XYToCell((int)vector3.x, (int)vector3.y);
                 if (Grid.IsValidCell(num))
                 {
                     Element         element3       = ElementLoader.FindElementByName(WeightedRandom.Choose(this.element, rnd).element);
                     Sim.PhysicsData defaultValues3 = element3.defaultValues;
                     defaultValues3.temperature = temperatureMin + world.heatOffset[num] * temperatureRange;
                     SetValues(num, element3, defaultValues3, invalid);
                 }
             }
         }
     }
 }
Example #4
0
        public unsafe static bool DoSettleSim(WorldGenSettings settings, Sim.Cell[] cells, float[] bgTemp, Sim.DiseaseCell[] dcs, WorldGen.OfflineCallbackFunction updateProgressFn, Data data, List <KeyValuePair <Vector2I, TemplateContainer> > templateSpawnTargets, Action <OfflineWorldGen.ErrorInfo> error_cb, Action <Sim.Cell[], float[], Sim.DiseaseCell[]> onSettleComplete)
        {
            Sim.SIM_Initialize(Sim.DLL_MessageHandler);
            SimMessages.CreateSimElementsTable(ElementLoader.elements);
            SimMessages.CreateWorldGenHACKDiseaseTable(WorldGen.diseaseIds);
            Sim.DiseaseCell[] dc = new Sim.DiseaseCell[dcs.Length];
            SimMessages.SimDataInitializeFromCells(Grid.WidthInCells, Grid.HeightInCells, cells, bgTemp, dc);
            int num = 500;

            updateProgressFn(UI.WORLDGEN.SETTLESIM.key, 0f, WorldGenProgressStages.Stages.SettleSim);
            Vector2I min = new Vector2I(0, 0);
            Vector2I max = new Vector2I(Grid.WidthInCells, Grid.HeightInCells);

            byte[] bytes = null;
            using (MemoryStream memoryStream = new MemoryStream())
            {
                using (BinaryWriter writer = new BinaryWriter(memoryStream))
                {
                    try
                    {
                        Sim.Save(writer);
                    }
                    catch (Exception ex)
                    {
                        string message    = ex.Message;
                        string stackTrace = ex.StackTrace;
                        WorldGenLogger.LogException(message, stackTrace);
                        return(updateProgressFn(new StringKey("Exception in Sim Save"), -1f, WorldGenProgressStages.Stages.Failure));
                    }
                }
                bytes = memoryStream.ToArray();
            }
            FastReader reader = new FastReader(bytes);

            if (Sim.Load(reader) != 0)
            {
                updateProgressFn(UI.WORLDGEN.FAILED.key, -1f, WorldGenProgressStages.Stages.Failure);
                return(true);
            }
            byte[] array = new byte[Grid.CellCount];
            for (int i = 0; i < Grid.CellCount; i++)
            {
                array[i] = byte.MaxValue;
            }
            for (int j = 0; j < num; j++)
            {
                SimMessages.NewGameFrame(0.2f, min, max);
                IntPtr intPtr = Sim.HandleMessage(SimMessageHashes.PrepareGameData, array.Length, array);
                updateProgressFn(UI.WORLDGEN.SETTLESIM.key, (float)j / (float)num * 100f, WorldGenProgressStages.Stages.SettleSim);
                if (!(intPtr == IntPtr.Zero))
                {
                    Sim.GameDataUpdate *ptr = (Sim.GameDataUpdate *)(void *) intPtr;
                    for (int k = 0; k < ptr->numSubstanceChangeInfo; k++)
                    {
                        Sim.SubstanceChangeInfo substanceChangeInfo = ptr->substanceChangeInfo[k];
                        int cellIdx = substanceChangeInfo.cellIdx;
                        cells[cellIdx].elementIdx   = ptr->elementIdx[cellIdx];
                        cells[cellIdx].insulation   = ptr->insulation[cellIdx];
                        cells[cellIdx].properties   = ptr->properties[cellIdx];
                        cells[cellIdx].temperature  = ptr->temperature[cellIdx];
                        cells[cellIdx].mass         = ptr->mass[cellIdx];
                        cells[cellIdx].strengthInfo = ptr->strengthInfo[cellIdx];
                    }
                    foreach (KeyValuePair <Vector2I, TemplateContainer> templateSpawnTarget in templateSpawnTargets)
                    {
                        Cell templateCellData;
                        for (int l = 0; l < templateSpawnTarget.Value.cells.Count; l++)
                        {
                            templateCellData = templateSpawnTarget.Value.cells[l];
                            Vector2I key  = templateSpawnTarget.Key;
                            int      x    = key.x;
                            Vector2I key2 = templateSpawnTarget.Key;
                            int      num2 = Grid.OffsetCell(Grid.XYToCell(x, key2.y), templateCellData.location_x, templateCellData.location_y);
                            if (Grid.IsValidCell(num2))
                            {
                                cells[num2].elementIdx  = (byte)ElementLoader.GetElementIndex(templateCellData.element);
                                cells[num2].temperature = templateCellData.temperature;
                                cells[num2].mass        = templateCellData.mass;
                                dcs[num2].diseaseIdx    = (byte)WorldGen.diseaseIds.FindIndex((string name) => name == templateCellData.diseaseName);
                                dcs[num2].elementCount  = templateCellData.diseaseCount;
                            }
                        }
                    }
                }
            }
            for (int m = 0; m < Grid.CellCount; m++)
            {
                int callbackIdx = (m != Grid.CellCount - 1) ? (-1) : 2147481337;
                SimMessages.ModifyCell(m, cells[m].elementIdx, cells[m].temperature, cells[m].mass, dcs[m].diseaseIdx, dcs[m].elementCount, SimMessages.ReplaceType.Replace, false, callbackIdx);
            }
            bool flag = false;

            while (!flag)
            {
                SimMessages.NewGameFrame(0.2f, min, max);
                IntPtr intPtr2 = Sim.HandleMessage(SimMessageHashes.PrepareGameData, array.Length, array);
                if (!(intPtr2 == IntPtr.Zero))
                {
                    Sim.GameDataUpdate *ptr2 = (Sim.GameDataUpdate *)(void *) intPtr2;
                    for (int n = 0; n < ptr2->numCallbackInfo; n++)
                    {
                        Sim.CallbackInfo callbackInfo = ptr2->callbackInfo[n];
                        if (callbackInfo.callbackIdx == 2147481337)
                        {
                            flag = true;
                            break;
                        }
                    }
                }
            }
            Sim.HandleMessage(SimMessageHashes.SettleWorldGen, 0, null);
            bool result = SaveSim(settings, data, error_cb);

            onSettleComplete(cells, bgTemp, dcs);
            Sim.Shutdown();
            return(result);
        }
Example #5
0
        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);
            }
        }