File: commands.c

package info (click to toggle)
crossfire-client 1.9.1-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 4,952 kB
  • ctags: 2,747
  • sloc: ansic: 29,140; sh: 5,795; makefile: 226; perl: 48
file content (1343 lines) | stat: -rw-r--r-- 41,799 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
const char *rcsid_common_commands_c =
    "$Id: commands.c,v 1.39 2006/05/15 05:57:43 mwedel Exp $";
/*
    Crossfire client, a client program for the crossfire program.

    Copyright (C) 2001 Mark Wedel & Crossfire Development Team

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

    The author can be reached via e-mail to crossfire-devel@real-time.com
*/


/* Handles commands received by the server.  This does not necessarily
 * handle all commands - some might be in other files (like init.c)
 *
 * This file handles commans from the server->client.  See player.c
 * for client->server commands.
 *
 * this file contains most of the commands for the dispatch loop most of
 * the functions are self-explanatory, the pixmap/bitmap commands recieve
 * the picture, and display it.  The drawinfo command draws a string
 * in the info window, the stats command updates the local copy of the stats
 * and displays it. handle_query prompts the user for input.
 * send_reply sends off the reply for the input.
 * player command gets the player information.
 * MapScroll scrolls the map on the client by some amount
 * MapCmd displays the map either with layer packing or stack packing. 
 *   packing/unpacking is best understood by looking at the server code
 *   (server/ericserver.c)
 *   stack packing is easy, for every map entry that changed, we pack
 *   1 byte for the x/y location, 1 byte for the count, and 2 bytes per
 *   face in the stack.
 *   layer packing is harder, but I seem to remember more efficient:
 *   first we pack in a list of all map cells that changed and are now
 *   empty.  The end of this list is a 255, which is bigger that 121, the
 *   maximum packed map location.  
 *   For each changed location we also pack in a list of all the faces and
 *   X/Y coordinates by layer, where the layer is the depth in the map.
 *   This essentially takes slices through the map rather than stacks.
 *   Then for each layer, (max is MAXMAPCELLFACES, a bad name) we start
 *   packing the layer into the message.  First we pack in a face, then
 *   for each place on the layer with the same face, we pack in the x/y
 *   location.  We mark the last x/y location with the high bit on 
 *   (11*11 = 121 < 128).  We then continue on with the next face, which
 *   is why the code marks the faces as -1 if they are finished.  Finally
 *   we mark the last face in the layer again with the high bit, clearly
 *   limiting the total number of faces to 32767, the code comments it's
 *   16384, I'm not clear why, but the second bit may be used somewhere
 *   else as well.
 *   The unpacking routines basically perform the opposite operations.
 */
int mapupdatesent=0;
#include <client.h>
#include <external.h>

#include "mapdata.h"


static void get_skill_info(char *data, int len)
{
    char *cp, *nl, *sn;
    int val;

    cp = data;
    do {
	nl = strchr(cp, '\n');
	if (nl) {
	    *nl=0;
	    nl++;
	}
	sn = strchr(cp, ':');
	if (!sn) {
	    LOG (LOG_WARNING,"common::get_skill_info","corrupt line: /%s/", cp);
	    return;
	}
	*sn=0;
	sn++;
	val = atoi(cp);
	val -= CS_STAT_SKILLINFO;
	if (val < 0 || val> CS_NUM_SKILLS) {
	    LOG (LOG_WARNING,"common::get_skill_info","invalid skill number %d", val);
	    return;
	}
	if (skill_names[val]) free(skill_names[val]);
	skill_names[val] = strdup_local(sn);
	cp = nl;
    } while (cp < (data + len));
}


/* handles the response from a 'requestinfo' command.  This function doesn't
 * do much itself other than dispatch to other functions.
 */

void ReplyInfoCmd(char *buf, int len)
{
    char *cp;
    int i;

    for (i=0; i<len; i++) {
	/* Either a space or newline represents a break */
	if (*(buf+i) == ' ' || *(buf+i) == '\n' ) break;
    }
    if (i>=len) {
	/* Don't print buf, as it may contain binary data */
	LOG (LOG_WARNING,"common::ReplyInfoCmd","Never found a space in the replyinfo");
	return;
    }
    /* Null out the space and put cp beyond it */
    cp = buf + i;
    *cp++ = '\0';
    if (!strcmp(buf,"image_info")) {
	get_image_info(cp, len - i - 1);   /* located in common/image.c */
    }
    else if (!strcmp(buf,"image_sums")) {
	get_image_sums(cp, len - i - 1);   /* located in common/image.c */
    }
    else if (!strcmp(buf,"skill_info")) {
	get_skill_info(cp, len - i - 1);   /* located in common/commands.c */
    }
}
    

/* Received a response to a setup from the server.
 * This function is basically the same as the server side function - we
 * just do some different processing on the data.
 */

