Merge branch 'master' of harrygodden.com:/home/fishladder
[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( colour_shader,
7
8 // VERTEX
9 "layout (location=0) in vec2 a_co;"
10 "uniform mat4 uPv;"
11 "uniform mat4 uMdl;"
12 ""
13 "void main()"
14 "{"
15 " vec4 vert_pos = uPv * uMdl * vec4( a_co.x, 0.0, a_co.y, 1.0 );"
16 " gl_Position = vert_pos;"
17 "}",
18
19 // FRAGMENT
20 "out vec4 FragColor;"
21 "uniform vec4 uColour;"
22 ""
23 "void main()"
24 "{"
25 " FragColor = uColour;"
26 "}"
27 ,
28 UNIFORMS({ "uPv", "uMdl", "uColour" })
29 )
30
31 /*
32 SHADER_DEFINE( tilemap_shader,
33
34 // VERTEX
35 "layout (location=0) in vec2 a_co;"
36 "uniform mat4 uPv;"
37 "uniform vec4 uTextureInfo;" // Cell dx,dy 1.0/mx, 1.0/my
38 "uniform "
39 ""
40 "uniform vec2 uPosition;"
41 "uniform int uCellIndex;"
42 ""
43 "out vec2 s_uv;"
44 ""
45 "void main()"
46 "{"
47 " gl_Position = uPv * vec4( uPosition.x + a_co.x, 0.0, uPosition.y + a_co.y, 0.0 );"
48 " s_uv = vec2( mod( uCellIndex,"
49 "}"
50 */
51
52 mat4 m_projection;
53 mat4 m_view;
54 mat4 m_mdl;
55
56 int main( int argc, char *argv[] )
57 {
58 vg_init( argc, argv, "FishLadder" );
59 }
60
61 #define CELL_FLAG_INPUT 0x1
62 #define CELL_FLAG_OUTPUT 0x2
63 #define CELL_FLAG_IO (CELL_FLAG_INPUT|CELL_FLAG_OUTPUT)
64 #define CELL_FLAG_WALL 0x4
65 #define CELL_FLAG_HOVER 0x8
66 #define CELL_FLAG_ITER 0x10
67 #define CELL_FLAG_CANAL 0x20
68 #define CELL_FLAG_SPLIT 0x40 /* Does this cell split and have an incoming vertical connection? */
69 #define CELL_FLAG_WALKABLE (CELL_FLAG_IO|CELL_FLAG_CANAL)
70 #define CELL_FLAG_VISITED 0x80
71 #define CELL_FLAG_UPLVL 0x100
72 #define CELL_FLAG_MERGE 0x200
73
74 static struct
75 {
76 u32 x,y;
77
78 struct cell
79 {
80 u32 flags;
81 u32 model_id;
82
83 char *conditions;
84
85 int level;
86 int state;
87 }
88 * cells;
89
90 struct fish
91 {
92 int alive;
93 int co[2];
94 int dir[2];
95 char data;
96 }
97 fishes[ 20 ];
98 int num_fishes;
99
100 vec3 origin;
101 struct cell *selected;
102 int select_valid;
103 int playing;
104 u32 frame;
105
106 u32 *io;
107
108 struct vstack
109 {
110 struct vframe
111 {
112 int x, y;
113 int i;
114 }
115 frames[ 64 ];
116
117 int level;
118 u32 flags;
119 }
120 stack;
121
122 GLuint tile_texture;
123 GLuint flow_texture;
124 }
125 map;
126
127 static void map_free(void)
128 {
129 for( int i = 0; i < arrlen( map.io ); i ++ )
130 {
131 arrfree( map.cells[ map.io[i] ].conditions );
132 }
133
134 arrfree( map.cells );
135 arrfree( map.io );
136 map.x = 0;
137 map.y = 0;
138 map.cells = NULL;
139 map.io = NULL;
140 }
141
142 static struct cell *map_tile_at( int pos[2] )
143 {
144 if( pos[0] >= 0 && pos[0] < map.x && pos[1] >= 0 && pos[1] < map.y )
145 return map.cells + pos[1]*map.x + pos[0];
146 return NULL;
147 }
148
149 static struct cell *map_tile_at_cond( int pos[2], u32 flags )
150 {
151 struct cell *cell = map_tile_at( pos );
152 if( cell && (cell->flags & flags) )
153 return cell;
154
155 return NULL;
156 }
157
158 static void map_tile_coords_from_index( int i, int coords[2] )
159 {
160 coords[0] = i % map.x;
161 coords[1] = (i - coords[0])/map.x;
162 }
163
164 static void map_stack_refresh(void)
165 {
166 for( int i = 0; i < map.x*map.y; i ++ )
167 map.cells[i].flags &= ~CELL_FLAG_VISITED;
168 }
169
170 static void map_stack_init( int coords[2] )
171 {
172 map.stack.level = 0;
173 map.stack.frames[0].i = 0;
174 map.stack.frames[0].x = coords[0];
175 map.stack.frames[0].y = coords[1];
176 }
177
178 static struct cell *map_stack_next(void)
179 {
180 struct cell *tile = NULL;
181
182 while( !tile )
183 {
184 struct vframe *frame = &map.stack.frames[ map.stack.level ];
185
186 int output_dirs[][2] = { {0,-1}, {-1,0}, {1,0} };
187
188 if( frame->i < 3 )
189 {
190 int *dir = output_dirs[ frame->i ];
191 tile = map_tile_at( (int[2]){frame->x+dir[0], frame->y+dir[1]} );
192 frame->i ++;
193
194 if( tile && !(tile->flags & CELL_FLAG_VISITED) )
195 {
196 map.stack.level ++;
197 frame[1].i = 0;
198 frame[1].x = frame[0].x+dir[0];
199 frame[1].y = frame[0].y+dir[1];
200 }
201 else
202 tile = NULL;
203 }
204 else
205 {
206 map.stack.level --;
207 tile = NULL;
208
209 if( map.stack.level < 0 )
210 return NULL;
211 }
212 }
213
214 return tile;
215 }
216
217 static int map_load( const char *str )
218 {
219 map_free();
220
221 char *c = str;
222
223 // Scan for width
224 for(;; map.x ++)
225 {
226 if( str[map.x] == ';' )
227 break;
228 else if( !str[map.x] )
229 {
230 vg_error( "Unexpected EOF when parsing level!\n" );
231 return 0;
232 }
233 }
234
235 struct cell *row = arraddnptr( map.cells, map.x );
236 int cx = 0;
237 int reg_start = 0, reg_end = 0;
238
239 for(;;)
240 {
241 if( !*c )
242 break;
243
244 if( *c == ';' )
245 {
246 c ++;
247
248 // Parse attribs
249 if( *c != '\n' )
250 {
251 while( *c )
252 {
253 if( reg_start < reg_end )
254 {
255 if( *c >= 'a' && *c <= 'z' )
256 {
257 arrpush( map.cells[ map.io[ reg_start ] ].conditions, *c );
258 }
259 else
260 {
261 if( *c == ',' || *c == '\n' )
262 {
263 reg_start ++;
264
265 if( *c == '\n' )
266 break;
267 }
268 else
269 {
270 vg_error( "Unkown attrib '%c' (row: %u)\n", *c, map.y );
271 return 0;
272 }
273 }
274 }
275 else
276 {
277 vg_error( "Over-assigned values (row: %u)\n", map.y );
278 return 0;
279 }
280
281 c ++;
282 }
283 }
284
285 if( reg_start != reg_end )
286 {
287 vg_error( "Not enough values assigned (row: %u, %u of %u)\n", map.y, reg_start, reg_end );
288 return 0;
289 }
290
291 if( cx != map.x )
292 {
293 vg_error( "Map row underflow (row: %u, %u<%u)\n", map.y, cx, map.x );
294 return 0;
295 }
296
297 row = arraddnptr( map.cells, map.x );
298 cx = 0;
299 map.y ++;
300 reg_end = reg_start = arrlen( map.io );
301 }
302 else
303 {
304 if( cx == map.x )
305 {
306 vg_error( "Map row overflow (row: %u, %u>%u)\n", map.y, cx, map.x );
307 return 0;
308 }
309
310 row[ cx ].conditions = NULL;
311
312 // Parse the various cell types
313 if( *c == '+' || *c == '-' )
314 {
315 arrpush( map.io, cx + map.y*map.x );
316 row[ cx ++ ].flags = *c == '+'? CELL_FLAG_INPUT: CELL_FLAG_OUTPUT;
317 reg_end ++;
318 }
319 else if( *c == '#' )
320 {
321 row[ cx ++ ].flags = CELL_FLAG_WALL;
322 }
323 else
324 {
325 row[ cx ++ ].flags = 0x00;
326 }
327 }
328
329 c ++;
330 }
331
332 // Origin top left corner
333 map.origin[0] = -((float)map.x) * 0.5f;
334 map.origin[2] = -((float)map.y) * 0.5f;
335
336 vg_success( "Map loaded! (%u:%u)\n", map.x, map.y );
337 return 1;
338 }
339
340 static int map_tile_availible( int co[2] )
341 {
342 // Extract 5x5 grid surrounding tile
343 u32 blob = 0x1000;
344 for( int y = vg_max( co[1]-2, 0 ); y < vg_min( map.y, co[1]+3 ); y ++ )
345 for( int x = vg_max( co[0]-2, 0 ); x < vg_min( map.x, co[0]+3 ); x ++ )
346 {
347 struct cell *cell = map_tile_at( (int[2]){ x, y } );
348
349 if( cell && (cell->flags & CELL_FLAG_WALKABLE) )
350 blob |= 0x1 << ((y-(co[1]-2))*5 + x-(co[0]-2));
351 }
352
353 // Run filter over center 3x3 grid to check for invalid configurations
354 int kernel[] = { 0, 1, 2, 5, 6, 7, 10, 11, 12 };
355 for( int i = 0; i < vg_list_size(kernel); i ++ )
356 {
357 if( blob & (0x1 << (6+kernel[i])) )
358 {
359 // (reference window: 0x1CE7) Illegal moves
360 // 0100011100010 ;
361 // 0000001100011 ;
362 // 0000011000110 ;
363 // 0110001100000 ;
364 // 1100011000000 ;
365 // 0100001100010 ;
366 // 0100011000010 ;
367
368 u32 invalid[] = { 0x8E2, 0x63, 0xC6, 0xC60, 0x18C0, 0x862, 0x8C2 };
369 u32 window = blob >> kernel[i];
370
371 for( int j = 0; j < vg_list_size(invalid); j ++ )
372 if((window & invalid[j]) == invalid[j])
373 return 0;
374 }
375 }
376
377 return 1;
378 }
379
380 void vg_update(void)
381 {
382 // Update camera
383 float ratio = (float)vg_window_y / (float)vg_window_x;
384 float const size = 7.5f;
385 glm_ortho( -size, size, -size*ratio, size*ratio, 0.1f, 100.f, m_projection );
386
387 glm_mat4_identity( m_view );
388 glm_translate_z( m_view, -10.f );
389 glm_rotate_x( m_view, 1.0f, m_view );
390
391 glm_mat4_mul( m_projection, m_view, vg_pv );
392
393 // Compute map update
394 for( int y = 0; y < map.y; y ++ )
395 {
396 for( int x = 0; x < map.x; x ++ )
397 {
398 struct cell *tile, *upper, *lower, *l, *r;
399 tile = map_tile_at( (int [2]){ x, y } );
400 tile->flags &= ~(CELL_FLAG_SPLIT|CELL_FLAG_MERGE|CELL_FLAG_UPLVL);
401
402 if( tile->flags & CELL_FLAG_WALKABLE )
403 {
404 r = map_tile_at_cond( (int[2]){ x+1, y }, CELL_FLAG_WALKABLE );
405 l = map_tile_at_cond( (int[2]){ x-1, y }, CELL_FLAG_WALKABLE );
406
407 if( r && l )
408 {
409 upper = map_tile_at_cond( (int[2]){ x, y-1 }, CELL_FLAG_WALKABLE );
410 lower = map_tile_at_cond( (int[2]){ x, y+1 }, CELL_FLAG_WALKABLE );
411
412 if( upper )
413 {
414 tile->flags |= CELL_FLAG_MERGE | CELL_FLAG_UPLVL;
415 }
416
417 if( lower )
418 {
419 tile->flags |= CELL_FLAG_SPLIT;
420 l->flags |= CELL_FLAG_UPLVL;
421 r->flags |= CELL_FLAG_UPLVL;
422 }
423 }
424 }
425 }
426 }
427
428 // Compute classification
429 /*
430 map_stack_refresh();
431
432 for( int i = 0; i < arrlen( map.io ); i ++ )
433 {
434 struct *cell cell = &map.cells[ map.io ];
435 int coords[2];
436
437 if( cell->flags & CELL_FLAG_INPUT )
438 {
439 map_tile_coords_from_index( map.io, coords );
440 map_stack_init( coords );
441
442 do
443 {
444 if( cell->flags & CELL_FLAG_CONNECTOR )
445 {
446
447 }
448 }
449 while( (cell = map_stack_next()) );
450 }
451 }*/
452
453 // Get mouse ray
454 vec3 ray_origin;
455 vec3 ray_dir;
456
457 mat4 pv_inverse;
458 vec4 vp = { 0.f, 0.f, vg_window_x, vg_window_y };
459 glm_mat4_inv( vg_pv, pv_inverse );
460 glm_unprojecti( (vec3){ vg_mouse_x, vg_window_y-vg_mouse_y, -1.f }, pv_inverse, vp, ray_dir );
461 glm_unprojecti( (vec3){ vg_mouse_x, vg_window_y-vg_mouse_y, 0.f }, pv_inverse, vp, ray_origin );
462 glm_vec3_sub( ray_dir, ray_origin, ray_dir );
463
464 // Get floor tile intersection
465 float ray_t = -ray_origin[1] / ray_dir[1];
466
467 vec3 tile_pos;
468 glm_vec3_copy( ray_origin, tile_pos );
469 glm_vec3_muladds( ray_dir, ray_t, tile_pos );
470 glm_vec3_sub( tile_pos, map.origin, tile_pos );
471
472 int tile_x = floorf( tile_pos[0] );
473 int tile_y = floorf( tile_pos[2] );
474
475 map.selected = map_tile_at( (int [2]){tile_x, tile_y} );
476
477 if( map.playing )
478 {
479 static int fish_counter = 0;
480 fish_counter ++;
481
482 if( fish_counter > 20 )
483 {
484 fish_counter = 0;
485
486 // Advance characters
487 for( int i = 0; i < map.num_fishes; i ++ )
488 {
489 struct fish *fish = map.fishes + i;
490
491 if( !fish->alive )
492 continue;
493
494 struct cell *tile, *next;
495 tile = map_tile_at( fish->co );
496
497 if( tile->flags & CELL_FLAG_OUTPUT )
498 {
499 vg_info( "Fish got zucced (%d)\n", i );
500 fish->alive = 0;
501 continue;
502 }
503
504 int die = 0;
505 if( tile->flags & CELL_FLAG_SPLIT )
506 {
507 die = 1;
508 int new_dir[][2] = { {0,-1},{1,0},{-1,0} };
509 int *test_dir;
510
511 for( int j = 0; j < 3; j ++ )
512 {
513 test_dir = new_dir[ tile->state ];
514 tile->state = (tile->state+1)%3;
515
516 next = map_tile_at( (int[2]){ fish->co[0]+test_dir[0], fish->co[1]+test_dir[1] } );
517 if( next && (next->flags & (CELL_FLAG_WALKABLE)) )
518 {
519 fish->dir[0] = test_dir[0];
520 fish->dir[1] = test_dir[1];
521 die = 0;
522 break;
523 }
524 }
525 }
526
527 next = map_tile_at( (int[2]){ fish->co[0]+fish->dir[0], fish->co[1]+fish->dir[1] } );
528 if( !next || (next && !(next->flags & CELL_FLAG_WALKABLE)) )
529 {
530 // Try UP
531 die = 1;
532 }
533
534 if( die )
535 {
536 vg_info( "Fish died! (%d)\n", i );
537 fish->alive = 0;
538 continue;
539 }
540
541
542 fish->co[0] += fish->dir[0];
543 fish->co[1] += fish->dir[1];
544 }
545
546 // Try spawn fish
547 for( int i = 0; i < arrlen( map.io ); i ++ )
548 {
549 struct cell *input = &map.cells[ map.io[i] ];
550
551 if( input->flags & CELL_FLAG_INPUT )
552 {
553 if( input->state < arrlen( input->conditions ) )
554 {
555 struct fish *fish = &map.fishes[ map.num_fishes ];
556 map_tile_coords_from_index( map.io[i], fish->co );
557
558 int output_dirs[][2] = { {0,-1}, {-1,0}, {1,0} };
559 int can_spawn = 0;
560
561 for( int i = 0; i < vg_list_size( output_dirs ); i ++ )
562 {
563 int *dir = output_dirs[i];
564 struct cell *next = map_tile_at( (int[2]){ fish->co[0]+dir[0], fish->co[1]+dir[1] } );
565 if( next && next->flags & CELL_FLAG_CANAL )
566 {
567 fish->dir[0] = dir[0];
568 fish->dir[1] = dir[1];
569 can_spawn = 1;
570 }
571 }
572
573 if( can_spawn )
574 {
575 fish->alive = 1;
576 input->state ++;
577 map.num_fishes ++;
578 }
579 }
580 }
581 }
582
583 vg_info( "There are now %u active fish\n", map.num_fishes );
584 }
585
586 if( vg_get_button_down( "go" ) )
587 {
588 map.playing = 0;
589 map.num_fishes = 0;
590
591 vg_info( "Ending!\n" );
592 }
593 }
594 else
595 {
596 if( vg_get_button_down( "go" ) )
597 {
598 map.playing = 1;
599
600 // Reset everything
601 for( int i = 0; i < map.x*map.y; i ++ )
602 map.cells[ i ].state = 0;
603
604 vg_info( "Starting!\n" );
605 }
606
607 if( map.selected )
608 {
609 map.select_valid = map_tile_availible( (int[2]){ tile_x, tile_y } );
610
611 if( map.select_valid )
612 {
613 if( vg_get_button_down("primary") )
614 {
615 if( map.selected->flags & CELL_FLAG_CANAL )
616 {
617 map.selected->flags &= ~(CELL_FLAG_CANAL);
618 }
619 else
620 {
621 map.selected->flags |= CELL_FLAG_CANAL;
622 }
623 }
624 }
625 }
626 }
627 }
628
629 GLuint tile_vao;
630 GLuint tile_vbo;
631
632 void vg_render(void)
633 {
634 glViewport( 0,0, vg_window_x, vg_window_y );
635
636 glEnable( GL_DEPTH_TEST );
637 glClearColor( 0.94f, 0.94f, 0.94f, 1.0f );
638 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
639
640 glBindVertexArray( tile_vao );
641
642 SHADER_USE( colour_shader );
643 glUniformMatrix4fv( SHADER_UNIFORM( colour_shader, "uPv" ), 1, GL_FALSE, (float *)vg_pv );
644
645 for( int y = 0; y < map.y; y ++ )
646 {
647 for( int x = 0; x < map.x; x ++ )
648 {
649 glm_mat4_identity( m_mdl );
650 glm_translate( m_mdl,
651 (vec3){
652 map.origin[0] + (float)x + 0.5f,
653 0.f,
654 map.origin[2] + (float)y + 0.5f
655 }
656 );
657 glUniformMatrix4fv( SHADER_UNIFORM( colour_shader, "uMdl" ), 1, GL_FALSE, (float *)m_mdl );
658
659 struct cell *cell = &map.cells[ y*map.x+x ];
660
661 vec4 colour = { 0.7f, 0.7f, 0.7f, 1.f };
662
663 if( cell->flags & CELL_FLAG_INPUT ) glm_vec3_copy( (vec3){ 0.9f,0.5f,0.5f }, colour );
664 else if( cell->flags & CELL_FLAG_OUTPUT ) glm_vec3_copy( (vec3){ 0.5f,0.9f,0.5f }, colour );
665 else if( cell->flags & CELL_FLAG_WALL ) glm_vec3_copy( (vec3){ 0.1f,0.1f,0.1f }, colour );
666 else if( cell->flags & CELL_FLAG_CANAL ) glm_vec3_copy( (vec3){ 0.5f,0.5f,0.8f }, colour );
667
668 if( cell->flags & CELL_FLAG_SPLIT )
669 glm_vec3_copy( (vec3){ 0.6f, 0.f, 0.9f }, colour );
670 else if( cell->flags & CELL_FLAG_MERGE )
671 glm_vec3_copy( (vec3){ 0.f, 0.6f, 0.8f }, colour );
672
673 if( map.selected == cell )
674 {
675 if( !map.select_valid )
676 glm_vec3_copy( (vec3){ 1.f, 0.f, 0.f }, colour );
677
678 float flash = sinf( vg_time*2.5f ) * 0.25f + 0.75f;
679 glm_vec3_scale( colour, flash, colour );
680 }
681
682 glUniform4fv( SHADER_UNIFORM( colour_shader, "uColour" ), 1, colour );
683 glDrawArrays( GL_TRIANGLES, 0, 6 );
684 }
685 }
686
687 glUniform4f( SHADER_UNIFORM( colour_shader, "uColour" ), 1.f, 0.f, 1.f, 1.f );
688
689 for( int i = 0; i < map.num_fishes; i ++ )
690 {
691 struct fish *fish = map.fishes + i;
692
693 if( fish->alive )
694 {
695 glm_mat4_identity( m_mdl );
696 glm_translate( m_mdl,
697 (vec3){
698 map.origin[0] + (float)fish->co[0] + 0.5f,
699 0.1f,
700 map.origin[2] + (float)fish->co[1] + 0.5f
701 }
702 );
703 glm_scale_uni( m_mdl, 0.2f );
704 glUniformMatrix4fv( SHADER_UNIFORM( colour_shader, "uMdl" ), 1, GL_FALSE, (float *)m_mdl );
705 glDrawArrays( GL_TRIANGLES, 0, 6 );
706 }
707 }
708 }
709
710 void vg_start(void)
711 {
712 SHADER_INIT( colour_shader );
713
714 glGenVertexArrays( 1, &tile_vao );
715 glGenBuffers( 1, &tile_vbo );
716
717 float quad_mesh[] =
718 {
719 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f,
720 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, 0.0f
721 };
722
723 glBindVertexArray( tile_vao );
724 glBindBuffer( GL_ARRAY_BUFFER, tile_vbo );
725 glBufferData
726 (
727 GL_ARRAY_BUFFER,
728 sizeof( quad_mesh ),
729 quad_mesh,
730 GL_STATIC_DRAW
731 );
732
733 glVertexAttribPointer( 0, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(float), (void*)0 );
734 glEnableVertexAttribArray( 0 );
735
736 VG_CHECK_GL();
737
738 map.tile_texture = vg_tex2d_rgba( "textures/rivertiles_flowm.tga" );
739 map.flow_texture = vg_tex2d_rgba( "textures/rivertiles_ripple.tga" );
740
741 map_load
742 (
743 "#####-#####;aa\n"
744 "# #;\n"
745 "# #;\n"
746 "# -;bb\n"
747 "# #;\n"
748 "# #;\n"
749 "#####+#####;abab\n"
750 );
751 }
752
753 void vg_free(void)
754 {
755 map_free();
756
757 glDeleteTextures( 1, &map.tile_texture );
758 glDeleteTextures( 1, &map.flow_texture );
759 }
760
761 void vg_ui(void)
762 {
763
764 }