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
;
337 static void map_free(void)
339 arrfree( world
.data
);
350 world
.initialzed
= 0;
353 static void io_reset(void)
355 for( int i
= 0; i
< arrlen( world
.io
); i
++ )
357 struct cell_terminal
*term
= &world
.io
[i
];
359 for( int j
= 0; j
< term
->run_count
; j
++ )
360 term
->runs
[j
].recv_count
= 0;
364 static void map_reclassify( v2i start
, v2i end
, int update_texbuffer
);
365 static int map_load( const char *str
, const char *name
)
367 //TODO: It may be worthwhile, at this point, to switch to binary encoding for save data
376 if( str
[world
.w
] == ';' )
378 else if( !str
[world
.w
] )
380 vg_error( "Unexpected EOF when parsing level\n" );
385 struct cell
*row
= arraddnptr( world
.data
, world
.w
);
387 int reg_start
= 0, reg_end
= 0;
389 u32
*links_to_make
= NULL
;
390 int links_satisfied
= 0;
392 char link_id_buffer
[32];
400 if( *c
== '\r' ) { c
++; continue; } // fuck off windows
406 if( *c
== '\r' ) c
++;
413 if( *c
== '\r' ) { c
++; continue; }
415 if( reg_start
< reg_end
)
417 struct cell_terminal
*terminal
= &world
.io
[ reg_start
];
418 struct terminal_run
*run
= &terminal
->runs
[ terminal
->run_count
-1 ];
420 if( *c
>= 'a' && *c
<= 'z' )
422 run
->conditions
[ run
->condition_count
++ ] = *c
;
426 if( *c
== ',' || *c
== '\n' )
435 terminal
->runs
[ terminal
->run_count
].condition_count
= 0;
436 terminal
->run_count
++;
437 world
.max_runs
= vg_max( world
.max_runs
, terminal
->run_count
);
441 vg_error( "Unkown attribute '%c' (row: %u)\n", *c
, world
.h
);
448 if( links_satisfied
< arrlen( links_to_make
) )
450 struct cell
*target
= &world
.data
[ links_to_make
[ links_satisfied
] ];
452 if( (((u32
)*c
>= (u32
)'0') && ((u32
)*c
<= (u32
)'9')) || *c
== '-' )
454 if( link_id_n
>= vg_list_size( link_id_buffer
)-1 )
456 vg_error( "Number was way too long to be parsed (row: %u)\n", world
.h
);
460 link_id_buffer
[ link_id_n
++ ] = *c
;
462 else if( *c
== ',' || *c
== '\n' )
464 link_id_buffer
[ link_id_n
] = 0x00;
465 int value
= atoi( link_id_buffer
);
467 target
->links
[value
>= 0? 1:0] = abs(value
);
476 vg_error( "Invalid character '%c' (row: %u)\n", *c
, world
.h
);
482 vg_error( "Too many values to assign (row: %u)\n", world
.h
);
491 // Registry length-error checks
492 if( reg_start
!= reg_end
)
494 vg_error( "Not enough spawn values assigned (row: %u, %u of %u)\n", world
.h
, reg_start
, reg_end
);
498 if( links_satisfied
!= arrlen( links_to_make
) )
500 vg_error( "Not enough link values assigned (row: %u, %u of %u)\n", world
.h
, links_satisfied
, arrlen( links_to_make
) );
506 vg_error( "Not enough cells to match previous row definition (row: %u, %u<%u)\n", world
.h
, cx
, world
.w
);
510 row
= arraddnptr( world
.data
, world
.w
);
513 reg_end
= reg_start
= arrlen( world
.io
);
515 arrsetlen( links_to_make
, 0 );
522 vg_error( "Too many cells to match previous row definition (row: %u, %u>%u)\n", world
.h
, cx
, world
.w
);
526 // Tile initialization
528 struct cell
*cell
= &row
[ cx
];
531 if( *c
== '+' || *c
== '-' )
533 struct cell_terminal
*term
= arraddnptr( world
.io
, 1 );
534 term
->id
= cx
+ world
.h
*world
.w
;
536 term
->runs
[0].condition_count
= 0;
538 cell
->state
= *c
== '+'? FLAG_INPUT
: FLAG_OUTPUT
;
541 else if( *c
== '#' ) cell
->state
= FLAG_WALL
;
542 else if( ((u32
)*c
>= (u32
)'A') && ((u32
)*c
<= (u32
)'A'+0xf) )
544 // Canal flag bits (4bit/16 value):
550 cell
->state
= ((u32
)*c
- (u32
)'A') & (FLAG_CANAL
|FLAG_IS_TRIGGER
);
552 if( cell
->state
& FLAG_IS_TRIGGER
)
553 arrpush( links_to_make
, cx
+ world
.h
*world
.w
);
559 else cell
->state
= 0x00;
567 // Update data texture to fill out the background
569 u8 info_buffer
[64*64*4];
570 for( int i
= 0; i
< 64*64; i
++ )
572 u8
*px
= &info_buffer
[i
*4];
579 glBindTexture( GL_TEXTURE_2D
, world
.background_data
);
580 glTexSubImage2D( GL_TEXTURE_2D
, 0, 0, 0, 64, 64, GL_RGBA
, GL_UNSIGNED_BYTE
, info_buffer
);
583 arrfree( links_to_make
);
585 map_reclassify( NULL
, NULL
, 1 );
588 for( int i
= 0; i
< world
.h
*world
.w
; i
++ )
590 struct cell
*src
= &world
.data
[i
];
591 if( src
->state
& FLAG_IS_TRIGGER
)
593 int link_id
= src
->links
[0]?0:1;
594 if( src
->links
[link_id
] <= world
.h
*world
.w
)
596 struct cell
*target
= &world
.data
[ src
->links
[link_id
] ];
597 if( (target
->state
& FLAG_CANAL
) && (target
->config
== k_cell_type_split
) )
599 if( target
->links
[ link_id
] )
601 vg_error( "Link target was already targeted\n" );
607 target
->links
[ link_id
] = i
;
608 target
->state
|= FLAG_TARGETED
;
613 vg_error( "Link target was invalid\n" );
619 vg_error( "Link target out of bounds\n" );
625 vg_success( "Map '%s' loaded! (%u:%u)\n", name
, world
.w
, world
.h
);
629 strncpy( world
.map_name
, name
, vg_list_size( world
.map_name
)-1 );
630 world
.initialzed
= 1;
634 arrfree( links_to_make
);
639 static struct cell
*pcell( v2i pos
)
641 return &world
.data
[ pos
[1]*world
.w
+ pos
[0] ];
644 static void map_serialize( FILE *stream
)
646 for( int y
= 0; y
< world
.h
; y
++ )
648 for( int x
= 0; x
< world
.w
; x
++ )
650 struct cell
*cell
= pcell( (v2i
){ x
, y
} );
652 if( cell
->state
& FLAG_WALL
) fputc( '#', stream
);
653 else if( cell
->state
& FLAG_INPUT
) fputc( '+', stream
);
654 else if( cell
->state
& FLAG_OUTPUT
) fputc( '-', stream
);
655 else if( cell
->state
& (FLAG_CANAL
|FLAG_IS_TRIGGER
|FLAG_RESERVED0
|FLAG_RESERVED1
) )
657 fputc( (cell
->state
& (FLAG_CANAL
|FLAG_IS_TRIGGER
|FLAG_RESERVED0
|FLAG_RESERVED1
)) + (u32
)'A', stream
);
659 else fputc( ' ', stream
);
662 fputc( ';', stream
);
664 int terminal_write_count
= 0;
666 for( int x
= 0; x
< world
.w
; x
++ )
668 for( int i
= 0; i
< arrlen( world
.io
); i
++ )
670 struct cell_terminal
*term
= &world
.io
[i
];
671 if( term
->id
== y
*world
.w
+x
)
673 if( terminal_write_count
)
674 fputc( ',', stream
);
675 terminal_write_count
++;
677 for( int j
= 0; j
< term
->run_count
; j
++ )
679 struct terminal_run
*run
= &term
->runs
[j
];
681 for( int k
= 0; k
< run
->condition_count
; k
++ )
682 fputc( run
->conditions
[k
], stream
);
684 if( j
< term
->run_count
-1 )
685 fputc( ':', stream
);
691 for( int x
= 0; x
< world
.w
; x
++ )
693 struct cell
*cell
= pcell( (v2i
){ x
,y
} );
694 if( cell
->state
& FLAG_IS_TRIGGER
)
696 if( terminal_write_count
)
697 fputc( ',', stream
);
698 terminal_write_count
++;
700 fprintf( stream
, "%d", cell
->links
[0]? -cell
->links
[0]: cell
->links
[1] );
704 fputc( '\n', stream
);
708 int main( int argc
, char *argv
[] )
710 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 sw_set_achievement( "CAN_DO_THAT" );
1517 fish
->state
= k_fish_state_soon_alive
;
1521 fish
->flow_reversed
= 1;
1523 switch( cell_entry
->config
)
1525 case k_cell_type_con_r
: fish
->dir
[0] = 1; break;
1526 case k_cell_type_con_l
: fish
->dir
[0] = -1; break;
1527 case k_cell_type_con_u
: fish
->dir
[1] = 1; break;
1528 case k_cell_type_con_d
: fish
->dir
[1] = -1; break;
1534 else { vg_error( "fish behaviour unimplemented for behaviour type (%d)\n" ); }
1536 if( fish
->state
>= k_fish_state_alive
)
1540 // Second pass (triggers)
1541 for( int i
= 0; i
< world
.num_fishes
; i
++ )
1543 struct fish
*fish
= &world
.fishes
[i
];
1545 if( fish
->state
== k_fish_state_alive
)
1547 v2i_add( fish
->pos
, fish
->dir
, fish
->pos
);
1548 struct cell
*cell_current
= pcell( fish
->pos
);
1550 if( cell_current
->state
& FLAG_IS_TRIGGER
)
1552 int trigger_id
= cell_current
->links
[0]?0:1;
1553 int connection_id
= cell_current
->links
[trigger_id
];
1554 int target_px
= connection_id
% world
.w
;
1555 int target_py
= (connection_id
- target_px
)/world
.w
;
1557 vg_line2( (v2f
){ fish
->pos
[0], fish
->pos
[1] }, (v2f
){ target_px
, target_py
}, 0xffffffff, 0xffffffff );
1559 struct cell
*target_peice
= &world
.data
[ cell_current
->links
[trigger_id
] ];
1561 cell_current
->state
|= FLAG_TRIGGERED
;
1564 target_peice
->state
|= FLAG_FLIP_FLOP
;
1566 target_peice
->state
&= ~FLAG_FLIP_FLOP
;
1571 // Third pass (collisions)
1572 struct fish
*fi
, *fj
;
1574 for( int i
= 0; i
< world
.num_fishes
; i
++ )
1576 fi
= &world
.fishes
[i
];
1578 if( fi
->state
== k_fish_state_alive
)
1580 int continue_again
= 0;
1582 for( int j
= i
+1; j
< world
.num_fishes
; j
++ )
1584 fj
= &world
.fishes
[j
];
1586 if( (fj
->state
== k_fish_state_alive
) )
1591 v2i_sub( fi
->pos
, fi
->dir
, fi_prev
);
1592 v2i_sub( fj
->pos
, fj
->dir
, fj_prev
);
1595 collide_next_frame
= (
1596 (fi
->pos
[0] == fj
->pos
[0]) &&
1597 (fi
->pos
[1] == fj
->pos
[1]))? 1: 0,
1598 collide_this_frame
= (
1599 (fi_prev
[0] == fj
->pos
[0]) &&
1600 (fi_prev
[1] == fj
->pos
[1]) &&
1601 (fj_prev
[0] == fi
->pos
[0]) &&
1602 (fj_prev
[1] == fi
->pos
[1])
1605 if( collide_next_frame
|| collide_this_frame
)
1607 // Shatter death (+0.5s)
1608 float death_time
= collide_this_frame
? 0.0f
: 0.5f
;
1610 fi
->state
= k_fish_state_soon_dead
;
1611 fj
->state
= k_fish_state_soon_dead
;
1612 fi
->death_time
= death_time
;
1613 fj
->death_time
= death_time
;
1620 if( continue_again
)
1626 for( int i
= 0; i
< arrlen( world
.io
); i
++ )
1628 struct cell_terminal
*term
= &world
.io
[ i
];
1629 int posx
= term
->id
% world
.w
;
1630 int posy
= (term
->id
- posx
)/world
.w
;
1631 int is_input
= world
.data
[ term
->id
].state
& FLAG_INPUT
;
1635 if( world
.sim_frame
< term
->runs
[ world
.sim_run
].condition_count
)
1637 struct fish
*fish
= &world
.fishes
[ world
.num_fishes
];
1638 fish
->pos
[0] = posx
;
1639 fish
->pos
[1] = posy
;
1640 fish
->state
= k_fish_state_alive
;
1641 fish
->payload
= term
->runs
[ world
.sim_run
].conditions
[ world
.sim_frame
];
1643 struct cell
*cell_ptr
= pcell( fish
->pos
);
1645 if( cell_ptr
->config
!= k_cell_type_stub
)
1647 struct cell_description
*desc
= &cell_descriptions
[ cell_ptr
->config
];
1649 v2i_copy( desc
->start
, fish
->dir
);
1650 fish
->flow_reversed
= 1;
1652 world
.num_fishes
++;
1659 if( alive_count
== 0 )
1661 world
.completed
= 1;
1663 for( int i
= 0; i
< arrlen( world
.io
); i
++ )
1665 struct cell_terminal
*term
= &world
.io
[ i
];
1666 int is_input
= world
.data
[ term
->id
].state
& FLAG_INPUT
;
1670 struct terminal_run
*run
= &term
->runs
[ world
.sim_run
];
1672 if( run
->recv_count
== run
->condition_count
)
1674 for( int j
= 0; j
< run
->condition_count
; j
++ )
1676 if( run
->recieved
[j
] != run
->conditions
[j
] )
1678 world
.completed
= 0;
1685 world
.completed
= 0;
1691 if( world
.completed
)
1693 if( world
.sim_run
< world
.max_runs
-1 )
1695 vg_success( "Run passed, starting next\n" );
1697 world
.sim_frame
= 0;
1698 world
.sim_start
= vg_time
;
1699 world
.num_fishes
= 0;
1701 for( int i
= 0; i
< world
.w
*world
.h
; i
++ )
1702 world
.data
[ i
].state
&= ~FLAG_FLIP_FLOP
;
1708 vg_success( "Level passed!\n" );
1711 for( int i
= 0; i
< world
.w
*world
.h
; i
++ )
1712 if( world
.data
[ i
].state
& FLAG_CANAL
)
1715 world
.score
= score
;
1716 world
.time
= world
.sim_frame
;
1721 vg_error( "Level failed :(\n" );
1724 // Copy into career data
1725 if( world
.ptr_career_level
)
1727 world
.ptr_career_level
->score
= world
.score
;
1728 world
.ptr_career_level
->time
= world
.time
;
1729 world
.ptr_career_level
->completed
= world
.completed
;
1732 simulation_stop(); // TODO: Async?
1739 float scaled_time
= 0.0f
;
1740 scaled_time
= (vg_time
-world
.sim_start
)*world
.sim_speed
;
1741 world
.frame_lerp
= scaled_time
- (float)world
.sim_frame
;
1744 for( int i
= 0; i
< world
.num_fishes
; i
++ )
1746 struct fish
*fish
= &world
.fishes
[i
];
1748 if( fish
->state
== k_fish_state_dead
)
1751 if( fish
->state
== k_fish_state_soon_dead
&& (world
.frame_lerp
> fish
->death_time
) )
1752 continue; // Todo: particle thing?
1754 struct cell
*cell
= pcell(fish
->pos
);
1755 struct cell_description
*desc
= &cell_descriptions
[ cell
->config
];
1759 float t
= world
.frame_lerp
;
1760 if( fish
->flow_reversed
&& !desc
->is_linear
)
1763 v2_copy( fish
->physics_co
, fish
->physics_v
);
1765 switch( cell
->config
)
1767 case k_cell_type_merge
:
1768 if( fish
->dir
[0] == 1 )
1773 case k_cell_type_con_r
: curve
= curve_1
; break;
1774 case k_cell_type_con_l
: curve
= curve_4
; break;
1775 case k_cell_type_con_u
: curve
= curve_2
; break;
1776 case k_cell_type_con_d
: curve
= curve_8
; break;
1777 case 3: curve
= curve_3
; break;
1778 case 6: curve
= curve_6
; break;
1779 case 9: curve
= curve_9
; break;
1780 case 12: curve
= curve_12
; break;
1782 if( t
> curve_7_linear_section
)
1784 t
-= curve_7_linear_section
;
1785 t
*= (1.0f
/(1.0f
-curve_7_linear_section
));
1787 curve
= cell
->state
& FLAG_FLIP_FLOP
? curve_7
: curve_7_1
;
1791 default: curve
= NULL
; break;
1797 float t3
= t
* t
* t
;
1799 float cA
= 3.0f
*t2
- 3.0f
*t3
;
1800 float cB
= 3.0f
*t3
- 6.0f
*t2
+ 3.0f
*t
;
1801 float cC
= 3.0f
*t2
- t3
- 3.0f
*t
+ 1.0f
;
1803 fish
->physics_co
[0] = t3
*curve
[3][0] + cA
*curve
[2][0] + cB
*curve
[1][0] + cC
*curve
[0][0];
1804 fish
->physics_co
[1] = t3
*curve
[3][1] + cA
*curve
[2][1] + cB
*curve
[1][1] + cC
*curve
[0][1];
1805 fish
->physics_co
[0] += (float)fish
->pos
[0];
1806 fish
->physics_co
[1] += (float)fish
->pos
[1];
1811 origin
[0] = (float)fish
->pos
[0] + (float)fish
->dir
[0]*-0.5f
+ 0.5f
;
1812 origin
[1] = (float)fish
->pos
[1] + (float)fish
->dir
[1]*-0.5f
+ 0.5f
;
1814 fish
->physics_co
[0] = origin
[0] + (float)fish
->dir
[0]*t
;
1815 fish
->physics_co
[1] = origin
[1] + (float)fish
->dir
[1]*t
;
1821 static void render_tiles( v2i start
, v2i end
, v4f
const regular_colour
, v4f
const selected_colour
)
1823 v2i full_start
= { 0,0 };
1824 v2i full_end
= { world
.w
, world
.h
};
1826 if( !start
|| !end
)
1832 glUniform4fv( SHADER_UNIFORM( shader_tile_main
, "uColour" ), 1, regular_colour
);
1834 for( int y
= start
[1]; y
< end
[1]; y
++ )
1836 for( int x
= start
[0]; x
< end
[0]; x
++ )
1838 struct cell
*cell
= pcell((v2i
){x
,y
});
1839 int selected
= world
.selected
== y
*world
.w
+ x
;
1841 int tile_offsets
[][2] =
1843 {2, 0}, {0, 3}, {0, 2}, {2, 2},
1844 {1, 0}, {2, 3}, {3, 2}, {1, 3},
1845 {3, 1}, {0, 1}, {1, 2}, {2, 1},
1846 {1, 1}, {3, 3}, {2, 1}, {2, 1}
1849 int uv
[2] = { 3, 0 };
1851 if( cell
->state
& (FLAG_CANAL
|FLAG_INPUT
|FLAG_OUTPUT
) )
1853 uv
[0] = tile_offsets
[ cell
->config
][0];
1854 uv
[1] = tile_offsets
[ cell
->config
][1];
1857 glUniform4f( SHADER_UNIFORM( shader_tile_main
, "uOffset" ), (float)x
, (float)y
, uv
[0], uv
[1] );
1860 glUniform4fv( SHADER_UNIFORM( shader_tile_main
, "uColour" ), 1, selected_colour
);
1862 glUniform4fv( SHADER_UNIFORM( shader_tile_main
, "uColour" ), 1, regular_colour
);
1870 static void draw_numbers( v3f coord
, int number
)
1873 v3_copy( coord
, pos
);
1874 int digits
[8]; int i
= 0;
1876 while( number
> 0 && i
< 8 )
1878 digits
[i
++] = number
% 10;
1879 number
= number
/ 10;
1882 for( int j
= 0; j
< i
; j
++ )
1884 glUniform3fv( SHADER_UNIFORM( shader_tile_colour
, "uOffset" ), 1, pos
);
1885 draw_mesh( MESH_NUMBERS_OFFSETS
[digits
[i
-j
-1]][0], MESH_NUMBERS_OFFSETS
[digits
[i
-j
-1]][1] );
1886 pos
[0] += pos
[2] * 0.75f
;
1890 void vg_render(void)
1892 glViewport( 0,0, vg_window_x
, vg_window_y
);
1894 glDisable( GL_DEPTH_TEST
);
1895 glClearColor( 0.369768f
, 0.3654f
, 0.42f
, 1.0f
);
1896 glClear( GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
1898 v4f
const colour_default
= {1.0f
, 1.0f
, 1.0f
, 1.0f
};
1899 v4f
const colour_selected
= {0.90f
, 0.92f
, 1.0f
, 1.0f
};
1901 // TILE SET RENDERING
1902 // todo: just slam everything into a mesh...
1903 // when user modifies a tile the neighbours can be easily uploaded to gpu mem
1905 // Currently we're uploading a fair amount of data every frame anyway.
1906 // NOTE: this is for final optimisations ONLY!
1907 // ======================================================================
1909 use_mesh( &world
.tile
);
1915 SHADER_USE( shader_background
);
1916 glUniformMatrix3fv( SHADER_UNIFORM( shader_background
, "uPv" ), 1, GL_FALSE
, (float *)vg_pv
);
1918 glActiveTexture( GL_TEXTURE0
);
1919 glBindTexture( GL_TEXTURE_2D
, world
.background_data
);
1920 glUniform1i( SHADER_UNIFORM( shader_background
, "uTexMain" ), 0 );
1922 glUniform3f( SHADER_UNIFORM( shader_background
, "uOffset" ), -16, -16, 64 );
1923 glUniform1f( SHADER_UNIFORM( shader_background
, "uVariance" ), 0.02f
);
1925 glActiveTexture( GL_TEXTURE1
);
1926 glBindTexture( GL_TEXTURE_2D
, world
.random_samples
);
1927 glUniform1i( SHADER_UNIFORM( shader_background
, "uSamplerNoise" ), 1 );
1934 SHADER_USE( shader_tile_main
);
1937 m2x2_identity( subtransform
);
1938 glUniformMatrix2fv( SHADER_UNIFORM( shader_tile_main
, "uSubTransform" ), 1, GL_FALSE
, (float *)subtransform
);
1939 glUniformMatrix3fv( SHADER_UNIFORM( shader_tile_main
, "uPv" ), 1, GL_FALSE
, (float *)vg_pv
);
1940 glUniform1f( SHADER_UNIFORM( shader_tile_main
, "uGhost" ), 0.0f
);
1941 glUniform1f( SHADER_UNIFORM( shader_tile_main
, "uForeground" ), 0.0f
);
1944 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
1945 glBlendEquation(GL_FUNC_ADD
);
1948 vg_tex2d_bind( &tex_tile_data
, 0 );
1949 glUniform1i( SHADER_UNIFORM( shader_tile_main
, "uTexGlyphs" ), 0 );
1951 vg_tex2d_bind( &tex_wood
, 1 );
1952 glUniform1i( SHADER_UNIFORM( shader_tile_main
, "uTexWood" ), 1 );
1954 render_tiles( NULL
, NULL
, colour_default
, colour_default
);
1958 SHADER_USE( shader_ball
);
1959 glUniformMatrix3fv( SHADER_UNIFORM( shader_ball
, "uPv" ), 1, GL_FALSE
, (float *)vg_pv
);
1961 vg_tex2d_bind( &tex_ball_noise
, 0 );
1962 glUniform1i( SHADER_UNIFORM( shader_ball
, "uTexMain" ), 0 );
1965 if( world
.simulating
)
1967 for( int i
= 0; i
< world
.num_fishes
; i
++ )
1969 struct fish
*fish
= &world
.fishes
[i
];
1971 if( fish
->state
== k_fish_state_dead
|| fish
->state
== k_fish_state_bg
)
1974 if( fish
->state
== k_fish_state_soon_dead
&& (world
.frame_lerp
> fish
->death_time
) )
1977 v4f dot_colour
= { 0.0f
, 0.0f
, 0.0f
, 1.0f
};
1978 colour_code_v3( fish
->payload
, dot_colour
);
1980 glUniform3fv( SHADER_UNIFORM( shader_ball
, "uColour" ), 1, dot_colour
);
1981 glUniform2fv( SHADER_UNIFORM( shader_ball
, "uOffset" ), 1, fish
->physics_co
);
1982 glUniform2f( SHADER_UNIFORM( shader_ball
, "uTexOffset" ), (float)i
* 1.2334, (float)i
* -0.3579f
);
1987 SHADER_USE( shader_tile_main
);
1990 vg_tex2d_bind( &tex_tile_data
, 0 );
1991 glUniform1i( SHADER_UNIFORM( shader_tile_main
, "uTexGlyphs" ), 0 );
1993 vg_tex2d_bind( &tex_wood
, 1 );
1994 glUniform1i( SHADER_UNIFORM( shader_tile_main
, "uTexWood" ), 1 );
1996 glUniform1f( SHADER_UNIFORM( shader_tile_main
, "uForeground" ), 1.0f
);
1997 render_tiles( NULL
, NULL
, colour_default
, colour_selected
);
2000 for( int y
= 2; y
< world
.h
-2; y
++ )
2002 for( int x
= 2; x
< world
.w
-2; x
++ )
2004 struct cell
*cell
= pcell((v2i
){x
,y
});
2006 if( cell
->state
& FLAG_CANAL
)
2008 if( cell
->config
== k_cell_type_split
)
2010 float rotation
= cell
->state
& FLAG_FLIP_FLOP
? vg_rad( -45.0f
): vg_rad( 45.0f
);
2012 if( cell
->state
& FLAG_FLIP_ROTATING
)
2014 if( (world
.frame_lerp
> curve_7_linear_section
) )
2016 float const rotation_speed
= 0.4f
;
2017 if( (world
.frame_lerp
< 1.0f
-rotation_speed
) )
2019 float t
= world
.frame_lerp
- curve_7_linear_section
;
2020 t
*= -2.0f
* (1.0f
/(1.0f
-(curve_7_linear_section
+rotation_speed
)));
2030 m2x2_create_rotation( subtransform
, rotation
);
2032 glUniformMatrix2fv( SHADER_UNIFORM( shader_tile_main
, "uSubTransform" ), 1, GL_FALSE
, (float *)subtransform
);
2033 glUniform4f( SHADER_UNIFORM( shader_tile_main
, "uOffset" ), (float)x
, (float)y
+ 0.125f
, cell
->state
& FLAG_TARGETED
? 3.0f
: 0.0f
, 0.0f
);
2041 if( world
.selected
!= -1 && !(world
.data
[ world
.selected
].state
& FLAG_CANAL
) && !id_drag_from
)
2043 v2i new_begin
= { world
.tile_x
- 2, world
.tile_y
- 2 };
2044 v2i new_end
= { world
.tile_x
+ 2, world
.tile_y
+ 2 };
2046 world
.data
[ world
.selected
].state
^= FLAG_CANAL
;
2047 map_reclassify( new_begin
, new_end
, 0 );
2049 m2x2_identity( subtransform
);
2050 glUniform1f( SHADER_UNIFORM( shader_tile_main
, "uGhost" ), 1.0f
);
2051 glUniformMatrix2fv( SHADER_UNIFORM( shader_tile_main
, "uSubTransform" ), 1, GL_FALSE
, (float *)subtransform
);
2052 glUniform2fv( SHADER_UNIFORM( shader_tile_main
, "uMousePos" ), 1, world
.tile_pos
);
2054 render_tiles( new_begin
, new_end
, colour_default
, colour_default
);
2056 world
.data
[ world
.selected
].state
^= FLAG_CANAL
;
2057 map_reclassify( new_begin
, new_end
, 0 );
2060 //glDisable(GL_BLEND);
2062 // Draw connecting wires
2063 glDisable(GL_BLEND
);
2065 SHADER_USE( shader_wire
);
2066 glBindVertexArray( world
.wire
.vao
);
2068 glUniformMatrix3fv( SHADER_UNIFORM( shader_wire
, "uPv" ), 1, GL_FALSE
, (float *)vg_pv
);
2069 glUniform4f( SHADER_UNIFORM( shader_wire
, "uColour" ), 0.2f
, 0.2f
, 0.2f
, 1.0f
);
2073 glUniform1f( SHADER_UNIFORM( shader_wire
, "uCurve" ), 0.4f
);
2074 glUniform3f( SHADER_UNIFORM( shader_wire
, "uStart" ), drag_from_co
[0], drag_from_co
[1], 0.06f
);
2075 glUniform3f( SHADER_UNIFORM( shader_wire
, "uEnd" ), drag_to_co
[0], drag_to_co
[1], 0.06f
);
2076 glDrawElements( GL_TRIANGLES
, world
.wire
.em
, GL_UNSIGNED_SHORT
, (void*)(0) );
2079 float rp_x1
= world
.frame_lerp
*9.0f
;
2080 float rp_x2
= 1.0f
-rp_x1
*expf(1.0f
-rp_x1
)* 0.36f
;
2082 for( int y
= 2; y
< world
.h
-2; y
++ )
2084 for( int x
= 2; x
< world
.w
-2; x
++ )
2086 struct cell
*cell
= pcell((v2i
){x
,y
});
2088 if( cell
->state
& FLAG_CANAL
)
2090 if( cell
->state
& FLAG_IS_TRIGGER
)
2092 int trigger_id
= cell
->links
[0]?0:1;
2094 int x2
= cell
->links
[trigger_id
] % world
.w
;
2095 int y2
= (cell
->links
[trigger_id
] - x2
) / world
.w
;
2100 startpoint
[0] = (float)x2
+ (trigger_id
? 0.75f
: 0.25f
);
2101 startpoint
[1] = (float)y2
+ 0.25f
;
2103 endpoint
[0] = x
+0.5f
;
2104 endpoint
[1] = y
+0.5f
;
2106 glUniform1f( SHADER_UNIFORM( shader_wire
, "uCurve" ), cell
->state
& FLAG_TRIGGERED
? rp_x2
* 0.4f
: 0.4f
);
2107 glUniform3f( SHADER_UNIFORM( shader_wire
, "uStart" ), startpoint
[0], startpoint
[1], 0.04f
);
2108 glUniform3f( SHADER_UNIFORM( shader_wire
, "uEnd" ), endpoint
[0], endpoint
[1], 0.04f
);
2109 glDrawElements( GL_TRIANGLES
, world
.wire
.em
, GL_UNSIGNED_SHORT
, (void*)(0) );
2115 SHADER_USE( shader_tile_colour
);
2116 glUniformMatrix3fv( SHADER_UNIFORM( shader_tile_colour
, "uPv" ), 1, GL_FALSE
, (float *)vg_pv
);
2117 use_mesh( &world
.circle
);
2119 int const filled_start
= 0;
2120 int const filled_count
= 32;
2121 int const empty_start
= 32;
2122 int const empty_count
= 32*2;
2125 for( int i
= 0; i
< arrlen( world
.io
); i
++ )
2127 struct cell_terminal
*term
= &world
.io
[ i
];
2128 int posx
= term
->id
% world
.w
;
2129 int posy
= (term
->id
- posx
)/world
.w
;
2130 int is_input
= world
.data
[ term
->id
].state
& FLAG_INPUT
;
2132 v4f dot_colour
= { 0.0f
, 0.0f
, 0.0f
, 1.0f
};
2134 for( int k
= 0; k
< term
->run_count
; k
++ )
2136 for( int j
= 0; j
< term
->runs
[k
].condition_count
; j
++ )
2138 float y_offset
= is_input
? 1.2f
: -0.2f
;
2139 y_offset
+= (is_input
? 0.2f
: -0.2f
) * (float)k
;
2141 glUniform3f( SHADER_UNIFORM( shader_tile_colour
, "uOffset" ), (float)posx
+ 0.2f
+ 0.2f
* (float)j
, (float)posy
+ y_offset
, 0.1f
);
2145 colour_code_v3( term
->runs
[k
].conditions
[j
], dot_colour
);
2146 glUniform4fv( SHADER_UNIFORM( shader_tile_colour
, "uColour" ), 1, dot_colour
);
2148 // Draw filled if tick not passed, draw empty if empty
2149 if( (world
.sim_frame
> j
&& world
.sim_run
>= k
) || world
.sim_run
> k
)
2150 draw_mesh( empty_start
, empty_count
);
2152 draw_mesh( filled_start
, filled_count
);
2156 if( term
->runs
[k
].recv_count
> j
)
2158 colour_code_v3( term
->runs
[k
].recieved
[j
], dot_colour
);
2159 v3_muls( dot_colour
, 0.8f
, dot_colour
);
2160 glUniform4fv( SHADER_UNIFORM( shader_tile_colour
, "uColour" ), 1, dot_colour
);
2162 draw_mesh( filled_start
, filled_count
);
2165 colour_code_v3( term
->runs
[k
].conditions
[j
], dot_colour
);
2166 glUniform4fv( SHADER_UNIFORM( shader_tile_colour
, "uColour" ), 1, dot_colour
);
2168 draw_mesh( empty_start
, empty_count
);
2174 if( world
.simulating
)
2176 glUniform4f( SHADER_UNIFORM( shader_tile_colour
, "uColour" ), 0.0f
, 0.0f
, 0.0f
, 1.0f
);
2177 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
);
2178 draw_mesh( filled_start
, filled_count
);
2182 float const score_bright
= 1.25f
;
2183 glUniform4f( SHADER_UNIFORM( shader_tile_colour
, "uColour" ),
2184 0.4f
*score_bright
, 0.39f
*score_bright
, 0.45f
*score_bright
, 1.0f
);
2186 use_mesh( &world
.numbers
);
2187 draw_numbers( (v3f
){ 2.0f
, (float)world
.h
-1.875f
, 0.3333f
}, world
.score
);
2189 // Level selection UI
2190 use_mesh( &world
.circle
);
2191 float ratio
= ((float)vg_window_x
/(float)vg_window_y
);
2193 m3x3f ui_view
= M3X3_IDENTITY
;
2194 m3x3_scale( ui_view
, (v3f
){ 1.0f
, ratio
, 1.0f
} );
2195 glUniformMatrix3fv( SHADER_UNIFORM( shader_tile_colour
, "uPv" ), 1, GL_FALSE
, (float *)ui_view
);
2197 // Calculate mouse in UIsp
2198 v3f mouse_ui_space
= { ((float)vg_mouse
[0] / (float)(vg_window_x
)) * 2.0f
- 1.0f
,
2199 (((float)vg_mouse
[1] / (float)(vg_window_y
)) * 2.0f
- 1.0f
)*(-1.0f
/ratio
), 0.0125f
};
2201 // Get selected level
2202 const float selection_scale
= 0.05f
;
2203 int const level_count
= vg_list_size( level_pack_1
);
2204 int level_select
= -1;
2206 if( mouse_ui_space
[0] <= -0.8f
)
2208 float levels_range
= (float)level_count
*selection_scale
*0.6f
;
2209 float level_offset
= ((-mouse_ui_space
[1] + levels_range
) / levels_range
) * 0.5f
* (float)level_count
;
2210 level_select
= ceilf( level_offset
);
2213 if( level_select
>= 0 && level_select
< vg_list_size( level_pack_1
) )
2215 glUniform4f( SHADER_UNIFORM( shader_tile_colour
, "uColour" ), 0.369768f
, 0.3654f
, 0.42f
, 1.0f
);
2217 use_mesh( &world
.tile
);
2218 glUniform3f( SHADER_UNIFORM( shader_tile_colour
, "uOffset" ),
2220 ((float)level_count
- (float)level_select
* 2.0f
) * selection_scale
* 0.6f
,
2225 use_mesh( &world
.circle
);
2227 if( vg_get_button_down( "primary" ) )
2229 console_changelevel( 1, level_pack_1
+ level_select
);
2233 else mouse_ui_space
[1] = INFINITY
;
2235 glUniform4f( SHADER_UNIFORM( shader_tile_colour
, "uColour" ), 0.4f
, 0.39f
, 0.45f
, 1.0f
);
2238 for( int i
= 0; i
< level_count
; i
++ )
2240 struct career_level
*clevel
= &career
.levels
[i
];
2242 v3f level_ui_space
= {
2244 ((float)level_count
- (float)i
* 2.0f
) * selection_scale
* 0.6f
+ selection_scale
* 0.5f
,
2245 selection_scale
* 0.5f
2248 float scale
= vg_clampf( 1.0f
- fabsf(level_ui_space
[1] - mouse_ui_space
[1]) * 2.0f
, 0.9f
, 1.0f
);
2249 level_ui_space
[2] *= scale
;
2251 glUniform3fv( SHADER_UNIFORM( shader_tile_colour
, "uOffset" ), 1, level_ui_space
);
2253 if( clevel
->completed
)
2254 draw_mesh( filled_start
, filled_count
);
2256 draw_mesh( empty_start
, empty_count
);
2260 glUniform4f( SHADER_UNIFORM( shader_tile_colour
, "uColour" ), 0.4f
*1.25f
, 0.39f
*1.25f
, 0.45f
*1.25f
, 1.0f
);
2262 use_mesh( &world
.numbers
);
2263 for( int i
= 0; i
< level_count
; i
++ )
2265 struct career_level
*clevel
= &career
.levels
[i
];
2267 v3f level_ui_space
= {
2269 ((float)level_count
- (float)i
* 2.0f
) * selection_scale
* 0.6f
+ selection_scale
* 0.5f
,
2273 if( clevel
->completed
)
2274 draw_numbers( level_ui_space
, clevel
->score
);
2276 level_ui_space
[0] = -0.975f
;
2277 level_ui_space
[1] -= 0.01f
;
2278 draw_numbers( level_ui_space
, i
);
2281 //use_mesh( &world.numbers );
2282 //draw_numbers( (v3f){ 0.0f, -0.5f, 0.1f }, 128765 );