only rebuild map chunks where user clicks
[fishladder.git] / fishladder.c
index 6d6486be3cbb1741602233b1a25505594dfb1141..eb86953ebe0d5ce3d90fb749d7ae99894d06477b 100644 (file)
@@ -193,10 +193,12 @@ sfx_system_t audio_system_sfx =
 sfx_set_t audio_tile_mod = 
 {
  .sources = "\
-sound/mod00.ogg\0\
-sound/mod01.ogg\0\
-sound/mod02.ogg\0\
-sound/mod03.ogg\0",
+sound/mod_01.ogg\0\
+sound/mod_02.ogg\0\
+sound/mod_03.ogg\0\
+sound/mod_04.ogg\0\
+sound/mod_05.ogg\0\
+sound/mod_06.ogg\0",
  .flags = 0
 };
 
@@ -208,10 +210,6 @@ m3x3f m_mdl;
 #define FLAG_OUTPUT 0x2
 #define FLAG_CANAL 0x4
 #define FLAG_WALL 0x8
-#define FLAG_DROP_L 0x10
-#define FLAG_SPLIT 0x20
-#define FLAG_MERGER 0x40
-#define FLAG_DROP_R 0x80
 #define FLAG_FLIP_FLOP 0x100
 #define FLAG_FLIP_ROTATING 0x200
 
@@ -356,6 +354,7 @@ static void map_free(void)
        world.io = NULL;
 }
 
+static void map_reclassify( v2i start, v2i end );
 static int map_load( const char *str )
 {
        map_free();
@@ -472,6 +471,7 @@ static int map_load( const char *str )
                c ++;
        }
        
+       map_reclassify( NULL, NULL );
        vg_success( "Map loaded! (%u:%u)\n", world.w, world.h );
        return 1;
 }
@@ -673,6 +673,41 @@ static int cell_interactive( v2i co )
        return 1;
 }
 
+static void map_reclassify( v2i start, v2i end )
+{
+       v2i full_start = { 2,2 };
+       v2i full_end = { world.w-2, world.h-2 };
+       
+       if( !start || !end )
+       {
+               start = full_start;
+               end = full_end;
+       }
+
+       for( int y = vg_max( start[1], full_start[1] ); y < vg_min( end[1], full_end[1] ); y ++ )
+       {
+               for( int x = vg_max( start[0], full_start[0] ); x < vg_min( end[0], full_end[0] ); x ++ )
+               {
+                       v2i dirs[] = {{1,0},{0,1},{-1,0},{0,-1}};
+                       
+                       u8 config = 0x00;
+                       
+                       if( pcell((v2i){x,y})->state & FLAG_CANAL )
+                       {
+                               for( int i = 0; i < vg_list_size( dirs ); i ++ )
+                               {
+                                       struct cell *neighbour = pcell((v2i){x+dirs[i][0], y+dirs[i][1]});
+                                       if( neighbour->state & (FLAG_CANAL|FLAG_INPUT|FLAG_OUTPUT) )
+                                               config |= 0x1 << i;
+                               }
+                       } 
+                       else config = 0xF;
+                       
+                       pcell((v2i){x,y})->config = config;
+               }
+       }
+}
+
 void vg_update(void)
 {
        static int curlevel = 0;
@@ -728,7 +763,13 @@ void vg_update(void)
                        if( vg_get_button_down("primary") )
                        {
                                world.data[ world.selected ].state ^= FLAG_CANAL;
-                               sfx_set_playrnd( &audio_tile_mod, &audio_system_sfx );
+                               
+                               if( world.data[ world.selected ].state & FLAG_CANAL )
+                                       sfx_set_playrnd( &audio_tile_mod, &audio_system_sfx, 3, 6 );
+                               else
+                                       sfx_set_playrnd( &audio_tile_mod, &audio_system_sfx, 0, 3 );
+                                       
+                               map_reclassify( (v2i){ tile_x -2, tile_y -2 }, (v2i){ tile_x +2, tile_y +2 } );
                        }
                }
                else
@@ -767,56 +808,7 @@ void vg_update(void)
                                world.io[i].recv_count = 0;
                }
        }
-       
-       // Simulation stuff
-       // ========================================================
-       
-       for( int y = 2; y < world.h-2; y ++ )
-       {
-               for( int x = 2; x < world.w-2; x ++ )
-               {
-                       v2i dirs[] = {{1,0},{0,1},{-1,0},{0,-1}};
-                       
-                       u8 config = 0x00;
-                       
-                       if( pcell((v2i){x,y})->state & FLAG_CANAL )
-                       {
-                               for( int i = 0; i < vg_list_size( dirs ); i ++ )
-                               {
-                                       struct cell *neighbour = pcell((v2i){x+dirs[i][0], y+dirs[i][1]});
-                                       if( neighbour->state & (FLAG_CANAL|FLAG_INPUT|FLAG_OUTPUT) )
-                                               config |= 0x1 << i;
-                               }
-                       } else config = 0xF;
-                       
-                       pcell((v2i){x,y})->config = config;
-                       pcell((v2i){x,y})->state &= ~(FLAG_DROP_L|FLAG_DROP_R|FLAG_SPLIT|FLAG_MERGER);
-               }
-       }
-       
-       for( int y = 2; y < world.h-2; y ++ )
-               for( int x = 2; x < world.w-2; x ++ )
-               {
-                       // R,D,L,-   1110 (splitter, 1 drop created)
-                       
-                       // R,-,L,U - 1011 (merger, 2 drop created)
-                       
-                       u8 config = pcell((v2i){x,y})->config;
-                       
-                       if( config == k_cell_type_split ) // splitter
-                       {
-                               struct cell *cell = pcell((v2i){x,y});
-                       
-                               cell->state |= (FLAG_SPLIT | FLAG_DROP_L | FLAG_DROP_R);
-                       }
-                       else if( config == k_cell_type_merge )
-                       {
-                               world.data[y*world.w+x-1].state |= FLAG_DROP_R;
-                               world.data[y*world.w+x+1].state |= FLAG_DROP_L;
-                               world.data[y*world.w+x].state |= FLAG_MERGER;
-                       }
-               }
-               
+
        // Fish ticks
        if( world.simulating )
        {
@@ -895,7 +887,7 @@ void vg_update(void)
                                        continue;
                                }
                                
-                               if( cell_current->state & FLAG_SPLIT )
+                               if( cell_current->config == k_cell_type_split )
                                {
                                        // Flip flop L/R
                                        fish->dir[0] = cell_current->state&FLAG_FLIP_FLOP?1:-1;
@@ -903,7 +895,7 @@ void vg_update(void)
                                        
                                        cell_current->state ^= FLAG_FLIP_FLOP;
                                }
-                               else if( cell_current->state & FLAG_MERGER )
+                               else if( cell_current->config == k_cell_type_merge )
                                {
                                        // Can only move up
                                        fish->dir[0] = 0;
@@ -1143,7 +1135,7 @@ void vg_render(void)
                {
                        struct cell *cell = pcell((v2i){x,y});
                        
-                       if( cell->state & FLAG_SPLIT )
+                       if( cell->config == k_cell_type_split )
                        {
                                float rotation = cell->state & FLAG_FLIP_FLOP? vg_rad( -45.0f ): vg_rad( 45.0f );