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 vec3 uOffset;"
15 "gl_Position = vec4( uPv * vec3( a_co * uOffset.z + uOffset.xy, 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
42 #define FLAG_FLIP_FLOP 0x100
45 { { 0.9f
, 0.2f
, 0.01f
},
46 { 0.2f
, 0.9f
, 0.14f
},
47 { 0.1f
, 0.3f
, 0.85f
} };
49 static void colour_code_v3( char cc
, v3f target
)
51 if( cc
>= 'a' && cc
<= 'z' )
55 if( id
< vg_list_size( colour_sets
) )
57 v3_copy( colour_sets
[ id
], target
);
62 v3_copy( (v3f
){0.0f
,0.0f
,0.0f
}, target
);
71 static void init_mesh( struct mesh
*m
, float *tris
, u32 length
)
73 m
->elements
= length
/3;
74 glGenVertexArrays( 1, &m
->vao
);
75 glGenBuffers( 1, &m
->vbo
);
77 glBindVertexArray( m
->vao
);
78 glBindBuffer( GL_ARRAY_BUFFER
, m
->vbo
);
79 glBufferData( GL_ARRAY_BUFFER
, length
*sizeof(float), tris
, GL_STATIC_DRAW
);
81 glVertexAttribPointer( 0, 2, GL_FLOAT
, GL_FALSE
, 2 * sizeof(float), (void*)0 );
82 glEnableVertexAttribArray( 0 );
87 static void free_mesh( struct mesh
*m
)
89 glDeleteVertexArrays( 1, &m
->vao
);
90 glDeleteBuffers( 1, &m
->vbo
);
93 static void draw_mesh( int const start
, int const count
)
95 glDrawArrays( GL_TRIANGLES
, start
*3, count
*3 );
98 static void use_mesh( struct mesh
*m
)
100 glBindVertexArray( m
->vao
);
120 // TODO: Split into input/output structures
130 struct mesh tile
, circle
;
146 static void map_free(void)
148 for( int i
= 0; i
< arrlen( world
.io
); i
++ )
149 arrfree( world
.io
[ i
].conditions
);
151 arrfree( world
.data
);
160 static int map_load( const char *str
)
169 if( str
[world
.w
] == ';' )
171 else if( !str
[world
.w
] )
173 vg_error( "Unexpected EOF when parsing level\n" );
178 struct cell
*row
= arraddnptr( world
.data
, world
.w
);
180 int reg_start
= 0, reg_end
= 0;
196 if( reg_start
< reg_end
)
198 if( *c
>= 'a' && *c
<= 'z' )
200 arrpush( world
.io
[ reg_start
].conditions
, *c
);
204 if( *c
== ',' || *c
== '\n' )
213 vg_error( "Unkown attribute '%c' (row: %u)\n", *c
, world
.h
);
220 vg_error( "Too many values to assign (row: %u)\n", world
.h
);
228 if( reg_start
!= reg_end
)
230 vg_error( "Not enough values assigned (row: %u, %u of %u)\n", world
.h
, reg_start
, reg_end
);
236 vg_error( "Not enough cells to match previous row definition (row: %u, %u<%u)\n", world
.h
, cx
, world
.w
);
240 row
= arraddnptr( world
.data
, world
.w
);
243 reg_end
= reg_start
= arrlen( world
.io
);
249 vg_error( "Too many cells to match previous row definition (row: %u, %u>%u)\n", world
.h
, cx
, world
.w
);
253 // Tile initialization
256 if( *c
== '+' || *c
== '-' )
258 struct cell_terminal term
= { .id
= cx
+ world
.h
*world
.w
};
259 arrpush( world
.io
, term
);
260 row
[ cx
++ ].state
= *c
== '+'? FLAG_INPUT
: FLAG_OUTPUT
;
265 row
[ cx
++ ].state
= FLAG_WALL
;
269 row
[ cx
++ ].state
= 0x00;
276 vg_success( "Map loaded! (%u:%u)\n", world
.w
, world
.h
);
280 static struct cell
*pcell( v2i pos
)
282 return &world
.data
[ pos
[1]*world
.w
+ pos
[0] ];
285 int main( int argc
, char *argv
[] )
287 vg_init( argc
, argv
, "FishLadder" );
290 void vg_register(void)
292 SHADER_INIT( shader_tile_colour
);
301 0.0f
, 0.0f
, 0.0f
, 1.0f
, 1.0f
, 1.0f
,
302 0.0f
, 0.0f
, 1.0f
, 1.0f
, 1.0f
, 0.0f
,
304 0.48f
, 0.48f
, 0.5f
, 0.52f
, 0.52f
, 0.52f
, // Static dot
305 0.375f
, 0.25f
, 0.5f
, 0.75f
, 0.625f
, 0.25f
, // Downwards pointing arrow
306 0.25f
, 0.625f
, 0.75f
, 0.5f
, 0.25f
, 0.375f
, // Left
307 0.625f
, 0.75f
, 0.5f
, 0.25f
, 0.375f
, 0.75f
, // up
308 0.75f
, 0.375f
, 0.25f
, 0.5f
, 0.75f
, 0.625f
311 init_mesh( &world
.tile
, quad_mesh
, vg_list_size(quad_mesh
) );
316 float circle_mesh
[32*6*3];
317 int res
= vg_list_size( circle_mesh
) / (6*3);
319 for( int i
= 0; i
< res
; i
++ )
321 v2f v0
= { sinf( ((float)i
/(float)res
)*VG_TAUf
), cosf( ((float)i
/(float)res
)*VG_TAUf
) };
322 v2f v1
= { sinf( ((float)(i
+1)/(float)res
)*VG_TAUf
), cosf( ((float)(i
+1)/(float)res
)*VG_TAUf
) };
324 circle_mesh
[ i
*6+0 ] = 0.0f
;
325 circle_mesh
[ i
*6+1 ] = 0.0f
;
327 v2_copy( v0
, circle_mesh
+ 32*6 + i
*12 );
328 v2_muls( v0
, 0.8f
, circle_mesh
+ 32*6 + i
*12+2 );
329 v2_copy( v1
, circle_mesh
+ 32*6 + i
*12+4 );
331 v2_copy( v1
, circle_mesh
+ 32*6 + i
*12+6 );
332 v2_muls( v1
, 0.8f
, circle_mesh
+ 32*6 + i
*12+8 );
333 v2_muls( v0
, 0.8f
, circle_mesh
+ 32*6 + i
*12+10 );
335 v2_copy( v0
, circle_mesh
+ i
*6+4 );
336 v2_copy( v1
, circle_mesh
+ i
*6+2 );
337 v2_copy( v0
, circle_mesh
+i
*6+4 );
338 v2_copy( v1
, circle_mesh
+i
*6+2 );
341 init_mesh( &world
.circle
, circle_mesh
, vg_list_size( circle_mesh
) );
347 "###-#####-###;aaa,aa\n"
354 "###+#####+###;aa,aaa\n"
361 free_mesh( &world
.tile
);
362 free_mesh( &world
.circle
);
367 static int cell_interactive( v2i co
)
370 if( co
[0] < 2 || co
[0] >= world
.w
-2 || co
[1] < 2 || co
[1] >= world
.h
-2 )
374 if( world
.data
[ world
.w
*co
[1] + co
[0] ].state
& (FLAG_WALL
|FLAG_INPUT
|FLAG_OUTPUT
) )
377 // List of 3x3 configurations that we do not allow
378 static u32 invalid_src
[][9] =
410 // Statically compile invalid configurations into bitmasks
411 static u32 invalid
[ vg_list_size(invalid_src
) ];
413 for( int i
= 0; i
< vg_list_size(invalid_src
); i
++ )
417 for( int j
= 0; j
< 3; j
++ )
418 for( int k
= 0; k
< 3; k
++ )
419 comped
|= invalid_src
[i
][ j
*3+k
] << ((j
*5)+k
);
424 // Extract 5x5 grid surrounding tile
426 for( int y
= co
[1]-2; y
< co
[1]+3; y
++ )
427 for( int x
= co
[0]-2; x
< co
[0]+3; x
++ )
429 struct cell
*cell
= pcell((v2i
){x
,y
});
431 if( cell
&& (cell
->state
& (FLAG_CANAL
|FLAG_INPUT
|FLAG_OUTPUT
)) )
432 blob
|= 0x1 << ((y
-(co
[1]-2))*5 + x
-(co
[0]-2));
435 // Run filter over center 3x3 grid to check for invalid configurations
436 int kernel
[] = { 0, 1, 2, 5, 6, 7, 10, 11, 12 };
437 for( int i
= 0; i
< vg_list_size(kernel
); i
++ )
439 if( blob
& (0x1 << (6+kernel
[i
])) )
441 u32 window
= blob
>> kernel
[i
];
443 for( int j
= 0; j
< vg_list_size(invalid
); j
++ )
444 if((window
& invalid
[j
]) == invalid
[j
])
454 float ratio
= (float)vg_window_y
/ (float)vg_window_x
;
455 float const size
= 9.5f
;
458 origin
[0] = -0.5f
* world
.w
;
459 origin
[1] = -0.5f
* world
.h
;
462 m3x3_projection( m_projection
, -size
, size
, -size
*ratio
, size
*ratio
);
463 m3x3_identity( m_view
);
464 m3x3_translate( m_view
, origin
);
465 m3x3_mul( m_projection
, m_view
, vg_pv
);
466 vg_projection_update();
471 v2_copy( vg_mouse_ws
, tile_pos
);
473 int tile_x
= floorf( tile_pos
[0] );
474 int tile_y
= floorf( tile_pos
[1] );
477 if( !world
.simulating
)
479 if( cell_interactive( (v2i
){ tile_x
, tile_y
} ))
481 world
.selected
= tile_y
* world
.w
+ tile_x
;
483 if( vg_get_button_down("primary") )
485 world
.data
[ world
.selected
].state
^= FLAG_CANAL
;
492 // Simulation stop/start
493 if( vg_get_button_down("go") )
495 if( world
.simulating
)
497 world
.simulating
= 0;
498 world
.num_fishes
= 0;
501 for( int i
= 0; i
< arrlen( world
.io
); i
++ )
502 world
.io
[i
].recv_count
= 0;
504 vg_info( "Stopping simulation!\n" );
508 vg_success( "Starting simulation!\n" );
510 world
.simulating
= 1;
511 world
.num_fishes
= 0;
513 world
.sim_start
= vg_time
;
515 for( int i
= 0; i
< world
.w
*world
.h
; i
++ )
517 world
.data
[ i
].state
&= ~FLAG_FLIP_FLOP
;
520 for( int i
= 0; i
< arrlen( world
.io
); i
++ )
521 world
.io
[i
].recv_count
= 0;
526 // ========================================================
528 // Reclassify world. TODO: Move into own function
529 for( int y
= 2; y
< world
.h
-2; y
++ )
531 for( int x
= 2; x
< world
.w
-2; x
++ )
533 v2i dirs
[] = {{1,0},{0,1},{-1,0},{0,-1}};
537 if( pcell((v2i
){x
,y
})->state
& FLAG_CANAL
)
539 for( int i
= 0; i
< vg_list_size( dirs
); i
++ )
541 struct cell
*neighbour
= pcell((v2i
){x
+dirs
[i
][0], y
+dirs
[i
][1]});
542 if( neighbour
->state
& (FLAG_CANAL
|FLAG_INPUT
|FLAG_OUTPUT
) )
547 pcell((v2i
){x
,y
})->config
= config
;
548 pcell((v2i
){x
,y
})->state
&= ~(FLAG_DROP_L
|FLAG_DROP_R
|FLAG_SPLIT
|FLAG_MERGER
);
552 for( int y
= 2; y
< world
.h
-2; y
++ )
553 for( int x
= 2; x
< world
.w
-2; x
++ )
555 // R,D,L,- 1110 (splitter, 1 drop created)
557 // R,-,L,U - 1011 (merger, 2 drop created)
559 u8 config
= pcell((v2i
){x
,y
})->config
;
561 if( config
== 0x7 ) // splitter
563 world
.data
[y
*world
.w
+x
].state
|= (FLAG_SPLIT
| FLAG_DROP_L
| FLAG_DROP_R
);
565 else if( config
== 0xD )
567 world
.data
[y
*world
.w
+x
-1].state
|= FLAG_DROP_R
;
568 world
.data
[y
*world
.w
+x
+1].state
|= FLAG_DROP_L
;
569 world
.data
[y
*world
.w
+x
].state
|= FLAG_MERGER
;
574 if( world
.simulating
)
576 while( world
.sim_frame
< (int)((vg_time
-world
.sim_start
)*2.0f
) )
578 vg_info( "frame: %u\n", world
.sim_frame
);
580 for( int i
= 0; i
< arrlen( world
.io
); i
++ )
582 struct cell_terminal
*term
= &world
.io
[ i
];
583 int posx
= term
->id
% world
.w
;
584 int posy
= (term
->id
- posx
)/world
.w
;
585 int is_input
= world
.data
[ term
->id
].state
& FLAG_INPUT
;
589 if( world
.sim_frame
< arrlen( term
->conditions
) )
591 struct fish
*fish
= &world
.fishes
[world
.num_fishes
++];
595 fish
->payload
= term
->conditions
[world
.sim_frame
];
599 v2i dirs
[] = {{1,0},{-1,0},{0,-1}};
600 for( int j
= 0; j
< vg_list_size(dirs
); j
++ )
603 if( pcell( (v2i
){ posx
+dirs
[j
][0], posy
+dirs
[j
][1] } )->state
& FLAG_CANAL
)
605 fish
->dir
[0] = dirs
[j
][0];
606 fish
->dir
[1] = dirs
[j
][1];
618 for( int i
= 0; i
< world
.num_fishes
; i
++ )
620 struct fish
*fish
= &world
.fishes
[i
];
621 struct cell
*cell_current
= pcell( fish
->pos
);
627 if( cell_current
->state
& FLAG_OUTPUT
)
629 for( int j
= 0; j
< arrlen( world
.io
); j
++ )
631 struct cell_terminal
*term
= &world
.io
[j
];
633 if( term
->id
== fish
->pos
[1]*world
.w
+ fish
->pos
[0] )
635 term
->recv
[ term
->recv_count
++ ] = fish
->payload
;
644 if( !(cell_current
->state
& (FLAG_INPUT
|FLAG_CANAL
)) )
650 if( cell_current
->state
& FLAG_SPLIT
)
653 fish
->dir
[0] = cell_current
->state
&FLAG_FLIP_FLOP
?1:-1;
656 cell_current
->state
^= FLAG_FLIP_FLOP
;
658 else if( cell_current
->state
& FLAG_MERGER
)
666 struct cell
*cell_next
= pcell( (v2i
){ fish
->pos
[0]+fish
->dir
[0], fish
->pos
[1]+fish
->dir
[1] } );
667 if( !(cell_next
->state
& FLAG_CANAL
) )
669 // Try other directions for valid, so down, left, right..
670 v2i dirs
[] = {{1,0},{-1,0},{0,-1}};
671 vg_info( "Trying some other directions...\n" );
673 for( int j
= 0; j
< vg_list_size(dirs
); j
++ )
675 if( (dirs
[j
][0] == -fish
->dir
[0]) && (dirs
[j
][1] == -fish
->dir
[1]) )
678 if( pcell( (v2i
){ fish
->pos
[0]+dirs
[j
][0], fish
->pos
[1]+dirs
[j
][1] } )->state
& FLAG_CANAL
)
680 fish
->dir
[0] = dirs
[j
][0];
681 fish
->dir
[1] = dirs
[j
][1];
687 fish
->pos
[0] += fish
->dir
[0];
688 fish
->pos
[1] += fish
->dir
[1];
699 glViewport( 0,0, vg_window_x
, vg_window_y
);
701 glDisable( GL_DEPTH_TEST
);
702 glClearColor( 0.8f
, 0.8f
, 0.8f
, 1.0f
);
703 glClear( GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
705 use_mesh( &world
.tile
);
706 SHADER_USE( shader_tile_colour
);
707 glUniformMatrix3fv( SHADER_UNIFORM( shader_tile_colour
, "uPv" ), 1, GL_FALSE
, (float *)vg_pv
);
710 glUniform4f( SHADER_UNIFORM( shader_tile_colour
, "uColour" ), 0.5f
, 0.5f
, 0.5f
, 1.0f
);
711 for( int y
= 0; y
< world
.h
; y
++ )
712 for( int x
= 0; x
< world
.w
; x
++ )
714 struct cell
*cell
= pcell((v2i
){x
,y
});
716 if( cell
->state
& FLAG_CANAL
)
721 glUniform3f( SHADER_UNIFORM( shader_tile_colour
, "uOffset" ), (float)x
- 0.2f
, (float)y
- 0.15f
, 1.0f
);
725 for( int y
= 0; y
< world
.h
; y
++ )
727 for( int x
= 0; x
< world
.w
; x
++ )
729 struct cell
*cell
= pcell((v2i
){x
,y
});
730 int selected
= world
.selected
== y
*world
.w
+ x
;
732 if( cell
->state
& FLAG_CANAL
&& !selected
)
735 glUniform3f( SHADER_UNIFORM( shader_tile_colour
, "uOffset" ), (float)x
, (float)y
, 1.0f
);
739 if( cell
->state
& FLAG_WALL
) { v4_copy( (v4f
){ 0.2f
, 0.2f
, 0.2f
, 1.0f
}, colour
); }
740 else if( cell
->state
& FLAG_CANAL
) { v4_copy( (v4f
){ 0.6f
, 0.6f
, 0.6f
, 1.0f
}, colour
); }
741 else if( cell
->state
& FLAG_INPUT
) { v4_copy( (v4f
){ 0.5f
, 0.5f
, 0.5f
, 1.0f
}, colour
); }
742 else if( cell
->state
& FLAG_OUTPUT
) { v4_copy( (v4f
){ 0.2f
, 0.7f
, 0.3f
, 1.0f
}, colour
); }
743 else v4_copy( (v4f
){ 0.9f
, 0.9f
, 0.9f
, 1.0f
}, colour
);
745 //if( cell->water[world.frame&0x1] )
746 // v4_copy( (v4f){ 0.2f, 0.3f, 0.7f * (float)(cell->water[world.frame&0x1]) * (1.0f/16.0f), 1.0f }, colour );
749 v3_muls( colour
, sinf( vg_time
)*0.25f
+ 0.5f
, colour
);
751 //if( cell->state & (FLAG_SPLIT) )
752 // v4_copy( (v4f){ 0.75f, 0.75f, 0.02f, 1.0f }, colour );
753 //if( cell->state & (FLAG_MERGER) )
754 // v4_copy( (v4f){ 0.75f, 0.02f, 0.75f, 1.0f }, colour );
756 glUniform4fv( SHADER_UNIFORM( shader_tile_colour
, "uColour" ), 1, colour
);
762 use_mesh( &world
.circle
);
765 for( int i
= 0; i
< arrlen( world
.io
); i
++ )
767 struct cell_terminal
*term
= &world
.io
[ i
];
768 int posx
= term
->id
% world
.w
;
769 int posy
= (term
->id
- posx
)/world
.w
;
770 int is_input
= world
.data
[ term
->id
].state
& FLAG_INPUT
;
772 int const filled_start
= 0;
773 int const filled_count
= 32;
774 int const empty_start
= 32;
775 int const empty_count
= 32*2;
777 v4f dot_colour
= { 0.0f
, 0.0f
, 0.0f
, 1.0f
};
779 for( int j
= 0; j
< arrlen( term
->conditions
); j
++ )
781 glUniform3f( SHADER_UNIFORM( shader_tile_colour
, "uOffset" ), (float)posx
+ 0.2f
+ 0.2f
* (float)j
, (float)posy
+ 0.2f
, 0.1f
);
785 colour_code_v3( term
->conditions
[j
], dot_colour
);
786 glUniform4fv( SHADER_UNIFORM( shader_tile_colour
, "uColour" ), 1, dot_colour
);
788 // Draw filled if tick not passed, draw empty if empty
789 if( world
.sim_frame
> j
)
790 draw_mesh( empty_start
, empty_count
);
792 draw_mesh( filled_start
, filled_count
);
796 if( term
->recv_count
> j
)
798 colour_code_v3( term
->recv
[j
], dot_colour
);
799 v3_muls( dot_colour
, 0.8f
, dot_colour
);
800 glUniform4fv( SHADER_UNIFORM( shader_tile_colour
, "uColour" ), 1, dot_colour
);
802 draw_mesh( filled_start
, filled_count
);
805 colour_code_v3( term
->conditions
[j
], dot_colour
);
806 glUniform4fv( SHADER_UNIFORM( shader_tile_colour
, "uColour" ), 1, dot_colour
);
808 draw_mesh( empty_start
, empty_count
);
814 if( world
.simulating
)
816 float scaled_time
= (vg_time
-world
.sim_start
)*2.0f
;
817 float lerp
= 1.0f
-(scaled_time
- (float)world
.sim_frame
);
819 v4f dot_colour
= { 0.0f
, 0.0f
, 0.0f
, 1.0f
};
821 for( int i
= 0; i
< world
.num_fishes
; i
++ )
823 struct fish
*fish
= &world
.fishes
[i
];
828 colour_code_v3( fish
->payload
, dot_colour
);
829 glUniform4fv( SHADER_UNIFORM( shader_tile_colour
, "uColour" ), 1, dot_colour
);
831 glUniform3f( SHADER_UNIFORM( shader_tile_colour
, "uOffset" ), (float)fish
->pos
[0] + 0.5f
- (float)fish
->dir
[0]*lerp
, (float)fish
->pos
[1] + 0.25f
- (float)fish
->dir
[1]*lerp
, 0.25f
);