public bool TryGetValue(int x, int z, out HeightMapInfo[] heights, out int heightIndex)
        {
            int fx = x >> 7;
            int fz = z >> 7;

            heightIndex = ((z - (fz << 7)) << 7) + (x - (fx << 7));
            int key = ((fz + 1024) << 16) + (fx + 1024);

            if (key != lastKey || lastSector < 0)
            {
                int poolIndex;
                if (!sectorsDict.TryGetValue(key, out poolIndex) || key != sectorsPool[poolIndex].key)
                {
                    int leastUsed = int.MaxValue;
                    for (int k = 0; k < sectorsPool.Length; k++)
                    {
                        if (sectorsPool[k].uses < leastUsed)
                        {
                            leastUsed = sectorsPool[k].uses;
                            poolIndex = k;
                        }
                    }

                    // free entry from dictionary
                    HeightMapInfoPoolEntry sector = sectorsPool[poolIndex];
                    if (sector.key > 0)
                    {
                        sectorsDict.Remove(sector.key);
                    }

                    // set new key and add to dictionary
                    sector.key       = key;
                    sector.uses      = 0;
                    sectorsDict[key] = poolIndex;

                    // alloc buffer if it's the first time
                    if (sector.heights == null)
                    {
                        sector.heights = new HeightMapInfo[16384];
                    }
                    else
                    {
                        for (int k = 0; k < sector.heights.Length; k++)
                        {
                            sector.heights[k].biome       = null;
                            sector.heights[k].moisture    = 0;
                            sector.heights[k].groundLevel = 0;
                        }
                    }
                }
                lastKey    = key;
                lastSector = poolIndex;
            }

            HeightMapInfoPoolEntry theSector = sectorsPool[lastSector];

            theSector.uses++;
            heights = theSector.heights;
            return(heights[heightIndex].groundLevel != 0);
        }
        public bool TryGetValue(int x, int z, out HeightMapInfo[] Heights, out int heightIndex)
        {
            int fx = x >> 7;
            int fz = z >> 7;

            heightIndex = ((z - (fz << 7)) << 7) + (x - (fx << 7));
            int Key = ((fz + 1024) << 16) + (fx + 1024);

            if (Key != mLastKey || mLastSector < 0)
            {
                if (!mSectors.TryGetValue(Key, out int poolIndex) || Key != mSectorsPool[poolIndex].Key)
                {
                    int leastUsed = int.MaxValue;
                    for (int k = 0; k < mSectorsPool.Length; k++)
                    {
                        if (mSectorsPool[k].Uses < leastUsed)
                        {
                            leastUsed = mSectorsPool[k].Uses;
                            poolIndex = k;
                        }
                    }

                    HeightMapInfoPoolEntry sector = mSectorsPool[poolIndex];
                    if (sector.Key > 0)
                    {
                        mSectors.Remove(sector.Key);
                    }

                    sector.Key    = Key;
                    sector.Uses   = 0;
                    mSectors[Key] = poolIndex;

                    if (sector.Heights == null)
                    {
                        sector.Heights = new HeightMapInfo[16384];
                    }
                    else
                    {
                        for (int k = 0; k < sector.Heights.Length; k++)
                        {
                            sector.Heights[k].Biome       = null;
                            sector.Heights[k].Moisture    = 0;
                            sector.Heights[k].GroundLevel = 0;
                        }
                    }
                }
                mLastKey    = Key;
                mLastSector = poolIndex;
            }

            HeightMapInfoPoolEntry theSector = mSectorsPool[mLastSector];

            theSector.Uses++;
            Heights = theSector.Heights;
            return(Heights[heightIndex].GroundLevel != 0);
        }
 public HeightMapCache(int poolSize)
 {
     sectorsDict = new FastHashSet <int>(16);
     sectorsPool = new HeightMapInfoPoolEntry[poolSize];
     for (int k = 0; k < sectorsPool.Length; k++)
     {
         sectorsPool[k] = new HeightMapInfoPoolEntry();
     }
     lastSector = -1;
 }
        public HeightMapCache(int poolSize)
        {
            mSectors     = new FastHashSet <int>(16);
            mSectorsPool = new HeightMapInfoPoolEntry[poolSize];
            for (int i = 0; i < mSectorsPool.Length; i++)
            {
                mSectorsPool[i] = new HeightMapInfoPoolEntry();
            }

            mLastSector = -1;
        }