1 // Copyright (C) 2021 Harry Godden (hgn) - All Rights Reserved
5 #include "fishladder_resources.h"
7 const char *level_pack_1
[] = {
32 levels
[ vg_list_size( level_pack_1
) ];
34 career
= { .version
= 1 };
37 static void career_serialize(void)
39 vg_asset_write( "sav/game.sav", &career
, sizeof( struct career_state
) );
42 static void career_load(void)
45 struct career_state
*cr
= vg_asset_read_s( "sav/game.sav", &sz
);
47 memset( (void*)career
.levels
, 0, vg_list_size(level_pack_1
) * sizeof(struct career_level
) );
51 if( sz
> sizeof( struct career_state
) )
52 vg_warn( "This save file is too big! Some levels will be lost\n" );
54 if( sz
<= offsetof( struct career_state
, levels
) )
56 vg_error( "This save file is too small to have a header\n" );
61 u32
const size_header
= offsetof(struct career_state
, levels
);
62 u32
const size_levels
= sizeof(struct career_state
)-size_header
;
63 u32
const size_levels_input
= sz
- size_header
;
65 memcpy( (void*)career
.levels
, (void*)cr
->levels
, size_levels_input
);
67 if( sz
< sizeof( struct career_state
) )
69 memset( ((void*)career
.levels
) + size_levels_input
, 0, size_levels
-size_levels_input
);
73 vg_success( "Loaded save file... Info:\n" );
75 for( int i
= 0; i
< vg_list_size( career
.levels
); i
++ )
77 struct career_level
*lvl
= &career
.levels
[i
];
78 vg_info( "Score: %u, Time: %u, Completed: %u\n", lvl
->score
, lvl
->time
, lvl
->completed
);
83 vg_info( "No save file... Using blank one\n" );
91 #define FLAG_CANAL 0x1
92 #define FLAG_IS_TRIGGER 0x2
93 #define FLAG_RESERVED0 0x4
94 #define FLAG_RESERVED1 0x8
96 #define FLAG_INPUT 0x10
97 #define FLAG_OUTPUT 0x20
98 #define FLAG_WALL 0x40
100 #define FLAG_FLIP_FLOP 0x100
101 #define FLAG_TRIGGERED 0x200
102 #define FLAG_FLIP_ROTATING 0x400
103 #define FLAG_TARGETED 0x800
106 0000 0 | 0001 1 | 0010 2 | 0011 3
110 0100 4 | 0101 5 | 0110 6 | 0111 7
114 1000 8 | 1001 9 | 1010 10 | 1011 11
118 1100 12 | 1101 13 | 1110 14 | 1111 15
124 struct cell_description
132 cell_descriptions
[] =
136 { .start
= { 1, 0 }, .end
= { -1, 0 } },
137 { .start
= { 0, 1 }, .end
= { 0, -1 } },
138 { .start
= { 0, 1 }, .end
= { 1, 0 } },
140 { .start
= { -1, 0 }, .end
= { 1, 0 } },
141 { .start
= { -1, 0 }, .end
= { 1, 0 }, .is_linear
= 1 },
142 { .start
= { 0, 1 }, .end
= { -1, 0 } },
143 { .start
= { 0, 1 }, .is_special
= 1 },
145 { .start
= { 0, -1 }, .end
= { 0, 1 } },
146 { .start
= { 1, 0 }, .end
= { 0, -1 } },
147 { .start
= { 0, 1 }, .end
= { 0, -1 }, .is_linear
= 1 },
150 { .start
= { -1, 0 }, .end
= { 0, -1 } },
151 { .end
= { 0, -1 }, .is_special
= 1 },
158 k_cell_type_stub
= 0,
159 k_cell_type_ramp_right
= 3,
160 k_cell_type_ramp_left
= 6,
161 k_cell_type_split
= 7,
162 k_cell_type_merge
= 13,
163 k_cell_type_con_r
= 1,
164 k_cell_type_con_u
= 2,
165 k_cell_type_con_l
= 4,
166 k_cell_type_con_d
= 8
169 v2f
const curve_3
[] = {{0.5f
,1.0f
},{0.5f
,0.625f
},{0.625f
,0.5f
},{1.0f
,0.5f
}};
170 v2f
const curve_6
[] = {{0.5f
,1.0f
},{0.5f
,0.625f
},{0.375f
,0.5f
},{0.0f
,0.5f
}};
171 v2f
const curve_9
[] = {{1.0f
,0.5f
},{0.625f
,0.5f
},{0.5f
,0.375f
},{0.5f
,0.0f
}};
172 v2f
const curve_12
[]= {{0.0f
,0.5f
},{0.375f
,0.5f
},{0.5f
,0.375f
},{0.5f
,0.0f
}};
174 v2f
const curve_1
[] = {{1.0f
,0.5f
},{0.8f
,0.5f
},{0.3f
,0.5f
},{0.2f
,0.5f
}};
175 v2f
const curve_4
[] = {{0.0f
,0.5f
},{0.3f
,0.5f
},{0.5f
,0.5f
},{0.8f
,0.5f
}};
176 v2f
const curve_2
[] = {{0.5f
,1.0f
},{0.5f
,0.8f
},{0.5f
,0.3f
},{0.5f
,0.2f
}};
177 v2f
const curve_8
[] = {{0.5f
,0.0f
},{0.5f
,0.3f
},{0.5f
,0.5f
},{0.5f
,0.8f
}};
179 v2f
const curve_7
[] = {{0.5f
,0.8438f
},{0.875f
,0.8438f
},{0.625f
,0.5f
},{1.0f
,0.5f
}};
180 v2f
const curve_7_1
[] = {{0.5f
,0.8438f
},{1.0f
-0.875f
,0.8438f
},{1.0-0.625f
,0.5f
},{0.0f
,0.5f
}};
182 float const curve_7_linear_section
= 0.1562f
;
185 { { 0.9f
, 0.6f
, 0.20f
},
186 { 0.2f
, 0.9f
, 0.14f
},
187 { 0.4f
, 0.8f
, 1.00f
} };
189 static void colour_code_v3( char cc
, v3f target
)
191 if( cc
>= 'a' && cc
<= 'z' )
195 if( id
< vg_list_size( colour_sets
) )
197 v3_copy( colour_sets
[ id
], target
);
202 v3_copy( (v3f
){0.0f
,0.0f
,0.0f
}, target
);
211 static void init_mesh( struct mesh
*m
, float const *tris
, u32 length
)
213 m
->elements
= length
/3;
214 glGenVertexArrays( 1, &m
->vao
);
215 glGenBuffers( 1, &m
->vbo
);
217 glBindVertexArray( m
->vao
);
218 glBindBuffer( GL_ARRAY_BUFFER
, m
->vbo
);
219 glBufferData( GL_ARRAY_BUFFER
, length
*sizeof(float), tris
, GL_STATIC_DRAW
);
221 glVertexAttribPointer( 0, 2, GL_FLOAT
, GL_FALSE
, 2 * sizeof(float), (void*)0 );
222 glEnableVertexAttribArray( 0 );
227 static void free_mesh( struct mesh
*m
)
229 glDeleteVertexArrays( 1, &m
->vao
);
230 glDeleteBuffers( 1, &m
->vbo
);
233 static void draw_mesh( int const start
, int const count
)
235 glDrawArrays( GL_TRIANGLES
, start
*3, count
*3 );
238 static void use_mesh( struct mesh
*m
)
240 glBindVertexArray( m
->vao
);
245 k_fish_state_soon_dead
= -1,
246 k_fish_state_dead
= 0,
271 int sim_run
, max_runs
;
285 int condition_count
, recv_count
;
297 struct mesh tile
, circle
, numbers
;
300 GLuint vao
, vbo
, ebo
;
305 GLuint background_data
;
306 GLuint random_samples
;
308 int selected
, tile_x
, tile_y
;
315 enum e_fish_state state
;
327 struct career_level
*ptr_career_level
;
335 static void map_free(void)
337 arrfree( world
.data
);
348 world
.initialzed
= 0;
351 static void io_reset(void)
353 for( int i
= 0; i
< arrlen( world
.io
); i
++ )
355 struct cell_terminal
*term
= &world
.io
[i
];
357 for( int j
= 0; j
< term
->run_count
; j
++ )
358 term
->runs
[j
].recv_count
= 0;
362 static void map_reclassify( v2i start
, v2i end
, int update_texbuffer
);
363 static int map_load( const char *str
, const char *name
)
365 //TODO: It may be worthwhile, at this point, to switch to binary encoding for save data
374 if( str
[world
.w
] == ';' )
376 else if( !str
[world
.w
] )
378 vg_error( "Unexpected EOF when parsing level\n" );
383 struct cell
*row
= arraddnptr( world
.data
, world
.w
);
385 int reg_start
= 0, reg_end
= 0;
387 u32
*links_to_make
= NULL
;
388 int links_satisfied
= 0;
390 char link_id_buffer
[32];
398 if( *c
== '\r' ) { c
++; continue; } // fuck off windows
404 if( *c
== '\r' ) c
++;
411 if( *c
== '\r' ) { c
++; continue; }
413 if( reg_start
< reg_end
)
415 struct cell_terminal
*terminal
= &world
.io
[ reg_start
];
416 struct terminal_run
*run
= &terminal
->runs
[ terminal
->run_count
-1 ];
418 if( *c
>= 'a' && *c
<= 'z' )
420 run
->conditions
[ run
->condition_count
++ ] = *c
;
424 if( *c
== ',' || *c
== '\n' )
433 terminal
->runs
[ terminal
->run_count
].condition_count
= 0;
434 terminal
->run_count
++;
435 world
.max_runs
= vg_max( world
.max_runs
, terminal
->run_count
);
439 vg_error( "Unkown attribute '%c' (row: %u)\n", *c
, world
.h
);
446 if( links_satisfied
< arrlen( links_to_make
) )
448 struct cell
*target
= &world
.data
[ links_to_make
[ links_satisfied
] ];
450 if( (((u32
)*c
>= (u32
)'0') && ((u32
)*c
<= (u32
)'9')) || *c
== '-' )
452 if( link_id_n
>= vg_list_size( link_id_buffer
)-1 )
454 vg_error( "Number was way too long to be parsed (row: %u)\n", world
.h
);
458 link_id_buffer
[ link_id_n
++ ] = *c
;
460 else if( *c
== ',' || *c
== '\n' )
462 link_id_buffer
[ link_id_n
] = 0x00;
463 int value
= atoi( link_id_buffer
);
465 target
->links
[value
>= 0? 1:0] = abs(value
);
474 vg_error( "Invalid character '%c' (row: %u)\n", *c
, world
.h
);
480 vg_error( "Too many values to assign (row: %u)\n", world
.h
);
489 // Registry length-error checks
490 if( reg_start
!= reg_end
)
492 vg_error( "Not enough spawn values assigned (row: %u, %u of %u)\n", world
.h
, reg_start
, reg_end
);
496 if( links_satisfied
!= arrlen( links_to_make
) )
498 vg_error( "Not enough link values assigned (row: %u, %u of %u)\n", world
.h
, links_satisfied
, arrlen( links_to_make
) );
504 vg_error( "Not enough cells to match previous row definition (row: %u, %u<%u)\n", world
.h
, cx
, world
.w
);
508 row
= arraddnptr( world
.data
, world
.w
);
511 reg_end
= reg_start
= arrlen( world
.io
);
513 arrsetlen( links_to_make
, 0 );
520 vg_error( "Too many cells to match previous row definition (row: %u, %u>%u)\n", world
.h
, cx
, world
.w
);
524 // Tile initialization
526 struct cell
*cell
= &row
[ cx
];
529 if( *c
== '+' || *c
== '-' )
531 struct cell_terminal
*term
= arraddnptr( world
.io
, 1 );
532 term
->id
= cx
+ world
.h
*world
.w
;
534 term
->runs
[0].condition_count
= 0;
536 cell
->state
= *c
== '+'? FLAG_INPUT
: FLAG_OUTPUT
;
539 else if( *c
== '#' ) cell
->state
= FLAG_WALL
;
540 else if( ((u32
)*c
>= (u32
)'A') && ((u32
)*c
<= (u32
)'A'+0xf) )
542 // Canal flag bits (4bit/16 value):
548 cell
->state
= ((u32
)*c
- (u32
)'A') & (FLAG_CANAL
|FLAG_IS_TRIGGER
);
550 if( cell
->state
& FLAG_IS_TRIGGER
)
551 arrpush( links_to_make
, cx
+ world
.h
*world
.w
);
557 else cell
->state
= 0x00;
565 // Update data texture to fill out the background
567 u8 info_buffer
[64*64*4];
568 for( int i
= 0; i
< 64*64; i
++ )
570 u8
*px
= &info_buffer
[i
*4];
577 glBindTexture( GL_TEXTURE_2D
, world
.background_data
);
578 glTexSubImage2D( GL_TEXTURE_2D
, 0, 0, 0, 64, 64, GL_RGBA
, GL_UNSIGNED_BYTE
, info_buffer
);
581 arrfree( links_to_make
);
583 map_reclassify( NULL
, NULL
, 1 );
586 for( int i
= 0; i
< world
.h
*world
.w
; i
++ )
588 struct cell
*src
= &world
.data
[i
];
589 if( src
->state
& FLAG_IS_TRIGGER
)
591 int link_id
= src
->links
[0]?0:1;
592 if( src
->links
[link_id
] <= world
.h
*world
.w
)
594 struct cell
*target
= &world
.data
[ src
->links
[link_id
] ];
595 if( (target
->state
& FLAG_CANAL
) && (target
->config
== k_cell_type_split
) )
597 if( target
->links
[ link_id
] )
599 vg_error( "Link target was already targeted\n" );
605 target
->links
[ link_id
] = i
;
606 target
->state
|= FLAG_TARGETED
;
611 vg_error( "Link target was invalid\n" );
617 vg_error( "Link target out of bounds\n" );
623 vg_success( "Map '%s' loaded! (%u:%u)\n", name
, world
.w
, world
.h
);
627 strncpy( world
.map_name
, name
, vg_list_size( world
.map_name
)-1 );
628 world
.initialzed
= 1;
632 arrfree( links_to_make
);
637 static struct cell
*pcell( v2i pos
)
639 return &world
.data
[ pos
[1]*world
.w
+ pos
[0] ];
642 static void map_serialize( FILE *stream
)
644 for( int y
= 0; y
< world
.h
; y
++ )
646 for( int x
= 0; x
< world
.w
; x
++ )
648 struct cell
*cell
= pcell( (v2i
){ x
, y
} );
650 if( cell
->state
& FLAG_WALL
) fputc( '#', stream
);
651 else if( cell
->state
& FLAG_INPUT
) fputc( '+', stream
);
652 else if( cell
->state
& FLAG_OUTPUT
) fputc( '-', stream
);
653 else if( cell
->state
& (FLAG_CANAL
|FLAG_IS_TRIGGER
|FLAG_RESERVED0
|FLAG_RESERVED1
) )
655 fputc( (cell
->state
& (FLAG_CANAL
|FLAG_IS_TRIGGER
|FLAG_RESERVED0
|FLAG_RESERVED1
)) + (u32
)'A', stream
);
657 else fputc( ' ', stream
);
660 fputc( ';', stream
);
662 int terminal_write_count
= 0;
664 for( int x
= 0; x
< world
.w
; x
++ )
666 for( int i
= 0; i
< arrlen( world
.io
); i
++ )
668 struct cell_terminal
*term
= &world
.io
[i
];
669 if( term
->id
== y
*world
.w
+x
)
671 if( terminal_write_count
)
672 fputc( ',', stream
);
673 terminal_write_count
++;
675 for( int j
= 0; j
< term
->run_count
; j
++ )
677 struct terminal_run
*run
= &term
->runs
[j
];
679 for( int k
= 0; k
< run
->condition_count
; k
++ )
680 fputc( run
->conditions
[k
], stream
);
682 if( j
< term
->run_count
-1 )
683 fputc( ':', stream
);
689 for( int x
= 0; x
< world
.w
; x
++ )
691 struct cell
*cell
= pcell( (v2i
){ x
,y
} );
692 if( cell
->state
& FLAG_IS_TRIGGER
)
694 if( terminal_write_count
)
695 fputc( ',', stream
);
696 terminal_write_count
++;
698 fprintf( stream
, "%d", cell
->links
[0]? -cell
->links
[0]: cell
->links
[1] );
702 fputc( '\n', stream
);
706 int main( int argc
, char *argv
[] )
708 vg_init( argc
, argv
, "Marble Computing | SPACE: Test | LeftClick: Toggle tile | RightClick: Drag wire" );
711 static int console_credits( int argc
, char const *argv
[] )
713 vg_info( "Aknowledgements:\n" );
714 vg_info( " GLFW zlib/libpng glfw.org\n" );
715 vg_info( " miniaudio MIT0 miniaud.io\n" );
716 vg_info( " QOI MIT phoboslab.org\n" );
717 vg_info( " STB library MIT nothings.org\n" );
718 vg_info( " Weiholmir font justfredrik.itch.io\n" );
722 static int console_save_map( int argc
, char const *argv
[] )
724 if( !world
.initialzed
)
726 vg_error( "Tried to save uninitialized map!\n" );
730 char map_path
[ 256 ];
732 strcpy( map_path
, "sav/" );
733 strcat( map_path
, world
.map_name
);
734 strcat( map_path
, ".map" );
736 FILE *test_writer
= fopen( map_path
, "wb" );
739 vg_info( "Saving map to '%s'\n", map_path
);
740 map_serialize( test_writer
);
742 fclose( test_writer
);
747 vg_error( "Unable to open stream for writing\n" );
752 static int console_load_map( int argc
, char const *argv
[] )
754 char map_path
[ 256 ];
759 strcpy( map_path
, "sav/" );
760 strcat( map_path
, argv
[0] );
761 strcat( map_path
, ".map" );
763 char *text_source
= vg_textasset_read( map_path
);
767 strcpy( map_path
, "maps/" );
768 strcat( map_path
, argv
[0] );
769 strcat( map_path
, ".map" );
771 text_source
= vg_textasset_read( map_path
);
776 vg_info( "Loading map: '%s'\n", map_path
);
777 world
.ptr_career_level
= NULL
;
779 if( !map_load( text_source
, argv
[0] ) )
787 for( int i
= 0; i
< vg_list_size( level_pack_1
); i
++ )
789 if( !strcmp( level_pack_1
[i
], argv
[0] ) )
791 world
.ptr_career_level
= career
.levels
+ i
;
800 vg_error( "Missing maps '%s'\n", argv
[0] );
806 vg_error( "Missing argument <map_path>\n" );
811 static void simulation_stop(void)
813 world
.simulating
= 0;
814 world
.num_fishes
= 0;
820 sfx_system_fadeout( &audio_system_balls_rolling
, 44100 );
822 vg_info( "Stopping simulation!\n" );
825 static int console_changelevel( int argc
, char const *argv
[] )
829 // Save current level
830 console_save_map( 0, NULL
);
831 if( console_load_map( argc
, argv
) )
839 vg_error( "Missing argument <map_path>\n" );
847 vg_function_push( (struct vg_cmd
){
848 .name
= "_map_write",
849 .function
= console_save_map
852 vg_function_push( (struct vg_cmd
){
854 .function
= console_load_map
857 vg_function_push( (struct vg_cmd
){
859 .function
= console_changelevel
862 vg_function_push( (struct vg_cmd
){
864 .function
= console_credits
871 0.0f
, 0.0f
, 0.0f
, 1.0f
, 1.0f
, 1.0f
,
872 0.0f
, 0.0f
, 1.0f
, 1.0f
, 1.0f
, 0.0f
,
874 0.0f
, 0.0f
, 0.0f
, 1.0f
, 4.0f
, 1.0f
,
875 0.0f
, 0.0f
, 4.0f
, 1.0f
, 4.0f
, 0.0f
878 init_mesh( &world
.tile
, quad_mesh
, vg_list_size(quad_mesh
) );
883 float circle_mesh
[32*6*3];
884 int res
= vg_list_size( circle_mesh
) / (6*3);
886 for( int i
= 0; i
< res
; i
++ )
888 v2f v0
= { sinf( ((float)i
/(float)res
)*VG_TAUf
), cosf( ((float)i
/(float)res
)*VG_TAUf
) };
889 v2f v1
= { sinf( ((float)(i
+1)/(float)res
)*VG_TAUf
), cosf( ((float)(i
+1)/(float)res
)*VG_TAUf
) };
891 circle_mesh
[ i
*6+0 ] = 0.0f
;
892 circle_mesh
[ i
*6+1 ] = 0.0f
;
894 v2_copy( v0
, circle_mesh
+ 32*6 + i
*12 );
895 v2_muls( v0
, 0.8f
, circle_mesh
+ 32*6 + i
*12+2 );
896 v2_copy( v1
, circle_mesh
+ 32*6 + i
*12+4 );
898 v2_copy( v1
, circle_mesh
+ 32*6 + i
*12+6 );
899 v2_muls( v1
, 0.8f
, circle_mesh
+ 32*6 + i
*12+8 );
900 v2_muls( v0
, 0.8f
, circle_mesh
+ 32*6 + i
*12+10 );
902 v2_copy( v0
, circle_mesh
+ i
*6+4 );
903 v2_copy( v1
, circle_mesh
+ i
*6+2 );
904 v2_copy( v0
, circle_mesh
+i
*6+4 );
905 v2_copy( v1
, circle_mesh
+i
*6+2 );
908 init_mesh( &world
.circle
, circle_mesh
, vg_list_size( circle_mesh
) );
913 init_mesh( &world
.numbers
,
915 vg_list_size( MESH_NUMBERS_BUFFER
)
918 for( int i
= 0; i
< 10; i
++ )
920 vg_info( "offset: %u, length: %u\n", MESH_NUMBERS_OFFSETS
[i
][0], MESH_NUMBERS_OFFSETS
[i
][1] );
926 int const num_segments
= 64;
928 struct mesh_wire
*mw
= &world
.wire
;
930 v2f wire_points
[ num_segments
* 2 ];
931 u16 wire_indices
[ 6*(num_segments
-1) ];
933 for( int i
= 0; i
< num_segments
; i
++ )
935 float l
= (float)i
/ (float)(num_segments
-1);
937 v2_copy( (v2f
){ l
, -0.5f
}, wire_points
[i
*2+0] );
938 v2_copy( (v2f
){ l
, 0.5f
}, wire_points
[i
*2+1] );
940 if( i
< num_segments
-1 )
942 wire_indices
[ i
*6+0 ] = i
*2 + 0;
943 wire_indices
[ i
*6+1 ] = i
*2 + 1;
944 wire_indices
[ i
*6+2 ] = i
*2 + 3;
945 wire_indices
[ i
*6+3 ] = i
*2 + 0;
946 wire_indices
[ i
*6+4 ] = i
*2 + 3;
947 wire_indices
[ i
*6+5 ] = i
*2 + 2;
951 glGenVertexArrays( 1, &mw
->vao
);
952 glGenBuffers( 1, &mw
->vbo
);
953 glGenBuffers( 1, &mw
->ebo
);
954 glBindVertexArray( mw
->vao
);
956 glBindBuffer( GL_ARRAY_BUFFER
, mw
->vbo
);
958 glBufferData( GL_ARRAY_BUFFER
, sizeof( wire_points
), wire_points
, GL_STATIC_DRAW
);
959 glBindVertexArray( mw
->vao
);
961 glBindBuffer( GL_ELEMENT_ARRAY_BUFFER
, mw
->ebo
);
962 glBufferData( GL_ELEMENT_ARRAY_BUFFER
, sizeof( wire_indices
), wire_indices
, GL_STATIC_DRAW
);
965 glVertexAttribPointer( 0, 2, GL_FLOAT
, GL_FALSE
, 2*sizeof(float), (void*)0 );
966 glEnableVertexAttribArray( 0 );
970 mw
->em
= vg_list_size( wire_indices
);
973 // Create info data texture
975 glGenTextures( 1, &world
.background_data
);
976 glBindTexture( GL_TEXTURE_2D
, world
.background_data
);
977 glTexImage2D( GL_TEXTURE_2D
, 0, GL_RGBA
, 64, 64, 0, GL_RGBA
, GL_UNSIGNED_BYTE
, NULL
);
981 // Create random smaples texture
983 u8
*data
= malloc(512*512*2);
984 for( int i
= 0; i
< 512*512*2; i
++ )
985 data
[ i
] = rand()/(RAND_MAX
/255);
987 glGenTextures( 1, &world
.random_samples
);
988 glBindTexture( GL_TEXTURE_2D
, world
.random_samples
);
989 glTexImage2D( GL_TEXTURE_2D
, 0, GL_RG
, 512, 512, 0, GL_RG
, GL_UNSIGNED_BYTE
, data
);
996 resource_load_main();
1000 console_load_map( 1, level_pack_1
);
1005 console_save_map( 0, NULL
);
1008 resource_free_main();
1010 glDeleteTextures( 1, &world
.background_data
);
1011 glDeleteTextures( 1, &world
.random_samples
);
1013 glDeleteVertexArrays( 1, &world
.wire
.vao
);
1014 glDeleteBuffers( 1, &world
.wire
.vbo
);
1015 glDeleteBuffers( 1, &world
.wire
.ebo
);
1017 free_mesh( &world
.tile
);
1018 free_mesh( &world
.circle
);
1019 free_mesh( &world
.numbers
);
1024 static int world_check_pos_ok( v2i co
)
1026 return (co
[0] < 2 || co
[0] >= world
.w
-2 || co
[1] < 2 || co
[1] >= world
.h
-2)? 0: 1;
1029 static int cell_interactive( v2i co
)
1032 if( !world_check_pos_ok( co
) )
1036 if( world
.data
[ world
.w
*co
[1] + co
[0] ].state
& (FLAG_WALL
|FLAG_INPUT
|FLAG_OUTPUT
) )
1039 // List of 3x3 configurations that we do not allow
1040 static u32 invalid_src
[][9] =
1072 // Statically compile invalid configurations into bitmasks
1073 static u32 invalid
[ vg_list_size(invalid_src
) ];
1075 for( int i
= 0; i
< vg_list_size(invalid_src
); i
++ )
1079 for( int j
= 0; j
< 3; j
++ )
1080 for( int k
= 0; k
< 3; k
++ )
1081 comped
|= invalid_src
[i
][ j
*3+k
] << ((j
*5)+k
);
1083 invalid
[i
] = comped
;
1086 // Extract 5x5 grid surrounding tile
1088 for( int y
= co
[1]-2; y
< co
[1]+3; y
++ )
1089 for( int x
= co
[0]-2; x
< co
[0]+3; x
++ )
1091 struct cell
*cell
= pcell((v2i
){x
,y
});
1093 if( cell
&& (cell
->state
& (FLAG_CANAL
|FLAG_INPUT
|FLAG_OUTPUT
)) )
1094 blob
|= 0x1 << ((y
-(co
[1]-2))*5 + x
-(co
[0]-2));
1097 // Run filter over center 3x3 grid to check for invalid configurations
1098 int kernel
[] = { 0, 1, 2, 5, 6, 7, 10, 11, 12 };
1099 for( int i
= 0; i
< vg_list_size(kernel
); i
++ )
1101 if( blob
& (0x1 << (6+kernel
[i
])) )
1103 u32 window
= blob
>> kernel
[i
];
1105 for( int j
= 0; j
< vg_list_size(invalid
); j
++ )
1106 if((window
& invalid
[j
]) == invalid
[j
])
1114 static void map_reclassify( v2i start
, v2i end
, int update_texbuffer
)
1116 v2i full_start
= { 1,1 };
1117 v2i full_end
= { world
.w
-1, world
.h
-1 };
1119 if( !start
|| !end
)
1126 u8 info_buffer
[64*64*4];
1129 int px0
= vg_max( start
[0], full_start
[0] ),
1130 px1
= vg_min( end
[0], full_end
[0] ),
1131 py0
= vg_max( start
[1], full_start
[1] ),
1132 py1
= vg_min( end
[1], full_end
[1] );
1134 for( int y
= py0
; y
< py1
; y
++ )
1136 for( int x
= px0
; x
< px1
; x
++ )
1138 struct cell
*cell
= pcell((v2i
){x
,y
});
1140 v2i dirs
[] = {{1,0},{0,1},{-1,0},{0,-1}};
1145 if( cell
->state
& (FLAG_CANAL
|FLAG_INPUT
|FLAG_OUTPUT
) )
1147 for( int i
= 0; i
< vg_list_size( dirs
); i
++ )
1149 struct cell
*neighbour
= pcell((v2i
){x
+dirs
[i
][0], y
+dirs
[i
][1]});
1150 if( neighbour
->state
& (FLAG_CANAL
|FLAG_INPUT
|FLAG_OUTPUT
) )
1158 if( cell
->state
& FLAG_WALL
)
1164 pcell((v2i
){x
,y
})->config
= config
;
1166 u8
*info_px
= &info_buffer
[ (pixel_id
++)*4 ];
1167 info_px
[0] = height
;
1168 info_px
[1] = cell
->state
& FLAG_WALL
? 0: 255;
1174 ((cell
->state
& FLAG_IS_TRIGGER
) && (cell
->config
== 0xF || cell
->config
== k_cell_type_split
)) ||
1175 ((cell
->state
& FLAG_TARGETED
) && (cell
->config
!= k_cell_type_split
))
1176 ) && update_texbuffer
1178 cell
->state
&= ~(FLAG_TARGETED
|FLAG_IS_TRIGGER
);
1179 for( u32 i
= 0; i
< 2; i
++ )
1181 if( cell
->links
[i
] )
1183 struct cell
*other_ptr
= &world
.data
[ cell
->links
[i
] ];
1184 other_ptr
->links
[ i
] = 0;
1185 other_ptr
->state
&= ~FLAG_IS_TRIGGER
;
1187 if( other_ptr
->links
[ i
^ 0x1 ] == 0 )
1188 other_ptr
->state
&= ~FLAG_TARGETED
;
1198 if( update_texbuffer
)
1200 glBindTexture( GL_TEXTURE_2D
, world
.background_data
);
1201 glTexSubImage2D( GL_TEXTURE_2D
, 0, px0
+ 16, py0
+ 16, px1
-px0
, py1
-py0
, GL_RGBA
, GL_UNSIGNED_BYTE
, info_buffer
);
1205 u16 id_drag_from
= 0;
1209 void vg_update(void)
1211 // Fit within screen
1213 float r1
= (float)vg_window_y
/ (float)vg_window_x
,
1214 r2
= (float)world
.h
/ (float)world
.w
,
1217 size
= ( r2
< r1
? (float)world
.w
* 0.5f
: ((float)world
.h
* 0.5f
) / r1
) + 2.5f
;
1218 m3x3_projection( m_projection
, -size
, size
, -size
*r1
, size
*r1
);
1221 origin
[0] = floorf( -0.5f
* world
.w
);
1222 origin
[1] = floorf( -0.5f
* world
.h
);
1225 m3x3_identity( m_view
);
1226 m3x3_translate( m_view
, origin
);
1227 m3x3_mul( m_projection
, m_view
, vg_pv
);
1228 vg_projection_update();
1231 v2_copy( vg_mouse_ws
, world
.tile_pos
);
1233 world
.tile_x
= floorf( world
.tile_pos
[0] );
1234 world
.tile_y
= floorf( world
.tile_pos
[1] );
1237 if( !world
.simulating
)
1239 v2_copy( vg_mouse_ws
, drag_to_co
);
1241 if( cell_interactive( (v2i
){ world
.tile_x
, world
.tile_y
} ))
1243 world
.selected
= world
.tile_y
* world
.w
+ world
.tile_x
;
1245 static u32 modify_state
= 0;
1247 struct cell
*cell_ptr
= &world
.data
[world
.selected
];
1249 if( vg_get_button_down("primary") )
1251 modify_state
= (cell_ptr
->state
& FLAG_CANAL
) ^ FLAG_CANAL
;
1254 if( vg_get_button("primary") && ((cell_ptr
->state
& FLAG_CANAL
) != modify_state
) )
1256 cell_ptr
->state
&= ~FLAG_CANAL
;
1257 cell_ptr
->state
|= modify_state
;
1259 if( cell_ptr
->state
& FLAG_CANAL
)
1261 cell_ptr
->links
[0] = 0;
1262 cell_ptr
->links
[1] = 0;
1264 sfx_set_playrnd( &audio_tile_mod
, &audio_system_sfx
, 3, 6 );
1269 sfx_set_playrnd( &audio_tile_mod
, &audio_system_sfx
, 0, 3 );
1273 map_reclassify( (v2i
){ world
.tile_x
-2, world
.tile_y
-2 },
1274 (v2i
){ world
.tile_x
+2, world
.tile_y
+2 }, 1 );
1277 if( vg_get_button_down("secondary") && !(cell_ptr
->config
== k_cell_type_split
) )
1279 id_drag_from
= world
.selected
;
1280 drag_from_co
[0] = world
.tile_x
+ 0.5f
;
1281 drag_from_co
[1] = world
.tile_y
+ 0.5f
;
1284 if( id_drag_from
&& (cell_ptr
->config
== k_cell_type_split
) )
1286 float local_x
= vg_mouse_ws
[0] - (float)world
.tile_x
;
1287 drag_to_co
[0] = (float)world
.tile_x
+ (local_x
> 0.5f
? 0.75f
: 0.25f
);
1288 drag_to_co
[1] = (float)world
.tile_y
+ 0.25f
;
1290 if( vg_get_button_up("secondary") )
1292 struct cell
*drag_ptr
= &world
.data
[id_drag_from
];
1293 u32 link_id
= local_x
> 0.5f
? 1: 0;
1295 // Cleanup existing connections
1296 if( cell_ptr
->links
[ link_id
] )
1298 vg_warn( "Destroying existing connection on link %u (%hu)\n", link_id
, cell_ptr
->links
[ link_id
] );
1300 struct cell
*current_connection
= &world
.data
[ cell_ptr
->links
[ link_id
]];
1301 current_connection
->state
&= ~FLAG_IS_TRIGGER
;
1302 current_connection
->links
[ link_id
] = 0;
1305 if( drag_ptr
->links
[ link_id
^ 0x1 ] )
1307 vg_warn( "Destroying alternate link %u (%hu)\n", link_id
^ 0x1, drag_ptr
->links
[ link_id
^ 0x1 ] );
1309 struct cell
*current_connection
= &world
.data
[ drag_ptr
->links
[ link_id
^ 0x1 ]];
1310 if( !current_connection
->links
[ link_id
] )
1311 current_connection
->state
&= ~FLAG_TARGETED
;
1313 current_connection
->links
[ link_id
^ 0x1 ] = 0;
1314 drag_ptr
->links
[ link_id
^ 0x1 ] = 0;
1317 // Create the new connection
1318 vg_success( "Creating connection on link %u (%hu)\n", link_id
, id_drag_from
);
1320 cell_ptr
->links
[ link_id
] = id_drag_from
;
1321 drag_ptr
->links
[ link_id
] = world
.selected
;
1323 cell_ptr
->state
|= FLAG_TARGETED
;
1324 drag_ptr
->state
|= FLAG_IS_TRIGGER
;
1330 world
.selected
= -1;
1332 if( !(vg_get_button("secondary") && id_drag_from
) )
1337 world
.selected
= -1;
1341 // Simulation stop/start
1342 if( vg_get_button_down("go") )
1344 if( world
.simulating
)
1350 vg_success( "Starting simulation!\n" );
1352 sfx_set_playrnd( &audio_rolls
, &audio_system_balls_rolling
, 0, 1 );
1354 world
.simulating
= 1;
1355 world
.num_fishes
= 0;
1356 world
.sim_frame
= 0;
1357 world
.sim_start
= vg_time
;
1360 for( int i
= 0; i
< world
.w
*world
.h
; i
++ )
1361 world
.data
[ i
].state
&= ~FLAG_FLIP_FLOP
;
1368 if( world
.simulating
)
1370 while( world
.sim_frame
< (int)((vg_time
-world
.sim_start
)*2.0f
) )
1372 //vg_info( "frame: %u\n", world.sim_frame );
1373 sfx_set_playrnd( &audio_random
, &audio_system_balls_switching
, 0, 9 );
1375 // Update splitter deltas
1376 for( int i
= 0; i
< world
.h
*world
.w
; i
++ )
1378 struct cell
*cell
= &world
.data
[i
];
1379 if( cell
->config
== k_cell_type_split
)
1381 cell
->state
&= ~FLAG_FLIP_ROTATING
;
1383 if( cell
->state
& FLAG_IS_TRIGGER
)
1384 cell
->state
&= ~FLAG_TRIGGERED
;
1387 int alive_count
= 0;
1389 // Update fish positions
1390 for( int i
= 0; i
< world
.num_fishes
; i
++ )
1392 struct fish
*fish
= &world
.fishes
[i
];
1394 if( fish
->state
== k_fish_state_soon_dead
)
1395 fish
->state
= k_fish_state_dead
;
1397 if( fish
->state
< k_fish_state_alive
)
1400 struct cell
*cell_current
= pcell( fish
->pos
);
1402 if( fish
->state
== k_fish_state_alive
)
1405 if( cell_current
->state
& FLAG_OUTPUT
)
1407 for( int j
= 0; j
< arrlen( world
.io
); j
++ )
1409 struct cell_terminal
*term
= &world
.io
[j
];
1411 if( term
->id
== fish
->pos
[1]*world
.w
+ fish
->pos
[0] )
1413 struct terminal_run
*run
= &term
->runs
[ world
.sim_run
];
1414 if( run
->recv_count
< vg_list_size( run
->recieved
) )
1415 run
->recieved
[ run
->recv_count
++ ] = fish
->payload
;
1421 fish
->state
= k_fish_state_dead
;
1426 if( cell_current
->config
== k_cell_type_merge
)
1431 fish
->flow_reversed
= 0;
1435 if( cell_current
->config
== k_cell_type_split
)
1438 fish
->dir
[0] = cell_current
->state
&FLAG_FLIP_FLOP
?1:-1;
1441 if( !(cell_current
->state
& FLAG_TARGETED
) )
1442 cell_current
->state
^= FLAG_FLIP_FLOP
;
1446 // Apply cell out-flow
1447 struct cell_description
*desc
= &cell_descriptions
[ cell_current
->config
];
1449 v2i_copy( fish
->flow_reversed
? desc
->start
: desc
->end
, fish
->dir
);
1453 v2i_add( fish
->pos
, fish
->dir
, pos_next
);
1455 struct cell
*cell_next
= pcell( pos_next
);
1457 if( cell_next
->state
& (FLAG_CANAL
|FLAG_OUTPUT
) )
1459 struct cell_description
*desc
= &cell_descriptions
[ cell_next
->config
];
1461 if( cell_next
->config
== k_cell_type_merge
)
1463 if( fish
->dir
[0] == 0 )
1464 fish
->state
= k_fish_state_dead
;
1466 fish
->flow_reversed
= 0;
1470 if( cell_next
->config
== k_cell_type_split
)
1472 if( fish
->dir
[0] == 0 )
1474 sfx_set_playrnd( &audio_splitter
, &audio_system_balls_important
, 0, 1 );
1475 cell_next
->state
|= FLAG_FLIP_ROTATING
;
1477 fish
->flow_reversed
= 0;
1480 fish
->state
= k_fish_state_dead
;
1483 fish
->flow_reversed
= ( fish
->dir
[0] != -desc
->start
[0] ||
1484 fish
->dir
[1] != -desc
->start
[1] )? 1: 0;
1488 fish
->state
= world_check_pos_ok( fish
->pos
)? k_fish_state_bg
: k_fish_state_dead
;
1491 v2i_add( fish
->pos
, fish
->dir
, fish
->pos
);
1493 else if( fish
->state
== k_fish_state_bg
)
1495 v2i_add( fish
->pos
, fish
->dir
, fish
->pos
);
1497 if( !world_check_pos_ok( fish
->pos
) )
1498 fish
->state
= k_fish_state_dead
;
1501 struct cell
*cell_entry
= pcell( fish
->pos
);
1503 if( cell_entry
->state
& FLAG_CANAL
)
1505 if( cell_entry
->config
== k_cell_type_con_r
|| cell_entry
->config
== k_cell_type_con_u
1506 || cell_entry
->config
== k_cell_type_con_l
|| cell_entry
->config
== k_cell_type_con_d
)
1508 fish
->state
= k_fish_state_alive
;
1512 fish
->flow_reversed
= 1;
1514 switch( cell_entry
->config
)
1516 case k_cell_type_con_r
: fish
->dir
[0] = 1; break;
1517 case k_cell_type_con_l
: fish
->dir
[0] = -1; break;
1518 case k_cell_type_con_u
: fish
->dir
[1] = 1; break;
1519 case k_cell_type_con_d
: fish
->dir
[1] = -1; break;
1525 else { vg_error( "fish behaviour unimplemented for behaviour type (%d)\n" ); }
1527 if( fish
->state
>= k_fish_state_alive
)
1531 // Second pass (triggers)
1532 for( int i
= 0; i
< world
.num_fishes
; i
++ )
1534 struct fish
*fish
= &world
.fishes
[i
];
1536 if( fish
->state
== k_fish_state_alive
)
1538 struct cell
*cell_current
= pcell( fish
->pos
);
1540 if( cell_current
->state
& FLAG_IS_TRIGGER
)
1542 int trigger_id
= cell_current
->links
[0]?0:1;
1543 int connection_id
= cell_current
->links
[trigger_id
];
1544 int target_px
= connection_id
% world
.w
;
1545 int target_py
= (connection_id
- target_px
)/world
.w
;
1547 vg_line2( (v2f
){ fish
->pos
[0], fish
->pos
[1] }, (v2f
){ target_px
, target_py
}, 0xffffffff, 0xffffffff );
1549 struct cell
*target_peice
= &world
.data
[ cell_current
->links
[trigger_id
] ];
1551 cell_current
->state
|= FLAG_TRIGGERED
;
1554 target_peice
->state
|= FLAG_FLIP_FLOP
;
1556 target_peice
->state
&= ~FLAG_FLIP_FLOP
;
1561 // Third pass (collisions)
1562 for( int i
= 0; i
< world
.num_fishes
; i
++ )
1564 if( world
.fishes
[i
].state
== k_fish_state_alive
)
1566 for( int j
= i
+1; j
< world
.num_fishes
; j
++ )
1568 if( (world
.fishes
[j
].state
== k_fish_state_alive
) &&
1569 (world
.fishes
[i
].pos
[0] == world
.fishes
[j
].pos
[0]) &&
1570 (world
.fishes
[i
].pos
[1] == world
.fishes
[j
].pos
[1]) )
1572 // Shatter death (+0.5s)
1573 world
.fishes
[i
].state
= k_fish_state_soon_dead
;
1574 world
.fishes
[j
].state
= k_fish_state_soon_dead
;
1575 world
.fishes
[i
].death_time
= 0.5f
;
1576 world
.fishes
[j
].death_time
= 0.5f
;
1583 for( int i
= 0; i
< arrlen( world
.io
); i
++ )
1585 struct cell_terminal
*term
= &world
.io
[ i
];
1586 int posx
= term
->id
% world
.w
;
1587 int posy
= (term
->id
- posx
)/world
.w
;
1588 int is_input
= world
.data
[ term
->id
].state
& FLAG_INPUT
;
1592 if( world
.sim_frame
< term
->runs
[ world
.sim_run
].condition_count
)
1594 struct fish
*fish
= &world
.fishes
[ world
.num_fishes
];
1595 fish
->pos
[0] = posx
;
1596 fish
->pos
[1] = posy
;
1597 fish
->state
= k_fish_state_alive
;
1598 fish
->payload
= term
->runs
[ world
.sim_run
].conditions
[ world
.sim_frame
];
1600 struct cell
*cell_ptr
= pcell( fish
->pos
);
1602 if( cell_ptr
->config
!= k_cell_type_stub
)
1604 struct cell_description
*desc
= &cell_descriptions
[ cell_ptr
->config
];
1606 v2i_copy( desc
->start
, fish
->dir
);
1607 fish
->flow_reversed
= 1;
1609 world
.num_fishes
++;
1616 if( alive_count
== 0 )
1618 world
.completed
= 1;
1620 for( int i
= 0; i
< arrlen( world
.io
); i
++ )
1622 struct cell_terminal
*term
= &world
.io
[ i
];
1623 int is_input
= world
.data
[ term
->id
].state
& FLAG_INPUT
;
1627 struct terminal_run
*run
= &term
->runs
[ world
.sim_run
];
1629 if( run
->recv_count
== run
->condition_count
)
1631 for( int j
= 0; j
< run
->condition_count
; j
++ )
1633 if( run
->recieved
[j
] != run
->conditions
[j
] )
1635 world
.completed
= 0;
1642 world
.completed
= 0;
1648 if( world
.completed
)
1650 if( world
.sim_run
< world
.max_runs
-1 )
1652 vg_success( "Run passed, starting next\n" );
1654 world
.sim_frame
= 0;
1655 world
.sim_start
= vg_time
;
1656 world
.num_fishes
= 0;
1661 vg_success( "Level passed!\n" );
1664 for( int i
= 0; i
< world
.w
*world
.h
; i
++ )
1665 if( world
.data
[ i
].state
& FLAG_CANAL
)
1668 world
.score
= score
;
1669 world
.time
= world
.sim_frame
;
1674 vg_error( "Level failed :(\n" );
1677 // Copy into career data
1678 if( world
.ptr_career_level
)
1680 world
.ptr_career_level
->score
= world
.score
;
1681 world
.ptr_career_level
->time
= world
.time
;
1682 world
.ptr_career_level
->completed
= world
.completed
;
1685 simulation_stop(); // TODO: Async?
1692 float scaled_time
= 0.0f
;
1693 scaled_time
= (vg_time
-world
.sim_start
)*2.0f
;
1694 world
.frame_lerp
= scaled_time
- (float)world
.sim_frame
;
1697 for( int i
= 0; i
< world
.num_fishes
; i
++ )
1699 struct fish
*fish
= &world
.fishes
[i
];
1701 if( fish
->state
== k_fish_state_dead
)
1704 if( fish
->state
== k_fish_state_soon_dead
&& (world
.frame_lerp
> fish
->death_time
) )
1705 continue; // Todo: particle thing?
1707 struct cell
*cell
= pcell(fish
->pos
);
1708 struct cell_description
*desc
= &cell_descriptions
[ cell
->config
];
1712 float t
= world
.frame_lerp
;
1713 if( fish
->flow_reversed
&& !desc
->is_linear
)
1716 v2_copy( fish
->physics_co
, fish
->physics_v
);
1718 switch( cell
->config
)
1720 case k_cell_type_merge
:
1721 if( fish
->dir
[0] == 1 )
1726 case k_cell_type_con_r
: curve
= curve_1
; break;
1727 case k_cell_type_con_l
: curve
= curve_4
; break;
1728 case k_cell_type_con_u
: curve
= curve_2
; break;
1729 case k_cell_type_con_d
: curve
= curve_8
; break;
1730 case 3: curve
= curve_3
; break;
1731 case 6: curve
= curve_6
; break;
1732 case 9: curve
= curve_9
; break;
1733 case 12: curve
= curve_12
; break;
1735 if( t
> curve_7_linear_section
)
1737 t
-= curve_7_linear_section
;
1738 t
*= (1.0f
/(1.0f
-curve_7_linear_section
));
1740 curve
= cell
->state
& FLAG_FLIP_FLOP
? curve_7
: curve_7_1
;
1744 default: curve
= NULL
; break;
1750 float t3
= t
* t
* t
;
1752 float cA
= 3.0f
*t2
- 3.0f
*t3
;
1753 float cB
= 3.0f
*t3
- 6.0f
*t2
+ 3.0f
*t
;
1754 float cC
= 3.0f
*t2
- t3
- 3.0f
*t
+ 1.0f
;
1756 fish
->physics_co
[0] = t3
*curve
[3][0] + cA
*curve
[2][0] + cB
*curve
[1][0] + cC
*curve
[0][0];
1757 fish
->physics_co
[1] = t3
*curve
[3][1] + cA
*curve
[2][1] + cB
*curve
[1][1] + cC
*curve
[0][1];
1758 fish
->physics_co
[0] += (float)fish
->pos
[0];
1759 fish
->physics_co
[1] += (float)fish
->pos
[1];
1764 origin
[0] = (float)fish
->pos
[0] + (float)fish
->dir
[0]*-0.5f
+ 0.5f
;
1765 origin
[1] = (float)fish
->pos
[1] + (float)fish
->dir
[1]*-0.5f
+ 0.5f
;
1767 fish
->physics_co
[0] = origin
[0] + (float)fish
->dir
[0]*t
;
1768 fish
->physics_co
[1] = origin
[1] + (float)fish
->dir
[1]*t
;
1774 static void render_tiles( v2i start
, v2i end
, v4f
const regular_colour
, v4f
const selected_colour
)
1776 v2i full_start
= { 0,0 };
1777 v2i full_end
= { world
.w
, world
.h
};
1779 if( !start
|| !end
)
1785 glUniform4fv( SHADER_UNIFORM( shader_tile_main
, "uColour" ), 1, regular_colour
);
1787 for( int y
= start
[1]; y
< end
[1]; y
++ )
1789 for( int x
= start
[0]; x
< end
[0]; x
++ )
1791 struct cell
*cell
= pcell((v2i
){x
,y
});
1792 int selected
= world
.selected
== y
*world
.w
+ x
;
1794 int tile_offsets
[][2] =
1796 {2, 0}, {0, 3}, {0, 2}, {2, 2},
1797 {1, 0}, {2, 3}, {3, 2}, {1, 3},
1798 {3, 1}, {0, 1}, {1, 2}, {2, 1},
1799 {1, 1}, {3, 3}, {2, 1}, {2, 1}
1802 int uv
[2] = { 3, 0 };
1804 if( cell
->state
& (FLAG_CANAL
|FLAG_INPUT
|FLAG_OUTPUT
) )
1806 uv
[0] = tile_offsets
[ cell
->config
][0];
1807 uv
[1] = tile_offsets
[ cell
->config
][1];
1810 glUniform4f( SHADER_UNIFORM( shader_tile_main
, "uOffset" ), (float)x
, (float)y
, uv
[0], uv
[1] );
1813 glUniform4fv( SHADER_UNIFORM( shader_tile_main
, "uColour" ), 1, selected_colour
);
1815 glUniform4fv( SHADER_UNIFORM( shader_tile_main
, "uColour" ), 1, regular_colour
);
1823 static void draw_numbers( v3f coord
, int number
)
1826 v3_copy( coord
, pos
);
1827 int digits
[8]; int i
= 0;
1829 while( number
> 0 && i
< 8 )
1831 digits
[i
++] = number
% 10;
1832 number
= number
/ 10;
1835 for( int j
= 0; j
< i
; j
++ )
1837 glUniform3fv( SHADER_UNIFORM( shader_tile_colour
, "uOffset" ), 1, pos
);
1838 draw_mesh( MESH_NUMBERS_OFFSETS
[digits
[i
-j
-1]][0], MESH_NUMBERS_OFFSETS
[digits
[i
-j
-1]][1] );
1839 pos
[0] += pos
[2] * 0.75f
;
1843 void vg_render(void)
1845 glViewport( 0,0, vg_window_x
, vg_window_y
);
1847 glDisable( GL_DEPTH_TEST
);
1848 glClearColor( 0.369768f
, 0.3654f
, 0.42f
, 1.0f
);
1849 glClear( GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
1851 v4f
const colour_default
= {1.0f
, 1.0f
, 1.0f
, 1.0f
};
1852 v4f
const colour_selected
= {0.90f
, 0.92f
, 1.0f
, 1.0f
};
1854 // TILE SET RENDERING
1855 // todo: just slam everything into a mesh...
1856 // when user modifies a tile the neighbours can be easily uploaded to gpu mem
1858 // Currently we're uploading a fair amount of data every frame anyway.
1859 // NOTE: this is for final optimisations ONLY!
1860 // ======================================================================
1862 use_mesh( &world
.tile
);
1868 SHADER_USE( shader_background
);
1869 glUniformMatrix3fv( SHADER_UNIFORM( shader_background
, "uPv" ), 1, GL_FALSE
, (float *)vg_pv
);
1871 glActiveTexture( GL_TEXTURE0
);
1872 glBindTexture( GL_TEXTURE_2D
, world
.background_data
);
1873 glUniform1i( SHADER_UNIFORM( shader_background
, "uTexMain" ), 0 );
1875 glUniform3f( SHADER_UNIFORM( shader_background
, "uOffset" ), -16, -16, 64 );
1876 glUniform1f( SHADER_UNIFORM( shader_background
, "uVariance" ), 0.02f
);
1878 glActiveTexture( GL_TEXTURE1
);
1879 glBindTexture( GL_TEXTURE_2D
, world
.random_samples
);
1880 glUniform1i( SHADER_UNIFORM( shader_background
, "uSamplerNoise" ), 1 );
1887 SHADER_USE( shader_tile_main
);
1890 m2x2_identity( subtransform
);
1891 glUniformMatrix2fv( SHADER_UNIFORM( shader_tile_main
, "uSubTransform" ), 1, GL_FALSE
, (float *)subtransform
);
1892 glUniformMatrix3fv( SHADER_UNIFORM( shader_tile_main
, "uPv" ), 1, GL_FALSE
, (float *)vg_pv
);
1893 glUniform1f( SHADER_UNIFORM( shader_tile_main
, "uGhost" ), 0.0f
);
1894 glUniform1f( SHADER_UNIFORM( shader_tile_main
, "uForeground" ), 0.0f
);
1897 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
1898 glBlendEquation(GL_FUNC_ADD
);
1901 vg_tex2d_bind( &tex_tile_data
, 0 );
1902 glUniform1i( SHADER_UNIFORM( shader_tile_main
, "uTexGlyphs" ), 0 );
1904 vg_tex2d_bind( &tex_wood
, 1 );
1905 glUniform1i( SHADER_UNIFORM( shader_tile_main
, "uTexWood" ), 1 );
1907 render_tiles( NULL
, NULL
, colour_default
, colour_default
);
1911 SHADER_USE( shader_ball
);
1912 glUniformMatrix3fv( SHADER_UNIFORM( shader_ball
, "uPv" ), 1, GL_FALSE
, (float *)vg_pv
);
1914 vg_tex2d_bind( &tex_ball
, 0 );
1915 glUniform1i( SHADER_UNIFORM( shader_ball
, "uTexMain" ), 0 );
1918 if( world
.simulating
)
1920 for( int i
= 0; i
< world
.num_fishes
; i
++ )
1922 struct fish
*fish
= &world
.fishes
[i
];
1924 if( fish
->state
== k_fish_state_dead
|| fish
->state
== k_fish_state_bg
)
1927 if( fish
->state
== k_fish_state_soon_dead
&& (world
.frame_lerp
> fish
->death_time
) )
1930 v4f dot_colour
= { 0.0f
, 0.0f
, 0.0f
, 1.0f
};
1931 colour_code_v3( fish
->payload
, dot_colour
);
1933 glUniform3fv( SHADER_UNIFORM( shader_ball
, "uColour" ), 1, dot_colour
);
1934 glUniform2fv( SHADER_UNIFORM( shader_ball
, "uOffset" ), 1, fish
->physics_co
);
1939 SHADER_USE( shader_tile_main
);
1942 vg_tex2d_bind( &tex_tile_data
, 0 );
1943 glUniform1i( SHADER_UNIFORM( shader_tile_main
, "uTexGlyphs" ), 0 );
1945 vg_tex2d_bind( &tex_wood
, 1 );
1946 glUniform1i( SHADER_UNIFORM( shader_tile_main
, "uTexWood" ), 1 );
1948 glUniform1f( SHADER_UNIFORM( shader_tile_main
, "uForeground" ), 1.0f
);
1949 render_tiles( NULL
, NULL
, colour_default
, colour_selected
);
1952 for( int y
= 2; y
< world
.h
-2; y
++ )
1954 for( int x
= 2; x
< world
.w
-2; x
++ )
1956 struct cell
*cell
= pcell((v2i
){x
,y
});
1958 if( cell
->state
& FLAG_CANAL
)
1960 if( cell
->config
== k_cell_type_split
)
1962 float rotation
= cell
->state
& FLAG_FLIP_FLOP
? vg_rad( -45.0f
): vg_rad( 45.0f
);
1964 if( cell
->state
& FLAG_FLIP_ROTATING
)
1966 if( (world
.frame_lerp
> curve_7_linear_section
) )
1968 float const rotation_speed
= 0.4f
;
1969 if( (world
.frame_lerp
< 1.0f
-rotation_speed
) )
1971 float t
= world
.frame_lerp
- curve_7_linear_section
;
1972 t
*= -2.0f
* (1.0f
/(1.0f
-(curve_7_linear_section
+rotation_speed
)));
1982 m2x2_create_rotation( subtransform
, rotation
);
1984 glUniformMatrix2fv( SHADER_UNIFORM( shader_tile_main
, "uSubTransform" ), 1, GL_FALSE
, (float *)subtransform
);
1985 glUniform4f( SHADER_UNIFORM( shader_tile_main
, "uOffset" ), (float)x
, (float)y
+ 0.125f
, cell
->state
& FLAG_TARGETED
? 3.0f
: 0.0f
, 0.0f
);
1993 if( world
.selected
!= -1 && !(world
.data
[ world
.selected
].state
& FLAG_CANAL
) && !id_drag_from
)
1995 v2i new_begin
= { world
.tile_x
- 2, world
.tile_y
- 2 };
1996 v2i new_end
= { world
.tile_x
+ 2, world
.tile_y
+ 2 };
1998 world
.data
[ world
.selected
].state
^= FLAG_CANAL
;
1999 map_reclassify( new_begin
, new_end
, 0 );
2001 m2x2_identity( subtransform
);
2002 glUniform1f( SHADER_UNIFORM( shader_tile_main
, "uGhost" ), 1.0f
);
2003 glUniformMatrix2fv( SHADER_UNIFORM( shader_tile_main
, "uSubTransform" ), 1, GL_FALSE
, (float *)subtransform
);
2004 glUniform2fv( SHADER_UNIFORM( shader_tile_main
, "uMousePos" ), 1, world
.tile_pos
);
2006 render_tiles( new_begin
, new_end
, colour_default
, colour_default
);
2008 world
.data
[ world
.selected
].state
^= FLAG_CANAL
;
2009 map_reclassify( new_begin
, new_end
, 0 );
2012 //glDisable(GL_BLEND);
2014 // Draw connecting wires
2015 glDisable(GL_BLEND
);
2017 SHADER_USE( shader_wire
);
2018 glBindVertexArray( world
.wire
.vao
);
2020 glUniformMatrix3fv( SHADER_UNIFORM( shader_wire
, "uPv" ), 1, GL_FALSE
, (float *)vg_pv
);
2021 glUniform4f( SHADER_UNIFORM( shader_wire
, "uColour" ), 0.2f
, 0.2f
, 0.2f
, 1.0f
);
2025 glUniform1f( SHADER_UNIFORM( shader_wire
, "uCurve" ), 0.4f
);
2026 glUniform3f( SHADER_UNIFORM( shader_wire
, "uStart" ), drag_from_co
[0], drag_from_co
[1], 0.06f
);
2027 glUniform3f( SHADER_UNIFORM( shader_wire
, "uEnd" ), drag_to_co
[0], drag_to_co
[1], 0.06f
);
2028 glDrawElements( GL_TRIANGLES
, world
.wire
.em
, GL_UNSIGNED_SHORT
, (void*)(0) );
2031 float rp_x1
= world
.frame_lerp
*9.0f
;
2032 float rp_x2
= 1.0f
-rp_x1
*expf(1.0f
-rp_x1
)* 0.36f
;
2034 for( int y
= 2; y
< world
.h
-2; y
++ )
2036 for( int x
= 2; x
< world
.w
-2; x
++ )
2038 struct cell
*cell
= pcell((v2i
){x
,y
});
2040 if( cell
->state
& FLAG_CANAL
)
2042 if( cell
->state
& FLAG_IS_TRIGGER
)
2044 int trigger_id
= cell
->links
[0]?0:1;
2046 int x2
= cell
->links
[trigger_id
] % world
.w
;
2047 int y2
= (cell
->links
[trigger_id
] - x2
) / world
.w
;
2052 startpoint
[0] = (float)x2
+ (trigger_id
? 0.75f
: 0.25f
);
2053 startpoint
[1] = (float)y2
+ 0.25f
;
2055 endpoint
[0] = x
+0.5f
;
2056 endpoint
[1] = y
+0.5f
;
2058 glUniform1f( SHADER_UNIFORM( shader_wire
, "uCurve" ), cell
->state
& FLAG_TRIGGERED
? rp_x2
* 0.4f
: 0.4f
);
2059 glUniform3f( SHADER_UNIFORM( shader_wire
, "uStart" ), startpoint
[0], startpoint
[1], 0.04f
);
2060 glUniform3f( SHADER_UNIFORM( shader_wire
, "uEnd" ), endpoint
[0], endpoint
[1], 0.04f
);
2061 glDrawElements( GL_TRIANGLES
, world
.wire
.em
, GL_UNSIGNED_SHORT
, (void*)(0) );
2067 SHADER_USE( shader_tile_colour
);
2068 glUniformMatrix3fv( SHADER_UNIFORM( shader_tile_colour
, "uPv" ), 1, GL_FALSE
, (float *)vg_pv
);
2069 use_mesh( &world
.circle
);
2071 int const filled_start
= 0;
2072 int const filled_count
= 32;
2073 int const empty_start
= 32;
2074 int const empty_count
= 32*2;
2077 for( int i
= 0; i
< arrlen( world
.io
); i
++ )
2079 struct cell_terminal
*term
= &world
.io
[ i
];
2080 int posx
= term
->id
% world
.w
;
2081 int posy
= (term
->id
- posx
)/world
.w
;
2082 int is_input
= world
.data
[ term
->id
].state
& FLAG_INPUT
;
2084 v4f dot_colour
= { 0.0f
, 0.0f
, 0.0f
, 1.0f
};
2086 for( int k
= 0; k
< term
->run_count
; k
++ )
2088 for( int j
= 0; j
< term
->runs
[k
].condition_count
; j
++ )
2090 float y_offset
= is_input
? 1.2f
: -0.2f
;
2091 y_offset
+= (is_input
? 0.2f
: -0.2f
) * (float)k
;
2093 glUniform3f( SHADER_UNIFORM( shader_tile_colour
, "uOffset" ), (float)posx
+ 0.2f
+ 0.2f
* (float)j
, (float)posy
+ y_offset
, 0.1f
);
2097 colour_code_v3( term
->runs
[k
].conditions
[j
], dot_colour
);
2098 glUniform4fv( SHADER_UNIFORM( shader_tile_colour
, "uColour" ), 1, dot_colour
);
2100 // Draw filled if tick not passed, draw empty if empty
2101 if( (world
.sim_frame
> j
&& world
.sim_run
>= k
) || world
.sim_run
> k
)
2102 draw_mesh( empty_start
, empty_count
);
2104 draw_mesh( filled_start
, filled_count
);
2108 if( term
->runs
[k
].recv_count
> j
)
2110 colour_code_v3( term
->runs
[k
].recieved
[j
], dot_colour
);
2111 v3_muls( dot_colour
, 0.8f
, dot_colour
);
2112 glUniform4fv( SHADER_UNIFORM( shader_tile_colour
, "uColour" ), 1, dot_colour
);
2114 draw_mesh( filled_start
, filled_count
);
2117 colour_code_v3( term
->runs
[k
].conditions
[j
], dot_colour
);
2118 glUniform4fv( SHADER_UNIFORM( shader_tile_colour
, "uColour" ), 1, dot_colour
);
2120 draw_mesh( empty_start
, empty_count
);
2126 if( world
.simulating
)
2128 glUniform4f( SHADER_UNIFORM( shader_tile_colour
, "uColour" ), 0.0f
, 0.0f
, 0.0f
, 1.0f
);
2129 glUniform3f( SHADER_UNIFORM( shader_tile_colour
, "uOffset" ), -0.5f
+ cosf( vg_time
* 4.0f
) * 0.2f
, sinf( vg_time
* 4.0f
) * 0.2f
+ (float)world
.h
* 0.5f
, 0.05f
);
2130 draw_mesh( filled_start
, filled_count
);
2134 float const score_bright
= 1.25f
;
2135 glUniform4f( SHADER_UNIFORM( shader_tile_colour
, "uColour" ),
2136 0.4f
*score_bright
, 0.39f
*score_bright
, 0.45f
*score_bright
, 1.0f
);
2138 use_mesh( &world
.numbers
);
2139 draw_numbers( (v3f
){ 2.0f
, (float)world
.h
-1.875f
, 0.3333f
}, world
.score
);
2141 // Level selection UI
2142 use_mesh( &world
.circle
);
2143 float ratio
= ((float)vg_window_x
/(float)vg_window_y
);
2145 m3x3f ui_view
= M3X3_IDENTITY
;
2146 m3x3_scale( ui_view
, (v3f
){ 1.0f
, ratio
, 1.0f
} );
2147 glUniformMatrix3fv( SHADER_UNIFORM( shader_tile_colour
, "uPv" ), 1, GL_FALSE
, (float *)ui_view
);
2149 // Calculate mouse in UIsp
2150 v3f mouse_ui_space
= { ((float)vg_mouse
[0] / (float)(vg_window_x
)) * 2.0f
- 1.0f
,
2151 (((float)vg_mouse
[1] / (float)(vg_window_y
)) * 2.0f
- 1.0f
)*(-1.0f
/ratio
), 0.0125f
};
2153 // Get selected level
2154 const float selection_scale
= 0.05f
;
2155 int const level_count
= vg_list_size( level_pack_1
);
2156 int level_select
= -1;
2158 if( mouse_ui_space
[0] <= -0.8f
)
2160 float levels_range
= (float)level_count
*selection_scale
*0.6f
;
2161 float level_offset
= ((-mouse_ui_space
[1] + levels_range
) / levels_range
) * 0.5f
* (float)level_count
;
2162 level_select
= ceilf( level_offset
);
2165 if( level_select
>= 0 && level_select
< vg_list_size( level_pack_1
) )
2167 glUniform4f( SHADER_UNIFORM( shader_tile_colour
, "uColour" ), 0.369768f
, 0.3654f
, 0.42f
, 1.0f
);
2169 use_mesh( &world
.tile
);
2170 glUniform3f( SHADER_UNIFORM( shader_tile_colour
, "uOffset" ),
2172 ((float)level_count
- (float)level_select
* 2.0f
) * selection_scale
* 0.6f
,
2177 use_mesh( &world
.circle
);
2179 if( vg_get_button_down( "primary" ) )
2181 console_changelevel( 1, level_pack_1
+ level_select
);
2185 else mouse_ui_space
[1] = INFINITY
;
2187 glUniform4f( SHADER_UNIFORM( shader_tile_colour
, "uColour" ), 0.4f
, 0.39f
, 0.45f
, 1.0f
);
2190 for( int i
= 0; i
< level_count
; i
++ )
2192 struct career_level
*clevel
= &career
.levels
[i
];
2194 v3f level_ui_space
= {
2196 ((float)level_count
- (float)i
* 2.0f
) * selection_scale
* 0.6f
+ selection_scale
* 0.5f
,
2197 selection_scale
* 0.5f
2200 float scale
= vg_clampf( 1.0f
- fabsf(level_ui_space
[1] - mouse_ui_space
[1]) * 2.0f
, 0.9f
, 1.0f
);
2201 level_ui_space
[2] *= scale
;
2203 glUniform3fv( SHADER_UNIFORM( shader_tile_colour
, "uOffset" ), 1, level_ui_space
);
2205 if( clevel
->completed
)
2206 draw_mesh( filled_start
, filled_count
);
2208 draw_mesh( empty_start
, empty_count
);
2212 glUniform4f( SHADER_UNIFORM( shader_tile_colour
, "uColour" ), 0.4f
*1.25f
, 0.39f
*1.25f
, 0.45f
*1.25f
, 1.0f
);
2214 use_mesh( &world
.numbers
);
2215 for( int i
= 0; i
< level_count
; i
++ )
2217 struct career_level
*clevel
= &career
.levels
[i
];
2219 v3f level_ui_space
= {
2221 ((float)level_count
- (float)i
* 2.0f
) * selection_scale
* 0.6f
+ selection_scale
* 0.5f
,
2225 if( clevel
->completed
)
2226 draw_numbers( level_ui_space
, clevel
->score
);
2228 level_ui_space
[0] = -0.975f
;
2229 level_ui_space
[1] -= 0.01f
;
2230 draw_numbers( level_ui_space
, i
);
2233 //use_mesh( &world.numbers );
2234 //draw_numbers( (v3f){ 0.0f, -0.5f, 0.1f }, 128765 );