<commit_before>﻿using UnityEngine;
using System.Collections;
using System;
using System.Collections.Generic;
using System.Linq;
using RSG.Utils;

namespace HexTiles
{
    [RequireComponent(typeof(MeshFilter), typeof(MeshRenderer), typeof(MeshCollider))]
    public class HexTile : MonoBehaviour
    {
        /// <summary>
        /// Total diameter of the hex.
        /// </summary>
        private float size = 1f;

        /// <summary>
        /// Total diameter of the hex.
        /// </summary>
        public float Diameter
        {
            get
            {
                return size;
            }
            set
            {
                size = value;
            }
        }

        /// <summary>
        /// Helper for easily getting the elevation of a tile.
        /// </summary>
        public float Elevation
        {
            get
            {
                return transform.localPosition.y;
            }
        }

        [SerializeField]
        private List<SidePiece> sidePieces = new List<SidePiece>();

        /// <summary>
        /// Draw gizmos for debugging the hex vertex placement.
        /// </summary>
        [SerializeField]
        [Tooltip("Draw gizmos for debugging the hex vertex placement")]
        private bool debugDrawGizmos = false;

        /// <summary>
        /// Tileset textures are 3 hexes wide for the tops of the tiles.
        /// </summary>
        private static readonly float hexWidthUV = 1f / 3f;

        /// <summary>
        /// Offset for the side piece textures, since they only use the lower half of the texture we apply to the mesh.
        /// </summary>
        private static readonly float sidePieceUVOffsetY = 0.5f;

        /// <summary>
        /// Get the UV coordinates of a given hex tile.
        /// </summary>
        private static Vector2 HexCoordsToUV(HexCoords hIn)
        {
            var x = hexWidthUV/2f * 3f/2f * hIn.Q;
            var y = hexWidthUV/2f * Mathf.Sqrt(3f) * (hIn.R + hIn.Q / 2f);

            return new Vector2(x, y);
        }

