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