1 // Copyright (C) 2021 Harry Godden (hgn) - All Rights Reserved
6 SHADER_DEFINE( colour_shader
,
9 "layout (location=0) in vec2 a_co;"
15 " vec4 vert_pos = uPv * uMdl * vec4( a_co.x, 0.0, a_co.y, 1.0 );"
16 " gl_Position = vert_pos;"
21 "uniform vec4 uColour;"
25 " FragColor = uColour;"
28 UNIFORMS({ "uPv", "uMdl", "uColour" })
32 SHADER_DEFINE( tilemap_shader,
35 "layout (location=0) in vec2 a_co;"
37 "uniform vec4 uTextureInfo;" // Cell dx,dy 1.0/mx, 1.0/my
40 "uniform vec2 uPosition;"
41 "uniform int uCellIndex;"
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,"
56 int main( int argc
, char *argv
[] )
58 vg_init( argc
, argv
, "FishLadder" );
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
101 struct cell
*selected
;
127 static void map_free(void)
129 for( int i
= 0; i
< arrlen( map
.io
); i
++ )
131 arrfree( map
.cells
[ map
.io
[i
] ].conditions
);
134 arrfree( map
.cells
);
142 static struct cell
*map_tile_at( int pos
[2] )
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];
149 static struct cell
*map_tile_at_cond( int pos
[2], u32 flags
)
151 struct cell
*cell
= map_tile_at( pos
);
152 if( cell
&& (cell
->flags
& flags
) )
158 static void map_tile_coords_from_index( int i
, int coords
[2] )
160 coords
[0] = i
% map
.x
;
161 coords
[1] = (i
- coords
[0])/map
.x
;
164 static void map_stack_refresh(void)
166 for( int i
= 0; i
< map
.x
*map
.y
; i
++ )
167 map
.cells
[i
].flags
&= ~CELL_FLAG_VISITED
;
170 static void map_stack_init( int coords
[2] )
173 map
.stack
.frames
[0].i
= 0;
174 map
.stack
.frames
[0].x
= coords
[0];
175 map
.stack
.frames
[0].y
= coords
[1];
178 static struct cell
*map_stack_next(void)
180 struct cell
*tile
= NULL
;
184 struct vframe
*frame
= &map
.stack
.frames
[ map
.stack
.level
];
186 int output_dirs
[][2] = { {0,-1}, {-1,0}, {1,0} };
190 int *dir
= output_dirs
[ frame
->i
];
191 tile
= map_tile_at( (int[2]){frame
->x
+dir
[0], frame
->y
+dir
[1]} );
194 if( tile
&& !(tile
->flags
& CELL_FLAG_VISITED
) )
198 frame
[1].x
= frame
[0].x
+dir
[0];
199 frame
[1].y
= frame
[0].y
+dir
[1];
209 if( map
.stack
.level
< 0 )
217 static int map_load( const char *str
)
226 if( str
[map
.x
] == ';' )
228 else if( !str
[map
.x
] )
230 vg_error( "Unexpected EOF when parsing level!\n" );
235 struct cell
*row
= arraddnptr( map
.cells
, map
.x
);
237 int reg_start
= 0, reg_end
= 0;
253 if( reg_start
< reg_end
)
255 if( *c
>= 'a' && *c
<= 'z' )
257 arrpush( map
.cells
[ map
.io
[ reg_start
] ].conditions
, *c
);
261 if( *c
== ',' || *c
== '\n' )
270 vg_error( "Unkown attrib '%c' (row: %u)\n", *c
, map
.y
);
277 vg_error( "Over-assigned values (row: %u)\n", map
.y
);
285 if( reg_start
!= reg_end
)
287 vg_error( "Not enough values assigned (row: %u, %u of %u)\n", map
.y
, reg_start
, reg_end
);
293 vg_error( "Map row underflow (row: %u, %u<%u)\n", map
.y
, cx
, map
.x
);
297 row
= arraddnptr( map
.cells
, map
.x
);
300 reg_end
= reg_start
= arrlen( map
.io
);
306 vg_error( "Map row overflow (row: %u, %u>%u)\n", map
.y
, cx
, map
.x
);
310 row
[ cx
].conditions
= NULL
;
312 // Parse the various cell types
313 if( *c
== '+' || *c
== '-' )
315 arrpush( map
.io
, cx
+ map
.y
*map
.x
);
316 row
[ cx
++ ].flags
= *c
== '+'? CELL_FLAG_INPUT
: CELL_FLAG_OUTPUT
;
321 row
[ cx
++ ].flags
= CELL_FLAG_WALL
;
325 row
[ cx
++ ].flags
= 0x00;
332 // Origin top left corner
333 map
.origin
[0] = -((float)map
.x
) * 0.5f
;
334 map
.origin
[2] = -((float)map
.y
) * 0.5f
;
336 vg_success( "Map loaded! (%u:%u)\n", map
.x
, map
.y
);
340 static int map_tile_availible( int co
[2] )
342 // Extract 5x5 grid surrounding tile
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
++ )
347 struct cell
*cell
= map_tile_at( (int[2]){ x
, y
} );
349 if( cell
&& (cell
->flags
& CELL_FLAG_WALKABLE
) )
350 blob
|= 0x1 << ((y
-(co
[1]-2))*5 + x
-(co
[0]-2));
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
++ )
357 if( blob
& (0x1 << (6+kernel
[i
])) )
359 // (reference window: 0x1CE7) Illegal moves
368 u32 invalid
[] = { 0x8E2, 0x63, 0xC6, 0xC60, 0x18C0, 0x862, 0x8C2 };
369 u32 window
= blob
>> kernel
[i
];
371 for( int j
= 0; j
< vg_list_size(invalid
); j
++ )
372 if((window
& invalid
[j
]) == invalid
[j
])
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
);
387 glm_mat4_identity( m_view
);
388 glm_translate_z( m_view
, -10.f
);
389 glm_rotate_x( m_view
, 1.0f
, m_view
);
391 glm_mat4_mul( m_projection
, m_view
, vg_pv
);
393 // Compute map update
394 for( int y
= 0; y
< map
.y
; y
++ )
396 for( int x
= 0; x
< map
.x
; x
++ )
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
);
402 if( tile
->flags
& CELL_FLAG_WALKABLE
)
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
);
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
);
414 tile
->flags
|= CELL_FLAG_MERGE
| CELL_FLAG_UPLVL
;
419 tile
->flags
|= CELL_FLAG_SPLIT
;
420 l
->flags
|= CELL_FLAG_UPLVL
;
421 r
->flags
|= CELL_FLAG_UPLVL
;
428 // Compute classification
432 for( int i = 0; i < arrlen( map.io ); i ++ )
434 struct *cell cell = &map.cells[ map.io ];
437 if( cell->flags & CELL_FLAG_INPUT )
439 map_tile_coords_from_index( map.io, coords );
440 map_stack_init( coords );
444 if( cell->flags & CELL_FLAG_CONNECTOR )
449 while( (cell = map_stack_next()) );
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
);
464 // Get floor tile intersection
465 float ray_t
= -ray_origin
[1] / ray_dir
[1];
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
);
472 int tile_x
= floorf( tile_pos
[0] );
473 int tile_y
= floorf( tile_pos
[2] );
475 map
.selected
= map_tile_at( (int [2]){tile_x
, tile_y
} );
479 static int fish_counter
= 0;
482 if( fish_counter
> 20 )
486 // Advance characters
487 for( int i
= 0; i
< map
.num_fishes
; i
++ )
489 struct fish
*fish
= map
.fishes
+ i
;
494 struct cell
*tile
, *next
;
495 tile
= map_tile_at( fish
->co
);
497 if( tile
->flags
& CELL_FLAG_OUTPUT
)
499 vg_info( "Fish got zucced (%d)\n", i
);
505 if( tile
->flags
& CELL_FLAG_SPLIT
)
508 int new_dir
[][2] = { {0,-1},{1,0},{-1,0} };
511 for( int j
= 0; j
< 3; j
++ )
513 test_dir
= new_dir
[ tile
->state
];
514 tile
->state
= (tile
->state
+1)%3;
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
)) )
519 fish
->dir
[0] = test_dir
[0];
520 fish
->dir
[1] = test_dir
[1];
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
)) )
536 vg_info( "Fish died! (%d)\n", i
);
542 fish
->co
[0] += fish
->dir
[0];
543 fish
->co
[1] += fish
->dir
[1];
547 for( int i
= 0; i
< arrlen( map
.io
); i
++ )
549 struct cell
*input
= &map
.cells
[ map
.io
[i
] ];
551 if( input
->flags
& CELL_FLAG_INPUT
)
553 if( input
->state
< arrlen( input
->conditions
) )
555 struct fish
*fish
= &map
.fishes
[ map
.num_fishes
];
556 map_tile_coords_from_index( map
.io
[i
], fish
->co
);
558 int output_dirs
[][2] = { {0,-1}, {-1,0}, {1,0} };
561 for( int i
= 0; i
< vg_list_size( output_dirs
); i
++ )
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
)
567 fish
->dir
[0] = dir
[0];
568 fish
->dir
[1] = dir
[1];
583 vg_info( "There are now %u active fish\n", map
.num_fishes
);
586 if( vg_get_button_down( "go" ) )
591 vg_info( "Ending!\n" );
596 if( vg_get_button_down( "go" ) )
601 for( int i
= 0; i
< map
.x
*map
.y
; i
++ )
602 map
.cells
[ i
].state
= 0;
604 vg_info( "Starting!\n" );
609 map
.select_valid
= map_tile_availible( (int[2]){ tile_x
, tile_y
} );
611 if( map
.select_valid
)
613 if( vg_get_button_down("primary") )
615 if( map
.selected
->flags
& CELL_FLAG_CANAL
)
617 map
.selected
->flags
&= ~(CELL_FLAG_CANAL
);
621 map
.selected
->flags
|= CELL_FLAG_CANAL
;
634 glViewport( 0,0, vg_window_x
, vg_window_y
);
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
);
640 glBindVertexArray( tile_vao
);
642 SHADER_USE( colour_shader
);
643 glUniformMatrix4fv( SHADER_UNIFORM( colour_shader
, "uPv" ), 1, GL_FALSE
, (float *)vg_pv
);
645 for( int y
= 0; y
< map
.y
; y
++ )
647 for( int x
= 0; x
< map
.x
; x
++ )
649 glm_mat4_identity( m_mdl
);
650 glm_translate( m_mdl
,
652 map
.origin
[0] + (float)x
+ 0.5f
,
654 map
.origin
[2] + (float)y
+ 0.5f
657 glUniformMatrix4fv( SHADER_UNIFORM( colour_shader
, "uMdl" ), 1, GL_FALSE
, (float *)m_mdl
);
659 struct cell
*cell
= &map
.cells
[ y
*map
.x
+x
];
661 vec4 colour
= { 0.7f
, 0.7f
, 0.7f
, 1.f
};
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
);
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
);
673 if( map
.selected
== cell
)
675 if( !map
.select_valid
)
676 glm_vec3_copy( (vec3
){ 1.f
, 0.f
, 0.f
}, colour
);
678 float flash
= sinf( vg_time
*2.5f
) * 0.25f
+ 0.75f
;
679 glm_vec3_scale( colour
, flash
, colour
);
682 glUniform4fv( SHADER_UNIFORM( colour_shader
, "uColour" ), 1, colour
);
683 glDrawArrays( GL_TRIANGLES
, 0, 6 );
687 glUniform4f( SHADER_UNIFORM( colour_shader
, "uColour" ), 1.f
, 0.f
, 1.f
, 1.f
);
689 for( int i
= 0; i
< map
.num_fishes
; i
++ )
691 struct fish
*fish
= map
.fishes
+ i
;
695 glm_mat4_identity( m_mdl
);
696 glm_translate( m_mdl
,
698 map
.origin
[0] + (float)fish
->co
[0] + 0.5f
,
700 map
.origin
[2] + (float)fish
->co
[1] + 0.5f
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 );
712 SHADER_INIT( colour_shader
);
714 glGenVertexArrays( 1, &tile_vao
);
715 glGenBuffers( 1, &tile_vbo
);
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
723 glBindVertexArray( tile_vao
);
724 glBindBuffer( GL_ARRAY_BUFFER
, tile_vbo
);
733 glVertexAttribPointer( 0, 2, GL_FLOAT
, GL_FALSE
, 2 * sizeof(float), (void*)0 );
734 glEnableVertexAttribArray( 0 );
738 map
.tile_texture
= vg_tex2d_rgba( "textures/rivertiles_flowm.tga" );
739 map
.flow_texture
= vg_tex2d_rgba( "textures/rivertiles_ripple.tga" );
757 glDeleteTextures( 1, &map
.tile_texture
);
758 glDeleteTextures( 1, &map
.flow_texture
);