From 35c2e5165a504edfde7cf6ea6d984451714c7de2 Mon Sep 17 00:00:00 2001 From: hgn Date: Sun, 25 Jul 2021 21:44:56 +0100 Subject: [PATCH] revision to flow algorithm --- fishladder.c | 77 ++++++++++++++++++++++++++++------------------------ 1 file changed, 42 insertions(+), 35 deletions(-) diff --git a/fishladder.c b/fishladder.c index f6dc1e9..e1ca2fa 100644 --- a/fishladder.c +++ b/fishladder.c @@ -106,6 +106,7 @@ static struct int level; int state; int flowing[2]; + int source_count; } * cells; @@ -265,10 +266,14 @@ static void map_update_visual(void) for( int i = 0; i < map.y*map.x; i ++ ) { - map.cells[i].flowing[compute_flow_counter] = map.cells[i].flowing[compute_flow_counter^0x1]; + struct cell *cell = map.cells + i; - if( map.cells[i].flowing[compute_flow_counter] ) - map.cells[i].flowing[compute_flow_counter] --; + 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 ++ ) @@ -321,36 +326,40 @@ static void map_update_visual(void) u8 *cellbytes = celldata + (cr[1]*map.x+cr[0])*4; - if( cell->flowing[ compute_flow_counter ^ 0x1 ] == 128 ) + 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] ) { - switch( cellbytes[0] ) - { - // DOWN - case 1: case 5: case 11: case 9: case 3: - map_tile( (int[2]){ cr[0], cr[1]+1 } )->flowing[ compute_flow_counter ] += k_rate_flow; - break; - - // R - case 2: case 6: - if( celldata[ (cr[1]*map.x+cr[0]+1)*4 ] != 14 ) - map_tile( (int[2]){ cr[0]+1, cr[1] } )->flowing[ compute_flow_counter ] += k_rate_flow; - break; - - // L - case 8: case 12: - if( celldata[ (cr[1]*map.x+cr[0]-1)*4 ] != 14 ) - map_tile( (int[2]){ cr[0]-1, cr[1] } )->flowing[ compute_flow_counter ] += k_rate_flow; - break; - - // L+R - case 10: case 14: - if( celldata[ (cr[1]*map.x+cr[0]+1)*4 ] != 14 ) - map_tile( (int[2]){ cr[0]+1, cr[1] } )->flowing[ compute_flow_counter ] += k_rate_flow; - if( celldata[ (cr[1]*map.x+cr[0]-1)*4 ] != 14 ) - map_tile( (int[2]){ cr[0]-1, cr[1] } )->flowing[ compute_flow_counter ] += k_rate_flow; - break; - default: break; - } + // 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 ) @@ -374,9 +383,6 @@ static void map_update_visual(void) celldata[ i*4+2 ] = map.cells[i].flowing[ compute_flow_counter ]; } - // Trace out route - - glBindBuffer( GL_ARRAY_BUFFER, map.tiles_vbo ); glBufferSubData( GL_ARRAY_BUFFER, 16*sizeof(float) + 1024*2*sizeof(float), map.x*map.y*4, celldata ); } @@ -476,6 +482,7 @@ static int map_load( const char *str ) row[ cx ].conditions = NULL; row[ cx ].flowing[ 0 ] = 0; + row[ cx ].source_count = 0; // Parse the various cell types if( *c == '+' || *c == '-' ) -- 2.25.1