1 // Copyright (C) 2021 Harry Godden (hgn) - All Rights Reserved
4 #define VG_STEAM_APPID 1218140U
6 #include "fishladder_resources.h"
13 3. Merge (and explode)
14 4. Principle 1 (divide colours)
15 5. Principle 2 (combine colours)
28 const char *level_pack_1
[] = {
53 levels
[ vg_list_size( level_pack_1
) ];
55 career
= { .version
= 1 };
58 static void career_serialize(void)
60 vg_asset_write( "sav/game.sav", &career
, sizeof( struct career_state
) );
63 static void career_load(void)
66 struct career_state
*cr
= vg_asset_read_s( "sav/game.sav", &sz
);
68 memset( (void*)career
.levels
, 0, vg_list_size(level_pack_1
) * sizeof(struct career_level
) );
72 if( sz
> sizeof( struct career_state
) )
73 vg_warn( "This save file is too big! Some levels will be lost\n" );
75 if( sz
<= offsetof( struct career_state
, levels
) )
77 vg_error( "This save file is too small to have a header\n" );
82 u32
const size_header
= offsetof(struct career_state
, levels
);
83 u32
const size_levels
= sizeof(struct career_state
)-size_header
;
84 u32
const size_levels_input
= sz
- size_header
;
86 memcpy( (void*)career
.levels
, (void*)cr
->levels
, size_levels_input
);
88 if( sz
< sizeof( struct career_state
) )
90 memset( ((void*)career
.levels
) + size_levels_input
, 0, size_levels
-size_levels_input
);
94 vg_success( "Loaded save file... Info:\n" );
96 for( int i
= 0; i
< vg_list_size( career
.levels
); i
++ )
98 struct career_level
*lvl
= &career
.levels
[i
];
99 vg_info( "Score: %u, Time: %u, Completed: %u\n", lvl
->score
, lvl
->time
, lvl
->completed
);
104 vg_info( "No save file... Using blank one\n" );
112 #define FLAG_CANAL 0x1
113 #define FLAG_IS_TRIGGER 0x2
114 #define FLAG_RESERVED0 0x4
115 #define FLAG_RESERVED1 0x8
117 #define FLAG_INPUT 0x10
118 #define FLAG_OUTPUT 0x20
119 #define FLAG_WALL 0x40
121 #define FLAG_FLIP_FLOP 0x100
122 #define FLAG_TRIGGERED 0x200
123 #define FLAG_FLIP_ROTATING 0x400
124 #define FLAG_TARGETED 0x800
127 0000 0 | 0001 1 | 0010 2 | 0011 3
131 0100 4 | 0101 5 | 0110 6 | 0111 7
135 1000 8 | 1001 9 | 1010 10 | 1011 11
139 1100 12 | 1101 13 | 1110 14 | 1111 15
145 struct cell_description
153 cell_descriptions
[] =
157 { .start
= { 1, 0 }, .end
= { -1, 0 } },
158 { .start
= { 0, 1 }, .end
= { 0, -1 } },
159 { .start
= { 0, 1 }, .end
= { 1, 0 } },
161 { .start
= { -1, 0 }, .end
= { 1, 0 } },
162 { .start
= { -1, 0 }, .end
= { 1, 0 }, .is_linear
= 1 },
163 { .start
= { 0, 1 }, .end
= { -1, 0 } },
164 { .start
= { 0, 1 }, .is_special
= 1 },
166 { .start
= { 0, -1 }, .end
= { 0, 1 } },
167 { .start
= { 1, 0 }, .end
= { 0, -1 } },
168 { .start
= { 0, 1 }, .end
= { 0, -1 }, .is_linear
= 1 },
171 { .start
= { -1, 0 }, .end
= { 0, -1 } },
172 { .end
= { 0, -1 }, .is_special
= 1 },
179 k_cell_type_stub
= 0,
180 k_cell_type_ramp_right
= 3,
181 k_cell_type_ramp_left
= 6,
182 k_cell_type_split
= 7,
183 k_cell_type_merge
= 13,
184 k_cell_type_con_r
= 1,
185 k_cell_type_con_u
= 2,
186 k_cell_type_con_l
= 4,
187 k_cell_type_con_d
= 8
190 v2f
const curve_3
[] = {{0.5f
,1.0f
},{0.5f
,0.625f
},{0.625f
,0.5f
},{1.0f
,0.5f
}};
191 v2f
const curve_6
[] = {{0.5f
,1.0f
},{0.5f
,0.625f
},{0.375f
,0.5f
},{0.0f
,0.5f
}};
192 v2f
const curve_9
[] = {{1.0f
,0.5f
},{0.625f
,0.5f
},{0.5f
,0.375f
},{0.5f
,0.0f
}};
193 v2f
const curve_12
[]= {{0.0f
,0.5f
},{0.375f
,0.5f
},{0.5f
,0.375f
},{0.5f
,0.0f
}};
195 v2f
const curve_1
[] = {{1.0f
,0.5f
},{0.8f
,0.5f
},{0.3f
,0.5f
},{0.2f
,0.5f
}};
196 v2f
const curve_4
[] = {{0.0f
,0.5f
},{0.3f
,0.5f
},{0.5f
,0.5f
},{0.8f
,0.5f
}};
197 v2f
const curve_2
[] = {{0.5f
,1.0f
},{0.5f
,0.8f
},{0.5f
,0.3f
},{0.5f
,0.2f
}};
198 v2f
const curve_8
[] = {{0.5f
,0.0f
},{0.5f
,0.3f
},{0.5f
,0.5f
},{0.5f
,0.8f
}};
200 v2f
const curve_7
[] = {{0.5f
,0.8438f
},{0.875f
,0.8438f
},{0.625f
,0.5f
},{1.0f
,0.5f
}};
201 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
}};
203 float const curve_7_linear_section
= 0.1562f
;
206 { { 1.0f
, 0.9f
, 0.3f
},
207 { 0.2f
, 0.9f
, 0.14f
},
208 { 0.4f
, 0.8f
, 1.00f
} };
210 static void colour_code_v3( char cc
, v3f target
)
212 if( cc
>= 'a' && cc
<= 'z' )
216 if( id
< vg_list_size( colour_sets
) )
218 v3_copy( colour_sets
[ id
], target
);
223 v3_copy( (v3f
){0.0f
,0.0f
,0.0f
}, target
);
232 static void init_mesh( struct mesh
*m
, float const *tris
, u32 length
)
234 m
->elements
= length
/3;
235 glGenVertexArrays( 1, &m
->vao
);
236 glGenBuffers( 1, &m
->vbo
);
238 glBindVertexArray( m
->vao
);
239 glBindBuffer( GL_ARRAY_BUFFER
, m
->vbo
);
240 glBufferData( GL_ARRAY_BUFFER
, length
*sizeof(float), tris
, GL_STATIC_DRAW
);
242 glVertexAttribPointer( 0, 2, GL_FLOAT
, GL_FALSE
, 2 * sizeof(float), (void*)0 );
243 glEnableVertexAttribArray( 0 );
248 static void free_mesh( struct mesh
*m
)
250 glDeleteVertexArrays( 1, &m
->vao
);
251 glDeleteBuffers( 1, &m
->vbo
);
254 static void draw_mesh( int const start
, int const count
)
256 glDrawArrays( GL_TRIANGLES
, start
*3, count
*3 );
259 static void use_mesh( struct mesh
*m
)
261 glBindVertexArray( m
->vao
);
266 k_fish_state_soon_dead
= -1,
267 k_fish_state_dead
= 0,
270 k_fish_state_soon_alive
293 int sim_run
, max_runs
;
308 int condition_count
, recv_count
;
320 struct mesh tile
, circle
, numbers
;
323 GLuint vao
, vbo
, ebo
;
328 GLuint background_data
;
329 GLuint random_samples
;
331 int selected
, tile_x
, tile_y
;
338 enum e_fish_state state
;
350 struct career_level
*ptr_career_level
;
357 static void map_free(void)
359 arrfree( world
.data
);
370 world
.initialzed
= 0;
373 static void io_reset(void)
375 for( int i
= 0; i
< arrlen( world
.io
); i
++ )
377 struct cell_terminal
*term
= &world
.io
[i
];
379 for( int j
= 0; j
< term
->run_count
; j
++ )
380 term
->runs
[j
].recv_count
= 0;
384 static void map_reclassify( v2i start
, v2i end
, int update_texbuffer
);
385 static int map_load( const char *str
, const char *name
)
387 //TODO: It may be worthwhile, at this point, to switch to binary encoding for save data
396 if( str
[world
.w
] == ';' )
398 else if( !str
[world
.w
] )
400 vg_error( "Unexpected EOF when parsing level\n" );
405 struct cell
*row
= arraddnptr( world
.data
, world
.w
);
407 int reg_start
= 0, reg_end
= 0;
409 u32
*links_to_make
= NULL
;
410 int links_satisfied
= 0;
412 char link_id_buffer
[32];
420 if( *c
== '\r' ) { c
++; continue; } // fuck off windows
426 if( *c
== '\r' ) c
++;
433 if( *c
== '\r' ) { c
++; continue; }
435 if( reg_start
< reg_end
)
437 struct cell_terminal
*terminal
= &world
.io
[ reg_start
];
438 struct terminal_run
*run
= &terminal
->runs
[ terminal
->run_count
-1 ];
440 if( *c
>= 'a' && *c
<= 'z' )
442 run
->conditions
[ run
->condition_count
++ ] = *c
;
446 if( *c
== ',' || *c
== '\n' )
455 terminal
->runs
[ terminal
->run_count
].condition_count
= 0;
456 terminal
->run_count
++;
457 world
.max_runs
= vg_max( world
.max_runs
, terminal
->run_count
);
461 vg_error( "Unkown attribute '%c' (row: %u)\n", *c
, world
.h
);
468 if( links_satisfied
< arrlen( links_to_make
) )
470 struct cell
*target
= &world
.data
[ links_to_make
[ links_satisfied
] ];
472 if( (((u32
)*c
>= (u32
)'0') && ((u32
)*c
<= (u32
)'9')) || *c
== '-' )
474 if( link_id_n
>= vg_list_size( link_id_buffer
)-1 )
476 vg_error( "Number was way too long to be parsed (row: %u)\n", world
.h
);
480 link_id_buffer
[ link_id_n
++ ] = *c
;
482 else if( *c
== ',' || *c
== '\n' )
484 link_id_buffer
[ link_id_n
] = 0x00;
485 int value
= atoi( link_id_buffer
);
487 target
->links
[value
>= 0? 1:0] = abs(value
);
496 vg_error( "Invalid character '%c' (row: %u)\n", *c
, world
.h
);
502 vg_error( "Too many values to assign (row: %u)\n", world
.h
);
511 // Registry length-error checks
512 if( reg_start
!= reg_end
)
514 vg_error( "Not enough spawn values assigned (row: %u, %u of %u)\n", world
.h
, reg_start
, reg_end
);
518 if( links_satisfied
!= arrlen( links_to_make
) )
520 vg_error( "Not enough link values assigned (row: %u, %u of %u)\n", world
.h
, links_satisfied
, arrlen( links_to_make
) );
526 vg_error( "Not enough cells to match previous row definition (row: %u, %u<%u)\n", world
.h
, cx
, world
.w
);
530 row
= arraddnptr( world
.data
, world
.w
);
533 reg_end
= reg_start
= arrlen( world
.io
);
535 arrsetlen( links_to_make
, 0 );
542 vg_error( "Too many cells to match previous row definition (row: %u, %u>%u)\n", world
.h
, cx
, world
.w
);
546 // Tile initialization
548 struct cell
*cell
= &row
[ cx
];
551 if( *c
== '+' || *c
== '-' )
553 struct cell_terminal
*term
= arraddnptr( world
.io
, 1 );
554 term
->id
= cx
+ world
.h
*world
.w
;
556 term
->runs
[0].condition_count
= 0;
558 cell
->state
= *c
== '+'? FLAG_INPUT
: FLAG_OUTPUT
;
561 else if( *c
== '#' ) cell
->state
= FLAG_WALL
;
562 else if( ((u32
)*c
>= (u32
)'A') && ((u32
)*c
<= (u32
)'A'+0xf) )
564 // Canal flag bits (4bit/16 value):
570 cell
->state
= ((u32
)*c
- (u32
)'A') & (FLAG_CANAL
|FLAG_IS_TRIGGER
);
572 if( cell
->state
& FLAG_IS_TRIGGER
)
573 arrpush( links_to_make
, cx
+ world
.h
*world
.w
);
579 else cell
->state
= 0x00;
587 // Update data texture to fill out the background
589 u8 info_buffer
[64*64*4];
590 for( int i
= 0; i
< 64*64; i
++ )
592 u8
*px
= &info_buffer
[i
*4];
599 glBindTexture( GL_TEXTURE_2D
, world
.background_data
);
600 glTexSubImage2D( GL_TEXTURE_2D
, 0, 0, 0, 64, 64, GL_RGBA
, GL_UNSIGNED_BYTE
, info_buffer
);
603 arrfree( links_to_make
);
605 map_reclassify( NULL
, NULL
, 1 );
608 for( int i
= 0; i
< world
.h
*world
.w
; i
++ )
610 struct cell
*src
= &world
.data
[i
];
611 if( src
->state
& FLAG_IS_TRIGGER
)
613 int link_id
= src
->links
[0]?0:1;
614 if( src
->links
[link_id
] <= world
.h
*world
.w
)
616 struct cell
*target
= &world
.data
[ src
->links
[link_id
] ];
617 if( (target
->state
& FLAG_CANAL
) && (target
->config
== k_cell_type_split
) )
619 if( target
->links
[ link_id
] )
621 vg_error( "Link target was already targeted\n" );
627 target
->links
[ link_id
] = i
;
628 target
->state
|= FLAG_TARGETED
;
633 vg_error( "Link target was invalid\n" );
639 vg_error( "Link target out of bounds\n" );
645 vg_success( "Map '%s' loaded! (%u:%u)\n", name
, world
.w
, world
.h
);
649 strncpy( world
.map_name
, name
, vg_list_size( world
.map_name
)-1 );
650 world
.initialzed
= 1;
654 arrfree( links_to_make
);
659 static struct cell
*pcell( v2i pos
)
661 return &world
.data
[ pos
[1]*world
.w
+ pos
[0] ];
664 static void map_serialize( FILE *stream
)
666 for( int y
= 0; y
< world
.h
; y
++ )
668 for( int x
= 0; x
< world
.w
; x
++ )
670 struct cell
*cell
= pcell( (v2i
){ x
, y
} );
672 if( cell
->state
& FLAG_WALL
) fputc( '#', stream
);
673 else if( cell
->state
& FLAG_INPUT
) fputc( '+', stream
);
674 else if( cell
->state
& FLAG_OUTPUT
) fputc( '-', stream
);
675 else if( cell
->state
& (FLAG_CANAL
|FLAG_IS_TRIGGER
|FLAG_RESERVED0
|FLAG_RESERVED1
) )
677 fputc( (cell
->state
& (FLAG_CANAL
|FLAG_IS_TRIGGER
|FLAG_RESERVED0
|FLAG_RESERVED1
)) + (u32
)'A', stream
);
679 else fputc( ' ', stream
);
682 fputc( ';', stream
);
684 int terminal_write_count
= 0;
686 for( int x
= 0; x
< world
.w
; x
++ )
688 for( int i
= 0; i
< arrlen( world
.io
); i
++ )
690 struct cell_terminal
*term
= &world
.io
[i
];
691 if( term
->id
== y
*world
.w
+x
)
693 if( terminal_write_count
)
694 fputc( ',', stream
);
695 terminal_write_count
++;
697 for( int j
= 0; j
< term
->run_count
; j
++ )
699 struct terminal_run
*run
= &term
->runs
[j
];
701 for( int k
= 0; k
< run
->condition_count
; k
++ )
702 fputc( run
->conditions
[k
], stream
);
704 if( j
< term
->run_count
-1 )
705 fputc( ':', stream
);
711 for( int x
= 0; x
< world
.w
; x
++ )
713 struct cell
*cell
= pcell( (v2i
){ x
,y
} );
714 if( cell
->state
& FLAG_IS_TRIGGER
)
716 if( terminal_write_count
)
717 fputc( ',', stream
);
718 terminal_write_count
++;
720 fprintf( stream
, "%d", cell
->links
[0]? -cell
->links
[0]: cell
->links
[1] );
724 fputc( '\n', stream
);
728 int main( int argc
, char *argv
[] )
730 vg_init( argc
, argv
, "Marble Computing | SPACE: Test | LeftClick: Toggle tile | RightClick: Drag wire" );
734 static int console_credits( int argc
, char const *argv
[] )
736 vg_info( "Aknowledgements:\n" );
737 vg_info( " GLFW zlib/libpng glfw.org\n" );
738 vg_info( " miniaudio MIT0 miniaud.io\n" );
739 vg_info( " QOI MIT phoboslab.org\n" );
740 vg_info( " STB library MIT nothings.org\n" );
741 vg_info( " Weiholmir font justfredrik.itch.io\n" );
745 static int console_save_map( int argc
, char const *argv
[] )
747 if( !world
.initialzed
)
749 vg_error( "Tried to save uninitialized map!\n" );
753 char map_path
[ 256 ];
755 strcpy( map_path
, "sav/" );
756 strcat( map_path
, world
.map_name
);
757 strcat( map_path
, ".map" );
759 FILE *test_writer
= fopen( map_path
, "wb" );
762 vg_info( "Saving map to '%s'\n", map_path
);
763 map_serialize( test_writer
);
765 fclose( test_writer
);
770 vg_error( "Unable to open stream for writing\n" );
775 static int console_load_map( int argc
, char const *argv
[] )
777 char map_path
[ 256 ];
782 strcpy( map_path
, "sav/" );
783 strcat( map_path
, argv
[0] );
784 strcat( map_path
, ".map" );
786 char *text_source
= vg_textasset_read( map_path
);
790 strcpy( map_path
, "maps/" );
791 strcat( map_path
, argv
[0] );
792 strcat( map_path
, ".map" );
794 text_source
= vg_textasset_read( map_path
);
799 vg_info( "Loading map: '%s'\n", map_path
);
800 world
.ptr_career_level
= NULL
;
802 if( !map_load( text_source
, argv
[0] ) )
810 for( int i
= 0; i
< vg_list_size( level_pack_1
); i
++ )
812 if( !strcmp( level_pack_1
[i
], argv
[0] ) )
814 world
.ptr_career_level
= career
.levels
+ i
;
823 vg_error( "Missing maps '%s'\n", argv
[0] );
829 vg_error( "Missing argument <map_path>\n" );
834 static void simulation_stop(void)
836 world
.simulating
= 0;
837 world
.num_fishes
= 0;
843 sfx_system_fadeout( &audio_system_balls_rolling
, 44100 );
845 vg_info( "Stopping simulation!\n" );
848 static int console_changelevel( int argc
, char const *argv
[] )
852 // Save current level
853 console_save_map( 0, NULL
);
854 if( console_load_map( argc
, argv
) )
862 vg_error( "Missing argument <map_path>\n" );
870 vg_function_push( (struct vg_cmd
){
871 .name
= "_map_write",
872 .function
= console_save_map
875 vg_function_push( (struct vg_cmd
){
877 .function
= console_load_map
880 vg_function_push( (struct vg_cmd
){
882 .function
= console_changelevel
885 vg_function_push( (struct vg_cmd
){
887 .function
= console_credits
894 0.0f
, 0.0f
, 0.0f
, 1.0f
, 1.0f
, 1.0f
,
895 0.0f
, 0.0f
, 1.0f
, 1.0f
, 1.0f
, 0.0f
,
897 0.0f
, 0.0f
, 0.0f
, 1.0f
, 4.0f
, 1.0f
,
898 0.0f
, 0.0f
, 4.0f
, 1.0f
, 4.0f
, 0.0f
901 init_mesh( &world
.tile
, quad_mesh
, vg_list_size(quad_mesh
) );
906 float circle_mesh
[32*6*3];
907 int res
= vg_list_size( circle_mesh
) / (6*3);
909 for( int i
= 0; i
< res
; i
++ )
911 v2f v0
= { sinf( ((float)i
/(float)res
)*VG_TAUf
), cosf( ((float)i
/(float)res
)*VG_TAUf
) };
912 v2f v1
= { sinf( ((float)(i
+1)/(float)res
)*VG_TAUf
), cosf( ((float)(i
+1)/(float)res
)*VG_TAUf
) };
914 circle_mesh
[ i
*6+0 ] = 0.0f
;
915 circle_mesh
[ i
*6+1 ] = 0.0f
;
917 v2_copy( v0
, circle_mesh
+ 32*6 + i
*12 );
918 v2_muls( v0
, 0.8f
, circle_mesh
+ 32*6 + i
*12+2 );
919 v2_copy( v1
, circle_mesh
+ 32*6 + i
*12+4 );
921 v2_copy( v1
, circle_mesh
+ 32*6 + i
*12+6 );
922 v2_muls( v1
, 0.8f
, circle_mesh
+ 32*6 + i
*12+8 );
923 v2_muls( v0
, 0.8f
, circle_mesh
+ 32*6 + i
*12+10 );
925 v2_copy( v0
, circle_mesh
+ i
*6+4 );
926 v2_copy( v1
, circle_mesh
+ i
*6+2 );
927 v2_copy( v0
, circle_mesh
+i
*6+4 );
928 v2_copy( v1
, circle_mesh
+i
*6+2 );
931 init_mesh( &world
.circle
, circle_mesh
, vg_list_size( circle_mesh
) );
936 init_mesh( &world
.numbers
,
938 vg_list_size( MESH_NUMBERS_BUFFER
)
941 for( int i
= 0; i
< 10; i
++ )
943 vg_info( "offset: %u, length: %u\n", MESH_NUMBERS_OFFSETS
[i
][0], MESH_NUMBERS_OFFSETS
[i
][1] );
949 int const num_segments
= 64;
951 struct mesh_wire
*mw
= &world
.wire
;
953 v2f wire_points
[ num_segments
* 2 ];
954 u16 wire_indices
[ 6*(num_segments
-1) ];
956 for( int i
= 0; i
< num_segments
; i
++ )
958 float l
= (float)i
/ (float)(num_segments
-1);
960 v2_copy( (v2f
){ l
, -0.5f
}, wire_points
[i
*2+0] );
961 v2_copy( (v2f
){ l
, 0.5f
}, wire_points
[i
*2+1] );
963 if( i
< num_segments
-1 )
965 wire_indices
[ i
*6+0 ] = i
*2 + 0;
966 wire_indices
[ i
*6+1 ] = i
*2 + 1;
967 wire_indices
[ i
*6+2 ] = i
*2 + 3;
968 wire_indices
[ i
*6+3 ] = i
*2 + 0;
969 wire_indices
[ i
*6+4 ] = i
*2 + 3;
970 wire_indices
[ i
*6+5 ] = i
*2 + 2;
974 glGenVertexArrays( 1, &mw
->vao
);
975 glGenBuffers( 1, &mw
->vbo
);
976 glGenBuffers( 1, &mw
->ebo
);
977 glBindVertexArray( mw
->vao
);
979 glBindBuffer( GL_ARRAY_BUFFER
, mw
->vbo
);
981 glBufferData( GL_ARRAY_BUFFER
, sizeof( wire_points
), wire_points
, GL_STATIC_DRAW
);
982 glBindVertexArray( mw
->vao
);
984 glBindBuffer( GL_ELEMENT_ARRAY_BUFFER
, mw
->ebo
);
985 glBufferData( GL_ELEMENT_ARRAY_BUFFER
, sizeof( wire_indices
), wire_indices
, GL_STATIC_DRAW
);
988 glVertexAttribPointer( 0, 2, GL_FLOAT
, GL_FALSE
, 2*sizeof(float), (void*)0 );
989 glEnableVertexAttribArray( 0 );
993 mw
->em
= vg_list_size( wire_indices
);
996 // Create info data texture
998 glGenTextures( 1, &world
.background_data
);
999 glBindTexture( GL_TEXTURE_2D
, world
.background_data
);
1000 glTexImage2D( GL_TEXTURE_2D
, 0, GL_RGBA
, 64, 64, 0, GL_RGBA
, GL_UNSIGNED_BYTE
, NULL
);
1004 // Create random smaples texture
1006 u8
*data
= malloc(512*512*2);
1007 for( int i
= 0; i
< 512*512*2; i
++ )
1008 data
[ i
] = rand()/(RAND_MAX
/255);
1010 glGenTextures( 1, &world
.random_samples
);
1011 glBindTexture( GL_TEXTURE_2D
, world
.random_samples
);
1012 glTexImage2D( GL_TEXTURE_2D
, 0, GL_RG
, 512, 512, 0, GL_RG
, GL_UNSIGNED_BYTE
, data
);
1019 resource_load_main();
1021 // Restore gamestate
1023 console_load_map( 1, level_pack_1
);
1028 console_save_map( 0, NULL
);
1031 resource_free_main();
1033 glDeleteTextures( 1, &world
.background_data
);
1034 glDeleteTextures( 1, &world
.random_samples
);
1036 glDeleteVertexArrays( 1, &world
.wire
.vao
);
1037 glDeleteBuffers( 1, &world
.wire
.vbo
);
1038 glDeleteBuffers( 1, &world
.wire
.ebo
);
1040 free_mesh( &world
.tile
);
1041 free_mesh( &world
.circle
);
1042 free_mesh( &world
.numbers
);
1047 static int world_check_pos_ok( v2i co
)
1049 return (co
[0] < 2 || co
[0] >= world
.w
-2 || co
[1] < 2 || co
[1] >= world
.h
-2)? 0: 1;
1052 static int cell_interactive( v2i co
)
1055 if( !world_check_pos_ok( co
) )
1059 if( world
.data
[ world
.w
*co
[1] + co
[0] ].state
& (FLAG_WALL
|FLAG_INPUT
|FLAG_OUTPUT
) )
1062 // List of 3x3 configurations that we do not allow
1063 static u32 invalid_src
[][9] =
1095 // Statically compile invalid configurations into bitmasks
1096 static u32 invalid
[ vg_list_size(invalid_src
) ];
1098 for( int i
= 0; i
< vg_list_size(invalid_src
); i
++ )
1102 for( int j
= 0; j
< 3; j
++ )
1103 for( int k
= 0; k
< 3; k
++ )
1104 comped
|= invalid_src
[i
][ j
*3+k
] << ((j
*5)+k
);
1106 invalid
[i
] = comped
;
1109 // Extract 5x5 grid surrounding tile
1111 for( int y
= co
[1]-2; y
< co
[1]+3; y
++ )
1112 for( int x
= co
[0]-2; x
< co
[0]+3; x
++ )
1114 struct cell
*cell
= pcell((v2i
){x
,y
});
1116 if( cell
&& (cell
->state
& (FLAG_CANAL
|FLAG_INPUT
|FLAG_OUTPUT
)) )
1117 blob
|= 0x1 << ((y
-(co
[1]-2))*5 + x
-(co
[0]-2));
1120 // Run filter over center 3x3 grid to check for invalid configurations
1121 int kernel
[] = { 0, 1, 2, 5, 6, 7, 10, 11, 12 };
1122 for( int i
= 0; i
< vg_list_size(kernel
); i
++ )
1124 if( blob
& (0x1 << (6+kernel
[i
])) )
1126 u32 window
= blob
>> kernel
[i
];
1128 for( int j
= 0; j
< vg_list_size(invalid
); j
++ )
1129 if((window
& invalid
[j
]) == invalid
[j
])
1137 static void map_reclassify( v2i start
, v2i end
, int update_texbuffer
)
1139 v2i full_start
= { 1,1 };
1140 v2i full_end
= { world
.w
-1, world
.h
-1 };
1142 if( !start
|| !end
)
1149 u8 info_buffer
[64*64*4];
1152 int px0
= vg_max( start
[0], full_start
[0] ),
1153 px1
= vg_min( end
[0], full_end
[0] ),
1154 py0
= vg_max( start
[1], full_start
[1] ),
1155 py1
= vg_min( end
[1], full_end
[1] );
1157 for( int y
= py0
; y
< py1
; y
++ )
1159 for( int x
= px0
; x
< px1
; x
++ )
1161 struct cell
*cell
= pcell((v2i
){x
,y
});
1163 v2i dirs
[] = {{1,0},{0,1},{-1,0},{0,-1}};
1168 if( cell
->state
& (FLAG_CANAL
|FLAG_INPUT
|FLAG_OUTPUT
) )
1170 for( int i
= 0; i
< vg_list_size( dirs
); i
++ )
1172 struct cell
*neighbour
= pcell((v2i
){x
+dirs
[i
][0], y
+dirs
[i
][1]});
1173 if( neighbour
->state
& (FLAG_CANAL
|FLAG_INPUT
|FLAG_OUTPUT
) )
1181 if( cell
->state
& FLAG_WALL
)
1187 pcell((v2i
){x
,y
})->config
= config
;
1189 u8
*info_px
= &info_buffer
[ (pixel_id
++)*4 ];
1190 info_px
[0] = height
;
1191 info_px
[1] = cell
->state
& FLAG_WALL
? 0: 255;
1197 ((cell
->state
& FLAG_IS_TRIGGER
) && (cell
->config
== 0xF || cell
->config
== k_cell_type_split
)) ||
1198 ((cell
->state
& FLAG_TARGETED
) && (cell
->config
!= k_cell_type_split
))
1199 ) && update_texbuffer
1201 cell
->state
&= ~(FLAG_TARGETED
|FLAG_IS_TRIGGER
);
1202 for( u32 i
= 0; i
< 2; i
++ )
1204 if( cell
->links
[i
] )
1206 struct cell
*other_ptr
= &world
.data
[ cell
->links
[i
] ];
1207 other_ptr
->links
[ i
] = 0;
1208 other_ptr
->state
&= ~FLAG_IS_TRIGGER
;
1210 if( other_ptr
->links
[ i
^ 0x1 ] == 0 )
1211 other_ptr
->state
&= ~FLAG_TARGETED
;
1221 if( update_texbuffer
)
1223 glBindTexture( GL_TEXTURE_2D
, world
.background_data
);
1224 glTexSubImage2D( GL_TEXTURE_2D
, 0, px0
+ 16, py0
+ 16, px1
-px0
, py1
-py0
, GL_RGBA
, GL_UNSIGNED_BYTE
, info_buffer
);
1228 u16 id_drag_from
= 0;
1232 void vg_update(void)
1234 // Fit within screen
1236 float r1
= (float)vg_window_y
/ (float)vg_window_x
,
1237 r2
= (float)world
.h
/ (float)world
.w
,
1240 size
= ( r2
< r1
? (float)world
.w
* 0.5f
: ((float)world
.h
* 0.5f
) / r1
) + 2.5f
;
1241 m3x3_projection( m_projection
, -size
, size
, -size
*r1
, size
*r1
);
1244 origin
[0] = floorf( -0.5f
* world
.w
);
1245 origin
[1] = floorf( -0.5f
* world
.h
);
1248 m3x3_identity( m_view
);
1249 m3x3_translate( m_view
, origin
);
1250 m3x3_mul( m_projection
, m_view
, vg_pv
);
1251 vg_projection_update();
1254 v2_copy( vg_mouse_ws
, world
.tile_pos
);
1256 world
.tile_x
= floorf( world
.tile_pos
[0] );
1257 world
.tile_y
= floorf( world
.tile_pos
[1] );
1260 if( !world
.simulating
)
1262 v2_copy( vg_mouse_ws
, drag_to_co
);
1264 if( cell_interactive( (v2i
){ world
.tile_x
, world
.tile_y
} ))
1266 world
.selected
= world
.tile_y
* world
.w
+ world
.tile_x
;
1268 static u32 modify_state
= 0;
1270 struct cell
*cell_ptr
= &world
.data
[world
.selected
];
1272 if( vg_get_button_down("primary") )
1274 modify_state
= (cell_ptr
->state
& FLAG_CANAL
) ^ FLAG_CANAL
;
1277 if( vg_get_button("primary") && ((cell_ptr
->state
& FLAG_CANAL
) != modify_state
) )
1279 cell_ptr
->state
&= ~FLAG_CANAL
;
1280 cell_ptr
->state
|= modify_state
;
1282 if( cell_ptr
->state
& FLAG_CANAL
)
1284 cell_ptr
->links
[0] = 0;
1285 cell_ptr
->links
[1] = 0;
1287 sfx_set_playrnd( &audio_tile_mod
, &audio_system_sfx
, 3, 6 );
1292 sfx_set_playrnd( &audio_tile_mod
, &audio_system_sfx
, 0, 3 );
1296 map_reclassify( (v2i
){ world
.tile_x
-2, world
.tile_y
-2 },
1297 (v2i
){ world
.tile_x
+2, world
.tile_y
+2 }, 1 );
1300 if( vg_get_button_down("secondary") && !(cell_ptr
->config
== k_cell_type_split
) )
1302 id_drag_from
= world
.selected
;
1303 drag_from_co
[0] = world
.tile_x
+ 0.5f
;
1304 drag_from_co
[1] = world
.tile_y
+ 0.5f
;
1307 if( id_drag_from
&& (cell_ptr
->config
== k_cell_type_split
) )
1309 float local_x
= vg_mouse_ws
[0] - (float)world
.tile_x
;
1310 drag_to_co
[0] = (float)world
.tile_x
+ (local_x
> 0.5f
? 0.75f
: 0.25f
);
1311 drag_to_co
[1] = (float)world
.tile_y
+ 0.25f
;
1313 if( vg_get_button_up("secondary") )
1315 struct cell
*drag_ptr
= &world
.data
[id_drag_from
];
1316 u32 link_id
= local_x
> 0.5f
? 1: 0;
1318 // Cleanup existing connections
1319 if( cell_ptr
->links
[ link_id
] )
1321 vg_warn( "Destroying existing connection on link %u (%hu)\n", link_id
, cell_ptr
->links
[ link_id
] );
1323 struct cell
*current_connection
= &world
.data
[ cell_ptr
->links
[ link_id
]];
1324 current_connection
->state
&= ~FLAG_IS_TRIGGER
;
1325 current_connection
->links
[ link_id
] = 0;
1328 if( drag_ptr
->links
[ link_id
^ 0x1 ] )
1330 vg_warn( "Destroying alternate link %u (%hu)\n", link_id
^ 0x1, drag_ptr
->links
[ link_id
^ 0x1 ] );
1332 struct cell
*current_connection
= &world
.data
[ drag_ptr
->links
[ link_id
^ 0x1 ]];
1333 if( !current_connection
->links
[ link_id
] )
1334 current_connection
->state
&= ~FLAG_TARGETED
;
1336 current_connection
->links
[ link_id
^ 0x1 ] = 0;
1337 drag_ptr
->links
[ link_id
^ 0x1 ] = 0;
1340 // Create the new connection
1341 vg_success( "Creating connection on link %u (%hu)\n", link_id
, id_drag_from
);
1343 cell_ptr
->links
[ link_id
] = id_drag_from
;
1344 drag_ptr
->links
[ link_id
] = world
.selected
;
1346 cell_ptr
->state
|= FLAG_TARGETED
;
1347 drag_ptr
->state
|= FLAG_IS_TRIGGER
;
1353 world
.selected
= -1;
1355 if( !(vg_get_button("secondary") && id_drag_from
) )
1360 world
.selected
= -1;
1364 // Simulation stop/start
1365 if( vg_get_button_down("go") )
1367 if( world
.simulating
)
1373 vg_success( "Starting simulation!\n" );
1375 sfx_set_playrnd( &audio_rolls
, &audio_system_balls_rolling
, 0, 1 );
1377 world
.simulating
= 1;
1378 world
.num_fishes
= 0;
1379 world
.sim_frame
= 0;
1380 world
.sim_start
= vg_time
;
1382 world
.sim_speed
= 2.5f
;
1384 for( int i
= 0; i
< world
.w
*world
.h
; i
++ )
1385 world
.data
[ i
].state
&= ~FLAG_FLIP_FLOP
;
1392 if( world
.simulating
)
1394 while( world
.sim_frame
< (int)((vg_time
-world
.sim_start
)*world
.sim_speed
) )
1396 //vg_info( "frame: %u\n", world.sim_frame );
1397 sfx_set_playrnd( &audio_random
, &audio_system_balls_switching
, 0, 9 );
1399 // Update splitter deltas
1400 for( int i
= 0; i
< world
.h
*world
.w
; i
++ )
1402 struct cell
*cell
= &world
.data
[i
];
1403 if( cell
->config
== k_cell_type_split
)
1405 cell
->state
&= ~FLAG_FLIP_ROTATING
;
1407 if( cell
->state
& FLAG_IS_TRIGGER
)
1408 cell
->state
&= ~FLAG_TRIGGERED
;
1411 int alive_count
= 0;
1413 // Update fish positions
1414 for( int i
= 0; i
< world
.num_fishes
; i
++ )
1416 struct fish
*fish
= &world
.fishes
[i
];
1418 if( fish
->state
== k_fish_state_soon_dead
)
1419 fish
->state
= k_fish_state_dead
;
1421 if( fish
->state
== k_fish_state_soon_alive
)
1422 fish
->state
= k_fish_state_alive
;
1424 if( fish
->state
< k_fish_state_alive
)
1427 struct cell
*cell_current
= pcell( fish
->pos
);
1429 if( fish
->state
== k_fish_state_alive
)
1432 if( cell_current
->state
& FLAG_OUTPUT
)
1434 for( int j
= 0; j
< arrlen( world
.io
); j
++ )
1436 struct cell_terminal
*term
= &world
.io
[j
];
1438 if( term
->id
== fish
->pos
[1]*world
.w
+ fish
->pos
[0] )
1440 struct terminal_run
*run
= &term
->runs
[ world
.sim_run
];
1441 if( run
->recv_count
< vg_list_size( run
->recieved
) )
1442 run
->recieved
[ run
->recv_count
++ ] = fish
->payload
;
1448 fish
->state
= k_fish_state_dead
;
1453 if( cell_current
->config
== k_cell_type_merge
)
1458 fish
->flow_reversed
= 0;
1462 if( cell_current
->config
== k_cell_type_split
)
1465 fish
->dir
[0] = cell_current
->state
&FLAG_FLIP_FLOP
?1:-1;
1468 if( !(cell_current
->state
& FLAG_TARGETED
) )
1469 cell_current
->state
^= FLAG_FLIP_FLOP
;
1473 // Apply cell out-flow
1474 struct cell_description
*desc
= &cell_descriptions
[ cell_current
->config
];
1476 v2i_copy( fish
->flow_reversed
? desc
->start
: desc
->end
, fish
->dir
);
1480 v2i_add( fish
->pos
, fish
->dir
, pos_next
);
1482 struct cell
*cell_next
= pcell( pos_next
);
1484 if( cell_next
->state
& (FLAG_CANAL
|FLAG_OUTPUT
) )
1486 struct cell_description
*desc
= &cell_descriptions
[ cell_next
->config
];
1488 if( cell_next
->config
== k_cell_type_merge
)
1490 if( fish
->dir
[0] == 0 )
1491 fish
->state
= k_fish_state_dead
;
1493 fish
->flow_reversed
= 0;
1497 if( cell_next
->config
== k_cell_type_split
)
1499 if( fish
->dir
[0] == 0 )
1501 sfx_set_playrnd( &audio_splitter
, &audio_system_balls_important
, 0, 1 );
1502 cell_next
->state
|= FLAG_FLIP_ROTATING
;
1504 fish
->flow_reversed
= 0;
1507 fish
->state
= k_fish_state_dead
;
1510 fish
->flow_reversed
= ( fish
->dir
[0] != -desc
->start
[0] ||
1511 fish
->dir
[1] != -desc
->start
[1] )? 1: 0;
1515 fish
->state
= world_check_pos_ok( fish
->pos
)? k_fish_state_bg
: k_fish_state_dead
;
1518 //v2i_add( fish->pos, fish->dir, fish->pos );
1520 else if( fish
->state
== k_fish_state_bg
)
1522 v2i_add( fish
->pos
, fish
->dir
, fish
->pos
);
1524 if( !world_check_pos_ok( fish
->pos
) )
1525 fish
->state
= k_fish_state_dead
;
1528 struct cell
*cell_entry
= pcell( fish
->pos
);
1530 if( cell_entry
->state
& FLAG_CANAL
)
1532 if( cell_entry
->config
== k_cell_type_con_r
|| cell_entry
->config
== k_cell_type_con_u
1533 || cell_entry
->config
== k_cell_type_con_l
|| cell_entry
->config
== k_cell_type_con_d
)
1535 sw_set_achievement( "CAN_DO_THAT" );
1537 fish
->state
= k_fish_state_soon_alive
;
1541 fish
->flow_reversed
= 1;
1543 switch( cell_entry
->config
)
1545 case k_cell_type_con_r
: fish
->dir
[0] = 1; break;
1546 case k_cell_type_con_l
: fish
->dir
[0] = -1; break;
1547 case k_cell_type_con_u
: fish
->dir
[1] = 1; break;
1548 case k_cell_type_con_d
: fish
->dir
[1] = -1; break;
1554 else { vg_error( "fish behaviour unimplemented for behaviour type (%d)\n" ); }
1556 if( fish
->state
>= k_fish_state_alive
)
1560 // Second pass (triggers)
1561 for( int i
= 0; i
< world
.num_fishes
; i
++ )
1563 struct fish
*fish
= &world
.fishes
[i
];
1565 if( fish
->state
== k_fish_state_alive
)
1567 v2i_add( fish
->pos
, fish
->dir
, fish
->pos
);
1568 struct cell
*cell_current
= pcell( fish
->pos
);
1570 if( cell_current
->state
& FLAG_IS_TRIGGER
)
1572 int trigger_id
= cell_current
->links
[0]?0:1;
1573 int connection_id
= cell_current
->links
[trigger_id
];
1574 int target_px
= connection_id
% world
.w
;
1575 int target_py
= (connection_id
- target_px
)/world
.w
;
1577 vg_line2( (v2f
){ fish
->pos
[0], fish
->pos
[1] }, (v2f
){ target_px
, target_py
}, 0xffffffff, 0xffffffff );
1579 struct cell
*target_peice
= &world
.data
[ cell_current
->links
[trigger_id
] ];
1581 cell_current
->state
|= FLAG_TRIGGERED
;
1584 target_peice
->state
|= FLAG_FLIP_FLOP
;
1586 target_peice
->state
&= ~FLAG_FLIP_FLOP
;
1591 // Third pass (collisions)
1592 struct fish
*fi
, *fj
;
1594 for( int i
= 0; i
< world
.num_fishes
; i
++ )
1596 fi
= &world
.fishes
[i
];
1598 if( fi
->state
== k_fish_state_alive
)
1600 int continue_again
= 0;
1602 for( int j
= i
+1; j
< world
.num_fishes
; j
++ )
1604 fj
= &world
.fishes
[j
];
1606 if( (fj
->state
== k_fish_state_alive
) )
1611 v2i_sub( fi
->pos
, fi
->dir
, fi_prev
);
1612 v2i_sub( fj
->pos
, fj
->dir
, fj_prev
);
1615 collide_next_frame
= (
1616 (fi
->pos
[0] == fj
->pos
[0]) &&
1617 (fi
->pos
[1] == fj
->pos
[1]))? 1: 0,
1618 collide_this_frame
= (
1619 (fi_prev
[0] == fj
->pos
[0]) &&
1620 (fi_prev
[1] == fj
->pos
[1]) &&
1621 (fj_prev
[0] == fi
->pos
[0]) &&
1622 (fj_prev
[1] == fi
->pos
[1])
1625 if( collide_next_frame
|| collide_this_frame
)
1627 sw_set_achievement( "BANG" );
1629 // Shatter death (+0.5s)
1630 float death_time
= collide_this_frame
? 0.0f
: 0.5f
;
1632 fi
->state
= k_fish_state_soon_dead
;
1633 fj
->state
= k_fish_state_soon_dead
;
1634 fi
->death_time
= death_time
;
1635 fj
->death_time
= death_time
;
1642 if( continue_again
)
1648 for( int i
= 0; i
< arrlen( world
.io
); i
++ )
1650 struct cell_terminal
*term
= &world
.io
[ i
];
1651 int posx
= term
->id
% world
.w
;
1652 int posy
= (term
->id
- posx
)/world
.w
;
1653 int is_input
= world
.data
[ term
->id
].state
& FLAG_INPUT
;
1657 if( world
.sim_frame
< term
->runs
[ world
.sim_run
].condition_count
)
1659 struct fish
*fish
= &world
.fishes
[ world
.num_fishes
];
1660 fish
->pos
[0] = posx
;
1661 fish
->pos
[1] = posy
;
1662 fish
->state
= k_fish_state_alive
;
1663 fish
->payload
= term
->runs
[ world
.sim_run
].conditions
[ world
.sim_frame
];
1665 struct cell
*cell_ptr
= pcell( fish
->pos
);
1667 if( cell_ptr
->config
!= k_cell_type_stub
)
1669 struct cell_description
*desc
= &cell_descriptions
[ cell_ptr
->config
];
1671 v2i_copy( desc
->start
, fish
->dir
);
1672 fish
->flow_reversed
= 1;
1674 world
.num_fishes
++;
1681 if( alive_count
== 0 )
1683 world
.completed
= 1;
1685 for( int i
= 0; i
< arrlen( world
.io
); i
++ )
1687 struct cell_terminal
*term
= &world
.io
[ i
];
1688 int is_input
= world
.data
[ term
->id
].state
& FLAG_INPUT
;
1692 struct terminal_run
*run
= &term
->runs
[ world
.sim_run
];
1694 if( run
->recv_count
== run
->condition_count
)
1696 for( int j
= 0; j
< run
->condition_count
; j
++ )
1698 if( run
->recieved
[j
] != run
->conditions
[j
] )
1700 world
.completed
= 0;
1707 world
.completed
= 0;
1713 if( world
.completed
)
1715 if( world
.sim_run
< world
.max_runs
-1 )
1717 vg_success( "Run passed, starting next\n" );
1719 world
.sim_frame
= 0;
1720 world
.sim_start
= vg_time
;
1721 world
.num_fishes
= 0;
1723 for( int i
= 0; i
< world
.w
*world
.h
; i
++ )
1724 world
.data
[ i
].state
&= ~FLAG_FLIP_FLOP
;
1730 vg_success( "Level passed!\n" );
1733 for( int i
= 0; i
< world
.w
*world
.h
; i
++ )
1734 if( world
.data
[ i
].state
& FLAG_CANAL
)
1737 world
.score
= score
;
1738 world
.time
= world
.sim_frame
;
1743 if( world
.sim_run
> 0 )
1744 sw_set_achievement( "GOOD_ENOUGH" );
1746 vg_error( "Level failed :(\n" );
1749 // Copy into career data
1750 if( world
.ptr_career_level
)
1752 world
.ptr_career_level
->score
= world
.score
;
1753 world
.ptr_career_level
->time
= world
.time
;
1754 world
.ptr_career_level
->completed
= world
.completed
;
1757 simulation_stop(); // TODO: Async?
1764 float scaled_time
= 0.0f
;
1765 scaled_time
= (vg_time
-world
.sim_start
)*world
.sim_speed
;
1766 world
.frame_lerp
= scaled_time
- (float)world
.sim_frame
;
1769 for( int i
= 0; i
< world
.num_fishes
; i
++ )
1771 struct fish
*fish
= &world
.fishes
[i
];
1773 if( fish
->state
== k_fish_state_dead
)
1776 if( fish
->state
== k_fish_state_soon_dead
&& (world
.frame_lerp
> fish
->death_time
) )
1777 continue; // Todo: particle thing?
1779 struct cell
*cell
= pcell(fish
->pos
);
1780 struct cell_description
*desc
= &cell_descriptions
[ cell
->config
];
1784 float t
= world
.frame_lerp
;
1785 if( fish
->flow_reversed
&& !desc
->is_linear
)
1788 v2_copy( fish
->physics_co
, fish
->physics_v
);
1790 switch( cell
->config
)
1792 case k_cell_type_merge
:
1793 if( fish
->dir
[0] == 1 )
1798 case k_cell_type_con_r
: curve
= curve_1
; break;
1799 case k_cell_type_con_l
: curve
= curve_4
; break;
1800 case k_cell_type_con_u
: curve
= curve_2
; break;
1801 case k_cell_type_con_d
: curve
= curve_8
; break;
1802 case 3: curve
= curve_3
; break;
1803 case 6: curve
= curve_6
; break;
1804 case 9: curve
= curve_9
; break;
1805 case 12: curve
= curve_12
; break;
1807 if( t
> curve_7_linear_section
)
1809 t
-= curve_7_linear_section
;
1810 t
*= (1.0f
/(1.0f
-curve_7_linear_section
));
1812 curve
= cell
->state
& FLAG_FLIP_FLOP
? curve_7
: curve_7_1
;
1816 default: curve
= NULL
; break;
1822 float t3
= t
* t
* t
;
1824 float cA
= 3.0f
*t2
- 3.0f
*t3
;
1825 float cB
= 3.0f
*t3
- 6.0f
*t2
+ 3.0f
*t
;
1826 float cC
= 3.0f
*t2
- t3
- 3.0f
*t
+ 1.0f
;
1828 fish
->physics_co
[0] = t3
*curve
[3][0] + cA
*curve
[2][0] + cB
*curve
[1][0] + cC
*curve
[0][0];
1829 fish
->physics_co
[1] = t3
*curve
[3][1] + cA
*curve
[2][1] + cB
*curve
[1][1] + cC
*curve
[0][1];
1830 fish
->physics_co
[0] += (float)fish
->pos
[0];
1831 fish
->physics_co
[1] += (float)fish
->pos
[1];
1836 origin
[0] = (float)fish
->pos
[0] + (float)fish
->dir
[0]*-0.5f
+ 0.5f
;
1837 origin
[1] = (float)fish
->pos
[1] + (float)fish
->dir
[1]*-0.5f
+ 0.5f
;
1839 fish
->physics_co
[0] = origin
[0] + (float)fish
->dir
[0]*t
;
1840 fish
->physics_co
[1] = origin
[1] + (float)fish
->dir
[1]*t
;
1846 static void render_tiles( v2i start
, v2i end
, v4f
const regular_colour
, v4f
const selected_colour
)
1848 v2i full_start
= { 0,0 };
1849 v2i full_end
= { world
.w
, world
.h
};
1851 if( !start
|| !end
)
1857 glUniform4fv( SHADER_UNIFORM( shader_tile_main
, "uColour" ), 1, regular_colour
);
1859 for( int y
= start
[1]; y
< end
[1]; y
++ )
1861 for( int x
= start
[0]; x
< end
[0]; x
++ )
1863 struct cell
*cell
= pcell((v2i
){x
,y
});
1864 int selected
= world
.selected
== y
*world
.w
+ x
;
1866 int tile_offsets
[][2] =
1868 {2, 0}, {0, 3}, {0, 2}, {2, 2},
1869 {1, 0}, {2, 3}, {3, 2}, {1, 3},
1870 {3, 1}, {0, 1}, {1, 2}, {2, 1},
1871 {1, 1}, {3, 3}, {2, 1}, {2, 1}
1874 int uv
[2] = { 3, 0 };
1876 if( cell
->state
& (FLAG_CANAL
|FLAG_INPUT
|FLAG_OUTPUT
) )
1878 uv
[0] = tile_offsets
[ cell
->config
][0];
1879 uv
[1] = tile_offsets
[ cell
->config
][1];
1882 glUniform4f( SHADER_UNIFORM( shader_tile_main
, "uOffset" ), (float)x
, (float)y
, uv
[0], uv
[1] );
1885 glUniform4fv( SHADER_UNIFORM( shader_tile_main
, "uColour" ), 1, selected_colour
);
1887 glUniform4fv( SHADER_UNIFORM( shader_tile_main
, "uColour" ), 1, regular_colour
);
1895 static void draw_numbers( v3f coord
, int number
)
1898 v3_copy( coord
, pos
);
1899 int digits
[8]; int i
= 0;
1901 while( number
> 0 && i
< 8 )
1903 digits
[i
++] = number
% 10;
1904 number
= number
/ 10;
1907 for( int j
= 0; j
< i
; j
++ )
1909 glUniform3fv( SHADER_UNIFORM( shader_tile_colour
, "uOffset" ), 1, pos
);
1910 draw_mesh( MESH_NUMBERS_OFFSETS
[digits
[i
-j
-1]][0], MESH_NUMBERS_OFFSETS
[digits
[i
-j
-1]][1] );
1911 pos
[0] += pos
[2] * 0.75f
;
1915 void vg_render(void)
1917 glViewport( 0,0, vg_window_x
, vg_window_y
);
1919 glDisable( GL_DEPTH_TEST
);
1920 glClearColor( 0.369768f
, 0.3654f
, 0.42f
, 1.0f
);
1921 glClear( GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
1923 v4f
const colour_default
= {1.0f
, 1.0f
, 1.0f
, 1.0f
};
1924 v4f
const colour_selected
= {0.90f
, 0.92f
, 1.0f
, 1.0f
};
1926 // TILE SET RENDERING
1927 // todo: just slam everything into a mesh...
1928 // when user modifies a tile the neighbours can be easily uploaded to gpu mem
1930 // Currently we're uploading a fair amount of data every frame anyway.
1931 // NOTE: this is for final optimisations ONLY!
1932 // ======================================================================
1934 use_mesh( &world
.tile
);
1940 SHADER_USE( shader_background
);
1941 glUniformMatrix3fv( SHADER_UNIFORM( shader_background
, "uPv" ), 1, GL_FALSE
, (float *)vg_pv
);
1943 glActiveTexture( GL_TEXTURE0
);
1944 glBindTexture( GL_TEXTURE_2D
, world
.background_data
);
1945 glUniform1i( SHADER_UNIFORM( shader_background
, "uTexMain" ), 0 );
1947 glUniform3f( SHADER_UNIFORM( shader_background
, "uOffset" ), -16, -16, 64 );
1948 glUniform1f( SHADER_UNIFORM( shader_background
, "uVariance" ), 0.02f
);
1950 glActiveTexture( GL_TEXTURE1
);
1951 glBindTexture( GL_TEXTURE_2D
, world
.random_samples
);
1952 glUniform1i( SHADER_UNIFORM( shader_background
, "uSamplerNoise" ), 1 );
1959 SHADER_USE( shader_tile_main
);
1962 m2x2_identity( subtransform
);
1963 glUniformMatrix2fv( SHADER_UNIFORM( shader_tile_main
, "uSubTransform" ), 1, GL_FALSE
, (float *)subtransform
);
1964 glUniformMatrix3fv( SHADER_UNIFORM( shader_tile_main
, "uPv" ), 1, GL_FALSE
, (float *)vg_pv
);
1965 glUniform1f( SHADER_UNIFORM( shader_tile_main
, "uGhost" ), 0.0f
);
1966 glUniform1f( SHADER_UNIFORM( shader_tile_main
, "uForeground" ), 0.0f
);
1969 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
1970 glBlendEquation(GL_FUNC_ADD
);
1973 vg_tex2d_bind( &tex_tile_data
, 0 );
1974 glUniform1i( SHADER_UNIFORM( shader_tile_main
, "uTexGlyphs" ), 0 );
1976 vg_tex2d_bind( &tex_wood
, 1 );
1977 glUniform1i( SHADER_UNIFORM( shader_tile_main
, "uTexWood" ), 1 );
1979 render_tiles( NULL
, NULL
, colour_default
, colour_default
);
1983 SHADER_USE( shader_ball
);
1984 glUniformMatrix3fv( SHADER_UNIFORM( shader_ball
, "uPv" ), 1, GL_FALSE
, (float *)vg_pv
);
1986 vg_tex2d_bind( &tex_ball_noise
, 0 );
1987 glUniform1i( SHADER_UNIFORM( shader_ball
, "uTexMain" ), 0 );
1990 if( world
.simulating
)
1992 for( int i
= 0; i
< world
.num_fishes
; i
++ )
1994 struct fish
*fish
= &world
.fishes
[i
];
1996 if( fish
->state
== k_fish_state_dead
|| fish
->state
== k_fish_state_bg
)
1999 if( fish
->state
== k_fish_state_soon_dead
&& (world
.frame_lerp
> fish
->death_time
) )
2002 v4f dot_colour
= { 0.0f
, 0.0f
, 0.0f
, 1.0f
};
2003 colour_code_v3( fish
->payload
, dot_colour
);
2005 glUniform3fv( SHADER_UNIFORM( shader_ball
, "uColour" ), 1, dot_colour
);
2006 glUniform2fv( SHADER_UNIFORM( shader_ball
, "uOffset" ), 1, fish
->physics_co
);
2007 glUniform2f( SHADER_UNIFORM( shader_ball
, "uTexOffset" ), (float)i
* 1.2334, (float)i
* -0.3579f
);
2012 SHADER_USE( shader_tile_main
);
2015 vg_tex2d_bind( &tex_tile_data
, 0 );
2016 glUniform1i( SHADER_UNIFORM( shader_tile_main
, "uTexGlyphs" ), 0 );
2018 vg_tex2d_bind( &tex_wood
, 1 );
2019 glUniform1i( SHADER_UNIFORM( shader_tile_main
, "uTexWood" ), 1 );
2021 glUniform1f( SHADER_UNIFORM( shader_tile_main
, "uForeground" ), 1.0f
);
2022 render_tiles( NULL
, NULL
, colour_default
, colour_selected
);
2025 for( int y
= 2; y
< world
.h
-2; y
++ )
2027 for( int x
= 2; x
< world
.w
-2; x
++ )
2029 struct cell
*cell
= pcell((v2i
){x
,y
});
2031 if( cell
->state
& FLAG_CANAL
)
2033 if( cell
->config
== k_cell_type_split
)
2035 float rotation
= cell
->state
& FLAG_FLIP_FLOP
? vg_rad( -45.0f
): vg_rad( 45.0f
);
2037 if( cell
->state
& FLAG_FLIP_ROTATING
)
2039 if( (world
.frame_lerp
> curve_7_linear_section
) )
2041 float const rotation_speed
= 0.4f
;
2042 if( (world
.frame_lerp
< 1.0f
-rotation_speed
) )
2044 float t
= world
.frame_lerp
- curve_7_linear_section
;
2045 t
*= -2.0f
* (1.0f
/(1.0f
-(curve_7_linear_section
+rotation_speed
)));
2055 m2x2_create_rotation( subtransform
, rotation
);
2057 glUniformMatrix2fv( SHADER_UNIFORM( shader_tile_main
, "uSubTransform" ), 1, GL_FALSE
, (float *)subtransform
);
2058 glUniform4f( SHADER_UNIFORM( shader_tile_main
, "uOffset" ), (float)x
, (float)y
+ 0.125f
, cell
->state
& FLAG_TARGETED
? 3.0f
: 0.0f
, 0.0f
);
2066 if( world
.selected
!= -1 && !(world
.data
[ world
.selected
].state
& FLAG_CANAL
) && !id_drag_from
)
2068 v2i new_begin
= { world
.tile_x
- 2, world
.tile_y
- 2 };
2069 v2i new_end
= { world
.tile_x
+ 2, world
.tile_y
+ 2 };
2071 world
.data
[ world
.selected
].state
^= FLAG_CANAL
;
2072 map_reclassify( new_begin
, new_end
, 0 );
2074 m2x2_identity( subtransform
);
2075 glUniform1f( SHADER_UNIFORM( shader_tile_main
, "uGhost" ), 1.0f
);
2076 glUniformMatrix2fv( SHADER_UNIFORM( shader_tile_main
, "uSubTransform" ), 1, GL_FALSE
, (float *)subtransform
);
2077 glUniform2fv( SHADER_UNIFORM( shader_tile_main
, "uMousePos" ), 1, world
.tile_pos
);
2079 render_tiles( new_begin
, new_end
, colour_default
, colour_default
);
2081 world
.data
[ world
.selected
].state
^= FLAG_CANAL
;
2082 map_reclassify( new_begin
, new_end
, 0 );
2085 //glDisable(GL_BLEND);
2087 // Draw connecting wires
2088 glDisable(GL_BLEND
);
2090 SHADER_USE( shader_wire
);
2091 glBindVertexArray( world
.wire
.vao
);
2093 glUniformMatrix3fv( SHADER_UNIFORM( shader_wire
, "uPv" ), 1, GL_FALSE
, (float *)vg_pv
);
2094 glUniform4f( SHADER_UNIFORM( shader_wire
, "uColour" ), 0.2f
, 0.2f
, 0.2f
, 1.0f
);
2098 glUniform1f( SHADER_UNIFORM( shader_wire
, "uCurve" ), 0.4f
);
2099 glUniform3f( SHADER_UNIFORM( shader_wire
, "uStart" ), drag_from_co
[0], drag_from_co
[1], 0.06f
);
2100 glUniform3f( SHADER_UNIFORM( shader_wire
, "uEnd" ), drag_to_co
[0], drag_to_co
[1], 0.06f
);
2101 glDrawElements( GL_TRIANGLES
, world
.wire
.em
, GL_UNSIGNED_SHORT
, (void*)(0) );
2104 float rp_x1
= world
.frame_lerp
*9.0f
;
2105 float rp_x2
= 1.0f
-rp_x1
*expf(1.0f
-rp_x1
)* 0.36f
;
2107 for( int y
= 2; y
< world
.h
-2; y
++ )
2109 for( int x
= 2; x
< world
.w
-2; x
++ )
2111 struct cell
*cell
= pcell((v2i
){x
,y
});
2113 if( cell
->state
& FLAG_CANAL
)
2115 if( cell
->state
& FLAG_IS_TRIGGER
)
2117 int trigger_id
= cell
->links
[0]?0:1;
2119 int x2
= cell
->links
[trigger_id
] % world
.w
;
2120 int y2
= (cell
->links
[trigger_id
] - x2
) / world
.w
;
2125 startpoint
[0] = (float)x2
+ (trigger_id
? 0.75f
: 0.25f
);
2126 startpoint
[1] = (float)y2
+ 0.25f
;
2128 endpoint
[0] = x
+0.5f
;
2129 endpoint
[1] = y
+0.5f
;
2131 glUniform1f( SHADER_UNIFORM( shader_wire
, "uCurve" ), cell
->state
& FLAG_TRIGGERED
? rp_x2
* 0.4f
: 0.4f
);
2132 glUniform3f( SHADER_UNIFORM( shader_wire
, "uStart" ), startpoint
[0], startpoint
[1], 0.04f
);
2133 glUniform3f( SHADER_UNIFORM( shader_wire
, "uEnd" ), endpoint
[0], endpoint
[1], 0.04f
);
2134 glDrawElements( GL_TRIANGLES
, world
.wire
.em
, GL_UNSIGNED_SHORT
, (void*)(0) );
2140 SHADER_USE( shader_tile_colour
);
2141 glUniformMatrix3fv( SHADER_UNIFORM( shader_tile_colour
, "uPv" ), 1, GL_FALSE
, (float *)vg_pv
);
2142 use_mesh( &world
.circle
);
2144 int const filled_start
= 0;
2145 int const filled_count
= 32;
2146 int const empty_start
= 32;
2147 int const empty_count
= 32*2;
2150 for( int i
= 0; i
< arrlen( world
.io
); i
++ )
2152 struct cell_terminal
*term
= &world
.io
[ i
];
2153 int posx
= term
->id
% world
.w
;
2154 int posy
= (term
->id
- posx
)/world
.w
;
2155 int is_input
= world
.data
[ term
->id
].state
& FLAG_INPUT
;
2157 v4f dot_colour
= { 0.0f
, 0.0f
, 0.0f
, 1.0f
};
2159 for( int k
= 0; k
< term
->run_count
; k
++ )
2161 for( int j
= 0; j
< term
->runs
[k
].condition_count
; j
++ )
2163 float y_offset
= is_input
? 1.2f
: -0.2f
;
2164 y_offset
+= (is_input
? 0.2f
: -0.2f
) * (float)k
;
2166 glUniform3f( SHADER_UNIFORM( shader_tile_colour
, "uOffset" ), (float)posx
+ 0.2f
+ 0.2f
* (float)j
, (float)posy
+ y_offset
, 0.1f
);
2170 colour_code_v3( term
->runs
[k
].conditions
[j
], dot_colour
);
2171 glUniform4fv( SHADER_UNIFORM( shader_tile_colour
, "uColour" ), 1, dot_colour
);
2173 // Draw filled if tick not passed, draw empty if empty
2174 if( (world
.sim_frame
> j
&& world
.sim_run
>= k
) || world
.sim_run
> k
)
2175 draw_mesh( empty_start
, empty_count
);
2177 draw_mesh( filled_start
, filled_count
);
2181 if( term
->runs
[k
].recv_count
> j
)
2183 colour_code_v3( term
->runs
[k
].recieved
[j
], dot_colour
);
2184 v3_muls( dot_colour
, 0.8f
, dot_colour
);
2185 glUniform4fv( SHADER_UNIFORM( shader_tile_colour
, "uColour" ), 1, dot_colour
);
2187 draw_mesh( filled_start
, filled_count
);
2190 colour_code_v3( term
->runs
[k
].conditions
[j
], dot_colour
);
2191 glUniform4fv( SHADER_UNIFORM( shader_tile_colour
, "uColour" ), 1, dot_colour
);
2193 draw_mesh( empty_start
, empty_count
);
2199 if( world
.simulating
)
2201 glUniform4f( SHADER_UNIFORM( shader_tile_colour
, "uColour" ), 0.0f
, 0.0f
, 0.0f
, 1.0f
);
2202 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
);
2203 draw_mesh( filled_start
, filled_count
);
2207 float const score_bright
= 1.25f
;
2208 glUniform4f( SHADER_UNIFORM( shader_tile_colour
, "uColour" ),
2209 0.4f
*score_bright
, 0.39f
*score_bright
, 0.45f
*score_bright
, 1.0f
);
2211 use_mesh( &world
.numbers
);
2212 draw_numbers( (v3f
){ 2.0f
, (float)world
.h
-1.875f
, 0.3333f
}, world
.score
);
2214 // Level selection UI
2215 use_mesh( &world
.circle
);
2216 float ratio
= ((float)vg_window_x
/(float)vg_window_y
);
2218 m3x3f ui_view
= M3X3_IDENTITY
;
2219 m3x3_scale( ui_view
, (v3f
){ 1.0f
, ratio
, 1.0f
} );
2220 glUniformMatrix3fv( SHADER_UNIFORM( shader_tile_colour
, "uPv" ), 1, GL_FALSE
, (float *)ui_view
);
2222 // Calculate mouse in UIsp
2223 v3f mouse_ui_space
= { ((float)vg_mouse
[0] / (float)(vg_window_x
)) * 2.0f
- 1.0f
,
2224 (((float)vg_mouse
[1] / (float)(vg_window_y
)) * 2.0f
- 1.0f
)*(-1.0f
/ratio
), 0.0125f
};
2226 // Get selected level
2227 const float selection_scale
= 0.05f
;
2228 int const level_count
= vg_list_size( level_pack_1
);
2229 int level_select
= -1;
2231 if( mouse_ui_space
[0] <= -0.8f
)
2233 float levels_range
= (float)level_count
*selection_scale
*0.6f
;
2234 float level_offset
= ((-mouse_ui_space
[1] + levels_range
) / levels_range
) * 0.5f
* (float)level_count
;
2235 level_select
= ceilf( level_offset
);
2238 if( level_select
>= 0 && level_select
< vg_list_size( level_pack_1
) )
2240 glUniform4f( SHADER_UNIFORM( shader_tile_colour
, "uColour" ), 0.369768f
, 0.3654f
, 0.42f
, 1.0f
);
2242 use_mesh( &world
.tile
);
2243 glUniform3f( SHADER_UNIFORM( shader_tile_colour
, "uOffset" ),
2245 ((float)level_count
- (float)level_select
* 2.0f
) * selection_scale
* 0.6f
,
2250 use_mesh( &world
.circle
);
2252 if( vg_get_button_down( "primary" ) )
2254 console_changelevel( 1, level_pack_1
+ level_select
);
2258 else mouse_ui_space
[1] = INFINITY
;
2260 glUniform4f( SHADER_UNIFORM( shader_tile_colour
, "uColour" ), 0.4f
, 0.39f
, 0.45f
, 1.0f
);
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
,
2270 selection_scale
* 0.5f
2273 float scale
= vg_clampf( 1.0f
- fabsf(level_ui_space
[1] - mouse_ui_space
[1]) * 2.0f
, 0.9f
, 1.0f
);
2274 level_ui_space
[2] *= scale
;
2276 glUniform3fv( SHADER_UNIFORM( shader_tile_colour
, "uOffset" ), 1, level_ui_space
);
2278 if( clevel
->completed
)
2279 draw_mesh( filled_start
, filled_count
);
2281 draw_mesh( empty_start
, empty_count
);
2285 glUniform4f( SHADER_UNIFORM( shader_tile_colour
, "uColour" ), 0.4f
*1.25f
, 0.39f
*1.25f
, 0.45f
*1.25f
, 1.0f
);
2287 use_mesh( &world
.numbers
);
2288 for( int i
= 0; i
< level_count
; i
++ )
2290 struct career_level
*clevel
= &career
.levels
[i
];
2292 v3f level_ui_space
= {
2294 ((float)level_count
- (float)i
* 2.0f
) * selection_scale
* 0.6f
+ selection_scale
* 0.5f
,
2298 if( clevel
->completed
)
2299 draw_numbers( level_ui_space
, clevel
->score
);
2301 level_ui_space
[0] = -0.975f
;
2302 level_ui_space
[1] -= 0.01f
;
2303 draw_numbers( level_ui_space
, i
);
2306 //use_mesh( &world.numbers );
2307 //draw_numbers( (v3f){ 0.0f, -0.5f, 0.1f }, 128765 );