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.41 by root, Sat Apr 15 01:52:53 2006 UTC vs.
Revision 1.42 by root, Sun Apr 16 07:25:29 2006 UTC

62 *w = (*w + 3) & ~3; 62 *w = (*w + 3) & ~3;
63 if (!*w) *w = 1; 63 if (!*w) *w = 1;
64 if (!*h) *h = 1; 64 if (!*h) *h = 1;
65} 65}
66 66
67typedef uint16_t mapface;
68
67typedef struct { 69typedef struct {
68 GLint name; 70 GLint name;
69 int w, h; 71 int w, h;
70 float s, t; 72 float s, t;
71 uint8_t r, g, b, a; 73 uint8_t r, g, b, a;
72} mapface; 74} maptex;
73 75
74typedef struct { 76typedef struct {
75 int16_t darkness; 77 int16_t darkness;
76 uint16_t face[3]; 78 mapface face[3];
77} mapcell; 79} mapcell;
78 80
79typedef struct { 81typedef struct {
80 int32_t c0, c1; 82 int32_t c0, c1;
81 mapcell *col; 83 mapcell *col;
82} maprow; 84} maprow;
83 85
84typedef struct map { 86typedef struct map {
85 int x, y, w, h; 87 int x, y, w, h;
86 int vx, vy; 88 int ox, oy; /* offset to virtual global coordinate system */
87 int faces; 89 int faces;
88 mapface *face; 90 mapface *face;
91
92 int texs;
93 maptex *tex;
89 94
90 uint32_t rows; 95 uint32_t rows;
91 maprow *row; 96 maprow *row;
92} *CFClient__Map; 97} *CFClient__Map;
93 98
114} 119}
115 120
116#define Append(type,ptr,sze,inc) (ptr) = (type *)append ((char *)ptr, (sze) * sizeof (type), (inc) * sizeof (type)) 121#define Append(type,ptr,sze,inc) (ptr) = (type *)append ((char *)ptr, (sze) * sizeof (type), (inc) * sizeof (type))
117#define Prepend(type,ptr,sze,inc) (ptr) = (type *)prepend ((char *)ptr, (sze) * sizeof (type), (inc) * sizeof (type)) 122#define Prepend(type,ptr,sze,inc) (ptr) = (type *)prepend ((char *)ptr, (sze) * sizeof (type), (inc) * sizeof (type))
118 123
124static maprow *
125map_get_row (CFClient__Map self, int y)
126{
127 if (0 > y)
128 {
129 int extend = - y + MAP_EXTEND_Y;
130 Prepend (maprow, self->row, self->rows, extend);
131
132 self->rows += extend;
133 self->y += extend;
134 y += extend;
135 }
136 else if (y >= self->rows)
137 {
138 int extend = y - self->rows + MAP_EXTEND_Y;
139 Append (maprow, self->row, self->rows, extend);
140 self->rows += extend;
141 }
142
143 return self->row + y;
144}
145
146static mapcell *
147row_get_cell (maprow *row, int x)
148{
149 if (!row->col)
150 {
151 Newz (0, row->col, MAP_EXTEND_X, mapcell);
152 row->c0 = x - MAP_EXTEND_X / 4;
153 row->c1 = row->c0 + MAP_EXTEND_X;
154 }
155
156 if (row->c0 > x)
157 {
158 int extend = row->c0 - x + MAP_EXTEND_X;
159 Prepend (mapcell, row->col, row->c1 - row->c0, extend);
160 row->c0 -= extend;
161 }
162 else if (x >= row->c1)
163 {
164 int extend = x - row->c1 + MAP_EXTEND_X;
165 Append (mapcell, row->col, row->c1 - row->c0, extend);
166 row->c1 += extend;
167 }
168
169 return row->col + (x - row->c0);
170}
171
172static mapcell *
173map_get_cell (CFClient__Map self, int x, int y)
174{
175 return row_get_cell (map_get_row (self, y), x);
176}
177
119static void 178static void
120map_clear (CFClient__Map self) 179map_clear (CFClient__Map self)
121{ 180{
122 int r; 181 int r;
123 182
126 185
127 Safefree (self->row); 186 Safefree (self->row);
128 187
129 self->x = 0; 188 self->x = 0;
130 self->y = 0; 189 self->y = 0;
131 self->vx = 0; 190 self->ox = 0;
132 self->vy = 0; 191 self->oy = 0;
133 self->row = 0; 192 self->row = 0;
134 self->rows = 0; 193 self->rows = 0;
135} 194}
136 195
137static void 196static void
454 513
455CFClient::Map 514CFClient::Map
456new (SV *class, int map_width, int map_height) 515new (SV *class, int map_width, int map_height)
457 CODE: 516 CODE:
458 New (0, RETVAL, 1, struct map); 517 New (0, RETVAL, 1, struct map);
459 RETVAL->x = 0; 518 RETVAL->x = 0;
460 RETVAL->y = 0; 519 RETVAL->y = 0;
461 RETVAL->w = map_width; 520 RETVAL->w = map_width;
462 RETVAL->h = map_height; 521 RETVAL->h = map_height;
463 RETVAL->vx = 0; 522 RETVAL->ox = 0;
464 RETVAL->vy = 0; 523 RETVAL->oy = 0;
465 RETVAL->faces = 8192; 524 RETVAL->faces = 8192;
466 Newz (0, RETVAL->face, RETVAL->faces, mapface); 525 Newz (0, RETVAL->face, RETVAL->faces, mapface);
526 RETVAL->texs = 8192;
527 Newz (0, RETVAL->tex, RETVAL->texs, maptex);
467 RETVAL->rows = 0; 528 RETVAL->rows = 0;
468 RETVAL->row = 0; 529 RETVAL->row = 0;
469 OUTPUT: 530 OUTPUT:
470 RETVAL 531 RETVAL
471 532
482clear (CFClient::Map self) 543clear (CFClient::Map self)
483 CODE: 544 CODE:
484 map_clear (self); 545 map_clear (self);
485 546
486void 547void
487set_texture (CFClient::Map self, int face, int name, int w, int h, float s, float t, int r, int g, int b, int a) 548set_face (CFClient::Map self, int face, int texid)
488 CODE: 549 CODE:
489{ 550{
490 while (self->faces < face) 551 while (self->faces <= face)
491 { 552 {
492 Append (mapface, self->face, self->faces, self->faces); 553 Append (mapface, self->face, self->faces, self->faces);
493 self->faces *= 2; 554 self->faces *= 2;
494 } 555 }
495 556
496 mapface *f = self->face + face; 557 self->face [face] = texid;
558}
497 559
560void
561set_texture (CFClient::Map self, int texid, int name, int w, int h, float s, float t, int r, int g, int b, int a)
562 CODE:
563{
564 while (self->texs <= texid)
565 {
566 Append (maptex, self->tex, self->texs, self->texs);
567 self->texs *= 2;
568 }
569
570 maptex *tex = self->tex + texid;
571
498 f->name = name; 572 tex->name = name;
499 f->w = w; 573 tex->w = w;
500 f->h = h; 574 tex->h = h;
501 f->s = s; 575 tex->s = s;
502 f->t = t; 576 tex->t = t;
503 f->r = r; 577 tex->r = r;
504 f->g = g; 578 tex->g = g;
505 f->b = b; 579 tex->b = b;
506 f->a = a; 580 tex->a = a;
507} 581}
508 582
509void 583void
510scroll (CFClient::Map self, int dx, int dy) 584scroll (CFClient::Map self, int dx, int dy)
511 CODE: 585 CODE:
518 if (dy > 0) 592 if (dy > 0)
519 map_blank (self, self->x, self->y, self->w, dy - 1); 593 map_blank (self, self->x, self->y, self->w, dy - 1);
520 else if (dy < 0) 594 else if (dy < 0)
521 map_blank (self, self->x, self->y + self->h + dy + 1, self->w, 1 - dy); 595 map_blank (self, self->x, self->y + self->h + dy + 1, self->w, 1 - dy);
522 596
523 self->x += dx; 597 self->x += dx; self->ox += dx;
524 self->y += dy; 598 self->y += dy; self->oy += dy;
525 599
526 while (self->y < 0) 600 while (self->y < 0)
527 { 601 {
528 Prepend (maprow, self->row, self->rows, MAP_EXTEND_Y); 602 Prepend (maprow, self->row, self->rows, MAP_EXTEND_Y);
529 603
530 self->rows += MAP_EXTEND_Y; 604 self->rows += MAP_EXTEND_Y;
531 self->y += MAP_EXTEND_Y; 605 self->y += MAP_EXTEND_Y;
532 } 606 }
533} 607}
608
609int
610ox (CFClient::Map self)
611 ALIAS:
612 oy = 1
613 CODE:
614 switch (ix)
615 {
616 case 0: RETVAL = self->ox; break;
617 case 1: RETVAL = self->oy; break;
618 }
619 OUTPUT:
620 RETVAL
534 621
535void 622void
536map1a_update (CFClient::Map self, SV *data_) 623map1a_update (CFClient::Map self, SV *data_)
537 CODE: 624 CODE:
538{ 625{
544 int flags = (data [0] << 8) + data [1]; data += 2; 631 int flags = (data [0] << 8) + data [1]; data += 2;
545 632
546 int x = ((flags >> 10) & 63) + self->x; 633 int x = ((flags >> 10) & 63) + self->x;
547 int y = ((flags >> 4) & 63) + self->y; 634 int y = ((flags >> 4) & 63) + self->y;
548 635
549 while (y >= self->rows) 636 mapcell *cell = map_get_cell (self, x, y);
550 {
551 Prepend (maprow, self->row, self->rows, MAP_EXTEND_Y);
552
553 self->rows += MAP_EXTEND_Y;
554 }
555
556 maprow *row = self->row + y;
557
558 if (!row->col)
559 {
560 Newz (0, row->col, MAP_EXTEND_X, mapcell);
561 row->c0 = self->x - MAP_EXTEND_X / 4;
562 row->c1 = row->c0 + MAP_EXTEND_X;
563 }
564
565 if (row->c0 > x)
566 {
567 int extend = row->c0 - x + MAP_EXTEND_X;
568 Prepend (mapcell, row->col, row->c1 - row->c0, extend);
569 row->c0 -= extend;
570 }
571 else if (x >= row->c1)
572 {
573 int extend = x - row->c1 + MAP_EXTEND_X;
574 Append (mapcell, row->col, row->c1 - row->c0, extend);
575 row->c1 += extend;
576 }
577
578 assert (y >= 0);
579 assert (y < self->rows);
580 assert (x >= row->c0);
581 assert (x < row->c1);
582 mapcell *cell = row->col + (x - row->c0);
583 637
584 if (flags & 15) 638 if (flags & 15)
585 { 639 {
586 if (cell->darkness < 0 && x < self->w && y < self->h) 640 if (cell->darkness < 0) // && x < self->w && y < self->h)
587 { 641 {
588 cell->darkness = 0; 642 cell->darkness = 0;
589 cell->face [0] = 0; 643 cell->face [0] = 0;
590 cell->face [1] = 0; 644 cell->face [1] = 0;
591 cell->face [2] = 0; 645 cell->face [2] = 0;
592 } 646 }
593 647
594 cell->darkness = flags & 8 ? *data++ : 255; 648 cell->darkness = flags & 8 ? *data++ : 255;
595 649
650 //TODO: don't trust server data to be in-range(!)
651
596 if (flags & 4) 652 if (flags & 4)
597 { 653 {
598 cell->face [0] = (data [0] << 8) + data [1]; data += 2; 654 cell->face [0] = self->face [(data [0] << 8) + data [1]]; data += 2;
599 } 655 }
600 656
601 if (flags & 2) 657 if (flags & 2)
602 { 658 {
603 cell->face [1] = (data [0] << 8) + data [1]; data += 2; 659 cell->face [1] = self->face [(data [0] << 8) + data [1]]; data += 2;
604 } 660 }
605 661
606 if (flags & 1) 662 if (flags & 1)
607 { 663 {
608 cell->face [2] = (data [0] << 8) + data [1]; data += 2; 664 cell->face [2] = self->face [(data [0] << 8) + data [1]]; data += 2;
609 } 665 }
610 } 666 }
611 else 667 else
612 cell->darkness = -1; 668 cell->darkness = -1;
613 } 669 }
643 { 699 {
644 mapcell *cell = row->col + (x - row->c0); 700 mapcell *cell = row->col + (x - row->c0);
645 701
646 for (z = 0; z <= 0; z++) 702 for (z = 0; z <= 0; z++)
647 { 703 {
648 uint16_t face = cell->face [z]; 704 mapface face = cell->face [z];
649 705
650 if (face) 706 if (face)
651 { 707 {
652 mapface tex = self->face [face]; 708 maptex tex = self->tex [face];
653 int a0 = 255 - tex.a; 709 int a0 = 255 - tex.a;
654 int a1 = tex.a; 710 int a1 = tex.a;
655 711
656 r = (r * a0 + tex.r * a1) / 255; 712 r = (r * a0 + tex.r * a1) / 255;
657 g = (g * a0 + tex.g * a1) / 255; 713 g = (g * a0 + tex.g * a1) / 255;
678 PPCODE: 734 PPCODE:
679{ 735{
680 int sw4 = (sw + 3) & ~3; 736 int sw4 = (sw + 3) & ~3;
681 SV *darkness_sv = sv_2mortal (newSV (sw4 * sh)); 737 SV *darkness_sv = sv_2mortal (newSV (sw4 * sh));
682 uint8_t *darkness = (uint8_t *)SvPVX (darkness_sv); 738 uint8_t *darkness = (uint8_t *)SvPVX (darkness_sv);
683 739 memset (darkness, 255, sw4 * sh);
684 SvPOK_only (darkness_sv); 740 SvPOK_only (darkness_sv);
685 SvCUR_set (darkness_sv, sw4 * sh); 741 SvCUR_set (darkness_sv, sw4 * sh);
686 742
687 int vx = self->x + (self->w - sw) / 2 - shift_x; 743 int vx = self->x + (self->w - sw) / 2 - shift_x;
688 int vy = self->y + (self->h - sh) / 2 - shift_y; 744 int vy = self->y + (self->h - sh) / 2 - shift_y;
689 745
690 if (0)
691 { 746 /*
692 int vx = self->vx = self->w >= sw 747 int vx = self->vx = self->w >= sw
693 ? self->x + (self->w - sw) / 2 748 ? self->x + (self->w - sw) / 2
694 : MIN (self->x, MAX (self->x + self->w - sw + 1, self->vx)); 749 : MIN (self->x, MAX (self->x + self->w - sw + 1, self->vx));
695 750
696 int vy = self->vy = self->h >= sh 751 int vy = self->vy = self->h >= sh
697 ? self->y + (self->h - sh) / 2 752 ? self->y + (self->h - sh) / 2
698 : MIN (self->y, MAX (self->y + self->h - sh + 1, self->vy)); 753 : MIN (self->y, MAX (self->y + self->h - sh + 1, self->vy));
699 } 754 */
700 755
701 glColor4ub (255, 255, 255, 255); 756 glColor4ub (255, 255, 255, 255);
702 757
703 glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); 758 glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
704 glEnable (GL_BLEND); 759 glEnable (GL_BLEND);
724 779
725 darkness[y * sw4 + x] = cell->darkness < 0 780 darkness[y * sw4 + x] = cell->darkness < 0
726 ? 255 - FOW_DARKNESS 781 ? 255 - FOW_DARKNESS
727 : 255 - cell->darkness; 782 : 255 - cell->darkness;
728 783
729 uint16_t face = cell->face [z]; 784 mapface face = cell->face [z];
730 785
731 if (face) 786 if (face)
732 { 787 {
733 mapface tex = self->face [face]; 788 maptex tex = self->tex [face];
734 789
735 int px = (x + 1) * 32 - tex.w; 790 int px = (x + 1) * 32 - tex.w;
736 int py = (y + 1) * 32 - tex.h; 791 int py = (y + 1) * 32 - tex.h;
737 792
738 if (last_name != tex.name) 793 if (last_name != tex.name)
760 PUSHs (sv_2mortal (newSViv (sw4))); 815 PUSHs (sv_2mortal (newSViv (sw4)));
761 PUSHs (sv_2mortal (newSViv (sh))); 816 PUSHs (sv_2mortal (newSViv (sh)));
762 PUSHs (darkness_sv); 817 PUSHs (darkness_sv);
763} 818}
764 819
820SV *
821get_rect (CFClient::Map self, int x0, int y0, int w, int h)
822 CODE:
823{
824 int x, y, x1, y1;
825 SV *data_sv = newSV (w * h * 7 + 5);
826 uint8_t *data = (uint8_t *)SvPVX (data_sv);
827
828 *data++ = 0; /* version 0 format */
829 *data++ = w >> 8; *data++ = w;
830 *data++ = h >> 8; *data++ = h;
831
832 // we need to do this 'cause we don't keep an absolute coord system for rows
833 // TODO: treat rows as we treat
834 map_get_row (self, y0 + self->y - self->oy);//D
835 map_get_row (self, y0 + self->y - self->oy + h - 1);//D
836
837 x0 += self->x - self->ox;
838 y0 += self->y - self->oy;
839
840 x1 = x0 + w;
841 y1 = y0 + h;
842
843 for (y = y0; y < y1; y++)
844 {
845 maprow *row = 0 <= y && y < self->rows
846 ? self->row + y
847 : 0;
848
849 for (x = x0; x < x1; x++)
850 {
851 if (row && row->c0 <= x && x < row->c1)
852 {
853 mapcell *cell = row->col + (x - row->c0);
854
855 uint8_t flags = 0;
856
857 if (cell->face [0]) flags |= 1;
858 if (cell->face [1]) flags |= 2;
859 if (cell->face [2]) flags |= 4;
860
861 *data++ = flags;
862
863 if (flags & 1)
864 {
865 *data++ = cell->face [0] >> 8;
866 *data++ = cell->face [0];
867 }
868
869 if (flags & 2)
870 {
871 *data++ = cell->face [1] >> 8;
872 *data++ = cell->face [1];
873 }
874
875 if (flags & 4)
876 {
877 *data++ = cell->face [2] >> 8;
878 *data++ = cell->face [2];
879 }
880 }
881 else
882 *data++ = 0;
883 }
884 }
885
886 SvPOK_only (data_sv);
887 SvCUR_set (data_sv, data - (uint8_t *)SvPVX (data_sv));
888 RETVAL = data_sv;
889}
890 OUTPUT:
891 RETVAL
892
893void
894set_rect (CFClient::Map self, int x0, int y0, uint8_t *data)
895 PPCODE:
896{
897 int x, y, z;
898 int x1, y1;
899
900 if (*data++ != 0)
901 return; /* version mismatch */
902
903 int w = *data++ << 8; w |= *data++;
904 int h = *data++ << 8; h |= *data++;
905
906 // we need to do this 'cause we don't keep an absolute coord system for rows
907 // TODO: treat rows as we treat
908 map_get_row (self, y0 + self->y - self->oy);//D
909 map_get_row (self, y0 + self->y - self->oy + h - 1);//D
910
911 x0 += self->x - self->ox;
912 y0 += self->y - self->oy;
913
914 x1 = x0 + w;
915 y1 = y0 + h;
916
917 for (y = y0; y < y1; y++)
918 {
919 maprow *row = map_get_row (self, y);
920
921 for (x = x0; x < x1; x++)
922 {
923 uint8_t flags = *data++;
924
925 if (flags)
926 {
927 mapface face[3] = { 0, 0, 0 };
928
929 mapcell *cell = row_get_cell (row, x);
930
931 if (flags & 1) { face[0] = *data++ << 8; face[0] |= *data++; }
932 if (flags & 2) { face[1] = *data++ << 8; face[1] |= *data++; }
933 if (flags & 4) { face[2] = *data++ << 8; face[2] |= *data++; }
934
935 if (cell->darkness <= 0)
936 {
937 cell->darkness = -1;
938
939 for (z = 0; z <= 2; z++)
940 {
941 cell->face[z] = face[z];
942
943 if (face[z] && (face[z] >= self->texs || !self->tex[face [z]].name))
944 XPUSHs (sv_2mortal (newSViv (face[z])));
945 }
946 }
947 }
948 }
949 }
950}
951

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines