ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/socket/request.C
Revision: 1.198
Committed: Wed Dec 5 19:03:27 2018 UTC (5 years, 5 months ago) by root
Content type: text/plain
Branch: MAIN
CVS Tags: HEAD
Changes since 1.197: +6 -6 lines
Log Message:
some bugfixes

File Contents

# Content
1 /*
2 * This file is part of Deliantra, the Roguelike Realtime MMORPG.
3 *
4 * Copyright (©) 2017,2018 Marc Alexander Lehmann / the Deliantra team
5 * Copyright (©) 2005,2006,2007,2008,2009,2010,2011,2012,2013,2014,2015,2016 Marc Alexander Lehmann / Robin Redeker / the Deliantra team
6 * Copyright (©) 2001 Mark Wedel
7 * Copyright (©) 1992 Frank Tore Johansen
8 *
9 * 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 *
14 * 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 *
19 * 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 *
23 * The authors can be reached via e-mail to <support@deliantra.net>
24 */
25
26 //+GPL
27
28 /**
29 * \file
30 * Client handling.
31 *
32 * \date 2003-12-02
33 *
34 * 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 *
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 #include <sys/types.h>
55 #include <sys/time.h>
56 #include <sys/socket.h>
57 #include <netinet/in.h>
58 #include <netdb.h>
59
60 #include <unistd.h>
61 #include <sys/time.h>
62
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 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 CS_STAT_RES_BLIND,
96 -1, /* Internal */
97 -1, /* life stealing */
98 -1 /* Disease - not fully done yet */
99 };
100
101 static void
102 socket_map_scroll (client *ns, int dx, int dy)
103 {
104 struct Map newmap;
105 int x, y, mx, my;
106
107 ns->send_packet_printf ("map_scroll %d %d", dx, dy);
108
109 /* 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
119 /* the x and y here are coordinates for the new map, i.e. if we moved
120 * (dx,dy), newmap[x][y] = oldmap[x-dx][y-dy]. For this reason,
121 * 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 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
136 memcpy (&(ns->lastmap), &newmap, sizeof (struct Map));
137
138 /* Make sure that the next "map1" command will be sent (even if it is
139 * empty).
140 */
141 ns->sent_scroll = 1;
142 }
143
144 static void
145 clear_map (player *pl)
146 {
147 pl->ns->mapinfo_queue_clear ();
148
149 memset (&pl->ns->lastmap, 0, sizeof (pl->ns->lastmap));
150
151 pl->ns->force_newmap = false;
152 pl->ns->send_packet ("newmap");
153 pl->ns->floorbox_reset ();
154 }
155
156 static void
157 send_map_info (player *pl)
158 {
159 client &socket = *pl->ns;
160 object *ob = pl->viewpoint;
161
162 if (socket.mapinfocmd)
163 {
164 if (ob->map && ob->map->path)
165 {
166 int flags = 0;
167
168 // due to historical glitches, the bit ordering and map index ordering differs
169 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 // these two are debatable
174 if (ob->map->tile_path [TILE_UP ]) flags |= 0x10;
175 if (ob->map->tile_path [TILE_DOWN ]) flags |= 0x20;
176
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 /** check for map/region change and send new map data */
187 static void
188 check_map_change (player *pl)
189 {
190 client &socket = *pl->ns;
191 object *ob = pl->viewpoint;
192
193 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 // first try to avoid a full newmap on tiled map scrolls
201 if (socket.current_map != ob->map && !socket.force_newmap)
202 {
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 send_map_info (pl);
217 }
218 }
219
220 if (socket.current_map != ob->map || socket.force_newmap)
221 {
222 clear_map (pl);
223 socket.current_map = ob->map;
224 send_map_info (pl);
225 }
226 else if (socket.current_x != ob->x || socket.current_y != ob->y)
227 {
228 int dx = ob->x - socket.current_x;
229 int dy = ob->y - socket.current_y;
230
231 socket_map_scroll (&socket, ob->x - socket.current_x, ob->y - socket.current_y);
232 socket.floorbox_reset ();
233 }
234
235 socket.current_x = ob->x;
236 socket.current_y = ob->y;
237 }
238
239 /**
240 * This sends the skill number to name mapping. We ignore
241 * the params - we always send the same info no matter what.
242 */
243 static void
244 send_skill_info (client *ns, char *params)
245 {
246 packet sl;
247 sl << "replyinfo skill_info\n";
248
249 for (int i = 0; i < skillvec.size (); ++i)
250 sl.printf ("%d:%s\n", CS_STAT_SKILLINFO + i, &skillvec [i]->name);
251
252 if (sl.length () > MAXSOCKBUF)
253 cleanup ("buffer overflow in send_skill_info!");
254
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 static void
263 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 cleanup ("buffer overflow in send_spell_paths!");
274
275 ns->send_packet (sl);
276 }
277
278 /**
279 * 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 * requestinfo is completely deprecated, use resource exti request
283 * and the fxix or http protocols.
284 */
285 void
286 RequestInfo (char *buf, int len, client *ns)
287 {
288 char *params;
289
290 /* find the first space, make it null, and update the
291 * params pointer.
292 */
293 for (params = buf; *params; params++)
294 if (*params == ' ')
295 {
296 *params++ = 0;
297 break;
298 }
299
300 if (!strcmp (buf, "skill_info"))
301 send_skill_info (ns, params);
302 else if (!strcmp (buf, "spell_paths"))
303 send_spell_paths (ns, params);
304 else
305 {
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 }
313
314 void
315 ExtCmd (char *buf, int len, player *pl)
316 {
317 INVOKE_PLAYER (EXTCMD, pl, ARG_DATA (buf, len));
318 }
319
320 void
321 ExtiCmd (char *buf, int len, client *ns)
322 {
323 INVOKE_CLIENT (EXTICMD, ns, ARG_DATA (buf, len));
324 }
325
326 //-GPL
327
328 void
329 client::mapinfo_queue_clear ()
330 {
331 for (auto &&i : mapinfo_queue)
332 free (i);
333
334 mapinfo_queue.clear ();
335 }
336
337 bool
338 client::mapinfo_try (char *buf)
339 {
340 char *token = buf;
341 buf += strlen (buf) + 9;
342
343 // initial map and its origin
344 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 int max_distance = 8; // limit maximum path length to something generous
348
349 while (*buf && map && max_distance)
350 {
351 int dir = *buf++ - '1';
352
353 // 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 // maybe we should only allow the four flat directions
357 if (IN_RANGE_EXC (dir, 0, ecb_array_length (dirmap)))
358 {
359 dir = dirmap [dir];
360
361 if (!map->tile_path [dir])
362 map = 0;
363 else if (map->tile_available (dir, false))
364 {
365 maptile *neigh = map->tile_map [dir];
366
367 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
375 map = neigh;
376 --max_distance;
377 }
378 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 else if (!map || !map->path)
388 send_packet_printf ("mapinfo %s nomap", token);
389 else
390 {
391 int flags = 0;
392
393 // due to historical glitches, the bit ordering and map index ordering differs
394 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 // these two are debatable
399 if (map->tile_path [TILE_UP ]) flags |= 0x10;
400 if (map->tile_path [TILE_DOWN ]) flags |= 0x20;
401
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
408 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
432 if (!(buf = strchr (buf, ' ')))
433 return;
434
435 if (!strncmp (buf, " spatial ", 9))
436 {
437 char *copy = strdup (token);
438 copy [buf - token] = 0;
439
440 #if 0
441 // this makes only sense when we flush the buffer immediately
442 if (pl->ns->mapinfo_try (copy))
443 free (copy);
444 else
445 #endif
446 pl->ns->mapinfo_queue.push_back (copy);
447 }
448 else
449 pl->ns->send_packet_printf ("mapinfo %s unsupported", token);
450 }
451
452 /** This is the Setup cmd */
453 void
454 SetUp (char *buf, int len, client * ns)
455 {
456 INVOKE_CLIENT (SETUP, ns, ARG_DATA (buf, len));
457 }
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 void
466 AddMeCmd (char *buf, int len, client *ns)
467 {
468 INVOKE_CLIENT (ADDME, ns, ARG_DATA (buf, len));
469 }
470
471 //+GPL
472
473 /**
474 * This handles the general commands from the client (ie, north, fire, cast,
475 * etc.)
476 */
477 void
478 PlayerCmd (char *buf, int len, player *pl)
479 {
480 /* 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
487 buf = strchr (buf, ' '); /* advance beyond the numbers */
488 if (!buf)
489 return;
490
491 buf++;
492 }
493
494 execute_newserver_command (pl->ob, (char *)buf);
495
496 /* 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 }
503
504 /** This is a reply to a previous query. */
505 void
506 ReplyCmd (char *buf, int len, client *ns)
507 {
508 if (ns->state == ST_CUSTOM)
509 {
510 INVOKE_CLIENT (REPLY, ns, ARG_DATA (buf, len));
511 return;
512 }
513
514 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 /* This is to synthesize how the data would be stored if it
521 * was normally entered. A bit of a hack, and should be cleaned up
522 * 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 switch (ns->state)
533 {
534 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
542 default:
543 LOG (llevError, "Unknown input state: %d\n", ns->state);
544 }
545 }
546
547 /**
548 * Client tells its version info.
549 */
550 void
551 VersionCmd (char *buf, int len, client *ns)
552 {
553 INVOKE_CLIENT (VERSION, ns, ARG_DATA (buf, len));
554 }
555
556 /** sound related functions. */
557 void
558 SetSound (char *buf, int len, client * ns)
559 {
560 ns->sound = atoi (buf);
561 }
562
563 /** client wants the map resent */
564 void
565 MapRedrawCmd (char *buf, int len, player *pl)
566 {
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 void
579 MoveCmd (char *buf, int len, player *pl)
580 {
581 int to, tag, nrof;
582
583 if (3 != sscanf (buf, "%d %d %d", &to, &tag, &nrof))
584 {
585 LOG (llevError, "Incomplete move command: %s\n", buf);
586 return;
587 }
588
589 esrv_move_object (pl->ob, to, tag, nrof);
590 }
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 void
603 send_query (client *ns, uint8 flags, const char *text)
604 {
605 ns->send_packet_printf ("query %d %s", flags, text ? text : "");
606 }
607
608 /**
609 * Get player's current range attack in obuf.
610 */
611 static void
612 rangetostring (player *pl, char *obuf)
613 {
614 dynbuf_text &buf = msg_dynbuf; buf.clear ();
615
616 #if 0
617 // print ranged/chosen_skill etc. objects every call
618 printf ("%s %s => %s (%s)\n",
619 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 #endif
625
626 if (pl->ranged_ob)
627 buf << " Range" << (pl->ob->current_weapon == pl->ranged_ob ? "*" : "") << ": " << pl->ranged_ob->name;
628
629 if (pl->combat_ob)
630 buf << " Combat" << (pl->ob->current_weapon == pl->combat_ob ? "*" : "") << ": " << pl->combat_ob->name;
631
632 #if 0
633 //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 #endif
637
638 //TODO: maybe golem should become the current_weapon, quite simply?
639 if (pl->golem)
640 buf << " Golem*: " << pl->golem->name;
641
642 buf << '\0';
643 buf.linearise (obuf);
644 }
645
646 #define AddIfInt64(Old,New,Type) if (Old != New) {\
647 Old = New; \
648 sl << uint8 (Type) << uint64 (New); \
649 }
650
651 #define AddIfInt(Old,New,Type) if (Old != New) {\
652 Old = New; \
653 sl << uint8 (Type) << uint32 (New); \
654 }
655
656 #define AddIfShort(Old,New,Type) if (Old != New) {\
657 Old = New; \
658 sl << uint8 (Type) << uint16 (New); \
659 }
660
661 #define AddIfFloat(Old,New,Type,mult) if (Old != New) {\
662 Old = New; \
663 sl << uint8 (Type) << uint32 (New*FLOAT_MULTI*mult); \
664 }
665
666 #define AddIfString(Old,New,Type) if (Old == NULL || strcmp(Old,New)) {\
667 free(Old); Old = strdup (New);\
668 sl << uint8 (Type) << data8 (New); \
669 }
670
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 void
678 esrv_update_stats (player *pl)
679 {
680 client *ns = pl->ns;
681 if (!ns)
682 return;
683
684 object *ob = pl->observe;
685 if (!ob)
686 return;
687
688 player *opl = ob->contr ? static_cast<player *>(ob->contr) : pl;
689
690 packet sl ("stats");
691
692 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
706 for (int s = 0; s < CS_NUM_SKILLS; s++)
707 if (object *skill = opl->last_skill_ob [s])
708 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 sl << uint8 (CS_STAT_SKILLINFO + s)
716 << uint8 (skill->level)
717 << uint64 (skill->stats.exp);
718 }
719
720 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 AddIfFloat (ns->last_speed, ob->speed, CS_STAT_SPEED, 1.f / TICK);
726 AddIfShort (ns->last_stats.food, ob->stats.food, CS_STAT_FOOD);
727 AddIfFloat (ns->last_weapon_sp, pl->weapon_sp, CS_STAT_WEAP_SP, 1.f / TICK);
728 AddIfInt (ns->last_weight_limit, weight_limit[ob->stats.Str], CS_STAT_WEIGHT_LIM);
729
730 int flags = (opl->fire_on ? SF_FIREON : 0)
731 | (opl->run_on ? SF_RUNON : 0);
732
733 AddIfShort (ns->last_flags, flags, CS_STAT_FLAGS);
734
735 for (int i = 0; i < NROFATTACKS; i++)
736 /* Skip ones we won't send */
737 if (atnr_cs_stat[i] >= 0)
738 AddIfShort (ns->last_resist[i], ob->resist[i], atnr_cs_stat[i]);
739
740 if (pl->ns->monitor_spells)
741 {
742 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 }
746
747 char buf[MAX_BUF];
748 rangetostring (opl, buf); /* we want use the new fire & run system in new client */
749 AddIfString (ns->stats.range, buf, CS_STAT_RANGE);
750 set_title (ob, buf);
751 AddIfString (ns->stats.title, buf, CS_STAT_TITLE);
752
753 /* Only send it away if we have some actual data */
754 if (sl.length () > 6)
755 ns->send_packet (sl);
756 }
757
758 /**
759 * Tells the client that here is a player it should start using.
760 */
761 void
762 esrv_new_player (player *pl)
763 {
764 sint32 weight = pl->ob->client_weight ();
765
766 packet sl ("player");
767
768 sl << uint32 (pl->ob->count)
769 << uint32 (weight)
770 << uint32 (pl->ob->face)
771 << data8 (pl->ob->name);
772
773 pl->ns->last_weight = weight;
774 pl->ns->send_packet (sl);
775 }
776
777 /******************************************************************************
778 *
779 * Start of map related commands.
780 *
781 ******************************************************************************/
782
783 /** Clears a map cell */
784 static void
785 map_clearcell (struct MapCell *cell, int count)
786 {
787 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 }
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 static int
818 update_space (packet &sl, client &ns, mapspace &ms, MapCell &lastcell, int layer)
819 {
820 object *ob = ms.faces_obj [layer];
821
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 uint16 face_num = ob && ob->face != blank_face ? ob->face : 0;
829
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 if (lastcell.faces[layer] != face_num)
834 {
835 lastcell.faces[layer] = face_num;
836
837 if (!ns.faces_sent[face_num])
838 if (ob)
839 ns.send_faces (ob);
840 else
841 ns.send_face (face_num, 10);
842
843 sl << uint16 (face_num);
844 return 1;
845 }
846
847 /* Nothing changed */
848 return 0;
849 }
850
851 /**
852 * Draws client map.
853 */
854 void
855 draw_client_map (player *pl)
856 {
857 object *ob = pl->viewpoint;
858 if (!pl->observe->active)
859 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 if (!ob->map || ob->map->state != MAP_ACTIVE)
866 return;
867
868 int startlen, oldlen;
869
870 check_map_change (pl);
871 pl->ob->prefetch_surrounding_maps ();
872
873 /* do LOS after calls to update_position */
874 /* unfortunately, we need to udpate los when observing, currently */
875 if (pl->do_los || pl->viewpoint != pl->ob)
876 {
877 pl->do_los = 0;
878 pl->update_los ();
879 }
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
904 client &socket = *pl->ns;
905
906 packet sl ("map1a");
907
908 startlen = sl.length ();
909
910 int hx = socket.mapx / 2;
911 int hy = socket.mapy / 2;
912
913 ordered_mapwalk_begin (ob, -hx, -hy, hx, hy)
914 int ax = dx + hx;
915 int ay = dy + hy;
916
917 int mask = (ax << 10) | (ay << 4);
918 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
938 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 || lastcell.faces[0]
947 || lastcell.faces[1]
948 || lastcell.faces[2]
949 || lastcell.stat_hp
950 || lastcell.flags
951 || 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 if (lastcell.count != d)
981 {
982 mask |= 0x8;
983
984 *last_ext |= 0x80;
985 last_ext = &sl[sl.length ()];
986 sl << uint8 (d);
987 }
988
989 lastcell.count = d;
990
991 mapspace &ms = m->at (nx, ny);
992 ms.update ();
993
994 // 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 || op->flag [FLAG_MONSTER]
1009 || op->flag [FLAG_ALIVE]
1010 || op->flag [FLAG_GENERATOR]))
1011 {
1012 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 }
1015
1016 if (ecb_expect_false (op->has_dialogue ()))
1017 flags |= 1;
1018
1019 if (ecb_expect_false (op->type == PLAYER))
1020 player = op == ob ? pl->ob->count
1021 : op == pl->ob ? ob->count
1022 : op->count;
1023 }
1024
1025 if (ecb_expect_false (lastcell.stat_hp != stat_hp))
1026 {
1027 lastcell.stat_hp = stat_hp;
1028
1029 mask |= 0x8;
1030 *last_ext |= 0x80;
1031 last_ext = &sl[sl.length ()];
1032
1033 sl << uint8 (5) << uint8 (stat_hp);
1034
1035 if (stat_width > 1)
1036 {
1037 *last_ext |= 0x80;
1038 last_ext = &sl[sl.length ()];
1039
1040 sl << uint8 (6) << uint8 (stat_width);
1041 }
1042 }
1043
1044 if (ecb_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
1055 if (ecb_expect_false (lastcell.flags != flags))
1056 {
1057 lastcell.flags = flags;
1058
1059 mask |= 0x8;
1060 *last_ext |= 0x80;
1061 last_ext = &sl[sl.length ()];
1062
1063 sl << uint8 (8) << uint8 (flags);
1064 }
1065
1066 // faces
1067
1068 /* 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 (ecb_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
1085 mask |= 0x1;
1086 sl << uint16 (ob->face);
1087
1088 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
1107 socket.flush_fx ();
1108
1109 if (sl.length () > startlen || socket.sent_scroll)
1110 {
1111 socket.send_packet (sl);
1112 socket.sent_scroll = 0;
1113 }
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 void
1121 esrv_update_spells (player *pl)
1122 {
1123 if (!pl->ns)
1124 return;
1125
1126 pl->ns->update_spells = false;
1127
1128 if (!pl->ns->monitor_spells)
1129 return;
1130
1131 for (object *spell = pl->ob->inv; spell; spell = spell->below)
1132 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 }
1175
1176 void
1177 esrv_remove_spell (player *pl, object *spell)
1178 {
1179 if (!pl->ns->monitor_spells)
1180 return;
1181
1182 if (!pl || !spell || spell->env != pl->ob)
1183 {
1184 LOG (llevError, "Invalid call to esrv_remove_spell");
1185 return;
1186 }
1187
1188 packet sl ("delspell");
1189
1190 sl << uint32 (spell->count);
1191
1192 pl->ns->send_packet (sl);
1193 }
1194
1195 /* appends the spell *spell to the Socklist we will send the data to. */
1196 static void
1197 append_spell (player *pl, packet &sl, object *spell)
1198 {
1199 int skill = 0;
1200
1201 if (!(spell->name))
1202 {
1203 LOG (llevError, "item number %d is a spell with no name.\n", spell->count);
1204 return;
1205 }
1206
1207 /* store costs and damage in the object struct, to compare to later */
1208 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 spell->cached_eat = casting_level (pl->ob, spell);
1211
1212 /* figure out which skill it uses, if it uses one */
1213 if (spell->skill)
1214 if (object *tmp = pl->find_skill (spell->skill))
1215 skill = CS_STAT_SKILLINFO + SKILL_INDEX (tmp);
1216
1217 // 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 pl->ns->send_face (spell->face);
1225
1226 /* send the current values */
1227 sl << uint32 (spell->count)
1228 << uint16 (spell->level)
1229 << uint16 (spell->casting_time)
1230 << uint16 (spell->cached_sp)
1231 << uint16 (spell->cached_grace)
1232 << uint16 (spell->cached_eat)
1233 << uint8 (skill)
1234 << uint32 (spell->path_attuned)
1235 << uint32 (spell->face)
1236 << data8 (spell->name);
1237
1238 if (pl->ns->monitor_spells < 2)
1239 sl << data16 (spell->msg);
1240 }
1241
1242 /**
1243 * This tells the client to add the spell *ob, if *ob is NULL, then add
1244 * all spells in the player's inventory.
1245 */
1246 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
1255 if (!pl->ns->monitor_spells)
1256 return;
1257
1258 packet sl ("addspell");
1259
1260 if (!spell)
1261 {
1262 for (spell = pl->ob->inv; spell; spell = spell->below)
1263 {
1264 /* were we to simply keep appending data here, we could exceed
1265 * 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 * check that the length of those 3 strings, plus the 23 bytes,
1269 * won't take us over the length limit for the socket, if it does,
1270 * we need to send what we already have, and restart packet formation
1271 */
1272 if (spell->type != SPELL)
1273 continue;
1274
1275 /* 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 if (sl.length () > (MAXSOCKBUF - (26 + strlen (spell->name) + (spell->msg ? strlen (spell->msg) : 0))))
1281 {
1282 pl->ns->flush_fx ();
1283 pl->ns->send_packet (sl);
1284
1285 sl.reset ();
1286 sl << "addspell ";
1287 }
1288
1289 append_spell (pl, sl, spell);
1290 }
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 append_spell (pl, sl, spell);
1299
1300 if (sl.length () > MAXSOCKBUF)
1301 cleanup ("buffer overflow in esrv_add_spells!");
1302
1303 /* finally, we can send the packet */
1304 pl->ns->flush_fx ();
1305 pl->ns->send_packet (sl);
1306 }
1307
1308 //-GPL
1309