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