aboutsummaryrefslogtreecommitdiff
path: root/map.c
blob: 9566aa264dbed8330cf7d1bcd4290f3d28bbdb49 (plain) (blame)
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
#include "asset.h"
#include "random.h"
#include "map.h"
#include "rect.h"
#include "render.h"
#include "world.h"

#define null_tile 0xffff

Rect tile_rects[] = {
#define x(n, x, y) \
	{ x, y, map_tile_size, map_tile_size },
	tiles_xmacro
#undef x
};

void generate_room(Map* m) {
	const Bitmap* b = get_bitmap(asset_id_rooms_img);
	const unsigned* p = (const unsigned*)&b[1];
	const int max_rooms = b->h / map_h;
	const int r = /*get_r() % max_rooms;*/ 1;
	int o = r * map_w * map_h;
	int e = map_w * map_h, i;
	for (i = 0; i < e; i++, o++) {
		int bit = 1 << (o & 0x1f);
		if (p[o >> 5] & bit)
			m->tiles[i] = tile_brick;
		else
			m->tiles[i] = null_tile;
	}
}

unsigned short get_collider(Map_Tile t) {
	switch (t) {
		case tile_stone:
		case tile_block1:
		case tile_block2:
		case tile_window_top:
		case tile_window_mid:
		case tile_window_bot:
		case tile_brick_floor:
		case tile_brick: return 1;
		case tile_brick_ramp5:
		case tile_stone_ramp5: return 2;
		case tile_brick_ramp6:
		case tile_stone_ramp6: return 3;
		case tile_brick_ramp7:
		case tile_stone_ramp7: return 4;
		case tile_brick_ramp8:
		case tile_stone_ramp8: return 5;
		default:
			return 0;
	}
}

void generate_collision(Map* m) {
	int e = map_w * map_h, i;
	for (i = 0; i < e; i++)
		m->collision[i] = get_collider(m->tiles[i]);
}

int imp_check(Map* m, Map_Tile t, int x, int y) {
	return
		x >= 0 &&
		y >= 0 &&
		x < map_w &&
		y < map_h &&
		m->tiles[x + y * map_w] == t;
}

#define check(ox, oy, t) \
	imp_check(m, t, x + ox, y + oy)
#define place(t) \
	m->tiles[x + y * map_w] = t
#define place_o(t, ox, oy) \
	m->tiles[x + ox + (y + oy) * map_w] = t

void generate_ramps(Map* m) {
	int x, y;
	for (y = 0; y < map_h; y++)
		for (x = 0; x < map_w; x++) {
			if (
				check( 0,  0, null_tile) &&
				check( 1,  0, null_tile) &&
				check(-1,  0, tile_brick) &&
				!check(-1, -1, tile_brick) &&
				check( 0,  1, tile_brick) &&
				check( 1,  1, tile_brick)
			) {
				place(tile_brick_ramp7);
				place_o(tile_brick_ramp8, 1, 0);
			}
			if (
				check( 0,  0, null_tile) &&
				check(-1,  0, null_tile) &&
				check( 1,  0, tile_brick) &&
				!check( 1, -1, tile_brick) &&
				check( 0,  1, tile_brick) &&
				check(-1,  1, tile_brick)
			) {
				place(tile_brick_ramp6);
				place_o(tile_brick_ramp5, -1, 0);
			}
			if (
				check(0, 0, tile_brick) && 
				check(0, 1, tile_brick) && 
				check(0, 2, tile_brick) && ((
				check(1, 0, null_tile) && 
				check(1, 1, null_tile) && 
				check(1, 2, null_tile)) || (
				check(-1, 0, null_tile) && 
				check(-1, 1, null_tile) && 
				check(-1, 2, null_tile)))
			) {
				place(tile_window_top);
				place_o(tile_window_mid, 0, 1);
				place_o(tile_window_bot, 0, 2);
			}
		}
}

void generate_floors(Map* m) {
	int x, y;
	for (y = 0; y < map_h; y++)
		for (x = 0; x < map_w; x++) {
			if (
				check(0, 0, tile_brick) &&
				check(0, -1, null_tile)
			) place(tile_brick_floor);
		}
}

#undef check
#undef place

void generate_floor(Map* m, World* w) {
	generate_room(m);
	generate_ramps(m);
	generate_floors(m);
	generate_collision(m);
	w->player.x = 16 << fbits;
	w->player.y = 16 << fbits;
}

void render_map(const Map* m, Renderer* r) {
	const Bitmap* tm = get_bitmap(asset_id_map_img);
	int t = 0, x, y;
	for (y = 0; y < map_h; y++) {
		for (x = 0; x < map_w; x++, t++) {
			int tile = m->tiles[t];
			if (tile != null_tile)
				ren_map(
					r,
					x * map_tile_size,
					y * map_tile_size,
					&tile_rects[tile],
					tm
				);
		}
	}
}