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