        /// <summary>
        /// Create the mesh used to render the hex.
        /// </summary>
        public void GenerateMesh(HexCoords position)
        {
            var mesh = GetComponent<MeshFilter>().mesh = new Mesh();
            mesh.name = "Procedural hex tile";

            var vertices = HexMetrics.GetHexVertices(Diameter).ToList();
            var triangles = new List<int>();

            // UV coordinates for tops of hex tiles.
            var uvBasePos = HexCoordsToUV(position);
            var uv = new List<Vector2>();

            // Each third tile needs a UV seam through the middle of it for textures to loop properly.
            var offsetCoords = position.ToOffset();
            if (offsetCoords.x % 2 == 0 && offsetCoords.y % 3 == 0)
            {
                vertices.Insert(4, vertices[0]);
                vertices.Insert(5, vertices[3]);

                triangles.AddRange(new int[]
                {
                    // Start with the preset triangles for the hex top.
                    0, 1, 2,
                    0, 2, 3,
                    4, 5, 7,
                    7, 5, 6
                });

                for (var i = 0; i < vertices.Count; i++)
                {
                    float uvY;
                    if (i == 0 || i == 3)
                    {
                        uvY = 0.0f;
                    }
                    else if (i == 5 || i == 4)
                    {
                        uvY = -0.5f;
                    }
                    else
                    {
                        var relativeVertexPosInUVSpace = vertices[i].z / Diameter * hexWidthUV;
                        uvY = -Utils.Mod(((uvBasePos.y + relativeVertexPosInUVSpace) / HexMetrics.hexHeightToWidth / 2), 0.5f);
                    }
                    uv.Add(new Vector2(
                        uvBasePos.x + vertices[i].x / Diameter * hexWidthUV,
                        uvY));
                }
            }
            else
            {
                triangles.AddRange(new int[]
                {
                    // Start with the preset triangles for the hex top.
                    0, 1, 2,
                    0, 2, 3,
                    0, 3, 5,
                    5, 3, 4
                });

                for (var i = 0; i < vertices.Count; i++)
                {
                    var relativeVertexPosInUVSpace = vertices[i].z / Diameter * hexWidthUV;
                    var uvY = -Utils.Mod(((uvBasePos.y + relativeVertexPosInUVSpace) / HexMetrics.hexHeightToWidth / 2), 0.5f);
                    uv.Add(new Vector2(
                        uvBasePos.x + vertices[i].x / Diameter * hexWidthUV, 
                        uvY));
                }
            }<commit_msg>Kind of hacky fix for bug with top hex tile placement<commit_after>﻿using UnityEngine;
using System.Collections;
using System;
using System.Collections.Generic;
using System.Linq;
using RSG.Utils;

namespace HexTiles
{
    [RequireComponent(typeof(MeshFilter), typeof(MeshRenderer), typeof(MeshCollider))]
    public class HexTile : MonoBehaviour
    {
        /// <summary>
        /// Total diameter of the hex.
        /// </summary>
        private float size = 1f;

        /// <summary>
        /// Total diameter of the hex.
        /// </summary>
        public float Diameter
        {
            get
            {
                return size;
            }
            set
            {
                size = value;
            }
        }

        /// <summary>
        /// Helper for easily getting the elevation of a tile.
        /// </summary>
        public float Elevation
        {
            get
            {
                return transform.localPosition.y;
            }
        }

        [SerializeField]
        private List<SidePiece> sidePieces = new List<SidePiece>();

        /// <summary>
        /// Draw gizmos for debugging the hex vertex placement.
        /// </summary>
        [SerializeField]
        [Tooltip("Draw gizmos for debugging the hex vertex placement")]
        private bool debugDrawGizmos = false;

        /// <summary>
        /// Tileset textures are 3 hexes wide for the tops of the tiles.
        /// </summary>
        private static readonly float hexWidthUV = 1f / 3f;

        /// <summary>
        /// Offset for the side piece textures, since they only use the lower half of the texture we apply to the mesh.
        /// </summary>
        private static readonly float sidePieceUVOffsetY = 0.5f;

        /// <summary>
        /// Get the UV coordinates of a given hex tile.
        /// </summary>
        private static Vector2 HexCoordsToUV(HexCoords hIn)
        {
            var x = hexWidthUV/2f * 3f/2f * hIn.Q;
            var y = hexWidthUV/2f * Mathf.Sqrt(3f) * (hIn.R + hIn.Q / 2f);

            return new Vector2(x, y);
        }

        /// <summary>
        /// Create the mesh used to render the hex.
        /// </summary>
        public void GenerateMesh(HexCoords position)
        {
            var mesh = GetComponent<MeshFilter>().mesh = new Mesh();
            mesh.name = "Procedural hex tile";

            var vertices = HexMetrics.GetHexVertices(Diameter).ToList();
            var triangles = new List<int>();

            // UV coordinates for tops of hex tiles.
            var uvBasePos = HexCoordsToUV(position);
            var uv = new List<Vector2>();

            // Each third tile needs a UV seam through the middle of it for textures to loop properly.
            var offsetCoords = position.ToOffset();
            if (offsetCoords.x % 2 == 0 && offsetCoords.y % 3 == 0)
            {
                vertices.Insert(4, vertices[0]);
                vertices.Insert(5, vertices[3]);

                triangles.AddRange(new int[]
                {
                    // Start with the preset triangles for the hex top.
                    0, 1, 2,
                    0, 2, 3,
                    4, 5, 7,
                    7, 5, 6
                });

                for (var i = 0; i < vertices.Count; i++)
                {
                    float uvY;
                    if (i == 5 || i == 4)
                    {
                        uvY = -0.5f;
                    }
                    else
                    {
                        var relativeVertexPosInUVSpace = vertices[i].z / Diameter * hexWidthUV;
                        uvY = -Utils.Mod(((uvBasePos.y + relativeVertexPosInUVSpace) / HexMetrics.hexHeightToWidth / 2), 0.5f);
                    }
                    uv.Add(new Vector2(
                        uvBasePos.x + vertices[i].x / Diameter * hexWidthUV,
                        uvY));
                }
            }
            else
            {
                triangles.AddRange(new int[]
                {
                    // Start with the preset triangles for the hex top.
                    0, 1, 2,
                    0, 2, 3,
                    0, 3, 5,
                    5, 3, 4
                });

                for (var i = 0; i < vertices.Count; i++)
                {
                    var relativeVertexPosInUVSpace = vertices[i].z / Diameter * hexWidthUV;
                    float uvY;
                    // Sometimes due to rounding the UV coordinate can end up on the wrong side of the texture
                    // for tiles at the edges of the texture. This will ensure that they appear on the correct
                    // side.
                    if ((offsetCoords.x % 2 != 0 && (offsetCoords.y + 1) % 3 == 0) && (i == 1 || i == 2))
                    {
                        uvY = -0.5f;
                    }
                    else
                    {
                        uvY = -Utils.Mod(((uvBasePos.y + relativeVertexPosInUVSpace) / HexMetrics.hexHeightToWidth / 2), 0.5f);
                    }
                    uv.Add(new Vector2(
                        uvBasePos.x + vertices[i].x / Diameter * hexWidthUV, 
                        uvY));
                }
            }
{'fraction_non_alphanumeric': 0.0978336136116332, 'fraction_numerical': 0.013354436640617272, 'mean_word_length': 1.107567229518449, 'pattern_counts': {'":': 0, '<': 49, '<?xml version=': 0, '>': 45, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1339622', 'n_tokens_mistral': 2819, 'n_tokens_neox': 2544, 'n_words': 787}