ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/Client.xs
(Generate patch)

Comparing deliantra/Deliantra-Client/Client.xs (file contents):
Revision 1.327 by root, Sun Nov 18 12:01:50 2018 UTC vs.
Revision 1.333 by root, Mon Nov 19 01:23:01 2018 UTC

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 */
111fast_sv_grow (SV *sv, STRLEN need) 105fast_sv_grow (SV *sv, STRLEN need)
112{ 106{
113 STRLEN len = SvLEN (sv); 107 STRLEN len = SvLEN (sv);
114 STRLEN want = SvCUR (sv) + need; 108 STRLEN want = SvCUR (sv) + need;
115 109
116 if (expect_false (len < want)) 110 if (ecb_expect_false (len < want))
117 { 111 {
118 do 112 do
119 len *= 2; 113 len *= 2;
120 while (len < want); 114 while (len < want);
121 115
255 if (!rect.height) rect.height = 1; 249 if (!rect.height) rect.height = 1;
256 250
257 *w = rect.width; 251 *w = rect.width;
258 *h = rect.height; 252 *h = rect.height;
259} 253}
254
255/////////////////////////////////////////////////////////////////////////////
260 256
261typedef uint16_t tileid; 257typedef uint16_t tileid;
262typedef uint16_t faceid; 258typedef uint16_t faceid;
263 259
264struct maptex 260struct maptex
287}; 283};
288 284
289struct mapgrid { 285struct mapgrid {
290 int x, y, w, h; 286 int x, y, w, h;
291 int ox, oy; /* offset to virtual global coordinate system */ 287 int ox, oy; /* offset to virtual global coordinate system */
292 std::vector<tileid> tile; 288 int faces; tileid *face2tile; // [faceid]
293 std::vector<maptex> tex; 289 int texs; maptex *tex; // [tileid]
294 290
295 int32_t rows; 291 int32_t rows;
296 maprow *row; 292 maprow *row;
297
298 ~mapgrid ()
299 {
300 clear_cells ();
301 }
302
303 void clear_cells ();
304}; 293};
305 294
306typedef mapgrid *DC__Map; 295typedef mapgrid *DC__Map;
307 296
308static char * 297template<typename T>
298static void
309prepend (char *ptr, int sze, int inc) 299prepend (T *&ptr, int sze, int inc)
310{ 300{
311 char *p; 301 T *p;
312 302
313 New (0, p, sze + inc, char); 303 Newx (p, inc + sze, T);
314 Zero (p, inc, char); 304 Zero (p, inc, T);
315 Move (ptr, p + inc, sze, char); 305 Move (ptr, p + inc, sze, T);
316 Safefree (ptr); 306 Safefree (ptr);
317 307
318 return p; 308 ptr = p;
319} 309}
320 310
321static char * 311template<typename T>
312static void
322append (char *ptr, int sze, int inc) 313append (T *&ptr, int sze, int inc)
323{ 314{
324 Renew (ptr, sze + inc, char); 315 Renew (ptr, sze + inc, T);
325 Zero (ptr + sze, inc, char); 316 Zero (ptr + sze, inc, T);
326
327 return ptr;
328} 317}
329 318
330#define Append(type,ptr,sze,inc) (ptr) = (type *)append ((char *)ptr, (sze) * sizeof (type), (inc) * sizeof (type)) 319static void
331#define Prepend(type,ptr,sze,inc) (ptr) = (type *)prepend ((char *)ptr, (sze) * sizeof (type), (inc) * sizeof (type)) 320need_facenum (struct mapgrid *self, faceid face)
321{
322 while (self->faces <= face)
323 {
324 append (self->face2tile, self->faces, self->faces);
325 self->faces *= 2;
326 }
327}
328
329static void
330need_texid (struct mapgrid *self, int texid)
331{
332 while (self->texs <= texid)
333 {
334 append (self->tex, self->texs, self->texs);
335 self->texs *= 2;
336 }
337}
332 338
333static maprow * 339static maprow *
334map_get_row (mapgrid *self, int y) 340map_get_row (mapgrid *self, int y)
335{ 341{
336 if (0 > y) 342 if (0 > y)
337 { 343 {
338 int extend = - y + MAP_EXTEND_Y; 344 int extend = - y + MAP_EXTEND_Y;
339 Prepend (maprow, self->row, self->rows, extend); 345 prepend (self->row, self->rows, extend);
340 346
341 self->rows += extend; 347 self->rows += extend;
342 self->y += extend; 348 self->y += extend;
343 y += extend; 349 y += extend;
344 } 350 }
345 else if (y >= self->rows) 351 else if (y >= self->rows)
346 { 352 {
347 int extend = y - self->rows + MAP_EXTEND_Y; 353 int extend = y - self->rows + MAP_EXTEND_Y;
348 Append (maprow, self->row, self->rows, extend); 354 append (self->row, self->rows, extend);
349 self->rows += extend; 355 self->rows += extend;
350 } 356 }
351 357
352 return self->row + y; 358 return self->row + y;
353} 359}
363 } 369 }
364 370
365 if (row->c0 > x) 371 if (row->c0 > x)
366 { 372 {
367 int extend = row->c0 - x + MAP_EXTEND_X; 373 int extend = row->c0 - x + MAP_EXTEND_X;
368 Prepend (mapcell, row->col, row->c1 - row->c0, extend); 374 prepend (row->col, row->c1 - row->c0, extend);
369 row->c0 -= extend; 375 row->c0 -= extend;
370 } 376 }
371 else if (x >= row->c1) 377 else if (x >= row->c1)
372 { 378 {
373 int extend = x - row->c1 + MAP_EXTEND_X; 379 int extend = x - row->c1 + MAP_EXTEND_X;
374 Append (mapcell, row->col, row->c1 - row->c0, extend); 380 append (row->col, row->c1 - row->c0, extend);
375 row->c1 += extend; 381 row->c1 += extend;
376 } 382 }
377 383
378 return row->col + (x - row->c0); 384 return row->col + (x - row->c0);
379} 385}
382map_get_cell (mapgrid *self, int x, int y) 388map_get_cell (mapgrid *self, int x, int y)
383{ 389{
384 return row_get_cell (map_get_row (self, y), x); 390 return row_get_cell (map_get_row (self, y), x);
385} 391}
386 392
387void mapgrid::clear_cells () 393static void
394map_clear (mapgrid *self)
388{ 395{
389 int r; 396 int r;
390 397
391 for (r = 0; r < rows; r++) 398 for (r = 0; r < self->rows; r++)
392 Safefree (row[r].col); 399 Safefree (self->row[r].col);
393 400
394 Safefree (row); 401 Safefree (self->row);
395 402
396 x = 0; 403 self->x = 0;
397 y = 0; 404 self->y = 0;
398 ox = 0; 405 self->ox = 0;
399 oy = 0; 406 self->oy = 0;
400 row = 0; 407 self->row = 0;
401 rows = 0; 408 self->rows = 0;
402} 409}
403 410
404#define CELL_CLEAR(cell) \ 411#define CELL_CLEAR(cell) \
405 do { \ 412 do { \
406 if ((cell)->player) \ 413 if ((cell)->player) \
520 n |= n >> 4; 527 n |= n >> 4;
521 n |= n >> 8; 528 n |= n >> 8;
522 n |= n >> 16; 529 n |= n >> 16;
523 530
524 return n + 1; 531 return n + 1;
525}
526
527static unsigned int
528popcount (unsigned int n)
529{
530 n -= (n >> 1) & 0x55555555U;
531 n = ((n >> 2) & 0x33333333U) + (n & 0x33333333U);
532 n = ((n >> 4) + n) & 0x0f0f0f0fU;
533 n *= 0x01010101U;
534
535 return n >> 24;
536} 532}
537 533
538/* SDL should provide this, really. */ 534/* SDL should provide this, really. */
539#define SDLK_MODIFIER_MIN 300 535#define SDLK_MODIFIER_MIN 300
540#define SDLK_MODIFIER_MAX 314 536#define SDLK_MODIFIER_MAX 314
871 867
872NV ceil (NV x) 868NV ceil (NV x)
873 869
874IV minpot (UV n) 870IV minpot (UV n)
875 871
872UV ld32 (UV n)
873 CODE:
874 RETVAL = ecb_ld32 (n);
875 OUTPUT:
876 RETVAL
877
876IV popcount (UV n) 878IV popcount (UV n)
879 CODE:
880 RETVAL = ecb_popcount32 (n);
881 OUTPUT:
882 RETVAL
877 883
878NV distance (NV dx, NV dy) 884NV distance (NV dx, NV dy)
879 CODE: 885 CODE:
880 RETVAL = pow (dx * dx + dy * dy, 0.5); 886 RETVAL = pow (dx * dx + dy * dy, 0.5);
881 OUTPUT: 887 OUTPUT:
1886PROTOTYPES: DISABLE 1892PROTOTYPES: DISABLE
1887 1893
1888DC::Map 1894DC::Map
1889new (SV *klass) 1895new (SV *klass)
1890 CODE: 1896 CODE:
1891 RETVAL = new mapgrid; 1897 New (0, RETVAL, 1, mapgrid);
1892 RETVAL->x = 0; 1898 RETVAL->x = 0;
1893 RETVAL->y = 0; 1899 RETVAL->y = 0;
1894 RETVAL->w = 0; 1900 RETVAL->w = 0;
1895 RETVAL->h = 0; 1901 RETVAL->h = 0;
1896 RETVAL->ox = 0; 1902 RETVAL->ox = 0;
1897 RETVAL->oy = 0; 1903 RETVAL->oy = 0;
1904 RETVAL->faces = 8192; Newz (0, RETVAL->face2tile, RETVAL->faces, tileid);
1905 RETVAL->texs = 8192; Newz (0, RETVAL->tex , RETVAL->texs , maptex);
1898 RETVAL->rows = 0; 1906 RETVAL->rows = 0;
1899 RETVAL->row = 0; 1907 RETVAL->row = 0;
1900 OUTPUT: 1908 OUTPUT:
1901 RETVAL 1909 RETVAL
1902 1910
1903void 1911void
1904DESTROY (DC::Map self) 1912DESTROY (DC::Map self)
1905 CODE: 1913 CODE:
1906{ 1914{
1915 map_clear (self);
1916 Safefree (self->face2tile);
1917 Safefree (self->tex);
1907 delete self; 1918 Safefree (self);
1908} 1919}
1909 1920
1910void 1921void
1911resize (DC::Map self, int map_width, int map_height) 1922resize (DC::Map self, int map_width, int map_height)
1912 CODE: 1923 CODE:
1914 self->h = map_height; 1925 self->h = map_height;
1915 1926
1916void 1927void
1917clear (DC::Map self) 1928clear (DC::Map self)
1918 CODE: 1929 CODE:
1919 self->clear_cells (); 1930 map_clear (self);
1920 1931
1921void 1932void
1922set_tileid (DC::Map self, int face, int tile) 1933set_tileid (DC::Map self, int face, int tile)
1923 CODE: 1934 CODE:
1924{ 1935{
1925 if (self->tile.size () <= face) self->tile.resize (face + 1); 1936 need_facenum (self, face); self->face2tile [face] = tile;
1926 self->tile[face] = tile; 1937 need_texid (self, tile);
1927 if (self->tex.size () <= tile) self->tex .resize (tile + 1);
1928} 1938}
1929 1939
1930void 1940void
1931set_smooth (DC::Map self, int face, int smooth, int level) 1941set_smooth (DC::Map self, int face, int smooth, int level)
1932 CODE: 1942 CODE:
1933{ 1943{
1944 tileid texid;
1945 maptex *tex;
1946
1934 if (face < 0 || face >= self->tile.size ()) 1947 if (face < 0 || face >= self->faces)
1935 return; 1948 return;
1936 1949
1937 if (smooth < 0 || smooth >= self->tile.size ()) 1950 if (smooth < 0 || smooth >= self->faces)
1938 return; 1951 return;
1939 1952
1940 tileid texid = self->tile[face]; 1953 texid = self->face2tile [face];
1941 1954
1942 if (!texid) 1955 if (!texid)
1943 return; 1956 return;
1944 1957
1945 maptex &tex = self->tex[texid]; 1958 tex = self->tex + texid;
1946 tex.smoothtile = self->tile[smooth]; 1959 tex->smoothtile = self->face2tile [smooth];
1947 tex.smoothlevel = level; 1960 tex->smoothlevel = level;
1948} 1961}
1949 1962
1950void 1963void
1951set_texture (DC::Map self, int texid, int name, int w, int h, float s, float t, int r, int g, int b, int a) 1964set_texture (DC::Map self, int texid, int name, int w, int h, float s, float t, int r, int g, int b, int a)
1952 CODE: 1965 CODE:
1953{ 1966{
1954 if (self->tex.size () < texid) self->tex.resize (texid + 1); 1967 need_texid (self, texid);
1955 1968
1956 { 1969 {
1957 maptex &tex = self->tex[texid]; 1970 maptex *tex = self->tex + texid;
1958 1971
1959 tex.name = name; 1972 tex->name = name;
1960 tex.w = w; 1973 tex->w = w;
1961 tex.h = h; 1974 tex->h = h;
1962 tex.s = s; 1975 tex->s = s;
1963 tex.t = t; 1976 tex->t = t;
1964 tex.r = r; 1977 tex->r = r;
1965 tex.g = g; 1978 tex->g = g;
1966 tex.b = b; 1979 tex->b = b;
1967 tex.a = a; 1980 tex->a = a;
1968 } 1981 }
1969 1982
1970 // somewhat hackish, but for textures that require it, it really 1983 // somewhat hackish, but for textures that require it, it really
1971 // improves the look, and most others don't suffer. 1984 // improves the look, and most others don't suffer.
1972 glBindTexture (GL_TEXTURE_2D, name); 1985 glBindTexture (GL_TEXTURE_2D, name);
1978} 1991}
1979 1992
1980void 1993void
1981expire_textures (DC::Map self, int texid, int count) 1994expire_textures (DC::Map self, int texid, int count)
1982 PPCODE: 1995 PPCODE:
1983 for (; texid < self->tex.size () && count; ++texid, --count) 1996 for (; texid < self->texs && count; ++texid, --count)
1984 { 1997 {
1985 maptex &tex = self->tex[texid]; 1998 maptex *tex = self->tex + texid;
1986 1999
1987 if (tex.name) 2000 if (tex->name)
1988 { 2001 {
1989 if (tex.unused) 2002 if (tex->unused)
1990 { 2003 {
1991 tex.name = 0; 2004 tex->name = 0;
1992 tex.unused = 0; 2005 tex->unused = 0;
1993 XPUSHs (sv_2mortal (newSViv (texid))); 2006 XPUSHs (sv_2mortal (newSViv (texid)));
1994 } 2007 }
1995 else 2008 else
1996 tex.unused = 1; 2009 tex->unused = 1;
1997 } 2010 }
1998 } 2011 }
1999 2012
2000int 2013int
2001ox (DC::Map self) 2014ox (DC::Map self)
2035 self->ox += dx; self->x += dx; 2048 self->ox += dx; self->x += dx;
2036 self->oy += dy; self->y += dy; 2049 self->oy += dy; self->y += dy;
2037 2050
2038 while (self->y < 0) 2051 while (self->y < 0)
2039 { 2052 {
2040 Prepend (maprow, self->row, self->rows, MAP_EXTEND_Y); 2053 prepend (self->row, self->rows, MAP_EXTEND_Y);
2041 2054
2042 self->rows += MAP_EXTEND_Y; 2055 self->rows += MAP_EXTEND_Y;
2043 self->y += MAP_EXTEND_Y; 2056 self->y += MAP_EXTEND_Y;
2044 } 2057 }
2045} 2058}
2112 } 2125 }
2113 2126
2114 for (z = 0; z <= 2; ++z) 2127 for (z = 0; z <= 2; ++z)
2115 if (flags & (4 >> z)) 2128 if (flags & (4 >> z))
2116 { 2129 {
2117 faceid face = (data[0] << 8) + data[1]; data += 2; 2130 faceid face = (data [0] << 8) + data [1]; data += 2;
2118 if (self->tile.size () <= face) self->tile.resize (face + 1); 2131 need_facenum (self, face);
2119 cell->tile[z] = self->tile[face]; 2132 cell->tile [z] = self->face2tile [face];
2120 2133
2121 if (cell->tile[z]) 2134 if (cell->tile [z])
2122 { 2135 {
2123 maptex &tex = self->tex[cell->tile[z]]; 2136 maptex *tex = self->tex + cell->tile [z];
2124 tex.unused = 0; 2137 tex->unused = 0;
2125 if (!tex.name) 2138 if (!tex->name)
2126 av_push (missing, newSViv (cell->tile [z])); 2139 av_push (missing, newSViv (cell->tile [z]));
2127 2140
2128 if (tex.smoothtile) 2141 if (tex->smoothtile)
2129 { 2142 {
2130 maptex &smooth = self->tex[tex.smoothtile]; 2143 maptex *smooth = self->tex + tex->smoothtile;
2131 smooth.unused = 0; 2144 smooth->unused = 0;
2132 if (!smooth.name) 2145 if (!smooth->name)
2133 av_push (missing, newSViv (tex.smoothtile)); 2146 av_push (missing, newSViv (tex->smoothtile));
2134 } 2147 }
2135 } 2148 }
2136 } 2149 }
2137 } 2150 }
2138 else 2151 else
2289 } 2302 }
2290 2303
2291 px = (x + 1) * Th - tex.w; 2304 px = (x + 1) * Th - tex.w;
2292 py = (y + 1) * Tw - tex.h; 2305 py = (y + 1) * Tw - tex.h;
2293 2306
2294 if (expect_false (cell->player == player) && expect_false (z == 2)) 2307 if (ecb_expect_false (cell->player == player) && ecb_expect_false (z == 2))
2295 { 2308 {
2296 pl_x = px; 2309 pl_x = px;
2297 pl_y = py; 2310 pl_y = py;
2298 pl_tex = tex; 2311 pl_tex = tex;
2299 continue; 2312 continue;
2342 skey.x = x ; skey.y = y ; smooth_or_bits (smooth, skey, 0x0400); 2355 skey.x = x ; skey.y = y ; smooth_or_bits (smooth, skey, 0x0400);
2343 skey.x = x + 2; skey.y = y ; smooth_or_bits (smooth, skey, 0x0800); 2356 skey.x = x + 2; skey.y = y ; smooth_or_bits (smooth, skey, 0x0800);
2344 } 2357 }
2345 } 2358 }
2346 2359
2347 if (expect_false (z == 2) && expect_false (cell->flags)) 2360 if (ecb_expect_false (z == 2) && ecb_expect_false (cell->flags))
2348 { 2361 {
2349 // overlays such as the speech bubble, probably more to come 2362 // overlays such as the speech bubble, probably more to come
2350 if (cell->flags & 1) 2363 if (cell->flags & 1)
2351 { 2364 {
2352 rc_key_t key_ov = key; 2365 rc_key_t key_ov = key;
2471 mapcell *cell = row->col + (x + mx - row->c0); 2484 mapcell *cell = row->col + (x + mx - row->c0);
2472 2485
2473 int px = x * Tw; 2486 int px = x * Tw;
2474 int py = y * Th; 2487 int py = y * Th;
2475 2488
2476 if (expect_false (cell->player == player)) 2489 if (ecb_expect_false (cell->player == player))
2477 { 2490 {
2478 px += sdx; 2491 px += sdx;
2479 py += sdy; 2492 py += sdy;
2480 } 2493 }
2481 2494
2787 2800
2788 for (z = 0; z <= 2; z++) 2801 for (z = 0; z <= 2; z++)
2789 { 2802 {
2790 tileid t = tile [z]; 2803 tileid t = tile [z];
2791 2804
2792 if (t >= self->tex.size () || (t && !self->tex[t].name)) 2805 if (t >= self->texs || (t && !self->tex [t].name))
2793 { 2806 {
2794 PUSHs (sv_2mortal (newSViv (t))); 2807 PUSHs (sv_2mortal (newSViv (t)));
2795 if (self->tex.size () <= t) self->tex.resize (t + 1); 2808 need_texid (self, t);
2796 } 2809 }
2797 2810
2798 cell->tile[z] = t; 2811 cell->tile [z] = t;
2799 } 2812 }
2800 } 2813 }
2801 } 2814 }
2802 } 2815 }
2803 } 2816 }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines