ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra/Deliantra/Protocol.pm
Revision: 1.47
Committed: Tue Sep 1 21:37:25 2009 UTC (14 years, 8 months ago) by root
Branch: MAIN
CVS Tags: rel-2_01, rel-1_30, rel-1_29, rel-1_24, rel-1_25, HEAD
Changes since 1.46: +1 -1 lines
Log Message:
1.24

File Contents

# Content
1 =head1 NAME
2
3 Deliantra::Protocol - client protocol module
4
5 =head1 SYNOPSIS
6
7 use base Deliantra::Protocol; # you have to subclass
8
9 =head1 DESCRIPTION
10
11 Base class to implement a crossfire client.
12
13 =over 4
14
15 =cut
16
17 package Deliantra::Protocol;
18
19 BGIN { die "FATAL: Deliantra::Protocol needs to be rewritten to be properly subclassed form Deliantra::Protocol::Base" }
20
21 our $VERSION = '0.1';
22
23 use common::sense;
24
25 sub feed_map1a {
26 my ($self, $data) = @_;
27
28 my $map = $self->{map} ||= [];
29
30 my ($dx, $dy) = delete @$self{qw(delayed_scroll_x delayed_scroll_y)};
31
32 if ($dx || $dy) {
33 my ($mx, $my, $mw, $mh) = @$self{qw(mapx mapy mapw maph)};
34
35 {
36 my @darkness;
37
38 if ($dx > 0) {
39 push @darkness, [$mx, $my, $dx - 1, $mh];
40 } elsif ($dx < 0) {
41 push @darkness, [$mx + $mw + $dx + 1, $my, 1 - $dx, $mh];
42 }
43
44 if ($dy > 0) {
45 push @darkness, [$mx, $my, $mw, $dy - 1];
46 } elsif ($dy < 0) {
47 push @darkness, [$mx, $my + $mh + $dy + 1, $mw, 1 - $dy];
48 }
49
50 for (@darkness) {
51 my ($x0, $y0, $w, $h) = @$_;
52 for my $x ($x0 .. $x0 + $w) {
53 for my $y ($y0 .. $y0 + $h) {
54
55 my $cell = $map->[$x][$y]
56 or next;
57
58 $cell->[0] = -1;
59 }
60 }
61 }
62 }
63
64 # now scroll
65
66 $self->{mapx} += $dx;
67 $self->{mapy} += $dy;
68
69 # shift in new space if moving to "negative indices"
70 if ($self->{mapy} < 0) {
71 unshift @$_, (undef) x -$self->{mapy} for @$map;
72 $self->{mapy} = 0;
73 }
74
75 if ($self->{mapx} < 0) {
76 unshift @$map, (undef) x -$self->{mapx};
77 $self->{mapx} = 0;
78 }
79
80 $self->map_scroll ($dx, $dy);
81 }
82
83 my @dirty;
84 my ($coord, $x, $y, $darkness, $fa, $fb, $fc, $cell);
85
86 while (length $data) {
87 $coord = unpack "n", substr $data, 0, 2, "";
88
89 $x = (($coord >> 10) & 63) + $self->{mapx};
90 $y = (($coord >> 4) & 63) + $self->{mapy};
91
92 $cell = $map->[$x][$y] ||= [];
93
94 if ($coord & 15) {
95 @$cell = () if $cell->[0] < 0;
96
97 $cell->[0] = $coord & 8
98 ? unpack "C", substr $data, 0, 1, ""
99 : 255;
100
101 $cell->[1] = unpack "n", substr $data, 0, 2, ""
102 if $coord & 4;
103 $cell->[2] = unpack "n", substr $data, 0, 2, ""
104 if $coord & 2;
105 $cell->[3] = unpack "n", substr $data, 0, 2, ""
106 if $coord & 1;
107 } else {
108 $cell->[0] = -1;
109 }
110
111 push @dirty, [$x, $y];
112 }
113
114 $self->map_update (\@dirty);
115 }
116
117 sub feed_newmap {
118 my ($self) = @_;
119
120 $self->{map} = [];
121 $self->{mapx} = 0;
122 $self->{mapy} = 0;
123
124 delete $self->{delayed_scroll_x};
125 delete $self->{delayed_scroll_y};
126
127 $self->map_clear;
128 }
129
130 sub feed_image {
131 my ($self, $data) = @_;
132
133 $self->SUPER::feed_image ($data);
134
135 my ($num, $len, $data) = unpack "NNa*", $data;
136
137 my @dirty;
138
139 for my $x (0..$self->{mapw} - 1) {
140 for my $y (0..$self->{maph} - 1) {
141 push @dirty, [$x, $y]
142 if grep $_ == $num, @{$self->{map}[$x][$y] || []};
143 }
144 }
145
146 $self->map_update (\@dirty);
147 }
148
149 =back
150
151 =head1 AUTHOR
152
153 Marc Lehmann <schmorp@schmorp.de>
154 http://home.schmorp.de/
155
156 Robin Redeker <elmex@ta-sa.org>
157 http://www.ta-sa.org/
158
159 =cut
160
161 1