1 // Copyright (C) 2021 Harry Godden (hgn) - All Rights Reserved
5 #include "fishladder_resources.h"
7 const char *level_pack_1
[] = {
32 levels
[ vg_list_size( level_pack_1
) ];
34 career
= { .version
= 1 };
37 static void career_serialize(void)
39 vg_asset_write( "sav/game.sav", &career
, sizeof( struct career_state
) );
42 static void career_load(void)
45 struct career_state
*cr
= vg_asset_read_s( "sav/game.sav", &sz
);
47 memset( (void*)career
.levels
, 0, vg_list_size(level_pack_1
) * sizeof(struct career_level
) );
51 if( sz
> sizeof( struct career_state
) )
52 vg_warn( "This save file is too big! Some levels will be lost\n" );
54 if( sz
<= offsetof( struct career_state
, levels
) )
56 vg_error( "This save file is too small to have a header\n" );
61 u32
const size_header
= offsetof(struct career_state
, levels
);
62 u32
const size_levels
= sizeof(struct career_state
)-size_header
;
63 u32
const size_levels_input
= sz
- size_header
;
65 memcpy( (void*)career
.levels
, (void*)cr
->levels
, size_levels_input
);
67 if( sz
< sizeof( struct career_state
) )
69 memset( ((void*)career
.levels
) + size_levels_input
, 0, size_levels
-size_levels_input
);
73 vg_success( "Loaded save file... Info:\n" );
75 for( int i
= 0; i
< vg_list_size( career
.levels
); i
++ )
77 struct career_level
*lvl
= &career
.levels
[i
];
78 vg_info( "Score: %u, Time: %u, Completed: %u\n", lvl
->score
, lvl
->time
, lvl
->completed
);
83 vg_info( "No save file... Using blank one\n" );
91 #define FLAG_CANAL 0x1
92 #define FLAG_IS_TRIGGER 0x2
93 #define FLAG_RESERVED0 0x4
94 #define FLAG_RESERVED1 0x8
96 #define FLAG_INPUT 0x10
97 #define FLAG_OUTPUT 0x20
98 #define FLAG_WALL 0x40
100 #define FLAG_FLIP_FLOP 0x100
101 #define FLAG_TRIGGERED 0x200
102 #define FLAG_FLIP_ROTATING 0x400
103 #define FLAG_TARGETED 0x800
106 0000 0 | 0001 1 | 0010 2 | 0011 3
110 0100 4 | 0101 5 | 0110 6 | 0111 7
114 1000 8 | 1001 9 | 1010 10 | 1011 11
118 1100 12 | 1101 13 | 1110 14 | 1111 15
126 k_cell_type_ramp_right
= 3,
127 k_cell_type_ramp_left
= 6,
128 k_cell_type_split
= 7,
129 k_cell_type_merge
= 13
133 { { 0.9f
, 0.6f
, 0.20f
},
134 { 0.2f
, 0.9f
, 0.14f
},
135 { 0.4f
, 0.8f
, 1.00f
} };
137 static void colour_code_v3( char cc
, v3f target
)
139 if( cc
>= 'a' && cc
<= 'z' )
143 if( id
< vg_list_size( colour_sets
) )
145 v3_copy( colour_sets
[ id
], target
);
150 v3_copy( (v3f
){0.0f
,0.0f
,0.0f
}, target
);
159 static void init_mesh( struct mesh
*m
, float const *tris
, u32 length
)
161 m
->elements
= length
/3;
162 glGenVertexArrays( 1, &m
->vao
);
163 glGenBuffers( 1, &m
->vbo
);
165 glBindVertexArray( m
->vao
);
166 glBindBuffer( GL_ARRAY_BUFFER
, m
->vbo
);
167 glBufferData( GL_ARRAY_BUFFER
, length
*sizeof(float), tris
, GL_STATIC_DRAW
);
169 glVertexAttribPointer( 0, 2, GL_FLOAT
, GL_FALSE
, 2 * sizeof(float), (void*)0 );
170 glEnableVertexAttribArray( 0 );
175 static void free_mesh( struct mesh
*m
)
177 glDeleteVertexArrays( 1, &m
->vao
);
178 glDeleteBuffers( 1, &m
->vbo
);
181 static void draw_mesh( int const start
, int const count
)
183 glDrawArrays( GL_TRIANGLES
, start
*3, count
*3 );
186 static void use_mesh( struct mesh
*m
)
188 glBindVertexArray( m
->vao
);
211 int sim_run
, max_runs
;
225 int condition_count
, recv_count
;
237 struct mesh tile
, circle
, numbers
;
240 GLuint vao
, vbo
, ebo
;
245 GLuint background_data
;
246 GLuint random_samples
;
248 int selected
, tile_x
, tile_y
;
266 struct career_level
*ptr_career_level
;
274 static void map_free(void)
276 arrfree( world
.data
);
287 world
.initialzed
= 0;
290 static void io_reset(void)
292 for( int i
= 0; i
< arrlen( world
.io
); i
++ )
294 struct cell_terminal
*term
= &world
.io
[i
];
296 for( int j
= 0; j
< term
->run_count
; j
++ )
297 term
->runs
[j
].recv_count
= 0;
301 static void map_reclassify( v2i start
, v2i end
, int update_texbuffer
);
302 static int map_load( const char *str
, const char *name
)
304 //TODO: It may be worthwhile, at this point, to switch to binary encoding for save data
313 if( str
[world
.w
] == ';' )
315 else if( !str
[world
.w
] )
317 vg_error( "Unexpected EOF when parsing level\n" );
322 struct cell
*row
= arraddnptr( world
.data
, world
.w
);
324 int reg_start
= 0, reg_end
= 0;
326 u32
*links_to_make
= NULL
;
327 int links_satisfied
= 0;
329 char link_id_buffer
[32];
337 if( *c
== '\r' ) { c
++; continue; } // fuck off windows
343 if( *c
== '\r' ) c
++;
350 if( *c
== '\r' ) { c
++; continue; }
352 if( reg_start
< reg_end
)
354 struct cell_terminal
*terminal
= &world
.io
[ reg_start
];
355 struct terminal_run
*run
= &terminal
->runs
[ terminal
->run_count
-1 ];
357 if( *c
>= 'a' && *c
<= 'z' )
359 run
->conditions
[ run
->condition_count
++ ] = *c
;
363 if( *c
== ',' || *c
== '\n' )
372 terminal
->runs
[ terminal
->run_count
].condition_count
= 0;
373 terminal
->run_count
++;
374 world
.max_runs
= vg_max( world
.max_runs
, terminal
->run_count
);
378 vg_error( "Unkown attribute '%c' (row: %u)\n", *c
, world
.h
);
385 if( links_satisfied
< arrlen( links_to_make
) )
387 struct cell
*target
= &world
.data
[ links_to_make
[ links_satisfied
] ];
389 if( (((u32
)*c
>= (u32
)'0') && ((u32
)*c
<= (u32
)'9')) || *c
== '-' )
391 if( link_id_n
>= vg_list_size( link_id_buffer
)-1 )
393 vg_error( "Number was way too long to be parsed (row: %u)\n", world
.h
);
397 link_id_buffer
[ link_id_n
++ ] = *c
;
399 else if( *c
== ',' || *c
== '\n' )
401 link_id_buffer
[ link_id_n
] = 0x00;
402 int value
= atoi( link_id_buffer
);
404 target
->links
[value
>= 0? 1:0] = abs(value
);
413 vg_error( "Invalid character '%c' (row: %u)\n", *c
, world
.h
);
419 vg_error( "Too many values to assign (row: %u)\n", world
.h
);
428 // Registry length-error checks
429 if( reg_start
!= reg_end
)
431 vg_error( "Not enough spawn values assigned (row: %u, %u of %u)\n", world
.h
, reg_start
, reg_end
);
435 if( links_satisfied
!= arrlen( links_to_make
) )
437 vg_error( "Not enough link values assigned (row: %u, %u of %u)\n", world
.h
, links_satisfied
, arrlen( links_to_make
) );
443 vg_error( "Not enough cells to match previous row definition (row: %u, %u<%u)\n", world
.h
, cx
, world
.w
);
447 row
= arraddnptr( world
.data
, world
.w
);
450 reg_end
= reg_start
= arrlen( world
.io
);
452 arrsetlen( links_to_make
, 0 );
459 vg_error( "Too many cells to match previous row definition (row: %u, %u>%u)\n", world
.h
, cx
, world
.w
);
463 // Tile initialization
465 struct cell
*cell
= &row
[ cx
];
467 if( *c
== '+' || *c
== '-' )
469 struct cell_terminal
*term
= arraddnptr( world
.io
, 1 );
470 term
->id
= cx
+ world
.h
*world
.w
;
472 term
->runs
[0].condition_count
= 0;
474 cell
->state
= *c
== '+'? FLAG_INPUT
: FLAG_OUTPUT
;
477 else if( *c
== '#' ) cell
->state
= FLAG_WALL
;
478 else if( ((u32
)*c
>= (u32
)'A') && ((u32
)*c
<= (u32
)'A'+0xf) )
480 // Canal flag bits (4bit/16 value):
486 cell
->state
= ((u32
)*c
- (u32
)'A') & (FLAG_CANAL
|FLAG_IS_TRIGGER
);
488 if( cell
->state
& FLAG_IS_TRIGGER
)
489 arrpush( links_to_make
, cx
+ world
.h
*world
.w
);
495 else cell
->state
= 0x00;
503 // Update data texture to fill out the background
505 u8 info_buffer
[64*64*4];
506 for( int i
= 0; i
< 64*64; i
++ )
508 u8
*px
= &info_buffer
[i
*4];
515 glBindTexture( GL_TEXTURE_2D
, world
.background_data
);
516 glTexSubImage2D( GL_TEXTURE_2D
, 0, 0, 0, 64, 64, GL_RGBA
, GL_UNSIGNED_BYTE
, info_buffer
);
519 arrfree( links_to_make
);
521 map_reclassify( NULL
, NULL
, 1 );
524 for( int i
= 0; i
< world
.h
*world
.w
; i
++ )
526 struct cell
*src
= &world
.data
[i
];
527 if( src
->state
& FLAG_IS_TRIGGER
)
529 int link_id
= src
->links
[0]?0:1;
530 if( src
->links
[link_id
] <= world
.h
*world
.w
)
532 struct cell
*target
= &world
.data
[ src
->links
[link_id
] ];
533 if( (target
->state
& FLAG_CANAL
) && (target
->config
== k_cell_type_split
) )
535 if( target
->links
[ link_id
] )
537 vg_error( "Link target was already targeted\n" );
543 target
->links
[ link_id
] = i
;
544 target
->state
|= FLAG_TARGETED
;
549 vg_error( "Link target was invalid\n" );
555 vg_error( "Link target out of bounds\n" );
561 vg_success( "Map '%s' loaded! (%u:%u)\n", name
, world
.w
, world
.h
);
565 strncpy( world
.map_name
, name
, vg_list_size( world
.map_name
)-1 );
566 world
.initialzed
= 1;
570 arrfree( links_to_make
);
575 static struct cell
*pcell( v2i pos
)
577 return &world
.data
[ pos
[1]*world
.w
+ pos
[0] ];
580 static void map_serialize( FILE *stream
)
582 for( int y
= 0; y
< world
.h
; y
++ )
584 for( int x
= 0; x
< world
.w
; x
++ )
586 struct cell
*cell
= pcell( (v2i
){ x
, y
} );
588 if( cell
->state
& FLAG_WALL
) fputc( '#', stream
);
589 else if( cell
->state
& FLAG_INPUT
) fputc( '+', stream
);
590 else if( cell
->state
& FLAG_OUTPUT
) fputc( '-', stream
);
591 else if( cell
->state
& (FLAG_CANAL
|FLAG_IS_TRIGGER
|FLAG_RESERVED0
|FLAG_RESERVED1
) )
593 fputc( (cell
->state
& (FLAG_CANAL
|FLAG_IS_TRIGGER
|FLAG_RESERVED0
|FLAG_RESERVED1
)) + (u32
)'A', stream
);
595 else fputc( ' ', stream
);
598 fputc( ';', stream
);
600 int terminal_write_count
= 0;
602 for( int x
= 0; x
< world
.w
; x
++ )
604 for( int i
= 0; i
< arrlen( world
.io
); i
++ )
606 struct cell_terminal
*term
= &world
.io
[i
];
607 if( term
->id
== y
*world
.w
+x
)
609 if( terminal_write_count
)
610 fputc( ',', stream
);
611 terminal_write_count
++;
613 for( int j
= 0; j
< term
->run_count
; j
++ )
615 struct terminal_run
*run
= &term
->runs
[j
];
617 for( int k
= 0; k
< run
->condition_count
; k
++ )
618 fputc( run
->conditions
[k
], stream
);
620 if( j
< term
->run_count
-1 )
621 fputc( ':', stream
);
627 for( int x
= 0; x
< world
.w
; x
++ )
629 struct cell
*cell
= pcell( (v2i
){ x
,y
} );
630 if( cell
->state
& FLAG_IS_TRIGGER
)
632 if( terminal_write_count
)
633 fputc( ',', stream
);
634 terminal_write_count
++;
636 fprintf( stream
, "%d", cell
->links
[0]? -cell
->links
[0]: cell
->links
[1] );
640 fputc( '\n', stream
);
644 int main( int argc
, char *argv
[] )
646 vg_init( argc
, argv
, "Fish (Marbles Computer) Ladder Simulator 2022 | N,M: change level | SPACE: Test | LeftClick: Toggle tile" );
649 static int console_save_map( int argc
, char const *argv
[] )
651 if( !world
.initialzed
)
653 vg_error( "Tried to save uninitialized map!\n" );
657 char map_path
[ 256 ];
659 strcpy( map_path
, "sav/" );
660 strcat( map_path
, world
.map_name
);
661 strcat( map_path
, ".map" );
663 FILE *test_writer
= fopen( map_path
, "wb" );
666 vg_info( "Saving map to '%s'\n", map_path
);
667 map_serialize( test_writer
);
669 fclose( test_writer
);
674 vg_error( "Unable to open stream for writing\n" );
679 static int console_load_map( int argc
, char const *argv
[] )
681 char map_path
[ 256 ];
686 strcpy( map_path
, "sav/" );
687 strcat( map_path
, argv
[0] );
688 strcat( map_path
, ".map" );
690 char *text_source
= vg_textasset_read( map_path
);
694 strcpy( map_path
, "maps/" );
695 strcat( map_path
, argv
[0] );
696 strcat( map_path
, ".map" );
698 text_source
= vg_textasset_read( map_path
);
703 vg_info( "Loading map: '%s'\n", map_path
);
704 world
.ptr_career_level
= NULL
;
706 if( !map_load( text_source
, argv
[0] ) )
714 for( int i
= 0; i
< vg_list_size( level_pack_1
); i
++ )
716 if( !strcmp( level_pack_1
[i
], argv
[0] ) )
718 world
.ptr_career_level
= career
.levels
+ i
;
727 vg_error( "Missing maps '%s'\n", argv
[0] );
733 vg_error( "Missing argument <map_path>\n" );
738 static void simulation_stop(void)
740 world
.simulating
= 0;
741 world
.num_fishes
= 0;
746 sfx_system_fadeout( &audio_system_balls_rolling
, 44100 );
748 vg_info( "Stopping simulation!\n" );
751 static int console_changelevel( int argc
, char const *argv
[] )
755 // Save current level
756 console_save_map( 0, NULL
);
757 if( console_load_map( argc
, argv
) )
765 vg_error( "Missing argument <map_path>\n" );
773 vg_function_push( (struct vg_cmd
){
774 .name
= "_map_write",
775 .function
= console_save_map
778 vg_function_push( (struct vg_cmd
){
780 .function
= console_load_map
783 vg_function_push( (struct vg_cmd
){
785 .function
= console_changelevel
792 0.0f
, 0.0f
, 0.0f
, 1.0f
, 1.0f
, 1.0f
,
793 0.0f
, 0.0f
, 1.0f
, 1.0f
, 1.0f
, 0.0f
,
795 0.0f
, 0.0f
, 0.0f
, 1.0f
, 4.0f
, 1.0f
,
796 0.0f
, 0.0f
, 4.0f
, 1.0f
, 4.0f
, 0.0f
799 init_mesh( &world
.tile
, quad_mesh
, vg_list_size(quad_mesh
) );
804 float circle_mesh
[32*6*3];
805 int res
= vg_list_size( circle_mesh
) / (6*3);
807 for( int i
= 0; i
< res
; i
++ )
809 v2f v0
= { sinf( ((float)i
/(float)res
)*VG_TAUf
), cosf( ((float)i
/(float)res
)*VG_TAUf
) };
810 v2f v1
= { sinf( ((float)(i
+1)/(float)res
)*VG_TAUf
), cosf( ((float)(i
+1)/(float)res
)*VG_TAUf
) };
812 circle_mesh
[ i
*6+0 ] = 0.0f
;
813 circle_mesh
[ i
*6+1 ] = 0.0f
;
815 v2_copy( v0
, circle_mesh
+ 32*6 + i
*12 );
816 v2_muls( v0
, 0.8f
, circle_mesh
+ 32*6 + i
*12+2 );
817 v2_copy( v1
, circle_mesh
+ 32*6 + i
*12+4 );
819 v2_copy( v1
, circle_mesh
+ 32*6 + i
*12+6 );
820 v2_muls( v1
, 0.8f
, circle_mesh
+ 32*6 + i
*12+8 );
821 v2_muls( v0
, 0.8f
, circle_mesh
+ 32*6 + i
*12+10 );
823 v2_copy( v0
, circle_mesh
+ i
*6+4 );
824 v2_copy( v1
, circle_mesh
+ i
*6+2 );
825 v2_copy( v0
, circle_mesh
+i
*6+4 );
826 v2_copy( v1
, circle_mesh
+i
*6+2 );
829 init_mesh( &world
.circle
, circle_mesh
, vg_list_size( circle_mesh
) );
834 init_mesh( &world
.numbers
,
836 vg_list_size( MESH_NUMBERS_BUFFER
)
839 for( int i
= 0; i
< 10; i
++ )
841 vg_info( "offset: %u, length: %u\n", MESH_NUMBERS_OFFSETS
[i
][0], MESH_NUMBERS_OFFSETS
[i
][1] );
847 int const num_segments
= 64;
849 struct mesh_wire
*mw
= &world
.wire
;
851 v2f wire_points
[ num_segments
* 2 ];
852 u16 wire_indices
[ 6*(num_segments
-1) ];
854 for( int i
= 0; i
< num_segments
; i
++ )
856 float l
= (float)i
/ (float)(num_segments
-1);
858 v2_copy( (v2f
){ l
, -0.5f
}, wire_points
[i
*2+0] );
859 v2_copy( (v2f
){ l
, 0.5f
}, wire_points
[i
*2+1] );
861 if( i
< num_segments
-1 )
863 wire_indices
[ i
*6+0 ] = i
*2 + 0;
864 wire_indices
[ i
*6+1 ] = i
*2 + 1;
865 wire_indices
[ i
*6+2 ] = i
*2 + 3;
866 wire_indices
[ i
*6+3 ] = i
*2 + 0;
867 wire_indices
[ i
*6+4 ] = i
*2 + 3;
868 wire_indices
[ i
*6+5 ] = i
*2 + 2;
872 glGenVertexArrays( 1, &mw
->vao
);
873 glGenBuffers( 1, &mw
->vbo
);
874 glGenBuffers( 1, &mw
->ebo
);
875 glBindVertexArray( mw
->vao
);
877 glBindBuffer( GL_ARRAY_BUFFER
, mw
->vbo
);
879 glBufferData( GL_ARRAY_BUFFER
, sizeof( wire_points
), wire_points
, GL_STATIC_DRAW
);
880 glBindVertexArray( mw
->vao
);
882 glBindBuffer( GL_ELEMENT_ARRAY_BUFFER
, mw
->ebo
);
883 glBufferData( GL_ELEMENT_ARRAY_BUFFER
, sizeof( wire_indices
), wire_indices
, GL_STATIC_DRAW
);
886 glVertexAttribPointer( 0, 2, GL_FLOAT
, GL_FALSE
, 2*sizeof(float), (void*)0 );
887 glEnableVertexAttribArray( 0 );
891 mw
->em
= vg_list_size( wire_indices
);
894 // Create info data texture
896 glGenTextures( 1, &world
.background_data
);
897 glBindTexture( GL_TEXTURE_2D
, world
.background_data
);
898 glTexImage2D( GL_TEXTURE_2D
, 0, GL_RGBA
, 64, 64, 0, GL_RGBA
, GL_UNSIGNED_BYTE
, NULL
);
902 // Create random smaples texture
904 u8
*data
= malloc(512*512*2);
905 for( int i
= 0; i
< 512*512*2; i
++ )
906 data
[ i
] = rand()/(RAND_MAX
/255);
908 glGenTextures( 1, &world
.random_samples
);
909 glBindTexture( GL_TEXTURE_2D
, world
.random_samples
);
910 glTexImage2D( GL_TEXTURE_2D
, 0, GL_RG
, 512, 512, 0, GL_RG
, GL_UNSIGNED_BYTE
, data
);
917 resource_load_main();
921 console_load_map( 1, level_pack_1
);
926 console_save_map( 0, NULL
);
929 resource_free_main();
931 glDeleteTextures( 1, &world
.background_data
);
932 glDeleteTextures( 1, &world
.random_samples
);
934 glDeleteVertexArrays( 1, &world
.wire
.vao
);
935 glDeleteBuffers( 1, &world
.wire
.vbo
);
936 glDeleteBuffers( 1, &world
.wire
.ebo
);
938 free_mesh( &world
.tile
);
939 free_mesh( &world
.circle
);
940 free_mesh( &world
.numbers
);
945 static int cell_interactive( v2i co
)
948 if( co
[0] < 2 || co
[0] >= world
.w
-2 || co
[1] < 2 || co
[1] >= world
.h
-2 )
952 if( world
.data
[ world
.w
*co
[1] + co
[0] ].state
& (FLAG_WALL
|FLAG_INPUT
|FLAG_OUTPUT
) )
955 // List of 3x3 configurations that we do not allow
956 static u32 invalid_src
[][9] =
988 // Statically compile invalid configurations into bitmasks
989 static u32 invalid
[ vg_list_size(invalid_src
) ];
991 for( int i
= 0; i
< vg_list_size(invalid_src
); i
++ )
995 for( int j
= 0; j
< 3; j
++ )
996 for( int k
= 0; k
< 3; k
++ )
997 comped
|= invalid_src
[i
][ j
*3+k
] << ((j
*5)+k
);
1002 // Extract 5x5 grid surrounding tile
1004 for( int y
= co
[1]-2; y
< co
[1]+3; y
++ )
1005 for( int x
= co
[0]-2; x
< co
[0]+3; x
++ )
1007 struct cell
*cell
= pcell((v2i
){x
,y
});
1009 if( cell
&& (cell
->state
& (FLAG_CANAL
|FLAG_INPUT
|FLAG_OUTPUT
)) )
1010 blob
|= 0x1 << ((y
-(co
[1]-2))*5 + x
-(co
[0]-2));
1013 // Run filter over center 3x3 grid to check for invalid configurations
1014 int kernel
[] = { 0, 1, 2, 5, 6, 7, 10, 11, 12 };
1015 for( int i
= 0; i
< vg_list_size(kernel
); i
++ )
1017 if( blob
& (0x1 << (6+kernel
[i
])) )
1019 u32 window
= blob
>> kernel
[i
];
1021 for( int j
= 0; j
< vg_list_size(invalid
); j
++ )
1022 if((window
& invalid
[j
]) == invalid
[j
])
1030 static void map_reclassify( v2i start
, v2i end
, int update_texbuffer
)
1032 v2i full_start
= { 1,1 };
1033 v2i full_end
= { world
.w
-1, world
.h
-1 };
1035 if( !start
|| !end
)
1042 u8 info_buffer
[64*64*4];
1045 int px0
= vg_max( start
[0], full_start
[0] ),
1046 px1
= vg_min( end
[0], full_end
[0] ),
1047 py0
= vg_max( start
[1], full_start
[1] ),
1048 py1
= vg_min( end
[1], full_end
[1] );
1050 for( int y
= py0
; y
< py1
; y
++ )
1052 for( int x
= px0
; x
< px1
; x
++ )
1054 struct cell
*cell
= pcell((v2i
){x
,y
});
1056 v2i dirs
[] = {{1,0},{0,1},{-1,0},{0,-1}};
1061 if( cell
->state
& (FLAG_CANAL
|FLAG_INPUT
|FLAG_OUTPUT
) )
1063 for( int i
= 0; i
< vg_list_size( dirs
); i
++ )
1065 struct cell
*neighbour
= pcell((v2i
){x
+dirs
[i
][0], y
+dirs
[i
][1]});
1066 if( neighbour
->state
& (FLAG_CANAL
|FLAG_INPUT
|FLAG_OUTPUT
) )
1074 if( cell
->state
& FLAG_WALL
)
1080 pcell((v2i
){x
,y
})->config
= config
;
1082 u8
*info_px
= &info_buffer
[ (pixel_id
++)*4 ];
1083 info_px
[0] = height
;
1084 info_px
[1] = cell
->state
& FLAG_WALL
? 0: 255;
1090 if( update_texbuffer
)
1092 glBindTexture( GL_TEXTURE_2D
, world
.background_data
);
1093 glTexSubImage2D( GL_TEXTURE_2D
, 0, px0
+ 16, py0
+ 16, px1
-px0
, py1
-py0
, GL_RGBA
, GL_UNSIGNED_BYTE
, info_buffer
);
1098 v2f
const curve_3
[] = {{0.5f
,1.0f
},{0.5f
,0.625f
},{0.625f
,0.5f
},{1.0f
,0.5f
}};
1099 v2f
const curve_6
[] = {{0.5f
,1.0f
},{0.5f
,0.625f
},{0.375f
,0.5f
},{0.0f
,0.5f
}};
1100 v2f
const curve_9
[] = {{1.0f
,0.5f
},{0.625f
,0.5f
},{0.5f
,0.375f
},{0.5f
,0.0f
}};
1101 v2f
const curve_12
[]= {{0.0f
,0.5f
},{0.375f
,0.5f
},{0.5f
,0.375f
},{0.5f
,0.0f
}};
1103 v2f
const curve_1
[] = {{1.0f
,0.5f
},{0.8f
,0.5f
},{0.3f
,0.5f
},{0.2f
,0.5f
}};
1104 v2f
const curve_4
[] = {{0.0f
,0.5f
},{0.3f
,0.5f
},{0.5f
,0.5f
},{0.8f
,0.5f
}};
1105 v2f
const curve_2
[] = {{0.5f
,1.0f
},{0.5f
,0.8f
},{0.5f
,0.3f
},{0.5f
,0.2f
}};
1106 v2f
const curve_8
[] = {{0.5f
,0.8f
},{0.5f
,0.5f
},{0.5f
,0.3f
},{0.5f
,0.0f
}};
1108 v2f
const curve_7
[] = {{0.5f
,0.8438f
},{0.875f
,0.8438f
},{0.625f
,0.5f
},{1.0f
,0.5f
}};
1109 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
}};
1111 float const curve_7_linear_section
= 0.1562f
;
1113 u16 id_drag_from
= 0;
1117 void vg_update(void)
1119 // Fit within screen
1121 float r1
= (float)vg_window_y
/ (float)vg_window_x
,
1122 r2
= (float)world
.h
/ (float)world
.w
,
1125 size
= ( r2
< r1
? (float)world
.w
* 0.5f
: ((float)world
.h
* 0.5f
) / r1
) + 2.5f
;
1126 m3x3_projection( m_projection
, -size
, size
, -size
*r1
, size
*r1
);
1129 origin
[0] = floorf( -0.5f
* world
.w
);
1130 origin
[1] = floorf( -0.5f
* world
.h
);
1133 m3x3_identity( m_view
);
1134 m3x3_translate( m_view
, origin
);
1135 m3x3_mul( m_projection
, m_view
, vg_pv
);
1136 vg_projection_update();
1139 v2_copy( vg_mouse_ws
, world
.tile_pos
);
1141 world
.tile_x
= floorf( world
.tile_pos
[0] );
1142 world
.tile_y
= floorf( world
.tile_pos
[1] );
1145 if( !world
.simulating
)
1147 v2_copy( vg_mouse_ws
, drag_to_co
);
1149 if( cell_interactive( (v2i
){ world
.tile_x
, world
.tile_y
} ))
1151 world
.selected
= world
.tile_y
* world
.w
+ world
.tile_x
;
1153 static u32 modify_state
= 0;
1155 struct cell
*cell_ptr
= &world
.data
[world
.selected
];
1157 if( vg_get_button_down("primary") )
1159 modify_state
= (cell_ptr
->state
& FLAG_CANAL
) ^ FLAG_CANAL
;
1162 if( vg_get_button("primary") && ((cell_ptr
->state
& FLAG_CANAL
) != modify_state
) )
1164 cell_ptr
->state
&= ~FLAG_CANAL
;
1165 cell_ptr
->state
|= modify_state
;
1167 if( cell_ptr
->state
& FLAG_CANAL
)
1169 sfx_set_playrnd( &audio_tile_mod
, &audio_system_sfx
, 3, 6 );
1174 if( cell_ptr
->state
& (FLAG_IS_TRIGGER
|FLAG_TARGETED
) )
1176 cell_ptr
->state
&= ~(FLAG_IS_TRIGGER
|FLAG_TARGETED
);
1177 for( u32 i
= 0; i
< 2; i
++ )
1179 if( cell_ptr
->links
[i
] )
1181 struct cell
*other_ptr
= &world
.data
[ cell_ptr
->links
[i
] ];
1182 other_ptr
->links
[ i
] = 0;
1183 other_ptr
->state
&= ~FLAG_IS_TRIGGER
;
1185 if( other_ptr
->links
[ i
^ 0x1 ] == 0 )
1186 other_ptr
->state
&= ~(FLAG_TARGETED
);
1191 sfx_set_playrnd( &audio_tile_mod
, &audio_system_sfx
, 0, 3 );
1195 cell_ptr
->links
[0] = 0;
1196 cell_ptr
->links
[1] = 0;
1198 map_reclassify( (v2i
){ world
.tile_x
-2, world
.tile_y
-2 },
1199 (v2i
){ world
.tile_x
+2, world
.tile_y
+2 }, 1 );
1202 if( vg_get_button_down("secondary") && !(cell_ptr
->config
== k_cell_type_split
) )
1204 id_drag_from
= world
.selected
;
1205 drag_from_co
[0] = world
.tile_x
+ 0.5f
;
1206 drag_from_co
[1] = world
.tile_y
+ 0.5f
;
1209 if( id_drag_from
&& (cell_ptr
->config
== k_cell_type_split
) )
1211 float local_x
= vg_mouse_ws
[0] - (float)world
.tile_x
;
1212 drag_to_co
[0] = (float)world
.tile_x
+ (local_x
> 0.5f
? 0.75f
: 0.25f
);
1213 drag_to_co
[1] = (float)world
.tile_y
+ 0.25f
;
1215 if( vg_get_button_up("secondary") )
1217 struct cell
*drag_ptr
= &world
.data
[id_drag_from
];
1218 u32 link_id
= local_x
> 0.5f
? 1: 0;
1220 // Cleanup existing connections
1221 if( cell_ptr
->links
[ link_id
] )
1223 vg_warn( "Destroying existing connection on link %u (%hu)\n", link_id
, cell_ptr
->links
[ link_id
] );
1225 struct cell
*current_connection
= &world
.data
[ cell_ptr
->links
[ link_id
]];
1226 current_connection
->state
&= ~FLAG_IS_TRIGGER
;
1227 current_connection
->links
[ link_id
] = 0;
1230 if( drag_ptr
->links
[ link_id
^ 0x1 ] )
1232 vg_warn( "Destroying alternate link %u (%hu)\n", link_id
^ 0x1, drag_ptr
->links
[ link_id
^ 0x1 ] );
1234 struct cell
*current_connection
= &world
.data
[ drag_ptr
->links
[ link_id
^ 0x1 ]];
1235 if( !current_connection
->links
[ link_id
] )
1236 current_connection
->state
&= ~FLAG_TARGETED
;
1238 current_connection
->links
[ link_id
^ 0x1 ] = 0;
1239 drag_ptr
->links
[ link_id
^ 0x1 ] = 0;
1242 // Create the new connection
1243 vg_success( "Creating connection on link %u (%hu)\n", link_id
, id_drag_from
);
1245 cell_ptr
->links
[ link_id
] = id_drag_from
;
1246 drag_ptr
->links
[ link_id
] = world
.selected
;
1248 cell_ptr
->state
|= FLAG_TARGETED
;
1249 drag_ptr
->state
|= FLAG_IS_TRIGGER
;
1255 world
.selected
= -1;
1257 if( !(vg_get_button("secondary") && id_drag_from
) )
1262 world
.selected
= -1;
1266 // Simulation stop/start
1267 if( vg_get_button_down("go") )
1269 if( world
.simulating
)
1275 vg_success( "Starting simulation!\n" );
1277 sfx_set_playrnd( &audio_rolls
, &audio_system_balls_rolling
, 0, 1 );
1279 world
.simulating
= 1;
1280 world
.num_fishes
= 0;
1281 world
.sim_frame
= 0;
1282 world
.sim_start
= vg_time
;
1285 for( int i
= 0; i
< world
.w
*world
.h
; i
++ )
1286 world
.data
[ i
].state
&= ~FLAG_FLIP_FLOP
;
1293 if( world
.simulating
)
1295 while( world
.sim_frame
< (int)((vg_time
-world
.sim_start
)*2.0f
) )
1297 //vg_info( "frame: %u\n", world.sim_frame );
1298 sfx_set_playrnd( &audio_random
, &audio_system_balls_switching
, 0, 9 );
1300 // Update splitter deltas
1301 for( int i
= 0; i
< world
.h
*world
.w
; i
++ )
1303 struct cell
*cell
= &world
.data
[i
];
1304 if( cell
->config
== k_cell_type_split
)
1306 cell
->state
&= ~FLAG_FLIP_ROTATING
;
1308 if( cell
->state
& FLAG_IS_TRIGGER
)
1309 cell
->state
&= ~FLAG_TRIGGERED
;
1312 int alive_count
= 0;
1314 // Update fish positions
1315 for( int i
= 0; i
< world
.num_fishes
; i
++ )
1317 struct fish
*fish
= &world
.fishes
[i
];
1319 if( fish
->alive
== -1 )
1322 if( fish
->alive
!= 1 )
1325 struct cell
*cell_current
= pcell( fish
->pos
);
1328 if( cell_current
->state
& FLAG_OUTPUT
)
1330 for( int j
= 0; j
< arrlen( world
.io
); j
++ )
1332 struct cell_terminal
*term
= &world
.io
[j
];
1334 if( term
->id
== fish
->pos
[1]*world
.w
+ fish
->pos
[0] )
1336 struct terminal_run
*run
= &term
->runs
[ world
.sim_run
];
1337 if( run
->recv_count
< vg_list_size( run
->recieved
) )
1338 run
->recieved
[ run
->recv_count
++ ] = fish
->payload
;
1348 if( cell_current
->config
== k_cell_type_split
)
1351 fish
->dir
[0] = cell_current
->state
&FLAG_FLIP_FLOP
?1:-1;
1354 if( !(cell_current
->state
& FLAG_TARGETED
) )
1355 cell_current
->state
^= FLAG_FLIP_FLOP
;
1357 else if( cell_current
->config
== k_cell_type_merge
)
1365 struct cell
*cell_next
= pcell( (v2i
){ fish
->pos
[0]+fish
->dir
[0], fish
->pos
[1]+fish
->dir
[1] } );
1366 if( !(cell_next
->state
& (FLAG_CANAL
|FLAG_OUTPUT
)) )
1368 // Try other directions for valid, so down, left, right..
1369 v2i dirs
[] = {{1,0},{-1,0},{0,-1}};
1370 //vg_info( "Trying some other directions...\n" );
1372 for( int j
= 0; j
< vg_list_size(dirs
); j
++ )
1374 if( (dirs
[j
][0] == -fish
->dir
[0]) && (dirs
[j
][1] == -fish
->dir
[1]) )
1377 if( pcell( (v2i
){ fish
->pos
[0]+dirs
[j
][0], fish
->pos
[1]+dirs
[j
][1] } )->state
& (FLAG_CANAL
|FLAG_OUTPUT
) )
1379 fish
->dir
[0] = dirs
[j
][0];
1380 fish
->dir
[1] = dirs
[j
][1];
1386 fish
->pos
[0] += fish
->dir
[0];
1387 fish
->pos
[1] += fish
->dir
[1];
1389 struct cell
*cell_entry
= pcell( fish
->pos
);
1391 if( !(cell_entry
->state
& (FLAG_INPUT
|FLAG_CANAL
|FLAG_OUTPUT
) ))
1397 if( cell_entry
->config
== k_cell_type_split
||
1398 cell_entry
->config
== k_cell_type_ramp_right
||
1399 cell_entry
->config
== k_cell_type_ramp_left
)
1401 // Special death (FALL)
1402 v2_sub( fish
->physics_co
, fish
->physics_v
, fish
->physics_v
);
1403 v2_divs( fish
->physics_v
, vg_time_delta
, fish
->physics_v
);
1406 vg_warn( "Special death (fall)\n" );
1411 if( cell_entry
->config
== k_cell_type_split
)
1413 sfx_set_playrnd( &audio_splitter
, &audio_system_balls_important
, 0, 1 );
1414 cell_entry
->state
|= FLAG_FLIP_ROTATING
;
1422 // Second pass (triggers)
1423 for( int i
= 0; i
< world
.num_fishes
; i
++ )
1425 struct fish
*fish
= &world
.fishes
[i
];
1427 if( fish
->alive
== 1 )
1429 struct cell
*cell_current
= pcell( fish
->pos
);
1431 if( cell_current
->state
& FLAG_IS_TRIGGER
)
1433 int trigger_id
= cell_current
->links
[0]?0:1;
1434 int connection_id
= cell_current
->links
[trigger_id
];
1435 int target_px
= connection_id
% world
.w
;
1436 int target_py
= (connection_id
- target_px
)/world
.w
;
1438 vg_line2( (v2f
){ fish
->pos
[0], fish
->pos
[1] }, (v2f
){ target_px
, target_py
}, 0xffffffff, 0xffffffff );
1440 struct cell
*target_peice
= &world
.data
[ cell_current
->links
[trigger_id
] ];
1442 cell_current
->state
|= FLAG_TRIGGERED
;
1445 target_peice
->state
|= FLAG_FLIP_FLOP
;
1447 target_peice
->state
&= ~FLAG_FLIP_FLOP
;
1452 // Third pass (collisions)
1453 for( int i
= 0; i
< world
.num_fishes
; i
++ )
1455 if( world
.fishes
[i
].alive
== 1 )
1457 for( int j
= i
+1; j
< world
.num_fishes
; j
++ )
1459 if( (world
.fishes
[j
].alive
== 1) && (world
.fishes
[i
].pos
[0] == world
.fishes
[j
].pos
[0]) &&
1460 (world
.fishes
[i
].pos
[1] == world
.fishes
[j
].pos
[1]) )
1462 // Shatter death (+0.5s)
1463 world
.fishes
[i
].alive
= -1;
1464 world
.fishes
[j
].alive
= -1;
1465 world
.fishes
[i
].death_time
= 0.5f
;
1466 world
.fishes
[j
].death_time
= 0.5f
;
1473 for( int i
= 0; i
< arrlen( world
.io
); i
++ )
1475 struct cell_terminal
*term
= &world
.io
[ i
];
1476 int posx
= term
->id
% world
.w
;
1477 int posy
= (term
->id
- posx
)/world
.w
;
1478 int is_input
= world
.data
[ term
->id
].state
& FLAG_INPUT
;
1482 if( world
.sim_frame
< term
->runs
[ world
.sim_run
].condition_count
)
1484 struct fish
*fish
= &world
.fishes
[world
.num_fishes
++];
1485 fish
->pos
[0] = posx
;
1486 fish
->pos
[1] = posy
;
1488 fish
->payload
= term
->runs
[ world
.sim_run
].conditions
[ world
.sim_frame
];
1492 v2i dirs
[] = {{1,0},{-1,0},{0,-1}};
1493 for( int j
= 0; j
< vg_list_size(dirs
); j
++ )
1494 if( pcell( (v2i
){ posx
+dirs
[j
][0], posy
+dirs
[j
][1] } )->state
& FLAG_CANAL
)
1496 fish
->dir
[0] = dirs
[j
][0];
1497 fish
->dir
[1] = dirs
[j
][1];
1510 if( alive_count
== 0 )
1512 world
.completed
= 1;
1514 for( int i
= 0; i
< arrlen( world
.io
); i
++ )
1516 struct cell_terminal
*term
= &world
.io
[ i
];
1517 int is_input
= world
.data
[ term
->id
].state
& FLAG_INPUT
;
1521 struct terminal_run
*run
= &term
->runs
[ world
.sim_run
];
1523 if( run
->recv_count
== run
->condition_count
)
1525 for( int j
= 0; j
< run
->condition_count
; j
++ )
1527 if( run
->recieved
[j
] != run
->conditions
[j
] )
1529 world
.completed
= 0;
1536 world
.completed
= 0;
1542 if( world
.completed
)
1544 if( world
.sim_run
< world
.max_runs
-1 )
1546 vg_success( "Run passed, starting next\n" );
1548 world
.sim_frame
= 0;
1549 world
.sim_start
= vg_time
;
1550 world
.num_fishes
= 0;
1555 vg_success( "Level passed!\n" );
1558 for( int i
= 0; i
< world
.w
*world
.h
; i
++ )
1559 if( world
.data
[ i
].state
& FLAG_CANAL
)
1562 world
.score
= score
;
1563 world
.time
= world
.sim_frame
;
1568 vg_error( "Level failed :(\n" );
1571 // Copy into career data
1572 if( world
.ptr_career_level
)
1574 world
.ptr_career_level
->score
= world
.score
;
1575 world
.ptr_career_level
->time
= world
.time
;
1576 world
.ptr_career_level
->completed
= world
.completed
;
1579 simulation_stop(); // TODO: Async?
1586 float scaled_time
= 0.0f
;
1587 scaled_time
= (vg_time
-world
.sim_start
)*2.0f
;
1588 world
.frame_lerp
= scaled_time
- (float)world
.sim_frame
;
1591 for( int i
= 0; i
< world
.num_fishes
; i
++ )
1593 struct fish
*fish
= &world
.fishes
[i
];
1595 if( fish
->alive
== 0 )
1598 if( fish
->alive
== -1 && (world
.frame_lerp
> fish
->death_time
) )
1599 continue; // Todo: particle thing?
1601 if( fish
->alive
== -2 )
1603 v2_muladds( fish
->physics_v
, (v2f
){ 0.0, -9.8f
}, vg_time_delta
, fish
->physics_v
);
1604 v2_muladds( fish
->physics_co
, fish
->physics_v
, vg_time_delta
, fish
->physics_co
);
1608 struct cell
*cell
= pcell(fish
->pos
);
1611 float t
= world
.frame_lerp
;
1613 v2_copy( fish
->physics_co
, fish
->physics_v
);
1615 switch( cell
->config
)
1618 if( fish
->dir
[0] == 1 )
1623 case 1: curve
= curve_1
; break;
1624 case 4: curve
= curve_4
; break;
1625 case 2: curve
= curve_2
; break;
1626 case 8: curve
= curve_8
; break;
1627 case 3: curve
= curve_3
; break;
1628 case 6: curve
= curve_6
; break;
1629 case 9: curve
= curve_9
; break;
1630 case 12: curve
= curve_12
; break;
1632 if( t
> curve_7_linear_section
)
1634 t
-= curve_7_linear_section
;
1635 t
*= (1.0f
/(1.0f
-curve_7_linear_section
));
1637 curve
= cell
->state
& FLAG_FLIP_FLOP
? curve_7
: curve_7_1
;
1641 default: curve
= NULL
; break;
1647 float t3
= t
* t
* t
;
1649 float cA
= 3.0f
*t2
- 3.0f
*t3
;
1650 float cB
= 3.0f
*t3
- 6.0f
*t2
+ 3.0f
*t
;
1651 float cC
= 3.0f
*t2
- t3
- 3.0f
*t
+ 1.0f
;
1653 fish
->physics_co
[0] = t3
*curve
[3][0] + cA
*curve
[2][0] + cB
*curve
[1][0] + cC
*curve
[0][0];
1654 fish
->physics_co
[1] = t3
*curve
[3][1] + cA
*curve
[2][1] + cB
*curve
[1][1] + cC
*curve
[0][1];
1655 fish
->physics_co
[0] += (float)fish
->pos
[0];
1656 fish
->physics_co
[1] += (float)fish
->pos
[1];
1661 origin
[0] = (float)fish
->pos
[0] + (float)fish
->dir
[0]*-0.5f
+ 0.5f
;
1662 origin
[1] = (float)fish
->pos
[1] + (float)fish
->dir
[1]*-0.5f
+ 0.5f
;
1664 fish
->physics_co
[0] = origin
[0] + (float)fish
->dir
[0]*t
;
1665 fish
->physics_co
[1] = origin
[1] + (float)fish
->dir
[1]*t
;
1672 static void render_tiles( v2i start
, v2i end
, v4f
const regular_colour
, v4f
const selected_colour
)
1674 v2i full_start
= { 0,0 };
1675 v2i full_end
= { world
.w
, world
.h
};
1677 if( !start
|| !end
)
1683 glUniform4fv( SHADER_UNIFORM( shader_tile_main
, "uColour" ), 1, regular_colour
);
1685 for( int y
= start
[1]; y
< end
[1]; y
++ )
1687 for( int x
= start
[0]; x
< end
[0]; x
++ )
1689 struct cell
*cell
= pcell((v2i
){x
,y
});
1690 int selected
= world
.selected
== y
*world
.w
+ x
;
1692 int tile_offsets
[][2] =
1694 {2, 0}, {0, 3}, {0, 2}, {2, 2},
1695 {1, 0}, {2, 3}, {3, 2}, {1, 3},
1696 {3, 1}, {0, 1}, {1, 2}, {2, 1},
1697 {1, 1}, {3, 3}, {2, 1}, {2, 1}
1700 int uv
[2] = { 3, 0 };
1702 if( cell
->state
& (FLAG_CANAL
|FLAG_INPUT
|FLAG_OUTPUT
) )
1704 uv
[0] = tile_offsets
[ cell
->config
][0];
1705 uv
[1] = tile_offsets
[ cell
->config
][1];
1708 glUniform4f( SHADER_UNIFORM( shader_tile_main
, "uOffset" ), (float)x
, (float)y
, uv
[0], uv
[1] );
1711 glUniform4fv( SHADER_UNIFORM( shader_tile_main
, "uColour" ), 1, selected_colour
);
1713 glUniform4fv( SHADER_UNIFORM( shader_tile_main
, "uColour" ), 1, regular_colour
);
1721 static void draw_numbers( v3f coord
, int number
)
1724 v3_copy( coord
, pos
);
1725 int digits
[8]; int i
= 0;
1727 while( number
> 0 && i
< 8 )
1729 digits
[i
++] = number
% 10;
1730 number
= number
/ 10;
1733 for( int j
= 0; j
< i
; j
++ )
1735 glUniform3fv( SHADER_UNIFORM( shader_tile_colour
, "uOffset" ), 1, pos
);
1736 draw_mesh( MESH_NUMBERS_OFFSETS
[digits
[i
-j
-1]][0], MESH_NUMBERS_OFFSETS
[digits
[i
-j
-1]][1] );
1737 pos
[0] += pos
[2] * 0.75f
;
1741 void vg_render(void)
1743 glViewport( 0,0, vg_window_x
, vg_window_y
);
1745 glDisable( GL_DEPTH_TEST
);
1746 glClearColor( 0.369768f
, 0.3654f
, 0.42f
, 1.0f
);
1747 glClear( GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
1749 v4f
const colour_default
= {1.0f
, 1.0f
, 1.0f
, 1.0f
};
1750 v4f
const colour_selected
= {0.90f
, 0.92f
, 1.0f
, 1.0f
};
1752 // TILE SET RENDERING
1753 // todo: just slam everything into a mesh...
1754 // when user modifies a tile the neighbours can be easily uploaded to gpu mem
1756 // Currently we're uploading a fair amount of data every frame anyway.
1757 // NOTE: this is for final optimisations ONLY!
1758 // ======================================================================
1760 use_mesh( &world
.tile
);
1766 SHADER_USE( shader_background
);
1767 glUniformMatrix3fv( SHADER_UNIFORM( shader_background
, "uPv" ), 1, GL_FALSE
, (float *)vg_pv
);
1769 glActiveTexture( GL_TEXTURE0
);
1770 glBindTexture( GL_TEXTURE_2D
, world
.background_data
);
1771 glUniform1i( SHADER_UNIFORM( shader_background
, "uTexMain" ), 0 );
1773 glUniform3f( SHADER_UNIFORM( shader_background
, "uOffset" ), -16, -16, 64 );
1774 glUniform1f( SHADER_UNIFORM( shader_background
, "uVariance" ), 0.02f
);
1776 glActiveTexture( GL_TEXTURE1
);
1777 glBindTexture( GL_TEXTURE_2D
, world
.random_samples
);
1778 glUniform1i( SHADER_UNIFORM( shader_background
, "uSamplerNoise" ), 1 );
1785 SHADER_USE( shader_tile_main
);
1788 m2x2_identity( subtransform
);
1789 glUniformMatrix2fv( SHADER_UNIFORM( shader_tile_main
, "uSubTransform" ), 1, GL_FALSE
, (float *)subtransform
);
1790 glUniformMatrix3fv( SHADER_UNIFORM( shader_tile_main
, "uPv" ), 1, GL_FALSE
, (float *)vg_pv
);
1791 glUniform1f( SHADER_UNIFORM( shader_tile_main
, "uGhost" ), 0.0f
);
1792 glUniform1f( SHADER_UNIFORM( shader_tile_main
, "uForeground" ), 0.0f
);
1795 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
1796 glBlendEquation(GL_FUNC_ADD
);
1799 vg_tex2d_bind( &tex_tile_data
, 0 );
1800 glUniform1i( SHADER_UNIFORM( shader_tile_main
, "uTexGlyphs" ), 0 );
1802 vg_tex2d_bind( &tex_wood
, 1 );
1803 glUniform1i( SHADER_UNIFORM( shader_tile_main
, "uTexWood" ), 1 );
1805 render_tiles( NULL
, NULL
, colour_default
, colour_default
);
1809 SHADER_USE( shader_ball
);
1810 glUniformMatrix3fv( SHADER_UNIFORM( shader_ball
, "uPv" ), 1, GL_FALSE
, (float *)vg_pv
);
1812 vg_tex2d_bind( &tex_ball
, 0 );
1813 glUniform1i( SHADER_UNIFORM( shader_ball
, "uTexMain" ), 0 );
1816 if( world
.simulating
)
1818 for( int i
= 0; i
< world
.num_fishes
; i
++ )
1820 struct fish
*fish
= &world
.fishes
[i
];
1822 if( fish
->alive
== 0 )
1825 if( fish
->alive
== -1 && (world
.frame_lerp
> fish
->death_time
) )
1828 v4f dot_colour
= { 0.0f
, 0.0f
, 0.0f
, 1.0f
};
1829 colour_code_v3( fish
->payload
, dot_colour
);
1831 glUniform3fv( SHADER_UNIFORM( shader_ball
, "uColour" ), 1, dot_colour
);
1832 glUniform2fv( SHADER_UNIFORM( shader_ball
, "uOffset" ), 1, fish
->physics_co
);
1837 SHADER_USE( shader_tile_main
);
1840 vg_tex2d_bind( &tex_tile_data
, 0 );
1841 glUniform1i( SHADER_UNIFORM( shader_tile_main
, "uTexGlyphs" ), 0 );
1843 vg_tex2d_bind( &tex_wood
, 1 );
1844 glUniform1i( SHADER_UNIFORM( shader_tile_main
, "uTexWood" ), 1 );
1846 glUniform1f( SHADER_UNIFORM( shader_tile_main
, "uForeground" ), 1.0f
);
1847 render_tiles( NULL
, NULL
, colour_default
, colour_selected
);
1850 for( int y
= 2; y
< world
.h
-2; y
++ )
1852 for( int x
= 2; x
< world
.w
-2; x
++ )
1854 struct cell
*cell
= pcell((v2i
){x
,y
});
1856 if( cell
->state
& FLAG_CANAL
)
1858 if( cell
->config
== k_cell_type_split
)
1860 float rotation
= cell
->state
& FLAG_FLIP_FLOP
? vg_rad( -45.0f
): vg_rad( 45.0f
);
1862 if( cell
->state
& FLAG_FLIP_ROTATING
)
1864 if( (world
.frame_lerp
> curve_7_linear_section
) )
1866 float const rotation_speed
= 0.4f
;
1867 if( (world
.frame_lerp
< 1.0f
-rotation_speed
) )
1869 float t
= world
.frame_lerp
- curve_7_linear_section
;
1870 t
*= -2.0f
* (1.0f
/(1.0f
-(curve_7_linear_section
+rotation_speed
)));
1880 m2x2_create_rotation( subtransform
, rotation
);
1882 glUniformMatrix2fv( SHADER_UNIFORM( shader_tile_main
, "uSubTransform" ), 1, GL_FALSE
, (float *)subtransform
);
1883 glUniform4f( SHADER_UNIFORM( shader_tile_main
, "uOffset" ), (float)x
, (float)y
+ 0.125f
, cell
->state
& FLAG_TARGETED
? 3.0f
: 0.0f
, 0.0f
);
1891 if( world
.selected
!= -1 && !(world
.data
[ world
.selected
].state
& FLAG_CANAL
) && !id_drag_from
)
1893 v2i new_begin
= { world
.tile_x
- 2, world
.tile_y
- 2 };
1894 v2i new_end
= { world
.tile_x
+ 2, world
.tile_y
+ 2 };
1896 world
.data
[ world
.selected
].state
^= FLAG_CANAL
;
1897 map_reclassify( new_begin
, new_end
, 0 );
1899 m2x2_identity( subtransform
);
1900 glUniform1f( SHADER_UNIFORM( shader_tile_main
, "uGhost" ), 1.0f
);
1901 glUniformMatrix2fv( SHADER_UNIFORM( shader_tile_main
, "uSubTransform" ), 1, GL_FALSE
, (float *)subtransform
);
1902 glUniform2fv( SHADER_UNIFORM( shader_tile_main
, "uMousePos" ), 1, world
.tile_pos
);
1904 render_tiles( new_begin
, new_end
, colour_default
, colour_default
);
1906 world
.data
[ world
.selected
].state
^= FLAG_CANAL
;
1907 map_reclassify( new_begin
, new_end
, 0 );
1910 //glDisable(GL_BLEND);
1912 // Draw connecting wires
1913 glDisable(GL_BLEND
);
1915 SHADER_USE( shader_wire
);
1916 glBindVertexArray( world
.wire
.vao
);
1918 glUniformMatrix3fv( SHADER_UNIFORM( shader_wire
, "uPv" ), 1, GL_FALSE
, (float *)vg_pv
);
1919 glUniform4f( SHADER_UNIFORM( shader_wire
, "uColour" ), 0.2f
, 0.2f
, 0.2f
, 1.0f
);
1923 glUniform1f( SHADER_UNIFORM( shader_wire
, "uCurve" ), 0.4f
);
1924 glUniform3f( SHADER_UNIFORM( shader_wire
, "uStart" ), drag_from_co
[0], drag_from_co
[1], 0.06f
);
1925 glUniform3f( SHADER_UNIFORM( shader_wire
, "uEnd" ), drag_to_co
[0], drag_to_co
[1], 0.06f
);
1926 glDrawElements( GL_TRIANGLES
, world
.wire
.em
, GL_UNSIGNED_SHORT
, (void*)(0) );
1929 float rp_x1
= world
.frame_lerp
*9.0f
;
1930 float rp_x2
= 1.0f
-rp_x1
*expf(1.0f
-rp_x1
)* 0.36f
;
1932 for( int y
= 2; y
< world
.h
-2; y
++ )
1934 for( int x
= 2; x
< world
.w
-2; x
++ )
1936 struct cell
*cell
= pcell((v2i
){x
,y
});
1938 if( cell
->state
& FLAG_CANAL
)
1940 if( cell
->state
& FLAG_IS_TRIGGER
)
1942 int trigger_id
= cell
->links
[0]?0:1;
1944 int x2
= cell
->links
[trigger_id
] % world
.w
;
1945 int y2
= (cell
->links
[trigger_id
] - x2
) / world
.w
;
1950 startpoint
[0] = (float)x2
+ (trigger_id
? 0.75f
: 0.25f
);
1951 startpoint
[1] = (float)y2
+ 0.25f
;
1953 endpoint
[0] = x
+0.5f
;
1954 endpoint
[1] = y
+0.5f
;
1956 glUniform1f( SHADER_UNIFORM( shader_wire
, "uCurve" ), cell
->state
& FLAG_TRIGGERED
? rp_x2
* 0.4f
: 0.4f
);
1957 glUniform3f( SHADER_UNIFORM( shader_wire
, "uStart" ), startpoint
[0], startpoint
[1], 0.04f
);
1958 glUniform3f( SHADER_UNIFORM( shader_wire
, "uEnd" ), endpoint
[0], endpoint
[1], 0.04f
);
1959 glDrawElements( GL_TRIANGLES
, world
.wire
.em
, GL_UNSIGNED_SHORT
, (void*)(0) );
1965 SHADER_USE( shader_tile_colour
);
1966 glUniformMatrix3fv( SHADER_UNIFORM( shader_tile_colour
, "uPv" ), 1, GL_FALSE
, (float *)vg_pv
);
1967 use_mesh( &world
.circle
);
1969 int const filled_start
= 0;
1970 int const filled_count
= 32;
1971 int const empty_start
= 32;
1972 int const empty_count
= 32*2;
1975 for( int i
= 0; i
< arrlen( world
.io
); i
++ )
1977 struct cell_terminal
*term
= &world
.io
[ i
];
1978 int posx
= term
->id
% world
.w
;
1979 int posy
= (term
->id
- posx
)/world
.w
;
1980 int is_input
= world
.data
[ term
->id
].state
& FLAG_INPUT
;
1982 v4f dot_colour
= { 0.0f
, 0.0f
, 0.0f
, 1.0f
};
1984 for( int k
= 0; k
< term
->run_count
; k
++ )
1986 for( int j
= 0; j
< term
->runs
[k
].condition_count
; j
++ )
1988 float y_offset
= is_input
? 1.2f
: -0.2f
;
1989 y_offset
+= (is_input
? 0.2f
: -0.2f
) * (float)k
;
1991 glUniform3f( SHADER_UNIFORM( shader_tile_colour
, "uOffset" ), (float)posx
+ 0.2f
+ 0.2f
* (float)j
, (float)posy
+ y_offset
, 0.1f
);
1995 colour_code_v3( term
->runs
[k
].conditions
[j
], dot_colour
);
1996 glUniform4fv( SHADER_UNIFORM( shader_tile_colour
, "uColour" ), 1, dot_colour
);
1998 // Draw filled if tick not passed, draw empty if empty
1999 if( world
.sim_frame
> j
&& world
.sim_run
>= k
)
2000 draw_mesh( empty_start
, empty_count
);
2002 draw_mesh( filled_start
, filled_count
);
2006 if( term
->runs
[k
].recv_count
> j
)
2008 colour_code_v3( term
->runs
[k
].recieved
[j
], dot_colour
);
2009 v3_muls( dot_colour
, 0.8f
, dot_colour
);
2010 glUniform4fv( SHADER_UNIFORM( shader_tile_colour
, "uColour" ), 1, dot_colour
);
2012 draw_mesh( filled_start
, filled_count
);
2015 colour_code_v3( term
->runs
[k
].conditions
[j
], dot_colour
);
2016 glUniform4fv( SHADER_UNIFORM( shader_tile_colour
, "uColour" ), 1, dot_colour
);
2018 draw_mesh( empty_start
, empty_count
);
2024 if( world
.simulating
)
2026 glUniform4f( SHADER_UNIFORM( shader_tile_colour
, "uColour" ), 0.0f
, 0.0f
, 0.0f
, 1.0f
);
2027 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
);
2028 draw_mesh( filled_start
, filled_count
);
2032 float const score_bright
= 1.25f
;
2033 glUniform4f( SHADER_UNIFORM( shader_tile_colour
, "uColour" ),
2034 0.4f
*score_bright
, 0.39f
*score_bright
, 0.45f
*score_bright
, 1.0f
);
2036 use_mesh( &world
.numbers
);
2037 draw_numbers( (v3f
){ 2.0f
, (float)world
.h
-1.875f
, 0.3333f
}, world
.score
);
2039 // Level selection UI
2040 use_mesh( &world
.circle
);
2041 float ratio
= ((float)vg_window_x
/(float)vg_window_y
);
2043 m3x3f ui_view
= M3X3_IDENTITY
;
2044 m3x3_scale( ui_view
, (v3f
){ 1.0f
, ratio
, 1.0f
} );
2045 glUniformMatrix3fv( SHADER_UNIFORM( shader_tile_colour
, "uPv" ), 1, GL_FALSE
, (float *)ui_view
);
2047 // Calculate mouse in UIsp
2048 v3f mouse_ui_space
= { ((float)vg_mouse
[0] / (float)(vg_window_x
)) * 2.0f
- 1.0f
,
2049 (((float)vg_mouse
[1] / (float)(vg_window_y
)) * 2.0f
- 1.0f
)*(-1.0f
/ratio
), 0.0125f
};
2051 // Get selected level
2052 const float selection_scale
= 0.05f
;
2053 int const level_count
= vg_list_size( level_pack_1
);
2054 int level_select
= -1;
2056 if( mouse_ui_space
[0] <= -0.8f
)
2058 float levels_range
= (float)level_count
*selection_scale
*0.6f
;
2059 float level_offset
= ((-mouse_ui_space
[1] + levels_range
) / levels_range
) * 0.5f
* (float)level_count
;
2060 level_select
= ceilf( level_offset
);
2063 if( level_select
>= 0 && level_select
< vg_list_size( level_pack_1
) )
2065 glUniform4f( SHADER_UNIFORM( shader_tile_colour
, "uColour" ), 0.369768f
, 0.3654f
, 0.42f
, 1.0f
);
2067 use_mesh( &world
.tile
);
2068 glUniform3f( SHADER_UNIFORM( shader_tile_colour
, "uOffset" ),
2070 ((float)level_count
- (float)level_select
* 2.0f
) * selection_scale
* 0.6f
,
2075 use_mesh( &world
.circle
);
2077 if( vg_get_button_down( "primary" ) )
2079 console_changelevel( 1, level_pack_1
+ level_select
);
2083 else mouse_ui_space
[1] = INFINITY
;
2085 glUniform4f( SHADER_UNIFORM( shader_tile_colour
, "uColour" ), 0.4f
, 0.39f
, 0.45f
, 1.0f
);
2088 for( int i
= 0; i
< level_count
; i
++ )
2090 struct career_level
*clevel
= &career
.levels
[i
];
2092 v3f level_ui_space
= {
2094 ((float)level_count
- (float)i
* 2.0f
) * selection_scale
* 0.6f
+ selection_scale
* 0.5f
,
2095 selection_scale
* 0.5f
2098 float scale
= vg_clampf( 1.0f
- fabsf(level_ui_space
[1] - mouse_ui_space
[1]) * 2.0f
, 0.9f
, 1.0f
);
2099 level_ui_space
[2] *= scale
;
2101 glUniform3fv( SHADER_UNIFORM( shader_tile_colour
, "uOffset" ), 1, level_ui_space
);
2103 if( clevel
->completed
)
2104 draw_mesh( filled_start
, filled_count
);
2106 draw_mesh( empty_start
, empty_count
);
2110 use_mesh( &world
.numbers
);
2111 for( int i
= 0; i
< level_count
; i
++ )
2113 struct career_level
*clevel
= &career
.levels
[i
];
2115 v3f level_ui_space
= {
2117 ((float)level_count
- (float)i
* 2.0f
) * selection_scale
* 0.6f
+ selection_scale
* 0.5f
,
2121 if( clevel
->completed
)
2123 draw_numbers( level_ui_space
, clevel
->score
);
2127 //use_mesh( &world.numbers );
2128 //draw_numbers( (v3f){ 0.0f, -0.5f, 0.1f }, 128765 );