level selector
[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 vg_start(void)
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 console_save_map( 0, NULL );
1038 career_serialize();
1039
1040 resource_free_main();
1041
1042 glDeleteTextures( 1, &world.background_data );
1043 glDeleteTextures( 1, &world.random_samples );
1044
1045 glDeleteVertexArrays( 1, &world.wire.vao );
1046 glDeleteBuffers( 1, &world.wire.vbo );
1047 glDeleteBuffers( 1, &world.wire.ebo );
1048
1049 free_mesh( &world.tile );
1050 free_mesh( &world.circle );
1051 free_mesh( &world.numbers );
1052
1053 map_free();
1054 }
1055
1056 static int world_check_pos_ok( v2i co )
1057 {
1058 return (co[0] < 2 || co[0] >= world.w-2 || co[1] < 2 || co[1] >= world.h-2)? 0: 1;
1059 }
1060
1061 static int cell_interactive( v2i co )
1062 {
1063 // Bounds check
1064 if( !world_check_pos_ok( co ) )
1065 return 0;
1066
1067 // Flags check
1068 if( world.data[ world.w*co[1] + co[0] ].state & (FLAG_WALL|FLAG_INPUT|FLAG_OUTPUT) )
1069 return 0;
1070
1071 // List of 3x3 configurations that we do not allow
1072 static u32 invalid_src[][9] =
1073 {
1074 { 0,1,0,
1075 1,1,1,
1076 0,1,0
1077 },
1078 { 0,0,0,
1079 0,1,1,
1080 0,1,1
1081 },
1082 { 0,0,0,
1083 1,1,0,
1084 1,1,0
1085 },
1086 { 0,1,1,
1087 0,1,1,
1088 0,0,0
1089 },
1090 { 1,1,0,
1091 1,1,0,
1092 0,0,0
1093 },
1094 { 0,1,0,
1095 0,1,1,
1096 0,1,0
1097 },
1098 { 0,1,0,
1099 1,1,0,
1100 0,1,0
1101 }
1102 };
1103
1104 // Statically compile invalid configurations into bitmasks
1105 static u32 invalid[ vg_list_size(invalid_src) ];
1106
1107 for( int i = 0; i < vg_list_size(invalid_src); i ++ )
1108 {
1109 u32 comped = 0x00;
1110
1111 for( int j = 0; j < 3; j ++ )
1112 for( int k = 0; k < 3; k ++ )
1113 comped |= invalid_src[i][ j*3+k ] << ((j*5)+k);
1114
1115 invalid[i] = comped;
1116 }
1117
1118 // Extract 5x5 grid surrounding tile
1119 u32 blob = 0x1000;
1120 for( int y = co[1]-2; y < co[1]+3; y ++ )
1121 for( int x = co[0]-2; x < co[0]+3; x ++ )
1122 {
1123 struct cell *cell = pcell((v2i){x,y});
1124
1125 if( cell && (cell->state & (FLAG_CANAL|FLAG_INPUT|FLAG_OUTPUT)) )
1126 blob |= 0x1 << ((y-(co[1]-2))*5 + x-(co[0]-2));
1127 }
1128
1129 // Run filter over center 3x3 grid to check for invalid configurations
1130 int kernel[] = { 0, 1, 2, 5, 6, 7, 10, 11, 12 };
1131 for( int i = 0; i < vg_list_size(kernel); i ++ )
1132 {
1133 if( blob & (0x1 << (6+kernel[i])) )
1134 {
1135 u32 window = blob >> kernel[i];
1136
1137 for( int j = 0; j < vg_list_size(invalid); j ++ )
1138 if((window & invalid[j]) == invalid[j])
1139 return 0;
1140 }
1141 }
1142
1143 return 1;
1144 }
1145
1146 static void map_reclassify( v2i start, v2i end, int update_texbuffer )
1147 {
1148 v2i full_start = { 1,1 };
1149 v2i full_end = { world.w-1, world.h-1 };
1150
1151 if( !start || !end )
1152 {
1153 start = full_start;
1154 end = full_end;
1155 }
1156
1157 // Texture data
1158 u8 info_buffer[64*64*4];
1159 u32 pixel_id = 0;
1160
1161 int px0 = vg_max( start[0], full_start[0] ),
1162 px1 = vg_min( end[0], full_end[0] ),
1163 py0 = vg_max( start[1], full_start[1] ),
1164 py1 = vg_min( end[1], full_end[1] );
1165
1166 for( int y = py0; y < py1; y ++ )
1167 {
1168 for( int x = px0; x < px1; x ++ )
1169 {
1170 struct cell *cell = pcell((v2i){x,y});
1171
1172 v2i dirs[] = {{1,0},{0,1},{-1,0},{0,-1}};
1173
1174 u8 height = 0;
1175 u8 config = 0x00;
1176
1177 if( cell->state & (FLAG_CANAL|FLAG_INPUT|FLAG_OUTPUT) )
1178 {
1179 for( int i = 0; i < vg_list_size( dirs ); i ++ )
1180 {
1181 struct cell *neighbour = pcell((v2i){x+dirs[i][0], y+dirs[i][1]});
1182 if( neighbour->state & (FLAG_CANAL|FLAG_INPUT|FLAG_OUTPUT) )
1183 config |= 0x1 << i;
1184 }
1185
1186 height = 128;
1187 }
1188 else
1189 {
1190 if( cell->state & FLAG_WALL )
1191 height = 255;
1192
1193 config = 0xF;
1194 }
1195
1196 pcell((v2i){x,y})->config = config;
1197
1198 u8 *info_px = &info_buffer[ (pixel_id ++)*4 ];
1199 info_px[0] = height;
1200 info_px[1] = cell->state & FLAG_WALL? 0: 255;
1201 info_px[2] = 0;
1202 info_px[3] = 0;
1203
1204 if(
1205 (
1206 ((cell->state & FLAG_IS_TRIGGER) && (cell->config == 0xF || cell->config == k_cell_type_split)) ||
1207 ((cell->state & FLAG_TARGETED) && (cell->config != k_cell_type_split))
1208 ) && update_texbuffer
1209 ){
1210 cell->state &= ~(FLAG_TARGETED|FLAG_IS_TRIGGER);
1211 for( u32 i = 0; i < 2; i ++ )
1212 {
1213 if( cell->links[i] )
1214 {
1215 struct cell *other_ptr = &world.data[ cell->links[i] ];
1216 other_ptr->links[ i ] = 0;
1217 other_ptr->state &= ~FLAG_IS_TRIGGER;
1218
1219 if( other_ptr->links[ i ^ 0x1 ] == 0 )
1220 other_ptr->state &= ~FLAG_TARGETED;
1221 }
1222 }
1223
1224 cell->links[0] = 0;
1225 cell->links[1] = 0;
1226 }
1227 }
1228 }
1229
1230 if( update_texbuffer )
1231 {
1232 glBindTexture( GL_TEXTURE_2D, world.background_data );
1233 glTexSubImage2D( GL_TEXTURE_2D, 0, px0 + 16, py0 + 16, px1-px0, py1-py0, GL_RGBA, GL_UNSIGNED_BYTE, info_buffer );
1234 }
1235 }
1236
1237 u16 id_drag_from = 0;
1238 v2f drag_from_co;
1239 v2f drag_to_co;
1240
1241 void vg_update(void)
1242 {
1243 // Fit within screen
1244
1245 float r1 = (float)vg_window_y / (float)vg_window_x,
1246 r2 = (float)world.h / (float)world.w,
1247 size;
1248
1249 size = ( r2 < r1? (float)world.w * 0.5f: ((float)world.h * 0.5f) / r1 ) + 2.5f;
1250 m3x3_projection( m_projection, -size, size, -size*r1, size*r1 );
1251
1252 v3f origin;
1253 origin[0] = floorf( -0.5f * world.w );
1254 origin[1] = floorf( -0.5f * world.h );
1255 origin[2] = 0.0f;
1256
1257 m3x3_identity( m_view );
1258 m3x3_translate( m_view, origin );
1259 m3x3_mul( m_projection, m_view, vg_pv );
1260 vg_projection_update();
1261
1262 // Input stuff
1263 v2_copy( vg_mouse_ws, world.tile_pos );
1264
1265 world.tile_x = floorf( world.tile_pos[0] );
1266 world.tile_y = floorf( world.tile_pos[1] );
1267
1268 // Tilemap editing
1269 if( !world.simulating && !gui_want_mouse() )
1270 {
1271 v2_copy( vg_mouse_ws, drag_to_co );
1272
1273 if( cell_interactive( (v2i){ world.tile_x, world.tile_y } ))
1274 {
1275 world.selected = world.tile_y * world.w + world.tile_x;
1276
1277 static u32 modify_state = 0;
1278
1279 struct cell *cell_ptr = &world.data[world.selected];
1280
1281 if( vg_get_button_down("primary") )
1282 {
1283 modify_state = (cell_ptr->state & FLAG_CANAL) ^ FLAG_CANAL;
1284 }
1285
1286 if( vg_get_button("primary") && ((cell_ptr->state & FLAG_CANAL) != modify_state) )
1287 {
1288 cell_ptr->state &= ~FLAG_CANAL;
1289 cell_ptr->state |= modify_state;
1290
1291 if( cell_ptr->state & FLAG_CANAL )
1292 {
1293 cell_ptr->links[0] = 0;
1294 cell_ptr->links[1] = 0;
1295
1296 sfx_set_playrnd( &audio_tile_mod, &audio_system_sfx, 3, 6 );
1297 world.score ++;
1298 }
1299 else
1300 {
1301 sfx_set_playrnd( &audio_tile_mod, &audio_system_sfx, 0, 3 );
1302 world.score --;
1303 }
1304
1305 map_reclassify( (v2i){ world.tile_x -2, world.tile_y -2 },
1306 (v2i){ world.tile_x +2, world.tile_y +2 }, 1 );
1307 }
1308
1309 if( vg_get_button_down("secondary") && !(cell_ptr->config == k_cell_type_split) )
1310 {
1311 id_drag_from = world.selected;
1312 drag_from_co[0] = world.tile_x + 0.5f;
1313 drag_from_co[1] = world.tile_y + 0.5f;
1314 }
1315
1316 if( id_drag_from && (cell_ptr->config == k_cell_type_split) )
1317 {
1318 float local_x = vg_mouse_ws[0] - (float)world.tile_x;
1319 drag_to_co[0] = (float)world.tile_x + (local_x > 0.5f? 0.75f: 0.25f);
1320 drag_to_co[1] = (float)world.tile_y + 0.25f;
1321
1322 if( vg_get_button_up("secondary") )
1323 {
1324 struct cell *drag_ptr = &world.data[id_drag_from];
1325 u32 link_id = local_x > 0.5f? 1: 0;
1326
1327 // Cleanup existing connections
1328 if( cell_ptr->links[ link_id ] )
1329 {
1330 vg_warn( "Destroying existing connection on link %u (%hu)\n", link_id, cell_ptr->links[ link_id ] );
1331
1332 struct cell *current_connection = &world.data[ cell_ptr->links[ link_id ]];
1333 current_connection->state &= ~FLAG_IS_TRIGGER;
1334 current_connection->links[ link_id ] = 0;
1335 }
1336
1337 if( drag_ptr->links[ link_id ^ 0x1 ] )
1338 {
1339 vg_warn( "Destroying alternate link %u (%hu)\n", link_id ^ 0x1, drag_ptr->links[ link_id ^ 0x1 ] );
1340
1341 struct cell *current_connection = &world.data[ drag_ptr->links[ link_id ^ 0x1 ]];
1342 if( !current_connection->links[ link_id ] )
1343 current_connection->state &= ~FLAG_TARGETED;
1344
1345 current_connection->links[ link_id ^ 0x1 ] = 0;
1346 drag_ptr->links[ link_id ^ 0x1 ] = 0;
1347 }
1348
1349 // Create the new connection
1350 vg_success( "Creating connection on link %u (%hu)\n", link_id, id_drag_from );
1351
1352 cell_ptr->links[ link_id ] = id_drag_from;
1353 drag_ptr->links[ link_id ] = world.selected;
1354
1355 cell_ptr->state |= FLAG_TARGETED;
1356 drag_ptr->state |= FLAG_IS_TRIGGER;
1357 id_drag_from = 0;
1358 }
1359 }
1360 }
1361 else
1362 world.selected = -1;
1363
1364 if( !(vg_get_button("secondary") && id_drag_from) )
1365 id_drag_from = 0;
1366 }
1367 else
1368 {
1369 world.selected = -1;
1370 id_drag_from = 0;
1371 }
1372
1373 // Simulation stop/start
1374 if( vg_get_button_down("go") )
1375 {
1376 if( world.simulating )
1377 {
1378 simulation_stop();
1379 }
1380 else
1381 {
1382 vg_success( "Starting simulation!\n" );
1383
1384 sfx_set_playrnd( &audio_rolls, &audio_system_balls_rolling, 0, 1 );
1385
1386 world.simulating = 1;
1387 world.num_fishes = 0;
1388 world.sim_frame = 0;
1389 world.sim_start = vg_time;
1390 world.sim_run = 0;
1391 world.sim_speed = 2.5f;
1392
1393 for( int i = 0; i < world.w*world.h; i ++ )
1394 world.data[ i ].state &= ~FLAG_FLIP_FLOP;
1395
1396 io_reset();
1397 }
1398 }
1399
1400 // Fish ticks
1401 if( world.simulating )
1402 {
1403 while( world.sim_frame < (int)((vg_time-world.sim_start)*world.sim_speed) )
1404 {
1405 //vg_info( "frame: %u\n", world.sim_frame );
1406 sfx_set_playrnd( &audio_random, &audio_system_balls_switching, 0, 9 );
1407
1408 // Update splitter deltas
1409 for( int i = 0; i < world.h*world.w; i ++ )
1410 {
1411 struct cell *cell = &world.data[i];
1412 if( cell->config == k_cell_type_split )
1413 {
1414 cell->state &= ~FLAG_FLIP_ROTATING;
1415 }
1416 if( cell->state & FLAG_IS_TRIGGER )
1417 cell->state &= ~FLAG_TRIGGERED;
1418 }
1419
1420 int alive_count = 0;
1421
1422 // Update fish positions
1423 for( int i = 0; i < world.num_fishes; i ++ )
1424 {
1425 struct fish *fish = &world.fishes[i];
1426
1427 if( fish->state == k_fish_state_soon_dead )
1428 fish->state = k_fish_state_dead;
1429
1430 if( fish->state == k_fish_state_soon_alive )
1431 fish->state = k_fish_state_alive;
1432
1433 if( fish->state < k_fish_state_alive )
1434 continue;
1435
1436 struct cell *cell_current = pcell( fish->pos );
1437
1438 if( fish->state == k_fish_state_alive )
1439 {
1440 // Apply to output
1441 if( cell_current->state & FLAG_OUTPUT )
1442 {
1443 for( int j = 0; j < arrlen( world.io ); j ++ )
1444 {
1445 struct cell_terminal *term = &world.io[j];
1446
1447 if( term->id == fish->pos[1]*world.w + fish->pos[0] )
1448 {
1449 struct terminal_run *run = &term->runs[ world.sim_run ];
1450 if( run->recv_count < vg_list_size( run->recieved ) )
1451 run->recieved[ run->recv_count ++ ] = fish->payload;
1452
1453 break;
1454 }
1455 }
1456
1457 fish->state = k_fish_state_dead;
1458 continue;
1459 }
1460
1461
1462 if( cell_current->config == k_cell_type_merge )
1463 {
1464 // Can only move up
1465 fish->dir[0] = 0;
1466 fish->dir[1] = -1;
1467 fish->flow_reversed = 0;
1468 }
1469 else
1470 {
1471 if( cell_current->config == k_cell_type_split )
1472 {
1473 // Flip flop L/R
1474 fish->dir[0] = cell_current->state&FLAG_FLIP_FLOP?1:-1;
1475 fish->dir[1] = 0;
1476
1477 if( !(cell_current->state & FLAG_TARGETED) )
1478 cell_current->state ^= FLAG_FLIP_FLOP;
1479 }
1480 else
1481 {
1482 // Apply cell out-flow
1483 struct cell_description *desc = &cell_descriptions[ cell_current->config ];
1484
1485 v2i_copy( fish->flow_reversed? desc->start: desc->end, fish->dir );
1486 }
1487
1488 v2i pos_next;
1489 v2i_add( fish->pos, fish->dir, pos_next );
1490
1491 struct cell *cell_next = pcell( pos_next );
1492
1493 if( cell_next->state & (FLAG_CANAL|FLAG_OUTPUT) )
1494 {
1495 struct cell_description *desc = &cell_descriptions[ cell_next->config ];
1496
1497 if( cell_next->config == k_cell_type_merge )
1498 {
1499 if( fish->dir[0] == 0 )
1500 fish->state = k_fish_state_dead;
1501 else
1502 fish->flow_reversed = 0;
1503 }
1504 else
1505 {
1506 if( cell_next->config == k_cell_type_split )
1507 {
1508 if( fish->dir[0] == 0 )
1509 {
1510 sfx_set_playrnd( &audio_splitter, &audio_system_balls_important, 0, 1 );
1511 cell_next->state |= FLAG_FLIP_ROTATING;
1512
1513 fish->flow_reversed = 0;
1514 }
1515 else
1516 fish->state = k_fish_state_dead;
1517 }
1518 else
1519 fish->flow_reversed = ( fish->dir[0] != -desc->start[0] ||
1520 fish->dir[1] != -desc->start[1] )? 1: 0;
1521 }
1522 }
1523 else
1524 fish->state = world_check_pos_ok( fish->pos )? k_fish_state_bg: k_fish_state_dead;
1525 }
1526
1527 //v2i_add( fish->pos, fish->dir, fish->pos );
1528 }
1529 else if( fish->state == k_fish_state_bg )
1530 {
1531 v2i_add( fish->pos, fish->dir, fish->pos );
1532
1533 if( !world_check_pos_ok( fish->pos ) )
1534 fish->state = k_fish_state_dead;
1535 else
1536 {
1537 struct cell *cell_entry = pcell( fish->pos );
1538
1539 if( cell_entry->state & FLAG_CANAL )
1540 {
1541 if( cell_entry->config == k_cell_type_con_r || cell_entry->config == k_cell_type_con_u
1542 || cell_entry->config == k_cell_type_con_l || cell_entry->config == k_cell_type_con_d )
1543 {
1544 sw_set_achievement( "CAN_DO_THAT" );
1545
1546 fish->state = k_fish_state_soon_alive;
1547
1548 fish->dir[0] = 0;
1549 fish->dir[1] = 0;
1550 fish->flow_reversed = 1;
1551
1552 switch( cell_entry->config )
1553 {
1554 case k_cell_type_con_r: fish->dir[0] = 1; break;
1555 case k_cell_type_con_l: fish->dir[0] = -1; break;
1556 case k_cell_type_con_u: fish->dir[1] = 1; break;
1557 case k_cell_type_con_d: fish->dir[1] = -1; break;
1558 }
1559 }
1560 }
1561 }
1562 }
1563 else { vg_error( "fish behaviour unimplemented for behaviour type (%d)\n" ); }
1564
1565 if( fish->state >= k_fish_state_alive )
1566 alive_count ++;
1567 }
1568
1569 // Second pass (triggers)
1570 for( int i = 0; i < world.num_fishes; i ++ )
1571 {
1572 struct fish *fish = &world.fishes[i];
1573
1574 if( fish->state == k_fish_state_alive )
1575 {
1576 v2i_add( fish->pos, fish->dir, fish->pos );
1577 struct cell *cell_current = pcell( fish->pos );
1578
1579 if( cell_current->state & FLAG_IS_TRIGGER )
1580 {
1581 int trigger_id = cell_current->links[0]?0:1;
1582 int connection_id = cell_current->links[trigger_id];
1583 int target_px = connection_id % world.w;
1584 int target_py = (connection_id - target_px)/world.w;
1585
1586 vg_line2( (v2f){ fish->pos[0], fish->pos[1] }, (v2f){ target_px, target_py }, 0xffffffff, 0xffffffff );
1587
1588 struct cell *target_peice = &world.data[ cell_current->links[trigger_id] ];
1589
1590 cell_current->state |= FLAG_TRIGGERED;
1591
1592 if( trigger_id )
1593 target_peice->state |= FLAG_FLIP_FLOP;
1594 else
1595 target_peice->state &= ~FLAG_FLIP_FLOP;
1596 }
1597 }
1598 }
1599
1600 // Third pass (collisions)
1601 struct fish *fi, *fj;
1602
1603 for( int i = 0; i < world.num_fishes; i ++ )
1604 {
1605 fi = &world.fishes[i];
1606
1607 if( fi->state == k_fish_state_alive )
1608 {
1609 int continue_again = 0;
1610
1611 for( int j = i+1; j < world.num_fishes; j ++ )
1612 {
1613 fj = &world.fishes[j];
1614
1615 if( (fj->state == k_fish_state_alive) )
1616 {
1617 v2i fi_prev;
1618 v2i fj_prev;
1619
1620 v2i_sub( fi->pos, fi->dir, fi_prev );
1621 v2i_sub( fj->pos, fj->dir, fj_prev );
1622
1623 int
1624 collide_next_frame = (
1625 (fi->pos[0] == fj->pos[0]) &&
1626 (fi->pos[1] == fj->pos[1]))? 1: 0,
1627 collide_this_frame = (
1628 (fi_prev[0] == fj->pos[0]) &&
1629 (fi_prev[1] == fj->pos[1]) &&
1630 (fj_prev[0] == fi->pos[0]) &&
1631 (fj_prev[1] == fi->pos[1])
1632 )? 1: 0;
1633
1634 if( collide_next_frame || collide_this_frame )
1635 {
1636 sw_set_achievement( "BANG" );
1637
1638 // Shatter death (+0.5s)
1639 float death_time = collide_this_frame? 0.0f: 0.5f;
1640
1641 fi->state = k_fish_state_soon_dead;
1642 fj->state = k_fish_state_soon_dead;
1643 fi->death_time = death_time;
1644 fj->death_time = death_time;
1645
1646 continue_again = 1;
1647 break;
1648 }
1649 }
1650 }
1651 if( continue_again )
1652 continue;
1653 }
1654 }
1655
1656 // Spawn fishes
1657 for( int i = 0; i < arrlen( world.io ); i ++ )
1658 {
1659 struct cell_terminal *term = &world.io[ i ];
1660 int posx = term->id % world.w;
1661 int posy = (term->id - posx)/world.w;
1662 int is_input = world.data[ term->id ].state & FLAG_INPUT;
1663
1664 if( is_input )
1665 {
1666 if( world.sim_frame < term->runs[ world.sim_run ].condition_count )
1667 {
1668 struct fish *fish = &world.fishes[ world.num_fishes ];
1669 fish->pos[0] = posx;
1670 fish->pos[1] = posy;
1671 fish->state = k_fish_state_alive;
1672 fish->payload = term->runs[ world.sim_run ].conditions[ world.sim_frame ];
1673
1674 struct cell *cell_ptr = pcell( fish->pos );
1675
1676 if( cell_ptr->config != k_cell_type_stub )
1677 {
1678 struct cell_description *desc = &cell_descriptions[ cell_ptr->config ];
1679
1680 v2i_copy( desc->start, fish->dir );
1681 fish->flow_reversed = 1;
1682
1683 world.num_fishes ++;
1684 alive_count ++;
1685 }
1686 }
1687 }
1688 }
1689
1690 if( alive_count == 0 )
1691 {
1692 world.completed = 1;
1693
1694 for( int i = 0; i < arrlen( world.io ); i ++ )
1695 {
1696 struct cell_terminal *term = &world.io[ i ];
1697 int is_input = world.data[ term->id ].state & FLAG_INPUT;
1698
1699 if( !is_input )
1700 {
1701 struct terminal_run *run = &term->runs[ world.sim_run ];
1702
1703 if( run->recv_count == run->condition_count )
1704 {
1705 for( int j = 0; j < run->condition_count; j ++ )
1706 {
1707 if( run->recieved[j] != run->conditions[j] )
1708 {
1709 world.completed = 0;
1710 break;
1711 }
1712 }
1713 }
1714 else
1715 {
1716 world.completed = 0;
1717 break;
1718 }
1719 }
1720 }
1721
1722 if( world.completed )
1723 {
1724 if( world.sim_run < world.max_runs-1 )
1725 {
1726 vg_success( "Run passed, starting next\n" );
1727 world.sim_run ++;
1728 world.sim_frame = 0;
1729 world.sim_start = vg_time;
1730 world.num_fishes = 0;
1731
1732 for( int i = 0; i < world.w*world.h; i ++ )
1733 world.data[ i ].state &= ~FLAG_FLIP_FLOP;
1734
1735 continue;
1736 }
1737 else
1738 {
1739 vg_success( "Level passed!\n" );
1740
1741 u32 score = 0;
1742 for( int i = 0; i < world.w*world.h; i ++ )
1743 if( world.data[ i ].state & FLAG_CANAL )
1744 score ++;
1745
1746 world.score = score;
1747 world.time = world.sim_frame;
1748 }
1749 }
1750 else
1751 {
1752 if( world.sim_run > 0 )
1753 sw_set_achievement( "GOOD_ENOUGH" );
1754
1755 vg_error( "Level failed :(\n" );
1756 }
1757
1758 // Copy into career data
1759 if( world.ptr_career_level )
1760 {
1761 world.ptr_career_level->score = world.score;
1762 world.ptr_career_level->time = world.time;
1763 world.ptr_career_level->completed = world.completed;
1764 }
1765
1766 simulation_stop(); // TODO: Async?
1767 break;
1768 }
1769
1770 world.sim_frame ++;
1771 }
1772
1773 float scaled_time = 0.0f;
1774 scaled_time = (vg_time-world.sim_start)*world.sim_speed;
1775 world.frame_lerp = scaled_time - (float)world.sim_frame;
1776
1777 // Update positions
1778 for( int i = 0; i < world.num_fishes; i ++ )
1779 {
1780 struct fish *fish = &world.fishes[i];
1781
1782 if( fish->state == k_fish_state_dead )
1783 continue;
1784
1785 if( fish->state == k_fish_state_soon_dead && (world.frame_lerp > fish->death_time) )
1786 continue; // Todo: particle thing?
1787
1788 struct cell *cell = pcell(fish->pos);
1789 struct cell_description *desc = &cell_descriptions[ cell->config ];
1790
1791 v2f const *curve;
1792
1793 float t = world.frame_lerp;
1794 if( fish->flow_reversed && !desc->is_linear )
1795 t = 1.0f-t;
1796
1797 v2_copy( fish->physics_co, fish->physics_v );
1798
1799 switch( cell->config )
1800 {
1801 case k_cell_type_merge:
1802 if( fish->dir[0] == 1 )
1803 curve = curve_12;
1804 else
1805 curve = curve_9;
1806 break;
1807 case k_cell_type_con_r: curve = curve_1; break;
1808 case k_cell_type_con_l: curve = curve_4; break;
1809 case k_cell_type_con_u: curve = curve_2; break;
1810 case k_cell_type_con_d: curve = curve_8; break;
1811 case 3: curve = curve_3; break;
1812 case 6: curve = curve_6; break;
1813 case 9: curve = curve_9; break;
1814 case 12: curve = curve_12; break;
1815 case 7:
1816 if( t > curve_7_linear_section )
1817 {
1818 t -= curve_7_linear_section;
1819 t *= (1.0f/(1.0f-curve_7_linear_section));
1820
1821 curve = cell->state & FLAG_FLIP_FLOP? curve_7: curve_7_1;
1822 }
1823 else curve = NULL;
1824 break;
1825 default: curve = NULL; break;
1826 }
1827
1828 if( curve )
1829 {
1830 float t2 = t * t;
1831 float t3 = t * t * t;
1832
1833 float cA = 3.0f*t2 - 3.0f*t3;
1834 float cB = 3.0f*t3 - 6.0f*t2 + 3.0f*t;
1835 float cC = 3.0f*t2 - t3 - 3.0f*t + 1.0f;
1836
1837 fish->physics_co[0] = t3*curve[3][0] + cA*curve[2][0] + cB*curve[1][0] + cC*curve[0][0];
1838 fish->physics_co[1] = t3*curve[3][1] + cA*curve[2][1] + cB*curve[1][1] + cC*curve[0][1];
1839 fish->physics_co[0] += (float)fish->pos[0];
1840 fish->physics_co[1] += (float)fish->pos[1];
1841 }
1842 else
1843 {
1844 v2f origin;
1845 origin[0] = (float)fish->pos[0] + (float)fish->dir[0]*-0.5f + 0.5f;
1846 origin[1] = (float)fish->pos[1] + (float)fish->dir[1]*-0.5f + 0.5f;
1847
1848 fish->physics_co[0] = origin[0] + (float)fish->dir[0]*t;
1849 fish->physics_co[1] = origin[1] + (float)fish->dir[1]*t;
1850 }
1851 }
1852 }
1853 }
1854
1855 static void render_tiles( v2i start, v2i end, v4f const regular_colour, v4f const selected_colour )
1856 {
1857 v2i full_start = { 0,0 };
1858 v2i full_end = { world.w, world.h };
1859
1860 if( !start || !end )
1861 {
1862 start = full_start;
1863 end = full_end;
1864 }
1865
1866 glUniform4fv( SHADER_UNIFORM( shader_tile_main, "uColour" ), 1, regular_colour );
1867
1868 for( int y = start[1]; y < end[1]; y ++ )
1869 {
1870 for( int x = start[0]; x < end[0]; x ++ )
1871 {
1872 struct cell *cell = pcell((v2i){x,y});
1873 int selected = world.selected == y*world.w + x;
1874
1875 int tile_offsets[][2] =
1876 {
1877 {2, 0}, {0, 3}, {0, 2}, {2, 2},
1878 {1, 0}, {2, 3}, {3, 2}, {1, 3},
1879 {3, 1}, {0, 1}, {1, 2}, {2, 1},
1880 {1, 1}, {3, 3}, {2, 1}, {2, 1}
1881 };
1882
1883 int uv[2] = { 3, 0 };
1884
1885 if( cell->state & (FLAG_CANAL|FLAG_INPUT|FLAG_OUTPUT) )
1886 {
1887 uv[0] = tile_offsets[ cell->config ][0];
1888 uv[1] = tile_offsets[ cell->config ][1];
1889 } else continue;
1890
1891 glUniform4f( SHADER_UNIFORM( shader_tile_main, "uOffset" ), (float)x, (float)y, uv[0], uv[1] );
1892 if( selected )
1893 {
1894 glUniform4fv( SHADER_UNIFORM( shader_tile_main, "uColour" ), 1, selected_colour );
1895 draw_mesh( 0, 2 );
1896 glUniform4fv( SHADER_UNIFORM( shader_tile_main, "uColour" ), 1, regular_colour );
1897 }
1898 else
1899 draw_mesh( 0, 2 );
1900 }
1901 }
1902 }
1903
1904 static void draw_numbers( v3f coord, int number )
1905 {
1906 v3f pos;
1907 v3_copy( coord, pos );
1908 int digits[8]; int i = 0;
1909
1910 while( number > 0 && i < 8 )
1911 {
1912 digits[i ++] = number % 10;
1913 number = number / 10;
1914 }
1915
1916 for( int j = 0; j < i; j ++ )
1917 {
1918 glUniform3fv( SHADER_UNIFORM( shader_tile_colour, "uOffset" ), 1, pos );
1919 draw_mesh( MESH_NUMBERS_OFFSETS[digits[i-j-1]][0], MESH_NUMBERS_OFFSETS[digits[i-j-1]][1] );
1920 pos[0] += pos[2] * 0.75f;
1921 }
1922 }
1923
1924 void vg_render(void)
1925 {
1926 glViewport( 0,0, vg_window_x, vg_window_y );
1927
1928 glDisable( GL_DEPTH_TEST );
1929 glClearColor( 0.369768f, 0.3654f, 0.42f, 1.0f );
1930 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
1931
1932 v4f const colour_default = {1.0f, 1.0f, 1.0f, 1.0f};
1933 v4f const colour_selected = {0.90f, 0.92f, 1.0f, 1.0f};
1934
1935 // TILE SET RENDERING
1936 // todo: just slam everything into a mesh...
1937 // when user modifies a tile the neighbours can be easily uploaded to gpu mem
1938 // in ~3 subBuffers
1939 // Currently we're uploading a fair amount of data every frame anyway.
1940 // NOTE: this is for final optimisations ONLY!
1941 // ======================================================================
1942
1943 use_mesh( &world.tile );
1944
1945 // Draw background
1946
1947 if(1){
1948
1949 SHADER_USE( shader_background );
1950 glUniformMatrix3fv( SHADER_UNIFORM( shader_background, "uPv" ), 1, GL_FALSE, (float *)vg_pv );
1951
1952 glActiveTexture( GL_TEXTURE0 );
1953 glBindTexture( GL_TEXTURE_2D, world.background_data );
1954 glUniform1i( SHADER_UNIFORM( shader_background, "uTexMain" ), 0 );
1955
1956 glUniform3f( SHADER_UNIFORM( shader_background, "uOffset" ), -16, -16, 64 );
1957 glUniform1f( SHADER_UNIFORM( shader_background, "uVariance" ), 0.02f );
1958
1959 glActiveTexture( GL_TEXTURE1 );
1960 glBindTexture( GL_TEXTURE_2D, world.random_samples );
1961 glUniform1i( SHADER_UNIFORM( shader_background, "uSamplerNoise" ), 1 );
1962
1963 draw_mesh( 0, 2 );
1964
1965 }
1966
1967
1968 // Level title
1969 ui_begin( &ui_global_ctx, 512, 256 );
1970
1971 ui_global_ctx.override_colour = 0xff9a8a89;
1972 //ui_text( &ui_global_ctx, world.map_title, 6, 0 );
1973 ui_global_ctx.override_colour = 0xffffffff;
1974
1975 ui_resolve( &ui_global_ctx );
1976
1977 m3x3f world_text;
1978 m3x3_copy( vg_pv, world_text );
1979 m3x3_translate( world_text, (v3f){ 1.55f, 1.9f, 0.0f } );
1980 m3x3_rotate( world_text, VG_PIf*0.5f );
1981 m3x3_scale( world_text, (v3f){0.01f,-0.01f,0.01f} );
1982
1983 ui_draw( &ui_global_ctx, world_text );
1984
1985 // Main
1986 // =========================================================================================
1987
1988 use_mesh( &world.tile );
1989 SHADER_USE( shader_tile_main );
1990
1991 m2x2f subtransform;
1992 m2x2_identity( subtransform );
1993 glUniformMatrix2fv( SHADER_UNIFORM( shader_tile_main, "uSubTransform" ), 1, GL_FALSE, (float *)subtransform );
1994 glUniformMatrix3fv( SHADER_UNIFORM( shader_tile_main, "uPv" ), 1, GL_FALSE, (float *)vg_pv );
1995 glUniform1f( SHADER_UNIFORM( shader_tile_main, "uGhost" ), 0.0f );
1996 glUniform1f( SHADER_UNIFORM( shader_tile_main, "uForeground" ), 0.0f );
1997
1998 glEnable(GL_BLEND);
1999 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
2000 glBlendEquation(GL_FUNC_ADD);
2001
2002 // Bind textures
2003 vg_tex2d_bind( &tex_tile_data, 0 );
2004 glUniform1i( SHADER_UNIFORM( shader_tile_main, "uTexGlyphs" ), 0 );
2005
2006 vg_tex2d_bind( &tex_wood, 1 );
2007 glUniform1i( SHADER_UNIFORM( shader_tile_main, "uTexWood" ), 1 );
2008
2009 render_tiles( NULL, NULL, colour_default, colour_default );
2010
2011
2012
2013 SHADER_USE( shader_ball );
2014 glUniformMatrix3fv( SHADER_UNIFORM( shader_ball, "uPv" ), 1, GL_FALSE, (float *)vg_pv );
2015
2016 vg_tex2d_bind( &tex_ball_noise, 0 );
2017 glUniform1i( SHADER_UNIFORM( shader_ball, "uTexMain" ), 0 );
2018
2019 // Draw 'fish'
2020 if( world.simulating )
2021 {
2022 for( int i = 0; i < world.num_fishes; i ++ )
2023 {
2024 struct fish *fish = &world.fishes[i];
2025
2026 if( fish->state == k_fish_state_dead || fish->state == k_fish_state_bg )
2027 continue;
2028
2029 if( fish->state == k_fish_state_soon_dead && (world.frame_lerp > fish->death_time) )
2030 continue;
2031
2032 v4f dot_colour = { 0.0f, 0.0f, 0.0f, 1.0f };
2033 colour_code_v3( fish->payload, dot_colour );
2034
2035 glUniform3fv( SHADER_UNIFORM( shader_ball, "uColour" ), 1, dot_colour );
2036 glUniform2fv( SHADER_UNIFORM( shader_ball, "uOffset" ), 1, fish->physics_co );
2037 glUniform2f( SHADER_UNIFORM( shader_ball, "uTexOffset" ), (float)i * 1.2334, (float)i * -0.3579f );
2038 draw_mesh( 0, 2 );
2039 }
2040 }
2041
2042 SHADER_USE( shader_tile_main );
2043
2044 // Bind textures
2045 vg_tex2d_bind( &tex_tile_data, 0 );
2046 glUniform1i( SHADER_UNIFORM( shader_tile_main, "uTexGlyphs" ), 0 );
2047
2048 vg_tex2d_bind( &tex_wood, 1 );
2049 glUniform1i( SHADER_UNIFORM( shader_tile_main, "uTexWood" ), 1 );
2050
2051 glUniform1f( SHADER_UNIFORM( shader_tile_main, "uForeground" ), 1.0f );
2052 render_tiles( NULL, NULL, colour_default, colour_selected );
2053
2054 // Draw splitters
2055 for( int y = 2; y < world.h-2; y ++ )
2056 {
2057 for( int x = 2; x < world.w-2; x ++ )
2058 {
2059 struct cell *cell = pcell((v2i){x,y});
2060
2061 if( cell->state & FLAG_CANAL )
2062 {
2063 if( cell->config == k_cell_type_split )
2064 {
2065 float rotation = cell->state & FLAG_FLIP_FLOP? vg_rad( -45.0f ): vg_rad( 45.0f );
2066
2067 if( cell->state & FLAG_FLIP_ROTATING )
2068 {
2069 if( (world.frame_lerp > curve_7_linear_section) )
2070 {
2071 float const rotation_speed = 0.4f;
2072 if( (world.frame_lerp < 1.0f-rotation_speed) )
2073 {
2074 float t = world.frame_lerp - curve_7_linear_section;
2075 t *= -2.0f * (1.0f/(1.0f-(curve_7_linear_section+rotation_speed)));
2076 t += 1.0f;
2077
2078 rotation *= t;
2079 }
2080 else
2081 rotation *= -1.0f;
2082 }
2083 }
2084
2085 m2x2_create_rotation( subtransform, rotation );
2086
2087 glUniformMatrix2fv( SHADER_UNIFORM( shader_tile_main, "uSubTransform" ), 1, GL_FALSE, (float *)subtransform );
2088 glUniform4f( SHADER_UNIFORM( shader_tile_main, "uOffset" ), (float)x, (float)y + 0.125f, cell->state & FLAG_TARGETED? 3.0f: 0.0f, 0.0f );
2089 draw_mesh( 0, 2 );
2090 }
2091 }
2092 }
2093 }
2094
2095 // Edit overlay
2096 if( world.selected != -1 && !(world.data[ world.selected ].state & FLAG_CANAL) && !id_drag_from )
2097 {
2098 v2i new_begin = { world.tile_x - 2, world.tile_y - 2 };
2099 v2i new_end = { world.tile_x + 2, world.tile_y + 2 };
2100
2101 world.data[ world.selected ].state ^= FLAG_CANAL;
2102 map_reclassify( new_begin, new_end, 0 );
2103
2104 m2x2_identity( subtransform );
2105 glUniform1f( SHADER_UNIFORM( shader_tile_main, "uGhost" ), 1.0f );
2106 glUniformMatrix2fv( SHADER_UNIFORM( shader_tile_main, "uSubTransform" ), 1, GL_FALSE, (float *)subtransform );
2107 glUniform2fv( SHADER_UNIFORM( shader_tile_main, "uMousePos" ), 1, world.tile_pos );
2108
2109 render_tiles( new_begin, new_end, colour_default, colour_default );
2110
2111 world.data[ world.selected ].state ^= FLAG_CANAL;
2112 map_reclassify( new_begin, new_end, 0 );
2113 }
2114
2115 //glDisable(GL_BLEND);
2116
2117 // Draw connecting wires
2118 glDisable(GL_BLEND);
2119
2120 SHADER_USE( shader_wire );
2121 glBindVertexArray( world.wire.vao );
2122
2123 glUniformMatrix3fv( SHADER_UNIFORM( shader_wire, "uPv" ), 1, GL_FALSE, (float *)vg_pv );
2124 glUniform4f( SHADER_UNIFORM( shader_wire, "uColour" ), 0.2f, 0.2f, 0.2f, 1.0f );
2125
2126 if( id_drag_from )
2127 {
2128 glUniform1f( SHADER_UNIFORM( shader_wire, "uCurve" ), 0.4f );
2129 glUniform3f( SHADER_UNIFORM( shader_wire, "uStart" ), drag_from_co[0], drag_from_co[1], 0.06f );
2130 glUniform3f( SHADER_UNIFORM( shader_wire, "uEnd" ), drag_to_co[0], drag_to_co[1], 0.06f );
2131 glDrawElements( GL_TRIANGLES, world.wire.em, GL_UNSIGNED_SHORT, (void*)(0) );
2132 }
2133
2134 float rp_x1 = world.frame_lerp*9.0f;
2135 float rp_x2 = 1.0f-rp_x1*expf(1.0f-rp_x1)* 0.36f;
2136
2137 for( int y = 2; y < world.h-2; y ++ )
2138 {
2139 for( int x = 2; x < world.w-2; x ++ )
2140 {
2141 struct cell *cell = pcell((v2i){x,y});
2142
2143 if( cell->state & FLAG_CANAL )
2144 {
2145 if( cell->state & FLAG_IS_TRIGGER )
2146 {
2147 int trigger_id = cell->links[0]?0:1;
2148
2149 int x2 = cell->links[trigger_id] % world.w;
2150 int y2 = (cell->links[trigger_id] - x2) / world.w;
2151
2152 v2f startpoint;
2153 v2f endpoint;
2154
2155 startpoint[0] = (float)x2 + (trigger_id? 0.75f: 0.25f);
2156 startpoint[1] = (float)y2 + 0.25f;
2157
2158 endpoint[0] = x+0.5f;
2159 endpoint[1] = y+0.5f;
2160
2161 glUniform1f( SHADER_UNIFORM( shader_wire, "uCurve" ), cell->state & FLAG_TRIGGERED? rp_x2 * 0.4f: 0.4f );
2162 glUniform3f( SHADER_UNIFORM( shader_wire, "uStart" ), startpoint[0], startpoint[1], 0.04f );
2163 glUniform3f( SHADER_UNIFORM( shader_wire, "uEnd" ), endpoint[0], endpoint[1], 0.04f );
2164 glDrawElements( GL_TRIANGLES, world.wire.em, GL_UNSIGNED_SHORT, (void*)(0) );
2165 }
2166 }
2167 }
2168 }
2169
2170 SHADER_USE( shader_tile_colour );
2171 glUniformMatrix3fv( SHADER_UNIFORM( shader_tile_colour, "uPv" ), 1, GL_FALSE, (float *)vg_pv );
2172 use_mesh( &world.circle );
2173
2174 int const filled_start = 0;
2175 int const filled_count = 32;
2176 int const empty_start = 32;
2177 int const empty_count = 32*2;
2178
2179 // Draw i/o arrays
2180 for( int i = 0; i < arrlen( world.io ); i ++ )
2181 {
2182 struct cell_terminal *term = &world.io[ i ];
2183 int posx = term->id % world.w;
2184 int posy = (term->id - posx)/world.w;
2185 int is_input = world.data[ term->id ].state & FLAG_INPUT;
2186
2187 v4f dot_colour = { 0.0f, 0.0f, 0.0f, 1.0f };
2188
2189 for( int k = 0; k < term->run_count; k ++ )
2190 {
2191 for( int j = 0; j < term->runs[k].condition_count; j ++ )
2192 {
2193 float y_offset = is_input? 1.2f: -0.2f;
2194 y_offset += (is_input? 0.2f: -0.2f) * (float)k;
2195
2196 glUniform3f( SHADER_UNIFORM( shader_tile_colour, "uOffset" ), (float)posx + 0.2f + 0.2f * (float)j, (float)posy + y_offset, 0.1f );
2197
2198 if( is_input )
2199 {
2200 colour_code_v3( term->runs[k].conditions[j], dot_colour );
2201 glUniform4fv( SHADER_UNIFORM( shader_tile_colour, "uColour" ), 1, dot_colour );
2202
2203 // Draw filled if tick not passed, draw empty if empty
2204 if( (world.sim_frame > j && world.sim_run >= k) || world.sim_run > k )
2205 draw_mesh( empty_start, empty_count );
2206 else
2207 draw_mesh( filled_start, filled_count );
2208 }
2209 else
2210 {
2211 if( term->runs[k].recv_count > j )
2212 {
2213 colour_code_v3( term->runs[k].recieved[j], dot_colour );
2214 v3_muls( dot_colour, 0.8f, dot_colour );
2215 glUniform4fv( SHADER_UNIFORM( shader_tile_colour, "uColour" ), 1, dot_colour );
2216
2217 draw_mesh( filled_start, filled_count );
2218 }
2219
2220 colour_code_v3( term->runs[k].conditions[j], dot_colour );
2221 glUniform4fv( SHADER_UNIFORM( shader_tile_colour, "uColour" ), 1, dot_colour );
2222
2223 draw_mesh( empty_start, empty_count );
2224 }
2225 }
2226 }
2227 }
2228
2229 if( world.simulating )
2230 {
2231 glUniform4f( SHADER_UNIFORM( shader_tile_colour, "uColour" ), 0.0f, 0.0f, 0.0f, 1.0f );
2232 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 );
2233 draw_mesh( filled_start, filled_count );
2234 }
2235
2236 // Draw score
2237 float const score_bright = 1.25f;
2238 glUniform4f( SHADER_UNIFORM( shader_tile_colour, "uColour" ),
2239 0.4f*score_bright, 0.39f*score_bright, 0.45f*score_bright, 1.0f );
2240
2241 use_mesh( &world.numbers );
2242 draw_numbers( (v3f){ 2.0f, (float)world.h-1.875f, 0.3333f }, world.score );
2243 }
2244
2245 static ui_colourset flcol_list_a = {
2246 .main = 0xff877979,
2247 .hover = 0xffa09393,
2248 .active = 0xffbfb1b0
2249 };
2250 static ui_colourset flcol_list_b = {
2251 .main = 0xff7c6e6e,
2252 .hover = 0xffa09393,
2253 .active = 0xffbfb1b0
2254 };
2255
2256 static ui_colourset flcol_list_complete_a = {
2257 .main = 0xff62a064,
2258 .hover = 0xff8dc18f,
2259 .active = 0xffb2ddb3
2260 };
2261
2262 static ui_colourset flcol_list_complete_b = {
2263 .main = 0xff79b37b,
2264 .hover = 0xff8dc18f,
2265 .active = 0xffb2ddb3
2266 };
2267
2268 static ui_colourset flcol_list_locked = {
2269 .main = 0xff655959,
2270 .hover = 0xff655959,
2271 .active = 0xff655959
2272 };
2273
2274
2275
2276 static void draw_levels_list( struct cmp_level *levels, int count, int unlocked )
2277 {
2278 static struct ui_scrollbar sb = {
2279 .bar_height = 400
2280 };
2281
2282 ui_px view_height = ui_global_ctx.cursor[3];
2283 ui_px level_height = 50;
2284
2285 // Level scroll view
2286 gui_new_node();
2287 {
2288 gui_fill_rect( ui_global_ctx.cursor, 0xff5a4e4d );
2289 gui_set_clip( ui_global_ctx.cursor );
2290
2291 ui_global_ctx.cursor[2] = 14;
2292 gui_align_right();
2293
2294 ui_px content_height = count*level_height;
2295 if( content_height > view_height )
2296 {
2297 ui_scrollbar( &ui_global_ctx, &sb, 1 );
2298 ui_global_ctx.cursor[1] -= ui_calculate_content_scroll( &sb, content_height );
2299 }
2300 else
2301 {
2302 gui_fill_rect( ui_global_ctx.cursor, 0xff807373 );
2303 }
2304
2305 ui_global_ctx.cursor[2] = 240;
2306 ui_global_ctx.cursor[3] = level_height;
2307 gui_align_left();
2308
2309 for( int i = 0; i < count; i ++ )
2310 {
2311 struct cmp_level *lvl_info = &levels[i];
2312
2313 if( i < unlocked )
2314 {
2315 if( lvl_info->completed_score != 0 )
2316 gui_override_colours( i&0x1? &flcol_list_complete_a: &flcol_list_complete_b );
2317 else
2318 gui_override_colours( i&0x1? &flcol_list_a: &flcol_list_b );
2319 }
2320 else
2321 gui_override_colours( &flcol_list_locked );
2322
2323 if( i < unlocked )
2324 {
2325 if( gui_button( 2 + i ) == k_button_click )
2326 {
2327 console_changelevel( 1, &lvl_info->map_name );
2328 }
2329
2330 ui_global_ctx.override_colour = 0xffffffff;
2331 gui_text( lvl_info->title, 3, 0 );
2332 ui_global_ctx.cursor[1] += 18;
2333 gui_text( "incomplete", 2, 0 );
2334 }
2335 else
2336 {
2337 gui_button( 2 + i );
2338
2339 ui_global_ctx.override_colour = 0xff786f6f;
2340 gui_text( "???", 3, 0 );
2341 ui_global_ctx.cursor[1] += 18;
2342 gui_text( "locked", 2, 0 );
2343 }
2344
2345 gui_end_down();
2346 }
2347
2348 gui_reset_colours();
2349 gui_release_clip();
2350 }
2351 gui_end_down();
2352 }
2353
2354 void vg_ui(void)
2355 {
2356 ui_global_ctx.cursor[0] = 0;
2357 ui_global_ctx.cursor[1] = 0;
2358 ui_global_ctx.cursor[2] = 256;
2359
2360 gui_fill_y();
2361
2362 ui_global_ctx.id_base = 4 << 16;
2363
2364 static int pack_selection = 0;
2365 static struct pack_info
2366 {
2367 struct cmp_level *levels;
2368 u32 level_count;
2369 const char *name;
2370 }
2371 pack_infos[] =
2372 {
2373 {
2374 .levels = cmp_levels_tutorials,
2375 .level_count = vg_list_size(cmp_levels_tutorials),
2376 .name = "Training"
2377 },
2378 {
2379 .levels = cmp_levels_basic,
2380 .level_count = vg_list_size(cmp_levels_basic),
2381 .name = "Main"
2382 },
2383 {
2384 .levels = cmp_levels_grad,
2385 .level_count = vg_list_size(cmp_levels_tutorials),
2386 .name = "Expert"
2387 }
2388 };
2389
2390 gui_new_node();
2391 {
2392 gui_fill_rect( ui_global_ctx.cursor, 0xff5577ff );
2393
2394 gui_text( "ASSIGNMENTS", 4, 0 );
2395
2396 ui_global_ctx.cursor[1] += 30;
2397 ui_global_ctx.cursor[3] = 25;
2398
2399 gui_new_node();
2400 {
2401 ui_rect_pad( ui_global_ctx.cursor, 2 );
2402 ui_global_ctx.cursor[2] = 84;
2403
2404 for( int i = 0; i < 3; i ++ )
2405 {
2406 if( i == pack_selection )
2407 gui_override_colours( &flcol_list_locked );
2408
2409 if( gui_button( 2000 + i ) == k_button_click )
2410 pack_selection = i;
2411
2412 ui_global_ctx.cursor[1] += 2;
2413 gui_text( pack_infos[i].name, 2, 0 );
2414 gui_end_right();
2415
2416 gui_reset_colours();
2417 }
2418 }
2419 gui_end_down();
2420
2421 ui_global_ctx.cursor[3] = 500;
2422
2423 draw_levels_list( pack_infos[ pack_selection ].levels, pack_infos[ pack_selection ].level_count, 3 );
2424 }
2425 gui_end();
2426 }