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