dont remember what i did
[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 // Fit within screen
575
576 float r1 = (float)vg_window_y / (float)vg_window_x,
577 r2 = (float)world.h / (float)world.w,
578 size;
579
580 size = ( r2 < r1? (float)world.w * 0.5f: ((float)world.h * 0.5f) / r1 ) + 2.5f;
581 m3x3_projection( m_projection, -size, size, -size*r1, size*r1 );
582
583 v3f origin;
584 origin[0] = floorf( -0.5f * world.w );
585 origin[1] = floorf( -0.5f * world.h );
586 origin[2] = 0.0f;
587
588 m3x3_identity( m_view );
589 m3x3_translate( m_view, origin );
590 m3x3_mul( m_projection, m_view, vg_pv );
591 vg_projection_update();
592
593 // Input stuff
594 v2_copy( vg_mouse_ws, world.tile_pos );
595
596 world.tile_x = floorf( world.tile_pos[0] );
597 world.tile_y = floorf( world.tile_pos[1] );
598
599 // Tilemap editing
600 if( !world.simulating )
601 {
602 if( cell_interactive( (v2i){ world.tile_x, world.tile_y } ))
603 {
604 world.selected = world.tile_y * world.w + world.tile_x;
605
606 if( vg_get_button_down("primary") )
607 {
608 world.data[ world.selected ].state ^= FLAG_CANAL;
609
610 if( world.data[ world.selected ].state & FLAG_CANAL )
611 sfx_set_playrnd( &audio_tile_mod, &audio_system_sfx, 3, 6 );
612 else
613 sfx_set_playrnd( &audio_tile_mod, &audio_system_sfx, 0, 3 );
614
615 map_reclassify( (v2i){ world.tile_x -2, world.tile_y -2 },
616 (v2i){ world.tile_x +2, world.tile_y +2 } );
617 }
618 }
619 else
620 world.selected = -1;
621 }
622 else world.selected = -1;
623
624 // Simulation stop/start
625 if( vg_get_button_down("go") )
626 {
627 if( world.simulating )
628 {
629 world.simulating = 0;
630 world.num_fishes = 0;
631 world.sim_frame = 0;
632
633 for( int i = 0; i < arrlen( world.io ); i ++ )
634 world.io[i].recv_count = 0;
635
636 vg_info( "Stopping simulation!\n" );
637
638 sfx_system_fadeout( &audio_system_balls_rolling, 44100 );
639 }
640 else
641 {
642 vg_success( "Starting simulation!\n" );
643
644 sfx_set_playrnd( &audio_rolls, &audio_system_balls_rolling, 0, 1 );
645
646 world.simulating = 1;
647 world.num_fishes = 0;
648 world.sim_frame = 0;
649 world.sim_start = vg_time;
650
651 for( int i = 0; i < world.w*world.h; i ++ )
652 {
653 world.data[ i ].state &= ~FLAG_FLIP_FLOP;
654 }
655
656 for( int i = 0; i < arrlen( world.io ); i ++ )
657 world.io[i].recv_count = 0;
658 }
659 }
660
661 // Fish ticks
662 if( world.simulating )
663 {
664 while( world.sim_frame < (int)((vg_time-world.sim_start)*2.0f) )
665 {
666 //vg_info( "frame: %u\n", world.sim_frame );
667 sfx_set_playrnd( &audio_random, &audio_system_balls_switching, 0, 9 );
668
669 // Update splitter deltas
670 for( int i = 0; i < world.h*world.w; i ++ )
671 {
672 struct cell *cell = &world.data[i];
673 if( cell->config == k_cell_type_split )
674 {
675 cell->state &= ~FLAG_FLIP_ROTATING;
676 }
677 }
678
679 // Update fish positions
680 for( int i = 0; i < world.num_fishes; i ++ )
681 {
682 struct fish *fish = &world.fishes[i];
683 struct cell *cell_current = pcell( fish->pos );
684
685 if( fish->alive == -1 )
686 fish->alive = 0;
687
688 if( fish->alive != 1 )
689 continue;
690
691 // Apply to output
692 if( cell_current->state & FLAG_OUTPUT )
693 {
694 for( int j = 0; j < arrlen( world.io ); j ++ )
695 {
696 struct cell_terminal *term = &world.io[j];
697
698 if( term->id == fish->pos[1]*world.w + fish->pos[0] )
699 {
700 term->recv[ term->recv_count ++ ] = fish->payload;
701 break;
702 }
703 }
704
705 fish->alive = 0;
706 continue;
707 }
708
709 if( cell_current->config == k_cell_type_split )
710 {
711 // Flip flop L/R
712 fish->dir[0] = cell_current->state&FLAG_FLIP_FLOP?1:-1;
713 fish->dir[1] = 0;
714
715 cell_current->state ^= FLAG_FLIP_FLOP;
716 }
717 else if( cell_current->config == k_cell_type_merge )
718 {
719 // Can only move up
720 fish->dir[0] = 0;
721 fish->dir[1] = -1;
722 }
723 else
724 {
725 struct cell *cell_next = pcell( (v2i){ fish->pos[0]+fish->dir[0], fish->pos[1]+fish->dir[1] } );
726 if( !(cell_next->state & (FLAG_CANAL|FLAG_OUTPUT)) )
727 {
728 // Try other directions for valid, so down, left, right..
729 v2i dirs[] = {{1,0},{-1,0},{0,-1}};
730 vg_info( "Trying some other directions...\n" );
731
732 for( int j = 0; j < vg_list_size(dirs); j ++ )
733 {
734 if( (dirs[j][0] == -fish->dir[0]) && (dirs[j][1] == -fish->dir[1]) )
735 continue;
736
737 if( pcell( (v2i){ fish->pos[0]+dirs[j][0], fish->pos[1]+dirs[j][1] } )->state & (FLAG_CANAL|FLAG_OUTPUT) )
738 {
739 fish->dir[0] = dirs[j][0];
740 fish->dir[1] = dirs[j][1];
741 }
742 }
743 }
744 }
745
746 fish->pos[0] += fish->dir[0];
747 fish->pos[1] += fish->dir[1];
748
749 struct cell *cell_entry = pcell( fish->pos );
750
751 if( !(cell_entry->state & (FLAG_INPUT|FLAG_CANAL|FLAG_OUTPUT) ))
752 fish->alive = 0;
753 else
754 {
755 if( fish->dir[0] )
756 {
757 if( cell_entry->config == k_cell_type_split ||
758 cell_entry->config == k_cell_type_ramp_right ||
759 cell_entry->config == k_cell_type_ramp_left )
760 {
761 // Special death (FALL)
762 v2_sub( fish->physics_co, fish->physics_v, fish->physics_v );
763 v2_divs( fish->physics_v, vg_time_delta, fish->physics_v );
764
765 fish->alive = -2;
766 vg_warn( "Special death (fall)\n" );
767 continue;
768 }
769 }
770
771 if( cell_entry->config == k_cell_type_split )
772 {
773 sfx_set_playrnd( &audio_splitter, &audio_system_balls_important, 0, 1 );
774 cell_entry->state |= FLAG_FLIP_ROTATING;
775 }
776 }
777 }
778
779 // Check for collisions
780 for( int i = 0; i < world.num_fishes; i ++ )
781 {
782 if( world.fishes[i].alive == 1 )
783 {
784 for( int j = i+1; j < world.num_fishes; j ++ )
785 {
786 if( (world.fishes[j].alive == 1) && (world.fishes[i].pos[0] == world.fishes[j].pos[0]) &&
787 (world.fishes[i].pos[1] == world.fishes[j].pos[1]) )
788 {
789 // Shatter death (+0.5s)
790 world.fishes[i].alive = -1;
791 world.fishes[j].alive = -1;
792 world.fishes[i].death_time = 0.5f;
793 world.fishes[j].death_time = 0.5f;
794 }
795 }
796 }
797 }
798
799 // Spawn fishes
800 for( int i = 0; i < arrlen( world.io ); i ++ )
801 {
802 struct cell_terminal *term = &world.io[ i ];
803 int posx = term->id % world.w;
804 int posy = (term->id - posx)/world.w;
805 int is_input = world.data[ term->id ].state & FLAG_INPUT;
806
807 if( is_input )
808 {
809 if( world.sim_frame < arrlen( term->conditions ) )
810 {
811 struct fish *fish = &world.fishes[world.num_fishes++];
812 fish->pos[0] = posx;
813 fish->pos[1] = posy;
814 fish->alive = 1;
815 fish->payload = term->conditions[world.sim_frame];
816
817 int can_spawn = 0;
818
819 v2i dirs[] = {{1,0},{-1,0},{0,-1}};
820 for( int j = 0; j < vg_list_size(dirs); j ++ )
821 if( pcell( (v2i){ posx+dirs[j][0], posy+dirs[j][1] } )->state & FLAG_CANAL )
822 {
823 fish->dir[0] = dirs[j][0];
824 fish->dir[1] = dirs[j][1];
825 can_spawn = 1;
826 break;
827 }
828
829 if( !can_spawn )
830 world.num_fishes--;
831 }
832 }
833 }
834
835 world.sim_frame ++;
836 }
837
838 float scaled_time = 0.0f;
839 scaled_time = (vg_time-world.sim_start)*2.0f;
840 world.frame_lerp = scaled_time - (float)world.sim_frame;
841
842 // Update positions
843 for( int i = 0; i < world.num_fishes; i ++ )
844 {
845 struct fish *fish = &world.fishes[i];
846
847 if( fish->alive == 0 )
848 continue;
849
850 if( fish->alive == -1 && (world.frame_lerp > fish->death_time) )
851 continue; // Todo: particle thing?
852
853 if( fish->alive == -2 )
854 {
855 v2_muladds( fish->physics_v, (v2f){ 0.0, -9.8f }, vg_time_delta, fish->physics_v );
856 v2_muladds( fish->physics_co, fish->physics_v, vg_time_delta, fish->physics_co );
857 }
858 else
859 {
860 struct cell *cell = pcell(fish->pos);
861 v2f const *curve;
862
863 float t = world.frame_lerp;
864
865 v2_copy( fish->physics_co, fish->physics_v );
866
867 switch( cell->config )
868 {
869 case 13:
870 if( fish->dir[0] == 1 )
871 curve = curve_12;
872 else
873 curve = curve_9;
874 break;
875 case 3: curve = curve_3; break;
876 case 6: curve = curve_6; break;
877 case 9: curve = curve_9; break;
878 case 12: curve = curve_12; break;
879 case 7:
880 if( t > curve_7_linear_section )
881 {
882 t -= curve_7_linear_section;
883 t *= (1.0f/(1.0f-curve_7_linear_section));
884
885 curve = cell->state & FLAG_FLIP_FLOP? curve_7: curve_7_1;
886 }
887 else curve = NULL;
888 break;
889 default: curve = NULL; break;
890 }
891
892 if( curve )
893 {
894 float t2 = t * t;
895 float t3 = t * t * t;
896
897 float cA = 3.0f*t2 - 3.0f*t3;
898 float cB = 3.0f*t3 - 6.0f*t2 + 3.0f*t;
899 float cC = 3.0f*t2 - t3 - 3.0f*t + 1.0f;
900
901 fish->physics_co[0] = t3*curve[3][0] + cA*curve[2][0] + cB*curve[1][0] + cC*curve[0][0];
902 fish->physics_co[1] = t3*curve[3][1] + cA*curve[2][1] + cB*curve[1][1] + cC*curve[0][1];
903 fish->physics_co[0] += (float)fish->pos[0];
904 fish->physics_co[1] += (float)fish->pos[1];
905 }
906 else
907 {
908 v2f origin;
909 origin[0] = (float)fish->pos[0] + (float)fish->dir[0]*-0.5f + 0.5f;
910 origin[1] = (float)fish->pos[1] + (float)fish->dir[1]*-0.5f + 0.5f;
911
912 fish->physics_co[0] = origin[0] + (float)fish->dir[0]*t;
913 fish->physics_co[1] = origin[1] + (float)fish->dir[1]*t;
914 }
915 }
916 }
917 }
918 }
919
920 static void render_tiles( v2i start, v2i end, v4f const regular_colour, v4f const selected_colour )
921 {
922 v2i full_start = { 0,0 };
923 v2i full_end = { world.w, world.h };
924
925 if( !start || !end )
926 {
927 start = full_start;
928 end = full_end;
929 }
930
931 glUniform4fv( SHADER_UNIFORM( shader_tile_main, "uColour" ), 1, regular_colour );
932
933 for( int y = start[1]; y < end[1]; y ++ )
934 {
935 for( int x = start[0]; x < end[0]; x ++ )
936 {
937 struct cell *cell = pcell((v2i){x,y});
938 int selected = world.selected == y*world.w + x;
939
940 int tile_offsets[][2] =
941 {
942 {2, 0}, {0, 3}, {0, 2}, {2, 2},
943 {1, 0}, {2, 3}, {3, 2}, {1, 3},
944 {3, 1}, {0, 1}, {1, 2}, {2, 1},
945 {1, 1}, {3, 3}, {2, 1}, {2, 1}
946 };
947
948 int uv[2] = { 3, 0 };
949
950 if( cell->state & (FLAG_CANAL|FLAG_INPUT|FLAG_OUTPUT) )
951 {
952 uv[0] = tile_offsets[ cell->config ][0];
953 uv[1] = tile_offsets[ cell->config ][1];
954 } else continue;
955
956 glUniform4f( SHADER_UNIFORM( shader_tile_main, "uOffset" ), (float)x, (float)y, uv[0], uv[1] );
957 if( selected )
958 {
959 glUniform4fv( SHADER_UNIFORM( shader_tile_main, "uColour" ), 1, selected_colour );
960 draw_mesh( 0, 2 );
961 glUniform4fv( SHADER_UNIFORM( shader_tile_main, "uColour" ), 1, regular_colour );
962 }
963 else
964 draw_mesh( 0, 2 );
965 }
966 }
967 }
968
969 void vg_render(void)
970 {
971 glViewport( 0,0, vg_window_x, vg_window_y );
972
973 glDisable( GL_DEPTH_TEST );
974 glClearColor( 0.369768f, 0.3654f, 0.42f, 1.0f );
975 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
976
977 v4f const colour_default = {1.0f, 1.0f, 1.0f, 1.0f};
978 v4f const colour_selected = {0.90f, 0.92f, 1.0f, 1.0f};
979
980 // TILE SET RENDERING
981 // todo: just slam everything into a mesh...
982 // when user modifies a tile the neighbours can be easily uploaded to gpu mem
983 // in ~3 subBuffers
984 // Currently we're uploading a fair amount of data every frame anyway.
985 // NOTE: this is for final optimisations ONLY!
986 // ======================================================================
987
988 use_mesh( &world.tile );
989
990 // Draw background
991
992 if(0){
993
994 SHADER_USE( shader_background );
995 glUniformMatrix3fv( SHADER_UNIFORM( shader_background, "uPv" ), 1, GL_FALSE, (float *)vg_pv );
996
997 vg_tex2d_bind( &tex_background, 0 );
998 glUniform1i( SHADER_UNIFORM( shader_background, "uTexMain" ), 0 );
999
1000 glUniform3f( SHADER_UNIFORM( shader_background, "uOffset" ), -50, -50, 100 );
1001
1002 draw_mesh( 0, 2 );
1003
1004 }
1005
1006
1007 SHADER_USE( shader_tile_main );
1008
1009 m2x2f subtransform;
1010 m2x2_identity( subtransform );
1011 glUniformMatrix2fv( SHADER_UNIFORM( shader_tile_main, "uSubTransform" ), 1, GL_FALSE, (float *)subtransform );
1012 glUniformMatrix3fv( SHADER_UNIFORM( shader_tile_main, "uPv" ), 1, GL_FALSE, (float *)vg_pv );
1013 glUniform1f( SHADER_UNIFORM( shader_tile_main, "uGhost" ), 0.0f );
1014 glUniform1f( SHADER_UNIFORM( shader_tile_main, "uForeground" ), 0.0f );
1015
1016 glEnable(GL_BLEND);
1017 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1018 glBlendEquation(GL_FUNC_ADD);
1019
1020 // Bind textures
1021 vg_tex2d_bind( &tex_tile_data, 0 );
1022 glUniform1i( SHADER_UNIFORM( shader_tile_main, "uTexGlyphs" ), 0 );
1023
1024 vg_tex2d_bind( &tex_wood, 1 );
1025 glUniform1i( SHADER_UNIFORM( shader_tile_main, "uTexWood" ), 1 );
1026
1027 render_tiles( NULL, NULL, colour_default, colour_default );
1028
1029
1030
1031 SHADER_USE( shader_ball );
1032 glUniformMatrix3fv( SHADER_UNIFORM( shader_ball, "uPv" ), 1, GL_FALSE, (float *)vg_pv );
1033
1034 vg_tex2d_bind( &tex_ball, 0 );
1035 glUniform1i( SHADER_UNIFORM( shader_ball, "uTexMain" ), 0 );
1036
1037 // Draw 'fish'
1038 if( world.simulating )
1039 {
1040 for( int i = 0; i < world.num_fishes; i ++ )
1041 {
1042 struct fish *fish = &world.fishes[i];
1043
1044 if( fish->alive == 0 )
1045 continue;
1046
1047 if( fish->alive == -1 && (world.frame_lerp > fish->death_time) )
1048 continue;
1049
1050 v4f dot_colour = { 0.0f, 0.0f, 0.0f, 1.0f };
1051 colour_code_v3( fish->payload, dot_colour );
1052
1053 glUniform3fv( SHADER_UNIFORM( shader_ball, "uColour" ), 1, dot_colour );
1054 glUniform2fv( SHADER_UNIFORM( shader_ball, "uOffset" ), 1, fish->physics_co );
1055 draw_mesh( 0, 32 );
1056 }
1057 }
1058
1059 SHADER_USE( shader_tile_main );
1060
1061 // Bind textures
1062 vg_tex2d_bind( &tex_tile_data, 0 );
1063 glUniform1i( SHADER_UNIFORM( shader_tile_main, "uTexGlyphs" ), 0 );
1064
1065 vg_tex2d_bind( &tex_wood, 1 );
1066 glUniform1i( SHADER_UNIFORM( shader_tile_main, "uTexWood" ), 1 );
1067
1068 glUniform1f( SHADER_UNIFORM( shader_tile_main, "uForeground" ), 1.0f );
1069 render_tiles( NULL, NULL, colour_default, colour_selected );
1070
1071 // Draw splitters
1072
1073 for( int y = 0; y < world.h; y ++ )
1074 {
1075 for( int x = 0; x < world.w; x ++ )
1076 {
1077 struct cell *cell = pcell((v2i){x,y});
1078
1079 if( cell->config == k_cell_type_split )
1080 {
1081 float rotation = cell->state & FLAG_FLIP_FLOP? vg_rad( -45.0f ): vg_rad( 45.0f );
1082
1083 if( cell->state & FLAG_FLIP_ROTATING )
1084 {
1085 if( (world.frame_lerp > curve_7_linear_section) )
1086 {
1087 float const rotation_speed = 0.4f;
1088 if( (world.frame_lerp < 1.0f-rotation_speed) )
1089 {
1090 float t = world.frame_lerp - curve_7_linear_section;
1091 t *= -2.0f * (1.0f/(1.0f-(curve_7_linear_section+rotation_speed)));
1092 t += 1.0f;
1093
1094 rotation *= t;
1095 }
1096 else
1097 rotation *= -1.0f;
1098 }
1099 }
1100
1101 m2x2_create_rotation( subtransform, rotation );
1102
1103 glUniformMatrix2fv( SHADER_UNIFORM( shader_tile_main, "uSubTransform" ), 1, GL_FALSE, (float *)subtransform );
1104 glUniform4f( SHADER_UNIFORM( shader_tile_main, "uOffset" ), (float)x, (float)y + 0.125f, 0.0f, 0.0f );
1105 draw_mesh( 0, 2 );
1106 }
1107 }
1108 }
1109
1110 // Edit overlay
1111 if( world.selected != -1 && !(world.data[ world.selected ].state & FLAG_CANAL) )
1112 {
1113 v2i new_begin = { world.tile_x - 2, world.tile_y - 2 };
1114 v2i new_end = { world.tile_x + 2, world.tile_y + 2 };
1115
1116 world.data[ world.selected ].state ^= FLAG_CANAL;
1117 map_reclassify( new_begin, new_end );
1118
1119 m2x2_identity( subtransform );
1120 glUniform1f( SHADER_UNIFORM( shader_tile_main, "uGhost" ), 1.0f );
1121 glUniformMatrix2fv( SHADER_UNIFORM( shader_tile_main, "uSubTransform" ), 1, GL_FALSE, (float *)subtransform );
1122 glUniform2fv( SHADER_UNIFORM( shader_tile_main, "uMousePos" ), 1, world.tile_pos );
1123
1124 render_tiles( new_begin, new_end, colour_default, colour_default );
1125
1126 world.data[ world.selected ].state ^= FLAG_CANAL;
1127 map_reclassify( new_begin, new_end );
1128 }
1129
1130 //glDisable(GL_BLEND);
1131
1132 glDisable(GL_BLEND);
1133
1134 SHADER_USE( shader_tile_colour );
1135 glUniformMatrix3fv( SHADER_UNIFORM( shader_tile_colour, "uPv" ), 1, GL_FALSE, (float *)vg_pv );
1136 use_mesh( &world.circle );
1137
1138 // Draw i/o arrays
1139 for( int i = 0; i < arrlen( world.io ); i ++ )
1140 {
1141 struct cell_terminal *term = &world.io[ i ];
1142 int posx = term->id % world.w;
1143 int posy = (term->id - posx)/world.w;
1144 int is_input = world.data[ term->id ].state & FLAG_INPUT;
1145
1146 int const filled_start = 0;
1147 int const filled_count = 32;
1148 int const empty_start = 32;
1149 int const empty_count = 32*2;
1150
1151 v4f dot_colour = { 0.0f, 0.0f, 0.0f, 1.0f };
1152
1153 for( int j = 0; j < arrlen( term->conditions ); j ++ )
1154 {
1155 float y_offset = is_input? 0.8f: 0.2f;
1156 glUniform3f( SHADER_UNIFORM( shader_tile_colour, "uOffset" ), (float)posx + 0.2f + 0.2f * (float)j, (float)posy + y_offset, 0.1f );
1157
1158 if( is_input )
1159 {
1160 colour_code_v3( term->conditions[j], dot_colour );
1161 glUniform4fv( SHADER_UNIFORM( shader_tile_colour, "uColour" ), 1, dot_colour );
1162
1163 // Draw filled if tick not passed, draw empty if empty
1164 if( world.sim_frame > j )
1165 draw_mesh( empty_start, empty_count );
1166 else
1167 draw_mesh( filled_start, filled_count );
1168 }
1169 else
1170 {
1171 if( term->recv_count > j )
1172 {
1173 colour_code_v3( term->recv[j], dot_colour );
1174 v3_muls( dot_colour, 0.8f, dot_colour );
1175 glUniform4fv( SHADER_UNIFORM( shader_tile_colour, "uColour" ), 1, dot_colour );
1176
1177 draw_mesh( filled_start, filled_count );
1178 }
1179
1180 colour_code_v3( term->conditions[j], dot_colour );
1181 glUniform4fv( SHADER_UNIFORM( shader_tile_colour, "uColour" ), 1, dot_colour );
1182
1183 draw_mesh( empty_start, empty_count );
1184 }
1185 }
1186 }
1187
1188 if( world.simulating )
1189 {
1190 glUniform4f( SHADER_UNIFORM( shader_tile_colour, "uColour" ), 0.0f, 0.0f, 0.0f, 1.0f );
1191 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 );
1192 draw_mesh( 0, 32 );
1193 }
1194 }
1195
1196 void vg_ui(void)
1197 {
1198 //ui_test();
1199 sfx_internal_debug_overlay();
1200 }