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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines