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