refactor cell classification
authorhgn <hgodden00@gmail.com>
Sat, 23 Oct 2021 19:17:35 +0000 (20:17 +0100)
committerhgn <hgodden00@gmail.com>
Sat, 23 Oct 2021 19:17:35 +0000 (20:17 +0100)
fishladder.c

index b8fbb2a452fb3a93ce78358b5f2305c76a8fdb40..54e1a537926a36f78a3c4adf5a8e719233b81196 100644 (file)
@@ -210,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
 
@@ -675,6 +671,35 @@ static int cell_interactive( v2i co )
        return 1;
 }
 
+// Entire world: 2 -> worldx/y-2
+
+static void fl_world_update( v2i start, v2i end )
+{
+       for( int y = start[1]; y < end[1]; y ++ )
+       {
+               for( int x = start[0]; x < 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;
@@ -773,56 +798,10 @@ void vg_update(void)
                                world.io[i].recv_count = 0;
                }
        }
+
+       // There was world reconfiguarion here previously...
+       fl_world_update( (v2i){2,2}, (v2i){world.w-2,world.h-2} );
        
-       // 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 )
        {
@@ -901,7 +880,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;
@@ -909,7 +888,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;
@@ -1149,7 +1128,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 );