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