<commit_before>﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;

namespace Lockstep
{
    public class HeightmapHelper : BehaviourHelper
    {
        public static HeightmapHelper Instance { get; private set; }

        [SerializeField]
        private Vector2d _size = new Vector2d(100, 100);

        public Vector2d Size { get { return _size; } }

        [SerializeField]
        private Vector2d _heightBounds = new Vector2d(-20, 50);

        public Vector2d HeightBounds { get { return _heightBounds; } }

        [SerializeField]
        private Vector2d _bottomLeft = new Vector2d(-50, -50);

        public Vector2d BottomLeft { get { return _bottomLeft; } }

        /// <summary>
        /// Interval distance between each consecutive scan
        /// </summary>
        [SerializeField,FixedNumber]
        private long _interval = FixedMath.Half;

        public long Interval { get { return _interval; } }

        const int CompressionShift = FixedMath.SHIFT_AMOUNT / 2;

        [SerializeField]
        private HeightMap[] _maps = new HeightMap[1];

        public HeightMap[] Maps { get { return _maps; } }

        public short[,] Scan(int scanLayers)
        {

            int widthPeriods = Size.x.Div(Interval).CeilToInt();

            int heightPeriods = Size.y.Div(Interval).CeilToInt();
            short[,] heightMap = new short[widthPeriods, heightPeriods];


            Vector3 startPos = _bottomLeft.ToVector3(HeightBounds.y.ToFloat());
            Vector3 scanPos = startPos;
            float dist = (HeightBounds.y - HeightBounds.x).ToFloat();

            float fRes = Interval.ToFloat();
            for (int x = 0; x < widthPeriods; x++)
            {
                scanPos.z = startPos.z;
                for (int y = 0; y < heightPeriods; y++)
                {
                    RaycastHit hit;
                    long height;
                    if (Physics.Raycast(scanPos, Vector3.down, out hit, dist, scanLayers, QueryTriggerInteraction.UseGlobal))
                    {
                        height = FixedMath.Create(hit.point.y);
                    } else
                    {
                        height = HeightBounds.x;
                    }
                   
                    heightMap [x, y] = Compress(height);
                    scanPos.z += fRes;
                }
                scanPos.x += fRes;
            }

            return heightMap;
        }

        protected override void OnInitialize()
        {
            Instance = this;
        }

        public long GetHeight(int mapIndex, Vector2d position)
        {
            if (mapIndex >= Maps.Length)
            {
                return this.HeightBounds.x;
            }
            HeightMap map = Maps [mapIndex];
            long normX = (position.x - this._bottomLeft.x).Div(Interval);
            long normY = (position.y - this._bottomLeft.y).Div(Interval);
            int gridX = FixedMath.ToInt(normX);
            int gridY = FixedMath.ToInt(normY);
            long fractionX = (normX - FixedMath.Create(gridX));
            long fractionY = (normY - FixedMath.Create(gridY));
            long baseHeight = map.GetHeight(gridX, gridY);

            int nextX = Mathf.Clamp(gridX + 1, 0, map.Map.Width);

            int nextY = Mathf.Clamp(gridY + 1, 0, map.Map.Height);

            //bilinear lerp
            long h1 = FixedMath.Lerp(baseHeight, map.GetHeight(nextX, gridY), fractionX);
            long h2 = FixedMath.Lerp(map.GetHeight(gridX, nextY), map.GetHeight(nextX, nextY), fractionX);
            return FixedMath.Lerp(h1, h2, fractionY);

            return baseHeight;
        }

        void OnDrawGizmos()
        {
            float fRes = Interval.ToFloat();
            Vector3 size = Vector3.one * (fRes * .95f);
            size.y = .1f;
            Color color = Color.grey;
            for (int i = 0; i < Maps.Length; i++)
            { 
                color *= 1.1f;
                HeightMap map = Maps [i];
                Vector3 startPos = this.BottomLeft.ToVector3(0);
                Vector3 drawPos = startPos;
                for (int x = 0; x < map.Map.Width; x++)
                {
                    drawPos.z = startPos.z;
                    for (int y = 0; y < map.Map.Height; y++)
                    {
                        drawPos.y = map.GetHeight(x, y).ToFloat();
                        Gizmos.DrawCube(drawPos, size);
                        drawPos.z += fRes;
                    }
                    drawPos.x += fRes;
                }                    
            }

        }

        /*
        [SerializeField]
        Terrain[] _visualizeTerrains;

        void SetTerrain(Terrain terrain, float[,] heights)
        {
            terrain.terrainData.SetHeights(0, 0, heights);
            terrain.transform.position = _bottomLeft.ToVector3(0);
        }
*/
        public static short Compress(long value)
        {
            long compressed = value >> CompressionShift;
            if (compressed > short.MaxValue)
                compressed = short.MaxValue;
            else if (compressed < short.MinValue)
                compressed = short.MinValue;
            return (short)compressed;
        }

        public static long Uncompress(short compressed)
        {
            return (long)(compressed << CompressionShift);
        }
    }
}<commit_msg>Add show toggle to HeightmapHelper<commit_after>﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;

namespace Lockstep
{
    public class HeightmapHelper : BehaviourHelper
    {
        public static HeightmapHelper Instance { get; private set; }

