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