Esempio n. 1
0
        public static IDMap Build( Lightmap map )
        {
            // Should be a zero zoom lightmap
            if ( map.Zoom != 0 ) throw new InvalidZoomFactorException( map.Zoom );

            // We build the map in 2048/2048 blocks
            IDMap result = new IDMap();
            IShader32 shader = new ImportIDShader();
            bool vd = map.VolatileDecompression;
            try {
                map.VolatileDecompression = true;
                for ( int y=0; y<Lightmap.BaseHeight; y+=BuildBlockSize ) {
                    int height = (y+BuildBlockSize > Lightmap.BaseHeight) ? (Lightmap.BaseHeight-y) : BuildBlockSize;

                    for ( int x=0; x<Lightmap.BaseWidth; x+=BuildBlockSize ) {
                        int width = (x+BuildBlockSize > Lightmap.BaseWidth) ? (Lightmap.BaseWidth-x) : BuildBlockSize;

                        // Get the bitmap from the lightmap
                        // And import it into ourselves
                        result.ImportBitmapBuffer( x, y, width, height, map.DecodeImage( new Rectangle( x, y, width, height ), shader ) );
                    }
                }
            }
            finally {
                map.VolatileDecompression = vd;
            }

            return result;
        }
Esempio n. 2
0
        public static IDGrid Build( IDMap idmap )
        {
            IDGrid result = new IDGrid();
            for ( int y=0; y<result.height; ++y ) {
                for ( int x=0; x<result.width; ++x ) {
                    result.BuildRegion( x, y, idmap );
                }
            }

            return result;
        }
Esempio n. 3
0
        private Lightmap( int zoom, ProvinceList provinces, AdjacencyTable adjacent, IDMap idmap )
        {
            if ( zoom < 0 ) throw new InvalidZoomFactorException( zoom );
            this.zoom = zoom;
            this.provinces = provinces;
            this.adjacent = adjacent;
            this.idmap = idmap;
            this.volatiledecompression = false;

            Size sz = CoordMap.ActualToZoomedBlocks( BaseSize );
            blocks = new GenericMapBlock[sz.Width*sz.Height];
        }
Esempio n. 4
0
        public static bool CheckAreaForOverflow( IDMap idmap, Rectangle area, out Rectangle errorArea )
        {
            int l, r, t, b;

            l = (int)Math.Floor((double)(area.Left / RegionWidth));
            t = (int)Math.Floor((double)(area.Top / RegionWidth));
            r = (int)Math.Ceiling((double)(area.Right / RegionWidth));
            b = (int)Math.Ceiling((double)(area.Bottom / RegionWidth));
            IDGrid result = new IDGrid();
            try {
                for ( int y=t; y<b; ++y ) {
                    for ( int x=l; x<r; ++x ) {
                        result.BuildRegion( x, y, idmap );
                    }
                }
                errorArea = Rectangle.Empty;
                return true;
            }
            catch ( RegionSizeOverflowException e ) {
                errorArea = e.Region;
                return false;
            }
        }
Esempio n. 5
0
 public IDMapShader( IDMap idmap, IIDConvertor idconvertor )
 {
     idc = idconvertor;
     this.idmap = idmap;
     this.diff = false;
 }
Esempio n. 6
0
 private Lightmap( int zoom, ProvinceList provinces, AdjacencyTable adjacent, IDMap idmap, BinaryReader reader )
     : this(zoom, provinces, adjacent, idmap)
 {
     ReadFrom( reader );
 }
Esempio n. 7
0
 public void Attach( ProvinceList provinces, AdjacencyTable adjacent, IDMap idmap )
 {
     this.provinces = provinces;
     this.adjacent = adjacent;
     this.idmap = idmap;
 }
Esempio n. 8
0
 public static Lightmap FromStream( int zoom, ProvinceList provinces, AdjacencyTable adjacent, IDMap idmap, BinaryReader reader )
 {
     return new Lightmap( zoom, provinces, adjacent, idmap, reader );
 }
Esempio n. 9
0
 public static IncognitaGrid Build( IDMap idmap, ProvinceList provinces )
 {
     return Build( idmap, provinces, IDGrid.Build( idmap ) );
 }
