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
];
468 if( *c
== '+' || *c
== '-' )
470 struct cell_terminal
*term
= arraddnptr( world
.io
, 1 );
471 term
->id
= cx
+ world
.h
*world
.w
;
473 term
->runs
[0].condition_count
= 0;
475 cell
->state
= *c
== '+'? FLAG_INPUT
: FLAG_OUTPUT
;
478 else if( *c
== '#' ) cell
->state
= FLAG_WALL
;
479 else if( ((u32
)*c
>= (u32
)'A') && ((u32
)*c
<= (u32
)'A'+0xf) )
481 // Canal flag bits (4bit/16 value):
487 cell
->state
= ((u32
)*c
- (u32
)'A') & (FLAG_CANAL
|FLAG_IS_TRIGGER
);
489 if( cell
->state
& FLAG_IS_TRIGGER
)
490 arrpush( links_to_make
, cx
+ world
.h
*world
.w
);
496 else cell
->state
= 0x00;
504 // Update data texture to fill out the background
506 u8 info_buffer
[64*64*4];
507 for( int i
= 0; i
< 64*64; i
++ )
509 u8
*px
= &info_buffer
[i
*4];
516 glBindTexture( GL_TEXTURE_2D
, world
.background_data
);
517 glTexSubImage2D( GL_TEXTURE_2D
, 0, 0, 0, 64, 64, GL_RGBA
, GL_UNSIGNED_BYTE
, info_buffer
);
520 arrfree( links_to_make
);
522 map_reclassify( NULL
, NULL
, 1 );
525 for( int i
= 0; i
< world
.h
*world
.w
; i
++ )
527 struct cell
*src
= &world
.data
[i
];
528 if( src
->state
& FLAG_IS_TRIGGER
)
530 int link_id
= src
->links
[0]?0:1;
531 if( src
->links
[link_id
] <= world
.h
*world
.w
)
533 struct cell
*target
= &world
.data
[ src
->links
[link_id
] ];
534 if( (target
->state
& FLAG_CANAL
) && (target
->config
== k_cell_type_split
) )
536 if( target
->links
[ link_id
] )
538 vg_error( "Link target was already targeted\n" );
544 target
->links
[ link_id
] = i
;
545 target
->state
|= FLAG_TARGETED
;
550 vg_error( "Link target was invalid\n" );
556 vg_error( "Link target out of bounds\n" );
562 vg_success( "Map '%s' loaded! (%u:%u)\n", name
, world
.w
, world
.h
);
566 strncpy( world
.map_name
, name
, vg_list_size( world
.map_name
)-1 );
567 world
.initialzed
= 1;
571 arrfree( links_to_make
);
576 static struct cell
*pcell( v2i pos
)
578 return &world
.data
[ pos
[1]*world
.w
+ pos
[0] ];
581 static void map_serialize( FILE *stream
)
583 for( int y
= 0; y
< world
.h
; y
++ )
585 for( int x
= 0; x
< world
.w
; x
++ )
587 struct cell
*cell
= pcell( (v2i
){ x
, y
} );
589 if( cell
->state
& FLAG_WALL
) fputc( '#', stream
);
590 else if( cell
->state
& FLAG_INPUT
) fputc( '+', stream
);
591 else if( cell
->state
& FLAG_OUTPUT
) fputc( '-', stream
);
592 else if( cell
->state
& (FLAG_CANAL
|FLAG_IS_TRIGGER
|FLAG_RESERVED0
|FLAG_RESERVED1
) )
594 fputc( (cell
->state
& (FLAG_CANAL
|FLAG_IS_TRIGGER
|FLAG_RESERVED0
|FLAG_RESERVED1
)) + (u32
)'A', stream
);
596 else fputc( ' ', stream
);
599 fputc( ';', stream
);
601 int terminal_write_count
= 0;
603 for( int x
= 0; x
< world
.w
; x
++ )
605 for( int i
= 0; i
< arrlen( world
.io
); i
++ )
607 struct cell_terminal
*term
= &world
.io
[i
];
608 if( term
->id
== y
*world
.w
+x
)
610 if( terminal_write_count
)
611 fputc( ',', stream
);
612 terminal_write_count
++;
614 for( int j
= 0; j
< term
->run_count
; j
++ )
616 struct terminal_run
*run
= &term
->runs
[j
];
618 for( int k
= 0; k
< run
->condition_count
; k
++ )
619 fputc( run
->conditions
[k
], stream
);
621 if( j
< term
->run_count
-1 )
622 fputc( ':', stream
);
628 for( int x
= 0; x
< world
.w
; x
++ )
630 struct cell
*cell
= pcell( (v2i
){ x
,y
} );
631 if( cell
->state
& FLAG_IS_TRIGGER
)
633 if( terminal_write_count
)
634 fputc( ',', stream
);
635 terminal_write_count
++;
637 fprintf( stream
, "%d", cell
->links
[0]? -cell
->links
[0]: cell
->links
[1] );
641 fputc( '\n', stream
);
645 int main( int argc
, char *argv
[] )
647 vg_init( argc
, argv
, "Marble Computing | SPACE: Test | LeftClick: Toggle tile | RightClick: Drag wire" );
650 static int console_credits( int argc
, char const *argv
[] )
652 vg_info( "Aknowledgements:\n" );
653 vg_info( " GLFW zlib/libpng glfw.org\n" );
654 vg_info( " miniaudio MIT0 miniaud.io\n" );
655 vg_info( " QOI MIT phoboslab.org\n" );
656 vg_info( " STB library MIT nothings.org\n" );
657 vg_info( " Weiholmir font justfredrik.itch.io\n" );
661 static int console_save_map( int argc
, char const *argv
[] )
663 if( !world
.initialzed
)
665 vg_error( "Tried to save uninitialized map!\n" );
669 char map_path
[ 256 ];
671 strcpy( map_path
, "sav/" );
672 strcat( map_path
, world
.map_name
);
673 strcat( map_path
, ".map" );
675 FILE *test_writer
= fopen( map_path
, "wb" );
678 vg_info( "Saving map to '%s'\n", map_path
);
679 map_serialize( test_writer
);
681 fclose( test_writer
);
686 vg_error( "Unable to open stream for writing\n" );
691 static int console_load_map( int argc
, char const *argv
[] )
693 char map_path
[ 256 ];
698 strcpy( map_path
, "sav/" );
699 strcat( map_path
, argv
[0] );
700 strcat( map_path
, ".map" );
702 char *text_source
= vg_textasset_read( map_path
);
706 strcpy( map_path
, "maps/" );
707 strcat( map_path
, argv
[0] );
708 strcat( map_path
, ".map" );
710 text_source
= vg_textasset_read( map_path
);
715 vg_info( "Loading map: '%s'\n", map_path
);
716 world
.ptr_career_level
= NULL
;
718 if( !map_load( text_source
, argv
[0] ) )
726 for( int i
= 0; i
< vg_list_size( level_pack_1
); i
++ )
728 if( !strcmp( level_pack_1
[i
], argv
[0] ) )
730 world
.ptr_career_level
= career
.levels
+ i
;
739 vg_error( "Missing maps '%s'\n", argv
[0] );
745 vg_error( "Missing argument <map_path>\n" );
750 static void simulation_stop(void)
752 world
.simulating
= 0;
753 world
.num_fishes
= 0;
758 sfx_system_fadeout( &audio_system_balls_rolling
, 44100 );
760 vg_info( "Stopping simulation!\n" );
763 static int console_changelevel( int argc
, char const *argv
[] )
767 // Save current level
768 console_save_map( 0, NULL
);
769 if( console_load_map( argc
, argv
) )
777 vg_error( "Missing argument <map_path>\n" );
785 vg_function_push( (struct vg_cmd
){
786 .name
= "_map_write",
787 .function
= console_save_map
790 vg_function_push( (struct vg_cmd
){
792 .function
= console_load_map
795 vg_function_push( (struct vg_cmd
){
797 .function
= console_changelevel
800 vg_function_push( (struct vg_cmd
){
802 .function
= console_credits
809 0.0f
, 0.0f
, 0.0f
, 1.0f
, 1.0f
, 1.0f
,
810 0.0f
, 0.0f
, 1.0f
, 1.0f
, 1.0f
, 0.0f
,
812 0.0f
, 0.0f
, 0.0f
, 1.0f
, 4.0f
, 1.0f
,
813 0.0f
, 0.0f
, 4.0f
, 1.0f
, 4.0f
, 0.0f
816 init_mesh( &world
.tile
, quad_mesh
, vg_list_size(quad_mesh
) );
821 float circle_mesh
[32*6*3];
822 int res
= vg_list_size( circle_mesh
) / (6*3);
824 for( int i
= 0; i
< res
; i
++ )
826 v2f v0
= { sinf( ((float)i
/(float)res
)*VG_TAUf
), cosf( ((float)i
/(float)res
)*VG_TAUf
) };
827 v2f v1
= { sinf( ((float)(i
+1)/(float)res
)*VG_TAUf
), cosf( ((float)(i
+1)/(float)res
)*VG_TAUf
) };
829 circle_mesh
[ i
*6+0 ] = 0.0f
;
830 circle_mesh
[ i
*6+1 ] = 0.0f
;
832 v2_copy( v0
, circle_mesh
+ 32*6 + i
*12 );
833 v2_muls( v0
, 0.8f
, circle_mesh
+ 32*6 + i
*12+2 );
834 v2_copy( v1
, circle_mesh
+ 32*6 + i
*12+4 );
836 v2_copy( v1
, circle_mesh
+ 32*6 + i
*12+6 );
837 v2_muls( v1
, 0.8f
, circle_mesh
+ 32*6 + i
*12+8 );
838 v2_muls( v0
, 0.8f
, circle_mesh
+ 32*6 + i
*12+10 );
840 v2_copy( v0
, circle_mesh
+ i
*6+4 );
841 v2_copy( v1
, circle_mesh
+ i
*6+2 );
842 v2_copy( v0
, circle_mesh
+i
*6+4 );
843 v2_copy( v1
, circle_mesh
+i
*6+2 );
846 init_mesh( &world
.circle
, circle_mesh
, vg_list_size( circle_mesh
) );
851 init_mesh( &world
.numbers
,
853 vg_list_size( MESH_NUMBERS_BUFFER
)
856 for( int i
= 0; i
< 10; i
++ )
858 vg_info( "offset: %u, length: %u\n", MESH_NUMBERS_OFFSETS
[i
][0], MESH_NUMBERS_OFFSETS
[i
][1] );
864 int const num_segments
= 64;
866 struct mesh_wire
*mw
= &world
.wire
;
868 v2f wire_points
[ num_segments
* 2 ];
869 u16 wire_indices
[ 6*(num_segments
-1) ];
871 for( int i
= 0; i
< num_segments
; i
++ )
873 float l
= (float)i
/ (float)(num_segments
-1);
875 v2_copy( (v2f
){ l
, -0.5f
}, wire_points
[i
*2+0] );
876 v2_copy( (v2f
){ l
, 0.5f
}, wire_points
[i
*2+1] );
878 if( i
< num_segments
-1 )
880 wire_indices
[ i
*6+0 ] = i
*2 + 0;
881 wire_indices
[ i
*6+1 ] = i
*2 + 1;
882 wire_indices
[ i
*6+2 ] = i
*2 + 3;
883 wire_indices
[ i
*6+3 ] = i
*2 + 0;
884 wire_indices
[ i
*6+4 ] = i
*2 + 3;
885 wire_indices
[ i
*6+5 ] = i
*2 + 2;
889 glGenVertexArrays( 1, &mw
->vao
);
890 glGenBuffers( 1, &mw
->vbo
);
891 glGenBuffers( 1, &mw
->ebo
);
892 glBindVertexArray( mw
->vao
);
894 glBindBuffer( GL_ARRAY_BUFFER
, mw
->vbo
);
896 glBufferData( GL_ARRAY_BUFFER
, sizeof( wire_points
), wire_points
, GL_STATIC_DRAW
);
897 glBindVertexArray( mw
->vao
);
899 glBindBuffer( GL_ELEMENT_ARRAY_BUFFER
, mw
->ebo
);
900 glBufferData( GL_ELEMENT_ARRAY_BUFFER
, sizeof( wire_indices
), wire_indices
, GL_STATIC_DRAW
);
903 glVertexAttribPointer( 0, 2, GL_FLOAT
, GL_FALSE
, 2*sizeof(float), (void*)0 );
904 glEnableVertexAttribArray( 0 );
908 mw
->em
= vg_list_size( wire_indices
);
911 // Create info data texture
913 glGenTextures( 1, &world
.background_data
);
914 glBindTexture( GL_TEXTURE_2D
, world
.background_data
);
915 glTexImage2D( GL_TEXTURE_2D
, 0, GL_RGBA
, 64, 64, 0, GL_RGBA
, GL_UNSIGNED_BYTE
, NULL
);
919 // Create random smaples texture
921 u8
*data
= malloc(512*512*2);
922 for( int i
= 0; i
< 512*512*2; i
++ )
923 data
[ i
] = rand()/(RAND_MAX
/255);
925 glGenTextures( 1, &world
.random_samples
);
926 glBindTexture( GL_TEXTURE_2D
, world
.random_samples
);
927 glTexImage2D( GL_TEXTURE_2D
, 0, GL_RG
, 512, 512, 0, GL_RG
, GL_UNSIGNED_BYTE
, data
);
934 resource_load_main();
938 console_load_map( 1, level_pack_1
);
943 console_save_map( 0, NULL
);
946 resource_free_main();
948 glDeleteTextures( 1, &world
.background_data
);
949 glDeleteTextures( 1, &world
.random_samples
);
951 glDeleteVertexArrays( 1, &world
.wire
.vao
);
952 glDeleteBuffers( 1, &world
.wire
.vbo
);
953 glDeleteBuffers( 1, &world
.wire
.ebo
);
955 free_mesh( &world
.tile
);
956 free_mesh( &world
.circle
);
957 free_mesh( &world
.numbers
);
962 static int cell_interactive( v2i co
)
965 if( co
[0] < 2 || co
[0] >= world
.w
-2 || co
[1] < 2 || co
[1] >= world
.h
-2 )
969 if( world
.data
[ world
.w
*co
[1] + co
[0] ].state
& (FLAG_WALL
|FLAG_INPUT
|FLAG_OUTPUT
) )
972 // List of 3x3 configurations that we do not allow
973 static u32 invalid_src
[][9] =
1005 // Statically compile invalid configurations into bitmasks
1006 static u32 invalid
[ vg_list_size(invalid_src
) ];
1008 for( int i
= 0; i
< vg_list_size(invalid_src
); i
++ )
1012 for( int j
= 0; j
< 3; j
++ )
1013 for( int k
= 0; k
< 3; k
++ )
1014 comped
|= invalid_src
[i
][ j
*3+k
] << ((j
*5)+k
);
1016 invalid
[i
] = comped
;
1019 // Extract 5x5 grid surrounding tile
1021 for( int y
= co
[1]-2; y
< co
[1]+3; y
++ )
1022 for( int x
= co
[0]-2; x
< co
[0]+3; x
++ )
1024 struct cell
*cell
= pcell((v2i
){x
,y
});
1026 if( cell
&& (cell
->state
& (FLAG_CANAL
|FLAG_INPUT
|FLAG_OUTPUT
)) )
1027 blob
|= 0x1 << ((y
-(co
[1]-2))*5 + x
-(co
[0]-2));
1030 // Run filter over center 3x3 grid to check for invalid configurations
1031 int kernel
[] = { 0, 1, 2, 5, 6, 7, 10, 11, 12 };
1032 for( int i
= 0; i
< vg_list_size(kernel
); i
++ )
1034 if( blob
& (0x1 << (6+kernel
[i
])) )
1036 u32 window
= blob
>> kernel
[i
];
1038 for( int j
= 0; j
< vg_list_size(invalid
); j
++ )
1039 if((window
& invalid
[j
]) == invalid
[j
])
1047 static void map_reclassify( v2i start
, v2i end
, int update_texbuffer
)
1049 v2i full_start
= { 1,1 };
1050 v2i full_end
= { world
.w
-1, world
.h
-1 };
1052 if( !start
|| !end
)
1059 u8 info_buffer
[64*64*4];
1062 int px0
= vg_max( start
[0], full_start
[0] ),
1063 px1
= vg_min( end
[0], full_end
[0] ),
1064 py0
= vg_max( start
[1], full_start
[1] ),
1065 py1
= vg_min( end
[1], full_end
[1] );
1067 for( int y
= py0
; y
< py1
; y
++ )
1069 for( int x
= px0
; x
< px1
; x
++ )
1071 struct cell
*cell
= pcell((v2i
){x
,y
});
1073 v2i dirs
[] = {{1,0},{0,1},{-1,0},{0,-1}};
1078 if( cell
->state
& (FLAG_CANAL
|FLAG_INPUT
|FLAG_OUTPUT
) )
1080 for( int i
= 0; i
< vg_list_size( dirs
); i
++ )
1082 struct cell
*neighbour
= pcell((v2i
){x
+dirs
[i
][0], y
+dirs
[i
][1]});
1083 if( neighbour
->state
& (FLAG_CANAL
|FLAG_INPUT
|FLAG_OUTPUT
) )
1091 if( cell
->state
& FLAG_WALL
)
1097 pcell((v2i
){x
,y
})->config
= config
;
1099 u8
*info_px
= &info_buffer
[ (pixel_id
++)*4 ];
1100 info_px
[0] = height
;
1101 info_px
[1] = cell
->state
& FLAG_WALL
? 0: 255;
1107 ((cell
->state
& FLAG_IS_TRIGGER
) && (cell
->config
== 0xF || cell
->config
== k_cell_type_split
)) ||
1108 ((cell
->state
& FLAG_TARGETED
) && (cell
->config
!= k_cell_type_split
))
1109 ) && update_texbuffer
1111 cell
->state
&= ~(FLAG_TARGETED
|FLAG_IS_TRIGGER
);
1112 for( u32 i
= 0; i
< 2; i
++ )
1114 if( cell
->links
[i
] )
1116 struct cell
*other_ptr
= &world
.data
[ cell
->links
[i
] ];
1117 other_ptr
->links
[ i
] = 0;
1118 other_ptr
->state
&= ~FLAG_IS_TRIGGER
;
1120 if( other_ptr
->links
[ i
^ 0x1 ] == 0 )
1121 other_ptr
->state
&= ~FLAG_TARGETED
;
1131 if( update_texbuffer
)
1133 glBindTexture( GL_TEXTURE_2D
, world
.background_data
);
1134 glTexSubImage2D( GL_TEXTURE_2D
, 0, px0
+ 16, py0
+ 16, px1
-px0
, py1
-py0
, GL_RGBA
, GL_UNSIGNED_BYTE
, info_buffer
);
1139 v2f
const curve_3
[] = {{0.5f
,1.0f
},{0.5f
,0.625f
},{0.625f
,0.5f
},{1.0f
,0.5f
}};
1140 v2f
const curve_6
[] = {{0.5f
,1.0f
},{0.5f
,0.625f
},{0.375f
,0.5f
},{0.0f
,0.5f
}};
1141 v2f
const curve_9
[] = {{1.0f
,0.5f
},{0.625f
,0.5f
},{0.5f
,0.375f
},{0.5f
,0.0f
}};
1142 v2f
const curve_12
[]= {{0.0f
,0.5f
},{0.375f
,0.5f
},{0.5f
,0.375f
},{0.5f
,0.0f
}};
1144 v2f
const curve_1
[] = {{1.0f
,0.5f
},{0.8f
,0.5f
},{0.3f
,0.5f
},{0.2f
,0.5f
}};
1145 v2f
const curve_4
[] = {{0.0f
,0.5f
},{0.3f
,0.5f
},{0.5f
,0.5f
},{0.8f
,0.5f
}};
1146 v2f
const curve_2
[] = {{0.5f
,1.0f
},{0.5f
,0.8f
},{0.5f
,0.3f
},{0.5f
,0.2f
}};
1147 v2f
const curve_8
[] = {{0.5f
,0.8f
},{0.5f
,0.5f
},{0.5f
,0.3f
},{0.5f
,0.0f
}};
1149 v2f
const curve_7
[] = {{0.5f
,0.8438f
},{0.875f
,0.8438f
},{0.625f
,0.5f
},{1.0f
,0.5f
}};
1150 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
}};
1152 float const curve_7_linear_section
= 0.1562f
;
1154 u16 id_drag_from
= 0;
1158 void vg_update(void)
1160 // Fit within screen
1162 float r1
= (float)vg_window_y
/ (float)vg_window_x
,
1163 r2
= (float)world
.h
/ (float)world
.w
,
1166 size
= ( r2
< r1
? (float)world
.w
* 0.5f
: ((float)world
.h
* 0.5f
) / r1
) + 2.5f
;
1167 m3x3_projection( m_projection
, -size
, size
, -size
*r1
, size
*r1
);
1170 origin
[0] = floorf( -0.5f
* world
.w
);
1171 origin
[1] = floorf( -0.5f
* world
.h
);
1174 m3x3_identity( m_view
);
1175 m3x3_translate( m_view
, origin
);
1176 m3x3_mul( m_projection
, m_view
, vg_pv
);
1177 vg_projection_update();
1180 v2_copy( vg_mouse_ws
, world
.tile_pos
);
1182 world
.tile_x
= floorf( world
.tile_pos
[0] );
1183 world
.tile_y
= floorf( world
.tile_pos
[1] );
1186 if( !world
.simulating
)
1188 v2_copy( vg_mouse_ws
, drag_to_co
);
1190 if( cell_interactive( (v2i
){ world
.tile_x
, world
.tile_y
} ))
1192 world
.selected
= world
.tile_y
* world
.w
+ world
.tile_x
;
1194 static u32 modify_state
= 0;
1196 struct cell
*cell_ptr
= &world
.data
[world
.selected
];
1198 if( vg_get_button_down("primary") )
1200 modify_state
= (cell_ptr
->state
& FLAG_CANAL
) ^ FLAG_CANAL
;
1203 if( vg_get_button("primary") && ((cell_ptr
->state
& FLAG_CANAL
) != modify_state
) )
1205 cell_ptr
->state
&= ~FLAG_CANAL
;
1206 cell_ptr
->state
|= modify_state
;
1208 if( cell_ptr
->state
& FLAG_CANAL
)
1210 cell_ptr
->links
[0] = 0;
1211 cell_ptr
->links
[1] = 0;
1213 sfx_set_playrnd( &audio_tile_mod
, &audio_system_sfx
, 3, 6 );
1218 sfx_set_playrnd( &audio_tile_mod
, &audio_system_sfx
, 0, 3 );
1222 map_reclassify( (v2i
){ world
.tile_x
-2, world
.tile_y
-2 },
1223 (v2i
){ world
.tile_x
+2, world
.tile_y
+2 }, 1 );
1226 if( vg_get_button_down("secondary") && !(cell_ptr
->config
== k_cell_type_split
) )
1228 id_drag_from
= world
.selected
;
1229 drag_from_co
[0] = world
.tile_x
+ 0.5f
;
1230 drag_from_co
[1] = world
.tile_y
+ 0.5f
;
1233 if( id_drag_from
&& (cell_ptr
->config
== k_cell_type_split
) )
1235 float local_x
= vg_mouse_ws
[0] - (float)world
.tile_x
;
1236 drag_to_co
[0] = (float)world
.tile_x
+ (local_x
> 0.5f
? 0.75f
: 0.25f
);
1237 drag_to_co
[1] = (float)world
.tile_y
+ 0.25f
;
1239 if( vg_get_button_up("secondary") )
1241 struct cell
*drag_ptr
= &world
.data
[id_drag_from
];
1242 u32 link_id
= local_x
> 0.5f
? 1: 0;
1244 // Cleanup existing connections
1245 if( cell_ptr
->links
[ link_id
] )
1247 vg_warn( "Destroying existing connection on link %u (%hu)\n", link_id
, cell_ptr
->links
[ link_id
] );
1249 struct cell
*current_connection
= &world
.data
[ cell_ptr
->links
[ link_id
]];
1250 current_connection
->state
&= ~FLAG_IS_TRIGGER
;
1251 current_connection
->links
[ link_id
] = 0;
1254 if( drag_ptr
->links
[ link_id
^ 0x1 ] )
1256 vg_warn( "Destroying alternate link %u (%hu)\n", link_id
^ 0x1, drag_ptr
->links
[ link_id
^ 0x1 ] );
1258 struct cell
*current_connection
= &world
.data
[ drag_ptr
->links
[ link_id
^ 0x1 ]];
1259 if( !current_connection
->links
[ link_id
] )
1260 current_connection
->state
&= ~FLAG_TARGETED
;
1262 current_connection
->links
[ link_id
^ 0x1 ] = 0;
1263 drag_ptr
->links
[ link_id
^ 0x1 ] = 0;
1266 // Create the new connection
1267 vg_success( "Creating connection on link %u (%hu)\n", link_id
, id_drag_from
);
1269 cell_ptr
->links
[ link_id
] = id_drag_from
;
1270 drag_ptr
->links
[ link_id
] = world
.selected
;
1272 cell_ptr
->state
|= FLAG_TARGETED
;
1273 drag_ptr
->state
|= FLAG_IS_TRIGGER
;
1279 world
.selected
= -1;
1281 if( !(vg_get_button("secondary") && id_drag_from
) )
1286 world
.selected
= -1;
1290 // Simulation stop/start
1291 if( vg_get_button_down("go") )
1293 if( world
.simulating
)
1299 vg_success( "Starting simulation!\n" );
1301 sfx_set_playrnd( &audio_rolls
, &audio_system_balls_rolling
, 0, 1 );
1303 world
.simulating
= 1;
1304 world
.num_fishes
= 0;
1305 world
.sim_frame
= 0;
1306 world
.sim_start
= vg_time
;
1309 for( int i
= 0; i
< world
.w
*world
.h
; i
++ )
1310 world
.data
[ i
].state
&= ~FLAG_FLIP_FLOP
;
1317 if( world
.simulating
)
1319 while( world
.sim_frame
< (int)((vg_time
-world
.sim_start
)*2.0f
) )
1321 //vg_info( "frame: %u\n", world.sim_frame );
1322 sfx_set_playrnd( &audio_random
, &audio_system_balls_switching
, 0, 9 );
1324 // Update splitter deltas
1325 for( int i
= 0; i
< world
.h
*world
.w
; i
++ )
1327 struct cell
*cell
= &world
.data
[i
];
1328 if( cell
->config
== k_cell_type_split
)
1330 cell
->state
&= ~FLAG_FLIP_ROTATING
;
1332 if( cell
->state
& FLAG_IS_TRIGGER
)
1333 cell
->state
&= ~FLAG_TRIGGERED
;
1336 int alive_count
= 0;
1338 // Update fish positions
1339 for( int i
= 0; i
< world
.num_fishes
; i
++ )
1341 struct fish
*fish
= &world
.fishes
[i
];
1343 if( fish
->alive
== -1 )
1346 if( fish
->alive
!= 1 )
1349 struct cell
*cell_current
= pcell( fish
->pos
);
1352 if( cell_current
->state
& FLAG_OUTPUT
)
1354 for( int j
= 0; j
< arrlen( world
.io
); j
++ )
1356 struct cell_terminal
*term
= &world
.io
[j
];
1358 if( term
->id
== fish
->pos
[1]*world
.w
+ fish
->pos
[0] )
1360 struct terminal_run
*run
= &term
->runs
[ world
.sim_run
];
1361 if( run
->recv_count
< vg_list_size( run
->recieved
) )
1362 run
->recieved
[ run
->recv_count
++ ] = fish
->payload
;
1372 if( cell_current
->config
== k_cell_type_split
)
1375 fish
->dir
[0] = cell_current
->state
&FLAG_FLIP_FLOP
?1:-1;
1378 if( !(cell_current
->state
& FLAG_TARGETED
) )
1379 cell_current
->state
^= FLAG_FLIP_FLOP
;
1381 else if( cell_current
->config
== k_cell_type_merge
)
1389 struct cell
*cell_next
= pcell( (v2i
){ fish
->pos
[0]+fish
->dir
[0], fish
->pos
[1]+fish
->dir
[1] } );
1390 if( !(cell_next
->state
& (FLAG_CANAL
|FLAG_OUTPUT
)) )
1392 // Try other directions for valid, so down, left, right..
1393 v2i dirs
[] = {{1,0},{-1,0},{0,-1}};
1394 //vg_info( "Trying some other directions...\n" );
1396 for( int j
= 0; j
< vg_list_size(dirs
); j
++ )
1398 if( (dirs
[j
][0] == -fish
->dir
[0]) && (dirs
[j
][1] == -fish
->dir
[1]) )
1401 if( pcell( (v2i
){ fish
->pos
[0]+dirs
[j
][0], fish
->pos
[1]+dirs
[j
][1] } )->state
& (FLAG_CANAL
|FLAG_OUTPUT
) )
1403 fish
->dir
[0] = dirs
[j
][0];
1404 fish
->dir
[1] = dirs
[j
][1];
1410 fish
->pos
[0] += fish
->dir
[0];
1411 fish
->pos
[1] += fish
->dir
[1];
1413 struct cell
*cell_entry
= pcell( fish
->pos
);
1415 if( !(cell_entry
->state
& (FLAG_INPUT
|FLAG_CANAL
|FLAG_OUTPUT
) ))
1421 if( cell_entry
->config
== k_cell_type_split
||
1422 cell_entry
->config
== k_cell_type_ramp_right
||
1423 cell_entry
->config
== k_cell_type_ramp_left
)
1425 // Special death (FALL)
1426 v2_sub( fish
->physics_co
, fish
->physics_v
, fish
->physics_v
);
1427 v2_divs( fish
->physics_v
, vg_time_delta
, fish
->physics_v
);
1430 vg_warn( "Special death (fall)\n" );
1435 if( cell_entry
->config
== k_cell_type_split
)
1437 sfx_set_playrnd( &audio_splitter
, &audio_system_balls_important
, 0, 1 );
1438 cell_entry
->state
|= FLAG_FLIP_ROTATING
;
1446 // Second pass (triggers)
1447 for( int i
= 0; i
< world
.num_fishes
; i
++ )
1449 struct fish
*fish
= &world
.fishes
[i
];
1451 if( fish
->alive
== 1 )
1453 struct cell
*cell_current
= pcell( fish
->pos
);
1455 if( cell_current
->state
& FLAG_IS_TRIGGER
)
1457 int trigger_id
= cell_current
->links
[0]?0:1;
1458 int connection_id
= cell_current
->links
[trigger_id
];
1459 int target_px
= connection_id
% world
.w
;
1460 int target_py
= (connection_id
- target_px
)/world
.w
;
1462 vg_line2( (v2f
){ fish
->pos
[0], fish
->pos
[1] }, (v2f
){ target_px
, target_py
}, 0xffffffff, 0xffffffff );
1464 struct cell
*target_peice
= &world
.data
[ cell_current
->links
[trigger_id
] ];
1466 cell_current
->state
|= FLAG_TRIGGERED
;
1469 target_peice
->state
|= FLAG_FLIP_FLOP
;
1471 target_peice
->state
&= ~FLAG_FLIP_FLOP
;
1476 // Third pass (collisions)
1477 for( int i
= 0; i
< world
.num_fishes
; i
++ )
1479 if( world
.fishes
[i
].alive
== 1 )
1481 for( int j
= i
+1; j
< world
.num_fishes
; j
++ )
1483 if( (world
.fishes
[j
].alive
== 1) && (world
.fishes
[i
].pos
[0] == world
.fishes
[j
].pos
[0]) &&
1484 (world
.fishes
[i
].pos
[1] == world
.fishes
[j
].pos
[1]) )
1486 // Shatter death (+0.5s)
1487 world
.fishes
[i
].alive
= -1;
1488 world
.fishes
[j
].alive
= -1;
1489 world
.fishes
[i
].death_time
= 0.5f
;
1490 world
.fishes
[j
].death_time
= 0.5f
;
1497 for( int i
= 0; i
< arrlen( world
.io
); i
++ )
1499 struct cell_terminal
*term
= &world
.io
[ i
];
1500 int posx
= term
->id
% world
.w
;
1501 int posy
= (term
->id
- posx
)/world
.w
;
1502 int is_input
= world
.data
[ term
->id
].state
& FLAG_INPUT
;
1506 if( world
.sim_frame
< term
->runs
[ world
.sim_run
].condition_count
)
1508 struct fish
*fish
= &world
.fishes
[world
.num_fishes
++];
1509 fish
->pos
[0] = posx
;
1510 fish
->pos
[1] = posy
;
1512 fish
->payload
= term
->runs
[ world
.sim_run
].conditions
[ world
.sim_frame
];
1516 v2i dirs
[] = {{1,0},{-1,0},{0,-1}};
1517 for( int j
= 0; j
< vg_list_size(dirs
); j
++ )
1518 if( pcell( (v2i
){ posx
+dirs
[j
][0], posy
+dirs
[j
][1] } )->state
& FLAG_CANAL
)
1520 fish
->dir
[0] = dirs
[j
][0];
1521 fish
->dir
[1] = dirs
[j
][1];
1534 if( alive_count
== 0 )
1536 world
.completed
= 1;
1538 for( int i
= 0; i
< arrlen( world
.io
); i
++ )
1540 struct cell_terminal
*term
= &world
.io
[ i
];
1541 int is_input
= world
.data
[ term
->id
].state
& FLAG_INPUT
;
1545 struct terminal_run
*run
= &term
->runs
[ world
.sim_run
];
1547 if( run
->recv_count
== run
->condition_count
)
1549 for( int j
= 0; j
< run
->condition_count
; j
++ )
1551 if( run
->recieved
[j
] != run
->conditions
[j
] )
1553 world
.completed
= 0;
1560 world
.completed
= 0;
1566 if( world
.completed
)
1568 if( world
.sim_run
< world
.max_runs
-1 )
1570 vg_success( "Run passed, starting next\n" );
1572 world
.sim_frame
= 0;
1573 world
.sim_start
= vg_time
;
1574 world
.num_fishes
= 0;
1579 vg_success( "Level passed!\n" );
1582 for( int i
= 0; i
< world
.w
*world
.h
; i
++ )
1583 if( world
.data
[ i
].state
& FLAG_CANAL
)
1586 world
.score
= score
;
1587 world
.time
= world
.sim_frame
;
1592 vg_error( "Level failed :(\n" );
1595 // Copy into career data
1596 if( world
.ptr_career_level
)
1598 world
.ptr_career_level
->score
= world
.score
;
1599 world
.ptr_career_level
->time
= world
.time
;
1600 world
.ptr_career_level
->completed
= world
.completed
;
1603 simulation_stop(); // TODO: Async?
1610 float scaled_time
= 0.0f
;
1611 scaled_time
= (vg_time
-world
.sim_start
)*2.0f
;
1612 world
.frame_lerp
= scaled_time
- (float)world
.sim_frame
;
1615 for( int i
= 0; i
< world
.num_fishes
; i
++ )
1617 struct fish
*fish
= &world
.fishes
[i
];
1619 if( fish
->alive
== 0 )
1622 if( fish
->alive
== -1 && (world
.frame_lerp
> fish
->death_time
) )
1623 continue; // Todo: particle thing?
1625 if( fish
->alive
== -2 )
1627 v2_muladds( fish
->physics_v
, (v2f
){ 0.0, -9.8f
}, vg_time_delta
, fish
->physics_v
);
1628 v2_muladds( fish
->physics_co
, fish
->physics_v
, vg_time_delta
, fish
->physics_co
);
1632 struct cell
*cell
= pcell(fish
->pos
);
1635 float t
= world
.frame_lerp
;
1637 v2_copy( fish
->physics_co
, fish
->physics_v
);
1639 switch( cell
->config
)
1642 if( fish
->dir
[0] == 1 )
1647 case 1: curve
= curve_1
; break;
1648 case 4: curve
= curve_4
; break;
1649 case 2: curve
= curve_2
; break;
1650 case 8: curve
= curve_8
; break;
1651 case 3: curve
= curve_3
; break;
1652 case 6: curve
= curve_6
; break;
1653 case 9: curve
= curve_9
; break;
1654 case 12: curve
= curve_12
; break;
1656 if( t
> curve_7_linear_section
)
1658 t
-= curve_7_linear_section
;
1659 t
*= (1.0f
/(1.0f
-curve_7_linear_section
));
1661 curve
= cell
->state
& FLAG_FLIP_FLOP
? curve_7
: curve_7_1
;
1665 default: curve
= NULL
; break;
1671 float t3
= t
* t
* t
;
1673 float cA
= 3.0f
*t2
- 3.0f
*t3
;
1674 float cB
= 3.0f
*t3
- 6.0f
*t2
+ 3.0f
*t
;
1675 float cC
= 3.0f
*t2
- t3
- 3.0f
*t
+ 1.0f
;
1677 fish
->physics_co
[0] = t3
*curve
[3][0] + cA
*curve
[2][0] + cB
*curve
[1][0] + cC
*curve
[0][0];
1678 fish
->physics_co
[1] = t3
*curve
[3][1] + cA
*curve
[2][1] + cB
*curve
[1][1] + cC
*curve
[0][1];
1679 fish
->physics_co
[0] += (float)fish
->pos
[0];
1680 fish
->physics_co
[1] += (float)fish
->pos
[1];
1685 origin
[0] = (float)fish
->pos
[0] + (float)fish
->dir
[0]*-0.5f
+ 0.5f
;
1686 origin
[1] = (float)fish
->pos
[1] + (float)fish
->dir
[1]*-0.5f
+ 0.5f
;
1688 fish
->physics_co
[0] = origin
[0] + (float)fish
->dir
[0]*t
;
1689 fish
->physics_co
[1] = origin
[1] + (float)fish
->dir
[1]*t
;
1696 static void render_tiles( v2i start
, v2i end
, v4f
const regular_colour
, v4f
const selected_colour
)
1698 v2i full_start
= { 0,0 };
1699 v2i full_end
= { world
.w
, world
.h
};
1701 if( !start
|| !end
)
1707 glUniform4fv( SHADER_UNIFORM( shader_tile_main
, "uColour" ), 1, regular_colour
);
1709 for( int y
= start
[1]; y
< end
[1]; y
++ )
1711 for( int x
= start
[0]; x
< end
[0]; x
++ )
1713 struct cell
*cell
= pcell((v2i
){x
,y
});
1714 int selected
= world
.selected
== y
*world
.w
+ x
;
1716 int tile_offsets
[][2] =
1718 {2, 0}, {0, 3}, {0, 2}, {2, 2},
1719 {1, 0}, {2, 3}, {3, 2}, {1, 3},
1720 {3, 1}, {0, 1}, {1, 2}, {2, 1},
1721 {1, 1}, {3, 3}, {2, 1}, {2, 1}
1724 int uv
[2] = { 3, 0 };
1726 if( cell
->state
& (FLAG_CANAL
|FLAG_INPUT
|FLAG_OUTPUT
) )
1728 uv
[0] = tile_offsets
[ cell
->config
][0];
1729 uv
[1] = tile_offsets
[ cell
->config
][1];
1732 glUniform4f( SHADER_UNIFORM( shader_tile_main
, "uOffset" ), (float)x
, (float)y
, uv
[0], uv
[1] );
1735 glUniform4fv( SHADER_UNIFORM( shader_tile_main
, "uColour" ), 1, selected_colour
);
1737 glUniform4fv( SHADER_UNIFORM( shader_tile_main
, "uColour" ), 1, regular_colour
);
1745 static void draw_numbers( v3f coord
, int number
)
1748 v3_copy( coord
, pos
);
1749 int digits
[8]; int i
= 0;
1751 while( number
> 0 && i
< 8 )
1753 digits
[i
++] = number
% 10;
1754 number
= number
/ 10;
1757 for( int j
= 0; j
< i
; j
++ )
1759 glUniform3fv( SHADER_UNIFORM( shader_tile_colour
, "uOffset" ), 1, pos
);
1760 draw_mesh( MESH_NUMBERS_OFFSETS
[digits
[i
-j
-1]][0], MESH_NUMBERS_OFFSETS
[digits
[i
-j
-1]][1] );
1761 pos
[0] += pos
[2] * 0.75f
;
1765 void vg_render(void)
1767 glViewport( 0,0, vg_window_x
, vg_window_y
);
1769 glDisable( GL_DEPTH_TEST
);
1770 glClearColor( 0.369768f
, 0.3654f
, 0.42f
, 1.0f
);
1771 glClear( GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
1773 v4f
const colour_default
= {1.0f
, 1.0f
, 1.0f
, 1.0f
};
1774 v4f
const colour_selected
= {0.90f
, 0.92f
, 1.0f
, 1.0f
};
1776 // TILE SET RENDERING
1777 // todo: just slam everything into a mesh...
1778 // when user modifies a tile the neighbours can be easily uploaded to gpu mem
1780 // Currently we're uploading a fair amount of data every frame anyway.
1781 // NOTE: this is for final optimisations ONLY!
1782 // ======================================================================
1784 use_mesh( &world
.tile
);
1790 SHADER_USE( shader_background
);
1791 glUniformMatrix3fv( SHADER_UNIFORM( shader_background
, "uPv" ), 1, GL_FALSE
, (float *)vg_pv
);
1793 glActiveTexture( GL_TEXTURE0
);
1794 glBindTexture( GL_TEXTURE_2D
, world
.background_data
);
1795 glUniform1i( SHADER_UNIFORM( shader_background
, "uTexMain" ), 0 );
1797 glUniform3f( SHADER_UNIFORM( shader_background
, "uOffset" ), -16, -16, 64 );
1798 glUniform1f( SHADER_UNIFORM( shader_background
, "uVariance" ), 0.02f
);
1800 glActiveTexture( GL_TEXTURE1
);
1801 glBindTexture( GL_TEXTURE_2D
, world
.random_samples
);
1802 glUniform1i( SHADER_UNIFORM( shader_background
, "uSamplerNoise" ), 1 );
1809 SHADER_USE( shader_tile_main
);
1812 m2x2_identity( subtransform
);
1813 glUniformMatrix2fv( SHADER_UNIFORM( shader_tile_main
, "uSubTransform" ), 1, GL_FALSE
, (float *)subtransform
);
1814 glUniformMatrix3fv( SHADER_UNIFORM( shader_tile_main
, "uPv" ), 1, GL_FALSE
, (float *)vg_pv
);
1815 glUniform1f( SHADER_UNIFORM( shader_tile_main
, "uGhost" ), 0.0f
);
1816 glUniform1f( SHADER_UNIFORM( shader_tile_main
, "uForeground" ), 0.0f
);
1819 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
1820 glBlendEquation(GL_FUNC_ADD
);
1823 vg_tex2d_bind( &tex_tile_data
, 0 );
1824 glUniform1i( SHADER_UNIFORM( shader_tile_main
, "uTexGlyphs" ), 0 );
1826 vg_tex2d_bind( &tex_wood
, 1 );
1827 glUniform1i( SHADER_UNIFORM( shader_tile_main
, "uTexWood" ), 1 );
1829 render_tiles( NULL
, NULL
, colour_default
, colour_default
);
1833 SHADER_USE( shader_ball
);
1834 glUniformMatrix3fv( SHADER_UNIFORM( shader_ball
, "uPv" ), 1, GL_FALSE
, (float *)vg_pv
);
1836 vg_tex2d_bind( &tex_ball
, 0 );
1837 glUniform1i( SHADER_UNIFORM( shader_ball
, "uTexMain" ), 0 );
1840 if( world
.simulating
)
1842 for( int i
= 0; i
< world
.num_fishes
; i
++ )
1844 struct fish
*fish
= &world
.fishes
[i
];
1846 if( fish
->alive
== 0 )
1849 if( fish
->alive
== -1 && (world
.frame_lerp
> fish
->death_time
) )
1852 v4f dot_colour
= { 0.0f
, 0.0f
, 0.0f
, 1.0f
};
1853 colour_code_v3( fish
->payload
, dot_colour
);
1855 glUniform3fv( SHADER_UNIFORM( shader_ball
, "uColour" ), 1, dot_colour
);
1856 glUniform2fv( SHADER_UNIFORM( shader_ball
, "uOffset" ), 1, fish
->physics_co
);
1861 SHADER_USE( shader_tile_main
);
1864 vg_tex2d_bind( &tex_tile_data
, 0 );
1865 glUniform1i( SHADER_UNIFORM( shader_tile_main
, "uTexGlyphs" ), 0 );
1867 vg_tex2d_bind( &tex_wood
, 1 );
1868 glUniform1i( SHADER_UNIFORM( shader_tile_main
, "uTexWood" ), 1 );
1870 glUniform1f( SHADER_UNIFORM( shader_tile_main
, "uForeground" ), 1.0f
);
1871 render_tiles( NULL
, NULL
, colour_default
, colour_selected
);
1874 for( int y
= 2; y
< world
.h
-2; y
++ )
1876 for( int x
= 2; x
< world
.w
-2; x
++ )
1878 struct cell
*cell
= pcell((v2i
){x
,y
});
1880 if( cell
->state
& FLAG_CANAL
)
1882 if( cell
->config
== k_cell_type_split
)
1884 float rotation
= cell
->state
& FLAG_FLIP_FLOP
? vg_rad( -45.0f
): vg_rad( 45.0f
);
1886 if( cell
->state
& FLAG_FLIP_ROTATING
)
1888 if( (world
.frame_lerp
> curve_7_linear_section
) )
1890 float const rotation_speed
= 0.4f
;
1891 if( (world
.frame_lerp
< 1.0f
-rotation_speed
) )
1893 float t
= world
.frame_lerp
- curve_7_linear_section
;
1894 t
*= -2.0f
* (1.0f
/(1.0f
-(curve_7_linear_section
+rotation_speed
)));
1904 m2x2_create_rotation( subtransform
, rotation
);
1906 glUniformMatrix2fv( SHADER_UNIFORM( shader_tile_main
, "uSubTransform" ), 1, GL_FALSE
, (float *)subtransform
);
1907 glUniform4f( SHADER_UNIFORM( shader_tile_main
, "uOffset" ), (float)x
, (float)y
+ 0.125f
, cell
->state
& FLAG_TARGETED
? 3.0f
: 0.0f
, 0.0f
);
1915 if( world
.selected
!= -1 && !(world
.data
[ world
.selected
].state
& FLAG_CANAL
) && !id_drag_from
)
1917 v2i new_begin
= { world
.tile_x
- 2, world
.tile_y
- 2 };
1918 v2i new_end
= { world
.tile_x
+ 2, world
.tile_y
+ 2 };
1920 world
.data
[ world
.selected
].state
^= FLAG_CANAL
;
1921 map_reclassify( new_begin
, new_end
, 0 );
1923 m2x2_identity( subtransform
);
1924 glUniform1f( SHADER_UNIFORM( shader_tile_main
, "uGhost" ), 1.0f
);
1925 glUniformMatrix2fv( SHADER_UNIFORM( shader_tile_main
, "uSubTransform" ), 1, GL_FALSE
, (float *)subtransform
);
1926 glUniform2fv( SHADER_UNIFORM( shader_tile_main
, "uMousePos" ), 1, world
.tile_pos
);
1928 render_tiles( new_begin
, new_end
, colour_default
, colour_default
);
1930 world
.data
[ world
.selected
].state
^= FLAG_CANAL
;
1931 map_reclassify( new_begin
, new_end
, 0 );
1934 //glDisable(GL_BLEND);
1936 // Draw connecting wires
1937 glDisable(GL_BLEND
);
1939 SHADER_USE( shader_wire
);
1940 glBindVertexArray( world
.wire
.vao
);
1942 glUniformMatrix3fv( SHADER_UNIFORM( shader_wire
, "uPv" ), 1, GL_FALSE
, (float *)vg_pv
);
1943 glUniform4f( SHADER_UNIFORM( shader_wire
, "uColour" ), 0.2f
, 0.2f
, 0.2f
, 1.0f
);
1947 glUniform1f( SHADER_UNIFORM( shader_wire
, "uCurve" ), 0.4f
);
1948 glUniform3f( SHADER_UNIFORM( shader_wire
, "uStart" ), drag_from_co
[0], drag_from_co
[1], 0.06f
);
1949 glUniform3f( SHADER_UNIFORM( shader_wire
, "uEnd" ), drag_to_co
[0], drag_to_co
[1], 0.06f
);
1950 glDrawElements( GL_TRIANGLES
, world
.wire
.em
, GL_UNSIGNED_SHORT
, (void*)(0) );
1953 float rp_x1
= world
.frame_lerp
*9.0f
;
1954 float rp_x2
= 1.0f
-rp_x1
*expf(1.0f
-rp_x1
)* 0.36f
;
1956 for( int y
= 2; y
< world
.h
-2; y
++ )
1958 for( int x
= 2; x
< world
.w
-2; x
++ )
1960 struct cell
*cell
= pcell((v2i
){x
,y
});
1962 if( cell
->state
& FLAG_CANAL
)
1964 if( cell
->state
& FLAG_IS_TRIGGER
)
1966 int trigger_id
= cell
->links
[0]?0:1;
1968 int x2
= cell
->links
[trigger_id
] % world
.w
;
1969 int y2
= (cell
->links
[trigger_id
] - x2
) / world
.w
;
1974 startpoint
[0] = (float)x2
+ (trigger_id
? 0.75f
: 0.25f
);
1975 startpoint
[1] = (float)y2
+ 0.25f
;
1977 endpoint
[0] = x
+0.5f
;
1978 endpoint
[1] = y
+0.5f
;
1980 glUniform1f( SHADER_UNIFORM( shader_wire
, "uCurve" ), cell
->state
& FLAG_TRIGGERED
? rp_x2
* 0.4f
: 0.4f
);
1981 glUniform3f( SHADER_UNIFORM( shader_wire
, "uStart" ), startpoint
[0], startpoint
[1], 0.04f
);
1982 glUniform3f( SHADER_UNIFORM( shader_wire
, "uEnd" ), endpoint
[0], endpoint
[1], 0.04f
);
1983 glDrawElements( GL_TRIANGLES
, world
.wire
.em
, GL_UNSIGNED_SHORT
, (void*)(0) );
1989 SHADER_USE( shader_tile_colour
);
1990 glUniformMatrix3fv( SHADER_UNIFORM( shader_tile_colour
, "uPv" ), 1, GL_FALSE
, (float *)vg_pv
);
1991 use_mesh( &world
.circle
);
1993 int const filled_start
= 0;
1994 int const filled_count
= 32;
1995 int const empty_start
= 32;
1996 int const empty_count
= 32*2;
1999 for( int i
= 0; i
< arrlen( world
.io
); i
++ )
2001 struct cell_terminal
*term
= &world
.io
[ i
];
2002 int posx
= term
->id
% world
.w
;
2003 int posy
= (term
->id
- posx
)/world
.w
;
2004 int is_input
= world
.data
[ term
->id
].state
& FLAG_INPUT
;
2006 v4f dot_colour
= { 0.0f
, 0.0f
, 0.0f
, 1.0f
};
2008 for( int k
= 0; k
< term
->run_count
; k
++ )
2010 for( int j
= 0; j
< term
->runs
[k
].condition_count
; j
++ )
2012 float y_offset
= is_input
? 1.2f
: -0.2f
;
2013 y_offset
+= (is_input
? 0.2f
: -0.2f
) * (float)k
;
2015 glUniform3f( SHADER_UNIFORM( shader_tile_colour
, "uOffset" ), (float)posx
+ 0.2f
+ 0.2f
* (float)j
, (float)posy
+ y_offset
, 0.1f
);
2019 colour_code_v3( term
->runs
[k
].conditions
[j
], dot_colour
);
2020 glUniform4fv( SHADER_UNIFORM( shader_tile_colour
, "uColour" ), 1, dot_colour
);
2022 // Draw filled if tick not passed, draw empty if empty
2023 if( world
.sim_frame
> j
&& world
.sim_run
>= k
)
2024 draw_mesh( empty_start
, empty_count
);
2026 draw_mesh( filled_start
, filled_count
);
2030 if( term
->runs
[k
].recv_count
> j
)
2032 colour_code_v3( term
->runs
[k
].recieved
[j
], dot_colour
);
2033 v3_muls( dot_colour
, 0.8f
, dot_colour
);
2034 glUniform4fv( SHADER_UNIFORM( shader_tile_colour
, "uColour" ), 1, dot_colour
);
2036 draw_mesh( filled_start
, filled_count
);
2039 colour_code_v3( term
->runs
[k
].conditions
[j
], dot_colour
);
2040 glUniform4fv( SHADER_UNIFORM( shader_tile_colour
, "uColour" ), 1, dot_colour
);
2042 draw_mesh( empty_start
, empty_count
);
2048 if( world
.simulating
)
2050 glUniform4f( SHADER_UNIFORM( shader_tile_colour
, "uColour" ), 0.0f
, 0.0f
, 0.0f
, 1.0f
);
2051 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
);
2052 draw_mesh( filled_start
, filled_count
);
2056 float const score_bright
= 1.25f
;
2057 glUniform4f( SHADER_UNIFORM( shader_tile_colour
, "uColour" ),
2058 0.4f
*score_bright
, 0.39f
*score_bright
, 0.45f
*score_bright
, 1.0f
);
2060 use_mesh( &world
.numbers
);
2061 draw_numbers( (v3f
){ 2.0f
, (float)world
.h
-1.875f
, 0.3333f
}, world
.score
);
2063 // Level selection UI
2064 use_mesh( &world
.circle
);
2065 float ratio
= ((float)vg_window_x
/(float)vg_window_y
);
2067 m3x3f ui_view
= M3X3_IDENTITY
;
2068 m3x3_scale( ui_view
, (v3f
){ 1.0f
, ratio
, 1.0f
} );
2069 glUniformMatrix3fv( SHADER_UNIFORM( shader_tile_colour
, "uPv" ), 1, GL_FALSE
, (float *)ui_view
);
2071 // Calculate mouse in UIsp
2072 v3f mouse_ui_space
= { ((float)vg_mouse
[0] / (float)(vg_window_x
)) * 2.0f
- 1.0f
,
2073 (((float)vg_mouse
[1] / (float)(vg_window_y
)) * 2.0f
- 1.0f
)*(-1.0f
/ratio
), 0.0125f
};
2075 // Get selected level
2076 const float selection_scale
= 0.05f
;
2077 int const level_count
= vg_list_size( level_pack_1
);
2078 int level_select
= -1;
2080 if( mouse_ui_space
[0] <= -0.8f
)
2082 float levels_range
= (float)level_count
*selection_scale
*0.6f
;
2083 float level_offset
= ((-mouse_ui_space
[1] + levels_range
) / levels_range
) * 0.5f
* (float)level_count
;
2084 level_select
= ceilf( level_offset
);
2087 if( level_select
>= 0 && level_select
< vg_list_size( level_pack_1
) )
2089 glUniform4f( SHADER_UNIFORM( shader_tile_colour
, "uColour" ), 0.369768f
, 0.3654f
, 0.42f
, 1.0f
);
2091 use_mesh( &world
.tile
);
2092 glUniform3f( SHADER_UNIFORM( shader_tile_colour
, "uOffset" ),
2094 ((float)level_count
- (float)level_select
* 2.0f
) * selection_scale
* 0.6f
,
2099 use_mesh( &world
.circle
);
2101 if( vg_get_button_down( "primary" ) )
2103 console_changelevel( 1, level_pack_1
+ level_select
);
2107 else mouse_ui_space
[1] = INFINITY
;
2109 glUniform4f( SHADER_UNIFORM( shader_tile_colour
, "uColour" ), 0.4f
, 0.39f
, 0.45f
, 1.0f
);
2112 for( int i
= 0; i
< level_count
; i
++ )
2114 struct career_level
*clevel
= &career
.levels
[i
];
2116 v3f level_ui_space
= {
2118 ((float)level_count
- (float)i
* 2.0f
) * selection_scale
* 0.6f
+ selection_scale
* 0.5f
,
2119 selection_scale
* 0.5f
2122 float scale
= vg_clampf( 1.0f
- fabsf(level_ui_space
[1] - mouse_ui_space
[1]) * 2.0f
, 0.9f
, 1.0f
);
2123 level_ui_space
[2] *= scale
;
2125 glUniform3fv( SHADER_UNIFORM( shader_tile_colour
, "uOffset" ), 1, level_ui_space
);
2127 if( clevel
->completed
)
2128 draw_mesh( filled_start
, filled_count
);
2130 draw_mesh( empty_start
, empty_count
);
2134 glUniform4f( SHADER_UNIFORM( shader_tile_colour
, "uColour" ), 0.4f
*1.25f
, 0.39f
*1.25f
, 0.45f
*1.25f
, 1.0f
);
2136 use_mesh( &world
.numbers
);
2137 for( int i
= 0; i
< level_count
; i
++ )
2139 struct career_level
*clevel
= &career
.levels
[i
];
2141 v3f level_ui_space
= {
2143 ((float)level_count
- (float)i
* 2.0f
) * selection_scale
* 0.6f
+ selection_scale
* 0.5f
,
2147 if( clevel
->completed
)
2148 draw_numbers( level_ui_space
, clevel
->score
);
2150 level_ui_space
[0] = -0.975f
;
2151 level_ui_space
[1] -= 0.01f
;
2152 draw_numbers( level_ui_space
, i
);
2155 //use_mesh( &world.numbers );
2156 //draw_numbers( (v3f){ 0.0f, -0.5f, 0.1f }, 128765 );