--- deliantra/Deliantra-Client/Client.xs 2006/04/15 01:52:53 1.41 +++ deliantra/Deliantra-Client/Client.xs 2006/04/16 07:25:29 1.42 @@ -64,16 +64,18 @@ if (!*h) *h = 1; } +typedef uint16_t mapface; + typedef struct { GLint name; int w, h; float s, t; uint8_t r, g, b, a; -} mapface; +} maptex; typedef struct { int16_t darkness; - uint16_t face[3]; + mapface face[3]; } mapcell; typedef struct { @@ -83,10 +85,13 @@ typedef struct map { int x, y, w, h; - int vx, vy; + int ox, oy; /* offset to virtual global coordinate system */ int faces; mapface *face; + int texs; + maptex *tex; + uint32_t rows; maprow *row; } *CFClient__Map; @@ -116,6 +121,60 @@ #define Append(type,ptr,sze,inc) (ptr) = (type *)append ((char *)ptr, (sze) * sizeof (type), (inc) * sizeof (type)) #define Prepend(type,ptr,sze,inc) (ptr) = (type *)prepend ((char *)ptr, (sze) * sizeof (type), (inc) * sizeof (type)) +static maprow * +map_get_row (CFClient__Map self, int y) +{ + if (0 > y) + { + int extend = - y + MAP_EXTEND_Y; + Prepend (maprow, self->row, self->rows, extend); + + self->rows += extend; + self->y += extend; + y += extend; + } + else if (y >= self->rows) + { + int extend = y - self->rows + MAP_EXTEND_Y; + Append (maprow, self->row, self->rows, extend); + self->rows += extend; + } + + return self->row + y; +} + +static mapcell * +row_get_cell (maprow *row, int x) +{ + if (!row->col) + { + Newz (0, row->col, MAP_EXTEND_X, mapcell); + row->c0 = x - MAP_EXTEND_X / 4; + row->c1 = row->c0 + MAP_EXTEND_X; + } + + if (row->c0 > x) + { + int extend = row->c0 - x + MAP_EXTEND_X; + Prepend (mapcell, row->col, row->c1 - row->c0, extend); + row->c0 -= extend; + } + else if (x >= row->c1) + { + int extend = x - row->c1 + MAP_EXTEND_X; + Append (mapcell, row->col, row->c1 - row->c0, extend); + row->c1 += extend; + } + + return row->col + (x - row->c0); +} + +static mapcell * +map_get_cell (CFClient__Map self, int x, int y) +{ + return row_get_cell (map_get_row (self, y), x); +} + static void map_clear (CFClient__Map self) { @@ -128,8 +187,8 @@ self->x = 0; self->y = 0; - self->vx = 0; - self->vy = 0; + self->ox = 0; + self->oy = 0; self->row = 0; self->rows = 0; } @@ -456,14 +515,16 @@ new (SV *class, int map_width, int map_height) CODE: New (0, RETVAL, 1, struct map); - RETVAL->x = 0; - RETVAL->y = 0; - RETVAL->w = map_width; - RETVAL->h = map_height; - RETVAL->vx = 0; - RETVAL->vy = 0; + RETVAL->x = 0; + RETVAL->y = 0; + RETVAL->w = map_width; + RETVAL->h = map_height; + RETVAL->ox = 0; + RETVAL->oy = 0; RETVAL->faces = 8192; Newz (0, RETVAL->face, RETVAL->faces, mapface); + RETVAL->texs = 8192; + Newz (0, RETVAL->tex, RETVAL->texs, maptex); RETVAL->rows = 0; RETVAL->row = 0; OUTPUT: @@ -484,26 +545,39 @@ map_clear (self); void -set_texture (CFClient::Map self, int face, int name, int w, int h, float s, float t, int r, int g, int b, int a) +set_face (CFClient::Map self, int face, int texid) CODE: { - while (self->faces < face) + while (self->faces <= face) { Append (mapface, self->face, self->faces, self->faces); self->faces *= 2; } - mapface *f = self->face + face; + self->face [face] = texid; +} + +void +set_texture (CFClient::Map self, int texid, int name, int w, int h, float s, float t, int r, int g, int b, int a) + CODE: +{ + while (self->texs <= texid) + { + Append (maptex, self->tex, self->texs, self->texs); + self->texs *= 2; + } + + maptex *tex = self->tex + texid; - f->name = name; - f->w = w; - f->h = h; - f->s = s; - f->t = t; - f->r = r; - f->g = g; - f->b = b; - f->a = a; + tex->name = name; + tex->w = w; + tex->h = h; + tex->s = s; + tex->t = t; + tex->r = r; + tex->g = g; + tex->b = b; + tex->a = a; } void @@ -520,8 +594,8 @@ else if (dy < 0) map_blank (self, self->x, self->y + self->h + dy + 1, self->w, 1 - dy); - self->x += dx; - self->y += dy; + self->x += dx; self->ox += dx; + self->y += dy; self->oy += dy; while (self->y < 0) { @@ -532,6 +606,19 @@ } } +int +ox (CFClient::Map self) + ALIAS: + oy = 1 + CODE: + switch (ix) + { + case 0: RETVAL = self->ox; break; + case 1: RETVAL = self->oy; break; + } + OUTPUT: + RETVAL + void map1a_update (CFClient::Map self, SV *data_) CODE: @@ -546,44 +633,11 @@ int x = ((flags >> 10) & 63) + self->x; int y = ((flags >> 4) & 63) + self->y; - while (y >= self->rows) - { - Prepend (maprow, self->row, self->rows, MAP_EXTEND_Y); - - self->rows += MAP_EXTEND_Y; - } - - maprow *row = self->row + y; - - if (!row->col) - { - Newz (0, row->col, MAP_EXTEND_X, mapcell); - row->c0 = self->x - MAP_EXTEND_X / 4; - row->c1 = row->c0 + MAP_EXTEND_X; - } - - if (row->c0 > x) - { - int extend = row->c0 - x + MAP_EXTEND_X; - Prepend (mapcell, row->col, row->c1 - row->c0, extend); - row->c0 -= extend; - } - else if (x >= row->c1) - { - int extend = x - row->c1 + MAP_EXTEND_X; - Append (mapcell, row->col, row->c1 - row->c0, extend); - row->c1 += extend; - } - - assert (y >= 0); - assert (y < self->rows); - assert (x >= row->c0); - assert (x < row->c1); - mapcell *cell = row->col + (x - row->c0); + mapcell *cell = map_get_cell (self, x, y); if (flags & 15) { - if (cell->darkness < 0 && x < self->w && y < self->h) + if (cell->darkness < 0) // && x < self->w && y < self->h) { cell->darkness = 0; cell->face [0] = 0; @@ -593,19 +647,21 @@ cell->darkness = flags & 8 ? *data++ : 255; + //TODO: don't trust server data to be in-range(!) + if (flags & 4) { - cell->face [0] = (data [0] << 8) + data [1]; data += 2; + cell->face [0] = self->face [(data [0] << 8) + data [1]]; data += 2; } if (flags & 2) { - cell->face [1] = (data [0] << 8) + data [1]; data += 2; + cell->face [1] = self->face [(data [0] << 8) + data [1]]; data += 2; } if (flags & 1) { - cell->face [2] = (data [0] << 8) + data [1]; data += 2; + cell->face [2] = self->face [(data [0] << 8) + data [1]]; data += 2; } } else @@ -645,11 +701,11 @@ for (z = 0; z <= 0; z++) { - uint16_t face = cell->face [z]; + mapface face = cell->face [z]; if (face) { - mapface tex = self->face [face]; + maptex tex = self->tex [face]; int a0 = 255 - tex.a; int a1 = tex.a; @@ -680,23 +736,22 @@ int sw4 = (sw + 3) & ~3; SV *darkness_sv = sv_2mortal (newSV (sw4 * sh)); uint8_t *darkness = (uint8_t *)SvPVX (darkness_sv); - + memset (darkness, 255, sw4 * sh); SvPOK_only (darkness_sv); SvCUR_set (darkness_sv, sw4 * sh); int vx = self->x + (self->w - sw) / 2 - shift_x; int vy = self->y + (self->h - sh) / 2 - shift_y; - if (0) - { - int vx = self->vx = self->w >= sw - ? self->x + (self->w - sw) / 2 - : MIN (self->x, MAX (self->x + self->w - sw + 1, self->vx)); - - int vy = self->vy = self->h >= sh - ? self->y + (self->h - sh) / 2 - : MIN (self->y, MAX (self->y + self->h - sh + 1, self->vy)); - } + /* + int vx = self->vx = self->w >= sw + ? self->x + (self->w - sw) / 2 + : MIN (self->x, MAX (self->x + self->w - sw + 1, self->vx)); + + int vy = self->vy = self->h >= sh + ? self->y + (self->h - sh) / 2 + : MIN (self->y, MAX (self->y + self->h - sh + 1, self->vy)); + */ glColor4ub (255, 255, 255, 255); @@ -726,11 +781,11 @@ ? 255 - FOW_DARKNESS : 255 - cell->darkness; - uint16_t face = cell->face [z]; + mapface face = cell->face [z]; if (face) { - mapface tex = self->face [face]; + maptex tex = self->tex [face]; int px = (x + 1) * 32 - tex.w; int py = (y + 1) * 32 - tex.h; @@ -762,3 +817,135 @@ PUSHs (darkness_sv); } +SV * +get_rect (CFClient::Map self, int x0, int y0, int w, int h) + CODE: +{ + int x, y, x1, y1; + SV *data_sv = newSV (w * h * 7 + 5); + uint8_t *data = (uint8_t *)SvPVX (data_sv); + + *data++ = 0; /* version 0 format */ + *data++ = w >> 8; *data++ = w; + *data++ = h >> 8; *data++ = h; + + // we need to do this 'cause we don't keep an absolute coord system for rows + // TODO: treat rows as we treat + map_get_row (self, y0 + self->y - self->oy);//D + map_get_row (self, y0 + self->y - self->oy + h - 1);//D + + x0 += self->x - self->ox; + y0 += self->y - self->oy; + + x1 = x0 + w; + y1 = y0 + h; + + for (y = y0; y < y1; y++) + { + maprow *row = 0 <= y && y < self->rows + ? self->row + y + : 0; + + for (x = x0; x < x1; x++) + { + if (row && row->c0 <= x && x < row->c1) + { + mapcell *cell = row->col + (x - row->c0); + + uint8_t flags = 0; + + if (cell->face [0]) flags |= 1; + if (cell->face [1]) flags |= 2; + if (cell->face [2]) flags |= 4; + + *data++ = flags; + + if (flags & 1) + { + *data++ = cell->face [0] >> 8; + *data++ = cell->face [0]; + } + + if (flags & 2) + { + *data++ = cell->face [1] >> 8; + *data++ = cell->face [1]; + } + + if (flags & 4) + { + *data++ = cell->face [2] >> 8; + *data++ = cell->face [2]; + } + } + else + *data++ = 0; + } + } + + SvPOK_only (data_sv); + SvCUR_set (data_sv, data - (uint8_t *)SvPVX (data_sv)); + RETVAL = data_sv; +} + OUTPUT: + RETVAL + +void +set_rect (CFClient::Map self, int x0, int y0, uint8_t *data) + PPCODE: +{ + int x, y, z; + int x1, y1; + + if (*data++ != 0) + return; /* version mismatch */ + + int w = *data++ << 8; w |= *data++; + int h = *data++ << 8; h |= *data++; + + // we need to do this 'cause we don't keep an absolute coord system for rows + // TODO: treat rows as we treat + map_get_row (self, y0 + self->y - self->oy);//D + map_get_row (self, y0 + self->y - self->oy + h - 1);//D + + x0 += self->x - self->ox; + y0 += self->y - self->oy; + + x1 = x0 + w; + y1 = y0 + h; + + for (y = y0; y < y1; y++) + { + maprow *row = map_get_row (self, y); + + for (x = x0; x < x1; x++) + { + uint8_t flags = *data++; + + if (flags) + { + mapface face[3] = { 0, 0, 0 }; + + mapcell *cell = row_get_cell (row, x); + + if (flags & 1) { face[0] = *data++ << 8; face[0] |= *data++; } + if (flags & 2) { face[1] = *data++ << 8; face[1] |= *data++; } + if (flags & 4) { face[2] = *data++ << 8; face[2] |= *data++; } + + if (cell->darkness <= 0) + { + cell->darkness = -1; + + for (z = 0; z <= 2; z++) + { + cell->face[z] = face[z]; + + if (face[z] && (face[z] >= self->texs || !self->tex[face [z]].name)) + XPUSHs (sv_2mortal (newSViv (face[z]))); + } + } + } + } + } +} +