Esempio n. 10
0
        private static void WriteRegions(EU2.Map.IDGrid idgrid, EU2.Data.ProvinceList provinces, IDMap idmap, System.Xml.XmlTextWriter writer)
        {
            writer.WriteStartDocument();
            writer.WriteStartElement("IDGrid");

            for(int y=0; y<idgrid.Height; ++y ) {
                for(int x=0; x<idgrid.Width; ++x ) {
                    writer.WriteStartElement("Region");
                    writer.WriteAttributeString("index-x", x.ToString());
                    writer.WriteAttributeString("index-y", y.ToString());
                    writer.WriteAttributeString("offset-x", (x*EU2.Map.IDGrid.RegionWidth).ToString());
                    writer.WriteAttributeString("offset-y", (y*EU2.Map.IDGrid.RegionHeight).ToString());

                    ushort[] list = idgrid.GetIDs(x,y);
                    int cnt=0;
                    for(; cnt<list.Length; ++cnt) {
                        if ( list[cnt] == 0 && cnt>0 ) break;
                    }

                    if ( cnt <= 1 ) {
                        writer.WriteAttributeString("provinceCount", "0");
                        Rectangle rect = Rectangle.Empty;
                        if ( !EU2.Map.IDGrid.CheckAreaForOverflow(idmap, EU2.Map.IDGrid.GetRegionRect(x,y), out rect) ) {
                            writer.WriteStartElement("Overflow");
                            writer.WriteAttributeString("x", rect.X.ToString());
                            writer.WriteAttributeString("y", rect.Y.ToString());
                            writer.WriteAttributeString("width", rect.Width.ToString());
                            writer.WriteAttributeString("height", rect.Height.ToString());
                            writer.WriteEndElement();
                        }
                    }
                    else {
                        writer.WriteAttributeString("provinceCount", cnt.ToString());
                        for(int i=0; i<list.Length; ++i) {
                            if ( list[i] == 0 && i>0 ) break;
                            writer.WriteStartElement("Province");
                            writer.WriteAttributeString("index", i.ToString());
                            writer.WriteAttributeString("id", list[i].ToString());
                            writer.WriteAttributeString("name", provinces[list[i]].Name);
                            writer.WriteEndElement();
                        }
                    }
                    writer.WriteEndElement();
                }
            }
            writer.WriteEndElement();
            writer.WriteEndDocument();
            writer.Flush();
        }
Esempio n. 11
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 );
            }
        }
Esempio n. 12
0
 public void BuildRegion( int regionx, int regiony, IDMap idmap )
 {
     BuildRegion(regionx, regiony, idmap, false);
 }
Esempio n. 13
0
 public void BuildRegion( Point region, IDMap idmap )
 {
     BuildRegion( region.X, region.Y, idmap );
 }
Esempio n. 14
0
 public IDMapShader( IDMap idmap )
 {
     this.idmap = idmap;
     this.diff = false;
 }
Esempio n. 15
0
        private static void DoExport( Lightmap map, Rectangle region, PngLevel pngLevel, IDMap idmap, string sourcefile, string target )
        {
            Console.WriteLine( "Decoding image..." );
            RawImage rawimg = map.DecodeImage( region );
            int[] shadebuffer, idbuffer, borderbuffer;
            new IOShader( Boot.DefaultConvertor ).MultiShade32( rawimg, out shadebuffer, out idbuffer, out borderbuffer );

            // We don't use the lightmap version as a source: this can lead to errors.
            // Use the idmap as source. For lightmap2 and lightmap3, this needs to be scaled down.
            // -- NOTE: if this is changed, don't forget to turn on the id converting in the IOShader.Multishade above!
            ushort[] tmpbuffer = null;

            if ( map.Zoom == 0 ) {
                tmpbuffer = idmap.ExportBitmapBuffer( rawimg.Bounds );
            }
            else {
                Console.WriteLine( "Scaling idmap..." );
                tmpbuffer = MapToolsLib.Utils.ScaleIDBuffer( idmap.ExportBitmapGrid( map.CoordMap.ZoomedToActual( rawimg.Bounds ) ), map.Zoom );
            }

            // Convert it to the map format
            for ( int i=0; i<idbuffer.Length; ++i ) {
                if ( tmpbuffer[i] >= Province.Count ) tmpbuffer[i] = Province.TerraIncognitaID;
                idbuffer[i] = Boot.DefaultConvertor.ConvertID( tmpbuffer[i] );
            }

            if ( pngLevel != PngLevel.None ) {
                string basetarget = Path.ChangeExtension( Path.GetFileNameWithoutExtension( target ) + "-###", Path.GetExtension( target ) );

                if ((pngLevel & PngLevel.Shading) > 0) {
                    target = basetarget.Replace("###", Boot.ShadingLayerName);
                    Console.WriteLine("Exporting to \"{0}\"...", Path.GetFileName(target));
                    Visualiser.CreateImage32(shadebuffer, rawimg.Size).Save(target, ImageFormat.Png);
                }

                if ((pngLevel & PngLevel.IDs) > 0) {
                    target = basetarget.Replace("###", Boot.IDLayerName);
                    Console.WriteLine("Exporting to \"{0}\"...", Path.GetFileName(target));
                    Visualiser.CreateImage32(idbuffer, rawimg.Size).Save(target, ImageFormat.Png);
                }

                if ((pngLevel & PngLevel.Borders) > 0) {
                    target = basetarget.Replace("###", Boot.BorderLayerName);
                    Console.WriteLine("Exporting to \"{0}\"...", Path.GetFileName(target));
                    Visualiser.CreateImage32(borderbuffer, rawimg.Size).Save(target, ImageFormat.Png);
                }
            }
            else {
                Console.WriteLine( "Exporting to \"{0}\"...", Path.GetFileName( target ) );

                PSD.File psd = new PSD.File( rawimg.Size );

                Bitmap img = new MapInfo( map.Zoom, rawimg.Location, rawimg.Size, sourcefile, Path.GetFileName( target ), Boot.DefaultConvertor ).AsBitmap();
                PSD.Layer l = psd.Layers.Add( new PSD.Layer( Boot.MapInfoLayerName, 0, 0, img ) );
                l.Opacity = 255;
                l.Visible = false;
                l.ProtectTransparancy = true;

                img = Visualiser.CreateImage32( shadebuffer, rawimg.Size );
                l = psd.Layers.Add( new PSD.Layer( Boot.ShadingLayerName, 0, 0, img ) );
                l.Opacity = 254;
                l.Visible = true;
                l.ProtectTransparancy = false;

                img = Visualiser.CreateImage32( idbuffer, rawimg.Size );

                l = psd.Layers.Add( new PSD.Layer( Boot.IDLayerName, 0, 0, img ) );
                l.Opacity = 254;
                l.Mode = PSD.LayerMode.Overlay;
                l.Visible = true;
                l.ProtectTransparancy = false;

                img = Visualiser.CreateImage32( borderbuffer, rawimg.Size, true );
                l = psd.Layers.Add( new PSD.Layer( Boot.BorderLayerName, 0, 0, img ) );
                l.Opacity = 254;
                l.Visible = true;
                l.ProtectTransparancy = false;

                // -- Write the psd
                FileStream stream = null;
                try {
                    stream = new FileStream( target, FileMode.Create, FileAccess.Write, FileShare.None );
                    psd.WriteTo( new BinaryWriter( stream ) );
                }
                finally {
                    if ( stream != null ) stream.Close();
                }
            }
        }
