Пример #1
0
        public static void Import(BlueprintData __instance, BinaryReader r)
        {
            if (r.BaseStream.Position != r.BaseStream.Length)
            {
                byte version = r.ReadByte();
                if (version >= 1)
                {
                    int len = r.ReadInt32();
                    __instance.reforms = new ReformData[len];
                    for (int i = 0; i < len; i++)
                    {
                        __instance.reforms[i] = new ReformData();
                        ReformData data = __instance.reforms[i];
                        data.Import(r);
                    }
                }

                if (version >= 2 && r.ReadBoolean())
                {
                    __instance.customColors = new Color[16];
                    for (int i = 0; i < 16; i++)
                    {
                        __instance.customColors[i] = new Color(
                            r.ReadSingle(),
                            r.ReadSingle(),
                            r.ReadSingle(),
                            r.ReadSingle());
                    }
                }
            }
        }
Пример #2
0
        public void GetData(out List <ReformData> reforms, out Color[] colors)
        {
            reforms = new List <ReformData>();
            colors  = null;

            using (IReaderProvider reader = NebulaModAPI.GetBinaryReader(ReformData))
            {
                int count = reader.BinaryReader.ReadInt32();
                for (int i = 0; i < count; i++)
                {
                    ReformData data = new ReformData
                    {
                        latitude  = reader.BinaryReader.ReadSingle(),
                        longitude = reader.BinaryReader.ReadSingle(),
                        type      = reader.BinaryReader.ReadInt32(),
                        color     = reader.BinaryReader.ReadInt32()
                    };
                    reforms.Add(data);
                }

                if (reader.BinaryReader.ReadBoolean())
                {
                    colors = new Color[16];
                    for (int i = 0; i < 16; i++)
                    {
                        colors[i] = new Color(
                            reader.BinaryReader.ReadSingle(),
                            reader.BinaryReader.ReadSingle(),
                            reader.BinaryReader.ReadSingle(),
                            reader.BinaryReader.ReadSingle());
                    }
                }
            }
        }
Пример #3
0
 public static void Import(this ReformData data, BinaryReader r)
 {
     r.ReadByte();
     data.areaIndex      = r.ReadInt32();
     data.type           = r.ReadInt32();
     data.color          = r.ReadInt32();
     data.localLatitude  = r.ReadSingle();
     data.localLongitude = r.ReadSingle();
 }
Пример #4
0
 public static void Export(this ReformData data, BinaryWriter w)
 {
     w.Write((byte)0);
     w.Write(data.areaIndex);
     w.Write(data.type);
     w.Write(data.color);
     w.Write(data.localLatitude);
     w.Write(data.localLongitude);
 }
Пример #5
0
        public static void RefreshReformPreviews(BlueprintData _blueprintData, PlanetData _planet, int _dotsCursor, IntVector4[] _tropicGratBoxConditionInfo,
                                                 float _yaw, int _segmentCnt, Vector4[] array)
        {
            if (!BlueprintCopyExtension.isEnabled)
            {
                return;
            }
            if (UndoManager.IgnoreAllEvents.Value)
            {
                return;
            }

            int   yawCount           = Mathf.FloorToInt(_yaw / 90f);
            float yawX               = yawCount == 1 || yawCount == 2 ? -1f : 1f;
            float yawY               = yawCount == 2 || yawCount == 3 ? -1f : 1f;
            float latitudeRadPerGrid = BlueprintUtils.GetLatitudeRadPerGrid(_segmentCnt);

            List <ReformData> reforms = BlueprintPasteExtension.reformPreviews;
            int reformsLength         = _blueprintData.reforms.Length;

            for (int i = 0; i < reformsLength; i++)
            {
                ReformData reformData = _blueprintData.reforms[i];
                for (int j = 0; j < _dotsCursor; j++)
                {
                    ReformData reformPreview = reforms[reformsLength * j + i];
                    Vector4    areaData      = array[j + reformData.areaIndex];

                    BlueprintUtilsPatch2.MirrorArea(ref areaData, yawX, yawY);

                    float   radPerGrid    = BlueprintUtils.GetLongitudeRadPerGrid(areaData.y, _segmentCnt);
                    Vector2 vector4       = BlueprintUtils.TransitionWidthAndHeight(_yaw, reformData.localLongitude - 0.5f, reformData.localLatitude - 0.5f);
                    float   longitudeRad  = areaData.x + vector4.x * radPerGrid * yawX;
                    float   finalLatitude = areaData.y + vector4.y * latitudeRadPerGrid * yawY;
                    finalLatitude = Math.Abs(finalLatitude) > 1.5707964f ? 1.5707964f * Math.Sign(finalLatitude) : finalLatitude;

                    reformPreview.latitude  = finalLatitude;
                    reformPreview.longitude = longitudeRad;
                }
            }
        }