void SetupCmd(char *buf, int len)
{
    int s;
    char *cmd, *param;

    /* run through the cmds of setup
     * syntax is setup <cmdname1> <parameter> <cmdname2> <parameter> ...
     *
     * we send the status of the cmd back, or a FALSE is the cmd is the server unknown
     * The client then must sort this out
     */

    LOG(0,"commands.c","Get SetupCmd:: %s\n", buf);
    for(s=0;;) {
	if(s>=len)	/* ugly, but for secure...*/
	    break;

	cmd = &buf[s];

	/* find the next space, and put a null there */
	for(;buf[s] && buf[s] != ' ';s++) ;
	buf[s++]=0;
	while (buf[s] == ' ') s++;

	if(s>=len)
	    break;

	param = &buf[s];

	for(;buf[s] && buf[s] != ' ';s++) ;
	buf[s++]=0;
	while (buf[s] == ' ') s++;
		
	/* what we do with the returned data depends on what the server
	 * returns to us.  In some cases, we may fall back to other
	 * methods, just report on error, or try another setup command.
	 */
	if (!strcmp(cmd,"sound")) {
	    if (!strcmp(param,"FALSE"))
		cs_print_string(csocket.fd, "setsound %d", want_config[CONFIG_SOUND]);
	} else if (!strcmp(cmd,"mapsize")) {
	    int x, y=0;
	    char *cp,tmpbuf[MAX_BUF];

	    if (!strcasecmp(param, "false")) {
		draw_info("Server only supports standard sized maps (11x11)", NDI_RED);
		/* Do this because we may have been playing on a big server before */
		use_config[CONFIG_MAPWIDTH]=11;
		use_config[CONFIG_MAPHEIGHT]=11;
		mapdata_set_size(use_config[CONFIG_MAPWIDTH], use_config[CONFIG_MAPHEIGHT]);
		resize_map_window(use_config[CONFIG_MAPWIDTH], use_config[CONFIG_MAPHEIGHT]);
		continue;
	    }
	    x = atoi(param);
	    for (cp = param; *cp!=0; cp++)
		if (*cp == 'x' || *cp == 'X') {
		    y = atoi(cp+1);
		    break;
		}
	    /* we wanted a size larger than the server supports.  Reduce our
	     * size to server maximum, and re-sent the setup command.
	     * Update our want sizes, and also tell the player what we are doing
	     */
	    if (use_config[CONFIG_MAPWIDTH] > x || use_config[CONFIG_MAPHEIGHT] > y) {
		if (use_config[CONFIG_MAPWIDTH] > x) use_config[CONFIG_MAPWIDTH] = x;
		if (use_config[CONFIG_MAPHEIGHT] > y) use_config[CONFIG_MAPHEIGHT] = y;
		mapdata_set_size(use_config[CONFIG_MAPWIDTH], use_config[CONFIG_MAPHEIGHT]);
		cs_print_string(csocket.fd,
				"setup mapsize %dx%d", use_config[CONFIG_MAPWIDTH], use_config[CONFIG_MAPHEIGHT]);
		sprintf(tmpbuf,"Server supports a max mapsize of %d x %d - requesting a %d x %d mapsize",
			x, y, use_config[CONFIG_MAPWIDTH], use_config[CONFIG_MAPHEIGHT]);
		draw_info(tmpbuf,NDI_RED);
	    }
	    else if (use_config[CONFIG_MAPWIDTH] == x && use_config[CONFIG_MAPHEIGHT] == y) {
		mapdata_set_size(use_config[CONFIG_MAPWIDTH], use_config[CONFIG_MAPHEIGHT]);
		resize_map_window(use_config[CONFIG_MAPWIDTH], use_config[CONFIG_MAPHEIGHT]);
	    }
	    else {
		/* Our request was not bigger than what server supports, and 
		 * not the same size, so whats the problem?  Tell the user that
		 * something is wrong.
		 */
		sprintf(tmpbuf,"Unable to set mapsize on server - we wanted %d x %d, server returned %d x %d",
			use_config[CONFIG_MAPWIDTH], use_config[CONFIG_MAPHEIGHT], x, y);
		draw_info(tmpbuf,NDI_RED);
	    }
	} else if (!strcmp(cmd,"sexp") || !strcmp(cmd,"darkness") || 
		!strcmp(cmd,"newmapcmd") || !strcmp(cmd, "spellmon") ) {
	        /* this really isn't an error or bug - in fact, it is expected if
		 * the user is playing on an older server.
		 */
		if (!strcmp(param,"FALSE")) {
		    LOG (LOG_WARNING,"common::SetupCmd","Server returned FALSE on setup command %s",cmd);
#if 0
/* This should really be a callback to the gui if it needs to re-examine
 * the results here.
 */
		    if( !strcmp( cmd, "newmapcmd") && fog_of_war == TRUE) {
			fprintf( stderr, "**Warning: Fog of war is active but server does not support the newmap command\n");
		    }
#endif
		}
	} else if (!strcmp(cmd, "facecache")) {
	    if (!strcmp(param, "FALSE") && want_config[CONFIG_CACHE]) {
		SendSetFaceMode(csocket, CF_FACE_CACHE);
	    }
	    else {
		use_config[CONFIG_CACHE] = atoi(param);
	    }
	} else if (!strcmp(cmd,"faceset")) {
	    if (!strcmp(param, "FALSE")) {
		draw_info("Server does not support other image sets, will use default", NDI_RED);
		face_info.faceset=0;
	    }
	} else if (!strcmp(cmd,"map2cmd")) {
	    if (!strcmp(param,"FALSE")) {
		draw_info("Server does not support map2cmd, will try map1acmd", NDI_RED);
		cs_print_string(csocket.fd,"setup map1acmd 1");
	    }
	} else if (!strcmp(cmd,"map1acmd")) {
	    if (!strcmp(param,"FALSE")) {
		draw_info("Server does not support map1acmd, will try map1cmd", NDI_RED);
		cs_print_string(csocket.fd,"setup map1cmd 1");
	    }
	} else if (!strcmp(cmd,"map1cmd")) {
	    /* Not much we can do about someone playing on an ancient server. */
	    if (!strcmp(param,"FALSE")) {
		draw_info("Server does not support map1cmd - This server is too old to support this client!", NDI_RED);
#ifdef WIN32
		closesocket(csocket.fd);
#else
		close(csocket.fd);
#endif
		csocket.fd = -1;
	    }
	} else if (!strcmp(cmd,"itemcmd")) {
	    /* don't really care - currently, the server will just
	     * fall back to the item1 transport mode.  If more item modes
	     * are used in the future, we should probably fall back one at
	     * a time or the like.
	     */
	} else if (!strcmp(cmd,"exp64")) {
	    /* If this server does not support the new skill code,
	     * fall back to the old which uses experience categories.
	     * for that, initialize the skill names appropriately.
	     * by doing so, the actual display code can be the same
	     * for both old and new.  If the server does support
	     * new exp system, send a request to get the mapping
	     * information.
	     */
	    if (!strcmp(param,"FALSE")) {
		int i;
		for (i=0; i<MAX_SKILL; i++) {
		    if (skill_names[i]) {
			free(skill_names[i]);
			skill_names[i] = NULL;
		    }
		}
		skill_names[0] = strdup_local("agility");
		skill_names[1] = strdup_local("personality");
		skill_names[2] = strdup_local("mental");
		skill_names[3] = strdup_local("physique");
		skill_names[4] = strdup_local("magic");
		skill_names[5] = strdup_local("wisdom");
	    }
	    else {
		cs_print_string(csocket.fd,"requestinfo skill_info");
	    }
	}
	else if (!strcmp(cmd,"extendedMapInfos")) {
	    if (!strcmp(param,"FALSE")) {
		use_config[CONFIG_SMOOTH]=0;
	    } else {
		/* Request all extended infos we want
		 * Should regroup everything for easyness
		 */
		if (use_config[CONFIG_SMOOTH]){
		    cs_print_string(csocket.fd,"toggleextendedinfos smooth");
		}
	    }
	}
	else if (!strcmp(cmd,"extendedTextInfos")){
	    if (strcmp(param,"FALSE")){ /* server didn't send FALSE*/
		/* Server seems to accept extended text infos. Let's tell
		 * it what extended text info we want
		 */
		char exttext[MAX_BUF];
		TextManager* manager = firstTextManager;

		while (manager){
		    snprintf(exttext,sizeof(exttext),"toggleextendedtext %d",manager->type);
		    cs_print_string(csocket.fd,exttext);
		    manager=manager->next;
		}
	    }
	}
	else {
		LOG (LOG_INFO,"common::SetupCmd","Got setup for a command we don't understand: %s %s",
		    cmd, param);
	}
    }
}
void ExtendedInfoSetCmd (char *data, int len){
    (void)data;(void)len; /* __UNUSED__ */
    /* Do nothing for now, perhaps later add some
     * support to check what server knows.
     */
   /* commented, no waranty string data is null terminated
   draw_info("ExtendedInfoSet returned from server: ",NDI_BLACK);
   draw_info(data,NDI_BLACK);
   */
}

