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