better console handling // simple map serialzer
[fishladder.git] / fishladder.c
index 0c07ddf56bc0e78f03b8180623f0b4d170602841..4817bdf9a2a20004f7795879f8f96c918fe4b910 100644 (file)
@@ -383,13 +383,72 @@ static struct cell *pcell( v2i pos )
        return &world.data[ pos[1]*world.w + pos[0] ];
 }
 
+static void map_serialize( FILE *stream )
+{
+       for( int y = 0; y < world.h; y ++ )
+       {
+               for( int x = 0; x < world.w; x ++ )
+               {
+                       struct cell *cell = pcell( (v2i){ x, y } );
+                       
+                       if( cell->state & FLAG_WALL ) fputc( '#', stream );
+                       else if( cell->state & FLAG_INPUT ) fputc( '+', stream );
+                       else if( cell->state & FLAG_OUTPUT ) fputc( '-', stream );
+                       else if( cell->state & FLAG_CANAL ) fputc( '*', stream );
+                       else fputc( ' ', stream );
+               }
+               
+               fputc( ';', stream );
+               
+               int terminal_write_count = 0;
+               
+               for( int x = 0; x < world.w; x ++ )
+               {
+                       for( int i = 0; i < arrlen( world.io ); i ++ )
+                       {
+                               struct cell_terminal *term = &world.io[i];
+                               if( term->id == y*world.w+x )
+                               {
+                                       if( terminal_write_count )
+                                               fputc( ',', stream );
+                                       terminal_write_count ++;
+                               
+                                       for( int j = 0; j < arrlen( term->conditions ); j ++ )
+                                               fputc( term->conditions[j], stream );
+                               }
+                       }
+               }
+               
+               fputc( '\n', stream );
+       }
+}
+
 int main( int argc, char *argv[] )
 {
        vg_init( argc, argv, "Fish (Marbles Computer) Ladder Simulator 2022 | N,M: change level | SPACE: Test | LeftClick: Toggle tile" );
 }
 
+static void console_save_map( int argc, char *argv[] )
+{
+       if( argc >= 1 )
+       {
+               FILE *test_writer = fopen( argv[0], "w" );
+               if( test_writer )
+               {
+                       map_serialize( test_writer );
+                       
+                       fclose( test_writer );
+               }
+       }
+}
+
 void vg_start(void)
 {
+       vg_function_push( (struct vg_cmd){
+               .name = "save_map",
+               .function = console_save_map
+       });
+
        // Quad mesh
        {
                float quad_mesh[] =