added score counters/career stuff
[fishladder.git] / fishladder.c
1 // Copyright (C) 2021 Harry Godden (hgn) - All Rights Reserved
2
3 //#define VG_STEAM
4 #include "vg/vg.h"
5 #include "fishladder_resources.h"
6
7 const char *level_pack_1[] = {
8 "level0",
9 "level1",
10 "level2",
11 "level3",
12 "level4",
13 "level5"
14 };
15
16 #pragma pack(push,1)
17 struct career_state
18 {
19 u32 version;
20
21 struct career_level
22 {
23 u32 score;
24 u32 time;
25 u32 completed;
26 }
27 levels[ vg_list_size( level_pack_1 ) ];
28 }
29 career = { .version = 1 };
30 #pragma pack(pop)
31
32 static void career_serialize(void)
33 {
34 vg_asset_write( "sav/game.sav", &career, sizeof( struct career_state ) );
35 }
36
37 static void career_load(void)
38 {
39 i64 sz;
40 struct career_state *cr = vg_asset_read_s( "sav/game.sav", &sz );
41
42 memset( (void*)career.levels, 0, vg_list_size(level_pack_1) * sizeof(struct career_level) );
43
44 if( cr )
45 {
46 if( sz > sizeof( struct career_state ) )
47 vg_warn( "This save file is too big! Some levels will be lost\n" );
48
49 if( sz <= offsetof( struct career_state, levels ) )
50 {
51 vg_error( "This save file is too small to have a header\n" );
52 free( cr );
53 return;
54 }
55
56 u32 const size_header = offsetof(struct career_state, levels);
57 u32 const size_levels = sizeof(struct career_state)-size_header;
58 u32 const size_levels_input = sz - size_header;
59
60 memcpy( (void*)career.levels, (void*)cr->levels, size_levels );
61
62 if( sz < sizeof( struct career_state ) )
63 {
64 memset( ((void*)career.levels) + size_levels_input, 0, size_levels-size_levels_input );
65 }
66
67 free( cr );
68 vg_success( "Loaded save file... Info:\n" );
69
70 for( int i = 0; i < vg_list_size( career.levels ); i ++ )
71 {
72 struct career_level *lvl = &career.levels[i];
73 vg_info( "Score: %u, Time: %u, Completed: %u\n", lvl->score, lvl->time, lvl->completed );
74 }
75 }
76 else
77 {
78 vg_info( "No save file... Using blank one\n" );
79 }
80 }
81
82 m3x3f m_projection;
83 m3x3f m_view;
84 m3x3f m_mdl;
85
86 #define FLAG_INPUT 0x1
87 #define FLAG_OUTPUT 0x2
88 #define FLAG_CANAL 0x4
89 #define FLAG_WALL 0x8
90 #define FLAG_FLIP_FLOP 0x100
91 #define FLAG_FLIP_ROTATING 0x200
92
93 /*
94 0000 0 | 0001 1 | 0010 2 | 0011 3
95 | | | | |
96 X | X= | X | X=
97 | | |
98 0100 4 | 0101 5 | 0110 6 | 0111 7
99 | | | | |
100 =X | =X= | =X | =X=
101 | | |
102 1000 8 | 1001 9 | 1010 10 | 1011 11
103 | | | | |
104 X | X= | X | X=
105 | | | | | | |
106 1100 12 | 1101 13 | 1110 14 | 1111 15
107 | | | | |
108 =X | =X= | =X | =X=
109 | | | | | | |
110 */
111
112 enum cell_type
113 {
114 k_cell_type_ramp_right = 3,
115 k_cell_type_ramp_left = 6,
116 k_cell_type_split = 7,
117 k_cell_type_merge = 13
118 };
119
120 v3f colour_sets[] =
121 { { 0.9f, 0.6f, 0.20f },
122 { 0.2f, 0.9f, 0.14f },
123 { 0.4f, 0.8f, 1.00f } };
124
125 static void colour_code_v3( char cc, v3f target )
126 {
127 if( cc >= 'a' && cc <= 'z' )
128 {
129 int id = cc - 'a';
130
131 if( id < vg_list_size( colour_sets ) )
132 {
133 v3_copy( colour_sets[ id ], target );
134 return;
135 }
136 }
137
138 v3_copy( (v3f){0.0f,0.0f,0.0f}, target );
139 }
140
141 struct mesh
142 {
143 GLuint vao, vbo;
144 u32 elements;
145 };
146
147 static void init_mesh( struct mesh *m, float const *tris, u32 length )
148 {
149 m->elements = length/3;
150 glGenVertexArrays( 1, &m->vao );
151 glGenBuffers( 1, &m->vbo );
152
153 glBindVertexArray( m->vao );
154 glBindBuffer( GL_ARRAY_BUFFER, m->vbo );
155 glBufferData( GL_ARRAY_BUFFER, length*sizeof(float), tris, GL_STATIC_DRAW );
156
157 glVertexAttribPointer( 0, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(float), (void*)0 );
158 glEnableVertexAttribArray( 0 );
159
160 VG_CHECK_GL();
161 }
162
163 static void free_mesh( struct mesh *m )
164 {
165 glDeleteVertexArrays( 1, &m->vao );
166 glDeleteBuffers( 1, &m->vbo );
167 }
168
169 static void draw_mesh( int const start, int const count )
170 {
171 glDrawArrays( GL_TRIANGLES, start*3, count*3 );
172 }
173
174 static void use_mesh( struct mesh *m )
175 {
176 glBindVertexArray( m->vao );
177 }
178
179 struct world
180 {
181 struct cell
182 {
183 u32 state;
184 u8 config;
185 }
186 *data;
187
188 u32 frame;
189
190 u32 sim_frame;
191 float sim_start;
192 int simulating;
193
194 float frame_lerp;
195
196 struct cell_terminal
197 {
198 // TODO: Split into input/output structures
199 char *conditions;
200 char recv[12];
201 int recv_count;
202 int id;
203 }
204 *io;
205
206 u32 w, h;
207
208 struct mesh tile, circle, numbers;
209
210 GLuint background_data;
211 GLuint random_samples;
212
213 int selected, tile_x, tile_y;
214 v2f tile_pos;
215
216 struct fish
217 {
218 v2i pos;
219 v2i dir;
220 int alive;
221 char payload;
222 float death_time;
223 v2f physics_v;
224 v2f physics_co;
225 }
226 fishes[16];
227
228 int num_fishes;
229
230 char map_name[128];
231 struct career_level *ptr_career_level;
232
233 u32 score;
234 u32 completed;
235 u32 time;
236
237 } world = {};
238
239 static void map_free(void)
240 {
241 for( int i = 0; i < arrlen( world.io ); i ++ )
242 arrfree( world.io[ i ].conditions );
243
244 arrfree( world.data );
245 arrfree( world.io );
246
247 world.w = 0;
248 world.h = 0;
249 world.data = NULL;
250 world.io = NULL;
251 world.score = 0;
252 world.time = 0;
253 world.completed = 0;
254 }
255
256 static void map_reclassify( v2i start, v2i end, int update_texbuffer );
257 static int map_load( const char *str, const char *name )
258 {
259 map_free();
260
261 char const *c = str;
262
263 // Scan for width
264 for(;; world.w ++)
265 {
266 if( str[world.w] == ';' )
267 break;
268 else if( !str[world.w] )
269 {
270 vg_error( "Unexpected EOF when parsing level\n" );
271 return 0;
272 }
273 }
274
275 struct cell *row = arraddnptr( world.data, world.w );
276 int cx = 0;
277 int reg_start = 0, reg_end = 0;
278
279 for(;;)
280 {
281 if( !*c )
282 break;
283
284 if( *c == ';' )
285 {
286 c ++;
287
288 // Parse attribs
289 if( *c != '\n' )
290 {
291 while( *c )
292 {
293 if( reg_start < reg_end )
294 {
295 if( *c >= 'a' && *c <= 'z' )
296 {
297 arrpush( world.io[ reg_start ].conditions, *c );
298 }
299 else
300 {
301 if( *c == ',' || *c == '\n' )
302 {
303 reg_start ++;
304
305 if( *c == '\n' )
306 break;
307 }
308 else
309 {
310 vg_error( "Unkown attribute '%c' (row: %u)\n", *c, world.h );
311 return 0;
312 }
313 }
314 }
315 else
316 {
317 vg_error( "Too many values to assign (row: %u)\n", world.h );
318 return 0;
319 }
320
321 c ++;
322 }
323 }
324
325 if( reg_start != reg_end )
326 {
327 vg_error( "Not enough values assigned (row: %u, %u of %u)\n", world.h, reg_start, reg_end );
328 return 0;
329 }
330
331 if( cx != world.w )
332 {
333 vg_error( "Not enough cells to match previous row definition (row: %u, %u<%u)\n", world.h, cx, world.w );
334 return 0;
335 }
336
337 row = arraddnptr( world.data, world.w );
338 cx = 0;
339 world.h ++;
340 reg_end = reg_start = arrlen( world.io );
341 }
342 else
343 {
344 if( cx == world.w )
345 {
346 vg_error( "Too many cells to match previous row definition (row: %u, %u>%u)\n", world.h, cx, world.w );
347 return 0;
348 }
349
350 // Tile initialization
351 // row[ cx ] .. etc
352 struct cell *cell = &row[ cx ];
353
354 if( *c == '+' || *c == '-' )
355 {
356 struct cell_terminal term = { .id = cx + world.h*world.w };
357 arrpush( world.io, term );
358 cell->state = *c == '+'? FLAG_INPUT: FLAG_OUTPUT;
359 reg_end ++;
360 }
361 else if( *c == '#' ) cell->state = FLAG_WALL;
362 else if( *c == '*' ) { cell->state = FLAG_CANAL; world.score ++; }
363 else cell->state = 0x00;
364
365 cx ++;
366 }
367
368 c ++;
369 }
370
371 // Update data texture to fill out the background
372 {
373 u8 info_buffer[64*64*4];
374 for( int i = 0; i < 64*64; i ++ )
375 {
376 u8 *px = &info_buffer[i*4];
377 px[0] = 255;
378 px[1] = 0;
379 px[2] = 0;
380 px[3] = 0;
381 }
382
383 glBindTexture( GL_TEXTURE_2D, world.background_data );
384 glTexSubImage2D( GL_TEXTURE_2D, 0, 0, 0, 64, 64, GL_RGBA, GL_UNSIGNED_BYTE, info_buffer );
385 }
386
387 map_reclassify( NULL, NULL, 1 );
388 vg_success( "Map '%s' loaded! (%u:%u)\n", name, world.w, world.h );
389
390 strncpy( world.map_name, name, vg_list_size( world.map_name )-1 );
391 return 1;
392 }
393
394 static struct cell *pcell( v2i pos )
395 {
396 return &world.data[ pos[1]*world.w + pos[0] ];
397 }
398
399 static void map_serialize( FILE *stream )
400 {
401 for( int y = 0; y < world.h; y ++ )
402 {
403 for( int x = 0; x < world.w; x ++ )
404 {
405 struct cell *cell = pcell( (v2i){ x, y } );
406
407 if( cell->state & FLAG_WALL ) fputc( '#', stream );
408 else if( cell->state & FLAG_INPUT ) fputc( '+', stream );
409 else if( cell->state & FLAG_OUTPUT ) fputc( '-', stream );
410 else if( cell->state & FLAG_CANAL ) fputc( '*', stream );
411 else fputc( ' ', stream );
412 }
413
414 fputc( ';', stream );
415
416 int terminal_write_count = 0;
417
418 for( int x = 0; x < world.w; x ++ )
419 {
420 for( int i = 0; i < arrlen( world.io ); i ++ )
421 {
422 struct cell_terminal *term = &world.io[i];
423 if( term->id == y*world.w+x )
424 {
425 if( terminal_write_count )
426 fputc( ',', stream );
427 terminal_write_count ++;
428
429 for( int j = 0; j < arrlen( term->conditions ); j ++ )
430 fputc( term->conditions[j], stream );
431 }
432 }
433 }
434
435 fputc( '\n', stream );
436 }
437 }
438
439 int main( int argc, char *argv[] )
440 {
441 vg_init( argc, argv, "Fish (Marbles Computer) Ladder Simulator 2022 | N,M: change level | SPACE: Test | LeftClick: Toggle tile" );
442 }
443
444 static int console_save_map( int argc, char const *argv[] )
445 {
446 char map_path[ 256 ];
447
448 strcpy( map_path, "sav/" );
449 strcat( map_path, world.map_name );
450 strcat( map_path, ".map" );
451
452 FILE *test_writer = fopen( map_path, "wb" );
453 if( test_writer )
454 {
455 map_serialize( test_writer );
456
457 fclose( test_writer );
458 return 1;
459 }
460 else
461 {
462 vg_error( "Unable to open stream for writing\n" );
463 return 0;
464 }
465 }
466
467 static int console_load_map( int argc, char const *argv[] )
468 {
469 char map_path[ 256 ];
470
471 if( argc >= 1 )
472 {
473 // try from saves
474 strcpy( map_path, "sav/" );
475 strcat( map_path, argv[0] );
476 strcat( map_path, ".map" );
477
478 char *text_source = vg_textasset_read( map_path );
479
480 if( !text_source )
481 {
482 strcpy( map_path, "maps/" );
483 strcat( map_path, argv[0] );
484 strcat( map_path, ".map" );
485
486 text_source = vg_textasset_read( map_path );
487 }
488
489 if( text_source )
490 {
491 map_load( text_source, argv[0] );
492 free( text_source );
493
494 // Update career link
495 world.ptr_career_level = NULL;
496
497 for( int i = 0; i < vg_list_size( level_pack_1 ); i ++ )
498 {
499 if( !strcmp( level_pack_1[i], argv[0] ) )
500 {
501 world.ptr_career_level = career.levels + i;
502 break;
503 }
504 }
505
506 return 1;
507 }
508 else
509 {
510 vg_error( "Missing maps '%s'\n", argv[0] );
511 return 0;
512 }
513 }
514 else
515 {
516 vg_error( "Missing argument <map_path>\n" );
517 return 0;
518 }
519 }
520
521 static void simulation_stop(void)
522 {
523 world.simulating = 0;
524 world.num_fishes = 0;
525 world.sim_frame = 0;
526
527 for( int i = 0; i < arrlen( world.io ); i ++ )
528 world.io[i].recv_count = 0;
529
530 sfx_system_fadeout( &audio_system_balls_rolling, 44100 );
531
532 vg_info( "Stopping simulation!\n" );
533 }
534
535 static int console_changelevel( int argc, char const *argv[] )
536 {
537 if( argc >= 1 )
538 {
539 // Save current level
540 if( console_save_map( 0, NULL ) )
541 if( console_load_map( argc, argv ) )
542 {
543 simulation_stop();
544 return 1;
545 }
546 }
547 else
548 {
549 vg_error( "Missing argument <map_path>\n" );
550 }
551
552 return 0;
553 }
554
555 void vg_start(void)
556 {
557 vg_function_push( (struct vg_cmd){
558 .name = "map_write",
559 .function = console_save_map
560 });
561
562 vg_function_push( (struct vg_cmd){
563 .name = "map_load",
564 .function = console_load_map
565 });
566
567 vg_function_push( (struct vg_cmd){
568 .name = "changelevel",
569 .function = console_changelevel
570 });
571
572 // Quad mesh
573 {
574 float quad_mesh[] =
575 {
576 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f,
577 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, 0.0f,
578
579 0.0f, 0.0f, 0.0f, 1.0f, 4.0f, 1.0f,
580 0.0f, 0.0f, 4.0f, 1.0f, 4.0f, 0.0f
581 };
582
583 init_mesh( &world.tile, quad_mesh, vg_list_size(quad_mesh) );
584 }
585
586 // Circle mesh
587 {
588 float circle_mesh[32*6*3];
589 int res = vg_list_size( circle_mesh ) / (6*3);
590
591 for( int i = 0; i < res; i ++ )
592 {
593 v2f v0 = { sinf( ((float)i/(float)res)*VG_TAUf ), cosf( ((float)i/(float)res)*VG_TAUf ) };
594 v2f v1 = { sinf( ((float)(i+1)/(float)res)*VG_TAUf ), cosf( ((float)(i+1)/(float)res)*VG_TAUf ) };
595
596 circle_mesh[ i*6+0 ] = 0.0f;
597 circle_mesh[ i*6+1 ] = 0.0f;
598
599 v2_copy( v0, circle_mesh + 32*6 + i*12 );
600 v2_muls( v0, 0.8f, circle_mesh + 32*6 + i*12+2 );
601 v2_copy( v1, circle_mesh + 32*6 + i*12+4 );
602
603 v2_copy( v1, circle_mesh + 32*6 + i*12+6 );
604 v2_muls( v1, 0.8f, circle_mesh + 32*6 + i*12+8 );
605 v2_muls( v0, 0.8f, circle_mesh + 32*6 + i*12+10 );
606
607 v2_copy( v0, circle_mesh + i*6+4 );
608 v2_copy( v1, circle_mesh + i*6+2 );
609 v2_copy( v0, circle_mesh+i*6+4 );
610 v2_copy( v1, circle_mesh+i*6+2 );
611 }
612
613 init_mesh( &world.circle, circle_mesh, vg_list_size( circle_mesh ) );
614 }
615
616 // Numbers mesh
617 {
618 init_mesh( &world.numbers,
619 MESH_NUMBERS_BUFFER,
620 vg_list_size( MESH_NUMBERS_BUFFER )
621 );
622
623 for( int i = 0; i < 10; i ++ )
624 {
625 vg_info( "offset: %u, length: %u\n", MESH_NUMBERS_OFFSETS[i][0], MESH_NUMBERS_OFFSETS[i][1] );
626 }
627 }
628
629 // Create info data texture
630 {
631 glGenTextures( 1, &world.background_data );
632 glBindTexture( GL_TEXTURE_2D, world.background_data );
633 glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, 64, 64, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL );
634 vg_tex2d_nearest();
635 }
636
637 // Create random smaples texture
638 {
639 u8 *data = malloc(512*512*2);
640 for( int i = 0; i < 512*512*2; i ++ )
641 data[ i ] = rand()/(RAND_MAX/255);
642
643 glGenTextures( 1, &world.random_samples );
644 glBindTexture( GL_TEXTURE_2D, world.random_samples );
645 glTexImage2D( GL_TEXTURE_2D, 0, GL_RG, 512, 512, 0, GL_RG, GL_UNSIGNED_BYTE, data );
646 vg_tex2d_linear();
647 vg_tex2d_repeat();
648
649 free( data );
650 }
651
652 resource_load_main();
653
654 // Restore gamestate
655 career_load();
656 console_load_map( 1, level_pack_1 );
657 }
658
659 void vg_free(void)
660 {
661 console_save_map( 0, NULL );
662 career_serialize();
663
664 resource_free_main();
665
666 glDeleteTextures( 1, &world.background_data );
667 glDeleteTextures( 1, &world.random_samples );
668
669 free_mesh( &world.tile );
670 free_mesh( &world.circle );
671 free_mesh( &world.numbers );
672
673 map_free();
674 }
675
676 static int cell_interactive( v2i co )
677 {
678 // Bounds check
679 if( co[0] < 2 || co[0] >= world.w-2 || co[1] < 2 || co[1] >= world.h-2 )
680 return 0;
681
682 // Flags check
683 if( world.data[ world.w*co[1] + co[0] ].state & (FLAG_WALL|FLAG_INPUT|FLAG_OUTPUT) )
684 return 0;
685
686 // List of 3x3 configurations that we do not allow
687 static u32 invalid_src[][9] =
688 {
689 { 0,1,0,
690 1,1,1,
691 0,1,0
692 },
693 { 0,0,0,
694 0,1,1,
695 0,1,1
696 },
697 { 0,0,0,
698 1,1,0,
699 1,1,0
700 },
701 { 0,1,1,
702 0,1,1,
703 0,0,0
704 },
705 { 1,1,0,
706 1,1,0,
707 0,0,0
708 },
709 { 0,1,0,
710 0,1,1,
711 0,1,0
712 },
713 { 0,1,0,
714 1,1,0,
715 0,1,0
716 }
717 };
718
719 // Statically compile invalid configurations into bitmasks
720 static u32 invalid[ vg_list_size(invalid_src) ];
721
722 for( int i = 0; i < vg_list_size(invalid_src); i ++ )
723 {
724 u32 comped = 0x00;
725
726 for( int j = 0; j < 3; j ++ )
727 for( int k = 0; k < 3; k ++ )
728 comped |= invalid_src[i][ j*3+k ] << ((j*5)+k);
729
730 invalid[i] = comped;
731 }
732
733 // Extract 5x5 grid surrounding tile
734 u32 blob = 0x1000;
735 for( int y = co[1]-2; y < co[1]+3; y ++ )
736 for( int x = co[0]-2; x < co[0]+3; x ++ )
737 {
738 struct cell *cell = pcell((v2i){x,y});
739
740 if( cell && (cell->state & (FLAG_CANAL|FLAG_INPUT|FLAG_OUTPUT)) )
741 blob |= 0x1 << ((y-(co[1]-2))*5 + x-(co[0]-2));
742 }
743
744 // Run filter over center 3x3 grid to check for invalid configurations
745 int kernel[] = { 0, 1, 2, 5, 6, 7, 10, 11, 12 };
746 for( int i = 0; i < vg_list_size(kernel); i ++ )
747 {
748 if( blob & (0x1 << (6+kernel[i])) )
749 {
750 u32 window = blob >> kernel[i];
751
752 for( int j = 0; j < vg_list_size(invalid); j ++ )
753 if((window & invalid[j]) == invalid[j])
754 return 0;
755 }
756 }
757
758 return 1;
759 }
760
761 static void map_reclassify( v2i start, v2i end, int update_texbuffer )
762 {
763 v2i full_start = { 1,1 };
764 v2i full_end = { world.w-1, world.h-1 };
765
766 if( !start || !end )
767 {
768 start = full_start;
769 end = full_end;
770 }
771
772 // Texture data
773 u8 info_buffer[64*64*4];
774 u32 pixel_id = 0;
775
776 int px0 = vg_max( start[0], full_start[0] ),
777 px1 = vg_min( end[0], full_end[0] ),
778 py0 = vg_max( start[1], full_start[1] ),
779 py1 = vg_min( end[1], full_end[1] );
780
781 for( int y = py0; y < py1; y ++ )
782 {
783 for( int x = px0; x < px1; x ++ )
784 {
785 struct cell *cell = pcell((v2i){x,y});
786
787 v2i dirs[] = {{1,0},{0,1},{-1,0},{0,-1}};
788
789 u8 height = 0;
790 u8 config = 0x00;
791
792 if( cell->state & (FLAG_CANAL|FLAG_INPUT|FLAG_OUTPUT) )
793 {
794 for( int i = 0; i < vg_list_size( dirs ); i ++ )
795 {
796 struct cell *neighbour = pcell((v2i){x+dirs[i][0], y+dirs[i][1]});
797 if( neighbour->state & (FLAG_CANAL|FLAG_INPUT|FLAG_OUTPUT) )
798 config |= 0x1 << i;
799 }
800
801 height = 128;
802 }
803 else
804 {
805 if( cell->state & FLAG_WALL )
806 height = 255;
807
808 config = 0xF;
809 }
810
811 pcell((v2i){x,y})->config = config;
812
813 u8 *info_px = &info_buffer[ (pixel_id ++)*4 ];
814 info_px[0] = height;
815 info_px[1] = cell->state & FLAG_WALL? 0: 255;
816 info_px[2] = 0;
817 info_px[3] = 0;
818 }
819 }
820
821 if( update_texbuffer )
822 {
823 glBindTexture( GL_TEXTURE_2D, world.background_data );
824 glTexSubImage2D( GL_TEXTURE_2D, 0, px0 + 16, py0 + 16, px1-px0, py1-py0, GL_RGBA, GL_UNSIGNED_BYTE, info_buffer );
825 }
826 }
827
828 v2f const curve_3[] = {{0.5f,1.0f},{0.5f,0.625f},{0.625f,0.5f},{1.0f,0.5f}};
829 v2f const curve_6[] = {{0.5f,1.0f},{0.5f,0.625f},{0.375f,0.5f},{0.0f,0.5f}};
830 v2f const curve_9[] = {{1.0f,0.5f},{0.625f,0.5f},{0.5f,0.375f},{0.5f,0.0f}};
831 v2f const curve_12[]= {{0.0f,0.5f},{0.375f,0.5f},{0.5f,0.375f},{0.5f,0.0f}};
832
833 v2f const curve_2[] = {{0.5f,1.0f},{0.5f,0.8f},{0.5f,0.3f},{0.5f,0.2f}};
834 v2f const curve_8[] = {{0.5f,0.8f},{0.5f,0.5f},{0.5f,0.3f},{0.5f,0.0f}};
835
836 v2f const curve_7[] = {{0.5f,0.8438f},{0.875f,0.8438f},{0.625f,0.5f},{1.0f,0.5f}};
837 v2f const curve_7_1[] = {{0.5f,0.8438f},{1.0f-0.875f,0.8438f},{1.0-0.625f,0.5f},{0.0f,0.5f}};
838
839 float const curve_7_linear_section = 0.1562f;
840
841 void vg_update(void)
842 {
843 // Fit within screen
844
845 float r1 = (float)vg_window_y / (float)vg_window_x,
846 r2 = (float)world.h / (float)world.w,
847 size;
848
849 size = ( r2 < r1? (float)world.w * 0.5f: ((float)world.h * 0.5f) / r1 ) + 2.5f;
850 m3x3_projection( m_projection, -size, size, -size*r1, size*r1 );
851
852 v3f origin;
853 origin[0] = floorf( -0.5f * world.w );
854 origin[1] = floorf( -0.5f * world.h );
855 origin[2] = 0.0f;
856
857 m3x3_identity( m_view );
858 m3x3_translate( m_view, origin );
859 m3x3_mul( m_projection, m_view, vg_pv );
860 vg_projection_update();
861
862 // Input stuff
863 v2_copy( vg_mouse_ws, world.tile_pos );
864
865 world.tile_x = floorf( world.tile_pos[0] );
866 world.tile_y = floorf( world.tile_pos[1] );
867
868 // Tilemap editing
869 if( !world.simulating )
870 {
871 if( cell_interactive( (v2i){ world.tile_x, world.tile_y } ))
872 {
873 world.selected = world.tile_y * world.w + world.tile_x;
874
875 if( vg_get_button_down("primary") )
876 {
877 world.data[ world.selected ].state ^= FLAG_CANAL;
878
879 if( world.data[ world.selected ].state & FLAG_CANAL )
880 {
881 sfx_set_playrnd( &audio_tile_mod, &audio_system_sfx, 3, 6 );
882 world.score ++;
883 }
884 else
885 {
886 sfx_set_playrnd( &audio_tile_mod, &audio_system_sfx, 0, 3 );
887 world.score --;
888 }
889
890 map_reclassify( (v2i){ world.tile_x -2, world.tile_y -2 },
891 (v2i){ world.tile_x +2, world.tile_y +2 }, 1 );
892 }
893 }
894 else
895 world.selected = -1;
896 }
897 else world.selected = -1;
898
899 // Simulation stop/start
900 if( vg_get_button_down("go") )
901 {
902 if( world.simulating )
903 {
904 simulation_stop();
905 }
906 else
907 {
908 vg_success( "Starting simulation!\n" );
909
910 sfx_set_playrnd( &audio_rolls, &audio_system_balls_rolling, 0, 1 );
911
912 world.simulating = 1;
913 world.num_fishes = 0;
914 world.sim_frame = 0;
915 world.sim_start = vg_time;
916
917 for( int i = 0; i < world.w*world.h; i ++ )
918 {
919 world.data[ i ].state &= ~FLAG_FLIP_FLOP;
920 }
921
922 for( int i = 0; i < arrlen( world.io ); i ++ )
923 world.io[i].recv_count = 0;
924 }
925 }
926
927 // Fish ticks
928 if( world.simulating )
929 {
930 while( world.sim_frame < (int)((vg_time-world.sim_start)*2.0f) )
931 {
932 //vg_info( "frame: %u\n", world.sim_frame );
933 sfx_set_playrnd( &audio_random, &audio_system_balls_switching, 0, 9 );
934
935 // Update splitter deltas
936 for( int i = 0; i < world.h*world.w; i ++ )
937 {
938 struct cell *cell = &world.data[i];
939 if( cell->config == k_cell_type_split )
940 {
941 cell->state &= ~FLAG_FLIP_ROTATING;
942 }
943 }
944
945 int alive_count = 0;
946
947 // Update fish positions
948 for( int i = 0; i < world.num_fishes; i ++ )
949 {
950 struct fish *fish = &world.fishes[i];
951 struct cell *cell_current = pcell( fish->pos );
952
953 if( fish->alive == -1 )
954 fish->alive = 0;
955
956 if( fish->alive != 1 )
957 continue;
958
959 // Apply to output
960 if( cell_current->state & FLAG_OUTPUT )
961 {
962 for( int j = 0; j < arrlen( world.io ); j ++ )
963 {
964 struct cell_terminal *term = &world.io[j];
965
966 if( term->id == fish->pos[1]*world.w + fish->pos[0] )
967 {
968 term->recv[ term->recv_count ++ ] = fish->payload;
969 break;
970 }
971 }
972
973 fish->alive = 0;
974 continue;
975 }
976
977 if( cell_current->config == k_cell_type_split )
978 {
979 // Flip flop L/R
980 fish->dir[0] = cell_current->state&FLAG_FLIP_FLOP?1:-1;
981 fish->dir[1] = 0;
982
983 cell_current->state ^= FLAG_FLIP_FLOP;
984 }
985 else if( cell_current->config == k_cell_type_merge )
986 {
987 // Can only move up
988 fish->dir[0] = 0;
989 fish->dir[1] = -1;
990 }
991 else
992 {
993 struct cell *cell_next = pcell( (v2i){ fish->pos[0]+fish->dir[0], fish->pos[1]+fish->dir[1] } );
994 if( !(cell_next->state & (FLAG_CANAL|FLAG_OUTPUT)) )
995 {
996 // Try other directions for valid, so down, left, right..
997 v2i dirs[] = {{1,0},{-1,0},{0,-1}};
998 vg_info( "Trying some other directions...\n" );
999
1000 for( int j = 0; j < vg_list_size(dirs); j ++ )
1001 {
1002 if( (dirs[j][0] == -fish->dir[0]) && (dirs[j][1] == -fish->dir[1]) )
1003 continue;
1004
1005 if( pcell( (v2i){ fish->pos[0]+dirs[j][0], fish->pos[1]+dirs[j][1] } )->state & (FLAG_CANAL|FLAG_OUTPUT) )
1006 {
1007 fish->dir[0] = dirs[j][0];
1008 fish->dir[1] = dirs[j][1];
1009 }
1010 }
1011 }
1012 }
1013
1014 fish->pos[0] += fish->dir[0];
1015 fish->pos[1] += fish->dir[1];
1016
1017 struct cell *cell_entry = pcell( fish->pos );
1018
1019 if( !(cell_entry->state & (FLAG_INPUT|FLAG_CANAL|FLAG_OUTPUT) ))
1020 fish->alive = 0;
1021 else
1022 {
1023 if( fish->dir[0] )
1024 {
1025 if( cell_entry->config == k_cell_type_split ||
1026 cell_entry->config == k_cell_type_ramp_right ||
1027 cell_entry->config == k_cell_type_ramp_left )
1028 {
1029 // Special death (FALL)
1030 v2_sub( fish->physics_co, fish->physics_v, fish->physics_v );
1031 v2_divs( fish->physics_v, vg_time_delta, fish->physics_v );
1032
1033 fish->alive = -2;
1034 vg_warn( "Special death (fall)\n" );
1035 continue;
1036 }
1037 }
1038
1039 if( cell_entry->config == k_cell_type_split )
1040 {
1041 sfx_set_playrnd( &audio_splitter, &audio_system_balls_important, 0, 1 );
1042 cell_entry->state |= FLAG_FLIP_ROTATING;
1043 }
1044 }
1045
1046 if( fish->alive )
1047 alive_count ++;
1048 }
1049
1050 // Check for collisions
1051 for( int i = 0; i < world.num_fishes; i ++ )
1052 {
1053 if( world.fishes[i].alive == 1 )
1054 {
1055 for( int j = i+1; j < world.num_fishes; j ++ )
1056 {
1057 if( (world.fishes[j].alive == 1) && (world.fishes[i].pos[0] == world.fishes[j].pos[0]) &&
1058 (world.fishes[i].pos[1] == world.fishes[j].pos[1]) )
1059 {
1060 // Shatter death (+0.5s)
1061 world.fishes[i].alive = -1;
1062 world.fishes[j].alive = -1;
1063 world.fishes[i].death_time = 0.5f;
1064 world.fishes[j].death_time = 0.5f;
1065 }
1066 }
1067 }
1068 }
1069
1070 // Spawn fishes
1071 for( int i = 0; i < arrlen( world.io ); i ++ )
1072 {
1073 struct cell_terminal *term = &world.io[ i ];
1074 int posx = term->id % world.w;
1075 int posy = (term->id - posx)/world.w;
1076 int is_input = world.data[ term->id ].state & FLAG_INPUT;
1077
1078 if( is_input )
1079 {
1080 if( world.sim_frame < arrlen( term->conditions ) )
1081 {
1082 struct fish *fish = &world.fishes[world.num_fishes++];
1083 fish->pos[0] = posx;
1084 fish->pos[1] = posy;
1085 fish->alive = 1;
1086 fish->payload = term->conditions[world.sim_frame];
1087
1088 int can_spawn = 0;
1089
1090 v2i dirs[] = {{1,0},{-1,0},{0,-1}};
1091 for( int j = 0; j < vg_list_size(dirs); j ++ )
1092 if( pcell( (v2i){ posx+dirs[j][0], posy+dirs[j][1] } )->state & FLAG_CANAL )
1093 {
1094 fish->dir[0] = dirs[j][0];
1095 fish->dir[1] = dirs[j][1];
1096 can_spawn = 1;
1097 break;
1098 }
1099
1100 if( !can_spawn )
1101 world.num_fishes--;
1102 else
1103 alive_count ++;
1104 }
1105 }
1106 }
1107
1108 if( alive_count == 0 )
1109 {
1110 world.completed = 1;
1111
1112 for( int i = 0; i < arrlen( world.io ); i ++ )
1113 {
1114 struct cell_terminal *term = &world.io[ i ];
1115 int is_input = world.data[ term->id ].state & FLAG_INPUT;
1116
1117 if( !is_input )
1118 {
1119 if( term->recv_count == arrlen( term->conditions ) )
1120 {
1121 for( int j = 0; j < arrlen( term->conditions ); j ++ )
1122 {
1123 if( term->recv[j] != term->conditions[j] )
1124 {
1125 world.completed = 0;
1126 break;
1127 }
1128 }
1129 }
1130 else
1131 {
1132 world.completed = 0;
1133 break;
1134 }
1135 }
1136 }
1137
1138 if( world.completed )
1139 {
1140 vg_success( "Level passed!\n" );
1141
1142 u32 score = 0;
1143 for( int i = 0; i < world.w*world.h; i ++ )
1144 if( world.data[ i ].state & FLAG_CANAL )
1145 score ++;
1146
1147 world.score = score;
1148 world.time = world.sim_frame;
1149 }
1150 else
1151 {
1152 vg_error( "Level failed :(\n" );
1153 }
1154
1155 // Copy into career data
1156 if( world.ptr_career_level )
1157 {
1158 world.ptr_career_level->score = world.score;
1159 world.ptr_career_level->time = world.time;
1160 world.ptr_career_level->completed = world.completed;
1161 }
1162
1163 simulation_stop(); // TODO: Async?
1164 break;
1165 }
1166
1167 world.sim_frame ++;
1168 }
1169
1170 float scaled_time = 0.0f;
1171 scaled_time = (vg_time-world.sim_start)*2.0f;
1172 world.frame_lerp = scaled_time - (float)world.sim_frame;
1173
1174 // Update positions
1175 for( int i = 0; i < world.num_fishes; i ++ )
1176 {
1177 struct fish *fish = &world.fishes[i];
1178
1179 if( fish->alive == 0 )
1180 continue;
1181
1182 if( fish->alive == -1 && (world.frame_lerp > fish->death_time) )
1183 continue; // Todo: particle thing?
1184
1185 if( fish->alive == -2 )
1186 {
1187 v2_muladds( fish->physics_v, (v2f){ 0.0, -9.8f }, vg_time_delta, fish->physics_v );
1188 v2_muladds( fish->physics_co, fish->physics_v, vg_time_delta, fish->physics_co );
1189 }
1190 else
1191 {
1192 struct cell *cell = pcell(fish->pos);
1193 v2f const *curve;
1194
1195 float t = world.frame_lerp;
1196
1197 v2_copy( fish->physics_co, fish->physics_v );
1198
1199 switch( cell->config )
1200 {
1201 case 13:
1202 if( fish->dir[0] == 1 )
1203 curve = curve_12;
1204 else
1205 curve = curve_9;
1206 break;
1207 case 2: curve = curve_2; break;
1208 case 8: curve = curve_8; break;
1209 case 3: curve = curve_3; break;
1210 case 6: curve = curve_6; break;
1211 case 9: curve = curve_9; break;
1212 case 12: curve = curve_12; break;
1213 case 7:
1214 if( t > curve_7_linear_section )
1215 {
1216 t -= curve_7_linear_section;
1217 t *= (1.0f/(1.0f-curve_7_linear_section));
1218
1219 curve = cell->state & FLAG_FLIP_FLOP? curve_7: curve_7_1;
1220 }
1221 else curve = NULL;
1222 break;
1223 default: curve = NULL; break;
1224 }
1225
1226 if( curve )
1227 {
1228 float t2 = t * t;
1229 float t3 = t * t * t;
1230
1231 float cA = 3.0f*t2 - 3.0f*t3;
1232 float cB = 3.0f*t3 - 6.0f*t2 + 3.0f*t;
1233 float cC = 3.0f*t2 - t3 - 3.0f*t + 1.0f;
1234
1235 fish->physics_co[0] = t3*curve[3][0] + cA*curve[2][0] + cB*curve[1][0] + cC*curve[0][0];
1236 fish->physics_co[1] = t3*curve[3][1] + cA*curve[2][1] + cB*curve[1][1] + cC*curve[0][1];
1237 fish->physics_co[0] += (float)fish->pos[0];
1238 fish->physics_co[1] += (float)fish->pos[1];
1239 }
1240 else
1241 {
1242 v2f origin;
1243 origin[0] = (float)fish->pos[0] + (float)fish->dir[0]*-0.5f + 0.5f;
1244 origin[1] = (float)fish->pos[1] + (float)fish->dir[1]*-0.5f + 0.5f;
1245
1246 fish->physics_co[0] = origin[0] + (float)fish->dir[0]*t;
1247 fish->physics_co[1] = origin[1] + (float)fish->dir[1]*t;
1248 }
1249 }
1250 }
1251 }
1252 }
1253
1254 static void render_tiles( v2i start, v2i end, v4f const regular_colour, v4f const selected_colour )
1255 {
1256 v2i full_start = { 0,0 };
1257 v2i full_end = { world.w, world.h };
1258
1259 if( !start || !end )
1260 {
1261 start = full_start;
1262 end = full_end;
1263 }
1264
1265 glUniform4fv( SHADER_UNIFORM( shader_tile_main, "uColour" ), 1, regular_colour );
1266
1267 for( int y = start[1]; y < end[1]; y ++ )
1268 {
1269 for( int x = start[0]; x < end[0]; x ++ )
1270 {
1271 struct cell *cell = pcell((v2i){x,y});
1272 int selected = world.selected == y*world.w + x;
1273
1274 int tile_offsets[][2] =
1275 {
1276 {2, 0}, {0, 3}, {0, 2}, {2, 2},
1277 {1, 0}, {2, 3}, {3, 2}, {1, 3},
1278 {3, 1}, {0, 1}, {1, 2}, {2, 1},
1279 {1, 1}, {3, 3}, {2, 1}, {2, 1}
1280 };
1281
1282 int uv[2] = { 3, 0 };
1283
1284 if( cell->state & (FLAG_CANAL|FLAG_INPUT|FLAG_OUTPUT) )
1285 {
1286 uv[0] = tile_offsets[ cell->config ][0];
1287 uv[1] = tile_offsets[ cell->config ][1];
1288 } else continue;
1289
1290 glUniform4f( SHADER_UNIFORM( shader_tile_main, "uOffset" ), (float)x, (float)y, uv[0], uv[1] );
1291 if( selected )
1292 {
1293 glUniform4fv( SHADER_UNIFORM( shader_tile_main, "uColour" ), 1, selected_colour );
1294 draw_mesh( 0, 2 );
1295 glUniform4fv( SHADER_UNIFORM( shader_tile_main, "uColour" ), 1, regular_colour );
1296 }
1297 else
1298 draw_mesh( 0, 2 );
1299 }
1300 }
1301 }
1302
1303 static void draw_numbers( v3f coord, int number )
1304 {
1305 v3f pos;
1306 v3_copy( coord, pos );
1307 int digits[8]; int i = 0;
1308
1309 while( number > 0 && i < 8 )
1310 {
1311 digits[i ++] = number % 10;
1312 number = number / 10;
1313 }
1314
1315 for( int j = 0; j < i; j ++ )
1316 {
1317 glUniform3fv( SHADER_UNIFORM( shader_tile_colour, "uOffset" ), 1, pos );
1318 draw_mesh( MESH_NUMBERS_OFFSETS[digits[i-j-1]][0], MESH_NUMBERS_OFFSETS[digits[i-j-1]][1] );
1319 pos[0] += pos[2] * 0.75f;
1320 }
1321 }
1322
1323 void vg_render(void)
1324 {
1325 glViewport( 0,0, vg_window_x, vg_window_y );
1326
1327 glDisable( GL_DEPTH_TEST );
1328 glClearColor( 0.369768f, 0.3654f, 0.42f, 1.0f );
1329 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
1330
1331 v4f const colour_default = {1.0f, 1.0f, 1.0f, 1.0f};
1332 v4f const colour_selected = {0.90f, 0.92f, 1.0f, 1.0f};
1333
1334 // TILE SET RENDERING
1335 // todo: just slam everything into a mesh...
1336 // when user modifies a tile the neighbours can be easily uploaded to gpu mem
1337 // in ~3 subBuffers
1338 // Currently we're uploading a fair amount of data every frame anyway.
1339 // NOTE: this is for final optimisations ONLY!
1340 // ======================================================================
1341
1342 use_mesh( &world.tile );
1343
1344 // Draw background
1345
1346 if(1){
1347
1348 SHADER_USE( shader_background );
1349 glUniformMatrix3fv( SHADER_UNIFORM( shader_background, "uPv" ), 1, GL_FALSE, (float *)vg_pv );
1350
1351 glActiveTexture( GL_TEXTURE0 );
1352 glBindTexture( GL_TEXTURE_2D, world.background_data );
1353 glUniform1i( SHADER_UNIFORM( shader_background, "uTexMain" ), 0 );
1354
1355 glUniform3f( SHADER_UNIFORM( shader_background, "uOffset" ), -16, -16, 64 );
1356 glUniform1f( SHADER_UNIFORM( shader_background, "uVariance" ), 0.02f );
1357
1358 glActiveTexture( GL_TEXTURE1 );
1359 glBindTexture( GL_TEXTURE_2D, world.random_samples );
1360 glUniform1i( SHADER_UNIFORM( shader_background, "uSamplerNoise" ), 1 );
1361
1362 draw_mesh( 0, 2 );
1363
1364 }
1365
1366
1367 SHADER_USE( shader_tile_main );
1368
1369 m2x2f subtransform;
1370 m2x2_identity( subtransform );
1371 glUniformMatrix2fv( SHADER_UNIFORM( shader_tile_main, "uSubTransform" ), 1, GL_FALSE, (float *)subtransform );
1372 glUniformMatrix3fv( SHADER_UNIFORM( shader_tile_main, "uPv" ), 1, GL_FALSE, (float *)vg_pv );
1373 glUniform1f( SHADER_UNIFORM( shader_tile_main, "uGhost" ), 0.0f );
1374 glUniform1f( SHADER_UNIFORM( shader_tile_main, "uForeground" ), 0.0f );
1375
1376 glEnable(GL_BLEND);
1377 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1378 glBlendEquation(GL_FUNC_ADD);
1379
1380 // Bind textures
1381 vg_tex2d_bind( &tex_tile_data, 0 );
1382 glUniform1i( SHADER_UNIFORM( shader_tile_main, "uTexGlyphs" ), 0 );
1383
1384 vg_tex2d_bind( &tex_wood, 1 );
1385 glUniform1i( SHADER_UNIFORM( shader_tile_main, "uTexWood" ), 1 );
1386
1387 render_tiles( NULL, NULL, colour_default, colour_default );
1388
1389
1390
1391 SHADER_USE( shader_ball );
1392 glUniformMatrix3fv( SHADER_UNIFORM( shader_ball, "uPv" ), 1, GL_FALSE, (float *)vg_pv );
1393
1394 vg_tex2d_bind( &tex_ball, 0 );
1395 glUniform1i( SHADER_UNIFORM( shader_ball, "uTexMain" ), 0 );
1396
1397 // Draw 'fish'
1398 if( world.simulating )
1399 {
1400 for( int i = 0; i < world.num_fishes; i ++ )
1401 {
1402 struct fish *fish = &world.fishes[i];
1403
1404 if( fish->alive == 0 )
1405 continue;
1406
1407 if( fish->alive == -1 && (world.frame_lerp > fish->death_time) )
1408 continue;
1409
1410 v4f dot_colour = { 0.0f, 0.0f, 0.0f, 1.0f };
1411 colour_code_v3( fish->payload, dot_colour );
1412
1413 glUniform3fv( SHADER_UNIFORM( shader_ball, "uColour" ), 1, dot_colour );
1414 glUniform2fv( SHADER_UNIFORM( shader_ball, "uOffset" ), 1, fish->physics_co );
1415 draw_mesh( 0, 32 );
1416 }
1417 }
1418
1419 SHADER_USE( shader_tile_main );
1420
1421 // Bind textures
1422 vg_tex2d_bind( &tex_tile_data, 0 );
1423 glUniform1i( SHADER_UNIFORM( shader_tile_main, "uTexGlyphs" ), 0 );
1424
1425 vg_tex2d_bind( &tex_wood, 1 );
1426 glUniform1i( SHADER_UNIFORM( shader_tile_main, "uTexWood" ), 1 );
1427
1428 glUniform1f( SHADER_UNIFORM( shader_tile_main, "uForeground" ), 1.0f );
1429 render_tiles( NULL, NULL, colour_default, colour_selected );
1430
1431 // Draw splitters
1432 for( int y = 2; y < world.h-2; y ++ )
1433 {
1434 for( int x = 2; x < world.w-2; x ++ )
1435 {
1436 struct cell *cell = pcell((v2i){x,y});
1437
1438 if( cell->config == k_cell_type_split )
1439 {
1440 float rotation = cell->state & FLAG_FLIP_FLOP? vg_rad( -45.0f ): vg_rad( 45.0f );
1441
1442 if( cell->state & FLAG_FLIP_ROTATING )
1443 {
1444 if( (world.frame_lerp > curve_7_linear_section) )
1445 {
1446 float const rotation_speed = 0.4f;
1447 if( (world.frame_lerp < 1.0f-rotation_speed) )
1448 {
1449 float t = world.frame_lerp - curve_7_linear_section;
1450 t *= -2.0f * (1.0f/(1.0f-(curve_7_linear_section+rotation_speed)));
1451 t += 1.0f;
1452
1453 rotation *= t;
1454 }
1455 else
1456 rotation *= -1.0f;
1457 }
1458 }
1459
1460 m2x2_create_rotation( subtransform, rotation );
1461
1462 glUniformMatrix2fv( SHADER_UNIFORM( shader_tile_main, "uSubTransform" ), 1, GL_FALSE, (float *)subtransform );
1463 glUniform4f( SHADER_UNIFORM( shader_tile_main, "uOffset" ), (float)x, (float)y + 0.125f, 0.0f, 0.0f );
1464 draw_mesh( 0, 2 );
1465 }
1466 }
1467 }
1468
1469 // Edit overlay
1470 if( world.selected != -1 && !(world.data[ world.selected ].state & FLAG_CANAL) )
1471 {
1472 v2i new_begin = { world.tile_x - 2, world.tile_y - 2 };
1473 v2i new_end = { world.tile_x + 2, world.tile_y + 2 };
1474
1475 world.data[ world.selected ].state ^= FLAG_CANAL;
1476 map_reclassify( new_begin, new_end, 0 );
1477
1478 m2x2_identity( subtransform );
1479 glUniform1f( SHADER_UNIFORM( shader_tile_main, "uGhost" ), 1.0f );
1480 glUniformMatrix2fv( SHADER_UNIFORM( shader_tile_main, "uSubTransform" ), 1, GL_FALSE, (float *)subtransform );
1481 glUniform2fv( SHADER_UNIFORM( shader_tile_main, "uMousePos" ), 1, world.tile_pos );
1482
1483 render_tiles( new_begin, new_end, colour_default, colour_default );
1484
1485 world.data[ world.selected ].state ^= FLAG_CANAL;
1486 map_reclassify( new_begin, new_end, 0 );
1487 }
1488
1489 //glDisable(GL_BLEND);
1490
1491 glDisable(GL_BLEND);
1492
1493 SHADER_USE( shader_tile_colour );
1494 glUniformMatrix3fv( SHADER_UNIFORM( shader_tile_colour, "uPv" ), 1, GL_FALSE, (float *)vg_pv );
1495 use_mesh( &world.circle );
1496
1497 int const filled_start = 0;
1498 int const filled_count = 32;
1499 int const empty_start = 32;
1500 int const empty_count = 32*2;
1501
1502 // Draw i/o arrays
1503 for( int i = 0; i < arrlen( world.io ); i ++ )
1504 {
1505 struct cell_terminal *term = &world.io[ i ];
1506 int posx = term->id % world.w;
1507 int posy = (term->id - posx)/world.w;
1508 int is_input = world.data[ term->id ].state & FLAG_INPUT;
1509
1510 v4f dot_colour = { 0.0f, 0.0f, 0.0f, 1.0f };
1511
1512 for( int j = 0; j < arrlen( term->conditions ); j ++ )
1513 {
1514 float y_offset = is_input? 1.2f: -0.2f;
1515 glUniform3f( SHADER_UNIFORM( shader_tile_colour, "uOffset" ), (float)posx + 0.2f + 0.2f * (float)j, (float)posy + y_offset, 0.1f );
1516
1517 if( is_input )
1518 {
1519 colour_code_v3( term->conditions[j], dot_colour );
1520 glUniform4fv( SHADER_UNIFORM( shader_tile_colour, "uColour" ), 1, dot_colour );
1521
1522 // Draw filled if tick not passed, draw empty if empty
1523 if( world.sim_frame > j )
1524 draw_mesh( empty_start, empty_count );
1525 else
1526 draw_mesh( filled_start, filled_count );
1527 }
1528 else
1529 {
1530 if( term->recv_count > j )
1531 {
1532 colour_code_v3( term->recv[j], dot_colour );
1533 v3_muls( dot_colour, 0.8f, dot_colour );
1534 glUniform4fv( SHADER_UNIFORM( shader_tile_colour, "uColour" ), 1, dot_colour );
1535
1536 draw_mesh( filled_start, filled_count );
1537 }
1538
1539 colour_code_v3( term->conditions[j], dot_colour );
1540 glUniform4fv( SHADER_UNIFORM( shader_tile_colour, "uColour" ), 1, dot_colour );
1541
1542 draw_mesh( empty_start, empty_count );
1543 }
1544 }
1545 }
1546
1547 if( world.simulating )
1548 {
1549 glUniform4f( SHADER_UNIFORM( shader_tile_colour, "uColour" ), 0.0f, 0.0f, 0.0f, 1.0f );
1550 glUniform3f( SHADER_UNIFORM( shader_tile_colour, "uOffset" ), -0.5f + cosf( vg_time * 4.0f ) * 0.2f, sinf( vg_time * 4.0f ) * 0.2f + (float)world.h * 0.5f, 0.05f );
1551 draw_mesh( filled_start, filled_count );
1552 }
1553
1554 // Draw score
1555 float const score_bright = 1.25f;
1556 glUniform4f( SHADER_UNIFORM( shader_tile_colour, "uColour" ),
1557 0.4f*score_bright, 0.39f*score_bright, 0.45f*score_bright, 1.0f );
1558
1559 use_mesh( &world.numbers );
1560 draw_numbers( (v3f){ 2.0f, (float)world.h-1.875f, 0.3333f }, world.score );
1561
1562 // Level selection UI
1563 use_mesh( &world.circle );
1564 float ratio = ((float)vg_window_x/(float)vg_window_y);
1565
1566 m3x3f ui_view = M3X3_IDENTITY;
1567 m3x3_scale( ui_view, (v3f){ 1.0f, ratio, 1.0f } );
1568 glUniformMatrix3fv( SHADER_UNIFORM( shader_tile_colour, "uPv" ), 1, GL_FALSE, (float *)ui_view );
1569
1570 // Calculate mouse in UIsp
1571 v3f mouse_ui_space = { ((float)vg_mouse[0] / (float)(vg_window_x)) * 2.0f - 1.0f,
1572 (((float)vg_mouse[1] / (float)(vg_window_y)) * 2.0f - 1.0f)*(-1.0f/ratio), 0.0125f };
1573
1574 // Get selected level
1575 const float selection_scale = 0.05f;
1576 int const level_count = vg_list_size( level_pack_1 );
1577 int level_select = -1;
1578
1579 if( mouse_ui_space[0] <= -0.8f )
1580 {
1581 float levels_range = (float)level_count*selection_scale*0.6f;
1582 float level_offset = ((-mouse_ui_space[1] + levels_range) / levels_range) * 0.5f * (float)level_count;
1583 level_select = ceilf( level_offset );
1584
1585 // Draw selector
1586 if( level_select >= 0 && level_select < vg_list_size( level_pack_1 ) )
1587 {
1588 glUniform4f( SHADER_UNIFORM( shader_tile_colour, "uColour" ), 0.369768f, 0.3654f, 0.42f, 1.0f );
1589
1590 use_mesh( &world.tile );
1591 glUniform3f( SHADER_UNIFORM( shader_tile_colour, "uOffset" ),
1592 -1.0f,
1593 ((float)level_count - (float)level_select * 2.0f ) * selection_scale * 0.6f,
1594 selection_scale
1595 );
1596 draw_mesh( 2, 2 );
1597
1598 use_mesh( &world.circle );
1599
1600 if( vg_get_button_down( "primary" ) )
1601 {
1602 console_changelevel( 1, level_pack_1 + level_select );
1603 }
1604 }
1605 }
1606 else mouse_ui_space[1] = INFINITY;
1607
1608 glUniform4f( SHADER_UNIFORM( shader_tile_colour, "uColour" ), 0.4f, 0.39f, 0.45f, 1.0f );
1609
1610 // Draw levels
1611 for( int i = 0; i < level_count; i ++ )
1612 {
1613 struct career_level *clevel = &career.levels[i];
1614
1615 v3f level_ui_space = {
1616 -0.97f,
1617 ((float)level_count - (float)i * 2.0f ) * selection_scale * 0.6f + selection_scale * 0.5f,
1618 selection_scale * 0.5f
1619 };
1620
1621 float scale = vg_clampf( 1.0f - fabsf(level_ui_space[1] - mouse_ui_space[1]) * 2.0f, 0.9f, 1.0f );
1622 level_ui_space[2] *= scale;
1623
1624 glUniform3fv( SHADER_UNIFORM( shader_tile_colour, "uOffset" ), 1, level_ui_space );
1625
1626 if( clevel->completed )
1627 draw_mesh( filled_start, filled_count );
1628 else
1629 draw_mesh( empty_start, empty_count );
1630 }
1631
1632 //use_mesh( &world.numbers );
1633 //draw_numbers( (v3f){ 0.0f, -0.5f, 0.1f }, 128765 );
1634 }
1635
1636 void vg_ui(void)
1637 {
1638 //ui_test();
1639 }