Пример #6
0
        static IEnumerable <CodeInstruction> AddMoreData(IEnumerable <CodeInstruction> instructions, ILGenerator generator)
        {
            CodeMatcher matcher = new CodeMatcher(instructions, generator)
                                  .MatchForward(true,
                                                new CodeMatch(OpCodes.Ldarg_S),
                                                new CodeMatch(OpCodes.Ldc_I4_0)
                                                ).Advance(3);

            Label contLabel = (Label)matcher.Operand;

            //allow generation when there is no buildings
            matcher.Advance(-3)
            .SetAndAdvance(OpCodes.Pop, null)
            .InsertAndAdvance(Transpilers.EmitDelegate <Func <bool> >(() => BlueprintCopyExtension.isEnabled && BlueprintCopyExtension.reformSelection.Count > 0))
            .InsertAndAdvance(new CodeInstruction(OpCodes.Brtrue, contLabel))
            .InsertAndAdvance(new CodeInstruction(OpCodes.Ldarg_S, 4))
            .InsertAndAdvance(new CodeInstruction(OpCodes.Ldc_I4_0));


            //Replace function call to include foundations
            matcher.MatchForward(false,
                                 new CodeMatch(OpCodes.Call, AccessTools.Method(typeof(BlueprintUtils), nameof(BlueprintUtils.GetBoundingRange))))
            .SetInstruction(Transpilers.EmitDelegate <Func <PlanetData, PlanetAuxData, int[], int, float, BPGratBox> >((data, auxData, arg3, i, f) =>
            {
                BlueprintCopyExtension.CopyReforms();
                return(ReformBPUtils.GetBoundingRange(data, auxData, arg3, i, BlueprintCopyExtension.tmpReformList, f));
            }));

            //Add initialization
            matcher.MatchForward(false,
                                 new CodeMatch(OpCodes.Ldarg_0),
                                 new CodeMatch(OpCodes.Ldloc_S),
                                 new CodeMatch(OpCodes.Newarr),
                                 new CodeMatch(OpCodes.Stfld)
                                 ).Advance(1)
            .InsertAndAdvance(Transpilers.EmitDelegate <Action <BlueprintData> >(data =>
            {
                if (BlueprintCopyExtension.isEnabled && !UndoManager.IgnoreAllEvents.Value)
                {
                    data.reforms = new ReformData[BlueprintCopyExtension.reformSelection.Count];

                    int i = 0;
                    foreach (var kv in BlueprintCopyExtension.reformSelection)
                    {
                        data.reforms[i]           = kv.Value;
                        data.reforms[i].areaIndex = -1;
                        i++;
                    }
                }
                else
                {
                    data.reforms = new ReformData[0];
                }
            }))
            .InsertAndAdvance(new CodeInstruction(OpCodes.Ldarg_0));

            //Just to anchor
            matcher.MatchForward(true,
                                 new CodeMatch(OpCodes.Call,
                                               AccessTools.Method(typeof(BlueprintUtils), nameof(BlueprintUtils.GetLongitudeSegmentCount), new[] { typeof(Vector3), typeof(int) })),
                                 new CodeMatch(OpCodes.Stloc_S),
                                 new CodeMatch(OpCodes.Ldloc_S),
                                 new CodeMatch(OpCodes.Ldloc_S),
                                 new CodeMatch(OpCodes.Call,
                                               AccessTools.Method(typeof(BlueprintUtils), nameof(BlueprintUtils.GetLongitudeRadPerGrid), new[] { typeof(int), typeof(int) })));

            // add my code
            matcher.MatchForward(false,
                                 new CodeMatch(OpCodes.Ldc_I4_0),
                                 new CodeMatch(OpCodes.Stloc_S))
            .Advance(1)
            .InsertAndAdvance(new CodeInstruction(OpCodes.Ldarg_0))
            .InsertAndAdvance(new CodeInstruction(OpCodes.Ldloc_S, 18))
            .InsertAndAdvance(new CodeInstruction(OpCodes.Ldloc_S, 17))
            .InsertAndAdvance(new CodeInstruction(OpCodes.Ldloc_S, 7))
            .InsertAndAdvance(new CodeInstruction(OpCodes.Ldloc_S, 20))
            .InsertAndAdvance(new CodeInstruction(OpCodes.Ldloc_S, 11))
            .InsertAndAdvance(new CodeInstruction(OpCodes.Ldloc_S, 19))
            .InsertAndAdvance(Transpilers.EmitDelegate <AddAction>(
                                  (blueprint, bpgratBox, i, array, longitudeRadPerGrid, latitudeRadPerGrid, longitudeSegmentCount) =>
            {
                if (!BlueprintCopyExtension.isEnabled)
                {
                    return;
                }
                if (UndoManager.IgnoreAllEvents.Value)
                {
                    return;
                }

                for (int j = 0; j < BlueprintCopyExtension.reformSelection.Count; j++)
                {
                    if (blueprint.reforms[j].areaIndex >= 0)
                    {
                        continue;
                    }

                    ReformData data = blueprint.reforms[j];

                    if (!(bpgratBox.y - 1E-05f <= data.latitude) || !(data.latitude <= bpgratBox.w + 1E-05f))
                    {
                        continue;
                    }

                    blueprint.reforms[j].areaIndex      = i;
                    blueprint.reforms[j].localLongitude = (data.longitude - array[i].x) / longitudeRadPerGrid;
                    blueprint.reforms[j].localLatitude  = (data.latitude - array[i].y) / latitudeRadPerGrid;

                    if (blueprint.reforms[j].localLongitude < -0.5001f)
                    {
                        blueprint.reforms[j].localLongitude += longitudeSegmentCount * 5;
                    }
                }
            }));

            //Add null check to buildings iteration
            matcher.MatchForward(true,
                                 new CodeMatch(OpCodes.Ldc_I4_0),
                                 new CodeMatch(OpCodes.Newarr),
                                 new CodeMatch(OpCodes.Stfld))
            .Advance(8)
            .CreateLabel(out Label exitLabel)
            .Advance(-2)
            .InsertAndAdvance(new CodeInstruction(OpCodes.Pop))
            .InsertAndAdvance(new CodeInstruction(OpCodes.Ldarg_2))
            .InsertAndAdvance(new CodeInstruction(OpCodes.Brfalse, exitLabel))
            .InsertAndAdvance(new CodeInstruction(OpCodes.Ldloc_S, 21));


            return(matcher.InstructionEnumeration());
        }
Пример #7
0
        public static void ItterateOnReform(BuildTool_BlueprintCopy __instance, BPGratBox box, Action <int, ReformData> action, bool fullCircle = false)
        {
            if (Mathf.Abs(box.y - box.w) < 0.005f)
            {
                return;
            }

            PlatformSystem platform = __instance.factory.platformSystem;

            if (platform == null)
            {
                return;
            }
            platform.EnsureReformData();

            GetSegmentCount(box.startLatitudeRad, box.startLongitudeRad, out float startLatCount, out float startLongCount, out int startCount);
            GetSegmentCount(box.endLatitudeRad, box.endLongitudeRad, out float endLatCount, out float endLongCount, out int endCount);

            startLatCount  = GridSnappingPatches.Snap(startLatCount);
            startLongCount = GridSnappingPatches.Snap(startLongCount);
            endLatCount    = GridSnappingPatches.Snap(endLatCount);
            endLongCount   = GridSnappingPatches.Snap(endLongCount);

            startLatCount  = Mathf.Round(startLatCount * 10f);
            endLatCount    = Mathf.Round(endLatCount * 10f);
            startLongCount = Mathf.Round(startLongCount * 10f);
            endLongCount   = Mathf.Round(endLongCount * 10f);

            float latDelta = endLatCount - startLatCount;
            int   segmentCount;

            float longDelta;

            if (startCount != endCount)
            {
                Vector2 center = GetSphericalCenter(box.startLatitudeRad, box.startLongitudeRad, box.endLatitudeRad, box.endLongitudeRad);

                GetSegmentCount(center.x, center.y, out float _, out float _, out int midCount);
                segmentCount = midCount;
                if (startCount == midCount)
                {
                    GetSegmentCount(box.startLatitudeRad, box.endLongitudeRad, out float _, out float nlongCount);
                    nlongCount = Mathf.Round(nlongCount * 10f);
                    longDelta  = nlongCount - startLongCount;
                }
                else
                {
                    GetSegmentCount(box.endLatitudeRad, box.startLongitudeRad, out float _, out float nlongCount);
                    nlongCount     = Mathf.Round(nlongCount * 10f);
                    longDelta      = endLongCount - nlongCount;
                    startLongCount = nlongCount;
                }
            }
            else
            {
                segmentCount = startCount;
                longDelta    = endLongCount - startLongCount;
            }

            if (fullCircle)
            {
                longDelta = segmentCount * 10;
            }

            if (longDelta < 0)
            {
                longDelta = segmentCount * 10 + longDelta;
            }

            int latSize  = Mathf.RoundToInt(latDelta) / 2;
            int longSize = Mathf.RoundToInt(longDelta) / 2;

            if (latSize == 0)
            {
                latSize = 1;
            }
            if (longSize == 0)
            {
                longSize = 1;
            }

            startLatCount  += 1;
            startLongCount += 1;

            int latOffset   = 0;
            int longOffset  = 0;
            int longCounter = 0;

            float latCount = platform.latitudeCount / 10f;

            for (int i = 0; i < longSize * latSize; i++)
            {
                longCounter++;
                float currentLat  = (startLatCount + latOffset) / 10f;
                float currentLong = (startLongCount + longOffset) / 10f;

                currentLong = Mathf.Repeat(currentLong, segmentCount);

                float latRad  = (currentLat + 0.1f) / currentGrid.segment * 6.2831855f;
                float longRad = (currentLong + 0.1f) / segmentCount * 6.2831855f;

                longOffset += 2;
                if (longCounter % longSize == 0)
                {
                    longOffset = 0;
                    latOffset += 2;
                }

                if (currentLat >= latCount || currentLat <= -latCount)
                {
                    continue;
                }

                int reformIndex = platform.GetReformIndexForSegment(currentLat, currentLong);

                int reformType  = platform.GetReformType(reformIndex);
                int reformColor = platform.GetReformColor(reformIndex);

                if (!platform.IsTerrainReformed(reformType))
                {
                    continue;
                }

                ReformData reform = new ReformData
                {
                    latitude  = latRad,
                    longitude = longRad,
                    type      = reformType,
                    color     = reformColor
                };

                action(reformIndex, reform);
            }
        }