/* Handles when the server says we can't be added.  In reality, we need to
 * close the connection and quit out, because the client is going to close
 * us down anyways.
 */
void AddMeFail(char *data, int len)
{
    (void)data;(void)len; /* __UNUSED__ */
    LOG(1,"commands.c","addme_failed received.\n");
    return;
}

/* This is really a throwaway command - there really isn't any reason to
 * send addme_success commands.
 */
void AddMeSuccess(char *data, int len)
{
    (void)data;(void)len; /* __UNUSED__ */
    LOG(1,"commands.c","addme_success received.\n");
    return;
}

void GoodbyeCmd(char *data, int len)
{
    (void)data;(void)len; /* __UNUSED__ */
    /* This could probably be greatly improved - I am not sure if anything
     * needs to be saved here, but certainly it should be possible to
     * reconnect to the server or a different server without having to
     * rerun the client.
     */
    LOG (LOG_WARNING,"common::GoodbyeCmd","Received goodbye command from server - exiting");
    exit(0);
}

Animations animations[MAXANIM];

void AnimCmd(unsigned char *data, int len)
{
    short anum;
    int    i,j;

    anum=GetShort_String(data);
    if (anum<0 || anum > MAXANIM) {
	LOG (LOG_WARNING,"common::AnimCmd","animation number invalid: %d", anum);
	return;
    }
    animations[anum].flags=GetShort_String(data+2);
    animations[anum].num_animations = (len-4)/2;
    if (animations[anum].num_animations<1) {
	    LOG (LOG_WARNING,"common::AnimCmd","num animations invalid: %d",
		    animations[anum].num_animations);
	    return;
    }
    animations[anum].faces = malloc(sizeof(uint16)*animations[anum].num_animations);
    for (i=4,j=0; i<len; i+=2,j++)
	animations[anum].faces[j]=GetShort_String(data+i);

    if (j!=animations[anum].num_animations)
	LOG(LOG_WARNING,"common::AnimCmd",
        "Calculated animations does not equal stored animations? (%d!=%d)",
		j, animations[anum].num_animations);

    animations[anum].speed=0;
    animations[anum].speed_left=0;
    animations[anum].phase=0;

    LOG(LOG_DEBUG,"common::AnimCmd","Received animation %d, %d faces", anum, animations[anum].num_animations);
}


/* This receives the smooth mapping from the server.  Because this
 * information is reference a lot, the smoothing face is stored
 * in the pixmap data - this makes access much faster than searching
 * an array of data for the face to use.
 */

void SmoothCmd(unsigned char *data, int len){
    uint16 faceid;
    uint16 smoothing;

    /* len is unused.
     * We should check that we don't have an invalid short command.
     * Hence, the compiler warning is valid.
     */

    faceid=GetShort_String(data);
    smoothing=GetShort_String(data+2);
    addsmooth(faceid, smoothing);
}

void DrawInfoCmd(char *data, int len)
{
    int color=atoi(data);
    char *buf;

    (void)len; /* __UNUSED__ */
    buf = strchr(data, ' ');
    if (!buf) {
	LOG(LOG_WARNING,"common::DrawInfoCmd","got no data");
	buf="";
    }
    else buf++;
    if (color!=NDI_BLACK)
	draw_color_info(color, buf);
    else
	draw_info(buf,NDI_BLACK);

}
TextManager* firstTextManager = NULL;

