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