ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/socket/request.C
Revision: 1.173
Committed: Thu Apr 15 00:37:24 2010 UTC (14 years, 1 month ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.172: +2 -6 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 elmex 1.1 /*
2 root 1.123 * This file is part of Deliantra, the Roguelike Realtime MMORPG.
3 pippijn 1.64 *
4 root 1.165 * Copyright (©) 2005,2006,2007,2008,2009,2010 Marc Alexander Lehmann / Robin Redeker / the Deliantra team
5 root 1.164 * Copyright (©) 2001 Mark Wedel
6     * Copyright (©) 1992 Frank Tore Johansen
7 pippijn 1.64 *
8 root 1.150 * 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 pippijn 1.64 *
13 root 1.112 * 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 pippijn 1.64 *
18 root 1.150 * 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.100 *
22 root 1.123 * The authors can be reached via e-mail to <support@deliantra.net>
23 pippijn 1.64 */
24 elmex 1.1
25 root 1.155 //+GPL
26    
27 elmex 1.1 /**
28     * \file
29     * Client handling.
30     *
31     * \date 2003-12-02
32     *
33     * This file implements all of the goo on the server side for handling
34     * clients. It's got a bunch of global variables for keeping track of
35     * each of the clients.
36     *
37     * Note: All functions that are used to process data from the client
38     * have the prototype of (char *data, int datalen, int client_num). This
39     * way, we can use one dispatch table.
40     *
41     * esrv_map_new starts updating the map
42     *
43     */
44    
45     #include <global.h>
46     #include <sproto.h>
47    
48     #include <living.h>
49    
50     /* This block is basically taken from socket.c - I assume if it works there,
51     * it should work here.
52     */
53 pippijn 1.26 #include <sys/types.h>
54     #include <sys/time.h>
55     #include <sys/socket.h>
56     #include <netinet/in.h>
57     #include <netdb.h>
58 elmex 1.1
59 root 1.38 #include <unistd.h>
60     #include <sys/time.h>
61 elmex 1.1
62     #include "sounds.h"
63    
64     /**
65     * This table translates the attack numbers as used within the
66     * program to the value we use when sending STATS command to the
67     * client. If a value is -1, then we don't send that to the
68     * client.
69     */
70 root 1.172 static short atnr_cs_stat[NROFATTACKS] =
71     {
72     CS_STAT_RES_PHYS,
73     CS_STAT_RES_MAG,
74     CS_STAT_RES_FIRE,
75     CS_STAT_RES_ELEC,
76     CS_STAT_RES_COLD,
77     CS_STAT_RES_CONF,
78     CS_STAT_RES_ACID,
79     CS_STAT_RES_DRAIN,
80     -1 /* weaponmagic */,
81     CS_STAT_RES_GHOSTHIT,
82     CS_STAT_RES_POISON,
83     CS_STAT_RES_SLOW,
84     CS_STAT_RES_PARA,
85     CS_STAT_TURN_UNDEAD,
86     CS_STAT_RES_FEAR,
87     -1 /* Cancellation */,
88     CS_STAT_RES_DEPLETE,
89     CS_STAT_RES_DEATH,
90     -1 /* Chaos */,
91     -1 /* Counterspell */,
92     -1 /* Godpower */,
93     CS_STAT_RES_HOLYWORD,
94 root 1.16 CS_STAT_RES_BLIND,
95     -1, /* Internal */
96     -1, /* life stealing */
97     -1 /* Disease - not fully done yet */
98 elmex 1.1 };
99    
100 root 1.2 static void
101 root 1.43 socket_map_scroll (client *ns, int dx, int dy)
102 root 1.2 {
103 root 1.16 struct Map newmap;
104     int x, y, mx, my;
105 root 1.2
106 root 1.42 ns->send_packet_printf ("map_scroll %d %d", dx, dy);
107 root 1.2
108 root 1.16 /* If we are using the Map1aCmd, we may in fact send
109     * head information that is outside the viewable map.
110     * So set the mx,my to the max value we want to
111     * look for. Removed code to do so - it caused extra
112     * complexities for the client, and probably doesn't make
113     * that much difference in bandwidth.
114     */
115     mx = ns->mapx;
116     my = ns->mapy;
117 root 1.2
118 root 1.16 /* the x and y here are coordinates for the new map, i.e. if we moved
119 root 1.143 * (dx,dy), newmap[x][y] = oldmap[x-dx][y-dy]. For this reason,
120 root 1.16 * if the destination x or y coordinate is outside the viewable
121     * area, we clear the values - otherwise, the old values
122     * are preserved, and the check_head thinks it needs to clear them.
123     */
124     for (x = 0; x < mx; x++)
125 root 1.143 for (y = 0; y < my; y++)
126     if (x >= ns->mapx || y >= ns->mapy)
127     /* clear cells outside the viewable area */
128     memset (&newmap.cells[x][y], 0, sizeof (struct MapCell));
129     else if ((x + dx) < 0 || (x + dx) >= ns->mapx || (y + dy) < 0 || (y + dy) >= ns->mapy)
130     /* clear newly visible tiles within the viewable area */
131     memset (&(newmap.cells[x][y]), 0, sizeof (struct MapCell));
132     else
133     memcpy (&(newmap.cells[x][y]), &(ns->lastmap.cells[x + dx][y + dy]), sizeof (struct MapCell));
134 root 1.2
135 root 1.16 memcpy (&(ns->lastmap), &newmap, sizeof (struct Map));
136 root 1.2
137 root 1.16 /* Make sure that the next "map1" command will be sent (even if it is
138     * empty).
139     */
140     ns->sent_scroll = 1;
141 root 1.2 }
142    
143 root 1.7 static void
144     clear_map (player *pl)
145     {
146 root 1.119 pl->ns->mapinfo_queue_clear ();
147    
148 root 1.50 memset (&pl->ns->lastmap, 0, sizeof (pl->ns->lastmap));
149 root 1.7
150 root 1.79 pl->ns->force_newmap = false;
151    
152 root 1.50 if (pl->ns->newmapcmd == 1)
153     pl->ns->send_packet ("newmap");
154 root 1.7
155 root 1.50 pl->ns->floorbox_reset ();
156 root 1.7 }
157    
158 root 1.131 static void
159     send_map_info (player *pl)
160     {
161     client &socket = *pl->ns;
162 root 1.154 object *ob = pl->viewpoint;
163 root 1.131
164     if (socket.mapinfocmd)
165     {
166     if (ob->map && ob->map->path[0])
167     {
168     int flags = 0;
169    
170     if (ob->map->tile_path[0]) flags |= 1;
171     if (ob->map->tile_path[1]) flags |= 2;
172     if (ob->map->tile_path[2]) flags |= 4;
173     if (ob->map->tile_path[3]) flags |= 8;
174    
175     socket.send_packet_printf ("mapinfo - spatial %d %d %d %d %d %s",
176     flags, socket.mapx / 2 - ob->x, socket.mapy / 2 - ob->y,
177     ob->map->width, ob->map->height, &ob->map->path);
178     }
179     else
180     socket.send_packet ("mapinfo current");
181     }
182     }
183    
184 root 1.66 /** check for map/region change and send new map data */
185 elmex 1.1 static void
186     check_map_change (player *pl)
187     {
188 root 1.50 client &socket = *pl->ns;
189 root 1.154 object *ob = pl->viewpoint;
190 elmex 1.1
191 root 1.131 region *reg = ob->region ();
192     if (socket.current_region != reg)
193     {
194     INVOKE_PLAYER (REGION_CHANGE, pl, ARG_REGION (reg), ARG_REGION (socket.current_region));
195     socket.current_region = reg;
196     }
197    
198 root 1.130 // first try to aovid a full newmap on tiled map scrolls
199 root 1.131 if (socket.current_map != ob->map && !socket.force_newmap)
200 root 1.130 {
201     rv_vector rv;
202    
203     get_rangevector_from_mapcoord (socket.current_map, socket.current_x, socket.current_y, ob, &rv, 0);
204    
205     // manhattan distance is very handy here
206     if (rv.distance < 8) // 8 works nicely for speed << 70 and buggy gcfclient
207     {
208     socket.current_map = ob->map;
209     socket.current_x = ob->x;
210     socket.current_y = ob->y;
211    
212     socket_map_scroll (&socket, rv.distance_x, rv.distance_y);
213     socket.floorbox_reset ();
214 root 1.131 send_map_info (pl);
215 root 1.130 }
216     }
217    
218 root 1.85 if (socket.current_map != ob->map || socket.force_newmap)
219 root 1.79 {
220 root 1.7 clear_map (pl);
221 root 1.80 socket.current_map = ob->map;
222 root 1.131 send_map_info (pl);
223 elmex 1.1 }
224 root 1.2 else if (socket.current_x != ob->x || socket.current_y != ob->y)
225     {
226 root 1.7 int dx = ob->x - socket.current_x;
227     int dy = ob->y - socket.current_y;
228    
229 root 1.146 socket_map_scroll (&socket, ob->x - socket.current_x, ob->y - socket.current_y);
230     socket.floorbox_reset ();
231 root 1.2 }
232    
233     socket.current_x = ob->x;
234     socket.current_y = ob->y;
235 elmex 1.1 }
236    
237 root 1.40 /**
238 root 1.157 * This sends the skill number to name mapping. We ignore
239     * the params - we always send the same info no matter what.
240     */
241 root 1.158 static void
242 root 1.157 send_skill_info (client *ns, char *params)
243     {
244     packet sl;
245     sl << "replyinfo skill_info\n";
246    
247 root 1.172 for (int i = 0; i < skillvec.size (); ++i)
248     sl.printf ("%d:%s\n", CS_STAT_SKILLINFO + i, &skillvec [i]->name);
249 root 1.157
250     if (sl.length () > MAXSOCKBUF)
251     {
252     LOG (llevError, "Buffer overflow in send_skill_info!\n");
253     fatal (0);
254     }
255    
256     ns->send_packet (sl);
257     }
258    
259     /**
260     * This sends the spell path to name mapping. We ignore
261     * the params - we always send the same info no matter what.
262     */
263 root 1.158 static void
264 root 1.157 send_spell_paths (client * ns, char *params)
265     {
266     packet sl;
267    
268     sl << "replyinfo spell_paths\n";
269    
270     for (int i = 0; i < NRSPELLPATHS; i++)
271     sl.printf ("%d:%s\n", 1 << i, spellpathnames[i]);
272    
273     if (sl.length () > MAXSOCKBUF)
274     {
275     LOG (llevError, "Buffer overflow in send_spell_paths!\n");
276     fatal (0);
277     }
278    
279     ns->send_packet (sl);
280     }
281    
282     /**
283 root 1.40 * RequestInfo is sort of a meta command. There is some specific
284     * request of information, but we call other functions to provide
285     * that information.
286     */
287     void
288 root 1.114 RequestInfo (char *buf, int len, client *ns)
289 root 1.40 {
290 root 1.114 char *params;
291 root 1.40
292     /* find the first space, make it null, and update the
293     * params pointer.
294     */
295 root 1.114 for (params = buf; *params; params++)
296     if (*params == ' ')
297 root 1.40 {
298 root 1.114 *params++ = 0;
299 root 1.40 break;
300     }
301    
302 root 1.153 if (!strcmp (buf, "skill_info"))
303 root 1.40 send_skill_info (ns, params);
304     else if (!strcmp (buf, "spell_paths"))
305     send_spell_paths (ns, params);
306     else
307 root 1.114 {
308     // undo tokenisation above and send replyinfo with the request unchanged
309     if (*params)
310     *--params = ' ';
311    
312     ns->send_packet_printf ("replyinfo %s", buf);
313     }
314 root 1.40 }
315    
316 root 1.16 void
317     ExtCmd (char *buf, int len, player *pl)
318 elmex 1.1 {
319 root 1.10 INVOKE_PLAYER (EXTCMD, pl, ARG_DATA (buf, len));
320 elmex 1.1 }
321    
322 root 1.16 void
323 root 1.45 ExtiCmd (char *buf, int len, client *ns)
324     {
325     INVOKE_CLIENT (EXTICMD, ns, ARG_DATA (buf, len));
326     }
327    
328 root 1.155 //-GPL
329    
330 root 1.45 void
331 root 1.119 client::mapinfo_queue_clear ()
332 elmex 1.1 {
333 root 1.119 for (auto (i, mapinfo_queue.begin ()); i != mapinfo_queue.end (); ++i)
334     free (*i);
335 root 1.16
336 root 1.119 mapinfo_queue.clear ();
337     }
338 elmex 1.1
339 root 1.119 bool
340     client::mapinfo_try (char *buf)
341     {
342     char *token = buf;
343     buf += strlen (buf) + 9;
344 elmex 1.1
345 root 1.119 // initial map and its origin
346 root 1.154 maptile *map = pl->viewpoint->map;
347     int mapx = pl->ns->mapx / 2 - pl->viewpoint->x;
348     int mapy = pl->ns->mapy / 2 - pl->viewpoint->y;
349 root 1.119 int max_distance = 8; // limit maximum path length to something generous
350 elmex 1.1
351 root 1.119 while (*buf && map && max_distance)
352 elmex 1.1 {
353 root 1.119 int dir = *buf++ - '1';
354 elmex 1.1
355 root 1.119 if (dir >= 0 && dir <= 3)
356     {
357     if (!map->tile_path [dir])
358     map = 0;
359 root 1.120 else if (map->tile_available (dir, false))
360 root 1.119 {
361     maptile *neigh = map->tile_map [dir];
362 elmex 1.1
363 root 1.119 switch (dir)
364     {
365     case 0: mapy -= neigh->height; break;
366     case 2: mapy += map ->height; break;
367     case 3: mapx -= neigh->width ; break;
368     case 1: mapx += map ->width ; break;
369     }
370 elmex 1.1
371 root 1.119 map = neigh;
372     --max_distance;
373 elmex 1.1 }
374 root 1.119 else
375     return 0;
376     }
377     else
378     max_distance = 0;
379     }
380    
381     if (!max_distance)
382     send_packet_printf ("mapinfo %s error", token);
383 root 1.120 else if (!map || !map->path)
384 root 1.119 send_packet_printf ("mapinfo %s nomap", token);
385     else
386     {
387     int flags = 0;
388    
389     if (map->tile_path[0]) flags |= 1;
390     if (map->tile_path[1]) flags |= 2;
391     if (map->tile_path[2]) flags |= 4;
392     if (map->tile_path[3]) flags |= 8;
393    
394     send_packet_printf ("mapinfo %s spatial %d %d %d %d %d %s", token, flags, mapx, mapy, map->width, map->height, &map->path);
395     }
396    
397     return 1;
398     }
399 elmex 1.1
400 root 1.119 void
401     client::mapinfo_queue_run ()
402     {
403     if (mapinfo_queue.empty () || !pl)
404     return;
405    
406     for (int i = 0; i < mapinfo_queue.size (); ++i)
407     if (mapinfo_try (mapinfo_queue [i]))
408     {
409     free (mapinfo_queue [i]);
410     mapinfo_queue.erase (i);
411     }
412     else
413     ++i;
414     }
415    
416     void
417     MapInfoCmd (char *buf, int len, player *pl)
418     {
419     // <mapinfo tag spatial tile-path
420     // >mapinfo tag spatial flags x y w h hash
421    
422     char *token = buf;
423 elmex 1.1
424 root 1.119 if (!(buf = strchr (buf, ' ')))
425     return;
426 elmex 1.1
427 root 1.119 if (!strncmp (buf, " spatial ", 9))
428     {
429     char *copy = strdup (token);
430     copy [buf - token] = 0;
431 root 1.16
432 root 1.119 #if 0
433     // this makes only sense when we flush the buffer immediately
434     if (pl->ns->mapinfo_try (copy))
435     free (copy);
436 elmex 1.1 else
437 root 1.119 #endif
438     pl->ns->mapinfo_queue.push_back (copy);
439 elmex 1.1 }
440     else
441 root 1.119 pl->ns->send_packet_printf ("mapinfo %s unsupported", token);
442 elmex 1.1 }
443    
444 root 1.83 /** This is the Setup cmd */
445 root 1.16 void
446 root 1.41 SetUp (char *buf, int len, client * ns)
447 elmex 1.1 {
448 root 1.83 INVOKE_CLIENT (SETUP, ns, ARG_DATA (buf, len));
449 elmex 1.1 }
450    
451     /**
452     * The client has requested to be added to the game.
453     * This is what takes care of it. We tell the client how things worked out.
454     * I am not sure if this file is the best place for this function. however,
455     * it either has to be here or init_sockets needs to be exported.
456     */
457 root 1.16 void
458 root 1.77 AddMeCmd (char *buf, int len, client *ns)
459 elmex 1.1 {
460 root 1.51 INVOKE_CLIENT (ADDME, ns, ARG_DATA (buf, len));
461 elmex 1.1 }
462    
463 root 1.155 //+GPL
464    
465 elmex 1.1 /**
466     * This handles the general commands from the client (ie, north, fire, cast,
467     * etc.)
468     */
469 root 1.16 void
470     PlayerCmd (char *buf, int len, player *pl)
471 elmex 1.1 {
472 root 1.16 /* Check if there is a count. In theory, a zero count could also be
473     * sent, so check for that also.
474     */
475     if (atoi (buf) || buf[0] == '0')
476     {
477     pl->count = atoi ((char *) buf);
478 root 1.44
479 root 1.16 buf = strchr (buf, ' '); /* advance beyond the numbers */
480     if (!buf)
481 root 1.44 return;
482    
483 root 1.16 buf++;
484 elmex 1.1 }
485 root 1.44
486 root 1.151 execute_newserver_command (pl->ob, (char *)buf);
487 root 1.44
488 root 1.16 /* Perhaps something better should be done with a left over count.
489     * Cleaning up the input should probably be done first - all actions
490     * for the command that issued the count should be done before any other
491     * commands.
492     */
493     pl->count = 0;
494 elmex 1.1 }
495    
496     /**
497     * This handles the general commands from the client (ie, north, fire, cast,
498     * etc.). It is a lot like PlayerCmd above, but is called with the
499     * 'ncom' method which gives more information back to the client so it
500     * can throttle.
501     */
502 root 1.16 void
503 root 1.39 NewPlayerCmd (char *buf, int len, player *pl)
504 elmex 1.1 {
505 root 1.44 if (len <= 6)
506 root 1.16 {
507 root 1.113 LOG (llevDebug, "%s: corrupt ncom command <%s>: not long enough (%d) - discarding\n", pl->ns->host, buf, len);
508 root 1.16 return;
509     }
510    
511 root 1.44 uint16 cmdid = net_uint16 ((uint8 *)buf);
512     sint32 repeat = net_sint32 ((uint8 *)buf + 2);
513 root 1.30
514 root 1.16 /* -1 is special - no repeat, but don't update */
515     if (repeat != -1)
516 root 1.28 pl->count = repeat;
517    
518 root 1.44 buf += 6; //len -= 6;
519 root 1.16
520 root 1.44 execute_newserver_command (pl->ob, buf);
521 root 1.28
522 root 1.16 /* Perhaps something better should be done with a left over count.
523     * Cleaning up the input should probably be done first - all actions
524     * for the command that issued the count should be done before any other
525     * commands.
526     */
527     pl->count = 0;
528    
529 root 1.44 //TODO: schmorp thinks whatever this calculates, it makes no sense at all
530 root 1.159 int time = pl->ob->has_active_speed ()
531 root 1.161 ? (int) (MAX_TIME / pl->ob->speed)
532 root 1.159 : MAX_TIME * 100;
533 root 1.27
534 root 1.28 /* Send confirmation of command execution now */
535 root 1.47 packet sl ("comc");
536     sl << uint16 (cmdid) << uint32 (time);
537 root 1.50 pl->ns->send_packet (sl);
538 elmex 1.1 }
539    
540     /** This is a reply to a previous query. */
541 root 1.16 void
542 root 1.49 ReplyCmd (char *buf, int len, client *ns)
543 elmex 1.1 {
544 root 1.50 if (ns->state == ST_CUSTOM)
545     {
546     INVOKE_CLIENT (REPLY, ns, ARG_DATA (buf, len));
547     return;
548     }
549    
550 root 1.49 if (!ns->pl)
551     return; //TODO: depends on the exact reply we are after
552     //TODO: but right now, we always have a ns->pl
553    
554     player *pl = ns->pl;
555    
556 root 1.16 /* This is to synthesize how the data would be stored if it
557 root 1.44 * was normally entered. A bit of a hack, and should be cleaned up
558 root 1.16 * once all the X11 code is removed from the server.
559     *
560     * We pass 13 to many of the functions because this way they
561     * think it was the carriage return that was entered, and the
562     * function then does not try to do additional input.
563     */
564     snprintf (pl->write_buf, sizeof (pl->write_buf), ":%s", buf);
565    
566     /* this avoids any hacking here */
567    
568 root 1.50 switch (ns->state)
569 root 1.16 {
570 root 1.44 case ST_PLAYING:
571     LOG (llevError, "Got reply message with ST_PLAYING input state\n");
572     break;
573    
574     case ST_GET_PARTY_PASSWORD: /* Get password for party */
575     receive_party_password (pl->ob, 13);
576     break;
577 elmex 1.1
578 root 1.44 default:
579 root 1.50 LOG (llevError, "Unknown input state: %d\n", ns->state);
580 elmex 1.1 }
581     }
582    
583     /**
584 root 1.169 * Client tells its version info.
585 elmex 1.1 */
586 root 1.16 void
587 root 1.169 VersionCmd (char *buf, int len, client *ns)
588 elmex 1.1 {
589 root 1.169 INVOKE_CLIENT (VERSION, ns, ARG_DATA (buf, len));
590 elmex 1.1 }
591    
592     /** sound related functions. */
593 root 1.16 void
594 root 1.41 SetSound (char *buf, int len, client * ns)
595 elmex 1.1 {
596 root 1.16 ns->sound = atoi (buf);
597 elmex 1.1 }
598    
599     /** client wants the map resent */
600 root 1.16 void
601     MapRedrawCmd (char *buf, int len, player *pl)
602 elmex 1.1 {
603     /* This function is currently disabled; just clearing the map state results in
604     * display errors. It should clear the cache and send a newmap command.
605     * Unfortunately this solution does not work because some client versions send
606     * a mapredraw command after receiving a newmap command.
607     */
608     }
609    
610     /**
611     * Moves an object (typically, container to inventory).
612     * syntax is: move (to) (tag) (nrof)
613     */
614 root 1.16 void
615     MoveCmd (char *buf, int len, player *pl)
616 elmex 1.1 {
617 root 1.129 int to, tag, nrof;
618 elmex 1.1
619 root 1.129 if (3 != sscanf (buf, "%d %d %d", &to, &tag, &nrof))
620 root 1.16 {
621 root 1.129 LOG (llevError, "Incomplete move command: %s\n", buf);
622     return;
623 elmex 1.1 }
624 root 1.44
625 root 1.129 esrv_move_object (pl->ob, to, tag, nrof);
626 elmex 1.1 }
627    
628     /******************************************************************************
629     *
630     * Start of commands the server sends to the client.
631     *
632     ******************************************************************************/
633    
634     /**
635     * Asks the client to query the user. This way, the client knows
636     * it needs to send something back (vs just printing out a message)
637     */
638 root 1.16 void
639 root 1.82 send_query (client *ns, uint8 flags, const char *text)
640 elmex 1.1 {
641 root 1.44 ns->send_packet_printf ("query %d %s", flags, text ? text : "");
642 elmex 1.1 }
643    
644 root 1.91 /**
645     * Get player's current range attack in obuf.
646     */
647     static void
648 root 1.95 rangetostring (player *pl, char *obuf)
649 root 1.91 {
650 root 1.149 dynbuf_text &buf = msg_dynbuf; buf.clear ();
651 root 1.91
652 root 1.167 #if 0
653     // print ranged/chosen_skill etc. objects every call
654     printf ("%s %s => %s (%s)\n",
655 root 1.166 pl->ranged_ob ? &pl->ranged_ob->name : "-",
656     pl->combat_ob ? &pl->combat_ob->name : "-",
657     pl->ob->current_weapon ? &pl->ob->current_weapon->name : "-",
658     pl->ob->chosen_skill ? &pl->ob->chosen_skill->name : "-"
659     );
660 root 1.167 #endif
661 root 1.166
662 root 1.93 if (pl->ranged_ob)
663 root 1.96 buf << " Range" << (pl->ob->current_weapon == pl->ranged_ob ? "*" : "") << ": " << pl->ranged_ob->name;
664 root 1.93
665     if (pl->combat_ob)
666 root 1.96 buf << " Combat" << (pl->ob->current_weapon == pl->combat_ob ? "*" : "") << ": " << pl->combat_ob->name;
667 root 1.91
668 root 1.104 #if 0
669 root 1.99 //TODO: remove this when slot system is working, this is only for debugging
670     if (pl->ob->chosen_skill)
671     buf << " Skill*: " << pl->ob->chosen_skill->name;
672 root 1.104 #endif
673 root 1.99
674 root 1.96 //TODO: maybe golem should become the current_weapon, quite simply?
675 root 1.91 if (pl->golem)
676 root 1.96 buf << " Golem*: " << pl->golem->name;
677 root 1.91
678 root 1.109 buf << '\0';
679 root 1.94 buf.linearise (obuf);
680 root 1.91 }
681    
682 elmex 1.1 #define AddIfInt64(Old,New,Type) if (Old != New) {\
683 root 1.12 Old = New; \
684 root 1.28 sl << uint8 (Type) << uint64 (New); \
685 root 1.12 }
686 elmex 1.1
687     #define AddIfInt(Old,New,Type) if (Old != New) {\
688 root 1.12 Old = New; \
689 root 1.28 sl << uint8 (Type) << uint32 (New); \
690 root 1.12 }
691 elmex 1.1
692     #define AddIfShort(Old,New,Type) if (Old != New) {\
693 root 1.12 Old = New; \
694 root 1.28 sl << uint8 (Type) << uint16 (New); \
695 root 1.12 }
696 elmex 1.1
697 root 1.98 #define AddIfFloat(Old,New,Type,mult) if (Old != New) {\
698 root 1.12 Old = New; \
699 root 1.98 sl << uint8 (Type) << uint32 (New*FLOAT_MULTI*mult); \
700 root 1.12 }
701 elmex 1.1
702     #define AddIfString(Old,New,Type) if (Old == NULL || strcmp(Old,New)) {\
703 root 1.28 free(Old); Old = strdup (New);\
704     sl << uint8 (Type) << data8 (New); \
705 root 1.12 }
706 elmex 1.1
707     /**
708     * Sends a statistics update. We look at the old values,
709     * and only send what has changed. Stat mapping values are in newclient.h
710     * Since this gets sent a lot, this is actually one of the few binary
711     * commands for now.
712     */
713 root 1.16 void
714     esrv_update_stats (player *pl)
715 elmex 1.1 {
716 root 1.16 char buf[MAX_BUF];
717     uint16 flags;
718    
719 root 1.61 client *ns = pl->ns;
720     if (!ns)
721     return;
722    
723 root 1.101 object *ob = pl->observe;
724 root 1.61 if (!ob)
725 root 1.53 return;
726    
727 root 1.106 player *opl = ob->contr ? static_cast<player *>(ob->contr) : pl;
728    
729 root 1.47 packet sl ("stats");
730 elmex 1.1
731 root 1.61 AddIfShort (ns->last_stats.hp, ob->stats.hp, CS_STAT_HP);
732     AddIfShort (ns->last_stats.maxhp, ob->stats.maxhp, CS_STAT_MAXHP);
733     AddIfShort (ns->last_stats.sp, ob->stats.sp, CS_STAT_SP);
734     AddIfShort (ns->last_stats.maxsp, ob->stats.maxsp, CS_STAT_MAXSP);
735     AddIfShort (ns->last_stats.grace, ob->stats.grace, CS_STAT_GRACE);
736     AddIfShort (ns->last_stats.maxgrace, ob->stats.maxgrace, CS_STAT_MAXGRACE);
737     AddIfShort (ns->last_stats.Str, ob->stats.Str, CS_STAT_STR);
738     AddIfShort (ns->last_stats.Dex, ob->stats.Dex, CS_STAT_DEX);
739     AddIfShort (ns->last_stats.Con, ob->stats.Con, CS_STAT_CON);
740     AddIfShort (ns->last_stats.Int, ob->stats.Int, CS_STAT_INT);
741     AddIfShort (ns->last_stats.Wis, ob->stats.Wis, CS_STAT_WIS);
742     AddIfShort (ns->last_stats.Pow, ob->stats.Pow, CS_STAT_POW);
743     AddIfShort (ns->last_stats.Cha, ob->stats.Cha, CS_STAT_CHA);
744 root 1.22
745 root 1.172 for (int s = 0; s < CS_NUM_SKILLS; s++)
746 root 1.138 if (object *skill = opl->last_skill_ob [s])
747 root 1.61 if (skill->stats.exp != ns->last_skill_exp [s])
748     {
749     ns->last_skill_exp [s] = skill->stats.exp;
750    
751     /* Always send along the level if exp changes. This is only
752     * 1 extra byte, but keeps processing simpler.
753     */
754 root 1.172 sl << uint8 (CS_STAT_SKILLINFO + s)
755 root 1.61 << uint8 (skill->level)
756     << uint64 (skill->stats.exp);
757     }
758 root 1.28
759 root 1.61 AddIfInt64 (ns->last_stats.exp, ob->stats.exp, CS_STAT_EXP64);
760     AddIfShort (ns->last_level, ob->level, CS_STAT_LEVEL);
761     AddIfShort (ns->last_stats.wc, ob->stats.wc, CS_STAT_WC);
762     AddIfShort (ns->last_stats.ac, ob->stats.ac, CS_STAT_AC);
763     AddIfShort (ns->last_stats.dam, ob->stats.dam, CS_STAT_DAM);
764 root 1.159 AddIfFloat (ns->last_speed, ob->speed, CS_STAT_SPEED, 1.f / TICK);
765 root 1.61 AddIfShort (ns->last_stats.food, ob->stats.food, CS_STAT_FOOD);
766 root 1.159 AddIfFloat (ns->last_weapon_sp, pl->weapon_sp, CS_STAT_WEAP_SP, 1.f / TICK);
767 root 1.61 AddIfInt (ns->last_weight_limit, weight_limit[ob->stats.Str], CS_STAT_WEIGHT_LIM);
768 root 1.22
769 root 1.172 flags = (opl->fire_on ? SF_FIREON : 0)
770     | (opl->run_on ? SF_RUNON : 0);
771 root 1.16
772 root 1.61 AddIfShort (ns->last_flags, flags, CS_STAT_FLAGS);
773 root 1.22
774 root 1.160 for (int i = 0; i < NROFATTACKS; i++)
775 root 1.173 /* Skip ones we won't send */
776     if (atnr_cs_stat[i] >= 0)
777 root 1.160 AddIfShort (ns->last_resist[i], ob->resist[i], atnr_cs_stat[i]);
778 root 1.22
779 root 1.50 if (pl->ns->monitor_spells)
780 root 1.16 {
781 root 1.61 AddIfInt (ns->last_path_attuned, ob->path_attuned, CS_STAT_SPELL_ATTUNE);
782     AddIfInt (ns->last_path_repelled, ob->path_repelled, CS_STAT_SPELL_REPEL);
783     AddIfInt (ns->last_path_denied, ob->path_denied, CS_STAT_SPELL_DENY);
784 root 1.16 }
785 root 1.22
786 root 1.106 rangetostring (opl, buf); /* we want use the new fire & run system in new client */
787 root 1.61 AddIfString (ns->stats.range, buf, CS_STAT_RANGE);
788     set_title (ob, buf);
789     AddIfString (ns->stats.title, buf, CS_STAT_TITLE);
790 root 1.16
791     /* Only send it away if we have some actual data */
792 root 1.34 if (sl.length () > 6)
793 root 1.61 ns->send_packet (sl);
794 elmex 1.1 }
795    
796     /**
797     * Tells the client that here is a player it should start using.
798     */
799 root 1.16 void
800 root 1.126 esrv_new_player (player *pl)
801 elmex 1.1 {
802 root 1.126 sint32 weight = pl->ob->client_weight ();
803    
804 root 1.47 packet sl ("player");
805 elmex 1.1
806 root 1.28 sl << uint32 (pl->ob->count)
807     << uint32 (weight)
808 root 1.69 << uint32 (pl->ob->face)
809 root 1.28 << data8 (pl->ob->name);
810 root 1.16
811 root 1.61 pl->ns->last_weight = weight;
812 root 1.50 pl->ns->send_packet (sl);
813 elmex 1.1 }
814    
815     /******************************************************************************
816     *
817     * Start of map related commands.
818     *
819     ******************************************************************************/
820    
821     /** Clears a map cell */
822 root 1.16 static void
823 root 1.70 map_clearcell (struct MapCell *cell, int count)
824 elmex 1.1 {
825 root 1.75 cell->faces[0] = 0;
826     cell->faces[1] = 0;
827     cell->faces[2] = 0;
828     cell->smooth[0] = 0;
829     cell->smooth[1] = 0;
830     cell->smooth[2] = 0;
831     cell->count = count;
832     cell->stat_hp = 0;
833     cell->flags = 0;
834     cell->player = 0;
835 elmex 1.1 }
836    
837     #define MAX_LAYERS 3
838    
839     /**
840     * Removes the need to replicate the same code for each layer.
841     * this returns true if this space is now in fact different than
842     * it was.
843     * sl is the socklist this data is going into.
844     * ns is the socket we are working on - all the info we care
845     * about is in this socket structure, so now need not pass the
846     * entire player object.
847     * layer is the layer to update, with 2 being the floor and 0 the
848     * top layer (this matches what the GET_MAP_FACE and GET_MAP_FACE_OBJ)
849     * take. Interesting to note that before this function, the map1 function
850     * numbers the spaces differently - I think this was a leftover from
851     * the map command, where the faces stack up. Sinces that is no longer
852     * the case, it seems to make more sense to have these layer values
853     * actually match.
854     */
855 root 1.16 static int
856 root 1.75 update_space (packet &sl, client &ns, mapspace &ms, MapCell &lastcell, int layer)
857 elmex 1.1 {
858 root 1.75 object *ob = ms.faces_obj [layer];
859 root 1.16
860     /* If there is no object for this space, or if the face for the object
861     * is the blank face, set the face number to zero.
862     * else if we have the stored head object for this space, that takes
863     * precedence over the other object for this space.
864     * otherwise, we do special head processing
865     */
866 root 1.75 uint16 face_num = ob && ob->face != blank_face ? ob->face : 0;
867 root 1.16
868     /* We've gotten what face we want to use for the object. Now see if
869     * if it has changed since we last sent it to the client.
870     */
871 root 1.75 if (lastcell.faces[layer] != face_num)
872 root 1.16 {
873 root 1.75 lastcell.faces[layer] = face_num;
874 root 1.73
875 root 1.74 if (!ns.faces_sent[face_num])
876 root 1.73 if (ob)
877     ns.send_faces (ob);
878     else
879 root 1.116 ns.send_face (face_num, 10);
880 root 1.28
881     sl << uint16 (face_num);
882 root 1.16 return 1;
883 elmex 1.1 }
884 root 1.28
885 root 1.16 /* Nothing changed */
886     return 0;
887 elmex 1.1 }
888    
889 root 1.155 //-GPL
890    
891 root 1.62 // prefetch (and touch) all maps within a specific distancd
892     static void
893     prefetch_surrounding_maps (maptile *map, int distance)
894     {
895 root 1.147 map->touch ();
896 root 1.62
897     if (--distance)
898 root 1.162 for (int dir = 4; dir--; )
899 root 1.62 if (const shstr &path = map->tile_path [dir])
900     if (maptile *&neigh = map->tile_map [dir])
901     prefetch_surrounding_maps (neigh, distance);
902     else
903     neigh = maptile::find_async (path, map);
904     }
905    
906     // prefetch a generous area around the player
907     static void
908     prefetch_surrounding_maps (object *op)
909     {
910     prefetch_surrounding_maps (op->map, 3);
911     }
912    
913 root 1.155 //+GPL
914    
915 elmex 1.1 /**
916 root 1.101 * Draws client map.
917 elmex 1.1 */
918 root 1.16 void
919 root 1.101 draw_client_map (player *pl)
920 elmex 1.1 {
921 root 1.154 object *ob = pl->viewpoint;
922     if (!pl->observe->active)
923 root 1.101 return;
924    
925     /* If player is just joining the game, he isn't here yet, so the map
926     * can get swapped out. If so, don't try to send them a map. All will
927     * be OK once they really log in.
928     */
929 root 1.141 if (!ob->map || ob->map->in_memory != MAP_ACTIVE)
930 root 1.101 return;
931    
932 root 1.141 int startlen, oldlen;
933 root 1.16
934 root 1.101 check_map_change (pl);
935     prefetch_surrounding_maps (pl->ob);
936 root 1.16
937 root 1.101 /* do LOS after calls to update_position */
938 root 1.143 /* unfortunately, we need to udpate los when observing, currently */
939 root 1.154 if (pl->do_los || pl->viewpoint != pl->ob)
940 root 1.101 {
941     pl->do_los = 0;
942 root 1.143 pl->update_los ();
943 root 1.101 }
944    
945     /**
946     * This function uses the new map1 protocol command to send the map
947     * to the client. It is necessary because the old map command supports
948     * a maximum map size of 15x15.
949     * This function is much simpler than the old one. This is because
950     * the old function optimized to send as few face identifiers as possible,
951     * at the expense of sending more coordinate location (coordinates were
952     * only 1 byte, faces 2 bytes, so this was a worthwhile savings). Since
953     * we need 2 bytes for coordinates and 2 bytes for faces, such a trade off
954     * maps no sense. Instead, we actually really only use 12 bits for coordinates,
955     * and use the other 4 bits for other informatiion. For full documentation
956     * of what we send, see the doc/Protocol file.
957     * I will describe internally what we do:
958     * the ns->lastmap shows how the map last looked when sent to the client.
959     * in the lastmap structure, there is a cells array, which is set to the
960     * maximum viewable size (As set in config.h).
961     * in the cells, there are faces and a count value.
962     * we use the count value to hold the darkness value. If -1, then this space
963     * is not viewable.
964     * we use faces[0] faces[1] faces[2] to hold what the three layers
965     * look like.
966     */
967 root 1.58
968 root 1.101 client &socket = *pl->ns;
969 root 1.16
970 root 1.47 packet sl (socket.mapmode == Map1Cmd ? "map1" : "map1a");
971 root 1.28
972 root 1.34 startlen = sl.length ();
973 root 1.25
974 root 1.141 int hx = socket.mapx / 2;
975     int hy = socket.mapy / 2;
976 root 1.76
977 root 1.144 ordered_mapwalk_begin (ob, -hx, -hy, hx, hy)
978     int ax = dx + hx;
979     int ay = dy + hy;
980    
981 root 1.145 int mask = (ax << 10) | (ay << 4);
982 root 1.144 MapCell &lastcell = socket.lastmap.cells[ax][ay];
983    
984     /* If the coordinates are not valid, or it is too dark to see,
985     * we tell the client as such
986     */
987     if (!m)
988     {
989     /* space is out of map. Update space and clear values
990     * if this hasn't already been done. If the space is out
991     * of the map, it shouldn't have a head.
992     */
993     if (lastcell.count != -1)
994     {
995     sl << uint16 (mask);
996     map_clearcell (&lastcell, -1);
997     }
998    
999     continue;
1000     }
1001 root 1.162
1002 root 1.144 int d = pl->blocked_los_uc (dx, dy);
1003    
1004     if (d > 3)
1005     {
1006     /* This block deals with spaces that are not visible for whatever
1007     * reason. Still may need to send the head for this space.
1008     */
1009     if (lastcell.count != -1
1010     || lastcell.faces[0]
1011     || lastcell.faces[1]
1012     || lastcell.faces[2]
1013     || lastcell.stat_hp
1014     || lastcell.flags
1015     || lastcell.player)
1016     sl << uint16 (mask);
1017    
1018     /* properly clear a previously sent big face */
1019     map_clearcell (&lastcell, -1);
1020     }
1021     else
1022     {
1023     /* In this block, the space is visible.
1024     */
1025    
1026     /* Rather than try to figure out what everything that we might
1027     * need to send is, then form the packet after that,
1028     * we presume that we will in fact form a packet, and update
1029     * the bits by what we do actually send. If we send nothing,
1030     * we just back out sl.length () to the old value, and no harm
1031     * is done.
1032     * I think this is simpler than doing a bunch of checks to see
1033     * what if anything we need to send, setting the bits, then
1034     * doing those checks again to add the real data.
1035     */
1036     oldlen = sl.length ();
1037    
1038     sl << uint16 (mask);
1039    
1040     unsigned char dummy;
1041     unsigned char *last_ext = &dummy;
1042    
1043     /* Darkness changed */
1044 root 1.146 if (lastcell.count != d)
1045 root 1.144 {
1046     mask |= 0x8;
1047    
1048 root 1.148 *last_ext |= 0x80;
1049     last_ext = &sl[sl.length ()];
1050     sl << uint8 (d);
1051 root 1.144 }
1052    
1053     lastcell.count = d;
1054    
1055     mapspace &ms = m->at (nx, ny);
1056     ms.update ();
1057    
1058 root 1.148 // extmap handling
1059     uint8 stat_hp = 0;
1060     uint8 stat_width = 0;
1061     uint8 flags = 0;
1062     tag_t player = 0;
1063    
1064     // send hp information, if applicable
1065     if (object *op = ms.faces_obj [0])
1066     if (op->is_head () && !op->invisible)
1067     {
1068     if (op->stats.maxhp > op->stats.hp
1069     && op->stats.maxhp > 0
1070     && (op->type == PLAYER
1071     || op->type == DOOR // does not work, have maxhp 0
1072 root 1.171 || op->flag [FLAG_MONSTER]
1073     || op->flag [FLAG_ALIVE]
1074     || op->flag [FLAG_GENERATOR]))
1075 root 1.16 {
1076 root 1.148 stat_hp = 255 - (op->stats.hp * 255 + 254) / op->stats.maxhp;
1077     stat_width = op->arch->max_x - op->arch->x; //TODO: should be upper-left edge
1078 root 1.16 }
1079 root 1.12
1080 root 1.148 if (expect_false (op->has_dialogue ()))
1081     flags |= 1;
1082 root 1.144
1083 root 1.148 if (expect_false (op->type == PLAYER))
1084     player = op == ob ? pl->ob->count
1085     : op == pl->ob ? ob->count
1086     : op->count;
1087     }
1088 root 1.144
1089 root 1.148 if (expect_false (lastcell.stat_hp != stat_hp))
1090     {
1091     lastcell.stat_hp = stat_hp;
1092 root 1.144
1093 root 1.148 mask |= 0x8;
1094     *last_ext |= 0x80;
1095     last_ext = &sl[sl.length ()];
1096 root 1.144
1097 root 1.148 sl << uint8 (5) << uint8 (stat_hp);
1098 root 1.144
1099 root 1.148 if (stat_width > 1)
1100 root 1.144 {
1101     *last_ext |= 0x80;
1102     last_ext = &sl[sl.length ()];
1103    
1104 root 1.148 sl << uint8 (6) << uint8 (stat_width);
1105 root 1.144 }
1106 root 1.148 }
1107    
1108     if (expect_false (lastcell.player != player))
1109     {
1110     lastcell.player = player;
1111    
1112     mask |= 0x8;
1113     *last_ext |= 0x80;
1114     last_ext = &sl[sl.length ()];
1115    
1116     sl << uint8 (0x47) << uint8 (4) << (uint32)player;
1117     }
1118 root 1.144
1119 root 1.148 if (expect_false (lastcell.flags != flags))
1120     {
1121     lastcell.flags = flags;
1122 root 1.144
1123 root 1.148 mask |= 0x8;
1124     *last_ext |= 0x80;
1125     last_ext = &sl[sl.length ()];
1126 root 1.144
1127 root 1.148 sl << uint8 (8) << uint8 (flags);
1128 root 1.144 }
1129    
1130 root 1.148 // faces
1131    
1132 root 1.144 /* Floor face */
1133     if (update_space (sl, socket, ms, lastcell, 2))
1134     mask |= 0x4;
1135    
1136     /* Middle face */
1137     if (update_space (sl, socket, ms, lastcell, 1))
1138     mask |= 0x2;
1139    
1140     if (expect_false (ob->invisible)
1141     && ob->invisible & (ob->invisible < 50 ? 1 : 7)
1142     && ms.player () == ob)
1143     {
1144     // force player to be visible to himself if invisible
1145     if (lastcell.faces[0] != ob->face)
1146     {
1147     lastcell.faces[0] = ob->face;
1148 root 1.25
1149 root 1.75 mask |= 0x1;
1150 root 1.144 sl << uint16 (ob->face);
1151 root 1.25
1152 root 1.144 socket.send_faces (ob);
1153     }
1154     }
1155     /* Top face */
1156     else if (update_space (sl, socket, ms, lastcell, 0))
1157     mask |= 0x1;
1158    
1159     /* Check to see if we are in fact sending anything for this
1160     * space by checking the mask. If so, update the mask.
1161     * if not, reset the len to that from before adding the mask
1162     * value, so we don't send those bits.
1163     */
1164     if (mask & 0xf)
1165     sl[oldlen + 1] = mask & 0xff;
1166     else
1167     sl.reset (oldlen);
1168     } /* else this is a viewable space */
1169     ordered_mapwalk_end
1170 root 1.16
1171 root 1.84 socket.flush_fx ();
1172    
1173 root 1.34 if (sl.length () > startlen || socket.sent_scroll)
1174 root 1.16 {
1175 root 1.37 socket.send_packet (sl);
1176 root 1.16 socket.sent_scroll = 0;
1177 elmex 1.1 }
1178     }
1179    
1180     /**
1181     * This looks for any spells the player may have that have changed their stats.
1182     * it then sends an updspell packet for each spell that has changed in this way
1183     */
1184 root 1.16 void
1185     esrv_update_spells (player *pl)
1186     {
1187 root 1.53 if (!pl->ns)
1188     return;
1189    
1190 root 1.160 pl->ns->update_spells = false;
1191    
1192 root 1.50 if (!pl->ns->monitor_spells)
1193 root 1.16 return;
1194 root 1.28
1195     for (object *spell = pl->ob->inv; spell; spell = spell->below)
1196 root 1.163 if (spell->type == SPELL)
1197     {
1198     int flags = 0;
1199     int val;
1200    
1201     /* check if we need to update it */
1202     val = SP_level_spellpoint_cost (pl->ob, spell, SPELL_MANA);
1203     if (spell->cached_sp != val)
1204     {
1205     spell->cached_sp = val;
1206     flags |= UPD_SP_MANA;
1207     }
1208    
1209     val = SP_level_spellpoint_cost (pl->ob, spell, SPELL_GRACE);
1210     if (spell->cached_grace != val)
1211     {
1212     spell->cached_grace = val;
1213     flags |= UPD_SP_GRACE;
1214     }
1215    
1216     val = casting_level (pl->ob, spell);
1217     if (spell->cached_eat != val)
1218     {
1219     spell->cached_eat = val;
1220     flags |= UPD_SP_LEVEL;
1221     }
1222    
1223     if (flags)
1224     {
1225     packet sl;
1226    
1227     sl << "updspell "
1228     << uint8 (flags)
1229     << uint32 (spell->count);
1230    
1231     if (flags & UPD_SP_MANA ) sl << uint16 (spell->cached_sp);
1232     if (flags & UPD_SP_GRACE) sl << uint16 (spell->cached_grace);
1233     if (flags & UPD_SP_LEVEL) sl << uint16 (spell->cached_eat);
1234    
1235     pl->ns->send_packet (sl);
1236     }
1237     }
1238 elmex 1.1 }
1239    
1240 root 1.16 void
1241     esrv_remove_spell (player *pl, object *spell)
1242     {
1243 root 1.50 if (!pl->ns->monitor_spells)
1244 root 1.16 return;
1245 root 1.28
1246 root 1.16 if (!pl || !spell || spell->env != pl->ob)
1247     {
1248     LOG (llevError, "Invalid call to esrv_remove_spell");
1249     return;
1250     }
1251 root 1.27
1252 root 1.47 packet sl ("delspell");
1253 root 1.28
1254 root 1.47 sl << uint32 (spell->count);
1255 root 1.27
1256 root 1.50 pl->ns->send_packet (sl);
1257 elmex 1.1 }
1258    
1259     /* appends the spell *spell to the Socklist we will send the data to. */
1260 root 1.16 static void
1261 root 1.33 append_spell (player *pl, packet &sl, object *spell)
1262 root 1.16 {
1263 pippijn 1.67 int i, skill = 0;
1264 root 1.16
1265     if (!(spell->name))
1266     {
1267     LOG (llevError, "item number %d is a spell with no name.\n", spell->count);
1268     return;
1269     }
1270 root 1.27
1271 root 1.16 /* store costs and damage in the object struct, to compare to later */
1272 elmex 1.132 spell->cached_sp = SP_level_spellpoint_cost (pl->ob, spell, SPELL_MANA);
1273     spell->cached_grace = SP_level_spellpoint_cost (pl->ob, spell, SPELL_GRACE);
1274 root 1.163 spell->cached_eat = casting_level (pl->ob, spell);
1275 elmex 1.1
1276 root 1.16 /* figure out which skill it uses, if it uses one */
1277     if (spell->skill)
1278 root 1.139 if (object *tmp = pl->find_skill (spell->skill))
1279 root 1.172 skill = CS_STAT_SKILLINFO + SKILL_INDEX (tmp);
1280 elmex 1.1
1281 root 1.69 // spells better have a face
1282     if (!spell->face)
1283     {
1284     LOG (llevError, "%s: spell has no face, but face is mandatory.\n", &spell->name);
1285     spell->face = face_find ("burnout.x11", blank_face);
1286     }
1287    
1288 root 1.117 pl->ns->send_face (spell->face);
1289    
1290 root 1.27 /* send the current values */
1291     sl << uint32 (spell->count)
1292     << uint16 (spell->level)
1293     << uint16 (spell->casting_time)
1294 elmex 1.132 << uint16 (spell->cached_sp)
1295     << uint16 (spell->cached_grace)
1296     << uint16 (spell->cached_eat)
1297 root 1.27 << uint8 (skill)
1298     << uint32 (spell->path_attuned)
1299 root 1.69 << uint32 (spell->face)
1300 root 1.27 << data8 (spell->name)
1301     << data16 (spell->msg);
1302 elmex 1.1 }
1303    
1304     /**
1305     * This tells the client to add the spell *ob, if *ob is NULL, then add
1306     * all spells in the player's inventory.
1307     */
1308 root 1.16 void
1309     esrv_add_spells (player *pl, object *spell)
1310     {
1311     if (!pl)
1312     {
1313     LOG (llevError, "esrv_add_spells, tried to add a spell to a NULL player");
1314     return;
1315     }
1316 root 1.28
1317 root 1.50 if (!pl->ns->monitor_spells)
1318 root 1.16 return;
1319 root 1.28
1320 root 1.47 packet sl ("addspell");
1321 root 1.28
1322 root 1.16 if (!spell)
1323     {
1324 root 1.69 for (spell = pl->ob->inv; spell; spell = spell->below)
1325 root 1.16 {
1326     /* were we to simply keep appending data here, we could exceed
1327     * MAXSOCKBUF if the player has enough spells to add, we know that
1328     * append_spells will always append 19 data bytes, plus 4 length
1329     * bytes and 3 strings (because that is the spec) so we need to
1330     * check that the length of those 3 strings, plus the 23 bytes,
1331     * won't take us over the length limit for the socket, if it does,
1332     * we need to send what we already have, and restart packet formation
1333     */
1334 root 1.117 if (spell->type != SPELL)
1335     continue;
1336    
1337 root 1.16 /* Seeing crashes by overflowed buffers. Quick arithemetic seems
1338     * to show add_spell is 26 bytes + 2 strings. However, the overun
1339     * is hundreds of bytes off, so correcting 22 vs 26 doesn't seem
1340     * like it will fix this
1341     */
1342 root 1.118 if (sl.length () > (MAXSOCKBUF - (26 + strlen (spell->name) + (spell->msg ? strlen (spell->msg) : 0))))
1343 root 1.16 {
1344 root 1.117 pl->ns->flush_fx ();
1345 root 1.50 pl->ns->send_packet (sl);
1346 root 1.34
1347     sl.reset ();
1348     sl << "addspell ";
1349 root 1.16 }
1350 root 1.27
1351     append_spell (pl, sl, spell);
1352 root 1.16 }
1353     }
1354     else if (spell->type != SPELL)
1355     {
1356     LOG (llevError, "Asked to send a non-spell object as a spell");
1357     return;
1358     }
1359     else
1360 root 1.27 append_spell (pl, sl, spell);
1361    
1362 root 1.118 if (sl.length () > MAXSOCKBUF)
1363 root 1.16 {
1364     LOG (llevError, "Buffer overflow in esrv_add_spells!\n");
1365     fatal (0);
1366     }
1367 root 1.27
1368 root 1.16 /* finally, we can send the packet */
1369 root 1.117 pl->ns->flush_fx ();
1370 root 1.50 pl->ns->send_packet (sl);
1371 elmex 1.1 }
1372 root 1.28
1373 root 1.155 //-GPL
1374