D:\ValheimDev\Dumps\Old\assembly_valheim\TerrainComp.cs D:\ValheimDev\Dumps\Latest\assembly_valheim\TerrainComp.cs
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using UnityEngine; using UnityEngine;
   
public class TerrainComp : MonoBehaviour public class TerrainComp : MonoBehaviour
{ {
    private void Awake()     private void Awake()
    {     {
        this.m_nview = base.GetComponent<ZNetView>();         this.m_nview = base.GetComponent<ZNetView>();
        this.m_hmap = Heightmap.FindHeightmap(base.transform.position);         this.m_hmap = Heightmap.FindHeightmap(base.transform.position);
        if (this.m_hmap == null)         if (this.m_hmap == null)
        {         {
            ZLog.LogWarning("Terrain compiler could not find hmap");             ZLog.LogWarning("Terrain compiler could not find hmap");
            return;             return;
        }         }
        TerrainComp terrainComp = TerrainComp.FindTerrainCompiler(base.transform.position);         TerrainComp terrainComp = TerrainComp.FindTerrainCompiler(base.transform.position);
        if (terrainComp)         if (terrainComp)
        {         {
            ZLog.LogWarning("Found another terrain compiler in this area, removing it");             ZLog.LogWarning("Found another terrain compiler in this area, removing it");
            ZNetScene.instance.Destroy(terrainComp.gameObject);             ZNetScene.instance.Destroy(terrainComp.gameObject);
        }         }
        TerrainComp.s_instances.Add(this);         TerrainComp.s_instances.Add(this);
        this.m_nview.Register<ZPackage>("ApplyOperation", new Action<long, ZPackage>(this.RPC_ApplyOperation));         this.m_nview.Register<ZPackage>("ApplyOperation", new Action<long, ZPackage>(this.RPC_ApplyOperation));
        this.Initialize();         this.Initialize();
        this.CheckLoad();         this.CheckLoad();
    }     }
   
    private void OnDestroy()     private void OnDestroy()
    {     {
        TerrainComp.s_instances.Remove(this);         TerrainComp.s_instances.Remove(this);
    }     }
   
    private void Update()     private void Update()
    {     {
        if (!this.m_nview.IsValid())         if (!this.m_nview.IsValid())
        {         {
            return;             return;
        }         }
        this.CheckLoad();         this.CheckLoad();
    }     }
   
    private void Initialize()     private void Initialize()
    {     {
        this.m_initialized = true;         this.m_initialized = true;
        this.m_width = this.m_hmap.m_width;         this.m_width = this.m_hmap.m_width;
        this.m_size = (float)this.m_width * this.m_hmap.m_scale;         this.m_size = (float)this.m_width * this.m_hmap.m_scale;
        int num = this.m_width + 1;         int num = this.m_width + 1;
        this.m_modifiedHeight = new bool[num * num];         this.m_modifiedHeight = new bool[num * num];
        this.m_levelDelta = new float[num * num];         this.m_levelDelta = new float[num * num];
        this.m_smoothDelta = new float[num * num];         this.m_smoothDelta = new float[num * num];
.        this.m_modifiedPaint = new bool[this.m_width * this.m_width];         this.m_modifiedPaint = new bool[num * num];
        this.m_paintMask = new Color[this.m_width * this.m_width];         this.m_paintMask = new Color[num * num];
    }     }
   
    private void Save()     private void Save()
    {     {
        if (!this.m_initialized)         if (!this.m_initialized)
        {         {
            return;             return;
        }         }
        if (!this.m_nview.IsValid() || !this.m_nview.IsOwner())         if (!this.m_nview.IsValid() || !this.m_nview.IsOwner())
        {         {
            return;             return;
        }         }
        ZPackage zpackage = new ZPackage();         ZPackage zpackage = new ZPackage();
        zpackage.Write(1);         zpackage.Write(1);
        zpackage.Write(this.m_operations);         zpackage.Write(this.m_operations);
        zpackage.Write(this.m_lastOpPoint);         zpackage.Write(this.m_lastOpPoint);
        zpackage.Write(this.m_lastOpRadius);         zpackage.Write(this.m_lastOpRadius);
        zpackage.Write(this.m_modifiedHeight.Length);         zpackage.Write(this.m_modifiedHeight.Length);
        for (int i = 0; i < this.m_modifiedHeight.Length; i++)         for (int i = 0; i < this.m_modifiedHeight.Length; i++)
        {         {
            zpackage.Write(this.m_modifiedHeight[i]);             zpackage.Write(this.m_modifiedHeight[i]);
            if (this.m_modifiedHeight[i])             if (this.m_modifiedHeight[i])
            {             {
                zpackage.Write(this.m_levelDelta[i]);                 zpackage.Write(this.m_levelDelta[i]);
                zpackage.Write(this.m_smoothDelta[i]);                 zpackage.Write(this.m_smoothDelta[i]);
            }             }
        }         }
        zpackage.Write(this.m_modifiedPaint.Length);         zpackage.Write(this.m_modifiedPaint.Length);
        for (int j = 0; j < this.m_modifiedPaint.Length; j++)         for (int j = 0; j < this.m_modifiedPaint.Length; j++)
        {         {
            zpackage.Write(this.m_modifiedPaint[j]);             zpackage.Write(this.m_modifiedPaint[j]);
            if (this.m_modifiedPaint[j])             if (this.m_modifiedPaint[j])
            {             {
                zpackage.Write(this.m_paintMask[j].r);                 zpackage.Write(this.m_paintMask[j].r);
                zpackage.Write(this.m_paintMask[j].g);                 zpackage.Write(this.m_paintMask[j].g);
                zpackage.Write(this.m_paintMask[j].b);                 zpackage.Write(this.m_paintMask[j].b);
                zpackage.Write(this.m_paintMask[j].a);                 zpackage.Write(this.m_paintMask[j].a);
            }             }
        }         }
        byte[] array = Utils.Compress(zpackage.GetArray());         byte[] array = Utils.Compress(zpackage.GetArray());
        this.m_nview.GetZDO().Set(ZDOVars.s_TCData, array);         this.m_nview.GetZDO().Set(ZDOVars.s_TCData, array);
        this.m_lastDataRevision = this.m_nview.GetZDO().DataRevision;         this.m_lastDataRevision = this.m_nview.GetZDO().DataRevision;
    }     }
   
    private void CheckLoad()     private void CheckLoad()
    {     {
        if (this.m_nview.GetZDO().DataRevision != this.m_lastDataRevision)         if (this.m_nview.GetZDO().DataRevision != this.m_lastDataRevision)
        {         {
            int operations = this.m_operations;             int operations = this.m_operations;
            if (this.Load())             if (this.Load())
            {             {
                this.m_hmap.Poke(false);                 this.m_hmap.Poke(false);
                if (ClutterSystem.instance)                 if (ClutterSystem.instance)
                {                 {
                    if (this.m_operations == operations + 1)                     if (this.m_operations == operations + 1)
                    {                     {
                        ClutterSystem.instance.ResetGrass(this.m_lastOpPoint, this.m_lastOpRadius);                         ClutterSystem.instance.ResetGrass(this.m_lastOpPoint, this.m_lastOpRadius);
                        return;                         return;
                    }                     }
                    ClutterSystem.instance.ResetGrass(this.m_hmap.transform.position, (float)this.m_hmap.m_width * this.m_hmap.m_scale / 2f);                     ClutterSystem.instance.ResetGrass(this.m_hmap.transform.position, (float)this.m_hmap.m_width * this.m_hmap.m_scale / 2f);
                }                 }
            }             }
        }         }
    }     }
   
    private bool Load()     private bool Load()
    {     {
        this.m_lastDataRevision = this.m_nview.GetZDO().DataRevision;         this.m_lastDataRevision = this.m_nview.GetZDO().DataRevision;
        byte[] byteArray = this.m_nview.GetZDO().GetByteArray(ZDOVars.s_TCData, null);         byte[] byteArray = this.m_nview.GetZDO().GetByteArray(ZDOVars.s_TCData, null);
        if (byteArray == null)         if (byteArray == null)
        {         {
            return false;             return false;
        }         }
        ZPackage zpackage = new ZPackage(Utils.Decompress(byteArray));         ZPackage zpackage = new ZPackage(Utils.Decompress(byteArray));
        zpackage.ReadInt();         zpackage.ReadInt();
        this.m_operations = zpackage.ReadInt();         this.m_operations = zpackage.ReadInt();
        this.m_lastOpPoint = zpackage.ReadVector3();         this.m_lastOpPoint = zpackage.ReadVector3();
        this.m_lastOpRadius = zpackage.ReadSingle();         this.m_lastOpRadius = zpackage.ReadSingle();
        int num = zpackage.ReadInt();         int num = zpackage.ReadInt();
        if (num != this.m_modifiedHeight.Length)         if (num != this.m_modifiedHeight.Length)
        {         {
            ZLog.LogWarning("Terrain data load error, height array missmatch");             ZLog.LogWarning("Terrain data load error, height array missmatch");
            return false;             return false;
        }         }
        for (int i = 0; i < num; i++)         for (int i = 0; i < num; i++)
        {         {
            this.m_modifiedHeight[i] = zpackage.ReadBool();             this.m_modifiedHeight[i] = zpackage.ReadBool();
            if (this.m_modifiedHeight[i])             if (this.m_modifiedHeight[i])
            {             {
                this.m_levelDelta[i] = zpackage.ReadSingle();                 this.m_levelDelta[i] = zpackage.ReadSingle();
                this.m_smoothDelta[i] = zpackage.ReadSingle();                 this.m_smoothDelta[i] = zpackage.ReadSingle();
            }             }
            else             else
            {             {
                this.m_levelDelta[i] = 0f;                 this.m_levelDelta[i] = 0f;
                this.m_smoothDelta[i] = 0f;                 this.m_smoothDelta[i] = 0f;
            }             }
        }         }
        int num2 = zpackage.ReadInt();         int num2 = zpackage.ReadInt();
        for (int j = 0; j < num2; j++)         for (int j = 0; j < num2; j++)
        {         {
            this.m_modifiedPaint[j] = zpackage.ReadBool();             this.m_modifiedPaint[j] = zpackage.ReadBool();
            if (this.m_modifiedPaint[j])             if (this.m_modifiedPaint[j])
            {             {
                Color color = default(Color);                 Color color = default(Color);
                color.r = zpackage.ReadSingle();                 color.r = zpackage.ReadSingle();
                color.g = zpackage.ReadSingle();                 color.g = zpackage.ReadSingle();
                color.b = zpackage.ReadSingle();                 color.b = zpackage.ReadSingle();
                color.a = zpackage.ReadSingle();                 color.a = zpackage.ReadSingle();
                this.m_paintMask[j] = color;                 this.m_paintMask[j] = color;
            }             }
            else             else
            {             {
                this.m_paintMask[j] = Color.black;                 this.m_paintMask[j] = Color.black;
            }             }
        }         }
.          if (num2 == this.m_width * this.m_width)
          {
              Color[] array = new Color[this.m_paintMask.Length];
              this.m_paintMask.CopyTo(array, 0);
              bool[] array2 = new bool[this.m_modifiedPaint.Length];
              this.m_modifiedPaint.CopyTo(array2, 0);
              int num3 = this.m_width + 1;
              for (int k = 0; k < this.m_paintMask.Length; k++)
              {
                  int num4 = k / num3;
                  int num5 = (k + 1) / num3;
                  int num6 = k - num4;
                  if (num4 == this.m_width)
                  {
                      num6 -= this.m_width;
                  }
                  if (k > 0 && (k - num4) % this.m_width == 0 && (k + 1 - num5) % this.m_width == 0)
                  {
                      num6--;
                  }
                  this.m_paintMask[k] = array[num6];
                  this.m_modifiedPaint[k] = array2[num6];
              }
          }
        return true;         return true;
    }     }
   
    public static TerrainComp FindTerrainCompiler(Vector3 pos)     public static TerrainComp FindTerrainCompiler(Vector3 pos)
    {     {
        foreach (TerrainComp terrainComp in TerrainComp.s_instances)         foreach (TerrainComp terrainComp in TerrainComp.s_instances)
        {         {
            float num = terrainComp.m_size / 2f;             float num = terrainComp.m_size / 2f;
            Vector3 position = terrainComp.transform.position;             Vector3 position = terrainComp.transform.position;
            if (pos.x >= position.x - num && pos.x <= position.x + num && pos.z >= position.z - num && pos.z <= position.z + num)             if (pos.x >= position.x - num && pos.x <= position.x + num && pos.z >= position.z - num && pos.z <= position.z + num)
            {             {
                return terrainComp;                 return terrainComp;
            }             }
        }         }
        return null;         return null;
    }     }
   
    public void ApplyToHeightmap(Texture2D clearedMask, List<float> heights, float[] baseHeights, float[] levelOnlyHeights, Heightmap hm)     public void ApplyToHeightmap(Texture2D clearedMask, List<float> heights, float[] baseHeights, float[] levelOnlyHeights, Heightmap hm)
    {     {
        if (!this.m_initialized)         if (!this.m_initialized)
        {         {
            return;             return;
        }         }
        int num = this.m_width + 1;         int num = this.m_width + 1;
        for (int i = 0; i < num; i++)         for (int i = 0; i < num; i++)
        {         {
            for (int j = 0; j < num; j++)             for (int j = 0; j < num; j++)
            {             {
                int num2 = i * num + j;                 int num2 = i * num + j;
                float num3 = this.m_levelDelta[num2];                 float num3 = this.m_levelDelta[num2];
                float num4 = this.m_smoothDelta[num2];                 float num4 = this.m_smoothDelta[num2];
                if (num3 != 0f || num4 != 0f)                 if (num3 != 0f || num4 != 0f)
                {                 {
                    float num5 = heights[num2];                     float num5 = heights[num2];
                    float num6 = baseHeights[num2];                     float num6 = baseHeights[num2];
                    float num7 = num5 + num3 + num4;                     float num7 = num5 + num3 + num4;
                    num7 = Mathf.Clamp(num7, num6 - 8f, num6 + 8f);                     num7 = Mathf.Clamp(num7, num6 - 8f, num6 + 8f);
                    heights[num2] = num7;                     heights[num2] = num7;
                }                 }
            }             }
        }         }
.        for (int k = 0; k < this.m_width; k++)         for (int k = 0; k < num; k++)
        {         {
.            for (int l = 0; l < this.m_width; l++)             for (int l = 0; l < num; l++)
            {             {
.                int num8 = k * this.m_width + l;                 int num8 = k * num + l;
                if (this.m_modifiedPaint[num8])                 if (this.m_modifiedPaint[num8])
                {                 {
                    clearedMask.SetPixel(l, k, this.m_paintMask[num8]);                     clearedMask.SetPixel(l, k, this.m_paintMask[num8]);
                }                 }
            }             }
        }         }
    }     }
   
    public void ApplyOperation(TerrainOp modifier)     public void ApplyOperation(TerrainOp modifier)
    {     {
        ZPackage zpackage = new ZPackage();         ZPackage zpackage = new ZPackage();
        zpackage.Write(modifier.transform.position);         zpackage.Write(modifier.transform.position);
        modifier.m_settings.Serialize(zpackage);         modifier.m_settings.Serialize(zpackage);
        this.m_nview.InvokeRPC("ApplyOperation", new object[] { zpackage });         this.m_nview.InvokeRPC("ApplyOperation", new object[] { zpackage });
    }     }
   
    private void RPC_ApplyOperation(long sender, ZPackage pkg)     private void RPC_ApplyOperation(long sender, ZPackage pkg)
    {     {
        if (!this.m_nview.IsOwner())         if (!this.m_nview.IsOwner())
        {         {
            return;             return;
        }         }
        TerrainOp.Settings settings = new TerrainOp.Settings();         TerrainOp.Settings settings = new TerrainOp.Settings();
        Vector3 vector = pkg.ReadVector3();         Vector3 vector = pkg.ReadVector3();
        settings.Deserialize(pkg);         settings.Deserialize(pkg);
        this.DoOperation(vector, settings);         this.DoOperation(vector, settings);
    }     }
   
    private void DoOperation(Vector3 pos, TerrainOp.Settings modifier)     private void DoOperation(Vector3 pos, TerrainOp.Settings modifier)
    {     {
        if (!this.m_initialized)         if (!this.m_initialized)
        {         {
            return;             return;
        }         }
        this.InternalDoOperation(pos, modifier);         this.InternalDoOperation(pos, modifier);
        this.Save();         this.Save();
        this.m_hmap.Poke(false);         this.m_hmap.Poke(false);
        if (ClutterSystem.instance)         if (ClutterSystem.instance)
        {         {
            ClutterSystem.instance.ResetGrass(pos, modifier.GetRadius());             ClutterSystem.instance.ResetGrass(pos, modifier.GetRadius());
        }         }
    }     }
   
    private void InternalDoOperation(Vector3 pos, TerrainOp.Settings modifier)     private void InternalDoOperation(Vector3 pos, TerrainOp.Settings modifier)
    {     {
        if (modifier.m_level)         if (modifier.m_level)
        {         {
            this.LevelTerrain(pos + Vector3.up * modifier.m_levelOffset, modifier.m_levelRadius, modifier.m_square);             this.LevelTerrain(pos + Vector3.up * modifier.m_levelOffset, modifier.m_levelRadius, modifier.m_square);
        }         }
        if (modifier.m_raise)         if (modifier.m_raise)
        {         {
            this.RaiseTerrain(pos, modifier.m_raiseRadius, modifier.m_raiseDelta, modifier.m_square, modifier.m_raisePower);             this.RaiseTerrain(pos, modifier.m_raiseRadius, modifier.m_raiseDelta, modifier.m_square, modifier.m_raisePower);
        }         }
        if (modifier.m_smooth)         if (modifier.m_smooth)
        {         {
            this.SmoothTerrain(pos + Vector3.up * modifier.m_levelOffset, modifier.m_smoothRadius, modifier.m_square, modifier.m_smoothPower);             this.SmoothTerrain(pos + Vector3.up * modifier.m_levelOffset, modifier.m_smoothRadius, modifier.m_square, modifier.m_smoothPower);
        }         }
        if (modifier.m_paintCleared)         if (modifier.m_paintCleared)
        {         {
            this.PaintCleared(pos, modifier.m_paintRadius, modifier.m_paintType, modifier.m_paintHeightCheck, false);             this.PaintCleared(pos, modifier.m_paintRadius, modifier.m_paintType, modifier.m_paintHeightCheck, false);
        }         }
        this.m_operations++;         this.m_operations++;
        this.m_lastOpPoint = pos;         this.m_lastOpPoint = pos;
        this.m_lastOpRadius = modifier.GetRadius();         this.m_lastOpRadius = modifier.GetRadius();
    }     }
   
    private void LevelTerrain(Vector3 worldPos, float radius, bool square)     private void LevelTerrain(Vector3 worldPos, float radius, bool square)
    {     {
        int num;         int num;
        int num2;         int num2;
        this.m_hmap.WorldToVertex(worldPos, out num, out num2);         this.m_hmap.WorldToVertex(worldPos, out num, out num2);
        Vector3 vector = worldPos - base.transform.position;         Vector3 vector = worldPos - base.transform.position;
        float num3 = radius / this.m_hmap.m_scale;         float num3 = radius / this.m_hmap.m_scale;
        int num4 = Mathf.CeilToInt(num3);         int num4 = Mathf.CeilToInt(num3);
        int num5 = this.m_width + 1;         int num5 = this.m_width + 1;
        Vector2 vector2 = new Vector2((float)num, (float)num2);         Vector2 vector2 = new Vector2((float)num, (float)num2);
        for (int i = num2 - num4; i <= num2 + num4; i++)         for (int i = num2 - num4; i <= num2 + num4; i++)
        {         {
            for (int j = num - num4; j <= num + num4; j++)             for (int j = num - num4; j <= num + num4; j++)
            {             {
                if ((square || Vector2.Distance(vector2, new Vector2((float)j, (float)i)) <= num3) && j >= 0 && i >= 0 && j < num5 && i < num5)                 if ((square || Vector2.Distance(vector2, new Vector2((float)j, (float)i)) <= num3) && j >= 0 && i >= 0 && j < num5 && i < num5)
                {                 {
                    float height = this.m_hmap.GetHeight(j, i);                     float height = this.m_hmap.GetHeight(j, i);
                    float num6 = vector.y - height;                     float num6 = vector.y - height;
                    int num7 = i * num5 + j;                     int num7 = i * num5 + j;
                    num6 += this.m_smoothDelta[num7];                     num6 += this.m_smoothDelta[num7];
                    this.m_smoothDelta[num7] = 0f;                     this.m_smoothDelta[num7] = 0f;
                    this.m_levelDelta[num7] += num6;                     this.m_levelDelta[num7] += num6;
                    this.m_levelDelta[num7] = Mathf.Clamp(this.m_levelDelta[num7], -8f, 8f);                     this.m_levelDelta[num7] = Mathf.Clamp(this.m_levelDelta[num7], -8f, 8f);
                    this.m_modifiedHeight[num7] = true;                     this.m_modifiedHeight[num7] = true;
                }                 }
            }             }
        }         }
    }     }
   
    private void RaiseTerrain(Vector3 worldPos, float radius, float delta, bool square, float power)     private void RaiseTerrain(Vector3 worldPos, float radius, float delta, bool square, float power)
    {     {
        int num;         int num;
        int num2;         int num2;
        this.m_hmap.WorldToVertex(worldPos, out num, out num2);         this.m_hmap.WorldToVertex(worldPos, out num, out num2);
        Vector3 vector = worldPos - base.transform.position;         Vector3 vector = worldPos - base.transform.position;
        float num3 = radius / this.m_hmap.m_scale;         float num3 = radius / this.m_hmap.m_scale;
        int num4 = Mathf.CeilToInt(num3);         int num4 = Mathf.CeilToInt(num3);
        int num5 = this.m_width + 1;         int num5 = this.m_width + 1;
        Vector2 vector2 = new Vector2((float)num, (float)num2);         Vector2 vector2 = new Vector2((float)num, (float)num2);
        for (int i = num2 - num4; i <= num2 + num4; i++)         for (int i = num2 - num4; i <= num2 + num4; i++)
        {         {
            for (int j = num - num4; j <= num + num4; j++)             for (int j = num - num4; j <= num + num4; j++)
            {             {
                if (j >= 0 && i >= 0 && j < num5 && i < num5)                 if (j >= 0 && i >= 0 && j < num5 && i < num5)
                {                 {
                    float num6 = 1f;                     float num6 = 1f;
                    if (!square)                     if (!square)
                    {                     {
                        float num7 = Vector2.Distance(vector2, new Vector2((float)j, (float)i));                         float num7 = Vector2.Distance(vector2, new Vector2((float)j, (float)i));
                        if (num7 > num3)                         if (num7 > num3)
                        {                         {
                            goto IL_191;                             goto IL_191;
                        }                         }
                        if (power > 0f)                         if (power > 0f)
                        {                         {
                            num6 = num7 / num3;                             num6 = num7 / num3;
                            num6 = 1f - num6;                             num6 = 1f - num6;
                            if (power != 1f)                             if (power != 1f)
                            {                             {
                                num6 = Mathf.Pow(num6, power);                                 num6 = Mathf.Pow(num6, power);
                            }                             }
                        }                         }
                    }                     }
                    float height = this.m_hmap.GetHeight(j, i);                     float height = this.m_hmap.GetHeight(j, i);
                    float num8 = delta * num6;                     float num8 = delta * num6;
                    float num9 = vector.y + num8;                     float num9 = vector.y + num8;
                    if (delta >= 0f || num9 <= height)                     if (delta >= 0f || num9 <= height)
                    {                     {
                        if (delta > 0f)                         if (delta > 0f)
                        {                         {
                            if (num9 < height)                             if (num9 < height)
                            {                             {
                                goto IL_191;                                 goto IL_191;
                            }                             }
                            if (num9 > height + num8)                             if (num9 > height + num8)
                            {                             {
                                num9 = height + num8;                                 num9 = height + num8;
                            }                             }
                        }                         }
                        int num10 = i * num5 + j;                         int num10 = i * num5 + j;
                        float num11 = num9 - height + this.m_smoothDelta[num10];                         float num11 = num9 - height + this.m_smoothDelta[num10];
                        this.m_smoothDelta[num10] = 0f;                         this.m_smoothDelta[num10] = 0f;
                        this.m_levelDelta[num10] += num11;                         this.m_levelDelta[num10] += num11;
                        this.m_levelDelta[num10] = Mathf.Clamp(this.m_levelDelta[num10], -8f, 8f);                         this.m_levelDelta[num10] = Mathf.Clamp(this.m_levelDelta[num10], -8f, 8f);
                        this.m_modifiedHeight[num10] = true;                         this.m_modifiedHeight[num10] = true;
                    }                     }
                }                 }
                IL_191:;                 IL_191:;
            }             }
        }         }
    }     }
   
    private void SmoothTerrain(Vector3 worldPos, float radius, bool square, float power)     private void SmoothTerrain(Vector3 worldPos, float radius, bool square, float power)
    {     {
        int num;         int num;
        int num2;         int num2;
        this.m_hmap.WorldToVertex(worldPos, out num, out num2);         this.m_hmap.WorldToVertex(worldPos, out num, out num2);
        float num3 = worldPos.y - base.transform.position.y;         float num3 = worldPos.y - base.transform.position.y;
        float num4 = radius / this.m_hmap.m_scale;         float num4 = radius / this.m_hmap.m_scale;
        int num5 = Mathf.CeilToInt(num4);         int num5 = Mathf.CeilToInt(num4);
        Vector2 vector = new Vector2((float)num, (float)num2);         Vector2 vector = new Vector2((float)num, (float)num2);
        int num6 = this.m_width + 1;         int num6 = this.m_width + 1;
        for (int i = num2 - num5; i <= num2 + num5; i++)         for (int i = num2 - num5; i <= num2 + num5; i++)
        {         {
            for (int j = num - num5; j <= num + num5; j++)             for (int j = num - num5; j <= num + num5; j++)
            {             {
                float num7 = Vector2.Distance(vector, new Vector2((float)j, (float)i));                 float num7 = Vector2.Distance(vector, new Vector2((float)j, (float)i));
                if (num7 <= num4 && j >= 0 && i >= 0 && j < num6 && i < num6)                 if (num7 <= num4 && j >= 0 && i >= 0 && j < num6 && i < num6)
                {                 {
                    float num8 = num7 / num4;                     float num8 = num7 / num4;
                    if (power == 3f)                     if (power == 3f)
                    {                     {
                        num8 = num8 * num8 * num8;                         num8 = num8 * num8 * num8;
                    }                     }
                    else                     else
                    {                     {
                        num8 = Mathf.Pow(num8, power);                         num8 = Mathf.Pow(num8, power);
                    }                     }
                    float height = this.m_hmap.GetHeight(j, i);                     float height = this.m_hmap.GetHeight(j, i);
                    float num9 = 1f - num8;                     float num9 = 1f - num8;
                    float num10 = Mathf.Lerp(height, num3, num9) - height;                     float num10 = Mathf.Lerp(height, num3, num9) - height;
                    int num11 = i * num6 + j;                     int num11 = i * num6 + j;
                    this.m_smoothDelta[num11] += num10;                     this.m_smoothDelta[num11] += num10;
                    this.m_smoothDelta[num11] = Mathf.Clamp(this.m_smoothDelta[num11], -1f, 1f);                     this.m_smoothDelta[num11] = Mathf.Clamp(this.m_smoothDelta[num11], -1f, 1f);
                    this.m_modifiedHeight[num11] = true;                     this.m_modifiedHeight[num11] = true;
                }                 }
            }             }
        }         }
    }     }
   
    private void PaintCleared(Vector3 worldPos, float radius, TerrainModifier.PaintType paintType, bool heightCheck, bool apply)     private void PaintCleared(Vector3 worldPos, float radius, TerrainModifier.PaintType paintType, bool heightCheck, bool apply)
    {     {
        worldPos.x -= 0.5f;         worldPos.x -= 0.5f;
        worldPos.z -= 0.5f;         worldPos.z -= 0.5f;
        float num = worldPos.y - base.transform.position.y;         float num = worldPos.y - base.transform.position.y;
        int num2;         int num2;
        int num3;         int num3;
.        this.m_hmap.WorldToVertex(worldPos, out num2, out num3);         this.m_hmap.WorldToVertexMask(worldPos, out num2, out num3);
        float num4 = radius / this.m_hmap.m_scale;         float num4 = radius / this.m_hmap.m_scale;
        int num5 = Mathf.CeilToInt(num4);         int num5 = Mathf.CeilToInt(num4);
        Vector2 vector = new Vector2((float)num2, (float)num3);         Vector2 vector = new Vector2((float)num2, (float)num3);
        for (int i = num3 - num5; i <= num3 + num5; i++)         for (int i = num3 - num5; i <= num3 + num5; i++)
        {         {
            for (int j = num2 - num5; j <= num2 + num5; j++)             for (int j = num2 - num5; j <= num2 + num5; j++)
            {             {
                float num6 = Vector2.Distance(vector, new Vector2((float)j, (float)i));                 float num6 = Vector2.Distance(vector, new Vector2((float)j, (float)i));
.                if (j >= 0 && i >= 0 && j < this.m_width && i < this.m_width && (!heightCheck || this.m_hmap.GetHeight(j, i) <= num))                 int num7 = this.m_width + 1; 
                  if (j >= 0 && i >= 0 && j < num7 && i < num7 && (!heightCheck || this.m_hmap.GetHeight(j, i) <= num))
                {                 {
.                    float num7 = 1f - Mathf.Clamp01(num6 / num4);                     float num8 = 1f - Mathf.Clamp01(num6 / num4);
                    num7 = Mathf.Pow(num7, 0.1f);                     num8 = Mathf.Pow(num8, 0.1f);
                    Color color = this.m_hmap.GetPaintMask(j, i);                     Color color = this.m_hmap.GetPaintMask(j, i);
                    float a = color.a;                     float a = color.a;
                    switch (paintType)                     switch (paintType)
                    {                     {
                    case TerrainModifier.PaintType.Dirt:                     case TerrainModifier.PaintType.Dirt:
.                        color = Color.Lerp(color, Heightmap.m_paintMaskDirt, num7);                         color = Color.Lerp(color, Heightmap.m_paintMaskDirt, num8);
                        break;                         break;
                    case TerrainModifier.PaintType.Cultivate:                     case TerrainModifier.PaintType.Cultivate:
.                        color = Color.Lerp(color, Heightmap.m_paintMaskCultivated, num7);                         color = Color.Lerp(color, Heightmap.m_paintMaskCultivated, num8);
                        break;                         break;
                    case TerrainModifier.PaintType.Paved:                     case TerrainModifier.PaintType.Paved:
.                        color = Color.Lerp(color, Heightmap.m_paintMaskPaved, num7);                         color = Color.Lerp(color, Heightmap.m_paintMaskPaved, num8);
                        break;                         break;
                    case TerrainModifier.PaintType.Reset:                     case TerrainModifier.PaintType.Reset:
.                        color = Color.Lerp(color, Heightmap.m_paintMaskNothing, num7);                         color = Color.Lerp(color, Heightmap.m_paintMaskNothing, num8);
                        break;                         break;
                    }                     }
                    color.a = a;                     color.a = a;
.                    this.m_modifiedPaint[i * this.m_width + j] = true;                     this.m_modifiedPaint[i * num7 + j] = true;
                    this.m_paintMask[i * this.m_width + j] = color;                     this.m_paintMask[i * num7 + j] = color;
                }                 }
            }             }
        }         }
    }     }
   
    public bool IsOwner()     public bool IsOwner()
    {     {
        return this.m_nview.IsValid() && this.m_nview.IsOwner();         return this.m_nview.IsValid() && this.m_nview.IsOwner();
    }     }
   
    public void UpdatePaintMask(Heightmap hmap)     public void UpdatePaintMask(Heightmap hmap)
    {     {
        for (int i = 0; i < this.m_width; i++)         for (int i = 0; i < this.m_width; i++)
        {         {
            for (int j = 0; j < this.m_width; j++)             for (int j = 0; j < this.m_width; j++)
            {             {
                int num = i * this.m_width + j;                 int num = i * this.m_width + j;
                if (this.m_modifiedPaint[num])                 if (this.m_modifiedPaint[num])
                {                 {
                    Color color = this.m_paintMask[num];                     Color color = this.m_paintMask[num];
                    color.a = hmap.GetPaintMask(j, i).a;                     color.a = hmap.GetPaintMask(j, i).a;
                    this.m_paintMask[num] = color;                     this.m_paintMask[num] = color;
                }                 }
            }             }
        }         }
        this.Save();         this.Save();
        hmap.Poke(false);         hmap.Poke(false);
    }     }
   
    public static void UpgradeTerrain()     public static void UpgradeTerrain()
    {     {
        if (Player.m_localPlayer == null)         if (Player.m_localPlayer == null)
        {         {
            return;             return;
        }         }
        List<Heightmap> list = new List<Heightmap>();         List<Heightmap> list = new List<Heightmap>();
        Heightmap.FindHeightmap(Player.m_localPlayer.transform.position, 150f, list);         Heightmap.FindHeightmap(Player.m_localPlayer.transform.position, 150f, list);
        bool flag = false;         bool flag = false;
        using (List<Heightmap>.Enumerator enumerator = list.GetEnumerator())         using (List<Heightmap>.Enumerator enumerator = list.GetEnumerator())
        {         {
            while (enumerator.MoveNext())             while (enumerator.MoveNext())
            {             {
                if (TerrainComp.UpgradeTerrain(enumerator.Current))                 if (TerrainComp.UpgradeTerrain(enumerator.Current))
                {                 {
                    flag = true;                     flag = true;
                }                 }
            }             }
        }         }
        if (!flag)         if (!flag)
        {         {
            global::Console.instance.Print("Nothing to optimize");             global::Console.instance.Print("Nothing to optimize");
            return;             return;
        }         }
        global::Console.instance.Print("Optimized terrain");         global::Console.instance.Print("Optimized terrain");
    }     }
   
    public static bool UpgradeTerrain(Heightmap hmap)     public static bool UpgradeTerrain(Heightmap hmap)
    {     {
        List<TerrainModifier> allInstances = TerrainModifier.GetAllInstances();         List<TerrainModifier> allInstances = TerrainModifier.GetAllInstances();
        int num = 0;         int num = 0;
        List<TerrainModifier> list = new List<TerrainModifier>();         List<TerrainModifier> list = new List<TerrainModifier>();
        foreach (TerrainModifier terrainModifier in allInstances)         foreach (TerrainModifier terrainModifier in allInstances)
        {         {
            ZNetView component = terrainModifier.GetComponent<ZNetView>();             ZNetView component = terrainModifier.GetComponent<ZNetView>();
            if (!(component == null) && component.IsValid() && component.IsOwner() && terrainModifier.m_playerModifiction)             if (!(component == null) && component.IsValid() && component.IsOwner() && terrainModifier.m_playerModifiction)
            {             {
                if (!hmap.CheckTerrainModIsContained(terrainModifier))                 if (!hmap.CheckTerrainModIsContained(terrainModifier))
                {                 {
                    num++;                     num++;
                }                 }
                else                 else
                {                 {
                    list.Add(terrainModifier);                     list.Add(terrainModifier);
                }                 }
            }             }
        }         }
        if (list.Count == 0)         if (list.Count == 0)
        {         {
            return false;             return false;
        }         }
        TerrainComp andCreateTerrainCompiler = hmap.GetAndCreateTerrainCompiler();         TerrainComp andCreateTerrainCompiler = hmap.GetAndCreateTerrainCompiler();
        if (!andCreateTerrainCompiler.IsOwner())         if (!andCreateTerrainCompiler.IsOwner())
        {         {
            global::Console.instance.Print("Skipping terrain at " + hmap.transform.position.ToString() + " ( another player is currently the owner )");             global::Console.instance.Print("Skipping terrain at " + hmap.transform.position.ToString() + " ( another player is currently the owner )");
            return false;             return false;
        }         }
        int num2 = andCreateTerrainCompiler.m_width + 1;         int num2 = andCreateTerrainCompiler.m_width + 1;
        float[] array = new float[andCreateTerrainCompiler.m_modifiedHeight.Length];         float[] array = new float[andCreateTerrainCompiler.m_modifiedHeight.Length];
        for (int i = 0; i < num2; i++)         for (int i = 0; i < num2; i++)
        {         {
            for (int j = 0; j < num2; j++)             for (int j = 0; j < num2; j++)
            {             {
                array[i * num2 + j] = hmap.GetHeight(j, i);                 array[i * num2 + j] = hmap.GetHeight(j, i);
            }             }
        }         }
        Color[] array2 = new Color[andCreateTerrainCompiler.m_paintMask.Length];         Color[] array2 = new Color[andCreateTerrainCompiler.m_paintMask.Length];
.        for (int k = 0; k < andCreateTerrainCompiler.m_width; k++)         for (int k = 0; k < num2; k++)
        {         {
.            for (int l = 0; l < andCreateTerrainCompiler.m_width; l++)             for (int l = 0; l < num2; l++)
            {             {
.                array2[k * andCreateTerrainCompiler.m_width + l] = hmap.GetPaintMask(l, k);                 array2[k * num2 + l] = hmap.GetPaintMask(l, k);
            }             }
        }         }
        foreach (TerrainModifier terrainModifier2 in list)         foreach (TerrainModifier terrainModifier2 in list)
        {         {
            terrainModifier2.enabled = false;             terrainModifier2.enabled = false;
            terrainModifier2.GetComponent<ZNetView>().Destroy();             terrainModifier2.GetComponent<ZNetView>().Destroy();
        }         }
        hmap.Poke(false);         hmap.Poke(false);
        int num3 = 0;         int num3 = 0;
        for (int m = 0; m < num2; m++)         for (int m = 0; m < num2; m++)
        {         {
            for (int n = 0; n < num2; n++)             for (int n = 0; n < num2; n++)
            {             {
                int num4 = m * num2 + n;                 int num4 = m * num2 + n;
                float num5 = array[num4];                 float num5 = array[num4];
                float height = hmap.GetHeight(n, m);                 float height = hmap.GetHeight(n, m);
                float num6 = num5 - height;                 float num6 = num5 - height;
                if (Mathf.Abs(num6) >= 0.001f)                 if (Mathf.Abs(num6) >= 0.001f)
                {                 {
                    andCreateTerrainCompiler.m_modifiedHeight[num4] = true;                     andCreateTerrainCompiler.m_modifiedHeight[num4] = true;
                    andCreateTerrainCompiler.m_levelDelta[num4] += num6;                     andCreateTerrainCompiler.m_levelDelta[num4] += num6;
                    num3++;                     num3++;
                }                 }
            }             }
        }         }
        int num7 = 0;         int num7 = 0;
.        for (int num8 = 0; num8 < andCreateTerrainCompiler.m_width; num8++)         for (int num8 = 0; num8 < num2; num8++)
        {         {
.            for (int num9 = 0; num9 < andCreateTerrainCompiler.m_width; num9++)             for (int num9 = 0; num9 < num2; num9++)
            {             {
.                int num10 = num8 * andCreateTerrainCompiler.m_width + num9;                 int num10 = num8 * num2 + num9;
                Color color = array2[num10];                 Color color = array2[num10];
                Color paintMask = hmap.GetPaintMask(num9, num8);                 Color paintMask = hmap.GetPaintMask(num9, num8);
                if (!(color == paintMask))                 if (!(color == paintMask))
                {                 {
                    andCreateTerrainCompiler.m_modifiedPaint[num10] = true;                     andCreateTerrainCompiler.m_modifiedPaint[num10] = true;
                    andCreateTerrainCompiler.m_paintMask[num10] = color;                     andCreateTerrainCompiler.m_paintMask[num10] = color;
                    num7++;                     num7++;
                }                 }
            }             }
        }         }
        andCreateTerrainCompiler.Save();         andCreateTerrainCompiler.Save();
        hmap.Poke(false);         hmap.Poke(false);
        if (ClutterSystem.instance)         if (ClutterSystem.instance)
        {         {
            ClutterSystem.instance.ResetGrass(hmap.transform.position, (float)hmap.m_width * hmap.m_scale / 2f);             ClutterSystem.instance.ResetGrass(hmap.transform.position, (float)hmap.m_width * hmap.m_scale / 2f);
        }         }
        global::Console.instance.Print(string.Concat(new string[]         global::Console.instance.Print(string.Concat(new string[]
        {         {
            "Operations optimized:",             "Operations optimized:",
            list.Count.ToString(),             list.Count.ToString(),
            "  height changes:",             "  height changes:",
            num3.ToString(),             num3.ToString(),
            "  paint changes:",             "  paint changes:",
            num7.ToString()             num7.ToString()
        }));         }));
        return true;         return true;
    }     }
   
    private const int terrainCompVersion = 1;     private const int terrainCompVersion = 1;
   
    private static readonly List<TerrainComp> s_instances = new List<TerrainComp>();     private static readonly List<TerrainComp> s_instances = new List<TerrainComp>();
   
    private bool m_initialized;     private bool m_initialized;
   
    private int m_width;     private int m_width;
   
    private float m_size;     private float m_size;
   
    private int m_operations;     private int m_operations;
   
    private bool[] m_modifiedHeight;     private bool[] m_modifiedHeight;
   
    private float[] m_levelDelta;     private float[] m_levelDelta;
   
    private float[] m_smoothDelta;     private float[] m_smoothDelta;
   
    private bool[] m_modifiedPaint;     private bool[] m_modifiedPaint;
   
    private Color[] m_paintMask;     private Color[] m_paintMask;
   
    private Heightmap m_hmap;     private Heightmap m_hmap;
   
    private ZNetView m_nview;     private ZNetView m_nview;
   
    private uint m_lastDataRevision = uint.MaxValue;     private uint m_lastDataRevision = uint.MaxValue;
   
    private Vector3 m_lastOpPoint;     private Vector3 m_lastOpPoint;
   
    private float m_lastOpRadius;     private float m_lastOpRadius;
} }