        [SerializeField]
        private Vector2d _size = new Vector2d(100, 100);

        public Vector2d Size { get { return _size; } }

        [SerializeField]
        private Vector2d _heightBounds = new Vector2d(-20, 50);

        public Vector2d HeightBounds { get { return _heightBounds; } }

        [SerializeField]
        private Vector2d _bottomLeft = new Vector2d(-50, -50);

        public Vector2d BottomLeft { get { return _bottomLeft; } }

        /// <summary>
        /// Interval distance between each consecutive scan
        /// </summary>
        [SerializeField,FixedNumber]
        private long _interval = FixedMath.Half;

        public long Interval { get { return _interval; } }

        const int CompressionShift = FixedMath.SHIFT_AMOUNT / 2;

        [SerializeField]
        private HeightMap[] _maps = new HeightMap[1];

        public HeightMap[] Maps { get { return _maps; } }

        public short[,] Scan(int scanLayers)
        {

            int widthPeriods = Size.x.Div(Interval).CeilToInt();

            int heightPeriods = Size.y.Div(Interval).CeilToInt();
            short[,] heightMap = new short[widthPeriods, heightPeriods];


            Vector3 startPos = _bottomLeft.ToVector3(HeightBounds.y.ToFloat());
            Vector3 scanPos = startPos;
            float dist = (HeightBounds.y - HeightBounds.x).ToFloat();

            float fRes = Interval.ToFloat();
            for (int x = 0; x < widthPeriods; x++)
            {
                scanPos.z = startPos.z;
                for (int y = 0; y < heightPeriods; y++)
                {
                    RaycastHit hit;
                    long height;
                    if (Physics.Raycast(scanPos, Vector3.down, out hit, dist, scanLayers, QueryTriggerInteraction.UseGlobal))
                    {
                        height = FixedMath.Create(hit.point.y);
                    } else
                    {
                        height = HeightBounds.x;
                    }
                   
                    heightMap [x, y] = Compress(height);
                    scanPos.z += fRes;
                }
                scanPos.x += fRes;
            }

            return heightMap;
        }

        protected override void OnInitialize()
        {
            Instance = this;
        }

        public long GetHeight(int mapIndex, Vector2d position)
        {
            if (mapIndex >= Maps.Length)
            {
                return this.HeightBounds.x;
            }
            HeightMap map = Maps [mapIndex];
            long normX = (position.x - this._bottomLeft.x).Div(Interval);
            long normY = (position.y - this._bottomLeft.y).Div(Interval);
            int gridX = FixedMath.ToInt(normX);
            int gridY = FixedMath.ToInt(normY);
            long fractionX = (normX - FixedMath.Create(gridX));
            long fractionY = (normY - FixedMath.Create(gridY));
            long baseHeight = map.GetHeight(gridX, gridY);

            int nextX = Mathf.Clamp(gridX + 1, 0, map.Map.Width);

            int nextY = Mathf.Clamp(gridY + 1, 0, map.Map.Height);

            //bilinear lerp
            long h1 = FixedMath.Lerp(baseHeight, map.GetHeight(nextX, gridY), fractionX);
            long h2 = FixedMath.Lerp(map.GetHeight(gridX, nextY), map.GetHeight(nextX, nextY), fractionX);
            return FixedMath.Lerp(h1, h2, fractionY);

            return baseHeight;
        }
        [SerializeField]
        private bool _show;
        void OnDrawGizmos()
        {
            if (_show == false) return;
            float fRes = Interval.ToFloat();
            Vector3 size = Vector3.one * (fRes * .95f);
            size.y = .1f;
            Color color = Color.grey;
            for (int i = 0; i < Maps.Length; i++)
            { 
                color *= 1.1f;
                HeightMap map = Maps [i];
                Vector3 startPos = this.BottomLeft.ToVector3(0);
                Vector3 drawPos = startPos;
                for (int x = 0; x < map.Map.Width; x++)
                {
                    drawPos.z = startPos.z;
                    for (int y = 0; y < map.Map.Height; y++)
                    {
                        drawPos.y = map.GetHeight(x, y).ToFloat();
                        Gizmos.DrawCube(drawPos, size);
                        drawPos.z += fRes;
                    }
                    drawPos.x += fRes;
                }                    
            }

        }

        /*
        [SerializeField]
        Terrain[] _visualizeTerrains;

        void SetTerrain(Terrain terrain, float[,] heights)
        {
            terrain.terrainData.SetHeights(0, 0, heights);
            terrain.transform.position = _bottomLeft.ToVector3(0);
        }
*/
        public static short Compress(long value)
        {
            long compressed = value >> CompressionShift;
            if (compressed > short.MaxValue)
                compressed = short.MaxValue;
            else if (compressed < short.MinValue)
                compressed = short.MinValue;
            return (short)compressed;
        }

        public static long Uncompress(short compressed)
        {
            return (long)(compressed << CompressionShift);
        }
    }
}
{'fraction_non_alphanumeric': 0.09800164248562825, 'fraction_numerical': 0.010584907382060407, 'mean_word_length': 1.471251409244645, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6699240', 'n_tokens_mistral': 3184, 'n_tokens_neox': 2925, 'n_words': 710}