ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/socket/request.C
(Generate patch)

Comparing deliantra/server/socket/request.C (file contents):
Revision 1.1 by elmex, Sun Aug 13 17:16:06 2006 UTC vs.
Revision 1.12 by root, Tue Aug 29 08:01:38 2006 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines