1 // Copyright (C) 2021 Harry Godden (hgn) - All Rights Reserved
4 #define VG_STEAM_APPID 1218140U
6 #include "fishladder_resources.h"
13 3. Merge (and explode)
14 4. Principle 1 (divide colours)
15 5. Principle 2 (combine colours)
25 New things to program:
26 UI text element renderer (SDF) DONE(sorta)
27 Particle system thing for ball collision
28 Level descriptions / titles HALF
30 Play button / Speed controller
37 const char *level_pack_1
[] = {
64 levels
[ NUM_CAMPAIGN_LEVELS
];
68 static void career_serialize(void)
70 struct dcareer_state encoded
;
72 encoded
.total_unlocked
= career_local
.total_unlocked
;
74 for( int i
= 0; i
< vg_list_size( career_serializable
); i
++ )
76 struct serializable_set
*set
= &career_serializable
[i
];
78 for( int j
= 0; j
< set
->count
; j
++ )
80 struct cmp_level
*lvl
= &set
->pack
[j
];
81 struct dlevel_state
*dest
= &encoded
.levels
[lvl
->serial_id
];
83 dest
->score
= lvl
->completed_score
;
84 dest
->reserved
[0] = 0;
85 dest
->reserved
[1] = 0;
86 dest
->reserved
[2] = 0;
90 vg_asset_write( "sav/game.sv2", &encoded
, sizeof( struct dcareer_state
) );
93 static void career_load(void)
96 struct dcareer_state encoded
;
97 memset( (void*)&encoded
, 0, sizeof( struct dcareer_state
) );
99 // Load and copy data into encoded
100 void *cr
= vg_asset_read_s( "sav/game.sv2", &sz
);
104 if( sz
> sizeof( struct dcareer_state
) )
105 vg_warn( "This save file is too big! Some levels will be lost\n" );
107 if( sz
<= offsetof( struct dcareer_state
, levels
) )
109 vg_warn( "This save file is too small to have a header. Creating a blank one\n" );
114 memcpy( (void*)&encoded
, cr
, VG_MIN( sizeof( struct dcareer_state
), sz
) );
118 vg_info( "No save file... Using blank one\n" );
120 // Decode everything from dstate
121 for( int i
= 0; i
< vg_list_size( career_serializable
); i
++ )
123 struct serializable_set
*set
= &career_serializable
[i
];
125 for( int j
= 0; j
< set
->count
; j
++ )
127 struct cmp_level
*lvl
= &set
->pack
[j
];
128 struct dlevel_state
*src
= &encoded
.levels
[lvl
->serial_id
];
130 lvl
->completed_score
= src
->score
;
140 #define FLAG_CANAL 0x1
141 #define FLAG_IS_TRIGGER 0x2
142 #define FLAG_RESERVED0 0x4
143 #define FLAG_RESERVED1 0x8
145 #define FLAG_INPUT 0x10
146 #define FLAG_OUTPUT 0x20
147 #define FLAG_WALL 0x40
149 #define FLAG_FLIP_FLOP 0x100
150 #define FLAG_TRIGGERED 0x200
151 #define FLAG_FLIP_ROTATING 0x400
152 #define FLAG_TARGETED 0x800
155 0000 0 | 0001 1 | 0010 2 | 0011 3
159 0100 4 | 0101 5 | 0110 6 | 0111 7
163 1000 8 | 1001 9 | 1010 10 | 1011 11
167 1100 12 | 1101 13 | 1110 14 | 1111 15
173 struct cell_description
181 cell_descriptions
[] =
185 { .start
= { 1, 0 }, .end
= { -1, 0 } },
186 { .start
= { 0, 1 }, .end
= { 0, -1 } },
187 { .start
= { 0, 1 }, .end
= { 1, 0 } },
189 { .start
= { -1, 0 }, .end
= { 1, 0 } },
190 { .start
= { -1, 0 }, .end
= { 1, 0 }, .is_linear
= 1 },
191 { .start
= { 0, 1 }, .end
= { -1, 0 } },
192 { .start
= { 0, 1 }, .is_special
= 1 },
194 { .start
= { 0, -1 }, .end
= { 0, 1 } },
195 { .start
= { 1, 0 }, .end
= { 0, -1 } },
196 { .start
= { 0, 1 }, .end
= { 0, -1 }, .is_linear
= 1 },
199 { .start
= { -1, 0 }, .end
= { 0, -1 } },
200 { .end
= { 0, -1 }, .is_special
= 1 },
207 k_cell_type_stub
= 0,
208 k_cell_type_ramp_right
= 3,
209 k_cell_type_ramp_left
= 6,
210 k_cell_type_split
= 7,
211 k_cell_type_merge
= 13,
212 k_cell_type_con_r
= 1,
213 k_cell_type_con_u
= 2,
214 k_cell_type_con_l
= 4,
215 k_cell_type_con_d
= 8
218 v2f
const curve_3
[] = {{0.5f
,1.0f
},{0.5f
,0.625f
},{0.625f
,0.5f
},{1.0f
,0.5f
}};
219 v2f
const curve_6
[] = {{0.5f
,1.0f
},{0.5f
,0.625f
},{0.375f
,0.5f
},{0.0f
,0.5f
}};
220 v2f
const curve_9
[] = {{1.0f
,0.5f
},{0.625f
,0.5f
},{0.5f
,0.375f
},{0.5f
,0.0f
}};
221 v2f
const curve_12
[]= {{0.0f
,0.5f
},{0.375f
,0.5f
},{0.5f
,0.375f
},{0.5f
,0.0f
}};
223 v2f
const curve_1
[] = {{1.0f
,0.5f
},{0.8f
,0.5f
},{0.3f
,0.5f
},{0.2f
,0.5f
}};
224 v2f
const curve_4
[] = {{0.0f
,0.5f
},{0.3f
,0.5f
},{0.5f
,0.5f
},{0.8f
,0.5f
}};
225 v2f
const curve_2
[] = {{0.5f
,1.0f
},{0.5f
,0.8f
},{0.5f
,0.3f
},{0.5f
,0.2f
}};
226 v2f
const curve_8
[] = {{0.5f
,0.0f
},{0.5f
,0.3f
},{0.5f
,0.5f
},{0.5f
,0.8f
}};
228 v2f
const curve_7
[] = {{0.5f
,0.8438f
},{0.875f
,0.8438f
},{0.625f
,0.5f
},{1.0f
,0.5f
}};
229 v2f
const curve_7_1
[] = {{0.5f
,0.8438f
},{1.0f
-0.875f
,0.8438f
},{1.0-0.625f
,0.5f
},{0.0f
,0.5f
}};
231 float const curve_7_linear_section
= 0.1562f
;
234 { { 1.0f
, 0.9f
, 0.3f
},
235 { 0.2f
, 0.9f
, 0.14f
},
236 { 0.4f
, 0.8f
, 1.00f
} };
238 static void colour_code_v3( char cc
, v3f target
)
240 if( cc
>= 'a' && cc
<= 'z' )
244 if( id
< vg_list_size( colour_sets
) )
246 v3_copy( colour_sets
[ id
], target
);
251 v3_copy( (v3f
){0.0f
,0.0f
,0.0f
}, target
);
260 static void init_mesh( struct mesh
*m
, float const *tris
, u32 length
)
262 m
->elements
= length
/3;
263 glGenVertexArrays( 1, &m
->vao
);
264 glGenBuffers( 1, &m
->vbo
);
266 glBindVertexArray( m
->vao
);
267 glBindBuffer( GL_ARRAY_BUFFER
, m
->vbo
);
268 glBufferData( GL_ARRAY_BUFFER
, length
*sizeof(float), tris
, GL_STATIC_DRAW
);
270 glVertexAttribPointer( 0, 2, GL_FLOAT
, GL_FALSE
, 2 * sizeof(float), (void*)0 );
271 glEnableVertexAttribArray( 0 );
276 static void free_mesh( struct mesh
*m
)
278 glDeleteVertexArrays( 1, &m
->vao
);
279 glDeleteBuffers( 1, &m
->vbo
);
282 static void draw_mesh( int const start
, int const count
)
284 glDrawArrays( GL_TRIANGLES
, start
*3, count
*3 );
287 static void use_mesh( struct mesh
*m
)
289 glBindVertexArray( m
->vao
);
294 k_fish_state_soon_dead
= -1,
295 k_fish_state_dead
= 0,
298 k_fish_state_soon_alive
321 int sim_run
, max_runs
;
336 int condition_count
, recv_count
;
348 struct mesh tile
, circle
, numbers
;
351 GLuint vao
, vbo
, ebo
;
356 GLuint background_data
;
357 GLuint random_samples
;
359 int selected
, tile_x
, tile_y
;
366 enum e_fish_state state
;
378 //struct career_level *ptr_career_level;
379 struct cmp_level
*pCmpLevel
;
386 void leaderboard_set_score( struct cmp_level
*cmp_level
, u32 score
);
388 static void map_free(void)
390 arrfree( world
.data
);
401 world
.initialzed
= 0;
404 static void io_reset(void)
406 for( int i
= 0; i
< arrlen( world
.io
); i
++ )
408 struct cell_terminal
*term
= &world
.io
[i
];
410 for( int j
= 0; j
< term
->run_count
; j
++ )
411 term
->runs
[j
].recv_count
= 0;
415 int random_noise
[] = {
416 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,
417 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,
418 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,
419 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,
420 0xD6,0x27,0x94,0x66,0xB5,0x1D,0x7E,0xDF,0x96,0x28,0x38,0x3A,0xA0,0xE8,0x71,0x09,0x62,0x5E,0x9D,0x53,0x58,0x1B,0x7D,0x0D,0x2D,0x99,0x77,0x83,0xC3,0x89,0xC2,0xA2,
421 0xA7,0x1D,0x78,0x80,0x37,0xC1,0x87,0xFF,0x65,0xBF,0x2C,0xF1,0xE5,0xB3,0x09,0xE0,0x25,0x92,0x83,0x0F,0x8A,0x57,0x3C,0x0B,0xC6,0xBC,0x44,0x16,0xE3,0xCE,0xC3,0x0D,
422 0x69,0xD3,0xC6,0x99,0xB8,0x46,0x44,0xC4,0xF3,0x1E,0xBF,0xF5,0xB4,0xDB,0xFB,0x93,0xA1,0x7B,0xC9,0x08,0x77,0x22,0xE5,0x02,0xEF,0x9E,0x90,0x94,0x8A,0xA6,0x3D,0x7E,
423 0xA2,0xA0,0x10,0x82,0x47,0x5C,0xAA,0xF8,0x2F,0x0D,0x9F,0x76,0xDA,0x99,0x0F,0xCB,0xE2,0x02,0x0C,0x75,0xCA,0x35,0x29,0xA6,0x49,0x83,0x6D,0x91,0xB4,0xEC,0x31,0x69,
424 0xBA,0x13,0xF3,0xC7,0x21,0x06,0xC8,0x79,0xEF,0xB1,0x9C,0x6A,0xEE,0x64,0x9A,0xDC,0x1E,0xC6,0x18,0x93,0xA9,0x7E,0x89,0x7D,0x96,0xE5,0x44,0xB8,0x00,0x15,0xAF,0x8C,
425 0x78,0x8F,0xA8,0x05,0xA7,0x07,0x25,0x9A,0xC8,0x5D,0x90,0x1A,0x41,0x53,0x30,0xD3,0x24,0x33,0x71,0xB4,0x50,0x6E,0xE4,0xEA,0x0D,0x2B,0x6D,0xF5,0x17,0x08,0x74,0x49,
426 0x71,0xC2,0xAC,0xF7,0xDC,0xB2,0x7E,0xCC,0xB6,0x1B,0xB8,0xA9,0x52,0xCF,0x6B,0x51,0xD2,0x4E,0xC9,0x43,0xEE,0x2E,0x92,0x24,0xBB,0x47,0x4D,0x0C,0x3E,0x21,0x53,0x19,
427 0xD4,0x82,0xE2,0xC6,0x93,0x85,0x0A,0xF8,0xFA,0x04,0x07,0xD3,0x1D,0xEC,0x03,0x66,0xFD,0xB1,0xFB,0x8F,0xC5,0xDE,0xE8,0x29,0xDF,0x23,0x09,0x9D,0x7C,0x43,0x3D,0x4D,
428 0x89,0xB9,0x6F,0xB4,0x6B,0x4A,0x51,0xC3,0x94,0xF4,0x7C,0x5E,0x19,0x87,0x79,0xC1,0x80,0x0C,0x45,0x12,0xEC,0x95,0xF3,0x31,0x68,0x42,0xE1,0x06,0x57,0x0E,0xA7,0xFB,
429 0x78,0x96,0x87,0x23,0xA5,0x20,0x7A,0x09,0x3A,0x45,0xE6,0xD9,0x5E,0x6A,0xD6,0xAA,0x29,0x50,0x92,0x4E,0xD0,0xB5,0x91,0xC2,0x9A,0xCF,0x07,0xFE,0xB2,0x15,0xEB,0xE4,
430 0x84,0x40,0x14,0x47,0xFA,0x93,0xB9,0x06,0x69,0xDB,0xBD,0x4E,0xEA,0x52,0x9B,0xDE,0x5B,0x50,0x36,0xAB,0xB3,0x1F,0xD2,0xCD,0x9C,0x13,0x07,0x7E,0x8B,0xED,0x72,0x62,
431 0x74,0x77,0x3B,0x88,0xAC,0x5B,0x6A,0xBC,0xDA,0x99,0xE8,0x24,0x90,0x5A,0xCA,0x8D,0x5C,0x2B,0xF8,0xF1,0xE1,0x1D,0x94,0x11,0xEA,0xCC,0x02,0x09,0x1E,0xA2,0x48,0x67,
432 0x87,0x5A,0x7E,0xC6,0xCC,0xA3,0xFB,0xC5,0x36,0xEB,0x5C,0xE1,0xAF,0x1E,0xBE,0xE7,0xD8,0x8F,0x70,0xAE,0x42,0x05,0xF5,0xCD,0x2D,0xA2,0xB0,0xFD,0xEF,0x65,0x2C,0x22,
433 0xCB,0x8C,0x8B,0xAA,0x3D,0x86,0xE2,0xCD,0xBE,0xC3,0x42,0x38,0xE3,0x9C,0x08,0xB5,0xAE,0xBD,0x54,0x73,0x83,0x70,0x24,0x47,0xCA,0x4C,0x04,0xC4,0xE0,0x1D,0x40,0xED,
434 0xF4,0x2B,0x50,0x8E,0x97,0xB3,0xF0,0xA6,0x76,0xDB,0x49,0x30,0xE5,0xD9,0x71,0x07,0xB2,0xF1,0x0F,0xD6,0x77,0xAA,0x72,0xC0,0xAF,0x66,0xD8,0x40,0xC6,0x08,0x19,0x8C,
435 0xD9,0x8F,0x5A,0x75,0xAC,0xBE,0xC2,0x40,0x5B,0xBD,0x0D,0x1D,0x00,0xAF,0x26,0x5E,0x78,0x43,0xAA,0xC6,0x4F,0xF3,0xD8,0xE2,0x7F,0x0C,0x1E,0x77,0x4D,0x35,0x96,0x23,
436 0x32,0x44,0x03,0x8D,0x92,0xE7,0xFD,0x48,0x07,0xD0,0x58,0xFC,0x6D,0xC9,0x91,0x33,0xF0,0x23,0x45,0xA4,0x29,0xB9,0xF5,0xB0,0x68,0x8F,0x7B,0x59,0x15,0x8E,0xA6,0x66,
437 0x15,0xA0,0x76,0x9B,0x69,0xCB,0x38,0xA5,0xF4,0xB4,0x6B,0xDC,0x1F,0xAB,0xAE,0x12,0x77,0xC0,0x8C,0x4A,0x03,0xB9,0x73,0xD3,0x6D,0x52,0xC5,0xF5,0x6E,0x4E,0x4B,0xA3,
438 0x24,0x02,0x58,0xEE,0x5F,0xF9,0xD6,0xD0,0x1D,0xBC,0xF4,0xB8,0x4F,0xFD,0x4B,0x2D,0x34,0x77,0x46,0xE5,0xD4,0x33,0x7B,0x9C,0x35,0xCD,0xB0,0x5D,0x06,0x39,0x99,0xEB,
439 0x0C,0xD0,0x0F,0xF7,0x92,0xB5,0x58,0x5B,0x5E,0x79,0x12,0xF4,0x05,0xF6,0x21,0x07,0x0B,0x49,0x1A,0xFB,0xD4,0x98,0xC4,0xEF,0x7A,0xD6,0xCA,0xA1,0xDA,0xB3,0x51,0x00,
440 0x76,0xEC,0x08,0x48,0x40,0x35,0xD7,0x94,0xBE,0xF5,0x7B,0xA4,0x20,0x81,0x5F,0x82,0xF3,0x6F,0x96,0x24,0x98,0xB6,0x49,0x18,0xC8,0xC5,0x8C,0xD2,0x38,0x7F,0xC4,0xF6,
441 0xAA,0x87,0xDC,0x73,0x5B,0xA1,0xAF,0xE5,0x3D,0x37,0x6B,0x85,0xED,0x38,0x62,0x7D,0x57,0xBD,0xCF,0xB5,0x1B,0xA8,0xBB,0x32,0x33,0xD3,0x34,0x5A,0xC1,0x5D,0xFB,0x28,
442 0x6E,0xE1,0x67,0x51,0xBB,0x31,0x92,0x83,0xAC,0xAA,0x72,0x52,0xFD,0x13,0x4F,0x73,0xD3,0xF0,0x5E,0xFC,0xBA,0xB1,0x3C,0x7B,0x08,0x76,0x03,0x38,0x1E,0xD1,0xCC,0x33,
443 0xA3,0x1E,0xFC,0xE0,0x82,0x30,0x27,0x93,0x71,0x35,0x75,0x77,0xBA,0x78,0x10,0x33,0xCD,0xAB,0xCF,0x8E,0xAD,0xF9,0x32,0xC9,0x15,0x9F,0xD6,0x6D,0xA8,0xAE,0xB1,0x3F,
444 0x90,0xEB,0xD4,0xF9,0x31,0x81,0xA3,0x53,0x99,0x4B,0x3C,0x93,0x3B,0xFE,0x55,0xFF,0x25,0x9F,0xCC,0x07,0xC5,0x2C,0x14,0xA7,0xA4,0x1E,0x6C,0xB6,0x91,0x2A,0xE0,0x3E,
445 0x7F,0x39,0x0A,0xD9,0x24,0x3C,0x01,0xA0,0x30,0x99,0x8E,0xB8,0x1D,0xF9,0xA7,0x78,0x86,0x95,0x35,0x0E,0x21,0xDA,0x7A,0x7B,0xAD,0x9F,0x4E,0xF6,0x63,0x5B,0x96,0xBB,
446 0x87,0x36,0x3F,0xA7,0x1A,0x66,0x91,0xCD,0xB0,0x3B,0xC0,0x4F,0x54,0xD2,0x5F,0xBB,0x38,0x89,0x1C,0x79,0x7E,0xA2,0x02,0xE4,0x80,0x84,0x1E,0x33,0xAB,0x74,0xFA,0xBE,
447 0x31,0x46,0x2E,0xC5,0x15,0xB9,0x12,0xE9,0xD3,0x73,0x43,0xEA,0x74,0x11,0xA7,0xC0,0xD5,0xD8,0x39,0x08,0x9F,0x4F,0xC7,0x71,0x25,0x09,0x51,0x65,0xD6,0xA8,0x02,0x1F
450 static int hash21i( v2i p
, u32 umod
)
452 return random_noise
[ (random_noise
[p
[1] & 1023] + p
[0]) & 1023 ] & umod
;
455 static void map_reclassify( v2i start
, v2i end
, int update_texbuffer
);
456 static int map_load( const char *str
, const char *name
)
458 //TODO: It may be worthwhile, at this point, to switch to binary encoding for save data
467 if( c
[world
.w
] == ';' )
469 else if( !c
[world
.w
] )
471 vg_error( "Unexpected EOF when parsing level\n" );
476 struct cell
*row
= arraddnptr( world
.data
, world
.w
);
478 int reg_start
= 0, reg_end
= 0;
480 u32
*links_to_make
= NULL
;
481 int links_satisfied
= 0;
483 char link_id_buffer
[32];
491 if( *c
== '\r' ) { c
++; continue; } // fuck off windows
497 if( *c
== '\r' ) c
++;
504 if( *c
== '\r' ) { c
++; continue; }
506 if( reg_start
< reg_end
)
508 struct cell_terminal
*terminal
= &world
.io
[ reg_start
];
509 struct terminal_run
*run
= &terminal
->runs
[ terminal
->run_count
-1 ];
511 if( *c
>= 'a' && *c
<= 'z' )
513 run
->conditions
[ run
->condition_count
++ ] = *c
;
517 if( *c
== ',' || *c
== '\n' )
526 terminal
->runs
[ terminal
->run_count
].condition_count
= 0;
527 terminal
->run_count
++;
528 world
.max_runs
= vg_max( world
.max_runs
, terminal
->run_count
);
532 vg_error( "Unkown attribute '%c' (row: %u)\n", *c
, world
.h
);
539 if( links_satisfied
< arrlen( links_to_make
) )
541 struct cell
*target
= &world
.data
[ links_to_make
[ links_satisfied
] ];
543 if( (((u32
)*c
>= (u32
)'0') && ((u32
)*c
<= (u32
)'9')) || *c
== '-' )
545 if( link_id_n
>= vg_list_size( link_id_buffer
)-1 )
547 vg_error( "Number was way too long to be parsed (row: %u)\n", world
.h
);
551 link_id_buffer
[ link_id_n
++ ] = *c
;
553 else if( *c
== ',' || *c
== '\n' )
555 link_id_buffer
[ link_id_n
] = 0x00;
556 int value
= atoi( link_id_buffer
);
558 target
->links
[value
>= 0? 1:0] = abs(value
);
567 vg_error( "Invalid character '%c' (row: %u)\n", *c
, world
.h
);
573 vg_error( "Too many values to assign (row: %u)\n", world
.h
);
582 // Registry length-error checks
583 if( reg_start
!= reg_end
)
585 vg_error( "Not enough spawn values assigned (row: %u, %u of %u)\n", world
.h
, reg_start
, reg_end
);
589 if( links_satisfied
!= arrlen( links_to_make
) )
591 vg_error( "Not enough link values assigned (row: %u, %u of %u)\n", world
.h
, links_satisfied
, arrlen( links_to_make
) );
597 vg_error( "Not enough cells to match previous row definition (row: %u, %u<%u)\n", world
.h
, cx
, world
.w
);
601 row
= arraddnptr( world
.data
, world
.w
);
604 reg_end
= reg_start
= arrlen( world
.io
);
606 arrsetlen( links_to_make
, 0 );
613 vg_error( "Too many cells to match previous row definition (row: %u, %u>%u)\n", world
.h
, cx
, world
.w
);
617 // Tile initialization
619 struct cell
*cell
= &row
[ cx
];
622 if( *c
== '+' || *c
== '-' )
624 struct cell_terminal
*term
= arraddnptr( world
.io
, 1 );
625 term
->id
= cx
+ world
.h
*world
.w
;
627 term
->runs
[0].condition_count
= 0;
629 cell
->state
= *c
== '+'? FLAG_INPUT
: FLAG_OUTPUT
;
632 else if( *c
== '#' ) cell
->state
= FLAG_WALL
;
633 else if( ((u32
)*c
>= (u32
)'A') && ((u32
)*c
<= (u32
)'A'+0xf) )
635 // Canal flag bits (4bit/16 value):
641 cell
->state
= ((u32
)*c
- (u32
)'A') & (FLAG_CANAL
|FLAG_IS_TRIGGER
);
643 if( cell
->state
& FLAG_IS_TRIGGER
)
644 arrpush( links_to_make
, cx
+ world
.h
*world
.w
);
650 else cell
->state
= 0x00;
658 // Update data texture to fill out the background
660 u8 info_buffer
[64*64*4];
661 for( int x
= 0; x
< 64; x
++ )
663 for( int y
= 0; y
< 64; y
++ )
665 u8
*px
= &info_buffer
[((x
*64)+y
)*4];
667 px
[0] = 0xFF-0x3F + hash21i( (v2i
){x
,y
}, 0x3F );
674 // Random walks.. kinda
675 for( int i
= 0; i
< arrlen(world
.io
); i
++ )
677 struct cell_terminal
*term
= &world
.io
[ i
];
678 int posx
= term
->id
% world
.w
;
679 int posy
= (term
->id
- posx
)/world
.w
;
689 turtle_dir
[1] = posy
<= 4? -1: 1;
690 original_y
= turtle_dir
[1];
692 for( int i
= 0; i
< 100; i
++ )
694 info_buffer
[((turtle
[1]*64)+turtle
[0])*4] = 0;
696 v2i_add( turtle_dir
, turtle
, turtle
);
698 if( turtle
[0] == 0 ) break;
699 if( turtle
[0] == 63 ) break;
700 if( turtle
[1] == 0 ) break;
701 if( turtle
[1] == 63 ) break;
703 int random_value
= hash21i( turtle
, 0xFF );
704 if( random_value
> 255-40 && !turtle_dir
[0] )
709 else if( random_value
> 255-80 && !turtle_dir
[0] )
714 else if( random_value
> 255-100 )
717 turtle_dir
[1] = original_y
;
722 glBindTexture( GL_TEXTURE_2D
, world
.background_data
);
723 glTexSubImage2D( GL_TEXTURE_2D
, 0, 0, 0, 64, 64, GL_RGBA
, GL_UNSIGNED_BYTE
, info_buffer
);
726 arrfree( links_to_make
);
728 map_reclassify( NULL
, NULL
, 1 );
731 for( int i
= 0; i
< world
.h
*world
.w
; i
++ )
733 struct cell
*src
= &world
.data
[i
];
734 if( src
->state
& FLAG_IS_TRIGGER
)
736 int link_id
= src
->links
[0]?0:1;
737 if( src
->links
[link_id
] <= world
.h
*world
.w
)
739 struct cell
*target
= &world
.data
[ src
->links
[link_id
] ];
740 if( (target
->state
& FLAG_CANAL
) && (target
->config
== k_cell_type_split
) )
742 if( target
->links
[ link_id
] )
744 vg_error( "Link target was already targeted\n" );
750 target
->links
[ link_id
] = i
;
751 target
->state
|= FLAG_TARGETED
;
756 vg_error( "Link target was invalid\n" );
762 vg_error( "Link target out of bounds\n" );
768 vg_success( "Map '%s' loaded! (%u:%u)\n", name
, world
.w
, world
.h
);
772 strncpy( world
.map_name
, name
, vg_list_size( world
.map_name
)-1 );
773 world
.initialzed
= 1;
777 arrfree( links_to_make
);
782 static struct cell
*pcell( v2i pos
)
784 return &world
.data
[ pos
[1]*world
.w
+ pos
[0] ];
787 static void map_serialize( FILE *stream
)
789 for( int y
= 0; y
< world
.h
; y
++ )
791 for( int x
= 0; x
< world
.w
; x
++ )
793 struct cell
*cell
= pcell( (v2i
){ x
, y
} );
795 if( cell
->state
& FLAG_WALL
) fputc( '#', stream
);
796 else if( cell
->state
& FLAG_INPUT
) fputc( '+', stream
);
797 else if( cell
->state
& FLAG_OUTPUT
) fputc( '-', stream
);
798 else if( cell
->state
& (FLAG_CANAL
|FLAG_IS_TRIGGER
|FLAG_RESERVED0
|FLAG_RESERVED1
) )
800 fputc( (cell
->state
& (FLAG_CANAL
|FLAG_IS_TRIGGER
|FLAG_RESERVED0
|FLAG_RESERVED1
)) + (u32
)'A', stream
);
802 else fputc( ' ', stream
);
805 fputc( ';', stream
);
807 int terminal_write_count
= 0;
809 for( int x
= 0; x
< world
.w
; x
++ )
811 for( int i
= 0; i
< arrlen( world
.io
); i
++ )
813 struct cell_terminal
*term
= &world
.io
[i
];
814 if( term
->id
== y
*world
.w
+x
)
816 if( terminal_write_count
)
817 fputc( ',', stream
);
818 terminal_write_count
++;
820 for( int j
= 0; j
< term
->run_count
; j
++ )
822 struct terminal_run
*run
= &term
->runs
[j
];
824 for( int k
= 0; k
< run
->condition_count
; k
++ )
825 fputc( run
->conditions
[k
], stream
);
827 if( j
< term
->run_count
-1 )
828 fputc( ':', stream
);
834 for( int x
= 0; x
< world
.w
; x
++ )
836 struct cell
*cell
= pcell( (v2i
){ x
,y
} );
837 if( cell
->state
& FLAG_IS_TRIGGER
)
839 if( terminal_write_count
)
840 fputc( ',', stream
);
841 terminal_write_count
++;
843 fprintf( stream
, "%d", cell
->links
[0]? -cell
->links
[0]: cell
->links
[1] );
847 fputc( '\n', stream
);
851 int main( int argc
, char *argv
[] )
853 vg_init( argc
, argv
, "Marble Computing | SPACE: Test | LeftClick: Toggle tile | RightClick: Drag wire" );
857 static int console_credits( int argc
, char const *argv
[] )
859 vg_info( "Aknowledgements:\n" );
860 vg_info( " GLFW zlib/libpng glfw.org\n" );
861 vg_info( " miniaudio MIT0 miniaud.io\n" );
862 vg_info( " QOI MIT phoboslab.org\n" );
863 vg_info( " STB library MIT nothings.org\n" );
864 vg_info( " Weiholmir font justfredrik.itch.io\n" );
868 static int console_save_map( int argc
, char const *argv
[] )
870 if( !world
.initialzed
)
872 vg_error( "Tried to save uninitialized map!\n" );
876 char map_path
[ 256 ];
878 strcpy( map_path
, "sav/" );
879 strcat( map_path
, world
.map_name
);
880 strcat( map_path
, ".map" );
882 FILE *test_writer
= fopen( map_path
, "wb" );
885 vg_info( "Saving map to '%s'\n", map_path
);
886 map_serialize( test_writer
);
888 fclose( test_writer
);
893 vg_error( "Unable to open stream for writing\n" );
898 static int console_load_map( int argc
, char const *argv
[] )
900 char map_path
[ 256 ];
905 strcpy( map_path
, "sav/" );
906 strcat( map_path
, argv
[0] );
907 strcat( map_path
, ".map" );
909 char *text_source
= vg_textasset_read( map_path
);
913 strcpy( map_path
, "maps/" );
914 strcat( map_path
, argv
[0] );
915 strcat( map_path
, ".map" );
917 text_source
= vg_textasset_read( map_path
);
922 vg_info( "Loading map: '%s'\n", map_path
);
923 world
.pCmpLevel
= NULL
;
925 if( !map_load( text_source
, argv
[0] ) )
936 vg_error( "Missing maps '%s'\n", argv
[0] );
942 vg_error( "Missing argument <map_path>\n" );
947 static void simulation_stop(void)
949 world
.simulating
= 0;
950 world
.num_fishes
= 0;
956 sfx_system_fadeout( &audio_system_balls_rolling
, 44100 );
958 vg_info( "Stopping simulation!\n" );
961 static int console_changelevel( int argc
, char const *argv
[] )
965 // Save current level
966 console_save_map( 0, NULL
);
967 if( console_load_map( argc
, argv
) )
975 vg_error( "Missing argument <map_path>\n" );
981 void leaderboard_found( LeaderboardFindResult_t
*pCallback
);
982 void leaderboard_downloaded( LeaderboardScoresDownloaded_t
*pCallback
);
986 // Steamworks callbacks
987 sw_leaderboard_found
= &leaderboard_found
;
988 sw_leaderboard_downloaded
= &leaderboard_downloaded
;
990 vg_function_push( (struct vg_cmd
){
991 .name
= "_map_write",
992 .function
= console_save_map
995 vg_function_push( (struct vg_cmd
){
997 .function
= console_load_map
1000 vg_function_push( (struct vg_cmd
){
1002 .function
= console_changelevel
1005 vg_function_push( (struct vg_cmd
){
1007 .function
= console_credits
1014 0.0f
, 0.0f
, 0.0f
, 1.0f
, 1.0f
, 1.0f
,
1015 0.0f
, 0.0f
, 1.0f
, 1.0f
, 1.0f
, 0.0f
,
1017 0.0f
, 0.0f
, 0.0f
, 1.0f
, 4.0f
, 1.0f
,
1018 0.0f
, 0.0f
, 4.0f
, 1.0f
, 4.0f
, 0.0f
1021 init_mesh( &world
.tile
, quad_mesh
, vg_list_size(quad_mesh
) );
1026 float circle_mesh
[32*6*3];
1027 int res
= vg_list_size( circle_mesh
) / (6*3);
1029 for( int i
= 0; i
< res
; i
++ )
1031 v2f v0
= { sinf( ((float)i
/(float)res
)*VG_TAUf
), cosf( ((float)i
/(float)res
)*VG_TAUf
) };
1032 v2f v1
= { sinf( ((float)(i
+1)/(float)res
)*VG_TAUf
), cosf( ((float)(i
+1)/(float)res
)*VG_TAUf
) };
1034 circle_mesh
[ i
*6+0 ] = 0.0f
;
1035 circle_mesh
[ i
*6+1 ] = 0.0f
;
1037 v2_copy( v0
, circle_mesh
+ 32*6 + i
*12 );
1038 v2_muls( v0
, 0.8f
, circle_mesh
+ 32*6 + i
*12+2 );
1039 v2_copy( v1
, circle_mesh
+ 32*6 + i
*12+4 );
1041 v2_copy( v1
, circle_mesh
+ 32*6 + i
*12+6 );
1042 v2_muls( v1
, 0.8f
, circle_mesh
+ 32*6 + i
*12+8 );
1043 v2_muls( v0
, 0.8f
, circle_mesh
+ 32*6 + i
*12+10 );
1045 v2_copy( v0
, circle_mesh
+ i
*6+4 );
1046 v2_copy( v1
, circle_mesh
+ i
*6+2 );
1047 v2_copy( v0
, circle_mesh
+i
*6+4 );
1048 v2_copy( v1
, circle_mesh
+i
*6+2 );
1051 init_mesh( &world
.circle
, circle_mesh
, vg_list_size( circle_mesh
) );
1056 init_mesh( &world
.numbers
,
1057 MESH_NUMBERS_BUFFER
,
1058 vg_list_size( MESH_NUMBERS_BUFFER
)
1061 for( int i
= 0; i
< 10; i
++ )
1063 vg_info( "offset: %u, length: %u\n", MESH_NUMBERS_OFFSETS
[i
][0], MESH_NUMBERS_OFFSETS
[i
][1] );
1069 int const num_segments
= 64;
1071 struct mesh_wire
*mw
= &world
.wire
;
1073 v2f wire_points
[ num_segments
* 2 ];
1074 u16 wire_indices
[ 6*(num_segments
-1) ];
1076 for( int i
= 0; i
< num_segments
; i
++ )
1078 float l
= (float)i
/ (float)(num_segments
-1);
1080 v2_copy( (v2f
){ l
, -0.5f
}, wire_points
[i
*2+0] );
1081 v2_copy( (v2f
){ l
, 0.5f
}, wire_points
[i
*2+1] );
1083 if( i
< num_segments
-1 )
1085 wire_indices
[ i
*6+0 ] = i
*2 + 0;
1086 wire_indices
[ i
*6+1 ] = i
*2 + 1;
1087 wire_indices
[ i
*6+2 ] = i
*2 + 3;
1088 wire_indices
[ i
*6+3 ] = i
*2 + 0;
1089 wire_indices
[ i
*6+4 ] = i
*2 + 3;
1090 wire_indices
[ i
*6+5 ] = i
*2 + 2;
1094 glGenVertexArrays( 1, &mw
->vao
);
1095 glGenBuffers( 1, &mw
->vbo
);
1096 glGenBuffers( 1, &mw
->ebo
);
1097 glBindVertexArray( mw
->vao
);
1099 glBindBuffer( GL_ARRAY_BUFFER
, mw
->vbo
);
1101 glBufferData( GL_ARRAY_BUFFER
, sizeof( wire_points
), wire_points
, GL_STATIC_DRAW
);
1102 glBindVertexArray( mw
->vao
);
1104 glBindBuffer( GL_ELEMENT_ARRAY_BUFFER
, mw
->ebo
);
1105 glBufferData( GL_ELEMENT_ARRAY_BUFFER
, sizeof( wire_indices
), wire_indices
, GL_STATIC_DRAW
);
1108 glVertexAttribPointer( 0, 2, GL_FLOAT
, GL_FALSE
, 2*sizeof(float), (void*)0 );
1109 glEnableVertexAttribArray( 0 );
1113 mw
->em
= vg_list_size( wire_indices
);
1116 // Create info data texture
1118 glGenTextures( 1, &world
.background_data
);
1119 glBindTexture( GL_TEXTURE_2D
, world
.background_data
);
1120 glTexImage2D( GL_TEXTURE_2D
, 0, GL_RGBA
, 64, 64, 0, GL_RGBA
, GL_UNSIGNED_BYTE
, NULL
);
1124 // Create random smaples texture
1126 u8
*data
= malloc(512*512*2);
1127 for( int i
= 0; i
< 512*512*2; i
++ )
1128 data
[ i
] = rand()/(RAND_MAX
/255);
1130 glGenTextures( 1, &world
.random_samples
);
1131 glBindTexture( GL_TEXTURE_2D
, world
.random_samples
);
1132 glTexImage2D( GL_TEXTURE_2D
, 0, GL_RG
, 512, 512, 0, GL_RG
, GL_UNSIGNED_BYTE
, data
);
1139 resource_load_main();
1141 // Restore gamestate
1143 console_load_map( 1, level_pack_1
);
1149 console_save_map( 0, NULL
);
1152 resource_free_main();
1154 glDeleteTextures( 1, &world
.background_data
);
1155 glDeleteTextures( 1, &world
.random_samples
);
1157 glDeleteVertexArrays( 1, &world
.wire
.vao
);
1158 glDeleteBuffers( 1, &world
.wire
.vbo
);
1159 glDeleteBuffers( 1, &world
.wire
.ebo
);
1161 free_mesh( &world
.tile
);
1162 free_mesh( &world
.circle
);
1163 free_mesh( &world
.numbers
);
1168 static int world_check_pos_ok( v2i co
)
1170 return (co
[0] < 2 || co
[0] >= world
.w
-2 || co
[1] < 2 || co
[1] >= world
.h
-2)? 0: 1;
1173 static int cell_interactive( v2i co
)
1176 if( !world_check_pos_ok( co
) )
1180 if( world
.data
[ world
.w
*co
[1] + co
[0] ].state
& (FLAG_WALL
|FLAG_INPUT
|FLAG_OUTPUT
) )
1183 // List of 3x3 configurations that we do not allow
1184 static u32 invalid_src
[][9] =
1216 // Statically compile invalid configurations into bitmasks
1217 static u32 invalid
[ vg_list_size(invalid_src
) ];
1219 for( int i
= 0; i
< vg_list_size(invalid_src
); i
++ )
1223 for( int j
= 0; j
< 3; j
++ )
1224 for( int k
= 0; k
< 3; k
++ )
1225 comped
|= invalid_src
[i
][ j
*3+k
] << ((j
*5)+k
);
1227 invalid
[i
] = comped
;
1230 // Extract 5x5 grid surrounding tile
1232 for( int y
= co
[1]-2; y
< co
[1]+3; y
++ )
1233 for( int x
= co
[0]-2; x
< co
[0]+3; x
++ )
1235 struct cell
*cell
= pcell((v2i
){x
,y
});
1237 if( cell
&& (cell
->state
& (FLAG_CANAL
|FLAG_INPUT
|FLAG_OUTPUT
)) )
1238 blob
|= 0x1 << ((y
-(co
[1]-2))*5 + x
-(co
[0]-2));
1241 // Run filter over center 3x3 grid to check for invalid configurations
1242 int kernel
[] = { 0, 1, 2, 5, 6, 7, 10, 11, 12 };
1243 for( int i
= 0; i
< vg_list_size(kernel
); i
++ )
1245 if( blob
& (0x1 << (6+kernel
[i
])) )
1247 u32 window
= blob
>> kernel
[i
];
1249 for( int j
= 0; j
< vg_list_size(invalid
); j
++ )
1250 if((window
& invalid
[j
]) == invalid
[j
])
1258 static void map_reclassify( v2i start
, v2i end
, int update_texbuffer
)
1260 v2i full_start
= { 1,1 };
1261 v2i full_end
= { world
.w
-1, world
.h
-1 };
1263 if( !start
|| !end
)
1270 u8 info_buffer
[64*64*4];
1273 int px0
= vg_max( start
[0], full_start
[0] ),
1274 px1
= vg_min( end
[0], full_end
[0] ),
1275 py0
= vg_max( start
[1], full_start
[1] ),
1276 py1
= vg_min( end
[1], full_end
[1] );
1278 for( int y
= py0
; y
< py1
; y
++ )
1280 for( int x
= px0
; x
< px1
; x
++ )
1282 struct cell
*cell
= pcell((v2i
){x
,y
});
1284 v2i dirs
[] = {{1,0},{0,1},{-1,0},{0,-1}};
1289 if( cell
->state
& (FLAG_CANAL
|FLAG_INPUT
|FLAG_OUTPUT
) )
1291 for( int i
= 0; i
< vg_list_size( dirs
); i
++ )
1293 struct cell
*neighbour
= pcell((v2i
){x
+dirs
[i
][0], y
+dirs
[i
][1]});
1294 if( neighbour
->state
& (FLAG_CANAL
|FLAG_INPUT
|FLAG_OUTPUT
) )
1302 if( cell
->state
& FLAG_WALL
)
1303 height
= 0xFF-0x3F + hash21i( (v2i
){x
,y
}, 0x3F );
1308 pcell((v2i
){x
,y
})->config
= config
;
1310 u8
*info_px
= &info_buffer
[ (pixel_id
++)*4 ];
1311 info_px
[0] = height
;
1312 info_px
[1] = cell
->state
& FLAG_WALL
? 0: 255;
1318 ((cell
->state
& FLAG_IS_TRIGGER
) && (cell
->config
== 0xF || cell
->config
== k_cell_type_split
)) ||
1319 ((cell
->state
& FLAG_TARGETED
) && (cell
->config
!= k_cell_type_split
))
1320 ) && update_texbuffer
1322 cell
->state
&= ~(FLAG_TARGETED
|FLAG_IS_TRIGGER
);
1323 for( u32 i
= 0; i
< 2; i
++ )
1325 if( cell
->links
[i
] )
1327 struct cell
*other_ptr
= &world
.data
[ cell
->links
[i
] ];
1328 other_ptr
->links
[ i
] = 0;
1329 other_ptr
->state
&= ~FLAG_IS_TRIGGER
;
1331 if( other_ptr
->links
[ i
^ 0x1 ] == 0 )
1332 other_ptr
->state
&= ~FLAG_TARGETED
;
1342 if( update_texbuffer
)
1344 glBindTexture( GL_TEXTURE_2D
, world
.background_data
);
1345 glTexSubImage2D( GL_TEXTURE_2D
, 0, px0
+ 16, py0
+ 16, px1
-px0
, py1
-py0
, GL_RGBA
, GL_UNSIGNED_BYTE
, info_buffer
);
1349 u16 id_drag_from
= 0;
1353 void vg_update(void)
1355 // Fit within screen
1357 float r1
= (float)vg_window_y
/ (float)vg_window_x
,
1358 r2
= (float)world
.h
/ (float)world
.w
,
1361 size
= ( r2
< r1
? (float)world
.w
* 0.5f
: ((float)world
.h
* 0.5f
) / r1
) + 2.5f
;
1362 m3x3_projection( m_projection
, -size
, size
, -size
*r1
, size
*r1
);
1365 origin
[0] = floorf( -0.5f
* world
.w
);
1366 origin
[1] = floorf( -0.5f
* world
.h
);
1369 m3x3_identity( m_view
);
1370 m3x3_translate( m_view
, origin
);
1371 m3x3_mul( m_projection
, m_view
, vg_pv
);
1372 vg_projection_update();
1375 v2_copy( vg_mouse_ws
, world
.tile_pos
);
1377 world
.tile_x
= floorf( world
.tile_pos
[0] );
1378 world
.tile_y
= floorf( world
.tile_pos
[1] );
1381 if( !world
.simulating
&& !gui_want_mouse() )
1383 v2_copy( vg_mouse_ws
, drag_to_co
);
1385 if( cell_interactive( (v2i
){ world
.tile_x
, world
.tile_y
} ))
1387 world
.selected
= world
.tile_y
* world
.w
+ world
.tile_x
;
1389 static u32 modify_state
= 0;
1391 struct cell
*cell_ptr
= &world
.data
[world
.selected
];
1393 if( vg_get_button_down("primary") )
1395 modify_state
= (cell_ptr
->state
& FLAG_CANAL
) ^ FLAG_CANAL
;
1398 if( vg_get_button("primary") && ((cell_ptr
->state
& FLAG_CANAL
) != modify_state
) )
1400 cell_ptr
->state
&= ~FLAG_CANAL
;
1401 cell_ptr
->state
|= modify_state
;
1403 if( cell_ptr
->state
& FLAG_CANAL
)
1405 cell_ptr
->links
[0] = 0;
1406 cell_ptr
->links
[1] = 0;
1408 sfx_set_playrnd( &audio_tile_mod
, &audio_system_sfx
, 3, 6 );
1413 sfx_set_playrnd( &audio_tile_mod
, &audio_system_sfx
, 0, 3 );
1417 map_reclassify( (v2i
){ world
.tile_x
-2, world
.tile_y
-2 },
1418 (v2i
){ world
.tile_x
+2, world
.tile_y
+2 }, 1 );
1421 if( vg_get_button_down("secondary") && !(cell_ptr
->config
== k_cell_type_split
) )
1423 id_drag_from
= world
.selected
;
1424 drag_from_co
[0] = world
.tile_x
+ 0.5f
;
1425 drag_from_co
[1] = world
.tile_y
+ 0.5f
;
1428 if( id_drag_from
&& (cell_ptr
->config
== k_cell_type_split
) )
1430 float local_x
= vg_mouse_ws
[0] - (float)world
.tile_x
;
1431 drag_to_co
[0] = (float)world
.tile_x
+ (local_x
> 0.5f
? 0.75f
: 0.25f
);
1432 drag_to_co
[1] = (float)world
.tile_y
+ 0.25f
;
1434 if( vg_get_button_up("secondary") )
1436 struct cell
*drag_ptr
= &world
.data
[id_drag_from
];
1437 u32 link_id
= local_x
> 0.5f
? 1: 0;
1439 // Cleanup existing connections
1440 if( cell_ptr
->links
[ link_id
] )
1442 vg_warn( "Destroying existing connection on link %u (%hu)\n", link_id
, cell_ptr
->links
[ link_id
] );
1444 struct cell
*current_connection
= &world
.data
[ cell_ptr
->links
[ link_id
]];
1445 current_connection
->state
&= ~FLAG_IS_TRIGGER
;
1446 current_connection
->links
[ link_id
] = 0;
1449 if( drag_ptr
->links
[ link_id
^ 0x1 ] )
1451 vg_warn( "Destroying alternate link %u (%hu)\n", link_id
^ 0x1, drag_ptr
->links
[ link_id
^ 0x1 ] );
1453 struct cell
*current_connection
= &world
.data
[ drag_ptr
->links
[ link_id
^ 0x1 ]];
1454 if( !current_connection
->links
[ link_id
] )
1455 current_connection
->state
&= ~FLAG_TARGETED
;
1457 current_connection
->links
[ link_id
^ 0x1 ] = 0;
1458 drag_ptr
->links
[ link_id
^ 0x1 ] = 0;
1461 // Create the new connection
1462 vg_success( "Creating connection on link %u (%hu)\n", link_id
, id_drag_from
);
1464 cell_ptr
->links
[ link_id
] = id_drag_from
;
1465 drag_ptr
->links
[ link_id
] = world
.selected
;
1467 cell_ptr
->state
|= FLAG_TARGETED
;
1468 drag_ptr
->state
|= FLAG_IS_TRIGGER
;
1474 world
.selected
= -1;
1476 if( !(vg_get_button("secondary") && id_drag_from
) )
1481 world
.selected
= -1;
1485 // Simulation stop/start
1486 if( vg_get_button_down("go") )
1488 if( world
.simulating
)
1494 vg_success( "Starting simulation!\n" );
1496 sfx_set_playrnd( &audio_rolls
, &audio_system_balls_rolling
, 0, 1 );
1498 world
.simulating
= 1;
1499 world
.num_fishes
= 0;
1500 world
.sim_frame
= 0;
1501 world
.sim_start
= vg_time
;
1503 world
.sim_speed
= 2.5f
;
1505 for( int i
= 0; i
< world
.w
*world
.h
; i
++ )
1506 world
.data
[ i
].state
&= ~FLAG_FLIP_FLOP
;
1513 if( world
.simulating
)
1515 while( world
.sim_frame
< (int)((vg_time
-world
.sim_start
)*world
.sim_speed
) )
1517 //vg_info( "frame: %u\n", world.sim_frame );
1518 sfx_set_playrnd( &audio_random
, &audio_system_balls_switching
, 0, 9 );
1520 // Update splitter deltas
1521 for( int i
= 0; i
< world
.h
*world
.w
; i
++ )
1523 struct cell
*cell
= &world
.data
[i
];
1524 if( cell
->config
== k_cell_type_split
)
1526 cell
->state
&= ~FLAG_FLIP_ROTATING
;
1528 if( cell
->state
& FLAG_IS_TRIGGER
)
1529 cell
->state
&= ~FLAG_TRIGGERED
;
1532 int alive_count
= 0;
1534 // Update fish positions
1535 for( int i
= 0; i
< world
.num_fishes
; i
++ )
1537 struct fish
*fish
= &world
.fishes
[i
];
1539 if( fish
->state
== k_fish_state_soon_dead
)
1540 fish
->state
= k_fish_state_dead
;
1542 if( fish
->state
== k_fish_state_soon_alive
)
1543 fish
->state
= k_fish_state_alive
;
1545 if( fish
->state
< k_fish_state_alive
)
1548 struct cell
*cell_current
= pcell( fish
->pos
);
1550 if( fish
->state
== k_fish_state_alive
)
1553 if( cell_current
->state
& FLAG_OUTPUT
)
1555 for( int j
= 0; j
< arrlen( world
.io
); j
++ )
1557 struct cell_terminal
*term
= &world
.io
[j
];
1559 if( term
->id
== fish
->pos
[1]*world
.w
+ fish
->pos
[0] )
1561 struct terminal_run
*run
= &term
->runs
[ world
.sim_run
];
1562 if( run
->recv_count
< vg_list_size( run
->recieved
) )
1563 run
->recieved
[ run
->recv_count
++ ] = fish
->payload
;
1569 fish
->state
= k_fish_state_dead
;
1574 if( cell_current
->config
== k_cell_type_merge
)
1579 fish
->flow_reversed
= 0;
1583 if( cell_current
->config
== k_cell_type_split
)
1586 fish
->dir
[0] = cell_current
->state
&FLAG_FLIP_FLOP
?1:-1;
1589 if( !(cell_current
->state
& FLAG_TARGETED
) )
1590 cell_current
->state
^= FLAG_FLIP_FLOP
;
1594 // Apply cell out-flow
1595 struct cell_description
*desc
= &cell_descriptions
[ cell_current
->config
];
1597 v2i_copy( fish
->flow_reversed
? desc
->start
: desc
->end
, fish
->dir
);
1601 v2i_add( fish
->pos
, fish
->dir
, pos_next
);
1603 struct cell
*cell_next
= pcell( pos_next
);
1605 if( cell_next
->state
& (FLAG_CANAL
|FLAG_OUTPUT
) )
1607 struct cell_description
*desc
= &cell_descriptions
[ cell_next
->config
];
1609 if( cell_next
->config
== k_cell_type_merge
)
1611 if( fish
->dir
[0] == 0 )
1612 fish
->state
= k_fish_state_dead
;
1614 fish
->flow_reversed
= 0;
1618 if( cell_next
->config
== k_cell_type_split
)
1620 if( fish
->dir
[0] == 0 )
1622 sfx_set_playrnd( &audio_splitter
, &audio_system_balls_important
, 0, 1 );
1623 cell_next
->state
|= FLAG_FLIP_ROTATING
;
1625 fish
->flow_reversed
= 0;
1628 fish
->state
= k_fish_state_dead
;
1631 fish
->flow_reversed
= ( fish
->dir
[0] != -desc
->start
[0] ||
1632 fish
->dir
[1] != -desc
->start
[1] )? 1: 0;
1636 fish
->state
= world_check_pos_ok( fish
->pos
)? k_fish_state_bg
: k_fish_state_dead
;
1639 //v2i_add( fish->pos, fish->dir, fish->pos );
1641 else if( fish
->state
== k_fish_state_bg
)
1643 v2i_add( fish
->pos
, fish
->dir
, fish
->pos
);
1645 if( !world_check_pos_ok( fish
->pos
) )
1646 fish
->state
= k_fish_state_dead
;
1649 struct cell
*cell_entry
= pcell( fish
->pos
);
1651 if( cell_entry
->state
& FLAG_CANAL
)
1653 if( cell_entry
->config
== k_cell_type_con_r
|| cell_entry
->config
== k_cell_type_con_u
1654 || cell_entry
->config
== k_cell_type_con_l
|| cell_entry
->config
== k_cell_type_con_d
)
1656 sw_set_achievement( "CAN_DO_THAT" );
1658 fish
->state
= k_fish_state_soon_alive
;
1662 fish
->flow_reversed
= 1;
1664 switch( cell_entry
->config
)
1666 case k_cell_type_con_r
: fish
->dir
[0] = 1; break;
1667 case k_cell_type_con_l
: fish
->dir
[0] = -1; break;
1668 case k_cell_type_con_u
: fish
->dir
[1] = 1; break;
1669 case k_cell_type_con_d
: fish
->dir
[1] = -1; break;
1675 else { vg_error( "fish behaviour unimplemented for behaviour type (%d)\n" ); }
1677 if( fish
->state
>= k_fish_state_alive
)
1681 // Second pass (triggers)
1682 for( int i
= 0; i
< world
.num_fishes
; i
++ )
1684 struct fish
*fish
= &world
.fishes
[i
];
1686 if( fish
->state
== k_fish_state_alive
)
1688 v2i_add( fish
->pos
, fish
->dir
, fish
->pos
);
1689 struct cell
*cell_current
= pcell( fish
->pos
);
1691 if( cell_current
->state
& FLAG_IS_TRIGGER
)
1693 int trigger_id
= cell_current
->links
[0]?0:1;
1694 int connection_id
= cell_current
->links
[trigger_id
];
1695 int target_px
= connection_id
% world
.w
;
1696 int target_py
= (connection_id
- target_px
)/world
.w
;
1698 vg_line2( (v2f
){ fish
->pos
[0], fish
->pos
[1] }, (v2f
){ target_px
, target_py
}, 0xffffffff, 0xffffffff );
1700 struct cell
*target_peice
= &world
.data
[ cell_current
->links
[trigger_id
] ];
1702 cell_current
->state
|= FLAG_TRIGGERED
;
1705 target_peice
->state
|= FLAG_FLIP_FLOP
;
1707 target_peice
->state
&= ~FLAG_FLIP_FLOP
;
1712 // Third pass (collisions)
1713 struct fish
*fi
, *fj
;
1715 for( int i
= 0; i
< world
.num_fishes
; i
++ )
1717 fi
= &world
.fishes
[i
];
1719 if( fi
->state
== k_fish_state_alive
)
1721 int continue_again
= 0;
1723 for( int j
= i
+1; j
< world
.num_fishes
; j
++ )
1725 fj
= &world
.fishes
[j
];
1727 if( (fj
->state
== k_fish_state_alive
) )
1732 v2i_sub( fi
->pos
, fi
->dir
, fi_prev
);
1733 v2i_sub( fj
->pos
, fj
->dir
, fj_prev
);
1736 collide_next_frame
= (
1737 (fi
->pos
[0] == fj
->pos
[0]) &&
1738 (fi
->pos
[1] == fj
->pos
[1]))? 1: 0,
1739 collide_this_frame
= (
1740 (fi_prev
[0] == fj
->pos
[0]) &&
1741 (fi_prev
[1] == fj
->pos
[1]) &&
1742 (fj_prev
[0] == fi
->pos
[0]) &&
1743 (fj_prev
[1] == fi
->pos
[1])
1746 if( collide_next_frame
|| collide_this_frame
)
1748 sw_set_achievement( "BANG" );
1750 // Shatter death (+0.5s)
1751 float death_time
= collide_this_frame
? 0.0f
: 0.5f
;
1753 fi
->state
= k_fish_state_soon_dead
;
1754 fj
->state
= k_fish_state_soon_dead
;
1755 fi
->death_time
= death_time
;
1756 fj
->death_time
= death_time
;
1763 if( continue_again
)
1769 for( int i
= 0; i
< arrlen( world
.io
); i
++ )
1771 struct cell_terminal
*term
= &world
.io
[ i
];
1772 int posx
= term
->id
% world
.w
;
1773 int posy
= (term
->id
- posx
)/world
.w
;
1774 int is_input
= world
.data
[ term
->id
].state
& FLAG_INPUT
;
1778 if( world
.sim_frame
< term
->runs
[ world
.sim_run
].condition_count
)
1780 struct fish
*fish
= &world
.fishes
[ world
.num_fishes
];
1781 fish
->pos
[0] = posx
;
1782 fish
->pos
[1] = posy
;
1783 fish
->state
= k_fish_state_alive
;
1784 fish
->payload
= term
->runs
[ world
.sim_run
].conditions
[ world
.sim_frame
];
1786 struct cell
*cell_ptr
= pcell( fish
->pos
);
1788 if( cell_ptr
->config
!= k_cell_type_stub
)
1790 struct cell_description
*desc
= &cell_descriptions
[ cell_ptr
->config
];
1792 v2i_copy( desc
->start
, fish
->dir
);
1793 fish
->flow_reversed
= 1;
1795 world
.num_fishes
++;
1802 if( alive_count
== 0 )
1804 world
.completed
= 1;
1806 for( int i
= 0; i
< arrlen( world
.io
); i
++ )
1808 struct cell_terminal
*term
= &world
.io
[ i
];
1809 int is_input
= world
.data
[ term
->id
].state
& FLAG_INPUT
;
1813 struct terminal_run
*run
= &term
->runs
[ world
.sim_run
];
1815 if( run
->recv_count
== run
->condition_count
)
1817 for( int j
= 0; j
< run
->condition_count
; j
++ )
1819 if( run
->recieved
[j
] != run
->conditions
[j
] )
1821 world
.completed
= 0;
1828 world
.completed
= 0;
1834 if( world
.completed
)
1836 if( world
.sim_run
< world
.max_runs
-1 )
1838 vg_success( "Run passed, starting next\n" );
1840 world
.sim_frame
= 0;
1841 world
.sim_start
= vg_time
;
1842 world
.num_fishes
= 0;
1844 for( int i
= 0; i
< world
.w
*world
.h
; i
++ )
1845 world
.data
[ i
].state
&= ~FLAG_FLIP_FLOP
;
1851 vg_success( "Level passed!\n" );
1854 for( int i
= 0; i
< world
.w
*world
.h
; i
++ )
1855 if( world
.data
[ i
].state
& FLAG_CANAL
)
1858 world
.score
= score
;
1859 world
.time
= world
.sim_frame
;
1864 if( world
.sim_run
> 0 )
1865 sw_set_achievement( "GOOD_ENOUGH" );
1867 vg_error( "Level failed :(\n" );
1870 // Copy into career data
1871 if( world
.pCmpLevel
)
1873 if( world
.score
< world
.pCmpLevel
->completed_score
|| world
.pCmpLevel
->completed_score
== 0 )
1874 leaderboard_set_score( world
.pCmpLevel
, world
.score
);
1876 world
.pCmpLevel
->completed_score
= world
.score
;
1879 simulation_stop(); // TODO: Async?
1886 float scaled_time
= 0.0f
;
1887 scaled_time
= (vg_time
-world
.sim_start
)*world
.sim_speed
;
1888 world
.frame_lerp
= scaled_time
- (float)world
.sim_frame
;
1891 for( int i
= 0; i
< world
.num_fishes
; i
++ )
1893 struct fish
*fish
= &world
.fishes
[i
];
1895 if( fish
->state
== k_fish_state_dead
)
1898 if( fish
->state
== k_fish_state_soon_dead
&& (world
.frame_lerp
> fish
->death_time
) )
1899 continue; // Todo: particle thing?
1901 struct cell
*cell
= pcell(fish
->pos
);
1902 struct cell_description
*desc
= &cell_descriptions
[ cell
->config
];
1906 float t
= world
.frame_lerp
;
1907 if( fish
->flow_reversed
&& !desc
->is_linear
)
1910 v2_copy( fish
->physics_co
, fish
->physics_v
);
1912 switch( cell
->config
)
1914 case k_cell_type_merge
:
1915 if( fish
->dir
[0] == 1 )
1920 case k_cell_type_con_r
: curve
= curve_1
; break;
1921 case k_cell_type_con_l
: curve
= curve_4
; break;
1922 case k_cell_type_con_u
: curve
= curve_2
; break;
1923 case k_cell_type_con_d
: curve
= curve_8
; break;
1924 case 3: curve
= curve_3
; break;
1925 case 6: curve
= curve_6
; break;
1926 case 9: curve
= curve_9
; break;
1927 case 12: curve
= curve_12
; break;
1929 if( t
> curve_7_linear_section
)
1931 t
-= curve_7_linear_section
;
1932 t
*= (1.0f
/(1.0f
-curve_7_linear_section
));
1934 curve
= cell
->state
& FLAG_FLIP_FLOP
? curve_7
: curve_7_1
;
1938 default: curve
= NULL
; break;
1944 float t3
= t
* t
* t
;
1946 float cA
= 3.0f
*t2
- 3.0f
*t3
;
1947 float cB
= 3.0f
*t3
- 6.0f
*t2
+ 3.0f
*t
;
1948 float cC
= 3.0f
*t2
- t3
- 3.0f
*t
+ 1.0f
;
1950 fish
->physics_co
[0] = t3
*curve
[3][0] + cA
*curve
[2][0] + cB
*curve
[1][0] + cC
*curve
[0][0];
1951 fish
->physics_co
[1] = t3
*curve
[3][1] + cA
*curve
[2][1] + cB
*curve
[1][1] + cC
*curve
[0][1];
1952 fish
->physics_co
[0] += (float)fish
->pos
[0];
1953 fish
->physics_co
[1] += (float)fish
->pos
[1];
1958 origin
[0] = (float)fish
->pos
[0] + (float)fish
->dir
[0]*-0.5f
+ 0.5f
;
1959 origin
[1] = (float)fish
->pos
[1] + (float)fish
->dir
[1]*-0.5f
+ 0.5f
;
1961 fish
->physics_co
[0] = origin
[0] + (float)fish
->dir
[0]*t
;
1962 fish
->physics_co
[1] = origin
[1] + (float)fish
->dir
[1]*t
;
1968 static void render_tiles( v2i start
, v2i end
, v4f
const regular_colour
, v4f
const selected_colour
)
1970 v2i full_start
= { 0,0 };
1971 v2i full_end
= { world
.w
, world
.h
};
1973 if( !start
|| !end
)
1979 glUniform4fv( SHADER_UNIFORM( shader_tile_main
, "uColour" ), 1, regular_colour
);
1981 for( int y
= start
[1]; y
< end
[1]; y
++ )
1983 for( int x
= start
[0]; x
< end
[0]; x
++ )
1985 struct cell
*cell
= pcell((v2i
){x
,y
});
1986 int selected
= world
.selected
== y
*world
.w
+ x
;
1988 int tile_offsets
[][2] =
1990 {2, 0}, {0, 3}, {0, 2}, {2, 2},
1991 {1, 0}, {2, 3}, {3, 2}, {1, 3},
1992 {3, 1}, {0, 1}, {1, 2}, {2, 1},
1993 {1, 1}, {3, 3}, {2, 1}, {2, 1}
1996 int uv
[2] = { 3, 0 };
1998 if( cell
->state
& (FLAG_CANAL
|FLAG_INPUT
|FLAG_OUTPUT
) )
2000 uv
[0] = tile_offsets
[ cell
->config
][0];
2001 uv
[1] = tile_offsets
[ cell
->config
][1];
2004 glUniform4f( SHADER_UNIFORM( shader_tile_main
, "uOffset" ), (float)x
, (float)y
, uv
[0], uv
[1] );
2007 glUniform4fv( SHADER_UNIFORM( shader_tile_main
, "uColour" ), 1, selected_colour
);
2009 glUniform4fv( SHADER_UNIFORM( shader_tile_main
, "uColour" ), 1, regular_colour
);
2017 static void draw_numbers( v3f coord
, int number
)
2020 v3_copy( coord
, pos
);
2021 int digits
[8]; int i
= 0;
2023 while( number
> 0 && i
< 8 )
2025 digits
[i
++] = number
% 10;
2026 number
= number
/ 10;
2029 for( int j
= 0; j
< i
; j
++ )
2031 glUniform3fv( SHADER_UNIFORM( shader_tile_colour
, "uOffset" ), 1, pos
);
2032 draw_mesh( MESH_NUMBERS_OFFSETS
[digits
[i
-j
-1]][0], MESH_NUMBERS_OFFSETS
[digits
[i
-j
-1]][1] );
2033 pos
[0] += pos
[2] * 0.75f
;
2037 void vg_render(void)
2039 glViewport( 0,0, vg_window_x
, vg_window_y
);
2041 glDisable( GL_DEPTH_TEST
);
2042 glClearColor( 0.369768f
, 0.3654f
, 0.42f
, 1.0f
);
2043 glClear( GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
2045 v4f
const colour_default
= {1.0f
, 1.0f
, 1.0f
, 1.0f
};
2046 v4f
const colour_selected
= {0.90f
, 0.92f
, 1.0f
, 1.0f
};
2048 // TILE SET RENDERING
2049 // todo: just slam everything into a mesh...
2050 // when user modifies a tile the neighbours can be easily uploaded to gpu mem
2052 // Currently we're uploading a fair amount of data every frame anyway.
2053 // NOTE: this is for final optimisations ONLY!
2054 // ======================================================================
2056 use_mesh( &world
.tile
);
2062 SHADER_USE( shader_background
);
2063 glUniformMatrix3fv( SHADER_UNIFORM( shader_background
, "uPv" ), 1, GL_FALSE
, (float *)vg_pv
);
2065 glActiveTexture( GL_TEXTURE0
);
2066 glBindTexture( GL_TEXTURE_2D
, world
.background_data
);
2067 glUniform1i( SHADER_UNIFORM( shader_background
, "uTexMain" ), 0 );
2069 glUniform3f( SHADER_UNIFORM( shader_background
, "uOffset" ), -16, -16, 64 );
2070 glUniform1f( SHADER_UNIFORM( shader_background
, "uVariance" ), 0.02f
);
2072 glActiveTexture( GL_TEXTURE1
);
2073 glBindTexture( GL_TEXTURE_2D
, world
.random_samples
);
2074 glUniform1i( SHADER_UNIFORM( shader_background
, "uSamplerNoise" ), 1 );
2082 ui_begin( &ui_global_ctx
, 512, 256 );
2084 ui_global_ctx
.override_colour
= 0xff9a8a89;
2085 //ui_text( &ui_global_ctx, world.map_title, 6, 0 );
2086 ui_global_ctx
.override_colour
= 0xffffffff;
2088 ui_resolve( &ui_global_ctx
);
2091 m3x3_copy( vg_pv
, world_text
);
2092 m3x3_translate( world_text
, (v3f
){ 1.55f
, 1.9f
, 0.0f
} );
2093 m3x3_rotate( world_text
, VG_PIf
*0.5f
);
2094 m3x3_scale( world_text
, (v3f
){0.01f
,-0.01f
,0.01f
} );
2096 ui_draw( &ui_global_ctx
, world_text
);
2099 // =========================================================================================
2101 use_mesh( &world
.tile
);
2102 SHADER_USE( shader_tile_main
);
2105 m2x2_identity( subtransform
);
2106 glUniformMatrix2fv( SHADER_UNIFORM( shader_tile_main
, "uSubTransform" ), 1, GL_FALSE
, (float *)subtransform
);
2107 glUniformMatrix3fv( SHADER_UNIFORM( shader_tile_main
, "uPv" ), 1, GL_FALSE
, (float *)vg_pv
);
2108 glUniform1f( SHADER_UNIFORM( shader_tile_main
, "uGhost" ), 0.0f
);
2109 glUniform1f( SHADER_UNIFORM( shader_tile_main
, "uForeground" ), 0.0f
);
2112 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
2113 glBlendEquation(GL_FUNC_ADD
);
2116 vg_tex2d_bind( &tex_tile_data
, 0 );
2117 glUniform1i( SHADER_UNIFORM( shader_tile_main
, "uTexGlyphs" ), 0 );
2119 vg_tex2d_bind( &tex_wood
, 1 );
2120 glUniform1i( SHADER_UNIFORM( shader_tile_main
, "uTexWood" ), 1 );
2122 render_tiles( NULL
, NULL
, colour_default
, colour_default
);
2126 SHADER_USE( shader_ball
);
2127 glUniformMatrix3fv( SHADER_UNIFORM( shader_ball
, "uPv" ), 1, GL_FALSE
, (float *)vg_pv
);
2129 vg_tex2d_bind( &tex_ball_noise
, 0 );
2130 glUniform1i( SHADER_UNIFORM( shader_ball
, "uTexMain" ), 0 );
2133 if( world
.simulating
)
2135 for( int i
= 0; i
< world
.num_fishes
; i
++ )
2137 struct fish
*fish
= &world
.fishes
[i
];
2139 if( fish
->state
== k_fish_state_dead
|| fish
->state
== k_fish_state_bg
)
2142 if( fish
->state
== k_fish_state_soon_dead
&& (world
.frame_lerp
> fish
->death_time
) )
2145 v4f dot_colour
= { 0.0f
, 0.0f
, 0.0f
, 1.0f
};
2146 colour_code_v3( fish
->payload
, dot_colour
);
2148 glUniform3fv( SHADER_UNIFORM( shader_ball
, "uColour" ), 1, dot_colour
);
2149 glUniform2fv( SHADER_UNIFORM( shader_ball
, "uOffset" ), 1, fish
->physics_co
);
2150 glUniform2f( SHADER_UNIFORM( shader_ball
, "uTexOffset" ), (float)i
* 1.2334, (float)i
* -0.3579f
);
2155 SHADER_USE( shader_tile_main
);
2158 vg_tex2d_bind( &tex_tile_data
, 0 );
2159 glUniform1i( SHADER_UNIFORM( shader_tile_main
, "uTexGlyphs" ), 0 );
2161 vg_tex2d_bind( &tex_wood
, 1 );
2162 glUniform1i( SHADER_UNIFORM( shader_tile_main
, "uTexWood" ), 1 );
2164 glUniform1f( SHADER_UNIFORM( shader_tile_main
, "uForeground" ), 1.0f
);
2165 render_tiles( NULL
, NULL
, colour_default
, colour_selected
);
2168 for( int y
= 2; y
< world
.h
-2; y
++ )
2170 for( int x
= 2; x
< world
.w
-2; x
++ )
2172 struct cell
*cell
= pcell((v2i
){x
,y
});
2174 if( cell
->state
& FLAG_CANAL
)
2176 if( cell
->config
== k_cell_type_split
)
2178 float rotation
= cell
->state
& FLAG_FLIP_FLOP
? vg_rad( -45.0f
): vg_rad( 45.0f
);
2180 if( cell
->state
& FLAG_FLIP_ROTATING
)
2182 if( (world
.frame_lerp
> curve_7_linear_section
) )
2184 float const rotation_speed
= 0.4f
;
2185 if( (world
.frame_lerp
< 1.0f
-rotation_speed
) )
2187 float t
= world
.frame_lerp
- curve_7_linear_section
;
2188 t
*= -2.0f
* (1.0f
/(1.0f
-(curve_7_linear_section
+rotation_speed
)));
2198 m2x2_create_rotation( subtransform
, rotation
);
2200 glUniformMatrix2fv( SHADER_UNIFORM( shader_tile_main
, "uSubTransform" ), 1, GL_FALSE
, (float *)subtransform
);
2201 glUniform4f( SHADER_UNIFORM( shader_tile_main
, "uOffset" ), (float)x
, (float)y
+ 0.125f
, cell
->state
& FLAG_TARGETED
? 3.0f
: 0.0f
, 0.0f
);
2209 if( world
.selected
!= -1 && !(world
.data
[ world
.selected
].state
& FLAG_CANAL
) && !id_drag_from
)
2211 v2i new_begin
= { world
.tile_x
- 2, world
.tile_y
- 2 };
2212 v2i new_end
= { world
.tile_x
+ 2, world
.tile_y
+ 2 };
2214 world
.data
[ world
.selected
].state
^= FLAG_CANAL
;
2215 map_reclassify( new_begin
, new_end
, 0 );
2217 m2x2_identity( subtransform
);
2218 glUniform1f( SHADER_UNIFORM( shader_tile_main
, "uGhost" ), 1.0f
);
2219 glUniformMatrix2fv( SHADER_UNIFORM( shader_tile_main
, "uSubTransform" ), 1, GL_FALSE
, (float *)subtransform
);
2220 glUniform2fv( SHADER_UNIFORM( shader_tile_main
, "uMousePos" ), 1, world
.tile_pos
);
2222 render_tiles( new_begin
, new_end
, colour_default
, colour_default
);
2224 world
.data
[ world
.selected
].state
^= FLAG_CANAL
;
2225 map_reclassify( new_begin
, new_end
, 0 );
2228 //glDisable(GL_BLEND);
2230 // Draw connecting wires
2231 glDisable(GL_BLEND
);
2233 SHADER_USE( shader_wire
);
2234 glBindVertexArray( world
.wire
.vao
);
2236 glUniformMatrix3fv( SHADER_UNIFORM( shader_wire
, "uPv" ), 1, GL_FALSE
, (float *)vg_pv
);
2237 glUniform4f( SHADER_UNIFORM( shader_wire
, "uColour" ), 0.2f
, 0.2f
, 0.2f
, 1.0f
);
2241 glUniform1f( SHADER_UNIFORM( shader_wire
, "uCurve" ), 0.4f
);
2242 glUniform3f( SHADER_UNIFORM( shader_wire
, "uStart" ), drag_from_co
[0], drag_from_co
[1], 0.06f
);
2243 glUniform3f( SHADER_UNIFORM( shader_wire
, "uEnd" ), drag_to_co
[0], drag_to_co
[1], 0.06f
);
2244 glDrawElements( GL_TRIANGLES
, world
.wire
.em
, GL_UNSIGNED_SHORT
, (void*)(0) );
2247 float rp_x1
= world
.frame_lerp
*9.0f
;
2248 float rp_x2
= 1.0f
-rp_x1
*expf(1.0f
-rp_x1
)* 0.36f
;
2250 for( int y
= 2; y
< world
.h
-2; y
++ )
2252 for( int x
= 2; x
< world
.w
-2; x
++ )
2254 struct cell
*cell
= pcell((v2i
){x
,y
});
2256 if( cell
->state
& FLAG_CANAL
)
2258 if( cell
->state
& FLAG_IS_TRIGGER
)
2260 int trigger_id
= cell
->links
[0]?0:1;
2262 int x2
= cell
->links
[trigger_id
] % world
.w
;
2263 int y2
= (cell
->links
[trigger_id
] - x2
) / world
.w
;
2268 startpoint
[0] = (float)x2
+ (trigger_id
? 0.75f
: 0.25f
);
2269 startpoint
[1] = (float)y2
+ 0.25f
;
2271 endpoint
[0] = x
+0.5f
;
2272 endpoint
[1] = y
+0.5f
;
2274 glUniform1f( SHADER_UNIFORM( shader_wire
, "uCurve" ), cell
->state
& FLAG_TRIGGERED
? rp_x2
* 0.4f
: 0.4f
);
2275 glUniform3f( SHADER_UNIFORM( shader_wire
, "uStart" ), startpoint
[0], startpoint
[1], 0.04f
);
2276 glUniform3f( SHADER_UNIFORM( shader_wire
, "uEnd" ), endpoint
[0], endpoint
[1], 0.04f
);
2277 glDrawElements( GL_TRIANGLES
, world
.wire
.em
, GL_UNSIGNED_SHORT
, (void*)(0) );
2283 SHADER_USE( shader_tile_colour
);
2284 glUniformMatrix3fv( SHADER_UNIFORM( shader_tile_colour
, "uPv" ), 1, GL_FALSE
, (float *)vg_pv
);
2285 use_mesh( &world
.circle
);
2287 int const filled_start
= 0;
2288 int const filled_count
= 32;
2289 int const empty_start
= 32;
2290 int const empty_count
= 32*2;
2293 for( int i
= 0; i
< arrlen( world
.io
); i
++ )
2295 struct cell_terminal
*term
= &world
.io
[ i
];
2296 int posx
= term
->id
% world
.w
;
2297 int posy
= (term
->id
- posx
)/world
.w
;
2298 int is_input
= world
.data
[ term
->id
].state
& FLAG_INPUT
;
2300 v4f dot_colour
= { 0.0f
, 0.0f
, 0.0f
, 1.0f
};
2302 for( int k
= 0; k
< term
->run_count
; k
++ )
2304 for( int j
= 0; j
< term
->runs
[k
].condition_count
; j
++ )
2306 float y_offset
= is_input
? 1.2f
: -0.2f
;
2307 y_offset
+= (is_input
? 0.2f
: -0.2f
) * (float)k
;
2309 glUniform3f( SHADER_UNIFORM( shader_tile_colour
, "uOffset" ), (float)posx
+ 0.2f
+ 0.2f
* (float)j
, (float)posy
+ y_offset
, 0.1f
);
2313 colour_code_v3( term
->runs
[k
].conditions
[j
], dot_colour
);
2314 glUniform4fv( SHADER_UNIFORM( shader_tile_colour
, "uColour" ), 1, dot_colour
);
2316 // Draw filled if tick not passed, draw empty if empty
2317 if( (world
.sim_frame
> j
&& world
.sim_run
>= k
) || world
.sim_run
> k
)
2318 draw_mesh( empty_start
, empty_count
);
2320 draw_mesh( filled_start
, filled_count
);
2324 if( term
->runs
[k
].recv_count
> j
)
2326 colour_code_v3( term
->runs
[k
].recieved
[j
], dot_colour
);
2327 v3_muls( dot_colour
, 0.8f
, dot_colour
);
2328 glUniform4fv( SHADER_UNIFORM( shader_tile_colour
, "uColour" ), 1, dot_colour
);
2330 draw_mesh( filled_start
, filled_count
);
2333 colour_code_v3( term
->runs
[k
].conditions
[j
], dot_colour
);
2334 glUniform4fv( SHADER_UNIFORM( shader_tile_colour
, "uColour" ), 1, dot_colour
);
2336 draw_mesh( empty_start
, empty_count
);
2342 if( world
.simulating
)
2344 glUniform4f( SHADER_UNIFORM( shader_tile_colour
, "uColour" ), 0.0f
, 0.0f
, 0.0f
, 1.0f
);
2345 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
);
2346 draw_mesh( filled_start
, filled_count
);
2350 float const score_bright
= 1.25f
;
2351 glUniform4f( SHADER_UNIFORM( shader_tile_colour
, "uColour" ),
2352 0.4f
*score_bright
, 0.39f
*score_bright
, 0.45f
*score_bright
, 1.0f
);
2354 use_mesh( &world
.numbers
);
2355 draw_numbers( (v3f
){ 2.0f
, (float)world
.h
-1.875f
, 0.3333f
}, world
.score
);
2358 static ui_colourset flcol_list_a
= {
2360 .hover
= 0xffa09393,
2361 .active
= 0xffbfb1b0
2363 static ui_colourset flcol_list_b
= {
2365 .hover
= 0xffa09393,
2366 .active
= 0xffbfb1b0
2369 static ui_colourset flcol_list_complete_a
= {
2371 .hover
= 0xff8dc18f,
2372 .active
= 0xffb2ddb3
2375 static ui_colourset flcol_list_complete_b
= {
2377 .hover
= 0xff8dc18f,
2378 .active
= 0xffb2ddb3
2381 static ui_colourset flcol_list_locked
= {
2383 .hover
= 0xff655959,
2384 .active
= 0xff655959
2389 SteamLeaderboard_t steam_leaderboard
;
2390 int leaderboard_show
;
2392 struct leaderboard_player
2397 int is_local_player
;
2400 char score_text
[ 16 ];
2401 char player_name
[ 48 ];
2402 GLuint texture
; // Not dynamic
2404 leaderboard_players
[10];
2405 int leaderboard_count
;
2409 struct cmp_level
*level
;
2416 struct cmp_level
*level_selected
;
2423 static int pack_selection
= 0;
2424 static struct pack_info
2426 struct cmp_level
*levels
;
2433 .levels
= cmp_levels_tutorials
,
2434 .level_count
= vg_list_size(cmp_levels_tutorials
),
2438 .levels
= cmp_levels_basic
,
2439 .level_count
= vg_list_size(cmp_levels_basic
),
2443 .levels
= cmp_levels_grad
,
2444 .level_count
= vg_list_size(cmp_levels_tutorials
),
2450 ui_global_ctx
.cursor
[0] = 0;
2451 ui_global_ctx
.cursor
[1] = 0;
2452 ui_global_ctx
.cursor
[2] = 256;
2456 ui_global_ctx
.id_base
= 4 << 16;
2460 gui_capture_mouse( 9999 );
2461 gui_fill_rect( ui_global_ctx
.cursor
, 0xff5a4e4d );
2463 gui_text( "ASSIGNMENTS", 8, 0 );
2465 ui_global_ctx
.cursor
[1] += 30;
2466 ui_global_ctx
.cursor
[3] = 25;
2470 ui_rect_pad( ui_global_ctx
.cursor
, 2 );
2471 ui_global_ctx
.cursor
[2] = 84;
2473 for( int i
= 0; i
< 3; i
++ )
2475 if( i
== pack_selection
)
2476 gui_override_colours( &flcol_list_locked
);
2478 if( gui_button( 2000 + i
) == k_button_click
)
2481 ui_global_ctx
.cursor
[1] += 2;
2482 gui_text( pack_infos
[i
].name
, 4, 0 );
2485 gui_reset_colours();
2490 ui_global_ctx
.cursor
[3] = 500;
2492 // DRAW LEVEL SELECTION LIST
2494 struct cmp_level
*levels
= pack_infos
[ pack_selection
].levels
;
2495 int count
= pack_infos
[ pack_selection
].level_count
;
2496 int unlocked
= 3000;
2498 static struct ui_scrollbar sb
= {
2502 ui_px view_height
= ui_global_ctx
.cursor
[3];
2503 ui_px level_height
= 50;
2505 // Level scroll view
2508 gui_fill_rect( ui_global_ctx
.cursor
, 0xff5a4e4d );
2509 gui_set_clip( ui_global_ctx
.cursor
);
2511 ui_global_ctx
.cursor
[2] = 14;
2514 ui_px content_height
= count
*level_height
;
2515 if( content_height
> view_height
)
2517 ui_scrollbar( &ui_global_ctx
, &sb
, 1 );
2518 ui_global_ctx
.cursor
[1] -= ui_calculate_content_scroll( &sb
, content_height
);
2522 gui_fill_rect( ui_global_ctx
.cursor
, 0xff807373 );
2525 ui_global_ctx
.cursor
[2] = 240;
2526 ui_global_ctx
.cursor
[3] = level_height
;
2529 for( int i
= 0; i
< count
; i
++ )
2531 struct cmp_level
*lvl_info
= &levels
[i
];
2535 if( lvl_info
->completed_score
!= 0 )
2536 gui_override_colours( i
&0x1? &flcol_list_complete_a
: &flcol_list_complete_b
);
2538 gui_override_colours( i
&0x1? &flcol_list_a
: &flcol_list_b
);
2541 gui_override_colours( &flcol_list_locked
);
2545 if( gui_button( 2 + i
) == k_button_click
)
2547 ui_data
.level_selected
= &levels
[i
];
2548 ui_data
.leaderboard_show
= 0;
2550 if( pack_selection
>= 1 )
2551 sw_find_leaderboard( ui_data
.level_selected
->map_name
);
2554 ui_global_ctx
.override_colour
= 0xffffffff;
2555 gui_text( lvl_info
->title
, 6, 0 );
2556 ui_global_ctx
.cursor
[1] += 18;
2557 gui_text( "incomplete", 4, 0 );
2561 gui_button( 2 + i
);
2563 ui_global_ctx
.override_colour
= 0xff786f6f;
2564 gui_text( "???", 6, 0 );
2565 ui_global_ctx
.cursor
[1] += 18;
2566 gui_text( "locked", 4, 0 );
2572 gui_reset_colours();
2580 // Selected level UI
2581 // ============================================================
2583 if( ui_data
.level_selected
)
2585 ui_global_ctx
.cursor
[0] += 16;
2586 ui_global_ctx
.cursor
[1] += 16;
2587 ui_global_ctx
.cursor
[2] = 512-40;
2588 ui_global_ctx
.cursor
[3] = 560-16;
2592 gui_capture_mouse( 9999 );
2594 gui_fill_rect( ui_global_ctx
.cursor
, 0xff5a4e4d );
2595 ui_global_ctx
.cursor
[1] += 4;
2596 gui_text( ui_data
.level_selected
->title
, 6, 0 );
2598 ui_global_ctx
.cursor
[1] += 30;
2599 ui_rect_pad( ui_global_ctx
.cursor
, 8 );
2600 ui_global_ctx
.cursor
[3] = 300;
2604 gui_fill_rect( ui_global_ctx
.cursor
, 0xff655959 );
2608 ui_text_use_paragraph( &ui_global_ctx
);
2609 ui_global_ctx
.cursor
[1] += 2;
2611 gui_text( ui_data
.level_selected
->description
, 5, 0 );
2612 ui_text_use_title( &ui_global_ctx
);
2614 // Buttons at the bottom
2615 ui_global_ctx
.cursor
[3] = 25;
2616 ui_global_ctx
.cursor
[2] = 80;
2619 ui_global_ctx
.cursor
[1] -= 8;
2621 if( gui_button( 3000 ) == k_button_click
)
2623 ui_data
.level_selected
= NULL
;
2625 gui_text( "BACK", 6, k_text_alignment_center
);
2629 ui_global_ctx
.cursor
[2] = 170;
2630 ui_global_ctx
.cursor
[0] -= 8 + 170 + 2;
2633 gui_override_colours( &flcol_list_locked
);
2634 if( gui_button( 3001 ) == k_button_click
)
2635 vg_error( "UNIMPLEMENTED\n" );
2637 ui_global_ctx
.override_colour
= 0xff888888;
2639 gui_text( "RESTORE SOLUTION", 6, k_text_alignment_center
);
2641 ui_global_ctx
.override_colour
= 0xffffffff;
2644 ui_global_ctx
.cursor
[0] += 2;
2645 ui_global_ctx
.cursor
[2] = 80;
2648 gui_override_colours( &flcol_list_complete_a
);
2649 if( gui_button( 3002 ) == k_button_click
)
2651 console_changelevel( 1, &ui_data
.level_selected
->map_name
);
2652 world
.pCmpLevel
= ui_data
.level_selected
;
2654 ui_data
.level_selected
= NULL
;
2655 ui_data
.leaderboard_show
= 0;
2657 gui_text( "PLAY", 6, k_text_alignment_center
);
2661 gui_reset_colours();
2665 if( ui_data
.leaderboard_show
)
2667 ui_global_ctx
.cursor
[0] += 16;
2668 ui_global_ctx
.cursor
[2] = 350;
2669 ui_global_ctx
.cursor
[3] = 25;
2674 gui_fill_rect( ui_global_ctx
.cursor
, 0xff5a4e4d );
2675 gui_text( "FRIEND LEADERBOARD", 6, 0 );
2679 ui_global_ctx
.cursor
[1] += 2;
2683 ui_global_ctx
.cursor
[3] = 32+8;
2685 for( int i
= 0; i
< ui_data
.leaderboard_count
; i
++ )
2689 gui_fill_rect( ui_global_ctx
.cursor
, i
&0x1? flcol_list_a
.main
: flcol_list_b
.main
);
2691 ui_global_ctx
.cursor
[0] += 4;
2692 ui_global_ctx
.cursor
[1] += 4;
2695 static const char *places
[] = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10" };
2696 gui_text( places
[i
], 7, 0 );
2697 ui_global_ctx
.cursor
[0] += 32;
2699 struct leaderboard_player
*player
= &ui_data
.leaderboard_players
[i
];
2702 ui_global_ctx
.cursor
[2] = 32;
2703 ui_global_ctx
.cursor
[3] = 32;
2707 gui_push_image( ui_global_ctx
.cursor
, player
->texture
);
2712 gui_text( player
->player_name
, 7, 0 );
2714 ui_global_ctx
.cursor
[2] = 50;
2717 gui_text( player
->score_text
, 7, k_text_alignment_right
);
2721 ui_global_ctx
.cursor
[1] += 2;
2729 void leaderboard_dispatch_score(void)
2731 sw_upload_leaderboard_score(
2732 ui_data
.upload_request
.level
->steam_leaderboard
,
2733 k_ELeaderboardUploadScoreMethodKeepBest
,
2734 ui_data
.upload_request
.score
,
2739 ui_data
.upload_request
.is_waiting
= 0;
2741 vg_success( "Dispatched leaderboard score\n" );
2744 void leaderboard_found( LeaderboardFindResult_t
*pCallback
)
2746 if( !pCallback
->m_bLeaderboardFound
)
2748 vg_error( "Leaderboard could not be found\n" );
2749 ui_data
.steam_leaderboard
= 0;
2753 const char *recieved_name
= sw_get_leaderboard_name( pCallback
->m_hSteamLeaderboard
);
2755 // Update UI state and request entries if this callback found the current UI level
2756 if( ui_data
.level_selected
)
2758 if( !strcmp( recieved_name
, ui_data
.level_selected
->map_name
) )
2760 sw_download_leaderboard_entries( pCallback
->m_hSteamLeaderboard
, k_ELeaderboardDataRequestFriends
, 0, 8 );
2761 ui_data
.level_selected
->steam_leaderboard
= pCallback
->m_hSteamLeaderboard
;
2765 // Dispatch the waiting request if there was one
2766 if( ui_data
.upload_request
.is_waiting
)
2768 if( !strcmp( recieved_name
, ui_data
.upload_request
.level
->map_name
) )
2770 ui_data
.upload_request
.level
->steam_leaderboard
= pCallback
->m_hSteamLeaderboard
;
2771 leaderboard_dispatch_score();
2777 void leaderboard_downloaded( LeaderboardScoresDownloaded_t
*pCallback
)
2779 // Update UI if this leaderboard matches what we currently have in view
2780 if( ui_data
.level_selected
->steam_leaderboard
== pCallback
->m_hSteamLeaderboard
)
2782 vg_info( "Recieved %d entries\n", pCallback
->m_cEntryCount
);
2783 ui_data
.leaderboard_count
= VG_MIN( pCallback
->m_cEntryCount
, 8 );
2785 u64_steamid local_player
= sw_get_steamid();
2787 for( int i
= 0; i
< ui_data
.leaderboard_count
; i
++ )
2789 LeaderboardEntry_t entry
;
2790 sw_get_downloaded_entry( pCallback
->m_hSteamLeaderboardEntries
, i
, &entry
, NULL
, 0 );
2792 struct leaderboard_player
*player
= &ui_data
.leaderboard_players
[i
];
2794 player
->id
= entry
.m_steamIDUser
.m_unAll64Bits
;
2795 strncpy( player
->player_name
, sw_get_friend_persona_name( player
->id
), vg_list_size( player
->player_name
)-1 );
2796 player
->score
= entry
.m_nScore
;
2798 snprintf( player
->score_text
, vg_list_size(player
->score_text
), "%d", player
->score
);
2799 player
->texture
= sw_get_player_image( player
->id
);
2801 if( player
->texture
== 0 )
2802 player
->texture
= tex_unkown
.name
;
2804 player
->is_local_player
= local_player
== player
->id
? 1: 0;
2807 if( ui_data
.leaderboard_count
)
2808 ui_data
.leaderboard_show
= 1;
2810 ui_data
.leaderboard_show
= 0;
2812 else vg_warn( "Downloaded leaderboard does not match requested!\n" );
2815 void leaderboard_set_score( struct cmp_level
*cmp_level
, u32 score
)
2817 if( ui_data
.upload_request
.is_waiting
)
2818 vg_warn( "You are uploading leaderboard entries too quickly!\n" );
2820 ui_data
.upload_request
.level
= cmp_level
;
2821 ui_data
.upload_request
.score
= score
;
2822 ui_data
.upload_request
.is_waiting
= 1;
2824 // If leaderboard ID has been downloaded already then just immediately dispatch this
2825 if( cmp_level
->steam_leaderboard
)
2826 leaderboard_dispatch_score();
2828 sw_find_leaderboard( cmp_level
->map_name
);