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
, splitter_l
, splitter_r
;
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
) );
348 #include "models/splitter_l.obj.h"
352 #include "models/splitter_r.obj.h"
355 init_mesh( &world
.splitter_l
, splitter_l
, vg_list_size( splitter_l
) );
356 init_mesh( &world
.splitter_r
, splitter_r
, vg_list_size( splitter_r
) );
362 "###-#####-###;aaa,aa\n"
369 "###+#####+###;aa,aaa\n"
376 free_mesh( &world
.tile
);
377 free_mesh( &world
.circle
);
378 free_mesh( &world
.splitter_l
);
379 free_mesh( &world
.splitter_r
);
384 static int cell_interactive( v2i co
)
387 if( co
[0] < 2 || co
[0] >= world
.w
-2 || co
[1] < 2 || co
[1] >= world
.h
-2 )
391 if( world
.data
[ world
.w
*co
[1] + co
[0] ].state
& (FLAG_WALL
|FLAG_INPUT
|FLAG_OUTPUT
) )
394 // List of 3x3 configurations that we do not allow
395 static u32 invalid_src
[][9] =
427 // Statically compile invalid configurations into bitmasks
428 static u32 invalid
[ vg_list_size(invalid_src
) ];
430 for( int i
= 0; i
< vg_list_size(invalid_src
); i
++ )
434 for( int j
= 0; j
< 3; j
++ )
435 for( int k
= 0; k
< 3; k
++ )
436 comped
|= invalid_src
[i
][ j
*3+k
] << ((j
*5)+k
);
441 // Extract 5x5 grid surrounding tile
443 for( int y
= co
[1]-2; y
< co
[1]+3; y
++ )
444 for( int x
= co
[0]-2; x
< co
[0]+3; x
++ )
446 struct cell
*cell
= pcell((v2i
){x
,y
});
448 if( cell
&& (cell
->state
& (FLAG_CANAL
|FLAG_INPUT
|FLAG_OUTPUT
)) )
449 blob
|= 0x1 << ((y
-(co
[1]-2))*5 + x
-(co
[0]-2));
452 // Run filter over center 3x3 grid to check for invalid configurations
453 int kernel
[] = { 0, 1, 2, 5, 6, 7, 10, 11, 12 };
454 for( int i
= 0; i
< vg_list_size(kernel
); i
++ )
456 if( blob
& (0x1 << (6+kernel
[i
])) )
458 u32 window
= blob
>> kernel
[i
];
460 for( int j
= 0; j
< vg_list_size(invalid
); j
++ )
461 if((window
& invalid
[j
]) == invalid
[j
])
471 float ratio
= (float)vg_window_y
/ (float)vg_window_x
;
472 float const size
= 9.5f
;
475 origin
[0] = -0.5f
* world
.w
;
476 origin
[1] = -0.5f
* world
.h
;
479 m3x3_projection( m_projection
, -size
, size
, -size
*ratio
, size
*ratio
);
480 m3x3_identity( m_view
);
481 m3x3_translate( m_view
, origin
);
482 m3x3_mul( m_projection
, m_view
, vg_pv
);
483 vg_projection_update();
488 v2_copy( vg_mouse_ws
, tile_pos
);
490 int tile_x
= floorf( tile_pos
[0] );
491 int tile_y
= floorf( tile_pos
[1] );
494 if( !world
.simulating
)
496 if( cell_interactive( (v2i
){ tile_x
, tile_y
} ))
498 world
.selected
= tile_y
* world
.w
+ tile_x
;
500 if( vg_get_button_down("primary") )
502 world
.data
[ world
.selected
].state
^= FLAG_CANAL
;
509 // Simulation stop/start
510 if( vg_get_button_down("go") )
512 if( world
.simulating
)
514 world
.simulating
= 0;
515 world
.num_fishes
= 0;
518 for( int i
= 0; i
< arrlen( world
.io
); i
++ )
519 world
.io
[i
].recv_count
= 0;
521 vg_info( "Stopping simulation!\n" );
525 vg_success( "Starting simulation!\n" );
527 world
.simulating
= 1;
528 world
.num_fishes
= 0;
530 world
.sim_start
= vg_time
;
532 for( int i
= 0; i
< world
.w
*world
.h
; i
++ )
534 world
.data
[ i
].state
&= ~FLAG_FLIP_FLOP
;
537 for( int i
= 0; i
< arrlen( world
.io
); i
++ )
538 world
.io
[i
].recv_count
= 0;
543 // ========================================================
545 // Reclassify world. TODO: Move into own function
546 for( int y
= 2; y
< world
.h
-2; y
++ )
548 for( int x
= 2; x
< world
.w
-2; x
++ )
550 v2i dirs
[] = {{1,0},{0,1},{-1,0},{0,-1}};
554 if( pcell((v2i
){x
,y
})->state
& FLAG_CANAL
)
556 for( int i
= 0; i
< vg_list_size( dirs
); i
++ )
558 struct cell
*neighbour
= pcell((v2i
){x
+dirs
[i
][0], y
+dirs
[i
][1]});
559 if( neighbour
->state
& (FLAG_CANAL
|FLAG_INPUT
|FLAG_OUTPUT
) )
564 pcell((v2i
){x
,y
})->config
= config
;
565 pcell((v2i
){x
,y
})->state
&= ~(FLAG_DROP_L
|FLAG_DROP_R
|FLAG_SPLIT
|FLAG_MERGER
);
569 for( int y
= 2; y
< world
.h
-2; y
++ )
570 for( int x
= 2; x
< world
.w
-2; x
++ )
572 // R,D,L,- 1110 (splitter, 1 drop created)
574 // R,-,L,U - 1011 (merger, 2 drop created)
576 u8 config
= pcell((v2i
){x
,y
})->config
;
578 if( config
== 0x7 ) // splitter
580 world
.data
[y
*world
.w
+x
].state
|= (FLAG_SPLIT
| FLAG_DROP_L
| FLAG_DROP_R
);
582 else if( config
== 0xD )
584 world
.data
[y
*world
.w
+x
-1].state
|= FLAG_DROP_R
;
585 world
.data
[y
*world
.w
+x
+1].state
|= FLAG_DROP_L
;
586 world
.data
[y
*world
.w
+x
].state
|= FLAG_MERGER
;
591 if( world
.simulating
)
593 while( world
.sim_frame
< (int)((vg_time
-world
.sim_start
)*2.0f
) )
595 vg_info( "frame: %u\n", world
.sim_frame
);
597 for( int i
= 0; i
< arrlen( world
.io
); i
++ )
599 struct cell_terminal
*term
= &world
.io
[ i
];
600 int posx
= term
->id
% world
.w
;
601 int posy
= (term
->id
- posx
)/world
.w
;
602 int is_input
= world
.data
[ term
->id
].state
& FLAG_INPUT
;
606 if( world
.sim_frame
< arrlen( term
->conditions
) )
608 struct fish
*fish
= &world
.fishes
[world
.num_fishes
++];
612 fish
->payload
= term
->conditions
[world
.sim_frame
];
616 v2i dirs
[] = {{1,0},{-1,0},{0,-1}};
617 for( int j
= 0; j
< vg_list_size(dirs
); j
++ )
620 if( pcell( (v2i
){ posx
+dirs
[j
][0], posy
+dirs
[j
][1] } )->state
& FLAG_CANAL
)
622 fish
->dir
[0] = dirs
[j
][0];
623 fish
->dir
[1] = dirs
[j
][1];
635 for( int i
= 0; i
< world
.num_fishes
; i
++ )
637 struct fish
*fish
= &world
.fishes
[i
];
638 struct cell
*cell_current
= pcell( fish
->pos
);
644 if( cell_current
->state
& FLAG_OUTPUT
)
646 for( int j
= 0; j
< arrlen( world
.io
); j
++ )
648 struct cell_terminal
*term
= &world
.io
[j
];
650 if( term
->id
== fish
->pos
[1]*world
.w
+ fish
->pos
[0] )
652 term
->recv
[ term
->recv_count
++ ] = fish
->payload
;
661 if( !(cell_current
->state
& (FLAG_INPUT
|FLAG_CANAL
)) )
667 if( cell_current
->state
& FLAG_SPLIT
)
670 fish
->dir
[0] = cell_current
->state
&FLAG_FLIP_FLOP
?1:-1;
673 cell_current
->state
^= FLAG_FLIP_FLOP
;
675 else if( cell_current
->state
& FLAG_MERGER
)
683 struct cell
*cell_next
= pcell( (v2i
){ fish
->pos
[0]+fish
->dir
[0], fish
->pos
[1]+fish
->dir
[1] } );
684 if( !(cell_next
->state
& FLAG_CANAL
) )
686 // Try other directions for valid, so down, left, right..
687 v2i dirs
[] = {{1,0},{-1,0},{0,-1}};
688 vg_info( "Trying some other directions...\n" );
690 for( int j
= 0; j
< vg_list_size(dirs
); j
++ )
692 if( (dirs
[j
][0] == -fish
->dir
[0]) && (dirs
[j
][1] == -fish
->dir
[1]) )
695 if( pcell( (v2i
){ fish
->pos
[0]+dirs
[j
][0], fish
->pos
[1]+dirs
[j
][1] } )->state
& FLAG_CANAL
)
697 fish
->dir
[0] = dirs
[j
][0];
698 fish
->dir
[1] = dirs
[j
][1];
704 fish
->pos
[0] += fish
->dir
[0];
705 fish
->pos
[1] += fish
->dir
[1];
716 glViewport( 0,0, vg_window_x
, vg_window_y
);
718 glDisable( GL_DEPTH_TEST
);
719 glClearColor( 0.8f
, 0.8f
, 0.8f
, 1.0f
);
720 glClear( GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
722 use_mesh( &world
.tile
);
723 SHADER_USE( shader_tile_colour
);
724 glUniformMatrix3fv( SHADER_UNIFORM( shader_tile_colour
, "uPv" ), 1, GL_FALSE
, (float *)vg_pv
);
727 glUniform4f( SHADER_UNIFORM( shader_tile_colour
, "uColour" ), 0.5f
, 0.5f
, 0.5f
, 1.0f
);
728 for( int y
= 0; y
< world
.h
; y
++ )
729 for( int x
= 0; x
< world
.w
; x
++ )
731 struct cell
*cell
= pcell((v2i
){x
,y
});
733 if( cell
->state
& FLAG_CANAL
)
738 glUniform3f( SHADER_UNIFORM( shader_tile_colour
, "uOffset" ), (float)x
- 0.2f
, (float)y
- 0.15f
, 1.0f
);
742 for( int y
= 0; y
< world
.h
; y
++ )
744 for( int x
= 0; x
< world
.w
; x
++ )
746 struct cell
*cell
= pcell((v2i
){x
,y
});
747 int selected
= world
.selected
== y
*world
.w
+ x
;
749 if( cell
->state
& FLAG_SPLIT
)
751 glUniform4f( SHADER_UNIFORM( shader_tile_colour
, "uColour" ), 0.9f
, 0.9f
, 0.9f
, 1.0f
);
752 glUniform3f( SHADER_UNIFORM( shader_tile_colour
, "uOffset" ), (float)x
, (float)y
, 1.0f
);
754 struct mesh
*splitter
= cell
->state
& FLAG_FLIP_FLOP
? &world
.splitter_r
: &world
.splitter_l
;
756 use_mesh( splitter
);
757 draw_mesh( 0, splitter
->elements
);
758 use_mesh( &world
.tile
);
761 if( (cell
->state
& FLAG_CANAL
) && !selected
)
764 glUniform3f( SHADER_UNIFORM( shader_tile_colour
, "uOffset" ), (float)x
, (float)y
, 1.0f
);
768 if( cell
->state
& FLAG_WALL
) { v4_copy( (v4f
){ 0.2f
, 0.2f
, 0.2f
, 1.0f
}, colour
); }
769 else if( cell
->state
& FLAG_CANAL
) { v4_copy( (v4f
){ 0.6f
, 0.6f
, 0.6f
, 1.0f
}, colour
); }
770 else if( cell
->state
& FLAG_INPUT
) { v4_copy( (v4f
){ 0.5f
, 0.5f
, 0.5f
, 1.0f
}, colour
); }
771 else if( cell
->state
& FLAG_OUTPUT
) { v4_copy( (v4f
){ 0.2f
, 0.7f
, 0.3f
, 1.0f
}, colour
); }
772 else v4_copy( (v4f
){ 0.9f
, 0.9f
, 0.9f
, 1.0f
}, colour
);
774 //if( cell->water[world.frame&0x1] )
775 // v4_copy( (v4f){ 0.2f, 0.3f, 0.7f * (float)(cell->water[world.frame&0x1]) * (1.0f/16.0f), 1.0f }, colour );
778 v3_muls( colour
, sinf( vg_time
)*0.25f
+ 0.5f
, colour
);
780 //if( cell->state & (FLAG_SPLIT) )
781 // v4_copy( (v4f){ 0.75f, 0.75f, 0.02f, 1.0f }, colour );
782 //if( cell->state & (FLAG_MERGER) )
783 // v4_copy( (v4f){ 0.75f, 0.02f, 0.75f, 1.0f }, colour );
785 glUniform4fv( SHADER_UNIFORM( shader_tile_colour
, "uColour" ), 1, colour
);
791 use_mesh( &world
.circle
);
794 for( int i
= 0; i
< arrlen( world
.io
); i
++ )
796 struct cell_terminal
*term
= &world
.io
[ i
];
797 int posx
= term
->id
% world
.w
;
798 int posy
= (term
->id
- posx
)/world
.w
;
799 int is_input
= world
.data
[ term
->id
].state
& FLAG_INPUT
;
801 int const filled_start
= 0;
802 int const filled_count
= 32;
803 int const empty_start
= 32;
804 int const empty_count
= 32*2;
806 v4f dot_colour
= { 0.0f
, 0.0f
, 0.0f
, 1.0f
};
808 for( int j
= 0; j
< arrlen( term
->conditions
); j
++ )
810 glUniform3f( SHADER_UNIFORM( shader_tile_colour
, "uOffset" ), (float)posx
+ 0.2f
+ 0.2f
* (float)j
, (float)posy
+ 0.2f
, 0.1f
);
814 colour_code_v3( term
->conditions
[j
], dot_colour
);
815 glUniform4fv( SHADER_UNIFORM( shader_tile_colour
, "uColour" ), 1, dot_colour
);
817 // Draw filled if tick not passed, draw empty if empty
818 if( world
.sim_frame
> j
)
819 draw_mesh( empty_start
, empty_count
);
821 draw_mesh( filled_start
, filled_count
);
825 if( term
->recv_count
> j
)
827 colour_code_v3( term
->recv
[j
], dot_colour
);
828 v3_muls( dot_colour
, 0.8f
, dot_colour
);
829 glUniform4fv( SHADER_UNIFORM( shader_tile_colour
, "uColour" ), 1, dot_colour
);
831 draw_mesh( filled_start
, filled_count
);
834 colour_code_v3( term
->conditions
[j
], dot_colour
);
835 glUniform4fv( SHADER_UNIFORM( shader_tile_colour
, "uColour" ), 1, dot_colour
);
837 draw_mesh( empty_start
, empty_count
);
843 if( world
.simulating
)
845 float scaled_time
= (vg_time
-world
.sim_start
)*2.0f
;
846 float lerp
= 1.0f
-(scaled_time
- (float)world
.sim_frame
);
848 v4f dot_colour
= { 0.0f
, 0.0f
, 0.0f
, 1.0f
};
850 for( int i
= 0; i
< world
.num_fishes
; i
++ )
852 struct fish
*fish
= &world
.fishes
[i
];
857 colour_code_v3( fish
->payload
, dot_colour
);
858 glUniform4fv( SHADER_UNIFORM( shader_tile_colour
, "uColour" ), 1, dot_colour
);
860 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
);