ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/socket/request.c
Revision: 1.1.1.3 (vendor branch)
Committed: Wed Mar 15 14:05:40 2006 UTC (18 years, 3 months ago) by elmex
Content type: text/plain
Branch: UPSTREAM
CVS Tags: UPSTREAM_2006_03_15
Changes since 1.1.1.2: +42 -41 lines
Log Message:
cvs -z9 -d:ext:elmex@cvs.schmorp.de:/schmorpforge import cf.schmorp.de UPSTREAM UPSTREAM_2006_03_15

File Contents

# Content
1 /*
2 * static char *rcsid_init_c =
3 * "$Id: request.c,v 1.86 2006/03/14 07:41:49 mwedel Exp $";
4 */
5
6 /*
7 CrossFire, A Multiplayer game for X-windows
8
9 Copyright (C) 2001 Mark Wedel
10 Copyright (C) 1992 Frank Tore Johansen
11
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 2 of the License, or
15 (at your option) any later version.
16
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
21
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25
26 The author can be reached via e-mail to crossfire-devel@real-time.com
27 */
28
29 /**
30 * \file
31 * Client handling.
32 *
33 * \date 2003-12-02
34 *
35 * This file implements all of the goo on the server side for handling
36 * clients. It's got a bunch of global variables for keeping track of
37 * each of the clients.
38 *
39 * Note: All functions that are used to process data from the client
40 * have the prototype of (char *data, int datalen, int client_num). This
41 * way, we can use one dispatch table.
42 *
43 * esrv_map_new starts updating the map
44 *
45 * esrv_map_setbelow allows filling in all of the faces for the map.
46 * if a face has not already been sent to the client, it is sent now.
47 *
48 * mapcellchanged, compactlayer, compactstack, perform the map compressing
49 * operations
50 *
51 * esrv_map_doneredraw finishes the map update, and ships across the
52 * map updates.
53 *
54 * esrv_map_scroll tells the client to scroll the map, and does similarily
55 * for the locally cached copy.
56 */
57
58 #include <global.h>
59 #include <sproto.h>
60
61 #include <newclient.h>
62 #include <newserver.h>
63 #include <living.h>
64 #include <commands.h>
65
66 /* This block is basically taken from socket.c - I assume if it works there,
67 * it should work here.
68 */
69 #ifndef WIN32 /* ---win32 exclude unix headers */
70 #include <sys/types.h>
71 #include <sys/time.h>
72 #include <sys/socket.h>
73 #include <netinet/in.h>
74 #include <netdb.h>
75 #endif /* win32 */
76
77 #ifdef HAVE_UNISTD_H
78 #include <unistd.h>
79 #endif
80
81 #ifdef HAVE_SYS_TIME_H
82 #include <sys/time.h>
83 #endif
84
85 #include "sounds.h"
86
87 /**
88 * This table translates the attack numbers as used within the
89 * program to the value we use when sending STATS command to the
90 * client. If a value is -1, then we don't send that to the
91 * client.
92 */
93 short atnr_cs_stat[NROFATTACKS] = {CS_STAT_RES_PHYS,
94 CS_STAT_RES_MAG,CS_STAT_RES_FIRE, CS_STAT_RES_ELEC,
95 CS_STAT_RES_COLD, CS_STAT_RES_CONF, CS_STAT_RES_ACID,
96 CS_STAT_RES_DRAIN, -1 /* weaponmagic */,
97 CS_STAT_RES_GHOSTHIT, CS_STAT_RES_POISON,
98 CS_STAT_RES_SLOW, CS_STAT_RES_PARA, CS_STAT_TURN_UNDEAD,
99 CS_STAT_RES_FEAR, -1 /* Cancellation */,
100 CS_STAT_RES_DEPLETE, CS_STAT_RES_DEATH,
101 -1 /* Chaos */, -1 /* Counterspell */,
102 -1 /* Godpower */, CS_STAT_RES_HOLYWORD,
103 CS_STAT_RES_BLIND,
104 -1, /* Internal */
105 -1, /* life stealing */
106 -1 /* Disease - not fully done yet */
107 };
108
109 /** This is the Setup cmd - easy first implementation */
110 void SetUp(char *buf, int len, NewSocket *ns)
111 {
112 int s, slen;
113 char *cmd, *param, cmdback[HUGE_BUF];
114
115 /* run through the cmds of setup
116 * syntax is setup <cmdname1> <parameter> <cmdname2> <parameter> ...
117 *
118 * we send the status of the cmd back, or a FALSE is the cmd is the server unknown
119 * The client then must sort this out
120 */
121
122 LOG(llevInfo,"Get SetupCmd:: %s\n", buf);
123 strcpy(cmdback,"setup");
124 for(s=0;s<len; ) {
125
126 cmd = &buf[s];
127
128 /* find the next space, and put a null there */
129 for(;buf[s] && buf[s] != ' ';s++) ;
130 buf[s++]=0;
131 while (buf[s] == ' ') s++;
132
133 if(s>=len)
134 break;
135
136 param = &buf[s];
137
138 for(;buf[s] && buf[s] != ' ';s++) ;
139 buf[s++]=0;
140 while (buf[s] == ' ') s++;
141
142 slen = strlen(cmdback);
143 safe_strcat(cmdback, " ", &slen, HUGE_BUF);
144 safe_strcat(cmdback, cmd, &slen, HUGE_BUF);
145 safe_strcat(cmdback, " ", &slen, HUGE_BUF);
146
147 if (!strcmp(cmd,"sound")) {
148 ns->sound = atoi(param);
149 safe_strcat(cmdback, param, &slen, HUGE_BUF);
150 }
151 else if (!strcmp(cmd,"exp64")) {
152 ns->exp64 = atoi(param);
153 safe_strcat(cmdback, param, &slen, HUGE_BUF);
154 } else if (!strcmp(cmd, "spellmon")) {
155 ns->monitor_spells = atoi(param);
156 safe_strcat(cmdback, param, &slen, HUGE_BUF);
157 } else if (!strcmp(cmd,"darkness")) {
158 ns->darkness = atoi(param);
159 safe_strcat(cmdback, param, &slen, HUGE_BUF);
160 } else if (!strcmp(cmd,"map1cmd")) {
161 if (atoi(param)) ns->mapmode = Map1Cmd;
162 /* if beyond this size, need to use map1cmd no matter what */
163 if (ns->mapx>11 || ns->mapy>11) ns->mapmode = Map1Cmd;
164 safe_strcat(cmdback, ns->mapmode == Map1Cmd?"1":"0", &slen, HUGE_BUF);
165 } else if (!strcmp(cmd,"map1acmd")) {
166 if (atoi(param)) ns->mapmode = Map1aCmd;
167 /* if beyond this size, need to use map1acmd no matter what */
168 if (ns->mapx>11 || ns->mapy>11) ns->mapmode = Map1aCmd;
169 safe_strcat(cmdback, ns->mapmode == Map1aCmd?"1":"0", &slen, HUGE_BUF);
170 } else if (!strcmp(cmd,"newmapcmd")) {
171 ns->newmapcmd= atoi(param);
172 safe_strcat(cmdback, param, &slen, HUGE_BUF);
173 } else if (!strcmp(cmd,"facecache")) {
174 ns->facecache = atoi(param);
175 safe_strcat(cmdback, param, &slen, HUGE_BUF);
176 } else if (!strcmp(cmd,"faceset")) {
177 char tmpbuf[20];
178 int q = atoi(param);
179
180 if (is_valid_faceset(q))
181 ns->faceset=q;
182 sprintf(tmpbuf,"%d", ns->faceset);
183 safe_strcat(cmdback, tmpbuf, &slen, HUGE_BUF);
184 /* if the client is using faceset, it knows about image2 command */
185 ns->image2=1;
186 } else if (!strcmp(cmd,"itemcmd")) {
187 /* Version of the item protocol command to use. Currently,
188 * only supported versions are 1 and 2. Using a numeric
189 * value will make it very easy to extend this in the future.
190 */
191 char tmpbuf[20];
192 int q = atoi(param);
193 if (q<1 || q>2) {
194 strcpy(tmpbuf,"FALSE");
195 } else {
196 ns->itemcmd = q;
197 sprintf(tmpbuf,"%d", ns->itemcmd);
198 }
199 safe_strcat(cmdback, tmpbuf, &slen, HUGE_BUF);
200 } else if (!strcmp(cmd,"mapsize")) {
201 int x, y=0;
202 char tmpbuf[MAX_BUF], *cp;
203
204 x = atoi(param);
205 for (cp = param; *cp!=0; cp++)
206 if (*cp == 'x' || *cp == 'X') {
207 y = atoi(cp+1);
208 break;
209 }
210 if (x < 9 || y < 9 || x>MAP_CLIENT_X || y > MAP_CLIENT_Y) {
211 sprintf(tmpbuf," %dx%d", MAP_CLIENT_X, MAP_CLIENT_Y);
212 safe_strcat(cmdback, tmpbuf, &slen, HUGE_BUF);
213 } else {
214 ns->mapx = x;
215 ns->mapy = y;
216 /* better to send back what we are really using and not the
217 * param as given to us in case it gets parsed differently.
218 */
219 sprintf(tmpbuf,"%dx%d", x,y);
220 safe_strcat(cmdback, tmpbuf, &slen, HUGE_BUF);
221 /* If beyond this size and still using orig map command, need to
222 * go to map1cmd.
223 */
224 if ((x>11 || y>11) && ns->mapmode == Map0Cmd) ns->mapmode = Map1Cmd;
225 }
226 } else if (!strcmp(cmd,"extendedMapInfos")) {
227 /* Added by tchize
228 * prepare to use the mapextended command
229 */
230 char tmpbuf[20];
231 ns->ext_mapinfos = (atoi(param));
232 sprintf(tmpbuf,"%d", ns->ext_mapinfos);
233 safe_strcat(cmdback, tmpbuf, &slen, HUGE_BUF);
234 } else if (!strcmp(cmd,"extendedTextInfos")) {
235 /* Added by tchize
236 * prepare to use the extended text commands
237 * Client toggle this to non zero to get exttext
238 */
239 char tmpbuf[20];
240
241 ns->has_readable_type = (atoi(param));
242 sprintf(tmpbuf,"%d", ns->has_readable_type);
243 safe_strcat(cmdback, tmpbuf, &slen, HUGE_BUF);
244 } else {
245 /* Didn't get a setup command we understood -
246 * report a failure to the client.
247 */
248 safe_strcat(cmdback, "FALSE", &slen, HUGE_BUF);
249 }
250 } /* for processing all the setup commands */
251 LOG(llevInfo,"SendBack SetupCmd:: %s\n", cmdback);
252 Write_String_To_Socket(ns, cmdback, strlen(cmdback));
253 }
254
255 /**
256 * The client has requested to be added to the game.
257 * This is what takes care of it. We tell the client how things worked out.
258 * I am not sure if this file is the best place for this function. however,
259 * it either has to be here or init_sockets needs to be exported.
260 */
261 void AddMeCmd(char *buf, int len, NewSocket *ns)
262 {
263 Settings oldsettings;
264 oldsettings=settings;
265 if (ns->status != Ns_Add || add_player(ns)) {
266 Write_String_To_Socket(ns, "addme_failed",12);
267 } else {
268 /* Basically, the add_player copies the socket structure into
269 * the player structure, so this one (which is from init_sockets)
270 * is not needed anymore. The write below should still work, as the
271 * stuff in ns is still relevant.
272 */
273 Write_String_To_Socket(ns, "addme_success",13);
274 socket_info.nconns--;
275 ns->status = Ns_Avail;
276 }
277 settings=oldsettings;
278 }
279
280 /** Reply to ExtendedInfos command */
281 void ToggleExtendedInfos (char *buf, int len, NewSocket *ns){
282 char cmdback[MAX_BUF];
283 char command[50];
284 int info,nextinfo;
285 cmdback[0]='\0';
286 nextinfo=0;
287 while (1){
288 /* 1. Extract an info*/
289 info=nextinfo;
290 while ( (info<len) && (buf[info]==' ') ) info++;
291 if (info>=len)
292 break;
293 nextinfo=info+1;
294 while ( (nextinfo<len) && (buf[nextinfo]!=' ') )
295 nextinfo++;
296 if (nextinfo-info>=49) /*Erroneous info asked*/
297 continue;
298 strncpy (command,&(buf[info]),nextinfo-info);
299 command[nextinfo-info]='\0';
300 /* 2. Interpret info*/
301 if (!strcmp("smooth",command)){
302 /* Toggle smoothing*/
303 ns->EMI_smooth=!ns->EMI_smooth;
304 }else{
305 /*bad value*/
306 }
307 /*3. Next info*/
308 }
309 strcpy (cmdback,"ExtendedInfoSet");
310 if (ns->EMI_smooth){
311 strcat (cmdback," ");
312 strcat (cmdback,"smoothing");
313 }
314 Write_String_To_Socket(ns, cmdback,strlen(cmdback));
315 }
316 /*
317 #define MSG_TYPE_BOOK 1
318 #define MSG_TYPE_CARD 2
319 #define MSG_TYPE_PAPER 3
320 #define MSG_TYPE_SIGN 4
321 #define MSG_TYPE_MONUMENT 5
322 #define MSG_TYPE_SCRIPTED_DIALOG 6*/
323 /** Reply to ExtendedInfos command */
324 void ToggleExtendedText (char *buf, int len, NewSocket *ns){
325 char cmdback[MAX_BUF];
326 char temp[10];
327 char command[50];
328 int info,nextinfo,i,flag;
329 cmdback[0]='\0';
330 nextinfo=0;
331 while (1){
332 /* 1. Extract an info*/
333 info=nextinfo;
334 while ( (info<len) && (buf[info]==' ') ) info++;
335 if (info>=len)
336 break;
337 nextinfo=info+1;
338 while ( (nextinfo<len) && (buf[nextinfo]!=' ') )
339 nextinfo++;
340 if (nextinfo-info>=49) /*Erroneous info asked*/
341 continue;
342 strncpy (command,&(buf[info]),nextinfo-info);
343 command[nextinfo-info]='\0';
344 /* 2. Interpret info*/
345 i = sscanf(command,"%d",&flag);
346 if ( (i==1) && (flag>0) && (flag<=MSG_TYPE_LAST))
347 ns->supported_readables|=(1<<flag);
348 /*3. Next info*/
349 }
350 /* Send resulting state */
351 strcpy (cmdback,"ExtendedTextSet");
352 for (i=0;i<=MSG_TYPE_LAST;i++)
353 if (ns->supported_readables &(1<<i)){
354 strcat (cmdback," ");
355 snprintf(temp,sizeof(temp),"%d",i);
356 strcat (cmdback,temp);
357 }
358 Write_String_To_Socket(ns, cmdback,strlen(cmdback));
359 }
360
361 /**
362 * A lot like the old AskSmooth (in fact, now called by AskSmooth).
363 * Basically, it makes no sense to wait for the client to request a
364 * a piece of data from us that we know the client wants. So
365 * if we know the client wants it, might as well push it to the
366 * client.
367 */
368 static void SendSmooth(NewSocket *ns, uint16 face) {
369 uint16 smoothface;
370 uint8 reply[MAX_BUF];
371 SockList sl;
372
373 /* If we can't find a face, return and set it so we won't try to send this
374 * again.
375 */
376 if ((!FindSmooth (face, &smoothface)) &&
377 (!FindSmooth ( smooth_face->number, &smoothface))) {
378
379 LOG(llevError,"could not findsmooth for %d. Neither default (%s)\n",face,smooth_face->name);
380 ns->faces_sent[face] |= NS_FACESENT_SMOOTH;
381 return;
382 }
383
384 if (!(ns->faces_sent[smoothface] & NS_FACESENT_FACE))
385 esrv_send_face(ns, smoothface, 0);
386
387 ns->faces_sent[face] |= NS_FACESENT_SMOOTH;
388
389 sl.buf=reply;
390 strcpy((char*)sl.buf,"smooth ");
391 sl.len=strlen((char*)sl.buf);
392 SockList_AddShort(&sl, face);
393 SockList_AddShort(&sl, smoothface);
394 Send_With_Handling(ns, &sl);
395 }
396
397 /**
398 * Tells client the picture it has to use
399 * to smooth a picture number given as argument.
400 */
401 void AskSmooth (char *buf, int len, NewSocket *ns){
402 uint16 facenbr;
403
404 facenbr=atoi (buf);
405 SendSmooth(ns, facenbr);
406 }
407
408
409
410
411
412 /**
413 * This handles the general commands from the client (ie, north, fire, cast,
414 * etc.)
415 */
416 void PlayerCmd(char *buf, int len, player *pl)
417 {
418
419 /* The following should never happen with a proper or honest client.
420 * Therefore, the error message doesn't have to be too clear - if
421 * someone is playing with a hacked/non working client, this gives them
422 * an idea of the problem, but they deserve what they get
423 */
424 if (pl->state!=ST_PLAYING) {
425 new_draw_info_format(NDI_UNIQUE, 0,pl->ob,
426 "You can not issue commands - state is not ST_PLAYING (%s)", buf);
427 return;
428 }
429 /* Check if there is a count. In theory, a zero count could also be
430 * sent, so check for that also.
431 */
432 if (atoi(buf) || buf[0]=='0') {
433 pl->count=atoi((char*)buf);
434 buf=strchr(buf,' '); /* advance beyond the numbers */
435 if (!buf) {
436 #ifdef ESRV_DEBUG
437 LOG(llevDebug,"PlayerCmd: Got count but no command.\n");
438 #endif
439 return;
440 }
441 buf++;
442 }
443 /* This should not happen anymore. */
444 if (pl->ob->speed_left<-1.0) {
445 LOG(llevError,"Player has negative time - shouldn't do command.\n");
446 }
447 /* In c_new.c */
448 execute_newserver_command(pl->ob, (char*)buf);
449 /* Perhaps something better should be done with a left over count.
450 * Cleaning up the input should probably be done first - all actions
451 * for the command that issued the count should be done before any other
452 * commands.
453 */
454
455 pl->count=0;
456
457 }
458
459
460 /**
461 * This handles the general commands from the client (ie, north, fire, cast,
462 * etc.). It is a lot like PlayerCmd above, but is called with the
463 * 'ncom' method which gives more information back to the client so it
464 * can throttle.
465 */
466 void NewPlayerCmd(uint8 *buf, int len, player *pl)
467 {
468 int time,repeat;
469 short packet;
470 char command[MAX_BUF];
471 SockList sl;
472
473 if (len < 7) {
474 LOG(llevDebug,"Corrupt ncom command - not long enough - discarding\n");
475 return;
476 }
477
478 packet = GetShort_String(buf);
479 repeat = GetInt_String(buf+2);
480 /* -1 is special - no repeat, but don't update */
481 if (repeat!=-1) {
482 pl->count=repeat;
483 }
484 if ((len-4) >= MAX_BUF) len=MAX_BUF-5;
485
486 strncpy(command, (char*)buf+6, len-4);
487 command[len-4]='\0';
488
489 /* The following should never happen with a proper or honest client.
490 * Therefore, the error message doesn't have to be too clear - if
491 * someone is playing with a hacked/non working client, this gives them
492 * an idea of the problem, but they deserve what they get
493 */
494 if (pl->state!=ST_PLAYING) {
495 new_draw_info_format(NDI_UNIQUE, 0,pl->ob,
496 "You can not issue commands - state is not ST_PLAYING (%s)", buf);
497 return;
498 }
499
500 /* This should not happen anymore. */
501 if (pl->ob->speed_left<-1.0) {
502 LOG(llevError,"Player has negative time - shouldn't do command.\n");
503 }
504 /* In c_new.c */
505 execute_newserver_command(pl->ob, command);
506 /* Perhaps something better should be done with a left over count.
507 * Cleaning up the input should probably be done first - all actions
508 * for the command that issued the count should be done before any other
509 * commands.
510 */
511 pl->count=0;
512
513 /* Send confirmation of command execution now */
514 sl.buf = (uint8*)command;
515 strcpy((char*)sl.buf,"comc ");
516 sl.len=5;
517 SockList_AddShort(&sl,packet);
518 if (FABS(pl->ob->speed) < 0.001) time=MAX_TIME * 100;
519 else
520 time = ( int )( MAX_TIME/ FABS(pl->ob->speed) );
521 SockList_AddInt(&sl,time);
522 Send_With_Handling(&pl->socket, &sl);
523 }
524
525
526 /** This is a reply to a previous query. */
527 void ReplyCmd(char *buf, int len, player *pl)
528 {
529 /* This is to synthesize how the data would be stored if it
530 * was normally entered. A bit of a hack, and should be cleaned up
531 * once all the X11 code is removed from the server.
532 *
533 * We pass 13 to many of the functions because this way they
534 * think it was the carriage return that was entered, and the
535 * function then does not try to do additional input.
536 */
537 snprintf(pl->write_buf, sizeof(pl->write_buf), ":%s", buf);
538
539 /* this avoids any hacking here */
540
541 switch (pl->state) {
542 case ST_PLAYING:
543 LOG(llevError,"Got reply message with ST_PLAYING input state\n");
544 break;
545
546 case ST_PLAY_AGAIN:
547 /* We can check this for return value (2==quit). Maybe we
548 * should, and do something appropriate?
549 */
550 receive_play_again(pl->ob, buf[0]);
551 break;
552
553 case ST_ROLL_STAT:
554 key_roll_stat(pl->ob,buf[0]);
555 break;
556
557 case ST_CHANGE_CLASS:
558
559 key_change_class(pl->ob, buf[0]);
560 break;
561
562 case ST_CONFIRM_QUIT:
563 key_confirm_quit(pl->ob, buf[0]);
564 break;
565
566 case ST_CONFIGURE:
567 LOG(llevError,"In client input handling, but into configure state\n");
568 pl->state = ST_PLAYING;
569 break;
570
571 case ST_GET_NAME:
572 receive_player_name(pl->ob,13);
573 break;
574
575 case ST_GET_PASSWORD:
576 case ST_CONFIRM_PASSWORD:
577 receive_player_password(pl->ob,13);
578 break;
579
580 case ST_GET_PARTY_PASSWORD: /* Get password for party */
581 receive_party_password(pl->ob,13);
582 break;
583
584 default:
585 LOG(llevError,"Unknown input state: %d\n", pl->state);
586 }
587 }
588
589 /**
590 * Client tells its version. If there is a mismatch, we close the
591 * socket. In real life, all we should care about is the client having
592 * something older than the server. If we assume the client will be
593 * backwards compatible, having it be a later version should not be a
594 * problem.
595 */
596 void VersionCmd(char *buf, int len,NewSocket *ns)
597 {
598 char *cp;
599 char version_warning[256];
600
601 if (!buf) {
602 LOG(llevError, "CS: received corrupted version command\n");
603 return;
604 }
605
606 ns->cs_version = atoi(buf);
607 ns->sc_version = ns->cs_version;
608 if (VERSION_CS != ns->cs_version) {
609 #ifdef ESRV_DEBUG
610 LOG(llevDebug, "CS: csversion mismatch (%d,%d)\n", VERSION_CS,ns->cs_version);
611 #endif
612 }
613 cp = strchr(buf+1,' ');
614 if (!cp) return;
615 ns->sc_version = atoi(cp);
616 if (VERSION_SC != ns->sc_version) {
617 #ifdef ESRV_DEBUG
618 LOG(llevDebug, "CS: scversion mismatch (%d,%d)\n",VERSION_SC,ns->sc_version);
619 #endif
620 }
621 cp = strchr(cp+1, ' ');
622 if (cp) {
623 LOG(llevDebug,"CS: connection from client of type <%s>, ip %s\n", cp, ns->host);
624
625 /* This is first implementation - i skip all beta DX clients with it
626 * Add later stuff here for other clients
627 */
628
629 /* these are old dxclients */
630 /* Version 1024 added support for singular + plural name values -
631 * requiing this minimal value reduces complexity of that code, and it
632 * has been around for a long time.
633 */
634 if(!strcmp(" CF DX CLIENT", cp) || ns->sc_version < 1024 )
635 {
636 sprintf(version_warning,"drawinfo %d %s", NDI_RED, "**** VERSION WARNING ****\n**** CLIENT IS TOO OLD!! UPDATE THE CLIENT!! ****");
637 Write_String_To_Socket(ns, version_warning, strlen(version_warning));
638 }
639
640 }
641 }
642
643 /** sound related functions. */
644
645 void SetSound(char *buf, int len, NewSocket *ns)
646 {
647 ns->sound = atoi(buf);
648 }
649
650 /** client wants the map resent */
651
652 void MapRedrawCmd(char *buf, int len, player *pl)
653 {
654 /* This function is currently disabled; just clearing the map state results in
655 * display errors. It should clear the cache and send a newmap command.
656 * Unfortunately this solution does not work because some client versions send
657 * a mapredraw command after receiving a newmap command.
658 */
659 #if 0
660 /* Okay, this is MAJOR UGLY. but the only way I know how to
661 * clear the "cache"
662 */
663 memset(&pl->socket.lastmap, 0, sizeof(struct Map));
664 draw_client_map(pl->ob);
665 #endif
666 }
667
668 /** Newmap command */
669 void MapNewmapCmd( player *pl)
670 {
671 if( pl->socket.newmapcmd == 1) {
672 memset(&pl->socket.lastmap, 0, sizeof(pl->socket.lastmap));
673 Write_String_To_Socket( &pl->socket, "newmap", 6);
674 }
675 }
676
677
678
679 /**
680 * Moves an object (typically, container to inventory).
681 * syntax is: move (to) (tag) (nrof)
682 */
683 void MoveCmd(char *buf, int len,player *pl)
684 {
685 int vals[3], i;
686
687 /* A little funky here. We only cycle for 2 records, because
688 * we obviously am not going to find a space after the third
689 * record. Perhaps we should just replace this with a
690 * sscanf?
691 */
692 for (i=0; i<2; i++) {
693 vals[i]=atoi(buf);
694 if (!(buf = strchr(buf, ' '))) {
695 LOG(llevError,"Incomplete move command: %s\n", buf);
696 return;
697 }
698 buf++;
699 }
700 vals[2]=atoi(buf);
701
702 /* LOG(llevDebug,"Move item %d (nrof=%d) to %d.\n", vals[1], vals[2], vals[0]);*/
703 esrv_move_object(pl->ob,vals[0], vals[1], vals[2]);
704 }
705
706
707
708 /******************************************************************************
709 *
710 * Start of commands the server sends to the client.
711 *
712 ******************************************************************************/
713
714 /**
715 * Asks the client to query the user. This way, the client knows
716 * it needs to send something back (vs just printing out a message)
717 */
718 void send_query(NewSocket *ns, uint8 flags, char *text)
719 {
720 char buf[MAX_BUF];
721
722 sprintf(buf,"query %d %s", flags, text?text:"");
723 Write_String_To_Socket(ns, buf, strlen(buf));
724 }
725
726 #define AddIfInt64(Old,New,Type) if (Old != New) {\
727 Old = New; \
728 SockList_AddChar(&sl, Type); \
729 SockList_AddInt64(&sl, New); \
730 }
731
732 #define AddIfInt(Old,New,Type) if (Old != New) {\
733 Old = New; \
734 SockList_AddChar(&sl, Type); \
735 SockList_AddInt(&sl, New); \
736 }
737
738 #define AddIfShort(Old,New,Type) if (Old != New) {\
739 Old = New; \
740 SockList_AddChar(&sl, Type); \
741 SockList_AddShort(&sl, New); \
742 }
743
744 #define AddIfFloat(Old,New,Type) if (Old != New) {\
745 Old = New; \
746 SockList_AddChar(&sl, Type); \
747 SockList_AddInt(&sl,(long)(New*FLOAT_MULTI));\
748 }
749
750 #define AddIfString(Old,New,Type) if (Old == NULL || strcmp(Old,New)) {\
751 if (Old) free(Old);\
752 Old = strdup_local(New);\
753 SockList_AddChar(&sl, Type); \
754 SockList_AddChar(&sl, ( char )strlen(New)); \
755 strcpy((char*)sl.buf + sl.len, New); \
756 sl.len += strlen(New); \
757 }
758
759 /**
760 * Sends a statistics update. We look at the old values,
761 * and only send what has changed. Stat mapping values are in newclient.h
762 * Since this gets sent a lot, this is actually one of the few binary
763 * commands for now.
764 */
765 void esrv_update_stats(player *pl)
766 {
767 SockList sl;
768 char buf[MAX_BUF];
769 uint16 flags;
770
771 sl.buf=malloc(MAXSOCKBUF);
772 strcpy((char*)sl.buf,"stats ");
773 sl.len=strlen((char*)sl.buf);
774
775 if(pl->ob != NULL)
776 {
777 AddIfShort(pl->last_stats.hp, pl->ob->stats.hp, CS_STAT_HP);
778 AddIfShort(pl->last_stats.maxhp, pl->ob->stats.maxhp, CS_STAT_MAXHP);
779 AddIfShort(pl->last_stats.sp, pl->ob->stats.sp, CS_STAT_SP);
780 AddIfShort(pl->last_stats.maxsp, pl->ob->stats.maxsp, CS_STAT_MAXSP);
781 AddIfShort(pl->last_stats.grace, pl->ob->stats.grace, CS_STAT_GRACE);
782 AddIfShort(pl->last_stats.maxgrace, pl->ob->stats.maxgrace, CS_STAT_MAXGRACE);
783 AddIfShort(pl->last_stats.Str, pl->ob->stats.Str, CS_STAT_STR);
784 AddIfShort(pl->last_stats.Int, pl->ob->stats.Int, CS_STAT_INT);
785 AddIfShort(pl->last_stats.Pow, pl->ob->stats.Pow, CS_STAT_POW);
786 AddIfShort(pl->last_stats.Wis, pl->ob->stats.Wis, CS_STAT_WIS);
787 AddIfShort(pl->last_stats.Dex, pl->ob->stats.Dex, CS_STAT_DEX);
788 AddIfShort(pl->last_stats.Con, pl->ob->stats.Con, CS_STAT_CON);
789 AddIfShort(pl->last_stats.Cha, pl->ob->stats.Cha, CS_STAT_CHA);
790 }
791 if(pl->socket.exp64) {
792 uint8 s;
793 for(s=0;s<NUM_SKILLS;s++) {
794 if (pl->last_skill_ob[s] &&
795 pl->last_skill_exp[s] != pl->last_skill_ob[s]->stats.exp) {
796
797 /* Always send along the level if exp changes. This is only
798 * 1 extra byte, but keeps processing simpler.
799 */
800 SockList_AddChar(&sl, ( char )( s + CS_STAT_SKILLINFO ));
801 SockList_AddChar(&sl, ( char )pl->last_skill_ob[s]->level);
802 SockList_AddInt64(&sl, pl->last_skill_ob[s]->stats.exp);
803 pl->last_skill_exp[s] = pl->last_skill_ob[s]->stats.exp;
804 }
805 }
806 }
807 if (pl->socket.exp64) {
808 AddIfInt64(pl->last_stats.exp, pl->ob->stats.exp, CS_STAT_EXP64);
809 } else {
810 AddIfInt(pl->last_stats.exp, ( int )pl->ob->stats.exp, CS_STAT_EXP);
811 }
812 AddIfShort(pl->last_level, ( char )pl->ob->level, CS_STAT_LEVEL);
813 AddIfShort(pl->last_stats.wc, pl->ob->stats.wc, CS_STAT_WC);
814 AddIfShort(pl->last_stats.ac, pl->ob->stats.ac, CS_STAT_AC);
815 AddIfShort(pl->last_stats.dam, pl->ob->stats.dam, CS_STAT_DAM);
816 AddIfFloat(pl->last_speed, pl->ob->speed, CS_STAT_SPEED);
817 AddIfShort(pl->last_stats.food, pl->ob->stats.food, CS_STAT_FOOD);
818 AddIfFloat(pl->last_weapon_sp, pl->weapon_sp, CS_STAT_WEAP_SP);
819 AddIfInt(pl->last_weight_limit, (sint32)weight_limit[pl->ob->stats.Str], CS_STAT_WEIGHT_LIM);
820 flags=0;
821 if (pl->fire_on) flags |=SF_FIREON;
822 if (pl->run_on) flags |= SF_RUNON;
823
824 AddIfShort(pl->last_flags, flags, CS_STAT_FLAGS);
825 if (pl->socket.sc_version<1025) {
826 AddIfShort(pl->last_resist[ATNR_PHYSICAL], pl->ob->resist[ATNR_PHYSICAL], CS_STAT_ARMOUR);
827 } else {
828 int i;
829
830 for (i=0; i<NROFATTACKS; i++) {
831 /* Skip ones we won't send */
832 if (atnr_cs_stat[i]==-1) continue;
833 AddIfShort(pl->last_resist[i], pl->ob->resist[i], ( char )atnr_cs_stat[i]);
834 }
835 }
836 if (pl->socket.monitor_spells) {
837 AddIfInt(pl->last_path_attuned, pl->ob->path_attuned, CS_STAT_SPELL_ATTUNE);
838 AddIfInt(pl->last_path_repelled, pl->ob->path_repelled, CS_STAT_SPELL_REPEL);
839 AddIfInt(pl->last_path_denied, pl->ob->path_denied, CS_STAT_SPELL_DENY);
840 }
841 rangetostring(pl->ob, buf); /* we want use the new fire & run system in new client */
842 AddIfString(pl->socket.stats.range, buf, CS_STAT_RANGE);
843 set_title(pl->ob, buf);
844 AddIfString(pl->socket.stats.title, buf, CS_STAT_TITLE);
845
846 /* Only send it away if we have some actual data */
847 if (sl.len>6) {
848 #ifdef ESRV_DEBUG
849 LOG(llevDebug,"Sending stats command, %d bytes long.\n", sl.len);
850 #endif
851 Send_With_Handling(&pl->socket, &sl);
852 }
853 free(sl.buf);
854 }
855
856
857 /**
858 * Tells the client that here is a player it should start using.
859 */
860 void esrv_new_player(player *pl, uint32 weight)
861 {
862 SockList sl;
863
864 pl->last_weight = weight;
865
866 sl.buf=malloc(MAXSOCKBUF);
867
868 strcpy((char*)sl.buf,"player ");
869 sl.len=strlen((char*)sl.buf);
870 SockList_AddInt(&sl, pl->ob->count);
871 SockList_AddInt(&sl, weight);
872 SockList_AddInt(&sl, pl->ob->face->number);
873
874 SockList_AddChar(&sl, ( char )strlen(pl->ob->name));
875 strcpy((char*)sl.buf+sl.len, pl->ob->name);
876 sl.len += strlen(pl->ob->name);
877
878 Send_With_Handling(&pl->socket, &sl);
879 free(sl.buf);
880 SET_FLAG(pl->ob, FLAG_CLIENT_SENT);
881 }
882
883
884 /**
885 * Need to send an animation sequence to the client.
886 * We will send appropriate face commands to the client if we haven't
887 * sent them the face yet (this can become quite costly in terms of
888 * how much we are sending - on the other hand, this should only happen
889 * when the player logs in and picks stuff up.
890 */
891 void esrv_send_animation(NewSocket *ns, short anim_num)
892 {
893 SockList sl;
894 int i;
895
896 /* Do some checking on the anim_num we got. Note that the animations
897 * are added in contigous order, so if the number is in the valid
898 * range, it must be a valid animation.
899 */
900 if (anim_num < 0 || anim_num > num_animations) {
901 LOG(llevError,"esrv_send_anim (%d) out of bounds??\n",anim_num);
902 return;
903 }
904
905 sl.buf = malloc(MAXSOCKBUF);
906 strcpy((char*)sl.buf, "anim ");
907 sl.len=5;
908 SockList_AddShort(&sl, anim_num);
909 SockList_AddShort(&sl, 0); /* flags - not used right now */
910 /* Build up the list of faces. Also, send any information (ie, the
911 * the face itself) down to the client.
912 */
913 for (i=0; i<animations[anim_num].num_animations; i++) {
914 if (!(ns->faces_sent[animations[anim_num].faces[i]] & NS_FACESENT_FACE))
915 esrv_send_face(ns,animations[anim_num].faces[i],0);
916 SockList_AddShort(&sl, animations[anim_num].faces[i]); /* flags - not used right now */
917 }
918 Send_With_Handling(ns, &sl);
919 free(sl.buf);
920 ns->anims_sent[anim_num] = 1;
921 }
922
923
924 /******************************************************************************
925 *
926 * Start of map related commands.
927 *
928 ******************************************************************************/
929
930 /**
931 * This adds face_num to a map cell at x,y. If the client doesn't have
932 * the face yet, we will also send it.
933 */
934 static void esrv_map_setbelow(NewSocket *ns, int x,int y,
935 short face_num, struct Map *newmap)
936 {
937 if(newmap->cells[x][y].count >= MAP_LAYERS) {
938 LOG(llevError,"Too many faces in map cell %d %d\n",x,y);
939 return;
940 abort();
941 }
942 newmap->cells[x][y].faces[newmap->cells[x][y].count] = face_num;
943 newmap->cells[x][y].count ++;
944 if (!(ns->faces_sent[face_num] & NS_FACESENT_FACE))
945 esrv_send_face(ns,face_num,0);
946 }
947
948 struct LayerCell {
949 uint16 xy;
950 short face;
951 };
952
953 struct MapLayer {
954 int count;
955 struct LayerCell lcells[MAP_CLIENT_X * MAP_CLIENT_Y];
956 };
957
958 /** Checkes if map cells have changed */
959 static int mapcellchanged(NewSocket *ns,int i,int j, struct Map *newmap)
960 {
961 int k;
962
963 if (ns->lastmap.cells[i][j].count != newmap->cells[i][j].count)
964 return 1;
965 for(k=0;k<newmap->cells[i][j].count;k++) {
966 if (ns->lastmap.cells[i][j].faces[k] !=
967 newmap->cells[i][j].faces[k]) {
968 return 1;
969 }
970 }
971 return 0;
972 }
973
974 /**
975 * Basically, what this does is pack the data into layers.
976 * cnum is the client number, cur is the the buffer we put all of
977 * this data into. we return the end of the data. layers is
978 * how many layers of data we should back.
979 */
980 static uint8 *compactlayer(NewSocket *ns, unsigned char *cur, int numlayers,
981 struct Map *newmap)
982 {
983 int x,y,k;
984 int face;
985 unsigned char *fcur;
986 struct MapLayer layers[MAP_LAYERS];
987
988 for(k = 0;k<MAP_LAYERS;k++)
989 layers[k].count = 0;
990 fcur = cur;
991 for(x=0;x<ns->mapx;x++) {
992 for(y=0;y<ns->mapy;y++) {
993 if (!mapcellchanged(ns,x,y,newmap))
994 continue;
995 if (newmap->cells[x][y].count == 0) {
996 *cur = x*ns->mapy+y; /* mark empty space */
997 cur++;
998 continue;
999 }
1000 for(k=0;k<newmap->cells[x][y].count;k++) {
1001 layers[k].lcells[layers[k].count].xy = x*ns->mapy+y;
1002 layers[k].lcells[layers[k].count].face =
1003 newmap->cells[x][y].faces[k];
1004 layers[k].count++;
1005 }
1006 }
1007 }
1008 /* If no data, return now. */
1009 if (fcur == cur && layers[0].count == 0)
1010 return cur;
1011 *cur = 255; /* mark end of explicitly cleared cells */
1012 cur++;
1013 /* First pack by layers. */
1014 for(k=0;k<numlayers;k++) {
1015 if (layers[k].count == 0)
1016 break; /* once a layer is entirely empty, no layer below it can
1017 have anything in it either */
1018 /* Pack by entries in thie layer */
1019 for(x=0;x<layers[k].count;) {
1020 fcur = cur;
1021 *cur = layers[k].lcells[x].face >> 8;
1022 cur++;
1023 *cur = layers[k].lcells[x].face & 0xFF;
1024 cur++;
1025 face = layers[k].lcells[x].face;
1026 /* Now, we back the redundant data into 1 byte xy pairings */
1027 for(y=x;y<layers[k].count;y++) {
1028 if (layers[k].lcells[y].face == face) {
1029 *cur = ( uint8 )layers[k].lcells[y].xy;
1030 cur++;
1031 layers[k].lcells[y].face = -1;
1032 }
1033 }
1034 *(cur-1) = *(cur-1) | 128; /* mark for end of xy's; 11*11 < 128 */
1035 /* forward over the now redundant data */
1036 while(x < layers[k].count &&
1037 layers[k].lcells[x].face == -1)
1038 x++;
1039 }
1040 *fcur = *fcur | 128; /* mark for end of faces at this layer */
1041 }
1042 return cur;
1043 }
1044
1045 static void esrv_map_doneredraw(NewSocket *ns, struct Map *newmap)
1046 {
1047 static long frames,bytes,tbytes,tframes;
1048 uint8 *cur;
1049 SockList sl;
1050
1051
1052 sl.buf=malloc(MAXSOCKBUF);
1053 strcpy((char*)sl.buf,"map ");
1054 sl.len=strlen((char*)sl.buf);
1055
1056 cur = compactlayer(ns,sl.buf+sl.len,MAP_LAYERS,newmap);
1057 sl.len=cur-sl.buf;
1058
1059 /* LOG(llevDebug, "Sending map command.\n");*/
1060
1061 if (sl.len>( int )strlen("map ") || ns->sent_scroll) {
1062 /* All of this is just accounting stuff */
1063 if (tframes>100) {
1064 tframes = tbytes = 0;
1065 }
1066 tframes++;
1067 frames++;
1068 tbytes += sl.len;
1069 bytes += sl.len;
1070 memcpy(&ns->lastmap,newmap,sizeof(struct Map));
1071 Send_With_Handling(ns, &sl);
1072 ns->sent_scroll = 0;
1073 }
1074 free(sl.buf);
1075 }
1076
1077
1078 /** Clears a map cell */
1079 static void map_clearcell(struct MapCell *cell, int face0, int face1, int face2, int count)
1080 {
1081 cell->count=count;
1082 cell->faces[0] = face0;
1083 cell->faces[1] = face1;
1084 cell->faces[2] = face2;
1085 }
1086
1087 #define MAX_HEAD_POS MAX(MAX_CLIENT_X, MAX_CLIENT_Y)
1088 #define MAX_LAYERS 3
1089
1090 /* Using a global really isn't a good approach, but saves the over head of
1091 * allocating and deallocating such a block of data each time run through,
1092 * and saves the space of allocating this in the socket object when we only
1093 * need it for this cycle. If the serve is ever threaded, this needs to be
1094 * re-examined.
1095 */
1096
1097 static object *heads[MAX_HEAD_POS * MAX_HEAD_POS * MAX_LAYERS];
1098
1099 /**
1100 * Returns true if any of the heads for this
1101 * space is set. Returns false if all are blank - this is used
1102 * for empty space checking.
1103 */
1104 static inline int have_head(int ax, int ay) {
1105
1106 if (heads[(ay * MAX_HEAD_POS + ax) * MAX_LAYERS] ||
1107 heads[(ay * MAX_HEAD_POS + ax) * MAX_LAYERS + 1] ||
1108 heads[(ay * MAX_HEAD_POS + ax) * MAX_LAYERS + 2]) return 1;
1109 return 0;
1110 }
1111
1112 /**
1113 * check_head is a bit simplistic version of update_space below.
1114 * basically, it only checks the that the head on space ax,ay at layer
1115 * needs to get sent - if so, it adds the data, sending the head
1116 * if needed, and returning 1. If this no data needs to get
1117 * sent, it returns zero.
1118 */
1119 static inline int check_head(SockList *sl, NewSocket *ns, int ax, int ay, int layer)
1120 {
1121 short face_num;
1122
1123 if (heads[(ay * MAX_HEAD_POS + ax) * MAX_LAYERS + layer])
1124 face_num = heads[(ay * MAX_HEAD_POS + ax) * MAX_LAYERS + layer]->face->number;
1125 else
1126 face_num = 0;
1127
1128 if (face_num != ns->lastmap.cells[ax][ay].faces[layer]) {
1129 SockList_AddShort(sl, face_num);
1130 if (face_num && !(ns->faces_sent[face_num] & NS_FACESENT_FACE))
1131 esrv_send_face(ns, face_num, 0);
1132 heads[(ay * MAX_HEAD_POS + ax) * MAX_LAYERS + layer] = NULL;
1133 ns->lastmap.cells[ax][ay].faces[layer] = face_num;
1134 return 1;
1135 }
1136
1137 return 0; /* No change */
1138 }
1139
1140 /**
1141 * Removes the need to replicate the same code for each layer.
1142 * this returns true if this space is now in fact different than
1143 * it was.
1144 * sl is the socklist this data is going into.
1145 * ns is the socket we are working on - all the info we care
1146 * about is in this socket structure, so now need not pass the
1147 * entire player object.
1148 * mx and my are map coordinate offsets for map mp
1149 * sx and sy are the offsets into the socket structure that
1150 * holds the old values.
1151 * layer is the layer to update, with 2 being the floor and 0 the
1152 * top layer (this matches what the GET_MAP_FACE and GET_MAP_FACE_OBJ)
1153 * take. Interesting to note that before this function, the map1 function
1154 * numbers the spaces differently - I think this was a leftover from
1155 * the map command, where the faces stack up. Sinces that is no longer
1156 * the case, it seems to make more sense to have these layer values
1157 * actually match.
1158 */
1159
1160 static inline int update_space(SockList *sl, NewSocket *ns, mapstruct *mp, int mx, int my, int sx, int sy, int layer)
1161 {
1162 object *ob, *head;
1163 uint16 face_num;
1164 int bx, by,i;
1165
1166 /* If there is a multipart object stored away, treat that as more important.
1167 * If not, then do the normal processing.
1168 */
1169
1170 head = heads[(sy * MAX_HEAD_POS + sx) * MAX_LAYERS + layer];
1171
1172 /* Check to see if this head is part of the set of objects
1173 * we would normally send for this space. If so, then
1174 * don't use the head value. We need to do the check
1175 * here and not when setting up the heads[] value for two reasons -
1176 * 1) the heads[] values will get used even if the space is not visible.
1177 * 2) its possible the head is not on the same map as a part, and I'd
1178 * rather not need to do the map translation overhead.
1179 * 3) We need to do some extra checking to make sure that we will
1180 * otherwise send the image as this layer, eg, either it matches
1181 * the head value, or is not multipart.
1182 */
1183 if (head && !head->more) {
1184 for (i=0; i<MAP_LAYERS; i++) {
1185 ob = GET_MAP_FACE_OBJ(mp, mx, my, i);
1186 if (!ob) continue;
1187
1188 if (ob->head) ob=ob->head;
1189
1190 if (ob == head) {
1191 heads[(sy * MAX_HEAD_POS + sx) * MAX_LAYERS + layer] = NULL;
1192 head = NULL;
1193 break;
1194 }
1195 }
1196 }
1197
1198 ob = head;
1199 if (!ob) ob = GET_MAP_FACE_OBJ(mp, mx, my, layer);
1200
1201 /* If there is no object for this space, or if the face for the object
1202 * is the blank face, set the face number to zero.
1203 * else if we have the stored head object for this space, that takes
1204 * precedence over the other object for this space.
1205 * otherwise, we do special head processing
1206 */
1207 if (!ob || ob->face == blank_face) face_num=0;
1208 else if (head){
1209 /* if this is a head that had previously been stored */
1210 face_num = ob->face->number;
1211 } else {
1212 /* if the faces for the different parts of a multipart object
1213 * are the same, we only want to send the bottom right most
1214 * portion of the object. That info is in the tail_.. values
1215 * of the head. Note that for the head itself, ob->head will
1216 * be null, so we only do this block if we are working on
1217 * a tail piece.
1218 */
1219
1220 /* tail_x and tail_y will only be set in the head object. If
1221 * this is the head object and these are set, we proceed
1222 * with logic to only send bottom right. Similarly, if
1223 * this is one of the more parts but the head has those values
1224 * set, we want to do the processing. There can be cases where
1225 * the head is not visible but one of its parts is, so we just
1226 * can always expect that ob->arch->tail_x will be true for all
1227 * object we may want to display.
1228 */
1229 if ((ob->arch->tail_x || ob->arch->tail_y) ||
1230 (ob->head && (ob->head->arch->tail_x || ob->head->arch->tail_y))) {
1231
1232 if (ob->head) head = ob->head;
1233 else head = ob;
1234
1235 /* Basically figure out where the offset is from where we are right
1236 * now. the ob->arch->clone.{x,y} values hold the offset that this current
1237 * piece is from the head, and the tail is where the tail is from the
1238 * head. Note that bx and by will equal sx and sy if we are already working
1239 * on the bottom right corner. If ob is the head, the clone values
1240 * will be zero, so the right thing will still happen.
1241 */
1242 bx = sx + head->arch->tail_x - ob->arch->clone.x;
1243 by = sy + head->arch->tail_y - ob->arch->clone.y;
1244
1245 /* I don't think this can ever happen, but better to check for it just
1246 * in case.
1247 */
1248 if (bx < sx || by < sy) {
1249 LOG(llevError,"update_space: bx (%d) or by (%d) is less than sx (%d) or sy (%d)\n",
1250 bx, by, sx, sy);
1251 face_num = 0;
1252 }
1253 /* single part object, multipart object with non merged faces,
1254 * of multipart object already at lower right.
1255 */
1256 else if (bx == sx && by == sy) {
1257 face_num = ob->face->number;
1258
1259 /* if this face matches one stored away, clear that one away.
1260 * this code relies on the fact that the map1 commands
1261 * goes from 2 down to 0.
1262 */
1263 for (i=0; i<MAP_LAYERS; i++)
1264 if (heads[(sy * MAX_HEAD_POS + sx) * MAX_LAYERS + i] &&
1265 heads[(sy * MAX_HEAD_POS + sx) * MAX_LAYERS + i]->face->number == face_num)
1266 heads[(sy * MAX_HEAD_POS + sx) * MAX_LAYERS + i] = NULL;
1267 }
1268 else {
1269 /* If this head is stored away, clear it - otherwise,
1270 * there can be cases where a object is on multiple layers -
1271 * we only want to send it once.
1272 */
1273 face_num = head->face->number;
1274 for (i=0; i<MAP_LAYERS; i++)
1275 if (heads[(by * MAX_HEAD_POS + bx) * MAX_LAYERS + i] &&
1276 heads[(by * MAX_HEAD_POS + bx) * MAX_LAYERS + i]->face->number == face_num)
1277 heads[(by * MAX_HEAD_POS + bx) * MAX_LAYERS + i] = NULL;
1278
1279 /* First, try to put the new head on the same layer. If that is used up,
1280 * then find another layer.
1281 */
1282 if (heads[(by * MAX_HEAD_POS + bx) * MAX_LAYERS + layer] == NULL) {
1283 heads[(by * MAX_HEAD_POS + bx) * MAX_LAYERS + layer] = head;
1284 } else for (i=0; i<MAX_LAYERS; i++) {
1285 if (heads[(by * MAX_HEAD_POS + bx) * MAX_LAYERS + i] == NULL ||
1286 heads[(by * MAX_HEAD_POS + bx) * MAX_LAYERS + i] == head) {
1287 heads[(by * MAX_HEAD_POS + bx) * MAX_LAYERS + i] = head;
1288 }
1289 }
1290 face_num = 0; /* Don't send this object - we'll send the head later */
1291 }
1292 } else {
1293 /* In this case, we are already at the lower right or single part object,
1294 * so nothing special
1295 */
1296 face_num = ob->face->number;
1297
1298 /* clear out any head entries that have the same face as this one */
1299 for (bx=0; bx<layer; bx++)
1300 if (heads[(sy * MAX_HEAD_POS + sx) * MAX_LAYERS + bx] &&
1301 heads[(sy * MAX_HEAD_POS + sx) * MAX_LAYERS + bx]->face->number == face_num)
1302 heads[(sy * MAX_HEAD_POS + sx) * MAX_LAYERS + bx] = NULL;
1303 }
1304 } /* else not already head object or blank face */
1305
1306 /* This is a real hack. Basically, if we have nothing to send for this layer,
1307 * but there is a head on the next layer, send that instead.
1308 * Without this, what happens is you can get the case where the player stands
1309 * on the same space as the head. However, if you have overlapping big objects
1310 * of the same type, what happens then is it doesn't think it needs to send
1311 * This tends to make stacking also work/look better.
1312 */
1313 if (!face_num && layer > 0 && heads[(sy * MAX_HEAD_POS + sx) * MAX_LAYERS + layer -1]) {
1314 face_num = heads[(sy * MAX_HEAD_POS + sx) * MAX_LAYERS + layer -1]->face->number;
1315 heads[(sy * MAX_HEAD_POS + sx) * MAX_LAYERS + layer -1] = NULL;
1316 }
1317
1318 /* Another hack - because of heads and whatnot, this face may match one
1319 * we already sent for a lower layer. In that case, don't send
1320 * this one.
1321 */
1322 if (face_num && layer+1<MAP_LAYERS && ns->lastmap.cells[sx][sy].faces[layer+1] == face_num) {
1323 face_num = 0;
1324 }
1325
1326 /* We've gotten what face we want to use for the object. Now see if
1327 * if it has changed since we last sent it to the client.
1328 */
1329 if (ns->lastmap.cells[sx][sy].faces[layer] != face_num) {
1330 ns->lastmap.cells[sx][sy].faces[layer] = face_num;
1331 if (!(ns->faces_sent[face_num] & NS_FACESENT_FACE))
1332 esrv_send_face(ns, face_num, 0);
1333 SockList_AddShort(sl, face_num);
1334 return 1;
1335 }
1336 /* Nothing changed */
1337 return 0;
1338 }
1339
1340 /**
1341 * This function is mainly a copy of update_space,
1342 * except it handles update of the smoothing updates,
1343 * not the face updates.
1344 * Removes the need to replicate the same code for each layer.
1345 * this returns true if this smooth is now in fact different
1346 * than it was.
1347 * sl is the socklist this data is going into.
1348 * ns is the socket we are working on - all the info we care
1349 * about is in this socket structure, so know need to pass the
1350 * entire player object.
1351 * mx and my are map coordinate offsets for map mp
1352 * sx and sy are the offsets into the socket structure that
1353 * holds the old values.
1354 * layer is the layer to update, with 2 being the floor and 0 the
1355 * top layer (this matches what the GET_MAP_FACE and GET_MAP_FACE_OBJ
1356 * take.
1357 */
1358
1359 static inline int update_smooth(SockList *sl, NewSocket *ns, mapstruct *mp, int mx, int my, int sx, int sy, int layer)
1360 {
1361 object *ob;
1362 int smoothlevel; /* old face_num;*/
1363
1364 ob = GET_MAP_FACE_OBJ(mp, mx, my, layer);
1365
1366 /* If there is no object for this space, or if the face for the object
1367 * is the blank face, set the smoothlevel to zero.
1368 */
1369 if (!ob || ob->face == blank_face || MAP_NOSMOOTH(mp)) smoothlevel=0;
1370 else {
1371 smoothlevel = ob->smoothlevel;
1372 if (smoothlevel && !(ns->faces_sent[ob->face->number] & NS_FACESENT_SMOOTH))
1373 SendSmooth(ns, ob->face->number);
1374 } /* else not already head object or blank face */
1375
1376 /* We've gotten what face we want to use for the object. Now see if
1377 * if it has changed since we last sent it to the client.
1378 */
1379 if (smoothlevel>255)
1380 smoothlevel=255;
1381 else if (smoothlevel<0)
1382 smoothlevel=0;
1383 if (ns->lastmap.cells[sx][sy].smooth[layer] != smoothlevel) {
1384 ns->lastmap.cells[sx][sy].smooth[layer] = smoothlevel;
1385 SockList_AddChar(sl, (uint8) (smoothlevel&0xFF));
1386 return 1;
1387 }
1388 /* Nothing changed */
1389 return 0;
1390 }
1391
1392 /**
1393 * Returns the size of a data for a map square as returned by
1394 * mapextended. There are CLIENTMAPX*CLIENTMAPY*LAYERS entries
1395 * available.
1396 */
1397 int getExtendedMapInfoSize(NewSocket* ns){
1398 int result=0;
1399 if (ns->ext_mapinfos){
1400 if (ns->EMI_smooth)
1401 result+=1; /*One byte for smoothlevel*/
1402 }
1403 return result;
1404 }
1405 /**
1406 * This function uses the new map1 protocol command to send the map
1407 * to the client. It is necessary because the old map command supports
1408 * a maximum map size of 15x15.
1409 * This function is much simpler than the old one. This is because
1410 * the old function optimized to send as few face identifiers as possible,
1411 * at the expense of sending more coordinate location (coordinates were
1412 * only 1 byte, faces 2 bytes, so this was a worthwhile savings). Since
1413 * we need 2 bytes for coordinates and 2 bytes for faces, such a trade off
1414 * maps no sense. Instead, we actually really only use 12 bits for coordinates,
1415 * and use the other 4 bits for other informatiion. For full documentation
1416 * of what we send, see the doc/Protocol file.
1417 * I will describe internally what we do:
1418 * the socket->lastmap shows how the map last looked when sent to the client.
1419 * in the lastmap structure, there is a cells array, which is set to the
1420 * maximum viewable size (As set in config.h).
1421 * in the cells, there are faces and a count value.
1422 * we use the count value to hold the darkness value. If -1, then this space
1423 * is not viewable.
1424 * we use faces[0] faces[1] faces[2] to hold what the three layers
1425 * look like.
1426 */
1427 void draw_client_map1(object *pl)
1428 {
1429 int x,y,ax, ay, d, startlen, max_x, max_y, oldlen;
1430 sint16 nx, ny;
1431 int estartlen, eoldlen;
1432 SockList sl;
1433 SockList esl; /*For extended Map info*/
1434 uint16 mask,emask;
1435 uint8 eentrysize;
1436 uint16 ewhatstart,ewhatflag;
1437 uint8 extendedinfos;
1438 mapstruct *m;
1439
1440 sl.buf=malloc(MAXSOCKBUF);
1441 if (pl->contr->socket.mapmode == Map1Cmd)
1442 strcpy((char*)sl.buf,"map1 ");
1443 else
1444 strcpy((char*)sl.buf,"map1a ");
1445 sl.len=strlen((char*)sl.buf);
1446 startlen = sl.len;
1447 /*Extendedmapinfo structure initialisation*/
1448 if (pl->contr->socket.ext_mapinfos){
1449 esl.buf=malloc(MAXSOCKBUF);
1450 strcpy((char*)esl.buf,"mapextended ");
1451 esl.len=strlen((char*)esl.buf);
1452 extendedinfos=EMI_NOREDRAW;
1453 if (pl->contr->socket.EMI_smooth)
1454 extendedinfos|=EMI_SMOOTH;
1455 ewhatstart=esl.len;
1456 ewhatflag=extendedinfos; /*The EMI_NOREDRAW bit
1457 could need to be taken away*/
1458 SockList_AddChar(&esl, extendedinfos);
1459 eentrysize=getExtendedMapInfoSize(&(pl->contr->socket));
1460 SockList_AddChar(&esl, eentrysize);
1461 estartlen = esl.len;
1462 } else {
1463 /* suppress compiler warnings */
1464 ewhatstart = 0;
1465 ewhatflag = 0;
1466 estartlen = 0;
1467 }
1468 /* Init data to zero */
1469 memset(heads, 0, sizeof(object *) * MAX_HEAD_POS * MAX_HEAD_POS * MAX_LAYERS);
1470
1471 /* x,y are the real map locations. ax, ay are viewport relative
1472 * locations.
1473 */
1474 ay=0;
1475
1476 /* We could do this logic as conditionals in the if statement,
1477 * but that started to get a bit messy to look at.
1478 */
1479 max_x = pl->x+(pl->contr->socket.mapx+1)/2;
1480 max_y = pl->y+(pl->contr->socket.mapy+1)/2;
1481 if (pl->contr->socket.mapmode == Map1aCmd) {
1482 max_x += MAX_HEAD_OFFSET;
1483 max_y += MAX_HEAD_OFFSET;
1484 }
1485
1486 for(y=pl->y-pl->contr->socket.mapy/2; y<max_y; y++,ay++) {
1487 ax=0;
1488 for(x=pl->x-pl->contr->socket.mapx/2;x<max_x;x++,ax++) {
1489
1490 mask = (ax & 0x3f) << 10 | (ay & 0x3f) << 4;
1491 emask = (ax & 0x3f) << 10 | (ay & 0x3f) << 4;
1492
1493 /* If this space is out of the normal viewable area, we only check
1494 * the heads value ax or ay will only be greater than what
1495 * the client wants if using the map1a command - this is because
1496 * if the map1a command is not used, max_x and max_y will be
1497 * set to lower values.
1498 */
1499 if (ax >= pl->contr->socket.mapx || ay >= pl->contr->socket.mapy) {
1500 int i, got_one;
1501
1502 oldlen = sl.len;
1503
1504
1505 SockList_AddShort(&sl, mask);
1506
1507 if (check_head(&sl, &pl->contr->socket, ax, ay, 2))
1508 mask |= 0x4;
1509 if (check_head(&sl, &pl->contr->socket, ax, ay, 1))
1510 mask |= 0x2;
1511 if (check_head(&sl, &pl->contr->socket, ax, ay, 0))
1512 mask |= 0x1;
1513
1514 /* If all we are doing is sending 0 (blank) faces, we don't
1515 * actually need to send that - just the coordinates
1516 * with no faces tells the client to blank out the
1517 * space.
1518 */
1519 got_one=0;
1520 for (i=oldlen+2; i<sl.len; i++) {
1521 if (sl.buf[i]) got_one=1;
1522 }
1523
1524 if (got_one && (mask & 0xf)) {
1525 sl.buf[oldlen+1] = mask & 0xff;
1526 } else { /*either all faces blank, either no face at all*/
1527 if (mask & 0xf) /*at least 1 face, we know it's blank, only send coordinates*/
1528 sl.len = oldlen + 2;
1529 else
1530 sl.len = oldlen;
1531 }
1532 /*What concerns extendinfos, nothing to be done for now
1533 * (perhaps effects layer later)
1534 */
1535 continue; /* don't do processing below */
1536 }
1537
1538 d = pl->contr->blocked_los[ax][ay];
1539
1540 /* If the coordinates are not valid, or it is too dark to see,
1541 * we tell the client as such
1542 */
1543 nx=x;
1544 ny=y;
1545 m = get_map_from_coord(pl->map, &nx, &ny);
1546 if (!m) {
1547 /* space is out of map. Update space and clear values
1548 * if this hasn't already been done. If the space is out
1549 * of the map, it shouldn't have a head
1550 */
1551 if (pl->contr->socket.lastmap.cells[ax][ay].count != -1) {
1552 SockList_AddShort(&sl, mask);
1553 map_clearcell(&pl->contr->socket.lastmap.cells[ax][ay],0,0,0,-1);
1554 }
1555 } else if (d>3) {
1556 int need_send=0, count;
1557 /* This block deals with spaces that are not visible for whatever
1558 * reason. Still may need to send the head for this space.
1559 */
1560
1561 oldlen = sl.len;
1562 #if 0
1563 /* First thing we do is blank out this space (clear it)
1564 * if not already done. If the client is using darkness, and
1565 * this space is at the edge, we also include the darkness.
1566 */
1567 if (d==4) {
1568 if (pl->contr->socket.darkness && pl->contr->socket.lastmap.cells[ax][ay].count != d) {
1569 mask |= 8;
1570 SockList_AddShort(&sl, mask);
1571 SockList_AddChar(&sl, 0);
1572 }
1573 count = d;
1574 } else
1575 #endif
1576 {
1577 SockList_AddShort(&sl, mask);
1578 if (pl->contr->socket.lastmap.cells[ax][ay].count != -1) need_send=1;
1579 count = -1;
1580 }
1581
1582 if (pl->contr->socket.mapmode == Map1aCmd && have_head(ax, ay)) {
1583 /* Now check to see if any heads need to be sent */
1584
1585 if (check_head(&sl, &pl->contr->socket, ax, ay, 2))
1586 mask |= 0x4;
1587 if (check_head(&sl, &pl->contr->socket, ax, ay, 1))
1588 mask |= 0x2;
1589 if (check_head(&sl, &pl->contr->socket, ax, ay, 0))
1590 mask |= 0x1;
1591 pl->contr->socket.lastmap.cells[ax][ay].count = count;
1592
1593 } else {
1594 struct MapCell *cell = &pl->contr->socket.lastmap.cells[ax][ay];
1595 /* properly clear a previously sent big face */
1596 if(cell->faces[0] != 0
1597 || cell->faces[1] != 0
1598 || cell->faces[2] != 0)
1599 need_send = 1;
1600 map_clearcell(&pl->contr->socket.lastmap.cells[ax][ay], 0, 0, 0, count);
1601 }
1602
1603 if ((mask & 0xf) || need_send) {
1604 sl.buf[oldlen+1] = mask & 0xff;
1605 } else {
1606 sl.len = oldlen;
1607 }
1608 } else {
1609 /* In this block, the space is visible or there are head objects
1610 * we need to send.
1611 */
1612
1613 /* Rather than try to figure out what everything that we might
1614 * need to send is, then form the packet after that,
1615 * we presume that we will in fact form a packet, and update
1616 * the bits by what we do actually send. If we send nothing,
1617 * we just back out sl.len to the old value, and no harm
1618 * is done.
1619 * I think this is simpler than doing a bunch of checks to see
1620 * what if anything we need to send, setting the bits, then
1621 * doing those checks again to add the real data.
1622 */
1623 oldlen = sl.len;
1624 mask = (ax & 0x3f) << 10 | (ay & 0x3f) << 4;
1625 eoldlen = esl.len;
1626 emask = (ax & 0x3f) << 10 | (ay & 0x3f) << 4;
1627 SockList_AddShort(&sl, mask);
1628
1629 if (pl->contr->socket.ext_mapinfos)
1630 SockList_AddShort(&esl, emask);
1631
1632 /* Darkness changed */
1633 if (pl->contr->socket.lastmap.cells[ax][ay].count != d && pl->contr->socket.darkness) {
1634 pl->contr->socket.lastmap.cells[ax][ay].count = d;
1635 mask |= 0x8; /* darkness bit */
1636
1637 /* Protocol defines 255 full bright, 0 full dark.
1638 * We currently don't have that many darkness ranges,
1639 * so we current what limited values we do have.
1640 */
1641 if (d==0) SockList_AddChar(&sl, 255);
1642 else if (d==1) SockList_AddChar(&sl, 191);
1643 else if (d==2) SockList_AddChar(&sl, 127);
1644 else if (d==3) SockList_AddChar(&sl, 63);
1645 }
1646 else {
1647 /* need to reset from -1 so that if it does become blocked again,
1648 * the code that deals with that can detect that it needs to tell
1649 * the client that this space is now blocked.
1650 */
1651 pl->contr->socket.lastmap.cells[ax][ay].count = d;
1652 }
1653
1654 /* Floor face */
1655 if (update_space(&sl, &pl->contr->socket, m, nx, ny, ax, ay, 2))
1656 mask |= 0x4;
1657
1658 if (pl->contr->socket.EMI_smooth)
1659 if (update_smooth(&esl, &pl->contr->socket, m, nx, ny, ax, ay, 2)){
1660 emask |= 0x4;
1661 }
1662
1663 /* Middle face */
1664 if (update_space(&sl, &pl->contr->socket, m, nx, ny, ax, ay, 1))
1665 mask |= 0x2;
1666
1667 if (pl->contr->socket.EMI_smooth)
1668 if (update_smooth(&esl, &pl->contr->socket, m, nx, ny, ax, ay, 1)){
1669 emask |= 0x2;
1670 }
1671
1672
1673 if(nx == pl->x && ny == pl->y && pl->invisible & (pl->invisible < 50 ? 4 : 1)) {
1674 if (pl->contr->socket.lastmap.cells[ax][ay].faces[0] != pl->face->number) {
1675 pl->contr->socket.lastmap.cells[ax][ay].faces[0] = pl->face->number;
1676 mask |= 0x1;
1677 if (!(pl->contr->socket.faces_sent[pl->face->number] &NS_FACESENT_FACE))
1678 esrv_send_face(&pl->contr->socket, pl->face->number, 0);
1679 SockList_AddShort(&sl, pl->face->number);
1680 }
1681 }
1682 /* Top face */
1683 else {
1684 if (update_space(&sl, &pl->contr->socket, m, nx, ny, ax, ay, 0))
1685 mask |= 0x1;
1686 if (pl->contr->socket.EMI_smooth)
1687 if (update_smooth(&esl, &pl->contr->socket, m, nx, ny, ax, ay, 0)){
1688 emask |= 0x1;
1689 }
1690 }
1691 /* Check to see if we are in fact sending anything for this
1692 * space by checking the mask. If so, update the mask.
1693 * if not, reset the len to that from before adding the mask
1694 * value, so we don't send those bits.
1695 */
1696 if (mask & 0xf) {
1697 sl.buf[oldlen+1] = mask & 0xff;
1698 } else {
1699 sl.len = oldlen;
1700 }
1701 if (emask & 0xf) {
1702 esl.buf[eoldlen+1] = emask & 0xff;
1703 } else {
1704 esl.len = eoldlen;
1705 }
1706 } /* else this is a viewable space */
1707 } /* for x loop */
1708 } /* for y loop */
1709
1710 /* Verify that we in fact do need to send this */
1711 if (pl->contr->socket.ext_mapinfos){
1712 if (!(sl.len>startlen || pl->contr->socket.sent_scroll)){
1713 /* No map data will follow, so don't say the client
1714 * it doesn't need draw!
1715 */
1716 ewhatflag&=(~EMI_NOREDRAW);
1717 esl.buf[ewhatstart+1] = ewhatflag & 0xff;
1718 }
1719 if (esl.len>estartlen) {
1720 Send_With_Handling(&pl->contr->socket, &esl);
1721 }
1722 free(esl.buf);
1723 }
1724 if (sl.len>startlen || pl->contr->socket.sent_scroll) {
1725 Send_With_Handling(&pl->contr->socket, &sl);
1726 pl->contr->socket.sent_scroll = 0;
1727 }
1728 free(sl.buf);
1729 }
1730
1731 /**
1732 * Draws client map.
1733 */
1734 void draw_client_map(object *pl)
1735 {
1736 int i,j;
1737 sint16 ax, ay, nx, ny;/* ax and ay goes from 0 to max-size of arrays */
1738 New_Face *face,*floor;
1739 New_Face *floor2;
1740 int d, mflags;
1741 struct Map newmap;
1742 mapstruct *m, *pm;
1743
1744 if (pl->type != PLAYER) {
1745 LOG(llevError,"draw_client_map called with non player/non eric-server\n");
1746 return;
1747 }
1748
1749 if (pl->contr->transport) {
1750 pm = pl->contr->transport->map;
1751 }
1752 else
1753 pm = pl->map;
1754
1755 /* If player is just joining the game, he isn't here yet, so the map
1756 * can get swapped out. If so, don't try to send them a map. All will
1757 * be OK once they really log in.
1758 */
1759 if (pm==NULL || pm->in_memory!=MAP_IN_MEMORY) return;
1760
1761 memset(&newmap, 0, sizeof(struct Map));
1762
1763 for(j = (pl->y - pl->contr->socket.mapy/2) ; j < (pl->y + (pl->contr->socket.mapy+1)/2); j++) {
1764 for(i = (pl->x - pl->contr->socket.mapx/2) ; i < (pl->x + (pl->contr->socket.mapx+1)/2); i++) {
1765 ax=i;
1766 ay=j;
1767 m = pm;
1768 mflags = get_map_flags(m, &m, ax, ay, &ax, &ay);
1769 if (mflags & P_OUT_OF_MAP)
1770 continue;
1771 if (mflags & P_NEED_UPDATE)
1772 update_position(m, ax, ay);
1773 /* If a map is visible to the player, we don't want to swap it out
1774 * just to reload it. This should really call something like
1775 * swap_map, but this is much more efficient and 'good enough'
1776 */
1777 if (mflags & P_NEW_MAP)
1778 m->timeout = 50;
1779 }
1780 }
1781 /* do LOS after calls to update_position */
1782 if(pl->contr->do_los) {
1783 update_los(pl);
1784 pl->contr->do_los = 0;
1785 }
1786
1787 if (pl->contr->socket.mapmode == Map1Cmd || pl->contr->socket.mapmode == Map1aCmd) {
1788 /* Big maps need a different drawing mechanism to work */
1789 draw_client_map1(pl);
1790 return;
1791 }
1792
1793 if(pl->invisible & (pl->invisible < 50 ? 4 : 1)) {
1794 esrv_map_setbelow(&pl->contr->socket,pl->contr->socket.mapx/2,
1795 pl->contr->socket.mapy/2,pl->face->number,&newmap);
1796 }
1797
1798 /* j and i are the y and x coordinates of the real map (which is
1799 * basically some number of spaces around the player)
1800 * ax and ay are values from within the viewport (ie, 0, 0 is upper
1801 * left corner) and are thus disconnected from the map values.
1802 * Subtract 1 from the max values so that we properly handle cases where
1803 * player has specified an even map. Otherwise, we try to send them too
1804 * much, ie, if mapx is 10, we would try to send from -5 to 5, which is actually
1805 * 11 spaces. Now, we would send from -5 to 4, which is properly. If mapx is
1806 * odd, this still works fine.
1807 */
1808 ay=0;
1809 for(j=pl->y-pl->contr->socket.mapy/2; j<=pl->y+(pl->contr->socket.mapy-1)/2;j++, ay++) {
1810 ax=0;
1811 for(i=pl->x-pl->contr->socket.mapx/2;i<=pl->x+(pl->contr->socket.mapx-1)/2;i++, ax++) {
1812
1813 d = pl->contr->blocked_los[ax][ay];
1814 /* note the out_of_map and d>3 checks are both within the same
1815 * negation check.
1816 */
1817 nx = i;
1818 ny = j;
1819 m = get_map_from_coord(pm, &nx, &ny);
1820 if (m && d<4) {
1821 face = GET_MAP_FACE(m, nx, ny,0);
1822 floor2 = GET_MAP_FACE(m, nx, ny,1);
1823 floor = GET_MAP_FACE(m, nx, ny,2);
1824
1825 /* If all is blank, send a blank face. */
1826 if ((!face || face == blank_face) &&
1827 (!floor2 || floor2 == blank_face) &&
1828 (!floor || floor == blank_face)) {
1829 esrv_map_setbelow(&pl->contr->socket,ax,ay,
1830 blank_face->number,&newmap);
1831 } else { /* actually have something interesting */
1832 /* send the darkness mask, if any. */
1833 if (d && pl->contr->socket.darkness)
1834 esrv_map_setbelow(&pl->contr->socket,ax,ay,
1835 dark_faces[d-1]->number,&newmap);
1836
1837 if (face && face != blank_face)
1838 esrv_map_setbelow(&pl->contr->socket,ax,ay,
1839 face->number,&newmap);
1840 if (floor2 && floor2 != blank_face)
1841 esrv_map_setbelow(&pl->contr->socket,ax,ay,
1842 floor2->number,&newmap);
1843 if (floor && floor != blank_face)
1844 esrv_map_setbelow(&pl->contr->socket,ax,ay,
1845 floor->number,&newmap);
1846 }
1847 } /* Is a valid space */
1848 }
1849 }
1850 esrv_map_doneredraw(&pl->contr->socket, &newmap);
1851 }
1852
1853
1854 void esrv_map_scroll(NewSocket *ns,int dx,int dy)
1855 {
1856 struct Map newmap;
1857 int x,y, mx, my;
1858 char buf[MAXSOCKBUF];
1859
1860 sprintf(buf,"map_scroll %d %d", dx, dy);
1861 Write_String_To_Socket(ns, buf, strlen(buf));
1862
1863 /* If we are using the Map1aCmd, we may in fact send
1864 * head information that is outside the viewable map.
1865 * So set the mx,my to the max value we want to
1866 * look for. Removed code to do so - it caused extra
1867 * complexities for the client, and probably doesn't make
1868 * that much difference in bandwidth.
1869 */
1870 mx = ns->mapx;
1871 my = ns->mapy;
1872
1873 if (ns->mapmode == Map1aCmd) {
1874 mx += MAX_HEAD_OFFSET;
1875 my += MAX_HEAD_OFFSET;
1876 }
1877
1878 /* the x and y here are coordinates for the new map, i.e. if we moved
1879 * (dx,dy), newmap[x][y] = oldmap[x-dx][y-dy]. For this reason,
1880 * if the destination x or y coordinate is outside the viewable
1881 * area, we clear the values - otherwise, the old values
1882 * are preserved, and the check_head thinks it needs to clear them.
1883 */
1884 for(x=0; x<mx; x++) {
1885 for(y=0; y<my; y++) {
1886 if(x >= ns->mapx || y >= ns->mapy) {
1887 /* clear cells outside the viewable area */
1888 memset(&newmap.cells[x][y], 0, sizeof(struct MapCell));
1889 }
1890 else if ((x+dx) < 0 || (x+dx) >= ns->mapx || (y+dy) < 0 || (y + dy) >= ns->mapy) {
1891 /* clear newly visible tiles within the viewable area */
1892 memset(&(newmap.cells[x][y]), 0, sizeof(struct MapCell));
1893 }
1894 else {
1895 memcpy(&(newmap.cells[x][y]),
1896 &(ns->lastmap.cells[x+dx][y+dy]),sizeof(struct MapCell));
1897 }
1898 }
1899 }
1900
1901 memcpy(&(ns->lastmap), &newmap,sizeof(struct Map));
1902
1903 /* Make sure that the next "map1" command will be sent (even if it is
1904 * empty).
1905 */
1906 ns->sent_scroll = 1;
1907 }
1908
1909 /*****************************************************************************/
1910 /* GROS: The following one is used to allow a plugin to send a generic cmd to*/
1911 /* a player. Of course, the client need to know the command to be able to */
1912 /* manage it ! */
1913 /*****************************************************************************/
1914 void send_plugin_custom_message(object *pl, char *buf)
1915 {
1916 cs_write_string(&pl->contr->socket,buf,strlen(buf));
1917 }
1918
1919 /**
1920 * This sends the skill number to name mapping. We ignore
1921 * the params - we always send the same info no matter what.
1922 */
1923 void send_skill_info(NewSocket *ns, char *params)
1924 {
1925 SockList sl;
1926 int i;
1927
1928 sl.buf = malloc(MAXSOCKBUF);
1929 strcpy((char*)sl.buf,"replyinfo skill_info\n");
1930 for (i=1; i< NUM_SKILLS; i++) {
1931 sprintf((char*)sl.buf + strlen((char*)sl.buf), "%d:%s\n", i + CS_STAT_SKILLINFO,
1932 skill_names[i]);
1933 }
1934 sl.len = strlen((char*)sl.buf);
1935 if (sl.len > MAXSOCKBUF) {
1936 LOG(llevError,"Buffer overflow in send_skill_info!\n");
1937 fatal(0);
1938 }
1939 Send_With_Handling(ns, &sl);
1940 free(sl.buf);
1941 }
1942
1943 /**
1944 * This sends the spell path to name mapping. We ignore
1945 * the params - we always send the same info no matter what.
1946 */
1947 void send_spell_paths (NewSocket *ns, char *params) {
1948 SockList sl;
1949 int i;
1950
1951 sl.buf = malloc(MAXSOCKBUF);
1952 strcpy((char*)sl.buf,"replyinfo spell_paths\n");
1953 for(i=0; i<NRSPELLPATHS; i++)
1954 sprintf((char*)sl.buf + strlen((char*)sl.buf), "%d:%s\n", 1<<i, spellpathnames[i]);
1955 sl.len = strlen((char*)sl.buf);
1956 if (sl.len > MAXSOCKBUF) {
1957 LOG(llevError,"Buffer overflow in send_spell_paths!\n");
1958 fatal(0);
1959 }
1960 Send_With_Handling(ns, &sl);
1961 free(sl.buf);
1962 }
1963
1964 /**
1965 * This looks for any spells the player may have that have changed their stats.
1966 * it then sends an updspell packet for each spell that has changed in this way
1967 */
1968 void esrv_update_spells(player *pl) {
1969 SockList sl;
1970 int flags=0;
1971 object *spell;
1972 if (!pl->socket.monitor_spells) return;
1973 for (spell=pl->ob->inv; spell!=NULL; spell=spell->below) {
1974 if (spell->type == SPELL) {
1975 /* check if we need to update it*/
1976 if (spell->last_sp != SP_level_spellpoint_cost(pl->ob, spell, SPELL_MANA)) {
1977 spell->last_sp = SP_level_spellpoint_cost(pl->ob, spell, SPELL_MANA);
1978 flags |= UPD_SP_MANA;
1979 }
1980 if (spell->last_grace != SP_level_spellpoint_cost(pl->ob, spell, SPELL_GRACE)) {
1981 spell->last_grace = SP_level_spellpoint_cost(pl->ob, spell, SPELL_GRACE);
1982 flags |= UPD_SP_GRACE;
1983 }
1984 if (spell->last_eat != spell->stats.dam+SP_level_dam_adjust(pl->ob, spell)) {
1985 spell->last_eat = spell->stats.dam+SP_level_dam_adjust(pl->ob, spell);
1986 flags |= UPD_SP_DAMAGE;
1987 }
1988 if (flags !=0) {
1989 sl.buf = malloc(MAXSOCKBUF);
1990 strcpy((char*)sl.buf,"updspell ");
1991 sl.len=strlen((char*)sl.buf);
1992 SockList_AddChar(&sl, flags);
1993 SockList_AddInt(&sl, spell->count);
1994 if (flags & UPD_SP_MANA) SockList_AddShort(&sl, spell->last_sp);
1995 if (flags & UPD_SP_GRACE) SockList_AddShort(&sl, spell->last_grace);
1996 if (flags & UPD_SP_DAMAGE) SockList_AddShort(&sl, spell->last_eat);
1997 flags = 0;
1998 Send_With_Handling(&pl->socket, &sl);
1999 free(sl.buf);
2000 }
2001 }
2002 }
2003 }
2004
2005 void esrv_remove_spell(player *pl, object *spell) {
2006 SockList sl;
2007
2008 if (!pl->socket.monitor_spells) return;
2009 if (!pl || !spell || spell->env != pl->ob) {
2010 LOG(llevError, "Invalid call to esrv_remove_spell");
2011 return;
2012 }
2013 sl.buf = malloc(MAXSOCKBUF);
2014 strcpy((char*)sl.buf,"delspell ");
2015 sl.len=strlen((char*)sl.buf);
2016 SockList_AddInt(&sl, spell->count);
2017 Send_With_Handling(&pl->socket, &sl);
2018 free(sl.buf);
2019 }
2020
2021 /* appends the spell *spell to the Socklist we will send the data to. */
2022 static void append_spell (player *pl, SockList *sl, object *spell) {
2023 int len, i, skill=0;
2024
2025 if (!(spell->name)) {
2026 LOG(llevError, "item number %d is a spell with no name.\n", spell->count);
2027 return;
2028 }
2029 SockList_AddInt(sl, spell->count);
2030 SockList_AddShort(sl, spell->level);
2031 SockList_AddShort(sl, spell->casting_time);
2032 /* store costs and damage in the object struct, to compare to later */
2033 spell->last_sp = SP_level_spellpoint_cost(pl->ob, spell, SPELL_MANA);
2034 spell->last_grace = SP_level_spellpoint_cost(pl->ob, spell, SPELL_GRACE);
2035 spell->last_eat = spell->stats.dam+SP_level_dam_adjust(pl->ob, spell);
2036 /* send the current values */
2037 SockList_AddShort(sl, spell->last_sp);
2038 SockList_AddShort(sl, spell->last_grace);
2039 SockList_AddShort(sl, spell->last_eat);
2040
2041 /* figure out which skill it uses, if it uses one */
2042 if (spell->skill) {
2043 for (i=1; i< NUM_SKILLS; i++)
2044 if (!strcmp(spell->skill, skill_names[i])) {
2045 skill = i+CS_STAT_SKILLINFO;
2046 break;
2047 }
2048 }
2049 SockList_AddChar(sl, skill);
2050
2051 SockList_AddInt(sl, spell->path_attuned);
2052 SockList_AddInt(sl, (spell->face)?spell->face->number:0);
2053
2054 len = strlen(spell->name);
2055 SockList_AddChar(sl, (char)len);
2056 memcpy(sl->buf+sl->len, spell->name, len);
2057 sl->len+=len;
2058
2059 if (!spell->msg) {
2060 SockList_AddShort(sl, 0);
2061 }
2062 else {
2063 len = strlen(spell->msg);
2064 SockList_AddShort(sl, len);
2065 memcpy(sl->buf+sl->len, spell->msg, len);
2066 sl->len+=len;
2067 }
2068 }
2069
2070 /**
2071 * This tells the client to add the spell *ob, if *ob is NULL, then add
2072 * all spells in the player's inventory.
2073 */
2074 void esrv_add_spells(player *pl, object *spell) {
2075 SockList sl;
2076 if (!pl) {
2077 LOG(llevError, "esrv_add_spells, tried to add a spell to a NULL player");
2078 return;
2079 }
2080 if (!pl->socket.monitor_spells) return;
2081 sl.buf = malloc(MAXSOCKBUF);
2082 strcpy((char*)sl.buf,"addspell ");
2083 sl.len=strlen((char*)sl.buf);
2084 if (!spell) {
2085 for (spell=pl->ob->inv; spell!=NULL; spell=spell->below) {
2086 /* were we to simply keep appending data here, we could exceed
2087 * MAXSOCKBUF if the player has enough spells to add, we know that
2088 * append_spells will always append 19 data bytes, plus 4 length
2089 * bytes and 3 strings (because that is the spec) so we need to
2090 * check that the length of those 3 strings, plus the 23 bytes,
2091 * won't take us over the length limit for the socket, if it does,
2092 * we need to send what we already have, and restart packet formation
2093 */
2094 /* Seeing crashes by overflowed buffers. Quick arithemetic seems
2095 * to show add_spell is 26 bytes + 2 strings. However, the overun
2096 * is hundreds of bytes off, so correcting 22 vs 26 doesn't seem
2097 * like it will fix this
2098 */
2099 if (spell->type != SPELL) continue;
2100 if (sl.len > (MAXSOCKBUF - (26 + strlen(spell->name) +
2101 (spell->msg?strlen(spell->msg):0)))) {
2102 Send_With_Handling(&pl->socket, &sl);
2103 strcpy((char*)sl.buf,"addspell ");
2104 sl.len=strlen((char*)sl.buf);
2105 }
2106 append_spell(pl, &sl, spell);
2107 }
2108 }
2109 else if (spell->type != SPELL) {
2110 LOG(llevError, "Asked to send a non-spell object as a spell");
2111 return;
2112 }
2113 else append_spell(pl, &sl, spell);
2114 if (sl.len > MAXSOCKBUF) {
2115 LOG(llevError,"Buffer overflow in esrv_add_spells!\n");
2116 fatal(0);
2117 }
2118 /* finally, we can send the packet */
2119 Send_With_Handling(&pl->socket, &sl);
2120 free(sl.buf);
2121 }
2122