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.30 by pippijn, Sat Jan 6 14:42:31 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 *
391 * and track back the call. 529 * and track back the call.
392 */ 530 */
393 LOG (llevDebug, "We have not sent item %s (%d)\n", &op->name, op->count); 531 LOG (llevDebug, "We have not sent item %s (%d)\n", &op->name, op->count);
394 } 532 }
395 533
396 packet sl; 534 packet sl ("upditem");
397 535
398 sl << "upditem "
399 << uint8 (flags); 536 sl << uint8 (flags);
400 537
401 if (op->head) 538 if (op->head)
402 op = op->head; 539 op = op->head;
403 540
404 sl << uint32 (op->count); 541 sl << uint32 (op->count);
419 op->contr->last_weight = weight; 556 op->contr->last_weight = weight;
420 } 557 }
421 558
422 if (flags & UPD_FACE) 559 if (flags & UPD_FACE)
423 { 560 {
424 if (!(pl->contr->socket->faces_sent[op->face->number] & NS_FACESENT_FACE)) 561 if (!(pl->contr->ns->faces_sent[op->face->number] & NS_FACESENT_FACE))
425 esrv_send_face (pl->contr->socket, op->face->number, 0); 562 esrv_send_face (pl->contr->ns, op->face->number, 0);
426 563
427 sl << uint32 (op->face->number); 564 sl << uint32 (op->face->number);
428 } 565 }
429 566
430 if (flags & UPD_NAME) 567 if (flags & UPD_NAME)
484 } 621 }
485 622
486 if (flags & UPD_NROF) 623 if (flags & UPD_NROF)
487 sl << uint32 (op->nrof); 624 sl << uint32 (op->nrof);
488 625
489 pl->contr->socket->send_packet (sl); 626 pl->contr->ns->send_packet (sl);
490} 627}
491 628
492/** 629/**
493 * Sends item's info to player. 630 * Sends item's info to player.
494 */ 631 */
504 /* if the item is on the ground, mark that the look needs to 641 /* if the item is on the ground, mark that the look needs to
505 * be updated. 642 * be updated.
506 */ 643 */
507 if (!op->env) 644 if (!op->env)
508 { 645 {
509 pl->contr->socket->update_look = 1; 646 pl->contr->ns->floorbox_update ();
510 return; 647 return;
511 } 648 }
512 } 649 }
513 650
514 packet sl; 651 packet sl;
515 652
516 sl.printf ("item%d ", pl->contr->socket->itemcmd); 653 sl.printf ("item%d ", pl->contr->ns->itemcmd);
517 654
518 if (op->head) 655 if (op->head)
519 op = op->head; 656 op = op->head;
520 657
521 sl << uint32 (op->env ? op->env->count : 0); 658 sl << uint32 (op->env ? op->env->count : 0);
522 659
523 add_object_to_socklist (*pl->contr->socket, sl, op); 660 add_object_to_socklist (*pl->contr->ns, sl, op);
524 661
525 pl->contr->socket->send_packet (sl); 662 pl->contr->ns->send_packet (sl);
526 SET_FLAG (op, FLAG_CLIENT_SENT); 663 SET_FLAG (op, FLAG_CLIENT_SENT);
527} 664}
528 665
529/** 666/**
530 * Tells the client to delete an item. Uses the item 667 * Tells the client to delete an item. Uses the item
532 */ 669 */
533 670
534void 671void
535esrv_del_item (player *pl, int tag) 672esrv_del_item (player *pl, int tag)
536{ 673{
537 packet sl; 674 packet sl ("delitem");
538 675
539 sl << "delitem "
540 << uint32 (tag); 676 sl << uint32 (tag);
541 677
542 pl->socket->send_packet (sl); 678 pl->ns->send_packet (sl);
543} 679}
544 680
545 681
546/******************************************************************************* 682/*******************************************************************************
547 * 683 *
568 else if (op->type == CONTAINER && pl->container == op) 704 else if (op->type == CONTAINER && pl->container == op)
569 for (tmp = op->inv; tmp; tmp = tmp->below) 705 for (tmp = op->inv; tmp; tmp = tmp->below)
570 if (tmp->count == count) 706 if (tmp->count == count)
571 return tmp; 707 return tmp;
572 708
573 for (op = get_map_ob (pl->map, pl->x, pl->y); op; op = op->above) 709 for (op = GET_MAP_OB (pl->map, pl->x, pl->y); op; op = op->above)
574 if (op->head && op->head->count == count) 710 if (op->head && op->head->count == count)
575 return op; 711 return op;
576 else if (op->count == count) 712 else if (op->count == count)
577 return op; 713 return op;
578 else if (op->type == CONTAINER && pl->container == op) 714 else if (op->type == CONTAINER && pl->container == op)
618 return; 754 return;
619 755
620 /* If the high bit is set, player applied a pseudo object. */ 756 /* If the high bit is set, player applied a pseudo object. */
621 if (tag & 0x80000000) 757 if (tag & 0x80000000)
622 { 758 {
623 pl->socket->look_position = tag & 0x7fffffff; 759 pl->ns->look_position = tag & 0x7fffffff;
624 pl->socket->update_look = 1; 760 pl->ns->floorbox_update ();
625 return; 761 return;
626 } 762 }
627 763
628 object *op = esrv_get_ob_from_count (pl->ob, tag); 764 object *op = esrv_get_ob_from_count (pl->ob, tag);
629 765
634 } 770 }
635 771
636 player_apply (pl->ob, op, 0, 0); 772 player_apply (pl->ob, op, 0, 0);
637} 773}
638 774
639/** Client wants to apply some object. Lets do so. */ 775/** Client wants to lock some object. Lets do so. */
640void 776void
641LockItem (char *data, int len, player *pl) 777LockItem (char *data, int len, player *pl)
642{ 778{
643 int flag = data[0]; 779 int flag = data[0];
644 tag_t tag = net_uint32 ((uint8 *)data + 1); 780 tag_t tag = net_uint32 ((uint8 *)data + 1);
656 SET_FLAG (op, FLAG_INV_LOCKED); 792 SET_FLAG (op, FLAG_INV_LOCKED);
657 793
658 esrv_update_item (UPD_FLAGS, pl->ob, op); 794 esrv_update_item (UPD_FLAGS, pl->ob, op);
659} 795}
660 796
661/** Client wants to apply some object. Lets do so. */ 797/** Client wants to mark some object. Lets do so. */
662void 798void
663MarkItem (char *data, int len, player *pl) 799MarkItem (char *data, int len, player *pl)
664{ 800{
665 tag_t tag = net_uint32 ((uint8 *)data); 801 tag_t tag = net_uint32 ((uint8 *)data);
666 object *op = esrv_get_ob_from_count (pl->ob, tag); 802 object *op = esrv_get_ob_from_count (pl->ob, tag);
697 833
698 m = get_map_from_coord (op->map, &x, &y); 834 m = get_map_from_coord (op->map, &x, &y);
699 if (!m) 835 if (!m)
700 return; 836 return;
701 837
702 for (tmp = get_map_ob (m, x, y); tmp != NULL && tmp->above != NULL; tmp = tmp->above); 838 for (tmp = GET_MAP_OB (m, x, y); tmp != NULL && tmp->above != NULL; tmp = tmp->above);
703 839
704 for (; tmp != NULL; tmp = tmp->below) 840 for (; tmp != NULL; tmp = tmp->below)
705 { 841 {
706 if (tmp->invisible && !QUERY_FLAG (op, FLAG_WIZ)) 842 if (tmp->invisible && !QUERY_FLAG (op, FLAG_WIZ))
707 continue; 843 continue;
754 { 890 {
755 return; 891 return;
756 } 892 }
757 dy = atoi (cp); 893 dy = atoi (cp);
758 894
759 if (FABS (dx) > pl->socket->mapx / 2 || FABS (dy) > pl->socket->mapy / 2) 895 if (FABS (dx) > pl->ns->mapx / 2 || FABS (dy) > pl->ns->mapy / 2)
760 return; 896 return;
761 897
762 if (pl->blocked_los[dx + pl->socket->mapx / 2][dy + pl->socket->mapy / 2]) 898 if (pl->blocked_los[dx + pl->ns->mapx / 2][dy + pl->ns->mapy / 2])
763 return; 899 return;
764 900
765 look_at (pl->ob, dx, dy); 901 look_at (pl->ob, dx, dy);
766} 902}
767 903

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines