1 // Copyright (C) 2021 Harry Godden (hgn) - All Rights Reserved
5 #include "fishladder_resources.h"
7 const char *level_pack_1
[] = {
30 levels
[ vg_list_size( level_pack_1
) ];
32 career
= { .version
= 1 };
35 static void career_serialize(void)
37 vg_asset_write( "sav/game.sav", &career
, sizeof( struct career_state
) );
40 static void career_load(void)
43 struct career_state
*cr
= vg_asset_read_s( "sav/game.sav", &sz
);
45 memset( (void*)career
.levels
, 0, vg_list_size(level_pack_1
) * sizeof(struct career_level
) );
49 if( sz
> sizeof( struct career_state
) )
50 vg_warn( "This save file is too big! Some levels will be lost\n" );
52 if( sz
<= offsetof( struct career_state
, levels
) )
54 vg_error( "This save file is too small to have a header\n" );
59 u32
const size_header
= offsetof(struct career_state
, levels
);
60 u32
const size_levels
= sizeof(struct career_state
)-size_header
;
61 u32
const size_levels_input
= sz
- size_header
;
63 memcpy( (void*)career
.levels
, (void*)cr
->levels
, size_levels_input
);
65 if( sz
< sizeof( struct career_state
) )
67 memset( ((void*)career
.levels
) + size_levels_input
, 0, size_levels
-size_levels_input
);
71 vg_success( "Loaded save file... Info:\n" );
73 for( int i
= 0; i
< vg_list_size( career
.levels
); i
++ )
75 struct career_level
*lvl
= &career
.levels
[i
];
76 vg_info( "Score: %u, Time: %u, Completed: %u\n", lvl
->score
, lvl
->time
, lvl
->completed
);
81 vg_info( "No save file... Using blank one\n" );
89 #define FLAG_CANAL 0x1
90 #define FLAG_IS_TRIGGER 0x2
91 #define FLAG_RESERVED0 0x4
92 #define FLAG_RESERVED1 0x8
94 #define FLAG_INPUT 0x10
95 #define FLAG_OUTPUT 0x20
96 #define FLAG_WALL 0x40
98 #define FLAG_FLIP_FLOP 0x100
99 #define FLAG_FLIP_ROTATING 0x200
100 #define FLAG_TARGETED 0x400
103 0000 0 | 0001 1 | 0010 2 | 0011 3
107 0100 4 | 0101 5 | 0110 6 | 0111 7
111 1000 8 | 1001 9 | 1010 10 | 1011 11
115 1100 12 | 1101 13 | 1110 14 | 1111 15
123 k_cell_type_ramp_right
= 3,
124 k_cell_type_ramp_left
= 6,
125 k_cell_type_split
= 7,
126 k_cell_type_merge
= 13
130 { { 0.9f
, 0.6f
, 0.20f
},
131 { 0.2f
, 0.9f
, 0.14f
},
132 { 0.4f
, 0.8f
, 1.00f
} };
134 static void colour_code_v3( char cc
, v3f target
)
136 if( cc
>= 'a' && cc
<= 'z' )
140 if( id
< vg_list_size( colour_sets
) )
142 v3_copy( colour_sets
[ id
], target
);
147 v3_copy( (v3f
){0.0f
,0.0f
,0.0f
}, target
);
156 static void init_mesh( struct mesh
*m
, float const *tris
, u32 length
)
158 m
->elements
= length
/3;
159 glGenVertexArrays( 1, &m
->vao
);
160 glGenBuffers( 1, &m
->vbo
);
162 glBindVertexArray( m
->vao
);
163 glBindBuffer( GL_ARRAY_BUFFER
, m
->vbo
);
164 glBufferData( GL_ARRAY_BUFFER
, length
*sizeof(float), tris
, GL_STATIC_DRAW
);
166 glVertexAttribPointer( 0, 2, GL_FLOAT
, GL_FALSE
, 2 * sizeof(float), (void*)0 );
167 glEnableVertexAttribArray( 0 );
172 static void free_mesh( struct mesh
*m
)
174 glDeleteVertexArrays( 1, &m
->vao
);
175 glDeleteBuffers( 1, &m
->vbo
);
178 static void draw_mesh( int const start
, int const count
)
180 glDrawArrays( GL_TRIANGLES
, start
*3, count
*3 );
183 static void use_mesh( struct mesh
*m
)
185 glBindVertexArray( m
->vao
);
208 int sim_run
, max_runs
;
222 int condition_count
, recv_count
;
234 struct mesh tile
, circle
, numbers
;
236 GLuint background_data
;
237 GLuint random_samples
;
239 int selected
, tile_x
, tile_y
;
257 struct career_level
*ptr_career_level
;
265 static void map_free(void)
267 arrfree( world
.data
);
278 world
.initialzed
= 0;
281 static void map_reclassify( v2i start
, v2i end
, int update_texbuffer
);
282 static int map_load( const char *str
, const char *name
)
284 //TODO: It may be worthwhile, at this point, to switch to binary encoding for save data
293 if( str
[world
.w
] == ';' )
295 else if( !str
[world
.w
] )
297 vg_error( "Unexpected EOF when parsing level\n" );
302 struct cell
*row
= arraddnptr( world
.data
, world
.w
);
304 int reg_start
= 0, reg_end
= 0;
306 u32
*links_to_make
= NULL
;
307 int links_satisfied
= 0;
309 char link_id_buffer
[32];
317 if( *c
== '\r' ) { c
++; continue; } // fuck off windows
323 if( *c
== '\r' ) c
++;
330 if( *c
== '\r' ) { c
++; continue; }
332 if( reg_start
< reg_end
)
334 struct cell_terminal
*terminal
= &world
.io
[ reg_start
];
335 struct terminal_run
*run
= &terminal
->runs
[ terminal
->run_count
-1 ];
337 if( *c
>= 'a' && *c
<= 'z' )
339 run
->conditions
[ run
->condition_count
++ ] = *c
;
343 if( *c
== ',' || *c
== '\n' )
352 terminal
->runs
[ terminal
->run_count
].condition_count
= 0;
353 terminal
->run_count
++;
354 world
.max_runs
= vg_max( world
.max_runs
, terminal
->run_count
);
358 vg_error( "Unkown attribute '%c' (row: %u)\n", *c
, world
.h
);
365 if( links_satisfied
< arrlen( links_to_make
) )
367 struct cell
*target
= &world
.data
[ links_to_make
[ links_satisfied
] ];
369 if( (((u32
)*c
>= (u32
)'0') && ((u32
)*c
<= (u32
)'9')) || *c
== '-' )
371 if( link_id_n
>= vg_list_size( link_id_buffer
)-1 )
373 vg_error( "Number was way too long to be parsed (row: %u)\n", world
.h
);
377 link_id_buffer
[ link_id_n
++ ] = *c
;
379 else if( *c
== ',' || *c
== '\n' )
381 link_id_buffer
[ link_id_n
] = 0x00;
382 int value
= atoi( link_id_buffer
);
384 target
->links
[value
>= 0? 1:0] = abs(value
);
393 vg_error( "Invalid character '%c' (row: %u)\n", *c
, world
.h
);
399 vg_error( "Too many values to assign (row: %u)\n", world
.h
);
408 // Registry length-error checks
409 if( reg_start
!= reg_end
)
411 vg_error( "Not enough spawn values assigned (row: %u, %u of %u)\n", world
.h
, reg_start
, reg_end
);
415 if( links_satisfied
!= arrlen( links_to_make
) )
417 vg_error( "Not enough link values assigned (row: %u, %u of %u)\n", world
.h
, links_satisfied
, arrlen( links_to_make
) );
423 vg_error( "Not enough cells to match previous row definition (row: %u, %u<%u)\n", world
.h
, cx
, world
.w
);
427 row
= arraddnptr( world
.data
, world
.w
);
430 reg_end
= reg_start
= arrlen( world
.io
);
432 arrsetlen( links_to_make
, 0 );
439 vg_error( "Too many cells to match previous row definition (row: %u, %u>%u)\n", world
.h
, cx
, world
.w
);
443 // Tile initialization
445 struct cell
*cell
= &row
[ cx
];
447 if( *c
== '+' || *c
== '-' )
449 struct cell_terminal
*term
= arraddnptr( world
.io
, 1 );
450 term
->id
= cx
+ world
.h
*world
.w
;
452 term
->runs
[0].condition_count
= 0;
454 cell
->state
= *c
== '+'? FLAG_INPUT
: FLAG_OUTPUT
;
457 else if( *c
== '#' ) cell
->state
= FLAG_WALL
;
458 else if( ((u32
)*c
>= (u32
)'A') && ((u32
)*c
<= (u32
)'A'+0xf) )
460 // Canal flag bits (4bit/16 value):
466 cell
->state
= ((u32
)*c
- (u32
)'A') & (FLAG_CANAL
|FLAG_IS_TRIGGER
);
468 if( cell
->state
& FLAG_IS_TRIGGER
)
469 arrpush( links_to_make
, cx
+ world
.h
*world
.w
);
475 else cell
->state
= 0x00;
483 // Update data texture to fill out the background
485 u8 info_buffer
[64*64*4];
486 for( int i
= 0; i
< 64*64; i
++ )
488 u8
*px
= &info_buffer
[i
*4];
495 glBindTexture( GL_TEXTURE_2D
, world
.background_data
);
496 glTexSubImage2D( GL_TEXTURE_2D
, 0, 0, 0, 64, 64, GL_RGBA
, GL_UNSIGNED_BYTE
, info_buffer
);
499 arrfree( links_to_make
);
501 map_reclassify( NULL
, NULL
, 1 );
504 for( int i
= 0; i
< world
.h
*world
.w
; i
++ )
506 struct cell
*src
= &world
.data
[i
];
507 if( src
->state
& FLAG_IS_TRIGGER
)
509 int link_id
= src
->links
[0]?0:1;
510 if( src
->links
[link_id
] <= world
.h
*world
.w
)
512 struct cell
*target
= &world
.data
[ src
->links
[link_id
] ];
513 if( (target
->state
& FLAG_CANAL
) && (target
->config
== k_cell_type_split
) )
515 if( target
->links
[ link_id
] )
517 vg_error( "Link target was already targeted\n" );
523 target
->links
[ link_id
] = i
;
524 target
->state
|= FLAG_TARGETED
;
529 vg_error( "Link target was invalid\n" );
535 vg_error( "Link target out of bounds\n" );
541 vg_success( "Map '%s' loaded! (%u:%u)\n", name
, world
.w
, world
.h
);
543 strncpy( world
.map_name
, name
, vg_list_size( world
.map_name
)-1 );
544 world
.initialzed
= 1;
548 arrfree( links_to_make
);
553 static struct cell
*pcell( v2i pos
)
555 return &world
.data
[ pos
[1]*world
.w
+ pos
[0] ];
558 static void map_serialize( FILE *stream
)
560 for( int y
= 0; y
< world
.h
; y
++ )
562 for( int x
= 0; x
< world
.w
; x
++ )
564 struct cell
*cell
= pcell( (v2i
){ x
, y
} );
566 if( cell
->state
& FLAG_WALL
) fputc( '#', stream
);
567 else if( cell
->state
& FLAG_INPUT
) fputc( '+', stream
);
568 else if( cell
->state
& FLAG_OUTPUT
) fputc( '-', stream
);
569 else if( cell
->state
& (FLAG_CANAL
|FLAG_IS_TRIGGER
|FLAG_RESERVED0
|FLAG_RESERVED1
) )
571 fputc( (cell
->state
& (FLAG_CANAL
|FLAG_IS_TRIGGER
|FLAG_RESERVED0
|FLAG_RESERVED1
)) + (u32
)'A', stream
);
573 else fputc( ' ', stream
);
576 fputc( ';', stream
);
578 int terminal_write_count
= 0;
580 for( int x
= 0; x
< world
.w
; x
++ )
582 for( int i
= 0; i
< arrlen( world
.io
); i
++ )
584 struct cell_terminal
*term
= &world
.io
[i
];
585 if( term
->id
== y
*world
.w
+x
)
587 if( terminal_write_count
)
588 fputc( ',', stream
);
589 terminal_write_count
++;
591 for( int j
= 0; j
< term
->run_count
; j
++ )
593 struct terminal_run
*run
= &term
->runs
[j
];
595 for( int k
= 0; k
< run
->condition_count
; k
++ )
596 fputc( run
->conditions
[k
], stream
);
598 if( j
< term
->run_count
-1 )
599 fputc( ':', stream
);
605 for( int x
= 0; x
< world
.w
; x
++ )
607 struct cell
*cell
= pcell( (v2i
){ x
,y
} );
608 if( cell
->state
& FLAG_IS_TRIGGER
)
610 if( terminal_write_count
)
611 fputc( ',', stream
);
612 terminal_write_count
++;
614 fprintf( stream
, "%d", cell
->links
[0]? -cell
->links
[0]: cell
->links
[1] );
618 fputc( '\n', stream
);
622 int main( int argc
, char *argv
[] )
624 vg_init( argc
, argv
, "Fish (Marbles Computer) Ladder Simulator 2022 | N,M: change level | SPACE: Test | LeftClick: Toggle tile" );
627 static int console_save_map( int argc
, char const *argv
[] )
629 if( !world
.initialzed
)
631 vg_error( "Tried to save uninitialized map!\n" );
635 char map_path
[ 256 ];
637 strcpy( map_path
, "sav/" );
638 strcat( map_path
, world
.map_name
);
639 strcat( map_path
, ".map" );
641 FILE *test_writer
= fopen( map_path
, "wb" );
644 vg_info( "Saving map to '%s'\n", map_path
);
645 map_serialize( test_writer
);
647 fclose( test_writer
);
652 vg_error( "Unable to open stream for writing\n" );
657 static int console_load_map( int argc
, char const *argv
[] )
659 char map_path
[ 256 ];
664 strcpy( map_path
, "sav/" );
665 strcat( map_path
, argv
[0] );
666 strcat( map_path
, ".map" );
668 char *text_source
= vg_textasset_read( map_path
);
672 strcpy( map_path
, "maps/" );
673 strcat( map_path
, argv
[0] );
674 strcat( map_path
, ".map" );
676 text_source
= vg_textasset_read( map_path
);
681 vg_info( "Loading map: '%s'\n", map_path
);
682 world
.ptr_career_level
= NULL
;
684 if( !map_load( text_source
, argv
[0] ) )
692 for( int i
= 0; i
< vg_list_size( level_pack_1
); i
++ )
694 if( !strcmp( level_pack_1
[i
], argv
[0] ) )
696 world
.ptr_career_level
= career
.levels
+ i
;
705 vg_error( "Missing maps '%s'\n", argv
[0] );
711 vg_error( "Missing argument <map_path>\n" );
716 static void io_reset(void)
718 for( int i
= 0; i
< arrlen( world
.io
); i
++ )
720 struct cell_terminal
*term
= &world
.io
[i
];
722 for( int j
= 0; j
< term
->run_count
; j
++ )
723 term
->runs
[j
].recv_count
= 0;
727 static void simulation_stop(void)
729 world
.simulating
= 0;
730 world
.num_fishes
= 0;
735 sfx_system_fadeout( &audio_system_balls_rolling
, 44100 );
737 vg_info( "Stopping simulation!\n" );
740 static int console_changelevel( int argc
, char const *argv
[] )
744 // Save current level
745 console_save_map( 0, NULL
);
746 if( console_load_map( argc
, argv
) )
754 vg_error( "Missing argument <map_path>\n" );
762 vg_function_push( (struct vg_cmd
){
763 .name
= "_map_write",
764 .function
= console_save_map
767 vg_function_push( (struct vg_cmd
){
769 .function
= console_load_map
772 vg_function_push( (struct vg_cmd
){
774 .function
= console_changelevel
781 0.0f
, 0.0f
, 0.0f
, 1.0f
, 1.0f
, 1.0f
,
782 0.0f
, 0.0f
, 1.0f
, 1.0f
, 1.0f
, 0.0f
,
784 0.0f
, 0.0f
, 0.0f
, 1.0f
, 4.0f
, 1.0f
,
785 0.0f
, 0.0f
, 4.0f
, 1.0f
, 4.0f
, 0.0f
788 init_mesh( &world
.tile
, quad_mesh
, vg_list_size(quad_mesh
) );
793 float circle_mesh
[32*6*3];
794 int res
= vg_list_size( circle_mesh
) / (6*3);
796 for( int i
= 0; i
< res
; i
++ )
798 v2f v0
= { sinf( ((float)i
/(float)res
)*VG_TAUf
), cosf( ((float)i
/(float)res
)*VG_TAUf
) };
799 v2f v1
= { sinf( ((float)(i
+1)/(float)res
)*VG_TAUf
), cosf( ((float)(i
+1)/(float)res
)*VG_TAUf
) };
801 circle_mesh
[ i
*6+0 ] = 0.0f
;
802 circle_mesh
[ i
*6+1 ] = 0.0f
;
804 v2_copy( v0
, circle_mesh
+ 32*6 + i
*12 );
805 v2_muls( v0
, 0.8f
, circle_mesh
+ 32*6 + i
*12+2 );
806 v2_copy( v1
, circle_mesh
+ 32*6 + i
*12+4 );
808 v2_copy( v1
, circle_mesh
+ 32*6 + i
*12+6 );
809 v2_muls( v1
, 0.8f
, circle_mesh
+ 32*6 + i
*12+8 );
810 v2_muls( v0
, 0.8f
, circle_mesh
+ 32*6 + i
*12+10 );
812 v2_copy( v0
, circle_mesh
+ i
*6+4 );
813 v2_copy( v1
, circle_mesh
+ i
*6+2 );
814 v2_copy( v0
, circle_mesh
+i
*6+4 );
815 v2_copy( v1
, circle_mesh
+i
*6+2 );
818 init_mesh( &world
.circle
, circle_mesh
, vg_list_size( circle_mesh
) );
823 init_mesh( &world
.numbers
,
825 vg_list_size( MESH_NUMBERS_BUFFER
)
828 for( int i
= 0; i
< 10; i
++ )
830 vg_info( "offset: %u, length: %u\n", MESH_NUMBERS_OFFSETS
[i
][0], MESH_NUMBERS_OFFSETS
[i
][1] );
834 // Create info data texture
836 glGenTextures( 1, &world
.background_data
);
837 glBindTexture( GL_TEXTURE_2D
, world
.background_data
);
838 glTexImage2D( GL_TEXTURE_2D
, 0, GL_RGBA
, 64, 64, 0, GL_RGBA
, GL_UNSIGNED_BYTE
, NULL
);
842 // Create random smaples texture
844 u8
*data
= malloc(512*512*2);
845 for( int i
= 0; i
< 512*512*2; i
++ )
846 data
[ i
] = rand()/(RAND_MAX
/255);
848 glGenTextures( 1, &world
.random_samples
);
849 glBindTexture( GL_TEXTURE_2D
, world
.random_samples
);
850 glTexImage2D( GL_TEXTURE_2D
, 0, GL_RG
, 512, 512, 0, GL_RG
, GL_UNSIGNED_BYTE
, data
);
857 resource_load_main();
861 console_load_map( 1, level_pack_1
);
866 console_save_map( 0, NULL
);
869 resource_free_main();
871 glDeleteTextures( 1, &world
.background_data
);
872 glDeleteTextures( 1, &world
.random_samples
);
874 free_mesh( &world
.tile
);
875 free_mesh( &world
.circle
);
876 free_mesh( &world
.numbers
);
881 static int cell_interactive( v2i co
)
884 if( co
[0] < 2 || co
[0] >= world
.w
-2 || co
[1] < 2 || co
[1] >= world
.h
-2 )
888 if( world
.data
[ world
.w
*co
[1] + co
[0] ].state
& (FLAG_WALL
|FLAG_INPUT
|FLAG_OUTPUT
) )
891 // List of 3x3 configurations that we do not allow
892 static u32 invalid_src
[][9] =
924 // Statically compile invalid configurations into bitmasks
925 static u32 invalid
[ vg_list_size(invalid_src
) ];
927 for( int i
= 0; i
< vg_list_size(invalid_src
); i
++ )
931 for( int j
= 0; j
< 3; j
++ )
932 for( int k
= 0; k
< 3; k
++ )
933 comped
|= invalid_src
[i
][ j
*3+k
] << ((j
*5)+k
);
938 // Extract 5x5 grid surrounding tile
940 for( int y
= co
[1]-2; y
< co
[1]+3; y
++ )
941 for( int x
= co
[0]-2; x
< co
[0]+3; x
++ )
943 struct cell
*cell
= pcell((v2i
){x
,y
});
945 if( cell
&& (cell
->state
& (FLAG_CANAL
|FLAG_INPUT
|FLAG_OUTPUT
)) )
946 blob
|= 0x1 << ((y
-(co
[1]-2))*5 + x
-(co
[0]-2));
949 // Run filter over center 3x3 grid to check for invalid configurations
950 int kernel
[] = { 0, 1, 2, 5, 6, 7, 10, 11, 12 };
951 for( int i
= 0; i
< vg_list_size(kernel
); i
++ )
953 if( blob
& (0x1 << (6+kernel
[i
])) )
955 u32 window
= blob
>> kernel
[i
];
957 for( int j
= 0; j
< vg_list_size(invalid
); j
++ )
958 if((window
& invalid
[j
]) == invalid
[j
])
966 static void map_reclassify( v2i start
, v2i end
, int update_texbuffer
)
968 v2i full_start
= { 1,1 };
969 v2i full_end
= { world
.w
-1, world
.h
-1 };
978 u8 info_buffer
[64*64*4];
981 int px0
= vg_max( start
[0], full_start
[0] ),
982 px1
= vg_min( end
[0], full_end
[0] ),
983 py0
= vg_max( start
[1], full_start
[1] ),
984 py1
= vg_min( end
[1], full_end
[1] );
986 for( int y
= py0
; y
< py1
; y
++ )
988 for( int x
= px0
; x
< px1
; x
++ )
990 struct cell
*cell
= pcell((v2i
){x
,y
});
992 v2i dirs
[] = {{1,0},{0,1},{-1,0},{0,-1}};
997 if( cell
->state
& (FLAG_CANAL
|FLAG_INPUT
|FLAG_OUTPUT
) )
999 for( int i
= 0; i
< vg_list_size( dirs
); i
++ )
1001 struct cell
*neighbour
= pcell((v2i
){x
+dirs
[i
][0], y
+dirs
[i
][1]});
1002 if( neighbour
->state
& (FLAG_CANAL
|FLAG_INPUT
|FLAG_OUTPUT
) )
1010 if( cell
->state
& FLAG_WALL
)
1016 pcell((v2i
){x
,y
})->config
= config
;
1018 u8
*info_px
= &info_buffer
[ (pixel_id
++)*4 ];
1019 info_px
[0] = height
;
1020 info_px
[1] = cell
->state
& FLAG_WALL
? 0: 255;
1026 if( update_texbuffer
)
1028 glBindTexture( GL_TEXTURE_2D
, world
.background_data
);
1029 glTexSubImage2D( GL_TEXTURE_2D
, 0, px0
+ 16, py0
+ 16, px1
-px0
, py1
-py0
, GL_RGBA
, GL_UNSIGNED_BYTE
, info_buffer
);
1034 v2f
const curve_3
[] = {{0.5f
,1.0f
},{0.5f
,0.625f
},{0.625f
,0.5f
},{1.0f
,0.5f
}};
1035 v2f
const curve_6
[] = {{0.5f
,1.0f
},{0.5f
,0.625f
},{0.375f
,0.5f
},{0.0f
,0.5f
}};
1036 v2f
const curve_9
[] = {{1.0f
,0.5f
},{0.625f
,0.5f
},{0.5f
,0.375f
},{0.5f
,0.0f
}};
1037 v2f
const curve_12
[]= {{0.0f
,0.5f
},{0.375f
,0.5f
},{0.5f
,0.375f
},{0.5f
,0.0f
}};
1039 v2f
const curve_1
[] = {{1.0f
,0.5f
},{0.8f
,0.5f
},{0.3f
,0.5f
},{0.2f
,0.5f
}};
1040 v2f
const curve_4
[] = {{0.0f
,0.5f
},{0.3f
,0.5f
},{0.5f
,0.5f
},{0.8f
,0.5f
}};
1041 v2f
const curve_2
[] = {{0.5f
,1.0f
},{0.5f
,0.8f
},{0.5f
,0.3f
},{0.5f
,0.2f
}};
1042 v2f
const curve_8
[] = {{0.5f
,0.8f
},{0.5f
,0.5f
},{0.5f
,0.3f
},{0.5f
,0.0f
}};
1044 v2f
const curve_7
[] = {{0.5f
,0.8438f
},{0.875f
,0.8438f
},{0.625f
,0.5f
},{1.0f
,0.5f
}};
1045 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
}};
1047 float const curve_7_linear_section
= 0.1562f
;
1049 void vg_update(void)
1051 // Fit within screen
1053 float r1
= (float)vg_window_y
/ (float)vg_window_x
,
1054 r2
= (float)world
.h
/ (float)world
.w
,
1057 size
= ( r2
< r1
? (float)world
.w
* 0.5f
: ((float)world
.h
* 0.5f
) / r1
) + 2.5f
;
1058 m3x3_projection( m_projection
, -size
, size
, -size
*r1
, size
*r1
);
1061 origin
[0] = floorf( -0.5f
* world
.w
);
1062 origin
[1] = floorf( -0.5f
* world
.h
);
1065 m3x3_identity( m_view
);
1066 m3x3_translate( m_view
, origin
);
1067 m3x3_mul( m_projection
, m_view
, vg_pv
);
1068 vg_projection_update();
1071 v2_copy( vg_mouse_ws
, world
.tile_pos
);
1073 world
.tile_x
= floorf( world
.tile_pos
[0] );
1074 world
.tile_y
= floorf( world
.tile_pos
[1] );
1076 static u16 id_drag_from
= 0;
1077 static v2f drag_from_co
;
1078 static v2f drag_to_co
;
1081 if( !world
.simulating
)
1083 v2_copy( vg_mouse_ws
, drag_to_co
);
1085 if( cell_interactive( (v2i
){ world
.tile_x
, world
.tile_y
} ))
1087 world
.selected
= world
.tile_y
* world
.w
+ world
.tile_x
;
1089 static u32 modify_state
= 0;
1091 struct cell
*cell_ptr
= &world
.data
[world
.selected
];
1093 if( vg_get_button_down("primary") )
1095 modify_state
= (cell_ptr
->state
& FLAG_CANAL
) ^ FLAG_CANAL
;
1098 if( vg_get_button("primary") && ((cell_ptr
->state
& FLAG_CANAL
) != modify_state
) )
1100 cell_ptr
->state
&= ~FLAG_CANAL
;
1101 cell_ptr
->state
|= modify_state
;
1103 if( cell_ptr
->state
& FLAG_CANAL
)
1105 sfx_set_playrnd( &audio_tile_mod
, &audio_system_sfx
, 3, 6 );
1110 if( cell_ptr
->state
& (FLAG_IS_TRIGGER
|FLAG_TARGETED
) )
1112 cell_ptr
->state
&= ~(FLAG_IS_TRIGGER
|FLAG_TARGETED
);
1113 for( u32 i
= 0; i
< 2; i
++ )
1115 if( cell_ptr
->links
[i
] )
1117 struct cell
*other_ptr
= &world
.data
[ cell_ptr
->links
[i
] ];
1118 other_ptr
->links
[ i
] = 0;
1119 other_ptr
->state
&= ~FLAG_IS_TRIGGER
;
1121 if( other_ptr
->links
[ i
^ 0x1 ] == 0 )
1122 other_ptr
->state
&= ~(FLAG_TARGETED
);
1127 sfx_set_playrnd( &audio_tile_mod
, &audio_system_sfx
, 0, 3 );
1131 cell_ptr
->links
[0] = 0;
1132 cell_ptr
->links
[1] = 0;
1134 map_reclassify( (v2i
){ world
.tile_x
-2, world
.tile_y
-2 },
1135 (v2i
){ world
.tile_x
+2, world
.tile_y
+2 }, 1 );
1138 if( vg_get_button_down("secondary") && !(cell_ptr
->config
== k_cell_type_split
) )
1140 id_drag_from
= world
.selected
;
1141 drag_from_co
[0] = world
.tile_x
+ 0.5f
;
1142 drag_from_co
[1] = world
.tile_y
+ 0.5f
;
1145 if( id_drag_from
&& (cell_ptr
->config
== k_cell_type_split
) )
1147 float local_x
= vg_mouse_ws
[0] - (float)world
.tile_x
;
1148 drag_to_co
[0] = (float)world
.tile_x
+ (local_x
> 0.5f
? 0.75f
: 0.25f
);
1149 drag_to_co
[1] = (float)world
.tile_y
+ 0.25f
;
1151 if( vg_get_button_up("secondary") )
1153 struct cell
*drag_ptr
= &world
.data
[id_drag_from
];
1154 u32 link_id
= local_x
> 0.5f
? 1: 0;
1156 // Cleanup existing connections
1157 if( cell_ptr
->links
[ link_id
] )
1159 vg_warn( "Destroying existing connection on link %u (%hu)\n", link_id
, cell_ptr
->links
[ link_id
] );
1161 struct cell
*current_connection
= &world
.data
[ cell_ptr
->links
[ link_id
]];
1162 current_connection
->state
&= ~FLAG_IS_TRIGGER
;
1163 current_connection
->links
[ link_id
] = 0;
1166 if( drag_ptr
->links
[ link_id
^ 0x1 ] )
1168 vg_warn( "Destroying alternate link %u (%hu)\n", link_id
^ 0x1, drag_ptr
->links
[ link_id
^ 0x1 ] );
1170 struct cell
*current_connection
= &world
.data
[ drag_ptr
->links
[ link_id
^ 0x1 ]];
1171 if( !current_connection
->links
[ link_id
] )
1172 current_connection
->state
&= ~FLAG_TARGETED
;
1174 current_connection
->links
[ link_id
^ 0x1 ] = 0;
1175 drag_ptr
->links
[ link_id
^ 0x1 ] = 0;
1178 // Create the new connection
1179 vg_success( "Creating connection on link %u (%hu)\n", link_id
, id_drag_from
);
1181 cell_ptr
->links
[ link_id
] = id_drag_from
;
1182 drag_ptr
->links
[ link_id
] = world
.selected
;
1184 cell_ptr
->state
|= FLAG_TARGETED
;
1185 drag_ptr
->state
|= FLAG_IS_TRIGGER
;
1191 world
.selected
= -1;
1193 if( vg_get_button("secondary") && id_drag_from
)
1195 vg_line2( drag_from_co
, drag_to_co
, 0xff00ff00, 0xffffff00 );
1204 world
.selected
= -1;
1208 // Simulation stop/start
1209 if( vg_get_button_down("go") )
1211 if( world
.simulating
)
1217 vg_success( "Starting simulation!\n" );
1219 sfx_set_playrnd( &audio_rolls
, &audio_system_balls_rolling
, 0, 1 );
1221 world
.simulating
= 1;
1222 world
.num_fishes
= 0;
1223 world
.sim_frame
= 0;
1224 world
.sim_start
= vg_time
;
1227 for( int i
= 0; i
< world
.w
*world
.h
; i
++ )
1228 world
.data
[ i
].state
&= ~FLAG_FLIP_FLOP
;
1235 if( world
.simulating
)
1237 while( world
.sim_frame
< (int)((vg_time
-world
.sim_start
)*2.0f
) )
1239 //vg_info( "frame: %u\n", world.sim_frame );
1240 sfx_set_playrnd( &audio_random
, &audio_system_balls_switching
, 0, 9 );
1242 // Update splitter deltas
1243 for( int i
= 0; i
< world
.h
*world
.w
; i
++ )
1245 struct cell
*cell
= &world
.data
[i
];
1246 if( cell
->config
== k_cell_type_split
)
1248 cell
->state
&= ~FLAG_FLIP_ROTATING
;
1252 int alive_count
= 0;
1254 // Update fish positions
1255 for( int i
= 0; i
< world
.num_fishes
; i
++ )
1257 struct fish
*fish
= &world
.fishes
[i
];
1258 struct cell
*cell_current
= pcell( fish
->pos
);
1260 if( fish
->alive
== -1 )
1263 if( fish
->alive
!= 1 )
1267 if( cell_current
->state
& FLAG_OUTPUT
)
1269 for( int j
= 0; j
< arrlen( world
.io
); j
++ )
1271 struct cell_terminal
*term
= &world
.io
[j
];
1273 if( term
->id
== fish
->pos
[1]*world
.w
+ fish
->pos
[0] )
1275 struct terminal_run
*run
= &term
->runs
[ world
.sim_run
];
1276 if( run
->recv_count
< vg_list_size( run
->recieved
) )
1277 run
->recieved
[ run
->recv_count
++ ] = fish
->payload
;
1287 if( cell_current
->config
== k_cell_type_split
)
1290 fish
->dir
[0] = cell_current
->state
&FLAG_FLIP_FLOP
?1:-1;
1293 if( !(cell_current
->state
& FLAG_TARGETED
) )
1294 cell_current
->state
^= FLAG_FLIP_FLOP
;
1296 else if( cell_current
->config
== k_cell_type_merge
)
1304 struct cell
*cell_next
= pcell( (v2i
){ fish
->pos
[0]+fish
->dir
[0], fish
->pos
[1]+fish
->dir
[1] } );
1305 if( !(cell_next
->state
& (FLAG_CANAL
|FLAG_OUTPUT
)) )
1307 // Try other directions for valid, so down, left, right..
1308 v2i dirs
[] = {{1,0},{-1,0},{0,-1}};
1309 //vg_info( "Trying some other directions...\n" );
1311 for( int j
= 0; j
< vg_list_size(dirs
); j
++ )
1313 if( (dirs
[j
][0] == -fish
->dir
[0]) && (dirs
[j
][1] == -fish
->dir
[1]) )
1316 if( pcell( (v2i
){ fish
->pos
[0]+dirs
[j
][0], fish
->pos
[1]+dirs
[j
][1] } )->state
& (FLAG_CANAL
|FLAG_OUTPUT
) )
1318 fish
->dir
[0] = dirs
[j
][0];
1319 fish
->dir
[1] = dirs
[j
][1];
1325 fish
->pos
[0] += fish
->dir
[0];
1326 fish
->pos
[1] += fish
->dir
[1];
1328 struct cell
*cell_entry
= pcell( fish
->pos
);
1330 if( !(cell_entry
->state
& (FLAG_INPUT
|FLAG_CANAL
|FLAG_OUTPUT
) ))
1336 if( cell_entry
->config
== k_cell_type_split
||
1337 cell_entry
->config
== k_cell_type_ramp_right
||
1338 cell_entry
->config
== k_cell_type_ramp_left
)
1340 // Special death (FALL)
1341 v2_sub( fish
->physics_co
, fish
->physics_v
, fish
->physics_v
);
1342 v2_divs( fish
->physics_v
, vg_time_delta
, fish
->physics_v
);
1345 vg_warn( "Special death (fall)\n" );
1350 if( cell_entry
->config
== k_cell_type_split
)
1352 sfx_set_playrnd( &audio_splitter
, &audio_system_balls_important
, 0, 1 );
1353 cell_entry
->state
|= FLAG_FLIP_ROTATING
;
1356 if( cell_entry
->state
& FLAG_IS_TRIGGER
)
1358 int trigger_id
= cell_entry
->links
[0]?0:1;
1359 int connection_id
= cell_entry
->links
[trigger_id
];
1360 int target_px
= connection_id
% world
.w
;
1361 int target_py
= (connection_id
- target_px
)/world
.w
;
1363 vg_line2( (v2f
){ fish
->pos
[0], fish
->pos
[1] }, (v2f
){ target_px
, target_py
}, 0xffffffff, 0xffffffff );
1365 struct cell
*target_peice
= &world
.data
[ cell_entry
->links
[trigger_id
] ];
1368 target_peice
->state
|= FLAG_FLIP_FLOP
;
1370 target_peice
->state
&= ~FLAG_FLIP_FLOP
;
1378 // Check for collisions
1379 for( int i
= 0; i
< world
.num_fishes
; i
++ )
1381 if( world
.fishes
[i
].alive
== 1 )
1383 for( int j
= i
+1; j
< world
.num_fishes
; j
++ )
1385 if( (world
.fishes
[j
].alive
== 1) && (world
.fishes
[i
].pos
[0] == world
.fishes
[j
].pos
[0]) &&
1386 (world
.fishes
[i
].pos
[1] == world
.fishes
[j
].pos
[1]) )
1388 // Shatter death (+0.5s)
1389 world
.fishes
[i
].alive
= -1;
1390 world
.fishes
[j
].alive
= -1;
1391 world
.fishes
[i
].death_time
= 0.5f
;
1392 world
.fishes
[j
].death_time
= 0.5f
;
1399 for( int i
= 0; i
< arrlen( world
.io
); i
++ )
1401 struct cell_terminal
*term
= &world
.io
[ i
];
1402 int posx
= term
->id
% world
.w
;
1403 int posy
= (term
->id
- posx
)/world
.w
;
1404 int is_input
= world
.data
[ term
->id
].state
& FLAG_INPUT
;
1408 if( world
.sim_frame
< term
->runs
[ world
.sim_run
].condition_count
)
1410 struct fish
*fish
= &world
.fishes
[world
.num_fishes
++];
1411 fish
->pos
[0] = posx
;
1412 fish
->pos
[1] = posy
;
1414 fish
->payload
= term
->runs
[ world
.sim_run
].conditions
[ world
.sim_frame
];
1418 v2i dirs
[] = {{1,0},{-1,0},{0,-1}};
1419 for( int j
= 0; j
< vg_list_size(dirs
); j
++ )
1420 if( pcell( (v2i
){ posx
+dirs
[j
][0], posy
+dirs
[j
][1] } )->state
& FLAG_CANAL
)
1422 fish
->dir
[0] = dirs
[j
][0];
1423 fish
->dir
[1] = dirs
[j
][1];
1436 if( alive_count
== 0 )
1438 world
.completed
= 1;
1440 for( int i
= 0; i
< arrlen( world
.io
); i
++ )
1442 struct cell_terminal
*term
= &world
.io
[ i
];
1443 int is_input
= world
.data
[ term
->id
].state
& FLAG_INPUT
;
1447 struct terminal_run
*run
= &term
->runs
[ world
.sim_run
];
1449 if( run
->recv_count
== run
->condition_count
)
1451 for( int j
= 0; j
< run
->condition_count
; j
++ )
1453 if( run
->recieved
[j
] != run
->conditions
[j
] )
1455 world
.completed
= 0;
1462 world
.completed
= 0;
1468 if( world
.completed
)
1470 if( world
.sim_run
< world
.max_runs
-1 )
1472 vg_success( "Run passed, starting next\n" );
1474 world
.sim_frame
= 0;
1475 world
.sim_start
= vg_time
;
1480 vg_success( "Level passed!\n" );
1483 for( int i
= 0; i
< world
.w
*world
.h
; i
++ )
1484 if( world
.data
[ i
].state
& FLAG_CANAL
)
1487 world
.score
= score
;
1488 world
.time
= world
.sim_frame
;
1493 vg_error( "Level failed :(\n" );
1496 // Copy into career data
1497 if( world
.ptr_career_level
)
1499 world
.ptr_career_level
->score
= world
.score
;
1500 world
.ptr_career_level
->time
= world
.time
;
1501 world
.ptr_career_level
->completed
= world
.completed
;
1504 simulation_stop(); // TODO: Async?
1511 float scaled_time
= 0.0f
;
1512 scaled_time
= (vg_time
-world
.sim_start
)*2.0f
;
1513 world
.frame_lerp
= scaled_time
- (float)world
.sim_frame
;
1516 for( int i
= 0; i
< world
.num_fishes
; i
++ )
1518 struct fish
*fish
= &world
.fishes
[i
];
1520 if( fish
->alive
== 0 )
1523 if( fish
->alive
== -1 && (world
.frame_lerp
> fish
->death_time
) )
1524 continue; // Todo: particle thing?
1526 if( fish
->alive
== -2 )
1528 v2_muladds( fish
->physics_v
, (v2f
){ 0.0, -9.8f
}, vg_time_delta
, fish
->physics_v
);
1529 v2_muladds( fish
->physics_co
, fish
->physics_v
, vg_time_delta
, fish
->physics_co
);
1533 struct cell
*cell
= pcell(fish
->pos
);
1536 float t
= world
.frame_lerp
;
1538 v2_copy( fish
->physics_co
, fish
->physics_v
);
1540 switch( cell
->config
)
1543 if( fish
->dir
[0] == 1 )
1548 case 1: curve
= curve_1
; break;
1549 case 4: curve
= curve_4
; break;
1550 case 2: curve
= curve_2
; break;
1551 case 8: curve
= curve_8
; break;
1552 case 3: curve
= curve_3
; break;
1553 case 6: curve
= curve_6
; break;
1554 case 9: curve
= curve_9
; break;
1555 case 12: curve
= curve_12
; break;
1557 if( t
> curve_7_linear_section
)
1559 t
-= curve_7_linear_section
;
1560 t
*= (1.0f
/(1.0f
-curve_7_linear_section
));
1562 curve
= cell
->state
& FLAG_FLIP_FLOP
? curve_7
: curve_7_1
;
1566 default: curve
= NULL
; break;
1572 float t3
= t
* t
* t
;
1574 float cA
= 3.0f
*t2
- 3.0f
*t3
;
1575 float cB
= 3.0f
*t3
- 6.0f
*t2
+ 3.0f
*t
;
1576 float cC
= 3.0f
*t2
- t3
- 3.0f
*t
+ 1.0f
;
1578 fish
->physics_co
[0] = t3
*curve
[3][0] + cA
*curve
[2][0] + cB
*curve
[1][0] + cC
*curve
[0][0];
1579 fish
->physics_co
[1] = t3
*curve
[3][1] + cA
*curve
[2][1] + cB
*curve
[1][1] + cC
*curve
[0][1];
1580 fish
->physics_co
[0] += (float)fish
->pos
[0];
1581 fish
->physics_co
[1] += (float)fish
->pos
[1];
1586 origin
[0] = (float)fish
->pos
[0] + (float)fish
->dir
[0]*-0.5f
+ 0.5f
;
1587 origin
[1] = (float)fish
->pos
[1] + (float)fish
->dir
[1]*-0.5f
+ 0.5f
;
1589 fish
->physics_co
[0] = origin
[0] + (float)fish
->dir
[0]*t
;
1590 fish
->physics_co
[1] = origin
[1] + (float)fish
->dir
[1]*t
;
1597 static void render_tiles( v2i start
, v2i end
, v4f
const regular_colour
, v4f
const selected_colour
)
1599 v2i full_start
= { 0,0 };
1600 v2i full_end
= { world
.w
, world
.h
};
1602 if( !start
|| !end
)
1608 glUniform4fv( SHADER_UNIFORM( shader_tile_main
, "uColour" ), 1, regular_colour
);
1610 for( int y
= start
[1]; y
< end
[1]; y
++ )
1612 for( int x
= start
[0]; x
< end
[0]; x
++ )
1614 struct cell
*cell
= pcell((v2i
){x
,y
});
1615 int selected
= world
.selected
== y
*world
.w
+ x
;
1617 int tile_offsets
[][2] =
1619 {2, 0}, {0, 3}, {0, 2}, {2, 2},
1620 {1, 0}, {2, 3}, {3, 2}, {1, 3},
1621 {3, 1}, {0, 1}, {1, 2}, {2, 1},
1622 {1, 1}, {3, 3}, {2, 1}, {2, 1}
1625 int uv
[2] = { 3, 0 };
1627 if( cell
->state
& (FLAG_CANAL
|FLAG_INPUT
|FLAG_OUTPUT
) )
1629 uv
[0] = tile_offsets
[ cell
->config
][0];
1630 uv
[1] = tile_offsets
[ cell
->config
][1];
1633 glUniform4f( SHADER_UNIFORM( shader_tile_main
, "uOffset" ), (float)x
, (float)y
, uv
[0], uv
[1] );
1636 glUniform4fv( SHADER_UNIFORM( shader_tile_main
, "uColour" ), 1, selected_colour
);
1638 glUniform4fv( SHADER_UNIFORM( shader_tile_main
, "uColour" ), 1, regular_colour
);
1646 static void draw_numbers( v3f coord
, int number
)
1649 v3_copy( coord
, pos
);
1650 int digits
[8]; int i
= 0;
1652 while( number
> 0 && i
< 8 )
1654 digits
[i
++] = number
% 10;
1655 number
= number
/ 10;
1658 for( int j
= 0; j
< i
; j
++ )
1660 glUniform3fv( SHADER_UNIFORM( shader_tile_colour
, "uOffset" ), 1, pos
);
1661 draw_mesh( MESH_NUMBERS_OFFSETS
[digits
[i
-j
-1]][0], MESH_NUMBERS_OFFSETS
[digits
[i
-j
-1]][1] );
1662 pos
[0] += pos
[2] * 0.75f
;
1666 void vg_render(void)
1668 glViewport( 0,0, vg_window_x
, vg_window_y
);
1670 glDisable( GL_DEPTH_TEST
);
1671 glClearColor( 0.369768f
, 0.3654f
, 0.42f
, 1.0f
);
1672 glClear( GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
1674 v4f
const colour_default
= {1.0f
, 1.0f
, 1.0f
, 1.0f
};
1675 v4f
const colour_selected
= {0.90f
, 0.92f
, 1.0f
, 1.0f
};
1677 // TILE SET RENDERING
1678 // todo: just slam everything into a mesh...
1679 // when user modifies a tile the neighbours can be easily uploaded to gpu mem
1681 // Currently we're uploading a fair amount of data every frame anyway.
1682 // NOTE: this is for final optimisations ONLY!
1683 // ======================================================================
1685 use_mesh( &world
.tile
);
1691 SHADER_USE( shader_background
);
1692 glUniformMatrix3fv( SHADER_UNIFORM( shader_background
, "uPv" ), 1, GL_FALSE
, (float *)vg_pv
);
1694 glActiveTexture( GL_TEXTURE0
);
1695 glBindTexture( GL_TEXTURE_2D
, world
.background_data
);
1696 glUniform1i( SHADER_UNIFORM( shader_background
, "uTexMain" ), 0 );
1698 glUniform3f( SHADER_UNIFORM( shader_background
, "uOffset" ), -16, -16, 64 );
1699 glUniform1f( SHADER_UNIFORM( shader_background
, "uVariance" ), 0.02f
);
1701 glActiveTexture( GL_TEXTURE1
);
1702 glBindTexture( GL_TEXTURE_2D
, world
.random_samples
);
1703 glUniform1i( SHADER_UNIFORM( shader_background
, "uSamplerNoise" ), 1 );
1710 SHADER_USE( shader_tile_main
);
1713 m2x2_identity( subtransform
);
1714 glUniformMatrix2fv( SHADER_UNIFORM( shader_tile_main
, "uSubTransform" ), 1, GL_FALSE
, (float *)subtransform
);
1715 glUniformMatrix3fv( SHADER_UNIFORM( shader_tile_main
, "uPv" ), 1, GL_FALSE
, (float *)vg_pv
);
1716 glUniform1f( SHADER_UNIFORM( shader_tile_main
, "uGhost" ), 0.0f
);
1717 glUniform1f( SHADER_UNIFORM( shader_tile_main
, "uForeground" ), 0.0f
);
1720 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
1721 glBlendEquation(GL_FUNC_ADD
);
1724 vg_tex2d_bind( &tex_tile_data
, 0 );
1725 glUniform1i( SHADER_UNIFORM( shader_tile_main
, "uTexGlyphs" ), 0 );
1727 vg_tex2d_bind( &tex_wood
, 1 );
1728 glUniform1i( SHADER_UNIFORM( shader_tile_main
, "uTexWood" ), 1 );
1730 render_tiles( NULL
, NULL
, colour_default
, colour_default
);
1734 SHADER_USE( shader_ball
);
1735 glUniformMatrix3fv( SHADER_UNIFORM( shader_ball
, "uPv" ), 1, GL_FALSE
, (float *)vg_pv
);
1737 vg_tex2d_bind( &tex_ball
, 0 );
1738 glUniform1i( SHADER_UNIFORM( shader_ball
, "uTexMain" ), 0 );
1741 if( world
.simulating
)
1743 for( int i
= 0; i
< world
.num_fishes
; i
++ )
1745 struct fish
*fish
= &world
.fishes
[i
];
1747 if( fish
->alive
== 0 )
1750 if( fish
->alive
== -1 && (world
.frame_lerp
> fish
->death_time
) )
1753 v4f dot_colour
= { 0.0f
, 0.0f
, 0.0f
, 1.0f
};
1754 colour_code_v3( fish
->payload
, dot_colour
);
1756 glUniform3fv( SHADER_UNIFORM( shader_ball
, "uColour" ), 1, dot_colour
);
1757 glUniform2fv( SHADER_UNIFORM( shader_ball
, "uOffset" ), 1, fish
->physics_co
);
1762 SHADER_USE( shader_tile_main
);
1765 vg_tex2d_bind( &tex_tile_data
, 0 );
1766 glUniform1i( SHADER_UNIFORM( shader_tile_main
, "uTexGlyphs" ), 0 );
1768 vg_tex2d_bind( &tex_wood
, 1 );
1769 glUniform1i( SHADER_UNIFORM( shader_tile_main
, "uTexWood" ), 1 );
1771 glUniform1f( SHADER_UNIFORM( shader_tile_main
, "uForeground" ), 1.0f
);
1772 render_tiles( NULL
, NULL
, colour_default
, colour_selected
);
1775 for( int y
= 2; y
< world
.h
-2; y
++ )
1777 for( int x
= 2; x
< world
.w
-2; x
++ )
1779 struct cell
*cell
= pcell((v2i
){x
,y
});
1781 if( cell
->state
& FLAG_CANAL
)
1783 if( cell
->state
& FLAG_IS_TRIGGER
)
1785 int trigger_id
= cell
->links
[0]?0:1;
1787 int x2
= cell
->links
[trigger_id
] % world
.w
;
1788 int y2
= (cell
->links
[trigger_id
] - x2
) / world
.w
;
1794 startpoint
[0] = (float)x2
+ (trigger_id
? 0.75f
: 0.25f
);
1795 startpoint
[1] = (float)y2
+ 0.25f
;
1797 endpoint
[0] = x
+0.5f
;
1798 endpoint
[1] = y
+0.5f
;
1799 v2_add( startpoint
, endpoint
, midpoint
);
1800 v2_muls( midpoint
, 0.5f
, midpoint
);
1801 midpoint
[1] += -0.8f
;
1804 v2f lastpoint
; v2f curpoint
;
1805 v2_copy( startpoint
, lastpoint
);
1807 for( int i
= 0; i
< 10; i
++ )
1809 t
= ((float)i
+1.f
)/10.0f
;
1812 v2_muls( startpoint
, ta
*ta
, curpoint
);
1813 v2_muladds( curpoint
, midpoint
, 2.0f
*ta
*t
, curpoint
);
1814 v2_muladds( curpoint
, endpoint
, t
*t
, curpoint
);
1816 vg_line2( lastpoint
, curpoint
, 0xff444444, 0xff444444 );
1817 v2_copy( curpoint
, lastpoint
);
1821 if( cell
->config
== k_cell_type_split
)
1823 float rotation
= cell
->state
& FLAG_FLIP_FLOP
? vg_rad( -45.0f
): vg_rad( 45.0f
);
1825 if( cell
->state
& FLAG_FLIP_ROTATING
)
1827 if( (world
.frame_lerp
> curve_7_linear_section
) )
1829 float const rotation_speed
= 0.4f
;
1830 if( (world
.frame_lerp
< 1.0f
-rotation_speed
) )
1832 float t
= world
.frame_lerp
- curve_7_linear_section
;
1833 t
*= -2.0f
* (1.0f
/(1.0f
-(curve_7_linear_section
+rotation_speed
)));
1843 m2x2_create_rotation( subtransform
, rotation
);
1845 glUniformMatrix2fv( SHADER_UNIFORM( shader_tile_main
, "uSubTransform" ), 1, GL_FALSE
, (float *)subtransform
);
1846 glUniform4f( SHADER_UNIFORM( shader_tile_main
, "uOffset" ), (float)x
, (float)y
+ 0.125f
, cell
->state
& FLAG_TARGETED
? 3.0f
: 0.0f
, 0.0f
);
1854 if( world
.selected
!= -1 && !(world
.data
[ world
.selected
].state
& FLAG_CANAL
) )
1856 v2i new_begin
= { world
.tile_x
- 2, world
.tile_y
- 2 };
1857 v2i new_end
= { world
.tile_x
+ 2, world
.tile_y
+ 2 };
1859 world
.data
[ world
.selected
].state
^= FLAG_CANAL
;
1860 map_reclassify( new_begin
, new_end
, 0 );
1862 m2x2_identity( subtransform
);
1863 glUniform1f( SHADER_UNIFORM( shader_tile_main
, "uGhost" ), 1.0f
);
1864 glUniformMatrix2fv( SHADER_UNIFORM( shader_tile_main
, "uSubTransform" ), 1, GL_FALSE
, (float *)subtransform
);
1865 glUniform2fv( SHADER_UNIFORM( shader_tile_main
, "uMousePos" ), 1, world
.tile_pos
);
1867 render_tiles( new_begin
, new_end
, colour_default
, colour_default
);
1869 world
.data
[ world
.selected
].state
^= FLAG_CANAL
;
1870 map_reclassify( new_begin
, new_end
, 0 );
1873 //glDisable(GL_BLEND);
1875 glDisable(GL_BLEND
);
1877 SHADER_USE( shader_tile_colour
);
1878 glUniformMatrix3fv( SHADER_UNIFORM( shader_tile_colour
, "uPv" ), 1, GL_FALSE
, (float *)vg_pv
);
1879 use_mesh( &world
.circle
);
1881 int const filled_start
= 0;
1882 int const filled_count
= 32;
1883 int const empty_start
= 32;
1884 int const empty_count
= 32*2;
1887 for( int i
= 0; i
< arrlen( world
.io
); i
++ )
1889 struct cell_terminal
*term
= &world
.io
[ i
];
1890 int posx
= term
->id
% world
.w
;
1891 int posy
= (term
->id
- posx
)/world
.w
;
1892 int is_input
= world
.data
[ term
->id
].state
& FLAG_INPUT
;
1894 v4f dot_colour
= { 0.0f
, 0.0f
, 0.0f
, 1.0f
};
1896 for( int k
= 0; k
< term
->run_count
; k
++ )
1898 for( int j
= 0; j
< term
->runs
[k
].condition_count
; j
++ )
1900 float y_offset
= is_input
? 1.2f
: -0.2f
;
1901 y_offset
+= (is_input
? 0.2f
: -0.2f
) * (float)k
;
1903 glUniform3f( SHADER_UNIFORM( shader_tile_colour
, "uOffset" ), (float)posx
+ 0.2f
+ 0.2f
* (float)j
, (float)posy
+ y_offset
, 0.1f
);
1907 colour_code_v3( term
->runs
[k
].conditions
[j
], dot_colour
);
1908 glUniform4fv( SHADER_UNIFORM( shader_tile_colour
, "uColour" ), 1, dot_colour
);
1910 // Draw filled if tick not passed, draw empty if empty
1911 if( world
.sim_frame
> j
&& world
.sim_run
>= k
)
1912 draw_mesh( empty_start
, empty_count
);
1914 draw_mesh( filled_start
, filled_count
);
1918 if( term
->runs
[k
].recv_count
> j
)
1920 colour_code_v3( term
->runs
[k
].recieved
[j
], dot_colour
);
1921 v3_muls( dot_colour
, 0.8f
, dot_colour
);
1922 glUniform4fv( SHADER_UNIFORM( shader_tile_colour
, "uColour" ), 1, dot_colour
);
1924 draw_mesh( filled_start
, filled_count
);
1927 colour_code_v3( term
->runs
[k
].conditions
[j
], dot_colour
);
1928 glUniform4fv( SHADER_UNIFORM( shader_tile_colour
, "uColour" ), 1, dot_colour
);
1930 draw_mesh( empty_start
, empty_count
);
1936 if( world
.simulating
)
1938 glUniform4f( SHADER_UNIFORM( shader_tile_colour
, "uColour" ), 0.0f
, 0.0f
, 0.0f
, 1.0f
);
1939 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
);
1940 draw_mesh( filled_start
, filled_count
);
1944 float const score_bright
= 1.25f
;
1945 glUniform4f( SHADER_UNIFORM( shader_tile_colour
, "uColour" ),
1946 0.4f
*score_bright
, 0.39f
*score_bright
, 0.45f
*score_bright
, 1.0f
);
1948 use_mesh( &world
.numbers
);
1949 draw_numbers( (v3f
){ 2.0f
, (float)world
.h
-1.875f
, 0.3333f
}, world
.score
);
1951 // Level selection UI
1952 use_mesh( &world
.circle
);
1953 float ratio
= ((float)vg_window_x
/(float)vg_window_y
);
1955 m3x3f ui_view
= M3X3_IDENTITY
;
1956 m3x3_scale( ui_view
, (v3f
){ 1.0f
, ratio
, 1.0f
} );
1957 glUniformMatrix3fv( SHADER_UNIFORM( shader_tile_colour
, "uPv" ), 1, GL_FALSE
, (float *)ui_view
);
1959 // Calculate mouse in UIsp
1960 v3f mouse_ui_space
= { ((float)vg_mouse
[0] / (float)(vg_window_x
)) * 2.0f
- 1.0f
,
1961 (((float)vg_mouse
[1] / (float)(vg_window_y
)) * 2.0f
- 1.0f
)*(-1.0f
/ratio
), 0.0125f
};
1963 // Get selected level
1964 const float selection_scale
= 0.05f
;
1965 int const level_count
= vg_list_size( level_pack_1
);
1966 int level_select
= -1;
1968 if( mouse_ui_space
[0] <= -0.8f
)
1970 float levels_range
= (float)level_count
*selection_scale
*0.6f
;
1971 float level_offset
= ((-mouse_ui_space
[1] + levels_range
) / levels_range
) * 0.5f
* (float)level_count
;
1972 level_select
= ceilf( level_offset
);
1975 if( level_select
>= 0 && level_select
< vg_list_size( level_pack_1
) )
1977 glUniform4f( SHADER_UNIFORM( shader_tile_colour
, "uColour" ), 0.369768f
, 0.3654f
, 0.42f
, 1.0f
);
1979 use_mesh( &world
.tile
);
1980 glUniform3f( SHADER_UNIFORM( shader_tile_colour
, "uOffset" ),
1982 ((float)level_count
- (float)level_select
* 2.0f
) * selection_scale
* 0.6f
,
1987 use_mesh( &world
.circle
);
1989 if( vg_get_button_down( "primary" ) )
1991 console_changelevel( 1, level_pack_1
+ level_select
);
1995 else mouse_ui_space
[1] = INFINITY
;
1997 glUniform4f( SHADER_UNIFORM( shader_tile_colour
, "uColour" ), 0.4f
, 0.39f
, 0.45f
, 1.0f
);
2000 for( int i
= 0; i
< level_count
; i
++ )
2002 struct career_level
*clevel
= &career
.levels
[i
];
2004 v3f level_ui_space
= {
2006 ((float)level_count
- (float)i
* 2.0f
) * selection_scale
* 0.6f
+ selection_scale
* 0.5f
,
2007 selection_scale
* 0.5f
2010 float scale
= vg_clampf( 1.0f
- fabsf(level_ui_space
[1] - mouse_ui_space
[1]) * 2.0f
, 0.9f
, 1.0f
);
2011 level_ui_space
[2] *= scale
;
2013 glUniform3fv( SHADER_UNIFORM( shader_tile_colour
, "uOffset" ), 1, level_ui_space
);
2015 if( clevel
->completed
)
2016 draw_mesh( filled_start
, filled_count
);
2018 draw_mesh( empty_start
, empty_count
);
2022 use_mesh( &world
.numbers
);
2023 for( int i
= 0; i
< level_count
; i
++ )
2025 struct career_level
*clevel
= &career
.levels
[i
];
2027 v3f level_ui_space
= {
2029 ((float)level_count
- (float)i
* 2.0f
) * selection_scale
* 0.6f
+ selection_scale
* 0.5f
,
2033 if( clevel
->completed
)
2035 draw_numbers( level_ui_space
, clevel
->score
);
2039 //use_mesh( &world.numbers );
2040 //draw_numbers( (v3f){ 0.0f, -0.5f, 0.1f }, 128765 );