Esempio n. 16
0
 public IDMapShader( IDMap idmap, bool diff )
 {
     this.idmap = idmap;
     this.diff = diff;
     this.idc = null;
 }
Esempio n. 17
0
 public static Lightmap CreateEmpty( int zoom, ProvinceList provinces, AdjacencyTable adjacent, IDMap idmap )
 {
     Lightmap result = new Lightmap( zoom, provinces, adjacent, idmap );
     for ( int i=0; i<result.blocks.Length; ++i ) result.blocks[i] = new MapBlock();
     return result;
 }
Esempio n. 18
0
 public static Lightmap FromFile( int zoom, ProvinceList provinces, AdjacencyTable adjacent, IDMap idmap, string path )
 {
     FileStream stream = null;
     try {
         stream = new FileStream( path, FileMode.Open, FileAccess.Read, FileShare.Read );
         return new Lightmap( zoom, provinces, adjacent, idmap, new BinaryReader( stream ) );
     }
     finally {
         if ( stream != null ) stream.Close();
     }
 }
Esempio n. 19
0
        private static void WriteRegions(EU2.Map.IDGrid idgrid, EU2.Data.ProvinceList provinces, IDMap idmap, EU2.IO.CSVWriter writer)
        {
            writer.EndRow( "region-index-x;region-index-y;region-offset-x;region-offset-x;region-provinceCount;index;provinceId;name" );

            for(int y=0; y<idgrid.Height; ++y ) {
                for(int x=0; x<idgrid.Width; ++x ) {
                    ushort[] list = idgrid.GetIDs(x,y);
                    if ( list.Length == 0 ) {
                        writer.Write(x);
                        writer.Write(y);
                        writer.Write(x*EU2.Map.IDGrid.RegionWidth);
                        writer.Write(y*EU2.Map.IDGrid.RegionHeight);
                        writer.Write(0);

                        Rectangle rect = Rectangle.Empty;
                        if ( !EU2.Map.IDGrid.CheckAreaForOverflow(idmap, EU2.Map.IDGrid.GetRegionRect(x,y), out rect) ) {
                            writer.Write("Overflow");
                            writer.Write(rect.X);
                            writer.Write(rect.Y);
                            writer.Write(rect.Width);
                            writer.Write(rect.Height);
                        }
                        writer.EndRow();
                    }
                    else {
                        int i=0;
                        for(; i<list.Length; ++i) {
                            if ( list[i] == 0 && i>0 ) break;
                        }
                        int cnt = i;

                        for(i=0; i<list.Length; ++i) {
                            if ( list[i] == 0 && i>0 ) break;
                            writer.Write(x);
                            writer.Write(y);
                            writer.Write(x*EU2.Map.IDGrid.RegionWidth);
                            writer.Write(y*EU2.Map.IDGrid.RegionHeight);
                            writer.Write(cnt);
                            writer.Write(i);
                            writer.Write(list[i]);
                            writer.Write(provinces[list[i]].Name);
                            writer.EndRow();
                        }
                    }
                }
            }
            writer.Flush();
        }
Esempio n. 20
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;
        }