ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/socket/request.c
Revision: 1.2
Committed: Sat Apr 15 22:38:31 2006 UTC (18 years, 1 month ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.1: +98 -55 lines
Log Message:
implement first part of mapinfo command, make way but disable plugincmd

File Contents

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