+#define CELL_FLAG_INPUT 0x1
+#define CELL_FLAG_OUTPUT 0x2
+ #define CELL_FLAG_IO (CELL_FLAG_INPUT|CELL_FLAG_OUTPUT)
+#define CELL_FLAG_WALL 0x4
+#define CELL_FLAG_HOVER 0x8
+#define CELL_FLAG_ITER 0x10
+#define CELL_FLAG_CANAL 0x20
+#define CELL_FLAG_SPLIT 0x40 /* Does this cell split and have an incoming vertical connection? */
+#define CELL_FLAG_WALKABLE (CELL_FLAG_IO|CELL_FLAG_CANAL)
+#define CELL_FLAG_VISITED 0x80
+#define CELL_FLAG_UPLVL 0x100
+#define CELL_FLAG_MERGE 0x200
+
+static struct
+{
+ u32 x,y;
+
+ struct cell
+ {
+ u32 flags;
+ u32 model_id;
+
+ char *conditions;
+
+ int level;
+ int state;
+ int flowing[2];
+ int source_count;
+ }
+ * cells;
+
+ struct fish
+ {
+ int alive;
+ int co[2];
+ int dir[2];
+ char data;
+ }
+ fishes[ 20 ];
+ int num_fishes;
+
+ vec3 origin;
+ struct cell *selected;
+ int select_valid;
+ int playing;
+ u32 frame;
+
+ u32 *io;
+
+ struct vstack
+ {
+ struct vframe
+ {
+ int x, y;
+ int i;
+ }
+ frames[ 64 ];
+
+ int level;
+ u32 flags;
+ }
+ stack;
+
+ GLuint tile_texture;
+ GLuint flow_texture;
+
+ GLuint tiles_vao;
+ GLuint tiles_vbo;
+}
+map;
+
+static void map_free(void)
+{
+ for( int i = 0; i < arrlen( map.io ); i ++ )
+ {
+ arrfree( map.cells[ map.io[i] ].conditions );
+ }
+
+ arrfree( map.cells );
+ arrfree( map.io );
+ map.x = 0;
+ map.y = 0;
+ map.cells = NULL;
+ map.io = NULL;
+}
+
+static struct cell *map_tile( int pos[2] )
+{
+ return map.cells + pos[1]*map.x + pos[0];
+}
+
+static struct cell *map_tile_at( int pos[2] )
+{
+ if( pos[0] >= 0 && pos[0] < map.x && pos[1] >= 0 && pos[1] < map.y )
+ return map.cells + pos[1]*map.x + pos[0];
+ return NULL;
+}
+
+static struct cell *map_tile_at_cond( int pos[2], u32 flags )
+{
+ struct cell *cell = map_tile_at( pos );
+ if( cell && (cell->flags & flags) )
+ return cell;
+
+ return NULL;
+}
+
+static void map_tile_coords_from_index( int i, int coords[2] )
+{
+ coords[0] = i % map.x;
+ coords[1] = (i - coords[0])/map.x;
+}
+
+static void map_stack_refresh(void)
+{
+ for( int i = 0; i < map.x*map.y; i ++ )
+ map.cells[i].flags &= ~CELL_FLAG_VISITED;
+}
+
+static void map_stack_init( int coords[2] )
+{
+ map.stack.level = 0;
+ map.stack.frames[0].i = 0;
+ map.stack.frames[0].x = coords[0];
+ map.stack.frames[0].y = coords[1];
+}
+
+static struct cell *map_stack_next(void)
+{
+ struct cell *tile = NULL;
+
+ while( !tile )
+ {
+ struct vframe *frame = &map.stack.frames[ map.stack.level ];
+
+ int output_dirs[][2] = { {0,1}, {-1,0}, {1,0} };
+
+ if( frame->i < 3 )
+ {
+ int *dir = output_dirs[ frame->i ];
+ tile = map_tile_at_cond( (int[2]){frame->x+dir[0], frame->y+dir[1]}, CELL_FLAG_WALKABLE );
+
+ if( tile && !(tile->flags & CELL_FLAG_VISITED) )
+ {
+ map.stack.level ++;
+ frame[1].i = 0;
+ frame[1].x = frame[0].x+dir[0];
+ frame[1].y = frame[0].y+dir[1];
+
+ tile->flags |= CELL_FLAG_VISITED;
+
+ if( frame->i == 0 )
+ frame->i = 400;
+ }
+ else
+ tile = NULL;
+
+ frame->i ++;
+ }
+ else
+ {
+ map.stack.level --;
+ tile = NULL;
+
+ if( map.stack.level < 0 )
+ return NULL;
+ }
+ }
+
+ return tile;
+}
+
+static void map_stack_current_coords( int coords[2], int offset )
+{
+ coords[0] = map.stack.frames[ map.stack.level+offset ].x;
+ coords[1] = map.stack.frames[ map.stack.level+offset ].y;
+}
+
+static void map_update_visual(void)
+{
+ u8 celldata[ 4096 ];
+
+ static int compute_flow_counter = 0;
+ compute_flow_counter ^= 0x1;
+
+ for( int i = 0; i < map.y*map.x; i ++ )
+ {
+ struct cell *cell = map.cells + i;
+
+ cell->flowing[compute_flow_counter] = cell->flowing[compute_flow_counter^0x1];
+
+ if( !map.cells[i].source_count )
+ cell->flowing[ compute_flow_counter ] = vg_max( cell->flowing[ compute_flow_counter ] - 10, 0 );
+
+ map.cells[i].source_count = 0;
+ }
+
+ for( int y = 0; y < map.y; y ++ )
+ {
+ for( int x = 0; x < map.x; x ++ )
+ {
+ struct cell *cur = map.cells + y*map.x + x;
+ u8 *cellbytes = celldata + (y*map.x+x)*4;
+
+ if( cur->flags & CELL_FLAG_WALKABLE )
+ {
+ struct cell *a, *b, *c, *d;
+
+ a = map_tile_at_cond( (int[2]){ x,y+1 }, CELL_FLAG_WALKABLE );
+ b = map_tile_at_cond( (int[2]){ x+1,y }, CELL_FLAG_WALKABLE );
+ c = map_tile_at_cond( (int[2]){ x,y-1 }, CELL_FLAG_WALKABLE );
+ d = map_tile_at_cond( (int[2]){ x-1,y }, CELL_FLAG_WALKABLE );
+
+ u32 config = (a?0x1:0x0) | (b?0x2:0x0) | (c?0x4:0x0) | (d?0x8:0x0);
+ cellbytes[ 0 ] = config;
+
+ if( cur->flags & CELL_FLAG_OUTPUT )
+ cur->flowing[ compute_flow_counter ] = 128;
+ }
+ else
+ {
+ // TODO: Random background tiles
+ cellbytes[ 0 ] = 1;
+ }
+ }
+ }
+
+ int const k_rate_flow = 16;
+
+ map_stack_refresh();
+ for( int i = 0; i < arrlen( map.io ); i ++ )
+ {
+ int inputcoord[2];
+ if( map.cells[ map.io[i] ].flags & CELL_FLAG_OUTPUT )
+ {
+ map_tile_coords_from_index( map.io[i], inputcoord );
+ map_stack_init( inputcoord );
+ struct cell *cell = map_tile_at( inputcoord );
+
+ int cr[2];
+
+ do
+ {
+ map_stack_current_coords( cr, 0 );
+
+ u8 *cellbytes = celldata + (cr[1]*map.x+cr[0])*4;
+
+ int outflow = cell->flowing[ compute_flow_counter ^ 0x1 ];
+ int sourcing = outflow? 1: 0;
+ int outrate = outflow == 128? k_rate_flow: 0;
+
+ struct cell *a, *b, *d;
+ a = map_tile( (int[2]){ cr[0], cr[1]+1 } );
+ b = map_tile( (int[2]){ cr[0]+1, cr[1] } );
+ d = map_tile( (int[2]){ cr[0]-1, cr[1] } );
+
+ int compute_l = 0, compute_r = 0;
+
+ switch( cellbytes[0] )
+ {
+ // DOWN
+ case 1: case 5: case 11: case 9: case 3:
+ a->flowing[ compute_flow_counter ] += outrate;
+ a->source_count += sourcing;
+ break;
+ case 2: case 6: compute_r = 1; break;
+ case 8: case 12: compute_l = 1; break;
+ case 10: case 14: compute_l = 1; compute_r = 1; break;
+ default: break;
+ }
+
+ if( compute_r && (celldata[ (cr[1]*map.x+cr[0]+1)*4 ] != 14) )
+ {
+ b->flowing[ compute_flow_counter ] += outrate;
+ b->source_count += sourcing;
+ }
+
+ if( compute_l && (celldata[ (cr[1]*map.x+cr[0]-1)*4 ] != 14) )
+ {
+ d->flowing[ compute_flow_counter ] += outrate;
+ d->source_count += sourcing;
+ }
+
+ if( cellbytes[0] == 10 )
+ {
+ int crl[2];
+ map_stack_current_coords( crl, -1 );
+
+ if( crl[0] < cr[0] )
+ {
+ cellbytes[0] = 15;
+ }
+ }
+ }
+ while( (cell = map_stack_next()) );
+ }
+ }
+
+ for( int i = 0; i < map.y*map.x; i ++ )
+ {
+ map.cells[i].flowing[ compute_flow_counter ] = vg_min( 128, map.cells[i].flowing[ compute_flow_counter ] );
+ celldata[ i*4+2 ] = map.cells[i].flowing[ compute_flow_counter ];
+ }
+
+ glBindBuffer( GL_ARRAY_BUFFER, map.tiles_vbo );
+ glBufferSubData( GL_ARRAY_BUFFER, 16*sizeof(float) + 1024*2*sizeof(float), map.x*map.y*4, celldata );
+}
+
+static int map_load( const char *str )
+{
+ map_free();
+
+ char *c = str;
+
+ // Scan for width
+ for(;; map.x ++)
+ {
+ if( str[map.x] == ';' )
+ break;
+ else if( !str[map.x] )
+ {
+ vg_error( "Unexpected EOF when parsing level!\n" );
+ return 0;
+ }
+ }
+
+ struct cell *row = arraddnptr( map.cells, map.x );
+ int cx = 0;
+ int reg_start = 0, reg_end = 0;
+
+ for(;;)
+ {
+ if( !*c )
+ break;
+
+ if( *c == ';' )
+ {
+ c ++;
+
+ // Parse attribs
+ if( *c != '\n' )
+ {
+ while( *c )
+ {
+ if( reg_start < reg_end )
+ {
+ if( *c >= 'a' && *c <= 'z' )
+ {
+ arrpush( map.cells[ map.io[ reg_start ] ].conditions, *c );
+ }
+ else
+ {
+ if( *c == ',' || *c == '\n' )
+ {
+ reg_start ++;
+
+ if( *c == '\n' )
+ break;
+ }
+ else
+ {
+ vg_error( "Unkown attrib '%c' (row: %u)\n", *c, map.y );
+ return 0;
+ }
+ }
+ }
+ else
+ {
+ vg_error( "Over-assigned values (row: %u)\n", map.y );
+ return 0;
+ }
+
+ c ++;
+ }
+ }
+
+ if( reg_start != reg_end )
+ {
+ vg_error( "Not enough values assigned (row: %u, %u of %u)\n", map.y, reg_start, reg_end );
+ return 0;
+ }
+
+ if( cx != map.x )
+ {
+ vg_error( "Map row underflow (row: %u, %u<%u)\n", map.y, cx, map.x );
+ return 0;
+ }
+
+ row = arraddnptr( map.cells, map.x );
+ cx = 0;
+ map.y ++;
+ reg_end = reg_start = arrlen( map.io );
+ }
+ else
+ {
+ if( cx == map.x )
+ {
+ vg_error( "Map row overflow (row: %u, %u>%u)\n", map.y, cx, map.x );
+ return 0;
+ }
+
+ row[ cx ].conditions = NULL;
+ row[ cx ].flowing[ 0 ] = 0;
+ row[ cx ].source_count = 0;
+
+ // Parse the various cell types
+ if( *c == '+' || *c == '-' )
+ {
+ arrpush( map.io, cx + map.y*map.x );
+ row[ cx ++ ].flags = *c == '+'? CELL_FLAG_INPUT: CELL_FLAG_OUTPUT;
+ reg_end ++;
+ }
+ else if( *c == '#' )
+ {
+ row[ cx ++ ].flags = CELL_FLAG_WALL;
+ }
+ else
+ {
+ row[ cx ++ ].flags = 0x00;
+ }
+ }
+
+ c ++;
+ }
+
+ // Origin top left corner
+ map.origin[0] = -((float)map.x) * 0.5f;
+ map.origin[2] = -((float)map.y) * 0.5f;
+
+ float *offset_array = (float *)malloc( map.x*map.y*2*sizeof(float) );
+
+ for( int y = 0; y < map.y; y ++ )
+ {
+ for( int x = 0; x < map.x; x ++ )
+ {
+ float *coord = offset_array + (y*map.x+x)*2;
+ coord[0] = x;
+ coord[1] = y;
+ }
+ }
+
+ glBindBuffer( GL_ARRAY_BUFFER, map.tiles_vbo );
+ glBufferSubData( GL_ARRAY_BUFFER, 16*sizeof(float), map.x*map.y*2*sizeof(float), offset_array );
+
+ free( offset_array );
+ vg_success( "Map loaded! (%u:%u)\n", map.x, map.y );
+ return 1;
+}
+
+static int map_tile_availible( int co[2] )
+{
+ // Extract 5x5 grid surrounding tile
+ u32 blob = 0x1000;
+ for( int y = vg_max( co[1]-2, 0 ); y < vg_min( map.y, co[1]+3 ); y ++ )
+ for( int x = vg_max( co[0]-2, 0 ); x < vg_min( map.x, co[0]+3 ); x ++ )
+ {
+ struct cell *cell = map_tile( (int[2]){ x, y } );
+
+ if( cell && (cell->flags & CELL_FLAG_WALKABLE) )
+ blob |= 0x1 << ((y-(co[1]-2))*5 + x-(co[0]-2));
+ }
+
+ // Run filter over center 3x3 grid to check for invalid configurations
+ int kernel[] = { 0, 1, 2, 5, 6, 7, 10, 11, 12 };
+ for( int i = 0; i < vg_list_size(kernel); i ++ )
+ {
+ if( blob & (0x1 << (6+kernel[i])) )
+ {
+ // (reference window: 0x1CE7) Illegal moves
+ // 0100011100010 ;
+ // 0000001100011 ;
+ // 0000011000110 ;
+ // 0110001100000 ;
+ // 1100011000000 ;
+ // 0100001100010 ;
+ // 0100011000010 ;
+
+ u32 invalid[] = { 0x8E2, 0x63, 0xC6, 0xC60, 0x18C0, 0x862, 0x8C2 };
+ u32 window = blob >> kernel[i];
+
+ for( int j = 0; j < vg_list_size(invalid); j ++ )
+ if((window & invalid[j]) == invalid[j])
+ return 0;
+ }
+ }
+
+ return 1;
+}
+