added visual indicators of.. stuff
[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 m3x3f m_projection;
31 m3x3f m_view;
32 m3x3f m_mdl;
33
34 #define FLAG_INPUT 0x1
35 #define FLAG_OUTPUT 0x2
36 #define FLAG_CANAL 0x4
37 #define FLAG_WALL 0x8
38 #define FLAG_DROP_L 0x10
39 #define FLAG_SPLIT 0x20
40 #define FLAG_MERGER 0x40
41 #define FLAG_DROP_R 0x80
42 #define FLAG_FLIP_FLOP 0x100
43
44 v3f colour_sets[] =
45 { { 0.9f, 0.2f, 0.01f },
46 { 0.2f, 0.9f, 0.14f },
47 { 0.1f, 0.3f, 0.85f } };
48
49 static void colour_code_v3( char cc, v3f target )
50 {
51 if( cc >= 'a' && cc <= 'z' )
52 {
53 int id = cc - 'a';
54
55 if( id < vg_list_size( colour_sets ) )
56 {
57 v3_copy( colour_sets[ id ], target );
58 return;
59 }
60 }
61
62 v3_copy( (v3f){0.0f,0.0f,0.0f}, target );
63 }
64
65 struct world
66 {
67 struct cell
68 {
69 u32 state;
70 u8 water[2];
71 u8 config;
72 u8 water_dir;
73 }
74 *data;
75
76 u32 frame;
77
78 u32 sim_frame;
79 float sim_start;
80 int simulating;
81
82 struct cell_terminal
83 {
84 // TODO: Split into input/output structures
85 char *conditions;
86 char recv[12];
87 int recv_count;
88 int id;
89 }
90 *io;
91
92 u32 w, h;
93
94 GLuint tile_vao;
95 GLuint tile_vbo;
96 GLuint circle_vao;
97 GLuint circle_vbo;
98
99 int selected;
100
101 struct fish
102 {
103 v2i pos;
104 v2i dir;
105 int alive;
106 char payload;
107 }
108 fishes[16];
109
110 int num_fishes;
111 } world = {};
112
113 static void map_free(void)
114 {
115 for( int i = 0; i < arrlen( world.io ); i ++ )
116 arrfree( world.io[ i ].conditions );
117
118 arrfree( world.data );
119 arrfree( world.io );
120
121 world.w = 0;
122 world.h = 0;
123 world.data = NULL;
124 world.io = NULL;
125 }
126
127 static int map_load( const char *str )
128 {
129 map_free();
130
131 char const *c = str;
132
133 // Scan for width
134 for(;; world.w ++)
135 {
136 if( str[world.w] == ';' )
137 break;
138 else if( !str[world.w] )
139 {
140 vg_error( "Unexpected EOF when parsing level\n" );
141 return 0;
142 }
143 }
144
145 struct cell *row = arraddnptr( world.data, world.w );
146 int cx = 0;
147 int reg_start = 0, reg_end = 0;
148
149 for(;;)
150 {
151 if( !*c )
152 break;
153
154 if( *c == ';' )
155 {
156 c ++;
157
158 // Parse attribs
159 if( *c != '\n' )
160 {
161 while( *c )
162 {
163 if( reg_start < reg_end )
164 {
165 if( *c >= 'a' && *c <= 'z' )
166 {
167 arrpush( world.io[ reg_start ].conditions, *c );
168 }
169 else
170 {
171 if( *c == ',' || *c == '\n' )
172 {
173 reg_start ++;
174
175 if( *c == '\n' )
176 break;
177 }
178 else
179 {
180 vg_error( "Unkown attribute '%c' (row: %u)\n", *c, world.h );
181 return 0;
182 }
183 }
184 }
185 else
186 {
187 vg_error( "Too many values to assign (row: %u)\n", world.h );
188 return 0;
189 }
190
191 c ++;
192 }
193 }
194
195 if( reg_start != reg_end )
196 {
197 vg_error( "Not enough values assigned (row: %u, %u of %u)\n", world.h, reg_start, reg_end );
198 return 0;
199 }
200
201 if( cx != world.w )
202 {
203 vg_error( "Not enough cells to match previous row definition (row: %u, %u<%u)\n", world.h, cx, world.w );
204 return 0;
205 }
206
207 row = arraddnptr( world.data, world.w );
208 cx = 0;
209 world.h ++;
210 reg_end = reg_start = arrlen( world.io );
211 }
212 else
213 {
214 if( cx == world.w )
215 {
216 vg_error( "Too many cells to match previous row definition (row: %u, %u>%u)\n", world.h, cx, world.w );
217 return 0;
218 }
219
220 // Tile initialization
221 // row[ cx ] .. etc
222 row[ cx ].water[0] = 0;
223 row[ cx ].water[1] = 0;
224
225 if( *c == '+' || *c == '-' )
226 {
227 struct cell_terminal term = { .id = cx + world.h*world.w };
228 arrpush( world.io, term );
229 row[ cx ++ ].state = *c == '+'? FLAG_INPUT: FLAG_OUTPUT;
230 reg_end ++;
231 }
232 else if( *c == '#' )
233 {
234 row[ cx ++ ].state = FLAG_WALL;
235 }
236 else
237 {
238 row[ cx ++ ].state = 0x00;
239 }
240 }
241
242 c ++;
243 }
244
245 vg_success( "Map loaded! (%u:%u)\n", world.w, world.h );
246 return 1;
247 }
248
249 static struct cell *pcell( v2i pos )
250 {
251 return &world.data[ pos[1]*world.w + pos[0] ];
252 }
253
254 int main( int argc, char *argv[] )
255 {
256 vg_init( argc, argv, "FishLadder" );
257 }
258
259 void vg_register(void)
260 {
261 SHADER_INIT( shader_tile_colour );
262 }
263
264 void vg_start(void)
265 {
266 // Quad mesh
267 {
268 glGenVertexArrays( 1, &world.tile_vao );
269 glGenBuffers( 1, &world.tile_vbo );
270
271 float quad_mesh[] =
272 {
273 0.01f, 0.01f, 0.01f, 0.99f, 0.99f, 0.99f,
274 0.01f, 0.01f, 0.99f, 0.99f, 0.99f, 0.01f,
275
276 0.48f, 0.48f, 0.5f, 0.52f, 0.52f, 0.52f, // Static dot
277 0.375f, 0.25f, 0.5f, 0.75f, 0.625f, 0.25f, // Downwards pointing arrow
278 0.25f, 0.625f, 0.75f, 0.5f, 0.25f, 0.375f, // Left
279 0.625f, 0.75f, 0.5f, 0.25f, 0.375f, 0.75f, // up
280 0.75f, 0.375f, 0.25f, 0.5f, 0.75f, 0.625f
281 };
282
283 glBindVertexArray( world.tile_vao );
284 glBindBuffer( GL_ARRAY_BUFFER, world.tile_vbo );
285 glBufferData
286 (
287 GL_ARRAY_BUFFER,
288 sizeof( quad_mesh ),
289 quad_mesh,
290 GL_STATIC_DRAW
291 );
292
293 glVertexAttribPointer( 0, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(float), (void*)0 );
294 glEnableVertexAttribArray( 0 );
295
296 VG_CHECK_GL();
297 }
298
299 // Circle mesh
300 {
301 glGenVertexArrays( 1, &world.circle_vao );
302 glGenBuffers( 1, &world.circle_vbo );
303
304 float circle_mesh[32*6*3];
305 int res = vg_list_size( circle_mesh ) / (6*3);
306
307 for( int i = 0; i < res; i ++ )
308 {
309 v2f v0 = { sinf( ((float)i/(float)res)*VG_TAUf ), cosf( ((float)i/(float)res)*VG_TAUf ) };
310 v2f v1 = { sinf( ((float)(i+1)/(float)res)*VG_TAUf ), cosf( ((float)(i+1)/(float)res)*VG_TAUf ) };
311
312 circle_mesh[ i*6+0 ] = 0.0f;
313 circle_mesh[ i*6+1 ] = 0.0f;
314
315 v2_copy( v0, circle_mesh + 32*6 + i*12 );
316 v2_muls( v0, 0.8f, circle_mesh + 32*6 + i*12+2 );
317 v2_copy( v1, circle_mesh + 32*6 + i*12+4 );
318
319 v2_copy( v1, circle_mesh + 32*6 + i*12+6 );
320 v2_muls( v1, 0.8f, circle_mesh + 32*6 + i*12+8 );
321 v2_muls( v0, 0.8f, circle_mesh + 32*6 + i*12+10 );
322
323 v2_copy( v0, circle_mesh + i*6+4 );
324 v2_copy( v1, circle_mesh + i*6+2 );
325 v2_copy( v0, circle_mesh+i*6+4 );
326 v2_copy( v1, circle_mesh+i*6+2 );
327 }
328
329 glBindVertexArray( world.circle_vao );
330 glBindBuffer( GL_ARRAY_BUFFER, world.circle_vbo );
331 glBufferData( GL_ARRAY_BUFFER, sizeof( circle_mesh ), circle_mesh, GL_STATIC_DRAW );
332
333 glVertexAttribPointer( 0, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(float), (void*)0 );
334 glEnableVertexAttribArray( 0 );
335
336 VG_CHECK_GL();
337 }
338
339 map_load
340 (
341 "#############;\n"
342 "###-#####-###;aaa,aa\n"
343 "## ##;\n"
344 "## ##;\n"
345 "## ##;\n"
346 "## ##;\n"
347 "## ##;\n"
348 "## ##;\n"
349 "###+#####+###;aa,aaa\n"
350 "#############;\n"
351 );
352 }
353
354 void vg_free(void)
355 {
356 glDeleteVertexArrays( 1, &world.tile_vao );
357 glDeleteBuffers( 1, &world.tile_vbo );
358
359 glDeleteVertexArrays( 1, &world.circle_vao );
360 glDeleteBuffers( 1, &world.circle_vbo );
361
362 map_free();
363 }
364
365 static int cell_interactive( v2i co )
366 {
367 // Bounds check
368 if( co[0] < 2 || co[0] >= world.w-2 || co[1] < 2 || co[1] >= world.h-2 )
369 return 0;
370
371 // Flags check
372 if( world.data[ world.w*co[1] + co[0] ].state & (FLAG_WALL|FLAG_INPUT|FLAG_OUTPUT) )
373 return 0;
374
375 // List of 3x3 configurations that we do not allow
376 static u32 invalid_src[][9] =
377 {
378 { 0,1,0,
379 1,1,1,
380 0,1,0
381 },
382 { 0,0,0,
383 0,1,1,
384 0,1,1
385 },
386 { 0,0,0,
387 1,1,0,
388 1,1,0
389 },
390 { 0,1,1,
391 0,1,1,
392 0,0,0
393 },
394 { 1,1,0,
395 1,1,0,
396 0,0,0
397 },
398 { 0,1,0,
399 0,1,1,
400 0,1,0
401 },
402 { 0,1,0,
403 1,1,0,
404 0,1,0
405 }
406 };
407
408 // Statically compile invalid configurations into bitmasks
409 static u32 invalid[ vg_list_size(invalid_src) ];
410
411 for( int i = 0; i < vg_list_size(invalid_src); i ++ )
412 {
413 u32 comped = 0x00;
414
415 for( int j = 0; j < 3; j ++ )
416 for( int k = 0; k < 3; k ++ )
417 comped |= invalid_src[i][ j*3+k ] << ((j*5)+k);
418
419 invalid[i] = comped;
420 }
421
422 // Extract 5x5 grid surrounding tile
423 u32 blob = 0x1000;
424 for( int y = co[1]-2; y < co[1]+3; y ++ )
425 for( int x = co[0]-2; x < co[0]+3; x ++ )
426 {
427 struct cell *cell = &world.data[ world.w*y + x ];
428
429 if( cell && (cell->state & (FLAG_CANAL|FLAG_INPUT|FLAG_OUTPUT)) )
430 blob |= 0x1 << ((y-(co[1]-2))*5 + x-(co[0]-2));
431 }
432
433 // Run filter over center 3x3 grid to check for invalid configurations
434 int kernel[] = { 0, 1, 2, 5, 6, 7, 10, 11, 12 };
435 for( int i = 0; i < vg_list_size(kernel); i ++ )
436 {
437 if( blob & (0x1 << (6+kernel[i])) )
438 {
439 u32 window = blob >> kernel[i];
440
441 for( int j = 0; j < vg_list_size(invalid); j ++ )
442 if((window & invalid[j]) == invalid[j])
443 return 0;
444 }
445 }
446
447 return 1;
448 }
449
450 void vg_update(void)
451 {
452 float ratio = (float)vg_window_y / (float)vg_window_x;
453 float const size = 9.5f;
454
455 v3f origin;
456 origin[0] = -0.5f * world.w;
457 origin[1] = -0.5f * world.h;
458 origin[2] = 0.0f;
459
460 m3x3_projection( m_projection, -size, size, size*ratio, -size*ratio );
461 m3x3_identity( m_view );
462 m3x3_translate( m_view, origin );
463 m3x3_mul( m_projection, m_view, vg_pv );
464 vg_projection_update();
465
466 // Input stuff
467
468 v2f tile_pos;
469 v2_copy( vg_mouse_ws, tile_pos );
470
471 int tile_x = floorf( tile_pos[0] );
472 int tile_y = floorf( tile_pos[1] );
473
474 // Tilemap editing
475 if( !world.simulating )
476 {
477 if( cell_interactive( (v2i){ tile_x, tile_y } ))
478 {
479 world.selected = tile_y * world.w + tile_x;
480
481 if( vg_get_button_down("primary") )
482 {
483 world.data[ world.selected ].state ^= FLAG_CANAL;
484 }
485 }
486 else
487 world.selected = -1;
488 }
489
490 // Simulation stop/start
491 if( vg_get_button_down("go") )
492 {
493 if( world.simulating )
494 {
495 world.simulating = 0;
496 world.num_fishes = 0;
497 world.sim_frame = 0;
498
499 for( int i = 0; i < arrlen( world.io ); i ++ )
500 world.io[i].recv_count = 0;
501
502 vg_info( "Stopping simulation!\n" );
503 }
504 else
505 {
506 vg_success( "Starting simulation!\n" );
507
508 world.simulating = 1;
509 world.num_fishes = 0;
510 world.sim_frame = 0;
511 world.sim_start = vg_time;
512
513 for( int i = 0; i < world.w*world.h; i ++ )
514 {
515 world.data[ i ].state &= ~FLAG_FLIP_FLOP;
516 }
517
518 for( int i = 0; i < arrlen( world.io ); i ++ )
519 world.io[i].recv_count = 0;
520 }
521 }
522
523 // Simulation stuff
524 // ========================================================
525
526 // Reclassify world. TODO: Move into own function
527 for( int y = 2; y < world.h-2; y ++ )
528 {
529 for( int x = 2; x < world.w-2; x ++ )
530 {
531 v2i dirs[] = {{1,0},{0,1},{-1,0},{0,-1}};
532
533 u8 config = 0x00;
534
535 if( world.data[y*world.w+x].state & FLAG_CANAL )
536 {
537 for( int i = 0; i < vg_list_size( dirs ); i ++ )
538 {
539 struct cell *neighbour = &world.data[(y+dirs[i][1])*world.w+x+dirs[i][0]];
540 if( neighbour->state & (FLAG_CANAL|FLAG_INPUT|FLAG_OUTPUT) )
541 config |= 0x1 << i;
542 }
543 } else config = 0xF;
544
545 world.data[ y*world.w+x ].config = config;
546 world.data[ y*world.w+x ].state &= ~(FLAG_DROP_L|FLAG_DROP_R|FLAG_SPLIT|FLAG_MERGER);
547 }
548 }
549
550 for( int y = 2; y < world.h-2; y ++ )
551 for( int x = 2; x < world.w-2; x ++ )
552 {
553 // R,D,L,- 1110 (splitter, 1 drop created)
554
555 // R,-,L,U - 1011 (merger, 2 drop created)
556
557 u8 config = world.data[y*world.w+x].config;
558
559 if( config == 0x7 ) // splitter
560 {
561 world.data[y*world.w+x ].state |= (FLAG_SPLIT | FLAG_DROP_L | FLAG_DROP_R);
562 }
563 else if( config == 0xD )
564 {
565 world.data[y*world.w+x-1].state |= FLAG_DROP_R;
566 world.data[y*world.w+x+1].state |= FLAG_DROP_L;
567 world.data[y*world.w+x].state |= FLAG_MERGER;
568 }
569 }
570
571 // Simulate world
572 static int update_tick = 0;
573 update_tick ++;
574
575 if( update_tick > 5 )
576 {
577 update_tick = 0;
578
579 u32 buffer_id = world.frame & 0x1;
580 u32 buffer_next = buffer_id ^ 0x1;
581
582 for( int y = 1; y < world.h-1; y ++ )
583 {
584 for( int x = 1; x < world.w-1; x ++ )
585 {
586 struct cell *cell = &world.data[y*world.w+x];
587
588 if( cell->state & FLAG_OUTPUT )
589 cell->water[ buffer_next ] = 16;
590 else
591 {
592 int has_source = 0;
593 cell->water[ buffer_next ] = 0;
594
595 if( cell->state & FLAG_CANAL )
596 {
597 v2i dirs[] = {{1,0},{-1,0},{0,-1}};
598
599 for( int i = 0; i < vg_list_size( dirs ); i ++ )
600 {
601 struct cell *neighbour = &world.data[(y+dirs[i][1])*world.w+x+dirs[i][0]];
602
603 // Non canals will be ignored
604 if( !(neighbour->state & (FLAG_CANAL|FLAG_OUTPUT)) )
605 continue;
606
607 // Only vertical pulls allowed on neighbour splitters
608 if( (neighbour->state & FLAG_SPLIT) && i != 2 )
609 continue;
610
611 // Only vertical pulls allowed for mergers
612 if( (cell->state & FLAG_MERGER) && i != 2 )
613 continue;
614
615 // Test for renewall cases if we have drop L/R check if i matches direction.
616 if( (((cell->state & FLAG_DROP_L)&&i==1) || ((cell->state & FLAG_DROP_R)&&i==0)) && neighbour->water[ buffer_id ] )
617 {
618 cell->water[ buffer_next ] = 16;
619 has_source = 1;
620 }
621 else
622 if( neighbour->water[ buffer_id ] > cell->water[ buffer_next ]+1 )
623 {
624 has_source = 1;
625 cell->water[ buffer_next ] = neighbour->water[ buffer_id ]-1;
626 }
627 }
628 }
629
630 if( !has_source && cell->water[ buffer_id ] )
631 cell->water[ buffer_next ] = cell->water[ buffer_id ]-1;
632 }
633 }
634 }
635
636 world.frame ^= 0x1;
637 }
638
639 // Fish ticks
640 if( world.simulating )
641 {
642 while( world.sim_frame < (int)((vg_time-world.sim_start)*2.0f) )
643 {
644 vg_info( "frame: %u\n", world.sim_frame );
645
646 for( int i = 0; i < arrlen( world.io ); i ++ )
647 {
648 struct cell_terminal *term = &world.io[ i ];
649 int posx = term->id % world.w;
650 int posy = (term->id - posx)/world.w;
651 int is_input = world.data[ term->id ].state & FLAG_INPUT;
652
653 if( is_input )
654 {
655 if( world.sim_frame < arrlen( term->conditions ) )
656 {
657 struct fish *fish = &world.fishes[world.num_fishes++];
658 fish->pos[0] = posx;
659 fish->pos[1] = posy;
660 fish->alive = 1;
661 fish->payload = term->conditions[world.sim_frame];
662
663 int can_spawn = 0;
664
665 v2i dirs[] = {{1,0},{-1,0},{0,-1}};
666 for( int j = 0; j < vg_list_size(dirs); j ++ )
667 {
668 v2i target;
669 if( world.data[ (posy+dirs[j][1])*world.w + posx+dirs[j][0] ].water[ world.frame & 0x1 ] )
670 {
671 fish->dir[0] = dirs[j][0];
672 fish->dir[1] = dirs[j][1];
673 can_spawn = 1;
674 break;
675 }
676 }
677
678 if( !can_spawn )
679 world.num_fishes--;
680 }
681 }
682 }
683
684 for( int i = 0; i < world.num_fishes; i ++ )
685 {
686 struct fish *fish = &world.fishes[i];
687 struct cell *cell_current = pcell( fish->pos );
688
689 if( !fish->alive )
690 continue;
691
692 // Apply to output
693 if( cell_current->state & FLAG_OUTPUT )
694 {
695 for( int j = 0; j < arrlen( world.io ); j ++ )
696 {
697 struct cell_terminal *term = &world.io[j];
698
699 if( term->id == fish->pos[1]*world.w + fish->pos[0] )
700 {
701 term->recv[ term->recv_count ++ ] = fish->payload;
702 break;
703 }
704 }
705
706 fish->alive = 0;
707 continue;
708 }
709
710 if( !(cell_current->water[ world.frame & 0x1 ] || cell_current->state & (FLAG_INPUT)) )
711 {
712 fish->alive = 0;
713 }
714 else
715 {
716 if( cell_current->state & FLAG_SPLIT )
717 {
718 // Flip flop L/R
719 fish->dir[0] = cell_current->state&FLAG_FLIP_FLOP?1:-1;
720 fish->dir[1] = 0;
721
722 cell_current->state ^= FLAG_FLIP_FLOP;
723 }
724 else if( cell_current->state & FLAG_MERGER )
725 {
726 // Can only move up
727 fish->dir[0] = 0;
728 fish->dir[1] = -1;
729 }
730 else
731 {
732 struct cell *cell_next = pcell( (v2i){ fish->pos[0]+fish->dir[0], fish->pos[1]+fish->dir[1] } );
733 if( !cell_next->water[ world.frame & 0x1 ] )
734 {
735 // Try other directions for valid
736 v2i dirs[] = {{1,0},{-1,0},{0,-1}};
737 vg_info( "Trying some other directions...\n" );
738
739 for( int j = 0; j < vg_list_size(dirs); j ++ )
740 {
741 if( (dirs[j][0] == -fish->dir[0]) && (dirs[j][1] == -fish->dir[1]) )
742 continue;
743
744 if( pcell( (v2i){ fish->pos[0]+dirs[j][0], fish->pos[1]+dirs[j][1] } )->water[ world.frame & 0x1 ] )
745 {
746 fish->dir[0] = dirs[j][0];
747 fish->dir[1] = dirs[j][1];
748 }
749 }
750 }
751 }
752
753 fish->pos[0] += fish->dir[0];
754 fish->pos[1] += fish->dir[1];
755 }
756 }
757
758 world.sim_frame ++;
759 }
760 }
761 }
762
763 void vg_render(void)
764 {
765 glViewport( 0,0, vg_window_x, vg_window_y );
766
767 glDisable( GL_DEPTH_TEST );
768 glClearColor( 0.01f, 0.01f, 0.01f, 1.0f );
769 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
770
771 glBindVertexArray( world.tile_vao );
772 SHADER_USE( shader_tile_colour );
773 glUniformMatrix3fv( SHADER_UNIFORM( shader_tile_colour, "uPv" ), 1, GL_FALSE, (float *)vg_pv );
774
775 for( int y = 0; y < world.h; y ++ )
776 {
777 for( int x = 0; x < world.w; x ++ )
778 {
779 glUniform3f( SHADER_UNIFORM( shader_tile_colour, "uOffset" ), (float)x, (float)y, 1.0f );
780
781 v4f colour;
782
783 struct cell *cell = &world.data[y*world.w+x];
784
785 if( cell->state & FLAG_WALL ) { v4_copy( (v4f){ 0.2f, 0.2f, 0.2f, 1.0f }, colour ); }
786 else if( cell->state & FLAG_CANAL ) { v4_copy( (v4f){ 0.6f, 0.6f, 0.6f, 1.0f }, colour ); }
787 else if( cell->state & FLAG_INPUT ) { v4_copy( (v4f){ 0.5f, 0.5f, 0.5f, 1.0f }, colour ); }
788 else if( cell->state & FLAG_OUTPUT ) { v4_copy( (v4f){ 0.2f, 0.7f, 0.3f, 1.0f }, colour ); }
789 else v4_copy( (v4f){ 0.9f, 0.9f, 0.9f, 1.0f }, colour );
790
791 if( cell->water[world.frame&0x1] )
792 v4_copy( (v4f){ 0.2f, 0.3f, 0.7f * (float)(cell->water[world.frame&0x1]) * (1.0f/16.0f), 1.0f }, colour );
793
794 if( world.selected == y*world.w + x )
795 v3_muls( colour, sinf( vg_time )*0.25f + 0.5f, colour );
796
797 //if( cell->state & (FLAG_SPLIT) )
798 // v4_copy( (v4f){ 0.75f, 0.75f, 0.02f, 1.0f }, colour );
799 //if( cell->state & (FLAG_MERGER) )
800 // v4_copy( (v4f){ 0.75f, 0.02f, 0.75f, 1.0f }, colour );
801
802 glUniform4fv( SHADER_UNIFORM( shader_tile_colour, "uColour" ), 1, colour );
803
804 glDrawArrays( GL_TRIANGLES, 0, 6 );
805
806 if( cell->state & FLAG_CANAL )
807 {
808 glUniform4f( SHADER_UNIFORM( shader_tile_colour, "uColour" ), 1.0f, 1.0f, 1.0f, 1.0f );
809 glDrawArrays( GL_TRIANGLES, 6, 3 );
810 }
811 }
812 }
813
814 glBindVertexArray( world.circle_vao );
815
816 // Draw i/o arrays
817 for( int i = 0; i < arrlen( world.io ); i ++ )
818 {
819 struct cell_terminal *term = &world.io[ i ];
820 int posx = term->id % world.w;
821 int posy = (term->id - posx)/world.w;
822 int is_input = world.data[ term->id ].state & FLAG_INPUT;
823
824 int const filled_start = 0;
825 int const filled_count = 32*3;
826 int const empty_start = 32*3;
827 int const empty_count = 32*6;
828
829 v4f dot_colour = { 0.0f, 0.0f, 0.0f, 1.0f };
830
831 for( int j = 0; j < arrlen( term->conditions ); j ++ )
832 {
833 glUniform3f( SHADER_UNIFORM( shader_tile_colour, "uOffset" ), (float)posx + 0.2f + 0.2f * (float)j, (float)posy + 0.2f, 0.1f );
834
835 if( is_input )
836 {
837 colour_code_v3( term->conditions[j], dot_colour );
838 glUniform4fv( SHADER_UNIFORM( shader_tile_colour, "uColour" ), 1, dot_colour );
839
840 // Draw filled if tick not passed, draw empty if empty
841 if( world.sim_frame > j )
842 glDrawArrays( GL_TRIANGLES, empty_start, empty_count );
843 else
844 glDrawArrays( GL_TRIANGLES, filled_start, filled_count );
845 }
846 else
847 {
848 if( term->recv_count > j )
849 {
850 colour_code_v3( term->recv[j], dot_colour );
851 v3_muls( dot_colour, 0.8f, dot_colour );
852 glUniform4fv( SHADER_UNIFORM( shader_tile_colour, "uColour" ), 1, dot_colour );
853
854 glDrawArrays( GL_TRIANGLES, filled_start, filled_count );
855 }
856
857 colour_code_v3( term->conditions[j], dot_colour );
858 glUniform4fv( SHADER_UNIFORM( shader_tile_colour, "uColour" ), 1, dot_colour );
859
860 glDrawArrays( GL_TRIANGLES, empty_start, empty_count );
861 }
862 }
863 }
864
865 // Draw 'fish'
866 if( world.simulating )
867 {
868 float scaled_time = (vg_time-world.sim_start)*2.0f;
869 float lerp = 1.0f-(scaled_time - (float)world.sim_frame);
870
871 v4f dot_colour = { 0.0f, 0.0f, 0.0f, 1.0f };
872
873 for( int i = 0; i < world.num_fishes; i ++ )
874 {
875 struct fish *fish = &world.fishes[i];
876
877 if( !fish->alive )
878 continue;
879
880 colour_code_v3( fish->payload, dot_colour );
881 glUniform4fv( SHADER_UNIFORM( shader_tile_colour, "uColour" ), 1, dot_colour );
882
883 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.25f );
884 glDrawArrays( GL_TRIANGLES, 0, 32*3 );
885 }
886 }
887 }
888
889 void vg_ui(void){}