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