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
);
82 // TODO: Split into input/output structures
111 static void map_free(void)
113 for( int i
= 0; i
< arrlen( world
.io
); i
++ )
114 arrfree( world
.io
[ i
].conditions
);
116 arrfree( world
.data
);
125 static int map_load( const char *str
)
134 if( str
[world
.w
] == ';' )
136 else if( !str
[world
.w
] )
138 vg_error( "Unexpected EOF when parsing level\n" );
143 struct cell
*row
= arraddnptr( world
.data
, world
.w
);
145 int reg_start
= 0, reg_end
= 0;
161 if( reg_start
< reg_end
)
163 if( *c
>= 'a' && *c
<= 'z' )
165 arrpush( world
.io
[ reg_start
].conditions
, *c
);
169 if( *c
== ',' || *c
== '\n' )
178 vg_error( "Unkown attribute '%c' (row: %u)\n", *c
, world
.h
);
185 vg_error( "Too many values to assign (row: %u)\n", world
.h
);
193 if( reg_start
!= reg_end
)
195 vg_error( "Not enough values assigned (row: %u, %u of %u)\n", world
.h
, reg_start
, reg_end
);
201 vg_error( "Not enough cells to match previous row definition (row: %u, %u<%u)\n", world
.h
, cx
, world
.w
);
205 row
= arraddnptr( world
.data
, world
.w
);
208 reg_end
= reg_start
= arrlen( world
.io
);
214 vg_error( "Too many cells to match previous row definition (row: %u, %u>%u)\n", world
.h
, cx
, world
.w
);
218 // Tile initialization
221 if( *c
== '+' || *c
== '-' )
223 struct cell_terminal term
= { .id
= cx
+ world
.h
*world
.w
};
224 arrpush( world
.io
, term
);
225 row
[ cx
++ ].state
= *c
== '+'? FLAG_INPUT
: FLAG_OUTPUT
;
230 row
[ cx
++ ].state
= FLAG_WALL
;
234 row
[ cx
++ ].state
= 0x00;
241 vg_success( "Map loaded! (%u:%u)\n", world
.w
, world
.h
);
245 static struct cell
*pcell( v2i pos
)
247 return &world
.data
[ pos
[1]*world
.w
+ pos
[0] ];
250 int main( int argc
, char *argv
[] )
252 vg_init( argc
, argv
, "FishLadder" );
255 void vg_register(void)
257 SHADER_INIT( shader_tile_colour
);
264 glGenVertexArrays( 1, &world
.tile_vao
);
265 glGenBuffers( 1, &world
.tile_vbo
);
269 0.0f
, 0.0f
, 0.0f
, 1.0f
, 1.0f
, 1.0f
,
270 0.0f
, 0.0f
, 1.0f
, 1.0f
, 1.0f
, 0.0f
,
272 0.48f
, 0.48f
, 0.5f
, 0.52f
, 0.52f
, 0.52f
, // Static dot
273 0.375f
, 0.25f
, 0.5f
, 0.75f
, 0.625f
, 0.25f
, // Downwards pointing arrow
274 0.25f
, 0.625f
, 0.75f
, 0.5f
, 0.25f
, 0.375f
, // Left
275 0.625f
, 0.75f
, 0.5f
, 0.25f
, 0.375f
, 0.75f
, // up
276 0.75f
, 0.375f
, 0.25f
, 0.5f
, 0.75f
, 0.625f
279 glBindVertexArray( world
.tile_vao
);
280 glBindBuffer( GL_ARRAY_BUFFER
, world
.tile_vbo
);
289 glVertexAttribPointer( 0, 2, GL_FLOAT
, GL_FALSE
, 2 * sizeof(float), (void*)0 );
290 glEnableVertexAttribArray( 0 );
297 glGenVertexArrays( 1, &world
.circle_vao
);
298 glGenBuffers( 1, &world
.circle_vbo
);
300 float circle_mesh
[32*6*3];
301 int res
= vg_list_size( circle_mesh
) / (6*3);
303 for( int i
= 0; i
< res
; i
++ )
305 v2f v0
= { sinf( ((float)i
/(float)res
)*VG_TAUf
), cosf( ((float)i
/(float)res
)*VG_TAUf
) };
306 v2f v1
= { sinf( ((float)(i
+1)/(float)res
)*VG_TAUf
), cosf( ((float)(i
+1)/(float)res
)*VG_TAUf
) };
308 circle_mesh
[ i
*6+0 ] = 0.0f
;
309 circle_mesh
[ i
*6+1 ] = 0.0f
;
311 v2_copy( v0
, circle_mesh
+ 32*6 + i
*12 );
312 v2_muls( v0
, 0.8f
, circle_mesh
+ 32*6 + i
*12+2 );
313 v2_copy( v1
, circle_mesh
+ 32*6 + i
*12+4 );
315 v2_copy( v1
, circle_mesh
+ 32*6 + i
*12+6 );
316 v2_muls( v1
, 0.8f
, circle_mesh
+ 32*6 + i
*12+8 );
317 v2_muls( v0
, 0.8f
, circle_mesh
+ 32*6 + i
*12+10 );
319 v2_copy( v0
, circle_mesh
+ i
*6+4 );
320 v2_copy( v1
, circle_mesh
+ i
*6+2 );
321 v2_copy( v0
, circle_mesh
+i
*6+4 );
322 v2_copy( v1
, circle_mesh
+i
*6+2 );
325 glBindVertexArray( world
.circle_vao
);
326 glBindBuffer( GL_ARRAY_BUFFER
, world
.circle_vbo
);
327 glBufferData( GL_ARRAY_BUFFER
, sizeof( circle_mesh
), circle_mesh
, GL_STATIC_DRAW
);
329 glVertexAttribPointer( 0, 2, GL_FLOAT
, GL_FALSE
, 2 * sizeof(float), (void*)0 );
330 glEnableVertexAttribArray( 0 );
338 "###-#####-###;aaa,aa\n"
345 "###+#####+###;aa,aaa\n"
352 glDeleteVertexArrays( 1, &world
.tile_vao
);
353 glDeleteBuffers( 1, &world
.tile_vbo
);
355 glDeleteVertexArrays( 1, &world
.circle_vao
);
356 glDeleteBuffers( 1, &world
.circle_vbo
);
361 static int cell_interactive( v2i co
)
364 if( co
[0] < 2 || co
[0] >= world
.w
-2 || co
[1] < 2 || co
[1] >= world
.h
-2 )
368 if( world
.data
[ world
.w
*co
[1] + co
[0] ].state
& (FLAG_WALL
|FLAG_INPUT
|FLAG_OUTPUT
) )
371 // List of 3x3 configurations that we do not allow
372 static u32 invalid_src
[][9] =
404 // Statically compile invalid configurations into bitmasks
405 static u32 invalid
[ vg_list_size(invalid_src
) ];
407 for( int i
= 0; i
< vg_list_size(invalid_src
); i
++ )
411 for( int j
= 0; j
< 3; j
++ )
412 for( int k
= 0; k
< 3; k
++ )
413 comped
|= invalid_src
[i
][ j
*3+k
] << ((j
*5)+k
);
418 // Extract 5x5 grid surrounding tile
420 for( int y
= co
[1]-2; y
< co
[1]+3; y
++ )
421 for( int x
= co
[0]-2; x
< co
[0]+3; x
++ )
423 struct cell
*cell
= pcell((v2i
){x
,y
});
425 if( cell
&& (cell
->state
& (FLAG_CANAL
|FLAG_INPUT
|FLAG_OUTPUT
)) )
426 blob
|= 0x1 << ((y
-(co
[1]-2))*5 + x
-(co
[0]-2));
429 // Run filter over center 3x3 grid to check for invalid configurations
430 int kernel
[] = { 0, 1, 2, 5, 6, 7, 10, 11, 12 };
431 for( int i
= 0; i
< vg_list_size(kernel
); i
++ )
433 if( blob
& (0x1 << (6+kernel
[i
])) )
435 u32 window
= blob
>> kernel
[i
];
437 for( int j
= 0; j
< vg_list_size(invalid
); j
++ )
438 if((window
& invalid
[j
]) == invalid
[j
])
448 float ratio
= (float)vg_window_y
/ (float)vg_window_x
;
449 float const size
= 9.5f
;
452 origin
[0] = -0.5f
* world
.w
;
453 origin
[1] = -0.5f
* world
.h
;
456 m3x3_projection( m_projection
, -size
, size
, -size
*ratio
, size
*ratio
);
457 m3x3_identity( m_view
);
458 m3x3_translate( m_view
, origin
);
459 m3x3_mul( m_projection
, m_view
, vg_pv
);
460 vg_projection_update();
465 v2_copy( vg_mouse_ws
, tile_pos
);
467 int tile_x
= floorf( tile_pos
[0] );
468 int tile_y
= floorf( tile_pos
[1] );
471 if( !world
.simulating
)
473 if( cell_interactive( (v2i
){ tile_x
, tile_y
} ))
475 world
.selected
= tile_y
* world
.w
+ tile_x
;
477 if( vg_get_button_down("primary") )
479 world
.data
[ world
.selected
].state
^= FLAG_CANAL
;
486 // Simulation stop/start
487 if( vg_get_button_down("go") )
489 if( world
.simulating
)
491 world
.simulating
= 0;
492 world
.num_fishes
= 0;
495 for( int i
= 0; i
< arrlen( world
.io
); i
++ )
496 world
.io
[i
].recv_count
= 0;
498 vg_info( "Stopping simulation!\n" );
502 vg_success( "Starting simulation!\n" );
504 world
.simulating
= 1;
505 world
.num_fishes
= 0;
507 world
.sim_start
= vg_time
;
509 for( int i
= 0; i
< world
.w
*world
.h
; i
++ )
511 world
.data
[ i
].state
&= ~FLAG_FLIP_FLOP
;
514 for( int i
= 0; i
< arrlen( world
.io
); i
++ )
515 world
.io
[i
].recv_count
= 0;
520 // ========================================================
522 // Reclassify world. TODO: Move into own function
523 for( int y
= 2; y
< world
.h
-2; y
++ )
525 for( int x
= 2; x
< world
.w
-2; x
++ )
527 v2i dirs
[] = {{1,0},{0,1},{-1,0},{0,-1}};
531 if( pcell((v2i
){x
,y
})->state
& FLAG_CANAL
)
533 for( int i
= 0; i
< vg_list_size( dirs
); i
++ )
535 struct cell
*neighbour
= pcell((v2i
){x
+dirs
[i
][0], y
+dirs
[i
][1]});
536 if( neighbour
->state
& (FLAG_CANAL
|FLAG_INPUT
|FLAG_OUTPUT
) )
541 pcell((v2i
){x
,y
})->config
= config
;
542 pcell((v2i
){x
,y
})->state
&= ~(FLAG_DROP_L
|FLAG_DROP_R
|FLAG_SPLIT
|FLAG_MERGER
);
546 for( int y
= 2; y
< world
.h
-2; y
++ )
547 for( int x
= 2; x
< world
.w
-2; x
++ )
549 // R,D,L,- 1110 (splitter, 1 drop created)
551 // R,-,L,U - 1011 (merger, 2 drop created)
553 u8 config
= pcell((v2i
){x
,y
})->config
;
555 if( config
== 0x7 ) // splitter
557 world
.data
[y
*world
.w
+x
].state
|= (FLAG_SPLIT
| FLAG_DROP_L
| FLAG_DROP_R
);
559 else if( config
== 0xD )
561 world
.data
[y
*world
.w
+x
-1].state
|= FLAG_DROP_R
;
562 world
.data
[y
*world
.w
+x
+1].state
|= FLAG_DROP_L
;
563 world
.data
[y
*world
.w
+x
].state
|= FLAG_MERGER
;
568 if( world
.simulating
)
570 while( world
.sim_frame
< (int)((vg_time
-world
.sim_start
)*2.0f
) )
572 vg_info( "frame: %u\n", world
.sim_frame
);
574 for( int i
= 0; i
< arrlen( world
.io
); i
++ )
576 struct cell_terminal
*term
= &world
.io
[ i
];
577 int posx
= term
->id
% world
.w
;
578 int posy
= (term
->id
- posx
)/world
.w
;
579 int is_input
= world
.data
[ term
->id
].state
& FLAG_INPUT
;
583 if( world
.sim_frame
< arrlen( term
->conditions
) )
585 struct fish
*fish
= &world
.fishes
[world
.num_fishes
++];
589 fish
->payload
= term
->conditions
[world
.sim_frame
];
593 v2i dirs
[] = {{1,0},{-1,0},{0,-1}};
594 for( int j
= 0; j
< vg_list_size(dirs
); j
++ )
597 if( pcell( (v2i
){ posx
+dirs
[j
][0], posy
+dirs
[j
][1] } )->state
& FLAG_CANAL
)
599 fish
->dir
[0] = dirs
[j
][0];
600 fish
->dir
[1] = dirs
[j
][1];
612 for( int i
= 0; i
< world
.num_fishes
; i
++ )
614 struct fish
*fish
= &world
.fishes
[i
];
615 struct cell
*cell_current
= pcell( fish
->pos
);
621 if( cell_current
->state
& FLAG_OUTPUT
)
623 for( int j
= 0; j
< arrlen( world
.io
); j
++ )
625 struct cell_terminal
*term
= &world
.io
[j
];
627 if( term
->id
== fish
->pos
[1]*world
.w
+ fish
->pos
[0] )
629 term
->recv
[ term
->recv_count
++ ] = fish
->payload
;
638 if( !(cell_current
->state
& (FLAG_INPUT
|FLAG_CANAL
)) )
644 if( cell_current
->state
& FLAG_SPLIT
)
647 fish
->dir
[0] = cell_current
->state
&FLAG_FLIP_FLOP
?1:-1;
650 cell_current
->state
^= FLAG_FLIP_FLOP
;
652 else if( cell_current
->state
& FLAG_MERGER
)
660 struct cell
*cell_next
= pcell( (v2i
){ fish
->pos
[0]+fish
->dir
[0], fish
->pos
[1]+fish
->dir
[1] } );
661 if( !(cell_next
->state
& FLAG_CANAL
) )
663 // Try other directions for valid, so down, left, right..
664 v2i dirs
[] = {{1,0},{-1,0},{0,-1}};
665 vg_info( "Trying some other directions...\n" );
667 for( int j
= 0; j
< vg_list_size(dirs
); j
++ )
669 if( (dirs
[j
][0] == -fish
->dir
[0]) && (dirs
[j
][1] == -fish
->dir
[1]) )
672 if( pcell( (v2i
){ fish
->pos
[0]+dirs
[j
][0], fish
->pos
[1]+dirs
[j
][1] } )->state
& FLAG_CANAL
)
674 fish
->dir
[0] = dirs
[j
][0];
675 fish
->dir
[1] = dirs
[j
][1];
681 fish
->pos
[0] += fish
->dir
[0];
682 fish
->pos
[1] += fish
->dir
[1];
693 glViewport( 0,0, vg_window_x
, vg_window_y
);
695 glDisable( GL_DEPTH_TEST
);
696 glClearColor( 0.8f
, 0.8f
, 0.8f
, 1.0f
);
697 glClear( GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
699 glBindVertexArray( world
.tile_vao
);
700 SHADER_USE( shader_tile_colour
);
701 glUniformMatrix3fv( SHADER_UNIFORM( shader_tile_colour
, "uPv" ), 1, GL_FALSE
, (float *)vg_pv
);
704 glUniform4f( SHADER_UNIFORM( shader_tile_colour
, "uColour" ), 0.5f
, 0.5f
, 0.5f
, 1.0f
);
705 for( int y
= 0; y
< world
.h
; y
++ )
706 for( int x
= 0; x
< world
.w
; x
++ )
708 struct cell
*cell
= pcell((v2i
){x
,y
});
710 if( cell
->state
& FLAG_CANAL
)
715 glUniform3f( SHADER_UNIFORM( shader_tile_colour
, "uOffset" ), (float)x
- 0.2f
, (float)y
- 0.15f
, 1.0f
);
716 glDrawArrays( GL_TRIANGLES
, 0, 6 );
719 for( int y
= 0; y
< world
.h
; y
++ )
721 for( int x
= 0; x
< world
.w
; x
++ )
723 struct cell
*cell
= pcell((v2i
){x
,y
});
724 int selected
= world
.selected
== y
*world
.w
+ x
;
726 if( cell
->state
& FLAG_CANAL
&& !selected
)
729 glUniform3f( SHADER_UNIFORM( shader_tile_colour
, "uOffset" ), (float)x
, (float)y
, 1.0f
);
733 if( cell
->state
& FLAG_WALL
) { v4_copy( (v4f
){ 0.2f
, 0.2f
, 0.2f
, 1.0f
}, colour
); }
734 else if( cell
->state
& FLAG_CANAL
) { v4_copy( (v4f
){ 0.6f
, 0.6f
, 0.6f
, 1.0f
}, colour
); }
735 else if( cell
->state
& FLAG_INPUT
) { v4_copy( (v4f
){ 0.5f
, 0.5f
, 0.5f
, 1.0f
}, colour
); }
736 else if( cell
->state
& FLAG_OUTPUT
) { v4_copy( (v4f
){ 0.2f
, 0.7f
, 0.3f
, 1.0f
}, colour
); }
737 else v4_copy( (v4f
){ 0.9f
, 0.9f
, 0.9f
, 1.0f
}, colour
);
739 //if( cell->water[world.frame&0x1] )
740 // v4_copy( (v4f){ 0.2f, 0.3f, 0.7f * (float)(cell->water[world.frame&0x1]) * (1.0f/16.0f), 1.0f }, colour );
743 v3_muls( colour
, sinf( vg_time
)*0.25f
+ 0.5f
, colour
);
745 //if( cell->state & (FLAG_SPLIT) )
746 // v4_copy( (v4f){ 0.75f, 0.75f, 0.02f, 1.0f }, colour );
747 //if( cell->state & (FLAG_MERGER) )
748 // v4_copy( (v4f){ 0.75f, 0.02f, 0.75f, 1.0f }, colour );
750 glUniform4fv( SHADER_UNIFORM( shader_tile_colour
, "uColour" ), 1, colour
);
752 glDrawArrays( GL_TRIANGLES
, 0, 6 );
754 if( cell
->state
& FLAG_CANAL
)
756 glUniform4f( SHADER_UNIFORM( shader_tile_colour
, "uColour" ), 1.0f
, 1.0f
, 1.0f
, 1.0f
);
757 glDrawArrays( GL_TRIANGLES
, 6, 3 );
762 glBindVertexArray( world
.circle_vao
);
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*3;
774 int const empty_start
= 32*3;
775 int const empty_count
= 32*6;
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 glDrawArrays( GL_TRIANGLES
, empty_start
, empty_count
);
792 glDrawArrays( GL_TRIANGLES
, 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 glDrawArrays( GL_TRIANGLES
, 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 glDrawArrays( GL_TRIANGLES
, 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
);
832 glDrawArrays( GL_TRIANGLES
, 0, 32*3 );