ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/socket/request.C
Revision: 1.196
Committed: Sun Nov 18 00:37:11 2018 UTC (5 years, 6 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.195: +2 -2 lines
Log Message:
some range based for loops

File Contents

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