Exemplo n.º 1
0
        public void BuildRegion( int regionx, int regiony, IDMap idmap, bool dontThrow )
        {
            Rectangle regionRect = new Rectangle( (regionx*RegionWidth)-256, (regiony*RegionHeight)-256, RegionWidth+512, RegionHeight+512 );
            ushort[] idlist = idmap.GetIDs( regionRect.X, regionRect.Y, regionRect.Width, regionRect.Height, GetIDOptions.SkipTI, null );
            //ushort[] idlist = idmap.GetIDs( regionx*RegionWidth, regiony*RegionHeight, RegionWidth, RegionHeight, false, false );
            if ( idlist.Length > RegionSize ) {
                if ( dontThrow ) return;
                throw new RegionSizeOverflowException( regionRect );
            }

            // Reset ids for this region
            int offset = GetOffsetFromRegion( regionx, regiony );
            for ( int i=0; i<RegionSize; ++i ) grid[offset+i] = Province.TerraIncognitaID;

            // Don't forget to sort...
            if ( idlist.Length > 0 ) {
                Array.Sort( idlist, new IDComparer() );
                if ( idlist[idlist.Length-1] == Province.MaxValue ) {
                    ushort[] idlist2 = new ushort[idlist.Length-1];
                    Array.Copy( idlist, 0, idlist2, 0, idlist2.Length );
                    idlist = idlist2;
                }
            }

            // Check again
            if ( idlist.Length == 0 )
                idlist = new ushort[] { Province.TerraIncognitaID };

            // copy the list...
            if ( idlist[0] == Province.TerraIncognitaID )
                idlist.CopyTo( grid, offset );
            else {
                //Start copying at index "1" to force a TI id in the beginning.
                if ( idlist.Length > RegionSize-1 ) throw new RegionSizeOverflowException( regionRect );  // Extra check
                idlist.CopyTo( grid, offset+1 );
            }
        }
Exemplo n.º 2
0
        public static IncognitaGrid Build( IDMap idmap, ProvinceList provinces, IDGrid idgrid )
        {
            IncognitaGrid result = new IncognitaGrid();

            double scansize = (double)(Lightmap.BlockSize << 1);
            double scanfactor = 256.0 / Math.Sqrt( (scansize/2.0) * (scansize/2.0) );

            Random rnd = new Random();
            int halfscan = (int)(scansize/2);
            for( int y=0; y<Lightmap.BaseHeight; y+=Lightmap.BlockSize ) {
                for( int x=0; x<Lightmap.BaseWidth; x+=Lightmap.BlockSize ) {
                    ushort[] list = idmap.GetIDs( x-halfscan, y-halfscan, (int)scansize, (int)scansize, GetIDOptions.SkipTI | GetIDOptions.SkipRivers, provinces );

                    if ( list.Length > 1 ) {
                        double[] distances;
                        ushort[] idlist;
                        int weight;

                        idmap.FindNearest( x, y, (int)scansize, 4, provinces, out idlist, out distances );
                        for ( int i=0; i<4; ++i ) {
                            if ( idlist[i] > Province.MaxID ) idlist[i] = Province.TerraIncognitaID;
                            weight = (int)(distances[i]*scanfactor);
                            weight = weight - 15 + (rnd.Next(0x7fff) & 31);
                            if ( weight < 0 ) weight = 0;
                            else if ( weight > 255 ) weight = 255;

                            result.grid[x >> Lightmap.BlockFactor,y >> Lightmap.BlockFactor].data[i].id = idgrid.MapID( x, y, idlist[i] );
                            result.grid[x >> Lightmap.BlockFactor,y >> Lightmap.BlockFactor].data[i].weight = (byte)weight;
                        }
                    }
                    else {
                        ushort id =  Province.TerraIncognitaID;
                        if ( list.Length > 0 ) {
                            id = list[0];
                            if ( id > Province.MaxID ) id = Province.TerraIncognitaID;
                        }

                        result.grid[x >> Lightmap.BlockFactor, y >> Lightmap.BlockFactor].data[0].id = idgrid.MapID( x, y, id );
                        result.grid[x >> Lightmap.BlockFactor, y >> Lightmap.BlockFactor].data[0].weight = 0;

                        result.grid[x >> Lightmap.BlockFactor, y >> Lightmap.BlockFactor].data[1].id = 0;
                        result.grid[x >> Lightmap.BlockFactor, y >> Lightmap.BlockFactor].data[1].weight = 255;

                        result.grid[x >> Lightmap.BlockFactor, y >> Lightmap.BlockFactor].data[2].id = 0;
                        result.grid[x >> Lightmap.BlockFactor, y >> Lightmap.BlockFactor].data[2].weight = 255;

                        result.grid[x >> Lightmap.BlockFactor, y >> Lightmap.BlockFactor].data[3].id = 0;
                        result.grid[x >> Lightmap.BlockFactor, y >> Lightmap.BlockFactor].data[3].weight = 255;
                    }
                }
            }

            return result;
        }