1 |
/* |
2 |
* This file is part of Deliantra, the Roguelike Realtime MMORPG. |
3 |
* |
4 |
* Copyright (©) 2005,2006,2007,2008,2009,2010,2011 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 |
/** \file |
26 |
* Image related communication |
27 |
* |
28 |
* \date 2003-12-02 |
29 |
* |
30 |
* This file deals with the image related communication to the |
31 |
* client. I've located all the functions in this file - this |
32 |
* localizes it more, and means that we don't need to declare |
33 |
* things like all the structures as globals. |
34 |
*/ |
35 |
|
36 |
#include <global.h> |
37 |
#include <sproto.h> |
38 |
|
39 |
#include "crc.h" |
40 |
|
41 |
#define MAX_IMAGE_SIZE 10000 |
42 |
|
43 |
/** |
44 |
* client requested an image. send it rate-limited |
45 |
* before flushing. |
46 |
*/ |
47 |
void |
48 |
AskFaceCmd (char *buf, int len, client *ns) |
49 |
{ |
50 |
int idx = 0, pri = 0; |
51 |
|
52 |
sscanf (buf, "%d %d", &idx, &pri); |
53 |
|
54 |
//TODO: somehow fetch default priority from send_fx here |
55 |
|
56 |
const facedata *d = face_data (idx, ns->faceset); |
57 |
|
58 |
if (!d) |
59 |
return; // doh |
60 |
|
61 |
client::ixsend ix; |
62 |
|
63 |
ix.pri = pri; |
64 |
ix.idx = idx; |
65 |
ix.ofs = d->data.size (); |
66 |
|
67 |
auto (pos, ns->ixface.end ()); |
68 |
|
69 |
// the by far most common case will be to insert |
70 |
// near the end, so little looping. |
71 |
while (pos != ns->ixface.begin ()) |
72 |
{ |
73 |
--pos; |
74 |
|
75 |
// sort within 2k bins, to slightly prefer smaller images |
76 |
if (pri > pos->pri || (pri == pos->pri && (ix.ofs >> 11) <= (pos->ofs >> 11))) |
77 |
{ |
78 |
++pos; |
79 |
break; |
80 |
} |
81 |
} |
82 |
|
83 |
ns->ixface.insert (pos, ix); |
84 |
|
85 |
#if 0 |
86 |
for (auto (i, ns->ixface.begin ()); i != ns->ixface.end (); ++i) |
87 |
fprintf (stderr, "<%d,%d> ", i->pri, i->ofs); |
88 |
fprintf (stderr, "\n"); |
89 |
#endif |
90 |
} |
91 |
|
92 |
/** |
93 |
* Sends a face to a client if they are in pixmap mode |
94 |
* nothing gets sent in bitmap mode. |
95 |
* If nocache is true (nonzero), ignore the cache setting from the client - |
96 |
* this is needed for the askface, in which we really do want to send the |
97 |
* face (and askface is the only place that should be setting it). Otherwise, |
98 |
* we look at the facecache, and if set, send the image name. |
99 |
*/ |
100 |
void |
101 |
client::send_face (faceidx facenum, int pri) |
102 |
{ |
103 |
// never send face 0. ever. it does not exist. |
104 |
if (!facenum) |
105 |
return; |
106 |
|
107 |
faceinfo *f = face_info (facenum); |
108 |
|
109 |
if (!f) |
110 |
{ |
111 |
LOG (llevError | logBacktrace, "client::send_face (%d) out of bounds??\n", facenum); |
112 |
return; |
113 |
} |
114 |
|
115 |
// refuse to send non-image faces |
116 |
if (!fx_want [f->type]) |
117 |
return; |
118 |
|
119 |
if (faces_sent [facenum]) |
120 |
return; |
121 |
|
122 |
faces_sent[facenum] = true; |
123 |
|
124 |
fxface.push_back (facenum); |
125 |
} |
126 |
|
127 |
void client::flush_fx () |
128 |
{ |
129 |
while (!fxface.empty ()) |
130 |
{ |
131 |
packet fx ("fx"); |
132 |
packet sx ("sx"); |
133 |
int type = 0; |
134 |
|
135 |
do |
136 |
{ |
137 |
faceidx facenum = fxface.back (); fxface.pop_back (); |
138 |
|
139 |
if (const faceinfo *f = face_info (facenum)) |
140 |
{ |
141 |
if (f->type != type) |
142 |
{ |
143 |
type = f->type; |
144 |
|
145 |
fx << ber32 (0) |
146 |
<< uint8 (1) << uint8 (type); |
147 |
} |
148 |
|
149 |
const facedata *d = f->data (faceset); |
150 |
|
151 |
fx << ber32 (facenum) |
152 |
<< data8 (d->chksum, d->chksum_len); |
153 |
|
154 |
if (smoothing) |
155 |
{ |
156 |
faceinfo *f = face_info (facenum); |
157 |
|
158 |
if (f->smooth) |
159 |
{ |
160 |
send_face (f->smooth, -110); |
161 |
|
162 |
sx << ber32 (facenum) |
163 |
<< ber32 (f->smooth) |
164 |
<< ber32 (f->smoothlevel); |
165 |
} |
166 |
} |
167 |
} |
168 |
} |
169 |
while (!fxface.empty () |
170 |
&& fx.room () > ber32::size + CHKSUM_MAXLEN + 1 + 3 /* type switch */ |
171 |
&& sx.room () > ber32::size * 3); |
172 |
|
173 |
send_packet (fx); |
174 |
if (sx.length () > 3) send_packet (sx); |
175 |
} |
176 |
} |
177 |
|
178 |
// send all faces of this object to the client |
179 |
// this uses more bandwidth initially, but makes |
180 |
// animations look much smoother, and every client |
181 |
// is supposed to do client-side caching anyways. |
182 |
void |
183 |
client::send_faces (object *ob) |
184 |
{ |
185 |
send_face (ob->face, 10); |
186 |
|
187 |
if (ob->animation_id) |
188 |
{ |
189 |
const animation &anim = ob->anim (); |
190 |
|
191 |
for (int i = 0; i < anim.num_animations; i++) |
192 |
send_face (anim.faces [i], -10); |
193 |
} |
194 |
} |
195 |
|
196 |
/** |
197 |
* Need to send an animation sequence to the client. |
198 |
* We will send appropriate face commands to the client if we haven't |
199 |
* sent them the face yet (this can become quite costly in terms of |
200 |
* how much we are sending - on the other hand, this should only happen |
201 |
* when the player logs in and picks stuff up. |
202 |
*/ |
203 |
void |
204 |
client::send_animation (short anim_num) |
205 |
{ |
206 |
/* Do some checking on the anim_num we got. Note that the animations |
207 |
* are added in contiguous order, so if the number is in the valid |
208 |
* range, it must be a valid animation. |
209 |
*/ |
210 |
if (anim_num < 0 || anim_num >= animations.size ()) |
211 |
{ |
212 |
LOG (llevError, "esrv_send_anim (%d) out of bounds??\n", anim_num); |
213 |
return; |
214 |
} |
215 |
|
216 |
packet sl ("anim"); |
217 |
|
218 |
sl << uint16 (anim_num) |
219 |
<< uint16 (0); /* flags - not used right now */ |
220 |
|
221 |
/* Build up the list of faces. Also, send any information (ie, the |
222 |
* the face itself) down to the client. |
223 |
*/ |
224 |
for (int i = 0; i < animations[anim_num].num_animations; i++) |
225 |
{ |
226 |
send_face (animations[anim_num].faces[i], -20); |
227 |
sl << uint16 (animations[anim_num].faces[i]); |
228 |
} |
229 |
|
230 |
send_packet (sl); |
231 |
|
232 |
anims_sent[anim_num] = 1; |
233 |
} |
234 |
|