void setTextManager(int type, ExtTextManager callback){
    TextManager* current = firstTextManager;
    while (current!=NULL){
        if (current->type == type){            
            current->callback=callback;
            return;
        }
        current = current -> next;
    }
    current = malloc(sizeof(TextManager));
    current->type = type;
    current->callback=callback;
    current->next = firstTextManager;
    firstTextManager=current;
}
static ExtTextManager getTextManager(int type) {
    TextManager* current = firstTextManager;
    while (current!=NULL){
        if (current->type == type){            
            return current->callback;
        }
        current = current -> next;
    }
    return NULL;
}
/* We must extract color, type, subtype and dispatch to callback*/
void DrawExtInfoCmd(char *data, int len)
{
    int color;
    int type, subtype;
    char *buf=data;
    int wordCount=3;
    ExtTextManager fnct;
    while(wordCount>0){
    	while (buf[0]==' ')
            buf++;
        wordCount--;
        while(buf[0]!=' ')
            if (buf[0]=='\0'){
                LOG(LOG_WARNING,
                    "common::DrawExtInfoCmd","Data is missing %d parameters %s",
                    wordCount,
                    data
                );
                return;
            } else
                buf++;
        if (buf[0]==' ')
            buf++; /*remove trailing space to send clean data to callback */
    }
    wordCount = sscanf(data,"%d %d %d",&color, &type, &subtype);
    if (wordCount!=3){
        LOG(LOG_WARNING,
            "common::DrawExtInfoCmd","Wrong parameters received. Could only parse %d out of 3 int in %s",
            wordCount,
            data);
        return;
    }
    fnct = getTextManager(type);
    if (fnct == NULL){
        LOG(LOG_WARNING,
            "common::DrawExtInfoCmd","Server send us a type %d but i can't find any callback for it",
            type);
        return;
    }
    fnct(color,type,subtype,buf);
}
void StatsCmd(unsigned char *data, int len)
{
    int i=0, c, redraw=0;
    sint64 last_exp;

    while (i<len) {
	c=data[i++];
	if (c>=CS_STAT_RESIST_START && c<=CS_STAT_RESIST_END) {
	    cpl.stats.resists[c-CS_STAT_RESIST_START]=GetShort_String(data+i);
	    i+=2;
	    cpl.stats.resist_change=1;
	} else if (c >= CS_STAT_SKILLINFO && c < (CS_STAT_SKILLINFO+CS_NUM_SKILLS)) {
	    /* We track to see if the exp has gone from 0 to some total value -
	     * we do this because the draw logic currently only draws skills where
	     * the player has exp.  We need to communicate to the draw function
	     * that it should draw all the players skills.  Using redraw is
	     * a little overkill, because a lot of the data may not be changing.
	     * OTOH, such a transition should only happen rarely, not not be a very
	     * big deal.
	     */
	    cpl.stats.skill_level[c - CS_STAT_SKILLINFO] = data[i++];
	    last_exp = cpl.stats.skill_exp[c - CS_STAT_SKILLINFO];
	    cpl.stats.skill_exp[c - CS_STAT_SKILLINFO] = GetInt64_String(data+i);
	    if (last_exp == 0 && cpl.stats.skill_exp[c - CS_STAT_SKILLINFO])
		redraw=1;
	    i += 8;
	} else {
	    switch (c) {
		case CS_STAT_HP:	cpl.stats.hp=GetShort_String(data+i); i+=2; break;
		case CS_STAT_MAXHP:	cpl.stats.maxhp=GetShort_String(data+i); i+=2; break;
		case CS_STAT_SP:	cpl.stats.sp=GetShort_String(data+i); i+=2; break;
		case CS_STAT_MAXSP:	cpl.stats.maxsp=GetShort_String(data+i); i+=2; break;
		case CS_STAT_GRACE:	cpl.stats.grace=GetShort_String(data+i); i+=2; break;
		case CS_STAT_MAXGRACE:	cpl.stats.maxgrace=GetShort_String(data+i); i+=2; break;
		case CS_STAT_STR:	cpl.stats.Str=GetShort_String(data+i); i+=2; break;
		case CS_STAT_INT:	cpl.stats.Int=GetShort_String(data+i); i+=2; break;
		case CS_STAT_POW:	cpl.stats.Pow=GetShort_String(data+i); i+=2; break;
		case CS_STAT_WIS:	cpl.stats.Wis=GetShort_String(data+i); i+=2; break;
		case CS_STAT_DEX:	cpl.stats.Dex=GetShort_String(data+i); i+=2; break;
		case CS_STAT_CON:	cpl.stats.Con=GetShort_String(data+i); i+=2; break;
		case CS_STAT_CHA:	cpl.stats.Cha=GetShort_String(data+i); i+=2; break;
		case CS_STAT_EXP:	cpl.stats.exp=GetInt_String(data+i); i+=4; break;
		case CS_STAT_EXP64:	cpl.stats.exp=GetInt64_String(data+i); i+=8; break;
		case CS_STAT_LEVEL:	cpl.stats.level=GetShort_String(data+i); i+=2; break;
		case CS_STAT_WC:	cpl.stats.wc=GetShort_String(data+i); i+=2; break;
		case CS_STAT_AC:	cpl.stats.ac=GetShort_String(data+i); i+=2; break;
		case CS_STAT_DAM:	cpl.stats.dam=GetShort_String(data+i); i+=2; break;
		case CS_STAT_ARMOUR:    cpl.stats.resists[0]=GetShort_String(data+i); i+=2; break;
		case CS_STAT_SPEED:	cpl.stats.speed=GetInt_String(data+i); i+=4; break;
		case CS_STAT_FOOD:	cpl.stats.food=GetShort_String(data+i); i+=2; break;
		case CS_STAT_WEAP_SP:	cpl.stats.weapon_sp=GetInt_String(data+i); i+=4; break;
		case CS_STAT_SPELL_ATTUNE:cpl.stats.attuned=GetInt_String(data+i); i+=4; 
						cpl.spells_updated = 1; break;
		case CS_STAT_SPELL_REPEL:cpl.stats.repelled=GetInt_String(data+i); i+=4;
						cpl.spells_updated = 1; break;
		case CS_STAT_SPELL_DENY:cpl.stats.denied=GetInt_String(data+i); i+=4;
						cpl.spells_updated = 1; break;

		case CS_STAT_FLAGS:	cpl.stats.flags=GetShort_String(data+i); i+=2; break;
		case CS_STAT_WEIGHT_LIM:set_weight_limit(cpl.stats.weight_limit=GetInt_String(data+i)); i+=4; break;

		/* Skill experience handling */
		/* We make the assumption based on current bindings in the protocol
		 * that these skip 2 values and are otherwise in order.
		 */
		case CS_STAT_SKILLEXP_AGILITY:
		case CS_STAT_SKILLEXP_PERSONAL:
		case CS_STAT_SKILLEXP_MENTAL:
		case CS_STAT_SKILLEXP_PHYSIQUE:
		case CS_STAT_SKILLEXP_MAGIC:
		case CS_STAT_SKILLEXP_WISDOM:
		    cpl.stats.skill_exp[(c-CS_STAT_SKILLEXP_START)/2] = GetInt_String(data+i);
		    i+=4;
		    break;

		case CS_STAT_SKILLEXP_AGLEVEL:
		case CS_STAT_SKILLEXP_PELEVEL:
		case CS_STAT_SKILLEXP_MELEVEL:
		case CS_STAT_SKILLEXP_PHLEVEL:
		case CS_STAT_SKILLEXP_MALEVEL:
		case CS_STAT_SKILLEXP_WILEVEL:
		    cpl.stats.skill_level[(c-CS_STAT_SKILLEXP_START-1)/2] = GetShort_String(data+i);
		    i+=2;
		    break;

		case CS_STAT_RANGE: {
		    int rlen=data[i++];
		    strncpy(cpl.range,(const char*)data+i,rlen);
		    cpl.range[rlen]='\0';
		    i += rlen;
		    break;
		}
		case CS_STAT_TITLE: {
		    int rlen=data[i++];
		    strncpy(cpl.title,(const char*)data+i,rlen);
		    cpl.title[rlen]='\0';
		    i += rlen;
		    break;
		}
		default:
		    LOG(LOG_WARNING,"common::StatsCmd","Unknown stat number %d",c);
/*		    abort();*/
	    }
	}
    }

    if (i>len) {
	    LOG(LOG_WARNING,"common::StatsCmd","got stats overflow, processed %d bytes out of %d",
            i, len);
    }
    draw_stats(redraw);
    draw_message_window(0);
}

