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