-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmap_flat.c
401 lines (308 loc) · 9.06 KB
/
map_flat.c
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
#include <stdbool.h>
#include <glib.h>
#include <SDL.h>
#include <SDL_ttf.h>
#include "types.h"
#include "platform.h"
#include "console.h"
#include "protocol.h"
#include "world.h"
#include "map.h"
struct state
{
struct flat_mode flat_mode;
bool track_pickups;
bool track_mobs;
};
static char *describe(void *data, GPtrArray *attribs)
{
struct state *state = data;
char *name = state->flat_mode.describe(state->flat_mode.data, attribs);
if (state->track_pickups) g_ptr_array_add(attribs, "pickups");
if (state->track_mobs) g_ptr_array_add(attribs, "mobs");
return name;
}
static int indicator_scale(void)
{
if (map_scale <= 5)
return map_scale + 2;
else if (map_scale <= 9)
return map_scale;
else
return map_scale - 2;
}
static coord_t s2w_offset(int sx, int sy, jint *xo, jint *zo)
{
/* Pixel map_w/2 equals middle (rounded down) of block player_pos.x.
* Pixel map_w/2 - (map_scale-1)/2 equals left edge of block player_pos.x.
* Compute offset from there, divide by scale, round toward negative. */
int px = map_w/2 - (map_scale-1)/2;
int py = map_h/2 - (map_scale-1)/2;
int dx = sx - px, dy = sy - py;
dx = dx >= 0 ? dx/map_scale : (dx-(map_scale-1))/map_scale;
dy = dy >= 0 ? dy/map_scale : (dy-(map_scale-1))/map_scale;
*xo = sx - (px + dx*map_scale);
*zo = sy - (py + dy*map_scale);
return COORD(player_pos.x + dx, player_pos.z + dy);
}
static coord3_t s2w(void *data, int sx, int sy)
{
struct state *state = data;
jint xo, zo;
coord_t cc = s2w_offset(sx, sy, &xo, &zo);
jint y = -1;
struct chunk *c = world_chunk(cc, false);
if (c)
{
jint cx = CHUNK_XOFF(cc.x);
jint cz = CHUNK_ZOFF(cc.z);
y = state->flat_mode.mapped_y(state->flat_mode.data, c, c->blocks[cx][cz], cx, cz);
}
return COORD3(cc.x, y, cc.z);
}
static void w2s(void *data, coord_t cc, int *sx, int *sy)
{
int px = map_w/2 - (map_scale-1)/2;
int py = map_h/2 - (map_scale-1)/2;
*sx = px + (cc.x - player_pos.x)*map_scale;
*sy = py + (cc.z - player_pos.z)*map_scale;
}
static bool handle_key(void *data, SDL_KeyboardEvent *e)
{
struct state *state = data;
switch (e->keysym.unicode)
{
case 'p':
state->track_pickups ^= true;
map_mode_changed();
return true;
case 'm':
state->track_mobs ^= true;
map_mode_changed();
return true;
default:
return state->flat_mode.handle_key(state->flat_mode.data, e);
}
}
static void update_player_pos(void *data)
{
struct state *state = data;
state->flat_mode.update_player_pos(state->flat_mode.data);
}
static void update_time(void *data)
{
struct state *state = data;
state->flat_mode.update_time(state->flat_mode.data);
}
static void draw_player(void *data, SDL_Surface *screen)
{
/* determine transform from player direction */
int txx, txy, tyx, tyy;
switch (player_yaw >> 1)
{
case 0: txx = 1, txy = 0, tyx = 0, tyy = 1; break;
case 1: txx = 0, txy = -1, tyx = 1, tyy = 0; break;
case 2: txx = -1, txy = 0, tyx = 0, tyy = -1; break;
case 3: txx = 0, txy = 1, tyx = -1, tyy = 0; break;
default: wtff("player_yaw = %d", player_yaw);
}
int s = indicator_scale();
int x0, y0;
w2s(data, coord3_xz(player_pos), &x0, &y0);
x0 += (map_scale - s)/2;
y0 += (map_scale - s)/2;
if (txx < 0 || txy < 0) x0 += s-1;
if (tyx < 0 || tyy < 0) y0 += s-1;
/* mechanism for drawing points */
SDL_LockSurface(screen);
unsigned char *pixels = screen->pixels;
uint16_t pitch = screen->pitch;
#define PT(ix, iy) \
do { \
int sx = x0 + txx*ix + txy*iy; \
int sy = y0 + tyx*ix + tyy*iy; \
uint32_t *p = (uint32_t *)&pixels[sy*pitch + sx*4]; \
/* TODO: Handle alpha in flat mode */ \
*p = pack_rgb(ignore_alpha(special_colors[COLOR_PLAYER])); \
} while (0)
/* draw the triangle shape */
if (player_yaw & 1)
{
/* diagonal */
for (int iy = 0; iy < s; iy++)
for (int ix = 0; ix <= iy; ix++)
PT(ix, iy);
}
else
{
/* cardinal */
int gap = 0;
for (int iy = s == 3 ? 1 : s/4; iy < s; iy++)
{
for (int ix = gap; ix < s-gap; ix++)
PT(ix, iy);
gap++;
}
}
#undef PT
SDL_UnlockSurface(screen);
}
static void draw_entity(void *data, SDL_Surface *screen, struct entity *e)
{
struct state *state = data;
if (e->type == ENTITY_MOB && !state->track_mobs)
return;
if (e->type == ENTITY_PICKUP && !state->track_pickups)
return;
rgba_t color;
switch (e->type)
{
case ENTITY_PLAYER: color = special_colors[COLOR_PLAYER]; break;
case ENTITY_MOB: color = special_colors[COLOR_MOB]; break;
case ENTITY_PICKUP: color = special_colors[COLOR_PICKUP]; break;
default: wtff("bad entity type: %d", e->type);
}
int s = indicator_scale();
int ex, ez;
w2s(data, e->pos, &ex, &ez);
ex += (map_scale - s)/2;
ez += (map_scale - s)/2;
if (ex < 0 || ez < 0 || ex+s > map_w || ez+s > map_h)
return;
SDL_Rect r = { .x = ex, .y = ez, .w = s, .h = s };
// TODO: handle alpha in surface mode
SDL_FillRect(screen, &r, pack_rgb(ignore_alpha(color)));
}
static void paint_chunk(void *data, SDL_Surface *region, coord_t cc)
{
struct state *state = data;
jint cxo = CHUNK_XIDX(REGION_XOFF(cc.x));
jint czo = CHUNK_ZIDX(REGION_ZOFF(cc.z));
struct chunk *c = world_chunk(cc, false);
if (!c)
return;
SDL_LockSurface(region);
uint32_t pitch = region->pitch;
unsigned char *pixels = (unsigned char *)region->pixels + czo*CHUNK_ZSIZE*pitch + cxo*CHUNK_XSIZE*4;
unsigned char *blocks = &c->blocks[0][0][0];
unsigned blocks_pitch = CHUNK_YSIZE;
unsigned blocks_xpitch = CHUNK_ZSIZE*blocks_pitch;
for (jint bz = 0; bz < CHUNK_ZSIZE; bz++)
{
uint32_t *p = (uint32_t*)pixels;
unsigned char *b = blocks;
for (jint bx = 0; bx < CHUNK_XSIZE; bx++)
{
jint y = state->flat_mode.mapped_y(state->flat_mode.data, c, b, bx, bz);
rgba_t rgba = state->flat_mode.block_color(state->flat_mode.data, c, b, bx, bz, y);
*p++ = pack_rgb(rgba);
b += blocks_xpitch;
}
pixels += pitch;
blocks += blocks_pitch;
}
SDL_UnlockSurface(region);
}
static void paint_region(void *data, struct map_region *region)
{
jint cidx = 0;
/* make sure the region has a surface for painting */
if (!region->surf)
{
region->surf = SDL_CreateRGBSurface(SDL_SWSURFACE, REGION_XSIZE, REGION_ZSIZE, 32,
screen_fmt->Rmask, screen_fmt->Gmask, screen_fmt->Bmask, 0);
if (!region->surf)
dief("SDL map surface init: %s", SDL_GetError());
SDL_LockSurface(region->surf);
SDL_Rect r = { .x = 0, .y = 0, .w = REGION_XSIZE, .h = REGION_ZSIZE };
SDL_FillRect(region->surf, &r, pack_rgb(ignore_alpha(special_colors[COLOR_UNLOADED])));
SDL_UnlockSurface(region->surf);
}
/* paint all dirty chunks */
region->dirty_flag = 0;
for (jint cz = 0; cz < REGION_SIZE; cz++)
{
for (jint cx = 0; cx < REGION_SIZE; cx++)
{
if (BITSET_TEST(region->dirty_chunk, cidx))
{
coord_t cc;
cc.x = region->key.x + (cx * CHUNK_XSIZE);
cc.z = region->key.z + (cz * CHUNK_ZSIZE);
paint_chunk(data, region->surf, cc);
BITSET_CLEAR(region->dirty_chunk, cidx);
}
cidx++;
}
}
}
static void draw_map(void *data, SDL_Surface *screen)
{
/* locate the screen corners in (sub-)block coordinates */
jint scr_x1, scr_z1;
jint scr_x1o, scr_z1o;
coord_t scr1 = s2w_offset(0, 0, &scr_x1o, &scr_z1o);
scr_x1 = scr1.x;
scr_z1 = scr1.z;
jint scr_x2, scr_z2;
jint scr_x2o, scr_z2o;
scr_x2 = scr_x1;
scr_z2 = scr_z1;
scr_x2o = scr_x1o + map_w;
scr_z2o = scr_z1o + map_h;
scr_x2 += scr_x2o / map_scale;
scr_z2 += scr_z2o / map_scale;
scr_x2o = scr_x2 % map_scale;
scr_z2o = scr_z2 % map_scale;
/* find the range of regions that intersect with the screen */
jint reg_x1, reg_x2, reg_z1, reg_z2;
reg_x1 = REGION_XIDX(scr_x1);
reg_z1 = REGION_ZIDX(scr_z1);
reg_x2 = REGION_XIDX(scr_x2 + REGION_XSIZE - 1);
reg_z2 = REGION_ZIDX(scr_z2 + REGION_ZSIZE - 1);
/* draw those regions */
SDL_LockSurface(screen);
for (jint reg_z = reg_z1; reg_z <= reg_z2; reg_z++)
{
for (jint reg_x = reg_x1; reg_x <= reg_x2; reg_x++)
{
/* get the region flat, paint it if dirty */
coord_t rc = COORD(reg_x * REGION_XSIZE, reg_z * REGION_ZSIZE);
struct map_region *region = map_get_region(rc, false);
if (!region)
continue; /* nothing to draw */
if (region->dirty_flag)
paint_region(data, region);
SDL_Surface *regs = region->surf;
if (!regs)
continue; /* hasn't been painted yet... */
SDL_LockSurface(regs);
/* try to find where to place the region */
int reg_sx, reg_sy;
reg_sx = (rc.x - scr_x1)*map_scale - scr_x1o;
reg_sy = (rc.z - scr_z1)*map_scale - scr_z1o;
map_blit_scaled(screen, regs, reg_sx, reg_sy, REGION_XSIZE, REGION_ZSIZE, map_scale);
SDL_UnlockSurface(regs);
}
}
SDL_UnlockSurface(screen);
}
struct map_mode *map_init_flat_mode(struct flat_mode flat_mode)
{
struct state *state = g_new(struct state, 1);
state->flat_mode = flat_mode;
state->track_pickups = false;
state->track_mobs = false;
struct map_mode *mode = g_new(struct map_mode, 1);
mode->data = state;
mode->describe = describe;
mode->s2w = s2w;
mode->w2s = w2s;
mode->handle_key = handle_key;
mode->update_player_pos = update_player_pos;
mode->update_time = update_time;
mode->draw_map = draw_map;
mode->draw_player = draw_player;
mode->draw_entity = draw_entity;
return mode;
}