ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/socket/request.C
Revision: 1.176
Committed: Thu Apr 29 15:59:10 2010 UTC (14 years, 1 month ago) by root
Content type: text/plain
Branch: MAIN
CVS Tags: rel-3_0
Changes since 1.175: +3 -12 lines
Log Message:
cleanup

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