private bool Generate( TestMode testmode ) { string seltypename = (string)this.RoofType.SelectedItem; foreach ( TileSet ts in this.TileSets ) { if ( seltypename == ts.Name ) { this.tileset = ts; break; } } short[] roofids = new short[ this.roofimage.Width * this.roofimage.Height ]; int i; bool fail = false; for ( i = 0; i < this.roofimage.Width * this.roofimage.Height; i++ ) { if ( (short)this.roofimage.Data[i] < 0 ) this.roofimage.Data[i] = (short)( - (short)this.roofimage.Data[i] ); } for ( i = 0; i < this.roofimage.Width * this.roofimage.Height ; i++ ) { if ( (short)this.roofimage.Data[i] == (short) 0 ) roofids[i] = 0; else { uint flags; flags = GetFlags( MakeLine( i - this.roofimage.Width ), MakeLine( i ), MakeLine( i + this.roofimage.Width ) ); roofids[i] = LookupID(flags); if ( roofids[i] == 0 ) { this.roofimage.Data[i] = (short)( - ( (short) this.roofimage.Data[i] ) ); fail = true; } if ( testmode != TestMode.NoTest ) { bool corner = !((flags & ~0x88878778) != 0) || !((flags & ~0x88887877) != 0) || !((flags & ~0x77878888) != 0) || !((flags & ~0x87787888) != 0) || !((flags & ~0x87777777) != 0) || !((flags & ~0x77877777) != 0) || !((flags & ~0x77777877) != 0) || !((flags & ~0x77777778) != 0); if ( testmode == TestMode.Test && !corner ) roofids[i] = 0; if ( testmode == TestMode.Rest && corner ) roofids[i] = 0; } } } if ( fail ) { // Redraw image this.roofimage.MakeBitmap(100, 100); this.Preview.Image = this.roofimage.Img; if ( MessageBox.Show( this, "This tileset cannot generate the roof you requested. Missing pieces are marked in red. Would you like to generate this roof anyway?", "Generation failed", MessageBoxButtons.YesNo, MessageBoxIcon.Question ) == DialogResult.No ) { return false; } } int j = 0; int p = 0; int dx; int dy = 0; int tilex = 0; int tiley = 0; int tilew = 0; int tileh = 0; int tilez = 0; int tileid = 0; string cmd; for ( j = 0; j < this.roofimage.Height; j++ ) { for ( i = 0; i < this.roofimage.Width; i++, p++ ) { if ( roofids[p] == 0 ) continue; for ( dx = 1; dx + i < this.roofimage.Width; dx++ ) { if ( ( ((short)roofids[ p + dx ]) != roofids[ p ] ) || ( ((short)roofimage.Data[p]) != ((short)roofimage.Data[p + dx]) ) ) break; } for ( dy = 1; dy + j < this.roofimage.Height; dy++ ) { if ( ( ((short)roofids[ p + roofimage.Width * dy ]) != roofids[p] ) || ( ((short) roofimage.Data[p]) != ((short)roofimage.Data[p + roofimage.Width * dy]) ) ) break; } dx--; dy--; tilez = z + ( 3 * (short)roofimage.Data[p] ) - 3; tileid = roofids[p]; if ( ( dx > 0 ) || ( dy > 0 ) ) { tilex = BasePoint.X + i; tiley = BasePoint.Y + j; if ( dy > dx ) { tilew = 1; tileh = dy + 1; while ( dy >= 0 ) { roofids[p + roofimage.Width * dy ] = 0; dy--; } } else { tilew = dx + 1; tileh = 1; while ( dx >= 0 ) { roofids[ p + dx ] = 0; dx--; } } i += dx; p += dx; } else { tilex = BasePoint.X + i; tiley = BasePoint.Y + j; tilew = 1; tileh = 1; } // Ok now send cmd = string.Format( "TileXYZ {0} {1} {2} {3} {4} static {5}\n", tilex, tiley, tilew, tileh, tilez, tileid ); SendToUO( cmd ); } } return true; }
private void ReadRoofTiles() { // Read TheBox.Embedded.rooftiles.cfg System.Reflection.Assembly theExe = this.GetType().Assembly; System.IO.Stream theStream = null; try { theStream = theExe. GetManifestResourceStream( "TheBox.Embedded.rooftiles.cfg" ); } catch ( Exception ) { throw new Exception("The roofing tab could not find the rooftiles.cfg component"); } // Get a stream reader System.IO.StreamReader sr = new System.IO.StreamReader( theStream ); string line; // Create new tileset TileSet ts = null; while ( ( line = sr.ReadLine() ) != null ) { if ( line.Length == 0 ) continue; // Line to process if ( line[0] == '[' ) { // New tileset starts here ts = new TileSet(); string name = ""; // Remove the first [ line = line.Substring( 1 ); while ( line[0] != ']' ) { name += line[0]; line = line.Substring( 1 ); } ts.Name = name; this.TileSets.Add( ts ); } else { if ( ts != null ) { // We have a tile set to add to TileMask tm = new TileMask(); // If we have a valid tile line add it to the set if ( tm.FromLine( line ) ) ts.Tiles.Add( tm ); } } } // Close streams sr.Close(); theStream.Close(); }