ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/socket/request.c
Revision: 1.1
Committed: Fri Feb 3 07:14:46 2006 UTC (18 years, 4 months ago) by root
Content type: text/plain
Branch: MAIN
Branch point for: UPSTREAM
Log Message:
Initial revision

File Contents

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