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

Comparing deliantra/server/socket/item.C (file contents):
Revision 1.13 by root, Thu Dec 14 00:13:26 2006 UTC vs.
Revision 1.19 by root, Thu Dec 14 04:30:33 2006 UTC

45/******************************************************************************* 45/*******************************************************************************
46 * 46 *
47 * Functions related to sending object data to the client. 47 * Functions related to sending object data to the client.
48 * 48 *
49 ******************************************************************************/ 49 ******************************************************************************/
50
51/**
52 * Adds string to socklist.
53 *
54 * This is a simple function that we use a lot here. It basically
55 * adds the specified buffer into the socklist, but prepends a
56 * single byte in length. If the data is longer than that byte, it is
57 * truncated approprately.
58 */
59inline void
60add_stringlen_to_sockbuf (const char *buf, SockList * sl)
61{
62 int len;
63
64 len = strlen (buf);
65 if (len > 255)
66 len = 255;
67 SockList_AddChar (sl, (char) len);
68 strncpy ((char *) sl->buf + sl->len, buf, len);
69 sl->len += len;
70}
71 50
72/** 51/**
73 * This is a similar to query_name, but returns flags 52 * This is a similar to query_name, but returns flags
74 * to be sended to client. 53 * to be sended to client.
75 */ 54 */
130 flags |= F_LOCKED; 109 flags |= F_LOCKED;
131 110
132 return flags; 111 return flags;
133} 112}
134 113
135/* Used in the send_look to put object head into SockList 114/* Used in the send_look to put object head into packet
136 * sl for socket ns. Need socket to know if we need to send 115 * sl for socket ns. Need socket to know if we need to send
137 * animation of face to the client. 116 * animation of face to the client.
138 */ 117 */
139static void 118static void
140add_object_to_socklist (NewSocket &ns, SockList &sl, object *head) 119add_object_to_socklist (client_socket &ns, packet &sl, object *head)
141{ 120{
142 int flags, len, anim_speed; 121 int flags, len, anim_speed;
143 char item_n[MAX_BUF]; 122 char item_n[MAX_BUF];
144 const char *item_p; 123 const char *item_p;
145 124
220{ 199{
221 object *tmp, *last; 200 object *tmp, *last;
222 int got_one = 0, start_look = 0, end_look = 0; 201 int got_one = 0, start_look = 0, end_look = 0;
223 char buf[MAX_BUF]; 202 char buf[MAX_BUF];
224 203
225 if (!pl->contr->socket.update_look) 204 if (!pl->contr->socket->update_look)
226 { 205 {
227 LOG (llevDebug, "esrv_draw_look called when update_look was not set\n"); 206 LOG (llevDebug, "esrv_draw_look called when update_look was not set\n");
228 return; 207 return;
229 } 208 }
230 else 209 else
231 pl->contr->socket.update_look = 0; 210 pl->contr->socket->update_look = 0;
232 211
233 if (QUERY_FLAG (pl, FLAG_REMOVED) 212 if (QUERY_FLAG (pl, FLAG_REMOVED)
234 || !pl->map 213 || !pl->map
235 || pl->map->in_memory != MAP_IN_MEMORY 214 || pl->map->in_memory != MAP_IN_MEMORY
236 || out_of_map (pl->map, pl->x, pl->y)) 215 || out_of_map (pl->map, pl->x, pl->y))
237 return; 216 return;
238 217
239 for (tmp = get_map_ob (pl->map, pl->x, pl->y); tmp && tmp->above; tmp = tmp->above); 218 for (tmp = get_map_ob (pl->map, pl->x, pl->y); tmp && tmp->above; tmp = tmp->above)
219 ;
240 220
241 SockList sl (MAXSOCKBUF); 221 packet sl;
242 222
243 Write_String_To_Socket (&pl->contr->socket, "delinv 0", sizeof ("delinv 0") - 1); 223 pl->contr->socket->send_packet ("delinv 0");
244 224
245 sl.printf ("item%d ", pl->contr->socket.itemcmd); 225 sl.printf ("item%d ", pl->contr->socket->itemcmd);
246 226
247 sl << uint32 (0); 227 sl << uint32 (0);
248 228
249 if (!(pl->contr->socket.faces_sent[empty_face->number] & NS_FACESENT_FACE)) 229 if (!(pl->contr->socket->faces_sent[empty_face->number] & NS_FACESENT_FACE))
250 esrv_send_face (&pl->contr->socket, empty_face->number, 0); 230 esrv_send_face (pl->contr->socket, empty_face->number, 0);
251 231
252 if (pl->contr->socket.look_position) 232 if (pl->contr->socket->look_position)
253 { 233 {
254 sl << uint32 (0x80000000 | (pl->contr->socket.look_position - NUM_LOOK_OBJECTS)) 234 sl << uint32 (0x80000000 | (pl->contr->socket->look_position - NUM_LOOK_OBJECTS))
255 << uint32 (0) 235 << uint32 (0)
256 << sint32 (-1) 236 << sint32 (-1)
257 << uint32 (empty_face->number); 237 << uint32 (empty_face->number);
258 238
259 sl.printf ("Click here to see %d previous items", NUM_LOOK_OBJECTS); 239 sl.printf ("Click here to see %d previous items", NUM_LOOK_OBJECTS);
260 240
261 sl << uint16 (0) 241 sl << uint16 (0)
262 << uint8 (0) 242 << uint8 (0)
263 << uint32 (0); 243 << uint32 (0);
264 244
265 if (pl->contr->socket.itemcmd == 2) 245 if (pl->contr->socket->itemcmd == 2)
266 sl << uint16 (0); 246 sl << uint16 (0);
267 } 247 }
268 248
269 for (last = NULL; tmp != last; tmp = tmp->below) 249 for (last = NULL; tmp != last; tmp = tmp->below)
270 { 250 {
277 last = last->below; 257 last = last->below;
278 } 258 }
279 259
280 if (LOOK_OBJ (tmp)) 260 if (LOOK_OBJ (tmp))
281 { 261 {
282 if (++start_look < pl->contr->socket.look_position) 262 if (++start_look < pl->contr->socket->look_position)
283 continue; 263 continue;
284 264
285 end_look++; 265 end_look++;
286 266
287 if (end_look > NUM_LOOK_OBJECTS) 267 if (end_look > NUM_LOOK_OBJECTS)
288 { 268 {
289 /* What we basically do is make a 'fake' object - when the user applies it, 269 /* What we basically do is make a 'fake' object - when the user applies it,
290 * we notice the special tag the object has, and act accordingly. 270 * we notice the special tag the object has, and act accordingly.
291 */ 271 */
292 SockList_AddInt (&sl, 0x80000000 | (pl->contr->socket.look_position + NUM_LOOK_OBJECTS)); 272 sl << uint32 (0x80000000 | (pl->contr->socket->look_position + NUM_LOOK_OBJECTS))
293 SockList_AddInt (&sl, 0); 273 << uint32 (0)
294 SockList_AddInt (&sl, (uint32) - 1); 274 << uint32 ((uint32) - 1)
295 SockList_AddInt (&sl, empty_face->number); 275 << uint32 (empty_face->number);
276
296 sprintf (buf, "Click here to see next group of items"); 277 sl.printf ("Click here to see next group of items");
297 add_stringlen_to_sockbuf (buf, &sl); 278
298 SockList_AddShort (&sl, 0); 279 sl << uint16 (0)
299 SockList_AddChar (&sl, 0); 280 << uint8 (0)
300 SockList_AddInt (&sl, 0); 281 << uint32 (0);
282
301 if (pl->contr->socket.itemcmd == 2) 283 if (pl->contr->socket->itemcmd == 2)
302 SockList_AddShort (&sl, 0); 284 sl << uint16 (0);
285
303 break; 286 break;
304 } 287 }
288
305 if (tmp->head) 289 if (tmp->head)
306 head = tmp->head; 290 head = tmp->head;
307 else 291 else
308 head = tmp; 292 head = tmp;
309 293
310 add_object_to_socklist (pl->contr->socket, sl, head); 294 add_object_to_socklist (*pl->contr->socket, sl, head);
311 got_one++; 295 got_one++;
312 296
313 if (sl.len >= (MAXSOCKBUF - MAXITEMLEN)) 297 if (sl.length () >= (MAXSOCKBUF - MAXITEMLEN))
314 { 298 {
315 Send_With_Handling (&pl->contr->socket, &sl); 299 Send_With_Handling (pl->contr->socket, &sl);
300
301 sl.reset ();
316 sprintf ((char *) sl.buf, "item%d ", pl->contr->socket.itemcmd); 302 sl.printf ("item%d ", pl->contr->socket->itemcmd);
317 sl.len = strlen ((char *) sl.buf); 303 sl << uint32 (0);
318 SockList_AddInt (&sl, 0);
319 got_one = 0; 304 got_one = 0;
320 } 305 }
321 } /* If LOOK_OBJ() */ 306 } /* If LOOK_OBJ() */
322 } 307 }
323 308
324 if (got_one) 309 if (got_one)
325 Send_With_Handling (&pl->contr->socket, &sl); 310 Send_With_Handling (pl->contr->socket, &sl);
326 311
327 sl.free ();
328} 312}
329 313
330/** 314/**
331 * Sends whole inventory. 315 * Sends whole inventory.
332 */ 316 */
333void 317void
334esrv_send_inventory (object *pl, object *op) 318esrv_send_inventory (object *pl, object *op)
335{ 319{
336 object *tmp; 320 object *tmp;
337 int got_one = 0; 321 int got_one = 0;
338 SockList sl;
339 322
340 sl.buf = (unsigned char *) malloc (MAXSOCKBUF); 323 packet sl;
341 324
342 sprintf ((char *) sl.buf, "delinv %d", op->count); 325 sl.printf ("delinv %d", op->count);
343 sl.len = strlen ((char *) sl.buf);
344 Send_With_Handling (&pl->contr->socket, &sl); 326 Send_With_Handling (pl->contr->socket, &sl);
345 327
328 sl.reset ();
346 sprintf ((char *) sl.buf, "item%d ", pl->contr->socket.itemcmd); 329 sl.printf ("item%d ", pl->contr->socket->itemcmd);
347 sl.len = strlen ((char *) sl.buf);
348 330
349 SockList_AddInt (&sl, op->count); 331 sl << uint32 (op->count);
350 332
351 for (tmp = op->inv; tmp; tmp = tmp->below) 333 for (tmp = op->inv; tmp; tmp = tmp->below)
352 { 334 {
353 object *head; 335 object *head;
354 336
357 else 339 else
358 head = tmp; 340 head = tmp;
359 341
360 if (LOOK_OBJ (head)) 342 if (LOOK_OBJ (head))
361 { 343 {
362 add_object_to_socklist (pl->contr->socket, sl, head); 344 add_object_to_socklist (*pl->contr->socket, sl, head);
363 345
364 got_one++; 346 got_one++;
365 347
366 /* IT is possible for players to accumulate a huge amount of 348 /* IT is possible for players to accumulate a huge amount of
367 * items (especially with some of the bags out there) to 349 * items (especially with some of the bags out there) to
368 * overflow the buffer. IF so, send multiple item commands. 350 * overflow the buffer. IF so, send multiple item commands.
369 */ 351 */
370 if (sl.len >= (MAXSOCKBUF - MAXITEMLEN)) 352 if (sl.length () >= (MAXSOCKBUF - MAXITEMLEN))
371 { 353 {
372 Send_With_Handling (&pl->contr->socket, &sl); 354 Send_With_Handling (pl->contr->socket, &sl);
355
356 sl.reset ();
373 sprintf ((char *) sl.buf, "item%d ", pl->contr->socket.itemcmd); 357 sl.printf ("item%d ", pl->contr->socket->itemcmd);
374 sl.len = strlen ((char *) sl.buf); 358 sl << uint32 (op->count);
375 SockList_AddInt (&sl, op->count);
376 got_one = 0; 359 got_one = 0;
377 } 360 }
378 } /* If LOOK_OBJ() */ 361 } /* If LOOK_OBJ() */
379 } 362 }
363
380 if (got_one) 364 if (got_one)
381 Send_With_Handling (&pl->contr->socket, &sl); 365 Send_With_Handling (pl->contr->socket, &sl);
382 sl.free (); 366
383} 367}
384 368
385/** 369/**
386 * Updates object *op for player *pl. 370 * Updates object *op for player *pl.
387 * 371 *
391 */ 375 */
392 376
393void 377void
394esrv_update_item (int flags, object *pl, object *op) 378esrv_update_item (int flags, object *pl, object *op)
395{ 379{
396 SockList sl;
397
398 /* If we have a request to send the player item, skip a few checks. */ 380 /* If we have a request to send the player item, skip a few checks. */
399 if (op != pl) 381 if (op != pl)
400 { 382 {
401 if (!LOOK_OBJ (op)) 383 if (!LOOK_OBJ (op))
402 return; 384 return;
413 * and track back the call. 395 * and track back the call.
414 */ 396 */
415 LOG (llevDebug, "We have not sent item %s (%d)\n", &op->name, op->count); 397 LOG (llevDebug, "We have not sent item %s (%d)\n", &op->name, op->count);
416 } 398 }
417 399
418 sl.buf = (unsigned char *) malloc (MAXSOCKBUF); 400 packet sl;
419 401
420 strcpy ((char *) sl.buf, "upditem "); 402 sl << "upditem "
421 sl.len = strlen ((char *) sl.buf); 403 << uint8 (flags);
422
423 SockList_AddChar (&sl, (char) flags);
424 404
425 if (op->head) 405 if (op->head)
426 op = op->head; 406 op = op->head;
427 407
428 SockList_AddInt (&sl, op->count); 408 sl << uint32 (op->count);
429 409
430 if (flags & UPD_LOCATION) 410 if (flags & UPD_LOCATION)
431 SockList_AddInt (&sl, op->env ? op->env->count : 0); 411 sl << uint32 (op->env ? op->env->count : 0);
432 412
433 if (flags & UPD_FLAGS) 413 if (flags & UPD_FLAGS)
434 SockList_AddInt (&sl, query_flags (op)); 414 sl << uint32 (query_flags (op));
435 415
436 if (flags & UPD_WEIGHT) 416 if (flags & UPD_WEIGHT)
437 { 417 {
438 sint32 weight = WEIGHT (op); 418 sint32 weight = WEIGHT (op);
439 419
440 SockList_AddInt (&sl, QUERY_FLAG (op, FLAG_NO_PICK) ? -1 : weight); 420 sl << uint32 (QUERY_FLAG (op, FLAG_NO_PICK) ? -1 : weight);
421
441 if (pl == op) 422 if (pl == op)
442 {
443 op->contr->last_weight = weight; 423 op->contr->last_weight = weight;
444 }
445 } 424 }
446 425
447 if (flags & UPD_FACE) 426 if (flags & UPD_FACE)
448 { 427 {
449 if (!(pl->contr->socket.faces_sent[op->face->number] & NS_FACESENT_FACE)) 428 if (!(pl->contr->socket->faces_sent[op->face->number] & NS_FACESENT_FACE))
450 esrv_send_face (&pl->contr->socket, op->face->number, 0); 429 esrv_send_face (pl->contr->socket, op->face->number, 0);
451 SockList_AddInt (&sl, op->face->number); 430
431 sl << uint32 (op->face->number);
452 } 432 }
453 433
454 if (flags & UPD_NAME) 434 if (flags & UPD_NAME)
455 { 435 {
456 int len; 436 int len;
473 } 453 }
474 454
475 strncpy (item_n + len + 1, item_p, 127); 455 strncpy (item_n + len + 1, item_p, 127);
476 item_n[254] = 0; 456 item_n[254] = 0;
477 len += strlen (item_n + 1 + len) + 1; 457 len += strlen (item_n + 1 + len) + 1;
478 SockList_AddChar (&sl, (char) len); 458
479 memcpy (sl.buf + sl.len, item_n, len); 459 sl << data8 (item_n, len);
480 sl.len += len;
481 } 460 }
482 461
483 if (flags & UPD_ANIM) 462 if (flags & UPD_ANIM)
484 SockList_AddShort (&sl, op->animation_id); 463 sl << uint16 (op->animation_id);
485 464
486 if (flags & UPD_ANIMSPEED) 465 if (flags & UPD_ANIMSPEED)
487 { 466 {
488 int anim_speed = 0; 467 int anim_speed = 0;
489 468
498 else if (FABS (op->speed) >= 1.0) 477 else if (FABS (op->speed) >= 1.0)
499 anim_speed = 1; 478 anim_speed = 1;
500 else 479 else
501 anim_speed = (int) (1.0 / FABS (op->speed)); 480 anim_speed = (int) (1.0 / FABS (op->speed));
502 } 481 }
482
503 if (anim_speed > 255) 483 if (anim_speed > 255)
504 anim_speed = 255; 484 anim_speed = 255;
505 } 485 }
506 SockList_AddChar (&sl, (char) anim_speed); 486
487 sl << uint8 (anim_speed);
507 } 488 }
489
508 if (flags & UPD_NROF) 490 if (flags & UPD_NROF)
509 SockList_AddInt (&sl, op->nrof); 491 sl << uint32 (op->nrof);
510 492
511 Send_With_Handling (&pl->contr->socket, &sl); 493 Send_With_Handling (pl->contr->socket, &sl);
512 sl.free ();
513} 494}
514 495
515/** 496/**
516 * Sends item's info to player. 497 * Sends item's info to player.
517 */ 498 */
518void 499void
519esrv_send_item (object *pl, object *op) 500esrv_send_item (object *pl, object *op)
520{ 501{
521 SockList sl;
522
523 /* If this is not the player object, do some more checks */ 502 /* If this is not the player object, do some more checks */
524 if (op != pl) 503 if (op != pl)
525 { 504 {
526 /* We only send 'visibile' objects to the client */ 505 /* We only send 'visibile' objects to the client */
527 if (!LOOK_OBJ (op)) 506 if (!LOOK_OBJ (op))
529 /* if the item is on the ground, mark that the look needs to 508 /* if the item is on the ground, mark that the look needs to
530 * be updated. 509 * be updated.
531 */ 510 */
532 if (!op->env) 511 if (!op->env)
533 { 512 {
534 pl->contr->socket.update_look = 1; 513 pl->contr->socket->update_look = 1;
535 return; 514 return;
536 } 515 }
537 } 516 }
538 517
539 sl.buf = (unsigned char *) malloc (MAXSOCKBUF); 518 packet sl;
540 519
541 sprintf ((char *) sl.buf, "item%d ", pl->contr->socket.itemcmd); 520 sl.printf ("item%d ", pl->contr->socket->itemcmd);
542 sl.len = strlen ((char *) sl.buf);
543 521
544 if (op->head) 522 if (op->head)
545 op = op->head; 523 op = op->head;
546 524
547 SockList_AddInt (&sl, op->env ? op->env->count : 0); 525 sl << uint32 (op->env ? op->env->count : 0);
548 526
549 add_object_to_socklist (pl->contr->socket, sl, op); 527 add_object_to_socklist (*pl->contr->socket, sl, op);
550 528
551 Send_With_Handling (&pl->contr->socket, &sl); 529 Send_With_Handling (pl->contr->socket, &sl);
552 SET_FLAG (op, FLAG_CLIENT_SENT); 530 SET_FLAG (op, FLAG_CLIENT_SENT);
553 531
554 sl.free ();
555} 532}
556 533
557/** 534/**
558 * Tells the client to delete an item. Uses the item 535 * Tells the client to delete an item. Uses the item
559 * command with a -1 location. 536 * command with a -1 location.
560 */ 537 */
561 538
562void 539void
563esrv_del_item (player *pl, int tag) 540esrv_del_item (player *pl, int tag)
564{ 541{
565 SockList sl; 542 packet sl;
566 543
567 sl.buf = (unsigned char *) malloc (MAXSOCKBUF); 544 sl << "delitem "
545 << uint32 (tag);
568 546
569 strcpy ((char *) sl.buf, "delitem ");
570 sl.len = strlen ((char *) sl.buf);
571 SockList_AddInt (&sl, tag);
572
573 Send_With_Handling (&pl->socket, &sl); 547 Send_With_Handling (pl->socket, &sl);
574 sl.free ();
575} 548}
576 549
577 550
578/******************************************************************************* 551/*******************************************************************************
579 * 552 *
650 return; 623 return;
651 624
652 /* If the high bit is set, player applied a pseudo object. */ 625 /* If the high bit is set, player applied a pseudo object. */
653 if (tag & 0x80000000) 626 if (tag & 0x80000000)
654 { 627 {
655 pl->socket.look_position = tag & 0x7fffffff; 628 pl->socket->look_position = tag & 0x7fffffff;
656 pl->socket.update_look = 1; 629 pl->socket->update_look = 1;
657 return; 630 return;
658 } 631 }
659 632
660 object *op = esrv_get_ob_from_count (pl->ob, tag); 633 object *op = esrv_get_ob_from_count (pl->ob, tag);
661 634
786 { 759 {
787 return; 760 return;
788 } 761 }
789 dy = atoi (cp); 762 dy = atoi (cp);
790 763
791 if (FABS (dx) > pl->socket.mapx / 2 || FABS (dy) > pl->socket.mapy / 2) 764 if (FABS (dx) > pl->socket->mapx / 2 || FABS (dy) > pl->socket->mapy / 2)
792 return; 765 return;
793 766
794 if (pl->blocked_los[dx + pl->socket.mapx / 2][dy + pl->socket.mapy / 2]) 767 if (pl->blocked_los[dx + pl->socket->mapx / 2][dy + pl->socket->mapy / 2])
795 return; 768 return;
796 769
797 look_at (pl->ob, dx, dy); 770 look_at (pl->ob, dx, dy);
798} 771}
799 772
810 return; 783 return;
811 } 784 }
812 785
813 if (!to) 786 if (!to)
814 { /* drop it to the ground */ 787 { /* drop it to the ground */
815
816/* LOG(llevDebug, "Drop it on the ground.\n");*/
817
818 if (op->map && !op->env) 788 if (op->map && !op->env)
819 {
820
821/* LOG(llevDebug,"Dropping object to ground that is already on ground\n");*/
822 return; 789 return;
823 } 790
824 /* If it is an active container, then we should drop all objects 791 /* If it is an active container, then we should drop all objects
825 * in the container and not the container itself. 792 * in the container and not the container itself.
826 */ 793 */
827 if (op->inv && QUERY_FLAG (op, FLAG_APPLIED)) 794 if (op->inv && QUERY_FLAG (op, FLAG_APPLIED))
828 { 795 {
831 for (current = op->inv; current != NULL; current = next) 798 for (current = op->inv; current != NULL; current = next)
832 { 799 {
833 next = current->below; 800 next = current->below;
834 drop_object (pl, current, 0); 801 drop_object (pl, current, 0);
835 } 802 }
803
836 esrv_update_item (UPD_WEIGHT, pl, op); 804 esrv_update_item (UPD_WEIGHT, pl, op);
837 } 805 }
838 else 806 else
839 { 807 {
840 drop_object (pl, op, nrof); 808 drop_object (pl, op, nrof);
849 817
850 pl->contr->count = nrof; 818 pl->contr->count = nrof;
851 pick_up (pl, op); 819 pick_up (pl, op);
852 return; 820 return;
853 } 821 }
822
854 env = esrv_get_ob_from_count (pl, to); 823 env = esrv_get_ob_from_count (pl, to);
855 if (!env) 824 if (!env)
856 { 825 {
857 LOG (llevDebug, "Player '%s' tried to move object to the unknown location (%d)\n", &pl->name, to); 826 LOG (llevDebug, "Player '%s' tried to move object to the unknown location (%d)\n", &pl->name, to);
858 return; 827 return;
859 } 828 }
829
860 /* put_object_in_sack presumes that necessary sanity checking 830 /* put_object_in_sack presumes that necessary sanity checking
861 * has already been done (eg, it can be picked up and fits in 831 * has already been done (eg, it can be picked up and fits in
862 * in a sack, so check for those things. We should also check 832 * in a sack, so check for those things. We should also check
863 * an make sure env is in fact a container for that matter. 833 * an make sure env is in fact a container for that matter.
864 */ 834 */
865 if (env->type == CONTAINER && can_pick (pl, op) && sack_can_hold (pl, env, op, nrof)) 835 if (env->type == CONTAINER && can_pick (pl, op) && sack_can_hold (pl, env, op, nrof))
866 { 836 {
867 put_object_in_sack (pl, env, op, nrof); 837 put_object_in_sack (pl, env, op, nrof);
868 } 838 }
869} 839}
840

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines