-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmap.h
More file actions
635 lines (518 loc) · 17.5 KB
/
map.h
File metadata and controls
635 lines (518 loc) · 17.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
#ifndef MAP_H
#define MAP_H
#include <math.h>
#include <raylib.h>
#include "gdict.h"
#include "cell.h"
#include "format.h"
#include "sprite.h"
#include "player.h"
#include "window.h"
typedef struct Map {
int width, height;
Array(Cell) data;
Array(char*) scripts;
Array(Sprite) sprites;
Player starting_player;
Color floor_colour,
sky_colour;
} Map;
typedef struct MapCast {
Cell *hit;
Vector2 end;
float distance;
int side;
} MapCast;
/* Creates a new, empty map. */
static Map map_new(int width, int height) {
// We have to clear the old textures and initialise the global textures
// This caused me a lot of confusion when debugging LOL
Array(Texture) textures = gdict_get("textures");
if (textures)
for (int i = 0; i < array_len(textures); i++)
UnloadTexture(textures[i]);
array_free(textures);
gdict_set("textures", array_new(0, 0, sizeof(Texture)));
Map map = {
.width = width,
.height = height,
.data = array_new(width * height, width * height, sizeof(Cell)),
.scripts = array_new(0, 0, sizeof(char*)),
.sprites = array_new(0, 0, sizeof(Sprite)),
.starting_player = PLAYER_DEFAULT,
.floor_colour = BLACK,
.sky_colour = BLUE
};
for (int i = 0; i < width * height; i++)
map.data[i] = CELL_DEFAULT;
return map;
}
/* Gets a pointer to the cell at `x, y`, if it exists. */
static Cell *map_get_cell(Map map, int x, int y) {
if (
x >= map.width || x < 0 ||
y >= map.height || y < 0
) return NULL;
return &(map.data[map.width * y + x]);
}
/* Gets a pointer to sprite by ID. */
static Sprite *map_get_sprite(Map map, int sprite_id) {
if (sprite_id < 1 || sprite_id > array_len(map.sprites))
return NULL;
return &(map.sprites[sprite_id - 1]);
}
/* Gets a script by ID. */
static char *map_get_script(Map map, int script_id) {
if (script_id < 1 || script_id > array_len(map.scripts))
return NULL;
return map.scripts[script_id - 1];
}
/* Gets a sprite texture by ID. */
static Texture map_get_texture(Map map, int sprite_id) {
Sprite *sprite = map_get_sprite(map, sprite_id);
if (!sprite)
return (Texture) { 0 };
else {
return ((Array(Texture))gdict_get("textures"))[sprite_id - 1];
}
}
/* Adds `sprite` to `map` and loads it properly. */
static void map_add_sprite(Map *map, Sprite sprite) {
if (sprite.pixels == NULL) // This shouldn't happen :/
return;
Texture tex = sprite_load(sprite);
if (tex.id == 0) // Again, shouldn't happen!
return;
gdict_set("textures", array_append(gdict_get("textures"), &tex));
map->sprites = array_append(map->sprites, &sprite);
}
/* Removes sprite of `sprite_id` from `map` safely. */
static void map_rem_sprite(Map map, int sprite_id) {
Sprite *sprite = map_get_sprite(map, sprite_id);
if (!sprite)
return;
int real_index = sprite_id - 1;
// Free the sprite
sprite_free(*sprite);
// Unload and remove tex
Array(Texture) textures = gdict_get("textures");
UnloadTexture(textures[real_index]);
array_pop(textures, real_index);
// Remove sprite
array_pop(map.sprites, real_index);
}
/* Adds `script` to `map`. */
static void map_add_script(Map *map, char *script) {
map->scripts = array_append(map->scripts, &script);
}
/* Removes `script` from `map` by ID. */
static void map_rem_script(Map map, int script_id) {
if (!map_get_script(map, script_id))
return;
int real_index = script_id - 1;
free(map.scripts[real_index]);
array_pop(map.scripts, real_index);
}
/* Gets the position of a pointer to a `Cell` in `map.data`. */
static Vector2 map_cell_pos(Map map, Cell *cell) {
if (!cell)
return (Vector2) { -1 };
for (int y = 0; y < map.height; y++)
for (int x = 0; x < map.width; x++)
if (map_get_cell(map, x, y) == cell)
return (Vector2) { x, y };
return (Vector2) { -1 };
}
/* Loads a map from `data`. */
static Map map_load(char *data) {
// Create the format table
Format *table = format_table(
format_obj(int),
format_obj(int),
format_fixed_list(1,
format_obj(Cell)
),
format_fixed_list(1,
format_fixed_list(1,
format_obj(char)
)
),
format_fixed_list(1,
format_Sprite
),
format_obj(Player),
format_obj(Color),
format_obj(Color)
);
// Extract
char *extracted = format_extract(table, data, NULL);
Map map = { 0 };
if (!extracted) { // FAIL!!
fprintf(stderr, "Failed to extract map data.\n");
return map;
}
// Unpack
map = *(Map*)array_flatten(extracted);
// Convert to `Array`s
map.data = array_from(map.data, table[2].size, sizeof(Cell));
map.scripts = array_from(map.scripts, table[3].size, sizeof(char*));
map.sprites = array_from(map.sprites, table[4].size, sizeof(Sprite));
// If another map was using textures, sorry!
Array(Texture) textures = gdict_get("textures");
if (textures) {
for (int i = 0; i < array_len(textures); i++)
UnloadTexture(textures[i]);
array_free(textures);
}
// Now load textures
textures = array_new(0, array_len(map.sprites), sizeof(Texture));
for (int i = 0; i < array_len(map.sprites); i++) {
Texture tex = sprite_load(map.sprites[i]);
textures = array_append(textures, &tex);
}
// Then store it in the global dictionary
gdict_set("textures", textures);
return map;
}
/* Loads a map from `filename`. */
static Map map_load_filename(const char *filename) {
FILE *f = fopen(filename, "rb");
if (!f)
return (Map) { 0 };
fseek(f, 0, SEEK_END);
long fs = ftell(f);
rewind(f);
char *map_data = (char*)malloc(fs + 1);
fread(map_data, 1, fs, f);
fclose(f);
// LOAD!!!
Map map = map_load(map_data);
free(map_data);
return map;
}
/* Saves a map to `data`. */
static Array(char) map_dump(const Map map) {
// Create the format table
Format *table = format_table(
format_obj(int),
format_obj(int),
format_fixed_list(array_len(map.data),
format_obj(Cell)
),
format_fixed_list(array_len(map.scripts),
format_cstr
),
format_fixed_list(array_len(map.sprites),
format_Sprite
),
format_obj(Player),
format_obj(Color),
format_obj(Color)
);
// Apply it!
Array(char) data = format_apply(table, (char*)&map, NULL);
if (!data) {
fprintf(stderr, "Failed to dump map!\n");
return NULL;
}
return data;
}
/* Dumps `map` to `filename`. */
static bool map_dump_filename(const Map map, const char *filename) {
FILE *f = fopen(filename, "wb");
if (!f)
return false;
// Dump map
Array(char) map_data = map_dump(map);
if (!map_data)
return false;
if (fwrite(map_data, 1, array_len(map_data), f) != array_len(map_data))
return false;
fclose(f);
array_free(map_data);
return true;
}
/* Frees everything `map`! */
static void map_free(Map map) {
// Free data
array_free(map.data);
// Free scripts
for (size_t i = 0; i < array_len(map.scripts); i++)
free(map.scripts[i]);
array_free(map.scripts);
// Free sprites
for (size_t i = 0; i < array_len(map.sprites); i++)
sprite_free(map.sprites[i]);
array_free(map.sprites);
// Free textures from global dict
Array(Texture) textures = gdict_get("textures");
if (textures) {
for (size_t i = 0; i < array_len(textures); i++)
UnloadTexture(textures[i]);
array_free(textures);
}
// Clear textures key
gdict_clear("textures");
}
/* Changes `map`'s size by `x`. */
static void map_size(Map *map, int x) {
int new_width = (map->width + x > 1) ? map->width + x : 1,
new_height = (map->height + x > 1) ? map->width + x : 1;
Array(Cell) new_data = array_new(
new_width * new_height,
new_width * new_height,
sizeof(Cell)
);
// Copy new data over
for (int y = 0; y < new_height; y++)
for (int x = 0; x < new_width; x++)
if (y < map->height && x < map->width)
new_data[y * new_width + x] = map->data[y * map->width + x];
else
new_data[y * new_width + x] = CELL_DEFAULT;
// Replace
array_free(map->data);
map->data = new_data;
map->width = new_width;
map->height = new_height;
}
#define map_grow(map) map_size(map, 1)
#define map_shrink(map) map_size(map, -1)
/* Returns `true` if `x, y` is the player's starting position. */
static bool map_spawn(Map map, int x, int y) {
return
floor(map.starting_player.position.x) == x
&& floor(map.starting_player.position.y) == y;
}
static float _map_cast_circle(Vector2 start, Vector2 direction, Circle cl) {
// Calculate the coefficients
float dx = start.x - cl.center.x;
float dy = start.y - cl.center.y;
float A = direction.x * direction.x + direction.y * direction.y;
float B = 2 * (dx * direction.x + dy * direction.y);
float C = dx * dx + dy * dy - cl.radius * cl.radius;
float discriminant = B * B - 4 * A * C;
if (discriminant < 0)
return -1;
float sqrt_discriminant = sqrt(discriminant);
float t1 = (-B - sqrt_discriminant) / (2 * A);
float t2 = (-B + sqrt_discriminant) / (2 * A);
if (t1 > 0 && t2 > 0)
return fmin(t1, t2);
else if (t1 > 0)
return t1;
else if (t2 > 0)
return t2;
return -1;
}
static float _map_cast_triangle(Vector2 start, Vector2 direction, Triangle tr) {
return 10;
}
static MapCast map_cast(Map map, Vector2 position, float angle, Cell *ignore) {
// Direction vector
Vector2 direction = (Vector2) {
cos(angle),
sin(angle)
};
Vector2 delta_dist = (Vector2) {
fabs(1 / direction.x),
fabs(1 / direction.y)
};
Vector2 step = (Vector2) {
direction.x < 0 ? -1 : 1,
direction.y < 0 ? -1 : 1
};
Vector2 map_pos = (Vector2) {
floor(position.x),
floor(position.y)
};
Vector2 side_dist = (Vector2) {
direction.x < 0 ? (position.x - map_pos.x) * delta_dist.x : (map_pos.x + 1 - position.x) * delta_dist.x,
direction.y < 0 ? (position.y - map_pos.y) * delta_dist.y : (map_pos.y + 1 - position.y) * delta_dist.y
};
int hit = 0;
int side = 0;
float perp_dist = -1;
while (hit == 0 && (map_pos.x >= 0 && map_pos.x < map.width) && (map_pos.y >= 0 && map_pos.y < map.height)) {
if (side_dist.x < side_dist.y) {
side_dist.x += delta_dist.x;
map_pos.x += step.x;
side = 0;
} else {
side_dist.y += delta_dist.y;
map_pos.y += step.y;
side = 1;
}
Cell *cell = map_get_cell(map, map_pos.x, map_pos.y);
if (cell && cell == ignore)
continue;
if (cell)
if (cell->sprite_id != 0)
switch (cell->shape) {
case S_SQUARE:
hit = 1;
break;
case S_CIRCLE:
perp_dist = _map_cast_circle(position, direction,
circle((Vector2) { map_pos.x + 0.5f, map_pos.y + 0.5f }, 0.4f));
if (perp_dist != -1)
hit = 1;
break;
case S_TRIANGLE:
perp_dist = _map_cast_triangle(position, direction,
triangle(
(Vector2) { map_pos.x, map_pos.y },
(Vector2) { map_pos.x, map_pos.y + 1.0f },
(Vector2) { map_pos.x + 1.0f, map_pos.y + 1.0f }
)
);
if (perp_dist != -1)
hit = 1;
break;
}
}
if (perp_dist == -1)
if (side == 0) {
perp_dist = (map_pos.x - position.x + (1 - step.x) / 2) / direction.x;
} else {
perp_dist = (map_pos.y - position.y + (1 - step.y) / 2) / direction.y;
}
return (MapCast) {
.hit = map_get_cell(map, floor(map_pos.x), floor(map_pos.y)),
.end = (Vector2) {
position.x + perp_dist * direction.x,
position.y + perp_dist * direction.y
},
.distance = perp_dist,
.side = side
};
}
/* Draws a map in 3 dimensions! */
static void map_draw_3d(Map map, Player player, Vector2 position, int length, int FOV) {
// Draw floor and ceiling
DrawRectangle(0, 0, length, length / 2, map.floor_colour);
DrawRectangle(0, length / 2, length, length / 2, map.sky_colour);
for (float x = 0; x < length; x++) {
float angle = x * (PI / 180) * FOV / length + (player.looking - PI / 6);
MapCast cast = map_cast(map, player.position, angle, NULL);
// Hit?
if (!cast.hit) {
continue;
}
// Get the texture, if not any, skip
Texture texture = map_get_texture(map, cast.hit->sprite_id);
if (texture.id == 0) {
continue;
}
int single_wall_height = (length / cast.distance);
int wall_height = single_wall_height * cast.hit->height;
int y_pos = length / 2 - length / cast.distance / 2 + 0/*bobbing_offset*/;
// Vertical offset & wall height
y_pos -= (single_wall_height * (cast.hit->vertical_offset + (cast.hit->height - 1)));
// Get the position in the texture to draw
float wall_hit_pos = (cast.side != 0) ? cast.end.x : cast.end.y;
int texture_x = (int)(wall_hit_pos * texture.width) % texture.width;
if (texture_x < 0) texture_x += texture.width;
Rectangle source_rect = {
texture_x,
0,
1,
texture.height
};
Rectangle to_rect = {
x,
y_pos,
1,
wall_height
};
if (cast.distance != 0)
DrawTexturePro(
texture,
source_rect, to_rect,
(Vector2) {0, 0}, 0,
cast.hit->shape == S_SQUARE
? cast.side
? WHITE
: (Color) { 200, 200, 200, 255 }
: WHITE);
}
}
/* Draws map in 2D from a top down perspective.
* Returns the highlighted cell. */
static Cell *map_draw_2d(Map map, Player player, Vector2 pos, Cell *selected, int length,
bool digits_mode, bool highlight_enabled, bool grid_enabled,
bool draw_player, bool shoot_rays) {
int scale = map.width > map.height ? map.width : map.height;
int step = length / scale;
Vector2 mouse = GetMousePosition();
// DRAW!!!
Cell *highlighted = NULL;
for (int y = 0; y < scale; y++) {
for (int x = 0; x < scale; x++) {
// Target rect
Rectangle target = (Rectangle) {
.x = pos.x + (x * step),
.y = pos.y + (y * step),
.width = step,
.height = step
};
Cell *cell = map_get_cell(map, x, y);
if (!cell) { // Practically impossible
continue;
}
cell_draw(*cell, target, digits_mode, map.floor_colour);
// Grid
if (grid_enabled)
DrawRectangleLinesEx(
target, 0.5, WHITE
);
// Highlighted cell
if (highlight_enabled)
if (CheckCollisionPointRec(mouse, target)) {
highlighted = cell;
DrawRectangleLinesEx(
target, 2, GREEN
);
}
// Selected cell
if (cell == selected)
DrawRectangleLinesEx(
target, 2, YELLOW
);
// Mark player position
if (draw_player
&& floor(player.position.x) == x
&& floor(player.position.y) == y) {
int half_step = step / 2;
int center_x = target.x + half_step,
center_y = target.y + half_step;
DrawRectangle(
center_x - half_step / 2,
center_y - half_step / 2,
half_step, half_step, RED
);
Vector2 dest = {
.x = center_x + half_step * cos(player.looking * PI / 180),
.y = center_y + half_step * sin(player.looking * PI / 180)
};
DrawLineEx((Vector2) { center_x, center_y }, dest, 3, WHITE);
}
}
}
// Shoot rays!
if (shoot_rays)
for (float i = -0.5; i < 0.5; i += 0.01) {
MapCast cast = map_cast(map, player.position, player.looking + i, NULL);
Vector2 position = {
.x = (player.position.x + 0.5f) * step,
.y = (player.position.y + 0.5f) * step
};
Vector2 end = {
.x = cast.end.x * step,
.y = cast.end.y * step
};
DrawLineEx(position, end, 1, RED);
}
return highlighted;
}
#endif