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