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.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
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 *
415 op->contr->last_weight = weight; 556 op->contr->last_weight = weight;
416 } 557 }
417 558
418 if (flags & UPD_FACE) 559 if (flags & UPD_FACE)
419 { 560 {
420 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))
421 esrv_send_face (pl->contr->socket, op->face->number, 0); 562 esrv_send_face (pl->contr->ns, op->face->number, 0);
422 563
423 sl << uint32 (op->face->number); 564 sl << uint32 (op->face->number);
424 } 565 }
425 566
426 if (flags & UPD_NAME) 567 if (flags & UPD_NAME)
480 } 621 }
481 622
482 if (flags & UPD_NROF) 623 if (flags & UPD_NROF)
483 sl << uint32 (op->nrof); 624 sl << uint32 (op->nrof);
484 625
485 pl->contr->socket->send_packet (sl); 626 pl->contr->ns->send_packet (sl);
486} 627}
487 628
488/** 629/**
489 * Sends item's info to player. 630 * Sends item's info to player.
490 */ 631 */
500 /* 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
501 * be updated. 642 * be updated.
502 */ 643 */
503 if (!op->env) 644 if (!op->env)
504 { 645 {
505 pl->contr->socket->floorbox_update (); 646 pl->contr->ns->floorbox_update ();
506 return; 647 return;
507 } 648 }
508 } 649 }
509 650
510 packet sl; 651 packet sl;
511 652
512 sl.printf ("item%d ", pl->contr->socket->itemcmd); 653 sl.printf ("item%d ", pl->contr->ns->itemcmd);
513 654
514 if (op->head) 655 if (op->head)
515 op = op->head; 656 op = op->head;
516 657
517 sl << uint32 (op->env ? op->env->count : 0); 658 sl << uint32 (op->env ? op->env->count : 0);
518 659
519 add_object_to_socklist (*pl->contr->socket, sl, op); 660 add_object_to_socklist (*pl->contr->ns, sl, op);
520 661
521 pl->contr->socket->send_packet (sl); 662 pl->contr->ns->send_packet (sl);
522 SET_FLAG (op, FLAG_CLIENT_SENT); 663 SET_FLAG (op, FLAG_CLIENT_SENT);
523} 664}
524 665
525/** 666/**
526 * Tells the client to delete an item. Uses the item 667 * Tells the client to delete an item. Uses the item
532{ 673{
533 packet sl ("delitem"); 674 packet sl ("delitem");
534 675
535 sl << uint32 (tag); 676 sl << uint32 (tag);
536 677
537 pl->socket->send_packet (sl); 678 pl->ns->send_packet (sl);
538} 679}
539 680
540 681
541/******************************************************************************* 682/*******************************************************************************
542 * 683 *
563 else if (op->type == CONTAINER && pl->container == op) 704 else if (op->type == CONTAINER && pl->container == op)
564 for (tmp = op->inv; tmp; tmp = tmp->below) 705 for (tmp = op->inv; tmp; tmp = tmp->below)
565 if (tmp->count == count) 706 if (tmp->count == count)
566 return tmp; 707 return tmp;
567 708
568 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)
569 if (op->head && op->head->count == count) 710 if (op->head && op->head->count == count)
570 return op; 711 return op;
571 else if (op->count == count) 712 else if (op->count == count)
572 return op; 713 return op;
573 else if (op->type == CONTAINER && pl->container == op) 714 else if (op->type == CONTAINER && pl->container == op)
613 return; 754 return;
614 755
615 /* If the high bit is set, player applied a pseudo object. */ 756 /* If the high bit is set, player applied a pseudo object. */
616 if (tag & 0x80000000) 757 if (tag & 0x80000000)
617 { 758 {
618 pl->socket->look_position = tag & 0x7fffffff; 759 pl->ns->look_position = tag & 0x7fffffff;
619 pl->socket->floorbox_update (); 760 pl->ns->floorbox_update ();
620 return; 761 return;
621 } 762 }
622 763
623 object *op = esrv_get_ob_from_count (pl->ob, tag); 764 object *op = esrv_get_ob_from_count (pl->ob, tag);
624 765
692 833
693 m = get_map_from_coord (op->map, &x, &y); 834 m = get_map_from_coord (op->map, &x, &y);
694 if (!m) 835 if (!m)
695 return; 836 return;
696 837
697 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);
698 839
699 for (; tmp != NULL; tmp = tmp->below) 840 for (; tmp != NULL; tmp = tmp->below)
700 { 841 {
701 if (tmp->invisible && !QUERY_FLAG (op, FLAG_WIZ)) 842 if (tmp->invisible && !QUERY_FLAG (op, FLAG_WIZ))
702 continue; 843 continue;
749 { 890 {
750 return; 891 return;
751 } 892 }
752 dy = atoi (cp); 893 dy = atoi (cp);
753 894
754 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)
755 return; 896 return;
756 897
757 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])
758 return; 899 return;
759 900
760 look_at (pl->ob, dx, dy); 901 look_at (pl->ob, dx, dy);
761} 902}
762 903

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines