Esempio n. 1
0
 internal static bool DebugToggle(out int count)
 {
     count = 0;
     if (EnvMan.instance != null)
     {
         debugTerrain = !debugTerrain;
         foreach (TerrainModifier mod in TerrainModifier.GetAllInstances().Where(x => x != null && x.m_playerModifiction))
         {
             Light light = SetupLight(mod);
             count++;
         }
         EnvMan.instance.m_dirLight.enabled = !debugTerrain;
         foreach (Heightmap map in Heightmap.GetAllHeightmaps().Where(x => x != null))
         {
             Material mat = m_materialInstance(map);
             if (mat != null)
             {
                 if (debugTerrain)
                 {
                     mat.SetTexture("_DiffuseTex0", Texture2D.whiteTexture);
                 }
                 else
                 {
                     mat.SetTexture("_DiffuseTex0", map.m_material.GetTexture("_DiffuseTex0"));
                 }
             }
         }
         return(true);
     }
     else
     {
         return(false);
     }
 }
Esempio n. 2
0
    // Token: 0x06000D83 RID: 3459 RVA: 0x0006014C File Offset: 0x0005E34C
    private void ApplyModifiers()
    {
        List <TerrainModifier> allInstances = TerrainModifier.GetAllInstances();

        float[] array     = null;
        float[] levelOnly = null;
        foreach (TerrainModifier terrainModifier in allInstances)
        {
            if (terrainModifier.enabled && this.TerrainVSModifier(terrainModifier))
            {
                if (terrainModifier.m_playerModifiction && array == null)
                {
                    array     = this.m_heights.ToArray();
                    levelOnly = this.m_heights.ToArray();
                }
                this.ApplyModifier(terrainModifier, array, levelOnly);
            }
        }
        this.m_clearedMask.Apply();
    }
Esempio n. 3
0
    private void ApplyModifiers()
    {
        List <TerrainModifier> allInstances = TerrainModifier.GetAllInstances();

        float[] baseHeights = (float[])null;
        float[] levelOnly   = (float[])null;
        foreach (TerrainModifier modifier in allInstances)
        {
            if (modifier.enabled && this.TerrainVSModifier(modifier))
            {
                if (modifier.m_playerModifiction && baseHeights == null)
                {
                    baseHeights = this.m_heights.ToArray();
                    levelOnly   = this.m_heights.ToArray();
                }
                this.ApplyModifier(modifier, baseHeights, levelOnly);
            }
        }
        this.m_clearedMask.Apply();
    }
Esempio n. 4
0
 public static void Reset(Vector3 centerLocation, float radius)
 {
     radius = Mathf.Clamp(radius, 2f, 50f);
     try
     {
         foreach (TerrainModifier terrainModifier in TerrainModifier.GetAllInstances())
         {
             if (terrainModifier != null && Utils.DistanceXZ(Player.m_localPlayer.transform.position, terrainModifier.transform.position) < radius)
             {
                 ZNetView component = terrainModifier.GetComponent <ZNetView>();
                 if (component != null && component.IsValid())
                 {
                     component.ClaimOwnership();
                     component.Destroy();
                 }
             }
         }
     }
     catch (Exception)
     {
     }
 }
Esempio n. 5
0
        private static int ResetTerrain(Vector3 center, float radius)
        {
            int resets            = 0;
            List <Heightmap> list = new List <Heightmap>();


            Heightmap.FindHeightmap(center, radius + 100, list);


            List <TerrainModifier> allInstances = TerrainModifier.GetAllInstances();

            foreach (TerrainModifier terrainModifier in allInstances)
            {
                Vector3  position = terrainModifier.transform.position;
                ZNetView nview    = terrainModifier.GetComponent <ZNetView>();
                if (nview != null && nview.IsValid() && nview.IsOwner() && Utils.DistanceXZ(position, center) <= radius)
                {
                    //Dbgl($"TerrainModifier {position}, player {playerPos}, distance: {Utils.DistanceXZ(position, playerPos)}");
                    resets++;
                    foreach (Heightmap heightmap in list)
                    {
                        if (heightmap.TerrainVSModifier(terrainModifier))
                        {
                            heightmap.Poke(true);
                        }
                    }
                    nview.Destroy();
                }
            }
            Dbgl($"Reset {resets} mod edits");

            using (List <Heightmap> .Enumerator enumerator = list.GetEnumerator())
            {
                while (enumerator.MoveNext())
                {
                    TerrainComp terrainComp = TerrainComp.FindTerrainCompiler(enumerator.Current.transform.position);
                    if (!terrainComp)
                    {
                        continue;
                    }
                    Traverse traverse = Traverse.Create(terrainComp);

                    if (!traverse.Field("m_initialized").GetValue <bool>())
                    {
                        continue;
                    }

                    enumerator.Current.WorldToVertex(center, out int x, out int y);

                    bool[]  m_modifiedHeight = traverse.Field("m_modifiedHeight").GetValue <bool[]>();
                    float[] m_levelDelta     = traverse.Field("m_levelDelta").GetValue <float[]>();
                    float[] m_smoothDelta    = traverse.Field("m_smoothDelta").GetValue <float[]>();
                    bool[]  m_modifiedPaint  = traverse.Field("m_modifiedPaint").GetValue <bool[]>();
                    Color[] m_paintMask      = traverse.Field("m_paintMask").GetValue <Color[]>();

                    int m_width = traverse.Field("m_width").GetValue <int>();

                    Dbgl($"Checking heightmap at {terrainComp.transform.position}");
                    int  thisResets = 0;
                    bool thisReset  = false;
                    int  num        = m_width + 1;
                    for (int h = 0; h < num; h++)
                    {
                        for (int w = 0; w < num; w++)
                        {
                            int idx = h * num + w;

                            if (!m_modifiedHeight[idx])
                            {
                                continue;
                            }

                            //Dbgl($"Player coord {x},{y} coord {w},{h}, distance {CoordDistance(x, y, w, h)} has edits. ");

                            if (CoordDistance(x, y, w, h) > radius)
                            {
                                continue;
                            }

                            //Dbgl("In range, resetting");

                            resets++;
                            thisResets++;
                            thisReset = true;

                            m_modifiedHeight[idx] = false;
                            m_levelDelta[idx]     = 0;
                            m_smoothDelta[idx]    = 0;
                        }
                    }

                    num = m_width;
                    for (int h = 0; h < num; h++)
                    {
                        for (int w = 0; w < num; w++)
                        {
                            int idx = h * num + w;

                            if (!m_modifiedPaint[idx])
                            {
                                continue;
                            }

                            if (CoordDistance(x, y, w, h) > radius)
                            {
                                continue;
                            }

                            thisReset            = true;
                            m_modifiedPaint[idx] = false;
                            m_paintMask[idx]     = Color.clear;
                        }
                    }

                    if (thisReset)
                    {
                        Dbgl($"\tReset {thisResets} comp edits");

                        traverse.Field("m_modifiedHeight").SetValue(m_modifiedHeight);
                        traverse.Field("m_levelDelta").SetValue(m_levelDelta);
                        traverse.Field("m_smoothDelta").SetValue(m_smoothDelta);
                        traverse.Field("m_modifiedPaint").SetValue(m_modifiedPaint);
                        traverse.Field("m_paintMask").SetValue(m_paintMask);

                        traverse.Method("Save").GetValue();
                        enumerator.Current.Poke(true);
                    }
                }
            }

            if (resets > 0 && ClutterSystem.instance)
            {
                ClutterSystem.instance.ResetGrass(center, radius);
            }

            return(resets);
        }
    /// <summary>
    /// <inheritdoc/>
    /// </summary>
    /// <remarks>Does not currently take into account terrain changes</remarks>
    /// <remarks>I hate the heightmaps so god damn much.</remarks>
    public float Tilt(Vector3 worldCoordinate)
    {
        var point = WorldToZoneCoordinate(worldCoordinate);

#if false && DEBUG
        if (Player.m_localPlayer != null)
        {
            Log.LogTrace("ZoneId: " + zone.ZoneId);
            Log.LogTrace("HM ZonePos: " + Heightmap.FindHeightmap(point).transform.position);
            Log.LogTrace("HM ZoneId: " + ZoneSystem.instance.GetZone(Heightmap.FindHeightmap(point).transform.position));
            Log.LogTrace("Real ZoneId: " + ZoneSystem.instance.GetZone(point));
        }
#endif

        var center = point;
        var north  = center + North;
        var south  = center + South;
        var west   = center + West;
        var east   = center + East;

#if false && DEBUG
        if (Player.m_localPlayer != null)
        {
            Log.LogTrace("Built in Tilt: ");
            Log.LogTrace("\t" + Heightmap.FindHeightmap(point).CalcNormal(center.x, center.y));
            Log.LogTrace("\t" + Vector3.Angle(Vector3.up, Heightmap.FindHeightmap(point).CalcNormal(center.x, center.y)));


            Log.LogTrace("WorldToZoneCoordinate: " + center);
            Heightmap.FindHeightmap(point).WorldToVertex(point, out var wtvX, out var wtvY);
            Log.LogTrace("WorldToVertex: " + new Vector2i(wtvX, wtvY));

            Log.LogTrace("Original index in BaseHeights");
            Log.LogTrace($"\tY: {wtvY} * {65} = {wtvY * 65}");
            Log.LogTrace($"\tX: {wtvX}");
            Log.LogTrace($"\t == {wtvY * 65 + wtvX}");

            var hm        = Heightmap.FindHeightmap(point);
            var modifiers =
                TerrainModifier.GetAllInstances()
                .Where(x => x.enabled && hm.TerrainVSModifier(x))
                .ToList();

            Log.LogTrace("Terrain Modifiers: " + modifiers.Count);

            modifiers = modifiers
                        .Where(x =>
            {
                var modPos = x.transform.position + Vector3.up * x.m_levelOffset;
                if (x.m_level)
                {
                    return(Vector3.Distance(modPos, point) <= x.m_levelRadius);
                }
                else if (x.m_smooth)
                {
                    return(Vector3.Distance(modPos, point) <= x.m_smoothRadius);
                }

                return(false);
            })
                        .ToList();

            foreach (var modifier in modifiers)
            {
                var modPos = modifier.transform.position + Vector3.up * modifier.m_levelOffset;
                Log.LogTrace("\t" + (modPos - modifier.transform.position));
            }
        }
#endif
        var centerHeight = Height(center);
        var northHeight  = Height(north);
        var southHeight  = Height(south);
        var westHeight   = Height(west);
        var eastHeight   = Height(east);

#if false && DEBUG
        Log.LogTrace("Heights:");
        Log.LogTrace($"\tC: " + zone.Height(center));
        Log.LogTrace($"\tN: " + northHeight);
        Log.LogTrace($"\tS: " + southHeight);
        Log.LogTrace($"\tW: " + westHeight);
        Log.LogTrace($"\tE: " + eastHeight);

        if (Player.m_localPlayer != null)
        {
            Log.LogTrace("WorldToVertex:");
            Log.LogTrace($"\tScale: " + Heightmap.FindHeightmap(point).m_scale);
            Log.LogTrace($"\tWidth: " + Heightmap.FindHeightmap(point).m_width);
            Log.LogTrace($"\tY: " + Heightmap.FindHeightmap(point).transform.position.y);

            Log.LogTrace("Original heights:");
            {
                Heightmap.GetHeight(point, out var centerHeight);
                Heightmap.GetHeight(point + new Vector3(0, 2), out var nHeight);
                Heightmap.GetHeight(point + new Vector3(0, -2), out var sHeight);
                Heightmap.GetHeight(point + new Vector3(-2, 0), out var wHeight);
                Heightmap.GetHeight(point + new Vector3(2, 0), out var eHeight);

                Log.LogTrace("\tC: " + centerHeight);
                Log.LogTrace("\tN: " + nHeight);
                Log.LogTrace("\tS: " + sHeight);
                Log.LogTrace("\tW: " + wHeight);
                Log.LogTrace("\tE: " + eHeight);
            }

            Log.LogTrace("Original Base heights:");
            {
                float cHeight = Heightmap.FindHeightmap(point).GetBaseHeight(center.x, center.y);
                float nHeight = Heightmap.FindHeightmap(point).GetBaseHeight(north.x, north.y);
                float sHeight = Heightmap.FindHeightmap(point).GetBaseHeight(south.x, south.y);
                float wHeight = Heightmap.FindHeightmap(point).GetBaseHeight(west.x, west.y);
                float eHeight = Heightmap.FindHeightmap(point).GetBaseHeight(east.x, east.y);

                Log.LogTrace("\tC: " + cHeight);
                Log.LogTrace("\tN: " + nHeight);
                Log.LogTrace("\tS: " + sHeight);
                Log.LogTrace("\tW: " + wHeight);
                Log.LogTrace("\tE: " + eHeight);
            }
        }
#endif

        var toNorth = (new Vector3(North.x, northHeight - centerHeight, North.y));
        var toSouth = (new Vector3(South.x, southHeight - centerHeight, South.y));
        var toWest  = (new Vector3(West.x, westHeight - centerHeight, West.y));
        var toEast  = (new Vector3(East.x, eastHeight - centerHeight, East.y));

#if false && DEBUG
        Log.LogTrace($"North: " + toNorth);
        Log.LogTrace($"South: " + toSouth);
        Log.LogTrace($"West : " + toWest);
        Log.LogTrace($"East : " + toEast);
#endif

        toNorth = toNorth.normalized;
        toSouth = toSouth.normalized;
        toWest  = toWest.normalized;
        toEast  = toEast.normalized;

#if false && DEBUG
        Log.LogTrace($"North: " + toNorth);
        Log.LogTrace($"South: " + toSouth);
        Log.LogTrace($"West : " + toWest);
        Log.LogTrace($"East : " + toEast);
#endif
        var angleNorth = AngleByCross(toNorth, toEast);
        var angleEast  = AngleByCross(toEast, toSouth);
        var angleSouth = AngleByCross(toSouth, toWest);
        var angleWest  = AngleByCross(toWest, toNorth);

        float AngleByCross(Vector3 p1, Vector3 p2)
        {
            var cross = Vector3.Cross(p1, p2);

            return(Mathf.Abs(Vector3.Angle(Vector2.up, cross)));
        }

#if false && DEBUG
        Log.LogTrace($"North: " + angleNorth);
        Log.LogTrace($"South: " + angleSouth);
        Log.LogTrace($"West : " + angleWest);
        Log.LogTrace($"East : " + angleEast);
#endif

        var avgAngle = (angleNorth + angleSouth + angleWest + angleEast) / 4;

        return(avgAngle);
    }
Esempio n. 7
0
 public static void Postfix(ref Console __instance)
 {
     string[] part = __instance.m_input.text.Trim().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
     if (part.Length <= 0)
     {
         return;
     }
     if (part[0].StartsWith("/"))
     {
         part[0] = part[0].Substring(1);
     }
     if (part[0] == "help")
     {
         __instance.Print("countterrain [radius=max] - count nearby terrain modifications");
         __instance.Print("resetterrain [radius=" + Settings.resetRadius.Value + "] [type] - remove nearby terrain modifications");
         __instance.Print("undoreset - restore removed terrain modifications");
         __instance.Print("debugterrain - visualize terrain modifications");
         __instance.Print("debugstrength [new strength] - visualization strength");
         __instance.Print("debugdistance [new distance] - visualization distance");
     }
     else if (part[0] == "countterrain")
     {
         float radius = float.PositiveInfinity;
         if (part.Length > 1)
         {
             if (!float.TryParse(part[1], out radius))
             {
                 __instance.Print("Could not parse radius: " + part[1]);
                 return;
             }
         }
         if (Player.m_localPlayer != null)
         {
             Vector3           playerPos = Player.m_localPlayer.transform.position;
             TerrainModifier[] mods      = TerrainModifier.GetAllInstances().Where(x =>
                                                                                   x != null &&
                                                                                   x.m_playerModifiction &&
                                                                                   Utils.DistanceXZ(playerPos, x.transform.position) <= radius
                                                                                   ).ToArray();
             int levelCount  = mods.Where(x => x != null && x.m_level).Count();
             int smoothCount = mods.Where(x => x != null && x.m_smooth).Count();
             int paintCount  = mods.Where(x => x != null && x.m_paintCleared && !x.m_level && !x.m_smooth).Count();
             __instance.Print("Counted " + mods.Length + " terrain modifications");
             __instance.Print(levelCount + " level modifications");
             __instance.Print(smoothCount + " smooth modifications");
             __instance.Print(paintCount + " paint modifications");
         }
         else
         {
             __instance.Print("This command only works in game");
         }
     }
     else if (part[0] == "resetterrain")
     {
         float       radius = Settings.resetRadius.Value;
         TerrainType type   = TerrainType.all;
         if (part.Length > 1)
         {
             if (!float.TryParse(part[1], out radius))
             {
                 __instance.Print("Could not parse radius: " + part[1]);
                 return;
             }
         }
         if (part.Length > 2)
         {
             if (!Enum.TryParse(part[2].ToLowerInvariant(), out type) || int.TryParse(part[2], out _))
             {
                 __instance.Print("Could not parse type: " + part[2]);
                 return;
             }
         }
         if (Player.m_localPlayer != null)
         {
             Vector3           playerPos = Player.m_localPlayer.transform.position;
             TerrainModifier[] mods      = TerrainModifier.GetAllInstances().Where(x =>
                                                                                   x != null &&
                                                                                   x.m_playerModifiction &&
                                                                                   Utils.DistanceXZ(playerPos, x.transform.position) <= radius &&
                                                                                   (type != TerrainType.level || x.m_level) &&
                                                                                   (type != TerrainType.smooth || x.m_smooth) &&
                                                                                   (type != TerrainType.paint || (x.m_paintCleared && !x.m_level && !x.m_smooth))
                                                                                   ).ToArray();
             TerrainModifier[]  mods2       = mods.Where(x => x != null && m_nview(x) != null && m_nview(x).IsValid() && m_nview(x).IsOwner()).ToArray();
             List <RestoreData> restoreData = new List <RestoreData>();
             foreach (TerrainModifier mod in mods2)
             {
                 restoreData.Add(new RestoreData(mod.transform.position, ZNetScene.instance.GetPrefab(ZNetView.GetPrefabName(mod.gameObject)), mod.GetCreationTime()));
                 ZNetScene.instance.Destroy(mod.gameObject);
             }
             if (restoreData.Count > 0 && Settings.restoreMax.Value > 0)
             {
                 restoreInfo.Add(restoreData.ToArray());
                 while (restoreData.Count > Settings.restoreMax.Value)
                 {
                     restoreData.RemoveAt(0);
                 }
             }
             __instance.Print("Removed " + mods2.Length + " terrain modifications");
             if (mods2.Length < mods.Length)
             {
                 __instance.Print("Could not remove " + (mods.Length - mods2.Length) + " terrain mods due to ownership");
             }
         }
         else
         {
             __instance.Print("This command only works in game");
         }
     }
     else if (part[0] == "undoreset")
     {
         if (restoreInfo.Count > 0)
         {
             RestoreData[] restoreData = restoreInfo.Last();
             restoreInfo.Remove(restoreData);
             int restored = 0;
             foreach (RestoreData data in restoreData)
             {
                 GameObject instance         = UnityEngine.Object.Instantiate(data.prefab, data.position, Quaternion.identity);
                 ZNetView   view             = instance.GetComponent <ZNetView>();
                 view.GetZDO().m_timeCreated = data.creationTime;
                 restored++;
             }
             __instance.Print("Restored " + restored + " terrain modifiers");
         }
         else
         {
             __instance.Print("No available restore data");
         }
     }
     else if (part[0] == "debugterrain")
     {
         if (DebugToggle(out int count))
         {
             __instance.Print((debugTerrain ? "Enabled" : "Disabled") + " lights on " + count + " terrain modifications");
         }
         else
         {
             __instance.Print("This command only works in game");
         }
     }
     else if (part[0] == "debugstrength")
     {
         if (part.Length > 1)
         {
             if (float.TryParse(part[1], out float strength))
             {
                 Settings.lightStrength.Value = strength;
                 __instance.Print("Light strength set to " + strength);
             }
             else
             {
                 __instance.Print("Could not parse intensity: " + part[1]);
             }
         }
         else
         {
             __instance.Print("Current light strength is " + Settings.lightStrength.Value);
         }
     }
     else if (part[0] == "debugdistance")
     {
         if (part.Length > 1)
         {
             if (float.TryParse(part[1], out float distance))
             {
                 Settings.lightDistance.Value = distance;
                 __instance.Print("Light distance set to " + distance);
             }
             else
             {
                 __instance.Print("Could not parse distance: " + part[1]);
             }
         }
         else
         {
             __instance.Print("Current light distance is " + Settings.lightDistance.Value);
         }
     }
 }