1 // Copyright (C) 2021 Harry Godden (hgn) - All Rights Reserved
6 SHADER_DEFINE( shader_tile_colour
,
9 "layout (location=0) in vec2 a_co;"
11 "uniform vec2 uOffset;"
15 "gl_Position = vec4( uPv * vec3( a_co + uOffset, 1.0 ), 1.0 );"
20 "uniform vec4 uColour;"
24 "FragColor = uColour;"
27 UNIFORMS({ "uPv", "uOffset", "uColour" })
34 #define FLAG_INPUT 0x1
35 #define FLAG_OUTPUT 0x2
36 #define FLAG_CANAL 0x4
38 #define FLAG_DROP_L 0x10
39 #define FLAG_SPLIT 0x20
40 #define FLAG_MERGER 0x40
41 #define FLAG_DROP_R 0x80
71 static void map_free(void)
73 for( int i
= 0; i
< arrlen( world
.io
); i
++ )
74 arrfree( world
.io
[ i
].conditions
);
76 arrfree( world
.data
);
85 static int map_load( const char *str
)
94 if( str
[world
.w
] == ';' )
96 else if( !str
[world
.w
] )
98 vg_error( "Unexpected EOF when parsing level\n" );
103 struct cell
*row
= arraddnptr( world
.data
, world
.w
);
105 int reg_start
= 0, reg_end
= 0;
121 if( reg_start
< reg_end
)
123 if( *c
>= 'a' && *c
<= 'z' )
125 arrpush( world
.io
[ reg_start
].conditions
, *c
);
129 if( *c
== ',' || *c
== '\n' )
138 vg_error( "Unkown attribute '%c' (row: %u)\n", *c
, world
.h
);
145 vg_error( "Too many values to assign (row: %u)\n", world
.h
);
153 if( reg_start
!= reg_end
)
155 vg_error( "Not enough values assigned (row: %u, %u of %u)\n", world
.h
, reg_start
, reg_end
);
161 vg_error( "Not enough cells to match previous row definition (row: %u, %u<%u)\n", world
.h
, cx
, world
.w
);
165 row
= arraddnptr( world
.data
, world
.w
);
168 reg_end
= reg_start
= arrlen( world
.io
);
174 vg_error( "Too many cells to match previous row definition (row: %u, %u>%u)\n", world
.h
, cx
, world
.w
);
178 // Tile initialization
180 row
[ cx
].water
[0] = 0;
181 row
[ cx
].water
[1] = 0;
183 if( *c
== '+' || *c
== '-' )
185 struct cell_terminal term
= { .id
= cx
+ world
.h
*world
.w
};
186 arrpush( world
.io
, term
);
187 row
[ cx
++ ].state
= *c
== '+'? FLAG_INPUT
: FLAG_OUTPUT
;
192 row
[ cx
++ ].state
= FLAG_WALL
;
196 row
[ cx
++ ].state
= 0x00;
203 vg_success( "Map loaded! (%u:%u)\n", world
.w
, world
.h
);
207 int main( int argc
, char *argv
[] )
209 vg_init( argc
, argv
, "FishLadder" );
212 void vg_register(void)
214 SHADER_INIT( shader_tile_colour
);
219 glGenVertexArrays( 1, &world
.tile_vao
);
220 glGenBuffers( 1, &world
.tile_vbo
);
224 0.05f
, 0.05f
, 0.05f
, 0.95f
, 0.95f
, 0.95f
,
225 0.05f
, 0.05f
, 0.95f
, 0.95f
, 0.95f
, 0.05f
,
227 0.48f
, 0.48f
, 0.5f
, 0.52f
, 0.52f
, 0.52f
, // Static dot
228 0.375f
, 0.25f
, 0.5f
, 0.75f
, 0.625f
, 0.25f
, // Downwards pointing arrow
229 0.25f
, 0.625f
, 0.75f
, 0.5f
, 0.25f
, 0.375f
, // Left
230 0.625f
, 0.75f
, 0.5f
, 0.25f
, 0.375f
, 0.75f
, // up
231 0.75f
, 0.375f
, 0.25f
, 0.5f
, 0.75f
, 0.625f
235 glBindVertexArray( world
.tile_vao
);
236 glBindBuffer( GL_ARRAY_BUFFER
, world
.tile_vbo
);
245 glVertexAttribPointer( 0, 2, GL_FLOAT
, GL_FALSE
, 2 * sizeof(float), (void*)0 );
246 glEnableVertexAttribArray( 0 );
253 "###-#####-###;aaa,aa\n"
260 "###+#####+###;aa,aaa\n"
267 glDeleteVertexArrays( 1, &world
.tile_vao
);
268 glDeleteBuffers( 1, &world
.tile_vbo
);
273 static int cell_interactive( v2i co
)
276 if( co
[0] < 2 || co
[0] >= world
.w
-2 || co
[1] < 2 || co
[1] >= world
.h
-2 )
280 if( world
.data
[ world
.w
*co
[1] + co
[0] ].state
& (FLAG_WALL
|FLAG_INPUT
|FLAG_OUTPUT
) )
283 // List of 3x3 configurations that we do not allow
284 static u32 invalid_src
[][9] =
316 // Statically compile invalid configurations into bitmasks
317 static u32 invalid
[ vg_list_size(invalid_src
) ];
319 for( int i
= 0; i
< vg_list_size(invalid_src
); i
++ )
323 for( int j
= 0; j
< 3; j
++ )
324 for( int k
= 0; k
< 3; k
++ )
325 comped
|= invalid_src
[i
][ j
*3+k
] << ((j
*5)+k
);
330 // Extract 5x5 grid surrounding tile
332 for( int y
= co
[1]-2; y
< co
[1]+3; y
++ )
333 for( int x
= co
[0]-2; x
< co
[0]+3; x
++ )
335 struct cell
*cell
= &world
.data
[ world
.w
*y
+ x
];
337 if( cell
&& (cell
->state
& (FLAG_CANAL
|FLAG_INPUT
|FLAG_OUTPUT
)) )
338 blob
|= 0x1 << ((y
-(co
[1]-2))*5 + x
-(co
[0]-2));
341 // Run filter over center 3x3 grid to check for invalid configurations
342 int kernel
[] = { 0, 1, 2, 5, 6, 7, 10, 11, 12 };
343 for( int i
= 0; i
< vg_list_size(kernel
); i
++ )
345 if( blob
& (0x1 << (6+kernel
[i
])) )
347 u32 window
= blob
>> kernel
[i
];
349 for( int j
= 0; j
< vg_list_size(invalid
); j
++ )
350 if((window
& invalid
[j
]) == invalid
[j
])
360 float ratio
= (float)vg_window_y
/ (float)vg_window_x
;
361 float const size
= 9.5f
;
364 origin
[0] = -0.5f
* world
.w
;
365 origin
[1] = -0.5f
* world
.h
;
368 m3x3_projection( m_projection
, -size
, size
, size
*ratio
, -size
*ratio
);
369 m3x3_identity( m_view
);
370 m3x3_translate( m_view
, origin
);
371 m3x3_mul( m_projection
, m_view
, vg_pv
);
372 vg_projection_update();
375 v2_copy( vg_mouse_ws
, tile_pos
);
377 int tile_x
= floorf( tile_pos
[0] );
378 int tile_y
= floorf( tile_pos
[1] );
380 if( cell_interactive( (v2i
){ tile_x
, tile_y
} ))
382 world
.selected
= tile_y
* world
.w
+ tile_x
;
384 if( vg_get_button_down("primary") )
386 world
.data
[ world
.selected
].state
^= FLAG_CANAL
;
392 // Reclassify world. TODO: Move into own function
393 for( int y
= 2; y
< world
.h
-2; y
++ )
395 for( int x
= 2; x
< world
.w
-2; x
++ )
397 v2i dirs
[] = {{1,0},{0,1},{-1,0},{0,-1}};
401 if( world
.data
[y
*world
.w
+x
].state
& FLAG_CANAL
)
403 for( int i
= 0; i
< vg_list_size( dirs
); i
++ )
405 struct cell
*neighbour
= &world
.data
[(y
+dirs
[i
][1])*world
.w
+x
+dirs
[i
][0]];
406 if( neighbour
->state
& (FLAG_CANAL
|FLAG_INPUT
|FLAG_OUTPUT
) )
411 world
.data
[ y
*world
.w
+x
].config
= config
;
412 world
.data
[ y
*world
.w
+x
].state
&= ~(FLAG_DROP_L
|FLAG_DROP_R
|FLAG_SPLIT
|FLAG_MERGER
);
416 for( int y
= 2; y
< world
.h
-2; y
++ )
417 for( int x
= 2; x
< world
.w
-2; x
++ )
419 // R,D,L,- 1110 (splitter, 1 drop created)
421 // R,-,L,U - 1011 (merger, 2 drop created)
423 u8 config
= world
.data
[y
*world
.w
+x
].config
;
425 if( config
== 0x7 ) // splitter
427 world
.data
[y
*world
.w
+x
].state
|= (FLAG_SPLIT
| FLAG_DROP_L
| FLAG_DROP_R
);
429 else if( config
== 0xD )
431 world
.data
[y
*world
.w
+x
-1].state
|= FLAG_DROP_R
;
432 world
.data
[y
*world
.w
+x
+1].state
|= FLAG_DROP_L
;
433 world
.data
[y
*world
.w
+x
].state
|= FLAG_MERGER
;
450 configs (Downlevels marked)
462 Path tracing with min function
474 Path tracing with min function
500 static int update_tick
= 0;
503 if( update_tick
> 5 )
507 u32 buffer_id
= world
.frame
& 0x1;
508 u32 buffer_next
= buffer_id
^ 0x1;
510 for( int y
= 1; y
< world
.h
-1; y
++ )
512 for( int x
= 1; x
< world
.w
-1; x
++ )
514 struct cell
*cell
= &world
.data
[y
*world
.w
+x
];
516 if( cell
->state
& FLAG_OUTPUT
)
517 cell
->water
[ buffer_next
] = 16;
521 cell
->water
[ buffer_next
] = 0;
523 if( cell
->state
& FLAG_CANAL
)
525 v2i dirs
[] = {{1,0},{-1,0},{0,-1}};
527 for( int i
= 0; i
< vg_list_size( dirs
); i
++ )
529 struct cell
*neighbour
= &world
.data
[(y
+dirs
[i
][1])*world
.w
+x
+dirs
[i
][0]];
531 // Non canals will be ignored
532 if( !(neighbour
->state
& (FLAG_CANAL
|FLAG_OUTPUT
)) )
535 // Only vertical pulls allowed on neighbour splitters
536 if( (neighbour
->state
& FLAG_SPLIT
) && i
!= 2 )
539 // Only vertical pulls allowed for mergers
540 if( (cell
->state
& FLAG_MERGER
) && i
!= 2 )
543 // Test for renewall cases if we have drop L/R check if i matches direction.
544 if( (((cell
->state
& FLAG_DROP_L
)&&i
==1) || ((cell
->state
& FLAG_DROP_R
)&&i
==0)) && neighbour
->water
[ buffer_id
] )
546 cell
->water
[ buffer_next
] = 16;
550 if( neighbour
->water
[ buffer_id
] > cell
->water
[ buffer_next
]+1 )
553 cell
->water
[ buffer_next
] = neighbour
->water
[ buffer_id
]-1;
558 if( !has_source
&& cell
->water
[ buffer_id
] )
559 cell
->water
[ buffer_next
] = cell
->water
[ buffer_id
]-1;
570 glViewport( 0,0, vg_window_x
, vg_window_y
);
572 glDisable( GL_DEPTH_TEST
);
573 glClearColor( 0.01f
, 0.01f
, 0.01f
, 1.0f
);
574 glClear( GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
576 glBindVertexArray( world
.tile_vao
);
577 SHADER_USE( shader_tile_colour
);
578 glUniformMatrix3fv( SHADER_UNIFORM( shader_tile_colour
, "uPv" ), 1, GL_FALSE
, (float *)vg_pv
);
580 for( int y
= 0; y
< world
.h
; y
++ )
582 for( int x
= 0; x
< world
.w
; x
++ )
584 glUniform2f( SHADER_UNIFORM( shader_tile_colour
, "uOffset" ), (float)x
, (float)y
);
588 struct cell
*cell
= &world
.data
[y
*world
.w
+x
];
590 if( cell
->state
& FLAG_WALL
) { v4_copy( (v4f
){ 0.2f
, 0.2f
, 0.2f
, 1.0f
}, colour
); }
591 else if( cell
->state
& FLAG_CANAL
) { v4_copy( (v4f
){ 0.6f
, 0.6f
, 0.6f
, 1.0f
}, colour
); }
592 else if( cell
->state
& FLAG_INPUT
) { v4_copy( (v4f
){ 0.2f
, 0.3f
, 0.7f
, 1.0f
}, colour
); }
593 else if( cell
->state
& FLAG_OUTPUT
) { v4_copy( (v4f
){ 0.2f
, 0.7f
, 0.3f
, 1.0f
}, colour
); }
594 else v4_copy( (v4f
){ 0.9f
, 0.9f
, 0.9f
, 1.0f
}, colour
);
596 if( cell
->water
[world
.frame
&0x1] )
597 v4_copy( (v4f
){ 0.2f
, 0.3f
, 0.7f
* (float)(cell
->water
[world
.frame
&0x1]) * (1.0f
/8.0f
), 1.0f
}, colour
);
599 if( world
.selected
== y
*world
.w
+ x
)
600 v3_muls( colour
, sinf( vg_time
)*0.25f
+ 0.5f
, colour
);
602 //if( cell->state & (FLAG_SPLIT) )
603 // v4_copy( (v4f){ 0.75f, 0.75f, 0.02f, 1.0f }, colour );
604 //if( cell->state & (FLAG_MERGER) )
605 // v4_copy( (v4f){ 0.75f, 0.02f, 0.75f, 1.0f }, colour );
607 glUniform4fv( SHADER_UNIFORM( shader_tile_colour
, "uColour" ), 1, colour
);
609 glDrawArrays( GL_TRIANGLES
, 0, 6 );
611 if( cell
->state
& FLAG_CANAL
)
613 glUniform4f( SHADER_UNIFORM( shader_tile_colour
, "uColour" ), 1.0f
, 1.0f
, 1.0f
, 1.0f
);
614 glDrawArrays( GL_TRIANGLES
, 6, 3 );