… | |
… | |
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 |
… | |
… | |
296 | maprow *row; |
316 | maprow *row; |
297 | }; |
317 | }; |
298 | |
318 | |
299 | typedef mapgrid *DC__Map; |
319 | typedef mapgrid *DC__Map; |
300 | |
320 | |
301 | static char * |
321 | template<typename T> |
|
|
322 | ecb_cold static void |
302 | prepend (char *ptr, int sze, int inc) |
323 | prepend (T *&ptr, int sze, int inc) |
303 | { |
324 | { |
304 | char *p; |
325 | T *p; |
305 | |
326 | |
306 | New (0, p, sze + inc, char); |
327 | Newx (p, inc + sze, T); |
307 | Zero (p, inc, char); |
328 | Zero (p, inc, T); |
308 | Move (ptr, p + inc, sze, char); |
329 | Move (ptr, p + inc, sze, T); |
309 | Safefree (ptr); |
330 | Safefree (ptr); |
310 | |
331 | |
311 | return p; |
332 | ptr = p; |
312 | } |
333 | } |
313 | |
334 | |
314 | static char * |
335 | template<typename T> |
|
|
336 | ecb_cold static void |
315 | append (char *ptr, int sze, int inc) |
337 | append (T *&ptr, int sze, int inc) |
316 | { |
338 | { |
317 | Renew (ptr, sze + inc, char); |
339 | Renew (ptr, sze + inc, T); |
318 | Zero (ptr + sze, inc, char); |
340 | Zero (ptr + sze, inc, T); |
319 | |
|
|
320 | return ptr; |
|
|
321 | } |
341 | } |
322 | |
|
|
323 | #define Append(type,ptr,sze,inc) (ptr) = (type *)append ((char *)ptr, (sze) * sizeof (type), (inc) * sizeof (type)) |
|
|
324 | #define Prepend(type,ptr,sze,inc) (ptr) = (type *)prepend ((char *)ptr, (sze) * sizeof (type), (inc) * sizeof (type)) |
|
|
325 | |
342 | |
326 | static void |
343 | static void |
327 | need_facenum (struct mapgrid *self, faceid face) |
344 | need_facenum (struct mapgrid *self, faceid face) |
328 | { |
345 | { |
329 | while (self->faces <= face) |
346 | if (ecb_expect_true (self->faces > face)) |
330 | { |
347 | return; |
|
|
348 | |
|
|
349 | size_t newsize = minpot (face + 1); |
331 | Append (tileid, self->face2tile, self->faces, self->faces); |
350 | append (self->face2tile, self->faces, newsize - self->faces); |
332 | self->faces *= 2; |
351 | self->faces = newsize; |
333 | } |
|
|
334 | } |
352 | } |
335 | |
353 | |
336 | static void |
354 | static void |
337 | need_texid (struct mapgrid *self, int texid) |
355 | need_texid (struct mapgrid *self, int texid) |
338 | { |
356 | { |
339 | while (self->texs <= texid) |
357 | if (ecb_expect_true (self->texs > texid)) |
340 | { |
358 | return; |
|
|
359 | |
|
|
360 | size_t newsize = minpot (texid + 1); |
341 | Append (maptex, self->tex, self->texs, self->texs); |
361 | append (self->tex, self->texs, newsize - self->texs); |
342 | self->texs *= 2; |
362 | self->texs = newsize; |
343 | } |
|
|
344 | } |
363 | } |
345 | |
364 | |
346 | static maprow * |
365 | static maprow * |
347 | map_get_row (mapgrid *self, int y) |
366 | map_get_row (mapgrid *self, int y) |
348 | { |
367 | { |
349 | if (0 > y) |
368 | if (0 > y) |
350 | { |
369 | { |
351 | int extend = - y + MAP_EXTEND_Y; |
370 | int extend = - y + MAP_EXTEND_Y; |
352 | Prepend (maprow, self->row, self->rows, extend); |
371 | prepend (self->row, self->rows, extend); |
353 | |
372 | |
354 | self->rows += extend; |
373 | self->rows += extend; |
355 | self->y += extend; |
374 | self->y += extend; |
356 | y += extend; |
375 | y += extend; |
357 | } |
376 | } |
358 | else if (y >= self->rows) |
377 | else if (y >= self->rows) |
359 | { |
378 | { |
360 | int extend = y - self->rows + MAP_EXTEND_Y; |
379 | int extend = y - self->rows + MAP_EXTEND_Y; |
361 | Append (maprow, self->row, self->rows, extend); |
380 | append (self->row, self->rows, extend); |
362 | self->rows += extend; |
381 | self->rows += extend; |
363 | } |
382 | } |
364 | |
383 | |
365 | return self->row + y; |
384 | return self->row + y; |
366 | } |
385 | } |
… | |
… | |
376 | } |
395 | } |
377 | |
396 | |
378 | if (row->c0 > x) |
397 | if (row->c0 > x) |
379 | { |
398 | { |
380 | int extend = row->c0 - x + MAP_EXTEND_X; |
399 | int extend = row->c0 - x + MAP_EXTEND_X; |
381 | Prepend (mapcell, row->col, row->c1 - row->c0, extend); |
400 | prepend (row->col, row->c1 - row->c0, extend); |
382 | row->c0 -= extend; |
401 | row->c0 -= extend; |
383 | } |
402 | } |
384 | else if (x >= row->c1) |
403 | else if (x >= row->c1) |
385 | { |
404 | { |
386 | int extend = x - row->c1 + MAP_EXTEND_X; |
405 | int extend = x - row->c1 + MAP_EXTEND_X; |
387 | Append (mapcell, row->col, row->c1 - row->c0, extend); |
406 | append (row->col, row->c1 - row->c0, extend); |
388 | row->c1 += extend; |
407 | row->c1 += extend; |
389 | } |
408 | } |
390 | |
409 | |
391 | return row->col + (x - row->c0); |
410 | return row->col + (x - row->c0); |
392 | } |
411 | } |
… | |
… | |
462 | { |
481 | { |
463 | 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; |
464 | } |
483 | } |
465 | }; |
484 | }; |
466 | |
485 | |
467 | 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; |
468 | |
487 | |
469 | namespace std { |
488 | namespace std { |
470 | template <> |
489 | template <> |
471 | struct hash<smooth_key> |
490 | struct hash<smooth_key> |
472 | { |
491 | { |
… | |
… | |
510 | ev.code = 1; |
529 | ev.code = 1; |
511 | ev.data1 = (void *)(long)channel; |
530 | ev.data1 = (void *)(long)channel; |
512 | ev.data2 = 0; |
531 | ev.data2 = 0; |
513 | |
532 | |
514 | SDL_PushEvent ((SDL_Event *)&ev); |
533 | SDL_PushEvent ((SDL_Event *)&ev); |
515 | } |
|
|
516 | |
|
|
517 | // approximately divide by 255 |
|
|
518 | static unsigned int |
|
|
519 | div255 (unsigned int n) |
|
|
520 | { |
|
|
521 | return (n + (n >> 8)) >> 8; |
|
|
522 | } |
|
|
523 | |
|
|
524 | static unsigned int |
|
|
525 | minpot (unsigned int n) |
|
|
526 | { |
|
|
527 | if (!n) |
|
|
528 | return 0; |
|
|
529 | |
|
|
530 | --n; |
|
|
531 | |
|
|
532 | n |= n >> 1; |
|
|
533 | n |= n >> 2; |
|
|
534 | n |= n >> 4; |
|
|
535 | n |= n >> 8; |
|
|
536 | n |= n >> 16; |
|
|
537 | |
|
|
538 | return n + 1; |
|
|
539 | } |
|
|
540 | |
|
|
541 | static unsigned int |
|
|
542 | popcount (unsigned int n) |
|
|
543 | { |
|
|
544 | n -= (n >> 1) & 0x55555555U; |
|
|
545 | n = ((n >> 2) & 0x33333333U) + (n & 0x33333333U); |
|
|
546 | n = ((n >> 4) + n) & 0x0f0f0f0fU; |
|
|
547 | n *= 0x01010101U; |
|
|
548 | |
|
|
549 | return n >> 24; |
|
|
550 | } |
534 | } |
551 | |
535 | |
552 | /* SDL should provide this, really. */ |
536 | /* SDL should provide this, really. */ |
553 | #define SDLK_MODIFIER_MIN 300 |
537 | #define SDLK_MODIFIER_MIN 300 |
554 | #define SDLK_MODIFIER_MAX 314 |
538 | #define SDLK_MODIFIER_MAX 314 |
… | |
… | |
885 | |
869 | |
886 | NV ceil (NV x) |
870 | NV ceil (NV x) |
887 | |
871 | |
888 | IV minpot (UV n) |
872 | IV minpot (UV n) |
889 | |
873 | |
|
|
874 | UV ld32 (UV n) |
|
|
875 | CODE: |
|
|
876 | RETVAL = ecb_ld32 (n); |
|
|
877 | OUTPUT: |
|
|
878 | RETVAL |
|
|
879 | |
890 | IV popcount (UV n) |
880 | IV popcount (UV n) |
|
|
881 | CODE: |
|
|
882 | RETVAL = ecb_popcount32 (n); |
|
|
883 | OUTPUT: |
|
|
884 | RETVAL |
891 | |
885 | |
892 | NV distance (NV dx, NV dy) |
886 | NV distance (NV dx, NV dy) |
893 | CODE: |
887 | CODE: |
894 | RETVAL = pow (dx * dx + dy * dy, 0.5); |
888 | RETVAL = pow (dx * dx + dy * dy, 0.5); |
895 | OUTPUT: |
889 | OUTPUT: |
… | |
… | |
2056 | self->ox += dx; self->x += dx; |
2050 | self->ox += dx; self->x += dx; |
2057 | self->oy += dy; self->y += dy; |
2051 | self->oy += dy; self->y += dy; |
2058 | |
2052 | |
2059 | while (self->y < 0) |
2053 | while (self->y < 0) |
2060 | { |
2054 | { |
2061 | Prepend (maprow, self->row, self->rows, MAP_EXTEND_Y); |
2055 | prepend (self->row, self->rows, MAP_EXTEND_Y); |
2062 | |
2056 | |
2063 | self->rows += MAP_EXTEND_Y; |
2057 | self->rows += MAP_EXTEND_Y; |
2064 | self->y += MAP_EXTEND_Y; |
2058 | self->y += MAP_EXTEND_Y; |
2065 | } |
2059 | } |
2066 | } |
2060 | } |
… | |
… | |
2310 | } |
2304 | } |
2311 | |
2305 | |
2312 | px = (x + 1) * Th - tex.w; |
2306 | px = (x + 1) * Th - tex.w; |
2313 | py = (y + 1) * Tw - tex.h; |
2307 | py = (y + 1) * Tw - tex.h; |
2314 | |
2308 | |
2315 | if (expect_false (cell->player == player) && expect_false (z == 2)) |
2309 | if (ecb_expect_false (cell->player == player) && ecb_expect_false (z == 2)) |
2316 | { |
2310 | { |
2317 | pl_x = px; |
2311 | pl_x = px; |
2318 | pl_y = py; |
2312 | pl_y = py; |
2319 | pl_tex = tex; |
2313 | pl_tex = tex; |
2320 | continue; |
2314 | continue; |
… | |
… | |
2363 | 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); |
2364 | 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); |
2365 | } |
2359 | } |
2366 | } |
2360 | } |
2367 | |
2361 | |
2368 | if (expect_false (z == 2) && expect_false (cell->flags)) |
2362 | if (ecb_expect_false (z == 2) && ecb_expect_false (cell->flags)) |
2369 | { |
2363 | { |
2370 | // overlays such as the speech bubble, probably more to come |
2364 | // overlays such as the speech bubble, probably more to come |
2371 | if (cell->flags & 1) |
2365 | if (cell->flags & 1) |
2372 | { |
2366 | { |
2373 | rc_key_t key_ov = key; |
2367 | rc_key_t key_ov = key; |
… | |
… | |
2492 | mapcell *cell = row->col + (x + mx - row->c0); |
2486 | mapcell *cell = row->col + (x + mx - row->c0); |
2493 | |
2487 | |
2494 | int px = x * Tw; |
2488 | int px = x * Tw; |
2495 | int py = y * Th; |
2489 | int py = y * Th; |
2496 | |
2490 | |
2497 | if (expect_false (cell->player == player)) |
2491 | if (ecb_expect_false (cell->player == player)) |
2498 | { |
2492 | { |
2499 | px += sdx; |
2493 | px += sdx; |
2500 | py += sdy; |
2494 | py += sdy; |
2501 | } |
2495 | } |
2502 | |
2496 | |