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