ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/deliantra/server/socket/image.C
Revision: 1.72
Committed: Sun Nov 11 04:29:11 2012 UTC (13 years, 11 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.71: +30 -9 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 elmex 1.1 /*
2 root 1.55 * This file is part of Deliantra, the Roguelike Realtime MMORPG.
3 root 1.69 *
4 root 1.67 * Copyright (©) 2005,2006,2007,2008,2009,2010,2011,2012 Marc Alexander Lehmann / Robin Redeker / the Deliantra team
5 root 1.63 * Copyright (©) 2001 Mark Wedel
6     * Copyright (©) 1992 Frank Tore Johansen
7 root 1.69 *
8 root 1.60 * 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 root 1.69 *
13 root 1.44 * 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 root 1.69 *
18 root 1.60 * 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 root 1.69 *
22 root 1.55 * The authors can be reached via e-mail to <support@deliantra.net>
23 root 1.17 */
24 elmex 1.1
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 root 1.20 #include "crc.h"
40    
41 elmex 1.1 #define MAX_IMAGE_SIZE 10000
42    
43     /**
44 root 1.43 * client requested an image. send it rate-limited
45     * before flushing.
46 elmex 1.1 */
47 root 1.3 void
48 root 1.43 AskFaceCmd (char *buf, int len, client *ns)
49 elmex 1.1 {
50 root 1.43 int idx = 0, pri = 0;
51    
52     sscanf (buf, "%d %d", &idx, &pri);
53    
54 root 1.51 //TODO: somehow fetch default priority from send_fx here
55    
56 root 1.43 const facedata *d = face_data (idx, ns->faceset);
57    
58     if (!d)
59     return; // doh
60    
61 root 1.72 /* cfperl_ix calls cf::face::ix which loads the data */
62     /* and then calls cf::client::send_ix to actually queue the ix */
63     cfperl_ix (ns, idx, pri, d->size, d->fofs);
64     }
65 root 1.43
66 root 1.72 void
67     client::ix_send (faceidx idx, sint16 pri, SV *data_sv)
68     {
69     STRLEN size;
70     char *data = SvPVbyte (data_sv, size);
71     ixsend ix;
72    
73     ix.pri = pri;
74     ix.idx = idx;
75     ix.ofs = size;
76     ix.data = (uint8 *)data;
77     ix.data_sv = SvREFCNT_inc (data_sv);
78 root 1.43
79 root 1.72 auto (pos, ixface.end ());
80 root 1.46
81 root 1.58 // the by far most common case will be to insert
82     // near the end, so little looping.
83 root 1.72 while (pos != ixface.begin ())
84 root 1.46 {
85 root 1.58 --pos;
86    
87     // sort within 2k bins, to slightly prefer smaller images
88     if (pri > pos->pri || (pri == pos->pri && (ix.ofs >> 11) <= (pos->ofs >> 11)))
89 root 1.46 {
90 root 1.58 ++pos;
91     break;
92 root 1.46 }
93     }
94    
95 root 1.72 ixface.insert (pos, ix);
96 root 1.45
97     #if 0
98 root 1.72 for (auto (i, ixface.begin ()); i != ixface.end (); ++i)
99 root 1.46 fprintf (stderr, "<%d,%d> ", i->pri, i->ofs);
100     fprintf (stderr, "\n");
101 root 1.45 #endif
102 root 1.43 }
103    
104 root 1.72 void
105     client::ix_pop ()
106     {
107     ixsend &ix = ixface.back ();
108    
109     SvREFCNT_dec (ix.data_sv);
110    
111     ixface.pop_back ();
112     }
113    
114 root 1.43 /**
115 root 1.70 * Sends a face offer (fx) to a client.
116 elmex 1.1 * If nocache is true (nonzero), ignore the cache setting from the client -
117     * this is needed for the askface, in which we really do want to send the
118     * face (and askface is the only place that should be setting it). Otherwise,
119     * we look at the facecache, and if set, send the image name.
120     */
121 root 1.3 void
122 root 1.50 client::send_face (faceidx facenum, int pri)
123 elmex 1.1 {
124 root 1.28 // never send face 0. ever. it does not exist.
125     if (!facenum)
126     return;
127    
128 root 1.50 faceinfo *f = face_info (facenum);
129 root 1.28
130 root 1.50 if (!f)
131 root 1.3 {
132 root 1.54 LOG (llevError | logBacktrace, "client::send_face (%d) out of bounds??\n", facenum);
133 root 1.3 return;
134     }
135    
136 root 1.68 // refuse to send non-image faces unless requested
137 root 1.50 if (!fx_want [f->type])
138     return;
139 root 1.49
140 root 1.50 if (faces_sent [facenum])
141 root 1.48 return;
142    
143 root 1.68 faces_sent [facenum] = true;
144 root 1.32
145 root 1.58 fxface.push_back (facenum);
146 root 1.32 }
147    
148 root 1.39 void client::flush_fx ()
149     {
150     while (!fxface.empty ())
151     {
152 root 1.40 packet fx ("fx");
153     packet sx ("sx");
154 root 1.50 int type = 0;
155 root 1.39
156     do
157     {
158     faceidx facenum = fxface.back (); fxface.pop_back ();
159    
160 root 1.50 if (const faceinfo *f = face_info (facenum))
161     {
162     if (f->type != type)
163     {
164     type = f->type;
165    
166     fx << ber32 (0)
167     << uint8 (1) << uint8 (type);
168     }
169    
170     const facedata *d = f->data (faceset);
171 root 1.39
172 root 1.40 fx << ber32 (facenum)
173 root 1.62 << data8 (d->chksum, d->chksum_len);
174 root 1.39
175 root 1.40 if (smoothing)
176     {
177     faceinfo *f = face_info (facenum);
178    
179     if (f->smooth)
180     {
181 root 1.50 send_face (f->smooth, -110);
182    
183 root 1.40 sx << ber32 (facenum)
184     << ber32 (f->smooth)
185     << ber32 (f->smoothlevel);
186     }
187     }
188 root 1.39 }
189     }
190 root 1.40 while (!fxface.empty ()
191 root 1.62 && fx.room () > ber32::size + CHKSUM_MAXLEN + 1 + 3 /* type switch */
192 root 1.40 && sx.room () > ber32::size * 3);
193 root 1.39
194 root 1.40 send_packet (fx);
195     if (sx.length () > 3) send_packet (sx);
196 root 1.39 }
197     }
198    
199 root 1.28 // send all faces of this object to the client
200     // this uses more bandwidth initially, but makes
201     // animations look much smoother, and every client
202     // is supposed to do client-side caching anyways.
203     void
204     client::send_faces (object *ob)
205     {
206 root 1.50 send_face (ob->face, 10);
207 root 1.28
208     if (ob->animation_id)
209     {
210 root 1.59 const animation &anim = ob->anim ();
211 root 1.28
212     for (int i = 0; i < anim.num_animations; i++)
213 root 1.50 send_face (anim.faces [i], -10);
214 root 1.28 }
215     }
216    
217     /**
218     * Need to send an animation sequence to the client.
219     * We will send appropriate face commands to the client if we haven't
220     * sent them the face yet (this can become quite costly in terms of
221     * how much we are sending - on the other hand, this should only happen
222     * when the player logs in and picks stuff up.
223     */
224     void
225     client::send_animation (short anim_num)
226     {
227     /* Do some checking on the anim_num we got. Note that the animations
228 root 1.61 * are added in contiguous order, so if the number is in the valid
229 root 1.28 * range, it must be a valid animation.
230     */
231 root 1.41 if (anim_num < 0 || anim_num >= animations.size ())
232 root 1.28 {
233     LOG (llevError, "esrv_send_anim (%d) out of bounds??\n", anim_num);
234     return;
235     }
236    
237     packet sl ("anim");
238    
239     sl << uint16 (anim_num)
240     << uint16 (0); /* flags - not used right now */
241    
242     /* Build up the list of faces. Also, send any information (ie, the
243     * the face itself) down to the client.
244     */
245     for (int i = 0; i < animations[anim_num].num_animations; i++)
246     {
247 root 1.50 send_face (animations[anim_num].faces[i], -20);
248 root 1.61 sl << uint16 (animations[anim_num].faces[i]);
249 elmex 1.1 }
250 root 1.6
251 root 1.28 send_packet (sl);
252    
253     anims_sent[anim_num] = 1;
254 elmex 1.1 }
255    
256 root 1.66 void
257     client::invalidate_face (faceidx idx)
258     {
259     faces_sent [idx] = false;
260     force_newmap = true;
261     }
262    
263     void
264     client::invalidate_all_faces ()
265     {
266     faces_sent.reset ();
267     force_newmap = true;
268     }
269