ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/socket/request.c
Revision: 1.4
Committed: Sun Apr 16 04:36:34 2006 UTC (18 years, 1 month ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.3: +5 -4 lines
Log Message:
more complicated than i thought (of course)
- move the coordinate system of mapinfo to the client map origin
- cf sends spurious scroll commands after map changes, so only flush, but don't update, the map after newmap

File Contents

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