void handle_query (char *data, int len)
{
    char *buf,*cp;
    uint8 flags = atoi(data);

    (void)len; /* __UNUSED__ */

    if (flags & CS_QUERY_HIDEINPUT)		/* no echo */
	cpl.no_echo=1;
    else
	cpl.no_echo=0;

    /* Let the window system know this may have changed */
    x_set_echo();

    /* The actual text is optional */
    buf = strchr(data,' ');
    if (buf) buf++;

    /* If we just get passed an empty string, why draw this? */
    if (buf) {
	cp = buf;
	while ((buf=strchr(buf,'\n'))!=NULL) {
	    *buf++='\0';
	    draw_info(cp, NDI_BLACK);
	    cp = buf;
	}
    /* Yes/no - don't do anything with it now */
    if (flags & CS_QUERY_YESNO) {}

    /* one character response expected */
    if (flags & CS_QUERY_SINGLECHAR)
	cpl.input_state = Reply_One;
    else
	cpl.input_state = Reply_Many;

    if (cp) draw_prompt(cp);
    }

      LOG(0,"commands.c","Received query.  Input state now %d\n", cpl.input_state);
}

/* Sends a reply to the server.  text contains the null terminated
 * string of text to send.  This function basically just packs
 * the stuff up.
 */

void send_reply(const char *text)
{
    cs_print_string(csocket.fd, "reply %s", text);

    /* Let the window system know that the (possibly hidden) query is over. */
    cpl.no_echo = 0;
    x_set_echo();
}



/* This function copies relevant data from the archetype to the
 * object.  Only copies data that was not set in the object
 * structure.
 *
 */

void PlayerCmd(unsigned char *data, int len)
{
    char name[MAX_BUF];
    int tag, weight, face,i=0,nlen;

    reset_player_data();
    tag = GetInt_String(data);
    i+=4;
    weight = GetInt_String(data+i);
    i+=4;
    face= GetInt_String(data+i);
    i+=4;
    nlen=data[i++];
    memcpy(name, (const char*)data+i, nlen);
    name[nlen]='\0';
    i+= nlen;

    if (i!=len) {
	LOG(LOG_WARNING,"common::PlayerCmd","lengths do not match (%d!=%d)",
        len, i);
    }
    new_player(tag, name, weight, face);
}

void item_actions (item *op)
{
    if (!op) return;
    if (op->open) {
	open_container (op);
	cpl.container = op;
    } else if (op->was_open) {
	close_container (op);
	cpl.container=NULL;
    }
}


/* common_item_cmd parses the data send to us from the server.
 * revision is what item command the data came from - newer
 * ones have addition fields.
 */
static void common_item_command(uint8 *data, int len, int revision)
{

    int weight, loc, tag, face, flags,pos=0,nlen,anim,nrof, type;
    uint8 animspeed;
    char name[MAX_BUF];

    loc = GetInt_String(data);
    pos+=4;

    if (pos == len) {
        LOG(LOG_WARNING,"common::ItemCmd","Got location with no other data");
        return;
    }
    else if (loc < 0) { /* delete following items */
        LOG(LOG_WARNING,"common::ItemCmd","Got location with negative value (%d)", loc);
        return;
    } else {
	while (pos < len) {
	    tag=GetInt_String(data+pos); pos+=4;
	    flags = GetInt_String(data+pos); pos+=4;
	    weight = GetInt_String(data+pos); pos+=4;
	    face = GetInt_String(data+pos); pos+=4;
	    nlen = data[pos++];
	    memcpy(name, (char*)data+pos, nlen);
	    pos += nlen;
	    name[nlen]='\0';
	    anim = GetShort_String(data+pos); pos+=2;
	    animspeed = data[pos++];
	    nrof = GetInt_String(data+pos); pos+=4;
	    if (revision >= 2) {
		type = GetShort_String(data+pos); pos+=2;
	    } else
		type = NO_ITEM_TYPE;
	    update_item (tag, loc, name, weight, face, flags, anim,
			 animspeed, nrof, type);
	    item_actions (locate_item(tag));
	}
	if (pos>len) 
	    LOG(LOG_WARNING,"common::ItemCmd","Overread buffer: %d > %d", pos, len);
    }
}

/* parsing the item data is 95% the same - we just need to pass the
 * revision so that the function knows what values to extact.
 */
void Item1Cmd(unsigned char *data, int len)
{
    common_item_command(data, len, 1);
}

void Item2Cmd(unsigned char *data, int len)
{
    common_item_command(data, len, 2);
}


/* UpdateItemCmd updates some attributes of an item */
void UpdateItemCmd(unsigned char *data, int len)
{
    int weight, loc, tag, face, sendflags,flags,pos=0,nlen,anim;
	uint32 nrof;
    char name[MAX_BUF];
    item *ip,*env=NULL;
    uint8 animspeed;

    sendflags = data[0];
    pos+=1;
    tag = GetInt_String(data+pos);
    pos+=4;
    ip = locate_item(tag);
    if (!ip) {
/*
	fprintf(stderr,"Got update_item command for item we don't have (%d)\n", tag);
*/
	return;
    }
    /* Copy all of these so we can pass the values to update_item and
     * don't need to figure out which ones were modified by this function.
     */
    *name='\0';
    loc=ip->env?ip->env->tag:0;
    weight=ip->weight * 1000;
    face = ip->face;
    flags = ip->flagsval;
    anim = ip->animation_id;
    animspeed = ip->anim_speed;
    nrof = ip->nrof;

    if (sendflags & UPD_LOCATION) {
	loc=GetInt_String(data+pos);
	env=locate_item(loc);
	LOG(LOG_WARNING,"common::UpdateItemCmd","Got tag of unknown object (%d) for new location", loc);
	pos+=4;
    }
    if (sendflags & UPD_FLAGS) {
	    flags = GetInt_String(data+pos);
	    pos+=4;
    }
    if (sendflags & UPD_WEIGHT) {
	    weight = GetInt_String(data+pos);
	    pos+=4;
    }
    if (sendflags & UPD_FACE) {
	    face = GetInt_String(data+pos);
	    pos+=4;
    }
    if (sendflags & UPD_NAME) {
	    nlen = data[pos++];
	    memcpy(name, (char*)data+pos, nlen);
	    pos += nlen;
	    name[nlen]='\0';
    }
    if (pos>len)  {
	    LOG(LOG_WARNING,"common::UpdateItemCmd","Overread buffer: %d > %d", pos, len);
	    return; /* we have bad data, probably don't want to store it then */
    }
    if (sendflags & UPD_ANIM) {
	    anim = GetShort_String(data+pos);
	    pos+=2;
    }
    if (sendflags & UPD_ANIMSPEED) {
	    animspeed = data[pos++];
    }
    if (sendflags & UPD_NROF) {
	    nrof = (uint32)GetInt_String(data+pos);
	    pos+=4;
    }
    /* update_item calls set_item_values which will then set the list
     * redraw flag, so we don't need to do an explicit redraw here.  Actually,
     * calling update_item is a little bit of overkill, since we
     * already determined some of the values in this function.
     */
    update_item (tag, loc, name, weight, face, flags,anim,animspeed,nrof, ip->type);
    item_actions (locate_item(tag));
}

void DeleteItem(unsigned char *data, int len)
{
    int pos=0,tag;

    while (pos<len) {
	tag=GetInt_String(data+pos); pos+=4;
	delete_item(tag);
    }
    if (pos>len) 
	LOG(LOG_WARNING,"common::ItemCmd","Overread buffer: %d > %d", pos, len);
}

void DeleteInventory(unsigned char *data, int len)
{
    int tag;
    (void)len; /* __UNUSED__ */

    tag=atoi((const char*)data);

    if (tag<0) {
	LOG(LOG_WARNING,"common::DeleteInventory","Invalid tag: %d", tag);
	return;
    }
    remove_item_inventory(locate_item(tag));
}

/******************************************************************************
 * Start of spell commands
 *****************************************************************************/

void AddspellCmd(unsigned char *data, int len) {
    uint8 nlen;
    uint16 mlen, pos = 0;
    Spell *newspell, *tmp;
    while (pos < len) {
	newspell = calloc(1, sizeof(Spell));
	newspell->tag = GetInt_String(data+pos); pos +=4;
	newspell->level = GetShort_String(data+pos); pos +=2;
	newspell->time = GetShort_String(data+pos); pos +=2;
	newspell->sp = GetShort_String(data+pos); pos +=2;
	newspell->grace = GetShort_String(data+pos); pos +=2;
	newspell->dam = GetShort_String(data+pos); pos +=2;
	newspell->skill_number = GetChar_String(data+pos); pos +=1;
	newspell->path = GetInt_String(data+pos); pos +=4;
	newspell->face = GetInt_String(data+pos); pos +=4;
	nlen=GetChar_String(data+pos); pos +=1;
	strncpy(newspell->name, (char*)data+pos, nlen); pos+=nlen;
	newspell->name[nlen]='\0'; /* to ensure we are null terminated */
	mlen=GetShort_String(data+pos); pos +=2;
	strncpy(newspell->message, (char*)data+pos, mlen); pos+=mlen;
	newspell->message[mlen]='\0'; /* to ensure we are null terminated */
	newspell->skill = skill_names[newspell->skill_number - CS_STAT_SKILLINFO];

	/* ok, we're done with putting in data, now to add to the player struct */
	if (!cpl.spelldata) cpl.spelldata=newspell;
	else { 
	    for (tmp=cpl.spelldata;tmp->next;tmp=tmp->next); 
	    tmp->next=newspell; 
	}
	/* now we'll check to see if we have more spells */
    }
    if (pos>len) 
	LOG(LOG_WARNING,"common::AddspellCmd","Overread buffer: %d > %d", pos, len);
    cpl.spells_updated = 1;
}

void UpdspellCmd(unsigned char *data, int len) {
    int flags, tag, pos = 0;
    Spell *tmp;
    if (!cpl.spelldata) {
	LOG(LOG_WARNING,"common::UpdspellCmd","I know no spells to update");
	return;
    }
    flags = GetChar_String(data+pos); pos +=1;
    tag = GetInt_String(data+pos); pos +=4;
    for (tmp=cpl.spelldata;tmp && tmp->tag != tag;tmp=tmp->next);
    if (!tmp) {
	LOG(LOG_WARNING,"common::UpdspellCmd","Invalid tag: %d", tag);
	return;
    }
    if (flags & UPD_SP_MANA) {
	tmp->sp = GetShort_String(data+pos); pos+=2;
    }
    if (flags & UPD_SP_GRACE) {
	tmp->grace = GetShort_String(data+pos); pos+=2;
    }
    if (flags & UPD_SP_DAMAGE) {
	tmp->dam = GetShort_String(data+pos); pos+=2;
    }
    if (pos>len) 
	LOG(LOG_WARNING,"common::UpdspellCmd","Overread buffer: %d > %d", pos, len);
    cpl.spells_updated = 1;

}

void DeleteSpell(unsigned char *data, int len) {
    int tag;
    Spell *tmp, *target;
    if (!cpl.spelldata) {
	LOG(LOG_WARNING,"common::DeleteSpell","I know no spells to delete");
	return;
    }
    tag = GetInt_String(data);
    /* special case, the first spell is the one removed */
    if (cpl.spelldata->tag == tag) {
	target = cpl.spelldata;
	if (target->next) cpl.spelldata = target->next;
	else cpl.spelldata = NULL;
	free(target);
	return;
    }
    for (tmp=cpl.spelldata;tmp->next && tmp->next->tag != tag;tmp=tmp->next);
    if (!tmp->next) {
	LOG(LOG_WARNING,"common::DeleteSpell","Invalid tag: %d", tag);
	return;
    }
    target=tmp->next;
    if (target->next) tmp->next = target->next;
    else tmp->next = NULL;
    free(target);
    cpl.spells_updated = 1;
}

/******************************************************************************
 * Start of map commands
 *****************************************************************************/

void NewmapCmd(unsigned char *data, int len)
{
    (void)data;(void)len; /* __UNUSED__ */
    mapdata_newmap();
}
/* This is the common processing block for the map1 and
 * map1a protocol commands.  The map1a mieks minor extensions
 * and are easy to deal with inline (in fact, this code
 * doesn't even care what rev is - just certain bits will
 * only bet set when using the map1a command.
 * rev is 0 for map1,
 * 1 for map1a.  It conceivable that there could be future
 * revisions.
 */

/* NUM_LAYERS should only be used for the map1{a} which only
 * has a few layers.  Map2 has 10 layers.  However, some of the
 * map1 logic requires this to be set right.
 */
#define NUM_LAYERS (MAP1_LAYERS-1)
static void map1_common(unsigned char *data, int len, int rev)
{
    int mask, x, y, pos = 0, layer;
    int darkness;
    int faces[MAXLAYERS];

    map1cmd=1;
    if (!mapupdatesent) /* see MapExtendedCmd */
        display_map_startupdate();

    while (pos <len) {
	mask = GetShort_String(data+pos); pos+=2;
	x = (mask >>10) & 0x3f;
	y = (mask >>4) & 0x3f;

	/* If there are no low bits (face/darkness), this space is
	 * not visible.
	 */
	if ((mask & 0xf) == 0) {
	    mapdata_set_face(x, y, -1, -1, -1, -1);
	    continue;	/* if empty mask, none of the checks will be true. */
	}
	/* If this space was previously stored for fog of war, need to clear
	 * it now.  Needs to be done before anything else happens that we
	 * might want to store in it.
	 */

	if (mask & 0x8) { /* darkness bit */
	    darkness = data[pos++];
	}
	else {
	    darkness = -1;
	}

	/* Reduce redundant by putting the get image
	 * and flags in a common block.  The layers
	 * are the inverse of the bit order unfortunately.
	 */
	for (layer=NUM_LAYERS; layer>=0; layer--) {
	    if (mask & (1 << layer)) {
		faces[NUM_LAYERS-layer] = GetShort_String(data+pos); pos +=2;
	    }
	    else {
		faces[NUM_LAYERS-layer] = -1;
	    }
	}

	mapdata_set_face(x, y, darkness, faces[0], faces[1], faces[2]);
    }
    mapupdatesent=0;
    display_map_doneupdate(FALSE, FALSE);

}

/* These wrapper functions actually are not needed since
 * the logic above doesn't care what the revision actually
 * is.
 */

void Map1Cmd(unsigned char *data, int len)
{
    map1_common(data, len, 0);
}
void Map1aCmd(unsigned char *data, int len)
{
    map1_common(data, len, 1);
}

void Map2Cmd(unsigned char *data, int len)
{
    int mask, x, y, pos = 0, space_len, value;
    uint8 type;

    display_map_startupdate();
    /* Not really using map1 protocol, but some draw logic differs from
     * the original draw logic, and map2 is closest.
     */
    map1cmd = 1;
    while (pos <len) {
	mask = GetShort_String(data+pos); pos+=2;
	x = ((mask >>10) & 0x3f) - MAP2_COORD_OFFSET;
	y = ((mask >>4) & 0x3f) - MAP2_COORD_OFFSET;

	/* This is a scroll then.  Go back and fetch another coordinate */
	if (mask & 0x1) {
	    mapdata_scroll(x, y);
	    continue;
	}

	/* Inner loop is for the data on the space itself */
	while (pos < len) {

	    type = data[pos++];
	    /* type == 255 means nothing more for this space */
	    if (type == 255) {
		mapdata_set_check_space(x, y);
		break;
	    }
	    space_len = type >> 5;
	    type &= 0x1f;
	    /* Clear the space */
	    if (type == 0) {
		mapdata_clear_space(x, y);
		continue;
	    }
	    else if (type == 1) {
		value = data[pos++];
		mapdata_set_darkness(x, y, value);
		continue;
	    } else if (type >= 0x10 && type <= 0x1a) {
		int layer, opt;

		/* This is face information for a layer. */
		layer = type & 0xf;

		/* This is the face */
		value = GetShort_String(data+pos); pos+=2;
		if (!(value & FACE_IS_ANIM))
		    mapdata_set_face_layer(x, y, value, layer);

		if (space_len > 2) {
		    opt = data[pos++];
		    if (value & FACE_IS_ANIM) {
			/* Animation speed */
			mapdata_set_anim_layer(x, y, value, opt, layer);
		    } else {
			/* Smooth info */
			mapdata_set_smooth(x, y, opt, layer);
		    }
		}
		/* Currently, if 4 bytes, must be a smooth byte */
		if (space_len > 3) {
		    opt = data[pos++];
		    mapdata_set_smooth(x, y, opt, layer);
		}
		continue;
	    } /* if image layer */
	} /* while pos<len inner loop for space */

    } /* While pos<len outer loop */
    mapupdatesent=0;
    display_map_doneupdate(FALSE, FALSE);
}


