1 |
/* |
2 |
* This file is part of Deliantra, the Roguelike Realtime MMORPG. |
3 |
* |
4 |
* Copyright (©) 2005,2006,2007,2008,2009,2010,2011,2012,2013,2014,2015,2016 Marc Alexander Lehmann / Robin Redeker / the Deliantra team |
5 |
* Copyright (©) 2001 Mark Wedel |
6 |
* Copyright (©) 1992 Frank Tore Johansen |
7 |
* |
8 |
* Deliantra is free software: you can redistribute it and/or modify it under |
9 |
* the terms of the Affero GNU General Public License as published by the |
10 |
* Free Software Foundation, either version 3 of the License, or (at your |
11 |
* option) any later version. |
12 |
* |
13 |
* This program is distributed in the hope that it will be useful, |
14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 |
* GNU General Public License for more details. |
17 |
* |
18 |
* You should have received a copy of the Affero GNU General Public License |
19 |
* and the GNU General Public License along with this program. If not, see |
20 |
* <http://www.gnu.org/licenses/>. |
21 |
* |
22 |
* The authors can be reached via e-mail to <support@deliantra.net> |
23 |
*/ |
24 |
|
25 |
/* |
26 |
* Image related communication |
27 |
* |
28 |
* This file deals with the image related communication to the |
29 |
* client. I've located all the functions in this file - this |
30 |
* localizes it more, and means that we don't need to declare |
31 |
* things like all the structures as globals. |
32 |
*/ |
33 |
|
34 |
#include <global.h> |
35 |
#include <sproto.h> |
36 |
|
37 |
#include "crc.h" |
38 |
|
39 |
#define MAX_IMAGE_SIZE 10000 |
40 |
|
41 |
/** |
42 |
* client requested an image. send it rate-limited |
43 |
* before flushing. |
44 |
*/ |
45 |
void |
46 |
AskFaceCmd (char *buf, int len, client *ns) |
47 |
{ |
48 |
int idx = 0, pri = 0; |
49 |
|
50 |
sscanf (buf, "%d %d", &idx, &pri); |
51 |
|
52 |
//TODO: somehow fetch default priority from send_fx here |
53 |
|
54 |
const faceinfo *f = face_info (idx); |
55 |
|
56 |
if (!f) |
57 |
return; // doh |
58 |
|
59 |
int set = ns->tileset; |
60 |
|
61 |
if (!f->face [set].chksum_len) |
62 |
set = 0; |
63 |
|
64 |
/* cfperl_ix calls cf::face::ix which loads the data */ |
65 |
/* and then calls cf::client::send_ix to actually queue the ix */ |
66 |
cfperl_ix (ns, set, idx, pri); |
67 |
} |
68 |
|
69 |
void |
70 |
client::ix_send (faceidx idx, sint16 pri, SV *data_sv) |
71 |
{ |
72 |
STRLEN size; |
73 |
char *data = SvPVbyte (data_sv, size); |
74 |
ixsend ix; |
75 |
|
76 |
ix.pri = pri; |
77 |
ix.idx = idx; |
78 |
ix.ofs = size; |
79 |
ix.data = (uint8 *)data; |
80 |
ix.data_sv = SvREFCNT_inc (data_sv); |
81 |
|
82 |
auto (pos, ixface.end ()); |
83 |
|
84 |
// the by far most common case will be to insert |
85 |
// near the end, so little looping. |
86 |
while (pos != ixface.begin ()) |
87 |
{ |
88 |
--pos; |
89 |
|
90 |
// sort within 2k bins, to slightly prefer smaller images |
91 |
if (pri > pos->pri || (pri == pos->pri && (ix.ofs >> 11) <= (pos->ofs >> 11))) |
92 |
{ |
93 |
++pos; |
94 |
break; |
95 |
} |
96 |
} |
97 |
|
98 |
ixface.insert (pos, ix); |
99 |
|
100 |
#if 0 |
101 |
for (auto (i, ixface.begin ()); i != ixface.end (); ++i) |
102 |
fprintf (stderr, "<%d,%d> ", i->pri, i->ofs); |
103 |
fprintf (stderr, "\n"); |
104 |
#endif |
105 |
} |
106 |
|
107 |
void |
108 |
client::ix_pop () |
109 |
{ |
110 |
ixsend &ix = ixface.back (); |
111 |
|
112 |
SvREFCNT_dec (ix.data_sv); |
113 |
|
114 |
ixface.pop_back (); |
115 |
} |
116 |
|
117 |
/** |
118 |
* Sends a face offer (fx) to a client. |
119 |
* If nocache is true (nonzero), ignore the cache setting from the client - |
120 |
* this is needed for the askface, in which we really do want to send the |
121 |
* face (and askface is the only place that should be setting it). Otherwise, |
122 |
* we look at the facecache, and if set, send the image name. |
123 |
*/ |
124 |
void |
125 |
client::send_face (faceidx facenum, int pri) |
126 |
{ |
127 |
// never send face 0. ever. it does not exist. |
128 |
if (!facenum) |
129 |
return; |
130 |
|
131 |
faceinfo *f = face_info (facenum); |
132 |
|
133 |
if (!f) |
134 |
{ |
135 |
LOG (llevError | logBacktrace, "client::send_face (%d) out of bounds??\n", facenum); |
136 |
return; |
137 |
} |
138 |
|
139 |
// refuse to send non-image faces unless requested |
140 |
if (!fx_want [f->type]) |
141 |
return; |
142 |
|
143 |
if (faces_sent [facenum]) |
144 |
return; |
145 |
|
146 |
faces_sent [facenum] = true; |
147 |
|
148 |
fxface.push_back (facenum); |
149 |
} |
150 |
|
151 |
void client::flush_fx () |
152 |
{ |
153 |
while (!fxface.empty ()) |
154 |
{ |
155 |
packet fx ("fx"); |
156 |
packet sx ("sx"); |
157 |
int type = 0; |
158 |
|
159 |
do |
160 |
{ |
161 |
faceidx facenum = fxface.back (); fxface.pop_back (); |
162 |
|
163 |
if (const faceinfo *f = face_info (facenum)) |
164 |
{ |
165 |
if (f->type != type) |
166 |
{ |
167 |
type = f->type; |
168 |
|
169 |
fx << ber32 (0) |
170 |
<< uint8 (1) << uint8 (type); |
171 |
} |
172 |
|
173 |
const facedata *d = f->data (tileset); |
174 |
|
175 |
fx << ber32 (facenum) |
176 |
<< data8 (d->chksum, d->chksum_len); |
177 |
|
178 |
if (smoothing) |
179 |
{ |
180 |
faceinfo *f = face_info (facenum); |
181 |
|
182 |
if (f->smooth) |
183 |
{ |
184 |
send_face (f->smooth, -110); |
185 |
|
186 |
sx << ber32 (facenum) |
187 |
<< ber32 (f->smooth) |
188 |
<< ber32 (f->smoothlevel); |
189 |
} |
190 |
} |
191 |
} |
192 |
} |
193 |
while (!fxface.empty () |
194 |
&& fx.room () > ber32::size + CHKSUM_MAXLEN + 1 + 3 /* type switch */ |
195 |
&& sx.room () > ber32::size * 3); |
196 |
|
197 |
send_packet (fx); |
198 |
if (sx.length () > 3) send_packet (sx); |
199 |
} |
200 |
} |
201 |
|
202 |
// send all faces of this object to the client |
203 |
// this uses more bandwidth initially, but makes |
204 |
// animations look much smoother, and every client |
205 |
// is supposed to do client-side caching anyways. |
206 |
void |
207 |
client::send_faces (object *ob) |
208 |
{ |
209 |
send_face (ob->face, 10); |
210 |
|
211 |
if (ob->animation_id) |
212 |
{ |
213 |
const animation &anim = ob->anim (); |
214 |
|
215 |
for (int i = 0; i < anim.num_animations; i++) |
216 |
send_face (anim.faces [i], -10); |
217 |
} |
218 |
} |
219 |
|
220 |
/** |
221 |
* Need to send an animation sequence to the client. |
222 |
* We will send appropriate face commands to the client if we haven't |
223 |
* sent them the face yet (this can become quite costly in terms of |
224 |
* how much we are sending - on the other hand, this should only happen |
225 |
* when the player logs in and picks stuff up. |
226 |
*/ |
227 |
void |
228 |
client::send_animation (short anim_num) |
229 |
{ |
230 |
/* Do some checking on the anim_num we got. Note that the animations |
231 |
* are added in contiguous order, so if the number is in the valid |
232 |
* range, it must be a valid animation. |
233 |
*/ |
234 |
if (anim_num < 0 || anim_num >= animations.size ()) |
235 |
{ |
236 |
LOG (llevError, "esrv_send_anim (%d) out of bounds??\n", anim_num); |
237 |
return; |
238 |
} |
239 |
|
240 |
packet sl ("anim"); |
241 |
|
242 |
sl << uint16 (anim_num) |
243 |
<< uint16 (0); /* flags - not used right now */ |
244 |
|
245 |
/* Build up the list of faces. Also, send any information (ie, the |
246 |
* the face itself) down to the client. |
247 |
*/ |
248 |
for (int i = 0; i < animations[anim_num].num_animations; i++) |
249 |
{ |
250 |
send_face (animations[anim_num].faces[i], -20); |
251 |
sl << uint16 (animations[anim_num].faces[i]); |
252 |
} |
253 |
|
254 |
send_packet (sl); |
255 |
|
256 |
anims_sent[anim_num] = 1; |
257 |
} |
258 |
|
259 |
void |
260 |
client::invalidate_face (faceidx idx) |
261 |
{ |
262 |
if (!faces_sent [idx]) |
263 |
return; |
264 |
|
265 |
faces_sent [idx] = false; |
266 |
send_face (idx); |
267 |
//TODO: check for active ix and abort it. |
268 |
} |
269 |
|
270 |
void |
271 |
client::invalidate_all_faces () |
272 |
{ |
273 |
for (faceidx i = 0; i < faces_sent.size (); ++i) |
274 |
invalidate_face (i); |
275 |
} |
276 |
|