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.27 by root, Wed Dec 20 01:19:11 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
219 pl->contr->socket->send_packet ("delinv 0"); 360 pl->contr->ns->send_packet ("delinv 0");
220 361
221 packet sl; 362 packet sl;
222 sl.printf ("item%d ", pl->contr->socket->itemcmd); 363 sl.printf ("item%d ", pl->contr->ns->itemcmd);
223 364
224 sl << uint32 (0); 365 sl << uint32 (0);
225 366
226 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))
227 esrv_send_face (pl->contr->socket, empty_face->number, 0); 368 esrv_send_face (pl->contr->ns, empty_face->number, 0);
228 369
229 if (pl->contr->socket->look_position) 370 if (pl->contr->ns->look_position)
230 { 371 {
231 char buf[80]; 372 char buf[80];
232 snprintf (buf, 80, "Apply this to see %d previous items", FLOORBOX_PAGESIZE); 373 snprintf (buf, 80, "Apply this to see %d previous items", FLOORBOX_PAGESIZE);
233 374
234 sl << uint32 (0x80000000 | (pl->contr->socket->look_position - FLOORBOX_PAGESIZE)) 375 sl << uint32 (0x80000000 | (pl->contr->ns->look_position - FLOORBOX_PAGESIZE))
235 << uint32 (0) 376 << uint32 (0)
236 << sint32 (-1) 377 << sint32 (-1)
237 << uint32 (empty_face->number) 378 << uint32 (empty_face->number)
238 << data8 (buf) 379 << data8 (buf)
239 << uint16 (0) 380 << uint16 (0)
240 << uint8 (0) 381 << uint8 (0)
241 << uint32 (0); 382 << uint32 (0);
242 383
243 if (pl->contr->socket->itemcmd == 2) 384 if (pl->contr->ns->itemcmd == 2)
244 sl << uint16 (0); 385 sl << uint16 (0);
245 } 386 }
246 387
247 for (last = NULL; tmp != last; tmp = tmp->below) 388 for (last = NULL; tmp != last; tmp = tmp->below)
248 { 389 {
255 last = last->below; 396 last = last->below;
256 } 397 }
257 398
258 if (LOOK_OBJ (tmp)) 399 if (LOOK_OBJ (tmp))
259 { 400 {
260 if (++start_look < pl->contr->socket->look_position) 401 if (++start_look < pl->contr->ns->look_position)
261 continue; 402 continue;
262 403
263 end_look++; 404 end_look++;
264 405
265 if (end_look > FLOORBOX_PAGESIZE) 406 if (end_look > FLOORBOX_PAGESIZE)
266 { 407 {
267 /* 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,
268 * we notice the special tag the object has, and act accordingly. 409 * we notice the special tag the object has, and act accordingly.
269 */ 410 */
270 sl << uint32 (0x80000000 | (pl->contr->socket->look_position + FLOORBOX_PAGESIZE)) 411 sl << uint32 (0x80000000 | (pl->contr->ns->look_position + FLOORBOX_PAGESIZE))
271 << uint32 (0) 412 << uint32 (0)
272 << uint32 ((uint32) - 1) 413 << uint32 ((uint32) - 1)
273 << uint32 (empty_face->number) 414 << uint32 (empty_face->number)
274 << data8 ("Apply this to see next group of items") 415 << data8 ("Apply this to see next group of items")
275 << uint16 (0) 416 << uint16 (0)
276 << uint8 (0) 417 << uint8 (0)
277 << uint32 (0); 418 << uint32 (0);
278 419
279 if (pl->contr->socket->itemcmd == 2) 420 if (pl->contr->ns->itemcmd == 2)
280 sl << uint16 (0); 421 sl << uint16 (0);
281 422
282 break; 423 break;
283 } 424 }
284 425
285 if (tmp->head) 426 if (tmp->head)
286 head = tmp->head; 427 head = tmp->head;
287 else 428 else
288 head = tmp; 429 head = tmp;
289 430
290 add_object_to_socklist (*pl->contr->socket, sl, head); 431 add_object_to_socklist (*pl->contr->ns, sl, head);
291 got_one++; 432 got_one++;
292 433
293 if (sl.length () >= (MAXSOCKBUF - MAXITEMLEN)) 434 if (sl.length () >= (MAXSOCKBUF - MAXITEMLEN))
294 { 435 {
295 pl->contr->socket->send_packet (sl); 436 pl->contr->ns->send_packet (sl);
296 437
297 sl.reset (); 438 sl.reset ();
298 sl.printf ("item%d ", pl->contr->socket->itemcmd); 439 sl.printf ("item%d ", pl->contr->ns->itemcmd);
299 sl << uint32 (0); 440 sl << uint32 (0);
300 got_one = 0; 441 got_one = 0;
301 } 442 }
302 } 443 }
303 } 444 }
304 445
305 if (got_one) 446 if (got_one)
306 pl->contr->socket->send_packet (sl); 447 pl->contr->ns->send_packet (sl);
307 448
308} 449}
309 450
310/** 451/**
311 * Sends whole inventory. 452 * Sends whole inventory.
315{ 456{
316 object *tmp; 457 object *tmp;
317 int got_one = 0; 458 int got_one = 0;
318 459
319 460
320 pl->contr->socket->send_packet_printf ("delinv %d", op->count); 461 pl->contr->ns->send_packet_printf ("delinv %d", op->count);
321 462
322 packet sl; 463 packet sl;
323 sl.printf ("item%d ", pl->contr->socket->itemcmd); 464 sl.printf ("item%d ", pl->contr->ns->itemcmd);
324 465
325 sl << uint32 (op->count); 466 sl << uint32 (op->count);
326 467
327 for (tmp = op->inv; tmp; tmp = tmp->below) 468 for (tmp = op->inv; tmp; tmp = tmp->below)
328 { 469 {
333 else 474 else
334 head = tmp; 475 head = tmp;
335 476
336 if (LOOK_OBJ (head)) 477 if (LOOK_OBJ (head))
337 { 478 {
338 add_object_to_socklist (*pl->contr->socket, sl, head); 479 add_object_to_socklist (*pl->contr->ns, sl, head);
339 480
340 got_one++; 481 got_one++;
341 482
342 /* IT is possible for players to accumulate a huge amount of 483 /* IT is possible for players to accumulate a huge amount of
343 * items (especially with some of the bags out there) to 484 * items (especially with some of the bags out there) to
344 * overflow the buffer. IF so, send multiple item commands. 485 * overflow the buffer. IF so, send multiple item commands.
345 */ 486 */
346 if (sl.length () >= (MAXSOCKBUF - MAXITEMLEN)) 487 if (sl.length () >= (MAXSOCKBUF - MAXITEMLEN))
347 { 488 {
348 pl->contr->socket->send_packet (sl); 489 pl->contr->ns->send_packet (sl);
349 490
350 sl.reset (); 491 sl.reset ();
351 sl.printf ("item%d ", pl->contr->socket->itemcmd); 492 sl.printf ("item%d ", pl->contr->ns->itemcmd);
352 sl << uint32 (op->count); 493 sl << uint32 (op->count);
353 got_one = 0; 494 got_one = 0;
354 } 495 }
355 } /* If LOOK_OBJ() */ 496 } /* If LOOK_OBJ() */
356 } 497 }
357 498
358 if (got_one) 499 if (got_one)
359 pl->contr->socket->send_packet (sl); 500 pl->contr->ns->send_packet (sl);
360} 501}
361 502
362/** 503/**
363 * Updates object *op for player *pl. 504 * Updates object *op for player *pl.
364 * 505 *
379 * is hopefully in the same place, so the client should preserve 520 * is hopefully in the same place, so the client should preserve
380 * order. 521 * order.
381 */ 522 */
382 } 523 }
383 524
525 client *ns = pl->contr->ns;
526 if (!ns)
527 return;
528
384 if (!QUERY_FLAG (op, FLAG_CLIENT_SENT)) 529 if (!QUERY_FLAG (op, FLAG_CLIENT_SENT))
385 { 530 {
386 /* 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
387 * 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
388 * and track back the call. 533 * and track back the call.
410 sint32 weight = WEIGHT (op); 555 sint32 weight = WEIGHT (op);
411 556
412 sl << uint32 (QUERY_FLAG (op, FLAG_NO_PICK) ? -1 : weight); 557 sl << uint32 (QUERY_FLAG (op, FLAG_NO_PICK) ? -1 : weight);
413 558
414 if (pl == op) 559 if (pl == op)
415 op->contr->last_weight = weight; 560 ns->last_weight = weight;
416 } 561 }
417 562
418 if (flags & UPD_FACE) 563 if (flags & UPD_FACE)
419 { 564 {
420 if (!(pl->contr->socket->faces_sent[op->face->number] & NS_FACESENT_FACE)) 565 if (!(ns->faces_sent[op->face->number] & NS_FACESENT_FACE))
421 esrv_send_face (pl->contr->socket, op->face->number, 0); 566 esrv_send_face (ns, op->face->number, 0);
422 567
423 sl << uint32 (op->face->number); 568 sl << uint32 (op->face->number);
424 } 569 }
425 570
426 if (flags & UPD_NAME) 571 if (flags & UPD_NAME)
480 } 625 }
481 626
482 if (flags & UPD_NROF) 627 if (flags & UPD_NROF)
483 sl << uint32 (op->nrof); 628 sl << uint32 (op->nrof);
484 629
485 pl->contr->socket->send_packet (sl); 630 pl->contr->ns->send_packet (sl);
486} 631}
487 632
488/** 633/**
489 * Sends item's info to player. 634 * Sends item's info to player.
490 */ 635 */
500 /* 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
501 * be updated. 646 * be updated.
502 */ 647 */
503 if (!op->env) 648 if (!op->env)
504 { 649 {
505 pl->contr->socket->floorbox_update (); 650 pl->contr->ns->floorbox_update ();
506 return; 651 return;
507 } 652 }
508 } 653 }
509 654
510 packet sl; 655 packet sl;
511 656
512 sl.printf ("item%d ", pl->contr->socket->itemcmd); 657 sl.printf ("item%d ", pl->contr->ns->itemcmd);
513 658
514 if (op->head) 659 if (op->head)
515 op = op->head; 660 op = op->head;
516 661
517 sl << uint32 (op->env ? op->env->count : 0); 662 sl << uint32 (op->env ? op->env->count : 0);
518 663
519 add_object_to_socklist (*pl->contr->socket, sl, op); 664 add_object_to_socklist (*pl->contr->ns, sl, op);
520 665
521 pl->contr->socket->send_packet (sl); 666 pl->contr->ns->send_packet (sl);
522 SET_FLAG (op, FLAG_CLIENT_SENT); 667 SET_FLAG (op, FLAG_CLIENT_SENT);
523} 668}
524 669
525/** 670/**
526 * Tells the client to delete an item. Uses the item 671 * Tells the client to delete an item. Uses the item
532{ 677{
533 packet sl ("delitem"); 678 packet sl ("delitem");
534 679
535 sl << uint32 (tag); 680 sl << uint32 (tag);
536 681
537 pl->socket->send_packet (sl); 682 pl->ns->send_packet (sl);
538} 683}
539 684
540 685
541/******************************************************************************* 686/*******************************************************************************
542 * 687 *
563 else if (op->type == CONTAINER && pl->container == op) 708 else if (op->type == CONTAINER && pl->container == op)
564 for (tmp = op->inv; tmp; tmp = tmp->below) 709 for (tmp = op->inv; tmp; tmp = tmp->below)
565 if (tmp->count == count) 710 if (tmp->count == count)
566 return tmp; 711 return tmp;
567 712
568 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)
569 if (op->head && op->head->count == count) 714 if (op->head && op->head->count == count)
570 return op; 715 return op;
571 else if (op->count == count) 716 else if (op->count == count)
572 return op; 717 return op;
573 else if (op->type == CONTAINER && pl->container == op) 718 else if (op->type == CONTAINER && pl->container == op)
613 return; 758 return;
614 759
615 /* If the high bit is set, player applied a pseudo object. */ 760 /* If the high bit is set, player applied a pseudo object. */
616 if (tag & 0x80000000) 761 if (tag & 0x80000000)
617 { 762 {
618 pl->socket->look_position = tag & 0x7fffffff; 763 pl->ns->look_position = tag & 0x7fffffff;
619 pl->socket->floorbox_update (); 764 pl->ns->floorbox_update ();
620 return; 765 return;
621 } 766 }
622 767
623 object *op = esrv_get_ob_from_count (pl->ob, tag); 768 object *op = esrv_get_ob_from_count (pl->ob, tag);
624 769
692 837
693 m = get_map_from_coord (op->map, &x, &y); 838 m = get_map_from_coord (op->map, &x, &y);
694 if (!m) 839 if (!m)
695 return; 840 return;
696 841
697 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);
698 843
699 for (; tmp != NULL; tmp = tmp->below) 844 for (; tmp != NULL; tmp = tmp->below)
700 { 845 {
701 if (tmp->invisible && !QUERY_FLAG (op, FLAG_WIZ)) 846 if (tmp->invisible && !QUERY_FLAG (op, FLAG_WIZ))
702 continue; 847 continue;
749 { 894 {
750 return; 895 return;
751 } 896 }
752 dy = atoi (cp); 897 dy = atoi (cp);
753 898
754 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)
755 return; 900 return;
756 901
757 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])
758 return; 903 return;
759 904
760 look_at (pl->ob, dx, dy); 905 look_at (pl->ob, dx, dy);
761} 906}
762 907

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines