+struct world world;
+
+// UTILITY
+// ===========================================================================================================
+
+static void colour_code_v3( char const cc, v3f target )
+{
+ static v3f colour_sets[] =
+ { { 1.0f, 0.9f, 0.3f },
+ { 0.2f, 0.9f, 0.14f },
+ { 0.4f, 0.8f, 1.00f },
+ { 0.882f, 0.204f, 0.922f }
+ };
+
+ 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 );
+}
+
+static int hash21i( v2i p, u32 umod )
+{
+ static const int random_noise[] =
+ {
+ 0x46,0xD5,0xB8,0xD3,0xF2,0xE5,0xCC,0x07,0xD0,0xB3,0x7A,0xA2,0xC3,0xDA,0xDC,0x7F,0xE0,0xB7,0x42,0xA0,0xBF,0x41,0x92,0x32,0x6F,0x0D,0x45,0xC7,0x54,0xDB,0x30,0xC2,
+ 0xD5,0xDA,0x55,0x09,0xDE,0x74,0x48,0x20,0xE1,0x24,0x5C,0x4D,0x6F,0x36,0xD8,0xE9,0x8D,0x8F,0x54,0x99,0x98,0x51,0xFE,0xDB,0x26,0x04,0x65,0x57,0x56,0xF3,0x53,0x30,
+ 0x3D,0x16,0xC0,0xB6,0xF2,0x47,0xCF,0x62,0xB0,0x6C,0x8F,0x4F,0x8C,0x4C,0x17,0xF0,0x19,0x7E,0x2D,0x81,0x8D,0xFB,0x10,0xD3,0x49,0x50,0x60,0xFD,0x38,0x15,0x3B,0xEE,
+ 0x05,0xC1,0xCF,0x62,0x97,0x75,0xDF,0x4E,0x4D,0x89,0x5E,0x88,0x5C,0x30,0x8C,0x54,0x1D,0x39,0x41,0xEA,0xA2,0x63,0x12,0x1B,0x8E,0x35,0x22,0x9B,0x98,0xA3,0x7F,0x80,
+ 0xD6,0x27,0x94,0x66,0xB5,0x1D,0x7E,0xDF,0x96,0x28,0x38,0x3A,0xA0,0xE8,0x71,0x09,0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0xD8,0x39,0x08,0x9F,0x4F,0xC7,0x71,0x25,0x09,0x51,0x65,0xD6,0xA8,0x02,0x1F
+ };
+
+ return random_noise[ (random_noise[p[1] & 1023] + p[0]) & 1023 ] & umod;
+}
+
+// MESH
+// ===========================================================================================================
+
+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 );
+}
+
+static struct cell_button *get_wbutton( enum e_world_button btn )
+{
+ return &world.buttons[ btn ];
+}
+
+void leaderboard_set_score( struct cmp_level *cmp_level, u32 score );
+
+// WORLD/MAP
+// ===========================================================================================================
+
+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 struct cell *pcell( v2i pos )
+{
+ return &world.data[ pos[1]*world.w + pos[0] ];
+}
+
+static void map_reclassify( v2i start, v2i end, int update_texbuffer )
+{
+ v2i full_start = { 1,1 };
+ v2i full_end = { world.w-1, world.h-1 };
+
+ if( !start || !end )
+ {
+ start = full_start;
+ end = full_end;
+ }
+
+ // Texture data
+ u8 info_buffer[64*64*4];
+ u32 pixel_id = 0;
+
+ int px0 = vg_max( start[0], full_start[0] ),
+ px1 = vg_min( end[0], full_end[0] ),
+ py0 = vg_max( start[1], full_start[1] ),
+ py1 = vg_min( end[1], full_end[1] );
+
+ for( int y = py0; y < py1; y ++ )
+ {
+ for( int x = px0; x < px1; x ++ )
+ {
+ struct cell *cell = pcell((v2i){x,y});
+
+ v2i dirs[] = {{1,0},{0,1},{-1,0},{0,-1}};
+
+ u8 height = 0;
+ u8 config = 0x00;
+
+ if( cell->state & (FLAG_CANAL|FLAG_INPUT|FLAG_OUTPUT) )
+ {
+ for( int i = 0; i < vg_list_size( dirs ); i ++ )
+ {
+ struct cell *neighbour = pcell((v2i){x+dirs[i][0], y+dirs[i][1]});
+ if( neighbour->state & (FLAG_CANAL|FLAG_INPUT|FLAG_OUTPUT) )
+ config |= 0x1 << i;
+ }
+
+ height = 128;
+ }
+ else
+ {
+ if( cell->state & FLAG_WALL )
+ height = 0xFF-0x3F + hash21i( (v2i){x,y}, 0x3F );
+
+ config = 0xF;
+ }
+
+ pcell((v2i){x,y})->config = config;
+
+ u8 *info_px = &info_buffer[ (pixel_id ++)*4 ];
+ info_px[0] = height;
+ info_px[1] = cell->state & FLAG_WALL? 0: 255;
+ info_px[2] = 0;
+ info_px[3] = 0;
+
+ if(
+ (
+ ((cell->state & FLAG_IS_TRIGGER) && (cell->config == 0xF || cell->config == k_cell_type_split)) ||
+ ((cell->state & FLAG_TARGETED) && (cell->config != k_cell_type_split))
+ ) && update_texbuffer
+ ){
+ cell->state &= ~(FLAG_TARGETED|FLAG_IS_TRIGGER);
+ for( u32 i = 0; i < 2; i ++ )
+ {
+ if( cell->links[i] )
+ {
+ struct cell *other_ptr = &world.data[ cell->links[i] ];
+ other_ptr->links[ i ] = 0;
+ other_ptr->state &= ~FLAG_IS_TRIGGER;
+
+ if( other_ptr->links[ i ^ 0x1 ] == 0 )
+ other_ptr->state &= ~FLAG_TARGETED;
+ }
+ }
+
+ cell->links[0] = 0;
+ cell->links[1] = 0;
+ }
+ }
+ }
+
+ if( update_texbuffer )
+ {
+ glBindTexture( GL_TEXTURE_2D, world.background_data );
+ glTexSubImage2D( GL_TEXTURE_2D, 0, px0 + 16, py0 + 16, px1-px0, py1-py0, GL_RGBA, GL_UNSIGNED_BYTE, info_buffer );
+ }
+}
+
+static u32 gen_text_buffer( const char *str, struct sdf_font *font, v2f origin, float size, u32 start )
+{
+ u32 count = 0;
+
+ v2f cursor;
+ v2f invUv;
+ v2_copy( origin, cursor );
+
+ float invScale = (size / (float)font->size);
+ invUv[0] = 1.0f / (float)font->width;
+ invUv[1] = 1.0f / (float)font->height;
+
+ u16 base_idx = start * 4;
+
+ const char *_c = str;
+ char c;
+ while( (c = *(_c ++)) )
+ {
+ if( c == '\n' )
+ {
+ cursor[1] -= size * 1.25f;
+ cursor[0] = origin[0];
+ }
+ else if( c >= 32 && c <= 126 )
+ {
+ struct sdf_char *pch = &font->characters[ c - ' ' ];
+ struct vector_glyph_vert *vt = &text_buffers.buffer[ count * 4 ];
+ u16 *ind = &text_buffers.indices[ count * 6 ];
+
+ // Emit quad
+ v2f p0; v2f uv0;
+ v2f p1; v2f uv1;
+
+ v2_muladds( cursor, (v2f){ pch->originX, -pch->originY }, -invScale, p0 );
+ v2_muladds( p0, (v2f){ pch->w, -pch->h }, invScale, p1 );
+
+ v2_mul( (v2f){ pch->uvx, pch->uvy }, invUv, uv0 );
+ v2_muladd( uv0, (v2f){ pch->w, pch->h }, invUv, uv1 );
+
+ v2_copy( p0, vt[0].co );
+ v2_copy( uv0, vt[0].uv );
+ vt[0].colour = 0xffffffff;
+
+ v2_copy( (v2f){ p0[0], p1[1] }, vt[1].co );
+ v2_copy( (v2f){ uv0[0], uv1[1] }, vt[1].uv );
+ vt[1].colour = 0xffffffff;
+
+ v2_copy( p1, vt[2].co );
+ v2_copy( uv1, vt[2].uv );
+ vt[2].colour = 0xffffffff;
+
+ v2_copy( (v2f){ p1[0], p0[1] }, vt[3].co );
+ v2_copy( (v2f){ uv1[0], uv0[1] }, vt[3].uv );
+ vt[3].colour = 0xffffffff;
+
+ // Emit indices
+ ind[0] = base_idx+count*4;
+ ind[1] = base_idx+count*4+1;
+ ind[2] = base_idx+count*4+2;
+ ind[3] = base_idx+count*4;
+ ind[4] = base_idx+count*4+2;
+ ind[5] = base_idx+count*4+3;
+
+ cursor[0] += (float)pch->advance * invScale;
+ count ++;
+ }
+ }
+
+ glBindVertexArray( text_buffers.vao );
+
+ glBindBuffer( GL_ARRAY_BUFFER, text_buffers.vbo );
+ glBufferSubData( GL_ARRAY_BUFFER,
+ start*4*sizeof( struct vector_glyph_vert ),
+ count*4*sizeof( struct vector_glyph_vert ),
+ text_buffers.buffer
+ );
+
+ glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, text_buffers.ebo );
+ glBufferSubData( GL_ELEMENT_ARRAY_BUFFER, start*6*sizeof(u16), count*6*sizeof(u16), text_buffers.indices );
+
+ return count;
+}
+
+static void gen_level_text( struct cmp_level *pLevel )
+{
+ text_buffers.title_count = gen_text_buffer( pLevel->title, &font_Ubuntu, (v2f){ -5.0f, -0.6f }, 0.6f, text_buffers.title_start );
+ text_buffers.desc_count = gen_text_buffer( pLevel->description, &font_Ubuntu, (v2f){ -5.0, -0.9f }, 0.25f, text_buffers.desc_start );
+}
+
+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( c[world.w] == ';' )
+ break;
+ else if( !c[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') || *c == ' ' )
+ {
+ 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->pos[0] = cx;
+ term->pos[1] = world.h;
+
+ 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 x = 0; x < 64; x ++ )
+ {
+ for( int y = 0; y < 64; y ++ )
+ {
+ u8 *px = &info_buffer[((x*64)+y)*4];
+
+ px[0] = 0xFF-0x3F + hash21i( (v2i){x,y}, 0x3F );
+ px[1] = 0;
+ px[2] = 0;
+ px[3] = 0;
+ }
+ }
+
+ // Level selection area
+
+ for( int i = 0; i < vg_list_size( button_grids ); i ++ )
+ {
+ struct button_grid *grid = &button_grids[ i ];
+
+ for( int y = 0; y < grid->dims[1]; y ++ )
+ {
+ for( int x = 0; x < grid->dims[0]; x ++ )
+ {
+ u8 *px = &info_buffer[((y+16+grid->origin[1])*64+16+x+grid->origin[0])*4];
+ px[0] = 0x10;
+ }
+ }
+ }
+
+ info_buffer[(((16+world.h-3)*64)+world.w+16-1)*4] = 0x30;
+ info_buffer[(((16+world.h-2)*64)+world.w+16-1)*4] = 0x30;
+
+ // Random walks.. kinda
+ for( int i = 0; i < arrlen(world.io); i ++ )
+ {
+ struct cell_terminal *term = &world.io[ i ];
+
+ v2i turtle;
+ v2i turtle_dir;
+ int original_y;
+
+ turtle[0] = 16+term->pos[0];
+ turtle[1] = 16+term->pos[1];
+
+ turtle_dir[0] = 0;
+ turtle_dir[1] = pcell(term->pos)->state & FLAG_INPUT? 1: -1;
+ original_y = turtle_dir[1];
+
+ info_buffer[((turtle[1]*64)+turtle[0])*4] = 0;
+ v2i_add( turtle_dir, turtle, turtle );
+
+ for( int i = 0; i < 100; i ++ )
+ {
+ info_buffer[((turtle[1]*64)+turtle[0])*4] = 0;
+
+ v2i_add( turtle_dir, turtle, turtle );
+
+ if( turtle[0] == 0 ) break;
+ if( turtle[0] == 63 ) break;
+ if( turtle[1] == 0 ) break;
+ if( turtle[1] == 63 ) break;
+
+ int random_value = hash21i( turtle, 0xFF );
+ if( random_value > 255-40 && !turtle_dir[0] )
+ {
+ turtle_dir[0] = -1;
+ turtle_dir[1] = 0;
+ }
+ else if( random_value > 255-80 && !turtle_dir[0] )
+ {
+ turtle_dir[0] = 1;
+ turtle_dir[1] = 0;
+ }
+ else if( random_value > 255-100 )
+ {
+ turtle_dir[0] = 0;
+ turtle_dir[1] = original_y;
+ }
+ }
+ }
+
+ 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 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( v2i_eq( term->pos, (v2i){x,y} ) )
+ {
+ 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 );
+ }
+}
+
+// CAREER STATE
+// ===========================================================================================================
+
+#pragma pack(push,1)
+struct dcareer_state
+{
+ u32 version;
+ i32 in_map;
+
+ u32 reserved[14];
+
+ struct dlevel_state
+ {
+ i32 score;
+ i32 unlocked;
+ i32 reserved[2];
+ }
+ levels[ NUM_CAMPAIGN_LEVELS ];
+};
+#pragma pack(pop)
+
+static int career_load_success = 0;
+
+static void career_serialize(void)
+{
+ if( !career_load_success )
+ return;
+
+ struct dcareer_state encoded;
+ encoded.version = 2;
+ encoded.in_map = world.pCmpLevel? world.pCmpLevel->serial_id: -1;
+
+ memset( encoded.reserved, 0, sizeof( encoded.reserved ) );
+
+ for( int i = 0; i < vg_list_size( career_serializable ); i ++ )
+ {
+ struct serializable_set *set = &career_serializable[i];
+
+ for( int j = 0; j < set->count; j ++ )
+ {
+ struct cmp_level *lvl = &set->pack[j];
+ struct dlevel_state *dest = &encoded.levels[lvl->serial_id];
+
+ dest->score = lvl->completed_score;
+ dest->unlocked = lvl->unlocked;
+ dest->reserved[0] = 0;
+ dest->reserved[1] = 0;
+ }
+ }
+
+ vg_asset_write( "sav/game.sv2", &encoded, sizeof( struct dcareer_state ) );
+}
+
+static void career_unlock_level( struct cmp_level *lvl );
+static void career_unlock_level( struct cmp_level *lvl )
+{
+ lvl->unlocked = 1;
+
+ if( lvl->linked )
+ career_unlock_level( lvl->linked );
+}
+
+static void career_pass_level( struct cmp_level *lvl, int score, int upload )
+{
+ if( score > 0 )
+ {
+ if( score < lvl->completed_score || lvl->completed_score == 0 )
+ {
+ if( !lvl->is_tutorial && upload )
+ leaderboard_set_score( lvl, score );
+
+ lvl->completed_score = score;
+ }
+
+ if( lvl->unlock ) career_unlock_level( lvl->unlock );
+
+ if( lvl->achievement )
+ {
+ sw_set_achievement( lvl->achievement );
+ }
+
+ // Check ALL maps to trigger master engineer
+ for( int i = 0; i < vg_list_size( career_serializable ); i ++ )
+ {
+ struct serializable_set *set = &career_serializable[i];
+
+ for( int j = 0; j < set->count; j ++ )
+ {
+ if( set->pack[j].completed_score == 0 )
+ return;
+ }
+ }
+
+ sw_set_achievement( "MASTER_ENGINEER" );
+ }
+}
+
+static void career_reset_level( struct cmp_level *lvl )
+{
+ lvl->unlocked = 0;
+ lvl->completed_score = 0;
+}
+
+static void career_load(void)
+{
+ i64 sz;
+ struct dcareer_state encoded;
+
+ // Blank save state
+ memset( (void*)&encoded, 0, sizeof( struct dcareer_state ) );
+ encoded.in_map = 0;
+ encoded.levels[0].unlocked = 1;
+
+ // Load and copy data into encoded
+ void *cr = vg_asset_read_s( "sav/game.sv2", &sz );
+
+ if( cr )
+ {
+ if( sz > sizeof( struct dcareer_state ) )
+ vg_warn( "This save file is too big! Some levels will be lost\n" );
+
+ if( sz <= offsetof( struct dcareer_state, levels ) )
+ {
+ vg_warn( "This save file is too small to have a header. Creating a blank one\n" );
+ free( cr );
+ }
+
+ memcpy( (void*)&encoded, cr, VG_MIN( sizeof( struct dcareer_state ), sz ) );
+ free( cr );
+ }
+ else
+ vg_info( "No save file... Using blank one\n" );
+
+ // Reset memory
+ for( int i = 0; i < vg_list_size( career_serializable ); i ++ )
+ {
+ struct serializable_set *set = &career_serializable[i];
+
+ for( int j = 0; j < set->count; j ++ )
+ career_reset_level( &set->pack[j] );
+ }
+
+ // Header information
+ // =================================
+
+ // Decode everything from dstate
+ for( int i = 0; i < vg_list_size( career_serializable ); i ++ )
+ {
+ struct serializable_set *set = &career_serializable[i];
+
+ for( int j = 0; j < set->count; j ++ )
+ {
+ struct cmp_level *lvl = &set->pack[j];
+ struct dlevel_state *src = &encoded.levels[lvl->serial_id];
+
+ if( src->unlocked ) career_unlock_level( lvl );
+ if( src->score ) lvl->completed_score = src->score;
+
+ // ...
+ if( lvl->serial_id == encoded.in_map )
+ {
+ if( console_changelevel( 1, &lvl->map_name ) )
+ {
+ world.pCmpLevel = lvl;
+ gen_level_text( world.pCmpLevel );
+ }
+ }
+ }
+ }
+
+ career_load_success = 1;
+}
+
+// MAIN GAMEPLAY
+// ===========================================================================================================
+static int is_simulation_running(void)