… | |
… | |
76 | # include <netinet/in.h> |
76 | # include <netinet/in.h> |
77 | # include <netinet/tcp.h> |
77 | # include <netinet/tcp.h> |
78 | # include <inttypes.h> |
78 | # include <inttypes.h> |
79 | #endif |
79 | #endif |
80 | |
80 | |
81 | #if __GNUC__ >= 4 |
81 | #include "ecb.h" |
82 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
82 | #include "salloc.h" |
83 | #else |
|
|
84 | # define expect(expr,value) (expr) |
|
|
85 | #endif |
|
|
86 | |
|
|
87 | #define expect_false(expr) expect ((expr) != 0, 0) |
|
|
88 | #define expect_true(expr) expect ((expr) != 0, 1) |
|
|
89 | |
83 | |
90 | #define OBJ_STR "\xef\xbf\xbc" /* U+FFFC, object replacement character */ |
84 | #define OBJ_STR "\xef\xbf\xbc" /* U+FFFC, object replacement character */ |
91 | |
85 | |
92 | /* this is used as fow flag as well, so has to have a different value */ |
86 | /* this is used as fow flag as well, so has to have a different value */ |
93 | /* then anything that is computed by incoming darkness */ |
87 | /* then anything that is computed by incoming darkness */ |
… | |
… | |
105 | #define KMOD_LRAM 0x10000 // our extension |
99 | #define KMOD_LRAM 0x10000 // our extension |
106 | |
100 | |
107 | #define TEXID_SPEECH 1 |
101 | #define TEXID_SPEECH 1 |
108 | #define TEXID_NOFACE 2 |
102 | #define TEXID_NOFACE 2 |
109 | |
103 | |
|
|
104 | // approximately divide by 255 |
|
|
105 | static unsigned int |
|
|
106 | div255 (unsigned int n) |
|
|
107 | { |
|
|
108 | return (n + (n >> 8)) >> 8; |
|
|
109 | } |
|
|
110 | |
|
|
111 | static unsigned int |
|
|
112 | minpot (unsigned int n) |
|
|
113 | { |
|
|
114 | if (!n) |
|
|
115 | return 0; |
|
|
116 | |
|
|
117 | --n; |
|
|
118 | |
|
|
119 | n |= n >> 1; |
|
|
120 | n |= n >> 2; |
|
|
121 | n |= n >> 4; |
|
|
122 | n |= n >> 8; |
|
|
123 | n |= n >> 16; |
|
|
124 | |
|
|
125 | return n + 1; |
|
|
126 | } |
|
|
127 | |
110 | static char * |
128 | static char * |
111 | fast_sv_grow (SV *sv, STRLEN need) |
129 | fast_sv_grow (SV *sv, STRLEN need) |
112 | { |
130 | { |
113 | STRLEN len = SvLEN (sv); |
131 | STRLEN len = SvLEN (sv); |
114 | STRLEN want = SvCUR (sv) + need; |
132 | STRLEN want = SvCUR (sv) + need; |
115 | |
133 | |
116 | if (expect_false (len < want)) |
134 | if (ecb_expect_false (len < want)) |
117 | { |
135 | { |
118 | do |
136 | do |
119 | len *= 2; |
137 | len *= 2; |
120 | while (len < want); |
138 | while (len < want); |
121 | |
139 | |
… | |
… | |
255 | if (!rect.height) rect.height = 1; |
273 | if (!rect.height) rect.height = 1; |
256 | |
274 | |
257 | *w = rect.width; |
275 | *w = rect.width; |
258 | *h = rect.height; |
276 | *h = rect.height; |
259 | } |
277 | } |
|
|
278 | |
|
|
279 | ///////////////////////////////////////////////////////////////////////////// |
260 | |
280 | |
261 | typedef uint16_t tileid; |
281 | typedef uint16_t tileid; |
262 | typedef uint16_t faceid; |
282 | typedef uint16_t faceid; |
263 | |
283 | |
264 | struct maptex |
284 | struct maptex |
… | |
… | |
297 | }; |
317 | }; |
298 | |
318 | |
299 | typedef mapgrid *DC__Map; |
319 | typedef mapgrid *DC__Map; |
300 | |
320 | |
301 | template<typename T> |
321 | template<typename T> |
302 | static void |
322 | ecb_cold static void |
303 | prepend (T *&ptr, int sze, int inc) |
323 | prepend (T *&ptr, int sze, int inc) |
304 | { |
324 | { |
305 | T *p; |
325 | T *p; |
306 | |
326 | |
307 | New (0, p, sze + inc, T); |
327 | Newx (p, inc + sze, T); |
308 | Zero (p, inc, T); |
328 | Zero (p, inc, T); |
309 | Move (ptr, p + inc, sze, T); |
329 | Move (ptr, p + inc, sze, T); |
310 | Safefree (ptr); |
330 | Safefree (ptr); |
311 | |
331 | |
312 | ptr = p; |
332 | ptr = p; |
313 | } |
333 | } |
314 | |
334 | |
315 | template<typename T> |
335 | template<typename T> |
316 | static void |
336 | ecb_cold static void |
317 | append (T *&ptr, int sze, int inc) |
337 | append (T *&ptr, int sze, int inc) |
318 | { |
338 | { |
319 | Renew (ptr, sze + inc, T); |
339 | Renew (ptr, sze + inc, T); |
320 | Zero (ptr + sze, inc, T); |
340 | Zero (ptr + sze, inc, T); |
321 | } |
341 | } |
322 | |
342 | |
323 | static void |
343 | static void |
324 | need_facenum (struct mapgrid *self, faceid face) |
344 | need_facenum (struct mapgrid *self, faceid face) |
325 | { |
345 | { |
326 | while (self->faces <= face) |
346 | if (ecb_expect_true (self->faces > face)) |
327 | { |
347 | return; |
|
|
348 | |
|
|
349 | size_t newsize = minpot (face + 1); |
328 | append (self->face2tile, self->faces, self->faces); |
350 | append (self->face2tile, self->faces, newsize - self->faces); |
329 | self->faces *= 2; |
351 | self->faces = newsize; |
330 | } |
|
|
331 | } |
352 | } |
332 | |
353 | |
333 | static void |
354 | static void |
334 | need_texid (struct mapgrid *self, int texid) |
355 | need_texid (struct mapgrid *self, int texid) |
335 | { |
356 | { |
336 | while (self->texs <= texid) |
357 | if (ecb_expect_true (self->texs > texid)) |
337 | { |
358 | return; |
|
|
359 | |
|
|
360 | size_t newsize = minpot (texid + 1); |
338 | append (self->tex, self->texs, self->texs); |
361 | append (self->tex, self->texs, newsize - self->texs); |
339 | self->texs *= 2; |
362 | self->texs = newsize; |
340 | } |
|
|
341 | } |
363 | } |
342 | |
364 | |
343 | static maprow * |
365 | static maprow * |
344 | map_get_row (mapgrid *self, int y) |
366 | map_get_row (mapgrid *self, int y) |
345 | { |
367 | { |
… | |
… | |
459 | { |
481 | { |
460 | return tile == o.tile && x == o.x && y == o.y && level == o.level; |
482 | return tile == o.tile && x == o.x && y == o.y && level == o.level; |
461 | } |
483 | } |
462 | }; |
484 | }; |
463 | |
485 | |
464 | typedef ska::flat_hash_map<smooth_key, IV> smooth_hash; |
486 | typedef ska::flat_hash_map<smooth_key, IV, std::hash<smooth_key>, std::equal_to<smooth_key>, slice_allocator<smooth_key>> smooth_hash; |
465 | |
487 | |
466 | namespace std { |
488 | namespace std { |
467 | template <> |
489 | template <> |
468 | struct hash<smooth_key> |
490 | struct hash<smooth_key> |
469 | { |
491 | { |
… | |
… | |
507 | ev.code = 1; |
529 | ev.code = 1; |
508 | ev.data1 = (void *)(long)channel; |
530 | ev.data1 = (void *)(long)channel; |
509 | ev.data2 = 0; |
531 | ev.data2 = 0; |
510 | |
532 | |
511 | SDL_PushEvent ((SDL_Event *)&ev); |
533 | SDL_PushEvent ((SDL_Event *)&ev); |
512 | } |
|
|
513 | |
|
|
514 | // approximately divide by 255 |
|
|
515 | static unsigned int |
|
|
516 | div255 (unsigned int n) |
|
|
517 | { |
|
|
518 | return (n + (n >> 8)) >> 8; |
|
|
519 | } |
|
|
520 | |
|
|
521 | static unsigned int |
|
|
522 | minpot (unsigned int n) |
|
|
523 | { |
|
|
524 | if (!n) |
|
|
525 | return 0; |
|
|
526 | |
|
|
527 | --n; |
|
|
528 | |
|
|
529 | n |= n >> 1; |
|
|
530 | n |= n >> 2; |
|
|
531 | n |= n >> 4; |
|
|
532 | n |= n >> 8; |
|
|
533 | n |= n >> 16; |
|
|
534 | |
|
|
535 | return n + 1; |
|
|
536 | } |
|
|
537 | |
|
|
538 | static unsigned int |
|
|
539 | popcount (unsigned int n) |
|
|
540 | { |
|
|
541 | n -= (n >> 1) & 0x55555555U; |
|
|
542 | n = ((n >> 2) & 0x33333333U) + (n & 0x33333333U); |
|
|
543 | n = ((n >> 4) + n) & 0x0f0f0f0fU; |
|
|
544 | n *= 0x01010101U; |
|
|
545 | |
|
|
546 | return n >> 24; |
|
|
547 | } |
534 | } |
548 | |
535 | |
549 | /* SDL should provide this, really. */ |
536 | /* SDL should provide this, really. */ |
550 | #define SDLK_MODIFIER_MIN 300 |
537 | #define SDLK_MODIFIER_MIN 300 |
551 | #define SDLK_MODIFIER_MAX 314 |
538 | #define SDLK_MODIFIER_MAX 314 |
… | |
… | |
882 | |
869 | |
883 | NV ceil (NV x) |
870 | NV ceil (NV x) |
884 | |
871 | |
885 | IV minpot (UV n) |
872 | IV minpot (UV n) |
886 | |
873 | |
|
|
874 | UV ld32 (UV n) |
|
|
875 | CODE: |
|
|
876 | RETVAL = ecb_ld32 (n); |
|
|
877 | OUTPUT: |
|
|
878 | RETVAL |
|
|
879 | |
887 | IV popcount (UV n) |
880 | IV popcount (UV n) |
|
|
881 | CODE: |
|
|
882 | RETVAL = ecb_popcount32 (n); |
|
|
883 | OUTPUT: |
|
|
884 | RETVAL |
888 | |
885 | |
889 | NV distance (NV dx, NV dy) |
886 | NV distance (NV dx, NV dy) |
890 | CODE: |
887 | CODE: |
891 | RETVAL = pow (dx * dx + dy * dy, 0.5); |
888 | RETVAL = pow (dx * dx + dy * dy, 0.5); |
892 | OUTPUT: |
889 | OUTPUT: |
… | |
… | |
2307 | } |
2304 | } |
2308 | |
2305 | |
2309 | px = (x + 1) * Th - tex.w; |
2306 | px = (x + 1) * Th - tex.w; |
2310 | py = (y + 1) * Tw - tex.h; |
2307 | py = (y + 1) * Tw - tex.h; |
2311 | |
2308 | |
2312 | if (expect_false (cell->player == player) && expect_false (z == 2)) |
2309 | if (ecb_expect_false (cell->player == player) && ecb_expect_false (z == 2)) |
2313 | { |
2310 | { |
2314 | pl_x = px; |
2311 | pl_x = px; |
2315 | pl_y = py; |
2312 | pl_y = py; |
2316 | pl_tex = tex; |
2313 | pl_tex = tex; |
2317 | continue; |
2314 | continue; |
… | |
… | |
2360 | skey.x = x ; skey.y = y ; smooth_or_bits (smooth, skey, 0x0400); |
2357 | skey.x = x ; skey.y = y ; smooth_or_bits (smooth, skey, 0x0400); |
2361 | skey.x = x + 2; skey.y = y ; smooth_or_bits (smooth, skey, 0x0800); |
2358 | skey.x = x + 2; skey.y = y ; smooth_or_bits (smooth, skey, 0x0800); |
2362 | } |
2359 | } |
2363 | } |
2360 | } |
2364 | |
2361 | |
2365 | if (expect_false (z == 2) && expect_false (cell->flags)) |
2362 | if (ecb_expect_false (z == 2) && ecb_expect_false (cell->flags)) |
2366 | { |
2363 | { |
2367 | // overlays such as the speech bubble, probably more to come |
2364 | // overlays such as the speech bubble, probably more to come |
2368 | if (cell->flags & 1) |
2365 | if (cell->flags & 1) |
2369 | { |
2366 | { |
2370 | rc_key_t key_ov = key; |
2367 | rc_key_t key_ov = key; |
… | |
… | |
2489 | mapcell *cell = row->col + (x + mx - row->c0); |
2486 | mapcell *cell = row->col + (x + mx - row->c0); |
2490 | |
2487 | |
2491 | int px = x * Tw; |
2488 | int px = x * Tw; |
2492 | int py = y * Th; |
2489 | int py = y * Th; |
2493 | |
2490 | |
2494 | if (expect_false (cell->player == player)) |
2491 | if (ecb_expect_false (cell->player == player)) |
2495 | { |
2492 | { |
2496 | px += sdx; |
2493 | px += sdx; |
2497 | py += sdy; |
2494 | py += sdy; |
2498 | } |
2495 | } |
2499 | |
2496 | |