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.24 by root, Sat Dec 16 22:14:42 2006 UTC vs.
Revision 1.31 by root, Mon Jan 8 18:18:36 2007 UTC

1/* 1/*
2 CrossFire, A Multiplayer game for X-windows 2 CrossFire, A Multiplayer game for X-windows
3 3
4 Copyright (C) 2005, 2006, 2007 Marc Lehmann & Crossfire+ Development Team
4 Copyright (C) 2002 Mark Wedel & Crossfire Development Team 5 Copyright (C) 2002 Mark Wedel & Crossfire Development Team
5 Copyright (C) 1992 Frank Tore Johansen 6 Copyright (C) 1992 Frank Tore Johansen
6 7
7 This program is free software; you can redistribute it and/or modify 8 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by 9 it under the terms of the GNU General Public License as published by
36#include <object.h> /* LOOK_OBJ */ 37#include <object.h> /* LOOK_OBJ */
37#include <sproto.h> 38#include <sproto.h>
38 39
39/** This is the maximum number of bytes we expect any one item to take up */ 40/** This is the maximum number of bytes we expect any one item to take up */
40#define MAXITEMLEN 300 41#define MAXITEMLEN 300
42
43#if 0
44tag_t
45client_container::tag () const
46{
47 switch (type)
48 {
49 case CC_INVENTORY:
50 return ns->pl->count;
51 case CC_MAPSPACE:
52 return 0;
53 case CC_CONTAINER:
54 return env->count;
55 }
56
57 abort ();
58}
59
60void
61client_container::clear ()
62{
63 switch (type)
64 {
65 case CC_INVENTORY:
66 abort ();
67
68 case CC_MAPSPACE:
69 case CC_CONTAINER:
70 ns->send_packet_printf ("delinv %d", tag ());
71 break;
72 }
73
74 for (iterator i = begin (); i != end (); ++i)
75 i->op->seen_by = 0;
76
77 vector< refitem, slice_allocator<refitem> >::clear ();
78}
79
80inline iterator
81client_container::merge_item (iterator i, object *op)
82{
83 if (i != end () && i->op == op)
84 return ++i;
85
86 if (op->seen_by)
87 return; // seen by another entity already
88
89 op->seen_by = this;
90
91 refitem ref;
92 ref.op = op;
93
94 return insert (i, ref);
95}
96
97void
98client_container::update (int offset)
99{
100 iterator i = begin ();
101
102 switch (type)
103 {
104 case CC_INVENTORY:
105 case CC_CONTAINER:
106 {
107 object *env = type == CC_INVENTORY
108 ? ns->pl->ob
109 : this->env;
110
111 // pass 1, erase all objects no longer in container
112 for (iterator j = begin (); j != end (); ++j)
113 if (j->op->env != env)
114 {
115 j->op->seen_by = 0;
116 erase (j);
117 }
118
119 // pass 2 merge items
120 for (object *op = env->inv; op; op = op->below)
121 {
122 if (--offset < 0)
123 i = merge_item (i, op);
124 else if (offset < -FLOORBOX_PAGESIZE)
125 break;
126 }
127 }
128 break;
129
130 case CC_MAPSPACE:
131 {
132 // pass 1, erase all objects no longer on space
133 for (iterator j = begin (); j != end (); ++j)
134 if (j->op->x != x || j->op->y != y || j->op->map != map)
135 {
136 j->op->seen_by = 0;
137 erase (j);
138 }
139
140 // pass 2 merge items
141 for (object *op = GET_MAP_OB (map, x, y); op; op = op->above)
142 {
143 if (--offset < 0)
144 i = merge_item (i, op);
145 else if (offset < -FLOORBOX_PAGESIZE)
146 break;
147 }
148 }
149 break;
150 }
151
152 // pass 3, erase all extra items
153 for (iterator j = i; j != end (); ++j)
154 j->op->seen_by = 0;
155
156 if (i != end ())
157 erase (i, end ());
158}
159
160void
161client_container::set_mapspace (maptile *map, int x, int y)
162{
163 if (type == CC_MAPSPACE
164 && this->map == map
165 && this->x == x
166 && this->y == y)
167 return;
168
169 clear ();
170
171 type = CC_MAPSPACE;
172 this->map = map;
173 this->x = x;
174 this->y = y;
175}
176
177void
178client_container::set_container (object *env)
179{
180}
181#endif
41 182
42/******************************************************************************* 183/*******************************************************************************
43 * 184 *
44 * Functions related to sending object data to the client. 185 * Functions related to sending object data to the client.
45 * 186 *
197{ 338{
198 object *tmp, *last; 339 object *tmp, *last;
199 int got_one = 0, start_look = 0, end_look = 0; 340 int got_one = 0, start_look = 0, end_look = 0;
200 char buf[MAX_BUF]; 341 char buf[MAX_BUF];
201 342
202 if (!pl->contr->socket->update_look) 343 if (!pl->contr->ns->update_look)
203 { 344 {
204 LOG (llevDebug, "esrv_draw_look called when update_look was not set\n"); 345 LOG (llevDebug, "esrv_draw_look called when update_look was not set\n");
205 return; 346 return;
206 } 347 }
207 else 348 else
208 pl->contr->socket->update_look = 0; 349 pl->contr->ns->update_look = 0;
209 350
210 if (QUERY_FLAG (pl, FLAG_REMOVED) 351 if (QUERY_FLAG (pl, FLAG_REMOVED)
211 || !pl->map 352 || !pl->map
212 || pl->map->in_memory != MAP_IN_MEMORY 353 || pl->map->in_memory != MAP_IN_MEMORY
213 || out_of_map (pl->map, pl->x, pl->y)) 354 || out_of_map (pl->map, pl->x, pl->y))
214 return; 355 return;
215 356
216 for (tmp = get_map_ob (pl->map, pl->x, pl->y); tmp && tmp->above; tmp = tmp->above) 357 for (tmp = GET_MAP_OB (pl->map, pl->x, pl->y); tmp && tmp->above; tmp = tmp->above)
217 ; 358 ;
218 359
360 pl->contr->ns->send_packet ("delinv 0");
361
219 packet sl; 362 packet sl;
220
221 pl->contr->socket->send_packet ("delinv 0");
222
223 sl.printf ("item%d ", pl->contr->socket->itemcmd); 363 sl.printf ("item%d ", pl->contr->ns->itemcmd);
224 364
225 sl << uint32 (0); 365 sl << uint32 (0);
226 366
227 if (!(pl->contr->socket->faces_sent[empty_face->number] & NS_FACESENT_FACE)) 367 if (!(pl->contr->ns->faces_sent[empty_face->number] & NS_FACESENT_FACE))
228 esrv_send_face (pl->contr->socket, empty_face->number, 0); 368 esrv_send_face (pl->contr->ns, empty_face->number, 0);
229 369
230 if (pl->contr->socket->look_position) 370 if (pl->contr->ns->look_position)
231 { 371 {
232 char buf[80]; 372 char buf[80];
233 snprintf (buf, 80, "Apply this to see %d previous items", NUM_LOOK_OBJECTS); 373 snprintf (buf, 80, "Apply this to see %d previous items", FLOORBOX_PAGESIZE);
234 374
235 sl << uint32 (0x80000000 | (pl->contr->socket->look_position - NUM_LOOK_OBJECTS)) 375 sl << uint32 (0x80000000 | (pl->contr->ns->look_position - FLOORBOX_PAGESIZE))
236 << uint32 (0) 376 << uint32 (0)
237 << sint32 (-1) 377 << sint32 (-1)
238 << uint32 (empty_face->number) 378 << uint32 (empty_face->number)
239 << data8 (buf) 379 << data8 (buf)
240 << uint16 (0) 380 << uint16 (0)
241 << uint8 (0) 381 << uint8 (0)
242 << uint32 (0); 382 << uint32 (0);
243 383
244 if (pl->contr->socket->itemcmd == 2) 384 if (pl->contr->ns->itemcmd == 2)
245 sl << uint16 (0); 385 sl << uint16 (0);
246 } 386 }
247 387
248 for (last = NULL; tmp != last; tmp = tmp->below) 388 for (last = NULL; tmp != last; tmp = tmp->below)
249 { 389 {
256 last = last->below; 396 last = last->below;
257 } 397 }
258 398
259 if (LOOK_OBJ (tmp)) 399 if (LOOK_OBJ (tmp))
260 { 400 {
261 if (++start_look < pl->contr->socket->look_position) 401 if (++start_look < pl->contr->ns->look_position)
262 continue; 402 continue;
263 403
264 end_look++; 404 end_look++;
265 405
266 if (end_look > NUM_LOOK_OBJECTS) 406 if (end_look > FLOORBOX_PAGESIZE)
267 { 407 {
268 /* What we basically do is make a 'fake' object - when the user applies it, 408 /* What we basically do is make a 'fake' object - when the user applies it,
269 * we notice the special tag the object has, and act accordingly. 409 * we notice the special tag the object has, and act accordingly.
270 */ 410 */
271 sl << uint32 (0x80000000 | (pl->contr->socket->look_position + NUM_LOOK_OBJECTS)) 411 sl << uint32 (0x80000000 | (pl->contr->ns->look_position + FLOORBOX_PAGESIZE))
272 << uint32 (0) 412 << uint32 (0)
273 << uint32 ((uint32) - 1) 413 << uint32 ((uint32) - 1)
274 << uint32 (empty_face->number) 414 << uint32 (empty_face->number)
275 << data8 ("Apply this to see next group of items") 415 << data8 ("Apply this to see next group of items")
276 << uint16 (0) 416 << uint16 (0)
277 << uint8 (0) 417 << uint8 (0)
278 << uint32 (0); 418 << uint32 (0);
279 419
280 if (pl->contr->socket->itemcmd == 2) 420 if (pl->contr->ns->itemcmd == 2)
281 sl << uint16 (0); 421 sl << uint16 (0);
282 422
283 break; 423 break;
284 } 424 }
285 425
286 if (tmp->head) 426 if (tmp->head)
287 head = tmp->head; 427 head = tmp->head;
288 else 428 else
289 head = tmp; 429 head = tmp;
290 430
291 add_object_to_socklist (*pl->contr->socket, sl, head); 431 add_object_to_socklist (*pl->contr->ns, sl, head);
292 got_one++; 432 got_one++;
293 433
294 if (sl.length () >= (MAXSOCKBUF - MAXITEMLEN)) 434 if (sl.length () >= (MAXSOCKBUF - MAXITEMLEN))
295 { 435 {
296 pl->contr->socket->send_packet (sl); 436 pl->contr->ns->send_packet (sl);
297 437
298 sl.reset (); 438 sl.reset ();
299 sl.printf ("item%d ", pl->contr->socket->itemcmd); 439 sl.printf ("item%d ", pl->contr->ns->itemcmd);
300 sl << uint32 (0); 440 sl << uint32 (0);
301 got_one = 0; 441 got_one = 0;
302 } 442 }
303 } 443 }
304 } 444 }
305 445
306 if (got_one) 446 if (got_one)
307 pl->contr->socket->send_packet (sl); 447 pl->contr->ns->send_packet (sl);
308 448
309} 449}
310 450
311/** 451/**
312 * Sends whole inventory. 452 * Sends whole inventory.
315esrv_send_inventory (object *pl, object *op) 455esrv_send_inventory (object *pl, object *op)
316{ 456{
317 object *tmp; 457 object *tmp;
318 int got_one = 0; 458 int got_one = 0;
319 459
460
461 pl->contr->ns->send_packet_printf ("delinv %d", op->count);
462
320 packet sl; 463 packet sl;
321
322 sl.printf ("delinv %d", op->count);
323 pl->contr->socket->send_packet (sl);
324
325 sl.reset ();
326 sl.printf ("item%d ", pl->contr->socket->itemcmd); 464 sl.printf ("item%d ", pl->contr->ns->itemcmd);
327 465
328 sl << uint32 (op->count); 466 sl << uint32 (op->count);
329 467
330 for (tmp = op->inv; tmp; tmp = tmp->below) 468 for (tmp = op->inv; tmp; tmp = tmp->below)
331 { 469 {
336 else 474 else
337 head = tmp; 475 head = tmp;
338 476
339 if (LOOK_OBJ (head)) 477 if (LOOK_OBJ (head))
340 { 478 {
341 add_object_to_socklist (*pl->contr->socket, sl, head); 479 add_object_to_socklist (*pl->contr->ns, sl, head);
342 480
343 got_one++; 481 got_one++;
344 482
345 /* IT is possible for players to accumulate a huge amount of 483 /* IT is possible for players to accumulate a huge amount of
346 * items (especially with some of the bags out there) to 484 * items (especially with some of the bags out there) to
347 * overflow the buffer. IF so, send multiple item commands. 485 * overflow the buffer. IF so, send multiple item commands.
348 */ 486 */
349 if (sl.length () >= (MAXSOCKBUF - MAXITEMLEN)) 487 if (sl.length () >= (MAXSOCKBUF - MAXITEMLEN))
350 { 488 {
351 pl->contr->socket->send_packet (sl); 489 pl->contr->ns->send_packet (sl);
352 490
353 sl.reset (); 491 sl.reset ();
354 sl.printf ("item%d ", pl->contr->socket->itemcmd); 492 sl.printf ("item%d ", pl->contr->ns->itemcmd);
355 sl << uint32 (op->count); 493 sl << uint32 (op->count);
356 got_one = 0; 494 got_one = 0;
357 } 495 }
358 } /* If LOOK_OBJ() */ 496 } /* If LOOK_OBJ() */
359 } 497 }
360 498
361 if (got_one) 499 if (got_one)
362 pl->contr->socket->send_packet (sl); 500 pl->contr->ns->send_packet (sl);
363} 501}
364 502
365/** 503/**
366 * Updates object *op for player *pl. 504 * Updates object *op for player *pl.
367 * 505 *
382 * is hopefully in the same place, so the client should preserve 520 * is hopefully in the same place, so the client should preserve
383 * order. 521 * order.
384 */ 522 */
385 } 523 }
386 524
525 client *ns = pl->contr->ns;
526 if (!ns)
527 return;
528
387 if (!QUERY_FLAG (op, FLAG_CLIENT_SENT)) 529 if (!QUERY_FLAG (op, FLAG_CLIENT_SENT))
388 { 530 {
389 /* FLAG_CLIENT_SENT is debug only. We are using it to see where 531 /* FLAG_CLIENT_SENT is debug only. We are using it to see where
390 * this is happening - we can set a breakpoint here in the debugger 532 * this is happening - we can set a breakpoint here in the debugger
391 * and track back the call. 533 * and track back the call.
392 */ 534 */
393 LOG (llevDebug, "We have not sent item %s (%d)\n", &op->name, op->count); 535 LOG (llevDebug, "We have not sent item %s (%d)\n", &op->name, op->count);
394 } 536 }
395 537
396 packet sl; 538 packet sl ("upditem");
397 539
398 sl << "upditem "
399 << uint8 (flags); 540 sl << uint8 (flags);
400 541
401 if (op->head) 542 if (op->head)
402 op = op->head; 543 op = op->head;
403 544
404 sl << uint32 (op->count); 545 sl << uint32 (op->count);
414 sint32 weight = WEIGHT (op); 555 sint32 weight = WEIGHT (op);
415 556
416 sl << uint32 (QUERY_FLAG (op, FLAG_NO_PICK) ? -1 : weight); 557 sl << uint32 (QUERY_FLAG (op, FLAG_NO_PICK) ? -1 : weight);
417 558
418 if (pl == op) 559 if (pl == op)
419 op->contr->last_weight = weight; 560 ns->last_weight = weight;
420 } 561 }
421 562
422 if (flags & UPD_FACE) 563 if (flags & UPD_FACE)
423 { 564 {
424 if (!(pl->contr->socket->faces_sent[op->face->number] & NS_FACESENT_FACE)) 565 if (!(ns->faces_sent[op->face->number] & NS_FACESENT_FACE))
425 esrv_send_face (pl->contr->socket, op->face->number, 0); 566 esrv_send_face (ns, op->face->number, 0);
426 567
427 sl << uint32 (op->face->number); 568 sl << uint32 (op->face->number);
428 } 569 }
429 570
430 if (flags & UPD_NAME) 571 if (flags & UPD_NAME)
484 } 625 }
485 626
486 if (flags & UPD_NROF) 627 if (flags & UPD_NROF)
487 sl << uint32 (op->nrof); 628 sl << uint32 (op->nrof);
488 629
489 pl->contr->socket->send_packet (sl); 630 pl->contr->ns->send_packet (sl);
490} 631}
491 632
492/** 633/**
493 * Sends item's info to player. 634 * Sends item's info to player.
494 */ 635 */
504 /* if the item is on the ground, mark that the look needs to 645 /* if the item is on the ground, mark that the look needs to
505 * be updated. 646 * be updated.
506 */ 647 */
507 if (!op->env) 648 if (!op->env)
508 { 649 {
509 pl->contr->socket->update_look = 1; 650 pl->contr->ns->floorbox_update ();
510 return; 651 return;
511 } 652 }
512 } 653 }
513 654
514 packet sl; 655 packet sl;
515 656
516 sl.printf ("item%d ", pl->contr->socket->itemcmd); 657 sl.printf ("item%d ", pl->contr->ns->itemcmd);
517 658
518 if (op->head) 659 if (op->head)
519 op = op->head; 660 op = op->head;
520 661
521 sl << uint32 (op->env ? op->env->count : 0); 662 sl << uint32 (op->env ? op->env->count : 0);
522 663
523 add_object_to_socklist (*pl->contr->socket, sl, op); 664 add_object_to_socklist (*pl->contr->ns, sl, op);
524 665
525 pl->contr->socket->send_packet (sl); 666 pl->contr->ns->send_packet (sl);
526 SET_FLAG (op, FLAG_CLIENT_SENT); 667 SET_FLAG (op, FLAG_CLIENT_SENT);
527} 668}
528 669
529/** 670/**
530 * Tells the client to delete an item. Uses the item 671 * Tells the client to delete an item. Uses the item
532 */ 673 */
533 674
534void 675void
535esrv_del_item (player *pl, int tag) 676esrv_del_item (player *pl, int tag)
536{ 677{
537 packet sl; 678 packet sl ("delitem");
538 679
539 sl << "delitem "
540 << uint32 (tag); 680 sl << uint32 (tag);
541 681
542 pl->socket->send_packet (sl); 682 pl->ns->send_packet (sl);
543} 683}
544 684
545 685
546/******************************************************************************* 686/*******************************************************************************
547 * 687 *
568 else if (op->type == CONTAINER && pl->container == op) 708 else if (op->type == CONTAINER && pl->container == op)
569 for (tmp = op->inv; tmp; tmp = tmp->below) 709 for (tmp = op->inv; tmp; tmp = tmp->below)
570 if (tmp->count == count) 710 if (tmp->count == count)
571 return tmp; 711 return tmp;
572 712
573 for (op = get_map_ob (pl->map, pl->x, pl->y); op; op = op->above) 713 for (op = GET_MAP_OB (pl->map, pl->x, pl->y); op; op = op->above)
574 if (op->head && op->head->count == count) 714 if (op->head && op->head->count == count)
575 return op; 715 return op;
576 else if (op->count == count) 716 else if (op->count == count)
577 return op; 717 return op;
578 else if (op->type == CONTAINER && pl->container == op) 718 else if (op->type == CONTAINER && pl->container == op)
618 return; 758 return;
619 759
620 /* If the high bit is set, player applied a pseudo object. */ 760 /* If the high bit is set, player applied a pseudo object. */
621 if (tag & 0x80000000) 761 if (tag & 0x80000000)
622 { 762 {
623 pl->socket->look_position = tag & 0x7fffffff; 763 pl->ns->look_position = tag & 0x7fffffff;
624 pl->socket->update_look = 1; 764 pl->ns->floorbox_update ();
625 return; 765 return;
626 } 766 }
627 767
628 object *op = esrv_get_ob_from_count (pl->ob, tag); 768 object *op = esrv_get_ob_from_count (pl->ob, tag);
629 769
634 } 774 }
635 775
636 player_apply (pl->ob, op, 0, 0); 776 player_apply (pl->ob, op, 0, 0);
637} 777}
638 778
639/** Client wants to apply some object. Lets do so. */ 779/** Client wants to lock some object. Lets do so. */
640void 780void
641LockItem (char *data, int len, player *pl) 781LockItem (char *data, int len, player *pl)
642{ 782{
643 int flag = data[0]; 783 int flag = data[0];
644 tag_t tag = net_uint32 ((uint8 *)data + 1); 784 tag_t tag = net_uint32 ((uint8 *)data + 1);
656 SET_FLAG (op, FLAG_INV_LOCKED); 796 SET_FLAG (op, FLAG_INV_LOCKED);
657 797
658 esrv_update_item (UPD_FLAGS, pl->ob, op); 798 esrv_update_item (UPD_FLAGS, pl->ob, op);
659} 799}
660 800
661/** Client wants to apply some object. Lets do so. */ 801/** Client wants to mark some object. Lets do so. */
662void 802void
663MarkItem (char *data, int len, player *pl) 803MarkItem (char *data, int len, player *pl)
664{ 804{
665 tag_t tag = net_uint32 ((uint8 *)data); 805 tag_t tag = net_uint32 ((uint8 *)data);
666 object *op = esrv_get_ob_from_count (pl->ob, tag); 806 object *op = esrv_get_ob_from_count (pl->ob, tag);
697 837
698 m = get_map_from_coord (op->map, &x, &y); 838 m = get_map_from_coord (op->map, &x, &y);
699 if (!m) 839 if (!m)
700 return; 840 return;
701 841
702 for (tmp = get_map_ob (m, x, y); tmp != NULL && tmp->above != NULL; tmp = tmp->above); 842 for (tmp = GET_MAP_OB (m, x, y); tmp != NULL && tmp->above != NULL; tmp = tmp->above);
703 843
704 for (; tmp != NULL; tmp = tmp->below) 844 for (; tmp != NULL; tmp = tmp->below)
705 { 845 {
706 if (tmp->invisible && !QUERY_FLAG (op, FLAG_WIZ)) 846 if (tmp->invisible && !QUERY_FLAG (op, FLAG_WIZ))
707 continue; 847 continue;
754 { 894 {
755 return; 895 return;
756 } 896 }
757 dy = atoi (cp); 897 dy = atoi (cp);
758 898
759 if (FABS (dx) > pl->socket->mapx / 2 || FABS (dy) > pl->socket->mapy / 2) 899 if (FABS (dx) > pl->ns->mapx / 2 || FABS (dy) > pl->ns->mapy / 2)
760 return; 900 return;
761 901
762 if (pl->blocked_los[dx + pl->socket->mapx / 2][dy + pl->socket->mapy / 2]) 902 if (pl->blocked_los[dx + pl->ns->mapx / 2][dy + pl->ns->mapy / 2])
763 return; 903 return;
764 904
765 look_at (pl->ob, dx, dy); 905 look_at (pl->ob, dx, dy);
766} 906}
767 907

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines