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