1 // Copyright (C) 2021 Harry Godden (hgn) - All Rights Reserved
5 #include "fishladder_resources.h"
7 const char *level_pack_1
[] = {
15 const char *level_pack
[] =
45 "###+##+####;bbb,bb\n"
50 "###-#####-###;a,aaa\n"
57 "######+######;aaaa\n"
62 "###-#####-###;aaa,aa\n"
69 "###+#####+###;aa,aaa\n"
74 "####-##########;abcb\n"
84 "##########+####;bcba\n"
92 #define FLAG_INPUT 0x1
93 #define FLAG_OUTPUT 0x2
94 #define FLAG_CANAL 0x4
96 #define FLAG_FLIP_FLOP 0x100
97 #define FLAG_FLIP_ROTATING 0x200
100 0000 0 | 0001 1 | 0010 2 | 0011 3
104 0100 4 | 0101 5 | 0110 6 | 0111 7
108 1000 8 | 1001 9 | 1010 10 | 1011 11
112 1100 12 | 1101 13 | 1110 14 | 1111 15
120 k_cell_type_ramp_right
= 3,
121 k_cell_type_ramp_left
= 6,
122 k_cell_type_split
= 7,
123 k_cell_type_merge
= 13
127 { { 0.9f
, 0.6f
, 0.20f
},
128 { 0.2f
, 0.9f
, 0.14f
},
129 { 0.4f
, 0.8f
, 1.00f
} };
131 static void colour_code_v3( char cc
, v3f target
)
133 if( cc
>= 'a' && cc
<= 'z' )
137 if( id
< vg_list_size( colour_sets
) )
139 v3_copy( colour_sets
[ id
], target
);
144 v3_copy( (v3f
){0.0f
,0.0f
,0.0f
}, target
);
153 static void init_mesh( struct mesh
*m
, float *tris
, u32 length
)
155 m
->elements
= length
/3;
156 glGenVertexArrays( 1, &m
->vao
);
157 glGenBuffers( 1, &m
->vbo
);
159 glBindVertexArray( m
->vao
);
160 glBindBuffer( GL_ARRAY_BUFFER
, m
->vbo
);
161 glBufferData( GL_ARRAY_BUFFER
, length
*sizeof(float), tris
, GL_STATIC_DRAW
);
163 glVertexAttribPointer( 0, 2, GL_FLOAT
, GL_FALSE
, 2 * sizeof(float), (void*)0 );
164 glEnableVertexAttribArray( 0 );
169 static void free_mesh( struct mesh
*m
)
171 glDeleteVertexArrays( 1, &m
->vao
);
172 glDeleteBuffers( 1, &m
->vbo
);
175 static void draw_mesh( int const start
, int const count
)
177 glDrawArrays( GL_TRIANGLES
, start
*3, count
*3 );
180 static void use_mesh( struct mesh
*m
)
182 glBindVertexArray( m
->vao
);
204 // TODO: Split into input/output structures
214 struct mesh tile
, circle
;
216 GLuint background_data
;
217 GLuint random_samples
;
219 int selected
, tile_x
, tile_y
;
239 static void map_free(void)
241 for( int i
= 0; i
< arrlen( world
.io
); i
++ )
242 arrfree( world
.io
[ i
].conditions
);
244 arrfree( world
.data
);
253 static void map_reclassify( v2i start
, v2i end
, int update_texbuffer
);
254 static int map_load( const char *str
, const char *name
)
263 if( str
[world
.w
] == ';' )
265 else if( !str
[world
.w
] )
267 vg_error( "Unexpected EOF when parsing level\n" );
272 struct cell
*row
= arraddnptr( world
.data
, world
.w
);
274 int reg_start
= 0, reg_end
= 0;
290 if( reg_start
< reg_end
)
292 if( *c
>= 'a' && *c
<= 'z' )
294 arrpush( world
.io
[ reg_start
].conditions
, *c
);
298 if( *c
== ',' || *c
== '\n' )
307 vg_error( "Unkown attribute '%c' (row: %u)\n", *c
, world
.h
);
314 vg_error( "Too many values to assign (row: %u)\n", world
.h
);
322 if( reg_start
!= reg_end
)
324 vg_error( "Not enough values assigned (row: %u, %u of %u)\n", world
.h
, reg_start
, reg_end
);
330 vg_error( "Not enough cells to match previous row definition (row: %u, %u<%u)\n", world
.h
, cx
, world
.w
);
334 row
= arraddnptr( world
.data
, world
.w
);
337 reg_end
= reg_start
= arrlen( world
.io
);
343 vg_error( "Too many cells to match previous row definition (row: %u, %u>%u)\n", world
.h
, cx
, world
.w
);
347 // Tile initialization
349 struct cell
*cell
= &row
[ cx
];
351 if( *c
== '+' || *c
== '-' )
353 struct cell_terminal term
= { .id
= cx
+ world
.h
*world
.w
};
354 arrpush( world
.io
, term
);
355 cell
->state
= *c
== '+'? FLAG_INPUT
: FLAG_OUTPUT
;
358 else if( *c
== '#' ) cell
->state
= FLAG_WALL
;
359 else if( *c
== '*' ) cell
->state
= FLAG_CANAL
;
360 else cell
->state
= 0x00;
368 // Update data texture to fill out the background
370 u8 info_buffer
[64*64*4];
371 for( int i
= 0; i
< 64*64; i
++ )
373 u8
*px
= &info_buffer
[i
*4];
380 glBindTexture( GL_TEXTURE_2D
, world
.background_data
);
381 glTexSubImage2D( GL_TEXTURE_2D
, 0, 0, 0, 64, 64, GL_RGBA
, GL_UNSIGNED_BYTE
, info_buffer
);
384 map_reclassify( NULL
, NULL
, 1 );
385 vg_success( "Map '%s' loaded! (%u:%u)\n", name
, world
.w
, world
.h
);
387 strncpy( world
.map_name
, name
, vg_list_size( world
.map_name
)-1 );
391 static struct cell
*pcell( v2i pos
)
393 return &world
.data
[ pos
[1]*world
.w
+ pos
[0] ];
396 static void map_serialize( FILE *stream
)
398 for( int y
= 0; y
< world
.h
; y
++ )
400 for( int x
= 0; x
< world
.w
; x
++ )
402 struct cell
*cell
= pcell( (v2i
){ x
, y
} );
404 if( cell
->state
& FLAG_WALL
) fputc( '#', stream
);
405 else if( cell
->state
& FLAG_INPUT
) fputc( '+', stream
);
406 else if( cell
->state
& FLAG_OUTPUT
) fputc( '-', stream
);
407 else if( cell
->state
& FLAG_CANAL
) fputc( '*', stream
);
408 else fputc( ' ', stream
);
411 fputc( ';', stream
);
413 int terminal_write_count
= 0;
415 for( int x
= 0; x
< world
.w
; x
++ )
417 for( int i
= 0; i
< arrlen( world
.io
); i
++ )
419 struct cell_terminal
*term
= &world
.io
[i
];
420 if( term
->id
== y
*world
.w
+x
)
422 if( terminal_write_count
)
423 fputc( ',', stream
);
424 terminal_write_count
++;
426 for( int j
= 0; j
< arrlen( term
->conditions
); j
++ )
427 fputc( term
->conditions
[j
], stream
);
432 fputc( '\n', stream
);
436 int main( int argc
, char *argv
[] )
438 vg_init( argc
, argv
, "Fish (Marbles Computer) Ladder Simulator 2022 | N,M: change level | SPACE: Test | LeftClick: Toggle tile" );
441 static int console_save_map( int argc
, char const *argv
[] )
443 char map_path
[ 256 ];
445 strcpy( map_path
, "sav/" );
446 strcat( map_path
, world
.map_name
);
447 strcat( map_path
, ".map" );
449 FILE *test_writer
= fopen( map_path
, "wb" );
452 map_serialize( test_writer
);
454 fclose( test_writer
);
459 vg_error( "Unable to open stream for writing\n" );
464 static int console_load_map( int argc
, char const *argv
[] )
466 char map_path
[ 256 ];
471 strcpy( map_path
, "sav/" );
472 strcat( map_path
, argv
[0] );
473 strcat( map_path
, ".map" );
475 char *text_source
= vg_textasset_read( map_path
);
479 strcpy( map_path
, "maps/" );
480 strcat( map_path
, argv
[0] );
481 strcat( map_path
, ".map" );
483 text_source
= vg_textasset_read( map_path
);
488 map_load( text_source
, argv
[0] );
494 vg_error( "Missing maps '%s'\n", argv
[0] );
500 vg_error( "Missing argument <map_path>\n" );
505 static int console_changelevel( int argc
, char const *argv
[] )
509 // Save current level
510 if( console_save_map( 0, NULL
) )
511 if( console_load_map( argc
, argv
) )
516 vg_error( "Missing argument <map_path>\n" );
524 vg_function_push( (struct vg_cmd
){
526 .function
= console_save_map
529 vg_function_push( (struct vg_cmd
){
531 .function
= console_load_map
534 vg_function_push( (struct vg_cmd
){
535 .name
= "changelevel",
536 .function
= console_changelevel
543 0.0f
, 0.0f
, 0.0f
, 1.0f
, 1.0f
, 1.0f
,
544 0.0f
, 0.0f
, 1.0f
, 1.0f
, 1.0f
, 0.0f
,
546 0.0f
, 0.0f
, 0.0f
, 1.0f
, 4.0f
, 1.0f
,
547 0.0f
, 0.0f
, 4.0f
, 1.0f
, 4.0f
, 0.0f
550 init_mesh( &world
.tile
, quad_mesh
, vg_list_size(quad_mesh
) );
555 float circle_mesh
[32*6*3];
556 int res
= vg_list_size( circle_mesh
) / (6*3);
558 for( int i
= 0; i
< res
; i
++ )
560 v2f v0
= { sinf( ((float)i
/(float)res
)*VG_TAUf
), cosf( ((float)i
/(float)res
)*VG_TAUf
) };
561 v2f v1
= { sinf( ((float)(i
+1)/(float)res
)*VG_TAUf
), cosf( ((float)(i
+1)/(float)res
)*VG_TAUf
) };
563 circle_mesh
[ i
*6+0 ] = 0.0f
;
564 circle_mesh
[ i
*6+1 ] = 0.0f
;
566 v2_copy( v0
, circle_mesh
+ 32*6 + i
*12 );
567 v2_muls( v0
, 0.8f
, circle_mesh
+ 32*6 + i
*12+2 );
568 v2_copy( v1
, circle_mesh
+ 32*6 + i
*12+4 );
570 v2_copy( v1
, circle_mesh
+ 32*6 + i
*12+6 );
571 v2_muls( v1
, 0.8f
, circle_mesh
+ 32*6 + i
*12+8 );
572 v2_muls( v0
, 0.8f
, circle_mesh
+ 32*6 + i
*12+10 );
574 v2_copy( v0
, circle_mesh
+ i
*6+4 );
575 v2_copy( v1
, circle_mesh
+ i
*6+2 );
576 v2_copy( v0
, circle_mesh
+i
*6+4 );
577 v2_copy( v1
, circle_mesh
+i
*6+2 );
580 init_mesh( &world
.circle
, circle_mesh
, vg_list_size( circle_mesh
) );
583 // Create info data texture
585 glGenTextures( 1, &world
.background_data
);
586 glBindTexture( GL_TEXTURE_2D
, world
.background_data
);
587 glTexImage2D( GL_TEXTURE_2D
, 0, GL_RGBA
, 64, 64, 0, GL_RGBA
, GL_UNSIGNED_BYTE
, NULL
);
591 // Create random smaples texture
593 u8
*data
= malloc(512*512*2);
594 for( int i
= 0; i
< 512*512*2; i
++ )
595 data
[ i
] = rand()/(RAND_MAX
/255);
597 glGenTextures( 1, &world
.random_samples
);
598 glBindTexture( GL_TEXTURE_2D
, world
.random_samples
);
599 glTexImage2D( GL_TEXTURE_2D
, 0, GL_RG
, 512, 512, 0, GL_RG
, GL_UNSIGNED_BYTE
, data
);
606 resource_load_main();
608 console_load_map( 1, level_pack_1
);
613 resource_free_main();
615 glDeleteTextures( 1, &world
.background_data
);
616 glDeleteTextures( 1, &world
.random_samples
);
618 free_mesh( &world
.tile
);
619 free_mesh( &world
.circle
);
624 static int cell_interactive( v2i co
)
627 if( co
[0] < 2 || co
[0] >= world
.w
-2 || co
[1] < 2 || co
[1] >= world
.h
-2 )
631 if( world
.data
[ world
.w
*co
[1] + co
[0] ].state
& (FLAG_WALL
|FLAG_INPUT
|FLAG_OUTPUT
) )
634 // List of 3x3 configurations that we do not allow
635 static u32 invalid_src
[][9] =
667 // Statically compile invalid configurations into bitmasks
668 static u32 invalid
[ vg_list_size(invalid_src
) ];
670 for( int i
= 0; i
< vg_list_size(invalid_src
); i
++ )
674 for( int j
= 0; j
< 3; j
++ )
675 for( int k
= 0; k
< 3; k
++ )
676 comped
|= invalid_src
[i
][ j
*3+k
] << ((j
*5)+k
);
681 // Extract 5x5 grid surrounding tile
683 for( int y
= co
[1]-2; y
< co
[1]+3; y
++ )
684 for( int x
= co
[0]-2; x
< co
[0]+3; x
++ )
686 struct cell
*cell
= pcell((v2i
){x
,y
});
688 if( cell
&& (cell
->state
& (FLAG_CANAL
|FLAG_INPUT
|FLAG_OUTPUT
)) )
689 blob
|= 0x1 << ((y
-(co
[1]-2))*5 + x
-(co
[0]-2));
692 // Run filter over center 3x3 grid to check for invalid configurations
693 int kernel
[] = { 0, 1, 2, 5, 6, 7, 10, 11, 12 };
694 for( int i
= 0; i
< vg_list_size(kernel
); i
++ )
696 if( blob
& (0x1 << (6+kernel
[i
])) )
698 u32 window
= blob
>> kernel
[i
];
700 for( int j
= 0; j
< vg_list_size(invalid
); j
++ )
701 if((window
& invalid
[j
]) == invalid
[j
])
709 static void map_reclassify( v2i start
, v2i end
, int update_texbuffer
)
711 v2i full_start
= { 1,1 };
712 v2i full_end
= { world
.w
-1, world
.h
-1 };
721 u8 info_buffer
[64*64*4];
724 int px0
= vg_max( start
[0], full_start
[0] ),
725 px1
= vg_min( end
[0], full_end
[0] ),
726 py0
= vg_max( start
[1], full_start
[1] ),
727 py1
= vg_min( end
[1], full_end
[1] );
729 for( int y
= py0
; y
< py1
; y
++ )
731 for( int x
= px0
; x
< px1
; x
++ )
733 struct cell
*cell
= pcell((v2i
){x
,y
});
735 v2i dirs
[] = {{1,0},{0,1},{-1,0},{0,-1}};
740 if( cell
->state
& (FLAG_CANAL
|FLAG_INPUT
|FLAG_OUTPUT
) )
742 for( int i
= 0; i
< vg_list_size( dirs
); i
++ )
744 struct cell
*neighbour
= pcell((v2i
){x
+dirs
[i
][0], y
+dirs
[i
][1]});
745 if( neighbour
->state
& (FLAG_CANAL
|FLAG_INPUT
|FLAG_OUTPUT
) )
753 if( cell
->state
& FLAG_WALL
)
759 pcell((v2i
){x
,y
})->config
= config
;
761 u8
*info_px
= &info_buffer
[ (pixel_id
++)*4 ];
763 info_px
[1] = cell
->state
& FLAG_WALL
? 0: 255;
769 if( update_texbuffer
)
771 glBindTexture( GL_TEXTURE_2D
, world
.background_data
);
772 glTexSubImage2D( GL_TEXTURE_2D
, 0, px0
+ 16, py0
+ 16, px1
-px0
, py1
-py0
, GL_RGBA
, GL_UNSIGNED_BYTE
, info_buffer
);
776 v2f
const curve_3
[] = {{0.5f
,1.0f
},{0.5f
,0.625f
},{0.625f
,0.5f
},{1.0f
,0.5f
}};
777 v2f
const curve_6
[] = {{0.5f
,1.0f
},{0.5f
,0.625f
},{0.375f
,0.5f
},{0.0f
,0.5f
}};
778 v2f
const curve_9
[] = {{1.0f
,0.5f
},{0.625f
,0.5f
},{0.5f
,0.375f
},{0.5f
,0.0f
}};
779 v2f
const curve_12
[]= {{0.0f
,0.5f
},{0.375f
,0.5f
},{0.5f
,0.375f
},{0.5f
,0.0f
}};
781 v2f
const curve_2
[] = {{0.5f
,1.0f
},{0.5f
,0.8f
},{0.5f
,0.3f
},{0.5f
,0.2f
}};
782 v2f
const curve_8
[] = {{0.5f
,0.8f
},{0.5f
,0.5f
},{0.5f
,0.3f
},{0.5f
,0.0f
}};
784 v2f
const curve_7
[] = {{0.5f
,0.8438f
},{0.875f
,0.8438f
},{0.625f
,0.5f
},{1.0f
,0.5f
}};
785 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
}};
787 float const curve_7_linear_section
= 0.1562f
;
791 static int curlevel
= 0;
792 int changelvl
= curlevel
;
793 if( vg_get_button_down( "prev" ) ) { if( curlevel
> 0 ) changelvl
--; }
794 else if( vg_get_button_down( "next" ) ) { if( curlevel
< vg_list_size( level_pack
)-1 ) changelvl
++; }
796 if( changelvl
!= curlevel
)
798 console_changelevel( 1, level_pack
+ changelvl
);
799 curlevel
= changelvl
;
802 world
.simulating
= 0;
803 world
.num_fishes
= 0;
806 for( int i
= 0; i
< arrlen( world
.io
); i
++ )
807 world
.io
[i
].recv_count
= 0;
809 vg_info( "Stopping simulation!\n" );
814 float r1
= (float)vg_window_y
/ (float)vg_window_x
,
815 r2
= (float)world
.h
/ (float)world
.w
,
818 size
= ( r2
< r1
? (float)world
.w
* 0.5f
: ((float)world
.h
* 0.5f
) / r1
) + 2.5f
;
819 m3x3_projection( m_projection
, -size
, size
, -size
*r1
, size
*r1
);
822 origin
[0] = floorf( -0.5f
* world
.w
);
823 origin
[1] = floorf( -0.5f
* world
.h
);
826 m3x3_identity( m_view
);
827 m3x3_translate( m_view
, origin
);
828 m3x3_mul( m_projection
, m_view
, vg_pv
);
829 vg_projection_update();
832 v2_copy( vg_mouse_ws
, world
.tile_pos
);
834 world
.tile_x
= floorf( world
.tile_pos
[0] );
835 world
.tile_y
= floorf( world
.tile_pos
[1] );
838 if( !world
.simulating
)
840 if( cell_interactive( (v2i
){ world
.tile_x
, world
.tile_y
} ))
842 world
.selected
= world
.tile_y
* world
.w
+ world
.tile_x
;
844 if( vg_get_button_down("primary") )
846 world
.data
[ world
.selected
].state
^= FLAG_CANAL
;
848 if( world
.data
[ world
.selected
].state
& FLAG_CANAL
)
849 sfx_set_playrnd( &audio_tile_mod
, &audio_system_sfx
, 3, 6 );
851 sfx_set_playrnd( &audio_tile_mod
, &audio_system_sfx
, 0, 3 );
853 map_reclassify( (v2i
){ world
.tile_x
-2, world
.tile_y
-2 },
854 (v2i
){ world
.tile_x
+2, world
.tile_y
+2 }, 1 );
860 else world
.selected
= -1;
862 // Simulation stop/start
863 if( vg_get_button_down("go") )
865 if( world
.simulating
)
867 world
.simulating
= 0;
868 world
.num_fishes
= 0;
871 for( int i
= 0; i
< arrlen( world
.io
); i
++ )
872 world
.io
[i
].recv_count
= 0;
874 vg_info( "Stopping simulation!\n" );
876 sfx_system_fadeout( &audio_system_balls_rolling
, 44100 );
880 vg_success( "Starting simulation!\n" );
882 sfx_set_playrnd( &audio_rolls
, &audio_system_balls_rolling
, 0, 1 );
884 world
.simulating
= 1;
885 world
.num_fishes
= 0;
887 world
.sim_start
= vg_time
;
889 for( int i
= 0; i
< world
.w
*world
.h
; i
++ )
891 world
.data
[ i
].state
&= ~FLAG_FLIP_FLOP
;
894 for( int i
= 0; i
< arrlen( world
.io
); i
++ )
895 world
.io
[i
].recv_count
= 0;
900 if( world
.simulating
)
902 while( world
.sim_frame
< (int)((vg_time
-world
.sim_start
)*2.0f
) )
904 //vg_info( "frame: %u\n", world.sim_frame );
905 sfx_set_playrnd( &audio_random
, &audio_system_balls_switching
, 0, 9 );
907 // Update splitter deltas
908 for( int i
= 0; i
< world
.h
*world
.w
; i
++ )
910 struct cell
*cell
= &world
.data
[i
];
911 if( cell
->config
== k_cell_type_split
)
913 cell
->state
&= ~FLAG_FLIP_ROTATING
;
917 // Update fish positions
918 for( int i
= 0; i
< world
.num_fishes
; i
++ )
920 struct fish
*fish
= &world
.fishes
[i
];
921 struct cell
*cell_current
= pcell( fish
->pos
);
923 if( fish
->alive
== -1 )
926 if( fish
->alive
!= 1 )
930 if( cell_current
->state
& FLAG_OUTPUT
)
932 for( int j
= 0; j
< arrlen( world
.io
); j
++ )
934 struct cell_terminal
*term
= &world
.io
[j
];
936 if( term
->id
== fish
->pos
[1]*world
.w
+ fish
->pos
[0] )
938 term
->recv
[ term
->recv_count
++ ] = fish
->payload
;
947 if( cell_current
->config
== k_cell_type_split
)
950 fish
->dir
[0] = cell_current
->state
&FLAG_FLIP_FLOP
?1:-1;
953 cell_current
->state
^= FLAG_FLIP_FLOP
;
955 else if( cell_current
->config
== k_cell_type_merge
)
963 struct cell
*cell_next
= pcell( (v2i
){ fish
->pos
[0]+fish
->dir
[0], fish
->pos
[1]+fish
->dir
[1] } );
964 if( !(cell_next
->state
& (FLAG_CANAL
|FLAG_OUTPUT
)) )
966 // Try other directions for valid, so down, left, right..
967 v2i dirs
[] = {{1,0},{-1,0},{0,-1}};
968 vg_info( "Trying some other directions...\n" );
970 for( int j
= 0; j
< vg_list_size(dirs
); j
++ )
972 if( (dirs
[j
][0] == -fish
->dir
[0]) && (dirs
[j
][1] == -fish
->dir
[1]) )
975 if( pcell( (v2i
){ fish
->pos
[0]+dirs
[j
][0], fish
->pos
[1]+dirs
[j
][1] } )->state
& (FLAG_CANAL
|FLAG_OUTPUT
) )
977 fish
->dir
[0] = dirs
[j
][0];
978 fish
->dir
[1] = dirs
[j
][1];
984 fish
->pos
[0] += fish
->dir
[0];
985 fish
->pos
[1] += fish
->dir
[1];
987 struct cell
*cell_entry
= pcell( fish
->pos
);
989 if( !(cell_entry
->state
& (FLAG_INPUT
|FLAG_CANAL
|FLAG_OUTPUT
) ))
995 if( cell_entry
->config
== k_cell_type_split
||
996 cell_entry
->config
== k_cell_type_ramp_right
||
997 cell_entry
->config
== k_cell_type_ramp_left
)
999 // Special death (FALL)
1000 v2_sub( fish
->physics_co
, fish
->physics_v
, fish
->physics_v
);
1001 v2_divs( fish
->physics_v
, vg_time_delta
, fish
->physics_v
);
1004 vg_warn( "Special death (fall)\n" );
1009 if( cell_entry
->config
== k_cell_type_split
)
1011 sfx_set_playrnd( &audio_splitter
, &audio_system_balls_important
, 0, 1 );
1012 cell_entry
->state
|= FLAG_FLIP_ROTATING
;
1017 // Check for collisions
1018 for( int i
= 0; i
< world
.num_fishes
; i
++ )
1020 if( world
.fishes
[i
].alive
== 1 )
1022 for( int j
= i
+1; j
< world
.num_fishes
; j
++ )
1024 if( (world
.fishes
[j
].alive
== 1) && (world
.fishes
[i
].pos
[0] == world
.fishes
[j
].pos
[0]) &&
1025 (world
.fishes
[i
].pos
[1] == world
.fishes
[j
].pos
[1]) )
1027 // Shatter death (+0.5s)
1028 world
.fishes
[i
].alive
= -1;
1029 world
.fishes
[j
].alive
= -1;
1030 world
.fishes
[i
].death_time
= 0.5f
;
1031 world
.fishes
[j
].death_time
= 0.5f
;
1038 for( int i
= 0; i
< arrlen( world
.io
); i
++ )
1040 struct cell_terminal
*term
= &world
.io
[ i
];
1041 int posx
= term
->id
% world
.w
;
1042 int posy
= (term
->id
- posx
)/world
.w
;
1043 int is_input
= world
.data
[ term
->id
].state
& FLAG_INPUT
;
1047 if( world
.sim_frame
< arrlen( term
->conditions
) )
1049 struct fish
*fish
= &world
.fishes
[world
.num_fishes
++];
1050 fish
->pos
[0] = posx
;
1051 fish
->pos
[1] = posy
;
1053 fish
->payload
= term
->conditions
[world
.sim_frame
];
1057 v2i dirs
[] = {{1,0},{-1,0},{0,-1}};
1058 for( int j
= 0; j
< vg_list_size(dirs
); j
++ )
1059 if( pcell( (v2i
){ posx
+dirs
[j
][0], posy
+dirs
[j
][1] } )->state
& FLAG_CANAL
)
1061 fish
->dir
[0] = dirs
[j
][0];
1062 fish
->dir
[1] = dirs
[j
][1];
1076 float scaled_time
= 0.0f
;
1077 scaled_time
= (vg_time
-world
.sim_start
)*2.0f
;
1078 world
.frame_lerp
= scaled_time
- (float)world
.sim_frame
;
1081 for( int i
= 0; i
< world
.num_fishes
; i
++ )
1083 struct fish
*fish
= &world
.fishes
[i
];
1085 if( fish
->alive
== 0 )
1088 if( fish
->alive
== -1 && (world
.frame_lerp
> fish
->death_time
) )
1089 continue; // Todo: particle thing?
1091 if( fish
->alive
== -2 )
1093 v2_muladds( fish
->physics_v
, (v2f
){ 0.0, -9.8f
}, vg_time_delta
, fish
->physics_v
);
1094 v2_muladds( fish
->physics_co
, fish
->physics_v
, vg_time_delta
, fish
->physics_co
);
1098 struct cell
*cell
= pcell(fish
->pos
);
1101 float t
= world
.frame_lerp
;
1103 v2_copy( fish
->physics_co
, fish
->physics_v
);
1105 switch( cell
->config
)
1108 if( fish
->dir
[0] == 1 )
1113 case 2: curve
= curve_2
; break;
1114 case 8: curve
= curve_8
; break;
1115 case 3: curve
= curve_3
; break;
1116 case 6: curve
= curve_6
; break;
1117 case 9: curve
= curve_9
; break;
1118 case 12: curve
= curve_12
; break;
1120 if( t
> curve_7_linear_section
)
1122 t
-= curve_7_linear_section
;
1123 t
*= (1.0f
/(1.0f
-curve_7_linear_section
));
1125 curve
= cell
->state
& FLAG_FLIP_FLOP
? curve_7
: curve_7_1
;
1129 default: curve
= NULL
; break;
1135 float t3
= t
* t
* t
;
1137 float cA
= 3.0f
*t2
- 3.0f
*t3
;
1138 float cB
= 3.0f
*t3
- 6.0f
*t2
+ 3.0f
*t
;
1139 float cC
= 3.0f
*t2
- t3
- 3.0f
*t
+ 1.0f
;
1141 fish
->physics_co
[0] = t3
*curve
[3][0] + cA
*curve
[2][0] + cB
*curve
[1][0] + cC
*curve
[0][0];
1142 fish
->physics_co
[1] = t3
*curve
[3][1] + cA
*curve
[2][1] + cB
*curve
[1][1] + cC
*curve
[0][1];
1143 fish
->physics_co
[0] += (float)fish
->pos
[0];
1144 fish
->physics_co
[1] += (float)fish
->pos
[1];
1149 origin
[0] = (float)fish
->pos
[0] + (float)fish
->dir
[0]*-0.5f
+ 0.5f
;
1150 origin
[1] = (float)fish
->pos
[1] + (float)fish
->dir
[1]*-0.5f
+ 0.5f
;
1152 fish
->physics_co
[0] = origin
[0] + (float)fish
->dir
[0]*t
;
1153 fish
->physics_co
[1] = origin
[1] + (float)fish
->dir
[1]*t
;
1160 static void render_tiles( v2i start
, v2i end
, v4f
const regular_colour
, v4f
const selected_colour
)
1162 v2i full_start
= { 0,0 };
1163 v2i full_end
= { world
.w
, world
.h
};
1165 if( !start
|| !end
)
1171 glUniform4fv( SHADER_UNIFORM( shader_tile_main
, "uColour" ), 1, regular_colour
);
1173 for( int y
= start
[1]; y
< end
[1]; y
++ )
1175 for( int x
= start
[0]; x
< end
[0]; x
++ )
1177 struct cell
*cell
= pcell((v2i
){x
,y
});
1178 int selected
= world
.selected
== y
*world
.w
+ x
;
1180 int tile_offsets
[][2] =
1182 {2, 0}, {0, 3}, {0, 2}, {2, 2},
1183 {1, 0}, {2, 3}, {3, 2}, {1, 3},
1184 {3, 1}, {0, 1}, {1, 2}, {2, 1},
1185 {1, 1}, {3, 3}, {2, 1}, {2, 1}
1188 int uv
[2] = { 3, 0 };
1190 if( cell
->state
& (FLAG_CANAL
|FLAG_INPUT
|FLAG_OUTPUT
) )
1192 uv
[0] = tile_offsets
[ cell
->config
][0];
1193 uv
[1] = tile_offsets
[ cell
->config
][1];
1196 glUniform4f( SHADER_UNIFORM( shader_tile_main
, "uOffset" ), (float)x
, (float)y
, uv
[0], uv
[1] );
1199 glUniform4fv( SHADER_UNIFORM( shader_tile_main
, "uColour" ), 1, selected_colour
);
1201 glUniform4fv( SHADER_UNIFORM( shader_tile_main
, "uColour" ), 1, regular_colour
);
1209 void vg_render(void)
1211 glViewport( 0,0, vg_window_x
, vg_window_y
);
1213 glDisable( GL_DEPTH_TEST
);
1214 glClearColor( 0.369768f
, 0.3654f
, 0.42f
, 1.0f
);
1215 glClear( GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
1217 v4f
const colour_default
= {1.0f
, 1.0f
, 1.0f
, 1.0f
};
1218 v4f
const colour_selected
= {0.90f
, 0.92f
, 1.0f
, 1.0f
};
1220 // TILE SET RENDERING
1221 // todo: just slam everything into a mesh...
1222 // when user modifies a tile the neighbours can be easily uploaded to gpu mem
1224 // Currently we're uploading a fair amount of data every frame anyway.
1225 // NOTE: this is for final optimisations ONLY!
1226 // ======================================================================
1228 use_mesh( &world
.tile
);
1234 SHADER_USE( shader_background
);
1235 glUniformMatrix3fv( SHADER_UNIFORM( shader_background
, "uPv" ), 1, GL_FALSE
, (float *)vg_pv
);
1237 glActiveTexture( GL_TEXTURE0
);
1238 glBindTexture( GL_TEXTURE_2D
, world
.background_data
);
1239 glUniform1i( SHADER_UNIFORM( shader_background
, "uTexMain" ), 0 );
1241 glUniform3f( SHADER_UNIFORM( shader_background
, "uOffset" ), -16, -16, 64 );
1242 glUniform1f( SHADER_UNIFORM( shader_background
, "uVariance" ), 0.02f
);
1244 glActiveTexture( GL_TEXTURE1
);
1245 glBindTexture( GL_TEXTURE_2D
, world
.random_samples
);
1246 glUniform1i( SHADER_UNIFORM( shader_background
, "uSamplerNoise" ), 1 );
1253 SHADER_USE( shader_tile_main
);
1256 m2x2_identity( subtransform
);
1257 glUniformMatrix2fv( SHADER_UNIFORM( shader_tile_main
, "uSubTransform" ), 1, GL_FALSE
, (float *)subtransform
);
1258 glUniformMatrix3fv( SHADER_UNIFORM( shader_tile_main
, "uPv" ), 1, GL_FALSE
, (float *)vg_pv
);
1259 glUniform1f( SHADER_UNIFORM( shader_tile_main
, "uGhost" ), 0.0f
);
1260 glUniform1f( SHADER_UNIFORM( shader_tile_main
, "uForeground" ), 0.0f
);
1263 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
1264 glBlendEquation(GL_FUNC_ADD
);
1267 vg_tex2d_bind( &tex_tile_data
, 0 );
1268 glUniform1i( SHADER_UNIFORM( shader_tile_main
, "uTexGlyphs" ), 0 );
1270 vg_tex2d_bind( &tex_wood
, 1 );
1271 glUniform1i( SHADER_UNIFORM( shader_tile_main
, "uTexWood" ), 1 );
1273 render_tiles( NULL
, NULL
, colour_default
, colour_default
);
1277 SHADER_USE( shader_ball
);
1278 glUniformMatrix3fv( SHADER_UNIFORM( shader_ball
, "uPv" ), 1, GL_FALSE
, (float *)vg_pv
);
1280 vg_tex2d_bind( &tex_ball
, 0 );
1281 glUniform1i( SHADER_UNIFORM( shader_ball
, "uTexMain" ), 0 );
1284 if( world
.simulating
)
1286 for( int i
= 0; i
< world
.num_fishes
; i
++ )
1288 struct fish
*fish
= &world
.fishes
[i
];
1290 if( fish
->alive
== 0 )
1293 if( fish
->alive
== -1 && (world
.frame_lerp
> fish
->death_time
) )
1296 v4f dot_colour
= { 0.0f
, 0.0f
, 0.0f
, 1.0f
};
1297 colour_code_v3( fish
->payload
, dot_colour
);
1299 glUniform3fv( SHADER_UNIFORM( shader_ball
, "uColour" ), 1, dot_colour
);
1300 glUniform2fv( SHADER_UNIFORM( shader_ball
, "uOffset" ), 1, fish
->physics_co
);
1305 SHADER_USE( shader_tile_main
);
1308 vg_tex2d_bind( &tex_tile_data
, 0 );
1309 glUniform1i( SHADER_UNIFORM( shader_tile_main
, "uTexGlyphs" ), 0 );
1311 vg_tex2d_bind( &tex_wood
, 1 );
1312 glUniform1i( SHADER_UNIFORM( shader_tile_main
, "uTexWood" ), 1 );
1314 glUniform1f( SHADER_UNIFORM( shader_tile_main
, "uForeground" ), 1.0f
);
1315 render_tiles( NULL
, NULL
, colour_default
, colour_selected
);
1319 for( int y
= 0; y
< world
.h
; y
++ )
1321 for( int x
= 0; x
< world
.w
; x
++ )
1323 struct cell
*cell
= pcell((v2i
){x
,y
});
1325 if( cell
->config
== k_cell_type_split
)
1327 float rotation
= cell
->state
& FLAG_FLIP_FLOP
? vg_rad( -45.0f
): vg_rad( 45.0f
);
1329 if( cell
->state
& FLAG_FLIP_ROTATING
)
1331 if( (world
.frame_lerp
> curve_7_linear_section
) )
1333 float const rotation_speed
= 0.4f
;
1334 if( (world
.frame_lerp
< 1.0f
-rotation_speed
) )
1336 float t
= world
.frame_lerp
- curve_7_linear_section
;
1337 t
*= -2.0f
* (1.0f
/(1.0f
-(curve_7_linear_section
+rotation_speed
)));
1347 m2x2_create_rotation( subtransform
, rotation
);
1349 glUniformMatrix2fv( SHADER_UNIFORM( shader_tile_main
, "uSubTransform" ), 1, GL_FALSE
, (float *)subtransform
);
1350 glUniform4f( SHADER_UNIFORM( shader_tile_main
, "uOffset" ), (float)x
, (float)y
+ 0.125f
, 0.0f
, 0.0f
);
1357 if( world
.selected
!= -1 && !(world
.data
[ world
.selected
].state
& FLAG_CANAL
) )
1359 v2i new_begin
= { world
.tile_x
- 2, world
.tile_y
- 2 };
1360 v2i new_end
= { world
.tile_x
+ 2, world
.tile_y
+ 2 };
1362 world
.data
[ world
.selected
].state
^= FLAG_CANAL
;
1363 map_reclassify( new_begin
, new_end
, 0 );
1365 m2x2_identity( subtransform
);
1366 glUniform1f( SHADER_UNIFORM( shader_tile_main
, "uGhost" ), 1.0f
);
1367 glUniformMatrix2fv( SHADER_UNIFORM( shader_tile_main
, "uSubTransform" ), 1, GL_FALSE
, (float *)subtransform
);
1368 glUniform2fv( SHADER_UNIFORM( shader_tile_main
, "uMousePos" ), 1, world
.tile_pos
);
1370 render_tiles( new_begin
, new_end
, colour_default
, colour_default
);
1372 world
.data
[ world
.selected
].state
^= FLAG_CANAL
;
1373 map_reclassify( new_begin
, new_end
, 0 );
1376 //glDisable(GL_BLEND);
1378 glDisable(GL_BLEND
);
1380 SHADER_USE( shader_tile_colour
);
1381 glUniformMatrix3fv( SHADER_UNIFORM( shader_tile_colour
, "uPv" ), 1, GL_FALSE
, (float *)vg_pv
);
1382 use_mesh( &world
.circle
);
1384 int const filled_start
= 0;
1385 int const filled_count
= 32;
1386 int const empty_start
= 32;
1387 int const empty_count
= 32*2;
1390 for( int i
= 0; i
< arrlen( world
.io
); i
++ )
1392 struct cell_terminal
*term
= &world
.io
[ i
];
1393 int posx
= term
->id
% world
.w
;
1394 int posy
= (term
->id
- posx
)/world
.w
;
1395 int is_input
= world
.data
[ term
->id
].state
& FLAG_INPUT
;
1397 v4f dot_colour
= { 0.0f
, 0.0f
, 0.0f
, 1.0f
};
1399 for( int j
= 0; j
< arrlen( term
->conditions
); j
++ )
1401 float y_offset
= is_input
? 1.2f
: -0.2f
;
1402 glUniform3f( SHADER_UNIFORM( shader_tile_colour
, "uOffset" ), (float)posx
+ 0.2f
+ 0.2f
* (float)j
, (float)posy
+ y_offset
, 0.1f
);
1406 colour_code_v3( term
->conditions
[j
], dot_colour
);
1407 glUniform4fv( SHADER_UNIFORM( shader_tile_colour
, "uColour" ), 1, dot_colour
);
1409 // Draw filled if tick not passed, draw empty if empty
1410 if( world
.sim_frame
> j
)
1411 draw_mesh( empty_start
, empty_count
);
1413 draw_mesh( filled_start
, filled_count
);
1417 if( term
->recv_count
> j
)
1419 colour_code_v3( term
->recv
[j
], dot_colour
);
1420 v3_muls( dot_colour
, 0.8f
, dot_colour
);
1421 glUniform4fv( SHADER_UNIFORM( shader_tile_colour
, "uColour" ), 1, dot_colour
);
1423 draw_mesh( filled_start
, filled_count
);
1426 colour_code_v3( term
->conditions
[j
], dot_colour
);
1427 glUniform4fv( SHADER_UNIFORM( shader_tile_colour
, "uColour" ), 1, dot_colour
);
1429 draw_mesh( empty_start
, empty_count
);
1434 if( world
.simulating
)
1436 glUniform4f( SHADER_UNIFORM( shader_tile_colour
, "uColour" ), 0.0f
, 0.0f
, 0.0f
, 1.0f
);
1437 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
);
1438 draw_mesh( filled_start
, filled_count
);
1441 // Level selection UI
1442 float ratio
= ((float)vg_window_x
/(float)vg_window_y
);
1444 m3x3f ui_view
= M3X3_IDENTITY
;
1445 m3x3_scale( ui_view
, (v3f
){ 1.0f
, -ratio
, 1.0f
} );
1446 glUniformMatrix3fv( SHADER_UNIFORM( shader_tile_colour
, "uPv" ), 1, GL_FALSE
, (float *)ui_view
);
1448 // Calculate mouse in UIsp
1449 v3f mouse_ui_space
= { ((float)vg_mouse
[0] / (float)(vg_window_x
)) * 2.0f
- 1.0f
,
1450 (((float)vg_mouse
[1] / (float)(vg_window_y
)) * 2.0f
- 1.0f
)*(1.0f
/ratio
), 0.0125f
};
1452 // Get selected level
1453 const float selection_scale
= 0.05f
;
1454 int const level_count
= vg_list_size( level_pack_1
);
1455 int level_select
= -1;
1457 if( mouse_ui_space
[0] <= -0.9f
)
1459 float levels_range
= (float)level_count
*selection_scale
*0.6f
;
1460 float level_offset
= ((mouse_ui_space
[1] + levels_range
) / levels_range
) * 0.5f
* (float)level_count
;
1461 level_select
= floorf( level_offset
);
1464 if( level_select
>= 0 && level_select
< vg_list_size( level_pack_1
) )
1466 glUniform4f( SHADER_UNIFORM( shader_tile_colour
, "uColour" ), 0.369768f
, 0.3654f
, 0.42f
, 1.0f
);
1468 use_mesh( &world
.tile
);
1469 glUniform3f( SHADER_UNIFORM( shader_tile_colour
, "uOffset" ),
1471 (-(float)level_count
+ (float)level_select
* 2.0f
) * selection_scale
* 0.6f
,
1476 use_mesh( &world
.circle
);
1479 if( vg_get_button_down( "primary" ) )
1481 console_changelevel( 1, level_pack_1
+ level_select
);
1485 glUniform4f( SHADER_UNIFORM( shader_tile_colour
, "uColour" ), 0.4f
, 0.39f
, 0.45f
, 1.0f
);
1488 for( int i
= 0; i
< level_count
; i
++ )
1490 v3f level_ui_space
= {
1492 (-(float)level_count
+ (float)i
* 2.0f
) * selection_scale
* 0.6f
+ selection_scale
* 0.5f
,
1493 selection_scale
* 0.5f
1496 float scale
= vg_clampf( 1.0f
- fabsf(level_ui_space
[1] - mouse_ui_space
[1]) * 2.0f
, 0.9f
, 1.0f
);
1497 level_ui_space
[2] *= scale
;
1499 glUniform3fv( SHADER_UNIFORM( shader_tile_colour
, "uOffset" ), 1, level_ui_space
);
1500 draw_mesh( empty_start
, empty_count
);
1503 glUniform3fv( SHADER_UNIFORM( shader_tile_colour
, "uOffset" ), 1, mouse_ui_space
);
1504 draw_mesh( empty_start
, empty_count
);