save file .sv2 format read/write
[fishladder.git] / fishladder.c
1 // Copyright (C) 2021 Harry Godden (hgn) - All Rights Reserved
2
3 #define VG_STEAM
4 #define VG_STEAM_APPID 1218140U
5 #include "vg/vg.h"
6 #include "fishladder_resources.h"
7
8 /*
9 Todo for release:
10 Tutorial levels:
11 1. Transport
12 2. Split
13 3. Merge (and explode)
14 4. Principle 1 (divide colours)
15 5. Principle 2 (combine colours)
16
17 Trainee levels:
18 Simple maths (x3)
19 Colour ordering (x2)
20 Routing problems (x2)
21
22 Medium levels:
23 Reverse order
24
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
29 Row Gridlines for I/O
30 Play button / Speed controller
31
32
33 After release:
34
35 */
36
37 const char *level_pack_1[] = {
38 "level0",
39 "level1",
40 "level2",
41 "level3",
42 "level4",
43 "level5",
44 "level6",
45 "level7_combine",
46 "xor_small",
47 "sort",
48 "thirds"
49 };
50
51 #pragma pack(push,1)
52 struct dcareer_state
53 {
54 u32 version;
55 i32 total_unlocked;
56
57 u32 reserved[14];
58
59 struct dlevel_state
60 {
61 i32 score;
62 i32 reserved[3];
63 }
64 levels[ NUM_CAMPAIGN_LEVELS ];
65 };
66 #pragma pack(pop)
67
68 static void career_serialize(void)
69 {
70 struct dcareer_state encoded;
71 encoded.version = 2;
72 encoded.total_unlocked = career_local.total_unlocked;
73
74 for( int i = 0; i < vg_list_size( career_serializable ); i ++ )
75 {
76 struct serializable_set *set = &career_serializable[i];
77
78 for( int j = 0; j < set->count; j ++ )
79 {
80 struct cmp_level *lvl = &set->pack[j];
81 struct dlevel_state *dest = &encoded.levels[lvl->serial_id];
82
83 dest->score = lvl->completed_score;
84 dest->reserved[0] = 0;
85 dest->reserved[1] = 0;
86 dest->reserved[2] = 0;
87 }
88 }
89
90 vg_asset_write( "sav/game.sv2", &encoded, sizeof( struct dcareer_state ) );
91 }
92
93 static void career_load(void)
94 {
95 i64 sz;
96 struct dcareer_state encoded;
97 memset( (void*)&encoded, 0, sizeof( struct dcareer_state ) );
98
99 // Load and copy data into encoded
100 void *cr = vg_asset_read_s( "sav/game.sv2", &sz );
101
102 if( cr )
103 {
104 if( sz > sizeof( struct dcareer_state ) )
105 vg_warn( "This save file is too big! Some levels will be lost\n" );
106
107 if( sz <= offsetof( struct dcareer_state, levels ) )
108 {
109 vg_warn( "This save file is too small to have a header. Creating a blank one\n" );
110 free( cr );
111 return;
112 }
113
114 memcpy( (void*)&encoded, cr, VG_MIN( sizeof( struct dcareer_state ), sz ) );
115 free( cr );
116 }
117 else
118 vg_info( "No save file... Using blank one\n" );
119
120 // Decode everything from dstate
121 for( int i = 0; i < vg_list_size( career_serializable ); i ++ )
122 {
123 struct serializable_set *set = &career_serializable[i];
124
125 for( int j = 0; j < set->count; j ++ )
126 {
127 struct cmp_level *lvl = &set->pack[j];
128 struct dlevel_state *src = &encoded.levels[lvl->serial_id];
129
130 lvl->completed_score = src->score;
131 // ...
132 }
133 }
134 }
135
136 m3x3f m_projection;
137 m3x3f m_view;
138 m3x3f m_mdl;
139
140 #define FLAG_CANAL 0x1
141 #define FLAG_IS_TRIGGER 0x2
142 #define FLAG_RESERVED0 0x4
143 #define FLAG_RESERVED1 0x8
144
145 #define FLAG_INPUT 0x10
146 #define FLAG_OUTPUT 0x20
147 #define FLAG_WALL 0x40
148
149 #define FLAG_FLIP_FLOP 0x100
150 #define FLAG_TRIGGERED 0x200
151 #define FLAG_FLIP_ROTATING 0x400
152 #define FLAG_TARGETED 0x800
153
154 /*
155 0000 0 | 0001 1 | 0010 2 | 0011 3
156 | | | | |
157 X | X= | X | X=
158 | | |
159 0100 4 | 0101 5 | 0110 6 | 0111 7
160 | | | | |
161 =X | =X= | =X | =X=
162 | | |
163 1000 8 | 1001 9 | 1010 10 | 1011 11
164 | | | | |
165 X | X= | X | X=
166 | | | | | | |
167 1100 12 | 1101 13 | 1110 14 | 1111 15
168 | | | | |
169 =X | =X= | =X | =X=
170 | | | | | | |
171 */
172
173 struct cell_description
174 {
175 v2i start;
176 v2i end;
177
178 int is_special;
179 int is_linear;
180 }
181 cell_descriptions[] =
182 {
183 // 0-3
184 {},
185 { .start = { 1, 0 }, .end = { -1, 0 } },
186 { .start = { 0, 1 }, .end = { 0, -1 } },
187 { .start = { 0, 1 }, .end = { 1, 0 } },
188 // 4-7
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 },
193 // 8-11
194 { .start = { 0, -1 }, .end = { 0, 1 } },
195 { .start = { 1, 0 }, .end = { 0, -1 } },
196 { .start = { 0, 1 }, .end = { 0, -1 }, .is_linear = 1 },
197 { },
198 // 12-15
199 { .start = { -1, 0 }, .end = { 0, -1 } },
200 { .end = { 0, -1 }, .is_special = 1 },
201 { },
202 { }
203 };
204
205 enum cell_type
206 {
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
216 };
217
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}};
222
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}};
227
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}};
230
231 float const curve_7_linear_section = 0.1562f;
232
233 v3f colour_sets[] =
234 { { 1.0f, 0.9f, 0.3f },
235 { 0.2f, 0.9f, 0.14f },
236 { 0.4f, 0.8f, 1.00f } };
237
238 static void colour_code_v3( char cc, v3f target )
239 {
240 if( cc >= 'a' && cc <= 'z' )
241 {
242 int id = cc - 'a';
243
244 if( id < vg_list_size( colour_sets ) )
245 {
246 v3_copy( colour_sets[ id ], target );
247 return;
248 }
249 }
250
251 v3_copy( (v3f){0.0f,0.0f,0.0f}, target );
252 }
253
254 struct mesh
255 {
256 GLuint vao, vbo;
257 u32 elements;
258 };
259
260 static void init_mesh( struct mesh *m, float const *tris, u32 length )
261 {
262 m->elements = length/3;
263 glGenVertexArrays( 1, &m->vao );
264 glGenBuffers( 1, &m->vbo );
265
266 glBindVertexArray( m->vao );
267 glBindBuffer( GL_ARRAY_BUFFER, m->vbo );
268 glBufferData( GL_ARRAY_BUFFER, length*sizeof(float), tris, GL_STATIC_DRAW );
269
270 glVertexAttribPointer( 0, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(float), (void*)0 );
271 glEnableVertexAttribArray( 0 );
272
273 VG_CHECK_GL();
274 }
275
276 static void free_mesh( struct mesh *m )
277 {
278 glDeleteVertexArrays( 1, &m->vao );
279 glDeleteBuffers( 1, &m->vbo );
280 }
281
282 static void draw_mesh( int const start, int const count )
283 {
284 glDrawArrays( GL_TRIANGLES, start*3, count*3 );
285 }
286
287 static void use_mesh( struct mesh *m )
288 {
289 glBindVertexArray( m->vao );
290 }
291
292 enum e_fish_state
293 {
294 k_fish_state_soon_dead = -1,
295 k_fish_state_dead = 0,
296 k_fish_state_alive,
297 k_fish_state_bg,
298 k_fish_state_soon_alive
299 };
300
301 struct world
302 {
303 #pragma pack(push,1)
304 struct cell
305 {
306 u16 state;
307 u16 links[2];
308 u8 config;
309 u8 pad0;
310 }
311 *data;
312 #pragma pack(pop)
313
314 int frame;
315
316 int initialzed;
317
318 int sim_frame;
319 float sim_start;
320 int simulating;
321 int sim_run, max_runs;
322
323 float sim_speed;
324 float frame_lerp;
325
326 struct cell_terminal
327 {
328 //char *conditions;
329 //char recv[12];
330
331 struct terminal_run
332 {
333 char conditions[8];
334 char recieved[8];
335
336 int condition_count, recv_count;
337 }
338 runs[8];
339
340 int run_count;
341
342 int id;
343 }
344 *io;
345
346 int w, h;
347
348 struct mesh tile, circle, numbers;
349 struct mesh_wire
350 {
351 GLuint vao, vbo, ebo;
352 u32 em;
353 }
354 wire;
355
356 GLuint background_data;
357 GLuint random_samples;
358
359 int selected, tile_x, tile_y;
360 v2f tile_pos;
361
362 struct fish
363 {
364 v2i pos;
365 v2i dir;
366 enum e_fish_state state;
367 char payload;
368 int flow_reversed;
369 float death_time;
370 v2f physics_v;
371 v2f physics_co;
372 }
373 fishes[16];
374
375 int num_fishes;
376
377 char map_name[64];
378 //struct career_level *ptr_career_level;
379 struct cmp_level *pCmpLevel;
380
381 u32 score;
382 u32 completed;
383 u32 time;
384 } world = {};
385
386 void leaderboard_set_score( struct cmp_level *cmp_level, u32 score );
387
388 static void map_free(void)
389 {
390 arrfree( world.data );
391 arrfree( world.io );
392
393 world.w = 0;
394 world.h = 0;
395 world.data = NULL;
396 world.io = NULL;
397 world.score = 0;
398 world.time = 0;
399 world.completed = 0;
400 world.max_runs = 0;
401 world.initialzed = 0;
402 }
403
404 static void io_reset(void)
405 {
406 for( int i = 0; i < arrlen( world.io ); i ++ )
407 {
408 struct cell_terminal *term = &world.io[i];
409
410 for( int j = 0; j < term->run_count; j ++ )
411 term->runs[j].recv_count = 0;
412 }
413 }
414
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
448 };
449
450 static int hash21i( v2i p, u32 umod )
451 {
452 return random_noise[ (random_noise[p[1] & 1023] + p[0]) & 1023 ] & umod;
453 }
454
455 static void map_reclassify( v2i start, v2i end, int update_texbuffer );
456 static int map_load( const char *str, const char *name )
457 {
458 //TODO: It may be worthwhile, at this point, to switch to binary encoding for save data
459
460 map_free();
461
462 char const *c = str;
463
464 // Scan for width
465 for(;; world.w ++)
466 {
467 if( c[world.w] == ';' )
468 break;
469 else if( !c[world.w] )
470 {
471 vg_error( "Unexpected EOF when parsing level\n" );
472 return 0;
473 }
474 }
475
476 struct cell *row = arraddnptr( world.data, world.w );
477 int cx = 0;
478 int reg_start = 0, reg_end = 0;
479
480 u32 *links_to_make = NULL;
481 int links_satisfied = 0;
482
483 char link_id_buffer[32];
484 int link_id_n = 0;
485
486 for(;;)
487 {
488 if( !*c )
489 break;
490
491 if( *c == '\r' ) { c ++; continue; } // fuck off windows
492
493 if( *c == ';' )
494 {
495 c ++;
496
497 if( *c == '\r' ) c ++;
498
499 // Parse attribs
500 if( *c != '\n' )
501 {
502 while( *c )
503 {
504 if( *c == '\r' ) { c ++; continue; }
505
506 if( reg_start < reg_end )
507 {
508 struct cell_terminal *terminal = &world.io[ reg_start ];
509 struct terminal_run *run = &terminal->runs[ terminal->run_count-1 ];
510
511 if( *c >= 'a' && *c <= 'z' )
512 {
513 run->conditions[ run->condition_count ++ ] = *c;
514 }
515 else
516 {
517 if( *c == ',' || *c == '\n' )
518 {
519 reg_start ++;
520
521 if( *c == '\n' )
522 break;
523 }
524 else if( *c == ':' )
525 {
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 );
529 }
530 else
531 {
532 vg_error( "Unkown attribute '%c' (row: %u)\n", *c, world.h );
533 goto IL_REG_ERROR;
534 }
535 }
536 }
537 else
538 {
539 if( links_satisfied < arrlen( links_to_make ) )
540 {
541 struct cell *target = &world.data[ links_to_make[ links_satisfied ] ];
542
543 if( (((u32)*c >= (u32)'0') && ((u32)*c <= (u32)'9')) || *c == '-' )
544 {
545 if( link_id_n >= vg_list_size( link_id_buffer )-1 )
546 {
547 vg_error( "Number was way too long to be parsed (row: %u)\n", world.h );
548 goto IL_REG_ERROR;
549 }
550
551 link_id_buffer[ link_id_n ++ ] = *c;
552 }
553 else if( *c == ',' || *c == '\n' )
554 {
555 link_id_buffer[ link_id_n ] = 0x00;
556 int value = atoi( link_id_buffer );
557
558 target->links[value >= 0? 1:0] = abs(value);
559 links_satisfied ++;
560 link_id_n = 0;
561
562 if( *c == '\n' )
563 break;
564 }
565 else
566 {
567 vg_error( "Invalid character '%c' (row: %u)\n", *c, world.h );
568 goto IL_REG_ERROR;
569 }
570 }
571 else
572 {
573 vg_error( "Too many values to assign (row: %u)\n", world.h );
574 goto IL_REG_ERROR;
575 }
576 }
577
578 c ++;
579 }
580 }
581
582 // Registry length-error checks
583 if( reg_start != reg_end )
584 {
585 vg_error( "Not enough spawn values assigned (row: %u, %u of %u)\n", world.h, reg_start, reg_end );
586 goto IL_REG_ERROR;
587 }
588
589 if( links_satisfied != arrlen( links_to_make ) )
590 {
591 vg_error( "Not enough link values assigned (row: %u, %u of %u)\n", world.h, links_satisfied, arrlen( links_to_make ) );
592 goto IL_REG_ERROR;
593 }
594
595 if( cx != world.w )
596 {
597 vg_error( "Not enough cells to match previous row definition (row: %u, %u<%u)\n", world.h, cx, world.w );
598 goto IL_REG_ERROR;
599 }
600
601 row = arraddnptr( world.data, world.w );
602 cx = 0;
603 world.h ++;
604 reg_end = reg_start = arrlen( world.io );
605
606 arrsetlen( links_to_make, 0 );
607 links_satisfied = 0;
608 }
609 else
610 {
611 if( cx == world.w )
612 {
613 vg_error( "Too many cells to match previous row definition (row: %u, %u>%u)\n", world.h, cx, world.w );
614 goto IL_REG_ERROR;
615 }
616
617 // Tile initialization
618 // row[ cx ] .. etc
619 struct cell *cell = &row[ cx ];
620 cell->config = 0xF;
621
622 if( *c == '+' || *c == '-' )
623 {
624 struct cell_terminal *term = arraddnptr( world.io, 1 );
625 term->id = cx + world.h*world.w;
626 term->run_count = 1;
627 term->runs[0].condition_count = 0;
628
629 cell->state = *c == '+'? FLAG_INPUT: FLAG_OUTPUT;
630 reg_end ++;
631 }
632 else if( *c == '#' ) cell->state = FLAG_WALL;
633 else if( ((u32)*c >= (u32)'A') && ((u32)*c <= (u32)'A'+0xf) )
634 {
635 // Canal flag bits (4bit/16 value):
636 // 0: Canal present
637 // 1: Is trigger
638 // 2: Reserved
639 // 3: Reserved
640
641 cell->state = ((u32)*c - (u32)'A') & (FLAG_CANAL|FLAG_IS_TRIGGER);
642
643 if( cell->state & FLAG_IS_TRIGGER )
644 arrpush( links_to_make, cx + world.h*world.w );
645
646 cell->links[0] = 0;
647 cell->links[1] = 0;
648 world.score ++;
649 }
650 else cell->state = 0x00;
651
652 cx ++;
653 }
654
655 c ++;
656 }
657
658 // Update data texture to fill out the background
659 {
660 u8 info_buffer[64*64*4];
661 for( int x = 0; x < 64; x ++ )
662 {
663 for( int y = 0; y < 64; y ++ )
664 {
665 u8 *px = &info_buffer[((x*64)+y)*4];
666
667 px[0] = 0xFF-0x3F + hash21i( (v2i){x,y}, 0x3F );
668 px[1] = 0;
669 px[2] = 0;
670 px[3] = 0;
671 }
672 }
673
674 // Random walks.. kinda
675 for( int i = 0; i < arrlen(world.io); i ++ )
676 {
677 struct cell_terminal *term = &world.io[ i ];
678 int posx = term->id % world.w;
679 int posy = (term->id - posx)/world.w;
680
681 v2i turtle;
682 v2i turtle_dir;
683 int original_y;
684
685 turtle[0] = 16+posx;
686 turtle[1] = 16+posy;
687
688 turtle_dir[0] = 0;
689 turtle_dir[1] = posy <= 4? -1: 1;
690 original_y = turtle_dir[1];
691
692 for( int i = 0; i < 100; i ++ )
693 {
694 info_buffer[((turtle[1]*64)+turtle[0])*4] = 0;
695
696 v2i_add( turtle_dir, turtle, turtle );
697
698 if( turtle[0] == 0 ) break;
699 if( turtle[0] == 63 ) break;
700 if( turtle[1] == 0 ) break;
701 if( turtle[1] == 63 ) break;
702
703 int random_value = hash21i( turtle, 0xFF );
704 if( random_value > 255-40 && !turtle_dir[0] )
705 {
706 turtle_dir[0] = -1;
707 turtle_dir[1] = 0;
708 }
709 else if( random_value > 255-80 && !turtle_dir[0] )
710 {
711 turtle_dir[0] = 1;
712 turtle_dir[1] = 0;
713 }
714 else if( random_value > 255-100 )
715 {
716 turtle_dir[0] = 0;
717 turtle_dir[1] = original_y;
718 }
719 }
720 }
721
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 );
724 }
725
726 arrfree( links_to_make );
727
728 map_reclassify( NULL, NULL, 1 );
729
730 // Validate links
731 for( int i = 0; i < world.h*world.w; i ++ )
732 {
733 struct cell *src = &world.data[i];
734 if( src->state & FLAG_IS_TRIGGER )
735 {
736 int link_id = src->links[0]?0:1;
737 if( src->links[link_id] <= world.h*world.w )
738 {
739 struct cell *target = &world.data[ src->links[link_id] ];
740 if( (target->state & FLAG_CANAL) && (target->config == k_cell_type_split) )
741 {
742 if( target->links[ link_id ] )
743 {
744 vg_error( "Link target was already targeted\n" );
745 goto IL_REG_ERROR;
746 }
747 else
748 {
749 // Valid link
750 target->links[ link_id ] = i;
751 target->state |= FLAG_TARGETED;
752 }
753 }
754 else
755 {
756 vg_error( "Link target was invalid\n" );
757 goto IL_REG_ERROR;
758 }
759 }
760 else
761 {
762 vg_error( "Link target out of bounds\n" );
763 goto IL_REG_ERROR;
764 }
765 }
766 }
767
768 vg_success( "Map '%s' loaded! (%u:%u)\n", name, world.w, world.h );
769
770 io_reset();
771
772 strncpy( world.map_name, name, vg_list_size( world.map_name )-1 );
773 world.initialzed = 1;
774 return 1;
775
776 IL_REG_ERROR:
777 arrfree( links_to_make );
778 map_free();
779 return 0;
780 }
781
782 static struct cell *pcell( v2i pos )
783 {
784 return &world.data[ pos[1]*world.w + pos[0] ];
785 }
786
787 static void map_serialize( FILE *stream )
788 {
789 for( int y = 0; y < world.h; y ++ )
790 {
791 for( int x = 0; x < world.w; x ++ )
792 {
793 struct cell *cell = pcell( (v2i){ x, y } );
794
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) )
799 {
800 fputc( (cell->state & (FLAG_CANAL|FLAG_IS_TRIGGER|FLAG_RESERVED0|FLAG_RESERVED1)) + (u32)'A', stream );
801 }
802 else fputc( ' ', stream );
803 }
804
805 fputc( ';', stream );
806
807 int terminal_write_count = 0;
808
809 for( int x = 0; x < world.w; x ++ )
810 {
811 for( int i = 0; i < arrlen( world.io ); i ++ )
812 {
813 struct cell_terminal *term = &world.io[i];
814 if( term->id == y*world.w+x )
815 {
816 if( terminal_write_count )
817 fputc( ',', stream );
818 terminal_write_count ++;
819
820 for( int j = 0; j < term->run_count; j ++ )
821 {
822 struct terminal_run *run = &term->runs[j];
823
824 for( int k = 0; k < run->condition_count; k ++ )
825 fputc( run->conditions[k], stream );
826
827 if( j < term->run_count-1 )
828 fputc( ':', stream );
829 }
830 }
831 }
832 }
833
834 for( int x = 0; x < world.w; x ++ )
835 {
836 struct cell *cell = pcell( (v2i){ x,y } );
837 if( cell->state & FLAG_IS_TRIGGER )
838 {
839 if( terminal_write_count )
840 fputc( ',', stream );
841 terminal_write_count ++;
842
843 fprintf( stream, "%d", cell->links[0]? -cell->links[0]: cell->links[1] );
844 }
845 }
846
847 fputc( '\n', stream );
848 }
849 }
850
851 int main( int argc, char *argv[] )
852 {
853 vg_init( argc, argv, "Marble Computing | SPACE: Test | LeftClick: Toggle tile | RightClick: Drag wire" );
854 return 0;
855 }
856
857 static int console_credits( int argc, char const *argv[] )
858 {
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" );
865 return 0;
866 }
867
868 static int console_save_map( int argc, char const *argv[] )
869 {
870 if( !world.initialzed )
871 {
872 vg_error( "Tried to save uninitialized map!\n" );
873 return 0;
874 }
875
876 char map_path[ 256 ];
877
878 strcpy( map_path, "sav/" );
879 strcat( map_path, world.map_name );
880 strcat( map_path, ".map" );
881
882 FILE *test_writer = fopen( map_path, "wb" );
883 if( test_writer )
884 {
885 vg_info( "Saving map to '%s'\n", map_path );
886 map_serialize( test_writer );
887
888 fclose( test_writer );
889 return 1;
890 }
891 else
892 {
893 vg_error( "Unable to open stream for writing\n" );
894 return 0;
895 }
896 }
897
898 static int console_load_map( int argc, char const *argv[] )
899 {
900 char map_path[ 256 ];
901
902 if( argc >= 1 )
903 {
904 // try from saves
905 strcpy( map_path, "sav/" );
906 strcat( map_path, argv[0] );
907 strcat( map_path, ".map" );
908
909 char *text_source = vg_textasset_read( map_path );
910
911 if( !text_source )
912 {
913 strcpy( map_path, "maps/" );
914 strcat( map_path, argv[0] );
915 strcat( map_path, ".map" );
916
917 text_source = vg_textasset_read( map_path );
918 }
919
920 if( text_source )
921 {
922 vg_info( "Loading map: '%s'\n", map_path );
923 world.pCmpLevel = NULL;
924
925 if( !map_load( text_source, argv[0] ) )
926 {
927 free( text_source );
928 return 0;
929 }
930
931 free( text_source );
932 return 1;
933 }
934 else
935 {
936 vg_error( "Missing maps '%s'\n", argv[0] );
937 return 0;
938 }
939 }
940 else
941 {
942 vg_error( "Missing argument <map_path>\n" );
943 return 0;
944 }
945 }
946
947 static void simulation_stop(void)
948 {
949 world.simulating = 0;
950 world.num_fishes = 0;
951 world.sim_frame = 0;
952 world.sim_run = 0;
953
954 io_reset();
955
956 sfx_system_fadeout( &audio_system_balls_rolling, 44100 );
957
958 vg_info( "Stopping simulation!\n" );
959 }
960
961 static int console_changelevel( int argc, char const *argv[] )
962 {
963 if( argc >= 1 )
964 {
965 // Save current level
966 console_save_map( 0, NULL );
967 if( console_load_map( argc, argv ) )
968 {
969 simulation_stop();
970 return 1;
971 }
972 }
973 else
974 {
975 vg_error( "Missing argument <map_path>\n" );
976 }
977
978 return 0;
979 }
980
981 void leaderboard_found( LeaderboardFindResult_t *pCallback );
982 void leaderboard_downloaded( LeaderboardScoresDownloaded_t *pCallback );
983
984 void vg_start(void)
985 {
986 // Steamworks callbacks
987 sw_leaderboard_found = &leaderboard_found;
988 sw_leaderboard_downloaded = &leaderboard_downloaded;
989
990 vg_function_push( (struct vg_cmd){
991 .name = "_map_write",
992 .function = console_save_map
993 });
994
995 vg_function_push( (struct vg_cmd){
996 .name = "_map_load",
997 .function = console_load_map
998 });
999
1000 vg_function_push( (struct vg_cmd){
1001 .name = "map",
1002 .function = console_changelevel
1003 });
1004
1005 vg_function_push( (struct vg_cmd){
1006 .name = "credits",
1007 .function = console_credits
1008 });
1009
1010 // Quad mesh
1011 {
1012 float quad_mesh[] =
1013 {
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,
1016
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
1019 };
1020
1021 init_mesh( &world.tile, quad_mesh, vg_list_size(quad_mesh) );
1022 }
1023
1024 // Circle mesh
1025 {
1026 float circle_mesh[32*6*3];
1027 int res = vg_list_size( circle_mesh ) / (6*3);
1028
1029 for( int i = 0; i < res; i ++ )
1030 {
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 ) };
1033
1034 circle_mesh[ i*6+0 ] = 0.0f;
1035 circle_mesh[ i*6+1 ] = 0.0f;
1036
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 );
1040
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 );
1044
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 );
1049 }
1050
1051 init_mesh( &world.circle, circle_mesh, vg_list_size( circle_mesh ) );
1052 }
1053
1054 // Numbers mesh
1055 {
1056 init_mesh( &world.numbers,
1057 MESH_NUMBERS_BUFFER,
1058 vg_list_size( MESH_NUMBERS_BUFFER )
1059 );
1060
1061 for( int i = 0; i < 10; i ++ )
1062 {
1063 vg_info( "offset: %u, length: %u\n", MESH_NUMBERS_OFFSETS[i][0], MESH_NUMBERS_OFFSETS[i][1] );
1064 }
1065 }
1066
1067 // Create wire mesh
1068 {
1069 int const num_segments = 64;
1070
1071 struct mesh_wire *mw = &world.wire;
1072
1073 v2f wire_points[ num_segments * 2 ];
1074 u16 wire_indices[ 6*(num_segments-1) ];
1075
1076 for( int i = 0; i < num_segments; i ++ )
1077 {
1078 float l = (float)i / (float)(num_segments-1);
1079
1080 v2_copy( (v2f){ l, -0.5f }, wire_points[i*2+0] );
1081 v2_copy( (v2f){ l, 0.5f }, wire_points[i*2+1] );
1082
1083 if( i < num_segments-1 )
1084 {
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;
1091 }
1092 }
1093
1094 glGenVertexArrays( 1, &mw->vao );
1095 glGenBuffers( 1, &mw->vbo );
1096 glGenBuffers( 1, &mw->ebo );
1097 glBindVertexArray( mw->vao );
1098
1099 glBindBuffer( GL_ARRAY_BUFFER, mw->vbo );
1100
1101 glBufferData( GL_ARRAY_BUFFER, sizeof( wire_points ), wire_points, GL_STATIC_DRAW );
1102 glBindVertexArray( mw->vao );
1103
1104 glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, mw->ebo );
1105 glBufferData( GL_ELEMENT_ARRAY_BUFFER, sizeof( wire_indices ), wire_indices, GL_STATIC_DRAW );
1106
1107 // XY
1108 glVertexAttribPointer( 0, 2, GL_FLOAT, GL_FALSE, 2*sizeof(float), (void*)0 );
1109 glEnableVertexAttribArray( 0 );
1110
1111 VG_CHECK_GL();
1112
1113 mw->em = vg_list_size( wire_indices );
1114 }
1115
1116 // Create info data texture
1117 {
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 );
1121 vg_tex2d_nearest();
1122 }
1123
1124 // Create random smaples texture
1125 {
1126 u8 *data = malloc(512*512*2);
1127 for( int i = 0; i < 512*512*2; i ++ )
1128 data[ i ] = rand()/(RAND_MAX/255);
1129
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 );
1133 vg_tex2d_linear();
1134 vg_tex2d_repeat();
1135
1136 free( data );
1137 }
1138
1139 resource_load_main();
1140
1141 // Restore gamestate
1142 career_load();
1143 console_load_map( 1, level_pack_1 );
1144 }
1145
1146 void vg_free(void)
1147 {
1148 sw_free_opengl();
1149 console_save_map( 0, NULL );
1150 career_serialize();
1151
1152 resource_free_main();
1153
1154 glDeleteTextures( 1, &world.background_data );
1155 glDeleteTextures( 1, &world.random_samples );
1156
1157 glDeleteVertexArrays( 1, &world.wire.vao );
1158 glDeleteBuffers( 1, &world.wire.vbo );
1159 glDeleteBuffers( 1, &world.wire.ebo );
1160
1161 free_mesh( &world.tile );
1162 free_mesh( &world.circle );
1163 free_mesh( &world.numbers );
1164
1165 map_free();
1166 }
1167
1168 static int world_check_pos_ok( v2i co )
1169 {
1170 return (co[0] < 2 || co[0] >= world.w-2 || co[1] < 2 || co[1] >= world.h-2)? 0: 1;
1171 }
1172
1173 static int cell_interactive( v2i co )
1174 {
1175 // Bounds check
1176 if( !world_check_pos_ok( co ) )
1177 return 0;
1178
1179 // Flags check
1180 if( world.data[ world.w*co[1] + co[0] ].state & (FLAG_WALL|FLAG_INPUT|FLAG_OUTPUT) )
1181 return 0;
1182
1183 // List of 3x3 configurations that we do not allow
1184 static u32 invalid_src[][9] =
1185 {
1186 { 0,1,0,
1187 1,1,1,
1188 0,1,0
1189 },
1190 { 0,0,0,
1191 0,1,1,
1192 0,1,1
1193 },
1194 { 0,0,0,
1195 1,1,0,
1196 1,1,0
1197 },
1198 { 0,1,1,
1199 0,1,1,
1200 0,0,0
1201 },
1202 { 1,1,0,
1203 1,1,0,
1204 0,0,0
1205 },
1206 { 0,1,0,
1207 0,1,1,
1208 0,1,0
1209 },
1210 { 0,1,0,
1211 1,1,0,
1212 0,1,0
1213 }
1214 };
1215
1216 // Statically compile invalid configurations into bitmasks
1217 static u32 invalid[ vg_list_size(invalid_src) ];
1218
1219 for( int i = 0; i < vg_list_size(invalid_src); i ++ )
1220 {
1221 u32 comped = 0x00;
1222
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);
1226
1227 invalid[i] = comped;
1228 }
1229
1230 // Extract 5x5 grid surrounding tile
1231 u32 blob = 0x1000;
1232 for( int y = co[1]-2; y < co[1]+3; y ++ )
1233 for( int x = co[0]-2; x < co[0]+3; x ++ )
1234 {
1235 struct cell *cell = pcell((v2i){x,y});
1236
1237 if( cell && (cell->state & (FLAG_CANAL|FLAG_INPUT|FLAG_OUTPUT)) )
1238 blob |= 0x1 << ((y-(co[1]-2))*5 + x-(co[0]-2));
1239 }
1240
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 ++ )
1244 {
1245 if( blob & (0x1 << (6+kernel[i])) )
1246 {
1247 u32 window = blob >> kernel[i];
1248
1249 for( int j = 0; j < vg_list_size(invalid); j ++ )
1250 if((window & invalid[j]) == invalid[j])
1251 return 0;
1252 }
1253 }
1254
1255 return 1;
1256 }
1257
1258 static void map_reclassify( v2i start, v2i end, int update_texbuffer )
1259 {
1260 v2i full_start = { 1,1 };
1261 v2i full_end = { world.w-1, world.h-1 };
1262
1263 if( !start || !end )
1264 {
1265 start = full_start;
1266 end = full_end;
1267 }
1268
1269 // Texture data
1270 u8 info_buffer[64*64*4];
1271 u32 pixel_id = 0;
1272
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] );
1277
1278 for( int y = py0; y < py1; y ++ )
1279 {
1280 for( int x = px0; x < px1; x ++ )
1281 {
1282 struct cell *cell = pcell((v2i){x,y});
1283
1284 v2i dirs[] = {{1,0},{0,1},{-1,0},{0,-1}};
1285
1286 u8 height = 0;
1287 u8 config = 0x00;
1288
1289 if( cell->state & (FLAG_CANAL|FLAG_INPUT|FLAG_OUTPUT) )
1290 {
1291 for( int i = 0; i < vg_list_size( dirs ); i ++ )
1292 {
1293 struct cell *neighbour = pcell((v2i){x+dirs[i][0], y+dirs[i][1]});
1294 if( neighbour->state & (FLAG_CANAL|FLAG_INPUT|FLAG_OUTPUT) )
1295 config |= 0x1 << i;
1296 }
1297
1298 height = 128;
1299 }
1300 else
1301 {
1302 if( cell->state & FLAG_WALL )
1303 height = 0xFF-0x3F + hash21i( (v2i){x,y}, 0x3F );
1304
1305 config = 0xF;
1306 }
1307
1308 pcell((v2i){x,y})->config = config;
1309
1310 u8 *info_px = &info_buffer[ (pixel_id ++)*4 ];
1311 info_px[0] = height;
1312 info_px[1] = cell->state & FLAG_WALL? 0: 255;
1313 info_px[2] = 0;
1314 info_px[3] = 0;
1315
1316 if(
1317 (
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
1321 ){
1322 cell->state &= ~(FLAG_TARGETED|FLAG_IS_TRIGGER);
1323 for( u32 i = 0; i < 2; i ++ )
1324 {
1325 if( cell->links[i] )
1326 {
1327 struct cell *other_ptr = &world.data[ cell->links[i] ];
1328 other_ptr->links[ i ] = 0;
1329 other_ptr->state &= ~FLAG_IS_TRIGGER;
1330
1331 if( other_ptr->links[ i ^ 0x1 ] == 0 )
1332 other_ptr->state &= ~FLAG_TARGETED;
1333 }
1334 }
1335
1336 cell->links[0] = 0;
1337 cell->links[1] = 0;
1338 }
1339 }
1340 }
1341
1342 if( update_texbuffer )
1343 {
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 );
1346 }
1347 }
1348
1349 u16 id_drag_from = 0;
1350 v2f drag_from_co;
1351 v2f drag_to_co;
1352
1353 void vg_update(void)
1354 {
1355 // Fit within screen
1356
1357 float r1 = (float)vg_window_y / (float)vg_window_x,
1358 r2 = (float)world.h / (float)world.w,
1359 size;
1360
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 );
1363
1364 v3f origin;
1365 origin[0] = floorf( -0.5f * world.w );
1366 origin[1] = floorf( -0.5f * world.h );
1367 origin[2] = 0.0f;
1368
1369 m3x3_identity( m_view );
1370 m3x3_translate( m_view, origin );
1371 m3x3_mul( m_projection, m_view, vg_pv );
1372 vg_projection_update();
1373
1374 // Input stuff
1375 v2_copy( vg_mouse_ws, world.tile_pos );
1376
1377 world.tile_x = floorf( world.tile_pos[0] );
1378 world.tile_y = floorf( world.tile_pos[1] );
1379
1380 // Tilemap editing
1381 if( !world.simulating && !gui_want_mouse() )
1382 {
1383 v2_copy( vg_mouse_ws, drag_to_co );
1384
1385 if( cell_interactive( (v2i){ world.tile_x, world.tile_y } ))
1386 {
1387 world.selected = world.tile_y * world.w + world.tile_x;
1388
1389 static u32 modify_state = 0;
1390
1391 struct cell *cell_ptr = &world.data[world.selected];
1392
1393 if( vg_get_button_down("primary") )
1394 {
1395 modify_state = (cell_ptr->state & FLAG_CANAL) ^ FLAG_CANAL;
1396 }
1397
1398 if( vg_get_button("primary") && ((cell_ptr->state & FLAG_CANAL) != modify_state) )
1399 {
1400 cell_ptr->state &= ~FLAG_CANAL;
1401 cell_ptr->state |= modify_state;
1402
1403 if( cell_ptr->state & FLAG_CANAL )
1404 {
1405 cell_ptr->links[0] = 0;
1406 cell_ptr->links[1] = 0;
1407
1408 sfx_set_playrnd( &audio_tile_mod, &audio_system_sfx, 3, 6 );
1409 world.score ++;
1410 }
1411 else
1412 {
1413 sfx_set_playrnd( &audio_tile_mod, &audio_system_sfx, 0, 3 );
1414 world.score --;
1415 }
1416
1417 map_reclassify( (v2i){ world.tile_x -2, world.tile_y -2 },
1418 (v2i){ world.tile_x +2, world.tile_y +2 }, 1 );
1419 }
1420
1421 if( vg_get_button_down("secondary") && !(cell_ptr->config == k_cell_type_split) )
1422 {
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;
1426 }
1427
1428 if( id_drag_from && (cell_ptr->config == k_cell_type_split) )
1429 {
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;
1433
1434 if( vg_get_button_up("secondary") )
1435 {
1436 struct cell *drag_ptr = &world.data[id_drag_from];
1437 u32 link_id = local_x > 0.5f? 1: 0;
1438
1439 // Cleanup existing connections
1440 if( cell_ptr->links[ link_id ] )
1441 {
1442 vg_warn( "Destroying existing connection on link %u (%hu)\n", link_id, cell_ptr->links[ link_id ] );
1443
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;
1447 }
1448
1449 if( drag_ptr->links[ link_id ^ 0x1 ] )
1450 {
1451 vg_warn( "Destroying alternate link %u (%hu)\n", link_id ^ 0x1, drag_ptr->links[ link_id ^ 0x1 ] );
1452
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;
1456
1457 current_connection->links[ link_id ^ 0x1 ] = 0;
1458 drag_ptr->links[ link_id ^ 0x1 ] = 0;
1459 }
1460
1461 // Create the new connection
1462 vg_success( "Creating connection on link %u (%hu)\n", link_id, id_drag_from );
1463
1464 cell_ptr->links[ link_id ] = id_drag_from;
1465 drag_ptr->links[ link_id ] = world.selected;
1466
1467 cell_ptr->state |= FLAG_TARGETED;
1468 drag_ptr->state |= FLAG_IS_TRIGGER;
1469 id_drag_from = 0;
1470 }
1471 }
1472 }
1473 else
1474 world.selected = -1;
1475
1476 if( !(vg_get_button("secondary") && id_drag_from) )
1477 id_drag_from = 0;
1478 }
1479 else
1480 {
1481 world.selected = -1;
1482 id_drag_from = 0;
1483 }
1484
1485 // Simulation stop/start
1486 if( vg_get_button_down("go") )
1487 {
1488 if( world.simulating )
1489 {
1490 simulation_stop();
1491 }
1492 else
1493 {
1494 vg_success( "Starting simulation!\n" );
1495
1496 sfx_set_playrnd( &audio_rolls, &audio_system_balls_rolling, 0, 1 );
1497
1498 world.simulating = 1;
1499 world.num_fishes = 0;
1500 world.sim_frame = 0;
1501 world.sim_start = vg_time;
1502 world.sim_run = 0;
1503 world.sim_speed = 2.5f;
1504
1505 for( int i = 0; i < world.w*world.h; i ++ )
1506 world.data[ i ].state &= ~FLAG_FLIP_FLOP;
1507
1508 io_reset();
1509 }
1510 }
1511
1512 // Fish ticks
1513 if( world.simulating )
1514 {
1515 while( world.sim_frame < (int)((vg_time-world.sim_start)*world.sim_speed) )
1516 {
1517 //vg_info( "frame: %u\n", world.sim_frame );
1518 sfx_set_playrnd( &audio_random, &audio_system_balls_switching, 0, 9 );
1519
1520 // Update splitter deltas
1521 for( int i = 0; i < world.h*world.w; i ++ )
1522 {
1523 struct cell *cell = &world.data[i];
1524 if( cell->config == k_cell_type_split )
1525 {
1526 cell->state &= ~FLAG_FLIP_ROTATING;
1527 }
1528 if( cell->state & FLAG_IS_TRIGGER )
1529 cell->state &= ~FLAG_TRIGGERED;
1530 }
1531
1532 int alive_count = 0;
1533
1534 // Update fish positions
1535 for( int i = 0; i < world.num_fishes; i ++ )
1536 {
1537 struct fish *fish = &world.fishes[i];
1538
1539 if( fish->state == k_fish_state_soon_dead )
1540 fish->state = k_fish_state_dead;
1541
1542 if( fish->state == k_fish_state_soon_alive )
1543 fish->state = k_fish_state_alive;
1544
1545 if( fish->state < k_fish_state_alive )
1546 continue;
1547
1548 struct cell *cell_current = pcell( fish->pos );
1549
1550 if( fish->state == k_fish_state_alive )
1551 {
1552 // Apply to output
1553 if( cell_current->state & FLAG_OUTPUT )
1554 {
1555 for( int j = 0; j < arrlen( world.io ); j ++ )
1556 {
1557 struct cell_terminal *term = &world.io[j];
1558
1559 if( term->id == fish->pos[1]*world.w + fish->pos[0] )
1560 {
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;
1564
1565 break;
1566 }
1567 }
1568
1569 fish->state = k_fish_state_dead;
1570 continue;
1571 }
1572
1573
1574 if( cell_current->config == k_cell_type_merge )
1575 {
1576 // Can only move up
1577 fish->dir[0] = 0;
1578 fish->dir[1] = -1;
1579 fish->flow_reversed = 0;
1580 }
1581 else
1582 {
1583 if( cell_current->config == k_cell_type_split )
1584 {
1585 // Flip flop L/R
1586 fish->dir[0] = cell_current->state&FLAG_FLIP_FLOP?1:-1;
1587 fish->dir[1] = 0;
1588
1589 if( !(cell_current->state & FLAG_TARGETED) )
1590 cell_current->state ^= FLAG_FLIP_FLOP;
1591 }
1592 else
1593 {
1594 // Apply cell out-flow
1595 struct cell_description *desc = &cell_descriptions[ cell_current->config ];
1596
1597 v2i_copy( fish->flow_reversed? desc->start: desc->end, fish->dir );
1598 }
1599
1600 v2i pos_next;
1601 v2i_add( fish->pos, fish->dir, pos_next );
1602
1603 struct cell *cell_next = pcell( pos_next );
1604
1605 if( cell_next->state & (FLAG_CANAL|FLAG_OUTPUT) )
1606 {
1607 struct cell_description *desc = &cell_descriptions[ cell_next->config ];
1608
1609 if( cell_next->config == k_cell_type_merge )
1610 {
1611 if( fish->dir[0] == 0 )
1612 fish->state = k_fish_state_dead;
1613 else
1614 fish->flow_reversed = 0;
1615 }
1616 else
1617 {
1618 if( cell_next->config == k_cell_type_split )
1619 {
1620 if( fish->dir[0] == 0 )
1621 {
1622 sfx_set_playrnd( &audio_splitter, &audio_system_balls_important, 0, 1 );
1623 cell_next->state |= FLAG_FLIP_ROTATING;
1624
1625 fish->flow_reversed = 0;
1626 }
1627 else
1628 fish->state = k_fish_state_dead;
1629 }
1630 else
1631 fish->flow_reversed = ( fish->dir[0] != -desc->start[0] ||
1632 fish->dir[1] != -desc->start[1] )? 1: 0;
1633 }
1634 }
1635 else
1636 fish->state = world_check_pos_ok( fish->pos )? k_fish_state_bg: k_fish_state_dead;
1637 }
1638
1639 //v2i_add( fish->pos, fish->dir, fish->pos );
1640 }
1641 else if( fish->state == k_fish_state_bg )
1642 {
1643 v2i_add( fish->pos, fish->dir, fish->pos );
1644
1645 if( !world_check_pos_ok( fish->pos ) )
1646 fish->state = k_fish_state_dead;
1647 else
1648 {
1649 struct cell *cell_entry = pcell( fish->pos );
1650
1651 if( cell_entry->state & FLAG_CANAL )
1652 {
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 )
1655 {
1656 sw_set_achievement( "CAN_DO_THAT" );
1657
1658 fish->state = k_fish_state_soon_alive;
1659
1660 fish->dir[0] = 0;
1661 fish->dir[1] = 0;
1662 fish->flow_reversed = 1;
1663
1664 switch( cell_entry->config )
1665 {
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;
1670 }
1671 }
1672 }
1673 }
1674 }
1675 else { vg_error( "fish behaviour unimplemented for behaviour type (%d)\n" ); }
1676
1677 if( fish->state >= k_fish_state_alive )
1678 alive_count ++;
1679 }
1680
1681 // Second pass (triggers)
1682 for( int i = 0; i < world.num_fishes; i ++ )
1683 {
1684 struct fish *fish = &world.fishes[i];
1685
1686 if( fish->state == k_fish_state_alive )
1687 {
1688 v2i_add( fish->pos, fish->dir, fish->pos );
1689 struct cell *cell_current = pcell( fish->pos );
1690
1691 if( cell_current->state & FLAG_IS_TRIGGER )
1692 {
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;
1697
1698 vg_line2( (v2f){ fish->pos[0], fish->pos[1] }, (v2f){ target_px, target_py }, 0xffffffff, 0xffffffff );
1699
1700 struct cell *target_peice = &world.data[ cell_current->links[trigger_id] ];
1701
1702 cell_current->state |= FLAG_TRIGGERED;
1703
1704 if( trigger_id )
1705 target_peice->state |= FLAG_FLIP_FLOP;
1706 else
1707 target_peice->state &= ~FLAG_FLIP_FLOP;
1708 }
1709 }
1710 }
1711
1712 // Third pass (collisions)
1713 struct fish *fi, *fj;
1714
1715 for( int i = 0; i < world.num_fishes; i ++ )
1716 {
1717 fi = &world.fishes[i];
1718
1719 if( fi->state == k_fish_state_alive )
1720 {
1721 int continue_again = 0;
1722
1723 for( int j = i+1; j < world.num_fishes; j ++ )
1724 {
1725 fj = &world.fishes[j];
1726
1727 if( (fj->state == k_fish_state_alive) )
1728 {
1729 v2i fi_prev;
1730 v2i fj_prev;
1731
1732 v2i_sub( fi->pos, fi->dir, fi_prev );
1733 v2i_sub( fj->pos, fj->dir, fj_prev );
1734
1735 int
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])
1744 )? 1: 0;
1745
1746 if( collide_next_frame || collide_this_frame )
1747 {
1748 sw_set_achievement( "BANG" );
1749
1750 // Shatter death (+0.5s)
1751 float death_time = collide_this_frame? 0.0f: 0.5f;
1752
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;
1757
1758 continue_again = 1;
1759 break;
1760 }
1761 }
1762 }
1763 if( continue_again )
1764 continue;
1765 }
1766 }
1767
1768 // Spawn fishes
1769 for( int i = 0; i < arrlen( world.io ); i ++ )
1770 {
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;
1775
1776 if( is_input )
1777 {
1778 if( world.sim_frame < term->runs[ world.sim_run ].condition_count )
1779 {
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 ];
1785
1786 struct cell *cell_ptr = pcell( fish->pos );
1787
1788 if( cell_ptr->config != k_cell_type_stub )
1789 {
1790 struct cell_description *desc = &cell_descriptions[ cell_ptr->config ];
1791
1792 v2i_copy( desc->start, fish->dir );
1793 fish->flow_reversed = 1;
1794
1795 world.num_fishes ++;
1796 alive_count ++;
1797 }
1798 }
1799 }
1800 }
1801
1802 if( alive_count == 0 )
1803 {
1804 world.completed = 1;
1805
1806 for( int i = 0; i < arrlen( world.io ); i ++ )
1807 {
1808 struct cell_terminal *term = &world.io[ i ];
1809 int is_input = world.data[ term->id ].state & FLAG_INPUT;
1810
1811 if( !is_input )
1812 {
1813 struct terminal_run *run = &term->runs[ world.sim_run ];
1814
1815 if( run->recv_count == run->condition_count )
1816 {
1817 for( int j = 0; j < run->condition_count; j ++ )
1818 {
1819 if( run->recieved[j] != run->conditions[j] )
1820 {
1821 world.completed = 0;
1822 break;
1823 }
1824 }
1825 }
1826 else
1827 {
1828 world.completed = 0;
1829 break;
1830 }
1831 }
1832 }
1833
1834 if( world.completed )
1835 {
1836 if( world.sim_run < world.max_runs-1 )
1837 {
1838 vg_success( "Run passed, starting next\n" );
1839 world.sim_run ++;
1840 world.sim_frame = 0;
1841 world.sim_start = vg_time;
1842 world.num_fishes = 0;
1843
1844 for( int i = 0; i < world.w*world.h; i ++ )
1845 world.data[ i ].state &= ~FLAG_FLIP_FLOP;
1846
1847 continue;
1848 }
1849 else
1850 {
1851 vg_success( "Level passed!\n" );
1852
1853 u32 score = 0;
1854 for( int i = 0; i < world.w*world.h; i ++ )
1855 if( world.data[ i ].state & FLAG_CANAL )
1856 score ++;
1857
1858 world.score = score;
1859 world.time = world.sim_frame;
1860 }
1861 }
1862 else
1863 {
1864 if( world.sim_run > 0 )
1865 sw_set_achievement( "GOOD_ENOUGH" );
1866
1867 vg_error( "Level failed :(\n" );
1868 }
1869
1870 // Copy into career data
1871 if( world.pCmpLevel )
1872 {
1873 if( world.score < world.pCmpLevel->completed_score || world.pCmpLevel->completed_score == 0 )
1874 leaderboard_set_score( world.pCmpLevel, world.score );
1875
1876 world.pCmpLevel->completed_score = world.score;
1877 }
1878
1879 simulation_stop(); // TODO: Async?
1880 break;
1881 }
1882
1883 world.sim_frame ++;
1884 }
1885
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;
1889
1890 // Update positions
1891 for( int i = 0; i < world.num_fishes; i ++ )
1892 {
1893 struct fish *fish = &world.fishes[i];
1894
1895 if( fish->state == k_fish_state_dead )
1896 continue;
1897
1898 if( fish->state == k_fish_state_soon_dead && (world.frame_lerp > fish->death_time) )
1899 continue; // Todo: particle thing?
1900
1901 struct cell *cell = pcell(fish->pos);
1902 struct cell_description *desc = &cell_descriptions[ cell->config ];
1903
1904 v2f const *curve;
1905
1906 float t = world.frame_lerp;
1907 if( fish->flow_reversed && !desc->is_linear )
1908 t = 1.0f-t;
1909
1910 v2_copy( fish->physics_co, fish->physics_v );
1911
1912 switch( cell->config )
1913 {
1914 case k_cell_type_merge:
1915 if( fish->dir[0] == 1 )
1916 curve = curve_12;
1917 else
1918 curve = curve_9;
1919 break;
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;
1928 case 7:
1929 if( t > curve_7_linear_section )
1930 {
1931 t -= curve_7_linear_section;
1932 t *= (1.0f/(1.0f-curve_7_linear_section));
1933
1934 curve = cell->state & FLAG_FLIP_FLOP? curve_7: curve_7_1;
1935 }
1936 else curve = NULL;
1937 break;
1938 default: curve = NULL; break;
1939 }
1940
1941 if( curve )
1942 {
1943 float t2 = t * t;
1944 float t3 = t * t * t;
1945
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;
1949
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];
1954 }
1955 else
1956 {
1957 v2f origin;
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;
1960
1961 fish->physics_co[0] = origin[0] + (float)fish->dir[0]*t;
1962 fish->physics_co[1] = origin[1] + (float)fish->dir[1]*t;
1963 }
1964 }
1965 }
1966 }
1967
1968 static void render_tiles( v2i start, v2i end, v4f const regular_colour, v4f const selected_colour )
1969 {
1970 v2i full_start = { 0,0 };
1971 v2i full_end = { world.w, world.h };
1972
1973 if( !start || !end )
1974 {
1975 start = full_start;
1976 end = full_end;
1977 }
1978
1979 glUniform4fv( SHADER_UNIFORM( shader_tile_main, "uColour" ), 1, regular_colour );
1980
1981 for( int y = start[1]; y < end[1]; y ++ )
1982 {
1983 for( int x = start[0]; x < end[0]; x ++ )
1984 {
1985 struct cell *cell = pcell((v2i){x,y});
1986 int selected = world.selected == y*world.w + x;
1987
1988 int tile_offsets[][2] =
1989 {
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}
1994 };
1995
1996 int uv[2] = { 3, 0 };
1997
1998 if( cell->state & (FLAG_CANAL|FLAG_INPUT|FLAG_OUTPUT) )
1999 {
2000 uv[0] = tile_offsets[ cell->config ][0];
2001 uv[1] = tile_offsets[ cell->config ][1];
2002 } else continue;
2003
2004 glUniform4f( SHADER_UNIFORM( shader_tile_main, "uOffset" ), (float)x, (float)y, uv[0], uv[1] );
2005 if( selected )
2006 {
2007 glUniform4fv( SHADER_UNIFORM( shader_tile_main, "uColour" ), 1, selected_colour );
2008 draw_mesh( 0, 2 );
2009 glUniform4fv( SHADER_UNIFORM( shader_tile_main, "uColour" ), 1, regular_colour );
2010 }
2011 else
2012 draw_mesh( 0, 2 );
2013 }
2014 }
2015 }
2016
2017 static void draw_numbers( v3f coord, int number )
2018 {
2019 v3f pos;
2020 v3_copy( coord, pos );
2021 int digits[8]; int i = 0;
2022
2023 while( number > 0 && i < 8 )
2024 {
2025 digits[i ++] = number % 10;
2026 number = number / 10;
2027 }
2028
2029 for( int j = 0; j < i; j ++ )
2030 {
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;
2034 }
2035 }
2036
2037 void vg_render(void)
2038 {
2039 glViewport( 0,0, vg_window_x, vg_window_y );
2040
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 );
2044
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};
2047
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
2051 // in ~3 subBuffers
2052 // Currently we're uploading a fair amount of data every frame anyway.
2053 // NOTE: this is for final optimisations ONLY!
2054 // ======================================================================
2055
2056 use_mesh( &world.tile );
2057
2058 // Draw background
2059
2060 if(1){
2061
2062 SHADER_USE( shader_background );
2063 glUniformMatrix3fv( SHADER_UNIFORM( shader_background, "uPv" ), 1, GL_FALSE, (float *)vg_pv );
2064
2065 glActiveTexture( GL_TEXTURE0 );
2066 glBindTexture( GL_TEXTURE_2D, world.background_data );
2067 glUniform1i( SHADER_UNIFORM( shader_background, "uTexMain" ), 0 );
2068
2069 glUniform3f( SHADER_UNIFORM( shader_background, "uOffset" ), -16, -16, 64 );
2070 glUniform1f( SHADER_UNIFORM( shader_background, "uVariance" ), 0.02f );
2071
2072 glActiveTexture( GL_TEXTURE1 );
2073 glBindTexture( GL_TEXTURE_2D, world.random_samples );
2074 glUniform1i( SHADER_UNIFORM( shader_background, "uSamplerNoise" ), 1 );
2075
2076 draw_mesh( 0, 2 );
2077
2078 }
2079
2080
2081 // Level title
2082 ui_begin( &ui_global_ctx, 512, 256 );
2083
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;
2087
2088 ui_resolve( &ui_global_ctx );
2089
2090 m3x3f world_text;
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} );
2095
2096 ui_draw( &ui_global_ctx, world_text );
2097
2098 // Main
2099 // =========================================================================================
2100
2101 use_mesh( &world.tile );
2102 SHADER_USE( shader_tile_main );
2103
2104 m2x2f subtransform;
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 );
2110
2111 glEnable(GL_BLEND);
2112 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
2113 glBlendEquation(GL_FUNC_ADD);
2114
2115 // Bind textures
2116 vg_tex2d_bind( &tex_tile_data, 0 );
2117 glUniform1i( SHADER_UNIFORM( shader_tile_main, "uTexGlyphs" ), 0 );
2118
2119 vg_tex2d_bind( &tex_wood, 1 );
2120 glUniform1i( SHADER_UNIFORM( shader_tile_main, "uTexWood" ), 1 );
2121
2122 render_tiles( NULL, NULL, colour_default, colour_default );
2123
2124
2125
2126 SHADER_USE( shader_ball );
2127 glUniformMatrix3fv( SHADER_UNIFORM( shader_ball, "uPv" ), 1, GL_FALSE, (float *)vg_pv );
2128
2129 vg_tex2d_bind( &tex_ball_noise, 0 );
2130 glUniform1i( SHADER_UNIFORM( shader_ball, "uTexMain" ), 0 );
2131
2132 // Draw 'fish'
2133 if( world.simulating )
2134 {
2135 for( int i = 0; i < world.num_fishes; i ++ )
2136 {
2137 struct fish *fish = &world.fishes[i];
2138
2139 if( fish->state == k_fish_state_dead || fish->state == k_fish_state_bg )
2140 continue;
2141
2142 if( fish->state == k_fish_state_soon_dead && (world.frame_lerp > fish->death_time) )
2143 continue;
2144
2145 v4f dot_colour = { 0.0f, 0.0f, 0.0f, 1.0f };
2146 colour_code_v3( fish->payload, dot_colour );
2147
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 );
2151 draw_mesh( 0, 2 );
2152 }
2153 }
2154
2155 SHADER_USE( shader_tile_main );
2156
2157 // Bind textures
2158 vg_tex2d_bind( &tex_tile_data, 0 );
2159 glUniform1i( SHADER_UNIFORM( shader_tile_main, "uTexGlyphs" ), 0 );
2160
2161 vg_tex2d_bind( &tex_wood, 1 );
2162 glUniform1i( SHADER_UNIFORM( shader_tile_main, "uTexWood" ), 1 );
2163
2164 glUniform1f( SHADER_UNIFORM( shader_tile_main, "uForeground" ), 1.0f );
2165 render_tiles( NULL, NULL, colour_default, colour_selected );
2166
2167 // Draw splitters
2168 for( int y = 2; y < world.h-2; y ++ )
2169 {
2170 for( int x = 2; x < world.w-2; x ++ )
2171 {
2172 struct cell *cell = pcell((v2i){x,y});
2173
2174 if( cell->state & FLAG_CANAL )
2175 {
2176 if( cell->config == k_cell_type_split )
2177 {
2178 float rotation = cell->state & FLAG_FLIP_FLOP? vg_rad( -45.0f ): vg_rad( 45.0f );
2179
2180 if( cell->state & FLAG_FLIP_ROTATING )
2181 {
2182 if( (world.frame_lerp > curve_7_linear_section) )
2183 {
2184 float const rotation_speed = 0.4f;
2185 if( (world.frame_lerp < 1.0f-rotation_speed) )
2186 {
2187 float t = world.frame_lerp - curve_7_linear_section;
2188 t *= -2.0f * (1.0f/(1.0f-(curve_7_linear_section+rotation_speed)));
2189 t += 1.0f;
2190
2191 rotation *= t;
2192 }
2193 else
2194 rotation *= -1.0f;
2195 }
2196 }
2197
2198 m2x2_create_rotation( subtransform, rotation );
2199
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 );
2202 draw_mesh( 0, 2 );
2203 }
2204 }
2205 }
2206 }
2207
2208 // Edit overlay
2209 if( world.selected != -1 && !(world.data[ world.selected ].state & FLAG_CANAL) && !id_drag_from )
2210 {
2211 v2i new_begin = { world.tile_x - 2, world.tile_y - 2 };
2212 v2i new_end = { world.tile_x + 2, world.tile_y + 2 };
2213
2214 world.data[ world.selected ].state ^= FLAG_CANAL;
2215 map_reclassify( new_begin, new_end, 0 );
2216
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 );
2221
2222 render_tiles( new_begin, new_end, colour_default, colour_default );
2223
2224 world.data[ world.selected ].state ^= FLAG_CANAL;
2225 map_reclassify( new_begin, new_end, 0 );
2226 }
2227
2228 //glDisable(GL_BLEND);
2229
2230 // Draw connecting wires
2231 glDisable(GL_BLEND);
2232
2233 SHADER_USE( shader_wire );
2234 glBindVertexArray( world.wire.vao );
2235
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 );
2238
2239 if( id_drag_from )
2240 {
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) );
2245 }
2246
2247 float rp_x1 = world.frame_lerp*9.0f;
2248 float rp_x2 = 1.0f-rp_x1*expf(1.0f-rp_x1)* 0.36f;
2249
2250 for( int y = 2; y < world.h-2; y ++ )
2251 {
2252 for( int x = 2; x < world.w-2; x ++ )
2253 {
2254 struct cell *cell = pcell((v2i){x,y});
2255
2256 if( cell->state & FLAG_CANAL )
2257 {
2258 if( cell->state & FLAG_IS_TRIGGER )
2259 {
2260 int trigger_id = cell->links[0]?0:1;
2261
2262 int x2 = cell->links[trigger_id] % world.w;
2263 int y2 = (cell->links[trigger_id] - x2) / world.w;
2264
2265 v2f startpoint;
2266 v2f endpoint;
2267
2268 startpoint[0] = (float)x2 + (trigger_id? 0.75f: 0.25f);
2269 startpoint[1] = (float)y2 + 0.25f;
2270
2271 endpoint[0] = x+0.5f;
2272 endpoint[1] = y+0.5f;
2273
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) );
2278 }
2279 }
2280 }
2281 }
2282
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 );
2286
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;
2291
2292 // Draw i/o arrays
2293 for( int i = 0; i < arrlen( world.io ); i ++ )
2294 {
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;
2299
2300 v4f dot_colour = { 0.0f, 0.0f, 0.0f, 1.0f };
2301
2302 for( int k = 0; k < term->run_count; k ++ )
2303 {
2304 for( int j = 0; j < term->runs[k].condition_count; j ++ )
2305 {
2306 float y_offset = is_input? 1.2f: -0.2f;
2307 y_offset += (is_input? 0.2f: -0.2f) * (float)k;
2308
2309 glUniform3f( SHADER_UNIFORM( shader_tile_colour, "uOffset" ), (float)posx + 0.2f + 0.2f * (float)j, (float)posy + y_offset, 0.1f );
2310
2311 if( is_input )
2312 {
2313 colour_code_v3( term->runs[k].conditions[j], dot_colour );
2314 glUniform4fv( SHADER_UNIFORM( shader_tile_colour, "uColour" ), 1, dot_colour );
2315
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 );
2319 else
2320 draw_mesh( filled_start, filled_count );
2321 }
2322 else
2323 {
2324 if( term->runs[k].recv_count > j )
2325 {
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 );
2329
2330 draw_mesh( filled_start, filled_count );
2331 }
2332
2333 colour_code_v3( term->runs[k].conditions[j], dot_colour );
2334 glUniform4fv( SHADER_UNIFORM( shader_tile_colour, "uColour" ), 1, dot_colour );
2335
2336 draw_mesh( empty_start, empty_count );
2337 }
2338 }
2339 }
2340 }
2341
2342 if( world.simulating )
2343 {
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 );
2347 }
2348
2349 // Draw score
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 );
2353
2354 use_mesh( &world.numbers );
2355 draw_numbers( (v3f){ 2.0f, (float)world.h-1.875f, 0.3333f }, world.score );
2356 }
2357
2358 static ui_colourset flcol_list_a = {
2359 .main = 0xff877979,
2360 .hover = 0xffa09393,
2361 .active = 0xffbfb1b0
2362 };
2363 static ui_colourset flcol_list_b = {
2364 .main = 0xff7c6e6e,
2365 .hover = 0xffa09393,
2366 .active = 0xffbfb1b0
2367 };
2368
2369 static ui_colourset flcol_list_complete_a = {
2370 .main = 0xff62a064,
2371 .hover = 0xff8dc18f,
2372 .active = 0xffb2ddb3
2373 };
2374
2375 static ui_colourset flcol_list_complete_b = {
2376 .main = 0xff79b37b,
2377 .hover = 0xff8dc18f,
2378 .active = 0xffb2ddb3
2379 };
2380
2381 static ui_colourset flcol_list_locked = {
2382 .main = 0xff655959,
2383 .hover = 0xff655959,
2384 .active = 0xff655959
2385 };
2386
2387 static struct
2388 {
2389 SteamLeaderboard_t steam_leaderboard;
2390 int leaderboard_show;
2391
2392 struct leaderboard_player
2393 {
2394 // Internal
2395 u64_steamid id;
2396 i32 score;
2397 int is_local_player;
2398
2399 // To be displayed
2400 char score_text[ 16 ];
2401 char player_name[ 48 ];
2402 GLuint texture; // Not dynamic
2403 }
2404 leaderboard_players[10];
2405 int leaderboard_count;
2406
2407 struct
2408 {
2409 struct cmp_level *level;
2410
2411 i32 score;
2412 int is_waiting;
2413 }
2414 upload_request;
2415
2416 struct cmp_level *level_selected;
2417 }
2418 ui_data;
2419
2420 void vg_ui(void)
2421 {
2422 // UI memory
2423 static int pack_selection = 0;
2424 static struct pack_info
2425 {
2426 struct cmp_level *levels;
2427 u32 level_count;
2428 const char *name;
2429 }
2430 pack_infos[] =
2431 {
2432 {
2433 .levels = cmp_levels_tutorials,
2434 .level_count = vg_list_size(cmp_levels_tutorials),
2435 .name = "Training"
2436 },
2437 {
2438 .levels = cmp_levels_basic,
2439 .level_count = vg_list_size(cmp_levels_basic),
2440 .name = "Main"
2441 },
2442 {
2443 .levels = cmp_levels_grad,
2444 .level_count = vg_list_size(cmp_levels_tutorials),
2445 .name = "Expert"
2446 }
2447 };
2448
2449 // UI Code
2450 ui_global_ctx.cursor[0] = 0;
2451 ui_global_ctx.cursor[1] = 0;
2452 ui_global_ctx.cursor[2] = 256;
2453
2454 gui_fill_y();
2455
2456 ui_global_ctx.id_base = 4 << 16;
2457
2458 gui_new_node();
2459 {
2460 gui_capture_mouse( 9999 );
2461 gui_fill_rect( ui_global_ctx.cursor, 0xff5a4e4d );
2462
2463 gui_text( "ASSIGNMENTS", 8, 0 );
2464
2465 ui_global_ctx.cursor[1] += 30;
2466 ui_global_ctx.cursor[3] = 25;
2467
2468 gui_new_node();
2469 {
2470 ui_rect_pad( ui_global_ctx.cursor, 2 );
2471 ui_global_ctx.cursor[2] = 84;
2472
2473 for( int i = 0; i < 3; i ++ )
2474 {
2475 if( i == pack_selection )
2476 gui_override_colours( &flcol_list_locked );
2477
2478 if( gui_button( 2000 + i ) == k_button_click )
2479 pack_selection = i;
2480
2481 ui_global_ctx.cursor[1] += 2;
2482 gui_text( pack_infos[i].name, 4, 0 );
2483 gui_end_right();
2484
2485 gui_reset_colours();
2486 }
2487 }
2488 gui_end_down();
2489
2490 ui_global_ctx.cursor[3] = 500;
2491
2492 // DRAW LEVEL SELECTION LIST
2493 {
2494 struct cmp_level *levels = pack_infos[ pack_selection ].levels;
2495 int count = pack_infos[ pack_selection ].level_count;
2496 int unlocked = 3000;
2497
2498 static struct ui_scrollbar sb = {
2499 .bar_height = 400
2500 };
2501
2502 ui_px view_height = ui_global_ctx.cursor[3];
2503 ui_px level_height = 50;
2504
2505 // Level scroll view
2506 gui_new_node();
2507 {
2508 gui_fill_rect( ui_global_ctx.cursor, 0xff5a4e4d );
2509 gui_set_clip( ui_global_ctx.cursor );
2510
2511 ui_global_ctx.cursor[2] = 14;
2512 gui_align_right();
2513
2514 ui_px content_height = count*level_height;
2515 if( content_height > view_height )
2516 {
2517 ui_scrollbar( &ui_global_ctx, &sb, 1 );
2518 ui_global_ctx.cursor[1] -= ui_calculate_content_scroll( &sb, content_height );
2519 }
2520 else
2521 {
2522 gui_fill_rect( ui_global_ctx.cursor, 0xff807373 );
2523 }
2524
2525 ui_global_ctx.cursor[2] = 240;
2526 ui_global_ctx.cursor[3] = level_height;
2527 gui_align_left();
2528
2529 for( int i = 0; i < count; i ++ )
2530 {
2531 struct cmp_level *lvl_info = &levels[i];
2532
2533 if( i < unlocked )
2534 {
2535 if( lvl_info->completed_score != 0 )
2536 gui_override_colours( i&0x1? &flcol_list_complete_a: &flcol_list_complete_b );
2537 else
2538 gui_override_colours( i&0x1? &flcol_list_a: &flcol_list_b );
2539 }
2540 else
2541 gui_override_colours( &flcol_list_locked );
2542
2543 if( i < unlocked )
2544 {
2545 if( gui_button( 2 + i ) == k_button_click )
2546 {
2547 ui_data.level_selected = &levels[i];
2548 ui_data.leaderboard_show = 0;
2549
2550 if( pack_selection >= 1 )
2551 sw_find_leaderboard( ui_data.level_selected->map_name );
2552 }
2553
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 );
2558 }
2559 else
2560 {
2561 gui_button( 2 + i );
2562
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 );
2567 }
2568
2569 gui_end_down();
2570 }
2571
2572 gui_reset_colours();
2573 gui_release_clip();
2574 }
2575 gui_end_down();
2576 }
2577 }
2578 gui_end_right();
2579
2580 // Selected level UI
2581 // ============================================================
2582
2583 if( ui_data.level_selected )
2584 {
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;
2589
2590 gui_new_node();
2591 {
2592 gui_capture_mouse( 9999 );
2593
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 );
2597
2598 ui_global_ctx.cursor[1] += 30;
2599 ui_rect_pad( ui_global_ctx.cursor, 8 );
2600 ui_global_ctx.cursor[3] = 300;
2601
2602 gui_new_node();
2603 {
2604 gui_fill_rect( ui_global_ctx.cursor, 0xff655959 );
2605 }
2606 gui_end_down();
2607
2608 ui_text_use_paragraph( &ui_global_ctx );
2609 ui_global_ctx.cursor[1] += 2;
2610
2611 gui_text( ui_data.level_selected->description, 5, 0 );
2612 ui_text_use_title( &ui_global_ctx );
2613
2614 // Buttons at the bottom
2615 ui_global_ctx.cursor[3] = 25;
2616 ui_global_ctx.cursor[2] = 80;
2617
2618 gui_align_bottom();
2619 ui_global_ctx.cursor[1] -= 8;
2620
2621 if( gui_button( 3000 ) == k_button_click )
2622 {
2623 ui_data.level_selected = NULL;
2624 }
2625 gui_text( "BACK", 6, k_text_alignment_center );
2626 gui_end();
2627
2628 gui_align_right();
2629 ui_global_ctx.cursor[2] = 170;
2630 ui_global_ctx.cursor[0] -= 8 + 170 + 2;
2631
2632 {
2633 gui_override_colours( &flcol_list_locked );
2634 if( gui_button( 3001 ) == k_button_click )
2635 vg_error( "UNIMPLEMENTED\n" );
2636
2637 ui_global_ctx.override_colour = 0xff888888;
2638
2639 gui_text( "RESTORE SOLUTION", 6, k_text_alignment_center );
2640 gui_end_right();
2641 ui_global_ctx.override_colour = 0xffffffff;
2642 }
2643
2644 ui_global_ctx.cursor[0] += 2;
2645 ui_global_ctx.cursor[2] = 80;
2646
2647 {
2648 gui_override_colours( &flcol_list_complete_a );
2649 if( gui_button( 3002 ) == k_button_click )
2650 {
2651 console_changelevel( 1, &ui_data.level_selected->map_name );
2652 world.pCmpLevel = ui_data.level_selected;
2653
2654 ui_data.level_selected = NULL;
2655 ui_data.leaderboard_show = 0;
2656 }
2657 gui_text( "PLAY", 6, k_text_alignment_center );
2658 gui_end();
2659 }
2660
2661 gui_reset_colours();
2662 }
2663 gui_end_right();
2664
2665 if( ui_data.leaderboard_show )
2666 {
2667 ui_global_ctx.cursor[0] += 16;
2668 ui_global_ctx.cursor[2] = 350;
2669 ui_global_ctx.cursor[3] = 25;
2670
2671 // If has results
2672 gui_new_node();
2673 {
2674 gui_fill_rect( ui_global_ctx.cursor, 0xff5a4e4d );
2675 gui_text( "FRIEND LEADERBOARD", 6, 0 );
2676 }
2677 gui_end_down();
2678
2679 ui_global_ctx.cursor[1] += 2;
2680
2681 gui_new_node();
2682 {
2683 ui_global_ctx.cursor[3] = 32+8;
2684
2685 for( int i = 0; i < ui_data.leaderboard_count; i ++ )
2686 {
2687 gui_new_node();
2688 {
2689 gui_fill_rect( ui_global_ctx.cursor, i&0x1? flcol_list_a.main: flcol_list_b.main );
2690
2691 ui_global_ctx.cursor[0] += 4;
2692 ui_global_ctx.cursor[1] += 4;
2693
2694 // 1,2,3 ...
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;
2698
2699 struct leaderboard_player *player = &ui_data.leaderboard_players[i];
2700
2701 // Players image
2702 ui_global_ctx.cursor[2] = 32;
2703 ui_global_ctx.cursor[3] = 32;
2704
2705 gui_new_node();
2706 {
2707 gui_push_image( ui_global_ctx.cursor, player->texture );
2708 }
2709 gui_end_right();
2710
2711 // Players name
2712 gui_text( player->player_name, 7, 0 );
2713
2714 ui_global_ctx.cursor[2] = 50;
2715 gui_align_right();
2716
2717 gui_text( player->score_text, 7, k_text_alignment_right );
2718 }
2719 gui_end_down();
2720
2721 ui_global_ctx.cursor[1] += 2;
2722 }
2723 }
2724 gui_end();
2725 }
2726 }
2727 }
2728
2729 void leaderboard_dispatch_score(void)
2730 {
2731 sw_upload_leaderboard_score(
2732 ui_data.upload_request.level->steam_leaderboard,
2733 k_ELeaderboardUploadScoreMethodKeepBest,
2734 ui_data.upload_request.score,
2735 NULL,
2736 0
2737 );
2738
2739 ui_data.upload_request.is_waiting = 0;
2740
2741 vg_success( "Dispatched leaderboard score\n" );
2742 }
2743
2744 void leaderboard_found( LeaderboardFindResult_t *pCallback )
2745 {
2746 if( !pCallback->m_bLeaderboardFound )
2747 {
2748 vg_error( "Leaderboard could not be found\n" );
2749 ui_data.steam_leaderboard = 0;
2750 }
2751 else
2752 {
2753 const char *recieved_name = sw_get_leaderboard_name( pCallback->m_hSteamLeaderboard );
2754
2755 // Update UI state and request entries if this callback found the current UI level
2756 if( ui_data.level_selected )
2757 {
2758 if( !strcmp( recieved_name, ui_data.level_selected->map_name ) )
2759 {
2760 sw_download_leaderboard_entries( pCallback->m_hSteamLeaderboard, k_ELeaderboardDataRequestFriends, 0, 8 );
2761 ui_data.level_selected->steam_leaderboard = pCallback->m_hSteamLeaderboard;
2762 }
2763 }
2764
2765 // Dispatch the waiting request if there was one
2766 if( ui_data.upload_request.is_waiting )
2767 {
2768 if( !strcmp( recieved_name, ui_data.upload_request.level->map_name ) )
2769 {
2770 ui_data.upload_request.level->steam_leaderboard = pCallback->m_hSteamLeaderboard;
2771 leaderboard_dispatch_score();
2772 }
2773 }
2774 }
2775 }
2776
2777 void leaderboard_downloaded( LeaderboardScoresDownloaded_t *pCallback )
2778 {
2779 // Update UI if this leaderboard matches what we currently have in view
2780 if( ui_data.level_selected->steam_leaderboard == pCallback->m_hSteamLeaderboard )
2781 {
2782 vg_info( "Recieved %d entries\n", pCallback->m_cEntryCount );
2783 ui_data.leaderboard_count = VG_MIN( pCallback->m_cEntryCount, 8 );
2784
2785 u64_steamid local_player = sw_get_steamid();
2786
2787 for( int i = 0; i < ui_data.leaderboard_count; i ++ )
2788 {
2789 LeaderboardEntry_t entry;
2790 sw_get_downloaded_entry( pCallback->m_hSteamLeaderboardEntries, i, &entry, NULL, 0 );
2791
2792 struct leaderboard_player *player = &ui_data.leaderboard_players[i];
2793
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;
2797
2798 snprintf( player->score_text, vg_list_size(player->score_text), "%d", player->score );
2799 player->texture = sw_get_player_image( player->id );
2800
2801 if( player->texture == 0 )
2802 player->texture = tex_unkown.name;
2803
2804 player->is_local_player = local_player == player->id? 1: 0;
2805 }
2806
2807 if( ui_data.leaderboard_count )
2808 ui_data.leaderboard_show = 1;
2809 else
2810 ui_data.leaderboard_show = 0;
2811 }
2812 else vg_warn( "Downloaded leaderboard does not match requested!\n" );
2813 }
2814
2815 void leaderboard_set_score( struct cmp_level *cmp_level, u32 score )
2816 {
2817 if( ui_data.upload_request.is_waiting )
2818 vg_warn( "You are uploading leaderboard entries too quickly!\n" );
2819
2820 ui_data.upload_request.level = cmp_level;
2821 ui_data.upload_request.score = score;
2822 ui_data.upload_request.is_waiting = 1;
2823
2824 // If leaderboard ID has been downloaded already then just immediately dispatch this
2825 if( cmp_level->steam_leaderboard )
2826 leaderboard_dispatch_score();
2827 else
2828 sw_find_leaderboard( cmp_level->map_name );
2829 }