… | |
… | |
287 | }; |
287 | }; |
288 | |
288 | |
289 | struct mapgrid { |
289 | struct mapgrid { |
290 | int x, y, w, h; |
290 | int x, y, w, h; |
291 | int ox, oy; /* offset to virtual global coordinate system */ |
291 | int ox, oy; /* offset to virtual global coordinate system */ |
292 | std::vector<tileid> tile; |
292 | int faces; tileid *face2tile; // [faceid] |
293 | std::vector<maptex> tex; |
293 | int texs; maptex *tex; // [tileid] |
294 | |
294 | |
295 | int32_t rows; |
295 | int32_t rows; |
296 | maprow *row; |
296 | maprow *row; |
297 | |
|
|
298 | ~mapgrid () |
|
|
299 | { |
|
|
300 | clear_cells (); |
|
|
301 | } |
|
|
302 | |
|
|
303 | void clear_cells (); |
|
|
304 | }; |
297 | }; |
305 | |
298 | |
306 | typedef mapgrid *DC__Map; |
299 | typedef mapgrid *DC__Map; |
307 | |
300 | |
308 | static char * |
301 | template<typename T> |
|
|
302 | static void |
309 | prepend (char *ptr, int sze, int inc) |
303 | prepend (T *&ptr, int sze, int inc) |
310 | { |
304 | { |
311 | char *p; |
305 | T *p; |
312 | |
306 | |
313 | New (0, p, sze + inc, char); |
307 | New (0, p, sze + inc, T); |
314 | Zero (p, inc, char); |
308 | Zero (p, inc, T); |
315 | Move (ptr, p + inc, sze, char); |
309 | Move (ptr, p + inc, sze, T); |
316 | Safefree (ptr); |
310 | Safefree (ptr); |
317 | |
311 | |
318 | return p; |
312 | ptr = p; |
319 | } |
313 | } |
320 | |
314 | |
321 | static char * |
315 | template<typename T> |
|
|
316 | static void |
322 | append (char *ptr, int sze, int inc) |
317 | append (T *&ptr, int sze, int inc) |
323 | { |
318 | { |
324 | Renew (ptr, sze + inc, char); |
319 | Renew (ptr, sze + inc, T); |
325 | Zero (ptr + sze, inc, char); |
320 | Zero (ptr + sze, inc, T); |
326 | |
|
|
327 | return ptr; |
|
|
328 | } |
321 | } |
329 | |
322 | |
330 | #define Append(type,ptr,sze,inc) (ptr) = (type *)append ((char *)ptr, (sze) * sizeof (type), (inc) * sizeof (type)) |
323 | static void |
331 | #define Prepend(type,ptr,sze,inc) (ptr) = (type *)prepend ((char *)ptr, (sze) * sizeof (type), (inc) * sizeof (type)) |
324 | need_facenum (struct mapgrid *self, faceid face) |
|
|
325 | { |
|
|
326 | while (self->faces <= face) |
|
|
327 | { |
|
|
328 | append (self->face2tile, self->faces, self->faces); |
|
|
329 | self->faces *= 2; |
|
|
330 | } |
|
|
331 | } |
|
|
332 | |
|
|
333 | static void |
|
|
334 | need_texid (struct mapgrid *self, int texid) |
|
|
335 | { |
|
|
336 | while (self->texs <= texid) |
|
|
337 | { |
|
|
338 | append (self->tex, self->texs, self->texs); |
|
|
339 | self->texs *= 2; |
|
|
340 | } |
|
|
341 | } |
332 | |
342 | |
333 | static maprow * |
343 | static maprow * |
334 | map_get_row (mapgrid *self, int y) |
344 | map_get_row (mapgrid *self, int y) |
335 | { |
345 | { |
336 | if (0 > y) |
346 | if (0 > y) |
337 | { |
347 | { |
338 | int extend = - y + MAP_EXTEND_Y; |
348 | int extend = - y + MAP_EXTEND_Y; |
339 | Prepend (maprow, self->row, self->rows, extend); |
349 | prepend (self->row, self->rows, extend); |
340 | |
350 | |
341 | self->rows += extend; |
351 | self->rows += extend; |
342 | self->y += extend; |
352 | self->y += extend; |
343 | y += extend; |
353 | y += extend; |
344 | } |
354 | } |
345 | else if (y >= self->rows) |
355 | else if (y >= self->rows) |
346 | { |
356 | { |
347 | int extend = y - self->rows + MAP_EXTEND_Y; |
357 | int extend = y - self->rows + MAP_EXTEND_Y; |
348 | Append (maprow, self->row, self->rows, extend); |
358 | append (self->row, self->rows, extend); |
349 | self->rows += extend; |
359 | self->rows += extend; |
350 | } |
360 | } |
351 | |
361 | |
352 | return self->row + y; |
362 | return self->row + y; |
353 | } |
363 | } |
… | |
… | |
363 | } |
373 | } |
364 | |
374 | |
365 | if (row->c0 > x) |
375 | if (row->c0 > x) |
366 | { |
376 | { |
367 | int extend = row->c0 - x + MAP_EXTEND_X; |
377 | int extend = row->c0 - x + MAP_EXTEND_X; |
368 | Prepend (mapcell, row->col, row->c1 - row->c0, extend); |
378 | prepend (row->col, row->c1 - row->c0, extend); |
369 | row->c0 -= extend; |
379 | row->c0 -= extend; |
370 | } |
380 | } |
371 | else if (x >= row->c1) |
381 | else if (x >= row->c1) |
372 | { |
382 | { |
373 | int extend = x - row->c1 + MAP_EXTEND_X; |
383 | int extend = x - row->c1 + MAP_EXTEND_X; |
374 | Append (mapcell, row->col, row->c1 - row->c0, extend); |
384 | append (row->col, row->c1 - row->c0, extend); |
375 | row->c1 += extend; |
385 | row->c1 += extend; |
376 | } |
386 | } |
377 | |
387 | |
378 | return row->col + (x - row->c0); |
388 | return row->col + (x - row->c0); |
379 | } |
389 | } |
… | |
… | |
382 | map_get_cell (mapgrid *self, int x, int y) |
392 | map_get_cell (mapgrid *self, int x, int y) |
383 | { |
393 | { |
384 | return row_get_cell (map_get_row (self, y), x); |
394 | return row_get_cell (map_get_row (self, y), x); |
385 | } |
395 | } |
386 | |
396 | |
387 | void mapgrid::clear_cells () |
397 | static void |
|
|
398 | map_clear (mapgrid *self) |
388 | { |
399 | { |
389 | int r; |
400 | int r; |
390 | |
401 | |
391 | for (r = 0; r < rows; r++) |
402 | for (r = 0; r < self->rows; r++) |
392 | Safefree (row[r].col); |
403 | Safefree (self->row[r].col); |
393 | |
404 | |
394 | Safefree (row); |
405 | Safefree (self->row); |
395 | |
406 | |
396 | x = 0; |
407 | self->x = 0; |
397 | y = 0; |
408 | self->y = 0; |
398 | ox = 0; |
409 | self->ox = 0; |
399 | oy = 0; |
410 | self->oy = 0; |
400 | row = 0; |
411 | self->row = 0; |
401 | rows = 0; |
412 | self->rows = 0; |
402 | } |
413 | } |
403 | |
414 | |
404 | #define CELL_CLEAR(cell) \ |
415 | #define CELL_CLEAR(cell) \ |
405 | do { \ |
416 | do { \ |
406 | if ((cell)->player) \ |
417 | if ((cell)->player) \ |
… | |
… | |
1886 | PROTOTYPES: DISABLE |
1897 | PROTOTYPES: DISABLE |
1887 | |
1898 | |
1888 | DC::Map |
1899 | DC::Map |
1889 | new (SV *klass) |
1900 | new (SV *klass) |
1890 | CODE: |
1901 | CODE: |
1891 | RETVAL = new mapgrid; |
1902 | New (0, RETVAL, 1, mapgrid); |
1892 | RETVAL->x = 0; |
1903 | RETVAL->x = 0; |
1893 | RETVAL->y = 0; |
1904 | RETVAL->y = 0; |
1894 | RETVAL->w = 0; |
1905 | RETVAL->w = 0; |
1895 | RETVAL->h = 0; |
1906 | RETVAL->h = 0; |
1896 | RETVAL->ox = 0; |
1907 | RETVAL->ox = 0; |
1897 | RETVAL->oy = 0; |
1908 | RETVAL->oy = 0; |
|
|
1909 | RETVAL->faces = 8192; Newz (0, RETVAL->face2tile, RETVAL->faces, tileid); |
|
|
1910 | RETVAL->texs = 8192; Newz (0, RETVAL->tex , RETVAL->texs , maptex); |
1898 | RETVAL->rows = 0; |
1911 | RETVAL->rows = 0; |
1899 | RETVAL->row = 0; |
1912 | RETVAL->row = 0; |
1900 | OUTPUT: |
1913 | OUTPUT: |
1901 | RETVAL |
1914 | RETVAL |
1902 | |
1915 | |
1903 | void |
1916 | void |
1904 | DESTROY (DC::Map self) |
1917 | DESTROY (DC::Map self) |
1905 | CODE: |
1918 | CODE: |
1906 | { |
1919 | { |
|
|
1920 | map_clear (self); |
|
|
1921 | Safefree (self->face2tile); |
|
|
1922 | Safefree (self->tex); |
1907 | delete self; |
1923 | Safefree (self); |
1908 | } |
1924 | } |
1909 | |
1925 | |
1910 | void |
1926 | void |
1911 | resize (DC::Map self, int map_width, int map_height) |
1927 | resize (DC::Map self, int map_width, int map_height) |
1912 | CODE: |
1928 | CODE: |
… | |
… | |
1914 | self->h = map_height; |
1930 | self->h = map_height; |
1915 | |
1931 | |
1916 | void |
1932 | void |
1917 | clear (DC::Map self) |
1933 | clear (DC::Map self) |
1918 | CODE: |
1934 | CODE: |
1919 | self->clear_cells (); |
1935 | map_clear (self); |
1920 | |
1936 | |
1921 | void |
1937 | void |
1922 | set_tileid (DC::Map self, int face, int tile) |
1938 | set_tileid (DC::Map self, int face, int tile) |
1923 | CODE: |
1939 | CODE: |
1924 | { |
1940 | { |
1925 | if (self->tile.size () <= face) self->tile.resize (face + 1); |
1941 | need_facenum (self, face); self->face2tile [face] = tile; |
1926 | self->tile[face] = tile; |
1942 | need_texid (self, tile); |
1927 | if (self->tex.size () <= tile) self->tex .resize (tile + 1); |
|
|
1928 | } |
1943 | } |
1929 | |
1944 | |
1930 | void |
1945 | void |
1931 | set_smooth (DC::Map self, int face, int smooth, int level) |
1946 | set_smooth (DC::Map self, int face, int smooth, int level) |
1932 | CODE: |
1947 | CODE: |
1933 | { |
1948 | { |
|
|
1949 | tileid texid; |
|
|
1950 | maptex *tex; |
|
|
1951 | |
1934 | if (face < 0 || face >= self->tile.size ()) |
1952 | if (face < 0 || face >= self->faces) |
1935 | return; |
1953 | return; |
1936 | |
1954 | |
1937 | if (smooth < 0 || smooth >= self->tile.size ()) |
1955 | if (smooth < 0 || smooth >= self->faces) |
1938 | return; |
1956 | return; |
1939 | |
1957 | |
1940 | tileid texid = self->tile[face]; |
1958 | texid = self->face2tile [face]; |
1941 | |
1959 | |
1942 | if (!texid) |
1960 | if (!texid) |
1943 | return; |
1961 | return; |
1944 | |
1962 | |
1945 | maptex &tex = self->tex[texid]; |
1963 | tex = self->tex + texid; |
1946 | tex.smoothtile = self->tile[smooth]; |
1964 | tex->smoothtile = self->face2tile [smooth]; |
1947 | tex.smoothlevel = level; |
1965 | tex->smoothlevel = level; |
1948 | } |
1966 | } |
1949 | |
1967 | |
1950 | void |
1968 | void |
1951 | set_texture (DC::Map self, int texid, int name, int w, int h, float s, float t, int r, int g, int b, int a) |
1969 | set_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: |
1970 | CODE: |
1953 | { |
1971 | { |
1954 | if (self->tex.size () < texid) self->tex.resize (texid + 1); |
1972 | need_texid (self, texid); |
1955 | |
1973 | |
1956 | { |
1974 | { |
1957 | maptex &tex = self->tex[texid]; |
1975 | maptex *tex = self->tex + texid; |
1958 | |
1976 | |
1959 | tex.name = name; |
1977 | tex->name = name; |
1960 | tex.w = w; |
1978 | tex->w = w; |
1961 | tex.h = h; |
1979 | tex->h = h; |
1962 | tex.s = s; |
1980 | tex->s = s; |
1963 | tex.t = t; |
1981 | tex->t = t; |
1964 | tex.r = r; |
1982 | tex->r = r; |
1965 | tex.g = g; |
1983 | tex->g = g; |
1966 | tex.b = b; |
1984 | tex->b = b; |
1967 | tex.a = a; |
1985 | tex->a = a; |
1968 | } |
1986 | } |
1969 | |
1987 | |
1970 | // somewhat hackish, but for textures that require it, it really |
1988 | // somewhat hackish, but for textures that require it, it really |
1971 | // improves the look, and most others don't suffer. |
1989 | // improves the look, and most others don't suffer. |
1972 | glBindTexture (GL_TEXTURE_2D, name); |
1990 | glBindTexture (GL_TEXTURE_2D, name); |
… | |
… | |
1978 | } |
1996 | } |
1979 | |
1997 | |
1980 | void |
1998 | void |
1981 | expire_textures (DC::Map self, int texid, int count) |
1999 | expire_textures (DC::Map self, int texid, int count) |
1982 | PPCODE: |
2000 | PPCODE: |
1983 | for (; texid < self->tex.size () && count; ++texid, --count) |
2001 | for (; texid < self->texs && count; ++texid, --count) |
1984 | { |
2002 | { |
1985 | maptex &tex = self->tex[texid]; |
2003 | maptex *tex = self->tex + texid; |
1986 | |
2004 | |
1987 | if (tex.name) |
2005 | if (tex->name) |
1988 | { |
2006 | { |
1989 | if (tex.unused) |
2007 | if (tex->unused) |
1990 | { |
2008 | { |
1991 | tex.name = 0; |
2009 | tex->name = 0; |
1992 | tex.unused = 0; |
2010 | tex->unused = 0; |
1993 | XPUSHs (sv_2mortal (newSViv (texid))); |
2011 | XPUSHs (sv_2mortal (newSViv (texid))); |
1994 | } |
2012 | } |
1995 | else |
2013 | else |
1996 | tex.unused = 1; |
2014 | tex->unused = 1; |
1997 | } |
2015 | } |
1998 | } |
2016 | } |
1999 | |
2017 | |
2000 | int |
2018 | int |
2001 | ox (DC::Map self) |
2019 | ox (DC::Map self) |
… | |
… | |
2035 | self->ox += dx; self->x += dx; |
2053 | self->ox += dx; self->x += dx; |
2036 | self->oy += dy; self->y += dy; |
2054 | self->oy += dy; self->y += dy; |
2037 | |
2055 | |
2038 | while (self->y < 0) |
2056 | while (self->y < 0) |
2039 | { |
2057 | { |
2040 | Prepend (maprow, self->row, self->rows, MAP_EXTEND_Y); |
2058 | prepend (self->row, self->rows, MAP_EXTEND_Y); |
2041 | |
2059 | |
2042 | self->rows += MAP_EXTEND_Y; |
2060 | self->rows += MAP_EXTEND_Y; |
2043 | self->y += MAP_EXTEND_Y; |
2061 | self->y += MAP_EXTEND_Y; |
2044 | } |
2062 | } |
2045 | } |
2063 | } |
… | |
… | |
2112 | } |
2130 | } |
2113 | |
2131 | |
2114 | for (z = 0; z <= 2; ++z) |
2132 | for (z = 0; z <= 2; ++z) |
2115 | if (flags & (4 >> z)) |
2133 | if (flags & (4 >> z)) |
2116 | { |
2134 | { |
2117 | faceid face = (data[0] << 8) + data[1]; data += 2; |
2135 | faceid face = (data [0] << 8) + data [1]; data += 2; |
2118 | if (self->tile.size () <= face) self->tile.resize (face + 1); |
2136 | need_facenum (self, face); |
2119 | cell->tile[z] = self->tile[face]; |
2137 | cell->tile [z] = self->face2tile [face]; |
2120 | |
2138 | |
2121 | if (cell->tile[z]) |
2139 | if (cell->tile [z]) |
2122 | { |
2140 | { |
2123 | maptex &tex = self->tex[cell->tile[z]]; |
2141 | maptex *tex = self->tex + cell->tile [z]; |
2124 | tex.unused = 0; |
2142 | tex->unused = 0; |
2125 | if (!tex.name) |
2143 | if (!tex->name) |
2126 | av_push (missing, newSViv (cell->tile [z])); |
2144 | av_push (missing, newSViv (cell->tile [z])); |
2127 | |
2145 | |
2128 | if (tex.smoothtile) |
2146 | if (tex->smoothtile) |
2129 | { |
2147 | { |
2130 | maptex &smooth = self->tex[tex.smoothtile]; |
2148 | maptex *smooth = self->tex + tex->smoothtile; |
2131 | smooth.unused = 0; |
2149 | smooth->unused = 0; |
2132 | if (!smooth.name) |
2150 | if (!smooth->name) |
2133 | av_push (missing, newSViv (tex.smoothtile)); |
2151 | av_push (missing, newSViv (tex->smoothtile)); |
2134 | } |
2152 | } |
2135 | } |
2153 | } |
2136 | } |
2154 | } |
2137 | } |
2155 | } |
2138 | else |
2156 | else |
… | |
… | |
2787 | |
2805 | |
2788 | for (z = 0; z <= 2; z++) |
2806 | for (z = 0; z <= 2; z++) |
2789 | { |
2807 | { |
2790 | tileid t = tile [z]; |
2808 | tileid t = tile [z]; |
2791 | |
2809 | |
2792 | if (t >= self->tex.size () || (t && !self->tex[t].name)) |
2810 | if (t >= self->texs || (t && !self->tex [t].name)) |
2793 | { |
2811 | { |
2794 | PUSHs (sv_2mortal (newSViv (t))); |
2812 | PUSHs (sv_2mortal (newSViv (t))); |
2795 | if (self->tex.size () <= t) self->tex.resize (t + 1); |
2813 | need_texid (self, t); |
2796 | } |
2814 | } |
2797 | |
2815 | |
2798 | cell->tile[z] = t; |
2816 | cell->tile [z] = t; |
2799 | } |
2817 | } |
2800 | } |
2818 | } |
2801 | } |
2819 | } |
2802 | } |
2820 | } |
2803 | } |
2821 | } |