falling animations
[fishladder.git] / fishladder.c
1 // Copyright (C) 2021 Harry Godden (hgn) - All Rights Reserved
2
3 //#define VG_STEAM
4 #include "vg/vg.h"
5 #include "fishladder_resources.h"
6
7 const char *level_pack[] =
8 {
9 // Level 0
10 "#########;\n"
11 "###-#####;acac\n"
12 "## ##;\n"
13 "## ##;\n"
14 "## ##;\n"
15 "## ##;\n"
16 "#####+###;acac\n"
17 "#########;\n",
18
19 // Level 1
20 "#########;\n"
21 "##-###-##;b,b\n"
22 "## ##;\n"
23 "## ##;\n"
24 "## ##;\n"
25 "## ##;\n"
26 "## ##;\n"
27 "####+####;bb\n"
28 "#########;\n",
29
30 // Level 2
31 "###########;\n"
32 "#####-#####;bbbbb\n"
33 "## ##;\n"
34 "## ###;\n"
35 "## # ##;\n"
36 "## ##;\n"
37 "###+##+####;bbb,bb\n"
38 "###########;\n",
39
40 // Level 3
41 "#############;\n"
42 "###-#####-###;a,aaa\n"
43 "## ##;\n"
44 "## ##;\n"
45 "## ##;\n"
46 "## ##;\n"
47 "## ##;\n"
48 "## ##;\n"
49 "######+######;aaaa\n"
50 "#############;\n",
51
52 // Level 4
53 "#############;\n"
54 "###-#####-###;aaa,aa\n"
55 "## ##;\n"
56 "## ##;\n"
57 "## ##;\n"
58 "## ##;\n"
59 "## ##;\n"
60 "## ##;\n"
61 "###+#####+###;aa,aaa\n"
62 "#############;\n"
63 };
64
65 m3x3f m_projection;
66 m3x3f m_view;
67 m3x3f m_mdl;
68
69 #define FLAG_INPUT 0x1
70 #define FLAG_OUTPUT 0x2
71 #define FLAG_CANAL 0x4
72 #define FLAG_WALL 0x8
73 #define FLAG_FLIP_FLOP 0x100
74 #define FLAG_FLIP_ROTATING 0x200
75
76 /*
77 0000 0 | 0001 1 | 0010 2 | 0011 3
78 | | | | |
79 X | X= | X | X=
80 | | |
81 0100 4 | 0101 5 | 0110 6 | 0111 7
82 | | | | |
83 =X | =X= | =X | =X=
84 | | |
85 1000 8 | 1001 9 | 1010 10 | 1011 11
86 | | | | |
87 X | X= | X | X=
88 | | | | | | |
89 1100 12 | 1101 13 | 1110 14 | 1111 15
90 | | | | |
91 =X | =X= | =X | =X=
92 | | | | | | |
93 */
94
95 enum cell_type
96 {
97 k_cell_type_ramp_right = 3,
98 k_cell_type_ramp_left = 6,
99 k_cell_type_split = 7,
100 k_cell_type_merge = 13
101 };
102
103 v3f colour_sets[] =
104 { { 0.9f, 0.2f, 0.01f },
105 { 0.2f, 0.9f, 0.14f },
106 { 0.1f, 0.3f, 0.85f } };
107
108 static void colour_code_v3( char cc, v3f target )
109 {
110 if( cc >= 'a' && cc <= 'z' )
111 {
112 int id = cc - 'a';
113
114 if( id < vg_list_size( colour_sets ) )
115 {
116 v3_copy( colour_sets[ id ], target );
117 return;
118 }
119 }
120
121 v3_copy( (v3f){0.0f,0.0f,0.0f}, target );
122 }
123
124 struct mesh
125 {
126 GLuint vao, vbo;
127 u32 elements;
128 };
129
130 static void init_mesh( struct mesh *m, float *tris, u32 length )
131 {
132 m->elements = length/3;
133 glGenVertexArrays( 1, &m->vao );
134 glGenBuffers( 1, &m->vbo );
135
136 glBindVertexArray( m->vao );
137 glBindBuffer( GL_ARRAY_BUFFER, m->vbo );
138 glBufferData( GL_ARRAY_BUFFER, length*sizeof(float), tris, GL_STATIC_DRAW );
139
140 glVertexAttribPointer( 0, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(float), (void*)0 );
141 glEnableVertexAttribArray( 0 );
142
143 VG_CHECK_GL();
144 }
145
146 static void free_mesh( struct mesh *m )
147 {
148 glDeleteVertexArrays( 1, &m->vao );
149 glDeleteBuffers( 1, &m->vbo );
150 }
151
152 static void draw_mesh( int const start, int const count )
153 {
154 glDrawArrays( GL_TRIANGLES, start*3, count*3 );
155 }
156
157 static void use_mesh( struct mesh *m )
158 {
159 glBindVertexArray( m->vao );
160 }
161
162 struct world
163 {
164 struct cell
165 {
166 u32 state;
167 u8 config;
168 }
169 *data;
170
171 u32 frame;
172
173 u32 sim_frame;
174 float sim_start;
175 int simulating;
176
177 float frame_lerp;
178
179 struct cell_terminal
180 {
181 // TODO: Split into input/output structures
182 char *conditions;
183 char recv[12];
184 int recv_count;
185 int id;
186 }
187 *io;
188
189 u32 w, h;
190
191 struct mesh tile, circle;
192
193 int selected, tile_x, tile_y;
194 v2f tile_pos;
195
196 struct fish
197 {
198 v2i pos;
199 v2i dir;
200 int alive;
201 char payload;
202 float death_time;
203 v2f physics_v;
204 v2f physics_co;
205 }
206 fishes[16];
207
208 int num_fishes;
209 } world = {};
210
211 static void map_free(void)
212 {
213 for( int i = 0; i < arrlen( world.io ); i ++ )
214 arrfree( world.io[ i ].conditions );
215
216 arrfree( world.data );
217 arrfree( world.io );
218
219 world.w = 0;
220 world.h = 0;
221 world.data = NULL;
222 world.io = NULL;
223 }
224
225 static void map_reclassify( v2i start, v2i end );
226 static int map_load( const char *str )
227 {
228 map_free();
229
230 char const *c = str;
231
232 // Scan for width
233 for(;; world.w ++)
234 {
235 if( str[world.w] == ';' )
236 break;
237 else if( !str[world.w] )
238 {
239 vg_error( "Unexpected EOF when parsing level\n" );
240 return 0;
241 }
242 }
243
244 struct cell *row = arraddnptr( world.data, world.w );
245 int cx = 0;
246 int reg_start = 0, reg_end = 0;
247
248 for(;;)
249 {
250 if( !*c )
251 break;
252
253 if( *c == ';' )
254 {
255 c ++;
256
257 // Parse attribs
258 if( *c != '\n' )
259 {
260 while( *c )
261 {
262 if( reg_start < reg_end )
263 {
264 if( *c >= 'a' && *c <= 'z' )
265 {
266 arrpush( world.io[ reg_start ].conditions, *c );
267 }
268 else
269 {
270 if( *c == ',' || *c == '\n' )
271 {
272 reg_start ++;
273
274 if( *c == '\n' )
275 break;
276 }
277 else
278 {
279 vg_error( "Unkown attribute '%c' (row: %u)\n", *c, world.h );
280 return 0;
281 }
282 }
283 }
284 else
285 {
286 vg_error( "Too many values to assign (row: %u)\n", world.h );
287 return 0;
288 }
289
290 c ++;
291 }
292 }
293
294 if( reg_start != reg_end )
295 {
296 vg_error( "Not enough values assigned (row: %u, %u of %u)\n", world.h, reg_start, reg_end );
297 return 0;
298 }
299
300 if( cx != world.w )
301 {
302 vg_error( "Not enough cells to match previous row definition (row: %u, %u<%u)\n", world.h, cx, world.w );
303 return 0;
304 }
305
306 row = arraddnptr( world.data, world.w );
307 cx = 0;
308 world.h ++;
309 reg_end = reg_start = arrlen( world.io );
310 }
311 else
312 {
313 if( cx == world.w )
314 {
315 vg_error( "Too many cells to match previous row definition (row: %u, %u>%u)\n", world.h, cx, world.w );
316 return 0;
317 }
318
319 // Tile initialization
320 // row[ cx ] .. etc
321
322 if( *c == '+' || *c == '-' )
323 {
324 struct cell_terminal term = { .id = cx + world.h*world.w };
325 arrpush( world.io, term );
326 row[ cx ++ ].state = *c == '+'? FLAG_INPUT: FLAG_OUTPUT;
327 reg_end ++;
328 }
329 else if( *c == '#' )
330 {
331 row[ cx ++ ].state = FLAG_WALL;
332 }
333 else
334 {
335 row[ cx ++ ].state = 0x00;
336 }
337 }
338
339 c ++;
340 }
341
342 map_reclassify( NULL, NULL );
343 vg_success( "Map loaded! (%u:%u)\n", world.w, world.h );
344 return 1;
345 }
346
347 static struct cell *pcell( v2i pos )
348 {
349 return &world.data[ pos[1]*world.w + pos[0] ];
350 }
351
352 int main( int argc, char *argv[] )
353 {
354 vg_init( argc, argv, "Fish (Marbles Computer) Ladder Simulator 2022 | N,M: change level | SPACE: Test | LeftClick: Toggle tile" );
355 }
356
357 void vg_start(void)
358 {
359 // Quad mesh
360 {
361 float quad_mesh[] =
362 {
363 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f,
364 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, 0.0f,
365
366 0.48f, 0.48f, 0.5f, 0.52f, 0.52f, 0.52f, // Static dot
367 0.375f, 0.25f, 0.5f, 0.75f, 0.625f, 0.25f, // Downwards pointing arrow
368 0.25f, 0.625f, 0.75f, 0.5f, 0.25f, 0.375f, // Left
369 0.625f, 0.75f, 0.5f, 0.25f, 0.375f, 0.75f, // up
370 0.75f, 0.375f, 0.25f, 0.5f, 0.75f, 0.625f
371 };
372
373 init_mesh( &world.tile, quad_mesh, vg_list_size(quad_mesh) );
374 }
375
376 // Circle mesh
377 {
378 float circle_mesh[32*6*3];
379 int res = vg_list_size( circle_mesh ) / (6*3);
380
381 for( int i = 0; i < res; i ++ )
382 {
383 v2f v0 = { sinf( ((float)i/(float)res)*VG_TAUf ), cosf( ((float)i/(float)res)*VG_TAUf ) };
384 v2f v1 = { sinf( ((float)(i+1)/(float)res)*VG_TAUf ), cosf( ((float)(i+1)/(float)res)*VG_TAUf ) };
385
386 circle_mesh[ i*6+0 ] = 0.0f;
387 circle_mesh[ i*6+1 ] = 0.0f;
388
389 v2_copy( v0, circle_mesh + 32*6 + i*12 );
390 v2_muls( v0, 0.8f, circle_mesh + 32*6 + i*12+2 );
391 v2_copy( v1, circle_mesh + 32*6 + i*12+4 );
392
393 v2_copy( v1, circle_mesh + 32*6 + i*12+6 );
394 v2_muls( v1, 0.8f, circle_mesh + 32*6 + i*12+8 );
395 v2_muls( v0, 0.8f, circle_mesh + 32*6 + i*12+10 );
396
397 v2_copy( v0, circle_mesh + i*6+4 );
398 v2_copy( v1, circle_mesh + i*6+2 );
399 v2_copy( v0, circle_mesh+i*6+4 );
400 v2_copy( v1, circle_mesh+i*6+2 );
401 }
402
403 init_mesh( &world.circle, circle_mesh, vg_list_size( circle_mesh ) );
404 }
405
406 resource_load_main();
407
408 map_load( level_pack[ 0 ] );
409 }
410
411 void vg_free(void)
412 {
413 resource_free_main();
414
415 free_mesh( &world.tile );
416 free_mesh( &world.circle );
417
418 map_free();
419 }
420
421 static int cell_interactive( v2i co )
422 {
423 // Bounds check
424 if( co[0] < 2 || co[0] >= world.w-2 || co[1] < 2 || co[1] >= world.h-2 )
425 return 0;
426
427 // Flags check
428 if( world.data[ world.w*co[1] + co[0] ].state & (FLAG_WALL|FLAG_INPUT|FLAG_OUTPUT) )
429 return 0;
430
431 // List of 3x3 configurations that we do not allow
432 static u32 invalid_src[][9] =
433 {
434 { 0,1,0,
435 1,1,1,
436 0,1,0
437 },
438 { 0,0,0,
439 0,1,1,
440 0,1,1
441 },
442 { 0,0,0,
443 1,1,0,
444 1,1,0
445 },
446 { 0,1,1,
447 0,1,1,
448 0,0,0
449 },
450 { 1,1,0,
451 1,1,0,
452 0,0,0
453 },
454 { 0,1,0,
455 0,1,1,
456 0,1,0
457 },
458 { 0,1,0,
459 1,1,0,
460 0,1,0
461 }
462 };
463
464 // Statically compile invalid configurations into bitmasks
465 static u32 invalid[ vg_list_size(invalid_src) ];
466
467 for( int i = 0; i < vg_list_size(invalid_src); i ++ )
468 {
469 u32 comped = 0x00;
470
471 for( int j = 0; j < 3; j ++ )
472 for( int k = 0; k < 3; k ++ )
473 comped |= invalid_src[i][ j*3+k ] << ((j*5)+k);
474
475 invalid[i] = comped;
476 }
477
478 // Extract 5x5 grid surrounding tile
479 u32 blob = 0x1000;
480 for( int y = co[1]-2; y < co[1]+3; y ++ )
481 for( int x = co[0]-2; x < co[0]+3; x ++ )
482 {
483 struct cell *cell = pcell((v2i){x,y});
484
485 if( cell && (cell->state & (FLAG_CANAL|FLAG_INPUT|FLAG_OUTPUT)) )
486 blob |= 0x1 << ((y-(co[1]-2))*5 + x-(co[0]-2));
487 }
488
489 // Run filter over center 3x3 grid to check for invalid configurations
490 int kernel[] = { 0, 1, 2, 5, 6, 7, 10, 11, 12 };
491 for( int i = 0; i < vg_list_size(kernel); i ++ )
492 {
493 if( blob & (0x1 << (6+kernel[i])) )
494 {
495 u32 window = blob >> kernel[i];
496
497 for( int j = 0; j < vg_list_size(invalid); j ++ )
498 if((window & invalid[j]) == invalid[j])
499 return 0;
500 }
501 }
502
503 return 1;
504 }
505
506 static void map_reclassify( v2i start, v2i end )
507 {
508 v2i full_start = { 1,1 };
509 v2i full_end = { world.w-1, world.h-1 };
510
511 if( !start || !end )
512 {
513 start = full_start;
514 end = full_end;
515 }
516
517 for( int y = vg_max( start[1], full_start[1] ); y < vg_min( end[1], full_end[1] ); y ++ )
518 {
519 for( int x = vg_max( start[0], full_start[0] ); x < vg_min( end[0], full_end[0] ); x ++ )
520 {
521 v2i dirs[] = {{1,0},{0,1},{-1,0},{0,-1}};
522
523 u8 config = 0x00;
524
525 if( pcell((v2i){x,y})->state & (FLAG_CANAL|FLAG_INPUT|FLAG_OUTPUT) )
526 {
527 for( int i = 0; i < vg_list_size( dirs ); i ++ )
528 {
529 struct cell *neighbour = pcell((v2i){x+dirs[i][0], y+dirs[i][1]});
530 if( neighbour->state & (FLAG_CANAL|FLAG_INPUT|FLAG_OUTPUT) )
531 config |= 0x1 << i;
532 }
533 }
534 else config = 0xF;
535
536 pcell((v2i){x,y})->config = config;
537 }
538 }
539 }
540
541 v2f const curve_3[] = {{0.5f,1.0f},{0.5f,0.625f},{0.625f,0.5f},{1.0f,0.5f}};
542 v2f const curve_6[] = {{0.5f,1.0f},{0.5f,0.625f},{0.375f,0.5f},{0.0f,0.5f}};
543 v2f const curve_9[] = {{1.0f,0.5f},{0.625f,0.5f},{0.5f,0.375f},{0.5f,0.0f}};
544 v2f const curve_12[]= {{0.0f,0.5f},{0.375f,0.5f},{0.5f,0.375f},{0.5f,0.0f}};
545
546 v2f const curve_7[] = {{0.5f,0.8438f},{0.875f,0.8438f},{0.625f,0.5f},{1.0f,0.5f}};
547 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}};
548
549 float const curve_7_linear_section = 0.1562f;
550
551 void vg_update(void)
552 {
553 static int curlevel = 0;
554 int changelvl = curlevel;
555 if( vg_get_button_down( "prev" ) ) { if( curlevel > 0 ) changelvl --; }
556 else if( vg_get_button_down( "next" ) ) { if( curlevel < vg_list_size( level_pack )-1 ) changelvl ++; }
557
558 if( changelvl != curlevel )
559 {
560 map_load( level_pack[ changelvl ] );
561 curlevel = changelvl;
562
563 // TEMP!!! code dupe
564 world.simulating = 0;
565 world.num_fishes = 0;
566 world.sim_frame = 0;
567
568 for( int i = 0; i < arrlen( world.io ); i ++ )
569 world.io[i].recv_count = 0;
570
571 vg_info( "Stopping simulation!\n" );
572 }
573
574 float ratio = (float)vg_window_y / (float)vg_window_x;
575 float const size = 9.5f;
576
577 v3f origin;
578 origin[0] = -0.5f * world.w;
579 origin[1] = -0.5f * world.h;
580 origin[2] = 0.0f;
581
582 m3x3_projection( m_projection, -size, size, -size*ratio, size*ratio );
583 m3x3_identity( m_view );
584 m3x3_translate( m_view, origin );
585 m3x3_mul( m_projection, m_view, vg_pv );
586 vg_projection_update();
587
588 // Input stuff
589 v2_copy( vg_mouse_ws, world.tile_pos );
590
591 world.tile_x = floorf( world.tile_pos[0] );
592 world.tile_y = floorf( world.tile_pos[1] );
593
594 // Tilemap editing
595 if( !world.simulating )
596 {
597 if( cell_interactive( (v2i){ world.tile_x, world.tile_y } ))
598 {
599 world.selected = world.tile_y * world.w + world.tile_x;
600
601 if( vg_get_button_down("primary") )
602 {
603 world.data[ world.selected ].state ^= FLAG_CANAL;
604
605 if( world.data[ world.selected ].state & FLAG_CANAL )
606 sfx_set_playrnd( &audio_tile_mod, &audio_system_sfx, 3, 6 );
607 else
608 sfx_set_playrnd( &audio_tile_mod, &audio_system_sfx, 0, 3 );
609
610 map_reclassify( (v2i){ world.tile_x -2, world.tile_y -2 },
611 (v2i){ world.tile_x +2, world.tile_y +2 } );
612 }
613 }
614 else
615 world.selected = -1;
616 }
617 else world.selected = -1;
618
619 // Simulation stop/start
620 if( vg_get_button_down("go") )
621 {
622 if( world.simulating )
623 {
624 world.simulating = 0;
625 world.num_fishes = 0;
626 world.sim_frame = 0;
627
628 for( int i = 0; i < arrlen( world.io ); i ++ )
629 world.io[i].recv_count = 0;
630
631 vg_info( "Stopping simulation!\n" );
632
633 sfx_system_fadeout( &audio_system_balls_rolling, 44100 );
634 }
635 else
636 {
637 vg_success( "Starting simulation!\n" );
638
639 sfx_set_playrnd( &audio_rolls, &audio_system_balls_rolling, 0, 1 );
640
641 world.simulating = 1;
642 world.num_fishes = 0;
643 world.sim_frame = 0;
644 world.sim_start = vg_time;
645
646 for( int i = 0; i < world.w*world.h; i ++ )
647 {
648 world.data[ i ].state &= ~FLAG_FLIP_FLOP;
649 }
650
651 for( int i = 0; i < arrlen( world.io ); i ++ )
652 world.io[i].recv_count = 0;
653 }
654 }
655
656 // Fish ticks
657 if( world.simulating )
658 {
659 while( world.sim_frame < (int)((vg_time-world.sim_start)*2.0f) )
660 {
661 //vg_info( "frame: %u\n", world.sim_frame );
662 sfx_set_playrnd( &audio_random, &audio_system_balls_switching, 0, 9 );
663
664 // Update splitter deltas
665 for( int i = 0; i < world.h*world.w; i ++ )
666 {
667 struct cell *cell = &world.data[i];
668 if( cell->config == k_cell_type_split )
669 {
670 cell->state &= ~FLAG_FLIP_ROTATING;
671 }
672 }
673
674 // Update fish positions
675 for( int i = 0; i < world.num_fishes; i ++ )
676 {
677 struct fish *fish = &world.fishes[i];
678 struct cell *cell_current = pcell( fish->pos );
679
680 if( fish->alive == -1 )
681 fish->alive = 0;
682
683 if( fish->alive != 1 )
684 continue;
685
686 // Apply to output
687 if( cell_current->state & FLAG_OUTPUT )
688 {
689 for( int j = 0; j < arrlen( world.io ); j ++ )
690 {
691 struct cell_terminal *term = &world.io[j];
692
693 if( term->id == fish->pos[1]*world.w + fish->pos[0] )
694 {
695 term->recv[ term->recv_count ++ ] = fish->payload;
696 break;
697 }
698 }
699
700 fish->alive = 0;
701 continue;
702 }
703
704 if( cell_current->config == k_cell_type_split )
705 {
706 // Flip flop L/R
707 fish->dir[0] = cell_current->state&FLAG_FLIP_FLOP?1:-1;
708 fish->dir[1] = 0;
709
710 cell_current->state ^= FLAG_FLIP_FLOP;
711 }
712 else if( cell_current->config == k_cell_type_merge )
713 {
714 // Can only move up
715 fish->dir[0] = 0;
716 fish->dir[1] = -1;
717 }
718 else
719 {
720 struct cell *cell_next = pcell( (v2i){ fish->pos[0]+fish->dir[0], fish->pos[1]+fish->dir[1] } );
721 if( !(cell_next->state & (FLAG_CANAL|FLAG_OUTPUT)) )
722 {
723 // Try other directions for valid, so down, left, right..
724 v2i dirs[] = {{1,0},{-1,0},{0,-1}};
725 vg_info( "Trying some other directions...\n" );
726
727 for( int j = 0; j < vg_list_size(dirs); j ++ )
728 {
729 if( (dirs[j][0] == -fish->dir[0]) && (dirs[j][1] == -fish->dir[1]) )
730 continue;
731
732 if( pcell( (v2i){ fish->pos[0]+dirs[j][0], fish->pos[1]+dirs[j][1] } )->state & (FLAG_CANAL|FLAG_OUTPUT) )
733 {
734 fish->dir[0] = dirs[j][0];
735 fish->dir[1] = dirs[j][1];
736 }
737 }
738 }
739 }
740
741 fish->pos[0] += fish->dir[0];
742 fish->pos[1] += fish->dir[1];
743
744 struct cell *cell_entry = pcell( fish->pos );
745
746 if( !(cell_entry->state & (FLAG_INPUT|FLAG_CANAL|FLAG_OUTPUT) ))
747 fish->alive = 0;
748 else
749 {
750 if( fish->dir[0] )
751 {
752 if( cell_entry->config == k_cell_type_split ||
753 cell_entry->config == k_cell_type_ramp_right ||
754 cell_entry->config == k_cell_type_ramp_left )
755 {
756 // Special death (FALL)
757 v2_sub( fish->physics_co, fish->physics_v, fish->physics_v );
758 v2_divs( fish->physics_v, vg_time_delta, fish->physics_v );
759
760 fish->alive = -2;
761 vg_warn( "Special death (fall)\n" );
762 continue;
763 }
764 }
765
766 if( cell_entry->config == k_cell_type_split )
767 {
768 sfx_set_playrnd( &audio_splitter, &audio_system_balls_important, 0, 1 );
769 cell_entry->state |= FLAG_FLIP_ROTATING;
770 }
771 }
772 }
773
774 // Check for collisions
775 for( int i = 0; i < world.num_fishes; i ++ )
776 {
777 if( world.fishes[i].alive == 1 )
778 {
779 for( int j = i+1; j < world.num_fishes; j ++ )
780 {
781 if( (world.fishes[j].alive == 1) && (world.fishes[i].pos[0] == world.fishes[j].pos[0]) &&
782 (world.fishes[i].pos[1] == world.fishes[j].pos[1]) )
783 {
784 // Shatter death (+0.5s)
785 world.fishes[i].alive = -1;
786 world.fishes[j].alive = -1;
787 world.fishes[i].death_time = 0.5f;
788 world.fishes[j].death_time = 0.5f;
789 }
790 }
791 }
792 }
793
794 // Spawn fishes
795 for( int i = 0; i < arrlen( world.io ); i ++ )
796 {
797 struct cell_terminal *term = &world.io[ i ];
798 int posx = term->id % world.w;
799 int posy = (term->id - posx)/world.w;
800 int is_input = world.data[ term->id ].state & FLAG_INPUT;
801
802 if( is_input )
803 {
804 if( world.sim_frame < arrlen( term->conditions ) )
805 {
806 struct fish *fish = &world.fishes[world.num_fishes++];
807 fish->pos[0] = posx;
808 fish->pos[1] = posy;
809 fish->alive = 1;
810 fish->payload = term->conditions[world.sim_frame];
811
812 int can_spawn = 0;
813
814 v2i dirs[] = {{1,0},{-1,0},{0,-1}};
815 for( int j = 0; j < vg_list_size(dirs); j ++ )
816 if( pcell( (v2i){ posx+dirs[j][0], posy+dirs[j][1] } )->state & FLAG_CANAL )
817 {
818 fish->dir[0] = dirs[j][0];
819 fish->dir[1] = dirs[j][1];
820 can_spawn = 1;
821 break;
822 }
823
824 if( !can_spawn )
825 world.num_fishes--;
826 }
827 }
828 }
829
830 world.sim_frame ++;
831 }
832
833 float scaled_time = 0.0f;
834 scaled_time = (vg_time-world.sim_start)*2.0f;
835 world.frame_lerp = scaled_time - (float)world.sim_frame;
836
837 // Update positions
838 for( int i = 0; i < world.num_fishes; i ++ )
839 {
840 struct fish *fish = &world.fishes[i];
841
842 if( fish->alive == 0 )
843 continue;
844
845 if( fish->alive == -1 && (world.frame_lerp > fish->death_time) )
846 continue; // Todo: particle thing?
847
848 if( fish->alive == -2 )
849 {
850 v2_muladds( fish->physics_v, (v2f){ 0.0, -9.8f }, vg_time_delta, fish->physics_v );
851 v2_muladds( fish->physics_co, fish->physics_v, vg_time_delta, fish->physics_co );
852 }
853 else
854 {
855 struct cell *cell = pcell(fish->pos);
856 v2f const *curve;
857
858 float t = world.frame_lerp;
859
860 v2_copy( fish->physics_co, fish->physics_v );
861
862 switch( cell->config )
863 {
864 case 13:
865 if( fish->dir[0] == 1 )
866 curve = curve_12;
867 else
868 curve = curve_9;
869 break;
870 case 3: curve = curve_3; break;
871 case 6: curve = curve_6; break;
872 case 9: curve = curve_9; break;
873 case 12: curve = curve_12; break;
874 case 7:
875 if( t > curve_7_linear_section )
876 {
877 t -= curve_7_linear_section;
878 t *= (1.0f/(1.0f-curve_7_linear_section));
879
880 curve = cell->state & FLAG_FLIP_FLOP? curve_7: curve_7_1;
881 }
882 else curve = NULL;
883 break;
884 default: curve = NULL; break;
885 }
886
887 if( curve )
888 {
889 float t2 = t * t;
890 float t3 = t * t * t;
891
892 float cA = 3.0f*t2 - 3.0f*t3;
893 float cB = 3.0f*t3 - 6.0f*t2 + 3.0f*t;
894 float cC = 3.0f*t2 - t3 - 3.0f*t + 1.0f;
895
896 fish->physics_co[0] = t3*curve[3][0] + cA*curve[2][0] + cB*curve[1][0] + cC*curve[0][0];
897 fish->physics_co[1] = t3*curve[3][1] + cA*curve[2][1] + cB*curve[1][1] + cC*curve[0][1];
898 fish->physics_co[0] += (float)fish->pos[0];
899 fish->physics_co[1] += (float)fish->pos[1];
900 }
901 else
902 {
903 v2f origin;
904 origin[0] = (float)fish->pos[0] + (float)fish->dir[0]*-0.5f + 0.5f;
905 origin[1] = (float)fish->pos[1] + (float)fish->dir[1]*-0.5f + 0.5f;
906
907 fish->physics_co[0] = origin[0] + (float)fish->dir[0]*t;
908 fish->physics_co[1] = origin[1] + (float)fish->dir[1]*t;
909 }
910 }
911 }
912 }
913 }
914
915 static void render_tiles( v2i start, v2i end, v4f const regular_colour, v4f const selected_colour )
916 {
917 v2i full_start = { 0,0 };
918 v2i full_end = { world.w, world.h };
919
920 if( !start || !end )
921 {
922 start = full_start;
923 end = full_end;
924 }
925
926 glUniform4fv( SHADER_UNIFORM( shader_tile_main, "uColour" ), 1, regular_colour );
927
928 for( int y = start[1]; y < end[1]; y ++ )
929 {
930 for( int x = start[0]; x < end[0]; x ++ )
931 {
932 struct cell *cell = pcell((v2i){x,y});
933 int selected = world.selected == y*world.w + x;
934
935 int tile_offsets[][2] =
936 {
937 {2, 0}, {0, 3}, {0, 2}, {2, 2},
938 {1, 0}, {2, 3}, {3, 2}, {1, 3},
939 {3, 1}, {0, 1}, {1, 2}, {2, 1},
940 {1, 1}, {3, 3}, {2, 1}, {2, 1}
941 };
942
943 int uv[2] = { 3, 0 };
944
945 if( cell->state & (FLAG_CANAL|FLAG_INPUT|FLAG_OUTPUT) )
946 {
947 uv[0] = tile_offsets[ cell->config ][0];
948 uv[1] = tile_offsets[ cell->config ][1];
949 }
950
951 glUniform4f( SHADER_UNIFORM( shader_tile_main, "uOffset" ), (float)x, (float)y, uv[0], uv[1] );
952 if( selected )
953 {
954 glUniform4fv( SHADER_UNIFORM( shader_tile_main, "uColour" ), 1, selected_colour );
955 draw_mesh( 0, 2 );
956 glUniform4fv( SHADER_UNIFORM( shader_tile_main, "uColour" ), 1, regular_colour );
957 }
958 else
959 draw_mesh( 0, 2 );
960 }
961 }
962 }
963
964 void vg_render(void)
965 {
966 glViewport( 0,0, vg_window_x, vg_window_y );
967
968 glDisable( GL_DEPTH_TEST );
969 glClearColor( 0.8f, 0.8f, 0.8f, 1.0f );
970 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
971
972 v4f const colour_default = {1.0f, 1.0f, 1.0f, 1.0f};
973 v4f const colour_selected = {0.90f, 0.92f, 1.0f, 1.0f};
974
975 // TILE SET RENDERING
976 // todo: just slam everything into a mesh...
977 // when user modifies a tile the neighbours can be easily uploaded to gpu mem
978 // in ~3 subBuffers
979 // Currently we're uploading a fair amount of data every frame anyway.
980 // NOTE: this is for final optimisations ONLY!
981 // ======================================================================
982
983 use_mesh( &world.tile );
984
985 SHADER_USE( shader_tile_main );
986
987 m2x2f subtransform;
988 m2x2_identity( subtransform );
989 glUniformMatrix2fv( SHADER_UNIFORM( shader_tile_main, "uSubTransform" ), 1, GL_FALSE, (float *)subtransform );
990 glUniformMatrix3fv( SHADER_UNIFORM( shader_tile_main, "uPv" ), 1, GL_FALSE, (float *)vg_pv );
991 glUniform1f( SHADER_UNIFORM( shader_tile_main, "uGhost" ), 0.0f );
992
993 // Bind textures
994 vg_tex2d_bind( &tex_tile_data, 0 );
995 glUniform1i( SHADER_UNIFORM( shader_tile_main, "uTexGlyphs" ), 0 );
996
997 vg_tex2d_bind( &tex_wood, 1 );
998 glUniform1i( SHADER_UNIFORM( shader_tile_main, "uTexWood" ), 1 );
999
1000 render_tiles( NULL, NULL, colour_default, colour_default );
1001
1002 glEnable(GL_BLEND);
1003 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1004 glBlendEquation(GL_FUNC_ADD);
1005
1006 SHADER_USE( shader_ball );
1007 glUniformMatrix3fv( SHADER_UNIFORM( shader_ball, "uPv" ), 1, GL_FALSE, (float *)vg_pv );
1008
1009 vg_tex2d_bind( &tex_ball, 0 );
1010 glUniform1i( SHADER_UNIFORM( shader_ball, "uTexMain" ), 0 );
1011
1012 // Draw 'fish'
1013 if( world.simulating )
1014 {
1015 for( int i = 0; i < world.num_fishes; i ++ )
1016 {
1017 struct fish *fish = &world.fishes[i];
1018
1019 if( fish->alive == 0 )
1020 continue;
1021
1022 if( fish->alive == -1 && (world.frame_lerp > fish->death_time) )
1023 continue;
1024
1025 v4f dot_colour = { 0.0f, 0.0f, 0.0f, 1.0f };
1026 colour_code_v3( fish->payload, dot_colour );
1027
1028 glUniform3fv( SHADER_UNIFORM( shader_ball, "uColour" ), 1, dot_colour );
1029 glUniform2fv( SHADER_UNIFORM( shader_ball, "uOffset" ), 1, fish->physics_co );
1030 draw_mesh( 0, 32 );
1031 }
1032 }
1033
1034 SHADER_USE( shader_tile_main );
1035
1036 // Bind textures
1037 vg_tex2d_bind( &tex_tile_data, 0 );
1038 glUniform1i( SHADER_UNIFORM( shader_tile_main, "uTexGlyphs" ), 0 );
1039
1040 vg_tex2d_bind( &tex_wood, 1 );
1041 glUniform1i( SHADER_UNIFORM( shader_tile_main, "uTexWood" ), 1 );
1042
1043 render_tiles( NULL, NULL, colour_default, colour_selected );
1044
1045 // Draw splitters
1046
1047 for( int y = 0; y < world.h; y ++ )
1048 {
1049 for( int x = 0; x < world.w; x ++ )
1050 {
1051 struct cell *cell = pcell((v2i){x,y});
1052
1053 if( cell->config == k_cell_type_split )
1054 {
1055 float rotation = cell->state & FLAG_FLIP_FLOP? vg_rad( -45.0f ): vg_rad( 45.0f );
1056
1057 if( cell->state & FLAG_FLIP_ROTATING )
1058 {
1059 if( (world.frame_lerp > curve_7_linear_section) )
1060 {
1061 float const rotation_speed = 0.4f;
1062 if( (world.frame_lerp < 1.0f-rotation_speed) )
1063 {
1064 float t = world.frame_lerp - curve_7_linear_section;
1065 t *= -2.0f * (1.0f/(1.0f-(curve_7_linear_section+rotation_speed)));
1066 t += 1.0f;
1067
1068 rotation *= t;
1069 }
1070 else
1071 rotation *= -1.0f;
1072 }
1073 }
1074
1075 m2x2_create_rotation( subtransform, rotation );
1076
1077 glUniformMatrix2fv( SHADER_UNIFORM( shader_tile_main, "uSubTransform" ), 1, GL_FALSE, (float *)subtransform );
1078 glUniform4f( SHADER_UNIFORM( shader_tile_main, "uOffset" ), (float)x, (float)y + 0.125f, 0.0f, 0.0f );
1079 draw_mesh( 0, 2 );
1080 }
1081 }
1082 }
1083
1084 // Edit overlay
1085 if( world.selected != -1 && !(world.data[ world.selected ].state & FLAG_CANAL) )
1086 {
1087 v2i new_begin = { world.tile_x - 2, world.tile_y - 2 };
1088 v2i new_end = { world.tile_x + 2, world.tile_y + 2 };
1089
1090 world.data[ world.selected ].state ^= FLAG_CANAL;
1091 map_reclassify( new_begin, new_end );
1092
1093 m2x2_identity( subtransform );
1094 glUniform1f( SHADER_UNIFORM( shader_tile_main, "uGhost" ), 1.0f );
1095 glUniformMatrix2fv( SHADER_UNIFORM( shader_tile_main, "uSubTransform" ), 1, GL_FALSE, (float *)subtransform );
1096 glUniform2fv( SHADER_UNIFORM( shader_tile_main, "uMousePos" ), 1, world.tile_pos );
1097
1098 render_tiles( new_begin, new_end, colour_default, colour_default );
1099
1100 world.data[ world.selected ].state ^= FLAG_CANAL;
1101 map_reclassify( new_begin, new_end );
1102 }
1103
1104 //glDisable(GL_BLEND);
1105
1106 glDisable(GL_BLEND);
1107
1108 SHADER_USE( shader_tile_colour );
1109 glUniformMatrix3fv( SHADER_UNIFORM( shader_tile_colour, "uPv" ), 1, GL_FALSE, (float *)vg_pv );
1110 use_mesh( &world.circle );
1111
1112 // Draw i/o arrays
1113 for( int i = 0; i < arrlen( world.io ); i ++ )
1114 {
1115 struct cell_terminal *term = &world.io[ i ];
1116 int posx = term->id % world.w;
1117 int posy = (term->id - posx)/world.w;
1118 int is_input = world.data[ term->id ].state & FLAG_INPUT;
1119
1120 int const filled_start = 0;
1121 int const filled_count = 32;
1122 int const empty_start = 32;
1123 int const empty_count = 32*2;
1124
1125 v4f dot_colour = { 0.0f, 0.0f, 0.0f, 1.0f };
1126
1127 for( int j = 0; j < arrlen( term->conditions ); j ++ )
1128 {
1129 float y_offset = is_input? 0.8f: 0.2f;
1130 glUniform3f( SHADER_UNIFORM( shader_tile_colour, "uOffset" ), (float)posx + 0.2f + 0.2f * (float)j, (float)posy + y_offset, 0.1f );
1131
1132 if( is_input )
1133 {
1134 colour_code_v3( term->conditions[j], dot_colour );
1135 glUniform4fv( SHADER_UNIFORM( shader_tile_colour, "uColour" ), 1, dot_colour );
1136
1137 // Draw filled if tick not passed, draw empty if empty
1138 if( world.sim_frame > j )
1139 draw_mesh( empty_start, empty_count );
1140 else
1141 draw_mesh( filled_start, filled_count );
1142 }
1143 else
1144 {
1145 if( term->recv_count > j )
1146 {
1147 colour_code_v3( term->recv[j], dot_colour );
1148 v3_muls( dot_colour, 0.8f, dot_colour );
1149 glUniform4fv( SHADER_UNIFORM( shader_tile_colour, "uColour" ), 1, dot_colour );
1150
1151 draw_mesh( filled_start, filled_count );
1152 }
1153
1154 colour_code_v3( term->conditions[j], dot_colour );
1155 glUniform4fv( SHADER_UNIFORM( shader_tile_colour, "uColour" ), 1, dot_colour );
1156
1157 draw_mesh( empty_start, empty_count );
1158 }
1159 }
1160 }
1161
1162 if( world.simulating )
1163 {
1164 glUniform4f( SHADER_UNIFORM( shader_tile_colour, "uColour" ), 0.0f, 0.0f, 0.0f, 1.0f );
1165 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 );
1166 draw_mesh( 0, 32 );
1167 }
1168 }
1169
1170 void vg_ui(void)
1171 {
1172 //ui_test();
1173 sfx_internal_debug_overlay();
1174 }