dc875272cd304b624f2df4227c785c7a0ff9149c
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
);
84 // TODO: Split into input/output structures
113 static void map_free(void)
115 for( int i
= 0; i
< arrlen( world
.io
); i
++ )
116 arrfree( world
.io
[ i
].conditions
);
118 arrfree( world
.data
);
127 static int map_load( const char *str
)
136 if( str
[world
.w
] == ';' )
138 else if( !str
[world
.w
] )
140 vg_error( "Unexpected EOF when parsing level\n" );
145 struct cell
*row
= arraddnptr( world
.data
, world
.w
);
147 int reg_start
= 0, reg_end
= 0;
163 if( reg_start
< reg_end
)
165 if( *c
>= 'a' && *c
<= 'z' )
167 arrpush( world
.io
[ reg_start
].conditions
, *c
);
171 if( *c
== ',' || *c
== '\n' )
180 vg_error( "Unkown attribute '%c' (row: %u)\n", *c
, world
.h
);
187 vg_error( "Too many values to assign (row: %u)\n", world
.h
);
195 if( reg_start
!= reg_end
)
197 vg_error( "Not enough values assigned (row: %u, %u of %u)\n", world
.h
, reg_start
, reg_end
);
203 vg_error( "Not enough cells to match previous row definition (row: %u, %u<%u)\n", world
.h
, cx
, world
.w
);
207 row
= arraddnptr( world
.data
, world
.w
);
210 reg_end
= reg_start
= arrlen( world
.io
);
216 vg_error( "Too many cells to match previous row definition (row: %u, %u>%u)\n", world
.h
, cx
, world
.w
);
220 // Tile initialization
222 row
[ cx
].water
[0] = 0;
223 row
[ cx
].water
[1] = 0;
225 if( *c
== '+' || *c
== '-' )
227 struct cell_terminal term
= { .id
= cx
+ world
.h
*world
.w
};
228 arrpush( world
.io
, term
);
229 row
[ cx
++ ].state
= *c
== '+'? FLAG_INPUT
: FLAG_OUTPUT
;
234 row
[ cx
++ ].state
= FLAG_WALL
;
238 row
[ cx
++ ].state
= 0x00;
245 vg_success( "Map loaded! (%u:%u)\n", world
.w
, world
.h
);
249 static struct cell
*pcell( v2i pos
)
251 return &world
.data
[ pos
[1]*world
.w
+ pos
[0] ];
254 int main( int argc
, char *argv
[] )
256 vg_init( argc
, argv
, "FishLadder" );
259 void vg_register(void)
261 SHADER_INIT( shader_tile_colour
);
268 glGenVertexArrays( 1, &world
.tile_vao
);
269 glGenBuffers( 1, &world
.tile_vbo
);
273 0.01f
, 0.01f
, 0.01f
, 0.99f
, 0.99f
, 0.99f
,
274 0.01f
, 0.01f
, 0.99f
, 0.99f
, 0.99f
, 0.01f
,
276 0.48f
, 0.48f
, 0.5f
, 0.52f
, 0.52f
, 0.52f
, // Static dot
277 0.375f
, 0.25f
, 0.5f
, 0.75f
, 0.625f
, 0.25f
, // Downwards pointing arrow
278 0.25f
, 0.625f
, 0.75f
, 0.5f
, 0.25f
, 0.375f
, // Left
279 0.625f
, 0.75f
, 0.5f
, 0.25f
, 0.375f
, 0.75f
, // up
280 0.75f
, 0.375f
, 0.25f
, 0.5f
, 0.75f
, 0.625f
283 glBindVertexArray( world
.tile_vao
);
284 glBindBuffer( GL_ARRAY_BUFFER
, world
.tile_vbo
);
293 glVertexAttribPointer( 0, 2, GL_FLOAT
, GL_FALSE
, 2 * sizeof(float), (void*)0 );
294 glEnableVertexAttribArray( 0 );
301 glGenVertexArrays( 1, &world
.circle_vao
);
302 glGenBuffers( 1, &world
.circle_vbo
);
304 float circle_mesh
[32*6*3];
305 int res
= vg_list_size( circle_mesh
) / (6*3);
307 for( int i
= 0; i
< res
; i
++ )
309 v2f v0
= { sinf( ((float)i
/(float)res
)*VG_TAUf
), cosf( ((float)i
/(float)res
)*VG_TAUf
) };
310 v2f v1
= { sinf( ((float)(i
+1)/(float)res
)*VG_TAUf
), cosf( ((float)(i
+1)/(float)res
)*VG_TAUf
) };
312 circle_mesh
[ i
*6+0 ] = 0.0f
;
313 circle_mesh
[ i
*6+1 ] = 0.0f
;
315 v2_copy( v0
, circle_mesh
+ 32*6 + i
*12 );
316 v2_muls( v0
, 0.8f
, circle_mesh
+ 32*6 + i
*12+2 );
317 v2_copy( v1
, circle_mesh
+ 32*6 + i
*12+4 );
319 v2_copy( v1
, circle_mesh
+ 32*6 + i
*12+6 );
320 v2_muls( v1
, 0.8f
, circle_mesh
+ 32*6 + i
*12+8 );
321 v2_muls( v0
, 0.8f
, circle_mesh
+ 32*6 + i
*12+10 );
323 v2_copy( v0
, circle_mesh
+ i
*6+4 );
324 v2_copy( v1
, circle_mesh
+ i
*6+2 );
325 v2_copy( v0
, circle_mesh
+i
*6+4 );
326 v2_copy( v1
, circle_mesh
+i
*6+2 );
329 glBindVertexArray( world
.circle_vao
);
330 glBindBuffer( GL_ARRAY_BUFFER
, world
.circle_vbo
);
331 glBufferData( GL_ARRAY_BUFFER
, sizeof( circle_mesh
), circle_mesh
, GL_STATIC_DRAW
);
333 glVertexAttribPointer( 0, 2, GL_FLOAT
, GL_FALSE
, 2 * sizeof(float), (void*)0 );
334 glEnableVertexAttribArray( 0 );
342 "###-#####-###;aaa,aa\n"
349 "###+#####+###;aa,aaa\n"
356 glDeleteVertexArrays( 1, &world
.tile_vao
);
357 glDeleteBuffers( 1, &world
.tile_vbo
);
359 glDeleteVertexArrays( 1, &world
.circle_vao
);
360 glDeleteBuffers( 1, &world
.circle_vbo
);
365 static int cell_interactive( v2i co
)
368 if( co
[0] < 2 || co
[0] >= world
.w
-2 || co
[1] < 2 || co
[1] >= world
.h
-2 )
372 if( world
.data
[ world
.w
*co
[1] + co
[0] ].state
& (FLAG_WALL
|FLAG_INPUT
|FLAG_OUTPUT
) )
375 // List of 3x3 configurations that we do not allow
376 static u32 invalid_src
[][9] =
408 // Statically compile invalid configurations into bitmasks
409 static u32 invalid
[ vg_list_size(invalid_src
) ];
411 for( int i
= 0; i
< vg_list_size(invalid_src
); i
++ )
415 for( int j
= 0; j
< 3; j
++ )
416 for( int k
= 0; k
< 3; k
++ )
417 comped
|= invalid_src
[i
][ j
*3+k
] << ((j
*5)+k
);
422 // Extract 5x5 grid surrounding tile
424 for( int y
= co
[1]-2; y
< co
[1]+3; y
++ )
425 for( int x
= co
[0]-2; x
< co
[0]+3; x
++ )
427 struct cell
*cell
= pcell((v2i
){x
,y
});
429 if( cell
&& (cell
->state
& (FLAG_CANAL
|FLAG_INPUT
|FLAG_OUTPUT
)) )
430 blob
|= 0x1 << ((y
-(co
[1]-2))*5 + x
-(co
[0]-2));
433 // Run filter over center 3x3 grid to check for invalid configurations
434 int kernel
[] = { 0, 1, 2, 5, 6, 7, 10, 11, 12 };
435 for( int i
= 0; i
< vg_list_size(kernel
); i
++ )
437 if( blob
& (0x1 << (6+kernel
[i
])) )
439 u32 window
= blob
>> kernel
[i
];
441 for( int j
= 0; j
< vg_list_size(invalid
); j
++ )
442 if((window
& invalid
[j
]) == invalid
[j
])
452 float ratio
= (float)vg_window_y
/ (float)vg_window_x
;
453 float const size
= 9.5f
;
456 origin
[0] = -0.5f
* world
.w
;
457 origin
[1] = -0.5f
* world
.h
;
460 m3x3_projection( m_projection
, -size
, size
, size
*ratio
, -size
*ratio
);
461 m3x3_identity( m_view
);
462 m3x3_translate( m_view
, origin
);
463 m3x3_mul( m_projection
, m_view
, vg_pv
);
464 vg_projection_update();
469 v2_copy( vg_mouse_ws
, tile_pos
);
471 int tile_x
= floorf( tile_pos
[0] );
472 int tile_y
= floorf( tile_pos
[1] );
475 if( !world
.simulating
)
477 if( cell_interactive( (v2i
){ tile_x
, tile_y
} ))
479 world
.selected
= tile_y
* world
.w
+ tile_x
;
481 if( vg_get_button_down("primary") )
483 world
.data
[ world
.selected
].state
^= FLAG_CANAL
;
490 // Simulation stop/start
491 if( vg_get_button_down("go") )
493 if( world
.simulating
)
495 world
.simulating
= 0;
496 world
.num_fishes
= 0;
499 for( int i
= 0; i
< arrlen( world
.io
); i
++ )
500 world
.io
[i
].recv_count
= 0;
502 vg_info( "Stopping simulation!\n" );
506 vg_success( "Starting simulation!\n" );
508 world
.simulating
= 1;
509 world
.num_fishes
= 0;
511 world
.sim_start
= vg_time
;
513 for( int i
= 0; i
< world
.w
*world
.h
; i
++ )
515 world
.data
[ i
].state
&= ~FLAG_FLIP_FLOP
;
518 for( int i
= 0; i
< arrlen( world
.io
); i
++ )
519 world
.io
[i
].recv_count
= 0;
524 // ========================================================
526 // Reclassify world. TODO: Move into own function
527 for( int y
= 2; y
< world
.h
-2; y
++ )
529 for( int x
= 2; x
< world
.w
-2; x
++ )
531 v2i dirs
[] = {{1,0},{0,1},{-1,0},{0,-1}};
535 if( pcell((v2i
){x
,y
})->state
& FLAG_CANAL
)
537 for( int i
= 0; i
< vg_list_size( dirs
); i
++ )
539 struct cell
*neighbour
= pcell((v2i
){x
+dirs
[i
][0], y
+dirs
[i
][1]});
540 if( neighbour
->state
& (FLAG_CANAL
|FLAG_INPUT
|FLAG_OUTPUT
) )
545 pcell((v2i
){x
,y
})->config
= config
;
546 pcell((v2i
){x
,y
})->state
&= ~(FLAG_DROP_L
|FLAG_DROP_R
|FLAG_SPLIT
|FLAG_MERGER
);
550 for( int y
= 2; y
< world
.h
-2; y
++ )
551 for( int x
= 2; x
< world
.w
-2; x
++ )
553 // R,D,L,- 1110 (splitter, 1 drop created)
555 // R,-,L,U - 1011 (merger, 2 drop created)
557 u8 config
= pcell((v2i
){x
,y
})->config
;
559 if( config
== 0x7 ) // splitter
561 world
.data
[y
*world
.w
+x
].state
|= (FLAG_SPLIT
| FLAG_DROP_L
| FLAG_DROP_R
);
563 else if( config
== 0xD )
565 world
.data
[y
*world
.w
+x
-1].state
|= FLAG_DROP_R
;
566 world
.data
[y
*world
.w
+x
+1].state
|= FLAG_DROP_L
;
567 world
.data
[y
*world
.w
+x
].state
|= FLAG_MERGER
;
572 static int update_tick
= 0;
575 if( update_tick
> 5 )
579 u32 buffer_id
= world
.frame
& 0x1;
580 u32 buffer_next
= buffer_id
^ 0x1;
582 for( int y
= 1; y
< world
.h
-1; y
++ )
584 for( int x
= 1; x
< world
.w
-1; x
++ )
586 struct cell
*cell
= pcell((v2i
){x
,y
});
588 if( cell
->state
& FLAG_OUTPUT
)
589 cell
->water
[ buffer_next
] = 16;
593 cell
->water
[ buffer_next
] = 0;
595 if( cell
->state
& FLAG_CANAL
)
597 v2i dirs
[] = {{1,0},{-1,0},{0,-1}};
599 for( int i
= 0; i
< vg_list_size( dirs
); i
++ )
601 struct cell
*neighbour
= &world
.data
[(y
+dirs
[i
][1])*world
.w
+x
+dirs
[i
][0]];
603 // Non canals will be ignored
604 if( !(neighbour
->state
& (FLAG_CANAL
|FLAG_OUTPUT
)) )
607 // Only vertical pulls allowed on neighbour splitters
608 if( (neighbour
->state
& FLAG_SPLIT
) && i
!= 2 )
611 // Only vertical pulls allowed for mergers
612 if( (cell
->state
& FLAG_MERGER
) && i
!= 2 )
615 // Test for renewall cases if we have drop L/R check if i matches direction.
616 if( (((cell
->state
& FLAG_DROP_L
)&&i
==1) || ((cell
->state
& FLAG_DROP_R
)&&i
==0)) && neighbour
->water
[ buffer_id
] )
618 cell
->water
[ buffer_next
] = 16;
622 if( neighbour
->water
[ buffer_id
] > cell
->water
[ buffer_next
]+1 )
625 cell
->water
[ buffer_next
] = neighbour
->water
[ buffer_id
]-1;
630 if( !has_source
&& cell
->water
[ buffer_id
] )
631 cell
->water
[ buffer_next
] = cell
->water
[ buffer_id
]-1;
640 if( world
.simulating
)
642 while( world
.sim_frame
< (int)((vg_time
-world
.sim_start
)*2.0f
) )
644 vg_info( "frame: %u\n", world
.sim_frame
);
646 for( int i
= 0; i
< arrlen( world
.io
); i
++ )
648 struct cell_terminal
*term
= &world
.io
[ i
];
649 int posx
= term
->id
% world
.w
;
650 int posy
= (term
->id
- posx
)/world
.w
;
651 int is_input
= world
.data
[ term
->id
].state
& FLAG_INPUT
;
655 if( world
.sim_frame
< arrlen( term
->conditions
) )
657 struct fish
*fish
= &world
.fishes
[world
.num_fishes
++];
661 fish
->payload
= term
->conditions
[world
.sim_frame
];
665 v2i dirs
[] = {{1,0},{-1,0},{0,-1}};
666 for( int j
= 0; j
< vg_list_size(dirs
); j
++ )
669 if( world
.data
[ (posy
+dirs
[j
][1])*world
.w
+ posx
+dirs
[j
][0] ].water
[ world
.frame
& 0x1 ] )
671 fish
->dir
[0] = dirs
[j
][0];
672 fish
->dir
[1] = dirs
[j
][1];
684 for( int i
= 0; i
< world
.num_fishes
; i
++ )
686 struct fish
*fish
= &world
.fishes
[i
];
687 struct cell
*cell_current
= pcell( fish
->pos
);
693 if( cell_current
->state
& FLAG_OUTPUT
)
695 for( int j
= 0; j
< arrlen( world
.io
); j
++ )
697 struct cell_terminal
*term
= &world
.io
[j
];
699 if( term
->id
== fish
->pos
[1]*world
.w
+ fish
->pos
[0] )
701 term
->recv
[ term
->recv_count
++ ] = fish
->payload
;
710 if( !(cell_current
->water
[ world
.frame
& 0x1 ] || cell_current
->state
& (FLAG_INPUT
)) )
716 if( cell_current
->state
& FLAG_SPLIT
)
719 fish
->dir
[0] = cell_current
->state
&FLAG_FLIP_FLOP
?1:-1;
722 cell_current
->state
^= FLAG_FLIP_FLOP
;
724 else if( cell_current
->state
& FLAG_MERGER
)
732 struct cell
*cell_next
= pcell( (v2i
){ fish
->pos
[0]+fish
->dir
[0], fish
->pos
[1]+fish
->dir
[1] } );
733 if( !cell_next
->water
[ world
.frame
& 0x1 ] )
735 // Try other directions for valid
736 v2i dirs
[] = {{1,0},{-1,0},{0,-1}};
737 vg_info( "Trying some other directions...\n" );
739 for( int j
= 0; j
< vg_list_size(dirs
); j
++ )
741 if( (dirs
[j
][0] == -fish
->dir
[0]) && (dirs
[j
][1] == -fish
->dir
[1]) )
744 if( pcell( (v2i
){ fish
->pos
[0]+dirs
[j
][0], fish
->pos
[1]+dirs
[j
][1] } )->water
[ world
.frame
& 0x1 ] )
746 fish
->dir
[0] = dirs
[j
][0];
747 fish
->dir
[1] = dirs
[j
][1];
753 fish
->pos
[0] += fish
->dir
[0];
754 fish
->pos
[1] += fish
->dir
[1];
765 glViewport( 0,0, vg_window_x
, vg_window_y
);
767 glDisable( GL_DEPTH_TEST
);
768 glClearColor( 0.01f
, 0.01f
, 0.01f
, 1.0f
);
769 glClear( GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
771 glBindVertexArray( world
.tile_vao
);
772 SHADER_USE( shader_tile_colour
);
773 glUniformMatrix3fv( SHADER_UNIFORM( shader_tile_colour
, "uPv" ), 1, GL_FALSE
, (float *)vg_pv
);
775 for( int y
= 0; y
< world
.h
; y
++ )
777 for( int x
= 0; x
< world
.w
; x
++ )
779 glUniform3f( SHADER_UNIFORM( shader_tile_colour
, "uOffset" ), (float)x
, (float)y
, 1.0f
);
783 struct cell
*cell
= pcell((v2i
){x
,y
});
785 if( cell
->state
& FLAG_WALL
) { v4_copy( (v4f
){ 0.2f
, 0.2f
, 0.2f
, 1.0f
}, colour
); }
786 else if( cell
->state
& FLAG_CANAL
) { v4_copy( (v4f
){ 0.6f
, 0.6f
, 0.6f
, 1.0f
}, colour
); }
787 else if( cell
->state
& FLAG_INPUT
) { v4_copy( (v4f
){ 0.5f
, 0.5f
, 0.5f
, 1.0f
}, colour
); }
788 else if( cell
->state
& FLAG_OUTPUT
) { v4_copy( (v4f
){ 0.2f
, 0.7f
, 0.3f
, 1.0f
}, colour
); }
789 else v4_copy( (v4f
){ 0.9f
, 0.9f
, 0.9f
, 1.0f
}, colour
);
791 if( cell
->water
[world
.frame
&0x1] )
792 v4_copy( (v4f
){ 0.2f
, 0.3f
, 0.7f
* (float)(cell
->water
[world
.frame
&0x1]) * (1.0f
/16.0f
), 1.0f
}, colour
);
794 if( world
.selected
== y
*world
.w
+ x
)
795 v3_muls( colour
, sinf( vg_time
)*0.25f
+ 0.5f
, colour
);
797 //if( cell->state & (FLAG_SPLIT) )
798 // v4_copy( (v4f){ 0.75f, 0.75f, 0.02f, 1.0f }, colour );
799 //if( cell->state & (FLAG_MERGER) )
800 // v4_copy( (v4f){ 0.75f, 0.02f, 0.75f, 1.0f }, colour );
802 glUniform4fv( SHADER_UNIFORM( shader_tile_colour
, "uColour" ), 1, colour
);
804 glDrawArrays( GL_TRIANGLES
, 0, 6 );
806 if( cell
->state
& FLAG_CANAL
)
808 glUniform4f( SHADER_UNIFORM( shader_tile_colour
, "uColour" ), 1.0f
, 1.0f
, 1.0f
, 1.0f
);
809 glDrawArrays( GL_TRIANGLES
, 6, 3 );
814 glBindVertexArray( world
.circle_vao
);
817 for( int i
= 0; i
< arrlen( world
.io
); i
++ )
819 struct cell_terminal
*term
= &world
.io
[ i
];
820 int posx
= term
->id
% world
.w
;
821 int posy
= (term
->id
- posx
)/world
.w
;
822 int is_input
= world
.data
[ term
->id
].state
& FLAG_INPUT
;
824 int const filled_start
= 0;
825 int const filled_count
= 32*3;
826 int const empty_start
= 32*3;
827 int const empty_count
= 32*6;
829 v4f dot_colour
= { 0.0f
, 0.0f
, 0.0f
, 1.0f
};
831 for( int j
= 0; j
< arrlen( term
->conditions
); j
++ )
833 glUniform3f( SHADER_UNIFORM( shader_tile_colour
, "uOffset" ), (float)posx
+ 0.2f
+ 0.2f
* (float)j
, (float)posy
+ 0.2f
, 0.1f
);
837 colour_code_v3( term
->conditions
[j
], dot_colour
);
838 glUniform4fv( SHADER_UNIFORM( shader_tile_colour
, "uColour" ), 1, dot_colour
);
840 // Draw filled if tick not passed, draw empty if empty
841 if( world
.sim_frame
> j
)
842 glDrawArrays( GL_TRIANGLES
, empty_start
, empty_count
);
844 glDrawArrays( GL_TRIANGLES
, filled_start
, filled_count
);
848 if( term
->recv_count
> j
)
850 colour_code_v3( term
->recv
[j
], dot_colour
);
851 v3_muls( dot_colour
, 0.8f
, dot_colour
);
852 glUniform4fv( SHADER_UNIFORM( shader_tile_colour
, "uColour" ), 1, dot_colour
);
854 glDrawArrays( GL_TRIANGLES
, filled_start
, filled_count
);
857 colour_code_v3( term
->conditions
[j
], dot_colour
);
858 glUniform4fv( SHADER_UNIFORM( shader_tile_colour
, "uColour" ), 1, dot_colour
);
860 glDrawArrays( GL_TRIANGLES
, empty_start
, empty_count
);
866 if( world
.simulating
)
868 float scaled_time
= (vg_time
-world
.sim_start
)*2.0f
;
869 float lerp
= 1.0f
-(scaled_time
- (float)world
.sim_frame
);
871 v4f dot_colour
= { 0.0f
, 0.0f
, 0.0f
, 1.0f
};
873 for( int i
= 0; i
< world
.num_fishes
; i
++ )
875 struct fish
*fish
= &world
.fishes
[i
];
880 colour_code_v3( fish
->payload
, dot_colour
);
881 glUniform4fv( SHADER_UNIFORM( shader_tile_colour
, "uColour" ), 1, dot_colour
);
883 glUniform3f( SHADER_UNIFORM( shader_tile_colour
, "uOffset" ), (float)fish
->pos
[0] + 0.5f
- (float)fish
->dir
[0]*lerp
, (float)fish
->pos
[1] + 0.5f
- (float)fish
->dir
[1]*lerp
, 0.25f
);
884 glDrawArrays( GL_TRIANGLES
, 0, 32*3 );