Discuss and request scripts and script accessories.
Site Admin
Post Apr 09, 2017 17:47
This is the source code to the CSV exporter available in v4.0.4 Pro Edition.

Execution starts in function 'main' at the bottom.

Code: Select all

//
// Comma Separated Values Export
//
// Exports the current tile map plane as a .CSV file.
//
// by David Walters
//
// Version 1.0, 2017/04/09
//

function write_output_row( buffer, bound, y )
{
	// For each column
	for ( var x = bound.min_x; x <= bound.max_x; ++x )
	{
		// Select the tile
		GCTile.Select( x, y );

		// Get it
		var tile = GCTile.Get();

		// Empty?
		if ( tile != null )
		{
			GCBuffer.Print( buffer, tile.Index );
		}

		// End of the row?
		if ( x == bound.max_x )
		{
			// New-line
			GCBuffer.Write8( buffer, '\n' );
		}
		else
		{			
			// Comma!
			GCBuffer.Write8( buffer, ',' );
		}
	}
}

function create_output( buffer, bound, origin )
{
	// Iterate over the bounding box. Max values are inclusive so need <=
	
	var height = bound.max_y - bound.min_y + 1;
	
	// Bottom-Left Origin?
	if ( origin == "bl" )
	{
		// Bottom-Left. Iterate each row, in reverse
		for ( var y = bound.max_y; y >= bound.min_y; --y )
		{
			write_output_row( buffer, bound, y );
			
			var pct = 100 - ( y * 100 / height );
			
			GCStatus.Progress( pct );
		}
	}
	else
	{
		// Top-Left. Iterate each row
		for ( var y = bound.min_y; y <= bound.max_y; ++y )
		{
			write_output_row( buffer, bound, y );
			
			var pct = ( y * 100 / height );
			
			GCStatus.Progress( pct );
		}
	}
	
	GCStatus.Progress( 100 );
}

function export_buffer( buffer )
{
	// Export
	var result;
	result = GCExport.BufferAs( buffer, 
								"Export Tile Map as CSV...", 
								null, 
								"csv", 
								"Comma-Separated Values" );
	
	// Error?
	switch ( result )
	{
	
	case GCOK:
		GCStatus.Message( "Export Complete." );
		GCUI.MessageBox( "Export Complete.", 
						 GCMB_OK | GCMB_INFO );
		break;
		
	case GCERR_ABORT:
		GCStatus.Message( "Export Cancelled." );
		break;

	case GCERR_ACCESS:
		GCStatus.Message( "Export Failed." );
		GCUI.MessageBox( "Export Failed. Cannot write to the file.", 
						 GCMB_OK | GCMB_STOP );
		break;

	default:
		GCStatus.Message( "Export Failed." );
		GCUI.MessageBox( "Export Failed. Internal Error.", 
						 GCMB_OK | GCMB_STOP );
		break;
		
	}
}

function main()
{
	// Get the origin
	var origin = GCRegion.Origin();
	
	// Error?
	if ( origin == null )
	{
		GCUI.MessageBox( "Export Failed. Internal Error.", 
						 GCMB_OK | GCMB_STOP );
		return;
	}
	
	// Begin blocking mode
	GCKernel.SetBlockingMode( true );

	// Get the bound
	var bound = GCFloor.FindBound();
	
	// Detect empty
	if ( bound == null )
	{
		GCUI.MessageBox( "Nothing to Export.", GCMB_OK | GCMB_INFO );
		return;
	}
	
	// Exporting
	GCStatus.Message( "Exporting to .CSV" );
	
	GCStatus.Progress( 0 );
	
	// Create a buffer
	var buffer = GCBuffer.Create();
	
	// Create
	create_output( buffer, bound, origin );
	
	// End blocking mode
	GCKernel.SetBlockingMode( false );
	
	// Export
	export_buffer( buffer );
	
	GCStatus.Progress( null );
	
	// Destroy buffer
	GCBuffer.Destroy( buffer );
}