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