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

File Contents

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