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