private SpriteMarker make_sprite(Vector3 pos, ConnectionSprite cs, Vector3 offset)
    {
        pos.z = -3f;
        var g  = GameObject.Instantiate(MapSpritePrefab);
        var sm = g.GetComponent <SpriteMarker>();

        sm.SetSprite(cs);
        sm.transform.position = pos + offset;

        m_sprites.Add(sm);
        //m_sprites.AddRange(sm.CreateMirrorSprites());

        return(sm);
    }
    private SpriteMarker make_bridge_sprite(Vector3 pos, ConnectionSprite cs, Vector3 offset, bool force, bool flip_x)
    {
        pos.z = -3f;
        var g  = GameObject.Instantiate(MapSpritePrefab);
        var sm = g.GetComponent <SpriteMarker>();

        sm.SetSprite(cs);
        sm.SetFlip(force, flip_x);
        sm.transform.position = pos + offset;

        m_sprites.Add(sm);
        m_sprites.AddRange(sm.CreateMirrorSprites(m_forced_max, m_forced_min, m_force_mirror));

        return(sm);
    }
    public List <SpriteMarker> PlaceSprites()
    {
        if (m_sprites != null)
        {
            foreach (SpriteMarker sm in m_sprites)
            {
                GameObject.Destroy(sm.gameObject);
            }
        }

        m_sprites = new List <SpriteMarker>();

        if (m_connection.ConnectionType == ConnectionType.MOUNTAIN)
        {
            if (PolyBorder == null)
            {
                return(m_sprites);
            }

            m_sprites = new List <SpriteMarker>();

            Vector3          last = new Vector3(-900, -900, 0);
            ConnectionSprite cs   = ArtManager.s_art_manager.GetConnectionSprite(ConnectionType.MOUNTAIN);

            if (cs == null)
            {
                return(m_sprites);
            }

            int ct = 0;

            make_sprite(PolyBorder.P1, cs, Vector3.zero);
            cs = ArtManager.s_art_manager.GetConnectionSprite(ConnectionType.MOUNTAIN);
            make_sprite(PolyBorder.P2, cs, Vector3.zero);
            cs = ArtManager.s_art_manager.GetConnectionSprite(ConnectionType.MOUNTAIN);

            foreach (Vector3 pt in PolyBorder.OrderedFinePoints)
            {
                if (Vector3.Distance(last, pt) < cs.Size && ct < m_border.OrderedFinePoints.Count - 1)
                {
                    ct++;
                    continue;
                }

                ct++;
                last = pt;

                make_sprite(pt, cs, Vector3.zero);

                cs = ArtManager.s_art_manager.GetConnectionSprite(ConnectionType.MOUNTAIN);
            }
        }
        else if (m_connection.ConnectionType == ConnectionType.MOUNTAINPASS)
        {
            if (PolyBorder == null)
            {
                return(m_sprites);
            }

            m_sprites = new List <SpriteMarker>();

            Vector3          last  = new Vector3(-900, -900, 0);
            ConnectionSprite cs    = ArtManager.s_art_manager.GetConnectionSprite(ConnectionType.MOUNTAIN);
            PolyBorder       other = PolyBorder.Reversed();

            int right_ct  = 0;
            int right_pos = 0;

            foreach (Vector3 pt in other.OrderedFinePoints)
            {
                if (Vector3.Distance(last, pt) < cs.Size)
                {
                    right_pos++;
                    continue;
                }

                last = pt;

                make_sprite(pt, cs, Vector3.zero);

                if (right_ct > 1)
                {
                    break;
                }

                cs = ArtManager.s_art_manager.GetConnectionSprite(ConnectionType.MOUNTAIN);

                right_ct++;
                right_pos++;
            }

            Vector3 endpt = other.OrderedFinePoints[right_pos];
            right_ct = -1;
            cs       = ArtManager.s_art_manager.GetConnectionSprite(ConnectionType.MOUNTAIN);
            bool is_mountain = true;

            foreach (Vector3 pt in PolyBorder.OrderedFinePoints)
            {
                if (Vector3.Distance(pt, endpt) < 0.08f)
                {
                    break;
                }

                if (Vector3.Distance(last, pt) < cs.Size)
                {
                    continue;
                }

                last = pt;

                if (!is_mountain)
                {
                    make_sprite(pt, cs, new Vector3(0, 0.01f));
                }
                else
                {
                    make_sprite(pt, cs, Vector3.zero);
                }

                if (Vector3.Distance(pt, endpt) < 2.0f)
                {
                    cs          = ArtManager.s_art_manager.GetConnectionSprite(ConnectionType.MOUNTAINPASS);
                    is_mountain = false;
                }
                else
                {
                    cs          = ArtManager.s_art_manager.GetConnectionSprite(ConnectionType.MOUNTAIN);
                    is_mountain = true;
                }

                right_ct--;
            }
        }

        return(m_sprites);
    }
    public List <SpriteMarker> PlaceSprites()
    {
        if (m_sprites != null)
        {
            foreach (SpriteMarker sm in m_sprites)
            {
                GameObject.Destroy(sm.gameObject);
            }
        }

        m_sprites = new List <SpriteMarker>();
        List <SpriteMarker> all = new List <SpriteMarker>();

        foreach (ConnectionWrapMarker m in m_wraps)
        {
            //all.AddRange(m.PlaceSprites());
        }

        if (PolyBorder != null)
        {
            Vector3 max = MapBorder.s_map_border.Maxs;
            Vector3 min = MapBorder.s_map_border.Mins;
            bool    p1  = false;
            bool    p2  = false;

            if (PolyBorder.P1.x < min.x ||
                PolyBorder.P1.y < min.y ||
                PolyBorder.P1.x > max.x ||
                PolyBorder.P1.y > max.y)
            {
                m_force_mirror = true;
                m_forced_min   = PolyBorder.P1;
                m_forced_max   = PolyBorder.P1;
                p1             = true;
            }
            if (PolyBorder.P2.x < min.x ||
                PolyBorder.P2.y < min.y ||
                PolyBorder.P2.x > max.x ||
                PolyBorder.P2.y > max.y)
            {
                m_force_mirror = true;
                m_forced_min   = PolyBorder.P2;
                m_forced_max   = PolyBorder.P2;
                p2             = true;
            }

            if (p1 && p2)
            {
                m_forced_max = max + new Vector3(0.02f, 0.02f);
                m_forced_min = max - new Vector3(0.02f, 0.02f);
            }
        }

        if (m_connection.ConnectionType == ConnectionType.MOUNTAIN)
        {
            if (PolyBorder == null)
            {
                return(m_sprites);
            }

            Vector3          last   = new Vector3(-900, -900, 0);
            ConnectionSprite cs     = ArtManager.s_art_manager.GetConnectionSprite(ConnectionType.MOUNTAIN);
            ConnectionSprite bottom = ArtManager.s_art_manager.GetMountainSpecSprite();

            if (cs == null)
            {
                return(m_sprites);
            }

            int ct = 0;

            make_sprite(PolyBorder.P1, cs, Vector3.zero);
            make_sprite(PolyBorder.P1, bottom, new Vector3(0, 0.01f));
            cs = ArtManager.s_art_manager.GetConnectionSprite(ConnectionType.MOUNTAIN);
            make_sprite(PolyBorder.P2, cs, Vector3.zero);
            make_sprite(PolyBorder.P2, bottom, new Vector3(0, 0.01f));
            cs = ArtManager.s_art_manager.GetConnectionSprite(ConnectionType.MOUNTAIN);

            foreach (Vector3 pt in PolyBorder.OrderedFinePoints)
            {
                if (Vector3.Distance(last, pt) < cs.Size && ct < PolyBorder.OrderedFinePoints.Count - 3)
                {
                    ct++;
                    continue;
                }

                ct++;
                last = pt;

                make_sprite(pt, cs, Vector3.zero);
                make_sprite(pt, bottom, new Vector3(0, 0.01f));

                cs     = ArtManager.s_art_manager.GetConnectionSprite(ConnectionType.MOUNTAIN);
                bottom = ArtManager.s_art_manager.GetMountainSpecSprite();
            }
        }
        else if (m_connection.ConnectionType == ConnectionType.MOUNTAINPASS)
        {
            if (PolyBorder == null)
            {
                return(m_sprites);
            }

            Vector3          last   = new Vector3(-900, -900, 0);
            ConnectionSprite cs     = ArtManager.s_art_manager.GetConnectionSprite(ConnectionType.MOUNTAIN);
            ConnectionSprite bottom = ArtManager.s_art_manager.GetMountainSpecSprite();
            PolyBorder       other  = PolyBorder.Reversed();

            make_sprite(PolyBorder.P1, cs, Vector3.zero);
            make_sprite(PolyBorder.P1, bottom, new Vector3(0, 0.01f));
            cs     = ArtManager.s_art_manager.GetConnectionSprite(ConnectionType.MOUNTAIN);
            bottom = ArtManager.s_art_manager.GetMountainSpecSprite();
            make_sprite(PolyBorder.P2, cs, Vector3.zero);
            make_sprite(PolyBorder.P2, bottom, new Vector3(0, 0.01f));
            cs     = ArtManager.s_art_manager.GetConnectionSprite(ConnectionType.MOUNTAIN);
            bottom = ArtManager.s_art_manager.GetMountainSpecSprite();

            make_sprite(other.OrderedFinePoints[0], cs, Vector3.zero);
            make_sprite(other.OrderedFinePoints[0], bottom, new Vector3(0, 0.01f));
            cs     = ArtManager.s_art_manager.GetConnectionSprite(ConnectionType.MOUNTAIN);
            bottom = ArtManager.s_art_manager.GetMountainSpecSprite();
            make_sprite(other.OrderedFinePoints[1], cs, Vector3.zero);
            make_sprite(other.OrderedFinePoints[1], bottom, new Vector3(0, 0.01f));
            cs     = ArtManager.s_art_manager.GetConnectionSprite(ConnectionType.MOUNTAIN);
            bottom = ArtManager.s_art_manager.GetMountainSpecSprite();
            make_sprite(other.OrderedFinePoints[2], cs, Vector3.zero);
            make_sprite(other.OrderedFinePoints[2], bottom, new Vector3(0, 0.01f));
            cs     = ArtManager.s_art_manager.GetConnectionSprite(ConnectionType.MOUNTAIN);
            bottom = ArtManager.s_art_manager.GetMountainSpecSprite();

            Vector3 endpt = other.OrderedFinePoints[2];
            cs = ArtManager.s_art_manager.GetConnectionSprite(ConnectionType.MOUNTAIN);
            bool is_mountain = true;

            foreach (Vector3 pt in PolyBorder.OrderedFinePoints)
            {
                if (Vector3.Distance(pt, endpt) < 0.05f)
                {
                    break;
                }

                if (Vector3.Distance(last, pt) < cs.Size)
                {
                    continue;
                }

                last = pt;

                if (!is_mountain)
                {
                    make_sprite(pt, cs, new Vector3(0, 0.01f));
                }
                else
                {
                    make_sprite(pt, cs, Vector3.zero);
                    make_sprite(pt, bottom, new Vector3(0, 0.01f));
                }

                if (Vector3.Distance(pt, endpt) < 0.6f)
                {
                    cs          = ArtManager.s_art_manager.GetConnectionSprite(ConnectionType.MOUNTAINPASS);
                    is_mountain = false;
                }
                else
                {
                    cs          = ArtManager.s_art_manager.GetConnectionSprite(ConnectionType.MOUNTAIN);
                    is_mountain = true;
                }

                bottom = ArtManager.s_art_manager.GetMountainSpecSprite();
            }
        }
        else if (m_connection.ConnectionType == ConnectionType.SHALLOWRIVER)
        {
            int mid = Mathf.RoundToInt(PolyBorder.OrderedPoints.Count * 0.25f);

            if (UnityEngine.Random.Range(0, 2) == 0)
            {
                mid = Mathf.RoundToInt(PolyBorder.OrderedPoints.Count * 0.75f);
            }

            Vector3 pos    = PolyBorder.OrderedPoints[mid];
            Vector3 pos2   = PolyBorder.OrderedPoints[mid + 1];
            Vector3 actual = (pos - pos2).normalized;//(PolyBorder.P2 - PolyBorder.P1).normalized;

            Vector3 dir = new Vector3(-0.05f, 1f);

            if (Mathf.Abs(actual.y) < 0.3f)
            {
                ConnectionSprite cs = ArtManager.s_art_manager.GetConnectionSprite(ConnectionType.RIVER);
                SpriteMarker     s  = make_sprite(pos, cs, Vector3.zero);
            }
            else
            {
                ConnectionSprite cs = ArtManager.s_art_manager.GetConnectionSprite(ConnectionType.SHALLOWRIVER);

                if ((actual.y < 0 && actual.x > 0) || (actual.y > 0 && actual.x < 0))
                {
                    make_bridge_sprite(pos, cs, Vector3.zero, true, true);
                    dir = new Vector3(1f, 0.3f);
                }
                else
                {
                    make_bridge_sprite(pos, cs, Vector3.zero, true, false);
                    dir = new Vector3(1f, -0.3f);
                }
            }

            float          cull      = 0.05f;
            List <Vector3> positions = new List <Vector3>();

            while (cull < 0.35f)
            {
                positions.Add(pos + (dir * cull));
                positions.Add(pos + (dir * -cull));

                cull += 0.05f;
            }

            m_culling_points = positions;
        }
        else if (m_connection.ConnectionType == ConnectionType.ROAD)
        {
            if (PolyBorder == null)
            {
                return(m_sprites);
            }

            ConnectionSprite cs = ArtManager.s_art_manager.GetConnectionSprite(ConnectionType.ROAD);

            if (cs == null)
            {
                return(m_sprites);
            }

            foreach (Vector3 pt in m_poly)
            {
                make_sprite(pt, cs, new Vector3(UnityEngine.Random.Range(-0.01f, 0.01f), UnityEngine.Random.Range(-0.01f, 0.01f)));

                cs = ArtManager.s_art_manager.GetConnectionSprite(ConnectionType.ROAD);
            }
        }

        all.AddRange(m_sprites);
        return(all);
    }