1 // Copyright (C) 2021 Harry Godden (hgn) - All Rights Reserved
5 #include "fishladder_resources.h"
7 const char *level_pack_1
[] = {
27 levels
[ vg_list_size( level_pack_1
) ];
29 career
= { .version
= 1 };
32 static void career_serialize(void)
34 vg_asset_write( "sav/game.sav", &career
, sizeof( struct career_state
) );
37 static void career_load(void)
40 struct career_state
*cr
= vg_asset_read_s( "sav/game.sav", &sz
);
42 memset( (void*)career
.levels
, 0, vg_list_size(level_pack_1
) * sizeof(struct career_level
) );
46 if( sz
> sizeof( struct career_state
) )
47 vg_warn( "This save file is too big! Some levels will be lost\n" );
49 if( sz
<= offsetof( struct career_state
, levels
) )
51 vg_error( "This save file is too small to have a header\n" );
56 u32
const size_header
= offsetof(struct career_state
, levels
);
57 u32
const size_levels
= sizeof(struct career_state
)-size_header
;
58 u32
const size_levels_input
= sz
- size_header
;
60 memcpy( (void*)career
.levels
, (void*)cr
->levels
, size_levels
);
62 if( sz
< sizeof( struct career_state
) )
64 memset( ((void*)career
.levels
) + size_levels_input
, 0, size_levels
-size_levels_input
);
68 vg_success( "Loaded save file... Info:\n" );
70 for( int i
= 0; i
< vg_list_size( career
.levels
); i
++ )
72 struct career_level
*lvl
= &career
.levels
[i
];
73 vg_info( "Score: %u, Time: %u, Completed: %u\n", lvl
->score
, lvl
->time
, lvl
->completed
);
78 vg_info( "No save file... Using blank one\n" );
86 #define FLAG_INPUT 0x1
87 #define FLAG_OUTPUT 0x2
88 #define FLAG_CANAL 0x4
90 #define FLAG_FLIP_FLOP 0x100
91 #define FLAG_FLIP_ROTATING 0x200
94 0000 0 | 0001 1 | 0010 2 | 0011 3
98 0100 4 | 0101 5 | 0110 6 | 0111 7
102 1000 8 | 1001 9 | 1010 10 | 1011 11
106 1100 12 | 1101 13 | 1110 14 | 1111 15
114 k_cell_type_ramp_right
= 3,
115 k_cell_type_ramp_left
= 6,
116 k_cell_type_split
= 7,
117 k_cell_type_merge
= 13
121 { { 0.9f
, 0.6f
, 0.20f
},
122 { 0.2f
, 0.9f
, 0.14f
},
123 { 0.4f
, 0.8f
, 1.00f
} };
125 static void colour_code_v3( char cc
, v3f target
)
127 if( cc
>= 'a' && cc
<= 'z' )
131 if( id
< vg_list_size( colour_sets
) )
133 v3_copy( colour_sets
[ id
], target
);
138 v3_copy( (v3f
){0.0f
,0.0f
,0.0f
}, target
);
147 static void init_mesh( struct mesh
*m
, float const *tris
, u32 length
)
149 m
->elements
= length
/3;
150 glGenVertexArrays( 1, &m
->vao
);
151 glGenBuffers( 1, &m
->vbo
);
153 glBindVertexArray( m
->vao
);
154 glBindBuffer( GL_ARRAY_BUFFER
, m
->vbo
);
155 glBufferData( GL_ARRAY_BUFFER
, length
*sizeof(float), tris
, GL_STATIC_DRAW
);
157 glVertexAttribPointer( 0, 2, GL_FLOAT
, GL_FALSE
, 2 * sizeof(float), (void*)0 );
158 glEnableVertexAttribArray( 0 );
163 static void free_mesh( struct mesh
*m
)
165 glDeleteVertexArrays( 1, &m
->vao
);
166 glDeleteBuffers( 1, &m
->vbo
);
169 static void draw_mesh( int const start
, int const count
)
171 glDrawArrays( GL_TRIANGLES
, start
*3, count
*3 );
174 static void use_mesh( struct mesh
*m
)
176 glBindVertexArray( m
->vao
);
198 // TODO: Split into input/output structures
208 struct mesh tile
, circle
, numbers
;
210 GLuint background_data
;
211 GLuint random_samples
;
213 int selected
, tile_x
, tile_y
;
231 struct career_level
*ptr_career_level
;
239 static void map_free(void)
241 for( int i
= 0; i
< arrlen( world
.io
); i
++ )
242 arrfree( world
.io
[ i
].conditions
);
244 arrfree( world
.data
);
256 static void map_reclassify( v2i start
, v2i end
, int update_texbuffer
);
257 static int map_load( const char *str
, const char *name
)
266 if( str
[world
.w
] == ';' )
268 else if( !str
[world
.w
] )
270 vg_error( "Unexpected EOF when parsing level\n" );
275 struct cell
*row
= arraddnptr( world
.data
, world
.w
);
277 int reg_start
= 0, reg_end
= 0;
293 if( reg_start
< reg_end
)
295 if( *c
>= 'a' && *c
<= 'z' )
297 arrpush( world
.io
[ reg_start
].conditions
, *c
);
301 if( *c
== ',' || *c
== '\n' )
310 vg_error( "Unkown attribute '%c' (row: %u)\n", *c
, world
.h
);
317 vg_error( "Too many values to assign (row: %u)\n", world
.h
);
325 if( reg_start
!= reg_end
)
327 vg_error( "Not enough values assigned (row: %u, %u of %u)\n", world
.h
, reg_start
, reg_end
);
333 vg_error( "Not enough cells to match previous row definition (row: %u, %u<%u)\n", world
.h
, cx
, world
.w
);
337 row
= arraddnptr( world
.data
, world
.w
);
340 reg_end
= reg_start
= arrlen( world
.io
);
346 vg_error( "Too many cells to match previous row definition (row: %u, %u>%u)\n", world
.h
, cx
, world
.w
);
350 // Tile initialization
352 struct cell
*cell
= &row
[ cx
];
354 if( *c
== '+' || *c
== '-' )
356 struct cell_terminal term
= { .id
= cx
+ world
.h
*world
.w
};
357 arrpush( world
.io
, term
);
358 cell
->state
= *c
== '+'? FLAG_INPUT
: FLAG_OUTPUT
;
361 else if( *c
== '#' ) cell
->state
= FLAG_WALL
;
362 else if( *c
== '*' ) { cell
->state
= FLAG_CANAL
; world
.score
++; }
363 else cell
->state
= 0x00;
371 // Update data texture to fill out the background
373 u8 info_buffer
[64*64*4];
374 for( int i
= 0; i
< 64*64; i
++ )
376 u8
*px
= &info_buffer
[i
*4];
383 glBindTexture( GL_TEXTURE_2D
, world
.background_data
);
384 glTexSubImage2D( GL_TEXTURE_2D
, 0, 0, 0, 64, 64, GL_RGBA
, GL_UNSIGNED_BYTE
, info_buffer
);
387 map_reclassify( NULL
, NULL
, 1 );
388 vg_success( "Map '%s' loaded! (%u:%u)\n", name
, world
.w
, world
.h
);
390 strncpy( world
.map_name
, name
, vg_list_size( world
.map_name
)-1 );
394 static struct cell
*pcell( v2i pos
)
396 return &world
.data
[ pos
[1]*world
.w
+ pos
[0] ];
399 static void map_serialize( FILE *stream
)
401 for( int y
= 0; y
< world
.h
; y
++ )
403 for( int x
= 0; x
< world
.w
; x
++ )
405 struct cell
*cell
= pcell( (v2i
){ x
, y
} );
407 if( cell
->state
& FLAG_WALL
) fputc( '#', stream
);
408 else if( cell
->state
& FLAG_INPUT
) fputc( '+', stream
);
409 else if( cell
->state
& FLAG_OUTPUT
) fputc( '-', stream
);
410 else if( cell
->state
& FLAG_CANAL
) fputc( '*', stream
);
411 else fputc( ' ', stream
);
414 fputc( ';', stream
);
416 int terminal_write_count
= 0;
418 for( int x
= 0; x
< world
.w
; x
++ )
420 for( int i
= 0; i
< arrlen( world
.io
); i
++ )
422 struct cell_terminal
*term
= &world
.io
[i
];
423 if( term
->id
== y
*world
.w
+x
)
425 if( terminal_write_count
)
426 fputc( ',', stream
);
427 terminal_write_count
++;
429 for( int j
= 0; j
< arrlen( term
->conditions
); j
++ )
430 fputc( term
->conditions
[j
], stream
);
435 fputc( '\n', stream
);
439 int main( int argc
, char *argv
[] )
441 vg_init( argc
, argv
, "Fish (Marbles Computer) Ladder Simulator 2022 | N,M: change level | SPACE: Test | LeftClick: Toggle tile" );
444 static int console_save_map( int argc
, char const *argv
[] )
446 char map_path
[ 256 ];
448 strcpy( map_path
, "sav/" );
449 strcat( map_path
, world
.map_name
);
450 strcat( map_path
, ".map" );
452 FILE *test_writer
= fopen( map_path
, "wb" );
455 map_serialize( test_writer
);
457 fclose( test_writer
);
462 vg_error( "Unable to open stream for writing\n" );
467 static int console_load_map( int argc
, char const *argv
[] )
469 char map_path
[ 256 ];
474 strcpy( map_path
, "sav/" );
475 strcat( map_path
, argv
[0] );
476 strcat( map_path
, ".map" );
478 char *text_source
= vg_textasset_read( map_path
);
482 strcpy( map_path
, "maps/" );
483 strcat( map_path
, argv
[0] );
484 strcat( map_path
, ".map" );
486 text_source
= vg_textasset_read( map_path
);
491 map_load( text_source
, argv
[0] );
494 // Update career link
495 world
.ptr_career_level
= NULL
;
497 for( int i
= 0; i
< vg_list_size( level_pack_1
); i
++ )
499 if( !strcmp( level_pack_1
[i
], argv
[0] ) )
501 world
.ptr_career_level
= career
.levels
+ i
;
510 vg_error( "Missing maps '%s'\n", argv
[0] );
516 vg_error( "Missing argument <map_path>\n" );
521 static void simulation_stop(void)
523 world
.simulating
= 0;
524 world
.num_fishes
= 0;
527 for( int i
= 0; i
< arrlen( world
.io
); i
++ )
528 world
.io
[i
].recv_count
= 0;
530 sfx_system_fadeout( &audio_system_balls_rolling
, 44100 );
532 vg_info( "Stopping simulation!\n" );
535 static int console_changelevel( int argc
, char const *argv
[] )
539 // Save current level
540 if( console_save_map( 0, NULL
) )
541 if( console_load_map( argc
, argv
) )
549 vg_error( "Missing argument <map_path>\n" );
557 vg_function_push( (struct vg_cmd
){
559 .function
= console_save_map
562 vg_function_push( (struct vg_cmd
){
564 .function
= console_load_map
567 vg_function_push( (struct vg_cmd
){
568 .name
= "changelevel",
569 .function
= console_changelevel
576 0.0f
, 0.0f
, 0.0f
, 1.0f
, 1.0f
, 1.0f
,
577 0.0f
, 0.0f
, 1.0f
, 1.0f
, 1.0f
, 0.0f
,
579 0.0f
, 0.0f
, 0.0f
, 1.0f
, 4.0f
, 1.0f
,
580 0.0f
, 0.0f
, 4.0f
, 1.0f
, 4.0f
, 0.0f
583 init_mesh( &world
.tile
, quad_mesh
, vg_list_size(quad_mesh
) );
588 float circle_mesh
[32*6*3];
589 int res
= vg_list_size( circle_mesh
) / (6*3);
591 for( int i
= 0; i
< res
; i
++ )
593 v2f v0
= { sinf( ((float)i
/(float)res
)*VG_TAUf
), cosf( ((float)i
/(float)res
)*VG_TAUf
) };
594 v2f v1
= { sinf( ((float)(i
+1)/(float)res
)*VG_TAUf
), cosf( ((float)(i
+1)/(float)res
)*VG_TAUf
) };
596 circle_mesh
[ i
*6+0 ] = 0.0f
;
597 circle_mesh
[ i
*6+1 ] = 0.0f
;
599 v2_copy( v0
, circle_mesh
+ 32*6 + i
*12 );
600 v2_muls( v0
, 0.8f
, circle_mesh
+ 32*6 + i
*12+2 );
601 v2_copy( v1
, circle_mesh
+ 32*6 + i
*12+4 );
603 v2_copy( v1
, circle_mesh
+ 32*6 + i
*12+6 );
604 v2_muls( v1
, 0.8f
, circle_mesh
+ 32*6 + i
*12+8 );
605 v2_muls( v0
, 0.8f
, circle_mesh
+ 32*6 + i
*12+10 );
607 v2_copy( v0
, circle_mesh
+ i
*6+4 );
608 v2_copy( v1
, circle_mesh
+ i
*6+2 );
609 v2_copy( v0
, circle_mesh
+i
*6+4 );
610 v2_copy( v1
, circle_mesh
+i
*6+2 );
613 init_mesh( &world
.circle
, circle_mesh
, vg_list_size( circle_mesh
) );
618 init_mesh( &world
.numbers
,
620 vg_list_size( MESH_NUMBERS_BUFFER
)
623 for( int i
= 0; i
< 10; i
++ )
625 vg_info( "offset: %u, length: %u\n", MESH_NUMBERS_OFFSETS
[i
][0], MESH_NUMBERS_OFFSETS
[i
][1] );
629 // Create info data texture
631 glGenTextures( 1, &world
.background_data
);
632 glBindTexture( GL_TEXTURE_2D
, world
.background_data
);
633 glTexImage2D( GL_TEXTURE_2D
, 0, GL_RGBA
, 64, 64, 0, GL_RGBA
, GL_UNSIGNED_BYTE
, NULL
);
637 // Create random smaples texture
639 u8
*data
= malloc(512*512*2);
640 for( int i
= 0; i
< 512*512*2; i
++ )
641 data
[ i
] = rand()/(RAND_MAX
/255);
643 glGenTextures( 1, &world
.random_samples
);
644 glBindTexture( GL_TEXTURE_2D
, world
.random_samples
);
645 glTexImage2D( GL_TEXTURE_2D
, 0, GL_RG
, 512, 512, 0, GL_RG
, GL_UNSIGNED_BYTE
, data
);
652 resource_load_main();
656 console_load_map( 1, level_pack_1
);
661 console_save_map( 0, NULL
);
664 resource_free_main();
666 glDeleteTextures( 1, &world
.background_data
);
667 glDeleteTextures( 1, &world
.random_samples
);
669 free_mesh( &world
.tile
);
670 free_mesh( &world
.circle
);
671 free_mesh( &world
.numbers
);
676 static int cell_interactive( v2i co
)
679 if( co
[0] < 2 || co
[0] >= world
.w
-2 || co
[1] < 2 || co
[1] >= world
.h
-2 )
683 if( world
.data
[ world
.w
*co
[1] + co
[0] ].state
& (FLAG_WALL
|FLAG_INPUT
|FLAG_OUTPUT
) )
686 // List of 3x3 configurations that we do not allow
687 static u32 invalid_src
[][9] =
719 // Statically compile invalid configurations into bitmasks
720 static u32 invalid
[ vg_list_size(invalid_src
) ];
722 for( int i
= 0; i
< vg_list_size(invalid_src
); i
++ )
726 for( int j
= 0; j
< 3; j
++ )
727 for( int k
= 0; k
< 3; k
++ )
728 comped
|= invalid_src
[i
][ j
*3+k
] << ((j
*5)+k
);
733 // Extract 5x5 grid surrounding tile
735 for( int y
= co
[1]-2; y
< co
[1]+3; y
++ )
736 for( int x
= co
[0]-2; x
< co
[0]+3; x
++ )
738 struct cell
*cell
= pcell((v2i
){x
,y
});
740 if( cell
&& (cell
->state
& (FLAG_CANAL
|FLAG_INPUT
|FLAG_OUTPUT
)) )
741 blob
|= 0x1 << ((y
-(co
[1]-2))*5 + x
-(co
[0]-2));
744 // Run filter over center 3x3 grid to check for invalid configurations
745 int kernel
[] = { 0, 1, 2, 5, 6, 7, 10, 11, 12 };
746 for( int i
= 0; i
< vg_list_size(kernel
); i
++ )
748 if( blob
& (0x1 << (6+kernel
[i
])) )
750 u32 window
= blob
>> kernel
[i
];
752 for( int j
= 0; j
< vg_list_size(invalid
); j
++ )
753 if((window
& invalid
[j
]) == invalid
[j
])
761 static void map_reclassify( v2i start
, v2i end
, int update_texbuffer
)
763 v2i full_start
= { 1,1 };
764 v2i full_end
= { world
.w
-1, world
.h
-1 };
773 u8 info_buffer
[64*64*4];
776 int px0
= vg_max( start
[0], full_start
[0] ),
777 px1
= vg_min( end
[0], full_end
[0] ),
778 py0
= vg_max( start
[1], full_start
[1] ),
779 py1
= vg_min( end
[1], full_end
[1] );
781 for( int y
= py0
; y
< py1
; y
++ )
783 for( int x
= px0
; x
< px1
; x
++ )
785 struct cell
*cell
= pcell((v2i
){x
,y
});
787 v2i dirs
[] = {{1,0},{0,1},{-1,0},{0,-1}};
792 if( cell
->state
& (FLAG_CANAL
|FLAG_INPUT
|FLAG_OUTPUT
) )
794 for( int i
= 0; i
< vg_list_size( dirs
); i
++ )
796 struct cell
*neighbour
= pcell((v2i
){x
+dirs
[i
][0], y
+dirs
[i
][1]});
797 if( neighbour
->state
& (FLAG_CANAL
|FLAG_INPUT
|FLAG_OUTPUT
) )
805 if( cell
->state
& FLAG_WALL
)
811 pcell((v2i
){x
,y
})->config
= config
;
813 u8
*info_px
= &info_buffer
[ (pixel_id
++)*4 ];
815 info_px
[1] = cell
->state
& FLAG_WALL
? 0: 255;
821 if( update_texbuffer
)
823 glBindTexture( GL_TEXTURE_2D
, world
.background_data
);
824 glTexSubImage2D( GL_TEXTURE_2D
, 0, px0
+ 16, py0
+ 16, px1
-px0
, py1
-py0
, GL_RGBA
, GL_UNSIGNED_BYTE
, info_buffer
);
828 v2f
const curve_3
[] = {{0.5f
,1.0f
},{0.5f
,0.625f
},{0.625f
,0.5f
},{1.0f
,0.5f
}};
829 v2f
const curve_6
[] = {{0.5f
,1.0f
},{0.5f
,0.625f
},{0.375f
,0.5f
},{0.0f
,0.5f
}};
830 v2f
const curve_9
[] = {{1.0f
,0.5f
},{0.625f
,0.5f
},{0.5f
,0.375f
},{0.5f
,0.0f
}};
831 v2f
const curve_12
[]= {{0.0f
,0.5f
},{0.375f
,0.5f
},{0.5f
,0.375f
},{0.5f
,0.0f
}};
833 v2f
const curve_2
[] = {{0.5f
,1.0f
},{0.5f
,0.8f
},{0.5f
,0.3f
},{0.5f
,0.2f
}};
834 v2f
const curve_8
[] = {{0.5f
,0.8f
},{0.5f
,0.5f
},{0.5f
,0.3f
},{0.5f
,0.0f
}};
836 v2f
const curve_7
[] = {{0.5f
,0.8438f
},{0.875f
,0.8438f
},{0.625f
,0.5f
},{1.0f
,0.5f
}};
837 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
}};
839 float const curve_7_linear_section
= 0.1562f
;
845 float r1
= (float)vg_window_y
/ (float)vg_window_x
,
846 r2
= (float)world
.h
/ (float)world
.w
,
849 size
= ( r2
< r1
? (float)world
.w
* 0.5f
: ((float)world
.h
* 0.5f
) / r1
) + 2.5f
;
850 m3x3_projection( m_projection
, -size
, size
, -size
*r1
, size
*r1
);
853 origin
[0] = floorf( -0.5f
* world
.w
);
854 origin
[1] = floorf( -0.5f
* world
.h
);
857 m3x3_identity( m_view
);
858 m3x3_translate( m_view
, origin
);
859 m3x3_mul( m_projection
, m_view
, vg_pv
);
860 vg_projection_update();
863 v2_copy( vg_mouse_ws
, world
.tile_pos
);
865 world
.tile_x
= floorf( world
.tile_pos
[0] );
866 world
.tile_y
= floorf( world
.tile_pos
[1] );
869 if( !world
.simulating
)
871 if( cell_interactive( (v2i
){ world
.tile_x
, world
.tile_y
} ))
873 world
.selected
= world
.tile_y
* world
.w
+ world
.tile_x
;
875 if( vg_get_button_down("primary") )
877 world
.data
[ world
.selected
].state
^= FLAG_CANAL
;
879 if( world
.data
[ world
.selected
].state
& FLAG_CANAL
)
881 sfx_set_playrnd( &audio_tile_mod
, &audio_system_sfx
, 3, 6 );
886 sfx_set_playrnd( &audio_tile_mod
, &audio_system_sfx
, 0, 3 );
890 map_reclassify( (v2i
){ world
.tile_x
-2, world
.tile_y
-2 },
891 (v2i
){ world
.tile_x
+2, world
.tile_y
+2 }, 1 );
897 else world
.selected
= -1;
899 // Simulation stop/start
900 if( vg_get_button_down("go") )
902 if( world
.simulating
)
908 vg_success( "Starting simulation!\n" );
910 sfx_set_playrnd( &audio_rolls
, &audio_system_balls_rolling
, 0, 1 );
912 world
.simulating
= 1;
913 world
.num_fishes
= 0;
915 world
.sim_start
= vg_time
;
917 for( int i
= 0; i
< world
.w
*world
.h
; i
++ )
919 world
.data
[ i
].state
&= ~FLAG_FLIP_FLOP
;
922 for( int i
= 0; i
< arrlen( world
.io
); i
++ )
923 world
.io
[i
].recv_count
= 0;
928 if( world
.simulating
)
930 while( world
.sim_frame
< (int)((vg_time
-world
.sim_start
)*2.0f
) )
932 //vg_info( "frame: %u\n", world.sim_frame );
933 sfx_set_playrnd( &audio_random
, &audio_system_balls_switching
, 0, 9 );
935 // Update splitter deltas
936 for( int i
= 0; i
< world
.h
*world
.w
; i
++ )
938 struct cell
*cell
= &world
.data
[i
];
939 if( cell
->config
== k_cell_type_split
)
941 cell
->state
&= ~FLAG_FLIP_ROTATING
;
947 // Update fish positions
948 for( int i
= 0; i
< world
.num_fishes
; i
++ )
950 struct fish
*fish
= &world
.fishes
[i
];
951 struct cell
*cell_current
= pcell( fish
->pos
);
953 if( fish
->alive
== -1 )
956 if( fish
->alive
!= 1 )
960 if( cell_current
->state
& FLAG_OUTPUT
)
962 for( int j
= 0; j
< arrlen( world
.io
); j
++ )
964 struct cell_terminal
*term
= &world
.io
[j
];
966 if( term
->id
== fish
->pos
[1]*world
.w
+ fish
->pos
[0] )
968 term
->recv
[ term
->recv_count
++ ] = fish
->payload
;
977 if( cell_current
->config
== k_cell_type_split
)
980 fish
->dir
[0] = cell_current
->state
&FLAG_FLIP_FLOP
?1:-1;
983 cell_current
->state
^= FLAG_FLIP_FLOP
;
985 else if( cell_current
->config
== k_cell_type_merge
)
993 struct cell
*cell_next
= pcell( (v2i
){ fish
->pos
[0]+fish
->dir
[0], fish
->pos
[1]+fish
->dir
[1] } );
994 if( !(cell_next
->state
& (FLAG_CANAL
|FLAG_OUTPUT
)) )
996 // Try other directions for valid, so down, left, right..
997 v2i dirs
[] = {{1,0},{-1,0},{0,-1}};
998 vg_info( "Trying some other directions...\n" );
1000 for( int j
= 0; j
< vg_list_size(dirs
); j
++ )
1002 if( (dirs
[j
][0] == -fish
->dir
[0]) && (dirs
[j
][1] == -fish
->dir
[1]) )
1005 if( pcell( (v2i
){ fish
->pos
[0]+dirs
[j
][0], fish
->pos
[1]+dirs
[j
][1] } )->state
& (FLAG_CANAL
|FLAG_OUTPUT
) )
1007 fish
->dir
[0] = dirs
[j
][0];
1008 fish
->dir
[1] = dirs
[j
][1];
1014 fish
->pos
[0] += fish
->dir
[0];
1015 fish
->pos
[1] += fish
->dir
[1];
1017 struct cell
*cell_entry
= pcell( fish
->pos
);
1019 if( !(cell_entry
->state
& (FLAG_INPUT
|FLAG_CANAL
|FLAG_OUTPUT
) ))
1025 if( cell_entry
->config
== k_cell_type_split
||
1026 cell_entry
->config
== k_cell_type_ramp_right
||
1027 cell_entry
->config
== k_cell_type_ramp_left
)
1029 // Special death (FALL)
1030 v2_sub( fish
->physics_co
, fish
->physics_v
, fish
->physics_v
);
1031 v2_divs( fish
->physics_v
, vg_time_delta
, fish
->physics_v
);
1034 vg_warn( "Special death (fall)\n" );
1039 if( cell_entry
->config
== k_cell_type_split
)
1041 sfx_set_playrnd( &audio_splitter
, &audio_system_balls_important
, 0, 1 );
1042 cell_entry
->state
|= FLAG_FLIP_ROTATING
;
1050 // Check for collisions
1051 for( int i
= 0; i
< world
.num_fishes
; i
++ )
1053 if( world
.fishes
[i
].alive
== 1 )
1055 for( int j
= i
+1; j
< world
.num_fishes
; j
++ )
1057 if( (world
.fishes
[j
].alive
== 1) && (world
.fishes
[i
].pos
[0] == world
.fishes
[j
].pos
[0]) &&
1058 (world
.fishes
[i
].pos
[1] == world
.fishes
[j
].pos
[1]) )
1060 // Shatter death (+0.5s)
1061 world
.fishes
[i
].alive
= -1;
1062 world
.fishes
[j
].alive
= -1;
1063 world
.fishes
[i
].death_time
= 0.5f
;
1064 world
.fishes
[j
].death_time
= 0.5f
;
1071 for( int i
= 0; i
< arrlen( world
.io
); i
++ )
1073 struct cell_terminal
*term
= &world
.io
[ i
];
1074 int posx
= term
->id
% world
.w
;
1075 int posy
= (term
->id
- posx
)/world
.w
;
1076 int is_input
= world
.data
[ term
->id
].state
& FLAG_INPUT
;
1080 if( world
.sim_frame
< arrlen( term
->conditions
) )
1082 struct fish
*fish
= &world
.fishes
[world
.num_fishes
++];
1083 fish
->pos
[0] = posx
;
1084 fish
->pos
[1] = posy
;
1086 fish
->payload
= term
->conditions
[world
.sim_frame
];
1090 v2i dirs
[] = {{1,0},{-1,0},{0,-1}};
1091 for( int j
= 0; j
< vg_list_size(dirs
); j
++ )
1092 if( pcell( (v2i
){ posx
+dirs
[j
][0], posy
+dirs
[j
][1] } )->state
& FLAG_CANAL
)
1094 fish
->dir
[0] = dirs
[j
][0];
1095 fish
->dir
[1] = dirs
[j
][1];
1108 if( alive_count
== 0 )
1110 world
.completed
= 1;
1112 for( int i
= 0; i
< arrlen( world
.io
); i
++ )
1114 struct cell_terminal
*term
= &world
.io
[ i
];
1115 int is_input
= world
.data
[ term
->id
].state
& FLAG_INPUT
;
1119 if( term
->recv_count
== arrlen( term
->conditions
) )
1121 for( int j
= 0; j
< arrlen( term
->conditions
); j
++ )
1123 if( term
->recv
[j
] != term
->conditions
[j
] )
1125 world
.completed
= 0;
1132 world
.completed
= 0;
1138 if( world
.completed
)
1140 vg_success( "Level passed!\n" );
1143 for( int i
= 0; i
< world
.w
*world
.h
; i
++ )
1144 if( world
.data
[ i
].state
& FLAG_CANAL
)
1147 world
.score
= score
;
1148 world
.time
= world
.sim_frame
;
1152 vg_error( "Level failed :(\n" );
1155 // Copy into career data
1156 if( world
.ptr_career_level
)
1158 world
.ptr_career_level
->score
= world
.score
;
1159 world
.ptr_career_level
->time
= world
.time
;
1160 world
.ptr_career_level
->completed
= world
.completed
;
1163 simulation_stop(); // TODO: Async?
1170 float scaled_time
= 0.0f
;
1171 scaled_time
= (vg_time
-world
.sim_start
)*2.0f
;
1172 world
.frame_lerp
= scaled_time
- (float)world
.sim_frame
;
1175 for( int i
= 0; i
< world
.num_fishes
; i
++ )
1177 struct fish
*fish
= &world
.fishes
[i
];
1179 if( fish
->alive
== 0 )
1182 if( fish
->alive
== -1 && (world
.frame_lerp
> fish
->death_time
) )
1183 continue; // Todo: particle thing?
1185 if( fish
->alive
== -2 )
1187 v2_muladds( fish
->physics_v
, (v2f
){ 0.0, -9.8f
}, vg_time_delta
, fish
->physics_v
);
1188 v2_muladds( fish
->physics_co
, fish
->physics_v
, vg_time_delta
, fish
->physics_co
);
1192 struct cell
*cell
= pcell(fish
->pos
);
1195 float t
= world
.frame_lerp
;
1197 v2_copy( fish
->physics_co
, fish
->physics_v
);
1199 switch( cell
->config
)
1202 if( fish
->dir
[0] == 1 )
1207 case 2: curve
= curve_2
; break;
1208 case 8: curve
= curve_8
; break;
1209 case 3: curve
= curve_3
; break;
1210 case 6: curve
= curve_6
; break;
1211 case 9: curve
= curve_9
; break;
1212 case 12: curve
= curve_12
; break;
1214 if( t
> curve_7_linear_section
)
1216 t
-= curve_7_linear_section
;
1217 t
*= (1.0f
/(1.0f
-curve_7_linear_section
));
1219 curve
= cell
->state
& FLAG_FLIP_FLOP
? curve_7
: curve_7_1
;
1223 default: curve
= NULL
; break;
1229 float t3
= t
* t
* t
;
1231 float cA
= 3.0f
*t2
- 3.0f
*t3
;
1232 float cB
= 3.0f
*t3
- 6.0f
*t2
+ 3.0f
*t
;
1233 float cC
= 3.0f
*t2
- t3
- 3.0f
*t
+ 1.0f
;
1235 fish
->physics_co
[0] = t3
*curve
[3][0] + cA
*curve
[2][0] + cB
*curve
[1][0] + cC
*curve
[0][0];
1236 fish
->physics_co
[1] = t3
*curve
[3][1] + cA
*curve
[2][1] + cB
*curve
[1][1] + cC
*curve
[0][1];
1237 fish
->physics_co
[0] += (float)fish
->pos
[0];
1238 fish
->physics_co
[1] += (float)fish
->pos
[1];
1243 origin
[0] = (float)fish
->pos
[0] + (float)fish
->dir
[0]*-0.5f
+ 0.5f
;
1244 origin
[1] = (float)fish
->pos
[1] + (float)fish
->dir
[1]*-0.5f
+ 0.5f
;
1246 fish
->physics_co
[0] = origin
[0] + (float)fish
->dir
[0]*t
;
1247 fish
->physics_co
[1] = origin
[1] + (float)fish
->dir
[1]*t
;
1254 static void render_tiles( v2i start
, v2i end
, v4f
const regular_colour
, v4f
const selected_colour
)
1256 v2i full_start
= { 0,0 };
1257 v2i full_end
= { world
.w
, world
.h
};
1259 if( !start
|| !end
)
1265 glUniform4fv( SHADER_UNIFORM( shader_tile_main
, "uColour" ), 1, regular_colour
);
1267 for( int y
= start
[1]; y
< end
[1]; y
++ )
1269 for( int x
= start
[0]; x
< end
[0]; x
++ )
1271 struct cell
*cell
= pcell((v2i
){x
,y
});
1272 int selected
= world
.selected
== y
*world
.w
+ x
;
1274 int tile_offsets
[][2] =
1276 {2, 0}, {0, 3}, {0, 2}, {2, 2},
1277 {1, 0}, {2, 3}, {3, 2}, {1, 3},
1278 {3, 1}, {0, 1}, {1, 2}, {2, 1},
1279 {1, 1}, {3, 3}, {2, 1}, {2, 1}
1282 int uv
[2] = { 3, 0 };
1284 if( cell
->state
& (FLAG_CANAL
|FLAG_INPUT
|FLAG_OUTPUT
) )
1286 uv
[0] = tile_offsets
[ cell
->config
][0];
1287 uv
[1] = tile_offsets
[ cell
->config
][1];
1290 glUniform4f( SHADER_UNIFORM( shader_tile_main
, "uOffset" ), (float)x
, (float)y
, uv
[0], uv
[1] );
1293 glUniform4fv( SHADER_UNIFORM( shader_tile_main
, "uColour" ), 1, selected_colour
);
1295 glUniform4fv( SHADER_UNIFORM( shader_tile_main
, "uColour" ), 1, regular_colour
);
1303 static void draw_numbers( v3f coord
, int number
)
1306 v3_copy( coord
, pos
);
1307 int digits
[8]; int i
= 0;
1309 while( number
> 0 && i
< 8 )
1311 digits
[i
++] = number
% 10;
1312 number
= number
/ 10;
1315 for( int j
= 0; j
< i
; j
++ )
1317 glUniform3fv( SHADER_UNIFORM( shader_tile_colour
, "uOffset" ), 1, pos
);
1318 draw_mesh( MESH_NUMBERS_OFFSETS
[digits
[i
-j
-1]][0], MESH_NUMBERS_OFFSETS
[digits
[i
-j
-1]][1] );
1319 pos
[0] += pos
[2] * 0.75f
;
1323 void vg_render(void)
1325 glViewport( 0,0, vg_window_x
, vg_window_y
);
1327 glDisable( GL_DEPTH_TEST
);
1328 glClearColor( 0.369768f
, 0.3654f
, 0.42f
, 1.0f
);
1329 glClear( GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
1331 v4f
const colour_default
= {1.0f
, 1.0f
, 1.0f
, 1.0f
};
1332 v4f
const colour_selected
= {0.90f
, 0.92f
, 1.0f
, 1.0f
};
1334 // TILE SET RENDERING
1335 // todo: just slam everything into a mesh...
1336 // when user modifies a tile the neighbours can be easily uploaded to gpu mem
1338 // Currently we're uploading a fair amount of data every frame anyway.
1339 // NOTE: this is for final optimisations ONLY!
1340 // ======================================================================
1342 use_mesh( &world
.tile
);
1348 SHADER_USE( shader_background
);
1349 glUniformMatrix3fv( SHADER_UNIFORM( shader_background
, "uPv" ), 1, GL_FALSE
, (float *)vg_pv
);
1351 glActiveTexture( GL_TEXTURE0
);
1352 glBindTexture( GL_TEXTURE_2D
, world
.background_data
);
1353 glUniform1i( SHADER_UNIFORM( shader_background
, "uTexMain" ), 0 );
1355 glUniform3f( SHADER_UNIFORM( shader_background
, "uOffset" ), -16, -16, 64 );
1356 glUniform1f( SHADER_UNIFORM( shader_background
, "uVariance" ), 0.02f
);
1358 glActiveTexture( GL_TEXTURE1
);
1359 glBindTexture( GL_TEXTURE_2D
, world
.random_samples
);
1360 glUniform1i( SHADER_UNIFORM( shader_background
, "uSamplerNoise" ), 1 );
1367 SHADER_USE( shader_tile_main
);
1370 m2x2_identity( subtransform
);
1371 glUniformMatrix2fv( SHADER_UNIFORM( shader_tile_main
, "uSubTransform" ), 1, GL_FALSE
, (float *)subtransform
);
1372 glUniformMatrix3fv( SHADER_UNIFORM( shader_tile_main
, "uPv" ), 1, GL_FALSE
, (float *)vg_pv
);
1373 glUniform1f( SHADER_UNIFORM( shader_tile_main
, "uGhost" ), 0.0f
);
1374 glUniform1f( SHADER_UNIFORM( shader_tile_main
, "uForeground" ), 0.0f
);
1377 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
1378 glBlendEquation(GL_FUNC_ADD
);
1381 vg_tex2d_bind( &tex_tile_data
, 0 );
1382 glUniform1i( SHADER_UNIFORM( shader_tile_main
, "uTexGlyphs" ), 0 );
1384 vg_tex2d_bind( &tex_wood
, 1 );
1385 glUniform1i( SHADER_UNIFORM( shader_tile_main
, "uTexWood" ), 1 );
1387 render_tiles( NULL
, NULL
, colour_default
, colour_default
);
1391 SHADER_USE( shader_ball
);
1392 glUniformMatrix3fv( SHADER_UNIFORM( shader_ball
, "uPv" ), 1, GL_FALSE
, (float *)vg_pv
);
1394 vg_tex2d_bind( &tex_ball
, 0 );
1395 glUniform1i( SHADER_UNIFORM( shader_ball
, "uTexMain" ), 0 );
1398 if( world
.simulating
)
1400 for( int i
= 0; i
< world
.num_fishes
; i
++ )
1402 struct fish
*fish
= &world
.fishes
[i
];
1404 if( fish
->alive
== 0 )
1407 if( fish
->alive
== -1 && (world
.frame_lerp
> fish
->death_time
) )
1410 v4f dot_colour
= { 0.0f
, 0.0f
, 0.0f
, 1.0f
};
1411 colour_code_v3( fish
->payload
, dot_colour
);
1413 glUniform3fv( SHADER_UNIFORM( shader_ball
, "uColour" ), 1, dot_colour
);
1414 glUniform2fv( SHADER_UNIFORM( shader_ball
, "uOffset" ), 1, fish
->physics_co
);
1419 SHADER_USE( shader_tile_main
);
1422 vg_tex2d_bind( &tex_tile_data
, 0 );
1423 glUniform1i( SHADER_UNIFORM( shader_tile_main
, "uTexGlyphs" ), 0 );
1425 vg_tex2d_bind( &tex_wood
, 1 );
1426 glUniform1i( SHADER_UNIFORM( shader_tile_main
, "uTexWood" ), 1 );
1428 glUniform1f( SHADER_UNIFORM( shader_tile_main
, "uForeground" ), 1.0f
);
1429 render_tiles( NULL
, NULL
, colour_default
, colour_selected
);
1432 for( int y
= 2; y
< world
.h
-2; y
++ )
1434 for( int x
= 2; x
< world
.w
-2; x
++ )
1436 struct cell
*cell
= pcell((v2i
){x
,y
});
1438 if( cell
->config
== k_cell_type_split
)
1440 float rotation
= cell
->state
& FLAG_FLIP_FLOP
? vg_rad( -45.0f
): vg_rad( 45.0f
);
1442 if( cell
->state
& FLAG_FLIP_ROTATING
)
1444 if( (world
.frame_lerp
> curve_7_linear_section
) )
1446 float const rotation_speed
= 0.4f
;
1447 if( (world
.frame_lerp
< 1.0f
-rotation_speed
) )
1449 float t
= world
.frame_lerp
- curve_7_linear_section
;
1450 t
*= -2.0f
* (1.0f
/(1.0f
-(curve_7_linear_section
+rotation_speed
)));
1460 m2x2_create_rotation( subtransform
, rotation
);
1462 glUniformMatrix2fv( SHADER_UNIFORM( shader_tile_main
, "uSubTransform" ), 1, GL_FALSE
, (float *)subtransform
);
1463 glUniform4f( SHADER_UNIFORM( shader_tile_main
, "uOffset" ), (float)x
, (float)y
+ 0.125f
, 0.0f
, 0.0f
);
1470 if( world
.selected
!= -1 && !(world
.data
[ world
.selected
].state
& FLAG_CANAL
) )
1472 v2i new_begin
= { world
.tile_x
- 2, world
.tile_y
- 2 };
1473 v2i new_end
= { world
.tile_x
+ 2, world
.tile_y
+ 2 };
1475 world
.data
[ world
.selected
].state
^= FLAG_CANAL
;
1476 map_reclassify( new_begin
, new_end
, 0 );
1478 m2x2_identity( subtransform
);
1479 glUniform1f( SHADER_UNIFORM( shader_tile_main
, "uGhost" ), 1.0f
);
1480 glUniformMatrix2fv( SHADER_UNIFORM( shader_tile_main
, "uSubTransform" ), 1, GL_FALSE
, (float *)subtransform
);
1481 glUniform2fv( SHADER_UNIFORM( shader_tile_main
, "uMousePos" ), 1, world
.tile_pos
);
1483 render_tiles( new_begin
, new_end
, colour_default
, colour_default
);
1485 world
.data
[ world
.selected
].state
^= FLAG_CANAL
;
1486 map_reclassify( new_begin
, new_end
, 0 );
1489 //glDisable(GL_BLEND);
1491 glDisable(GL_BLEND
);
1493 SHADER_USE( shader_tile_colour
);
1494 glUniformMatrix3fv( SHADER_UNIFORM( shader_tile_colour
, "uPv" ), 1, GL_FALSE
, (float *)vg_pv
);
1495 use_mesh( &world
.circle
);
1497 int const filled_start
= 0;
1498 int const filled_count
= 32;
1499 int const empty_start
= 32;
1500 int const empty_count
= 32*2;
1503 for( int i
= 0; i
< arrlen( world
.io
); i
++ )
1505 struct cell_terminal
*term
= &world
.io
[ i
];
1506 int posx
= term
->id
% world
.w
;
1507 int posy
= (term
->id
- posx
)/world
.w
;
1508 int is_input
= world
.data
[ term
->id
].state
& FLAG_INPUT
;
1510 v4f dot_colour
= { 0.0f
, 0.0f
, 0.0f
, 1.0f
};
1512 for( int j
= 0; j
< arrlen( term
->conditions
); j
++ )
1514 float y_offset
= is_input
? 1.2f
: -0.2f
;
1515 glUniform3f( SHADER_UNIFORM( shader_tile_colour
, "uOffset" ), (float)posx
+ 0.2f
+ 0.2f
* (float)j
, (float)posy
+ y_offset
, 0.1f
);
1519 colour_code_v3( term
->conditions
[j
], dot_colour
);
1520 glUniform4fv( SHADER_UNIFORM( shader_tile_colour
, "uColour" ), 1, dot_colour
);
1522 // Draw filled if tick not passed, draw empty if empty
1523 if( world
.sim_frame
> j
)
1524 draw_mesh( empty_start
, empty_count
);
1526 draw_mesh( filled_start
, filled_count
);
1530 if( term
->recv_count
> j
)
1532 colour_code_v3( term
->recv
[j
], dot_colour
);
1533 v3_muls( dot_colour
, 0.8f
, dot_colour
);
1534 glUniform4fv( SHADER_UNIFORM( shader_tile_colour
, "uColour" ), 1, dot_colour
);
1536 draw_mesh( filled_start
, filled_count
);
1539 colour_code_v3( term
->conditions
[j
], dot_colour
);
1540 glUniform4fv( SHADER_UNIFORM( shader_tile_colour
, "uColour" ), 1, dot_colour
);
1542 draw_mesh( empty_start
, empty_count
);
1547 if( world
.simulating
)
1549 glUniform4f( SHADER_UNIFORM( shader_tile_colour
, "uColour" ), 0.0f
, 0.0f
, 0.0f
, 1.0f
);
1550 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
);
1551 draw_mesh( filled_start
, filled_count
);
1555 float const score_bright
= 1.25f
;
1556 glUniform4f( SHADER_UNIFORM( shader_tile_colour
, "uColour" ),
1557 0.4f
*score_bright
, 0.39f
*score_bright
, 0.45f
*score_bright
, 1.0f
);
1559 use_mesh( &world
.numbers
);
1560 draw_numbers( (v3f
){ 2.0f
, (float)world
.h
-1.875f
, 0.3333f
}, world
.score
);
1562 // Level selection UI
1563 use_mesh( &world
.circle
);
1564 float ratio
= ((float)vg_window_x
/(float)vg_window_y
);
1566 m3x3f ui_view
= M3X3_IDENTITY
;
1567 m3x3_scale( ui_view
, (v3f
){ 1.0f
, ratio
, 1.0f
} );
1568 glUniformMatrix3fv( SHADER_UNIFORM( shader_tile_colour
, "uPv" ), 1, GL_FALSE
, (float *)ui_view
);
1570 // Calculate mouse in UIsp
1571 v3f mouse_ui_space
= { ((float)vg_mouse
[0] / (float)(vg_window_x
)) * 2.0f
- 1.0f
,
1572 (((float)vg_mouse
[1] / (float)(vg_window_y
)) * 2.0f
- 1.0f
)*(-1.0f
/ratio
), 0.0125f
};
1574 // Get selected level
1575 const float selection_scale
= 0.05f
;
1576 int const level_count
= vg_list_size( level_pack_1
);
1577 int level_select
= -1;
1579 if( mouse_ui_space
[0] <= -0.8f
)
1581 float levels_range
= (float)level_count
*selection_scale
*0.6f
;
1582 float level_offset
= ((-mouse_ui_space
[1] + levels_range
) / levels_range
) * 0.5f
* (float)level_count
;
1583 level_select
= ceilf( level_offset
);
1586 if( level_select
>= 0 && level_select
< vg_list_size( level_pack_1
) )
1588 glUniform4f( SHADER_UNIFORM( shader_tile_colour
, "uColour" ), 0.369768f
, 0.3654f
, 0.42f
, 1.0f
);
1590 use_mesh( &world
.tile
);
1591 glUniform3f( SHADER_UNIFORM( shader_tile_colour
, "uOffset" ),
1593 ((float)level_count
- (float)level_select
* 2.0f
) * selection_scale
* 0.6f
,
1598 use_mesh( &world
.circle
);
1600 if( vg_get_button_down( "primary" ) )
1602 console_changelevel( 1, level_pack_1
+ level_select
);
1606 else mouse_ui_space
[1] = INFINITY
;
1608 glUniform4f( SHADER_UNIFORM( shader_tile_colour
, "uColour" ), 0.4f
, 0.39f
, 0.45f
, 1.0f
);
1611 for( int i
= 0; i
< level_count
; i
++ )
1613 struct career_level
*clevel
= &career
.levels
[i
];
1615 v3f level_ui_space
= {
1617 ((float)level_count
- (float)i
* 2.0f
) * selection_scale
* 0.6f
+ selection_scale
* 0.5f
,
1618 selection_scale
* 0.5f
1621 float scale
= vg_clampf( 1.0f
- fabsf(level_ui_space
[1] - mouse_ui_space
[1]) * 2.0f
, 0.9f
, 1.0f
);
1622 level_ui_space
[2] *= scale
;
1624 glUniform3fv( SHADER_UNIFORM( shader_tile_colour
, "uOffset" ), 1, level_ui_space
);
1626 if( clevel
->completed
)
1627 draw_mesh( filled_start
, filled_count
);
1629 draw_mesh( empty_start
, empty_count
);
1632 //use_mesh( &world.numbers );
1633 //draw_numbers( (v3f){ 0.0f, -0.5f, 0.1f }, 128765 );