1 // Copyright (C) 2021 Harry Godden (hgn) - All Rights Reserved
4 //#define VG_STEAM_APPID 1218140U
6 #include "fishladder_resources.h"
8 const char *level_pack_1
[] = {
33 levels
[ vg_list_size( level_pack_1
) ];
35 career
= { .version
= 1 };
38 static void career_serialize(void)
40 vg_asset_write( "sav/game.sav", &career
, sizeof( struct career_state
) );
43 static void career_load(void)
46 struct career_state
*cr
= vg_asset_read_s( "sav/game.sav", &sz
);
48 memset( (void*)career
.levels
, 0, vg_list_size(level_pack_1
) * sizeof(struct career_level
) );
52 if( sz
> sizeof( struct career_state
) )
53 vg_warn( "This save file is too big! Some levels will be lost\n" );
55 if( sz
<= offsetof( struct career_state
, levels
) )
57 vg_error( "This save file is too small to have a header\n" );
62 u32
const size_header
= offsetof(struct career_state
, levels
);
63 u32
const size_levels
= sizeof(struct career_state
)-size_header
;
64 u32
const size_levels_input
= sz
- size_header
;
66 memcpy( (void*)career
.levels
, (void*)cr
->levels
, size_levels_input
);
68 if( sz
< sizeof( struct career_state
) )
70 memset( ((void*)career
.levels
) + size_levels_input
, 0, size_levels
-size_levels_input
);
74 vg_success( "Loaded save file... Info:\n" );
76 for( int i
= 0; i
< vg_list_size( career
.levels
); i
++ )
78 struct career_level
*lvl
= &career
.levels
[i
];
79 vg_info( "Score: %u, Time: %u, Completed: %u\n", lvl
->score
, lvl
->time
, lvl
->completed
);
84 vg_info( "No save file... Using blank one\n" );
92 #define FLAG_CANAL 0x1
93 #define FLAG_IS_TRIGGER 0x2
94 #define FLAG_RESERVED0 0x4
95 #define FLAG_RESERVED1 0x8
97 #define FLAG_INPUT 0x10
98 #define FLAG_OUTPUT 0x20
99 #define FLAG_WALL 0x40
101 #define FLAG_FLIP_FLOP 0x100
102 #define FLAG_TRIGGERED 0x200
103 #define FLAG_FLIP_ROTATING 0x400
104 #define FLAG_TARGETED 0x800
107 0000 0 | 0001 1 | 0010 2 | 0011 3
111 0100 4 | 0101 5 | 0110 6 | 0111 7
115 1000 8 | 1001 9 | 1010 10 | 1011 11
119 1100 12 | 1101 13 | 1110 14 | 1111 15
125 struct cell_description
133 cell_descriptions
[] =
137 { .start
= { 1, 0 }, .end
= { -1, 0 } },
138 { .start
= { 0, 1 }, .end
= { 0, -1 } },
139 { .start
= { 0, 1 }, .end
= { 1, 0 } },
141 { .start
= { -1, 0 }, .end
= { 1, 0 } },
142 { .start
= { -1, 0 }, .end
= { 1, 0 }, .is_linear
= 1 },
143 { .start
= { 0, 1 }, .end
= { -1, 0 } },
144 { .start
= { 0, 1 }, .is_special
= 1 },
146 { .start
= { 0, -1 }, .end
= { 0, 1 } },
147 { .start
= { 1, 0 }, .end
= { 0, -1 } },
148 { .start
= { 0, 1 }, .end
= { 0, -1 }, .is_linear
= 1 },
151 { .start
= { -1, 0 }, .end
= { 0, -1 } },
152 { .end
= { 0, -1 }, .is_special
= 1 },
159 k_cell_type_stub
= 0,
160 k_cell_type_ramp_right
= 3,
161 k_cell_type_ramp_left
= 6,
162 k_cell_type_split
= 7,
163 k_cell_type_merge
= 13,
164 k_cell_type_con_r
= 1,
165 k_cell_type_con_u
= 2,
166 k_cell_type_con_l
= 4,
167 k_cell_type_con_d
= 8
170 v2f
const curve_3
[] = {{0.5f
,1.0f
},{0.5f
,0.625f
},{0.625f
,0.5f
},{1.0f
,0.5f
}};
171 v2f
const curve_6
[] = {{0.5f
,1.0f
},{0.5f
,0.625f
},{0.375f
,0.5f
},{0.0f
,0.5f
}};
172 v2f
const curve_9
[] = {{1.0f
,0.5f
},{0.625f
,0.5f
},{0.5f
,0.375f
},{0.5f
,0.0f
}};
173 v2f
const curve_12
[]= {{0.0f
,0.5f
},{0.375f
,0.5f
},{0.5f
,0.375f
},{0.5f
,0.0f
}};
175 v2f
const curve_1
[] = {{1.0f
,0.5f
},{0.8f
,0.5f
},{0.3f
,0.5f
},{0.2f
,0.5f
}};
176 v2f
const curve_4
[] = {{0.0f
,0.5f
},{0.3f
,0.5f
},{0.5f
,0.5f
},{0.8f
,0.5f
}};
177 v2f
const curve_2
[] = {{0.5f
,1.0f
},{0.5f
,0.8f
},{0.5f
,0.3f
},{0.5f
,0.2f
}};
178 v2f
const curve_8
[] = {{0.5f
,0.0f
},{0.5f
,0.3f
},{0.5f
,0.5f
},{0.5f
,0.8f
}};
180 v2f
const curve_7
[] = {{0.5f
,0.8438f
},{0.875f
,0.8438f
},{0.625f
,0.5f
},{1.0f
,0.5f
}};
181 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
}};
183 float const curve_7_linear_section
= 0.1562f
;
186 { { 1.0f
, 0.9f
, 0.3f
},
187 { 0.2f
, 0.9f
, 0.14f
},
188 { 0.4f
, 0.8f
, 1.00f
} };
190 static void colour_code_v3( char cc
, v3f target
)
192 if( cc
>= 'a' && cc
<= 'z' )
196 if( id
< vg_list_size( colour_sets
) )
198 v3_copy( colour_sets
[ id
], target
);
203 v3_copy( (v3f
){0.0f
,0.0f
,0.0f
}, target
);
212 static void init_mesh( struct mesh
*m
, float const *tris
, u32 length
)
214 m
->elements
= length
/3;
215 glGenVertexArrays( 1, &m
->vao
);
216 glGenBuffers( 1, &m
->vbo
);
218 glBindVertexArray( m
->vao
);
219 glBindBuffer( GL_ARRAY_BUFFER
, m
->vbo
);
220 glBufferData( GL_ARRAY_BUFFER
, length
*sizeof(float), tris
, GL_STATIC_DRAW
);
222 glVertexAttribPointer( 0, 2, GL_FLOAT
, GL_FALSE
, 2 * sizeof(float), (void*)0 );
223 glEnableVertexAttribArray( 0 );
228 static void free_mesh( struct mesh
*m
)
230 glDeleteVertexArrays( 1, &m
->vao
);
231 glDeleteBuffers( 1, &m
->vbo
);
234 static void draw_mesh( int const start
, int const count
)
236 glDrawArrays( GL_TRIANGLES
, start
*3, count
*3 );
239 static void use_mesh( struct mesh
*m
)
241 glBindVertexArray( m
->vao
);
246 k_fish_state_soon_dead
= -1,
247 k_fish_state_dead
= 0,
250 k_fish_state_soon_alive
273 int sim_run
, max_runs
;
288 int condition_count
, recv_count
;
300 struct mesh tile
, circle
, numbers
;
303 GLuint vao
, vbo
, ebo
;
308 GLuint background_data
;
309 GLuint random_samples
;
311 int selected
, tile_x
, tile_y
;
318 enum e_fish_state state
;
330 struct career_level
*ptr_career_level
;
338 static void map_free(void)
340 arrfree( world
.data
);
351 world
.initialzed
= 0;
354 static void io_reset(void)
356 for( int i
= 0; i
< arrlen( world
.io
); i
++ )
358 struct cell_terminal
*term
= &world
.io
[i
];
360 for( int j
= 0; j
< term
->run_count
; j
++ )
361 term
->runs
[j
].recv_count
= 0;
365 static void map_reclassify( v2i start
, v2i end
, int update_texbuffer
);
366 static int map_load( const char *str
, const char *name
)
368 //TODO: It may be worthwhile, at this point, to switch to binary encoding for save data
377 if( str
[world
.w
] == ';' )
379 else if( !str
[world
.w
] )
381 vg_error( "Unexpected EOF when parsing level\n" );
386 struct cell
*row
= arraddnptr( world
.data
, world
.w
);
388 int reg_start
= 0, reg_end
= 0;
390 u32
*links_to_make
= NULL
;
391 int links_satisfied
= 0;
393 char link_id_buffer
[32];
401 if( *c
== '\r' ) { c
++; continue; } // fuck off windows
407 if( *c
== '\r' ) c
++;
414 if( *c
== '\r' ) { c
++; continue; }
416 if( reg_start
< reg_end
)
418 struct cell_terminal
*terminal
= &world
.io
[ reg_start
];
419 struct terminal_run
*run
= &terminal
->runs
[ terminal
->run_count
-1 ];
421 if( *c
>= 'a' && *c
<= 'z' )
423 run
->conditions
[ run
->condition_count
++ ] = *c
;
427 if( *c
== ',' || *c
== '\n' )
436 terminal
->runs
[ terminal
->run_count
].condition_count
= 0;
437 terminal
->run_count
++;
438 world
.max_runs
= vg_max( world
.max_runs
, terminal
->run_count
);
442 vg_error( "Unkown attribute '%c' (row: %u)\n", *c
, world
.h
);
449 if( links_satisfied
< arrlen( links_to_make
) )
451 struct cell
*target
= &world
.data
[ links_to_make
[ links_satisfied
] ];
453 if( (((u32
)*c
>= (u32
)'0') && ((u32
)*c
<= (u32
)'9')) || *c
== '-' )
455 if( link_id_n
>= vg_list_size( link_id_buffer
)-1 )
457 vg_error( "Number was way too long to be parsed (row: %u)\n", world
.h
);
461 link_id_buffer
[ link_id_n
++ ] = *c
;
463 else if( *c
== ',' || *c
== '\n' )
465 link_id_buffer
[ link_id_n
] = 0x00;
466 int value
= atoi( link_id_buffer
);
468 target
->links
[value
>= 0? 1:0] = abs(value
);
477 vg_error( "Invalid character '%c' (row: %u)\n", *c
, world
.h
);
483 vg_error( "Too many values to assign (row: %u)\n", world
.h
);
492 // Registry length-error checks
493 if( reg_start
!= reg_end
)
495 vg_error( "Not enough spawn values assigned (row: %u, %u of %u)\n", world
.h
, reg_start
, reg_end
);
499 if( links_satisfied
!= arrlen( links_to_make
) )
501 vg_error( "Not enough link values assigned (row: %u, %u of %u)\n", world
.h
, links_satisfied
, arrlen( links_to_make
) );
507 vg_error( "Not enough cells to match previous row definition (row: %u, %u<%u)\n", world
.h
, cx
, world
.w
);
511 row
= arraddnptr( world
.data
, world
.w
);
514 reg_end
= reg_start
= arrlen( world
.io
);
516 arrsetlen( links_to_make
, 0 );
523 vg_error( "Too many cells to match previous row definition (row: %u, %u>%u)\n", world
.h
, cx
, world
.w
);
527 // Tile initialization
529 struct cell
*cell
= &row
[ cx
];
532 if( *c
== '+' || *c
== '-' )
534 struct cell_terminal
*term
= arraddnptr( world
.io
, 1 );
535 term
->id
= cx
+ world
.h
*world
.w
;
537 term
->runs
[0].condition_count
= 0;
539 cell
->state
= *c
== '+'? FLAG_INPUT
: FLAG_OUTPUT
;
542 else if( *c
== '#' ) cell
->state
= FLAG_WALL
;
543 else if( ((u32
)*c
>= (u32
)'A') && ((u32
)*c
<= (u32
)'A'+0xf) )
545 // Canal flag bits (4bit/16 value):
551 cell
->state
= ((u32
)*c
- (u32
)'A') & (FLAG_CANAL
|FLAG_IS_TRIGGER
);
553 if( cell
->state
& FLAG_IS_TRIGGER
)
554 arrpush( links_to_make
, cx
+ world
.h
*world
.w
);
560 else cell
->state
= 0x00;
568 // Update data texture to fill out the background
570 u8 info_buffer
[64*64*4];
571 for( int i
= 0; i
< 64*64; i
++ )
573 u8
*px
= &info_buffer
[i
*4];
580 glBindTexture( GL_TEXTURE_2D
, world
.background_data
);
581 glTexSubImage2D( GL_TEXTURE_2D
, 0, 0, 0, 64, 64, GL_RGBA
, GL_UNSIGNED_BYTE
, info_buffer
);
584 arrfree( links_to_make
);
586 map_reclassify( NULL
, NULL
, 1 );
589 for( int i
= 0; i
< world
.h
*world
.w
; i
++ )
591 struct cell
*src
= &world
.data
[i
];
592 if( src
->state
& FLAG_IS_TRIGGER
)
594 int link_id
= src
->links
[0]?0:1;
595 if( src
->links
[link_id
] <= world
.h
*world
.w
)
597 struct cell
*target
= &world
.data
[ src
->links
[link_id
] ];
598 if( (target
->state
& FLAG_CANAL
) && (target
->config
== k_cell_type_split
) )
600 if( target
->links
[ link_id
] )
602 vg_error( "Link target was already targeted\n" );
608 target
->links
[ link_id
] = i
;
609 target
->state
|= FLAG_TARGETED
;
614 vg_error( "Link target was invalid\n" );
620 vg_error( "Link target out of bounds\n" );
626 vg_success( "Map '%s' loaded! (%u:%u)\n", name
, world
.w
, world
.h
);
630 strncpy( world
.map_name
, name
, vg_list_size( world
.map_name
)-1 );
631 world
.initialzed
= 1;
635 arrfree( links_to_make
);
640 static struct cell
*pcell( v2i pos
)
642 return &world
.data
[ pos
[1]*world
.w
+ pos
[0] ];
645 static void map_serialize( FILE *stream
)
647 for( int y
= 0; y
< world
.h
; y
++ )
649 for( int x
= 0; x
< world
.w
; x
++ )
651 struct cell
*cell
= pcell( (v2i
){ x
, y
} );
653 if( cell
->state
& FLAG_WALL
) fputc( '#', stream
);
654 else if( cell
->state
& FLAG_INPUT
) fputc( '+', stream
);
655 else if( cell
->state
& FLAG_OUTPUT
) fputc( '-', stream
);
656 else if( cell
->state
& (FLAG_CANAL
|FLAG_IS_TRIGGER
|FLAG_RESERVED0
|FLAG_RESERVED1
) )
658 fputc( (cell
->state
& (FLAG_CANAL
|FLAG_IS_TRIGGER
|FLAG_RESERVED0
|FLAG_RESERVED1
)) + (u32
)'A', stream
);
660 else fputc( ' ', stream
);
663 fputc( ';', stream
);
665 int terminal_write_count
= 0;
667 for( int x
= 0; x
< world
.w
; x
++ )
669 for( int i
= 0; i
< arrlen( world
.io
); i
++ )
671 struct cell_terminal
*term
= &world
.io
[i
];
672 if( term
->id
== y
*world
.w
+x
)
674 if( terminal_write_count
)
675 fputc( ',', stream
);
676 terminal_write_count
++;
678 for( int j
= 0; j
< term
->run_count
; j
++ )
680 struct terminal_run
*run
= &term
->runs
[j
];
682 for( int k
= 0; k
< run
->condition_count
; k
++ )
683 fputc( run
->conditions
[k
], stream
);
685 if( j
< term
->run_count
-1 )
686 fputc( ':', stream
);
692 for( int x
= 0; x
< world
.w
; x
++ )
694 struct cell
*cell
= pcell( (v2i
){ x
,y
} );
695 if( cell
->state
& FLAG_IS_TRIGGER
)
697 if( terminal_write_count
)
698 fputc( ',', stream
);
699 terminal_write_count
++;
701 fprintf( stream
, "%d", cell
->links
[0]? -cell
->links
[0]: cell
->links
[1] );
705 fputc( '\n', stream
);
709 int main( int argc
, char *argv
[] )
711 vg_init( argc
, argv
, "Marble Computing | SPACE: Test | LeftClick: Toggle tile | RightClick: Drag wire" );
714 static int console_credits( int argc
, char const *argv
[] )
716 vg_info( "Aknowledgements:\n" );
717 vg_info( " GLFW zlib/libpng glfw.org\n" );
718 vg_info( " miniaudio MIT0 miniaud.io\n" );
719 vg_info( " QOI MIT phoboslab.org\n" );
720 vg_info( " STB library MIT nothings.org\n" );
721 vg_info( " Weiholmir font justfredrik.itch.io\n" );
725 static int console_save_map( int argc
, char const *argv
[] )
727 if( !world
.initialzed
)
729 vg_error( "Tried to save uninitialized map!\n" );
733 char map_path
[ 256 ];
735 strcpy( map_path
, "sav/" );
736 strcat( map_path
, world
.map_name
);
737 strcat( map_path
, ".map" );
739 FILE *test_writer
= fopen( map_path
, "wb" );
742 vg_info( "Saving map to '%s'\n", map_path
);
743 map_serialize( test_writer
);
745 fclose( test_writer
);
750 vg_error( "Unable to open stream for writing\n" );
755 static int console_load_map( int argc
, char const *argv
[] )
757 char map_path
[ 256 ];
762 strcpy( map_path
, "sav/" );
763 strcat( map_path
, argv
[0] );
764 strcat( map_path
, ".map" );
766 char *text_source
= vg_textasset_read( map_path
);
770 strcpy( map_path
, "maps/" );
771 strcat( map_path
, argv
[0] );
772 strcat( map_path
, ".map" );
774 text_source
= vg_textasset_read( map_path
);
779 vg_info( "Loading map: '%s'\n", map_path
);
780 world
.ptr_career_level
= NULL
;
782 if( !map_load( text_source
, argv
[0] ) )
790 for( int i
= 0; i
< vg_list_size( level_pack_1
); i
++ )
792 if( !strcmp( level_pack_1
[i
], argv
[0] ) )
794 world
.ptr_career_level
= career
.levels
+ i
;
803 vg_error( "Missing maps '%s'\n", argv
[0] );
809 vg_error( "Missing argument <map_path>\n" );
814 static void simulation_stop(void)
816 world
.simulating
= 0;
817 world
.num_fishes
= 0;
823 sfx_system_fadeout( &audio_system_balls_rolling
, 44100 );
825 vg_info( "Stopping simulation!\n" );
828 static int console_changelevel( int argc
, char const *argv
[] )
832 // Save current level
833 console_save_map( 0, NULL
);
834 if( console_load_map( argc
, argv
) )
842 vg_error( "Missing argument <map_path>\n" );
850 vg_function_push( (struct vg_cmd
){
851 .name
= "_map_write",
852 .function
= console_save_map
855 vg_function_push( (struct vg_cmd
){
857 .function
= console_load_map
860 vg_function_push( (struct vg_cmd
){
862 .function
= console_changelevel
865 vg_function_push( (struct vg_cmd
){
867 .function
= console_credits
874 0.0f
, 0.0f
, 0.0f
, 1.0f
, 1.0f
, 1.0f
,
875 0.0f
, 0.0f
, 1.0f
, 1.0f
, 1.0f
, 0.0f
,
877 0.0f
, 0.0f
, 0.0f
, 1.0f
, 4.0f
, 1.0f
,
878 0.0f
, 0.0f
, 4.0f
, 1.0f
, 4.0f
, 0.0f
881 init_mesh( &world
.tile
, quad_mesh
, vg_list_size(quad_mesh
) );
886 float circle_mesh
[32*6*3];
887 int res
= vg_list_size( circle_mesh
) / (6*3);
889 for( int i
= 0; i
< res
; i
++ )
891 v2f v0
= { sinf( ((float)i
/(float)res
)*VG_TAUf
), cosf( ((float)i
/(float)res
)*VG_TAUf
) };
892 v2f v1
= { sinf( ((float)(i
+1)/(float)res
)*VG_TAUf
), cosf( ((float)(i
+1)/(float)res
)*VG_TAUf
) };
894 circle_mesh
[ i
*6+0 ] = 0.0f
;
895 circle_mesh
[ i
*6+1 ] = 0.0f
;
897 v2_copy( v0
, circle_mesh
+ 32*6 + i
*12 );
898 v2_muls( v0
, 0.8f
, circle_mesh
+ 32*6 + i
*12+2 );
899 v2_copy( v1
, circle_mesh
+ 32*6 + i
*12+4 );
901 v2_copy( v1
, circle_mesh
+ 32*6 + i
*12+6 );
902 v2_muls( v1
, 0.8f
, circle_mesh
+ 32*6 + i
*12+8 );
903 v2_muls( v0
, 0.8f
, circle_mesh
+ 32*6 + i
*12+10 );
905 v2_copy( v0
, circle_mesh
+ i
*6+4 );
906 v2_copy( v1
, circle_mesh
+ i
*6+2 );
907 v2_copy( v0
, circle_mesh
+i
*6+4 );
908 v2_copy( v1
, circle_mesh
+i
*6+2 );
911 init_mesh( &world
.circle
, circle_mesh
, vg_list_size( circle_mesh
) );
916 init_mesh( &world
.numbers
,
918 vg_list_size( MESH_NUMBERS_BUFFER
)
921 for( int i
= 0; i
< 10; i
++ )
923 vg_info( "offset: %u, length: %u\n", MESH_NUMBERS_OFFSETS
[i
][0], MESH_NUMBERS_OFFSETS
[i
][1] );
929 int const num_segments
= 64;
931 struct mesh_wire
*mw
= &world
.wire
;
933 v2f wire_points
[ num_segments
* 2 ];
934 u16 wire_indices
[ 6*(num_segments
-1) ];
936 for( int i
= 0; i
< num_segments
; i
++ )
938 float l
= (float)i
/ (float)(num_segments
-1);
940 v2_copy( (v2f
){ l
, -0.5f
}, wire_points
[i
*2+0] );
941 v2_copy( (v2f
){ l
, 0.5f
}, wire_points
[i
*2+1] );
943 if( i
< num_segments
-1 )
945 wire_indices
[ i
*6+0 ] = i
*2 + 0;
946 wire_indices
[ i
*6+1 ] = i
*2 + 1;
947 wire_indices
[ i
*6+2 ] = i
*2 + 3;
948 wire_indices
[ i
*6+3 ] = i
*2 + 0;
949 wire_indices
[ i
*6+4 ] = i
*2 + 3;
950 wire_indices
[ i
*6+5 ] = i
*2 + 2;
954 glGenVertexArrays( 1, &mw
->vao
);
955 glGenBuffers( 1, &mw
->vbo
);
956 glGenBuffers( 1, &mw
->ebo
);
957 glBindVertexArray( mw
->vao
);
959 glBindBuffer( GL_ARRAY_BUFFER
, mw
->vbo
);
961 glBufferData( GL_ARRAY_BUFFER
, sizeof( wire_points
), wire_points
, GL_STATIC_DRAW
);
962 glBindVertexArray( mw
->vao
);
964 glBindBuffer( GL_ELEMENT_ARRAY_BUFFER
, mw
->ebo
);
965 glBufferData( GL_ELEMENT_ARRAY_BUFFER
, sizeof( wire_indices
), wire_indices
, GL_STATIC_DRAW
);
968 glVertexAttribPointer( 0, 2, GL_FLOAT
, GL_FALSE
, 2*sizeof(float), (void*)0 );
969 glEnableVertexAttribArray( 0 );
973 mw
->em
= vg_list_size( wire_indices
);
976 // Create info data texture
978 glGenTextures( 1, &world
.background_data
);
979 glBindTexture( GL_TEXTURE_2D
, world
.background_data
);
980 glTexImage2D( GL_TEXTURE_2D
, 0, GL_RGBA
, 64, 64, 0, GL_RGBA
, GL_UNSIGNED_BYTE
, NULL
);
984 // Create random smaples texture
986 u8
*data
= malloc(512*512*2);
987 for( int i
= 0; i
< 512*512*2; i
++ )
988 data
[ i
] = rand()/(RAND_MAX
/255);
990 glGenTextures( 1, &world
.random_samples
);
991 glBindTexture( GL_TEXTURE_2D
, world
.random_samples
);
992 glTexImage2D( GL_TEXTURE_2D
, 0, GL_RG
, 512, 512, 0, GL_RG
, GL_UNSIGNED_BYTE
, data
);
999 resource_load_main();
1001 // Restore gamestate
1003 console_load_map( 1, level_pack_1
);
1008 console_save_map( 0, NULL
);
1011 resource_free_main();
1013 glDeleteTextures( 1, &world
.background_data
);
1014 glDeleteTextures( 1, &world
.random_samples
);
1016 glDeleteVertexArrays( 1, &world
.wire
.vao
);
1017 glDeleteBuffers( 1, &world
.wire
.vbo
);
1018 glDeleteBuffers( 1, &world
.wire
.ebo
);
1020 free_mesh( &world
.tile
);
1021 free_mesh( &world
.circle
);
1022 free_mesh( &world
.numbers
);
1027 static int world_check_pos_ok( v2i co
)
1029 return (co
[0] < 2 || co
[0] >= world
.w
-2 || co
[1] < 2 || co
[1] >= world
.h
-2)? 0: 1;
1032 static int cell_interactive( v2i co
)
1035 if( !world_check_pos_ok( co
) )
1039 if( world
.data
[ world
.w
*co
[1] + co
[0] ].state
& (FLAG_WALL
|FLAG_INPUT
|FLAG_OUTPUT
) )
1042 // List of 3x3 configurations that we do not allow
1043 static u32 invalid_src
[][9] =
1075 // Statically compile invalid configurations into bitmasks
1076 static u32 invalid
[ vg_list_size(invalid_src
) ];
1078 for( int i
= 0; i
< vg_list_size(invalid_src
); i
++ )
1082 for( int j
= 0; j
< 3; j
++ )
1083 for( int k
= 0; k
< 3; k
++ )
1084 comped
|= invalid_src
[i
][ j
*3+k
] << ((j
*5)+k
);
1086 invalid
[i
] = comped
;
1089 // Extract 5x5 grid surrounding tile
1091 for( int y
= co
[1]-2; y
< co
[1]+3; y
++ )
1092 for( int x
= co
[0]-2; x
< co
[0]+3; x
++ )
1094 struct cell
*cell
= pcell((v2i
){x
,y
});
1096 if( cell
&& (cell
->state
& (FLAG_CANAL
|FLAG_INPUT
|FLAG_OUTPUT
)) )
1097 blob
|= 0x1 << ((y
-(co
[1]-2))*5 + x
-(co
[0]-2));
1100 // Run filter over center 3x3 grid to check for invalid configurations
1101 int kernel
[] = { 0, 1, 2, 5, 6, 7, 10, 11, 12 };
1102 for( int i
= 0; i
< vg_list_size(kernel
); i
++ )
1104 if( blob
& (0x1 << (6+kernel
[i
])) )
1106 u32 window
= blob
>> kernel
[i
];
1108 for( int j
= 0; j
< vg_list_size(invalid
); j
++ )
1109 if((window
& invalid
[j
]) == invalid
[j
])
1117 static void map_reclassify( v2i start
, v2i end
, int update_texbuffer
)
1119 v2i full_start
= { 1,1 };
1120 v2i full_end
= { world
.w
-1, world
.h
-1 };
1122 if( !start
|| !end
)
1129 u8 info_buffer
[64*64*4];
1132 int px0
= vg_max( start
[0], full_start
[0] ),
1133 px1
= vg_min( end
[0], full_end
[0] ),
1134 py0
= vg_max( start
[1], full_start
[1] ),
1135 py1
= vg_min( end
[1], full_end
[1] );
1137 for( int y
= py0
; y
< py1
; y
++ )
1139 for( int x
= px0
; x
< px1
; x
++ )
1141 struct cell
*cell
= pcell((v2i
){x
,y
});
1143 v2i dirs
[] = {{1,0},{0,1},{-1,0},{0,-1}};
1148 if( cell
->state
& (FLAG_CANAL
|FLAG_INPUT
|FLAG_OUTPUT
) )
1150 for( int i
= 0; i
< vg_list_size( dirs
); i
++ )
1152 struct cell
*neighbour
= pcell((v2i
){x
+dirs
[i
][0], y
+dirs
[i
][1]});
1153 if( neighbour
->state
& (FLAG_CANAL
|FLAG_INPUT
|FLAG_OUTPUT
) )
1161 if( cell
->state
& FLAG_WALL
)
1167 pcell((v2i
){x
,y
})->config
= config
;
1169 u8
*info_px
= &info_buffer
[ (pixel_id
++)*4 ];
1170 info_px
[0] = height
;
1171 info_px
[1] = cell
->state
& FLAG_WALL
? 0: 255;
1177 ((cell
->state
& FLAG_IS_TRIGGER
) && (cell
->config
== 0xF || cell
->config
== k_cell_type_split
)) ||
1178 ((cell
->state
& FLAG_TARGETED
) && (cell
->config
!= k_cell_type_split
))
1179 ) && update_texbuffer
1181 cell
->state
&= ~(FLAG_TARGETED
|FLAG_IS_TRIGGER
);
1182 for( u32 i
= 0; i
< 2; i
++ )
1184 if( cell
->links
[i
] )
1186 struct cell
*other_ptr
= &world
.data
[ cell
->links
[i
] ];
1187 other_ptr
->links
[ i
] = 0;
1188 other_ptr
->state
&= ~FLAG_IS_TRIGGER
;
1190 if( other_ptr
->links
[ i
^ 0x1 ] == 0 )
1191 other_ptr
->state
&= ~FLAG_TARGETED
;
1201 if( update_texbuffer
)
1203 glBindTexture( GL_TEXTURE_2D
, world
.background_data
);
1204 glTexSubImage2D( GL_TEXTURE_2D
, 0, px0
+ 16, py0
+ 16, px1
-px0
, py1
-py0
, GL_RGBA
, GL_UNSIGNED_BYTE
, info_buffer
);
1208 u16 id_drag_from
= 0;
1212 void vg_update(void)
1214 // Fit within screen
1216 float r1
= (float)vg_window_y
/ (float)vg_window_x
,
1217 r2
= (float)world
.h
/ (float)world
.w
,
1220 size
= ( r2
< r1
? (float)world
.w
* 0.5f
: ((float)world
.h
* 0.5f
) / r1
) + 2.5f
;
1221 m3x3_projection( m_projection
, -size
, size
, -size
*r1
, size
*r1
);
1224 origin
[0] = floorf( -0.5f
* world
.w
);
1225 origin
[1] = floorf( -0.5f
* world
.h
);
1228 m3x3_identity( m_view
);
1229 m3x3_translate( m_view
, origin
);
1230 m3x3_mul( m_projection
, m_view
, vg_pv
);
1231 vg_projection_update();
1234 v2_copy( vg_mouse_ws
, world
.tile_pos
);
1236 world
.tile_x
= floorf( world
.tile_pos
[0] );
1237 world
.tile_y
= floorf( world
.tile_pos
[1] );
1240 if( !world
.simulating
)
1242 v2_copy( vg_mouse_ws
, drag_to_co
);
1244 if( cell_interactive( (v2i
){ world
.tile_x
, world
.tile_y
} ))
1246 world
.selected
= world
.tile_y
* world
.w
+ world
.tile_x
;
1248 static u32 modify_state
= 0;
1250 struct cell
*cell_ptr
= &world
.data
[world
.selected
];
1252 if( vg_get_button_down("primary") )
1254 modify_state
= (cell_ptr
->state
& FLAG_CANAL
) ^ FLAG_CANAL
;
1257 if( vg_get_button("primary") && ((cell_ptr
->state
& FLAG_CANAL
) != modify_state
) )
1259 cell_ptr
->state
&= ~FLAG_CANAL
;
1260 cell_ptr
->state
|= modify_state
;
1262 if( cell_ptr
->state
& FLAG_CANAL
)
1264 cell_ptr
->links
[0] = 0;
1265 cell_ptr
->links
[1] = 0;
1267 sfx_set_playrnd( &audio_tile_mod
, &audio_system_sfx
, 3, 6 );
1272 sfx_set_playrnd( &audio_tile_mod
, &audio_system_sfx
, 0, 3 );
1276 map_reclassify( (v2i
){ world
.tile_x
-2, world
.tile_y
-2 },
1277 (v2i
){ world
.tile_x
+2, world
.tile_y
+2 }, 1 );
1280 if( vg_get_button_down("secondary") && !(cell_ptr
->config
== k_cell_type_split
) )
1282 id_drag_from
= world
.selected
;
1283 drag_from_co
[0] = world
.tile_x
+ 0.5f
;
1284 drag_from_co
[1] = world
.tile_y
+ 0.5f
;
1287 if( id_drag_from
&& (cell_ptr
->config
== k_cell_type_split
) )
1289 float local_x
= vg_mouse_ws
[0] - (float)world
.tile_x
;
1290 drag_to_co
[0] = (float)world
.tile_x
+ (local_x
> 0.5f
? 0.75f
: 0.25f
);
1291 drag_to_co
[1] = (float)world
.tile_y
+ 0.25f
;
1293 if( vg_get_button_up("secondary") )
1295 struct cell
*drag_ptr
= &world
.data
[id_drag_from
];
1296 u32 link_id
= local_x
> 0.5f
? 1: 0;
1298 // Cleanup existing connections
1299 if( cell_ptr
->links
[ link_id
] )
1301 vg_warn( "Destroying existing connection on link %u (%hu)\n", link_id
, cell_ptr
->links
[ link_id
] );
1303 struct cell
*current_connection
= &world
.data
[ cell_ptr
->links
[ link_id
]];
1304 current_connection
->state
&= ~FLAG_IS_TRIGGER
;
1305 current_connection
->links
[ link_id
] = 0;
1308 if( drag_ptr
->links
[ link_id
^ 0x1 ] )
1310 vg_warn( "Destroying alternate link %u (%hu)\n", link_id
^ 0x1, drag_ptr
->links
[ link_id
^ 0x1 ] );
1312 struct cell
*current_connection
= &world
.data
[ drag_ptr
->links
[ link_id
^ 0x1 ]];
1313 if( !current_connection
->links
[ link_id
] )
1314 current_connection
->state
&= ~FLAG_TARGETED
;
1316 current_connection
->links
[ link_id
^ 0x1 ] = 0;
1317 drag_ptr
->links
[ link_id
^ 0x1 ] = 0;
1320 // Create the new connection
1321 vg_success( "Creating connection on link %u (%hu)\n", link_id
, id_drag_from
);
1323 cell_ptr
->links
[ link_id
] = id_drag_from
;
1324 drag_ptr
->links
[ link_id
] = world
.selected
;
1326 cell_ptr
->state
|= FLAG_TARGETED
;
1327 drag_ptr
->state
|= FLAG_IS_TRIGGER
;
1333 world
.selected
= -1;
1335 if( !(vg_get_button("secondary") && id_drag_from
) )
1340 world
.selected
= -1;
1344 // Simulation stop/start
1345 if( vg_get_button_down("go") )
1347 if( world
.simulating
)
1353 vg_success( "Starting simulation!\n" );
1355 sfx_set_playrnd( &audio_rolls
, &audio_system_balls_rolling
, 0, 1 );
1357 world
.simulating
= 1;
1358 world
.num_fishes
= 0;
1359 world
.sim_frame
= 0;
1360 world
.sim_start
= vg_time
;
1362 world
.sim_speed
= 2.5f
;
1364 for( int i
= 0; i
< world
.w
*world
.h
; i
++ )
1365 world
.data
[ i
].state
&= ~FLAG_FLIP_FLOP
;
1372 if( world
.simulating
)
1374 while( world
.sim_frame
< (int)((vg_time
-world
.sim_start
)*world
.sim_speed
) )
1376 //vg_info( "frame: %u\n", world.sim_frame );
1377 sfx_set_playrnd( &audio_random
, &audio_system_balls_switching
, 0, 9 );
1379 // Update splitter deltas
1380 for( int i
= 0; i
< world
.h
*world
.w
; i
++ )
1382 struct cell
*cell
= &world
.data
[i
];
1383 if( cell
->config
== k_cell_type_split
)
1385 cell
->state
&= ~FLAG_FLIP_ROTATING
;
1387 if( cell
->state
& FLAG_IS_TRIGGER
)
1388 cell
->state
&= ~FLAG_TRIGGERED
;
1391 int alive_count
= 0;
1393 // Update fish positions
1394 for( int i
= 0; i
< world
.num_fishes
; i
++ )
1396 struct fish
*fish
= &world
.fishes
[i
];
1398 if( fish
->state
== k_fish_state_soon_dead
)
1399 fish
->state
= k_fish_state_dead
;
1401 if( fish
->state
== k_fish_state_soon_alive
)
1402 fish
->state
= k_fish_state_alive
;
1404 if( fish
->state
< k_fish_state_alive
)
1407 struct cell
*cell_current
= pcell( fish
->pos
);
1409 if( fish
->state
== k_fish_state_alive
)
1412 if( cell_current
->state
& FLAG_OUTPUT
)
1414 for( int j
= 0; j
< arrlen( world
.io
); j
++ )
1416 struct cell_terminal
*term
= &world
.io
[j
];
1418 if( term
->id
== fish
->pos
[1]*world
.w
+ fish
->pos
[0] )
1420 struct terminal_run
*run
= &term
->runs
[ world
.sim_run
];
1421 if( run
->recv_count
< vg_list_size( run
->recieved
) )
1422 run
->recieved
[ run
->recv_count
++ ] = fish
->payload
;
1428 fish
->state
= k_fish_state_dead
;
1433 if( cell_current
->config
== k_cell_type_merge
)
1438 fish
->flow_reversed
= 0;
1442 if( cell_current
->config
== k_cell_type_split
)
1445 fish
->dir
[0] = cell_current
->state
&FLAG_FLIP_FLOP
?1:-1;
1448 if( !(cell_current
->state
& FLAG_TARGETED
) )
1449 cell_current
->state
^= FLAG_FLIP_FLOP
;
1453 // Apply cell out-flow
1454 struct cell_description
*desc
= &cell_descriptions
[ cell_current
->config
];
1456 v2i_copy( fish
->flow_reversed
? desc
->start
: desc
->end
, fish
->dir
);
1460 v2i_add( fish
->pos
, fish
->dir
, pos_next
);
1462 struct cell
*cell_next
= pcell( pos_next
);
1464 if( cell_next
->state
& (FLAG_CANAL
|FLAG_OUTPUT
) )
1466 struct cell_description
*desc
= &cell_descriptions
[ cell_next
->config
];
1468 if( cell_next
->config
== k_cell_type_merge
)
1470 if( fish
->dir
[0] == 0 )
1471 fish
->state
= k_fish_state_dead
;
1473 fish
->flow_reversed
= 0;
1477 if( cell_next
->config
== k_cell_type_split
)
1479 if( fish
->dir
[0] == 0 )
1481 sfx_set_playrnd( &audio_splitter
, &audio_system_balls_important
, 0, 1 );
1482 cell_next
->state
|= FLAG_FLIP_ROTATING
;
1484 fish
->flow_reversed
= 0;
1487 fish
->state
= k_fish_state_dead
;
1490 fish
->flow_reversed
= ( fish
->dir
[0] != -desc
->start
[0] ||
1491 fish
->dir
[1] != -desc
->start
[1] )? 1: 0;
1495 fish
->state
= world_check_pos_ok( fish
->pos
)? k_fish_state_bg
: k_fish_state_dead
;
1498 //v2i_add( fish->pos, fish->dir, fish->pos );
1500 else if( fish
->state
== k_fish_state_bg
)
1502 v2i_add( fish
->pos
, fish
->dir
, fish
->pos
);
1504 if( !world_check_pos_ok( fish
->pos
) )
1505 fish
->state
= k_fish_state_dead
;
1508 struct cell
*cell_entry
= pcell( fish
->pos
);
1510 if( cell_entry
->state
& FLAG_CANAL
)
1512 if( cell_entry
->config
== k_cell_type_con_r
|| cell_entry
->config
== k_cell_type_con_u
1513 || cell_entry
->config
== k_cell_type_con_l
|| cell_entry
->config
== k_cell_type_con_d
)
1515 fish
->state
= k_fish_state_soon_alive
;
1519 fish
->flow_reversed
= 1;
1521 switch( cell_entry
->config
)
1523 case k_cell_type_con_r
: fish
->dir
[0] = 1; break;
1524 case k_cell_type_con_l
: fish
->dir
[0] = -1; break;
1525 case k_cell_type_con_u
: fish
->dir
[1] = 1; break;
1526 case k_cell_type_con_d
: fish
->dir
[1] = -1; break;
1532 else { vg_error( "fish behaviour unimplemented for behaviour type (%d)\n" ); }
1534 if( fish
->state
>= k_fish_state_alive
)
1538 // Second pass (triggers)
1539 for( int i
= 0; i
< world
.num_fishes
; i
++ )
1541 struct fish
*fish
= &world
.fishes
[i
];
1543 if( fish
->state
== k_fish_state_alive
)
1545 v2i_add( fish
->pos
, fish
->dir
, fish
->pos
);
1546 struct cell
*cell_current
= pcell( fish
->pos
);
1548 if( cell_current
->state
& FLAG_IS_TRIGGER
)
1550 int trigger_id
= cell_current
->links
[0]?0:1;
1551 int connection_id
= cell_current
->links
[trigger_id
];
1552 int target_px
= connection_id
% world
.w
;
1553 int target_py
= (connection_id
- target_px
)/world
.w
;
1555 vg_line2( (v2f
){ fish
->pos
[0], fish
->pos
[1] }, (v2f
){ target_px
, target_py
}, 0xffffffff, 0xffffffff );
1557 struct cell
*target_peice
= &world
.data
[ cell_current
->links
[trigger_id
] ];
1559 cell_current
->state
|= FLAG_TRIGGERED
;
1562 target_peice
->state
|= FLAG_FLIP_FLOP
;
1564 target_peice
->state
&= ~FLAG_FLIP_FLOP
;
1569 // Third pass (collisions)
1570 struct fish
*fi
, *fj
;
1572 for( int i
= 0; i
< world
.num_fishes
; i
++ )
1574 fi
= &world
.fishes
[i
];
1576 if( fi
->state
== k_fish_state_alive
)
1578 int continue_again
= 0;
1580 for( int j
= i
+1; j
< world
.num_fishes
; j
++ )
1582 fj
= &world
.fishes
[j
];
1584 if( (fj
->state
== k_fish_state_alive
) )
1589 v2i_sub( fi
->pos
, fi
->dir
, fi_prev
);
1590 v2i_sub( fj
->pos
, fj
->dir
, fj_prev
);
1593 collide_next_frame
= (
1594 (fi
->pos
[0] == fj
->pos
[0]) &&
1595 (fi
->pos
[1] == fj
->pos
[1]))? 1: 0,
1596 collide_this_frame
= (
1597 (fi_prev
[0] == fj
->pos
[0]) &&
1598 (fi_prev
[1] == fj
->pos
[1]) &&
1599 (fj_prev
[0] == fi
->pos
[0]) &&
1600 (fj_prev
[1] == fi
->pos
[1])
1603 if( collide_next_frame
|| collide_this_frame
)
1605 // Shatter death (+0.5s)
1606 float death_time
= collide_this_frame
? 0.0f
: 0.5f
;
1608 fi
->state
= k_fish_state_soon_dead
;
1609 fj
->state
= k_fish_state_soon_dead
;
1610 fi
->death_time
= death_time
;
1611 fj
->death_time
= death_time
;
1618 if( continue_again
)
1624 for( int i
= 0; i
< arrlen( world
.io
); i
++ )
1626 struct cell_terminal
*term
= &world
.io
[ i
];
1627 int posx
= term
->id
% world
.w
;
1628 int posy
= (term
->id
- posx
)/world
.w
;
1629 int is_input
= world
.data
[ term
->id
].state
& FLAG_INPUT
;
1633 if( world
.sim_frame
< term
->runs
[ world
.sim_run
].condition_count
)
1635 struct fish
*fish
= &world
.fishes
[ world
.num_fishes
];
1636 fish
->pos
[0] = posx
;
1637 fish
->pos
[1] = posy
;
1638 fish
->state
= k_fish_state_alive
;
1639 fish
->payload
= term
->runs
[ world
.sim_run
].conditions
[ world
.sim_frame
];
1641 struct cell
*cell_ptr
= pcell( fish
->pos
);
1643 if( cell_ptr
->config
!= k_cell_type_stub
)
1645 struct cell_description
*desc
= &cell_descriptions
[ cell_ptr
->config
];
1647 v2i_copy( desc
->start
, fish
->dir
);
1648 fish
->flow_reversed
= 1;
1650 world
.num_fishes
++;
1657 if( alive_count
== 0 )
1659 world
.completed
= 1;
1661 for( int i
= 0; i
< arrlen( world
.io
); i
++ )
1663 struct cell_terminal
*term
= &world
.io
[ i
];
1664 int is_input
= world
.data
[ term
->id
].state
& FLAG_INPUT
;
1668 struct terminal_run
*run
= &term
->runs
[ world
.sim_run
];
1670 if( run
->recv_count
== run
->condition_count
)
1672 for( int j
= 0; j
< run
->condition_count
; j
++ )
1674 if( run
->recieved
[j
] != run
->conditions
[j
] )
1676 world
.completed
= 0;
1683 world
.completed
= 0;
1689 if( world
.completed
)
1691 if( world
.sim_run
< world
.max_runs
-1 )
1693 vg_success( "Run passed, starting next\n" );
1695 world
.sim_frame
= 0;
1696 world
.sim_start
= vg_time
;
1697 world
.num_fishes
= 0;
1699 for( int i
= 0; i
< world
.w
*world
.h
; i
++ )
1700 world
.data
[ i
].state
&= ~FLAG_FLIP_FLOP
;
1706 vg_success( "Level passed!\n" );
1709 for( int i
= 0; i
< world
.w
*world
.h
; i
++ )
1710 if( world
.data
[ i
].state
& FLAG_CANAL
)
1713 world
.score
= score
;
1714 world
.time
= world
.sim_frame
;
1719 vg_error( "Level failed :(\n" );
1722 // Copy into career data
1723 if( world
.ptr_career_level
)
1725 world
.ptr_career_level
->score
= world
.score
;
1726 world
.ptr_career_level
->time
= world
.time
;
1727 world
.ptr_career_level
->completed
= world
.completed
;
1730 simulation_stop(); // TODO: Async?
1737 float scaled_time
= 0.0f
;
1738 scaled_time
= (vg_time
-world
.sim_start
)*world
.sim_speed
;
1739 world
.frame_lerp
= scaled_time
- (float)world
.sim_frame
;
1742 for( int i
= 0; i
< world
.num_fishes
; i
++ )
1744 struct fish
*fish
= &world
.fishes
[i
];
1746 if( fish
->state
== k_fish_state_dead
)
1749 if( fish
->state
== k_fish_state_soon_dead
&& (world
.frame_lerp
> fish
->death_time
) )
1750 continue; // Todo: particle thing?
1752 struct cell
*cell
= pcell(fish
->pos
);
1753 struct cell_description
*desc
= &cell_descriptions
[ cell
->config
];
1757 float t
= world
.frame_lerp
;
1758 if( fish
->flow_reversed
&& !desc
->is_linear
)
1761 v2_copy( fish
->physics_co
, fish
->physics_v
);
1763 switch( cell
->config
)
1765 case k_cell_type_merge
:
1766 if( fish
->dir
[0] == 1 )
1771 case k_cell_type_con_r
: curve
= curve_1
; break;
1772 case k_cell_type_con_l
: curve
= curve_4
; break;
1773 case k_cell_type_con_u
: curve
= curve_2
; break;
1774 case k_cell_type_con_d
: curve
= curve_8
; break;
1775 case 3: curve
= curve_3
; break;
1776 case 6: curve
= curve_6
; break;
1777 case 9: curve
= curve_9
; break;
1778 case 12: curve
= curve_12
; break;
1780 if( t
> curve_7_linear_section
)
1782 t
-= curve_7_linear_section
;
1783 t
*= (1.0f
/(1.0f
-curve_7_linear_section
));
1785 curve
= cell
->state
& FLAG_FLIP_FLOP
? curve_7
: curve_7_1
;
1789 default: curve
= NULL
; break;
1795 float t3
= t
* t
* t
;
1797 float cA
= 3.0f
*t2
- 3.0f
*t3
;
1798 float cB
= 3.0f
*t3
- 6.0f
*t2
+ 3.0f
*t
;
1799 float cC
= 3.0f
*t2
- t3
- 3.0f
*t
+ 1.0f
;
1801 fish
->physics_co
[0] = t3
*curve
[3][0] + cA
*curve
[2][0] + cB
*curve
[1][0] + cC
*curve
[0][0];
1802 fish
->physics_co
[1] = t3
*curve
[3][1] + cA
*curve
[2][1] + cB
*curve
[1][1] + cC
*curve
[0][1];
1803 fish
->physics_co
[0] += (float)fish
->pos
[0];
1804 fish
->physics_co
[1] += (float)fish
->pos
[1];
1809 origin
[0] = (float)fish
->pos
[0] + (float)fish
->dir
[0]*-0.5f
+ 0.5f
;
1810 origin
[1] = (float)fish
->pos
[1] + (float)fish
->dir
[1]*-0.5f
+ 0.5f
;
1812 fish
->physics_co
[0] = origin
[0] + (float)fish
->dir
[0]*t
;
1813 fish
->physics_co
[1] = origin
[1] + (float)fish
->dir
[1]*t
;
1819 static void render_tiles( v2i start
, v2i end
, v4f
const regular_colour
, v4f
const selected_colour
)
1821 v2i full_start
= { 0,0 };
1822 v2i full_end
= { world
.w
, world
.h
};
1824 if( !start
|| !end
)
1830 glUniform4fv( SHADER_UNIFORM( shader_tile_main
, "uColour" ), 1, regular_colour
);
1832 for( int y
= start
[1]; y
< end
[1]; y
++ )
1834 for( int x
= start
[0]; x
< end
[0]; x
++ )
1836 struct cell
*cell
= pcell((v2i
){x
,y
});
1837 int selected
= world
.selected
== y
*world
.w
+ x
;
1839 int tile_offsets
[][2] =
1841 {2, 0}, {0, 3}, {0, 2}, {2, 2},
1842 {1, 0}, {2, 3}, {3, 2}, {1, 3},
1843 {3, 1}, {0, 1}, {1, 2}, {2, 1},
1844 {1, 1}, {3, 3}, {2, 1}, {2, 1}
1847 int uv
[2] = { 3, 0 };
1849 if( cell
->state
& (FLAG_CANAL
|FLAG_INPUT
|FLAG_OUTPUT
) )
1851 uv
[0] = tile_offsets
[ cell
->config
][0];
1852 uv
[1] = tile_offsets
[ cell
->config
][1];
1855 glUniform4f( SHADER_UNIFORM( shader_tile_main
, "uOffset" ), (float)x
, (float)y
, uv
[0], uv
[1] );
1858 glUniform4fv( SHADER_UNIFORM( shader_tile_main
, "uColour" ), 1, selected_colour
);
1860 glUniform4fv( SHADER_UNIFORM( shader_tile_main
, "uColour" ), 1, regular_colour
);
1868 static void draw_numbers( v3f coord
, int number
)
1871 v3_copy( coord
, pos
);
1872 int digits
[8]; int i
= 0;
1874 while( number
> 0 && i
< 8 )
1876 digits
[i
++] = number
% 10;
1877 number
= number
/ 10;
1880 for( int j
= 0; j
< i
; j
++ )
1882 glUniform3fv( SHADER_UNIFORM( shader_tile_colour
, "uOffset" ), 1, pos
);
1883 draw_mesh( MESH_NUMBERS_OFFSETS
[digits
[i
-j
-1]][0], MESH_NUMBERS_OFFSETS
[digits
[i
-j
-1]][1] );
1884 pos
[0] += pos
[2] * 0.75f
;
1888 void vg_render(void)
1890 glViewport( 0,0, vg_window_x
, vg_window_y
);
1892 glDisable( GL_DEPTH_TEST
);
1893 glClearColor( 0.369768f
, 0.3654f
, 0.42f
, 1.0f
);
1894 glClear( GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
1896 v4f
const colour_default
= {1.0f
, 1.0f
, 1.0f
, 1.0f
};
1897 v4f
const colour_selected
= {0.90f
, 0.92f
, 1.0f
, 1.0f
};
1899 // TILE SET RENDERING
1900 // todo: just slam everything into a mesh...
1901 // when user modifies a tile the neighbours can be easily uploaded to gpu mem
1903 // Currently we're uploading a fair amount of data every frame anyway.
1904 // NOTE: this is for final optimisations ONLY!
1905 // ======================================================================
1907 use_mesh( &world
.tile
);
1913 SHADER_USE( shader_background
);
1914 glUniformMatrix3fv( SHADER_UNIFORM( shader_background
, "uPv" ), 1, GL_FALSE
, (float *)vg_pv
);
1916 glActiveTexture( GL_TEXTURE0
);
1917 glBindTexture( GL_TEXTURE_2D
, world
.background_data
);
1918 glUniform1i( SHADER_UNIFORM( shader_background
, "uTexMain" ), 0 );
1920 glUniform3f( SHADER_UNIFORM( shader_background
, "uOffset" ), -16, -16, 64 );
1921 glUniform1f( SHADER_UNIFORM( shader_background
, "uVariance" ), 0.02f
);
1923 glActiveTexture( GL_TEXTURE1
);
1924 glBindTexture( GL_TEXTURE_2D
, world
.random_samples
);
1925 glUniform1i( SHADER_UNIFORM( shader_background
, "uSamplerNoise" ), 1 );
1932 SHADER_USE( shader_tile_main
);
1935 m2x2_identity( subtransform
);
1936 glUniformMatrix2fv( SHADER_UNIFORM( shader_tile_main
, "uSubTransform" ), 1, GL_FALSE
, (float *)subtransform
);
1937 glUniformMatrix3fv( SHADER_UNIFORM( shader_tile_main
, "uPv" ), 1, GL_FALSE
, (float *)vg_pv
);
1938 glUniform1f( SHADER_UNIFORM( shader_tile_main
, "uGhost" ), 0.0f
);
1939 glUniform1f( SHADER_UNIFORM( shader_tile_main
, "uForeground" ), 0.0f
);
1942 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
1943 glBlendEquation(GL_FUNC_ADD
);
1946 vg_tex2d_bind( &tex_tile_data
, 0 );
1947 glUniform1i( SHADER_UNIFORM( shader_tile_main
, "uTexGlyphs" ), 0 );
1949 vg_tex2d_bind( &tex_wood
, 1 );
1950 glUniform1i( SHADER_UNIFORM( shader_tile_main
, "uTexWood" ), 1 );
1952 render_tiles( NULL
, NULL
, colour_default
, colour_default
);
1956 SHADER_USE( shader_ball
);
1957 glUniformMatrix3fv( SHADER_UNIFORM( shader_ball
, "uPv" ), 1, GL_FALSE
, (float *)vg_pv
);
1959 vg_tex2d_bind( &tex_ball_noise
, 0 );
1960 glUniform1i( SHADER_UNIFORM( shader_ball
, "uTexMain" ), 0 );
1963 if( world
.simulating
)
1965 for( int i
= 0; i
< world
.num_fishes
; i
++ )
1967 struct fish
*fish
= &world
.fishes
[i
];
1969 if( fish
->state
== k_fish_state_dead
|| fish
->state
== k_fish_state_bg
)
1972 if( fish
->state
== k_fish_state_soon_dead
&& (world
.frame_lerp
> fish
->death_time
) )
1975 v4f dot_colour
= { 0.0f
, 0.0f
, 0.0f
, 1.0f
};
1976 colour_code_v3( fish
->payload
, dot_colour
);
1978 glUniform3fv( SHADER_UNIFORM( shader_ball
, "uColour" ), 1, dot_colour
);
1979 glUniform2fv( SHADER_UNIFORM( shader_ball
, "uOffset" ), 1, fish
->physics_co
);
1980 glUniform2f( SHADER_UNIFORM( shader_ball
, "uTexOffset" ), (float)i
* 1.2334, (float)i
* -0.3579f
);
1985 SHADER_USE( shader_tile_main
);
1988 vg_tex2d_bind( &tex_tile_data
, 0 );
1989 glUniform1i( SHADER_UNIFORM( shader_tile_main
, "uTexGlyphs" ), 0 );
1991 vg_tex2d_bind( &tex_wood
, 1 );
1992 glUniform1i( SHADER_UNIFORM( shader_tile_main
, "uTexWood" ), 1 );
1994 glUniform1f( SHADER_UNIFORM( shader_tile_main
, "uForeground" ), 1.0f
);
1995 render_tiles( NULL
, NULL
, colour_default
, colour_selected
);
1998 for( int y
= 2; y
< world
.h
-2; y
++ )
2000 for( int x
= 2; x
< world
.w
-2; x
++ )
2002 struct cell
*cell
= pcell((v2i
){x
,y
});
2004 if( cell
->state
& FLAG_CANAL
)
2006 if( cell
->config
== k_cell_type_split
)
2008 float rotation
= cell
->state
& FLAG_FLIP_FLOP
? vg_rad( -45.0f
): vg_rad( 45.0f
);
2010 if( cell
->state
& FLAG_FLIP_ROTATING
)
2012 if( (world
.frame_lerp
> curve_7_linear_section
) )
2014 float const rotation_speed
= 0.4f
;
2015 if( (world
.frame_lerp
< 1.0f
-rotation_speed
) )
2017 float t
= world
.frame_lerp
- curve_7_linear_section
;
2018 t
*= -2.0f
* (1.0f
/(1.0f
-(curve_7_linear_section
+rotation_speed
)));
2028 m2x2_create_rotation( subtransform
, rotation
);
2030 glUniformMatrix2fv( SHADER_UNIFORM( shader_tile_main
, "uSubTransform" ), 1, GL_FALSE
, (float *)subtransform
);
2031 glUniform4f( SHADER_UNIFORM( shader_tile_main
, "uOffset" ), (float)x
, (float)y
+ 0.125f
, cell
->state
& FLAG_TARGETED
? 3.0f
: 0.0f
, 0.0f
);
2039 if( world
.selected
!= -1 && !(world
.data
[ world
.selected
].state
& FLAG_CANAL
) && !id_drag_from
)
2041 v2i new_begin
= { world
.tile_x
- 2, world
.tile_y
- 2 };
2042 v2i new_end
= { world
.tile_x
+ 2, world
.tile_y
+ 2 };
2044 world
.data
[ world
.selected
].state
^= FLAG_CANAL
;
2045 map_reclassify( new_begin
, new_end
, 0 );
2047 m2x2_identity( subtransform
);
2048 glUniform1f( SHADER_UNIFORM( shader_tile_main
, "uGhost" ), 1.0f
);
2049 glUniformMatrix2fv( SHADER_UNIFORM( shader_tile_main
, "uSubTransform" ), 1, GL_FALSE
, (float *)subtransform
);
2050 glUniform2fv( SHADER_UNIFORM( shader_tile_main
, "uMousePos" ), 1, world
.tile_pos
);
2052 render_tiles( new_begin
, new_end
, colour_default
, colour_default
);
2054 world
.data
[ world
.selected
].state
^= FLAG_CANAL
;
2055 map_reclassify( new_begin
, new_end
, 0 );
2058 //glDisable(GL_BLEND);
2060 // Draw connecting wires
2061 glDisable(GL_BLEND
);
2063 SHADER_USE( shader_wire
);
2064 glBindVertexArray( world
.wire
.vao
);
2066 glUniformMatrix3fv( SHADER_UNIFORM( shader_wire
, "uPv" ), 1, GL_FALSE
, (float *)vg_pv
);
2067 glUniform4f( SHADER_UNIFORM( shader_wire
, "uColour" ), 0.2f
, 0.2f
, 0.2f
, 1.0f
);
2071 glUniform1f( SHADER_UNIFORM( shader_wire
, "uCurve" ), 0.4f
);
2072 glUniform3f( SHADER_UNIFORM( shader_wire
, "uStart" ), drag_from_co
[0], drag_from_co
[1], 0.06f
);
2073 glUniform3f( SHADER_UNIFORM( shader_wire
, "uEnd" ), drag_to_co
[0], drag_to_co
[1], 0.06f
);
2074 glDrawElements( GL_TRIANGLES
, world
.wire
.em
, GL_UNSIGNED_SHORT
, (void*)(0) );
2077 float rp_x1
= world
.frame_lerp
*9.0f
;
2078 float rp_x2
= 1.0f
-rp_x1
*expf(1.0f
-rp_x1
)* 0.36f
;
2080 for( int y
= 2; y
< world
.h
-2; y
++ )
2082 for( int x
= 2; x
< world
.w
-2; x
++ )
2084 struct cell
*cell
= pcell((v2i
){x
,y
});
2086 if( cell
->state
& FLAG_CANAL
)
2088 if( cell
->state
& FLAG_IS_TRIGGER
)
2090 int trigger_id
= cell
->links
[0]?0:1;
2092 int x2
= cell
->links
[trigger_id
] % world
.w
;
2093 int y2
= (cell
->links
[trigger_id
] - x2
) / world
.w
;
2098 startpoint
[0] = (float)x2
+ (trigger_id
? 0.75f
: 0.25f
);
2099 startpoint
[1] = (float)y2
+ 0.25f
;
2101 endpoint
[0] = x
+0.5f
;
2102 endpoint
[1] = y
+0.5f
;
2104 glUniform1f( SHADER_UNIFORM( shader_wire
, "uCurve" ), cell
->state
& FLAG_TRIGGERED
? rp_x2
* 0.4f
: 0.4f
);
2105 glUniform3f( SHADER_UNIFORM( shader_wire
, "uStart" ), startpoint
[0], startpoint
[1], 0.04f
);
2106 glUniform3f( SHADER_UNIFORM( shader_wire
, "uEnd" ), endpoint
[0], endpoint
[1], 0.04f
);
2107 glDrawElements( GL_TRIANGLES
, world
.wire
.em
, GL_UNSIGNED_SHORT
, (void*)(0) );
2113 SHADER_USE( shader_tile_colour
);
2114 glUniformMatrix3fv( SHADER_UNIFORM( shader_tile_colour
, "uPv" ), 1, GL_FALSE
, (float *)vg_pv
);
2115 use_mesh( &world
.circle
);
2117 int const filled_start
= 0;
2118 int const filled_count
= 32;
2119 int const empty_start
= 32;
2120 int const empty_count
= 32*2;
2123 for( int i
= 0; i
< arrlen( world
.io
); i
++ )
2125 struct cell_terminal
*term
= &world
.io
[ i
];
2126 int posx
= term
->id
% world
.w
;
2127 int posy
= (term
->id
- posx
)/world
.w
;
2128 int is_input
= world
.data
[ term
->id
].state
& FLAG_INPUT
;
2130 v4f dot_colour
= { 0.0f
, 0.0f
, 0.0f
, 1.0f
};
2132 for( int k
= 0; k
< term
->run_count
; k
++ )
2134 for( int j
= 0; j
< term
->runs
[k
].condition_count
; j
++ )
2136 float y_offset
= is_input
? 1.2f
: -0.2f
;
2137 y_offset
+= (is_input
? 0.2f
: -0.2f
) * (float)k
;
2139 glUniform3f( SHADER_UNIFORM( shader_tile_colour
, "uOffset" ), (float)posx
+ 0.2f
+ 0.2f
* (float)j
, (float)posy
+ y_offset
, 0.1f
);
2143 colour_code_v3( term
->runs
[k
].conditions
[j
], dot_colour
);
2144 glUniform4fv( SHADER_UNIFORM( shader_tile_colour
, "uColour" ), 1, dot_colour
);
2146 // Draw filled if tick not passed, draw empty if empty
2147 if( (world
.sim_frame
> j
&& world
.sim_run
>= k
) || world
.sim_run
> k
)
2148 draw_mesh( empty_start
, empty_count
);
2150 draw_mesh( filled_start
, filled_count
);
2154 if( term
->runs
[k
].recv_count
> j
)
2156 colour_code_v3( term
->runs
[k
].recieved
[j
], dot_colour
);
2157 v3_muls( dot_colour
, 0.8f
, dot_colour
);
2158 glUniform4fv( SHADER_UNIFORM( shader_tile_colour
, "uColour" ), 1, dot_colour
);
2160 draw_mesh( filled_start
, filled_count
);
2163 colour_code_v3( term
->runs
[k
].conditions
[j
], dot_colour
);
2164 glUniform4fv( SHADER_UNIFORM( shader_tile_colour
, "uColour" ), 1, dot_colour
);
2166 draw_mesh( empty_start
, empty_count
);
2172 if( world
.simulating
)
2174 glUniform4f( SHADER_UNIFORM( shader_tile_colour
, "uColour" ), 0.0f
, 0.0f
, 0.0f
, 1.0f
);
2175 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
);
2176 draw_mesh( filled_start
, filled_count
);
2180 float const score_bright
= 1.25f
;
2181 glUniform4f( SHADER_UNIFORM( shader_tile_colour
, "uColour" ),
2182 0.4f
*score_bright
, 0.39f
*score_bright
, 0.45f
*score_bright
, 1.0f
);
2184 use_mesh( &world
.numbers
);
2185 draw_numbers( (v3f
){ 2.0f
, (float)world
.h
-1.875f
, 0.3333f
}, world
.score
);
2187 // Level selection UI
2188 use_mesh( &world
.circle
);
2189 float ratio
= ((float)vg_window_x
/(float)vg_window_y
);
2191 m3x3f ui_view
= M3X3_IDENTITY
;
2192 m3x3_scale( ui_view
, (v3f
){ 1.0f
, ratio
, 1.0f
} );
2193 glUniformMatrix3fv( SHADER_UNIFORM( shader_tile_colour
, "uPv" ), 1, GL_FALSE
, (float *)ui_view
);
2195 // Calculate mouse in UIsp
2196 v3f mouse_ui_space
= { ((float)vg_mouse
[0] / (float)(vg_window_x
)) * 2.0f
- 1.0f
,
2197 (((float)vg_mouse
[1] / (float)(vg_window_y
)) * 2.0f
- 1.0f
)*(-1.0f
/ratio
), 0.0125f
};
2199 // Get selected level
2200 const float selection_scale
= 0.05f
;
2201 int const level_count
= vg_list_size( level_pack_1
);
2202 int level_select
= -1;
2204 if( mouse_ui_space
[0] <= -0.8f
)
2206 float levels_range
= (float)level_count
*selection_scale
*0.6f
;
2207 float level_offset
= ((-mouse_ui_space
[1] + levels_range
) / levels_range
) * 0.5f
* (float)level_count
;
2208 level_select
= ceilf( level_offset
);
2211 if( level_select
>= 0 && level_select
< vg_list_size( level_pack_1
) )
2213 glUniform4f( SHADER_UNIFORM( shader_tile_colour
, "uColour" ), 0.369768f
, 0.3654f
, 0.42f
, 1.0f
);
2215 use_mesh( &world
.tile
);
2216 glUniform3f( SHADER_UNIFORM( shader_tile_colour
, "uOffset" ),
2218 ((float)level_count
- (float)level_select
* 2.0f
) * selection_scale
* 0.6f
,
2223 use_mesh( &world
.circle
);
2225 if( vg_get_button_down( "primary" ) )
2227 console_changelevel( 1, level_pack_1
+ level_select
);
2231 else mouse_ui_space
[1] = INFINITY
;
2233 glUniform4f( SHADER_UNIFORM( shader_tile_colour
, "uColour" ), 0.4f
, 0.39f
, 0.45f
, 1.0f
);
2236 for( int i
= 0; i
< level_count
; i
++ )
2238 struct career_level
*clevel
= &career
.levels
[i
];
2240 v3f level_ui_space
= {
2242 ((float)level_count
- (float)i
* 2.0f
) * selection_scale
* 0.6f
+ selection_scale
* 0.5f
,
2243 selection_scale
* 0.5f
2246 float scale
= vg_clampf( 1.0f
- fabsf(level_ui_space
[1] - mouse_ui_space
[1]) * 2.0f
, 0.9f
, 1.0f
);
2247 level_ui_space
[2] *= scale
;
2249 glUniform3fv( SHADER_UNIFORM( shader_tile_colour
, "uOffset" ), 1, level_ui_space
);
2251 if( clevel
->completed
)
2252 draw_mesh( filled_start
, filled_count
);
2254 draw_mesh( empty_start
, empty_count
);
2258 glUniform4f( SHADER_UNIFORM( shader_tile_colour
, "uColour" ), 0.4f
*1.25f
, 0.39f
*1.25f
, 0.45f
*1.25f
, 1.0f
);
2260 use_mesh( &world
.numbers
);
2261 for( int i
= 0; i
< level_count
; i
++ )
2263 struct career_level
*clevel
= &career
.levels
[i
];
2265 v3f level_ui_space
= {
2267 ((float)level_count
- (float)i
* 2.0f
) * selection_scale
* 0.6f
+ selection_scale
* 0.5f
,
2271 if( clevel
->completed
)
2272 draw_numbers( level_ui_space
, clevel
->score
);
2274 level_ui_space
[0] = -0.975f
;
2275 level_ui_space
[1] -= 0.01f
;
2276 draw_numbers( level_ui_space
, i
);
2279 //use_mesh( &world.numbers );
2280 //draw_numbers( (v3f){ 0.0f, -0.5f, 0.1f }, 128765 );