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