void map_scrollCmd(char *data, int len)
{
    int dx,dy;
    char *buf;

    (void)len; /* __UNUSED__ */
    dx = atoi(data);
    buf = strchr(data,' ');
    if (!buf) {
	LOG(LOG_WARNING,"common::map_scrollCmd","Got short packet.");
	return;
    }
    buf++;
    dy = atoi(buf);

    display_map_startupdate();
    mapdata_scroll(dx, dy);
    display_map_doneupdate(FALSE, TRUE);
}

/* Extract smoothing infos from an extendedmapinfo packet part
 * data is located at the beginning of the smooth datas
 */
int ExtSmooth(unsigned char* data,int len,int x,int y,int layer){
    static int dx[8]={0,1,1,1,0,-1,-1,-1};
    static int dy[8]={-1,-1,0,1,1,1,0,-1};
    int i,rx,ry;
    int newsm;

    if (len<1)
        return 0;

    x+= pl_pos.x;
    y+= pl_pos.y;
    newsm=GetChar_String(data);

    if (the_map.cells[x][y].smooth[layer]!=newsm) {
	for (i=0;i<8;i++){
	    rx=x+dx[i];
	    ry=y+dy[i];
	    if ( (rx<0) || (ry<0) || (the_map.x<=rx) || (the_map.y<=ry))
		continue;
	    the_map.cells[x][y].need_resmooth=1;
	}
    }
    the_map.cells[x][y].smooth[layer]=newsm;
    return 1;/*Cause smooth infos only use 1 byte*/
}

/* Handle MapExtended command
 * Warning! if you add commands to extended, take
 * care that the 'layer' argument of main loop is
 * the opposite of the layer of the map.
 * so if you reference a layer, use NUM_LAYERS-layer
 */
void MapExtendedCmd(unsigned char *data, int len){
    int mask, x, y, pos=0, layer;
    int noredraw=0;
    int hassmooth=0;
    int entrysize;
    int startpackentry;
    map1cmd=1;
    if (!mapupdatesent)
        display_map_startupdate();
    mapupdatesent=1;
    mask = GetChar_String(data+pos); pos+=1;
    if (mask&EMI_NOREDRAW)
        noredraw=1;
    if (mask&EMI_SMOOTH){
        hassmooth=1;
    }
    while (mask&EMI_HASMOREBITS){
        /*There may be bits we ignore about*/
        mask = GetChar_String(data+pos); 
        pos+=1;
    }
    entrysize=GetChar_String(data+pos); 
    pos=pos+1;
    
    
    while (pos+entrysize+2 <=len) {
        mask = GetShort_String(data+pos); pos+=2;
        x = (mask >>10) & 0x3f;
        y = (mask >>4) & 0x3f;       
        for (layer=NUM_LAYERS; layer>=0; layer--) {
            if (mask & (1 << layer)) {
                /*handle an entry*/
                if (pos+entrysize>len)/*erroneous packet*/
                    break;
                startpackentry=pos;
                /* If you had extended infos to the server, this
                 * is where, in the client, you may add your code
                 */
                if (hassmooth)
                    pos=pos+ExtSmooth(data+pos,len-pos,x,y,NUM_LAYERS - layer);
                /* continue with other if you add new extended 
                 * infos to server
                 */
                 
                /* Now point to the next data */
                pos=startpackentry+entrysize; 
                
            }
        }
	}
    if (!noredraw){        
        display_map_doneupdate(FALSE, FALSE);
        mapupdatesent=0;
    }
}


void MagicMapCmd(unsigned char *data, int len)
{
    unsigned char *cp;
    int i;

    /* First, extract the size/position information. */
    if (sscanf((const char*)data,"%hd %hd %hd %hd", &cpl.mmapx, &cpl.mmapy, 
	&cpl.pmapx, &cpl.pmapy)!=4) {
	LOG(LOG_WARNING,"common::MagicMapCmd","Was not able to properly extract magic map size, pos");
	return;
    }
    /* Now we need to find the start of the actual data.  There are 4
     * space characters we need to skip over.
     */
    for (cp=data, i=0; i<4 && cp < (data+len); cp++)
	if (*cp==' ') i++;
    if (i!=4) {
	LOG(LOG_WARNING,"common::MagicMapCmd","Was unable to find start of magic map data");
	return;
    }
    i = len - (cp - data); /* This should be the number of bytes left */
    if (i != cpl.mmapx * cpl.mmapy) {
	LOG(LOG_WARNING,"common::MagicMapCmd","Magic map size mismatch.  Have %d bytes, should have %d",
		i, cpl.mmapx * cpl.mmapy);
	return;
    }
    if (cpl.magicmap) free(cpl.magicmap);
    cpl.magicmap = malloc(cpl.mmapx * cpl.mmapy);
    /* Order the server puts it in should be just fine.  Note that
     * the only requirement that this works is that magicmap by 8 bits,
     * being that is the size specified in the protocol and what the
     * server sends us.
     */
    memcpy(cpl.magicmap, cp, cpl.mmapx * cpl.mmapy);
    cpl.showmagic=1;
    draw_magic_map();
}

void SinkCmd(unsigned char *data, int len){
}

/* got a tick from the server.  We currently
 * don't care what tick number it is, but
 * just have the code in case at some time we do.
 */
void TickCmd(char *data, int len)
{

    tick = GetInt_String(data);

    /* Up to the specific client to decide what to do */
    client_tick(tick);
}