+const char *level_pack_1[] = {
+ "level0",
+ "level1",
+ "level2",
+ "level3",
+ "level4",
+ "level5",
+ "level6",
+ "level7_combine",
+ "xor_small",
+ "sort",
+ "thirds"
+};
+
+#pragma pack(push,1)
+struct career_state
+{
+ u32 version;
+
+ struct career_level
+ {
+ u32 score;
+ u32 time;
+ u32 completed;
+ }
+ levels[ vg_list_size( level_pack_1 ) ];
+}
+career = { .version = 1 };
+#pragma pack(pop)
+
+static void career_serialize(void)
+{
+ vg_asset_write( "sav/game.sav", &career, sizeof( struct career_state ) );
+}
+
+static void career_load(void)
+{
+ i64 sz;
+ struct career_state *cr = vg_asset_read_s( "sav/game.sav", &sz );
+
+ memset( (void*)career.levels, 0, vg_list_size(level_pack_1) * sizeof(struct career_level) );
+
+ if( cr )
+ {
+ if( sz > sizeof( struct career_state ) )
+ vg_warn( "This save file is too big! Some levels will be lost\n" );
+
+ if( sz <= offsetof( struct career_state, levels ) )
+ {
+ vg_error( "This save file is too small to have a header\n" );
+ free( cr );
+ return;
+ }
+
+ u32 const size_header = offsetof(struct career_state, levels);
+ u32 const size_levels = sizeof(struct career_state)-size_header;
+ u32 const size_levels_input = sz - size_header;
+
+ memcpy( (void*)career.levels, (void*)cr->levels, size_levels_input );
+
+ if( sz < sizeof( struct career_state ) )
+ {
+ memset( ((void*)career.levels) + size_levels_input, 0, size_levels-size_levels_input );
+ }
+
+ free( cr );
+ vg_success( "Loaded save file... Info:\n" );
+
+ for( int i = 0; i < vg_list_size( career.levels ); i ++ )
+ {
+ struct career_level *lvl = &career.levels[i];
+ vg_info( "Score: %u, Time: %u, Completed: %u\n", lvl->score, lvl->time, lvl->completed );
+ }
+ }
+ else
+ {
+ vg_info( "No save file... Using blank one\n" );
+ }
+}
+
+m3x3f m_projection;
+m3x3f m_view;
+m3x3f m_mdl;
+
+#define FLAG_CANAL 0x1
+#define FLAG_IS_TRIGGER 0x2
+#define FLAG_RESERVED0 0x4
+#define FLAG_RESERVED1 0x8
+
+#define FLAG_INPUT 0x10
+#define FLAG_OUTPUT 0x20
+#define FLAG_WALL 0x40
+
+#define FLAG_FLIP_FLOP 0x100
+#define FLAG_TRIGGERED 0x200
+#define FLAG_FLIP_ROTATING 0x400
+#define FLAG_TARGETED 0x800
+
+/*
+ 0000 0 | 0001 1 | 0010 2 | 0011 3
+ | | | | |
+ X | X= | X | X=
+ | | |
+ 0100 4 | 0101 5 | 0110 6 | 0111 7
+ | | | | |
+ =X | =X= | =X | =X=
+ | | |
+ 1000 8 | 1001 9 | 1010 10 | 1011 11
+ | | | | |
+ X | X= | X | X=
+ | | | | | | |
+ 1100 12 | 1101 13 | 1110 14 | 1111 15
+ | | | | |
+ =X | =X= | =X | =X=
+ | | | | | | |
+*/
+
+enum cell_type
+{
+ k_cell_type_ramp_right = 3,
+ k_cell_type_ramp_left = 6,
+ k_cell_type_split = 7,
+ k_cell_type_merge = 13
+};
+
+v3f colour_sets[] =
+{ { 0.9f, 0.6f, 0.20f },
+ { 0.2f, 0.9f, 0.14f },
+ { 0.4f, 0.8f, 1.00f } };
+
+static void colour_code_v3( char cc, v3f target )
+{
+ if( cc >= 'a' && cc <= 'z' )
+ {
+ int id = cc - 'a';
+
+ if( id < vg_list_size( colour_sets ) )
+ {
+ v3_copy( colour_sets[ id ], target );
+ return;
+ }
+ }
+
+ v3_copy( (v3f){0.0f,0.0f,0.0f}, target );
+}
+
+struct mesh
+{
+ GLuint vao, vbo;
+ u32 elements;
+};
+
+static void init_mesh( struct mesh *m, float const *tris, u32 length )
+{
+ m->elements = length/3;
+ glGenVertexArrays( 1, &m->vao );
+ glGenBuffers( 1, &m->vbo );
+
+ glBindVertexArray( m->vao );
+ glBindBuffer( GL_ARRAY_BUFFER, m->vbo );
+ glBufferData( GL_ARRAY_BUFFER, length*sizeof(float), tris, GL_STATIC_DRAW );
+
+ glVertexAttribPointer( 0, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(float), (void*)0 );
+ glEnableVertexAttribArray( 0 );
+
+ VG_CHECK_GL();
+}
+
+static void free_mesh( struct mesh *m )
+{
+ glDeleteVertexArrays( 1, &m->vao );
+ glDeleteBuffers( 1, &m->vbo );
+}
+
+static void draw_mesh( int const start, int const count )
+{
+ glDrawArrays( GL_TRIANGLES, start*3, count*3 );
+}
+
+static void use_mesh( struct mesh *m )
+{
+ glBindVertexArray( m->vao );
+}
+
+struct world
+{
+#pragma pack(push,1)
+ struct cell
+ {
+ u16 state;
+ u16 links[2];
+ u8 config;
+ u8 pad0;
+ }
+ *data;
+#pragma pack(pop)
+
+ int frame;
+
+ int initialzed;
+
+ int sim_frame;
+ float sim_start;
+ int simulating;
+ int sim_run, max_runs;
+
+ float frame_lerp;
+
+ struct cell_terminal
+ {
+ //char *conditions;
+ //char recv[12];
+
+ struct terminal_run
+ {
+ char conditions[8];
+ char recieved[8];
+
+ int condition_count, recv_count;
+ }
+ runs[8];
+
+ int run_count;
+
+ int id;
+ }
+ *io;
+
+ int w, h;
+
+ struct mesh tile, circle, numbers;
+ struct mesh_wire
+ {
+ GLuint vao, vbo, ebo;
+ u32 em;
+ }
+ wire;
+
+ GLuint background_data;
+ GLuint random_samples;
+
+ int selected, tile_x, tile_y;
+ v2f tile_pos;
+
+ struct fish
+ {
+ v2i pos;
+ v2i dir;
+ int alive;
+ char payload;
+ float death_time;
+ v2f physics_v;
+ v2f physics_co;
+ }
+ fishes[16];
+
+ int num_fishes;
+
+ char map_name[128];
+ struct career_level *ptr_career_level;
+
+ u32 score;
+ u32 completed;
+ u32 time;
+
+} world = {};
+
+static void map_free(void)
+{
+ arrfree( world.data );
+ arrfree( world.io );
+
+ world.w = 0;
+ world.h = 0;
+ world.data = NULL;
+ world.io = NULL;
+ world.score = 0;
+ world.time = 0;
+ world.completed = 0;
+ world.max_runs = 0;
+ world.initialzed = 0;
+}
+
+static void io_reset(void)
+{
+ for( int i = 0; i < arrlen( world.io ); i ++ )
+ {
+ struct cell_terminal *term = &world.io[i];
+
+ for( int j = 0; j < term->run_count; j ++ )
+ term->runs[j].recv_count = 0;
+ }
+}
+
+static void map_reclassify( v2i start, v2i end, int update_texbuffer );
+static int map_load( const char *str, const char *name )
+{
+ //TODO: It may be worthwhile, at this point, to switch to binary encoding for save data
+
+ map_free();
+
+ char const *c = str;
+
+ // Scan for width
+ for(;; world.w ++)
+ {
+ if( str[world.w] == ';' )
+ break;
+ else if( !str[world.w] )
+ {
+ vg_error( "Unexpected EOF when parsing level\n" );
+ return 0;
+ }
+ }
+
+ struct cell *row = arraddnptr( world.data, world.w );
+ int cx = 0;
+ int reg_start = 0, reg_end = 0;
+
+ u32 *links_to_make = NULL;
+ int links_satisfied = 0;
+
+ char link_id_buffer[32];
+ int link_id_n = 0;
+
+ for(;;)
+ {
+ if( !*c )
+ break;
+
+ if( *c == '\r' ) { c ++; continue; } // fuck off windows
+
+ if( *c == ';' )
+ {
+ c ++;
+
+ if( *c == '\r' ) c ++;
+
+ // Parse attribs
+ if( *c != '\n' )
+ {
+ while( *c )
+ {
+ if( *c == '\r' ) { c ++; continue; }
+
+ if( reg_start < reg_end )
+ {
+ struct cell_terminal *terminal = &world.io[ reg_start ];
+ struct terminal_run *run = &terminal->runs[ terminal->run_count-1 ];
+
+ if( *c >= 'a' && *c <= 'z' )
+ {
+ run->conditions[ run->condition_count ++ ] = *c;
+ }
+ else
+ {
+ if( *c == ',' || *c == '\n' )
+ {
+ reg_start ++;
+
+ if( *c == '\n' )
+ break;
+ }
+ else if( *c == ':' )
+ {
+ terminal->runs[ terminal->run_count ].condition_count = 0;
+ terminal->run_count ++;
+ world.max_runs = vg_max( world.max_runs, terminal->run_count );
+ }
+ else
+ {
+ vg_error( "Unkown attribute '%c' (row: %u)\n", *c, world.h );
+ goto IL_REG_ERROR;
+ }
+ }
+ }
+ else
+ {
+ if( links_satisfied < arrlen( links_to_make ) )
+ {
+ struct cell *target = &world.data[ links_to_make[ links_satisfied ] ];
+
+ if( (((u32)*c >= (u32)'0') && ((u32)*c <= (u32)'9')) || *c == '-' )
+ {
+ if( link_id_n >= vg_list_size( link_id_buffer )-1 )
+ {
+ vg_error( "Number was way too long to be parsed (row: %u)\n", world.h );
+ goto IL_REG_ERROR;
+ }
+
+ link_id_buffer[ link_id_n ++ ] = *c;
+ }
+ else if( *c == ',' || *c == '\n' )
+ {
+ link_id_buffer[ link_id_n ] = 0x00;
+ int value = atoi( link_id_buffer );
+
+ target->links[value >= 0? 1:0] = abs(value);
+ links_satisfied ++;
+ link_id_n = 0;
+
+ if( *c == '\n' )
+ break;
+ }
+ else
+ {
+ vg_error( "Invalid character '%c' (row: %u)\n", *c, world.h );
+ goto IL_REG_ERROR;
+ }
+ }
+ else
+ {
+ vg_error( "Too many values to assign (row: %u)\n", world.h );
+ goto IL_REG_ERROR;
+ }
+ }
+
+ c ++;
+ }
+ }
+
+ // Registry length-error checks
+ if( reg_start != reg_end )
+ {
+ vg_error( "Not enough spawn values assigned (row: %u, %u of %u)\n", world.h, reg_start, reg_end );
+ goto IL_REG_ERROR;
+ }
+
+ if( links_satisfied != arrlen( links_to_make ) )
+ {
+ vg_error( "Not enough link values assigned (row: %u, %u of %u)\n", world.h, links_satisfied, arrlen( links_to_make ) );
+ goto IL_REG_ERROR;
+ }
+
+ if( cx != world.w )
+ {
+ vg_error( "Not enough cells to match previous row definition (row: %u, %u<%u)\n", world.h, cx, world.w );
+ goto IL_REG_ERROR;
+ }
+
+ row = arraddnptr( world.data, world.w );
+ cx = 0;
+ world.h ++;
+ reg_end = reg_start = arrlen( world.io );
+
+ arrsetlen( links_to_make, 0 );
+ links_satisfied = 0;
+ }
+ else
+ {
+ if( cx == world.w )
+ {
+ vg_error( "Too many cells to match previous row definition (row: %u, %u>%u)\n", world.h, cx, world.w );
+ goto IL_REG_ERROR;
+ }
+
+ // Tile initialization
+ // row[ cx ] .. etc
+ struct cell *cell = &row[ cx ];
+ cell->config = 0xF;
+
+ if( *c == '+' || *c == '-' )
+ {
+ struct cell_terminal *term = arraddnptr( world.io, 1 );
+ term->id = cx + world.h*world.w;
+ term->run_count = 1;
+ term->runs[0].condition_count = 0;
+
+ cell->state = *c == '+'? FLAG_INPUT: FLAG_OUTPUT;
+ reg_end ++;
+ }
+ else if( *c == '#' ) cell->state = FLAG_WALL;
+ else if( ((u32)*c >= (u32)'A') && ((u32)*c <= (u32)'A'+0xf) )
+ {
+ // Canal flag bits (4bit/16 value):
+ // 0: Canal present
+ // 1: Is trigger
+ // 2: Reserved
+ // 3: Reserved
+
+ cell->state = ((u32)*c - (u32)'A') & (FLAG_CANAL|FLAG_IS_TRIGGER);
+
+ if( cell->state & FLAG_IS_TRIGGER )
+ arrpush( links_to_make, cx + world.h*world.w );
+
+ cell->links[0] = 0;
+ cell->links[1] = 0;
+ world.score ++;
+ }
+ else cell->state = 0x00;
+
+ cx ++;
+ }
+
+ c ++;
+ }
+
+ // Update data texture to fill out the background
+ {
+ u8 info_buffer[64*64*4];
+ for( int i = 0; i < 64*64; i ++ )
+ {
+ u8 *px = &info_buffer[i*4];
+ px[0] = 255;
+ px[1] = 0;
+ px[2] = 0;
+ px[3] = 0;
+ }
+
+ glBindTexture( GL_TEXTURE_2D, world.background_data );
+ glTexSubImage2D( GL_TEXTURE_2D, 0, 0, 0, 64, 64, GL_RGBA, GL_UNSIGNED_BYTE, info_buffer );
+ }
+
+ arrfree( links_to_make );
+
+ map_reclassify( NULL, NULL, 1 );
+
+ // Validate links
+ for( int i = 0; i < world.h*world.w; i ++ )
+ {
+ struct cell *src = &world.data[i];
+ if( src->state & FLAG_IS_TRIGGER )
+ {
+ int link_id = src->links[0]?0:1;
+ if( src->links[link_id] <= world.h*world.w )
+ {
+ struct cell *target = &world.data[ src->links[link_id] ];
+ if( (target->state & FLAG_CANAL) && (target->config == k_cell_type_split) )
+ {
+ if( target->links[ link_id ] )
+ {
+ vg_error( "Link target was already targeted\n" );
+ goto IL_REG_ERROR;
+ }
+ else
+ {
+ // Valid link
+ target->links[ link_id ] = i;
+ target->state |= FLAG_TARGETED;
+ }
+ }
+ else
+ {
+ vg_error( "Link target was invalid\n" );
+ goto IL_REG_ERROR;
+ }
+ }
+ else
+ {
+ vg_error( "Link target out of bounds\n" );
+ goto IL_REG_ERROR;
+ }
+ }
+ }
+
+ vg_success( "Map '%s' loaded! (%u:%u)\n", name, world.w, world.h );
+
+ io_reset();
+
+ strncpy( world.map_name, name, vg_list_size( world.map_name )-1 );
+ world.initialzed = 1;
+ return 1;
+
+IL_REG_ERROR:
+ arrfree( links_to_make );
+ map_free();
+ return 0;
+}
+
+static struct cell *pcell( v2i pos )
+{
+ return &world.data[ pos[1]*world.w + pos[0] ];
+}
+
+static void map_serialize( FILE *stream )
+{
+ for( int y = 0; y < world.h; y ++ )
+ {
+ for( int x = 0; x < world.w; x ++ )
+ {
+ struct cell *cell = pcell( (v2i){ x, y } );
+
+ if( cell->state & FLAG_WALL ) fputc( '#', stream );
+ else if( cell->state & FLAG_INPUT ) fputc( '+', stream );
+ else if( cell->state & FLAG_OUTPUT ) fputc( '-', stream );
+ else if( cell->state & (FLAG_CANAL|FLAG_IS_TRIGGER|FLAG_RESERVED0|FLAG_RESERVED1) )
+ {
+ fputc( (cell->state & (FLAG_CANAL|FLAG_IS_TRIGGER|FLAG_RESERVED0|FLAG_RESERVED1)) + (u32)'A', stream );
+ }
+ else fputc( ' ', stream );
+ }
+
+ fputc( ';', stream );
+
+ int terminal_write_count = 0;
+
+ for( int x = 0; x < world.w; x ++ )
+ {
+ for( int i = 0; i < arrlen( world.io ); i ++ )
+ {
+ struct cell_terminal *term = &world.io[i];
+ if( term->id == y*world.w+x )
+ {
+ if( terminal_write_count )
+ fputc( ',', stream );
+ terminal_write_count ++;
+
+ for( int j = 0; j < term->run_count; j ++ )
+ {
+ struct terminal_run *run = &term->runs[j];
+
+ for( int k = 0; k < run->condition_count; k ++ )
+ fputc( run->conditions[k], stream );
+
+ if( j < term->run_count-1 )
+ fputc( ':', stream );
+ }
+ }
+ }
+ }
+
+ for( int x = 0; x < world.w; x ++ )
+ {
+ struct cell *cell = pcell( (v2i){ x,y } );
+ if( cell->state & FLAG_IS_TRIGGER )
+ {
+ if( terminal_write_count )
+ fputc( ',', stream );
+ terminal_write_count ++;
+
+ fprintf( stream, "%d", cell->links[0]? -cell->links[0]: cell->links[1] );
+ }
+ }
+
+ fputc( '\n', stream );
+ }
+}