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